如何在链接时指定节的基地址,或者如何在链接后重新设置节的基址? [英] How to specify base addresses for sections when linking or alternatively how to rebase a section after linking?

查看:11
本文介绍了如何在链接时指定节的基地址,或者如何在链接后重新设置节的基址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

符号可以用 defsym 链接到特定地址,如图所示 这里.我的问题是是否可以对部分进行相同的操作?也就是说,给定一些 .o 目标文件,是否可以指定部分将重定位到的基地址?

或者,是否可以在最终链接之后重新设置部分?也就是在二进制文件中改变一个section的地址,并让所有受重定位影响的信息(例如section的相对分支等)正确重定位.

如果我的问题不清楚,我可以编辑它并添加图像以更好地解释.

解决方案

从你提到的问题和Linux的标签来看,我假设你使用的是GNU ld.p>

GNU ld 的简短回答是肯定的,段可以放在特定的地址.

更长的答案是您需要创建一个自定义链接器脚本来执行此操作,可以为 ld 指定 -T.如果您使用 gcc 作为 ld 的包装器,则需要通过 gcc -Wl 将其传递给链接器, 选项.

链接描述文件必须包含以下内容:

部分{.text 0x08049000:{foo.o (.text)bar.o (.text)}}

需要注意的是 -T 选项替换了 ld 使用的默认链接描述文件.您可能想要修改默认链接描述文件以执行您想要的操作.可以通过将 --verbose 选项传递给 ld 来转储默认链接描述文件,而无需任何其他选项.

有关链接器脚本的更多信息可在 LD 手册.

Symbols can be linked at certain addresses with defsym as illustrated here. My question is whether the same can be done with sections? That is, given some .o object file, is it possible to specify the base address that sections will be relocated to?

Alternatively, is it possible to rebase a section after the final link? That is, to change the address of a section in a binary and have all information affected by relocation (such as relative branches out of the section, etc.) to be relocated properly.

If my question is not clear, I can edit it and add an image to explain better.

解决方案

Judging by the question you reference and the tag of Linux, I am going to assume that you are using GNU ld.

The short answer for GNU ld is yes, sections can be placed at specific addresses.

The longer answer is that you will need to create a custom linker script to do that, which can be specified the -T for ld. If you are using gcc as a wrapper around ld, you will need pass it the linker via the gcc -Wl, option.

The linker script will have to include something like the following:

SECTIONS {
   .text 0x08049000 :
       {
       foo.o (.text)
       bar.o (.text)
       }
}

Something to watch out for though is that -T option replaces the default linker script that ld uses. You may want to modify the default linker script to do what you want. The default linker script can be dumped by passing the --verbose option to ld without any other options.

More info about linker scripts is available in the LD Manual.

这篇关于如何在链接时指定节的基地址,或者如何在链接后重新设置节的基址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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