9.2.48 --wrap
symbol
Use wrapper functions for symbol
.
Any undefined reference to symbol will be resolved to _wrap__
symbol. Any undefined reference to _real_
_symbol will be resolved to symbol
. This can be used to provide a wrapper for a system function. The wrapper function should be called _wrap__
symbol. If it wishes to call the system function, it should call _real__
symbol.
Here is an example:
#include <stdio.h>
#ifdef WRAP
#define WRAPIT2(X) _wrap__ ## X
#define WRAPIT(X) WRAPIT2(X)
#define REAL2(X) _real__ ## X
#define REAL(X) REAL2(X)
#else
#define WRAPIT(X) X
#define REAL(X) X
#endif
int REAL(foo)(int x) {
/* this is the real function */
/* we if we want to wrap it, then this is called _real__foo (or __real_foo from the linkers perspective
*/
return x +1;
}
#ifdef WRAP
/* wrap it and call the real one */
int WRAPIT(foo)(int x) {
return REAL(foo)(x)+1;
}
#endif
main() {
fprintf(stderr,"foo returns %d\n", foo(1));
}
The following will wrap foo
and make it call __wrapfoo
; __real_foo
will call the real foo
function. The output will be 3
.
xc-dsc-gcc wrap.c -save-temps -DWRAP -Wl,--wrap,_foo
The following will not wrap foo
, and make the 'real' foo
be simply foo
. The output will be 2
.
xc-dsc-gcc wrap.c -save-temps
The --wrap
option can be used to intercept a precompiled library call. Be aware that this option requires an assembly level symbol name. For example, in order to wrap the C symbol foo
, you must specify --wrap _foo
, since all C symbols will be given a leading underscore by the C compiler.