我在理解从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
都是完全相同的物体。为什么他们有不同的符号名?
发布于 2018-09-11 20:53:05
这是相同的地址,但重新定位类型是不同的。重新定位类型是在x86-64-abi中定义的。
有什么关系?
在0x14
和0x3b
:为了调用函数swap
,必须将全局变量shared
的地址移动到注册%rsi
。
但是,由于程序是用-mcmodel=small
编译的(默认为gcc,参见这个问题),编译器可以假设地址符合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位相对偏移量就足够了(因此使用了重定位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)
。它也可以在生成的二进制文件中看到--对于这两种不同的重新定位类型,不同的值将在占位符上进行修补。
那里是Eli关于这个主题的一篇很棒的文章。
https://stackoverflow.com/questions/52215495
复制相似问题