目标文件/可执行文件查看-C/C++
·
linux系统
# 查看文件格式
$ file simpleSection.o
simpleSection.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
目标文件查看
文件头
$ readelf -h simpleSection.o
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 1032 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 64 (bytes)
Number of section headers: 14
Section header string table index: 13
段表
$ readelf -S simpleSection.o
There are 14 section headers, starting at offset 0x408:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .text PROGBITS 0000000000000000 00000040
000000000000005a 0000000000000000 AX 0 0 1
[ 2] .rela.text RELA 0000000000000000 000002e8
0000000000000078 0000000000000018 I 11 1 8
[ 3] .data PROGBITS 0000000000000000 0000009c
0000000000000008 0000000000000000 WA 0 0 4
[ 4] .bss NOBITS 0000000000000000 000000a4
0000000000000008 0000000000000000 WA 0 0 4
[ 5] .rodata PROGBITS 0000000000000000 000000a4
0000000000000004 0000000000000000 A 0 0 1
[ 6] .comment PROGBITS 0000000000000000 000000a8
000000000000001c 0000000000000001 MS 0 0 1
[ 7] .note.GNU-stack PROGBITS 0000000000000000 000000c4
0000000000000000 0000000000000000 0 0 1
[ 8] .note.gnu.pr[...] NOTE 0000000000000000 000000c8
0000000000000030 0000000000000000 A 0 0 8
[ 9] .eh_frame PROGBITS 0000000000000000 000000f8
0000000000000058 0000000000000000 A 0 0 8
[10] .rela.eh_frame RELA 0000000000000000 00000360
0000000000000030 0000000000000018 I 11 9 8
[11] .symtab SYMTAB 0000000000000000 00000150
0000000000000138 0000000000000018 12 8 8
[12] .strtab STRTAB 0000000000000000 00000288
000000000000005f 0000000000000000 0 0 1
[13] .shstrtab STRTAB 0000000000000000 00000390
0000000000000074 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
D (mbind), l (large), p (processor specific)
# 关键的段
$ objdump -h simpleSection.o
simpleSection.o: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000005a 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000008 0000000000000000 0000000000000000 0000009c 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000008 0000000000000000 0000000000000000 000000a4 2**2
ALLOC
3 .rodata 00000004 0000000000000000 0000000000000000 000000a4 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .comment 0000001c 0000000000000000 0000000000000000 000000a8 2**0
CONTENTS, READONLY
5 .note.GNU-stack 00000000 0000000000000000 0000000000000000 000000c4 2**0
CONTENTS, READONLY
6 .note.gnu.property 00000030 0000000000000000 0000000000000000 000000c8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
7 .eh_frame 00000058 0000000000000000 0000000000000000 000000f8 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
# ELF文件中代码段、数据段和bss段的长度
$ size simpleSection.o
text data bss dec hex filename
230 8 8 246 f6 simpleSection.o
内容段
# -s 将所有段的内容以十六进制的形式的打印
# -d 将所有包含指令的段反汇编
$ objdump -s -d simpleSection.o
simpleSection.o: file format elf64-x86-64
Contents of section .text:
0000 554889e5 4883ec10 897dfc8b 45fc89c6 UH..H....}..E...
0010 488d0500 00000048 89c7b800 000000e8 H......H........
0020 00000000 90c9c355 4889e548 83ec10c7 .......UH..H....
0030 45f80100 00008b15 00000000 8b050000 E...............
0040 000001c2 8b45f801 c28b45fc 01d089c7 .....E....E.....
0050 e8000000 008b45f8 c9c3 ......E...
Contents of section .data:
0000 54000000 55000000 T...U...
Contents of section .rodata:
0000 25640a00 %d..
Contents of section .comment:
0000 00474343 3a202847 4e552920 31342e32 .GCC: (GNU) 14.2
0010 2e312032 30323530 32303700 .1 20250207.
Contents of section .note.gnu.property:
0000 04000000 20000000 05000000 474e5500 .... .......GNU.
0010 020001c0 04000000 01000000 00000000 ................
0020 010001c0 04000000 01000000 00000000 ................
Contents of section .eh_frame:
0000 14000000 00000000 017a5200 01781001 .........zR..x..
0010 1b0c0708 90010000 1c000000 1c000000 ................
0020 00000000 27000000 00410e10 8602430d ....'....A....C.
0030 06620c07 08000000 1c000000 3c000000 .b..........<...
0040 00000000 33000000 00410e10 8602430d ....3....A....C.
0050 066e0c07 08000000 .n......
Disassembly of section .text:
0000000000000000 <func1>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 83 ec 10 sub $0x10,%rsp
8: 89 7d fc mov %edi,-0x4(%rbp)
b: 8b 45 fc mov -0x4(%rbp),%eax
e: 89 c6 mov %eax,%esi
10: 48 8d 05 00 00 00 00 lea 0x0(%rip),%rax # 17 <func1+0x17>
17: 48 89 c7 mov %rax,%rdi
1a: b8 00 00 00 00 mov $0x0,%eax
1f: e8 00 00 00 00 call 24 <func1+0x24>
24: 90 nop
25: c9 leave
26: c3 ret
0000000000000027 <main>:
27: 55 push %rbp
28: 48 89 e5 mov %rsp,%rbp
2b: 48 83 ec 10 sub $0x10,%rsp
2f: c7 45 f8 01 00 00 00 movl $0x1,-0x8(%rbp)
36: 8b 15 00 00 00 00 mov 0x0(%rip),%edx # 3c <main+0x15>
3c: 8b 05 00 00 00 00 mov 0x0(%rip),%eax # 42 <main+0x1b>
42: 01 c2 add %eax,%edx
44: 8b 45 f8 mov -0x8(%rbp),%eax
47: 01 c2 add %eax,%edx
49: 8b 45 fc mov -0x4(%rbp),%eax
4c: 01 d0 add %edx,%eax
4e: 89 c7 mov %eax,%edi
50: e8 00 00 00 00 call 55 <main+0x2e>
55: 8b 45 f8 mov -0x8(%rbp),%eax
58: c9 leave
59: c3 ret
其他段
# 显示段表、符号表、重定位表等
$ objdump -x simpleSection.o
simpleSection.o: file format elf64-x86-64
simpleSection.o
architecture: i386:x86-64, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x0000000000000000
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000005a 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000008 0000000000000000 0000000000000000 0000009c 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000008 0000000000000000 0000000000000000 000000a4 2**2
ALLOC
3 .rodata 00000004 0000000000000000 0000000000000000 000000a4 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .comment 0000001c 0000000000000000 0000000000000000 000000a8 2**0
CONTENTS, READONLY
5 .note.GNU-stack 00000000 0000000000000000 0000000000000000 000000c4 2**0
CONTENTS, READONLY
6 .note.gnu.property 00000030 0000000000000000 0000000000000000 000000c8 2**3
CONTENTS, ALLOC, LOAD, READONLY, DATA
7 .eh_frame 00000058 0000000000000000 0000000000000000 000000f8 2**3
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 simpleSection.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 l d .data 0000000000000000 .data
0000000000000000 l d .bss 0000000000000000 .bss
0000000000000000 l d .rodata 0000000000000000 .rodata
0000000000000004 l O .data 0000000000000004 static_var.1
0000000000000004 l O .bss 0000000000000004 static_var2.0
0000000000000000 g O .data 0000000000000004 global_init_var
0000000000000000 g O .bss 0000000000000004 global_uinit_var
0000000000000000 g F .text 0000000000000027 func1
0000000000000000 *UND* 0000000000000000 printf
0000000000000027 g F .text 0000000000000033 main
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
0000000000000013 R_X86_64_PC32 .rodata-0x0000000000000004
0000000000000020 R_X86_64_PLT32 printf-0x0000000000000004
0000000000000038 R_X86_64_PC32 .data
000000000000003e R_X86_64_PC32 .bss
0000000000000051 R_X86_64_PLT32 func1-0x0000000000000004
RELOCATION RECORDS FOR [.eh_frame]:
OFFSET TYPE VALUE
0000000000000020 R_X86_64_PC32 .text
0000000000000040 R_X86_64_PC32 .text+0x0000000000000027
符号表
$ readelf -s simpleSection.o
Symbol table '.symtab' contains 13 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS simpleSection.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 .text
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3 .data
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 .bss
5: 0000000000000000 0 SECTION LOCAL DEFAULT 5 .rodata
6: 0000000000000004 4 OBJECT LOCAL DEFAULT 3 static_var.1
7: 0000000000000004 4 OBJECT LOCAL DEFAULT 4 static_var2.0
8: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 global_init_var
9: 0000000000000000 4 OBJECT GLOBAL DEFAULT 4 global_uinit_var
10: 0000000000000000 39 FUNC GLOBAL DEFAULT 1 func1
11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND printf
12: 0000000000000027 51 FUNC GLOBAL DEFAULT 1 main
可执行文件查看
程序头表
程序头:描述了ELF文件如何被操作系统映射到进程虚拟地址空间。
# 程序头表
$ readelf -l sectionMapping.elf
Elf file type is EXEC (Executable file)
Entry point 0x402d60
There are 12 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000000518 0x0000000000000518 R 0x1000
LOAD 0x0000000000001000 0x0000000000401000 0x0000000000401000
0x000000000007a7ed 0x000000000007a7ed R E 0x1000
LOAD 0x000000000007c000 0x000000000047c000 0x000000000047c000
0x0000000000031b88 0x0000000000031b88 R 0x1000
LOAD 0x00000000000ae0c8 0x00000000004ae0c8 0x00000000004ae0c8
0x0000000000005960 0x000000000000b1a0 RW 0x1000
NOTE 0x00000000000002e0 0x00000000004002e0 0x00000000004002e0
0x0000000000000024 0x0000000000000024 R 0x4
NOTE 0x00000000000adb28 0x00000000004adb28 0x00000000004adb28
0x0000000000000040 0x0000000000000040 R 0x8
NOTE 0x00000000000adb68 0x00000000004adb68 0x00000000004adb68
0x0000000000000020 0x0000000000000020 R 0x4
TLS 0x00000000000ae0c8 0x00000000004ae0c8 0x00000000004ae0c8
0x0000000000000038 0x0000000000000050 R 0x8
GNU_PROPERTY 0x00000000000adb28 0x00000000004adb28 0x00000000004adb28
0x0000000000000040 0x0000000000000040 R 0x8
GNU_SFRAME 0x00000000000a18a8 0x00000000004a18a8 0x00000000004a18a8
0x000000000000c18a 0x000000000000c18a R 0x8
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 0x10
GNU_RELRO 0x00000000000ae0c8 0x00000000004ae0c8 0x00000000004ae0c8
0x0000000000003f38 0x0000000000003f38 R 0x1
Section to Segment mapping:
Segment Sections...
00 .note.gnu.build-id .rela.plt
01 .init .plt .text .fini
02 .rodata .stapsdt.base rodata.cst32 .eh_frame .sframe .gcc_except_table .note.gnu.property .note.ABI-tag
03 .tdata .init_array .fini_array .data.rel.ro .got .got.plt .data .bss
04 .note.gnu.build-id
05 .note.gnu.property
06 .note.ABI-tag
07 .tdata .tbss
08 .note.gnu.property
09 .sframe
10
11 .tdata .init_array .fini_array .data.rel.ro .got
程序虚拟地址空间分布
$ gcc -o test singleton.c -static
$ ./test &
[1] 4869
$ cat /proc/4869/map
map_files/ maps
$ cat /proc/4869/maps
起始地址-结束地址 权限 偏移量 设备号 inode 路径名/描述
00400000-00401000 r--p 00000000 103:06 14811163 /home/user/.leetcode/test/test
00401000-0047c000 r-xp 00001000 103:06 14811163 /home/user/.leetcode/test/test
0047c000-004a2000 r--p 0007c000 103:06 14811163 /home/user/.leetcode/test/test
004a2000-004a7000 r--p 000a1000 103:06 14811163 /home/user/.leetcode/test/test
004a7000-004a9000 rw-p 000a6000 103:06 14811163 /home/user/.leetcode/test/test
004a9000-004af000 rw-p 00000000 00:00 0
12047000-12069000 rw-p 00000000 00:00 0 [heap]
70381afb7000-70381afbb000 r--p 00000000 00:00 0 [vvar]
70381afbb000-70381afbd000 r-xp 00000000 00:00 0 [vdso]
7fff43702000-7fff43723000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]
手动生成目标文件
# 二进制文件生成目标文件
$ objcopy -I binary -O elf64-x86-64 1.jpg pic.o
$ objdump -ht pic.o
pic.o: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .data 00094bbe 0000000000000000 0000000000000000 00000040 2**0
CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
0000000000000000 g .data 0000000000000000 _binary_1_jpg_start
0000000000094bbe g .data 0000000000000000 _binary_1_jpg_end
0000000000094bbe g *ABS* 0000000000000000 _binary_1_jpg_size
$ du -sh pic.o
596K pic.o
$ du -sh 1.jpg
596K 1.jpg
生成的目标文件有全局符号可用,代码可直接使用这个段处理文件。
Windows
# /ALL打印目标文件的所有相关信息
dumpbin /ALL simpleSection.obj > simpleSection.txt
# /SUMMARY输出所有段的段名和长度
dumpbin /SUMMARY simpleSection.obj
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐


所有评论(0)