二进制简介

C编译过程

预处理

c源文件 example.c

#include <stdio.h>
#define FORMAT_STRING "%s"
#define MESSAGE "hello,world!\n"
int main(int argc,char *argv[]){
	printf(FORMAT_STRING,MESSAGE);
	return 0;
}

预处理输出:

gcc -E -P example.c

其中 -E表示是只激活预处理 -P表示忽略调试信息

编译阶段

gcc -S -masm=intel example.c
cat example.s

    	.file	"example.c"
	.intel_syntax noprefix
	.text
	.section	.rodata
.LC0:
	.string	"hello,world!"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	push	rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	mov	rbp, rsp
	.cfi_def_cfa_register 6
	sub	rsp, 16
	mov	DWORD PTR -4[rbp], edi
	mov	QWORD PTR -16[rbp], rsi
	lea	rax, .LC0[rip]
	mov	rdi, rax
	call	puts@PLT
	mov	eax, 0
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 11.2.0-19ubuntu1) 11.2.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

汇编阶段

gcc -c example.c
file example.o
example.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

链接阶段

$ gcc example.c
$ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=593c9852d50234aa2987bdcbef83a2dfdec3e5a1, for GNU/Linux 3.2.0, not stripped
$ ./a.out
hello,world!

符号和剥离的二进制文件

查看符号信息

$ readelf --syms a.out

Symbol table '.dynsym' contains 7 entries:
Num:    Value          Size Type    Bind   Vis      Ndx Name
0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND _[...]@GLIBC_2.34 (2)
2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterT[...]
3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (3)
4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMC[...]
6: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND [...]@GLIBC_2.2.5 (3)

Symbol table '.symtab' contains 36 entries:
Num:    Value          Size Type    Bind   Vis      Ndx Name
0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS Scrt1.o
2: 000000000000038c    32 OBJECT  LOCAL  DEFAULT    4 __abi_tag
3: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
4: 0000000000001090     0 FUNC    LOCAL  DEFAULT   16 deregister_tm_clones
5: 00000000000010c0     0 FUNC    LOCAL  DEFAULT   16 register_tm_clones
6: 0000000000001100     0 FUNC    LOCAL  DEFAULT   16 __do_global_dtors_aux
7: 0000000000004010     1 OBJECT  LOCAL  DEFAULT   26 completed.0
8: 0000000000003dc0     0 OBJECT  LOCAL  DEFAULT   22 __do_global_dtor[...]
9: 0000000000001140     0 FUNC    LOCAL  DEFAULT   16 frame_dummy
10: 0000000000003db8     0 OBJECT  LOCAL  DEFAULT   21 __frame_dummy_in[...]
11: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS example.c
12: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
13: 00000000000020f0     0 OBJECT  LOCAL  DEFAULT   20 __FRAME_END__
14: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
15: 0000000000003dc8     0 OBJECT  LOCAL  DEFAULT   23 _DYNAMIC
16: 0000000000002014     0 NOTYPE  LOCAL  DEFAULT   19 __GNU_EH_FRAME_HDR
17: 0000000000003fb8     0 OBJECT  LOCAL  DEFAULT   24 _GLOBAL_OFFSET_TABLE_
18: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_mai[...]
19: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterT[...]
20: 0000000000004000     0 NOTYPE  WEAK   DEFAULT   25 data_start
21: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5
22: 0000000000004010     0 NOTYPE  GLOBAL DEFAULT   25 _edata
23: 0000000000001174     0 FUNC    GLOBAL HIDDEN    17 _fini
24: 0000000000004000     0 NOTYPE  GLOBAL DEFAULT   25 __data_start
25: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
26: 0000000000004008     0 OBJECT  GLOBAL HIDDEN    25 __dso_handle
27: 0000000000002000     4 OBJECT  GLOBAL DEFAULT   18 _IO_stdin_used
28: 0000000000004018     0 NOTYPE  GLOBAL DEFAULT   26 _end
29: 0000000000001060    38 FUNC    GLOBAL DEFAULT   16 _start
30: 0000000000004010     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start
31: 0000000000001149    41 FUNC    GLOBAL DEFAULT   16 main
32: 0000000000004010     0 OBJECT  GLOBAL HIDDEN    25 __TMC_END__
33: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMC[...]
34: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@G[...]
35: 0000000000001000     0 FUNC    GLOBAL HIDDEN    12 _init

剥离的二进制文件

$ strip --strip-all a.out
$ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=593c9852d50234aa2987bdcbef83a2dfdec3e5a1, for GNU/Linux 3.2.0, stripped
$ readelf --syms a.out

Symbol table '.dynsym' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND _[...]@GLIBC_2.34 (2)
     2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterT[...]
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (3)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMC[...]
     6: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND [...]@GLIBC_2.2.5 (3)

反汇编二进制文件

查看对象文件

$ objdump -sj .rodata example.o

example.o:     文件格式 elf64-x86-64

Contents of section .rodata:
 0000 68656c6c 6f2c776f 726c6421 00        hello,world!.   
$ objdump -M intel -d example.o

example.o:     文件格式 elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:	f3 0f 1e fa          	endbr64 
   4:	55                   	push   rbp
   5:	48 89 e5             	mov    rbp,rsp
   8:	48 83 ec 10          	sub    rsp,0x10
   c:	89 7d fc             	mov    DWORD PTR [rbp-0x4],edi
   f:	48 89 75 f0          	mov    QWORD PTR [rbp-0x10],rsi
  13:	48 8d 05 00 00 00 00 	lea    rax,[rip+0x0]        # 1a <main+0x1a>
  1a:	48 89 c7             	mov    rdi,rax
  1d:	e8 00 00 00 00       	call   22 <main+0x22>
  22:	b8 00 00 00 00       	mov    eax,0x0
  27:	c9                   	leave  
  28:	c3                   	ret  

.rodata节代表只读数据

通过readelf显示重定位符号:

$ readelf --relocs example.o
重定位节 '.rela.text' at offset 0x1a0 contains 2 entries:
  偏移量          信息           类型           符号值        符号名称 + 加数
000000000016  000300000002 R_X86_64_PC32     0000000000000000 .rodata - 4
00000000001e  000500000004 R_X86_64_PLT32    0000000000000000 puts - 4

重定位节 '.rela.eh_frame' at offset 0x1d0 contains 1 entry:
  偏移量          信息           类型           符号值        符号名称 + 加数
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0

检查完整的二进制执行体

用objdump 反汇编(剥离的)二进制文件

$ objdump -M intel -d a.out

a.out:     文件格式 elf64-x86-64


Disassembly of section .init:

0000000000001000 <.init>:
    1000:	f3 0f 1e fa          	endbr64 
    1004:	48 83 ec 08          	sub    rsp,0x8
    1008:	48 8b 05 d9 2f 00 00 	mov    rax,QWORD PTR [rip+0x2fd9]        # 3fe8 <puts@plt+0x2f98>
    100f:	48 85 c0             	test   rax,rax
    1012:	74 02                	je     1016 <__cxa_finalize@plt-0x2a>
    1014:	ff d0                	call   rax
    1016:	48 83 c4 08          	add    rsp,0x8
    101a:	c3                   	ret    

Disassembly of section .plt:

0000000000001020 <.plt>:
    1020:	ff 35 9a 2f 00 00    	push   QWORD PTR [rip+0x2f9a]        # 3fc0 <puts@plt+0x2f70>
    1026:	f2 ff 25 9b 2f 00 00 	bnd jmp QWORD PTR [rip+0x2f9b]        # 3fc8 <puts@plt+0x2f78>
    102d:	0f 1f 00             	nop    DWORD PTR [rax]
    1030:	f3 0f 1e fa          	endbr64 
    1034:	68 00 00 00 00       	push   0x0
    1039:	f2 e9 e1 ff ff ff    	bnd jmp 1020 <__cxa_finalize@plt-0x20>
    103f:	90                   	nop

Disassembly of section .plt.got:

0000000000001040 <__cxa_finalize@plt>:
    1040:	f3 0f 1e fa          	endbr64 
    1044:	f2 ff 25 ad 2f 00 00 	bnd jmp QWORD PTR [rip+0x2fad]        # 3ff8 <puts@plt+0x2fa8>
    104b:	0f 1f 44 00 00       	nop    DWORD PTR [rax+rax*1+0x0]

Disassembly of section .plt.sec:

0000000000001050 <puts@plt>:
    1050:	f3 0f 1e fa          	endbr64 
    1054:	f2 ff 25 75 2f 00 00 	bnd jmp QWORD PTR [rip+0x2f75]        # 3fd0 <puts@plt+0x2f80>
    105b:	0f 1f 44 00 00       	nop    DWORD PTR [rax+rax*1+0x0]

Disassembly of section .text:

0000000000001060 <.text>:
    1060:	f3 0f 1e fa          	endbr64 
    1064:	31 ed                	xor    ebp,ebp
    1066:	49 89 d1             	mov    r9,rdx
    1069:	5e                   	pop    rsi
    106a:	48 89 e2             	mov    rdx,rsp
    106d:	48 83 e4 f0          	and    rsp,0xfffffffffffffff0
    1071:	50                   	push   rax
    1072:	54                   	push   rsp
    1073:	45 31 c0             	xor    r8d,r8d
    1076:	31 c9                	xor    ecx,ecx
    1078:	48 8d 3d ca 00 00 00 	lea    rdi,[rip+0xca]        # 1149 <puts@plt+0xf9>
    107f:	ff 15 53 2f 00 00    	call   QWORD PTR [rip+0x2f53]        # 3fd8 <puts@plt+0x2f88>
    1085:	f4                   	hlt    
    1086:	66 2e 0f 1f 84 00 00 	cs nop WORD PTR [rax+rax*1+0x0]
    108d:	00 00 00 
    1090:	48 8d 3d 79 2f 00 00 	lea    rdi,[rip+0x2f79]        # 4010 <puts@plt+0x2fc0>
    1097:	48 8d 05 72 2f 00 00 	lea    rax,[rip+0x2f72]        # 4010 <puts@plt+0x2fc0>
    109e:	48 39 f8             	cmp    rax,rdi
    10a1:	74 15                	je     10b8 <puts@plt+0x68>
    10a3:	48 8b 05 36 2f 00 00 	mov    rax,QWORD PTR [rip+0x2f36]        # 3fe0 <puts@plt+0x2f90>
    10aa:	48 85 c0             	test   rax,rax
    10ad:	74 09                	je     10b8 <puts@plt+0x68>
    10af:	ff e0                	jmp    rax
    10b1:	0f 1f 80 00 00 00 00 	nop    DWORD PTR [rax+0x0]
    10b8:	c3                   	ret    
    10b9:	0f 1f 80 00 00 00 00 	nop    DWORD PTR [rax+0x0]
    10c0:	48 8d 3d 49 2f 00 00 	lea    rdi,[rip+0x2f49]        # 4010 <puts@plt+0x2fc0>
10c7:	48 8d 35 42 2f 00 00 	lea    rsi,[rip+0x2f42]        # 4010 <puts@plt+0x2fc0>
10ce:	48 29 fe             	sub    rsi,rdi
10d1:	48 89 f0             	mov    rax,rsi
10d4:	48 c1 ee 3f          	shr    rsi,0x3f
10d8:	48 c1 f8 03          	sar    rax,0x3
10dc:	48 01 c6             	add    rsi,rax
10df:	48 d1 fe             	sar    rsi,1
10e2:	74 14                	je     10f8 <puts@plt+0xa8>
10e4:	48 8b 05 05 2f 00 00 	mov    rax,QWORD PTR [rip+0x2f05]        # 3ff0 <puts@plt+0x2fa0>
10eb:	48 85 c0             	test   rax,rax
10ee:	74 08                	je     10f8 <puts@plt+0xa8>
10f0:	ff e0                	jmp    rax
10f2:	66 0f 1f 44 00 00    	nop    WORD PTR [rax+rax*1+0x0]
10f8:	c3                   	ret    
10f9:	0f 1f 80 00 00 00 00 	nop    DWORD PTR [rax+0x0]
1100:	f3 0f 1e fa          	endbr64 
1104:	80 3d 05 2f 00 00 00 	cmp    BYTE PTR [rip+0x2f05],0x0        # 4010 <puts@plt+0x2fc0>
110b:	75 2b                	jne    1138 <puts@plt+0xe8>
110d:	55                   	push   rbp
110e:	48 83 3d e2 2e 00 00 	cmp    QWORD PTR [rip+0x2ee2],0x0        # 3ff8 <puts@plt+0x2fa8>
1115:	00 
1116:	48 89 e5             	mov    rbp,rsp
1119:	74 0c                	je     1127 <puts@plt+0xd7>
111b:	48 8b 3d e6 2e 00 00 	mov    rdi,QWORD PTR [rip+0x2ee6]        # 4008 <puts@plt+0x2fb8>
1122:	e8 19 ff ff ff       	call   1040 <__cxa_finalize@plt>
1127:	e8 64 ff ff ff       	call   1090 <puts@plt+0x40>
112c:	c6 05 dd 2e 00 00 01 	mov    BYTE PTR [rip+0x2edd],0x1        # 4010 <puts@plt+0x2fc0>
1133:	5d                   	pop    rbp
1134:	c3                   	ret    
1135:	0f 1f 00             	nop    DWORD PTR [rax]
1138:	c3                   	ret    
1139:	0f 1f 80 00 00 00 00 	nop    DWORD PTR [rax+0x0]
1140:	f3 0f 1e fa          	endbr64 
1144:	e9 77 ff ff ff       	jmp    10c0 <puts@plt+0x70>
1149:	f3 0f 1e fa          	endbr64 
114d:	55                   	push   rbp
114e:	48 89 e5             	mov    rbp,rsp
1151:	48 83 ec 10          	sub    rsp,0x10
1155:	89 7d fc             	mov    DWORD PTR [rbp-0x4],edi
1158:	48 89 75 f0          	mov    QWORD PTR [rbp-0x10],rsi
115c:	48 8d 05 a1 0e 00 00 	lea    rax,[rip+0xea1]        # 2004 <puts@plt+0xfb4>
1163:	48 89 c7             	mov    rdi,rax
1166:	e8 e5 fe ff ff       	call   1050 <puts@plt>
116b:	b8 00 00 00 00       	mov    eax,0x0
1170:	c9                   	leave  
1171:	c3                   	ret    

Disassembly of section .fini:

0000000000001174 <.fini>:
    1174:	f3 0f 1e fa          	endbr64 
    1178:	48 83 ec 08          	sub    rsp,0x8
    117c:	48 83 c4 08          	add    rsp,0x8
    1180:	c3                   	ret   

加载并执行二进制文件

posted on 2022-09-08 18:46  huwenao  阅读(215)  评论(0)    收藏  举报