C程序的基礎系統代碼

一、簡單C程序的構建過程:

[root@test]# gcc -v  hello.c 

//首先是gcc蒐集系統信息
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix


//接下來編譯器cc1開始工作了,他的任務是將hello.c編譯成.s文件。

//首先顯示的是gcc自己的版本信息,顯示收集到的編譯選項(其中-v是我們命令行傳入的,其他的是他自己採用的)。

//然後顯示gcc 調用cc1的命令行。由於命令行帶了-v,所以接下來cc1也詳細輸出了自己的工作過程。這裏生成的彙編文件名爲/tmp/ccI5PLHj.s


gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'

/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/cc1 -quiet -v hello.c -quiet -dumpbase hello.c -mtune=generic -march=x86-64 -auxbase byte_order -version -o /tmp/ccI5PLHj.s

GNU C (GCC) version 4.8.2 20140120 (Red Hat 4.8.2-16) (x86_64-redhat-linux)
        compiled by GNU C version 4.8.2 20140120 (Red Hat 4.8.2-16), GMP version 5.1.1, MPFR version 3.1.1, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/include
 /usr/local/include
 /usr/include
End of search list.


//接下來彙編器as開始工作了。同樣是gcc先顯示相關信息,然後調用as的命令行。由於帶了-v選項,as也輸出了自己的相關信息。最終生成了目標文件/tmp/ccVS4a59.o
GNU C (GCC) version 4.8.2 20140120 (Red Hat 4.8.2-16) (x86_64-redhat-linux)
        compiled by GNU C version 4.8.2 20140120 (Red Hat 4.8.2-16), GMP version 5.1.1, MPFR version 3.1.1, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 16ae14b2bdacde6ba28f48ff4516d12b
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'

 as -v --64 -o /tmp/ccVS4a59.o /tmp/ccI5PLHj.s

GNU assembler version 2.23.2 (x86_64-unknown-linux-gnu) using BFD version (GNU Binutils) 2.23.2


//最後鏈接器登場。可見gcc調用的是collect2完成鏈接。collect2最終估計還是調用ld完成實際的鏈接操作。但這裏看不出來。
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.2/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.2/:/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.8.2/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../.. /tmp/ccVS4a59.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.8.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crtn.o



二、C程序包含的基礎目標文件:

從上一節的最後一步,可以看出,有4個基礎的目標文件,被鏈接到了最終的可執行程序中。他們是:

/usr/lib64/crt1.o
/usr/lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/crtbegin.o   
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/crtend.o 
/usr/lib64/crtn.o

通過objdump可以看出,crt1.o中包含了符號_start。而通過ld --verbose命令,可以看出默認鏈接腳本中,有ENTRY(_start)這一項。這些信息說明,用戶態程序加載後,第一條被執行的指令位於_start符號處。而_start符號又位於crt1.o中



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章