深刻理解计算机之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在作连接时传入了这么多参数,至于其中的缘由,就比较麻烦了,改日再写一篇文章介绍,今天先到这里。

参考