当前位置:网站首页>GCC compilation
GCC compilation
2022-07-02 00:00:00 【Day-3】
1 GCC The build process
First let's look at GCC Compilation process .hello.c The contents of the document are as follows .
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
gcc hello.c -o hello -save-temps --verbose
Add two compilation options when compiling , The first option is to save the intermediate files generated during compilation , The second is for viewing GCC Detailed workflow of compilation .
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 Assembly version 2.34 (x86_64-linux-gnu) Use BFD edition (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’
In this message , We can find out .GCC The compilation process mainly includes four processes , Pretreatment stage (Preprocess), Compile phase (Compile), assembly (Assemble) And link (Link), Used separately ccll,as and collect2.
among ,ccl It's a compiler , Used to convert source files hello.c Compiled into hello.s;as It's an assembler , take hello.s Compiled as hello.o, The linker collect It's right ld Encapsulation of commands , Is used to C The target file in the language runtime library and the required dynamic link library are linked to the executable hello.
2 Pretreatment stage
It mainly deals with # Preprocessing instructions at the beginning , Insert it directly into the program text after conversion , Get another C Program , Usually, the i As extension , Add compilation options to the command e Preprocessing can be performed separately .
gcc -E hello.c -o hello.i
Some processing rules :
- Recursive processing “#include” Preprocessing instruction , Copy the corresponding file contents to the location of the instruction .
- Delete all “”define# Instructions , And recursively expand all macro definitions at the referenced position
- Process all conditional preprocessing instructions
- Delete all comments
- Add line number and filename ID
3 Compile phase
Execute instructions separately
gcc -S hello.i -o -masm=intel -fno-asynchronous-unwind-tables
The first compilation option is to specify the file as we are familiar with intel, The second compilation option is used to generate a non cfi Macro assembly instructions , To improve readability .
Generated hello.s Contents of the file :
.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 Assembly stage
gcc -c hello.c -o hello.o
gcc -c hello.s -o hello.o
Both instructions are ok . The target file at this point hello.o Is a relocatable file , have access to objdump Command to view its contents .
Documents$ objdump -sd hello.o -M intel
hello.o: File format 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
At this time, because there is no link , The virtual address of the symbol in the object file cannot be determined , So we see the string “hello World” The address for 0x0000, Passing a string address as a parameter rdi The register is set to 0x0, and “call puts” Function in instruction puts() The address of is set to the address of the next instruction 0xe.
5 Link phase
GCC Dynamic links are used by default , Add compile options “static” Can be specified as a static link .
gcc hello.o -o hello -static
边栏推荐
- openwrt 开启KV漫游
- 多表操作-一对一,一对多与多对多
- [embedded system course design] a single key controls the LED light
- 【QT】对于Qt MSVC 2017无法编译的问题解决
- Correlation - intra group correlation coefficient
- Concepts of dictionary, hash table and array
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- Which securities company is the best to open a stock account? Is there a security guarantee
- PostgreSQL notes (10) dynamically execute syntax parsing process
- How to solve the image pop-up problem when pycharm calls Matplotlib to draw
猜你喜欢
【ES实战】ES上的安全性运行方式
Algolia's search needs are almost closed
【QT】QtCreator卸载与安装(非正常状态)
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
Correlation - intra group correlation coefficient
起床困难综合症(按位贪心)
Redis AOF log
Openvino model performance evaluation tool DL workbench
Difficult to get up syndrome (bit by bit greed)
Review data desensitization system
随机推荐
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
Iota in golang
Pytorch learning record
Relatively easy to understand PID understanding
Why does blocprovider feel similar to provider?
Jielizhi, production line assembly link [chapter]
Concepts of dictionary, hash table and array
Redis AOF log
【QT】對於Qt MSVC 2017無法編譯的問題解决
Is it safe to buy funds on Great Wall Securities?
vs2015 AdminDeployment.xml
TS初次使用、ts类型
Difficult to get up syndrome (bit by bit greed)
时间复杂度与空间复杂度
MySQL Replication中并行复制怎么实现
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
【QT】测试Qt是否能连接上数据库
Jielizhi Bluetooth headset quality control and production skills [chapter]
Correlation - intra group correlation coefficient
Learn online case practice