relocating code

Programming, for all ages and all languages.
Post Reply
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

relocating code

Post 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?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: relocating code

Post 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.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: relocating code

Post 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.
Post Reply