用Linux binutils设置ELF图像基地址的优雅方法? [英] Elegant way to set base address of ELF image with Linux binutils?

查看:91
本文介绍了用Linux binutils设置ELF图像基地址的优雅方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For a personal project, I need to write an executable that loads at a non-default memory address. From this SO question, I know I can set the entry address for an ELF and modify the section addresses manually so that the executable is effectively based at some address I choose. However, the answer suggests that this is only works if I don't glibc initialization (which I need for this project), and setting section memory addresses every time I compile would be difficult (not to mention tedious).

在构建时,似乎应该有一种更好的方法来为ELF设置基址,尽管如果需要的话,我将求助于脚本后构建.如果ld的此选项不是专用于PE端口,则将是完美的:

It seems like there should be a better way to set a base address for an ELF when building, though I'll resort to doing it manually with a script post-build if need be. This option for ld would be perfect if it wasn't specific to the PE port:

--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文件,但似乎该功能应该存在于某个地方.

I haven't yet found an ELF equivalent. Does none exist? Parsing the ELF file myself wouldn't be the end of the world, but it seems like this feature should exist somewhere.

推荐答案

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

The ELF entry point can be set in the linker response file, which can be passed to ld with -T

使用--verbose进行虚假链接将为您显示默认的链接器响应文件(该文件可能是系统特定的,但实际上并不坏,每个OS最多一个弓形文件).

Doing a bogus link with --verbose will show you the default linker responsefile (which might be system specific, but in reality it is not that bad, one per arch per OS for the most).

请注意,可能还有其他限制(例如,文本/代码段中的入口点)

Note that there might be additional constraints (like the entry point residing in a text/codesegment)

有关拖拽自定义链接器文件的实际示例,请参见Free Pascal项目,该项目可实现资源.

For a practical example of lugging along custom linker files, see the Free Pascal project, which does this to implement resources.

这篇关于用Linux binutils设置ELF图像基地址的优雅方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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