深刻理解計算機之hello world背後的故事

最近打算鞏固計算機基礎知識,網上一本評價極高的教材——深刻理解計算機系統,下面要將講的內容來自第一章一個小例子,不過對咱們瞭解C語言如何從源程序到最終的可執行程序頗有幫助,下面讓咱們開始吧。html

一步到位的hello world

首先一個簡單的C語言版本的hello world例子,保存在文件hello.c中。java

#include <stdio.h>

int main()
{
    printf("hello world\n");
}

通常而言,咱們一般能夠使用gcc命令將其轉化爲可執行程序node

gcc -o hello hello.c

執行上面命令後,就會在當前目錄生產一個hello的可執行文件。在Centos 64位機器上執行file hello,能夠獲得linux

hello: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), not stripped

直接執行./hello便可在控制檯輸出hello worldc++

條分縷析的hello world

爲了說明C語言源程序是如何轉化爲最終的可執行文件,首先看下面這個圖編程

clipboard.png

下面來分佈講解bash

預處理(Preprocessor)階段

這個階段處理#開頭的指示語句,hello.c中的#include<stdio.h>告知預處理器去加載stdio.h的內容,並把它插入到當前位置。jvm

cpp hello.c > hello.i
file hello.i
# hello.i: ASCII C program text

編譯(Compiler)階段

這個階段把C語言源程序編譯爲彙編程序,不一樣高級語言經由其編譯器處理後,獲得的一樣的彙編語言。函數

cc -S hello.i   #會生成 hello.s 文件
file hello.s
# hello.s: ASCII assembler program text

組裝(Assembly)階段

這一階段把彙編語言翻譯爲機器碼,結果保存在稱爲relocatable object program/file的文件中,以ELF(Executable and Linkable Format)格式存儲(包含一個符號表,沒有striped過),通常以.o結尾。ui

as -o hello.o hello.s
file hello.o
# hello.o: ELF 64-bit LSB relocatable, AMD x86-64, version 1 (SYSV), not stripped

連接(Linking)階段

注意到咱們的hello.c程序使用了printf函數,它是由C語言的標準庫函數,由C語言編譯器提供,printf函數應該會存在於一個printf.o的文件中,咱們須要某種手段把它合併到咱們的hello.o中,連接器就是作這件事的。最終生成的爲一個稱爲executable object file的文件,它能夠被裝載進內存而且執行。

# -lc 指定加載libc.a
ld -o hello /usr/lib64/crt*.o hello.o -lc

若是按照上面方式操做,可執行文件hello可以建立出來,可是運行./hello會報錯

-bash: ./hello: /lib/ld64.so.1: bad ELF interpreter: No such file or directory

貌似是路徑不對,到這裏,你可能會想到gcc爲何可以一次成功,gcc是怎麼調用ld的呢?咱們能夠經過-v選項來查看gcc調用ld時的參數

$ gcc -v hello.o -o 123
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-thre
ads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj
-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --w
ith-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-55)
 /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/l
d-linux-x86-64.so.2 -o 123 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux
/4.1.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 -L/lib/../lib64 -L/usr/
lib/../lib64 hello.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.1.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crtn.o

這裏重點是collect2這句,由於collect2能夠看做ld功能相同的程序,爲了方便閱讀,我這裏手動換了下行

/usr/libexec/gcc/x86_64-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_x86_64 
--hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 
-o 123 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crti.o 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o 
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 
-L/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64 
-L/lib/../lib64 
-L/usr/lib/../lib64 hello.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.1.2/crtend.o 
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crtn.o

能夠看到,gcc在作連接時傳入了這麼多參數,至於其中的緣由,就比較麻煩了,改日再寫一篇文章介紹,今天先到這裏。

參考