The compilation process of GCC under Linux

Source: Internet
Author: User

The development of Linux will inevitably use the GCC compilation. GCC (GNU Compiler Collection. GNU compiler Set), is a programming language compiler developed by GNU. It is a free software issued by the GNU compiler bundle under the GPL license, and is a key part of the GNU program.

When you compile a program with GCC, the compilation process can be subdivided into four stages:
Pretreatment (pre-processing)
Compiling (compiling)
Compilation (assembling)
Links (linking)

1, preprocessing the files in the source file including (include), precompiled statements (such as macro definition define, etc.) for analysis, the compilation option is GCC-E *.c

#define DEBUG "Debug" int main () {  char *a = Debug;  return 1;}
After preprocessing, you can see that debug is replaced with pre-defined content.

# 1 "hello.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "hello.c" int main () {  char *a = "Debug";  return 1;}
2, compile calls CC1 to compile, using the GCC-S option to generate assembly code, this stage according to the input file generated by the. O is the suffix of the target file. The resulting assembly code such as the following:

        . File   "hello.c"        . Section        . Rodata. LC0:        . String "Debug"        . Text.globl main        . Type   Main, @functionmain:. LFB0:        . Cfi_startproc        pushq   %rbp        . Cfi_def_cfa_offset        6, -16        cfi_offset    RSP,%RBP        . Cfi_def_cfa_register 6        movq    $. LC0, -8 (%RBP)        MOVL $    ,%eax        leave        . CFI_DEF_CFA 7, 8        ret        . Cfi_endproc. LFE0:        . Size   Main,.-main        . Ident  "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"        .        section . Note. Gnu-stack, "", @progbits
3, assembly assembly process is for assembly language steps, call as to work. Generally speaking,. S is the suffix of the assembly language source code files and assembly,. s suffix assembly language files are precompiled and compiled after the target file is generated with an. o suffix. This process generates the object code in elf format, which is compiled using gcc-c

Use readelf-a hello.o to see the specific elf information

ELF header:magic:7f 4c------CLASS:ELF64 Data:                            2 ' s complement, little endian version:1 (current) Os/abi: Unix-system V ABI version:0 Type:rel (relocata  ble file) machine:advanced Micro Devices x86-64 version:0x1 Entry          Point address:0x0 start of program headers:0 (bytes to file) Start of section headers: 296 (bytes into file) flags:0x0 size of this header:64 (bytes) Size of  Program headers:0 (bytes) Number of the program headers:0 Size of the Section headers:64 (bytes)              Number of section headers:13 section header string Table index:10section headers: [Nr] Name Type    Address       Offset Size entsize Flags Link Info Align [0] NULL 0 000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [1]. Text Progbit        S 0000000000000000 00000040 0000000000000013 0000000000000000 AX 0 0 4 [2]. Rela.text RELA 0000000000000000 00000568 0000000000000018 0000000000000018 1 8 [3]. da     Ta progbits 0000000000000000 00000054 0000000000000000 0000000000000000 WA 0 0      4 [4]. BSS nobits 0000000000000000 00000054 0000000000000000 0000000000000000 WA 0   0 4 [5]. Rodata progbits 0000000000000000 00000054 0000000000000006 0000000000000000 A 0 0 1 [6]. Comment progbits 0000000000000000 0000005a 000000000000002d 000000  0000000001 MS 0   0 1 [7]. Note.  Gnu-stack progbits 0000000000000000 00000087 0000000000000000 0000000000000000 0 0 1     [8]. eh_frame progbits 0000000000000000 00000088 0000000000000038 0000000000000000 A 0          0 8 [9]. rela.eh_frame rela 0000000000000000 00000580 0000000000000018 0000000000000018 8 8 [ten]. Shstrtab strtab 0000000000000000 000000c0 0000000000000061 000000000   0000000 0 0 1 [one]. Symtab symtab 0000000000000000 00000468 00000000000000f0 0000000000000018 9 8 []. strtab strtab 0000000000000000 00000558 00000 0000000000e 0000000000000000 0 0 1Key to Flags:w (write), A (Alloc), X (execute), M (merge), S (Strin GS) I (info), L (link order), G (group), X (unknown) O (extra OS processing required) O (OS specific), p (processor spec IFIC) There is no section groups in this file. There is no program headers in this file. Relocation section '. Rela.text ' at offset 0x568 contains 1 entries:offset Info Type Sym. Va Lue Sym. Name + Addend000000000008 00050000000b r_x86_64_32s 0000000000000000. Rodata + 0Relocation section '. rel A.eh_frame ' at offset 0x580 contains 1 entries:offset Info Type Sym. Value Sym. Name + A ddend000000000020 000200000002 r_x86_64_pc32 0000000000000000. Text + 0There is no unwind sections in this file. Symbol table '. Symtab ' contains entries:Num:Value Size Type Bind Vis Ndx Name 0:00000000 00000000 0 Notype Local default UND 1:0000000000000000 0 FILE Local default ABS hello.c 2:00000 00000000000 0 Section Local default 1 3:0000000000000000 0 section local default 3 4:000000000 0000000 0 Section LOCAL DEFAULT 4 5:00000000000000 section local default 5 6:0000000000000000 0 section local default 7 7:0000000000000000 0 section local default 8 8:0000000000000000 0 section Local default 6 9:0000000000000000 1 9 FUNC GLOBAL DEFAULT 1 Main


4, Link link process. Generate the code that can be run. There are two kinds of links, one is static link, the second is dynamic link.

The advantage of using static links is that there are fewer dynamic-link libraries to rely on, and that the version number of a dynamic-link library is not very sensitive and has good compatibility. The disadvantage is that the generated program is relatively large. The advantage of using dynamic linking is that the resulting program is smaller and consumes less memory.

GCC Hello.o-o Hello will be able to complete the final link operation and generate a running file, as to how to generate dynamic and static libraries, and how to link dynamic and static libraries, will be introduced later.


The compilation process of GCC under Linux

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.