Page 1 of 1

relocating code

Posted: Fri Jun 19, 2009 6:47 am
by yemista
How do you relocate code in memory? If the code is assembled to run at 0x00100000,
but you want it to be located at 0x0, but later on expect paging to make it think it is at 0xC0000000, will it work? Wont it eventually crash from a far jump because it was originally assembled to run at 0x00100000?

Re: relocating code

Posted: Fri Jun 19, 2009 7:42 am
by NickJohnson
You generally have code assembled to run in one place, then don't move it. In order to do what you're talking about, you need to compile the code you are running at 0x0 to run at 0x0, and the code that's run at 0xC0000000 to run at 0xC0000000. You then have to use paging to make it possible for both of these to be true (by mapping the code at 0x0 and 0xC0000000), temporarily. You then jump from the 0x0 code to the 0xC0000000 code.

Re: relocating code

Posted: Fri Jun 19, 2009 8:29 am
by earlz
Or possibly do a relocatable ELF or similar so that each time you switch paging you can "pause" execution, run a relocator on the ELF file, and then do a special function call to flush registers(can't use any address the registers once had!) and then just return to where the executable was running with a different EIP.

Or, much easier method: I believe with 64bit you can have freestanding executables. This means it uses RIP as a relative address to all data and all jumps. This means when you switch over the paging to another address, all you have to do is figure out the new RIP and poof everything works.