Linux命令 objdump - 顯示二進(jìn)制文件信息

2022-03-16 18:00 更新

objdump

顯示二進(jìn)制文件信息

補(bǔ)充說明

objdump命令 是用查看目標(biāo)文件或者可執(zhí)行的目標(biāo)文件的構(gòu)成的gcc工具。

選項(xiàng)

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

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

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

-C --demangle 
# 將底層的符號名解碼成用戶級名字,除了去掉所開頭的下劃線之外,還使得C++函數(shù)名以可理解的方式顯示出來。 
--debugging 
-g 
# 顯示調(diào)試信息。企圖解析保存在文件中的調(diào)試信息并以C語言的語法顯示出來。僅僅支持某些類型的調(diào)試信息。有些其他的格式被readelf -w支持。 

-e --debugging-tags 
# 類似-g選項(xiàng),但是生成的信息是和ctags工具相兼容的格式。 
--disassemble 
-d 
# 從objfile中反匯編那些特定指令機(jī)器碼的section。 

-D --disassemble-all 
# 與 -d 類似,但反匯編所有section. 

--prefix-addresses 
# 反匯編的時候,顯示每一行的完整地址。這是一種比較老的反匯編格式。 

-EB 
-EL 
--endian={big|little} 
# 指定目標(biāo)文件的小端。這個項(xiàng)將影響反匯編出來的指令。在反匯編的文件沒描述小端信息的時候用。例如S-records. 

-f 
--file-headers 
顯示objfile中每個文件的整體頭部摘要信息。 

-h 
--section-headers 
--headers 
顯示目標(biāo)文件各個section的頭部摘要信息。 

-H 
--help 
簡短的幫助信息。 

-i 
--info 
顯示對于 -b 或者 -m 選項(xiàng)可用的架構(gòu)和目標(biāo)格式列表。 

-j name
--section=name 
僅僅顯示指定名稱為name的section的信息 

-l
--line-numbers 
用文件名和行號標(biāo)注相應(yīng)的目標(biāo)代碼,僅僅和-d、-D或者-r一起使用使用-ld和使用-d的區(qū)別不是很大,在源碼級調(diào)試的時候有用,要求編譯時使用了-g之類的調(diào)試編譯選項(xiàng)。 

-m machine 
--architecture=machine 
指定反匯編目標(biāo)文件時使用的架構(gòu),當(dāng)待反匯編文件本身沒描述架構(gòu)信息的時候(比如S-records),這個選項(xiàng)很有用??梢杂?i選項(xiàng)列出這里能夠指定的架構(gòu). 

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

--dynamic-reloc 
-R 
顯示文件的動態(tài)重定位入口,僅僅對于動態(tài)目標(biāo)文件意義,比如某些共享庫。 

-s 
--full-contents 
顯示指定section的完整內(nèi)容。默認(rèn)所有的非空section都會被顯示。 

-S 
--source 
盡可能反匯編出源代碼,尤其當(dāng)編譯的時候指定了-g這種調(diào)試參數(shù)時,效果比較明顯。隱含了-d參數(shù)。 

--show-raw-insn 
反匯編的時候,顯示每條匯編指令對應(yīng)的機(jī)器碼,如不指定--prefix-addresses,這將是缺省選項(xiàng)。 

--no-show-raw-insn 
反匯編時,不顯示匯編指令的機(jī)器碼,如不指定--prefix-addresses,這將是缺省選項(xiàng)。 

--start-address=address 
從指定地址開始顯示數(shù)據(jù),該選項(xiàng)影響-d、-r和-s選項(xiàng)的輸出。 

--stop-address=address 
顯示數(shù)據(jù)直到指定地址為止,該項(xiàng)影響-d、-r和-s選項(xiàng)的輸出。 

-t 
--syms 
顯示文件的符號表入口。類似于nm -s提供的信息 

-T 
--dynamic-syms 
顯示文件的動態(tài)符號表入口,僅僅對動態(tài)目標(biāo)文件意義,比如某些共享庫。它顯示的信息類似于 nm -D|--dynamic 顯示的信息。 

-V 
--version 
版本信息 

--all-headers 
-x 
顯示所可用的頭信息,包括符號表、重定位入口。-x 等價于-a -f -h -r -t 同時指定。 

-z 
--disassemble-zeroes 
一般反匯編輸出將省略大塊的零,該選項(xiàng)使得這些零塊也被反匯編。 

@file 可以將選項(xiàng)集中到一個文件中,然后使用這個@file選項(xiàng)載入。

實(shí)例

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

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

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

對以上源代碼進(jìn)行編譯,如下: 

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

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

**查看當(dāng)前使用的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目標(biāo)文件打包而生成的靜態(tài)庫。命令的輸出類似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 

顯示可用的架構(gòu)和目標(biāo)結(jié)構(gòu)列表: 

[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 選項(xiàng)可用的架構(gòu)和目標(biāo)格式列表。 

**顯示mytest.o文件中的text段的內(nèi)容: **

[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.... 

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

**反匯編mytest.o中的text段內(nèi)容,并盡可能用源代碼形式表示: **

[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    

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

**反匯編出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    

這里,尤其當(dāng)編譯的時候指定了-g這種調(diào)試參數(shù)時,反匯編的效果比較明顯。隱含了-d參數(shù)。 

**顯示文件的符號表入口: **

[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函數(shù)可讀性增加了。 

**反匯編目標(biāo)文件的特定機(jī)器碼段: **

[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段的內(nèi)容進(jìn)行了反匯編。 

**反匯編特定段,并將匯編代碼對應(yīng)的文件名稱和行號對應(yīng)上: **

[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    

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

**顯示目標(biāo)文件各個段的頭部摘要信息: **

[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 

這里,更多的內(nèi)容參見man objdump中的這個選項(xiàng)。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號