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

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

问题描述

可以使用defsym在某些地址处链接符号,如图所示此处.我的问题是,是否可以对部分进行相同的处理?也就是说,给定某些.o对象文件,是否可以指定节将被重定位到的基址?

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.

推荐答案

从您所参考的问题和Linux标签来看,我将假设您使用的是GNU ld.

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

对于GNU ld的简短回答是,可以将部分放在特定的地址.

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

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

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)
       }
}

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

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天全站免登陆