传送门
objdump——目标文件信息揭秘
objdump是查看目标文件或者可执行的文件信息。
准备环境
测试文件test1.cpp
int test1_func1()
{
return 11;
}
int test_func2()
{
return 12;
}
测试文件test2.cpp
int test2_func1()
{
return 21;
}
int test2_func2()
{
return 22;
}
编译
[frank@LAPTOP-0OCJTGJR objdump]$ g++ -c -g test1.cpp test2.cpp
[frank@LAPTOP-0OCJTGJR objdump]$ ll
total 8
-rw-rw-r-- 1 frank frank 67 Feb 10 21:07 test1.cpp
-rw-rw-r-- 1 frank frank 2832 Feb 10 21:13 test1.o
-rw-rw-r-- 1 frank frank 68 Feb 10 21:07 test2.cpp
-rw-rw-r-- 1 frank frank 2840 Feb 10 21:13 test2.o
ar -r libtest.a test1.o test2.o
[frank@LAPTOP-0OCJTGJR objdump]$ ar -r libtest.a test1.o test2.o
ar: creating libtest.a
[frank@LAPTOP-0OCJTGJR objdump]$ ll
total 12
-rw-rw-r-- 1 frank frank 5948 Feb 10 21:15 libtest.a
-rw-rw-r-- 1 frank frank 67 Feb 10 21:07 test1.cpp
-rw-rw-r-- 1 frank frank 2832 Feb 10 21:13 test1.o
-rw-rw-r-- 1 frank frank 68 Feb 10 21:07 test2.cpp
-rw-rw-r-- 1 frank frank 2840 Feb 10 21:13 test2.o
可以看到生成了test1.cpp
,test2.cpp
,libtest.a
,三个文件。g++ -c -g test1.cpp test2.cpp
这里-g
是要生成调试信息。
objdump使用
查看当前使用的objdump的版本
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -v
GNU objdump version 2.27-41.base.el7_7.2
Copyright (C) 2016 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.
查看静态库库文件中的信息
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -a libtest.a
In archive libtest.a:
test1.o: file format elf64-x86-64
rw-rw-r-- 1000/1000 2832 Feb 10 21:13 2020 test1.o
test2.o: file format elf64-x86-64
rw-rw-r-- 1000/1000 2840 Feb 10 21:13 2020 test2.o
上面操作可以使用ar -tv
命令代替,效果相似。
[frank@LAPTOP-0OCJTGJR objdump]$ ar -tv libtest.a
rw-rw-r-- 1000/1000 2832 Feb 10 21:13 2020 test1.o
rw-rw-r-- 1000/1000 2840 Feb 10 21:13 2020 test2.o
显示可用的架构和目标结构列表
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -i
BFD header file version version 2.27-41.base.el7_7.2
elf64-x86-64
(header little endian, data little endian)
i386
elf32-i386
(header little endian, data little endian)
i386
...
...
反汇编
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -S test1.o
test1.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z11test1_func1v>:
int test1_func1()
{
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
return 11;
4: b8 0b 00 00 00 mov $0xb,%eax
}
9: 5d pop %rbp
a: c3 retq
000000000000000b <_Z10test_func2v>:
int test_func2()
{
b: 55 push %rbp
c: 48 89 e5 mov %rsp,%rbp
return 12;
f: b8 0c 00 00 00 mov $0xc,%eax
}
14: 5d pop %rbp
15: c3 retq
不加-g
,没有调试信息的反汇编效果
[frank@LAPTOP-0OCJTGJR objdump]$ rm -rf test2.o
[frank@LAPTOP-0OCJTGJR objdump]$ gcc -c test2.cpp
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -S test2.o
test2.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z11test2_func1v>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: b8 15 00 00 00 mov $0x15,%eax
9: 5d pop %rbp
a: c3 retq
000000000000000b <_Z11test2_func2v>:
b: 55 push %rbp
c: 48 89 e5 mov %rsp,%rbp
f: b8 16 00 00 00 mov $0x16,%eax
14: 5d pop %rbp
15: c3 retq
显示文件的符号表入口
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -t -C test1.o
test1.o: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 test1.cpp
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .debug_info 0000000000000000 .debug_info
0000000000000000 l d .debug_abbrev 0000000000000000 .debug_abbrev
0000000000000000 l d .debug_aranges 0000000000000000 .debug_aranges
0000000000000000 l d .debug_line 0000000000000000 .debug_line
0000000000000000 l d .debug_str 0000000000000000 .debug_str
0000000000000000 l d .note.GNU-stack 0000000000000000 .note.GNU-stack
0000000000000000 l d .eh_frame 0000000000000000 .eh_frame
0000000000000000 l d .comment 0000000000000000 .comment
0000000000000000 g F .text 000000000000000b _Z11test1_func1v
000000000000000b g F .text 000000000000000b _Z10test_func2v
这里,输出的信息类似nm -s
命令的输出,另外-C
参数增强可读性。
[frank@LAPTOP-0OCJTGJR objdump]$ nm -s test1.o
000000000000000b T _Z10test_func2v
0000000000000000 T _Z11test1_func1v
反汇编目标文件的特定机器码段
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -d test1.o
test1.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z11test1_func1v>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: b8 0b 00 00 00 mov $0xb,%eax
9: 5d pop %rbp
a: c3 retq
000000000000000b <_Z10test_func2v>:
b: 55 push %rbp
c: 48 89 e5 mov %rsp,%rbp
f: b8 0c 00 00 00 mov $0xc,%eax
14: 5d pop %rbp
15: c3 retq
这里与不带编译信息-g
的目标文件反汇编出来的效果是一样的参见objdump -S test2.o
。
反汇编对应行号
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -S -l test1.o
test1.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z11test1_func1v>:
_Z11test1_func1v():
/home/frank/C++/objdump/test1.cpp:2
int test1_func1()
{
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
/home/frank/C++/objdump/test1.cpp:3
return 11;
4: b8 0b 00 00 00 mov $0xb,%eax
/home/frank/C++/objdump/test1.cpp:4
}
9: 5d pop %rbp
a: c3 retq
000000000000000b <_Z10test_func2v>:
_Z10test_func2v():
/home/frank/C++/objdump/test1.cpp:6
int test_func2()
{
b: 55 push %rbp
c: 48 89 e5 mov %rsp,%rbp
/home/frank/C++/objdump/test1.cpp:7
return 12;
f: b8 0c 00 00 00 mov $0xc,%eax
/home/frank/C++/objdump/test1.cpp:8
}
14: 5d pop %rbp
15: c3 retq
可以看到,使用-l
选项好可以对应的源文件中的行号。
头部摘要
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -h test1.o
test1.o: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000016 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000000 0000000000000000 0000000000000000 00000056 2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 0000000000000000 0000000000000000 00000056 2**0
ALLOC
3 .debug_info 00000079 0000000000000000 0000000000000000 00000056 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
4 .debug_abbrev 0000003b 0000000000000000 0000000000000000 000000cf 2**0
CONTENTS, READONLY, DEBUGGING
5 .debug_aranges 00000030 0000000000000000 0000000000000000 0000010a 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
6 .debug_line 0000004a 0000000000000000 0000000000000000 0000013a 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
7 .debug_str 0000008a 0000000000000000 0000000000000000 00000184 2**0
CONTENTS, READONLY, DEBUGGING
8 .comment 00000012 0000000000000000 0000000000000000 0000020e 2**0
CONTENTS, READONLY
9 .note.GNU-stack 00000000 0000000000000000 0000000000000000 00000220 2**0
CONTENTS, READONLY
10 .eh_frame 00000058 0000000000000000 0000000000000000 00000220 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
[frank@LAPTOP-0OCJTGJR objdump]$
[frank@LAPTOP-0OCJTGJR objdump]$
[frank@LAPTOP-0OCJTGJR objdump]$
[frank@LAPTOP-0OCJTGJR objdump]$ objdump -h test2.o
test2.o: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000016 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000000 0000000000000000 0000000000000000 00000056 2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 0000000000000000 0000000000000000 00000056 2**0
ALLOC
3 .comment 00000012 0000000000000000 0000000000000000 00000056 2**0
CONTENTS, READONLY
4 .note.GNU-stack 00000000 0000000000000000 0000000000000000 00000068 2**0
CONTENTS, READONLY
5 .eh_frame 00000058 0000000000000000 0000000000000000 00000068 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
对比test1.o和test2.o(test2.o没有编译信息-g
),test2.o头部摘要没有调试信息。
选项
--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选项载入。