免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1089 | 回复: 0
打印 上一主题 下一主题

Relocation 分析 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-24 22:21 |只看该作者 |倒序浏览
    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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP