The manpage says > Only undefined references are replaced by the linker. So, translation unit internal references to symbol are not resolved to "__wrap_symbol". In the next example, the call to "f" in "g" is not resolved to "__wrap_f". The "undefined reference" rule can cause different views in non-LTO and LTO (or -r). >a.c<<e cat int foo(); int main() { return foo(); } e >b.c<<e cat int foo() { return 1; } e >c.c<<e cat int __wrap_foo() { return 2; } e gcc -fuse-ld=bfd a.c b.c c.c -Wl,--wrap=foo -o a gcc -fuse-ld=bfd -flto a.c b.c c.c -Wl,--wrap=foo -o a.lto gcc -fuse-ld=bfd a.c b.c c.c -r -o a.ro && gcc -fuse-ld=bfd a.ro -o rel ./a returns 2 while both a.lto and rel return 1. # -fuse-ld=gold has the same issue. In the non-LTO case, the symbol in a.o is essentially changed to __wrap_foo and then bound to the definition in c.o In the relocatable case and the LTO case, foo is defined, so it is not renamed to __wrap_foo. The definition in b.o is picked. I think the majority of use cases use --wrap for an external definition, so this strange behavior doesn't really matter. LLD's --wrap is implemented in such a way that LTO, non-LTO and relocatable links behave the same: --wrap is handled after all other symbol resolution steps. Symbol definitions are not affected (i.e. the section order is unchanged: main,foo,__wrap_foo). The symbol table of each object file is mangled (foo -> __wrap_foo; __real_foo -> foo) so that all relocations to foo or __real_foo will be redirected.
I agree with the commentary here, but don't think that we should change the way ld behaves.