生成目标文件
- test.cpp如下所示:
#include <iostream>
using namespace std;
int ga;
int gb = 100;
int main()
{
static int sa;
static int sb = 200;
int aa;
int ab = 300;
return 0;
}
- 使用如下命令生成test.o文件
g++ test.cpp -o test.o
查看目标文件
- 查看的基础知识:.rodata表示只读数据段,.comment表示注释信息段,每个段的第二行中的CONTENTS,ALLOC等表示段的各种属性。CONTENTS表示该段在文件中存在。LMA表示load memory address,即加载地址,VMA表示virtual memory address,即虚拟地址
// 可以查看该目标文件的信息
objdump -h test.o
test.o: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000007c 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000008 0000000000000000 0000000000000000 000000bc 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 0000000c 0000000000000000 0000000000000000 000000c4 2**2
ALLOC
3 .rodata 00000001 0000000000000000 0000000000000000 000000c4 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .init_array 00000008 0000000000000000 0000000000000000 000000c8 2**3
CONTENTS, ALLOC, LOAD, RELOC, DATA
5 .comment 0000002b 0000000000000000 0000000000000000 000000d0 2**0
CONTENTS, READONLY
6 .note.GNU-stack 00000000 0000000000000000 0000000000000000 000000fb 2**0
CONTENTS, READONLY
7 .note.gnu.property 00000020 0000000000000000 0000000000000000 00000100 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .eh_frame 00000078 0000000000000000 0000000000000000 00000120 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
查看.exe文件
// 查看.exe文件
objdump -h test
test: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .interp 0000001c 0000000000000318 0000000000000318 00000318 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
1 .note.gnu.property 00000020 0000000000000338 0000000000000338 00000338 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .note.gnu.build-id 00000024 0000000000000358 0000000000000358 00000358 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
3 .note.ABI-tag 00000020 000000000000037c 000000000000037c 0000037c 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .gnu.hash 00000024 00000000000003a0 00000000000003a0 000003a0 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .dynsym 000000d8 00000000000003c8 00000000000003c8 000003c8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
6 .dynstr 000000d5 00000000000004a0 00000000000004a0 000004a0 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
7 .gnu.version 00000012 0000000000000576 0000000000000576 00000576 2**1
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .gnu.version_r 00000040 0000000000000588 0000000000000588 00000588 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
9 .rela.dyn 000000f0 00000000000005c8 00000000000005c8 000005c8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
10 .rela.plt 00000030 00000000000006b8 00000000000006b8 000006b8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
11 .init 0000001b 0000000000001000 0000000000001000 00001000 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
12 .plt 00000030 0000000000001020 0000000000001020 00001020 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
13 .plt.got 00000010 0000000000001050 0000000000001050 00001050 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
14 .plt.sec 00000020 0000000000001060 0000000000001060 00001060 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
15 .text 000001e5 0000000000001080 0000000000001080 00001080 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
16 .fini 0000000d 0000000000001268 0000000000001268 00001268 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
17 .rodata 00000005 0000000000002000 0000000000002000 00002000 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
18 .eh_frame_hdr 00000054 0000000000002008 0000000000002008 00002008 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
19 .eh_frame 00000148 0000000000002060 0000000000002060 00002060 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
20 .init_array 00000010 0000000000003d90 0000000000003d90 00002d90 2**3
CONTENTS, ALLOC, LOAD, DATA
21 .fini_array 00000008 0000000000003da0 0000000000003da0 00002da0 2**3
CONTENTS, ALLOC, LOAD, DATA
22 .dynamic 00000200 0000000000003da8 0000000000003da8 00002da8 2**3
CONTENTS, ALLOC, LOAD, DATA
23 .got 00000058 0000000000003fa8 0000000000003fa8 00002fa8 2**3
CONTENTS, ALLOC, LOAD, DATA
24 .data 00000018 0000000000004000 0000000000004000 00003000 2**3
CONTENTS, ALLOC, LOAD, DATA
25 .bss 00000010 0000000000004018 0000000000004018 00003018 2**2
ALLOC
26 .comment 0000002a 0000000000000000 0000000000000000 00003018 2**0
CONTENTS, READONLY
查看ELF文件中的代码段和数据段,BSS段的长度
size test.o
1.代码段
// -s参数将各个段的内容以十六进制的方式打印
// -d将包含指令的段反汇编
objdump -s -d test.o
.text 000001e5 0000000000001080 0000000000001080 00001080 24
CONTENTS, ALLOC, LOAD, READONLY, CODE
.text 000001e5 0000000000001080 0000000000001080 00001080 24
CONTENTS, ALLOC, LOAD, READONLY, CODE