LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: swordhui

关于gcc, binutils, glibc

[复制链接]
 楼主| 发表于 2010-4-18 19:19:47 | 显示全部楼层
疑问11, /usr/lib/libgcc_s.so 由gcc提供, 动态链接.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-4-18 19:21:43 | 显示全部楼层
疑问13, crtend.o由gcc提供, 代码如下:
  1. gcc/i686-pc-linux-gnu/4.4.3/crtend.o:     file format elf32-i386
  2. Disassembly of section .text:
  3. 00000000 <__do_global_ctors_aux>:
  4.    0:        55                           push   %ebp
  5.    1:        89 e5                        mov    %esp,%ebp
  6.    3:        53                           push   %ebx
  7.    4:        83 ec 04                     sub    $0x4,%esp
  8.    7:        a1 fc ff ff ff               mov    0xfffffffc,%eax
  9.    c:        83 f8 ff                     cmp    $0xffffffff,%eax
  10.    f:        74 13                        je     24 <__do_global_ctors_aux+0x24>
  11.   11:        bb fc ff ff ff               mov    $0xfffffffc,%ebx
  12.   16:        66 90                        xchg   %ax,%ax
  13.   18:        83 eb 04                     sub    $0x4,%ebx
  14.   1b:        ff d0                        call   *%eax
  15.   1d:        8b 03                        mov    (%ebx),%eax
  16.   1f:        83 f8 ff                     cmp    $0xffffffff,%eax
  17.   22:        75 f4                        jne    18 <__do_global_ctors_aux+0x18>
  18.   24:        83 c4 04                     add    $0x4,%esp
  19.   27:        5b                           pop    %ebx
  20.   28:        5d                           pop    %ebp
  21.   29:        c3                           ret   
  22. Disassembly of section .init:
  23. 00000000 <.init>:
  24.    0:        e8 fc ff ff ff               call   1 <.init+0x1>
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-4-18 19:23:59 | 显示全部楼层
疑问14, crtn.o由glibc提供.
  1. root:/tmp/usr/lib# objdump -S /usr/lib/crtn.o
  2. /usr/lib/crtn.o:     file format elf32-i386
  3. Disassembly of section .init:
  4. 00000000 <.init>:
  5.    0:        58                           pop    %eax
  6.    1:        5b                           pop    %ebx
  7.    2:        c9                           leave  
  8.    3:        c3                           ret   
  9. Disassembly of section .fini:
  10. 00000000 <.fini>:
  11.    0:        59                           pop    %ecx
  12.    1:        5b                           pop    %ebx
  13.    2:        c9                           leave  
  14.    3:        c3                           ret   
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-4-18 19:45:23 | 显示全部楼层
接着再看看最终链接完成的 ./test
  1. OPSvr root #readelf -a ./test
  2. ELF Header:
  3.   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  4.   Class:                             ELF32
  5.   Data:                              2's complement, little endian
  6.   Version:                           1 (current)
  7.   OS/ABI:                            UNIX - System V
  8.   ABI Version:                       0
  9.   Type:                              EXEC (Executable file)
  10.   Machine:                           Intel 80386
  11.   Version:                           0x1
  12.   Entry point address:               0x80482f0
  13.   Start of program headers:          52 (bytes into file)
  14.   Start of section headers:          1892 (bytes into file)
  15.   Flags:                             0x0
  16.   Size of this header:               52 (bytes)
  17.   Size of program headers:           32 (bytes)
  18.   Number of program headers:         8
  19.   Size of section headers:           40 (bytes)
  20.   Number of section headers:         29
  21.   Section header string table index: 26
  22. Section Headers:
  23.   [Nr] Name              Type            Addr     Off    Size   ES *** Lk Inf Al
  24.   [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  25.   [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1
  26.   [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4
  27.   [ 3] .hash             HASH            08048168 000168 000028 04   A  4   0  4
  28.   [ 4] .dynsym           DYNSYM          08048190 000190 000050 10   A  5   1  4
  29.   [ 5] .dynstr           STRTAB          080481e0 0001e0 00004a 00   A  0   0  1
  30.   [ 6] .gnu.version      VERSYM          0804822a 00022a 00000a 02   A  4   0  2
  31.   [ 7] .gnu.version_r    VERNEED         08048234 000234 000020 00   A  5   1  4
  32.   [ 8] .rel.dyn          REL             08048254 000254 000008 08   A  4   0  4
  33.   [ 9] .rel.plt          REL             0804825c 00025c 000018 08   A  4  11  4
  34.   [10] .init             PROGBITS        08048274 000274 000030 00  AX  0   0  4
  35.   [11] .plt              PROGBITS        080482a4 0002a4 000040 04  AX  0   0  4
  36.   [12] .text             PROGBITS        080482f0 0002f0 00016c 00  AX  0   0 16
  37.   [13] .fini             PROGBITS        0804845c 00045c 00001c 00  AX  0   0  4
  38.   [14] .rodata           PROGBITS        08048478 000478 000014 00   A  0   0  4
  39.   [15] .eh_frame_hdr     PROGBITS        0804848c 00048c 00001c 00   A  0   0  4
  40.   [16] .eh_frame         PROGBITS        080484a8 0004a8 000058 00   A  0   0  4
  41.   [17] .ctors            PROGBITS        08049500 000500 000008 00  WA  0   0  4
  42.   [18] .dtors            PROGBITS        08049508 000508 000008 00  WA  0   0  4
  43.   [19] .jcr              PROGBITS        08049510 000510 000004 00  WA  0   0  4
  44.   [20] .dynamic          DYNAMIC         08049514 000514 0000c8 08  WA  5   0  4
  45.   [21] .got              PROGBITS        080495dc 0005dc 000004 04  WA  0   0  4
  46.   [22] .got.plt          PROGBITS        080495e0 0005e0 000018 04  WA  0   0  4
  47.   [23] .data             PROGBITS        080495f8 0005f8 000008 00  WA  0   0  4
  48.   [24] .bss              NOBITS          08049600 000600 000008 00  WA  0   0  4
  49.   [25] .comment          PROGBITS        00000000 000600 00007e 00      0   0  1
  50.   [26] .shstrtab         STRTAB          00000000 00067e 0000e5 00      0   0  1
  51.   [27] .symtab           SYMTAB          00000000 000bec 0003e0 10     28  42  4
  52.   [28] .strtab           STRTAB          00000000 000fcc 0001ef 00      0   0  1
  53. Key to Flags:
  54.   W (write), A (alloc), X (execute), M (merge), S (strings)
  55.   I (info), L (link order), G (group), x (unknown)
  56.   O (extra OS processing required) o (OS specific), p (processor specific)
  57. There are no section groups in this file.
  58. Program Headers:
  59.   Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  *** Align
  60.   PHDR           0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4
  61.   INTERP         0x000134 0x08048134 0x08048134 0x00013 0x00013 R   0x1
  62.       [Requesting program interpreter: /lib/ld-linux.so.2]
  63.   LOAD           0x000000 0x08048000 0x08048000 0x00500 0x00500 R E 0x1000
  64.   LOAD           0x000500 0x08049500 0x08049500 0x00100 0x00108 RW  0x1000
  65.   DYNAMIC        0x000514 0x08049514 0x08049514 0x000c8 0x000c8 RW  0x4
  66.   NOTE           0x000148 0x08048148 0x08048148 0x00020 0x00020 R   0x4
  67.   GNU_EH_FRAME   0x00048c 0x0804848c 0x0804848c 0x0001c 0x0001c R   0x4
  68.   GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4
  69. Section to Segment mapping:
  70.   Segment Sections...
  71.    00     
  72.    01     .interp
  73.    02     .interp .note.ABI-tag .hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
  74.    03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss
  75.    04     .dynamic
  76.    05     .note.ABI-tag
  77.    06     .eh_frame_hdr
  78.    07     
  79. Dynamic section at offset 0x514 contains 20 entries:
  80.   Tag        Type                         Name/Value
  81. 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
  82. 0x0000000c (INIT)                       0x8048274
  83. 0x0000000d (FINI)                       0x804845c
  84. 0x00000004 (HASH)                       0x8048168
  85. 0x00000005 (STRTAB)                     0x80481e0
  86. 0x00000006 (SYMTAB)                     0x8048190
  87. 0x0000000a (STRSZ)                      74 (bytes)
  88. 0x0000000b (SYMENT)                     16 (bytes)
  89. 0x00000015 (DEBUG)                      0x0
  90. 0x00000003 (PLTGOT)                     0x80495e0
  91. 0x00000002 (PLTRELSZ)                   24 (bytes)
  92. 0x00000014 (PLTREL)                     REL
  93. 0x00000017 (JMPREL)                     0x804825c
  94. 0x00000011 (REL)                        0x8048254
  95. 0x00000012 (RELSZ)                      8 (bytes)
  96. 0x00000013 (RELENT)                     8 (bytes)
  97. 0x6ffffffe (VERNEED)                    0x8048234
  98. 0x6fffffff (VERNEEDNUM)                 1
  99. 0x6ffffff0 (VERSYM)                     0x804822a
  100. 0x00000000 (NULL)                       0x0
  101. Relocation section '.rel.dyn' at offset 0x254 contains 1 entries:
  102. Offset     Info    Type            Sym.Value  Sym. Name
  103. 080495dc  00000106 R_386_GLOB_DAT    00000000   __gmon_start__
  104. Relocation section '.rel.plt' at offset 0x25c contains 3 entries:
  105. Offset     Info    Type            Sym.Value  Sym. Name
  106. 080495ec  00000107 R_386_JUMP_SLOT   00000000   __gmon_start__
  107. 080495f0  00000207 R_386_JUMP_SLOT   00000000   __libc_start_main
  108. 080495f4  00000407 R_386_JUMP_SLOT   00000000   puts
  109. There are no unwind sections in this file.
  110. Symbol table '.dynsym' contains 5 entries:
  111.    Num:    Value  Size Type    Bind   Vis      Ndx Name
  112.      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
  113.      1: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
  114.      2: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.0 (2)
  115.      3: 0804847c     4 OBJECT  GLOBAL DEFAULT   14 _IO_stdin_used
  116.      4: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.0 (2)
  117. Symbol table '.symtab' contains 62 entries:
  118.    Num:    Value  Size Type    Bind   Vis      Ndx Name
  119.      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
  120.      1: 08048134     0 SECTION LOCAL  DEFAULT    1
  121.      2: 08048148     0 SECTION LOCAL  DEFAULT    2
  122.      3: 08048168     0 SECTION LOCAL  DEFAULT    3
  123.      4: 08048190     0 SECTION LOCAL  DEFAULT    4
  124.      5: 080481e0     0 SECTION LOCAL  DEFAULT    5
  125.      6: 0804822a     0 SECTION LOCAL  DEFAULT    6
  126.      7: 08048234     0 SECTION LOCAL  DEFAULT    7
  127.      8: 08048254     0 SECTION LOCAL  DEFAULT    8
  128.      9: 0804825c     0 SECTION LOCAL  DEFAULT    9
  129.     10: 08048274     0 SECTION LOCAL  DEFAULT   10
  130.     11: 080482a4     0 SECTION LOCAL  DEFAULT   11
  131.     12: 080482f0     0 SECTION LOCAL  DEFAULT   12
  132.     13: 0804845c     0 SECTION LOCAL  DEFAULT   13
  133.     14: 08048478     0 SECTION LOCAL  DEFAULT   14
  134.     15: 0804848c     0 SECTION LOCAL  DEFAULT   15
  135.     16: 080484a8     0 SECTION LOCAL  DEFAULT   16
  136.     17: 08049500     0 SECTION LOCAL  DEFAULT   17
  137.     18: 08049508     0 SECTION LOCAL  DEFAULT   18
  138.     19: 08049510     0 SECTION LOCAL  DEFAULT   19
  139.     20: 08049514     0 SECTION LOCAL  DEFAULT   20
  140.     21: 080495dc     0 SECTION LOCAL  DEFAULT   21
  141.     22: 080495e0     0 SECTION LOCAL  DEFAULT   22
  142.     23: 080495f8     0 SECTION LOCAL  DEFAULT   23
  143.     24: 08049600     0 SECTION LOCAL  DEFAULT   24
  144.     25: 00000000     0 SECTION LOCAL  DEFAULT   25
  145.     26: 08049500     0 OBJECT  LOCAL  DEFAULT   17 __CTOR_LIST__
  146.     27: 08049508     0 OBJECT  LOCAL  DEFAULT   18 __DTOR_LIST__
  147.     28: 08049510     0 OBJECT  LOCAL  DEFAULT   19 __JCR_LIST__
  148.     29: 08048320     0 FUNC    LOCAL  DEFAULT   12 __do_global_dtors_aux
  149.     30: 08049600     1 OBJECT  LOCAL  DEFAULT   24 completed.5915
  150.     31: 08049604     4 OBJECT  LOCAL  DEFAULT   24 dtor_idx.5917
  151.     32: 08048380     0 FUNC    LOCAL  DEFAULT   12 frame_dummy
  152.     33: 08049504     0 OBJECT  LOCAL  DEFAULT   17 __CTOR_END__
  153.     34: 080484fc     0 OBJECT  LOCAL  DEFAULT   16 __FRAME_END__
  154.     35: 08049510     0 OBJECT  LOCAL  DEFAULT   19 __JCR_END__
  155.     36: 08048430     0 FUNC    LOCAL  DEFAULT   12 __do_global_ctors_aux
  156.     37: 00000000     0 FILE    LOCAL  DEFAULT  ABS test.c
  157.     38: 080495e0     0 OBJECT  LOCAL  HIDDEN   22 _GLOBAL_OFFSET_TABLE_
  158.     39: 08049500     0 NOTYPE  LOCAL  HIDDEN   17 __init_array_end
  159.     40: 08049500     0 NOTYPE  LOCAL  HIDDEN   17 __init_array_start
  160.     41: 08049514     0 OBJECT  LOCAL  HIDDEN   20 _DYNAMIC
  161.     42: 080495f8     0 NOTYPE  WEAK   DEFAULT   23 data_start
  162.     43: 080483c0     5 FUNC    GLOBAL DEFAULT   12 __libc_csu_fini
  163.     44: 080482f0     0 FUNC    GLOBAL DEFAULT   12 _start
  164.     45: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
  165.     46: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
  166.     47: 08048478     4 OBJECT  GLOBAL DEFAULT   14 _fp_hw
  167.     48: 0804845c     0 FUNC    GLOBAL DEFAULT   13 _fini
  168.     49: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
  169.     50: 0804847c     4 OBJECT  GLOBAL DEFAULT   14 _IO_stdin_used
  170.     51: 080495f8     0 NOTYPE  GLOBAL DEFAULT   23 __data_start
  171.     52: 080495fc     0 OBJECT  GLOBAL HIDDEN   23 __dso_handle
  172.     53: 0804950c     0 OBJECT  GLOBAL HIDDEN   18 __DTOR_END__
  173.     54: 080483d0    90 FUNC    GLOBAL DEFAULT   12 __libc_csu_init
  174.     55: 08049600     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
  175.     56: 08049608     0 NOTYPE  GLOBAL DEFAULT  ABS _end
  176.     57: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@@GLIBC_2.0
  177.     58: 08049600     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
  178.     59: 0804842a     0 FUNC    GLOBAL HIDDEN   12 __i686.get_pc_thunk.bx
  179.     60: 080483a4    28 FUNC    GLOBAL DEFAULT   12 main
  180.     61: 08048274     0 FUNC    GLOBAL DEFAULT   10 _init
  181. Histogram for bucket list length (total of 3 buckets):
  182. Length  Number     % of total  Coverage
  183.       0  0          (  0.0%)
  184.       1  2          ( 66.7%)     50.0%
  185.       2  1          ( 33.3%)    100.0%
  186. Version symbols section '.gnu.version' contains 5 entries:
  187. Addr: 000000000804822a  Offset: 0x00022a  Link: 4 (.dynsym)
  188.   000:   0 (*local*)       0 (*local*)       2 (GLIBC_2.0)     1 (*global*)   
  189.   004:   2 (GLIBC_2.0)  
  190. Version needs section '.gnu.version_r' contains 1 entries:
  191. Addr: 0x0000000008048234  Offset: 0x000234  Link: 5 (.dynstr)
  192.   000000: Version: 1  File: libc.so.6  Cnt: 1
  193.   0x0010:   Name: GLIBC_2.0  Flags: none  Version: 2
  194. Notes at offset 0x00000148 with length 0x00000020:
  195.   Owner                Data size        Description
  196.   GNU                0x00000010        NT_GNU_ABI_TAG (ABI version tag)
复制代码

对,虽然是简单的Hello Word, 也让人看不懂. 这就是现在的gcc和C库.

Load将这几个segment装载到内存后,从0x080482f0 运行程序, 即.text段的_start.  也即前面看到的crt1.o.

crt1.o的最后,跳转到__libc_start_main, 在libgcc.a中.
接着跳转的libgcc_s库中 <未完待续>
回复 支持 反对

使用道具 举报

发表于 2010-4-18 20:30:59 | 显示全部楼层
请老老实实打基础。
正常优化,不该受惊。

如果看 info 不顺眼,可到软件包官方站点看在线文档。

关于 ELF 可执行文件的分析一抓一大把,本论坛就有好几篇,请查阅。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2010-4-18 21:59:48 | 显示全部楼层
想把binutils, gcc, 和C库之间的接口理的明白些, 难那..
好好练功是必用的
我在坛中搜ELF关键词怎么啥也没有? 连这篇文章也没有
回复 支持 反对

使用道具 举报

发表于 2010-4-18 22:34:19 | 显示全部楼层
Post by swordhui;2083713

Thread model: posix
[color="Red"]疑问2: 还有什么thread model?



http://zh.wikipedia.org/zh-cn/POSIX

UNIX环境高级编程  第2版  刚到手的书。建议看下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表