objdump - display information from object files

来源:互联网 发布:黑马程序员视频百度云 编辑:程序博客网 时间:2024/06/12 22:23
因为计算机大都是采用哈佛结构,数据和指令分开存储的的程序组织形式,因此当我门编译,汇编,连接,(符号解吸,段的重定位)。而为了能让程序更好的被执行,最终生成的目标文件包含了足够多了信息生怕自己被歧视而不被执行。所以面对2进制的目标文件来说,我门是很难发现或者看到什么有用的信息,因此objdump这个工具的实际意义是多么的令人喜爱,只有当你要探索一个目标文件的组织形式的时候就能体会了
 
更多的细节建议看看《深入理解计算机》这本书的第二部分第七章节,虽然作者的逻辑思维与本人有异,有可能你会喜欢哦。
 
 
---------------objdump是用查看目标文件或者可执行的目标文件的构成的GCC工具----------
以下3条命令足够那些喜欢探索目标文件与源代码之间的丝丝的关系的朋友。
objdump  -x  obj  以某种分类信息的形式把目标文件的数据组织(被分为几大块)输出   
objdump  -t  obj  输出目标文件的符号表
objdump  -h  obj  输出目标文件的所有段概括
objdump  -j .text/.data -S  obj  输出指定段的信息,大概就是反汇编源代码把
 
以下为网上摘录文章。
 



★ 测试练习前的准备工作

cp /usr/lib/libyelp.a ~/workspace
// list symbols from object file
nm -s libyelp.a | more
// 显示每个object的符号表
libyelp_la-yelp-bz2-decompressor.o:
         U BZ2_bzDecompress
         U BZ2_bzDecompressEnd
         U BZ2_bzDecompressInit
00000000 r __PRETTY_FUNCTION__.18991
         U dcgettext
         U g_assertion_message
         U g_atomic_pointer_get
         U g_converter_get_type
00000000 b g_define_type_id__volatile.18945
         U g_intern_static_string
         U g_io_error_quark
         U g_log
         U g_object_new
         U g_once_init_enter_impl
         U g_once_init_leave
         U g_set_error_literal
         U g_type_add_interface_static
         U g_type_check_class_cast
         U g_type_check_instance_cast
         U g_type_class_peek_parent
         U g_type_register_static_simple
00000030 t yelp_bz2_decompressor_class_intern_init
00000320 t yelp_bz2_decompressor_constructed
000001d0 t yelp_bz2_decompressor_convert
000003a0 t yelp_bz2_decompressor_finalize
00000070 T yelp_bz2_decompressor_get_type
00000010 t yelp_bz2_decompressor_iface_init
00000000 t yelp_bz2_decompressor_init
000003f0 T yelp_bz2_decompressor_new
00000004 b yelp_bz2_decompressor_parent_class
00000140 t yelp_bz2_decompressor_reset
// extract from archives
ar tv libyelp.a
// 输出静态库中的object文件
rw-r--r-- 0/0   2944 Apr 12 16:12 2011 libyelp_la-yelp-bookmarks.o
rw-r--r-- 0/0   2928 Apr 12 16:12 2011 libyelp_la-yelp-debug.o
rw-r--r-- 0/0   1152 Apr 12 16:12 2011 libyelp_la-yelp-error.o
rw-r--r-- 0/0  18592 Apr 12 16:12 2011 libyelp_la-yelp-docbook-document.o
rw-r--r-- 0/0  29716 Apr 12 16:12 2011 libyelp_la-yelp-document.o
rw-r--r-- 0/0  13340 Apr 12 16:12 2011 libyelp_la-yelp-help-list.o
rw-r--r-- 0/0   9012 Apr 12 16:12 2011 libyelp_la-yelp-info-document.o
rw-r--r-- 0/0  19244 Apr 12 16:12 2011 libyelp_la-yelp-info-parser.o
rw-r--r-- 0/0  28392 Apr 12 16:12 2011 libyelp_la-yelp-location-entry.o
rw-r--r-- 0/0   3120 Apr 12 16:12 2011 libyelp_la-yelp-magic-decompressor.o
rw-r--r-- 0/0  19928 Apr 12 16:12 2011 libyelp_la-yelp-mallard-document.o
rw-r--r-- 0/0  10192 Apr 12 16:12 2011 libyelp_la-yelp-man-document.o
rw-r--r-- 0/0  19176 Apr 12 16:12 2011 libyelp_la-yelp-man-parser.o
rw-r--r-- 0/0   1712 Apr 12 16:12 2011 libyelp_la-yelp-marshal.o
rw-r--r-- 0/0  21856 Apr 12 16:12 2011 libyelp_la-yelp-settings.o
rw-r--r-- 0/0   7616 Apr 12 16:12 2011 libyelp_la-yelp-simple-document.o
rw-r--r-- 0/0   8088 Apr 12 16:12 2011 libyelp_la-yelp-sqlite-storage.o
rw-r--r-- 0/0   3316 Apr 12 16:12 2011 libyelp_la-yelp-storage.o
rw-r--r-- 0/0  10152 Apr 12 16:12 2011 libyelp_la-yelp-transform.o
rw-r--r-- 0/0  21748 Apr 12 16:12 2011 libyelp_la-yelp-uri.o
rw-r--r-- 0/0   5372 Apr 12 16:12 2011 libyelp_la-yelp-types.o
rw-r--r-- 0/0  40872 Apr 12 16:12 2011 libyelp_la-yelp-view.o
rw-r--r-- 0/0   3972 Apr 12 16:12 2011 libyelp_la-yelp-lzma-decompressor.o
rw-r--r-- 0/0   3860 Apr 12 16:12 2011 libyelp_la-yelp-bz2-decompressor.o
// 抽取libyelp_la-yelp-bz2-decompressor.o
ar xv libpcap.a libyelp_la-yelp-bz2-decompressor.o
// list symbols from object file
nm -s libyelp_la-yelp-bz2-decompressor.o

 

// 显示如下
libyelp_la-yelp-bz2-decompressor.o:
         U BZ2_bzDecompress
         U BZ2_bzDecompressEnd
         U BZ2_bzDecompressInit
00000000 r __PRETTY_FUNCTION__.18991
         U dcgettext
         U g_assertion_message
         U g_atomic_pointer_get
         U g_converter_get_type
00000000 b g_define_type_id__volatile.18945
         U g_intern_static_string
         U g_io_error_quark
         U g_log
         U g_object_new
         U g_once_init_enter_impl
         U g_once_init_leave
         U g_set_error_literal
         U g_type_add_interface_static
         U g_type_check_class_cast
         U g_type_check_instance_cast
         U g_type_class_peek_parent
         U g_type_register_static_simple
00000030 t yelp_bz2_decompressor_class_intern_init
00000320 t yelp_bz2_decompressor_constructed
000001d0 t yelp_bz2_decompressor_convert
000003a0 t yelp_bz2_decompressor_finalize
00000070 T yelp_bz2_decompressor_get_type
00000010 t yelp_bz2_decompressor_iface_init
00000000 t yelp_bz2_decompressor_init
000003f0 T yelp_bz2_decompressor_new
00000004 b yelp_bz2_decompressor_parent_class
00000140 t yelp_bz2_decompressor_reset

关于nm -s的显示请自己man nm查看

 objdump命令的man手册

objdump - 显示二进制文件信息

objdump
      [-a] [-b bfdname |
      --target=bfdname] [-C] [--debugging]
      [-d] [-D]
      [--disassemble-zeroes]
      [-EB|-EL|--endian={big|little}] [-f]
      [-h] [-i|--info]
      [-j section | --section=section]
      [-l] [-m machine ] [--prefix-addresses]
      [-r] [-R]
      [-s|--full-contents] [-S|--source]
      [--[no-]show-raw-insn] [--stabs] [-t]
      [-T] [-x]
      [--start-address=address] [--stop-address=address]
      [--adjust-vma=offset] [--version] [--help]
      objfile...

--archive-headers
-a 显示档案库的成员信息,与 ar tv 类似

    objdump -a libpcap.a
    和 ar -tv libpcap.a 显示结果比较比较
    显然这个选项没有什么意思。

--adjust-vma=offset
    When  dumping  information, first add offset to all
    the section addresses.  This is useful if the  sec-
    tion  addresses  do  not correspond  to the symbol
    table, which can happen when  putting  sections  at
    particular  addresses when using a format which can
    not represent section addresses, such as a.out.

-b bfdname
--target=bfdname
    指定目标码格式。这不是必须的,objdump能自动识别许多格式,
    比如:objdump -b oasys -m vax -h fu.o
    显示fu.o的头部摘要信息,明确指出该文件是Vax系统下用Oasys
    编译器生成的目标文件。objdump -i将给出这里可以指定的
    目标码格式列表

--demangle
-C 将底层的符号名解码成用户级名字,除了去掉所有开头
   的下划线之外,还使得C++函数名以可理解的方式显示出来。

--debugging 
    显示调试信息。企图解析保存在文件中的调试信息并以C语言
    的语法显示出来。仅仅支持某些类型的调试信息。

--disassemble
-d 反汇编那些应该还有指令机器码的section

--disassemble-all
-D 与 -d 类似,但反汇编所有section

--prefix-addresses
    反汇编的时候,显示每一行的完整地址。这是一种比较老的反汇编格式。
    显示效果并不理想,但可能会用到其中的某些显示,自己可以对比。

--disassemble-zeroes
    一般反汇编输出将省略大块的零,该选项使得这些零块也被反汇编。

-EB
-EL
--endian={big|little}
    这个选项将影响反汇编出来的指令。
    little-endian就是我们当年在dos下玩汇编的时候常说的高位在高地址,
    x86都是这种。

--file-headers
-f 显示objfile中每个文件的整体头部摘要信息。

--section-headers
--headers
-h 显示目标文件各个section的头部摘要信息。

--help 简短的帮助信息。

--info
-i 显示对于 -b 或者 -m 选项可用的架构和目标格式列表。

--section=name
-j name 仅仅显示指定section的信息

--line-numbers
-l 用文件名和行号标注相应的目标代码,仅仅和-d、-D或者-r一起使用
   使用-ld和使用-d的区别不是很大,在源码级调试的时候有用,要求
   编译时使用了-g之类的调试编译选项。

--architecture=machine
-m machine
    指定反汇编目标文件时使用的架构,当待反汇编文件本身没有描述
    架构信息的时候(比如S-records),这个选项很有用。可以用-i选项
    列出这里能够指定的架构

--reloc
-r 显示文件的重定位入口。如果和-d或者-D一起使用,重定位部分以反汇
   编后的格式显示出来。

--dynamic-reloc
-R 显示文件的动态重定位入口,仅仅对于动态目标文件有意义,比如某些
   共享库。

--full-contents
-s 显示指定section的完整内容。

    objdump --section=.text -s inet.o | more

--source
-S 尽可能反汇编出源代码,尤其当编译的时候指定了-g这种调试参数时,
   效果比较明显。隐含了-d参数。

--show-raw-insn
    反汇编的时候,显示每条汇编指令对应的机器码,除非指定了
    --prefix-addresses,这将是缺省选项。

--no-show-raw-insn
    反汇编时,不显示汇编指令的机器码,这是指定 --prefix-addresses
    选项时的缺省设置。

--stabs
    Display the contents of the .stab, .stab.index, and
    .stab.excl sections from an ELF file.  This is only
    useful  on  systems  (such as Solaris 2.0) in which
    .stab debugging symbol-table entries are carried in
    an ELF section.  In most other file formats, debug-
    ging  symbol-table  entries  are interleaved  with
    linkage symbols, and are visible in the --syms output.

--start-address=address
    从指定地址开始显示数据,该选项影响-d、-r和-s选项的输出。

--stop-address=address
    显示数据直到指定地址为止,该选项影响-d、-r和-s选项的输出。

--syms
-t 显示文件的符号表入口。类似于nm -s提供的信息

--dynamic-syms
-T 显示文件的动态符号表入口,仅仅对动态目标文件有意义,比如某些
   共享库。它显示的信息类似于 nm -D|--dynamic 显示的信息。

--version 版本信息

    objdump --version

--all-headers
-x 显示所有可用的头信息,包括符号表、重定位入口。-x 等价于
   -a -f -h -r -t 同时指定。

    objdump -x inet.o

参看 nm(1)
举例:
//test.cpp
class Base{
public:
  Base();
  int a;
};
Base::Base(){
}
class Foo: public Base{
public:
  int val;
  Foo * pnext;
};
int main(){
  Foo myFoo;
  return 0;
}
//makefile
all:test.o
g++ test.o -o test
test.o:test.cpp
g++ -c test.cpp -o test.o
// 反汇编全部段
objdump -D test.o
test.o:     file format elf32-i386
Disassembly of section .group:
00000000 <_ZN3FooC5Ev>:
   0: 01 00                 add    %eax,(%eax)
   2: 00 00                 add    %al,(%eax)
   4: 06                   push   %es
   5: 00 00                 add    %al,(%eax)
...
Disassembly of section .text:
00000000 <_ZN4BaseC1Ev>:
   0: 55                   push   %ebp
   1: 89 e5                 mov    %esp,%ebp
   3: 5d                   pop    %ebp
   4: c3                   ret    
00000005 <main>:
   5: 55                   push   %ebp
   6: 89 e5                 mov    %esp,%ebp
   8: 83 e4 f0             and    $0xfffffff0,%esp
   b: 83 ec 20             sub    $0x20,%esp
   e: 8d 44 24 14           lea    0x14(%esp),%eax
  12: 89 04 24             mov    %eax,(%esp)
  15: e8 fc ff ff ff       call   16 <main+0x11>
  1a: b8 00 00 00 00       mov    $0x0,%eax
  1f: c9                   leave  
  20: c3                   ret    
Disassembly of section .text._ZN3FooC2Ev:
00000000 <_ZN3FooC1Ev>:
   0: 55                   push   %ebp
   1: 89 e5                 mov    %esp,%ebp
   3: 83 ec 04             sub    $0x4,%esp
   6: 8b 45 08             mov    0x8(%ebp),%eax
   9: 89 04 24             mov    %eax,(%esp)
   c: e8 fc ff ff ff       call   d <_ZN3FooC1Ev+0xd>
  11: c9                   leave  
  12: c3                   ret    
Disassembly of section .comment:
00000000 <.comment>:
   0: 00 47 43             add    %al,0x43(%edi)
   3: 43                   inc    %ebx
   4: 3a 20                 cmp    (%eax),%ah
   6: 28 55 62             sub    %dl,0x62(%ebp)
   9: 75 6e                 jne    79 <main+0x74>
   b: 74 75                 je     82 <main+0x7d>
   d: 2f                   das    
   e: 4c                   dec    %esp
   f: 69 6e 61 72 6f 20 34 imul   $0x34206f72,0x61(%esi),%ebp
  16: 2e                   cs
  17: 35 2e 32 2d 38       xor    $0x382d322e,%eax
  1c: 75 62                 jne    80 <main+0x7b>
  1e: 75 6e                 jne    8e <main+0x89>
  20: 74 75                 je     97 <main+0x92>
  22: 34 29                 xor    $0x29,%al
  24: 20 34 2e             and    %dh,(%esi,%ebp,1)
  27: 35                   .byte 0x35
  28: 2e 32 00             xor    %cs:(%eax),%al
Disassembly of section .eh_frame:
00000000 <.eh_frame>:
   0: 14 00                 adc    $0x0,%al
   2: 00 00                 add    %al,(%eax)
   4: 00 00                 add    %al,(%eax)
   6: 00 00                 add    %al,(%eax)
   8: 01 7a 52             add    %edi,0x52(%edx)
   b: 00 01                 add    %al,(%ecx)
   d: 7c 08                 jl     17 <.eh_frame+0x17>
   f: 01 1b                 add    %ebx,(%ebx)
  11: 0c 04                 or     $0x4,%al
  13: 04 88                 add    $0x88,%al
  15: 01 00                 add    %eax,(%eax)
  17: 00 1c 00             add    %bl,(%eax,%eax,1)
  1a: 00 00                 add    %al,(%eax)
  1c: 1c 00                 sbb    $0x0,%al
  1e: 00 00                 add    %al,(%eax)
  20: 00 00                 add    %al,(%eax)
  22: 00 00                 add    %al,(%eax)
  24: 05 00 00 00 00       add    $0x0,%eax
  29: 41                   inc    %ecx
  2a: 0e                   push   %cs
  2b: 08 42 85             or     %al,-0x7b(%edx)
  2e: 02 0d 05 41 c5 0c     add    0xcc54105,%cl
  34: 04 04                 add    $0x4,%al
  36: 00 00                 add    %al,(%eax)
  38: 1c 00                 sbb    $0x0,%al
  3a: 00 00                 add    %al,(%eax)
  3c: 3c 00                 cmp    $0x0,%al
  3e: 00 00                 add    %al,(%eax)
  40: 00 00                 add    %al,(%eax)
  42: 00 00                 add    %al,(%eax)
  44: 13 00                 adc    (%eax),%eax
  46: 00 00                 add    %al,(%eax)
  48: 00 41 0e             add    %al,0xe(%ecx)
  4b: 08 42 85             or     %al,-0x7b(%edx)
  4e: 02 0d 05 4f c5 0c     add    0xcc54f05,%cl
  54: 04 04                 add    $0x4,%al
  56: 00 00                 add    %al,(%eax)
  58: 1c 00                 sbb    $0x0,%al
  5a: 00 00                 add    %al,(%eax)
  5c: 5c                   pop    %esp
  5d: 00 00                 add    %al,(%eax)
  5f: 00 05 00 00 00 1c     add    %al,0x1c000000
  65: 00 00                 add    %al,(%eax)
  67: 00 00                 add    %al,(%eax)
  69: 41                   inc    %ecx
  6a: 0e                   push   %cs
  6b: 08 42 85             or     %al,-0x7b(%edx)
  6e: 02 0d 05 58 c5 0c     add    0xcc55805,%cl
  74: 04 04                 add    $0x4,%al
...
// 反汇编 ,并解析一部分symbol为正常序号
objdump -DC test.o
test.o:     file format elf32-i386
Disassembly of section .group:
00000000 <_ZN3FooC5Ev>:
   0: 01 00                 add    %eax,(%eax)
   2: 00 00                 add    %al,(%eax)
   4: 06                   push   %es
   5: 00 00                 add    %al,(%eax)
...
Disassembly of section .text:
00000000 <Base::Base()>:
   0: 55                   push   %ebp
   1: 89 e5                 mov    %esp,%ebp
   3: 5d                   pop    %ebp
   4: c3                   ret    
00000005 <main>:
   5: 55                   push   %ebp
   6: 89 e5                 mov    %esp,%ebp
   8: 83 e4 f0             and    $0xfffffff0,%esp
   b: 83 ec 20             sub    $0x20,%esp
   e: 8d 44 24 14           lea    0x14(%esp),%eax
  12: 89 04 24             mov    %eax,(%esp)
  15: e8 fc ff ff ff       call   16 <main+0x11>
  1a: b8 00 00 00 00       mov    $0x0,%eax
  1f: c9                   leave  
  20: c3                   ret    
Disassembly of section .text._ZN3FooC2Ev:
00000000 <Foo::Foo()>:
   0: 55                   push   %ebp
   1: 89 e5                 mov    %esp,%ebp
   3: 83 ec 04             sub    $0x4,%esp
   6: 8b 45 08             mov    0x8(%ebp),%eax
   9: 89 04 24             mov    %eax,(%esp)
   c: e8 fc ff ff ff       call   d <Foo::Foo()+0xd>
  11: c9                   leave  
  12: c3                   ret    
Disassembly of section .comment:
00000000 <.comment>:
   0: 00 47 43             add    %al,0x43(%edi)
   3: 43                   inc    %ebx
   4: 3a 20                 cmp    (%eax),%ah
   6: 28 55 62             sub    %dl,0x62(%ebp)
   9: 75 6e                 jne    79 <main+0x74>
   b: 74 75                 je     82 <main+0x7d>
   d: 2f                   das    
   e: 4c                   dec    %esp
   f: 69 6e 61 72 6f 20 34 imul   $0x34206f72,0x61(%esi),%ebp
  16: 2e                   cs
  17: 35 2e 32 2d 38       xor    $0x382d322e,%eax
  1c: 75 62                 jne    80 <main+0x7b>
  1e: 75 6e                 jne    8e <main+0x89>
  20: 74 75                 je     97 <main+0x92>
  22: 34 29                 xor    $0x29,%al
  24: 20 34 2e             and    %dh,(%esi,%ebp,1)
  27: 35                   .byte 0x35
  28: 2e 32 00             xor    %cs:(%eax),%al
Disassembly of section .eh_frame:
00000000 <.eh_frame>:
   0: 14 00                 adc    $0x0,%al
   2: 00 00                 add    %al,(%eax)
   4: 00 00                 add    %al,(%eax)
   6: 00 00                 add    %al,(%eax)
   8: 01 7a 52             add    %edi,0x52(%edx)
   b: 00 01                 add    %al,(%ecx)
   d: 7c 08                 jl     17 <.eh_frame+0x17>
   f: 01 1b                 add    %ebx,(%ebx)
  11: 0c 04                 or     $0x4,%al
  13: 04 88                 add    $0x88,%al
  15: 01 00                 add    %eax,(%eax)
  17: 00 1c 00             add    %bl,(%eax,%eax,1)
  1a: 00 00                 add    %al,(%eax)
  1c: 1c 00                 sbb    $0x0,%al
  1e: 00 00                 add    %al,(%eax)
  20: 00 00                 add    %al,(%eax)
  22: 00 00                 add    %al,(%eax)
  24: 05 00 00 00 00       add    $0x0,%eax
  29: 41                   inc    %ecx
  2a: 0e                   push   %cs
  2b: 08 42 85             or     %al,-0x7b(%edx)
  2e: 02 0d 05 41 c5 0c     add    0xcc54105,%cl
  34: 04 04                 add    $0x4,%al
  36: 00 00                 add    %al,(%eax)
  38: 1c 00                 sbb    $0x0,%al
  3a: 00 00                 add    %al,(%eax)
  3c: 3c 00                 cmp    $0x0,%al
  3e: 00 00                 add    %al,(%eax)
  40: 00 00                 add    %al,(%eax)
  42: 00 00                 add    %al,(%eax)
  44: 13 00                 adc    (%eax),%eax
  46: 00 00                 add    %al,(%eax)
  48: 00 41 0e             add    %al,0xe(%ecx)
  4b: 08 42 85             or     %al,-0x7b(%edx)
  4e: 02 0d 05 4f c5 0c     add    0xcc54f05,%cl
  54: 04 04                 add    $0x4,%al
  56: 00 00                 add    %al,(%eax)
  58: 1c 00                 sbb    $0x0,%al
  5a: 00 00                 add    %al,(%eax)
  5c: 5c                   pop    %esp
  5d: 00 00                 add    %al,(%eax)
  5f: 00 05 00 00 00 1c     add    %al,0x1c000000
  65: 00 00                 add    %al,(%eax)
  67: 00 00                 add    %al,(%eax)
  69: 41                   inc    %ecx
  6a: 0e                   push   %cs
  6b: 08 42 85             or     %al,-0x7b(%edx)
  6e: 02 0d 05 58 c5 0c     add    0xcc55805,%cl
  74: 04 04                 add    $0x4,%al
...
//反汇编代码段和解析一部分symbol为正常c语言
objdump -dC test.o
test.o:     file format elf32-i386
Disassembly of section .text:
00000000 <Base::Base()>:
   0: 55                   push   %ebp
   1: 89 e5                 mov    %esp,%ebp
   3: 5d                   pop    %ebp
   4: c3                   ret    
00000005 <main>:
   5: 55                   push   %ebp
   6: 89 e5                 mov    %esp,%ebp
   8: 83 e4 f0             and    $0xfffffff0,%esp
   b: 83 ec 20             sub    $0x20,%esp
   e: 8d 44 24 14           lea    0x14(%esp),%eax
  12: 89 04 24             mov    %eax,(%esp)
  15: e8 fc ff ff ff       call   16 <main+0x11>
  1a: b8 00 00 00 00       mov    $0x0,%eax
  1f: c9                   leave  
  20: c3                   ret    
Disassembly of section .text._ZN3FooC2Ev:
00000000 <Foo::Foo()>:
   0: 55                   push   %ebp
   1: 89 e5                 mov    %esp,%ebp
   3: 83 ec 04             sub    $0x4,%esp
   6: 8b 45 08             mov    0x8(%ebp),%eax
   9: 89 04 24             mov    %eax,(%esp)
   c: e8 fc ff ff ff       call   d <Foo::Foo()+0xd>
  11: c9                   leave  
  12: c3                   ret    
原创粉丝点击