Here's my question:
There is a static library (xxx.lib) and some C files who are calling function foo()
in xxx.lib. I'm hoping to get a notification message every time foo()
is called. But I'm not allowed to change any source code written by others.
I've spent several days searching on the Internet and found several similar Q&As but none of these suggestions could really solve my problem. I list some of them:
use
gcc -wrap
: Override a function call in C Thank god, I'm using Microsoft C compiler and linker, and I can't find an equivalent option as-wrap
.Microsoft Detours: Detours intercepts C calls in runtime and re-direct the call to a trampoline function. But Detours is only free for IA32 version, and it's not open source.
I'm thinking about injecting a jmp instruction at the start of function
foo()
to redirect it to my own function. However it's not feasible whenfoo()
is empty, likevoid foo() ---> will be compiled into 0xC3 (ret) { but it'll need at least 8 bytes to inject a jmp }
I found a technology named Hotpatch on MSDN. It says the linker will add serveral bytes of padding at the beginning of each function. That's great, because I can replace the padding bytes with jmp instruction to realize the interception in runtime! But when I use the /FUNCTIONPADMIN option with the linker, it gives me a warning:
LINK : warning LNK4044: unrecognized option '/FUNCTIONPADMIN'; ignored
Anybody could tell me how could I make a "hotpatchable" image correctly? Is it a workable solution for my question ?
Do I still have any hope to realize it ?