开发者

What is the proper way to replace very small portions of a binary file programmatically?

开发者 https://www.devze.com 2022-12-21 23:38 出处:网络
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 oper

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 ch开发者_C百科ange those on the fly? (considering one has the assembler and assembly for it available in the main application)


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.


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 read/write/execute 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, 0fffff000h ;round down to the page size, 4K AKA 0x1000
add eax, 800h ;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.

0

精彩评论

暂无评论...
验证码 换一张
取 消