c - 目标文件重定位表中条目的含义
问题描述
我在理解从 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 和shared
2e 是完全一样的对象。为什么它们有不同的符号名称?
解决方案
那是相同的地址,但重定位类型不同。重定位类型在x86-64-abi中定义。
有什么不同?
at 0x14
and :为了调用函数,必须将0x3b
全局变量的地址shared
移到寄存器中。%rsi
swap
但是,因为程序是用-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>
显然,仅仅放置shared
via的绝对地址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
is-8
(例如可以通过调用readelf -r a.o
or看到),因为重定位的偏移量和实际的-objdump -r a.o
之间存在 8 个字节的差异。0x2e
%rip
0x36
P
是重定位的偏移量,即0x26
。P-A
是中的地址%rip
。
如您所见,结果S
与上述情况不同R_X86_64_32
,而是S - (P-A)
. 在生成的二进制文件中也可以看到 - 不同的值将在这两种不同重定位类型的占位符处修补。
Eli Bendersky有一篇关于这个主题的精彩文章。
推荐阅读
- c++ - Cmake 不制作 sln 文件(Visual Studio 2017)
- typescript - 重命名打字稿对象类型的键
- javascript - 检测 React/ReactDOM 开发/生产构建
- c# - Xamarin Forms 中数据模板视图单元格内的绑定上下文
- c# - 为 x86 和 x64 架构注册 .msi(安装项目)
- excel - 计算属于时间跨度的班次的哪一部分
- html - 与宽度相同的高度和其中“不简单”的内容的div
- java-8 - Wildfly 是否有任何推荐的元空间设置大小?
- javascript - 使用 Highcharts 在平行坐标图中突出显示不同颜色的线
- android - 不暴露我所依赖的jar文件