使用gcc自带的objdump工具实现反汇编


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格式的汇编,但是不同平台下可以看出其中的部分寄存器和地址不同。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM