工具接口标准(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的解释对不同的对象文件不同,但是重定位类型的意义保持不变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值