计算机原理系列之六 -------- 可执行文件详解

本文通过实例详细解析了可执行文件的生成过程及ELF格式结构,包括ELF header和section的具体内容,深入理解可执行文件的内部组成。

欢迎访问我的个人博客: luomuxiaoxiao.com



前面的文章我们详细的讲解了.o文件的结构及其编译过程,这篇文章我们从ELF文件的观点来分析可执行文件的结构。

一、生成可执行文件

我们以下面两个文件为例来研究多个文件生成一个可执行文件的过程。一个文件名为add.c,主要内容包含一个add函数,该函数功能为:将调用次数加1,然后,返回传递进来的两个参数之和。

extern int times;

int add(int n1, int n2) {
    times++;
    return n1 + n2;
}

另一个文件名为main.c,其功能为:调用两次add函数,并打印出调用次数和add函数的返回值。

#include <stdio.h>

extern int add(int, int);
int times = 0;

int main(void)
{
    int a = 2;
    int b = 3;

    printf("%d, The sum of a and b is %d\n", times, add(a,b));
    printf("%d, The sum of a and b is %d\n", times, add(a,b));
    return 0;
}

由于这次我们的主要研究对象是可执行文件,因此,使用下面的命令编译这两个文件:

gcc -c add.c -o add.o
gcc -c main.c -o main.o
gcc -o add add.o main.o

这样我们生成了链接前的两个输入文件:add.omain.o,以及链接之后的可执行文件add。其中.o的生成在可重定位文件详解编译过程分析已经做过类似的分析,这里就不再做过多的解释。

二、可执行文件结构

2.1 可执行文件的ELF header

下面我们开看一下生成的可执行文件add的结构,由于可执行文件也属于ELF格式的文件,因此,按照惯例,我们依然先使用readelf工具从文件头开始分析。

$ readelf -h add
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x400430
  Start of program headers:          64 (bytes into file)
  Start of section headers:          6712 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         31
  Section header string table index: 28

我们分析一下字段:

  • Type: EXEC (Executable file): 该文件是一个可执行文件;
  • Entry point address: 0x400430: 文件的入口地址为0x400430,该地址是代码段的起始地址;
  • xxx of program headers: xxx: 该文件中共有9个program header,每个的大小为56字节;
  • xxx of section headers: xxx: 该文件中共有31个section header,每个的大小为64字节。

这里可以看出,经过链接之后,生成了可执行文件,该文件的结构具有了如详解ELF文件中描述的完整的ELF文件结构。

2.2 可执行文件的section

请点击此处继续阅读


想第一时间查看我的文章吗?请关注我的微信公众号号,搜索“落木萧萧技术论坛”或登陆我的个人博客:www.luomuxiaoxiao.com,更多精彩文章等你。

qrcode
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值