Elegant way to set base address of ELF image with Linux binutils?

巧了我就是萌 提交于 2019-12-20 03:01:05

问题


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

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]

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.


回答1:


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

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)

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



来源:https://stackoverflow.com/questions/15302718/elegant-way-to-set-base-address-of-elf-image-with-linux-binutils

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!