- 论坛徽章:
- 0
|
Relocation是一个把符号引用和符号定义连接在一起的过程。例如,当一个程序执行中调用一个函数时,相关的调用指令必须把控制权传到被调用的函数手中,即从正确的地址开始执行(该地址即为被调用函数的开始地址)。换句话说,可以可以重装载(Relocatable)的文件必须有描述如何修改它们自身的section的信息,从而使得可执行文件和共享目标文件(shared object file)能够正确地建立一个程序在内存中的镜像。
/* Relocation table entry without addend (in section of type SHT_REL). */
typedef struct
{
Elf32_Addr r_offset; /* Address */
Elf32_Word r_info; /* Relocation type and symbol index */
} Elf32_Rel;
/* I have seen two different definitions of the Elf64_Rel and
Elf64_Rela structures, so we'll leave them out until Novell (or
whoever) gets their act together. */
/* The following, at least, is used on Sparc v9, MIPS, and Alpha. */
typedef struct
{
Elf64_Addr r_offset; /* Address */
Elf64_Xword r_info; /* Relocation type and symbol index */
} Elf64_Rel;
/* Relocation table entry with addend (in section of type SHT_RELA). */
typedef struct
{
Elf32_Addr r_offset; /* Address */
Elf32_Word r_info; /* Relocation type and symbol index */
Elf32_Sword r_addend; /* Addend */
} Elf32_Rela;
typedef struct
{
Elf64_Addr r_offset; /* Address */
Elf64_Xword r_info; /* Relocation type and symbol index */
Elf64_Sxword r_addend; /* Addend */
} Elf64_Rela;
/* How to extract and insert information held in the r_info field. */
#define ELF32_R_SYM(val) ((val) >> 8)
#define ELF32_R_TYPE(val) ((val) & 0xff)
#define ELF32_R_INFO(sym, type) (((sym) 8) + ((type) & 0xff))
#define ELF64_R_SYM(i) ((i) >> 32)
#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) 32) + (type))
r_offset: 指名在哪里执行relocation的动作。例如,指名的是一个虚拟的地址。
r_info: 包含symbol table index,同时也指明了relocation的类型。
r_addend: 指明了一个常量,用来计算保存在relocation field中的值。
至于使用哪种格式,是Rel还是Rela,取决于处理器。一个relocation section和另外两个section有关: symbol table & section to modify(用来修改的section). 前面提到的section header中的sh_info和sh_link描述了这种关系。不同的object file(目标文件)中队r_offset的解释有着细微的差别。在relocatable文件中,r_offset指的是在section内的偏移,也就是说,relocation section自己描述了如何修改文件中另一个section。此时的r_offset指明了在第二个section中的存储单位。在executable 和 shared object 文件中,r_offset指明的是虚拟地址。为了使文件的relocation entries 对动态链接器来说更有效,r_offset指明的是虚拟地址。尽管r_offset对于不同的object file有不同的含义,但是relocation type则是相同的含义。
Relocation Types
Relocation entries描述了如何改变指令和数据区域。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29336/showart_237406.html |
|