gpt4 book ai didi

linux - 使用 Linux binutils 设置 ELF 镜像基地址的优雅方法?

转载 作者:太空狗 更新时间:2023-10-29 12:19:09 24 4
gpt4 key购买 nike

对于个人项目,我需要编写一个加载到非默认内存地址的可执行文件。来自 this SO question ,我知道我可以设置 ELF 的入口地址并手动修改节地址,以便可执行文件有效地基于我选择的某个地址。但是,答案表明这仅在我不进行 glibc 初始化(我需要此项目)时才有效,并且每次编译时都很难设置段内存地址(更不用说乏味了)。

似乎应该有更好的方法来在构建时为 ELF 设置基址,但如果需要的话,我会在构建后使用脚本手动完成。 ld 的这个选项如果不是特定于 PE 端口的话将是完美的:

--image-base value
Use value as the base address of your program or dll. This is the lowest
memory location that will be used when your program or dll is loaded. To
reduce the need to relocate and improve performance of your dlls, each should
have a unique base address and not overlap any other dlls. The default is
0x400000 for executables, and 0x10000000 for dlls. [This option is specific
to the i386 PE targeted port of the linker]

我还没有找到等效的 ELF。不存在吗?自己解析 ELF 文件不会是世界末日,但似乎这个功能应该存在于某个地方。

最佳答案

ELF入口点可以在链接器响应文件中设置,可以用-T传递给ld

用 --verbose 做一个虚假链接会显示默认的链接器响应文件(这可能是系统特定的,但实际上它并没有那么糟糕,大多数情况下每个操作系统每个拱门一个)。

请注意,可能存在其他约束(例如位于文本/代码段中的入口点)

有关拖拽自定义链接器文件的实际示例,请参阅 Free Pascal 项目,它这样做是为了实现资源。

关于linux - 使用 Linux binutils 设置 ELF 镜像基地址的优雅方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15302718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com