objdump命令


 

轉載於:http://man.linuxde.net/objdump       

objdump命令是用查看目標文件或者可執行的目標文件的構成的gcc工具。

選項

--archive-headers 
-a 
顯示檔案庫的成員信息,類似ls -l將lib*.a的信息列出。 

-b bfdname 
--target=bfdname 
指定目標碼格式。這不是必須的,objdump能自動識別許多格式,比如: 

objdump -b oasys -m vax -h fu.o 
顯示fu.o的頭部摘要信息,明確指出該文件是Vax系統下用Oasys編譯器生成的目標文件。objdump -i將給出這里可以指定的目標碼格式列表。 

-C 
--demangle 
將底層的符號名解碼成用戶級名字,除了去掉所開頭的下划線之外,還使得C++函數名以可理解的方式顯示出來。 

--debugging 
-g 
顯示調試信息。企圖解析保存在文件中的調試信息並以C語言的語法顯示出來。僅僅支持某些類型的調試信息。有些其他的格式被readelf -w支持。 -e --debugging-tags 類似-g選項,但是生成的信息是和ctags工具相兼容的格式。 --disassemble -d 從objfile中反匯編那些特定指令機器碼的section。 -D --disassemble-all 與 -d 類似,但反匯編所有section. --prefix-addresses 反匯編的時候,顯示每一行的完整地址。這是一種比較老的反匯編格式。 -EB -EL --endian={big|little} 指定目標文件的小端。這個項將影響反匯編出來的指令。在反匯編的文件沒描述小端信息的時候用。例如S-records. -f --file-headers 顯示objfile中每個文件的整體頭部摘要信息。 -h --section-headers --headers 顯示目標文件各個section的頭部摘要信息。 -H --help 簡短的幫助信息。 -i --info 顯示對於 -b 或者 -m 選項可用的架構和目標格式列表。 -j name --section=name 僅僅顯示指定名稱為name的section的信息 -l --line-numbers 用文件名和行號標注相應的目標代碼,僅僅和-d、-D或者-r一起使用使用-ld和使用-d的區別不是很大,在源碼級調試的時候有用,要求編譯時使用了-g之類的調試編譯選項。 -m machine --architecture=machine 指定反匯編目標文件時使用的架構,當待反匯編文件本身沒描述架構信息的時候(比如S-records),這個選項很有用。可以用-i選項列出這里能夠指定的架構. --reloc -r 顯示文件的重定位入口。如果和-d或者-D一起使用,重定位部分以反匯編后的格式顯示出來。 --dynamic-reloc -R 顯示文件的動態重定位入口,僅僅對於動態目標文件意義,比如某些共享庫。 -s --full-contents 顯示指定section的完整內容。默認所有的非空section都會被顯示。 -S --source 盡可能反匯編出源代碼,尤其當編譯的時候指定了-g這種調試參數時,效果比較明顯。隱含了-d參數。 --show-raw-insn 反匯編的時候,顯示每條匯編指令對應的機器碼,如不指定--prefix-addresses,這將是缺省選項。 --no-show-raw-insn 反匯編時,不顯示匯編指令的機器碼,如不指定--prefix-addresses,這將是缺省選項。 --start-address=address 從指定地址開始顯示數據,該選項影響-d、-r和-s選項的輸出。 --stop-address=address 顯示數據直到指定地址為止,該項影響-d、-r和-s選項的輸出。 -t --syms 顯示文件的符號表入口。類似於nm -s提供的信息 -T --dynamic-syms 顯示文件的動態符號表入口,僅僅對動態目標文件意義,比如某些共享庫。它顯示的信息類似於 nm -D|--dynamic 顯示的信息。 -V --version 版本信息 --all-headers -x 顯示所可用的頭信息,包括符號表、重定位入口。-x 等價於-a -f -h -r -t 同時指定。 -z --disassemble-zeroes 一般反匯編輸出將省略大塊的零,該選項使得這些零塊也被反匯編。 @file 可以將選項集中到一個文件中,然后使用這個@file選項載入。

實例

首先,在給出后面大部分測試所基於的源代碼以及編譯指令。 源代碼如下: 

[root@localhost test]# nl mytest.cpp 
void printTest() {
    char a;
    a = 'a';
}

void printTest2() {
int a = 2;
a+=2;
} 

對以上源代碼進行編譯,如下: 

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

這里,生成的文件是mytest.o,為了方便測試包含了調試的信息,對可執行文件的測試,顯示的結果類似。 

查看當前使用的objdump的版本號: 

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

查看檔案庫文件中的信息: 

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

這里,libmy2.a是一個使用ar命令將多個*.o目標文件打包而生成的靜態庫。命令的輸出類似ar -tv,相比較ar -tv輸出如下: 

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

顯示可用的架構和目標結構列表: 

[root@localhost test]# objdump -i 
BFD header file version 2.17.50.0.6-14.el5 20061020 
elf32-i386 
(header little endian, data little endian) 
  i386 
a.out-i386-linux 
(header little endian, data little endian) 
  i386 
efi-app-ia32 
(header little endian, data little endian) 
  i386 
elf64-x86-64 
(header little endian, data little endian) 
  i386 
elf64-little 
(header little endian, data little endian) 
  i386 
elf64-big 
(header big endian, data big endian) 
  i386 
elf32-little 
(header little endian, data little endian) 
  i386 
elf32-big 
(header big endian, data big endian) 
  i386 
srec 
(header endianness unknown, data endianness unknown) 
  i386 
symbolsrec 
(header endianness unknown, data endianness unknown) 
  i386 
tekhex 
(header endianness unknown, data endianness unknown) 
  i386 
binary 
(header endianness unknown, data endianness unknown) 
  i386 
ihex 
(header endianness unknown, data endianness unknown) 
  i386 
trad-core 
(header endianness unknown, data endianness unknown) 

              elf32-i386 a.out-i386-linux efi-app-ia32 elf64-x86-64 
          i386 elf32-i386 a.out-i386-linux efi-app-ia32 elf64-x86-64 

              elf64-little elf64-big elf32-little elf32-big srec symbolsrec 
          i386 elf64-little elf64-big elf32-little elf32-big srec symbolsrec 

              tekhex binary ihex trad-core 
          i386 tekhex binary ihex --------- 

這里,顯示的信息是相對於 -b 或者 -m 選項可用的架構和目標格式列表。 

顯示mytest.o文件中的text段的內容: 

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

這里注意,不能單獨使用-j或者--section,例如objdump --section=.text mytest.o是不會運行成功的。 

反匯編mytest.o中的text段內容,並盡可能用源代碼形式表示: 

[root@localhost test]# objdump -j .text -S mytest.o 
mytest.o:     file format elf32-i386 
Disassembly of section .text: 
00000000 <_Z9printTestv>: 
void printTest() 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
{ 
        char a; 
        a = 'a'; 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
} 
   a:   c9                      leave  
   b:   c3                      ret    

000000c <_Z10printTest2v>: 
void printTest2() 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
{ 
        int a = 2; 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
        a+=2; 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
} 
  1d:   c9                      leave  
  1e:   c3                      ret    

這里注意,不能單獨使用-j或者--section,例如objdump -j .text mytest.o是不會運行成功的。另外-S命令對於包含調試信息的目標文件,顯示的效果比較好,如果編譯時沒有指定g++的-g選項,那么目標文件就不包含調試信息,那么顯示效果就差多了。 

反匯編出mytest.o的源代碼: 

[root@localhost test]# objdump -S mytest.o 
mytest.o:     file format elf32-i386 

Disassembly of section .text: 

00000000 <_Z9printTestv>: 
void printTest() 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
{ 
        char a; 
        a = 'a'; 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
} 
   a:   c9                      leave  
   b:   c3                      ret    

0000000c <_Z10printTest2v>: 
void printTest2() 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
{ 
       int a = 2; 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
        a+=2; 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
} 
  1d:   c9                      leave  
  1e:   c3                      ret    

這里,尤其當編譯的時候指定了-g這種調試參數時,反匯編的效果比較明顯。隱含了-d參數。 

顯示文件的符號表入口: 

[root@localhost test]# objdump -t mytest.o 
mytest.o:     file format elf32-i386 

SYMBOL TABLE: 
00000000 l    df *ABS*  00000000 mytest.cpp 
00000000 l    d  .text  00000000 .text 
00000000 l    d  .data  00000000 .data 
00000000 l    d  .bss   00000000 .bss 
00000000 l    d  .debug_abbrev  00000000 .debug_abbrev 
00000000 l    d  .debug_info    00000000 .debug_info 
00000000 l    d  .debug_line    00000000 .debug_line 
00000000 l    d  .debug_frame   00000000 .debug_frame 
00000000 l    d  .debug_loc     00000000 .debug_loc 
00000000 l    d  .debug_pubnames        00000000 .debug_pubnames 
00000000 l    d  .debug_aranges 00000000 .debug_aranges 
00000000 l    d  .note.GNU-stack        00000000 .note.GNU-stack 
00000000 l    d  .comment       00000000 .comment 
00000000 g     F .text  0000000c _Z9printTestv 
00000000         *UND*  00000000 __gxx_personality_v0 
0000000c g     F .text  00000013 _Z10printTest2v 

這里,輸出的信息類似nm -s命令的輸出,相比較之下,nm命令的輸出如下: 

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

顯示文件的符號表入口,將底層符號解碼並表示成用戶級別: 

[root@localhost test]# objdump -t -C mytest.o 
mytest.o:     file format elf32-i386 
SYMBOL TABLE: 
00000000 l    df *ABS*  00000000 mytest.cpp 
00000000 l    d  .text  00000000 .text 
00000000 l    d  .data  00000000 .data 
00000000 l    d  .bss   00000000 .bss 
00000000 l    d  .debug_abbrev  00000000 .debug_abbrev 
00000000 l    d  .debug_info    00000000 .debug_info 
00000000 l    d  .debug_line    00000000 .debug_line 
00000000 l    d  .debug_frame   00000000 .debug_frame 
00000000 l    d  .debug_loc     00000000 .debug_loc 
00000000 l    d  .debug_pubnames        00000000 .debug_pubnames 
00000000 l    d  .debug_aranges 00000000 .debug_aranges 
00000000 l    d  .note.GNU-stack        00000000 .note.GNU-stack 
00000000 l    d  .comment       00000000 .comment 
00000000 g     F .text  0000000c printTest() 
00000000         *UND*  00000000 __gxx_personality_v0 
0000000c g     F .text  00000013 printTest2() 

這里,和沒-C相比,printTest2函數可讀性增加了。 

反匯編目標文件的特定機器碼段: 

[root@localhost test]# objdump -d mytest.o 
mytest.o:     file format elf32-i386 
Disassembly of section .text: 

00000000 <_Z9printTestv>: 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
   a:   c9                      leave  
  b:   c3                      ret    

0000000c <_Z10printTest2v>: 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
  1d:   c9                      leave  
  1e:   c3                      ret    

這里,對text段的內容進行了反匯編。 

反匯編特定段,並將匯編代碼對應的文件名稱和行號對應上: 

[root@localhost test]# objdump -d -l mytest.o
mytest.o:     file format elf32-i386 
Disassembly of section .text: 

00000000 <_Z9printTestv>: 
_Z9printTestv(): 
/root/test/04_libraryTest/mytest.cpp:1 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
/root/test/04_libraryTest/mytest.cpp:4 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
/root/test/04_libraryTest/mytest.cpp:5 
   a:   c9                      leave  
   b:   c3                      ret    

0000000c <_Z10printTest2v>: 
_Z10printTest2v(): 
/root/test/04_libraryTest/mytest.cpp:6 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
/root/test/04_libraryTest/mytest.cpp:8 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
/root/test/04_libraryTest/mytest.cpp:9 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
/root/test/04_libraryTest/mytest.cpp:10 
  1d:   c9                      leave  
  1e:   c3                      ret    

這里,項"-d"從objfile中反匯編那些特定指令機器碼的section,而使用"-l"指定用文件名和行號標注相應的目標代碼,僅僅和-d、-D或者-r一起使用,使用-ld和使用-d的區別不是很大,在源碼級調試的時候有用,要求編譯時使用了-g之類的調試編譯選項。 

顯示目標文件各個段的頭部摘要信息: 

[root@localhost test]# objdump -h mytest.o 
mytest.o:     file format elf32-i386 

Sections: 
Idx Name          Size      VMA       LMA       File off  Algn 
  0 .text         0000001f  00000000  00000000  00000034  2**2 
                  CONTENTS, ALLOC, LOAD, readonly, CODE 
  1 .data         00000000  00000000  00000000  00000054  2**2 
                  CONTENTS, ALLOC, LOAD, DATA 
  2 .bss          00000000  00000000  00000000  00000054  2**2 
                  ALLOC 
  3 .debug_abbrev 00000046  00000000  00000000  00000054  2**0 
                  CONTENTS, READONLY, DEBUGGING 
  4 .debug_info   000000ed  00000000  00000000  0000009a  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  5 .debug_line   0000003e  00000000  00000000  00000187  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  6 .debug_frame  00000044  00000000  00000000  000001c8  2**2 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  7 .debug_loc    00000058  00000000  00000000  0000020c  2**0 
                  CONTENTS, READONLY, DEBUGGING 
  8 .debug_pubnames 0000002f  00000000  00000000  00000264  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  9 .debug_aranges 00000020  00000000  00000000  00000293  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
10 .comment      0000002e  00000000  00000000  000002b3  2**0 
                  CONTENTS, READONLY 
11 .note.GNU-stack 00000000  00000000  00000000  000002e1  2**0 
                  CONTENTS, READONLY 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM