工具接口标准(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成员。 |
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包含一个虚拟地址。为了使这些文件的重定位入口对动态链接器更有用,分节偏移(文件解释)让步给虚拟地址(内存解释)。