天天看点

菜狗杂谈----深入理解计算机系统ELF

ELF

0x1.简介

首先,有一种文件格式叫做对象文件(Object File),ELF(Executable Linkable Format)是Linux下对象文件一种存储格式,同时,遵从这种格式的文件也被称为ELF。顺便提一句,在windows下文件格式为PE,两者统一等级。

ELF包括二进制文件、可执行文件、目标代码、共享库和核心转储格式文件,Linux下的目标文件和可执行文件都按照该格式进行存储

ELF分为3种类型:

可重定位文件(Relocatable file):

编译器生成的.o文件,由链接器处理

包含代码与数据,可以和其他目标文件链接生成一个可执行文件或共享目标文件(不同命令编译生成静态库或动态库)

可执行文件(Executable file):

链接器对.o文件进行处理输出的文件,进程映像

在Linux中,可执行文件分为两种:

  1. Executable Object File,如编辑器vim,调试神器gdb等
  2. Executable shell,也就是shell脚本。但有一点需要区分:shell脚本本身只是一个文本文件,解释执行shell脚本的程序(如ubuntu的bash shell)才是可执行文件

共享目标文件(Shared Object File):

即动态库文件,后缀名为.so

相对于静态库而言,拥有更强的灵活性与可更新性。将模块化开发嵌入底层代码,使资源共享更为方便。但也拥有着对资源文件的强依赖性。

注意:

如今新版gcc为了安全,默认加入命令参数–enable-default-pie,引入了pie保护。因此直接使用gcc -o编译出的文件使用file命令查看时文件类型为Shared Object File,该参数使程序初始运行时在内存中的位置随机化,以防止return2libc和ROP等栈溢出攻击。如果想生成可执行文件,可选择手动关闭引入pie,只需在gcc命令中增加命令参数-no-pie即可

0x2.文件结构

ELF文件使用2种视图来展示文件结构:链接视图 和 执行视图

链接视图:静态链接器(即编译后参与生成最终ELF过程的链接器,如ld )会以链接视图解析 ELF。编译时生成的 .o(目标文件)以及链接后的 .so (共享库)均可通过链接视图解析

执行视图:动态链接器会以执行视图解析ELF并动态链接

区别:链接视图的程序头表(Program Header Table)是可选的(optional),而执行视图的程序头表必须存在

可视化结构图如下:

菜狗杂谈----深入理解计算机系统ELF

本次介绍所使用ELF文件示范用例:main.c test.c,代码如下:

main.c

/*main.c*/
#include <stdio.h>

int add(int, int);

int main(){
	printf("1");
	return add(20, 13);
}
           

test.c

/*test.c*/
int add(int i, int j){
	int x = i + j;
	return x;
}
           

使用

gcc -s main.c test.c

生成汇编文件main.s和test.s

使用

gcc -c main.s test.s

生成可重定位文件main.o和test.o

使用

gcc main.o test.o - o qwer.out

生成可执行文件qwer.out(gcc默认名称a.out,使用-o改变输出文件名)

使用

readelf

命令查看elf文件

ELF文件主要分4部分:
  1. ELF 文件头(ELF Header) ---- 在对象文件格式最前面,包含描述整个文件的基本属性

    表明这是一个ELF文件,定义如下(官方文档做了很详细的解释)

typedef struct{
	unsigned char e_ident[EI_NIDENT];   /* Magic number and other info */
	Elf32_Half e_type;   /* Object file type */
	Elf32_Half e_machine;  /* Architecture */
	Elf32_Word e_version; /* Object file version */
	Elf32_Addr e_entry; /* Entry point virtual address */
	Elf32_Off e_phoff;  /* Program header table file offset*/
	Elf32_Off e_shoff;  /* Section header table file offset*/
	Elf32_Word e_flags; /* Processor-specific flags */
	Elf32_Half e_ehsize; /* ELF header size in bytes */
	Elf32_Half e_phentsize;  /* Program header table entry size */
	Elf32_Half e_phnum; /* Program header table entry count */
	Elf32_Half e_shentsize;  /* Section header table entry size */
	Elf32_Half e_shnum; /* Section header table entry count */
	Elf32_Half e_shstrndx; /* Section header string table iunt */
} Elf32_Ehdr;


typedef struct{
	unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
	Elf64_Half e_type; /* Object file type */
	Elf64_Half e_machine; /* Architecture */
	Elf64_Word e_version; /* Object file version */
	Elf64_Addr e_entry; /* Entry point virtual address */
	Elf64_Off e_phoff;/* Program header table file offset*/
	Elf64_Off e_shoff;/* Section header table file offset*/
	Elf64_Word e_flags; /* Processor-specific flags */
	Elf64_Half e_ehsize; /* ELF header size in bytes */
	Elf64_Half e_phentsize;/* Program header table entry size */
	Elf64_Half e_phnum;/* Program header table entry count */
	Elf64_Half e_shentsize;/* Section header table entry size */
	Elf64_Half e_shnum; /* Section header table entry count */
	Elf64_Half e_shstrndx; /* Section header string table index */
} Elf64_Ehdr;
           

e_ident 保存着 ELF 的幻数和其他信息:

最前面四个字节是幻数,用字符串表示为 \177ELF

其后的字节如果是 32 位则是 ELFCLASS32 (1),如果是 64 位则是 ELFCLASS64 (2)

再其后的字节表示端序,小端序为 ELFDATA2LSB (1),大端序为 ELFDATA2LSB (2)

最后一个字节则表示 ELF 的版本

示例

使用readelf - h命令查看qwer.out的文件头(ubuntu 18.04 LTS)

菜狗杂谈----深入理解计算机系统ELF
  1. 程序头表(Program Header Table) ---- 告诉系统怎样创建一个进程映像

    程序头表是由 ELF 头的 e_phoff 和 e_phentsize 、e_phnum共同确定大小的表格组成

typedef struct{
	Elf32_Word p_type;/* Segment type */
	Elf32_Off p_offset;/* Segment file offset */
	Elf32_Addr p_vaddr; /* Segment virtual address */
	Elf32_Addr p_paddr; /* Segment physical address */
	Elf32_Word p_filesz; /* Segment size in file */
	Elf32_Word p_memsz; /* Segment size in memory */
	Elf32_Word p_flags; /* Segment flags */
	Elf32_Word p_align; /* Segment alignment */
	Elf64_Word p_type; /* Segment type */
	Elf64_Word p_flags; /* Segment flags */
} Elf32_Phdr;

typedef struct{
	Elf64_Off p_offset;/* Segment file offset */
	Elf64_Addr p_vaddr; /* Segment virtual address */
	Elf64_Addr p_paddr; /* Segment physical address */
	Elf64_Xword p_filesz; /* Segment size in file */
	Elf64_Xword p_memsz; /* Segment size in memory */
	Elf64_Xword p_align; /* Segment alignment */
} Elf64_Phdr;
           

e_phoff 指定的偏移量

e_phentsize 表示表格中程序头的大小,

e_phnum表示表格中程序头的数量

示例

使用

readelf -l qwer.out

命令查看程序头表

菜狗杂谈----深入理解计算机系统ELF
  1. 段(Section) ---- 包含链接视图中大量的对象文件信息
  2. 段表(Section Header Table) ---- 包含描述文件中所有段的信息

    段表(Section Header Table)是一个以 Elf32_Shdr结构体为元素的数组,每个结构体对应一个段,它描述了各个段的信息

typedef struct
{
Elf32_Word sh_name; /* Section name (string tbl index) */
Elf32_Word sh_type; /* Section type */
Elf32_Word sh_flags; /* Section flags */
Elf32_Addr sh_addr; /* Section virtual addr at execution */
Elf32_Off sh_offset;/* Section file offset */
Elf32_Word sh_size; /* Section size in bytes */
Elf32_Word sh_link; /* Link to another section */
Elf32_Word sh_info; /* Additional section information */
Elf32_Word sh_addralign;/* Section alignment */
Elf32_Word sh_entsize; /* Entry size if section holds table */
} Elf32_Shdr;

typedef struct
{
Elf64_Wordsh_name; /* Section name (string tbl index) */
Elf64_Word sh_type; /* Section type */
Elf64_Xword sh_flags; /* Section flags */
Elf64_Addr sh_addr; /* Section virtual addr at execution */
Elf64_Off sh_offset;/* Section file offset */
Elf64_Xword sh_size; /* Section size in bytes */
Elf64_Word sh_link; /* Link to another section */
Elf64_Word sh_info; /* Additional section information */
Elf64_Xword sh_addralign;/* Section alignment */
Elf64_Xword sh_entsize;/* Entry size if section holds table */
} Elf64_Shdr;
           

ELF 文件头的部分成员变量给出了段表的部分属性:

e_shoff成员给出了段表在 ELF 中的偏移

e_shnum 成员给出了段描述符的数量

e_shentsize 给出了每个段描述符的大小

使用

readelf -S qwer.out

查看段表信息

菜狗杂谈----深入理解计算机系统ELF

0x3示例

继续阅读