工具接口标准(TIS)可执行链接格式(ELF)规范版本 1.2
翻译以中英对照方式,英语水平有限,如有翻译不当的地方,请谅解。
ELF Header
ELF头
Some object file control structures can grow, because the ELF header contains their actual sizes. If the object file format changes, a program may encounter control structures that are larger or smaller than expected. Programs might therefore ignore "extra" information. The treatment of "missing" information depends on context and will be specified when and if extensions are defined.
一些对象文件控制结构能够增长,因为ELF头包含了它们的实际大小。加入对象文件格式变换,程序会遇到比预期更大或更小的控制结构。程序可能会忽略这些额外信息。 “missing”信息的处理应该基于上下文,当定义了扩展信息的时候将被指定。
Figure1-3. ELF Header
图1-3.ELF头
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
e_ident | The initial bytes mark the file as an object file and provide machine-independent data with which to decode and interpret the file's contents. Complete descriptions appear below, in "ELF Identification.'' | |||||||||||||||||||||||||||||||||
e_type | This member identifies the object file type.
Although the core file contents are unspecified, type ET_CORE is reserved to mark the file type. Values from ET_LOPROC through ET_HIPROC(inclusive) are reserved for processor-specific semantics. Other values are reserved and will be assigned to new object file types as necessary. 其它值被保留并在必须的时候指定给新对象类型。 | |||||||||||||||||||||||||||||||||
e_machine | This member's value specifies the required architecture for an individual file. 此成员的值指定需求的架构。
Other values are reserved and will be assigned to new machines as necessary. Processor-specific ELF names use the machine name to distinguish them. For example, the flags mentioned below use the prefix EF_;a flag named WIDGET for the EM_XYZ machine would be called EF_XYZ_WIDGET. 比如,下面提到的标记使用EF_前缀,一个名为WIDGET的标记在EM_XYZ的机器架构下被标识EF_XYZ_WIDGET | |||||||||||||||||||||||||||||||||
e_version | This member identifies the object file version.
The value 1 signifies the original file format; extensions will create new versions with higher numbers. The value of EV_CURRENT, though given as 1 above, will change as necessary to reflect the current version number. 值1表示初始文件格式的版本;扩展将创建新的版本号。在上面的表中EV_CURRENT虽然指定为1, 但为了反映当前版本号,在必要时将被修改。 | |||||||||||||||||||||||||||||||||
e_entry | This member gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero. 系统启动进程时,转换控制使用的虚拟地址。如果文件没有关联的入口点,此成员置零 | |||||||||||||||||||||||||||||||||
e_phoff | This member holds the program header table's file offset in bytes. If the file has no program header table, this member holds zero. 此成员包含程序头表在文件中的偏移(字节)。如果文件没有程序头表,此成员置零 | |||||||||||||||||||||||||||||||||
e_shoff | This member holds the section header table's file offset in bytes. If the file has no section header table, this member holds zero. 此成员包含分节头表在文件中的偏移(字节)。如果文件没有分节头表,此成员置零 | |||||||||||||||||||||||||||||||||
e_flags | This member holds processor-specific flags associated with the file. Flag names take the form EF_machine_flag. 此成员包含与文件关联的处理器特定标识。标记名来自于EF_machine_flag | |||||||||||||||||||||||||||||||||
e_ehsize | This member holds the ELF header's size in bytes. 此成员包含ELF头的大小(字节) | |||||||||||||||||||||||||||||||||
e_phentsize | This member holds the size in bytes of one entry in the file's program header table; all entries are the same size. 此成员包含文件程序头表中一个程序头的大小(字节);所有的程序头大小相同。 | |||||||||||||||||||||||||||||||||
e_phnum | This member holds the number of entries in the program header table. Thus the product of e_phentsize and e_phnum gives the table's size in bytes. If a file has no program header table, e_phnum holds the value zero. 假如一个文件没有程序头表,e_phnum的值为零 |
e_shentsize
This member holds a section header's size in bytes.
A section header is one entry in the section header table; all entries are the same size.
此成员包含分节头表中一个分节头的大小(字节);所有的分节头大小相同。
e_shnum
This member holds the number of entries in the section header table.
Thus the product of e_shentsize and e_shnum gives the section header table's size in bytes.
If a file has no section header table, e_shnum holds the value zero.
此成员包含分节头表中分节头的数量。因此,e_shentsize和e_shnum的乘积就是分节头表的大小。
假如一个文件没有分节头表,e_shnum的值为零
e_shstrndx
This member holds the section header table index of the entry associated with the section name string table.
If the file has no section name string table, this member holds the value SHN_UNDEF.
See "Sections" and "String Table" below for more information.
此成员包含分节头表索引是分节名符号表的入口, 假如文件没有分节名符号表,此成员置SHN_UNDEF。
下面的“分节”和“字符串表”含更过描述信息。
ELF Identification
ELF标识
As mentioned above, ELF provides an object file framework to support multiple processors, multiple data encodings, and multiple classes of machines. To support this object file family, the initial bytes of the file specify how to interpret the file, independent of the processor on which the inquiry is made and independent of the file's remaining contents.就像上面提到的一样,ELF提供一个对象文件框架来支持多种处理器、多种数据编码以及多种机器分类。为了支持这样的对象文件家族,文件的初始字节指定如何解释文件,不依赖于对文件进行查询的处理器以及不依赖于文件剩下的内容。
The initial bytes of an ELF header (and an object file) correspond to the e_ident member.
ELF头(和一个对象文件)初始字节相当于e_ident成员。
Figure 1-4. e_ident[] Identification Indexes
图1-4. e_ident[]标识索引
名字(Name) | 大小(Size) | 目的(Purpose) |
EI_MAG0 | 0 | 文件标识 |
EI_MAG1 | 1 | 文件标识 |
EI_MAG2 | 2 | 文件标识 |
EI_MAG3 | 3 | 文件标识 |
EI_CLASS | 4 | 文件分类 |
EI_DATA | 5 | 数据编码 |
EI_VERSION | 6 | 文件版本 |
EI_PAD | 7 | 填充数据开始位置 |
EI_NIDENT | 16 | e_ident大小 |
These indexes access bytes that hold the following values.
通过这些索引访问e_ident中的内容,这些内容定义如下:
EI_MAG0 to EI_MAG3 | A file's first 4 bytes hold a "magic number," identifying the file as an ELF object file.
| |||||||||||||||
EI_CLASS | The next byte, e_ident[EI_CLASS], identifies the file's class, or capacity.
The file format is designed to be portable among machines of various sizes, without imposing the sizes of the largest machine on the smallest. Class ELFCLASS32 supports machines with files and virtual address spaces up to 4 gigabytes; it uses the basic types defined above. Class ELFCLASS64 is incomplete and refers to the 64-bit architectures. Its appearance here shows how the object file may change. Other classes will be defined as necessary, with different basic types and sizes for object file data. 类型ELFCLASS32支持文件和虚拟地址空间到4G;其使用上面定义的基本数据类型。 类型ELFCLASS64未完成,其引用了64位架构。它的出现展示了对象文件可能会如何改变。 其它类型在必要时会被定义,同时会为对象文件数据定义不同的基本类型和大小 | |||||||||||||||
EI_DATA | Byte e_ident[EI_DATA] specifies the data encoding of the processor-specific data in the object file. The following encodings are
More information on these encodings appears below. Other values are reserved and will be assigned to new encodings as necessary. | |||||||||||||||
EI_VERSION | Byte e_ident[EI_VERSION] specifies the ELF header version number. Currently, this value must be EV_CURRENT, as explained above for e_version. | |||||||||||||||
EI_PAD | This value marks the beginning of the unused bytes in e_ident. These The value of EI_PAD will change in the future if currently unused bytes are given meanings. EI_PAD的值在将来修改了当前未使用的字节后会被改变 |
A file's data encoding specifies how to interpret the basic objects in a file. As described above, class ELFCLASS32 files use objects that occupy 1, 2, and 4 bytes. Under the defined encodings, objects are represented as shown below. Byte numbers appear in the upper left corners.Encoding ELFDATA2LSB specifies 2's complement values, with the least significant byte occupying the lowest address.
文件的数据编码指定了如何解释文件的基本对象。就像上面描述的一样,分类为ELFCLASS32的文件使用的对象中包含的成员一般占据1、2和4字节。在这种编码定义下,对象中的存储方式如下图。字节顺序在方框左上角。编码方式ELFDATA2LSB指定了2的补数值,最低有效字节占据最低的地址。
Figure 1-5. Data Encoding ELFDATA2LSB
图1-5. ELFDATA2LSB数据编码格式
Encoding ELFDATA2MSB specifies 2's complement values, with the most significant byte occupying the lowest address.
编码方式ELFDATA2MSB指定了2的补数值,最高有效字节占据最低的地址。
Figure 1-6. Data Encoding ELFDATA2MSB
图1-6. ELFDATA2MSB数据编码格式