Bug 25389 - - -Wl,--wrap -fuse-ld=bfd not supported with LTO
Summary: - -Wl,--wrap -fuse-ld=bfd not supported with LTO
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on: 24406
  Show dependency treegraph
Reported: 2020-01-15 08:23 UTC by cuilili
Modified: 2020-01-17 05:00 UTC (History)
2 users (show)

See Also:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description cuilili 2020-01-15 08:23:00 UTC
$ 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
Comment 1 H.J. Lu 2020-01-17 05:00:06 UTC
The testcase is invalid since

     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.