Question

I'm using DLL injection in a video game for the purpose of modding (I'm building a game API).

I want to directly access structures and variables in the EXE from my new DLL. The absolute addresses of variables are known (the EXE has a fixed image base) .

According the GNU ld documentation, I can use --defsym=symbol=expression to create a global symbol in the output file, containing the absolute address given by expression.

I can't get it to work.

If I declare the symbols as extern in my code, and use ld --defsym, I get undefined reference errors. But if I define the symbols in my code, it just uses the local (DLL) versions, not the EXE ones.

It seems the --defsym options are having no effect. Any insights would be greatly appreciated.

Update: --defsym works perfectly under Linux. When I try compiling on Windows using mingw, I get undefined reference errors.

Was it helpful?

Solution

I've figured out the problem.

After inspecting the assembly produced by mingw, I found the C symbol names are being prefixed by an underscore. This happens on Windows, but not on Linux.

There is a gcc option -fno-leading-underscore, but this results in an undefined reference to WinMain.

Simply adding leading underscores to the symbol names works.

Example: if my my symbol is named alien in C, using gcc --defsym=_alien=0x500000 works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top