Gcc Link Time Function Wrapping
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
|
|
We must name our implementation __wrap_some_func()
. So we have to add __wrap_
to the beginning of the function name.
|
|
Then we have to ask the linker to link our implementation.
|
|
Or in a Makefile
|
|
Sometimes the compiler decides not to globally export some symbols, if we reference these symbols in our reverse-engineered code, Linker will complain about missing symbols. If these symbols are still in the static library, we can export them globally manually.
Find out if the symbol is present,
|
|
then export it,
|
|