欢迎访问我的个人博客: 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.o
和main.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,更多精彩文章等你。
