$ cat a.c int w = 100; int z = 78; extern int foo() { return w + z; } extern int main() { return foo() + foo() * foo(); } $ cat b.c int x = 76; int y = 87; #define likely(z) __builtin_expect(!!(z), 1) extern int __wrap_foo() { if (likely(x)) { return __real_foo(); } int i,s; for (i = 0; i < 10000; i++) s = s + x * y - i; } $gcc -flto -fuse-ld=bfd -Wl,--wrap=foo a.c b.c /tmp/ccPX4lh2.ltrans0.ltrans.o: in function `__wrap_foo': <artificial>:(.text+0x6a): undefined reference to `foo' gcc version: 7.4.0 or gcc trunk binutils version: 2.33 or binutils trunk I tried different configurations, this works gcc -flto -fuse-ld=gold -Wl,--wrap=read -O3 a.c b.c gcc -flto -fuse-ld=gold -Wl,--wrap=read -O2 a.c b.c gcc -flto -fuse-ld=gold -Wl,--wrap=read -O1 a.c b.c This does not work: gcc -flto -fuse-ld=gold -Wl,--wrap=read -O0 a.c b.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O3 a.c b.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O2 a.c b.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O1 a.c b.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O0 a.c b.c
The testcase is invalid since '--wrap=SYMBOL' Use a wrapper function 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'. When SYMBOL definition is compiled with LTO, LTO will resolve all SYMBOL references to SYMBOL definition in LTO since LTO doesn't know --wrap.