加载器、链接器、动态链接器概念

Bruce / 2023-07-19 / 原文

动态链接器:

共享库(shared library)是致力于解决静态库缺陷的一个现代创新产物。共享库是一个目标模块,在运行或加载时,可以加载到任意的内存地址,并和一个在内存中的程序链接起来。这个过程称为动态链接(dynamic linking),是由一个叫做动态链接器(dynamic linker)的程序来执行的。

链接器:

将多个可重定位目标文件生成可执行目标文件的过程称为链接,完成此功能的部件称为链接器(ld)

加载器

加载器(loader)将可执行目标文件中的代码和数据从磁盘复制到内存中,然后通过跳转到程序的第一条指令或入口点来运行该程序。这个将程序复制到内存并运行的过程叫做加载。

修改一个二进制文件的动态链接器方法:

patchelf --set-interpreter /path/to/new/dynamic/linker your_program

readelf -h a.out输出信息:

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x41cc40
  Start of program headers:          64 (bytes into file)
  Start of section headers:          82900248 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  # program header的大小是56字节
  Size of program headers:           56 (bytes)
  # 9个program header
  Number of program headers:         9
  # section header的大小是64字节
  Size of section headers:           64 (bytes)
  # 42个section header
  Number of section headers:         42
  Section header string table index: 39