1. 程式人生 > >binutils工具集之---objdump

binutils工具集之---objdump

objdump content clas 軟件開發 nbsp ont logs span hit

在嵌入式軟件開發中,有時需要知道所生成的程序文件中的段信息以分析問題,或者需要查看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.

binutils工具集之---objdump