工具接口標準(TIS)可執行鏈接格式(ELF)規範-卷I-重定位(Relocation)

本文是對Tool Interface Standard (TIS) Executable and Linking Format (ELF) Specification Version 1.2的翻譯
工具接口標準(TIS)可執行鏈接格式(ELF)規範版本 1.2

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


Relocation
重定位

Relocation is the process of connecting symbolic references with symbolic definitions. For example, when a program calls a function, the associated call instruction must transfer control to the proper destination address at execution. In other words, relocatable files must have information that describes how to modify their section contents, thus allowing executable and shared object files to hold the right information for a process's program image. Relocation entries are these data.
重定位是連接符號引用和符號定義的處理。比如,當一個程序調用一個函數,相關的調用指令必須在執行期間轉換爲適當的目的地址。換句話說,重定位文件必須包含如何修改分節內容的描述信息,從而允許可執行和共享對象文件爲處理程序鏡像包含正確的信息。重定位入口是這些數據。
Figure 1-19. Relocation Entries
圖1-19.重定位入口

typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;

typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
Elf32_Sword r_addend;
} Elf32_Rela;

r_offset

This member gives the location at which to apply the relocation action. 

For a relocatable file, the value is the byte offset from the beginning of the section to the storage 

unit affected by the relocation. For an executable file or a shared object, 

the value is the virtual address of the storage unit affected by the relocation.
這個成員給出了應用重定位動作的定位。對一個重定位文件來說,這個值是從分節開始位置到受重定位影響的存儲單元的偏移(字節)。

對可執行文件或者共享對象,這個值是受重定位映像的存儲單元的虛擬地址。

r_info

This member gives both the symbol table index with respect to which the relocation must be made,

 and the type of relocation to apply. For example, a call instruction's relocation entry would hold 

the symbol table index of the function being called. If the index is STN_UNDEF, the undefined symbol index, 

the relocation uses 0 as the "symbol value.'' Relocation types are processor-specific; 

descriptions of their behavior appear in the processor supplement. When the text in the processor supplement 

refers to a relocation entry's relocation type or symbol table index, it means the result of applying ELF32_R_TYPE or ELF32_R_SYM, 

respectively, to the entry's r_info member.
這個成員包含關於必須做重定位的符號表索引,以及需要應用的重定位類型。舉例來說,

一個指定調用重定位入口應該包含被調用函數的符號表索引。假如索引是STN_UNDEF,表示未定義的符號索引,

重定位使用0作爲符號值。重定位類型是處理器相關的;在處理器增補部分進行描述。

當處理器增補部分的文字描述涉及重定位入口的重定位類型或符號表索引時,

它表示應用ELF32_R_TYPE或ELF32_R_SYM到入口的r_info成員。
#define ELF32_R_SYM(i) ((i)>>8)
#define ELF32_R_TYPE(i) ((unsigned char)(i))
#define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t))

r_addend

This member specifies a constant addend used to compute the value to be stored into the relocatable field.

這個成員指定一個常量加數,用於計算存儲到重定位字段的值。



As shown above, only Elf32_Rela entries contain an explicit addend. Entries of type Elf32_Rel store an implicit addend in the location to be modified. Depending on the processor architecture, one form or the other might be necessary or more convenient. Consequently, an implementation for a particular machine may use one form exclusively or either form depending on context.
A relocation section references two other sections: a symbol table and a section to modify. The section header's sh_info and sh_link members, described in "Sections'' above, specify these relationships. Relocation entries for different object files have slightly different interpretations for the r_offset member.
• In relocatable files, r_offset holds a section offset. That is, the relocation section itself describes how to modify another section in the file; relocation offsets designate a storage unit within the second section.
• In executable and shared object files, r_offset holds a virtual address. To make these files' relocation entries more useful for the dynamic linker, the section offset (file interpretation) gives way to a virtual address (memory interpretation).
Although the interpretation of r_offset changes for different object files to allow efficient access by the relevant programs, the relocation types' meanings stay the same.
如上所示,只有Elf32_Rela入口包含一個清晰的加數。類型Elf32_Rel的入口存儲了一個會在定位時被修改的隱含加數。依賴於處理器架構,一種形式或其它形式應該是必要的或者更方便的。因此,對於一種特定機器的實現應該僅使用一種形式或者依賴於上下文的任意一種形式。
一個重定位分節參照另外的2個分節:一個符號表和一個需要修改的分節。這個分節頭的成員sh_info和sh_link,在上面的“分節”章節進行了描述,指定了這些關係。不同對象文件的重定位入口對r_offset成員有稍許不同的解釋。
  • 在重定位文件,r_offset包含分節偏移。也就是說,這個重定位分節自身描述瞭如何在文件中修改另一個分節;重定位偏移指明瞭在第二個分節中的存儲單元;
  • 在可執行文件盒共享對象文件中,r_offset包含一個虛擬地址。爲了使這些文件的重定位入口對動態鏈接器更有用,分節偏移(文件解釋)讓步給虛擬地址(內存解釋)。
爲了使相關程序訪問更有效,雖然r_offset的解釋對不同的對象文件不同,但是重定位類型的意義保持不變。

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