用Linux binutils设置ELF图像基址的优雅方法?

时间:2022-10-30 04:54:00

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

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

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:

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

--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]

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等价物。不存在吗?自己解析ELF文件不会是世界末日,但似乎这个功能应该存在于某个地方。

1 个解决方案

#1


1  

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

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

Doing a bogus link with -v 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).

使用-v执行虚假链接将显示默认的链接器响应文件(可能是特定于系统的,但实际上它并不是那么糟糕,每个操作系统最多为每个操作系统一个)。

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

请注意,可能存在其他约束(例如驻留在text / codesegment中的入口点)

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

有关拖拽自定义链接器文件的实际示例,请参阅Free Pascal项目,该项目执行此操作以实现资源。

#1


1  

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

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

Doing a bogus link with -v 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).

使用-v执行虚假链接将显示默认的链接器响应文件(可能是特定于系统的,但实际上它并不是那么糟糕,每个操作系统最多为每个操作系统一个)。

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

请注意,可能存在其他约束(例如驻留在text / codesegment中的入口点)

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

有关拖拽自定义链接器文件的实际示例,请参阅Free Pascal项目,该项目执行此操作以实现资源。