天天看點

工具接口标準(TIS)可執行連結格式(ELF)規範-卷I-符号表(Symbol Table)

本文是對Tool Interface Standard (TIS) Executable and Linking Format (ELF) Specification Version 1.2的翻譯

工具接口标準(TIS)可執行連結格式(ELF)規範版本 1.2

翻譯以中英對照方式,英語水品有限,如有翻譯不當的地方,請諒解。

Symbol Table

符号表

An object file's symbol table holds information needed to locate and relocate a program's symbolic definitions and references. A symbol table index is a subscript into this array. Index 0 both designates the first entry in the table and serves as the undefined symbol index. The contents of the initial entry are specified later in this section.

對象檔案的符号表包含需要被定位和重定位的資訊(程式符号定義和引用),一個符号表索引是這個數組的下标。索引0指定了表的第一個入口,充當未定義符号索引。這個初始化入口的内容會在本節後面部分指定。

名字(Name) 值(Value)
STN_UNDEF

A symbol table entry has the following format.

一個符号表入口有以下格式

Figure 1-15. Symbol Table Entry 

圖1-15.符号表入口

typedef struct {

Elf32_Word st_name;

Elf32_Addr st_value;

Elf32_Word st_size;

unsigned char st_info;

unsigned char st_other;

Elf32_Half st_shndx;

} Elf32_Sym;

st_name

This member holds an index into the object file's symbol string table, which holds the character representations of the symbol names.

此成員包含一個對象檔案符号字元串表的索引,對象檔案符号字元串表包含符号名的字元表達。

st_value

This member gives the value of the associated symbol. Depending on the context, this may be an absolute value, 

an address, and so on; details appear below.

此成員給出關聯符号的值。依賴于上下文,此值可能是一個絕對值、一個位址等;細節會在後面定義。

st_size

Many symbols have associated sizes. For example, a data object's size is the number of bytes contained in the object. 

This member holds 0 if the symbol has no size or an unknown size.

許多符号都有關聯大小。比如,一個資料對象大小是該對象包含的位元組數。此成員在符号沒有大小或者大小未知的情況下為0。

st_info

This member specifies the symbol's type and binding attributes. A list of the values and meanings appears below. 

The following code shows how to manipulate the values.

此成員指定符号類型和綁定屬性。取值和意思的清單在後面定義。下面的代碼介紹了如何操作該成員的值。

#define ELF32_ST_BIND(i) ((i)>>4)

#define ELF32_ST_TYPE(i) ((i)&0xf)

#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))

st_other

This member currently holds 0 and has no defined meaning.

此成員目前沒有定義,被預設置0。

st_shndx

Every symbol table entry is "defined'' in relation to some section; this member holds the relevant section header table index. 

As Figure 1-7 and the related text describe, some section indexes indicate special meanings.

每個符号表入口定義了和某個分節的關系;此成員包含相關的分節頭表索引。像圖1-7以及相關文字描述的一樣,

某些分節索引指明了特殊的含義。

A symbol's binding determines the linkage visibility and behavior.

一個符号的綁定決定了連結的可見性和行為。

Figure1-16. Symbol Binding, ELF32_ST_BIND

圖1-16.符号綁定,ELF32_ST_BIND

名字(Name) 值(Value)
STB_LOCAL
STB_GLOBLE 1
STB_WEAK 2
STB_LOPROC 13
STB_HIPROC 15
STB_LOCAL

Local symbols are not visible outside the object file containing their definition. Local symbols of the same name may exist in multiple files without interfering with each other.

本地符号在包含其定義的對象檔案外是不可見的。在不同的檔案中同名的本地符号是不會互相幹擾的。

STB_GLOBAL

Global symbols are visible to all object files being combined. One file's definition of a global symbol will satisfy another file's undefined reference to the same global symbol.

全局符号在所有需要被組合的對象檔案中都是可見的。一個檔案的全局符号定義可以滿足另一個檔案中未定義的同名全局符号引用。

STB_WEAK

Weak symbols resemble global symbols, but their definitions have lower precedence.

弱符号像全局符号,但它們的定義具有低優先級。

STB_LOPROC through  STB_HIPROC

Values in this inclusive range are reserved for processor-specific semantics.

在此範圍内的值為處理器特定語義保留

In each symbol table, all symbols with STB_LOCAL binding precede the weak and global

symbols. A symbol's type provides a general classification for the associated entity.

在每個符号表,所有的本地符号綁定優先于弱符号和全局符号。符号類型提供普通的關聯入口分類。

Figure1-17. Symbol Types, ELF32_ST_TYPE

圖1-17.符号類型,ELF32_ST_TYPE

名字(Name) 值(Value)
STT_NOTYPE
STT_OBJECT 1
STT_FUNC 2
STT_SECTION 3
STT_FILE 4
STT_LOPROC 13
STT_HIPROC 15
STT_NOTYPE

The symbol's type is not specified.

未定義的符号類型

STT_OBJECT

The symbol is associated with a data object, such as a variable, an array, and so on.

關聯資料對象的符号,比如一個變量、一個數組等。

STT_FUNC

The symbol is associated with a function or other executable code.

關聯函數或其他可執行代碼的符号。

STT_SECTION

The symbol is associated with a section. Symbol table entries of this type exist primarily for relocation and normally have STB_LOCAL binding.

關聯一個分節的符号。此類型的符号表入口主要為重定位存在,有着本地綁定的屬性。

STT_LOPROC through STT_HIPROC

Values in this inclusive range are reserved for processor-specific semantics. 

If a symbol's value refers to a specific location within a section, its section index member, 

st_shndx, holds an index into the section header table. As the section moves during relocation,

 the symbol's value changes as well, and references to the symbol continue to "point'' to

 the same location in the program. Some special section index values give other semantics.

此範圍的值為處理器特定語義保留。假如一個符号值涉及一個分節中的指定位置,

它的分節索引成員st_shndx包含此分節頭表的索引。當分節在重定向中移動時,符号值也随之變化,

并且符号引用繼續指向程式中的相同定位。一些特殊的分節索引值有其它的語義。

STT_FILE

A file symbol has STB_LOCAL binding, its section index is SHN_ABS, 

and it precedes the other STB_LOCAL symbols for the file, if it is present.

一個檔案符号有着本地綁定的屬性,它的分節索引是SHN_ABS,并且它優先于針對該檔案的其它的本地符号。

The symbols in ELF object files convey specific information to the linker and loader. See the

operating system sections for a description of the actual linking model used in the system.

ELF對象檔案中的符号傳達特定資訊給連結器和加載器。在作業系統章節中有系統實際使用的連結模型的描述。

SHN_ABS

The symbol has an absolute value that will not change because of relocation.

此符号包含一個不會因為重定位改變的絕對值。

SHN_COMMON

The symbol labels a common block that has not yet been allocated. The symbol's value gives alignment constraints,

 similar to a section's sh_addralign member. That is, the link editor will allocate the storage for the symbol at an address

 that is a multiple of st_value. The symbol's size tells how many bytes are required.

此符号标記着一個還沒有被申請的通用塊。此符号值給出了對其限制,就像分節中的sh_addralign成員。

是以,連結編輯器會在一個st_value整數倍的位址處申請此變量的存儲空間。此符号大小告訴了所需的位元組數大小。

SHN_UNDEF

This section table index means the symbol is undefined. When the link editor combines this object file 

with another that defines the indicated symbol, this file's references to the symbol will be linked to the actual definition.

這個分節表索引意味着符号未定義。當連結編輯器将這個對象檔案和另一個定義了這個符号的對象檔案組合時,

這個符号在檔案中的引用将被連結到實際的定義。

As mentioned above, the symbol table entry for index 0 (STN_UNDEF) is reserved; it holds the

following.

就像上面提到的,索引0的符号表入口是被保留的;包含以下内容

Figure1-18. Symbol Table Entry: Index 0

圖1-13.特殊分節

名字(Name) 值(Value) 注解(Note)
st_name 沒有名字
st_value
st_size 沒有大小
st_info 沒有類型,本地綁定
st_other
st_shndx SHN_UNDEF 沒有分節

Symbol Values

符号值

Symbol table entries for different object file types have slightly different interpretations for the st_value member.

• In relocatable files, st_value holds alignment constraints for a symbol whose section index is SHN_COMMON.

• In relocatable files,st_value holds a section offset for a defined symbol. That is, st_value is an offset from the beginning of the section that st_shndx identifies.

• In executable and shared object files, st_value holds a virtual address. To make these files' symbols more useful for the dynamic linker, the section offset (file interpretation) gives way to a virtual address (memory interpretation) for which the section number is irrelevant.

不同對象檔案類型的符号表入口對st_value成員的有細微的差别。

  • 在重定位檔案,分節索引為SHN_COMMON的符号的st_value有對齊限制;
  • 在重定位檔案,一個定義的符号的st_value包含分節偏移。是以,st_value是從st_shndx辨別的分節開始處計算的偏移;
  • 在可執行檔案盒共享對象檔案,st_value包含一個虛拟位址。為了使檔案符号在動态連結器中更有用,分節偏移(檔案解釋)為了分節号無關讓步于虛拟位址(記憶體解釋)。

Although the symbol table values have similar meanings for different object files, the data

allow efficient access by the appropriate programs.

雖然符号表取值對不同的對象檔案有相同的意義,資料還是允許被适當的程式有效通路。

繼續閱讀