From 099879d99078f34eaf1aee7d330751c7a32cd419 Mon Sep 17 00:00:00 2001 From: mudongliang Date: Sun, 27 Sep 2015 14:38:17 +0800 Subject: [PATCH] translate the former two parts of elf file format Signed-off-by: mudongliang --- Theory/ELF.md | 72 ++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/Theory/ELF.md b/Theory/ELF.md index 783a04c..b856101 100644 --- a/Theory/ELF.md +++ b/Theory/ELF.md @@ -1,32 +1,34 @@ ELF文件格式 ================================================================================ -ELF (Executable and Linkable Format) is a standard file format for executable files, object code, shared libraries and core dumps. Linux and many UNIX-like operating systems use this format. Let's look at the structure of the ELF-64 Object File Format and some definitions in the linux kernel source code which related with it. +ELF (Executable and Linkable Format)是一种为可执行文件,目标文件,共享链接库和内核转储(core dumps)准备的标准文件格式。 Linux和很多类Unix操作系统都使用这个格式。 让我们来看一下64位ELF文件格式的结构以及内核源码中有关于它的一些定义。 -An ELF object file consists of the following parts: +一个ELF文件由以下三部分组成: -* ELF header - describes the main characteristics of the object file: type, CPU architecture, the virtual address of the entry point, the size and offset of the remaining parts, etc...; -* Program header table - lists the available segments and their attributes. Program header table need loaders for placing sections of the file as virtual memory segments; -* Section header table - contains the description of the sections. +* ELF头(ELF header) - 描述文件的主要特性:类型,CPU架构,入口地址,现有部分的大小和偏移等等; -Now let's have a closer look on these components. +* 程序头表(Program header table) 列举了所有有效的段(segments)和他们的属性。 程序头表需要加载器将文件中的节加载到虚拟内存段中; -**ELF header** +* 节头表(Section header table) - 包含对节(sections)的描述。 -The ELF header is located at the beginning of the object file. Its main purpose is to locate all other parts of the object file. The File header contains the following fields: +现在让我们对这些部分有一些更深的了解。 -* ELF identification - array of bytes which helps identify the file as an ELF object file and also provides information about general object file characteristic; -* Object file type - identifies the object file type. This field can describe that ELF file is a relocatable object file, an executable file, etc...; -* Target architecture; -* Version of the object file format; -* Virtual address of the program entry point; -* File offset of the program header table; -* File offset of the section header table; -* Size of an ELF header; -* Size of a program header table entry; -* and other fields... +**ELF头(ELF header)** -You can find the `elf64_hdr` structure which presents ELF64 header in the linux kernel source code: +ELF头(ELF header)位于文件的开始位置。它的主要目的是定位文件的其他部分。文件头主要包含以下字段: + +* ELF文件鉴定 - 一个字节数组用来确认文件是否是一个ELF文件,并且提供普通文件特征的信息; +* 文件类型 - 确定文件类型。这个字段描述文件是一个重定位文件,或可执行文件,或...; +* 目标结构; +* ELF文件格式的版本; +* 程序入口地址; +* 程序头表的文件偏移; +* 节头表的文件偏移; +* ELF头(ELF header)的大小; +* 程序头表的表项大小; +* 其他字段... + +你可以在内核源码种找到表示ELF64 header的结构体 `elf64_hdr`: ```C typedef struct elf64_hdr { @@ -47,24 +49,24 @@ typedef struct elf64_hdr { } Elf64_Ehdr; ``` -This structure defined in the [elf.h](https://github.com/torvalds/linux/blob/master/include/uapi/linux/elf.h#L220) +这个结构体定义在 [elf.h](https://github.com/torvalds/linux/blob/master/include/uapi/linux/elf.h#L220) -**Sections** +**节(sections)** -All data stores in a sections in an Elf object file. Sections identified by index in the section header table. Section header contains following fields: +所有的数据都存储在ELF文件的节(sections)中。我们通过节头表中的索引(index)来确认节(sections)。节头表表项包含以下字段: + +* 节的名字; +* 节的类型; +* 节的属性; +* 内存地址; +* 文件中的偏移; +* 节的大小; +* 到其他节的链接; +* 各种各样的信息; +* 地址对齐; +* 这个表项的大小,如果有的话; -* Section name; -* Section type; -* Section attributes; -* Virtual address in memory; -* Offset in file; -* Size of section; -* Link to other section; -* Miscellaneous information; -* Address alignment boundary; -* Size of entries, if section has table; - -And presented with the following `elf64_shdr` structure in the linux kernel: +而且,在linux内核中结构体 `elf64_shdr` 如下所示: ```C typedef struct elf64_shdr { @@ -83,7 +85,7 @@ typedef struct elf64_shdr { [elf.h](https://github.com/torvalds/linux/blob/master/include/uapi/linux/elf.h#L312) -**Program header table** +**程序头表(Program header table)** All sections are grouped into segments in an executable or shared object file. Program header is an array of structures which describe every segment. It looks like: