问题描述
通常,用户程序二进制文件将被装入程序地址空间中的低地址(通常在0x400000
附近),该地址将在elf
二进制文件中指定(对于linux).
Generally the user program binaries will be loaded in low address (usually around 0x400000
) in the programs address space which will be specified in the elf
binary (in the case of linux).
我们是否可以强制用户二进制文件在高地址处加载,可能在libc
或其他此类库已加载的地址的2GB
地址范围内?
Can we force a user binary to load at a high address, possibly within the 2GB
range of addresses where libc
or other such libraries are loaded?
我尝试过在网上找到解决方案,但是找不到任何具体的解决方案.
I have tried finding a solution on the net but could not find any concrete solution for this.
(我正在使用Ubuntu 12.10 64位OS)
(I am working on Ubuntu 12.10 64bit OS)
谢谢
推荐答案
除非二进制文件与位置无关(PIE),否则这是不可能的.正常(非PIE)二进制文件在链接时针对特定的加载地址进行了硬编码,并且在链接期间,重新定位到其他地址所需的信息已经丢失.
Unless the binary is position-independent (PIE), this is not possible. Normal (non-PIE) binaries are hard-coded for a particular load address at link time, and during linking, the information necessary for relocating to a different address was already lost.
编辑:以上假设您正在使用现有的二进制文件.如果您自己生成二进制文件,则可以使用以下链接选项控制硬编码到二进制文件中的加载地址:
The above is assuming you're working with an existing binary. If you are producing the binary yourself, you can control the load address that's hard-coded into it with the following link options:
-Wl,-Ttext-segment,0x80000000
用您想要的地址替换0x80000000
.某些地址(例如保留给内核使用的地址,通常从0xc0000000开始)将不起作用,并且该地址必须是页面对齐的(最后3个十六进制数字必须为0).
replacing 0x80000000
by your desired address. Certain addresses (such as those reserved for kernel use, typically beginning at 0xc0000000) will not work, and the address must be page-aligned (the last 3 hex digits must be 0).
这篇关于如何将程序加载到与目标地址不同的内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!