clinuxlinkerld

How to load a program in memory at a different address than it is intended for?


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

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.

(I am working on Ubuntu 12.10 64bit OS)


Solution

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

    Edit: 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
    

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