工具接口标准(TIS)可执行链接格式(ELF)规范版本 1.2
翻译以中英对照方式,英语水品有限,如有翻译不当的地方,请谅解。
Sections
分节
An object file's section header table lets one locate all the file's sections. The section header table is an array of Elf32_Shdr structures as described below. A section header table index is a subscript into this array. The ELF header's e_shoff member gives the byte offset from the beginning of the file to the section header table; e_shnum tells how many entries the section header table contains; e_shentsize gives the size in bytes of each entry.Some section header table indexes are reserved; an object file will not have sections for these special indexes.
对象文件中的分节头表可以定位文件中的所有分节。分节头表是Elf32_Shdr结构的数组,如下图定义。分节头表索引是访问该数组的下标。ELF头中的e_shoff成员给定了分节头表在文件中的开始位置(字节);e_shnum成员告诉了分节头表包含多少个入口;e_shentsize成员给定了每个入口的大小(字节)。
一些分节头表索引被保留;对象文件的分节不会包含这些特殊索引。
Figure 1-7. Special Section Indexes
图1-7.特殊分节索引
名字(Name) | 值(Value) |
SHN_UNDEF | 0 |
SHN_LORESERVE | 0xff00 |
SHN_LOPROC | 0xff00 |
SHN_HIPROC | 0xff1f |
SHN_ABS | 0xfff1 |
SHN_COMMON | 0xfff2 |
SHN_HIRESERVE | 0xffff |
SHN_UNDEF | This value marks an undefined, missing, irrelevant, or otherwise meaningless section reference. For example, a symbol "defined'' relative to section number SHN_UNDEF is an undefined symbol. 则这是一个未定义符号。 That is, if the e_shnum member of the ELF header says a file has 6 entries in the section header table, they have the indexes 0 through 5. The contents of the initial entry are specified later in this section. 说明分节头表有6个入口,索引从0到5。本节稍后将给出初始入口内容。 |
SHN_LORESERVE | This value specifies the lower bound of the range of reserved indexes. |
SHN_LOPROC through SHN_HIPROC | Values in this inclusive range are reserved for processor-specific semantics. 在此范围内的值保留给处理器特定语法使用 |
SHN_ABS | This value specifies absolute values for the corresponding reference. For example, symbols defined relative to section number SHN_ABS have absolute values and are not affected by relocation. 此值指定相对应的引用的绝对值。例如,符号如果定义分节索引为SHN_ABS,则说明该符号拥有不会被重定位的绝对地址。 |
SHN_COMMON | Symbols defined relative to this section are common symbols, such as FORTRAN COMMON or unallocated C external variables. 符号如果定义分节索引为SHN_COMMON,则说明是通用符号,比如FORTRAN COMMON或者未申请的C外部变量。 |
SHN_HIRESERVE | This value specifies the upper bound of the range of reserved indexes. The system reserves indexes between SHN_LORESERVE and SHN_HIRESERVE, inclusive; the values do not reference the section header table.That is, the section header table does not contain entries for the reserved indexes. 此值指定保留索引的最大值。系统保留索引在SHN_LORESERVE和SHN_HIRESERVE之间,包含边界值; 此值不会涉及分段头表。因此分节头表不会包含保留索引的入口。 |
Sections contain all information in an object file, except the ELF header, the program header table, and the section header table. Moreover, object files' sections satisfy several conditions.
• Every section in an object file has exactly one section header describing it. Section headers may exist that do not have a section.
• Each section occupies one contiguous (possibly empty) sequence of bytes within a file.
• Sections in a file may not overlap. No byte in a file resides in more than one section.
• An object file may have inactive space. The various headers and the sections might not "cover'' every byte in an object file. The contents of the inactive data are unspecified.
一个对象文件的分节包含所有的信息,除了ELF头、程序头表以及分节头表。此外,对象文件分节满足以下条件:
- 对象文件中的每个分节只有一个分节头对其进行描述。可能存在没有对应分节的分节头;
- 每个分节在文件中占据一个连续的空间(可能为空);
- 文件中的分节不会重叠。文件中的每个字节仅会存在于一个分节中;
- 对象文件可能存在不活跃空间。不同的头和分节可能不会使用对象文件中的每个字节,未激活数据的内容是未指定的;
A section header has the following structure.
一个分节头有以下结构
Figure 1-8. Section Header
图1-8.分节头
typedef struct {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
sh_name | This member specifies the name of the section. Its value is an index into the section header string table section [see "String Table'' below], giving the location of a null-terminated string. 此成员指定分节名。其值是分节头字符串表分节中的索引,给定含终结符的字符串位置 |
sh_type | This member categorizes the section's contents and semantics. Section types and their descriptions appear below. 此成员对分节内容和语义分类。分节类型和描述在下面会进行描述。 |
sh_flags | Sections support 1-bit flags that describe miscellaneous attributes. Flag definitions appear below. 分节支持单比特标志,用于描述混合属性。标记定义会在下面进行描述。 |
sh_addr | If the section will appear in the memory image of a process, this member gives the address at which the section's first byte should reside. Otherwise, the member contains 0. 假如分节会在进程内存映像中出现,此成员给定分节的第一个字节的内存地址。否则,此成员为0。 |
sh_offset | This member's value gives the byte offset from the beginning of the file to the first byte in the section. One section type, SHT_NOBITS described below, occupies no space in the file, and its sh_offset member locates the conceptual placement in the file. |
sh_size | This member gives the section's size in bytes. Unless the section type is SHT_NOBITS, the section occupies sh_size bytes in the file. A section of type SHT_NOBITS may have a non-zero size, but it occupies no space in the file. 此成员给定分节大小(字节)。除非分节类型为SHT_NOBITS,否则分节都将在文件中实际占用sh_size给定的大小 |
sh_link | This member holds a section header table index link, whose interpretation depends on the section type. A table below describes the values. |
sh_info | This member holds extra information, whose interpretation depends on the section type. A table below describes the values. 此成员包含额外信息,其值的解释依赖于分节类型。后面会有一个表对其进行描述。 |
sh_addralign | Some sections have address alignment constraints. For example, if a section holds a doubleword, the system must ensure doubleword alignment for the entire section. That is, the value of sh_addr must be congruent to 0, modulo the value of sh_addralign. Currently, only 0 and positive integral powers of two are allowed. Values 0 and 1 mean the section has no alignment constraints. 因此,sh_addr的值必须对sh_addralign取模后为0。 当前,只有0和2的整数幂的正整数是被允许的值。值0和1表示此分节没有对齐约束。 |
sh_entsize | Some sections hold a table of fixed-size entries, such as a symbol table. For such a section, this member gives the size in bytes of each entry. The member contains 0 if the section does not hold a table of fixed-size entries. 一些分节含有固定大小入口的表,比如符号表。对这样的表来说,此成员给定每个入口的大小(字节)。 如果分节没有包含固定大小的入口表,此成员为0。 |
Figure 1-9. Section Types, sh_type
图1-9.分节类型,sh_type
名字(Name) | 值(Value) |
SHT_NULL | 0 |
SHT_PROGBITS | 1 |
SHT_SYMTAB | 2 |
SHT_STRTAB | 3 |
SHT_RELA | 4 |
SHT_HASH | 5 |
SHT_DYNAMIC | 6 |
SHT_NOTE | 7 |
SHT_NOBITS | 8 |
SHT_REL | 9 |
SHT_SHLIB | 10 |
SHT_DYNSYM | 11 |
SHT_LOPROC | 0x70000000 |
SHT_HIPROC | 0x7fffffff |
SHT_LOUSER | 0x80000000 |
SHT_HIUSER | 0xffffffff |
SHT_NULL | This value marks the section header as inactive; it does not have an associated section. Other members of the section header have undefined values. 此成员标记分节头为未激活;没有相关联的分节。其它分节头成员设置为未定义的值。 |
SHT_PROGBITS | The section holds information defined by the program, whose format and meaning are determined solely by the program. 此分节包含由程序定义的信息,其格式和含义由程序单独决定 |
SHT_SYMTAB and SHT_DYNSYM | These sections hold a symbol table. 这些分节包含符号表 |
SHT_STRTAB | The section holds a string table. 此分节包含字符串表 |
SHT_RELA | The section holds relocation entries with explicit addends, such as type Elf32_Relafor the 32-bit class of object files. An object file may have multiple relocation sections. See "Relocation'' below for details. 此分节包含具有明确的加数的重定位入口,比如类型为Elf32_Rela的32位对象文件分类。一个对象文件可能含有多个重定位分节。下面的“Relocation”部分将描述更多细节。 |
SHT_HASH | The section holds a symbol hash table. 此分节包含符号Hash表 |
SHT_DYNAMIC | The section holds information for dynamic linking. 此分节包含动态链接信息 |
SHT_NOTE | This section holds information that marks the file in some way. 此分节包含用某些方式标记文件的信息 |
SHT_NOBITS | A section of this type occupies no space in the file but otherwise resembles SHT_PROGBITS. Although this section contains no bytes, the sh_offset member contains the conceptual file offset. 不像SHT_PROGBITS类型,此类型的分节不在文件中占据空间。虽然此分节不包含内容,但成员sh_offset仍然包含理论的文件偏移 |
SHT_REL | The section holds relocation entries without explicit addends, such as type Elf32_Rel for the 32-bit class of object files. An object file may have multiple relocation sections. See "Relocation'' below for details. 此分节包含没有明确加数的重定位入口,比如类型为Elf32_Rel的32位对象文件分类。 |
SHT_SHLIB | This section type is reserved but has unspecified semantics. 此分节类型被保留但未指定语义 |
SHT_LOPROC through SHT_HIPROC | Values in this inclusive range are reserved for processor-specific semantics. 在此范围内的值被保留给处理器特定语义 |
SHT_LOUSER | This value specifies the lower bound of the range of indexes reserved for application programs. 此值指定了应用程序保留的索引范围的最小值 |
SHT_HIUSER | This value specifies the upper bound of the range of indexes reserved for application programs. Section types between SHT_LOUSER and SHT_HIUSER may be used by the application, without conflicting with current or future system-defined section types. 此值指定了应用程序保留的索引范围的最大值。分节类型在SHT_LOUSER和SHT_HIUSER之间的值可以被应用程序使用,不会与当前或未来系统定义的分节类型冲突 |
Figure 1-10. Section Header Table Entry: Index 0
图1-10.分节头表入口:索引0
名字(Name) | 值(Value) | 注释(Note) |
sh_name | 0 | 没有名字 |
sh_type | SHT_NULL | 未激活 |
sh_flags | 0 | 没有标志 |
sh_addr | 0 | 没有地址 |
sh_offset | 0 | 没有文件偏移 |
sh_size | 0 | 没有大小 |
sh_link | SHN_UNDEF | 没有链接信息 |
sh_info | 0 | 没有辅助信息 |
sh_addralign | 0 | 没有对齐 |
sh_entsize | 0 | 没有入口 |
A section header's sh_flags member holds 1-bit flags that describe the section's attributes.
Defined values appear below; other values are reserved.
分节头的sh_flags成员包含比特标记,用于描述分节属性。下面将会定义值;其它值被保留。
Figure 1-11. Section Attribute Flags, sh_flags
图1-11.分节属性标志,sh_flags
名字(Name) | 值(Value) |
SHF_WRITE | 0x1 |
SHF_ALLOC | 0x2 |
SHF_EXECINSTR | 0x4 |
SHF_MASKPROC | 0xf0000000 |
If a flag bit is set in sh_flags, the attribute is "on'' for the section. Otherwise, the attribute
is "off" or does not apply. Undefined attributes are set to zero.
假如sh_flags的标志位被置位,分节的属性将被“打开”。否则,属性被“关闭”或不被应用。未定义属性被设置为0.
SHF_WRITE | The section contains data that should be writable during process execution. 在程序执行期间,分节包含数据能够被写 |
SHF_ALLOC | The section occupies memory during process execution. Some control sections do not reside in the memory image of an object file; this attribute is off for those sections. 在程序执行期间,分节会被保留在内存空间。一些对象文件中控制分节不会存在于内存映像中;这些分节的属性是关闭的。 |
SHF_EXECINSTR | The section contains executable machine instructions. 分节包含可执行的机器码 |
SHF_MASKPROC | All bits included in this mask are reserved for processor-specific semantics. 所有被此掩码覆盖的位都是为处理器特定语义保留的 |
Figure 1-12. sh_link and sh_info Interpretation
图1-10.分节头表入口:索引0
sh_type | sh_link | sh_info |
SHT_DYNAMIC | The section header index of the string table used by entries in the section. 本分节的所有入口使用的字符串表的分节头索引 | 0 |
SHT_HASH | The section header index of the symbol table to which the hash table applies. 哈希表使用的符号表的分节头索引 | 0 |
SHT_REL SHT_RELA | The section header index of the associated symbol table. 关联符号表的分节头索引 | The section header index of the section to which the relocation applies. 重定位信息使用的分节头索引 |
SHT_SYMTAB SHT_DYNSYM | This information is operating system specific. 操作系统特定信息 | This information is operating system specific. 操作系统特定信息 |
other | SHN_UNDEF | 0 |
Special Sections
特殊分节
Various sections in ELF are pre-defined and hold program and control information. These Sections are used by the operating system and have different types and attributes for different operating systems.
ELF中的不同分节都是被预定义并包含程序和控制信息。这些分节被操作系统使用,不同的操作系统有不同的类型和属性。
Executable files are created from individual object files and libraries through the linking process. The linker resolves the references (including subroutines and data references) among the different object files, adjusts the absolute references in the object files, and relocates instructions. The linking and loading processes, which are described in Chapter 2, require information defined in the object files and store this information in specific sections such as .dynamic.
可执行文件在链接过程中通过多个对象文件和库创建。连接器在不同的对象文件中解析引用(包括子程序和数据引用),在对象文件中调整绝对引用,重定位指令。链接和加载处理会在第二章描述,需要对象文件的特定分节如.dynamic中存储的信息。
Each operating system supports a set of linking models which fall into two categories:
每个操作系统支持两类链接模型集合
Static | A set of object files, system libraries and library archives are statically bound, references are resolved, and an executable file is created that is completely self contained. 对象文件、系统库和用户库静态绑定,引用被解析,可执行文件被完整的自包含创建 |
Dynamic | A set of object files, libraries, system shared resources and other shared libraries are linked together to create the executable. When this executable is loaded, other shared resources and dynamic libraries must be made available in the system for the program to run successfully. 对象文件、动态库、系统库共享资源和其它共享库被一起链接,用于创建可执行文件。 当可执行文件被加载,其它共享资源和动态库必须在系统中有效,程序才能运行成功 The general method used to resolve references at execution time for a dynamically linked executable file is described in the linkage model used by the operating system, and the actual implementation of this linkage model will contain processor-specific components. 动态链接可执行文件在程序执行期间用于解析引用的方法在操作系统使用的链接模型中描述,实际的链接模型实现会包含处理器特定组件 |
There are also sections that support debugging, such as .debug and .line, and program
control, including .bss, .data, .data1, .rodata, and .rodata1.
还有的分节支持调试,比如.debug和.line,以及程序控制,包含.bss、.data、.data1、.rodata和.rodata1.
Figure 1-13. Special Sections
图1-13.特殊分节
名字(Name) | 类型(Type) | 属性(Attribute) |
.bss | SHT_NOBITS | SHF_ALLOC+SHF_WRITE |
.comment | SHT_PROGBITS | none |
.data | SHT_PROGBITS | SHF_ALLOC + SHF_WRITE |
.data1 | SHT_PROGBITS | SHF_ALLOC + SHF_WRITE |
.debug | SHT_PROGBITS | none |
.dynamic | SHT_DYNAMIC | see below |
.hash | SHT_HASH | SHF_ALLOC |
.line | SHT_PROGBITS | none |
.note | SHT_NOTE | none |
.rodata | SHT_PROGBITS | SHF_ALLOC |
.rodata1 | SHT_PROGBITS | SHF_ALLOC |
.shstrtab | SHT_STRTAB | none |
.strtab | SHT_STRTAB | see below |
.symtab | SHT_SYMTAB | see below |
.text | SHT_PROGBITS | SHF_ALLOC + SHF_EXECINSTR |
.bss | This section holds uninitialized data that contribute to the program's memory image. By definition, the system initializes the data with zeros when the program begins to run. The section occupies no file space, as indicated by the section type, SHT_NOBITS. 此节包含未初始化数据,在程序内存映像中存在。根据定义,系统在程序开始运行时初始化这些数据为0。此分节通过分节类型SHT_NOBITS表示不占据文件空间。 |
.comment | This section holds version control information. 此分节包含版本控制信息。 |
.data and .data1 | These sections hold initialized data that contribute to the program's memory image. 此分节包含程序内存映像中存在的初始化的数据。 |
.debug | This section holds information for symbolic debugging. The contents are unspecified. All section names with the prefix .debug are reserved for future use. 此分节包含符号调试信息。内容未指定。所有的分节名都以.debug为前缀,都被保留给将来使用。 |
.dynamic | This section holds dynamic linking information and has attributes such as SHF_ALLOC and SHF_WRITE. Whether the SHF_WRITE bit is set is determined by the operating system and processor. 此分节包含动态链接信息,含有SHF_ALLOC和SHF_WRITE属性。 是否设置SHF_WRITE位是由操作系统和处理器决定的。 |
.hash | This section holds a symbol hash table. 此分节包含符号哈希表。 |
.line | This section holds line number information for symbolic debugging, which describes the correspondence between the source program and the machine code. The contents are unspecified. 此分节包含符号调试用的行号信息,描述源程序和机器码之间的对应关系。内容未指定。 |
.note | This section holds information in the format that is described in the "Note Section'' in Chapter 2. 此分节包含信息的格式在第二章节的“注解分节(Note Section)”描述。 |
.rodata and .rodata1 | These sections hold read-only data that typically contribute to a non-writable segment in the process image. See "Program Header'' in Chapter 2 for more information. 这些分节包含只读数据,具有代表性的属于在进程映像中不可写分段。第二章节的“程序头(Program Header)”会有更多信息 |
.shstrtab | This section holds section names. 此分节包含分节名 |
.strtab | This section holds strings, most commonly the strings that represent the names associated with symbol table entries. If a file has a loadable segment that includes the symbol string table, the section's attributes will include the SHF_ALLOC bit; otherwise, that bit will be off. |
.symtab | This section holds a symbol table, as "Symbol Table'' in this chapter |
.text | This section holds the "text,'' or executable instructions, of a program. 这个分节包含程序的“文本”,或者可执行指令。 |
Section names with a dot (.) prefix are reserved for the system, although applications may use these sections if their existing meanings are satisfactory. Applications may use names without the prefix to avoid conflicts with system sections. The object file format lets one define sections not in the list above. An object file may have more than one section with the same name.
分节名有一个系统保留的前缀点,但是应用程序还是可能会使用到这些名字。应用程序应该使用没有前缀的名字以避免和系统分节冲突。对象文件应该使用没有在上述列表中的分节名。一个对象文件可能会含有不止一个相同名字的分节。