to clarify, my question refers to wrapping/intercepting calls from one function/symbol to another function/symbol when the caller and the callee are defined in the same compilation unit with the GCC compiler and linker.
I have a situation resembling the following:
/* foo.c */
void foo(void)
{
/* ... some stuff */
bar();
}
void bar(void)
{
/* ... some other stuff */
}
I would like to wrap calls to these functions, and I can do that (to a point) with ld's --wrap
option (and then I implement __wrap_foo and __wrap_bar which in turn call __real_foo and __real_bar as expected by the result of ld's --wrap
option).
gcc -Wl,--wrap=foo -Wl,--wrap=bar ...
The problem I'm having is that this only takes effect for references to foo and bar from outside of this compilation unit (and resolved at link time). That is, calls to foo and bar from other functions within foo.c do not get wrapped.
I tried using objcopy --redefine-sym, but that only renames the symbols and their references.
I would like to replace calls to foo
and bar
(within foo.o) to __wrap_foo
and __wrap_bar
(just as they get resolved in other object files by the linker's --wrap
option) BEFORE I pass the *.o files to the linker's --wrap
options, and without having to modify foo.c's source code.
That way, the wrapping/interception takes place for all calls to foo
and bar
, and not just the ones taking place outside of foo.o.
Is this possible?
object file
so the calls to, say,foo
, get replaced with an offset to a symbol, say__real_foo
that will be resolved later by the linker? I ask in earnest btw. – Greerobjcopy --weaken-symbol=called_function
and link with a new object that definescalled_function()
. – Diarrhoea