为什么需要动态链接

例子

*lib.c*

#include <stdio.h>

void foobar(int i)
{
	printf("Printing form lib.so %d\n", i);
}

*program.c*

#include "lib.h"

int main()
{
	foobar(1);
	return 0;
}

将 lib.c 编译成共享对象(.so 文件)

gcc -fPIC -shared -o lib.so lib.c

-shared 表示产出共享对象,-fPIC 表示地址无关代码(PIC, Position-independent Code)

编译 program

gcc -o program program.c ./lib.so

hexdump program

当程序被装载的时候,系统的动态链接器会将程序所需要的所有动态链接库装载到进程的地址空间,并且将程序中所有未决议的符号绑定到相应的动态链接库中,并进行重定位工作。

readelf -S program

There are 31 section headers, starting at offset 0x3688:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .interp           PROGBITS        0000000000000318 000318 00001c 00   A  0   0  1
  [ 2] .note.gnu.property NOTE            0000000000000338 000338 000030 00   A  0   0  8
  [ 3] .note.gnu.build-id NOTE            0000000000000368 000368 000024 00   A  0   0  4
  [ 4] .note.ABI-tag     NOTE            000000000000038c 00038c 000020 00   A  0   0  4
  [ 5] .gnu.hash         GNU_HASH        00000000000003b0 0003b0 000024 00   A  6   0  8
  [ 6] .dynsym           DYNSYM          00000000000003d8 0003d8 0000a8 18   A  7   1  8
  [ 7] .dynstr           STRTAB          0000000000000480 000480 000098 00   A  0   0  1
  [ 8] .gnu.version      VERSYM          0000000000000518 000518 00000e 02   A  6   0  2
  [ 9] .gnu.version_r    VERNEED         0000000000000528 000528 000030 00   A  7   1  8
  [10] .rela.dyn         RELA            0000000000000558 000558 0000c0 18   A  6   0  8
  [11] .rela.plt         RELA            0000000000000618 000618 000018 18  AI  6  24  8
  [12] .init             PROGBITS        0000000000001000 001000 00001b 00  AX  0   0  4
  [13] .plt              PROGBITS        0000000000001020 001020 000020 10  AX  0   0 16
  [14] .plt.got          PROGBITS        0000000000001040 001040 000010 10  AX  0   0 16
  [15] .plt.sec          PROGBITS        0000000000001050 001050 000010 10  AX  0   0 16
  [16] .text             PROGBITS        0000000000001060 001060 000102 00  AX  0   0 16
  [17] .fini             PROGBITS        0000000000001164 001164 00000d 00  AX  0   0  4
  [18] .rodata           PROGBITS        0000000000002000 002000 000004 04  AM  0   0  4
  [19] .eh_frame_hdr     PROGBITS        0000000000002004 002004 000034 00   A  0   0  4
  [20] .eh_frame         PROGBITS        0000000000002038 002038 0000ac 00   A  0   0  8
  [21] .init_array       INIT_ARRAY      0000000000003da8 002da8 000008 08  WA  0   0  8
  [22] .fini_array       FINI_ARRAY      0000000000003db0 002db0 000008 08  WA  0   0  8
  [23] .dynamic          DYNAMIC         0000000000003db8 002db8 000200 10  WA  7   0  8
  [24] .got              PROGBITS        0000000000003fb8 002fb8 000048 08  WA  0   0  8
  [25] .data             PROGBITS        0000000000004000 003000 000010 00  WA  0   0  8
  [26] .bss              NOBITS          0000000000004010 003010 000008 00  WA  0   0  1
  [27] .comment          PROGBITS        0000000000000000 003010 000026 01  MS  0   0  1
  [28] .symtab           SYMTAB          0000000000000000 003038 000360 18     29  18  8
  [29] .strtab           STRTAB          0000000000000000 003398 0001d4 00      0   0  1
  [30] .shstrtab         STRTAB          0000000000000000 00356c 00011a 00      0   0  1

延迟绑定(PLT)

聊聊Linux动态链接中的PLT和GOT(2)--延迟重定位_海枫的博客-CSDN博客

动态链接相关结构

.interp 段