挖掘 SimpleSection.o
概述
目标文件包含代码和数据。程序源代码编译后的机器指令通常被放在代码段中,常见的代码段有 .code 或 .text。已初始化全局变量和局部静态变量通常放在数据段 .data 中,未初始化的全局变量和局部静态变量一般放在 .bss 段中。
编译目标文件
使用 gcc 编译但不链接,生成目标文件:
gcc -c SimpleSection.c
其中 -c 表示只编译不链接,输出 SimpleSection.o 文件。
查看目标文件结构
1. 查看段的基本信息
使用 objdump -h 参数可以查看目标文件的内部结构(段的基本信息):
objdump -h SimpleSection.o
输出结果:
SimpleSection.o: file format pe-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000060 0000000000000000 0000000000000000 0000012c 2**4
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000010 0000000000000000 0000000000000000 0000018c 2**4
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000010 0000000000000000 0000000000000000 00000000 2**4
ALLOC
3 .rdata 00000010 0000000000000000 0000000000000000 0000019c 2**4
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .xdata 00000018 0000000000000000 0000000000000000 000001ac 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .pdata 00000018 0000000000000000 0000000000000000 000001c4 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
6 .rdata$zzz 00000020 0000000000000000 0000000000000000 000001dc 2**4
CONTENTS, ALLOC, LOAD, READONLY, DATA
2. 查看完整信息
使用 -x 参数可以输出更多信息:
objdump -x SimpleSection.o
输出结果:
SimpleSection.o: file format pe-x86-64
SimpleSection.o
architecture: i386:x86-64, flags 0x00000039:
HAS_RELOC, HAS_DEBUG, HAS_SYMS, HAS_LOCALS
start address 0x0000000000000000
Characteristics 0x4
line numbers stripped
Time/DateThu Jan 1 08:00:00 1970
Magic0000
MajorLinkerVersion0
MinorLinkerVersion0
SizeOfCode0000000000000000
SizeOfInitializedData0000000000000000
SizeOfUninitializedData0000000000000000
AddressOfEntryPoint0000000000000000
BaseOfCode0000000000000000
ImageBase0000000000000000
SectionAlignment00000000
FileAlignment00000000
MajorOSystemVersion0
MinorOSystemVersion0
MajorImageVersion0
MinorImageVersion0
MajorSubsystemVersion0
MinorSubsystemVersion0
Win32Version00000000
SizeOfImage00000000
SizeOfHeaders00000000
CheckSum00000000
Subsystem00000000(unspecified)
DllCharacteristics00000000
SizeOfStackReserve0000000000000000
SizeOfStackCommit0000000000000000
SizeOfHeapReserve0000000000000000
SizeOfHeapCommit0000000000000000
LoaderFlags00000000
NumberOfRvaAndSizes00000000
The Data Directory
Entry 0 0000000000000000 00000000 Export Directory [.edata (or where ever we found it)]
Entry 1 0000000000000000 00000000 Import Directory [parts of .idata]
Entry 2 0000000000000000 00000000 Resource Directory [.rsrc]
Entry 3 0000000000000000 00000000 Exception Directory [.pdata]
Entry 4 0000000000000000 00000000 Security Directory
Entry 5 0000000000000000 00000000 Base Relocation Directory [.reloc]
Entry 6 0000000000000000 00000000 Debug Directory
Entry 7 0000000000000000 00000000 Description Directory
Entry 8 0000000000000000 00000000 Special Directory
Entry 9 0000000000000000 00000000 Thread Storage Directory [.tls]
Entry a 0000000000000000 00000000 Load Configuration Directory
Entry b 0000000000000000 00000000 Bound Import Directory
Entry c 0000000000000000 00000000 Import Address Table Directory
Entry d 0000000000000000 00000000 Delay Import Directory
Entry e 0000000000000000 00000000 CLR Runtime Header
Entry f 0000000000000000 00000000 Reserved
The Function Table (interpreted .pdata section contents)
vma:BeginAddress EndAddress UnwindData
0000000000000000:0000000000000000 0000000000000023 0000000000000000
000000000000000c:0000000000000023 000000000000005f 000000000000000c
Dump of .xdata
0000000000000000 (rva: 00000000): 0000000000000000 – 0000000000000023
Version: 1, Flags: none
Nbr codes: 3, Prologue size: 0x08, Frame offset: 0x0, Frame reg: rbp
pc+0x08: alloc small area: rsp = rsp – 0x20
pc+0x04: FPReg: rbp = rsp + 0x0 (info = 0x0)
pc+0x01: push rbp
000000000000000c (rva: 0000000c): 0000000000000023 – 000000000000005f
Version: 1, Flags: none
Nbr codes: 3, Prologue size: 0x08, Frame offset: 0x0, Frame reg: rbp
pc+0x08: alloc small area: rsp = rsp – 0x30
pc+0x04: FPReg: rbp = rsp + 0x0 (info = 0x0)
pc+0x01: push rbp
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000060 0000000000000000 0000000000000000 0000012c 2**4
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000010 0000000000000000 0000000000000000 0000018c 2**4
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000010 0000000000000000 0000000000000000 00000000 2**4
ALLOC
3 .rdata 00000010 0000000000000000 0000000000000000 0000019c 2**4
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .xdata 00000018 0000000000000000 0000000000000000 000001ac 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
5 .pdata 00000018 0000000000000000 0000000000000000 000001c4 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
6 .rdata$zzz 00000020 0000000000000000 0000000000000000 000001dc 2**4
CONTENTS, ALLOC, LOAD, READONLY, DATA
SYMBOL TABLE:
[ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x0000000000000000 SimpleSection.
File
[ 2](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x0000000000000000 func1
AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 4
[ 4](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x0000000000000023 main
AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0
[ 6](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x0000000000000004 static_var.1
[ 7](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x0000000000000004 static_var2.0
[ 8](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .text
AUX scnlen 0x5f nreloc 5 nlnno 0
[ 10](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .data
AUX scnlen 0x8 nreloc 0 nlnno 0
[ 12](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .bss
AUX scnlen 0x8 nreloc 0 nlnno 0
[ 14](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .rdata
AUX scnlen 0x3 nreloc 0 nlnno 0
[ 16](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .xdata
AUX scnlen 0x18 nreloc 0 nlnno 0
[ 18](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .pdata
AUX scnlen 0x18 nreloc 6 nlnno 0
[ 20](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .rdata$zzz
AUX scnlen 0x12 nreloc 0 nlnno 0
[ 22](sec 2)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x0000000000000000 global_init_var
[ 23](sec 3)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x0000000000000000 global_uninit_var
[ 24](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000000000000000 __main
[ 25](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000000000000000 printf
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
0000000000000011 IMAGE_REL_AMD64_REL32 .rdata
0000000000000018 IMAGE_REL_AMD64_REL32 printf
000000000000002c IMAGE_REL_AMD64_REL32 __main
0000000000000039 IMAGE_REL_AMD64_REL32 .data
000000000000003f IMAGE_REL_AMD64_REL32 .bss
RELOCATION RECORDS FOR [.pdata]:
OFFSET TYPE VALUE
0000000000000000 IMAGE_REL_AMD64_ADDR32NB .text
0000000000000004 IMAGE_REL_AMD64_ADDR32NB .text
0000000000000008 IMAGE_REL_AMD64_ADDR32NB .xdata
000000000000000c IMAGE_REL_AMD64_ADDR32NB .text
0000000000000010 IMAGE_REL_AMD64_ADDR32NB .text
0000000000000014 IMAGE_REL_AMD64_ADDR32NB .xdata
说明:目标文件中的段信息远比我们想象的要多很多,除了代码段、数据段、BSS段之外,还有其他辅助段。
查看段大小
使用 size 命令可以查看代码段和数据段的大小:
size SimpleSection.o
输出结果:
text data bss dec hexfilename
192 16 16 224 e0SimpleSection.o
说明:
- text:代码段大小,192 字节
- data:数据段大小,16 字节
- bss:BSS段大小,16 字节
- dec:总大小(十进制),224 字节
- hex:总大小(十六进制),0xe0 = 224 字节
代码段
查看代码段内容
使用 objdump -s -d 命令可以查看目标文件中的代码段信息:
objdump -s -d SimpleSection.o
参数说明:
- -s:将所有段的内容以十六进制打印出来
- -d:将所有包含指令的段反汇编
输出结果:
SimpleSection.o: file format pe-x86-64
Contents of section .text:
0000 554889e5 4883ec20 894d108b 4510488d UH..H.. .M..E.H.
0010 0d000000 0089c2e8 00000000 904883c4 ………….H..
0020 205dc355 4899e548 83ec30e8 00000000 ].UH..H..0…..
0030 c745fc01 0000008b 15040000 008b0504 .E…………..
0040 00000001 c28b45fc 01c28b45 f801d089 ……E….E….
0050 c1e8aaff ffff8b45 fc4883c4 305dc390 …….E.H..0]..
Contents of section .data:
0000 54000000 55000000 00000000 00000000 T…U………..
Contents of section .rdata:
0000 25640000 00000000 00000000 00000000 %d…………..
Contents of section .xdata:
0000 01080305 08320403 01500000 01080305 …..2…P……
0010 08520403 01500000 .R…P..
Contents of section .pdata:
0000 00000000 23000000 00000000 23000000 ….#…….#…
0010 5f000000 0c000000 _…….
Contents of section .rdata$zzz:
0000 4743433a 2028474e 55292031 352e322e GCC: (GNU) 15.2.
0010 30000000 00000000 00000000 00000000 0……………
Disassembly of section .text:
0000000000000000 <func1>:
0:55 push %rbp
1:48 89 e5 mov %rsp,%rbp
4:48 83 ec 20 sub $0x20,%rsp
8:89 4d 10 mov %ecx,0x10(%rbp)
b:8b 45 10 mov 0x10(%rbp),%eax
e:48 8d 0d 00 00 00 00 lea 0x0(%rip),%rcx # 15 <func1+0x15>
15:89 c2 mov %eax,%edx
17:e8 00 00 00 00 call 1c <func1+0x1c>
1c:90 nop
1d:48 83 c4 20 add $0x20,%rsp
21:5d pop %rbp
22:c3 ret
0000000000000023 <main>:
23:55 push %rbp
24:48 89 e5 mov %rsp,%rbp
27:48 83 ec 30 sub $0x30,%rsp
2b:e8 00 00 00 00 call 30 <main+0xd>
30:c7 45 fc 01 00 00 00 movl $0x1,-0x4(%rbp)
37:8b 15 04 00 00 00 mov 0x4(%rip),%edx # 41 <main+0x1e>
3d:8b 05 04 00 00 00 mov 0x4(%rip),%eax # 47 <main+0x24>
43:01 c2 add %eax,%edx
45:8b 45 fc mov -0x4(%rbp),%eax
48:01 c2 add %eax,%edx
4a:8b 45 f8 mov -0x8(%rbp),%eax
4d:01 d0 add %edx,%eax
4f:89 c1 mov %eax,%ecx
51:e8 aa ff ff ff call 0 <func1>
56:8b 45 fc mov -0x4(%rbp),%eax
59:48 83 c4 30 add $0x30,%rsp
5d:5d pop %rbp
5e:c3 ret
5f:90 nop
数据段和只读数据段
段的用途
- .data 段:保存已初始化的全局静态变量和局部静态变量
- .rodata 或 .rdata 段:存放只读数据,通常是程序中的只读变量(如 const 修饰的变量)和字符串常量
只读数据段的优势
查看完整段内容
使用 objdump -x -s -d 查看所有段内容:
objdump -x -s -d SimpleSection.o
输出结果(关键部分):
Contents of section .data:
0000 54000000 55000000 00000000 00000000 T…U………..
数据段内容分析:
| 0x00 | 54 | 84 | global_init_var | 全局已初始化变量 |
| 0x04 | 55 | 85 | static_var | 局部静态已初始化变量 |
| 0x08 | 00 | 0 | – | 填充字节(对齐) |
| 0x0C | 00 | 0 | – | 填充字节(对齐) |
其中:
- 0x54 是 84 的十六进制表示,对应 SimpleSection.c 中的 global_init_var = 84
- 0x55 是 85 的十六进制表示,对应 SimpleSection.c 中的 static_var = 85
注意:有时候编译器会将字符串常量放到 .data 段而不是 .rodata 段,这取决于编译器的优化策略。
BSS 段
BSS 段的特点
从 objdump -x -s -d 的输出中可以看到 BSS 段的信息:
Idx Name Size VMA LMA File off Algn
2 .bss 00000010 0000000000000000 0000000000000000 00000000 2**4
ALLOC
关键信息:
- 大小:0x10 = 16 字节
- 对齐:2**4 = 16 字节对齐
- 文件偏移:0x00,表示 BSS 段不占用文件空间
BSS 段的特殊性
BSS(Block Started by Symbol)段用于存放未初始化的全局变量和局部静态变量,如:
- global_uninit_var(未初始化全局变量)
- static_var2(未初始化局部静态变量)
BSS 段的特点:
总结
目标文件的典型段
| .text | 代码段 | 编译后的机器指令 |
| .data | 数据段 | 已初始化的全局/静态变量 |
| .bss | BSS段 | 未初始化的全局/静态变量 |
| .rodata / .rdata | 只读数据段 | 常量、字符串等 |
| .xdata | 异常数据 | 异常处理信息 |
| .pdata | 程序数据 | 函数表等元数据 |
常用命令
| gcc -c file.c | 编译但不链接,生成目标文件 |
| objdump -h file.o | 查看段的基本信息 |
| objdump -x file.o | 查看完整信息(符号表、重定位表等) |
| objdump -s -d file.o | 查看段内容和反汇编代码 |
| size file.o | 查看各段大小统计 |
![P11246 [GESP202409 六级] 小杨和整数拆分-171主机测评](https://www.171host.com/wp-content/uploads/2026/11/20261108100631-6af049a75855c-220x150.png)


![打卡信奥刷题(3678)用C++实现信奥题 P12274 [蓝桥杯 2024 国 Python B] 设计-171主机测评](https://www.171host.com/wp-content/uploads/2026/11/20261108053830-6af00ad6bdf8d-220x150.png)