1 GCC編譯過程
首先我們來看GCC的編譯過程。hello.c檔案内容如下。
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
gcc hello.c -o hello -save-temps --verbose
在編譯的時候添加兩個編譯選項,第一個選項是将編譯過程中的生成的中間檔案儲存下來,第二個用于檢視GCC編譯的詳細工作流程。
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: …/src/configure -v --with-pkgversion=‘Ubuntu 9.3.0-10ubuntu2’ --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
COLLECT_GCC_OPTIONS=‘-o’ ‘hello’ ‘-save-temps’ ‘-v’ ‘-mtune=generic’ ‘-march=x86-64’
/usr/lib/gcc/x86_64-linux-gnu/9/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu hello.c -mtune=generic -march=x86-64 -fpch-preprocess -fasynchronous-unwind-tables -fstack-protector-strong -Wformat -Wformat-security -fstack-clash-protection -fcf-protection -o hello.i
ignoring nonexistent directory “/usr/local/include/x86_64-linux-gnu”
ignoring nonexistent directory “/usr/lib/gcc/x86_64-linux-gnu/9/include-fixed”
ignoring nonexistent directory “/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/x86_64-linux-gnu/include”
#include “…” search starts here:
#include <…> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/9/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
COLLECT_GCC_OPTIONS=‘-o’ ‘hello’ ‘-save-temps’ ‘-v’ ‘-mtune=generic’ ‘-march=x86-64’
/usr/lib/gcc/x86_64-linux-gnu/9/cc1 -fpreprocessed hello.i -quiet -dumpbase hello.c -mtune=generic -march=x86-64 -auxbase hello -version -fasynchronous-unwind-tables -fstack-protector-strong -Wformat -Wformat-security -fstack-clash-protection -fcf-protection -o hello.s
GNU C17 (Ubuntu 9.3.0-10ubuntu2) version 9.3.0 (x86_64-linux-gnu)
compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C17 (Ubuntu 9.3.0-10ubuntu2) version 9.3.0 (x86_64-linux-gnu)
compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 18dc4c39b54390aa2b5013fb4339d43f
COLLECT_GCC_OPTIONS=‘-o’ ‘hello’ ‘-save-temps’ ‘-v’ ‘-mtune=generic’ ‘-march=x86-64’
as -v --64 -o hello.o hello.s
GNU彙編版本 2.34 (x86_64-linux-gnu) 使用BFD版本 (GNU Binutils for Ubuntu) 2.34
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/lib/:/lib/x86_64-linux-gnu/:/lib/…/lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/…/lib/:/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS=‘-o’ ‘hello’ ‘-save-temps’ ‘-v’ ‘-mtune=generic’ ‘-march=x86-64’
/usr/lib/gcc/x86_64-linux-gnu/9/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/9/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper -plugin-opt=-fresolution=hello.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o hello /usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/lib -L/lib/x86_64-linux-gnu -L/lib/…/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/…/lib -L/usr/lib/gcc/x86_64-linux-gnu/9/…/…/… hello.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/…/…/…/x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS=‘-o’ ‘hello’ ‘-save-temps’ ‘-v’ ‘-mtune=generic’ ‘-march=x86-64’
![]()
GCC編譯1 GCC編譯過程2 預處理階段3 編譯階段4 彙編階段5 連結階段
在這段資訊中,我們可以發現。GCC編譯過程主要包括四個過程,即預處理階段(Preprocess),編譯階段(Compile),彙編(Assemble)和連結(Link),分别使用了ccll,as和collect2。
其中,ccl是編譯器,用于将源檔案hello.c編譯為hello.s;as是彙編器,将hello.s彙編為hello.o,連結器collect是對ld指令的封裝,用于将C語言運作時庫中的目标檔案以及所需的動态連結庫連結到可執行hello。
2 預處理階段
主要處理源碼中以#開頭的預處理指令,将其轉換後直接插入程式文本中,得到另外一個C程式,通常以i作為擴充名,在指令中添加編譯選項e可以單獨執行預處理。
gcc -E hello.c -o hello.i
一些處理規則:
- 遞歸處理“#include”預處理指令,将對應的檔案内容複制到該指令的位置。
- 删除所有的“”define#指令,并在其被引用的位置遞歸的展開所有的宏定義
- 處理所有的條件預處理指令
- 删除所有注釋
- 添加行号和檔案名辨別
3 編譯階段
單獨執行指令
gcc -S hello.i -o -masm=intel -fno-asynchronous-unwind-tables
第一個編譯選項是将檔案指定為我們熟悉的intel,第二個編譯選項則用于生成沒有cfi宏的彙編指令,以提高可讀性。
生成的hello.s檔案中的内容:
.file "hello.c"
.text
.section .rodata
.LC0:
.string "Hello World!"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
leaq .LC0(%rip), %rdi
movl $0, %eax
call [email protected]
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 9.3.0-10ubuntu2) 9.3.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:
4 彙編階段
gcc -c hello.c -o hello.o
gcc -c hello.s -o hello.o
這兩條指令都是可以的。此時的目标檔案hello.o是一個可重定位的檔案,可以使用objdump指令來檢視其内容。
Documents$ objdump -sd hello.o -M intel
hello.o: 檔案格式 elf64-x86-64
Contents of section .text:
0000 f30f1efa 554889e5 488d3d00 000000b8 ....UH..H.=.....
0010 00000000 e8000000 00b80000 00005dc3 ..............].
Contents of section .rodata:
0000 48656c6c 6f20576f 726c6421 00 Hello World!.
Contents of section .comment:
0000 00474343 3a202855 62756e74 7520392e .GCC: (Ubuntu 9.
0010 332e302d 31307562 756e7475 32292039 3.0-10ubuntu2) 9
0020 2e332e30 00 .3.0.
Contents of section .note.gnu.property:
0000 04000000 10000000 05000000 474e5500 ............GNU.
0010 020000c0 04000000 03000000 00000000 ................
Contents of section .eh_frame:
0000 14000000 00000000 017a5200 01781001 .........zR..x..
0010 1b0c0708 90010000 1c000000 1c000000 ................
0020 00000000 20000000 00450e10 8602430d .... ....E....C.
0030 06570c07 08000000 .W......
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 8d 3d 00 00 00 00 lea rdi,[rip+0x0] # f <main+0xf>
f: b8 00 00 00 00 mov eax,0x0
14: e8 00 00 00 00 call 19 <main+0x19>
19: b8 00 00 00 00 mov eax,0x0
1e: 5d pop rbp
1f: c3 ret
此時由于還未進行連結,對象檔案中符号的虛拟位址無法确定,于是我們看到字元串“hello World”的位址為0x0000,作為參數傳遞字元串位址的rdi寄存器被設定為0x0,而“call puts”指令中函數puts()的位址被設定為下一條指令的位址0xe。
5 連結階段
GCC預設使用動态連結,添加編譯選項“static”可指定為靜态連結。
gcc hello.o -o hello -static