解决部分链接中的相对重定位 [英] Resolve relative relocations in partial link

查看:269
本文介绍了解决部分链接中的相对重定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,使用-r进行部分链接实际上并没有解决任何重定位问题,即使它们可以通过相对寻址来解决.例如,考虑f.og.o,其中f.o包含f(),这会在g.o中调用g().在链接之前,拆卸和重定位是按预期进行的.但是,在部分链接到新文件h.o之后(通过ld -r -o h.o f.o g.o),即使在理论上可以用相对地址解析,但仍需要重新放置对g()的调用.

I've noticed that using -r to do a partial link doesn't actually resolve any relocations, it seems, even if they could be resolved via relative addressing. For example, consider f.o and g.o, with f.o containing f() which calls g() within g.o. Before linking, the disassembly and relocations are as expected. After partial linking to a new file h.o (via ld -r -o h.o f.o g.o), however, there is still a relocation for the call to g(), even though in theory it could have been resolved with a relative address.

这是h.o(objdump -d h.o)的反汇编,其中包含f()g().您会看到对g()的调用仍未解决:

Here is the disassembly of h.o (objdump -d h.o), containing f() and g(). You can see that the call to g() is still unresolved:

h.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <f>:
   0:       55                      push   %rbp
   1:       48 89 e5                mov    %rsp,%rbp
   4:       e8 00 00 00 00          callq  9 <f+0x9>
   9:       90                      nop
   a:       5d                      pop    %rbp
   b:       c3                      retq

000000000000000c <g>:
   c:       55                      push   %rbp
   d:       48 89 e5                mov    %rsp,%rbp
  10:       90                      nop
  11:       5d                      pop    %rbp
  12:       c3                      retq

这是h.o(objdump -r h.o)的重定位表:

h.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
0000000000000005 R_X86_64_PC32     g-0x0000000000000004


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text
0000000000000058 R_X86_64_PC32     .text+0x000000000000000c

因此,只是想知道是否有任何方法可以在部分链接期间通过相对地址强制执行重定位.我尝试使用-fpic编译代码,但这仍然没有导致在部分链接期间执行重定位.

So, just wondering whether or not there was any way to force the relocation to be performed via a relative address during a partial link. I tried compiling the code with -fpic, but that still did not cause the relocation to be performed during the partial link.

请注意,我对此没有目标,应用或原因.只是探索ld的功能.

Note that I have no goal, application, or reason for this. Just exploring the functionality and capabilities of ld.

推荐答案

每个

Per this reference it looks like the reason that the relative symbols are not resolved is to give the linked more flexibility. If we wanted to instead resolve the symbol g to a different address, we would need to change those jumps in the code.

但是,如果将符号g更改为静态,则链接程序将解决跳转问题.在这种情况下,该符号将不再导出,因此以后的链接程序调用将无法对其进行操作.

However, if you change your symbol g to be static, then the linker will resolve the jumps. In that case, the symbol is no longer exported and thus not manipulable by future linker calls.

这篇关于解决部分链接中的相对重定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆