With the Visual C++ compiler, it is possible to create a DLL file, that can imitate another DLL file and redirect all function calls to the original DLL. Here is an article with a tool that can generate Visual C++ code automatically.
The generated function-stubs work (tested) and look like this:
extern "C" __declspec(naked) void __stdcall __E__0__()
{
__asm
{
jmp p[0]; // p[0] = GetProcAddress(hL,"AcceptEx");
}
}
Now I want to do the same thing with MinGW/GCC instead of MSVC.
__declspec(naked) isn't supported by GCC on i386, so we need another way. As suggested here, I could override functions by writing assembly code in the global scope. Here's my code that should do the trick:
__asm__
(
"jmp *%0"
: /* empty output list */
: "r" (pointer_to_original_function) /* p[0] in the example above */
);
My snippet uses GCC's extended ASM. But unfortunatelly this is only allowed inside of functions, not in the global scope!
So... how do I do that? My next approach would be to try it without extended ASM, but how do I get the pointer address in assembly then?
Here i'm trying to get it from a global variable, but it segfaults at repace_this_stub():
#include <stdio.h>
void try_to_jump_to_me()
{
printf("you made the jump\n");
}
void* target_pointer = try_to_jump_to_me;
__asm__ (
"replace_this_stub:"
"jmp target_pointer"
);
void replace_this_stub();
int main(int argc, char** argv)
{
printf("starting in main. \n");
replace_this_stub();
printf("back in main?\n");
}