工具接口標準(TIS)可執行鏈接格式(ELF)規範-卷I-分節(Sections)

本文是對Tool Interface Standard (TIS) Executable and Linking Format (ELF) Specification Version 1.2的翻譯
工具接口標準(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.
此值標記一個未定義、丟失、不相干或者其他無意義的分節引用。例如,如果符號"defined''的符號信息中分節號索引如果是SHN_UNDEF,

則這是一個未定義符號。
NOTE. Although index 0 is reserved as the undefined value, the section header table contains an entry for index 0. 

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.
注意:雖然索引0被保留作爲未定義值,但分節頭表仍然包含一個索引0的入口。因此,如果ELF頭中的e_shnum成員爲6,

說明分節頭表有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.
此成員值給定分節在文件中的位置(字節偏移)。分節類型SHT_NOBITS的分節不在文件中佔據空間,成員sh_offset給定其概念上的位置。

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.
此分節包含字符串,大多數字符串與符號表入口關聯,表示符號名。假如一個文件有一個包含符號字符串表的可加載分段,這個分節的屬性會設置SHF_ALLOC位;否則,該位不會被設置。

.symtab

This section holds a symbol table, as "Symbol Table'' in this chapter
describes. If a file has a loadable segment that includes the symbol table, the section's attributes will include the SHF_ALLOC bit; otherwise, that bit will be off.
此分節包含符號表,在本章的“符號表(Symbol Table)”部分會描述。假如一個文件包含的可加載分段,該分段中包含符號表,這個分節的屬性會設置SHF_ALLOC位,否則,該位不會被設置。

.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.
分節名有一個系統保留的前綴點,但是應用程序還是可能會使用到這些名字。應用程序應該使用沒有前綴的名字以避免和系統分節衝突。對象文件應該使用沒有在上述列表中的分節名。一個對象文件可能會含有不止一個相同名字的分節。

發佈了14 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章