当前位置:网站首页>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
边栏推荐
- SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
- MySQL: the difference between insert ignore, insert and replace
- [Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
- cookie、session、tooken
- golang中的iota
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- Using SqlCommand objects in code
- PyTorch学习记录
- [QT] test whether QT can connect to the database
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
猜你喜欢
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

【.Net Core】程序相关各种全局文件

Windows10 install WSL (I) (wslregisterdistribution error)

多表操作-一对一,一对多与多对多

Concepts of dictionary, hash table and array

Digital transformation has a long way to go, so how to take the key first step

使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object

BlocProvider为什么感觉和Provider很相似?

E-commerce RPA robot helps brand e-commerce to achieve high traffic

关联性——组内相关系数
随机推荐
Openvino model performance evaluation tool DL workbench
Jielizhi, production line assembly link [chapter]
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
VIM color the catalogue
ADO.NET 之sqlConnection 对象使用摘要
【.Net Core】程序相关各种全局文件
const // It is a const object... class nullptr_ t
How excel opens CSV files with more than one million lines
UDS bootloader of s32kxxx bootloader
牛客-练习赛101-推理小丑
Is there a piece of code that makes you convinced by human wisdom
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
Learn online case practice
使用htaccess文件禁止目录里的脚本执行权限
S32Kxxx bootloader之UDS bootloader
Jielizhi, production line assembly link [chapter]
Iota in golang
Relatively easy to understand PID understanding
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
