Bug 24486 - ld shouldn't complain undefined weak reference
Summary: ld shouldn't complain undefined weak reference
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: 2.33
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-04-25 20:33 UTC by H.J. Lu
Modified: 2019-04-26 14:57 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2019-04-25 20:33:08 UTC
[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.
Comment 1 Sourceware Commits 2019-04-26 14:52:59 UTC
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.
Comment 2 H.J. Lu 2019-04-26 14:57:06 UTC
Fixed for 2.33.