[hjl@gnu-cfl-1 lto-6]$ cat foo.c extern void bar (void) __attribute__((weak)); void foo (void) { if (bar) bar (); } [hjl@gnu-cfl-1 lto-6]$ cat lib.c int FLAGS_verbose; [hjl@gnu-cfl-1 lto-6]$ cat x.c extern int FLAGS_verbose; extern void bar (void); int a(const char *b) { return FLAGS_verbose; } void unused (void) { bar(); } int main() { a(""); return 0; } [hjl@gnu-cfl-1 lto-6]$ make clean rm -f *.o *.so x *.out ./*.s ./*.res [hjl@gnu-cfl-1 lto-6]$ cat foo.c extern void bar (void) __attribute__((weak)); void foo (void) { if (bar) bar (); } [hjl@gnu-cfl-1 lto-6]$ cat lib.c int FLAGS_verbose; [hjl@gnu-cfl-1 lto-6]$ cat x.c extern int FLAGS_verbose; extern void bar (void); int a(void) { return FLAGS_verbose; } void unused (void) { bar(); } int main() { return a (); } [hjl@gnu-cfl-1 lto-6]$ make gcc -B./ -O -flto -c -o x.o x.c gcc -B./ -O -c -o lib.o lib.c gcc -B./ -O -fpic -c -o foo.o foo.c gcc -B./ -shared -O -o libfoo.so foo.o gcc -B./ -shared -O -o libx.so lib.o libfoo.so gcc -B./ -o x x.o libx.so -Wl,--as-needed -L. -lfoo ./ld: ./libfoo.so: undefined reference to `bar' collect2: error: ld returned 1 exit status make: *** [Makefile:20: x] Error 1 [hjl@gnu-cfl-1 lto-6]$ readelf -s ./libfoo.so | grep bar 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND bar Weak undefined reference is OK.
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c54f15248ee990df6cea0b4730cd61b227a0e082 commit c54f15248ee990df6cea0b4730cd61b227a0e082 Author: H.J. Lu <hjl.tools@gmail.com> Date: Fri Apr 26 07:52:00 2019 -0700 Don't complain undefined weak dynamic reference When undefined non-weak references in IR objects are optimized out by LTO, we can have weak dynamic referencs to symbols marked with bfd_link_hash_undefined. We shouldn't complain such undefined weak dynamic references. bfd/ PR ld/24486 * elflink.c (elf_link_output_extsym): Don't complain undefined weak dynamic reference. ld/ PR ld/24486 * testsuite/ld-plugin/lto.exp: Run PR ld/24486 tests. * testsuite/ld-plugin/pr24486a.c: New file. * testsuite/ld-plugin/pr24486b.c: Likewise. * testsuite/ld-plugin/pr24486c.c: Likewise.
Fixed for 2.33.