在嵌入式软件开发中,有时需要知道所生成的程序文件中的段信息以分析问题,或者需要查看c语言对应的汇编代码,此时,objdump工具就可以帮大忙了。obj——object dump:转储。
#include<stdio.h> #include<time.h> int global1; int global2=3; static int static_global1; static int static_global2=3; void foo() { static int internal1; static int internal2=3; time(0); } static void bar() { } int main(void) { int local1; int local2=3; foo(); return 0; }
采用 -d选项,可以查看程序文件的汇编代码:
在使用-d进行反汇编时,另一个很有用的选项就是-S(大写),它的作用是告诉objdump在反汇编时同时显示汇编代码对应的c/c++源程序。
想查看对应反汇编,一定要在编译的时候加上-g选项生成debug信息,否则不会成功:
-f选项可以显示程序文件的头信息。
objdump另一个非常有用的选项是-s(小写),将它与-j参数配合使用,能查看某一个段中的具体内容。
更多指令参考man或者info介绍:
-s
--full-contents
Display the full contents of any sections requested. By default
all non-empty sections are displayed.
-S
--source
Display source code intermixed with disassembly, if possible.//这下知道为什么刚才说的要加上-g生成调试信息了把,-S并不是强制显示程序文件,而是
Implies -d. //在可能的情况下,进行显示,所以要想可能,就加上-g生成调试信息。
-d
--disassemble
Display the assembler mnemonics for the machine instructions from
objfile. This option only disassembles those sections which are
expected to contain instructions.
-D
--disassemble-all
Like -d, but disassemble the contents of all sections, not just
those expected to contain instructions.
This option also has a subtle effect on the disassembly of
instructions in code sections. When option -d is in effect objdump
will assume that any symbols present in a code section occur on the
boundary between instructions and it will refuse to disassemble
across such a boundary. When option -D is in effect however this
assumption is supressed. This means that it is possible for the
output of -d and -D to differ if, for example, data is stored in
code sections.
If the target is an ARM architecture this switch also has the
effect of forcing the disassembler to decode pieces of data found
in code sections as if they were instructions.