GCC 詳細編譯步驟

使用 GCC 來編譯程式看起來相當容易,如下面的例子:

[neokent@FEDORA Projects]$ gcc hello.c -o hello
[neokent@FEDORA Projects]$ ./hello
Hello World!

但 GCC 到底做了些什麼事呢?多加一個「-v」的參數以後來試試看(下面是實際跑出來的例子,但是為了美觀起見,稍微修改了一下排版)

[neokent@FEDORA Projects]$ gcc -v hello.c -o hello
使用內建 specs。

目的:i386-redhat-linux

配置為:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=i386-redhat-linux

執行緒模型:posix

gcc 版本 4.1.2 20070925 (Red Hat 4.1.2-33)

/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -quiet -v hello.c -quiet -dumpbase hello.c -mtune=generic -auxbase hello -version -o /tmp/ccdKC85q.s

忽略不存在的目錄「/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include」

#include "..." 搜尋從這裡開始:
#include <...> 搜尋從這裡開始:
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/4.1.2/include
/usr/include
搜尋清單結束。

GNU C 版本 4.1.2 20070925 (Red Hat 4.1.2-33) (i386-redhat-linux)
由 GNU C 版本 4.1.2 20070925 (Red Hat 4.1.2-33) 編譯。

GGC 準則:--param ggc-min-expand=64 --param ggc-min-heapsize=64394
Compiler executable checksum: ab322ce5b87a7c6c23d60970ec7b7b31
as -V -Qy -o /tmp/ccepcDPL.o /tmp/ccdKC85q.s
GNU assembler version 2.17.50.0.18 (i386-redhat-linux) using BFD version version 2.17.50.0.18-1 20070731

/usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr --build-id -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o hello /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. /tmp/ccepcDPL.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o

稍微整理一下,去掉一些參數設定的部分,可以將上述的步驟簡化成下面幾步:
  1. /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 hello.c -o /tmp/ccdKC85q.s
  2. as -o /tmp/ccepcDPL.o /tmp/ccdKC85q.s
  3. ld -o hello -o /tmp/ccepcDPL.o
以上三個命令分別對應於編譯步驟中的預處理+編譯組譯連結。預處理和編譯還是放在了一個命令(cc1)中進行的,可以把它再次拆分為以下兩步:
  1. cpp -o hello.i hello.c
  2. cc1 hello.i -o /tmp/ccdKC85q.s
為了詳細分析每一個編譯過程,我們寫一個 Makefile 來進行細部分解:

all:hello

hello:hello.o
ld -dynamic-linker \
/lib/ld-linux.so.2 -o hello /usr/lib/crt1.o \
/usr/lib/crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o \
hello.o \
-lc /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/crtn.o

hello.o:hello.s
as -o hello.o hello.s

hello.s:hello.i
/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -o hello.s hello.c

hello.i:hello.c
cpp -o hello.i hello.c

clean:
rm -rf hello.i hello.s hello.o hello

可以很容易的看見,在 make 完之後,產生了 hello.i hello.s hello.o hello,以下就一個一個來看。

1. 預處理

hello.i

# 1 "hello.c"
# 1 ""
# 1 ""
...
__extension__ typedef signed long long int __int64_t;
__extension__ typedef unsigned long long int __uint64_t;
...
extern int remove (__const char *__filename) __attribute__ ((__nothrow__));
extern int rename (__const char *__old, __const char *__new) __attribute__ ((__nothrow__));
...
int main()
{
printf( "Hello World!\n" );
return;
}

從 hello.i 裡面可以看出,預處理器把所有要包含(include)的文件(包括遞歸包含的文件)的內容都添加到了C的原始碼中,然後把其輸出到輸出文件。要注意的是,就算是巨集(MACRO)也會完全被展開。
PS: 由於 hello.i 的內容實在太多了,反正所有的工具都已經列上去了,要看完整內容的話,就再跑一次吧。

2. 編譯

hello.s

.file "hello.c"
.section .rodata
.LC0:
.string "Hello World!"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
movl $.LC0, (%esp)
call puts
addl $4, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.2 20070925 (Red Hat 4.1.2-33)"
.section .note.GNU-stack,"",@progbits

非常明顯,這是組合語言。這個檔案比預處理後的C檔案小了很多,去除了很多不必要的東西,比如說沒用到的類型宣告和函數宣告等。

3. 組譯

將第二步輸出的組語碼翻譯成符合一定格式的機器代碼,在Linux上一般表現為ELF目標文件。

[neokent@FEDORA 20080109_CompileProcedure]$ file hello.o
hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

4. 連結

將上步生成的物件和系統函式庫的物件連結起來,最終生成了可以在特定平台運行的可執行文件。為什麼還要連接系統函式庫中的某些物件(crt1.o, crti.o等)呢?這些物件都是用來初始化或者回收 C 運行時環境的,比如說堆疊記憶體分配上下文環境的初始化等,實際上 crt 也正是C RunTime的縮寫。這也暗示了另外一點:程式並不是從main函數開始執行的,而是從 crt 中的某個入口開始的,在Linux上此入口是 _start。
PS: crt1.o and crti.o provide initialization code and crtn.o does cleanup.

如果要改成使用靜態連結的話,可以將 Makefile 改成下面這個樣子:

hello_s:hello.o
ld -m elf_i386 -static \
-o hello_s /usr/lib/crt1.o \
/usr/lib/crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbeginT.o \
-L/usr/lib/gcc/i386-redhat-linux/4.1.2/ -L/usr/lib \
hello.o --start-group -lgcc -lgcc_eh \
-lc --end-group /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/crtn.o

我們來比較動態連結執行檔和靜態連結執行檔:

-rwxrwxr-x 1 neokent neokent 4667 2008-01-10 00:04 hello
-rwxrwxr-x 1 neokent neokent 545648 2008-01-10 00:04 hello_s

strip 過後

-rwxrwxr-x 1 neokent neokent 3024 2008-01-10 00:09 hello
-rwxrwxr-x 1 neokent neokent 490276 2008-01-10 00:08 hello_s

很明顯,靜態連結大的多了~

留言

這個網誌中的熱門文章

我弟家的新居感恩禮拜分享:善頌善禱

如何將Linux打造成OpenFlow Switch:Openvswitch

Openssl 範例程式:建立SSL連線