转载:objdump命令_Linux objdump 命令用法详解:显示二进制文件信息 (linuxde.net)

objdump命令

objdump命令是用查看目标文件或者可执行的目标文件的构成的gcc工具。

选项

  1. --archive-headers
  2. -a
  3. 显示档案库的成员信息,类似ls -llib*.a的信息列出。
  4.  
  5. -b bfdname
  6. --target=bfdname
  7. 指定目标码格式。这不是必须的,objdump能自动识别许多格式,比如:
  8.  
  9. objdump -b oasys -m vax -h fu.o
  10. 显示fu.o的头部摘要信息,明确指出该文件是Vax系统下用Oasys编译器生成的目标文件。objdump -i将给出这里可以指定的目标码格式列表。
  11.  
  12. -C
  13. --demangle
  14. 将底层的符号名解码成用户级名字,除了去掉所开头的下划线之外,还使得C++函数名以可理解的方式显示出来。
  15.  
  16. --debugging
  17. -g
  18. 显示调试信息。企图解析保存在文件中的调试信息并以C语言的语法显示出来。仅仅支持某些类型的调试信息。有些其他的格式被readelf -w支持。
  19.  
  20. -e
  21. --debugging-tags
  22. 类似-g选项,但是生成的信息是和ctags工具相兼容的格式。
  23.  
  24. --disassemble
  25. -d
  26. objfile中反汇编那些特定指令机器码的section
  27.  
  28. -D
  29. --disassemble-all
  30. -d 类似,但反汇编所有section.
  31.  
  32. --prefix-addresses
  33. 反汇编的时候,显示每一行的完整地址。这是一种比较老的反汇编格式。
  34.  
  35. -EB
  36. -EL
  37. --endian={big|little}
  38. 指定目标文件的小端。这个项将影响反汇编出来的指令。在反汇编的文件没描述小端信息的时候用。例如S-records.
  39.  
  40. -f
  41. --file-headers
  42. 显示objfile中每个文件的整体头部摘要信息。
  43.  
  44. -h
  45. --section-headers
  46. --headers
  47. 显示目标文件各个section的头部摘要信息。
  48.  
  49. -H
  50. --help
  51. 简短的帮助信息。
  52.  
  53. -i
  54. --info
  55. 显示对于 -b 或者 -m 选项可用的架构和目标格式列表。
  56.  
  57. -j name
  58. --section=name
  59. 仅仅显示指定名称为namesection的信息
  60.  
  61. -l
  62. --line-numbers
  63. 用文件名和行号标注相应的目标代码,仅仅和-d、-D或者-r一起使用使用-ld和使用-d的区别不是很大,在源码级调试的时候有用,要求编译时使用了-g之类的调试编译选项。
  64.  
  65. -m machine
  66. --architecture=machine
  67. 指定反汇编目标文件时使用的架构,当待反汇编文件本身没描述架构信息的时候(比如S-records),这个选项很有用。可以用-i选项列出这里能够指定的架构.
  68.  
  69. --reloc
  70. -r
  71. 显示文件的重定位入口。如果和-d或者-D一起使用,重定位部分以反汇编后的格式显示出来。
  72.  
  73. --dynamic-reloc
  74. -R
  75. 显示文件的动态重定位入口,仅仅对于动态目标文件意义,比如某些共享库。
  76.  
  77. -s
  78. --full-contents
  79. 显示指定section的完整内容。默认所有的非空section都会被显示。
  80.  
  81. -S
  82. --source
  83. 尽可能反汇编出源代码,尤其当编译的时候指定了-g这种调试参数时,效果比较明显。隐含了-d参数。
  84.  
  85. --show-raw-insn
  86. 反汇编的时候,显示每条汇编指令对应的机器码,如不指定--prefix-addresses,这将是缺省选项。
  87.  
  88. --no-show-raw-insn
  89. 反汇编时,不显示汇编指令的机器码,如不指定--prefix-addresses,这将是缺省选项。
  90.  
  91. --start-address=address
  92. 从指定地址开始显示数据,该选项影响-d、-r和-s选项的输出。
  93.  
  94. --stop-address=address
  95. 显示数据直到指定地址为止,该项影响-d、-r和-s选项的输出。
  96.  
  97. -t
  98. --syms
  99. 显示文件的符号表入口。类似于nm -s提供的信息
  100.  
  101. -T
  102. --dynamic-syms
  103. 显示文件的动态符号表入口,仅仅对动态目标文件意义,比如某些共享库。它显示的信息类似于 nm -D|--dynamic 显示的信息。
  104.  
  105. -V
  106. --version
  107. 版本信息
  108.  
  109. --all-headers
  110. -x
  111. 显示所可用的头信息,包括符号表、重定位入口。-x 等价于-a -f -h -r -t 同时指定。
  112.  
  113. -z
  114. --disassemble-zeroes
  115. 一般反汇编输出将省略大块的零,该选项使得这些零块也被反汇编。
  116.  
  117. @file 可以将选项集中到一个文件中,然后使用这个@file选项载入。

实例

首先,在给出后面大部分测试所基于的源代码以及编译指令。 源代码如下:

  1. [root@localhost test]# nl mytest.cpp
  1. void printTest() {
  2. char a;
  3. a = 'a';
  4. }
  5.  
  6. void printTest2() {
  7. int a = 2;
  8. a+=2;
  9. }

对以上源代码进行编译,如下:

  1. [root@localhost test]# g++ -c -g mytest.cpp

这里,生成的文件是mytest.o,为了方便测试包含了调试的信息,对可执行文件的测试,显示的结果类似。

查看当前使用的objdump的版本号: 

  1. [root@localhost test]# objdump -V
  2. GNU objdump 2.17.50.0.6-14.el5 20061020
  3. Copyright 2005 free Software Foundation, Inc.
  4. This program is free software; you may redistribute it under the terms of
  5. the GNU General Public License. This program has absolutely no warranty.

查看档案库文件中的信息: 

  1. [root@localhost test]# objdump -a libmy2.a
  2. In archive libmy2.a:
  3. myfile.o: file format elf32-i386
  4. rwxrwxrwx 0/0 2724 Nov 16 16:06 2009 myfile.o
  5. mytest.o: file format elf32-i386
  6. rw-r--r-- 0/0 727 Jul 13 15:32 2011 mytest.o

这里,libmy2.a是一个使用ar命令将多个*.o目标文件打包而生成的静态库。命令的输出类似ar -tv,相比较ar -tv输出如下: 

  1. [root@localhost test]# ar -tv libmy2.a
  2. rwxrwxrwx 0/0 2724 Nov 16 16:06 2009 myfile.o
  3. rw-r--r-- 0/0 727 Jul 13 15:32 2011 mytest.o

显示可用的架构和目标结构列表:

  1. [root@localhost test]# objdump -i
  2. BFD header file version 2.17.50.0.6-14.el5 20061020
  3. elf32-i386
  4. (header little endian, data little endian)
  5. i386
  6. a.out-i386-linux
  7. (header little endian, data little endian)
  8. i386
  9. efi-app-ia32
  10. (header little endian, data little endian)
  11. i386
  12. elf64-x86-64
  13. (header little endian, data little endian)
  14. i386
  15. elf64-little
  16. (header little endian, data little endian)
  17. i386
  18. elf64-big
  19. (header big endian, data big endian)
  20. i386
  21. elf32-little
  22. (header little endian, data little endian)
  23. i386
  24. elf32-big
  25. (header big endian, data big endian)
  26. i386
  27. srec
  28. (header endianness unknown, data endianness unknown)
  29. i386
  30. symbolsrec
  31. (header endianness unknown, data endianness unknown)
  32. i386
  33. tekhex
  34. (header endianness unknown, data endianness unknown)
  35. i386
  36. binary
  37. (header endianness unknown, data endianness unknown)
  38. i386
  39. ihex
  40. (header endianness unknown, data endianness unknown)
  41. i386
  42. trad-core
  43. (header endianness unknown, data endianness unknown)
  44.  
  45. elf32-i386 a.out-i386-linux efi-app-ia32 elf64-x86-64
  46. i386 elf32-i386 a.out-i386-linux efi-app-ia32 elf64-x86-64
  47.  
  48. elf64-little elf64-big elf32-little elf32-big srec symbolsrec
  49. i386 elf64-little elf64-big elf32-little elf32-big srec symbolsrec
  50.  
  51. tekhex binary ihex trad-core
  52. i386 tekhex binary ihex ---------

这里,显示的信息是相对于 -b 或者 -m 选项可用的架构和目标格式列表。

显示mytest.o文件中的text段的内容: 

  1. [root@localhost test]# objdump --section=.text -s mytest.o
  2. mytest.o: file format elf32-i386
  3. Contents of section .text:
  4. 0000 5589e583 ec10c645 ff61c9c3 5589e583 U......E.a..U...
  5. 0010 ec10c745 fc020000 008345fc 02c9c3 ...E......E....

这里注意,不能单独使用-j或者--section,例如objdump --section=.text mytest.o是不会运行成功的。

反汇编mytest.o中的text段内容,并尽可能用源代码形式表示: 

  1. [root@localhost test]# objdump -j .text -S mytest.o
  2. mytest.o: file format elf32-i386
  3. Disassembly of section .text:
  4. 00000000 <_Z9printTestv>:
  5. void printTest()
  6. 0: 55 push %ebp
  7. 1: 89 e5 mov %esp,%ebp
  8. 3: 83 ec 10 sub $0x10,%esp
  9. {
  10. char a;
  11. a = 'a';
  12. 6: c6 45 ff 61 movb $0x61,0xffffffff(%ebp)
  13. }
  14. a: c9 leave
  15. b: c3 ret
  16.  
  17. 000000c <_Z10printTest2v>:
  18. void printTest2()
  19. c: 55 push %ebp
  20. d: 89 e5 mov %esp,%ebp
  21. f: 83 ec 10 sub $0x10,%esp
  22. {
  23. int a = 2;
  24. 12: c7 45 fc 02 00 00 00 movl $0x2,0xfffffffc(%ebp)
  25. a+=2;
  26. 19: 83 45 fc 02 addl $0x2,0xfffffffc(%ebp)
  27. }
  28. 1d: c9 leave
  29. 1e: c3 ret

这里注意,不能单独使用-j或者--section,例如objdump -j .text mytest.o是不会运行成功的。另外-S命令对于包含调试信息的目标文件,显示的效果比较好,如果编译时没有指定g++的-g选项,那么目标文件就不包含调试信息,那么显示效果就差多了。

反汇编出mytest.o的源代码: 

  1. [root@localhost test]# objdump -S mytest.o
  2. mytest.o: file format elf32-i386
  3.  
  4. Disassembly of section .text:
  5.  
  6. 00000000 <_Z9printTestv>:
  7. void printTest()
  8. 0: 55 push %ebp
  9. 1: 89 e5 mov %esp,%ebp
  10. 3: 83 ec 10 sub $0x10,%esp
  11. {
  12. char a;
  13. a = 'a';
  14. 6: c6 45 ff 61 movb $0x61,0xffffffff(%ebp)
  15. }
  16. a: c9 leave
  17. b: c3 ret
  18.  
  19. 0000000c <_Z10printTest2v>:
  20. void printTest2()
  21. c: 55 push %ebp
  22. d: 89 e5 mov %esp,%ebp
  23. f: 83 ec 10 sub $0x10,%esp
  24. {
  25. int a = 2;
  26. 12: c7 45 fc 02 00 00 00 movl $0x2,0xfffffffc(%ebp)
  27. a+=2;
  28. 19: 83 45 fc 02 addl $0x2,0xfffffffc(%ebp)
  29. }
  30. 1d: c9 leave
  31. 1e: c3 ret

这里,尤其当编译的时候指定了-g这种调试参数时,反汇编的效果比较明显。隐含了-d参数。

显示文件的符号表入口: 

  1. [root@localhost test]# objdump -t mytest.o
  2. mytest.o: file format elf32-i386
  3.  
  4. SYMBOL TABLE:
  5. 00000000 l df *ABS* 00000000 mytest.cpp
  6. 00000000 l d .text 00000000 .text
  7. 00000000 l d .data 00000000 .data
  8. 00000000 l d .bss 00000000 .bss
  9. 00000000 l d .debug_abbrev 00000000 .debug_abbrev
  10. 00000000 l d .debug_info 00000000 .debug_info
  11. 00000000 l d .debug_line 00000000 .debug_line
  12. 00000000 l d .debug_frame 00000000 .debug_frame
  13. 00000000 l d .debug_loc 00000000 .debug_loc
  14. 00000000 l d .debug_pubnames 00000000 .debug_pubnames
  15. 00000000 l d .debug_aranges 00000000 .debug_aranges
  16. 00000000 l d .note.GNU-stack 00000000 .note.GNU-stack
  17. 00000000 l d .comment 00000000 .comment
  18. 00000000 g F .text 0000000c _Z9printTestv
  19. 00000000 *UND* 00000000 __gxx_personality_v0
  20. 0000000c g F .text 00000013 _Z10printTest2v

这里,输出的信息类似nm -s命令的输出,相比较之下,nm命令的输出如下:

  1. [root@localhost test]# nm -s mytest.o
  2. 0000000c T _Z10printTest2v
  3. 00000000 T _Z9printTestv
  4. U __gxx_personality_v0

显示文件的符号表入口,将底层符号解码并表示成用户级别: 

  1. [root@localhost test]# objdump -t -C mytest.o
  2. mytest.o: file format elf32-i386
  3. SYMBOL TABLE:
  4. 00000000 l df *ABS* 00000000 mytest.cpp
  5. 00000000 l d .text 00000000 .text
  6. 00000000 l d .data 00000000 .data
  7. 00000000 l d .bss 00000000 .bss
  8. 00000000 l d .debug_abbrev 00000000 .debug_abbrev
  9. 00000000 l d .debug_info 00000000 .debug_info
  10. 00000000 l d .debug_line 00000000 .debug_line
  11. 00000000 l d .debug_frame 00000000 .debug_frame
  12. 00000000 l d .debug_loc 00000000 .debug_loc
  13. 00000000 l d .debug_pubnames 00000000 .debug_pubnames
  14. 00000000 l d .debug_aranges 00000000 .debug_aranges
  15. 00000000 l d .note.GNU-stack 00000000 .note.GNU-stack
  16. 00000000 l d .comment 00000000 .comment
  17. 00000000 g F .text 0000000c printTest()
  18. 00000000 *UND* 00000000 __gxx_personality_v0
  19. 0000000c g F .text 00000013 printTest2()

这里,和没-C相比,printTest2函数可读性增加了。

反汇编目标文件的特定机器码段: 

  1. [root@localhost test]# objdump -d mytest.o
  2. mytest.o: file format elf32-i386
  3. Disassembly of section .text:
  4.  
  5. 00000000 <_Z9printTestv>:
  6. 0: 55 push %ebp
  7. 1: 89 e5 mov %esp,%ebp
  8. 3: 83 ec 10 sub $0x10,%esp
  9. 6: c6 45 ff 61 movb $0x61,0xffffffff(%ebp)
  10. a: c9 leave
  11. b: c3 ret
  12.  
  13. 0000000c <_Z10printTest2v>:
  14. c: 55 push %ebp
  15. d: 89 e5 mov %esp,%ebp
  16. f: 83 ec 10 sub $0x10,%esp
  17. 12: c7 45 fc 02 00 00 00 movl $0x2,0xfffffffc(%ebp)
  18. 19: 83 45 fc 02 addl $0x2,0xfffffffc(%ebp)
  19. 1d: c9 leave
  20. 1e: c3 ret

这里,对text段的内容进行了反汇编。

反汇编特定段,并将汇编代码对应的文件名称和行号对应上: 

  1. [root@localhost test]# objdump -d -l mytest.o
  2. mytest.o: file format elf32-i386
  3. Disassembly of section .text:
  4.  
  5. 00000000 <_Z9printTestv>:
  6. _Z9printTestv():
  7. /root/test/04_libraryTest/mytest.cpp:1
  8. 0: 55 push %ebp
  9. 1: 89 e5 mov %esp,%ebp
  10. 3: 83 ec 10 sub $0x10,%esp
  11. /root/test/04_libraryTest/mytest.cpp:4
  12. 6: c6 45 ff 61 movb $0x61,0xffffffff(%ebp)
  13. /root/test/04_libraryTest/mytest.cpp:5
  14. a: c9 leave
  15. b: c3 ret
  16.  
  17. 0000000c <_Z10printTest2v>:
  18. _Z10printTest2v():
  19. /root/test/04_libraryTest/mytest.cpp:6
  20. c: 55 push %ebp
  21. d: 89 e5 mov %esp,%ebp
  22. f: 83 ec 10 sub $0x10,%esp
  23. /root/test/04_libraryTest/mytest.cpp:8
  24. 12: c7 45 fc 02 00 00 00 movl $0x2,0xfffffffc(%ebp)
  25. /root/test/04_libraryTest/mytest.cpp:9
  26. 19: 83 45 fc 02 addl $0x2,0xfffffffc(%ebp)
  27. /root/test/04_libraryTest/mytest.cpp:10
  28. 1d: c9 leave
  29. 1e: c3 ret

这里,项"-d"从objfile中反汇编那些特定指令机器码的section,而使用"-l"指定用文件名和行号标注相应的目标代码,仅仅和-d、-D或者-r一起使用,使用-ld和使用-d的区别不是很大,在源码级调试的时候有用,要求编译时使用了-g之类的调试编译选项。

显示目标文件各个段的头部摘要信息: 

  1. [root@localhost test]# objdump -h mytest.o
  2. mytest.o: file format elf32-i386
  3.  
  4. Sections:
  5. Idx Name Size VMA LMA File off Algn
  6. 0 .text 0000001f 00000000 00000000 00000034 2**2
  7. CONTENTS, ALLOC, LOAD, readonly, CODE
  8. 1 .data 00000000 00000000 00000000 00000054 2**2
  9. CONTENTS, ALLOC, LOAD, DATA
  10. 2 .bss 00000000 00000000 00000000 00000054 2**2
  11. ALLOC
  12. 3 .debug_abbrev 00000046 00000000 00000000 00000054 2**0
  13. CONTENTS, READONLY, DEBUGGING
  14. 4 .debug_info 000000ed 00000000 00000000 0000009a 2**0
  15. CONTENTS, RELOC, READONLY, DEBUGGING
  16. 5 .debug_line 0000003e 00000000 00000000 00000187 2**0
  17. CONTENTS, RELOC, READONLY, DEBUGGING
  18. 6 .debug_frame 00000044 00000000 00000000 000001c8 2**2
  19. CONTENTS, RELOC, READONLY, DEBUGGING
  20. 7 .debug_loc 00000058 00000000 00000000 0000020c 2**0
  21. CONTENTS, READONLY, DEBUGGING
  22. 8 .debug_pubnames 0000002f 00000000 00000000 00000264 2**0
  23. CONTENTS, RELOC, READONLY, DEBUGGING
  24. 9 .debug_aranges 00000020 00000000 00000000 00000293 2**0
  25. CONTENTS, RELOC, READONLY, DEBUGGING
  26. 10 .comment 0000002e 00000000 00000000 000002b3 2**0
  27. CONTENTS, READONLY
  28. 11 .note.GNU-stack 00000000 00000000 00000000 000002e1 2**0
  29. CONTENTS, READONLY

这里,更多的内容参见man objdump中的这个选项。

objdump--反汇编查看的更多相关文章

  1. 使用objdump objcopy查看与修改符号表

    使用objdump objcopy查看与修改符号表动态库Linuxgccfunction    我们在 Linux 下运行一个程序,有时会无法启动,报缺少某某库.这时需要查看可执行程序或者动态库中的符 ...

  2. 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码

    浅析VS2010反汇编 2015年07月25日 21:53:11 阅读数:4374 第一篇 1. 如何进行反汇编 在调试的环境下,我们可以很方便地通过反汇编窗口查看程序生成的反汇编信息.如下图所示. ...

  3. gcc代码反汇编查看内存分布[1]: gcc

    # gcc -vgcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5) 重点: 代码中的内存分配, 地址从低到高: 代码段(RO, 保存函数代码) --&g ...

  4. gcc代码反汇编查看内存分布[2]: arm-linux-gcc

    arm-none-linux-gnueabi-gcc -v gcc version 4.4.1 (Sourcery G++ Lite 2010q1-202) 重点: 代码中的内存分配, 地址从低到高: ...

  5. dis反汇编查看实现

    dis库是python(默认的CPython)自带的一个库,可以用来分析字节码 >>> import dis >>> def add(a, b = 0): ... ...

  6. OBJ文件格式分析工具: objdump, nm,ar

    首先简要阐述关于gcc.glibc和 binutils模块之间的关系 一.关于gcc.glibc和binutils模块之间的关系 1.gcc(gnu collect compiler)是一组编译工具的 ...

  7. 绝对强大的三个linux指令: ar, nm, objdump

    前言如果普通编程不需要了解这些东西,如果想精确控制你的对象文件的格式或者你想查看一下文件对象里的内容以便作出某种判断,刚你可以看一下下面的工具:objdump, nm, ar.当然,本文不可能非常详细 ...

  8. 使用gcc不同选项来编译查看中间生成文件

    gcc编译C程序的总体流程如下图 用到的命令如下: .c---> .i gcc -E hello.c .c--->.s gcc -S hello.c .c--->.o gcc -c ...

  9. Linux内核调试方法总结之反汇编

    Linux反汇编调试方法 Linux内核模块或者应用程序经常因为各种各样的原因而崩溃,一般情况下都会打印函数调用栈信息,那么,这种情况下,我们怎么去定位问题呢?本文档介绍了一种反汇编的方法辅助定位此类 ...

  10. gdb查看虚函数表、函数地址

    1. 查看函数地址     看函数在代码的哪一行,使用info line就可以看到类似下面这中输出 点击(此处)折叠或打开 (gdb) info line a.cpp:10 Line 10 of &q ...

随机推荐

  1. 创建一个web项目

  2. expression select表达式动态构建

    参考: http://blog.csdn.net/tastelife/article/details/7340205 http://blog.csdn.net/sweety820/article/de ...

  3. freeswitch编译安装依赖

    ncurses:提供字符界面 zlib:数据压缩 libjpeg:JPEG图片格式数据的解码/编码/其他. lua:lua解释器 libedit:一种编辑操作的库,对一些可以交互操作的场景,或转为了自 ...

  4. Linux的bg和fg和jobs和nohup命令简单介绍

    我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命令,让我们轻松调度正在运行的 ...

  5. nohup命令的用法

    在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/mysql/bin/my ...

  6. 编译执行 VS 解释执行

    一般编译程序从对源程序执行途径的角度不同,可分为解释执行和编译执行. 所谓解释执行是借助于解释程序完成,即按源程序语句运行时的动态结构,直接逐句地边分析边翻译并执行.像自然语言翻译中的口译,随时进行翻 ...

  7. GCN数据集Cora、Citeseer、Pubmed文件分析

    简介 本文将对Cora.Citeseer.Pubmed 数据集进行详细介绍 Cora.Citeseer.Pubmed 数据集 来源 图 节点 边 特征 标签(y) Cora "Collect ...

  8. Docker系列(21)- DockerFile介绍

    DockerFile介绍 dockerfile是用来构建docker镜像的文件!命令参数脚本! 构建步骤 编写一个dockerfile文件 docker build构建成为一个镜像 docker ru ...

  9. centos7 .net core 使用supervisor守护进程后台运行

    安装supervisor yum install supervisor 配置supervisor vi /etc/supervisord.conf 拉到最后,这里的意思是 /etc/superviso ...

  10. 深入浅出WPF-06.Binding(绑定)01

    Binding(绑定) 先上图,再解释 针对这个图,我们先来说说什么是Binding. Binding就是一个桥梁,建立在数据和UI之间的桥梁.既然是数据驱动,那么我们就把数据称之为"源&q ...