The compilation process of GCC under Linux

Source: Internet
Author: User

development in Linux will inevitably use 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 using GCC to compile a program, the compilation process can be subdivided into four stages:
Pretreatment (pre-processing)
Compiling (compiling)
Compilation (assembling)
Links (linking)

1, preprocessing to the source code file contains (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 above, you can see that debug is replaced with predefined 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 can generate assembly code, this phase based on the input file generated by the. o suffix of the target file. The generated assembly code is as follows:

        . 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 pre-compiled and compiled with the. o suffix of the target file, this process generates the ELF format target code, using GCC-C for assembly

Use readelf-a hello.o to see detailed 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 executable code. There are two kinds of links, one is static link and the other is dynamic link. The advantage of using static links is that the dependent dynamic link libraries are less sensitive to the version of the dynamic-link library and have better compatibility; The disadvantage is that the generated programs are larger. The advantage of using dynamic linking is that the generated program is smaller and consumes less memory.

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


The compilation process of GCC under Linux

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.