首页 > 解决方案 > 目标文件重定位表中条目的含义

问题描述

我在理解从 C 源文件编译的重定位表的条目时遇到了一些问题。我的程序如下:

//a.c
extern int shared;
int main(){
    int a = 100;
    swap(&a, &shared);
    a = 200;
    shared = 1;
    swap(&a, &shared);
}
//b.c
int shared = 1;
void swap(int* a, int* b) {
    if (a != b)
        *b ^= *a ^= *b, *a ^= *b;
}

我使用以下命令编译并链接它们,gcc -c -fno-stack-protector a.c b.c并且ld a.o b.o -e main -o ab. 然后我objdump -r a.o检查它的重定位表。

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000014 R_X86_64_32       shared
0000000000000021 R_X86_64_PC32     swap-0x0000000000000004
000000000000002e R_X86_64_PC32     shared-0x0000000000000008
000000000000003b R_X86_64_32       shared
0000000000000048 R_X86_64_PC32     swap-0x0000000000000004

的拆卸a.o

Disassembly of section .text:

0000000000000000 <main>:
0:  55                      push   %rbp
1:  48 89 e5                mov    %rsp,%rbp
4:  48 83 ec 10             sub    $0x10,%rsp
8:  c7 45 fc 64 00 00 00    movl   $0x64,-0x4(%rbp)
f:  48 8d 45 fc             lea    -0x4(%rbp),%rax
13: be 00 00 00 00          mov    $0x0,%esi
18: 48 89 c7                mov    %rax,%rdi
1b: b8 00 00 00 00          mov    $0x0,%eax
20: e8 00 00 00 00          callq  25 <main+0x25>
25: c7 45 fc c8 00 00 00    movl   $0xc8,-0x4(%rbp)
2c: c7 05 00 00 00 00 01    movl   $0x1,0x0(%rip)  # 36 <main+0x36>
33: 00 00 00 
36: 48 8d 45 fc             lea    -0x4(%rbp),%rax
3a: be 00 00 00 00          mov    $0x0,%esi
3f: 48 89 c7                mov    %rax,%rdi
42: b8 00 00 00 00          mov    $0x0,%eax
47: e8 00 00 00 00          callq  4c <main+0x4c>
4c: b8 00 00 00 00          mov    $0x0,%eax
51: c9                      leaveq 
52: c3                      retq  

我的问题是: shared在 14 和shared2e 是完全一样的对象。为什么它们有不同的符号名称?

标签: clinkerx86-64relocation

解决方案


那是相同的地址,但重定位类型不同。重定位类型在x86-64-abi中定义。

有什么不同?

at 0x14and :为了调用函数,必须将0x3b全局变量的地址shared移到寄存器中。%rsiswap

但是,因为程序是用-mcmodel=small(gcc 的默认值,另请参见这个问题)编译的,编译器可以假设该地址适合 32 位并使用movl而不是movq(实际上编译器会使用其他指令,但movl与“naive”相比)movq很好地解释了差异),这将需要更多的字节进行编码。

因此,产生的重定位是R_X86_64_32(即 64 位地址被截断为 32 位而没有符号扩展)而不是R_X86_64_64,即链接器将写入地址的 4 个低字节而不是占位符,占位符也是 4 个字节宽。

0x2e您想将值写入1内存地址shared。但是,目标地址是相对于 给出的%rip,即相对于0x36

movl   $0x1,0x0(%rip)  # 36 <main+0x36>

显然,仅仅放置sharedvia的绝对地址R_X86_64_32不会有任何好处 - 需要更复杂的计算,这就是R_X86_64_PC32目的。

再一次,由于编译器可以假设的小代码模型,32 位 rip-relative 偏移量就足够了(因此使用重定位R_X86_64_PC32而不是重定位R_X86_64_PC64)并且占位符只有 4 个字节宽。

取自 x86-64-abi,重定位公式为(第 4.4 节):

result = S+A-P (32bit-word, i.e. the lower 4 bytes of the result) 
S = the value of the symbol whose index resides in the relocation entry 
A = the addend used to compute the value of the relocatable field 
P = the place (section offset or address) of the storage unit being relocated (computed using r_offset)

这意味着:

  • Sshared变量的地址。
  • Ais -8(例如可以通过调用readelf -r a.oor看到),因为重定位的偏移量和实际的-objdump -r a.o之间存在 8 个字节的差异。0x2e%rip0x36
  • P是重定位的偏移量,即0x26P-A是中的地址%rip

如您所见,结果S与上述情况不同R_X86_64_32,而是S - (P-A). 在生成的二进制文件中也可以看到 - 不同的值将在这两种不同重定位类型的占位符处修补。


Eli Bendersky有一篇关于这个主题的精彩文章。


推荐阅读