views:

58

answers:

2

I have a game code (from ioquake3 project) that compiles part of gameplay binaries on the fly (the qvm system). Now, one could potentially speed it up by loading previously saved binaries of this operation (with any change-of-files precautions in place).

But, pointers to functions saved in these binaries are not persistent through sessions.

What would be the proper way to change those on the fly? (considering one has the assembler and assembly for it available in the main application)

+2  A: 

That's pretty much what a normal loader for executables does. They usually work by storing a table of the places there are references to addresses that will need to be changed based on where the file is loaded. Typically, they store a relative address in each of those locations, so to load the file, you look at the table and add the base load address to what's at each of those addresses, and put that result into image as its loaded into memory.

Jerry Coffin
+2  A: 

Pass a table of relevant function pointers as a parameter. Alternatively, you can have generated code rely on a data area that's placed at a fixed, relative to code, location.

I recall when I was messing with this stuff, I'd set up a RWX memory page, use the first half for generated code, and the second half for data. The code, once it got control, would go like this:

call l
l:
pop eax ; eax has the current eip
and eax, fffff000 ;round down to the page size, 4K AKA 0x1000
add eax, 0x800 ;now eax points at the data area

...and so on.

For large scale generated code, you might want to store it as full blown DLLs and make use of system-provided relocation and address fixup services.

Seva Alekseyev