【问题标题】:meaning of an entry in a relocation table of an object file目标文件重定位表中条目的含义
【发布时间】:2018-09-07 04:51:22
【问题描述】:

我在理解从 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  

我的问题是: 14 处的shared 和2e 处的shared 是完全相同的对象。为什么它们有不同的符号名称?

【问题讨论】:

    标签: c linker x86-64 relocation


    【解决方案1】:

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

    有什么区别?

    在0x14和0x3b处:必须将全局变量shared的地址移到寄存器%rsi中才能调用函数swap。

    但是,由于程序是使用-mcmodel=small 编译的(gcc 的默认设置,另请参见this question),编译器可以假设地址适合 32 位并使用 movl 而不是 movq(实际上是否则编译器会使用其他指令,但将movl 与“天真”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>
    

    显然,将shared 的绝对地址放入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)
    

    这意味着:

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

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


    There 是 Eli Bendersky 的一篇关于此主题的精彩文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2015-01-09
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多