编译链接学习笔记2


[a@b ~]$ cd compile_test/
[a@b compile_test]$ ls
foo1.c  foo1.o  foo2.c  foo2.o  foo2.s  hello  hello.c  hello.o  libfoo.so
[a@b compile_test]$ readelf -s hello.o

Symbol table '.symtab' contains 10 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS hello.c
     2: 00000000     0 SECTION LOCAL  DEFAULT    1 
     3: 00000000     0 SECTION LOCAL  DEFAULT    3 
     4: 00000000     0 SECTION LOCAL  DEFAULT    4 
     5: 00000000     0 SECTION LOCAL  DEFAULT    6 
     6: 00000000     0 SECTION LOCAL  DEFAULT    5 
     7: 00000000    38 FUNC    GLOBAL DEFAULT    1 main
     8: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND foo2
     9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND foo2_func
[a@b compile_test]$ 
[a@b compile_test]$ readelf -s hello

Symbol table '.dynsym' contains 12 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     2: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.0 (2)
     4: 00000000     0 FUNC    GLOBAL DEFAULT  UND foo2_func
     5: 08049754     0 NOTYPE  GLOBAL DEFAULT  ABS _end
     6: 08049748     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
     7: 08049748     4 OBJECT  GLOBAL DEFAULT   25 foo2
     8: 0804859c     4 OBJECT  GLOBAL DEFAULT   15 _IO_stdin_used
     9: 08049748     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    10: 08048384     0 FUNC    GLOBAL DEFAULT   11 _init
    11: 0804857c     0 FUNC    GLOBAL DEFAULT   14 _fini

Symbol table '.symtab' contains 66 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 08048134     0 SECTION LOCAL  DEFAULT    1 
     2: 08048148     0 SECTION LOCAL  DEFAULT    2 
     3: 08048168     0 SECTION LOCAL  DEFAULT    3 
     4: 0804818c     0 SECTION LOCAL  DEFAULT    4 
     5: 080481cc     0 SECTION LOCAL  DEFAULT    5 
     6: 0804828c     0 SECTION LOCAL  DEFAULT    6 
     7: 08048322     0 SECTION LOCAL  DEFAULT    7 
     8: 0804833c     0 SECTION LOCAL  DEFAULT    8 
     9: 0804835c     0 SECTION LOCAL  DEFAULT    9 
    10: 0804836c     0 SECTION LOCAL  DEFAULT   10 
    11: 08048384     0 SECTION LOCAL  DEFAULT   11 
    12: 080483b4     0 SECTION LOCAL  DEFAULT   12 
    13: 08048400     0 SECTION LOCAL  DEFAULT   13 
    14: 0804857c     0 SECTION LOCAL  DEFAULT   14 
    15: 08048598     0 SECTION LOCAL  DEFAULT   15 
    16: 080485a4     0 SECTION LOCAL  DEFAULT   16 
    17: 080485c8     0 SECTION LOCAL  DEFAULT   17 
    18: 08049644     0 SECTION LOCAL  DEFAULT   18 
    19: 0804964c     0 SECTION LOCAL  DEFAULT   19 
    20: 08049654     0 SECTION LOCAL  DEFAULT   20 
    21: 08049658     0 SECTION LOCAL  DEFAULT   21 
    22: 08049728     0 SECTION LOCAL  DEFAULT   22 
    23: 0804972c     0 SECTION LOCAL  DEFAULT   23 
    24: 08049744     0 SECTION LOCAL  DEFAULT   24 
    25: 08049748     0 SECTION LOCAL  DEFAULT   25 
    26: 00000000     0 SECTION LOCAL  DEFAULT   26 
    27: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    28: 08049644     0 OBJECT  LOCAL  DEFAULT   18 __CTOR_LIST__
    29: 0804964c     0 OBJECT  LOCAL  DEFAULT   19 __DTOR_LIST__
    30: 08049654     0 OBJECT  LOCAL  DEFAULT   20 __JCR_LIST__
    31: 08048430     0 FUNC    LOCAL  DEFAULT   13 __do_global_dtors_aux
    32: 0804974c     1 OBJECT  LOCAL  DEFAULT   25 completed.5972
    33: 08049750     4 OBJECT  LOCAL  DEFAULT   25 dtor_idx.5974
    34: 08048490     0 FUNC    LOCAL  DEFAULT   13 frame_dummy
    35: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    36: 08049648     0 OBJECT  LOCAL  DEFAULT   18 __CTOR_END__
    37: 08048640     0 OBJECT  LOCAL  DEFAULT   17 __FRAME_END__
    38: 08049654     0 OBJECT  LOCAL  DEFAULT   20 __JCR_END__
    39: 08048550     0 FUNC    LOCAL  DEFAULT   13 __do_global_ctors_aux
    40: 00000000     0 FILE    LOCAL  DEFAULT  ABS hello.c
    41: 0804972c     0 OBJECT  LOCAL  DEFAULT   23 _GLOBAL_OFFSET_TABLE_
    42: 08049644     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_end
    43: 08049644     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_start
    44: 08049658     0 OBJECT  LOCAL  DEFAULT   21 _DYNAMIC
    45: 08049744     0 NOTYPE  WEAK   DEFAULT   24 data_start
    46: 080484e0     5 FUNC    GLOBAL DEFAULT   13 __libc_csu_fini
    47: 08048400     0 FUNC    GLOBAL DEFAULT   13 _start
    48: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    49: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    50: 08048598     4 OBJECT  GLOBAL DEFAULT   15 _fp_hw
    51: 0804857c     0 FUNC    GLOBAL DEFAULT   14 _fini
    52: 08049748     4 OBJECT  GLOBAL DEFAULT   25 foo2
    53: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
    54: 0804859c     4 OBJECT  GLOBAL DEFAULT   15 _IO_stdin_used
    55: 08049744     0 NOTYPE  GLOBAL DEFAULT   24 __data_start
    56: 080485a0     0 OBJECT  GLOBAL HIDDEN    15 __dso_handle
    57: 08049650     0 OBJECT  GLOBAL HIDDEN    19 __DTOR_END__
    58: 080484f0    90 FUNC    GLOBAL DEFAULT   13 __libc_csu_init
    59: 08049748     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    60: 00000000     0 FUNC    GLOBAL DEFAULT  UND foo2_func
    61: 08049754     0 NOTYPE  GLOBAL DEFAULT  ABS _end
    62: 08049748     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    63: 0804854a     0 FUNC    GLOBAL HIDDEN    13 __i686.get_pc_thunk.bx
    64: 080484b4    38 FUNC    GLOBAL DEFAULT   13 main
    65: 08048384     0 FUNC    GLOBAL DEFAULT   11 _init
[a@b compile_test]$ 
[a@b compile_test]$ 
[a@b compile_test]$ readelf -S hello
There are 30 section headers, starting at offset 0x89c:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1
  [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4
  [ 3] .note.gnu.build-i NOTE            08048168 000168 000024 00   A  0   0  4
  [ 4] .gnu.hash         GNU_HASH        0804818c 00018c 000040 04   A  5   0  4
  [ 5] .dynsym           DYNSYM          080481cc 0001cc 0000c0 10   A  6   1  4
  [ 6] .dynstr           STRTAB          0804828c 00028c 000096 00   A  0   0  1
  [ 7] .gnu.version      VERSYM          08048322 000322 000018 02   A  5   0  2
  [ 8] .gnu.version_r    VERNEED         0804833c 00033c 000020 00   A  6   1  4
  [ 9] .rel.dyn          REL             0804835c 00035c 000010 08   A  5   0  4
  [10] .rel.plt          REL             0804836c 00036c 000018 08   A  5  12  4
  [11] .init             PROGBITS        08048384 000384 000030 00  AX  0   0  4
  [12] .plt              PROGBITS        080483b4 0003b4 000040 04  AX  0   0  4
  [13] .text             PROGBITS        08048400 000400 00017c 00  AX  0   0 16
  [14] .fini             PROGBITS        0804857c 00057c 00001c 00  AX  0   0  4
  [15] .rodata           PROGBITS        08048598 000598 00000c 00   A  0   0  4
  [16] .eh_frame_hdr     PROGBITS        080485a4 0005a4 000024 00   A  0   0  4
  [17] .eh_frame         PROGBITS        080485c8 0005c8 00007c 00   A  0   0  4
  [18] .ctors            PROGBITS        08049644 000644 000008 00  WA  0   0  4
  [19] .dtors            PROGBITS        0804964c 00064c 000008 00  WA  0   0  4
  [20] .jcr              PROGBITS        08049654 000654 000004 00  WA  0   0  4
  [21] .dynamic          DYNAMIC         08049658 000658 0000d0 08  WA  6   0  4
  [22] .got              PROGBITS        08049728 000728 000004 04  WA  0   0  4
  [23] .got.plt          PROGBITS        0804972c 00072c 000018 04  WA  0   0  4
  [24] .data             PROGBITS        08049744 000744 000004 00  WA  0   0  4
  [25] .bss              NOBITS          08049748 000748 00000c 00  WA  0   0  4
  [26] .comment          PROGBITS        00000000 000748 000058 01  MS  0   0  1
  [27] .shstrtab         STRTAB          00000000 0007a0 0000fc 00      0   0  1
  [28] .symtab           SYMTAB          00000000 000d4c 000420 10     29  45  4
  [29] .strtab           STRTAB          00000000 00116c 0001fa 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[a@b compile_test]$ 
[a@b compile_test]$ 
[a@b compile_test]$ readelf -r hello

Relocation section '.rel.dyn' at offset 0x35c contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049728  00000106 R_386_GLOB_DAT    00000000   __gmon_start__
08049748  00000705 R_386_COPY        08049748   foo2

Relocation section '.rel.plt' at offset 0x36c contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
08049738  00000107 R_386_JUMP_SLOT   00000000   __gmon_start__
0804973c  00000307 R_386_JUMP_SLOT   00000000   __libc_start_main
08049740  00000407 R_386_JUMP_SLOT   00000000   foo2_func
[a@b compile_test]$ 
[a@b compile_test]$ nm hello | grep foo
08049748 B foo2
         U foo2_func
[a@b compile_test]$ readelf -S libfoo.so 
There are 27 section headers, starting at offset 0x738:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .note.gnu.build-i NOTE            000000d4 0000d4 000024 00   A  0   0  4
  [ 2] .gnu.hash         GNU_HASH        000000f8 0000f8 000048 04   A  3   0  4
  [ 3] .dynsym           DYNSYM          00000140 000140 0000d0 10   A  4   1  4
  [ 4] .dynstr           STRTAB          00000210 000210 00008b 00   A  0   0  1
  [ 5] .gnu.version      VERSYM          0000029c 00029c 00001a 02   A  3   0  2
  [ 6] .gnu.version_r    VERNEED         000002b8 0002b8 000020 00   A  4   1  4
  [ 7] .rel.dyn          REL             000002d8 0002d8 000030 08   A  3   0  4
  [ 8] .rel.plt          REL             00000308 000308 000010 08   A  3  10  4
  [ 9] .init             PROGBITS        00000318 000318 000030 00  AX  0   0  4
  [10] .plt              PROGBITS        00000348 000348 000030 04  AX  0   0  4
  [11] .text             PROGBITS        00000380 000380 000158 00  AX  0   0 16
  [12] .fini             PROGBITS        000004d8 0004d8 00001c 00  AX  0   0  4
  [13] .eh_frame         PROGBITS        000004f4 0004f4 000004 00   A  0   0  4
  [14] .ctors            PROGBITS        000014f8 0004f8 000008 00  WA  0   0  4
  [15] .dtors            PROGBITS        00001500 000500 000008 00  WA  0   0  4
  [16] .jcr              PROGBITS        00001508 000508 000004 00  WA  0   0  4
  [17] .data.rel.ro      PROGBITS        0000150c 00050c 000004 00  WA  0   0  4
  [18] .dynamic          DYNAMIC         00001510 000510 0000c0 08  WA  4   0  4
  [19] .got              PROGBITS        000015d0 0005d0 000014 04  WA  0   0  4
  [20] .got.plt          PROGBITS        000015e4 0005e4 000014 04  WA  0   0  4
  [21] .data             PROGBITS        000015f8 0005f8 000008 00  WA  0   0  4
  [22] .bss              NOBITS          00001600 000600 000008 00  WA  0   0  4
  [23] .comment          PROGBITS        00000000 000600 000058 01  MS  0   0  1
  [24] .shstrtab         STRTAB          00000000 000658 0000dd 00      0   0  1
  [25] .symtab           SYMTAB          00000000 000b70 000390 10     26  45  4
  [26] .strtab           STRTAB          00000000 000f00 0001a7 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[a@b compile_test]$ readelf -S /lib/
Display all 234 possibilities? (y or n)
[a@b compile_test]$ readelf -S /lib/ld-
ld-2.12.so     ld-linux.so.2  ld-lsb.so.3    
[a@b compile_test]$ readelf -S /lib/ld-2.12.so 
There are 30 section headers, starting at offset 0x227d8:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .note.gnu.build-i NOTE            004e7114 000114 000024 00   A  0   0  4
  [ 2] .hash             HASH            004e7138 000138 0000bc 04   A  4   0  4
  [ 3] .gnu.hash         GNU_HASH        004e71f4 0001f4 0000e0 04   A  4   0  4
  [ 4] .dynsym           DYNSYM          004e72d4 0002d4 0001c0 10   A  5   1  4
  [ 5] .dynstr           STRTAB          004e7494 000494 00017e 00   A  0   0  1
  [ 6] .gnu.version      VERSYM          004e7612 000612 000038 02   A  4   0  2
  [ 7] .gnu.version_d    VERDEF          004e764c 00064c 0000c8 00   A  5   6  4
  [ 8] .rel.dyn          REL             004e7714 000714 000078 08   A  4   0  4
  [ 9] .rel.plt          REL             004e778c 00078c 000030 08   A  4  10  4
  [10] .plt              PROGBITS        004e77bc 0007bc 000070 04  AX  0   0  4
  [11] .text             PROGBITS        004e7830 000830 017b2f 00  AX  0   0 16
  [12] __libc_freeres_fn PROGBITS        004ff360 018360 00055a 00  AX  0   0 16
  [13] .rodata           PROGBITS        004ff8c0 0188c0 0029e0 00   A  0   0 32
  [14] .stapsdt.base     PROGBITS        005022a0 01b2a0 000001 00   A  0   0  1
  [15] .eh_frame_hdr     PROGBITS        005022a4 01b2a4 0005ec 00   A  0   0  4
  [16] .eh_frame         PROGBITS        00502890 01b890 002110 00   A  0   0  4
  [17] .data.rel.ro      PROGBITS        00505c60 01dc60 00029c 00  WA  0   0 32
  [18] .dynamic          DYNAMIC         00505efc 01defc 0000c8 08  WA  5   0  4
  [19] .got              PROGBITS        00505fc4 01dfc4 000030 04  WA  0   0  4
  [20] .data             PROGBITS        00506000 01e000 000834 00  WA  0   0 32
  [21] __libc_subfreeres PROGBITS        00506834 01e834 000004 00  WA  0   0  4
  [22] .bss              NOBITS          00506838 01e838 0000c0 00  WA  0   0  8
  [23] .note.stapsdt     NOTE            00000000 01e838 0002b4 00      0   0  4
  [24] .comment          PROGBITS        00000000 01eaec 00002c 01  MS  0   0  1
  [25] .symtab           SYMTAB          00000000 01eb18 001ec0 10     26 465  4
  [26] .strtab           STRTAB          00000000 0209d8 001744 00      0   0  1
  [27] .gnu_debuglink    PROGBITS        00000000 02211c 000018 00      0   0  4
  [28] .gnu.prelink_undo PROGBITS        00000000 022134 000574 01      0   0  4
  [29] .shstrtab         STRTAB          00000000 0226a8 00012c 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[a@b compile_test]$ 
[a@b compile_test]$ readelf -r libfoo.so 

Relocation section '.rel.dyn' at offset 0x2d8 contains 6 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000150c  00000008 R_386_RELATIVE   
000015d0  00000106 R_386_GLOB_DAT    00000000   __gmon_start__
000015d4  00000206 R_386_GLOB_DAT    00000000   _Jv_RegisterClasses
000015d8  00000906 R_386_GLOB_DAT    000015fc   foo2
000015dc  00000306 R_386_GLOB_DAT    00000000   __cxa_finalize
000015e0  00000706 R_386_GLOB_DAT    000015f8   foo1

Relocation section '.rel.plt' at offset 0x308 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
000015f0  00000107 R_386_JUMP_SLOT   00000000   __gmon_start__
000015f4  00000307 R_386_JUMP_SLOT   00000000   __cxa_finalize
 

如果编译可重定位内核,那么内核打开了如下选项
--emit-relocs, 含义如下:将重定位部分和内容保留在完全链接的可执行文件中。后链接分析和优化工具可能需要此信息才能执行可执行文件的正确修改。这导致更大的可执行文件

ld - GNU链接器(Linux手册页)中
https://blog.csdn.net/wogezheerne/article/details/86582791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值