objdump介紹
objdump 有點像那個快速查看之類的工具,就是以一種可閱讀的格式讓你更多地了解二進制文件可能帶有的附加信息。對於一般只想讓自己程序跑起來的程序員,這個命令沒有更多意義,對於想進一步了解系統的程序員,應該掌握這種工具。
objdump是gcc編譯器下一款反匯編工具,能夠反匯編目標文件、可執行文件。
基礎指令:
至少必須給出以下選項之一:
-a, --archive-headers Display archive header information
-f, --file-headers Display the contents of the overall file header
-p, --private-headers Display object format specific file header contents
-P, --private=OPT,OPT... Display object format specific contents
-h, --[section-]headers Display the contents of the section headers
-x, --all-headers Display the contents of all headers
**-d, --disassemble Display assembler contents of executable sections**
-D, --disassemble-all Display assembler contents of all sections
**-S, --source Intermix source code with disassembly**
-s, --full-contents Display the full contents of all sections requested
-g, --debugging Display debug information in object file
-e, --debugging-tags Display debug information using ctags style
-G, --stabs Display (in raw form) any STABS info in the file
-W[lLiaprmfFsoRt] or
--dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,
=frames-interp,=str,=loc,=Ranges,=pubtypes,
=gdb_index,=trace_info,=trace_abbrev,=trace_aranges,
=addr,=cu_index]
Display DWARF info in the file
-t, --syms Display the contents of the symbol table(s)
-T, --dynamic-syms Display the contents of the dynamic symbol table
-r, --reloc Display the relocation entries in the file
-R, --dynamic-reloc Display the dynamic relocation entries in the file
@<file> Read options from <file>
-v, --version Display this program's version number
-i, --info List object formats and architectures supported
-H, --help Display this information
指定反匯編格式:
objdump -S -d -M intel main.o
下列 i386/x86-64 特定的反匯編器選項在使用 **-M** 開關時可用(使用逗號分隔多個選項):
x86-64 Disassemble in 64bit mode
i386 Disassemble in 32bit mode
i8086 在 16 位模式下反匯編
att 用 AT&T 語法顯示指令
intel 用 Intel 語法顯示指令
在Window上
源c代碼
/* praise1.c -- 使用不同類型的字符串 */
#include <stdio.h>
#define PRAISE "You are an extraordinary being."
int main(void)
{
char name[40];
printf("What's your name? ");
scanf("%s", name);
printf("Hello, %s.%s\n", name, PRAISE);
return 0;
}
編譯階段 | 命令 | 截斷后的產物 |
---|---|---|
源程序 | ||
預處理 | gcc -E | 替換了宏的C源程序(沒有了#define,#include…), 刪除了注釋 |
編譯 | gcc -S | 匯編源程序 |
匯編 | gcc -c | 目標文件,二進制文件, 允許有不在此文件中的外部變量、函數 |
鏈接 | gcc | 可執行程序,一般由多個目標文件或庫鏈接而成, 二進制文件,所有變量、函數都必須找得到 |
在終端輸入
gcc -c -g -o hello hello.c
objdump -s -d hello > hello.s
-c
代表生成未鏈接的目標文件。 hello.s
代表匯編文件
在Linux上
gcc -g -o hello hello.c //生成可執行文件
objdump -s -d hello > hello.s
linux 下目標文件(默認擴展名是.o)和可執行文件都是 ELF 格式(文件內容按照一定格式進行組織)的二進制文件; 類似的,Windows 下 VISUAL C++ 編譯出來的目標文件 (擴展名是.obj)采用 COFF 格式,而可執行文件 (擴展名是.exe)采用 PE 格式, ELF 和 PE 都是從 COFF 發展而來的。
雖然生成的都是AT&T格式的匯編,但是不同平台下可以看出其中的部分寄存器和地址不同。