免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5215 | 回复: 4
打印 上一主题 下一主题

[转载]UNIX 目标文件初探 [复制链接]

论坛徽章:
2
羊年新春福章
日期:2015-02-04 10:37:51射手座
日期:2015-02-04 10:38:43
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-10 00:46 |只看该作者 |倒序浏览
Bill Zimmerly (bill@zimmerly.com), 自由撰稿人兼知识工程师, Author
FROM:IBM.COM
2007 年 3 月 06 日
UNIX(R) 系统中运行的程序遵守一种称为目标文件格式的精心设计。了解更多关于目标文件格式的内容,以及可以用来研究系统中目标文件的工具。

计算机编程的最新技术将一种特殊的人性与一组特殊的工具结合在一起,用以生产出对其他人非常有帮助的一种神奇的产品,即软件。计算机程序员是一群注重细节的人,他们可以处理计算机中各种各样的困难。计算机的要求非常苛刻,并且不能容忍其中存在任何的偏差。毫无疑问,无论您的个性如何以及在工作中使用了何种辅助工具,计算机程序的编写都是非常困难的。

在 UNIX® 和 Linux® 中,任何事物都是文件。您可以认为,UNIX 和 Linux 编程实际上是编写处理各种文件的代码。系统由许多类型的文件组成,但目标文件具有一种特殊的设计,提供了灵活和多样的用途。

目标文件是包含带有附加地址和值的助记符号的路线图。这些符号可以用来对各种代码段和数据段进行命名,包括经过初始化的和未初始化的。它们也可以用来定位嵌入的调试信息,就像语义 Web,非常适合由程序进行阅读。

行业工具

计算机编程中使用的工具包括代码编辑器,如 vi 或 Emacs,您可以使用这些工具输入和编辑希望计算机在完成所需任务时执行的指令,以及编译器和连接器,它们可以生成真正实现这些目标的机器代码。

高级的工具,称为集成调试环境 (IDE),它以统一的外观集成了不同工具的功能。IDE 使得编辑器、编译器、连接器和调试器之间的界限变得很模糊。因此,为了更深入地研究和了解系统,在使用集成的套件之前,最好先单独地使用这些工具。(注意:IDE 也通常被称为集成开发环境。)

编译器可以将您在代码编辑器中创建的文本转换为目标文件。最初,目标文件被称为代码的中间表示形式,因为它用作连接编辑器(即连接器)的输入,而连接编辑器最终完成整个任务并生成可执行的程序作为输出。

从代码到可执行代码的转换过程经过了良好的定义并实现了自动化,而目标文件是这个链中有机的连接性环节。在这个转换过程中,目标文件作为连接编辑器所使用的映象,使得它们能够解析各种符号并将不同的代码和数据段连接在一起形成统一的整体。

历史

计算机编程领域中存在许多著名的目标文件格式。DOS 系列包括 COM、OBJ 和 EXE 格式。UNIX 和 Linux 使用 a.out、COFF 和 ELF。Microsoft® Windows® 使用可移植的执行文件 (PE) 格式,而 Macintosh 使用 PEF、Mach-O 和其他文件格式。

最初,各种类型的计算机具有自己独特的目标文件格式,但随着 UNIX 和其他在不同硬件平台上提供可移植性的操作系统的出现,一些常用的文件格式上升为通用的标准。其中包括 a.out、COFF 和 ELF 格式。

要了解目标文件,需要一组可以读取目标文件中不同部分并以更易于读取的格式显示这些内容的工具。本文将讨论这些工具中比较重要的方面。但首先,您必须创建一个工作台,并在其中建立一个研究对象。

工作台

启动一个 xterm 会话,让我们先创建一个空白的工作台,并开始对目标文件进行研究。下面的命令创建了一个目录,可以将目标文件放到该目录中进行研究:cd

  1. mkdir src
  2. cd src
  3. mkdir hw
  4. cd hw
复制代码



然后,使用您最喜欢的代码编辑器,在 $HOME/src/hw 目录中输入清单 1 中的程序,并命名为 hw.c。

清单 1. hw.c 程序       

  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.   printf("Hello World!\n");
  5.   return 0;
  6. }

复制代码


要使用 UNIX 工具库中提供的各种工具,可以将这个简单的“Hello World”程序作为研究的对象。您将学习构建和查看目标文件的输出,而不是使用任何快捷方法直接创建可执行文件(的确有许多这样的快捷方法)。

文件格式

C 编译器的正常输出是用于您所指定的目标处理器的汇编代码。汇编代码是汇编器的输入,在缺省情况下,汇编器将生成所有目标文件的祖先,即 a.out 文件。这个名称本身表示汇编输出 (Assembler Output)。要创建 a.out 文件,可以在 xterm 窗口中输入下面的命令:cc hw.c



注意:如果出现了任何错误或者没有创建 a.out 文件,那么您可能需要检查自己的系统或源文件 (hw.c),以找出其中的错误。还需要检查是否已将 cc 定义为运行您的 C/C++ 编译器。

最新的 C 编译器将编译和汇编步骤组合成一个步骤。您可以指定不同开关选项以查看 C 编译器的汇编输出。通过输入下面的命令,您可以看到 C 编译器的汇编输出:cc -S hw.c



这个命令生成了一个新的文件 hw.s,其中包含您通常无法看到的汇编输入文本,因为编译器在缺省情况下将生成 a.out 文件。正如所预期的,UNIX 汇编程序可以对这种输入文件进行汇编,以生成 a.out 文件。

UNIX 特定的工具

假定编译过程一切顺利,那么在该目录中就有了一个 a.out 文件,下面让我们来对其进行研究。有许多可用于研究目标文件的有价值的工具,下面便是其中一组:
nm:列出目标文件中的符号。
objdump:显示目标文件中的详细信息。
readelf:显示关于 ELF 目标文件的信息。

列表中的第一个工具是 nm,它可以列出目标文件中的符号。如果您输入 nm 命令,您将注意到在缺省情况下,它会寻找一个名为 a.out 的文件。如果没有找到该文件,这个工具会给出相应的提示。然而,如果该工具找到了编译器创建的 a.out 文件,它将显示类似清单 2 的清单。

清单 2. nm 命令的输出       

  1. 08049594 A __bss_start
  2. 080482e4 t call_gmon_start
  3. 08049594 b completed.4463
  4. 08049498 d __CTOR_END__
  5. 08049494 d __CTOR_LIST__
  6. 08049588 D __data_start
  7. 08049588 W data_start
  8. 0804842c t __do_global_ctors_aux
  9. 0804830c t __do_global_dtors_aux
  10. 0804958c D __dso_handle
  11. 080494a0 d __DTOR_END__
  12. 0804949c d __DTOR_LIST__
  13. 080494a8 d _DYNAMIC
  14. 08049594 A _edata
  15. 08049598 A _end
  16. 08048458 T _fini
  17. 08049494 a __fini_array_end
  18. 08049494 a __fini_array_start
  19. 08048478 R _fp_hw
  20. 0804833b t frame_dummy
  21. 08048490 r __FRAME_END__
  22. 08049574 d _GLOBAL_OFFSET_TABLE_
  23.          w __gmon_start__
  24. 08048308 T __i686.get_pc_thunk.bx
  25. 08048278 T _init
  26. 08049494 a __init_array_end
  27. 08049494 a __init_array_start
  28. 0804847c R _IO_stdin_used
  29. 080494a4 d __JCR_END__
  30. 080494a4 d __JCR_LIST__
  31.          w _Jv_RegisterClasses
  32. 080483e1 T __libc_csu_fini
  33. 08048390 T __libc_csu_init
  34.          U __libc_start_main@@GLIBC_2.0
  35. 08048360 T main
  36. 08049590 d p.4462
  37.          U puts@@GLIBC_2.0
  38. 080482c0 T _start

复制代码


这些包含可执行代码的段称为正文段。同样地,数据段包含了不可执行的信息或数据。另一种类型的段,称为 BSS 段,它包含以符号数据开头的块。

对于 nm 命令列出的每个符号,它们的值使用十六进制来表示(缺省行为),并且在该符号前面加上了一个表示符号类型的编码字符。常见的各种编码包括:A 表示绝对 (absolute),这意味着不能将该值更改为其他的连接;B 表示 BSS 段中的符号;而 C 表示引用未初始化的数据的一般符号。

可以将目标文件中所包含的不同的部分划分为段。段可以包含可执行代码、符号名称、初始数据值和许多其他类型的数据。有关这些类型的数据的详细信息,可以阅读 UNIX 中 nm 的 man 页面,其中按照该命令输出中的字符编码分别对每种类型进行了描述。

细节,细节…

在目标文件阶段,即使是一个简单的 Hello World 程序,其中也包含了大量的细节信息。nm 程序可用于列举符号及其类型和值,但是,要更仔细地研究目标文件中这些命名段的内容,需要使用功能更强大的工具。

其中两种功能强大的工具是 objdump 和 readelf 程序。通过输入下面的命令,您可以看到目标文件中包含可执行代码的每个段的汇编清单。对于这么一个小的程序,编译器生成了这么多的代码,真的很令人惊异!objdump -d a.out



这个命令生成的输出如清单 3 所示。每个可执行代码段将在需要特定的事件时执行,这些事件包括库的初始化和该程序本身主入口点。

清单 3. objdump 命令的输出       

  1. a.out:     file format elf32-i386

  2. Disassembly of section .init:

  3. 08048278 <_init>:
  4. 8048278:       55                      push   %ebp
  5. 8048279:       89 e5                   mov    %esp,%ebp
  6. 804827b:       83 ec 08                sub    $0x8,%esp
  7. 804827e:       e8 61 00 00 00          call   80482e4 <call_gmon_start>
  8. 8048283:       e8 b3 00 00 00          call   804833b <frame_dummy>
  9. 8048288:       e8 9f 01 00 00          call   804842c <__do_global_ctors_aux>
  10. 804828d:       c9                      leave
  11. 804828e:       c3                      ret
  12. Disassembly of section .plt:

  13. 08048290 <puts@plt-0x10>:
  14. 8048290:       ff 35 78 95 04 08       pushl  0x8049578
  15. 8048296:       ff 25 7c 95 04 08       jmp    *0x804957c
  16. 804829c:       00 00                   add    %al,(%eax)
  17.         ...

  18. 080482a0 <puts@plt>:
  19. 80482a0:       ff 25 80 95 04 08       jmp    *0x8049580
  20. 80482a6:       68 00 00 00 00          push   $0x0
  21. 80482ab:       e9 e0 ff ff ff          jmp    8048290 <_init+0x18>

  22. 080482b0 <__libc_start_main@plt>:
  23. 80482b0:       ff 25 84 95 04 08       jmp    *0x8049584
  24. 80482b6:       68 08 00 00 00          push   $0x8
  25. 80482bb:       e9 d0 ff ff ff          jmp    8048290 <_init+0x18>
  26. Disassembly of section .text:

  27. 080482c0 <_start>:
  28. 80482c0:       31 ed                   xor    %ebp,%ebp
  29. 80482c2:       5e                      pop    %esi
  30. 80482c3:       89 e1                   mov    %esp,%ecx
  31. 80482c5:       83 e4 f0                and    $0xfffffff0,%esp
  32. 80482c8:       50                      push   %eax
  33. 80482c9:       54                      push   %esp
  34. 80482ca:       52                      push   %edx
  35. 80482cb:       68 e1 83 04 08          push   $0x80483e1
  36. 80482d0:       68 90 83 04 08          push   $0x8048390
  37. 80482d5:       51                      push   %ecx
  38. 80482d6:       56                      push   %esi
  39. 80482d7:       68 60 83 04 08          push   $0x8048360
  40. 80482dc:       e8 cf ff ff ff          call   80482b0 <__libc_start_main@plt>
  41. 80482e1:       f4                      hlt
  42. 80482e2:       90                      nop
  43. 80482e3:       90                      nop

  44. 080482e4 <call_gmon_start>:
  45. 80482e4:       55                      push   %ebp
  46. 80482e5:       89 e5                   mov    %esp,%ebp
  47. 80482e7:       53                      push   %ebx
  48. 80482e8:       e8 1b 00 00 00          call   8048308 <__i686.get_pc_thunk.bx>
  49. 80482ed:       81 c3 87 12 00 00       add    $0x1287,%ebx
  50. 80482f3:       83 ec 04                sub    $0x4,%esp
  51. 80482f6:       8b 83 fc ff ff ff       mov    0xfffffffc(%ebx),%eax
  52. 80482fc:       85 c0                   test   %eax,%eax
  53. 80482fe:       74 02                   je     8048302 <call_gmon_start+0x1e>
  54. 8048300:       ff d0                   call   *%eax
  55. 8048302:       83 c4 04                add    $0x4,%esp
  56. 8048305:       5b                      pop    %ebx
  57. 8048306:       5d                      pop    %ebp
  58. 8048307:       c3                      ret

  59. 08048308 <__i686.get_pc_thunk.bx>:
  60. 8048308:       8b 1c 24                mov    (%esp),%ebx
  61. 804830b:       c3                      ret

  62. 0804830c <__do_global_dtors_aux>:
  63. 804830c:       55                      push   %ebp
  64. 804830d:       89 e5                   mov    %esp,%ebp
  65. 804830f:       83 ec 08                sub    $0x8,%esp
  66. 8048312:       80 3d 94 95 04 08 00    cmpb   $0x0,0x8049594
  67. 8048319:       74 0c                   je     8048327 <__do_global_dtors_aux+0x1b>
  68. 804831b:       eb 1c                   jmp    8048339 <__do_global_dtors_aux+0x2d>
  69. 804831d:       83 c0 04                add    $0x4,%eax
  70. 8048320:       a3 90 95 04 08          mov    %eax,0x8049590
  71. 8048325:       ff d2                   call   *%edx
  72. 8048327:       a1 90 95 04 08          mov    0x8049590,%eax
  73. 804832c:       8b 10                   mov    (%eax),%edx
  74. 804832e:       85 d2                   test   %edx,%edx
  75. 8048330:       75 eb                   jne    804831d <__do_global_dtors_aux+0x11>
  76. 8048332:       c6 05 94 95 04 08 01    movb   $0x1,0x8049594
  77. 8048339:       c9                      leave
  78. 804833a:       c3                      ret

  79. 0804833b <frame_dummy>:
  80. 804833b:       55                      push   %ebp
  81. 804833c:       89 e5                   mov    %esp,%ebp
  82. 804833e:       83 ec 08                sub    $0x8,%esp
  83. 8048341:       a1 a4 94 04 08          mov    0x80494a4,%eax
  84. 8048346:       85 c0                   test   %eax,%eax
  85. 8048348:       74 12                   je     804835c <frame_dummy+0x21>
  86. 804834a:       b8 00 00 00 00          mov    $0x0,%eax
  87. 804834f:       85 c0                   test   %eax,%eax
  88. 8048351:       74 09                   je     804835c <frame_dummy+0x21>
  89. 8048353:       c7 04 24 a4 94 04 08    movl   $0x80494a4,(%esp)
  90. 804835a:       ff d0                   call   *%eax
  91. 804835c:       c9                      leave
  92. 804835d:       c3                      ret
  93. 804835e:       90                      nop
  94. 804835f:       90                      nop

  95. 08048360 <main>:
  96. 8048360:       55                      push   %ebp
  97. 8048361:       89 e5                   mov    %esp,%ebp
  98. 8048363:       83 ec 08                sub    $0x8,%esp
  99. 8048366:       83 e4 f0                and    $0xfffffff0,%esp
  100. 8048369:       b8 00 00 00 00          mov    $0x0,%eax
  101. 804836e:       83 c0 0f                add    $0xf,%eax
  102. 8048371:       83 c0 0f                add    $0xf,%eax
  103. 8048374:       c1 e8 04                shr    $0x4,%eax
  104. 8048377:       c1 e0 04                shl    $0x4,%eax
  105. 804837a:       29 c4                   sub    %eax,%esp
  106. 804837c:       c7 04 24 80 84 04 08    movl   $0x8048480,(%esp)
  107. 8048383:       e8 18 ff ff ff          call   80482a0 <puts@plt>
  108. 8048388:       b8 00 00 00 00          mov    $0x0,%eax
  109. 804838d:       c9                      leave
  110. 804838e:       c3                      ret
  111. 804838f:       90                      nop

  112. 08048390 <__libc_csu_init>:
  113. 8048390:       55                      push   %ebp
  114. 8048391:       89 e5                   mov    %esp,%ebp
  115. 8048393:       57                      push   %edi
  116. 8048394:       56                      push   %esi
  117. 8048395:       31 f6                   xor    %esi,%esi
  118. 8048397:       53                      push   %ebx
  119. 8048398:       e8 6b ff ff ff          call   8048308 <__i686.get_pc_thunk.bx>
  120. 804839d:       81 c3 d7 11 00 00       add    $0x11d7,%ebx
  121. 80483a3:       83 ec 0c                sub    $0xc,%esp
  122. 80483a6:       e8 cd fe ff ff          call   8048278 <_init>
  123. 80483ab:       8d 83 20 ff ff ff       lea    0xffffff20(%ebx),%eax
  124. 80483b1:       8d 93 20 ff ff ff       lea    0xffffff20(%ebx),%edx
  125. 80483b7:       89 45 f0                mov    %eax,0xfffffff0(%ebp)
  126. 80483ba:       29 d0                   sub    %edx,%eax
  127. 80483bc:       c1 f8 02                sar    $0x2,%eax
  128. 80483bf:       39 c6                   cmp    %eax,%esi
  129. 80483c1:       73 16                   jae    80483d9 <__libc_csu_init+0x49>
  130. 80483c3:       89 d7                   mov    %edx,%edi
  131. 80483c5:       ff 14 b2                call   *(%edx,%esi,4)
  132. 80483c8:       8b 45 f0                mov    0xfffffff0(%ebp),%eax
  133. 80483cb:       83 c6 01                add    $0x1,%esi
  134. 80483ce:       29 f8                   sub    %edi,%eax
  135. 80483d0:       89 fa                   mov    %edi,%edx
  136. 80483d2:       c1 f8 02                sar    $0x2,%eax
  137. 80483d5:       39 c6                   cmp    %eax,%esi
  138. 80483d7:       72 ec                   jb     80483c5 <__libc_csu_init+0x35>
  139. 80483d9:       83 c4 0c                add    $0xc,%esp
  140. 80483dc:       5b                      pop    %ebx
  141. 80483dd:       5e                      pop    %esi
  142. 80483de:       5f                      pop    %edi
  143. 80483df:       5d                      pop    %ebp
  144. 80483e0:       c3                      ret

  145. 080483e1 <__libc_csu_fini>:
  146. 80483e1:       55                      push   %ebp
  147. 80483e2:       89 e5                   mov    %esp,%ebp
  148. 80483e4:       83 ec 18                sub    $0x18,%esp
  149. 80483e7:       89 5d f4                mov    %ebx,0xfffffff4(%ebp)
  150. 80483ea:       e8 19 ff ff ff          call   8048308 <__i686.get_pc_thunk.bx>
  151. 80483ef:       81 c3 85 11 00 00       add    $0x1185,%ebx
  152. 80483f5:       89 75 f8                mov    %esi,0xfffffff8(%ebp)
  153. 80483f8:       89 7d fc                mov    %edi,0xfffffffc(%ebp)
  154. 80483fb:       8d b3 20 ff ff ff       lea    0xffffff20(%ebx),%esi
  155. 8048401:       8d bb 20 ff ff ff       lea    0xffffff20(%ebx),%edi
  156. 8048407:       29 fe                   sub    %edi,%esi
  157. 8048409:       c1 fe 02                sar    $0x2,%esi
  158. 804840c:       eb 03                   jmp    8048411 <__libc_csu_fini+0x30>
  159. 804840e:       ff 14 b7                call   *(%edi,%esi,4)
  160. 8048411:       83 ee 01                sub    $0x1,%esi
  161. 8048414:       83 fe ff                cmp    $0xffffffff,%esi
  162. 8048417:       75 f5                   jne    804840e <__libc_csu_fini+0x2d>
  163. 8048419:       e8 3a 00 00 00          call   8048458 <_fini>
  164. 804841e:       8b 5d f4                mov    0xfffffff4(%ebp),%ebx
  165. 8048421:       8b 75 f8                mov    0xfffffff8(%ebp),%esi
  166. 8048424:       8b 7d fc                mov    0xfffffffc(%ebp),%edi
  167. 8048427:       89 ec                   mov    %ebp,%esp
  168. 8048429:       5d                      pop    %ebp
  169. 804842a:       c3                      ret
  170. 804842b:       90                      nop

  171. 0804842c <__do_global_ctors_aux>:
  172. 804842c:       55                      push   %ebp
  173. 804842d:       89 e5                   mov    %esp,%ebp
  174. 804842f:       53                      push   %ebx
  175. 8048430:       83 ec 04                sub    $0x4,%esp
  176. 8048433:       a1 94 94 04 08          mov    0x8049494,%eax
  177. 8048438:       83 f8 ff                cmp    $0xffffffff,%eax
  178. 804843b:       74 12                   je     804844f <__do_global_ctors_aux+0x23>
  179. 804843d:       bb 94 94 04 08          mov    $0x8049494,%ebx
  180. 8048442:       ff d0                   call   *%eax
  181. 8048444:       8b 43 fc                mov    0xfffffffc(%ebx),%eax
  182. 8048447:       83 eb 04                sub    $0x4,%ebx
  183. 804844a:       83 f8 ff                cmp    $0xffffffff,%eax
  184. 804844d:       75 f3                   jne    8048442 <__do_global_ctors_aux+0x16>
  185. 804844f:       83 c4 04                add    $0x4,%esp
  186. 8048452:       5b                      pop    %ebx
  187. 8048453:       5d                      pop    %ebp
  188. 8048454:       c3                      ret
  189. 8048455:       90                      nop
  190. 8048456:       90                      nop
  191. 8048457:       90                      nop
  192. Disassembly of section .fini:

  193. 08048458 <_fini>:
  194. 8048458:       55                      push   %ebp
  195. 8048459:       89 e5                   mov    %esp,%ebp
  196. 804845b:       53                      push   %ebx
  197. 804845c:       e8 a7 fe ff ff          call   8048308 <__i686.get_pc_thunk.bx>
  198. 8048461:       81 c3 13 11 00 00       add    $0x1113,%ebx
  199. 8048467:       83 ec 04                sub    $0x4,%esp
  200. 804846a:       e8 9d fe ff ff          call   804830c <__do_global_dtors_aux>
  201. 804846f:       83 c4 04                add    $0x4,%esp
  202. 8048472:       5b                      pop    %ebx
  203. 8048473:       5d                      pop    %ebp
  204. 8048474:       c3                      ret

复制代码


对于那些着迷于底层编程细节的程序员来说,这是一个功能非常强大的工具,可用于研究编译器和汇编器的输出。细节信息,比如这段代码中所显示的这些信息,可以揭示有关本地处理器本身运行方式的很多内容。对该处理器制造商提供的技术文档进行深入的研究,您可以收集关于一些有价值的信息,通过这些信息可以深入地了解内部的运行机制,因为功能程序提供了清晰的输出。

类似地,readelf 程序也可以清楚地列出目标文件中的内容。输入下面的命令,您将可以看到这一点:readelf -all a.out



这个命令生成的输出如清单 4 所示。ELF Header 为该文件中所有段入口显示了详细的摘要。在列举出这些 Header 中的内容之前,您可以看到 Header 的具体数目。在研究一个较大的目标文件时,该信息可能非常有用。

论坛徽章:
2
羊年新春福章
日期:2015-02-04 10:37:51射手座
日期:2015-02-04 10:38:43
2 [报告]
发表于 2007-03-10 00:47 |只看该作者
清单 4. readelf 命令的输出       

  1. ELF Header:
  2.   Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  3.   Class:                             ELF32
  4.   Data:                              2's complement, little endian
  5.   Version:                           1 (current)
  6.   OS/ABI:                            UNIX - System V
  7.   ABI Version:                       0
  8.   Type:                              EXEC (Executable file)
  9.   Machine:                           Intel 80386
  10.   Version:                           0x1
  11.   Entry point address:               0x80482c0
  12.   Start of program headers:          52 (bytes into file)
  13.   Start of section headers:          3504 (bytes into file)
  14.   Flags:                             0x0
  15.   Size of this header:               52 (bytes)
  16.   Size of program headers:           32 (bytes)
  17.   Number of program headers:         7
  18.   Size of section headers:           40 (bytes)
  19.   Number of section headers:         34
  20.   Section header string table index: 31

  21. Section Headers:
  22.   [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  23.   [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  24.   [ 1] .interp           PROGBITS        08048114 000114 000013 00   A  0   0  1
  25.   [ 2] .note.ABI-tag     NOTE            08048128 000128 000020 00   A  0   0  4
  26.   [ 3] .hash             HASH            08048148 000148 00002c 04   A  4   0  4
  27.   [ 4] .dynsym           DYNSYM          08048174 000174 000060 10   A  5   1  4
  28.   [ 5] .dynstr           STRTAB          080481d4 0001d4 00005e 00   A  0   0  1
  29.   [ 6] .gnu.version      VERSYM          08048232 000232 00000c 02   A  4   0  2
  30.   [ 7] .gnu.version_r    VERNEED         08048240 000240 000020 00   A  5   1  4
  31.   [ 8] .rel.dyn          REL             08048260 000260 000008 08   A  4   0  4
  32.   [ 9] .rel.plt          REL             08048268 000268 000010 08   A  4  11  4
  33.   [10] .init             PROGBITS        08048278 000278 000017 00  AX  0   0  1
  34.   [11] .plt              PROGBITS        08048290 000290 000030 04  AX  0   0  4
  35.   [12] .text             PROGBITS        080482c0 0002c0 000198 00  AX  0   0  4
  36.   [13] .fini             PROGBITS        08048458 000458 00001d 00  AX  0   0  1
  37.   [14] .rodata           PROGBITS        08048478 000478 000015 00   A  0   0  4
  38.   [15] .eh_frame         PROGBITS        08048490 000490 000004 00   A  0   0  4
  39.   [16] .ctors            PROGBITS        08049494 000494 000008 00  WA  0   0  4
  40.   [17] .dtors            PROGBITS        0804949c 00049c 000008 00  WA  0   0  4
  41.   [18] .jcr              PROGBITS        080494a4 0004a4 000004 00  WA  0   0  4
  42.   [19] .dynamic          DYNAMIC         080494a8 0004a8 0000c8 08  WA  5   0  4
  43.   [20] .got              PROGBITS        08049570 000570 000004 04  WA  0   0  4
  44.   [21] .got.plt          PROGBITS        08049574 000574 000014 04  WA  0   0  4
  45.   [22] .data             PROGBITS        08049588 000588 00000c 00  WA  0   0  4
  46.   [23] .bss              NOBITS          08049594 000594 000004 00  WA  0   0  4
  47.   [24] .comment          PROGBITS        00000000 000594 000126 00      0   0  1
  48.   [25] .debug_aranges    PROGBITS        00000000 0006c0 000088 00      0   0  8
  49.   [26] .debug_pubnames   PROGBITS        00000000 000748 000025 00      0   0  1
  50.   [27] .debug_info       PROGBITS        00000000 00076d 00022b 00      0   0  1
  51.   [28] .debug_abbrev     PROGBITS        00000000 000998 000076 00      0   0  1
  52.   [29] .debug_line       PROGBITS        00000000 000a0e 0001bb 00      0   0  1
  53.   [30] .debug_str        PROGBITS        00000000 000bc9 0000bf 01  MS  0   0  1
  54.   [31] .shstrtab         STRTAB          00000000 000c88 000127 00      0   0  1
  55.   [32] .symtab           SYMTAB          00000000 001300 000520 10     33  63  4
  56.   [33] .strtab           STRTAB          00000000 001820 0002d2 00      0   0  1
  57. Key to Flags:
  58.   W (write), A (alloc), X (execute), M (merge), S (strings)
  59.   I (info), L (link order), G (group), x (unknown)
  60.   O (extra OS processing required) o (OS specific), p (processor specific)

  61. There are no section groups in this file.

  62. Program Headers:
  63.   Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  64.   PHDR           0x000034 0x08048034 0x08048034 0x000e0 0x000e0 R E 0x4
  65.   INTERP         0x000114 0x08048114 0x08048114 0x00013 0x00013 R   0x1
  66.       [Requesting program interpreter: /lib/ld-linux.so.2]
  67.   LOAD           0x000000 0x08048000 0x08048000 0x00494 0x00494 R E 0x1000
  68.   LOAD           0x000494 0x08049494 0x08049494 0x00100 0x00104 RW  0x1000
  69.   DYNAMIC        0x0004a8 0x080494a8 0x080494a8 0x000c8 0x000c8 RW  0x4
  70.   NOTE           0x000128 0x08048128 0x08048128 0x00020 0x00020 R   0x4
  71.   GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4

  72. Section to Segment mapping:
  73.   Segment Sections...
  74.    00
  75.    01     .interp
  76.    02     .interp .note.ABI-tag .hash .dynsym .dynstr .gnu.version
  77.           .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame
  78.    03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss
  79.    04     .dynamic
  80.    05     .note.ABI-tag
  81.    06

  82. Dynamic section at offset 0x4a8 contains 20 entries:
  83.   Tag        Type                         Name/Value
  84. 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
  85. 0x0000000c (INIT)                       0x8048278
  86. 0x0000000d (FINI)                       0x8048458
  87. 0x00000004 (HASH)                       0x8048148
  88. 0x00000005 (STRTAB)                     0x80481d4
  89. 0x00000006 (SYMTAB)                     0x8048174
  90. 0x0000000a (STRSZ)                      94 (bytes)
  91. 0x0000000b (SYMENT)                     16 (bytes)
  92. 0x00000015 (DEBUG)                      0x0
  93. 0x00000003 (PLTGOT)                     0x8049574
  94. 0x00000002 (PLTRELSZ)                   16 (bytes)
  95. 0x00000014 (PLTREL)                     REL
  96. 0x00000017 (JMPREL)                     0x8048268
  97. 0x00000011 (REL)                        0x8048260
  98. 0x00000012 (RELSZ)                      8 (bytes)
  99. 0x00000013 (RELENT)                     8 (bytes)
  100. 0x6ffffffe (VERNEED)                    0x8048240
  101. 0x6fffffff (VERNEEDNUM)                 1
  102. 0x6ffffff0 (VERSYM)                     0x8048232
  103. 0x00000000 (NULL)                       0x0

  104. Relocation section '.rel.dyn' at offset 0x260 contains 1 entries:
  105. Offset     Info    Type            Sym.Value  Sym. Name
  106. 08049570  00000506 R_386_GLOB_DAT    00000000   __gmon_start__

  107. Relocation section '.rel.plt' at offset 0x268 contains 2 entries:
  108. Offset     Info    Type            Sym.Value  Sym. Name
  109. 08049580  00000107 R_386_JUMP_SLOT   00000000   puts
  110. 08049584  00000207 R_386_JUMP_SLOT   00000000   __libc_start_main

  111. There are no unwind sections in this file.

  112. Symbol table '.dynsym' contains 6 entries:
  113.    Num:    Value  Size Type    Bind   Vis      Ndx Name
  114.      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
  115.      1: 00000000   378 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.0 (2)
  116.      2: 00000000   230 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.0 (2)
  117.      3: 0804847c     4 OBJECT  GLOBAL DEFAULT   14 _IO_stdin_used
  118.      4: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
  119.      5: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

  120. Symbol table '.symtab' contains 82 entries:
  121.    Num:    Value  Size Type    Bind   Vis      Ndx Name
  122.      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
  123.      1: 08048114     0 SECTION LOCAL  DEFAULT    1
  124.      2: 08048128     0 SECTION LOCAL  DEFAULT    2
  125.      3: 08048148     0 SECTION LOCAL  DEFAULT    3
  126.      4: 08048174     0 SECTION LOCAL  DEFAULT    4
  127.      5: 080481d4     0 SECTION LOCAL  DEFAULT    5
  128.      6: 08048232     0 SECTION LOCAL  DEFAULT    6
  129.      7: 08048240     0 SECTION LOCAL  DEFAULT    7
  130.      8: 08048260     0 SECTION LOCAL  DEFAULT    8
  131.      9: 08048268     0 SECTION LOCAL  DEFAULT    9
  132.     10: 08048278     0 SECTION LOCAL  DEFAULT   10
  133.     11: 08048290     0 SECTION LOCAL  DEFAULT   11
  134.     12: 080482c0     0 SECTION LOCAL  DEFAULT   12
  135.     13: 08048458     0 SECTION LOCAL  DEFAULT   13
  136.     14: 08048478     0 SECTION LOCAL  DEFAULT   14
  137.     15: 08048490     0 SECTION LOCAL  DEFAULT   15
  138.     16: 08049494     0 SECTION LOCAL  DEFAULT   16
  139.     17: 0804949c     0 SECTION LOCAL  DEFAULT   17
  140.     18: 080494a4     0 SECTION LOCAL  DEFAULT   18
  141.     19: 080494a8     0 SECTION LOCAL  DEFAULT   19
  142.     20: 08049570     0 SECTION LOCAL  DEFAULT   20
  143.     21: 08049574     0 SECTION LOCAL  DEFAULT   21
  144.     22: 08049588     0 SECTION LOCAL  DEFAULT   22
  145.     23: 08049594     0 SECTION LOCAL  DEFAULT   23
  146.     24: 00000000     0 SECTION LOCAL  DEFAULT   24
  147.     25: 00000000     0 SECTION LOCAL  DEFAULT   25
  148.     26: 00000000     0 SECTION LOCAL  DEFAULT   26
  149.     27: 00000000     0 SECTION LOCAL  DEFAULT   27
  150.     28: 00000000     0 SECTION LOCAL  DEFAULT   28
  151.     29: 00000000     0 SECTION LOCAL  DEFAULT   29
  152.     30: 00000000     0 SECTION LOCAL  DEFAULT   30
  153.     31: 00000000     0 SECTION LOCAL  DEFAULT   31
  154.     32: 00000000     0 SECTION LOCAL  DEFAULT   32
  155.     33: 00000000     0 SECTION LOCAL  DEFAULT   33
  156.     34: 00000000     0 FILE    LOCAL  DEFAULT  ABS abi-note.S
  157.     35: 00000000     0 FILE    LOCAL  DEFAULT  ABS ../sysdeps/i386/elf/start
  158.     36: 00000000     0 FILE    LOCAL  DEFAULT  ABS init.c
  159.     37: 00000000     0 FILE    LOCAL  DEFAULT  ABS initfini.c
  160.     38: 00000000     0 FILE    LOCAL  DEFAULT  ABS /build/buildd/glibc-2.3.6
  161.     39: 080482e4     0 FUNC    LOCAL  DEFAULT   12 call_gmon_start
  162.     40: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
  163.     41: 08049494     0 OBJECT  LOCAL  DEFAULT   16 __CTOR_LIST__
  164.     42: 0804949c     0 OBJECT  LOCAL  DEFAULT   17 __DTOR_LIST__
  165.     43: 080494a4     0 OBJECT  LOCAL  DEFAULT   18 __JCR_LIST__
  166.     44: 08049594     1 OBJECT  LOCAL  DEFAULT   23 completed.4463
  167.     45: 08049590     0 OBJECT  LOCAL  DEFAULT   22 p.4462
  168.     46: 0804830c     0 FUNC    LOCAL  DEFAULT   12 __do_global_dtors_aux
  169.     47: 0804833b     0 FUNC    LOCAL  DEFAULT   12 frame_dummy
  170.     48: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
  171.     49: 08049498     0 OBJECT  LOCAL  DEFAULT   16 __CTOR_END__
  172.     50: 080494a0     0 OBJECT  LOCAL  DEFAULT   17 __DTOR_END__
  173.     51: 08048490     0 OBJECT  LOCAL  DEFAULT   15 __FRAME_END__
  174.     52: 080494a4     0 OBJECT  LOCAL  DEFAULT   18 __JCR_END__
  175.     53: 0804842c     0 FUNC    LOCAL  DEFAULT   12 __do_global_ctors_aux
  176.     54: 00000000     0 FILE    LOCAL  DEFAULT  ABS initfini.c
  177.     55: 00000000     0 FILE    LOCAL  DEFAULT  ABS /build/buildd/glibc-2.3.6
  178.     56: 00000000     0 FILE    LOCAL  DEFAULT  ABS hw.c
  179.     57: 080494a8     0 OBJECT  LOCAL  HIDDEN   19 _DYNAMIC
  180.     58: 08049494     0 NOTYPE  LOCAL  HIDDEN  ABS __fini_array_end
  181.     59: 08049494     0 NOTYPE  LOCAL  HIDDEN  ABS __fini_array_start
  182.     60: 08049494     0 NOTYPE  LOCAL  HIDDEN  ABS __init_array_end
  183.     61: 08049574     0 OBJECT  LOCAL  HIDDEN   21 _GLOBAL_OFFSET_TABLE_
  184.     62: 08049494     0 NOTYPE  LOCAL  HIDDEN  ABS __init_array_start
  185.     63: 08048478     4 OBJECT  GLOBAL DEFAULT   14 _fp_hw
  186.     64: 0804958c     0 OBJECT  GLOBAL HIDDEN   22 __dso_handle
  187.     65: 080483e1    74 FUNC    GLOBAL DEFAULT   12 __libc_csu_fini
  188.     66: 00000000   378 FUNC    GLOBAL DEFAULT  UND puts@@GLIBC_2.0
  189.     67: 08048278     0 FUNC    GLOBAL DEFAULT   10 _init
  190.     68: 080482c0     0 FUNC    GLOBAL DEFAULT   12 _start
  191.     69: 08048390    81 FUNC    GLOBAL DEFAULT   12 __libc_csu_init
  192.     70: 08049594     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
  193.     71: 08048360    47 FUNC    GLOBAL DEFAULT   12 main
  194.     72: 00000000   230 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
  195.     73: 08049588     0 NOTYPE  WEAK   DEFAULT   22 data_start
  196.     74: 08048458     0 FUNC    GLOBAL DEFAULT   13 _fini
  197.     75: 08049594     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
  198.     76: 08048308     0 FUNC    GLOBAL HIDDEN   12 __i686.get_pc_thunk.bx
  199.     77: 08049598     0 NOTYPE  GLOBAL DEFAULT  ABS _end
  200.     78: 0804847c     4 OBJECT  GLOBAL DEFAULT   14 _IO_stdin_used
  201.     79: 08049588     0 NOTYPE  GLOBAL DEFAULT   22 __data_start
  202.     80: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
  203.     81: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

  204. Histogram for bucket list length (total of 3 buckets):
  205. Length  Number     % of total  Coverage
  206.       0  0          (  0.0%)
  207.       1  1          ( 33.3%)     20.0%
  208.       2  2          ( 66.7%)    100.0%

  209. Version symbols section '.gnu.version' contains 6 entries:
  210. Addr: 0000000008048232  Offset: 0x000232  Link: 4 (.dynsym)
  211.   000:   0 (*local*)       2 (GLIBC_2.0)     2 (GLIBC_2.0)     1 (*global*)
  212.   004:   0 (*local*)       0 (*local*)

  213. Version needs section '.gnu.version_r' contains 1 entries:
  214. Addr: 0x0000000008048240  Offset: 0x000240  Link to section: 5 (.dynstr)
  215.   000000: Version: 1  File: libc.so.6  Cnt: 1
  216.   0x0010:   Name: GLIBC_2.0  Flags: none  Version: 2

  217. Notes at offset 0x00000128 with length 0x00000020:
  218.   Owner         Data size       Description
  219.   GNU           0x00000010      NT_VERSION (version)

复制代码


正如从该输出中看到的,简单的 a.out Hello World 文件中包含了大量有价值的细节信息,包括版本信息、柱状图、各种符号类型的表格,等等。通过使用本文中介绍的这几种工具分析目标文件,您可以慢慢地对可执行程序进行研究。

除了所有这些段之外,编译器可以将调试信息放入到目标文件中,并且还可以显示这些信息。输入下面的命令,仔细分析编译器的输出(假设您扮演了调试程序的角色):readelf --debug-dump a.out | less



这个命令生成的输出如清单 5 所示。调试工具,如 GDB,可以读取这些调试信息,并且当程序在调试器中运行的同时,您可以使用该工具显示更具描述性的标记,而不是对代码进行反汇编时的原始地址值。

论坛徽章:
2
羊年新春福章
日期:2015-02-04 10:37:51射手座
日期:2015-02-04 10:38:43
3 [报告]
发表于 2007-03-10 00:50 |只看该作者
清单 5. 该程序中的调试信息       

  1. The section .debug_aranges contains:

  2.   Length:                   28
  3.   Version:                  2
  4.   Offset into .debug_info:  0
  5.   Pointer Size:             4
  6.   Segment Size:             0

  7.     Address  Length
  8.     080482c0 34
  9.   Length:                   52
  10.   Version:                  2
  11.   Offset into .debug_info:  10b
  12.   Pointer Size:             4
  13.   Segment Size:             0

  14.     Address  Length
  15.     08048308 4
  16.     08048458 18
  17.     08048278 11
  18.     080482e4 36
  19.   Length:                   44
  20.   Version:                  2
  21.   Offset into .debug_info:  19b
  22.   Pointer Size:             4
  23.   Segment Size:             0

  24.     Address  Length
  25.     08048308 4
  26.     0804846f 6
  27.     0804828d 2

  28. Contents of the .debug_pubnames section:

  29.   Length:                              33
  30.   Version:                             2
  31.   Offset into .debug_info section:     122
  32.   Size of area in .debug_info section: 145

  33.     Offset      Name
  34.     121                 _IO_stdin_used

  35. The section .debug_info contains:

  36.   Compilation Unit @ offset 0x0:
  37.    Length:        118
  38.    Version:       2
  39.    Abbrev Offset: 0
  40.    Pointer Size:  4
  41. <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
  42.      DW_AT_stmt_list   : 0
  43.      DW_AT_low_pc      : 0x80482c0
  44.      DW_AT_high_pc     : 0x80482e2
  45.      DW_AT_name        : ../sysdeps/i386/elf/start.S
  46.      DW_AT_comp_dir    : /build/buildd/glibc-2.3.6/build-tree/glibc-2.3.6/csu
  47.      DW_AT_producer    : GNU AS 2.16.91
  48.      DW_AT_language    : 32769  (MIPS assembler)
  49.   Compilation Unit @ offset 0x7a:
  50.    Length:        141
  51.    Version:       2
  52.    Abbrev Offset: 20
  53.    Pointer Size:  4
  54. <0><85>: Abbrev Number: 1 (DW_TAG_compile_unit)
  55.      DW_AT_stmt_list   : 0x5b
  56.      DW_AT_high_pc     : 0x80482e4
  57.      DW_AT_low_pc      : 0x80482e4
  58.      DW_AT_producer    : (indirect string, offset: 0x62): GNU C 3.4.6
  59.      DW_AT_language    : 1      (ANSI C)
  60.      DW_AT_name        : (indirect string, offset: 0x0): init.c
  61.      DW_AT_comp_dir    : (indirect string, offset: 0x11): /build/buildd/...
  62. <1><9f>: Abbrev Number: 2 (DW_TAG_base_type)
  63.      DW_AT_name        : (indirect string, offset: 0x90): unsigned int
  64.      DW_AT_byte_size   : 4
  65.      DW_AT_encoding    : 7      (unsigned)
  66. <1><a6>: Abbrev Number: 2 (DW_TAG_base_type)
  67.      DW_AT_name        : (indirect string, offset: 0x54): unsigned char
  68.      DW_AT_byte_size   : 1
  69.      DW_AT_encoding    : 8      (unsigned char)
  70. <1><ad>: Abbrev Number: 2 (DW_TAG_base_type)
  71.      DW_AT_name        : (indirect string, offset: 0x9d): short unsigned int
  72.      DW_AT_byte_size   : 2
  73.      DW_AT_encoding    : 7      (unsigned)
  74. <1><b4>: Abbrev Number: 2 (DW_TAG_base_type)
  75.      DW_AT_name        : (indirect string, offset: 0x8b): long unsigned int
  76.      DW_AT_byte_size   : 4
  77.      DW_AT_encoding    : 7      (unsigned)
  78. <1><bb>: Abbrev Number: 2 (DW_TAG_base_type)
  79.      DW_AT_name        : (indirect string, offset: 0x56): signed char
  80.      DW_AT_byte_size   : 1
  81.      DW_AT_encoding    : 6      (signed char)
  82. <1><c2>: Abbrev Number: 2 (DW_TAG_base_type)
  83.      DW_AT_name        : (indirect string, offset: 0x7): short int
  84.      DW_AT_byte_size   : 2
  85.      DW_AT_encoding    : 5      (signed)
  86. <1><c9>: Abbrev Number: 3 (DW_TAG_base_type)
  87.      DW_AT_name        : int
  88.      DW_AT_byte_size   : 4
  89.      DW_AT_encoding    : 5      (signed)
  90. <1><d0>: Abbrev Number: 2 (DW_TAG_base_type)
  91.      DW_AT_name        : (indirect string, offset: 0x46): long long int
  92.      DW_AT_byte_size   : 8
  93.      DW_AT_encoding    : 5      (signed)
  94. <1><d7>: Abbrev Number: 2 (DW_TAG_base_type)
  95.      DW_AT_name        : (indirect string, offset: 0x86): long long unsigned int
  96.      DW_AT_byte_size   : 8
  97.      DW_AT_encoding    : 7      (unsigned)
  98. <1><de>: Abbrev Number: 2 (DW_TAG_base_type)
  99.      DW_AT_name        : (indirect string, offset: 0x4b): long int
  100.      DW_AT_byte_size   : 4
  101.      DW_AT_encoding    : 5      (signed)
  102. <1><e5>: Abbrev Number: 2 (DW_TAG_base_type)
  103.      DW_AT_name        : (indirect string, offset: 0x90): unsigned int
  104.      DW_AT_byte_size   : 4
  105.      DW_AT_encoding    : 7      (unsigned)
  106. <1><ec>: Abbrev Number: 2 (DW_TAG_base_type)
  107.      DW_AT_name        : (indirect string, offset: 0x5d): char
  108.      DW_AT_byte_size   : 1
  109.      DW_AT_encoding    : 6      (signed char)
  110. <1><f3>: Abbrev Number: 4 (DW_TAG_variable)
  111.      DW_AT_name        : (indirect string, offset: 0xb0): _IO_stdin_used
  112.      DW_AT_decl_file   : 1
  113.      DW_AT_decl_line   : 25
  114.      DW_AT_type        : <105>
  115.      DW_AT_external    : 1
  116.      DW_AT_location    : 5 byte block: 3 7c 84 4 8      (DW_OP_addr: 804847c)
  117. <1><105>: Abbrev Number: 5 (DW_TAG_const_type)
  118.      DW_AT_type        : <c9>
  119.   Compilation Unit @ offset 0x10b:
  120.    Length:        140
  121.    Version:       2
  122.    Abbrev Offset: 86
  123.    Pointer Size:  4
  124. <0><116>: Abbrev Number: 1 (DW_TAG_compile_unit)
  125.      DW_AT_stmt_list   : 0x82
  126.      DW_AT_name        : /build/buildd/glibc-2.3.6/build-tree/i386-libc/csu/crti.S
  127.      DW_AT_comp_dir    : /build/buildd/glibc-2.3.6/build-tree/glibc-2.3.6/csu
  128.      DW_AT_producer    : GNU AS 2.16.91
  129.      DW_AT_language    : 32769  (MIPS assembler)
  130.   Compilation Unit @ offset 0x19b:
  131.    Length:        140
  132.    Version:       2
  133.    Abbrev Offset: 102
  134.    Pointer Size:  4
  135. <0><1a6>: Abbrev Number: 1 (DW_TAG_compile_unit)
  136.      DW_AT_stmt_list   : 0x12f
  137.      DW_AT_name        : /build/buildd/glibc-2.3.6/build-tree/i386-libc/csu/crtn.S
  138.      DW_AT_comp_dir    : /build/buildd/glibc-2.3.6/build-tree/glibc-2.3.6/csu
  139.      DW_AT_producer    : GNU AS 2.16.91
  140.      DW_AT_language    : 32769  (MIPS assembler)

  141. Contents of the .debug_abbrev section:

  142.   Number TAG
  143.    1      DW_TAG_compile_unit    [no children]
  144.     DW_AT_stmt_list    DW_FORM_data4
  145.     DW_AT_low_pc       DW_FORM_addr
  146.     DW_AT_high_pc      DW_FORM_addr
  147.     DW_AT_name         DW_FORM_string
  148.     DW_AT_comp_dir     DW_FORM_string
  149.     DW_AT_producer     DW_FORM_string
  150.     DW_AT_language     DW_FORM_data2
  151.   Number TAG
  152.    1      DW_TAG_compile_unit    [has children]
  153.     DW_AT_stmt_list    DW_FORM_data4
  154.     DW_AT_high_pc      DW_FORM_addr
  155.     DW_AT_low_pc       DW_FORM_addr
  156.     DW_AT_producer     DW_FORM_strp
  157.     DW_AT_language     DW_FORM_data1
  158.     DW_AT_name         DW_FORM_strp
  159.     DW_AT_comp_dir     DW_FORM_strp
  160.    2      DW_TAG_base_type    [no children]
  161.     DW_AT_name         DW_FORM_strp
  162.     DW_AT_byte_size    DW_FORM_data1
  163.     DW_AT_encoding     DW_FORM_data1
  164.    3      DW_TAG_base_type    [no children]
  165.     DW_AT_name         DW_FORM_string
  166.     DW_AT_byte_size    DW_FORM_data1
  167.     DW_AT_encoding     DW_FORM_data1
  168.    4      DW_TAG_variable    [no children]
  169.     DW_AT_name         DW_FORM_strp
  170.     DW_AT_decl_file    DW_FORM_data1
  171.     DW_AT_decl_line    DW_FORM_data1
  172.     DW_AT_type         DW_FORM_ref4
  173.     DW_AT_external     DW_FORM_flag
  174.     DW_AT_location     DW_FORM_block1
  175.    5      DW_TAG_const_type    [no children]
  176.     DW_AT_type         DW_FORM_ref4
  177.   Number TAG
  178.    1      DW_TAG_compile_unit    [no children]
  179.     DW_AT_stmt_list    DW_FORM_data4
  180.     DW_AT_name         DW_FORM_string
  181.     DW_AT_comp_dir     DW_FORM_string
  182.     DW_AT_producer     DW_FORM_string
  183.     DW_AT_language     DW_FORM_data2
  184.   Number TAG
  185.    1      DW_TAG_compile_unit    [no children]
  186.     DW_AT_stmt_list    DW_FORM_data4
  187.     DW_AT_name         DW_FORM_string
  188.     DW_AT_comp_dir     DW_FORM_string
  189.     DW_AT_producer     DW_FORM_string
  190.     DW_AT_language     DW_FORM_data2


  191. Dump of debug contents of section .debug_line:

  192.   Length:                      87
  193.   DWARF Version:               2
  194.   Prologue Length:             50
  195.   Minimum Instruction Length:  1
  196.   Initial value of 'is_stmt':  1
  197.   Line Base:                   -5
  198.   Line Range:                  14
  199.   Opcode Base:                 13
  200.   (Pointer size:               4)

  201. Opcodes:
  202.   Opcode 1 has 0 args
  203.   Opcode 2 has 1 args
  204.   Opcode 3 has 1 args
  205.   Opcode 4 has 1 args
  206.   Opcode 5 has 1 args
  207.   Opcode 6 has 0 args
  208.   Opcode 7 has 0 args
  209.   Opcode 8 has 0 args
  210.   Opcode 9 has 1 args
  211.   Opcode 10 has 0 args
  212.   Opcode 11 has 0 args
  213.   Opcode 12 has 1 args

  214. The Directory Table:
  215.   ../sysdeps/i386/elf

  216. The File Name Table:
  217.   Entry Dir     Time    Size    Name
  218.   1     1       0       0       start.S

  219. Line Number Statements:
  220.   Extended opcode 2: set Address to 0x80482c0
  221.   Advance Line by 64 to 65
  222.   Copy
  223.   Special opcode 38: advance Address by 2 to 0x80482c2 and Line by 5 to 70
  224.   Special opcode 20: advance Address by 1 to 0x80482c3 and Line by 1 to 71
  225.   Special opcode 39: advance Address by 2 to 0x80482c5 and Line by 6 to 77
  226.   Special opcode 48: advance Address by 3 to 0x80482c8 and Line by 1 to 78
  227.   Special opcode 24: advance Address by 1 to 0x80482c9 and Line by 5 to 83
  228.   Special opcode 21: advance Address by 1 to 0x80482ca and Line by 2 to 85
  229.   Advance Line by 24 to 109
  230.   Special opcode 19: advance Address by 1 to 0x80482cb and Line by 0 to 109
  231.   Special opcode 76: advance Address by 5 to 0x80482d0 and Line by 1 to 110
  232.   Special opcode 77: advance Address by 5 to 0x80482d5 and Line by 2 to 112
  233.   Special opcode 20: advance Address by 1 to 0x80482d6 and Line by 1 to 113
  234.   Special opcode 21: advance Address by 1 to 0x80482d7 and Line by 2 to 115
  235.   Special opcode 79: advance Address by 5 to 0x80482dc and Line by 4 to 119
  236.   Special opcode 78: advance Address by 5 to 0x80482e1 and Line by 3 to 122
  237.   Advance PC by 1 to 0x80482e2
  238.   Extended opcode 1: End of Sequence


  239.   Length:                      35
  240.   DWARF Version:               2
  241.   Prologue Length:             29
  242.   Minimum Instruction Length:  1
  243.   Initial value of 'is_stmt':  1
  244.   Line Base:                   -5
  245.   Line Range:                  14
  246.   Opcode Base:                 13
  247.   (Pointer size:               4)

  248. Opcodes:
  249.   Opcode 1 has 0 args
  250.   Opcode 2 has 1 args
  251.   Opcode 3 has 1 args
  252.   Opcode 4 has 1 args
  253.   Opcode 5 has 1 args
  254.   Opcode 6 has 0 args
  255.   Opcode 7 has 0 args
  256.   Opcode 8 has 0 args
  257.   Opcode 9 has 1 args
  258.   Opcode 10 has 0 args
  259.   Opcode 11 has 0 args
  260.   Opcode 12 has 1 args

  261. The Directory Table is empty.

  262. The File Name Table:
  263.   Entry Dir     Time    Size    Name
  264.   1     0       0       0       init.c

  265. Line Number Statements:

  266.   Length:                      169
  267.   DWARF Version:               2
  268.   Prologue Length:             80
  269.   Minimum Instruction Length:  1
  270.   Initial value of 'is_stmt':  1
  271.   Line Base:                   -5
  272.   Line Range:                  14
  273.   Opcode Base:                 13
  274.   (Pointer size:               4)

  275. Opcodes:
  276.   Opcode 1 has 0 args
  277.   Opcode 2 has 1 args
  278.   Opcode 3 has 1 args
  279.   Opcode 4 has 1 args
  280.   Opcode 5 has 1 args
  281.   Opcode 6 has 0 args
  282.   Opcode 7 has 0 args
  283.   Opcode 8 has 0 args
  284.   Opcode 9 has 1 args
  285.   Opcode 10 has 0 args
  286.   Opcode 11 has 0 args
  287.   Opcode 12 has 1 args

  288. The Directory Table:
  289.   /build/buildd/glibc-2.3.6/build-tree/i386-libc/csu

  290. The File Name Table:
  291.   Entry Dir     Time    Size    Name
  292.   1     1       0       0       crti.S

  293. Line Number Statements:
  294.   Extended opcode 2: set Address to 0x8048308
  295.   Advance Line by 64 to 65
  296.   Copy
  297.   Special opcode 48: advance Address by 3 to 0x804830b and Line by 1 to 66
  298.   Advance PC by 1 to 0x804830c
  299.   Extended opcode 1: End of Sequence

  300.   Extended opcode 2: set Address to 0x8048458
  301.   Advance Line by 46 to 47
  302.   Copy
  303.   Special opcode 20: advance Address by 1 to 0x8048459 and Line by 1 to 48
  304.   Special opcode 34: advance Address by 2 to 0x804845b and Line by 1 to 49
  305.   Special opcode 20: advance Address by 1 to 0x804845c and Line by 1 to 50
  306.   Special opcode 76: advance Address by 5 to 0x8048461 and Line by 1 to 51
  307.   Special opcode 90: advance Address by 6 to 0x8048467 and Line by 1 to 52
  308.   Advance PC by 3 to 0x804846a
  309.   Extended opcode 1: End of Sequence

  310.   Extended opcode 2: set Address to 0x8048278
  311.   Advance Line by 31 to 32
  312.   Copy
  313.   Special opcode 20: advance Address by 1 to 0x8048279 and Line by 1 to 33
  314.   Special opcode 34: advance Address by 2 to 0x804827b and Line by 1 to 34
  315.   Special opcode 48: advance Address by 3 to 0x804827e and Line by 1 to 35
  316.   Advance PC by 5 to 0x8048283
  317.   Extended opcode 1: End of Sequence

  318.   Extended opcode 2: set Address to 0x80482e4
  319.   Advance Line by 10 to 11
  320.   Copy
  321.   Special opcode 20: advance Address by 1 to 0x80482e5 and Line by 1 to 12
  322.   Special opcode 34: advance Address by 2 to 0x80482e7 and Line by 1 to 13
  323.   Special opcode 20: advance Address by 1 to 0x80482e8 and Line by 1 to 14
  324.   Special opcode 76: advance Address by 5 to 0x80482ed and Line by 1 to 15
  325.   Special opcode 90: advance Address by 6 to 0x80482f3 and Line by 1 to 16
  326.   Special opcode 48: advance Address by 3 to 0x80482f6 and Line by 1 to 17
  327.   Special opcode 90: advance Address by 6 to 0x80482fc and Line by 1 to 18
  328.   Special opcode 34: advance Address by 2 to 0x80482fe and Line by 1 to 19
  329.   Special opcode 34: advance Address by 2 to 0x8048300 and Line by 1 to 20
  330.   Special opcode 35: advance Address by 2 to 0x8048302 and Line by 2 to 22
  331.   Special opcode 48: advance Address by 3 to 0x8048305 and Line by 1 to 23
  332.   Special opcode 20: advance Address by 1 to 0x8048306 and Line by 1 to 24
  333.   Special opcode 20: advance Address by 1 to 0x8048307 and Line by 1 to 25
  334.   Advance PC by 1 to 0x8048308
  335.   Extended opcode 1: End of Sequence


  336.   Length:                      136
  337.   DWARF Version:               2
  338.   Prologue Length:             80
  339.   Minimum Instruction Length:  1
  340.   Initial value of 'is_stmt':  1
  341.   Line Base:                   -5
  342.   Line Range:                  14
  343.   Opcode Base:                 13
  344.   (Pointer size:               4)

  345. Opcodes:
  346.   Opcode 1 has 0 args
  347.   Opcode 2 has 1 args
  348.   Opcode 3 has 1 args
  349.   Opcode 4 has 1 args
  350.   Opcode 5 has 1 args
  351.   Opcode 6 has 0 args
  352.   Opcode 7 has 0 args
  353.   Opcode 8 has 0 args
  354.   Opcode 9 has 1 args
  355.   Opcode 10 has 0 args
  356.   Opcode 11 has 0 args
  357.   Opcode 12 has 1 args

  358. The Directory Table:
  359.   /build/buildd/glibc-2.3.6/build-tree/i386-libc/csu

  360. The File Name Table:
  361.   Entry Dir     Time    Size    Name
  362.   1     1       0       0       crtn.S

  363. Line Number Statements:
  364.   Extended opcode 2: set Address to 0x8048308
  365.   Advance Line by 33 to 34
  366.   Copy
  367.   Special opcode 48: advance Address by 3 to 0x804830b and Line by 1 to 35
  368.   Advance PC by 1 to 0x804830c
  369.   Extended opcode 1: End of Sequence

  370.   Extended opcode 2: set Address to 0x804846f
  371.   Advance Line by 18 to 19
  372.   Copy
  373.   Special opcode 48: advance Address by 3 to 0x8048472 and Line by 1 to 20
  374.   Special opcode 20: advance Address by 1 to 0x8048473 and Line by 1 to 21
  375.   Special opcode 20: advance Address by 1 to 0x8048474 and Line by 1 to 22
  376.   Advance PC by 1 to 0x8048475
  377.   Extended opcode 1: End of Sequence

  378.   Extended opcode 2: set Address to 0x804828d
  379.   Advance Line by 9 to 10
  380.   Copy
  381.   Special opcode 20: advance Address by 1 to 0x804828e and Line by 1 to 11
  382.   Advance PC by 1 to 0x804828f
  383.   Extended opcode 1: End of Sequence


  384. Contents of the .debug_str section:

  385.   0x00000000 696e6974 2e630073 686f7274 20696e74 init.c.short int
  386.   0x00000010 002f6275 696c642f 6275696c 64642f67 ./build/buildd/g
  387.   0x00000020 6c696263 2d322e33 2e362f62 75696c64 libc-2.3.6/build
  388.   0x00000030 2d747265 652f676c 6962632d 322e332e -tree/glibc-2.3.
  389.   0x00000040 362f6373 75006c6f 6e67206c 6f6e6720 6/csu.long long
  390.   0x00000050 696e7400 756e7369 676e6564 20636861 int.unsigned cha
  391.   0x00000060 7200474e 55204320 332e342e 36202855 r.GNU C 3.4.6 (U
  392.   0x00000070 62756e74 7520332e 342e362d 31756275 buntu 3.4.6-1ubu
  393.   0x00000080 6e747532 29006c6f 6e67206c 6f6e6720 ntu2).long long
  394.   0x00000090 756e7369 676e6564 20696e74 0073686f unsigned int.sho
  395.   0x000000a0 72742075 6e736967 6e656420 696e7400 rt unsigned int.
  396.   0x000000b0 5f494f5f 73746469 6e5f7573 656400   _IO_stdin_used.

复制代码


可执行文件是目标文件

在 UNIX 中,可执行文件是 目标文件,并且您可以像对 a.out 文件那样对它们进行分析。可以进行一次有益的练习,更改到 /bin 或 /local/bin 目录,然后针对一些您最常用的命令,如 pwd、ps、cat 或 rm,运行 nm、objdump 和 readelf。通常,在您编写需要某种功能的程序时,如果标准的工具已经提供了这个功能,那么通过运行 objdump -d <command>,可以查看这些工具究竟如何完成这项任务。

如果您倾向于使用编译器和其他的语言工具,那么您可以对组成计算机系统的各种目标文件进行仔细研究,并且您将会发现这项工作是非常值得的。UNIX 操作系统具有许多层次,那些通过工具查看目标文件所公开的层次,非常接近底层硬件。通过这种方式,您可以真实地接触到系统。

结束语

研究目标文件可以极大地加深您对 UNIX 操作系统的认识,并且可以更深入地了解如何对软件的源代码进行汇编。我鼓励您使用本文中介绍的目标文件工具对系统中 /bin 或 /local/bin 目录中的程序进行分析,仔细研究其输出结果,并找出您的硬件制造商所提供的系统文档。

论坛徽章:
0
4 [报告]
发表于 2007-03-10 08:27 |只看该作者
沙发, 学而受益:)

论坛徽章:
0
5 [报告]
发表于 2007-04-01 17:22 |只看该作者
看不懂没到那个程度,,,,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP