GCC compilation process

Source: Internet
Author: User

We usually use GCC to compile C Programs.
GCC hello. C-O hello and so on. At most for GDB debugging, use the-G parameter!

In fact, we can use manual compilation to understand the entire GCC compilation process!

Let's use the simplest Hello World Program as an example:

#include "stdio.h"int main(){    printf("Hello World\n");    return  0;}
1. Pre-processing)

At this stage, the compiler compiles the header files in C source code, such as stdio. H. You can use the GCC option "-e" to view them.

Usage: gcc-e hello. C-O hello. I

Purpose: output the hello. c preprocessing file hello. I.

#lshello.c  #gcc -E hello.c -o hello.i#lshello.c  hello.i

Using Vim to view hello. I, we can see that hello. I contains many header files:

#1 "hello. C "#1" <command line> "#1" hello. C "#1"/usr/include/stdio. H "1 3 4 #28"/usr/include/stdio. H "3 4 #1"/usr/include/features. H "1 3 4 #324"/usr/include/features. H "3 4 #1"/usr/include/i386-linux-gnu/bits/predefs. H "1 3 4 #325"/usr/include/features. H "2 3 4 #357"/usr/include/features. H "3 4 ...... Extern void funlockfile (File * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); #940 "/usr/include/stdio. H "3 4 #2" hello. C "2int main () {printf (" Hello world \ n "); Return 0 ;}

We can also use gcc-M hello. C to generate file association information. Contains all source code that the target file depends on.

#lshello.c  hello.i#gcc -M hello.c hello.o: hello.c /usr/include/stdio.h /usr/include/features.h \ /usr/include/i386-linux-gnu/bits/predefs.h \ /usr/include/i386-linux-gnu/sys/cdefs.h \ /usr/include/i386-linux-gnu/bits/wordsize.h \ /usr/include/i386-linux-gnu/gnu/stubs.h \ /usr/include/i386-linux-gnu/gnu/stubs-32.h \ /usr/lib/gcc/i686-linux-gnu/4.7/include/stddef.h \ /usr/include/i386-linux-gnu/bits/types.h \ /usr/include/i386-linux-gnu/bits/typesizes.h /usr/include/libio.h \ /usr/include/_G_config.h /usr/include/wchar.h \ /usr/lib/gcc/i686-linux-gnu/4.7/include/stdarg.h \ /usr/include/i386-linux-gnu/bits/stdio_lim.h \ /usr/include/i386-linux-gnu/bits/sys_errlist.h#

2. Compiling)

The second step is the compilation phase. In this phase, GCC should first check the code standardization and whether there are syntax errors to determine the actual work to be done by the Code, after checking that the code is correct, GCC translates the code into an assembly language. You can use the "-s" option to view the Code. This option is only compiled but not compiled to generate the assembly code.
Usage: gcc-s -- verbose-ASM hello. I-O hello. s
Purpose: Compile the pre-processing output file hello. I into a hello. s file.

#gcc -S --verbose-asm hello.i -o hello.s#lshello.c  hello.i  hello.s

Use Vim to view the hello. s file:

. File "hello. C "# gnu c (Ubuntu/linaro 4.7.2-2ubuntu1) version 4.7.2 (i686-linux-gnu) # compiled by gnu c version 4.7.2, GMP version 5.0.2, mpfr version 3.1.0-P3, MPC version 0.9 # GGC Heuristics: -- Param GGC-Min-expand = 100 -- Param GGC-Min-heapsize = 131072 # transfer option:-fpreprocessed hello. i-mtune = generic-March = i686 #-auxbase-strip hello. s-fverbose-ASM-fstack-Protector # enabled options: -fasynchronous-Unwind-tables-fauto-inc-Dec #-fbranch-count-reg-fcommon-fdebug-types-section #-fdelete-Null-pointer-checks-fdwarf2-cfi-asm-fearly -inlining #-feliminate-unused-debug-types-ffunction-CSE-fgcse-lm-fgnu-runtime #-fident-Finline-Atomics-Fira-share-save-slots-Fira -Share-spill-slots #-fivopts-fkeep-static-consts-fleading-underscore-fmath-errno #-fmerge-debug-strings-fmove-loop-invariants-fpcc-struct -return #-fpeephole-fprefetch-loop-Arrays-fsched-critical-path-heuristic #-fsched-dep-count-heuristic-fsched-group-heuristic-fsched-interblock #- fsched-last-insn-heuristic-fsched-Rank-heuristic-fsched-spec #-fsched-spec-insn-heuristic-fsched-Stalled-insns-dep-fshow-column #- fsigned-Zeros-fsplit-IVS-in-unroller-fstack-Protector #-fstrict-volatile-bitfields-ftrapping-math-ftree-cselim-ftree-forwprop #-ftree-loop- if-convert-ftree-loop-im-ftree-loop-ivcanon #-ftree-loop-optimize-ftree-parallelize-loops =-ftree-phipdrop-ftree-PTA #-ftree -reassoc-ftree-scev-cprop-ftree-SLP-vectorize #-ftree-vect-loop-version-funit-at-a-time-funwind-tables #-fvect-cost -model-fverbose-ASM-fzero-initialized-in-BSS-M32-m80387 #-m96bit-long-double-maccumulate-outgoing-ARGs-malign-stringops #-mfancy-math-387-mfp-ret-in-387-mglibc-mieee -FP-MnO-red-zone #-mno-sse4-mpush-ARGs-msahf-MTLS-direct-seg-refs. section. rodata. lc0 :. string "Hello World ". text. globl main. type main, @ functionmain :. lfb0 :. cfi_startproc pushl % EBP #. cfi_def_cfa_offset 8. cfi_offset 5,-8 movl % ESP, % EBP #,. cfi_def_cfa_register 5 andl $-16, % ESP #, subl $16, % ESP #, movl $. lc0, (% ESP) #, call puts # movl $0, % eax #, d.1815 leave. cfi_restore 5. cfi_def_cfa 4, 4 RET

3. assembly phase)

In the Assembly phase, the ". s" file generated in the compilation phase is converted to the binary target code.
Usage:
Gcc-C hello. S-O hello. o

Purpose: Compile the output file test. s and output the file test. O.

#lshello.c  hello.i  hello.s#gcc -c hello.s -o hello.o#lshello.c  hello.i  hello.o  hello.s

Use File hello. O to view the hello. o file information:

#file hello.o hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped#

We can also use objdump to view the assembly information of Hello. O: objdump-d hello. o

# Objdump-d hello. O hello. o: File Format elf32-i386Disassembly of section. text: 00000000 <main>: Push % EBP 1: 89 E5 mov % ESP, % EBP 3: 83 E4 F0 and $0xfffffff0, % ESP 6: 83 EC 10 sub $0x10, % ESP 9: C7 04 24 00 00 00 00 movl $0x0, (% ESP) 10: e8 fc ff call 11 <main + 0x11> 15: B8 00 00 00 00 mov $0x0, % eax 1a: C9 leave 1b: C3 RET #

This is the same as how we use disassemble for debugging with GDB!

(gdb) disassemble Dump of assembler code for function main:   0x0804840c <+0>:push   %ebp   0x0804840d <+1>:mov    %esp,%ebp   0x0804840f <+3>:and    $0xfffffff0,%esp   0x08048412 <+6>:sub    $0x10,%esp=> 0x08048415 <+9>:movl   $0x80484c8,(%esp)   0x0804841c <+16>:call   0x80482f0 <puts@plt>   0x08048421 <+21>:mov    $0x0,%eax   0x08048426 <+26>:leave     0x08048427 <+27>:ret    End of assembler dump.(gdb) 

4. Link)


After successful compilation, it enters the link stage. No option link.

Usage: gcc-V hello. O-O hello

Purpose: link the compiled output file hello. oto the final Executable File hello.

# Use built-in specs for GCC-V hello. O-O hello. Collect_gcc = gcccollect_lto_wrapper =/usr/lib/GCC/i686-linux-gnu/4.7/Lto-wrapper target: i686-linux-gnu configured :.. /src/configure-V -- With-pkgversion = 'ubuntu/linaro 4.7.2-2ubuntu1' -- With-bugurl = file: // usr/share/doc/gcc-4.7/readme. bugs -- enable-languages ages = C, C ++, go, Fortran, objc, OBJ-C ++ -- prefix =/usr -- Program-suffix =-4.7 -- enable-shared -- enable-linker-Build-id -- With-system-zlib -- libexecdir =/usr /lib -- without-included-gettext -- enable-threads = POSIX -- With-gxx-include-Dir =/usr/include/C ++/4.7 -- libdir =/usr/lib -- enable-NLS -- With-sysroot =/-- enable-clocale = GNU -- enable-libstdcxx-Debug -- enable-libstdcxx-time = yes -- enable-GNU-unique-object -- enable -plugin -- enable-objc-GC -- enable-targets = all -- disable-werror -- With-arch-32 = i686 -- With-tune = generic -- enable-checking = release -- Build = i686-linux-gnu -- Host = i686-linux-gnu -- target = i686-linux-gnu thread model: posixgcc version 4.7.2 (Ubuntu/linaro 4.7.2-2ubuntu1) compiler_path =/usr/lib/GCC/i686-linux-gnu/4.7/:/usr/lib/GCC/i686-linux-gnu/4.7 /: /usr/lib/GCC/i686-linux-gnu/:/usr/lib/GCC/i686-linux-gnu/4.7 /: /usr/lib/GCC/i686-linux-gnu/LIBRARY_PATH =/usr/lib/GCC/i686-linux-gnu/4.7/:/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /i386-linux-gnu/:/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /.. /lib/:/lib/i386-linux-gnu //:/lib /.. /lib/:/usr/lib/i386-linux-gnu/:/usr/lib /.. /lib/:/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /:/lib /: /usr/lib/collect_gcc_options = '-v''-O ''Hello''-mtune = generic ''-March = i686'/usr/lib/GCC/i686-linux-gnu/4.7 /collect2 -- sysroot =/-- Build-id -- no-add-needed -- as-needed -- Eh-frame-HDR-M elf_i386 -- hash-style = GNU-dynamic-linker/ LIB/ld-linux.so.2-Z relro-O hello/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /i386-linux-gnu/crt1.o/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /i386-linux-gnu/crti. o/usr/lib/GCC/i686-linux-gnu/4.7/crtbegin. o-L/usr/lib/GCC/i686-linux-gnu/4.7-L/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /i386-linux-gnu-L/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /.. /lib-L/lib/i386-linux-gnu-L/lib /.. /lib-L/usr/lib/i386-linux-gnu-L/usr/lib /.. /lib-L/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. hello. o-lgcc -- as-needed-lgcc_s -- no-as-needed-LC-lgcc -- as-needed-lgcc_s -- no-as-needed/usr/lib/GCC/i686-linux-gnu/ 4.7/crtend. o/usr/lib/GCC/i686-linux-gnu/4.7 /.. /.. /.. /i386-linux-gnu/crtn. O #

We can use nm hello to view the used symbol information.

#nm hello0804a020 B __bss_start0804a020 b completed.63820804a018 D __data_start0804a018 W data_start08048350 t deregister_tm_clones080483c0 t __do_global_dtors_aux08049f0c t __do_global_dtors_aux_fini_array_entry0804a01c D __dso_handle08049f14 d _DYNAMIC0804a020 D _edata0804a024 B _end080484a8 T _fini080484c0 R _fp_hw080483e0 t frame_dummy08049f08 t __frame_dummy_init_array_entry080485c8 r __FRAME_END__0804a000 d _GLOBAL_OFFSET_TABLE_         w __gmon_start__080484a2 T __i686.get_pc_thunk.bx080482b0 T _init08049f0c t __init_array_end08049f08 t __init_array_start080484c4 R _IO_stdin_used         w _ITM_deregisterTMCloneTable         w _ITM_registerTMCloneTable08049f10 d __JCR_END__08049f10 d __JCR_LIST__         w _Jv_RegisterClasses080484a0 T __libc_csu_fini08048430 T __libc_csu_init         U __libc_start_main@@GLIBC_2.00804840c T main         U puts@@GLIBC_2.008048380 t register_tm_clones08048320 T _start0804a020 D __TMC_END__#


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.