GCC has an interesting flag we can use to switch out an implementation of a function at link time, called "wrap". I think this is usually used for testing functions with unpredictable inputs. But we can use this to add a reverse-engineered implementation and redirect all references in the object code to our implementation. If we want to replace
void some_func() {}
We must name our implementation __wrap_some_func()
. So we have to add __wrap_
to the beginning of the function name.
void __wrap_some_func() {
//some code