zoukankan      html  css  js  c++  java
  • 《深入理解计算机系统》☞hello world背后的故事

    一步到位的hello world


    首先一个简单的C语言版本的hello world例子,保存在文件hello.c中。

    #include <stdio.h>
    
    int main()
    {
        printf("hello world
    ");
    }
    

    一般而言,我们通常可以使用gcc命令将其转化为可执行程序

    gcc -o hello hello.c
    

    执行上面命令后,就会在当前目录生产一个hello的可执行文件。在Centos 64位机器上执行file hello,可以得到

    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 world。

    条分缕析的hello world


    为了说明C语言源程序是如何转化为最终的可执行文件,首先看下面这个图

    下面来分布讲解

    预处理(Preprocessor)阶段

    这个阶段处理#开头的指示语句,hello.c中的#include<stdio.h>告知预处理器去加载stdio.h的内容,并把它插入到当前位置。

    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结尾。

    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功能相同的程序,为了方便阅读,我这里手动换了下行

    --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在做链接时传入了这么多参数,至于其中的原因,就比较麻烦了,改日再写一篇文章介绍,今天先到这里。

    参考


  • 相关阅读:
    ELK Kafka json to elk
    ElasticSearch 日期赋值
    ELK Nxlog->Kafka->ElasticSearch
    Windows 安装Kafka
    图论路径简单算法
    鉴以往而知未来
    细微之处显文化
    Alt+数字键所能打出的符号表
    《愚公移山》与愚公精神
    Wikipedia's World
  • 原文地址:https://www.cnblogs.com/BinBinStory/p/7524165.html
Copyright © 2011-2022 走看看