Bug 15146

Summary: Reference from dummy plugin symbol isn't removed
Product: binutils Reporter: H.J. Lu <hjl.tools>
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: fweimer, jakub
Priority: P2    
Version: 2.24   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description H.J. Lu 2013-02-13 18:01:24 UTC
[hjl@gnu-6 pr15126]$ cat foo.c
int main() { }
[hjl@gnu-6 pr15126]$ cat bar.c
extern int xxx;

int
bar ()
{
  return xxx;
}
[hjl@gnu-6 pr15126]$ cat xxx.c
int xxx __attribute__ ((weak)) = 3;
[hjl@gnu-6 pr15126]$ cat yyy.c
[hjl@gnu-6 pr15126]$ make
gcc -flto   -c -o bar.o bar.c
gcc -flto   -c -o foo.o foo.c
gcc -shared -fPIC -o libxxx.so xxx.c
gcc -shared -fPIC -o libyyy.so yyy.c libxxx.so
gcc -flto -O2  -o x bar.o foo.o libyyy.so -Wl,-rpath-link,.
/usr/local/bin/ld: bar.o (symbol from plugin): undefined reference to symbol 'xxx'
/usr/local/bin/ld: note: 'xxx' is defined in DSO ./libxxx.so so try adding it to the linker command line
./libxxx.so: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make: *** [x] Error 1
Comment 1 Sourceware Commits 2013-02-16 17:54:42 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	hjl@sourceware.org	2013-02-16 17:54:37

Modified files:
	ld             : ChangeLog plugin.c 
	ld/testsuite   : ChangeLog 
	ld/testsuite/ld-plugin: lto.exp 
	bfd            : ChangeLog elflink.c 
Added files:
	ld/testsuite/ld-plugin: pr15146.d pr15146a.c pr15146b.c 
	                        pr15146d.c 

Log message:
	Don't add DT_NEEDED for references from the dummy bfd
	
	bfd/
	
	PR ld/15146
	* elflink.c (elf_link_add_object_symbols): Don't add DT_NEEDED
	for references from the dummy bfd.
	
	ld/
	
	PR ld/15146
	* plugin.c (plugin_notice): Replace the undefined dummy bfd with
	the real one.
	
	ld/testsuite/
	
	PR ld/15146
	* ld-plugin/pr15146.d: New file.
	* ld-plugin/pr15146a.c: Likewise.
	* ld-plugin/pr15146b.c: Likewise.
	* ld-plugin/pr15146c.c: Likewise.
	* ld-plugin/pr15146d.c: Likewise.
	
	* ld-plugin/lto.exp: Add tests for PR ld/15146.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&r1=1.2556&r2=1.2557
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/plugin.c.diff?cvsroot=src&r1=1.48&r2=1.49
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1686&r2=1.1687
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15146.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15146a.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15146b.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15146d.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/lto.exp.diff?cvsroot=src&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5969&r2=1.5970
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elflink.c.diff?cvsroot=src&r1=1.468&r2=1.469
Comment 2 H.J. Lu 2013-02-16 18:25:31 UTC
Fixed.
Comment 3 Sourceware Commits 2013-02-18 09:13:15 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	amodra@sourceware.org	2013-02-18 09:13:11

Modified files:
	ld/testsuite   : ChangeLog 
	ld/testsuite/ld-plugin: lto.exp 

Log message:
	* ld-plugin/lto.exp (Build pr15146b.so) Add -Wl,--no-as-needed.
	(PR ld/15146 (1), (2)): Likewise.
	(LTO 7): Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1687&r2=1.1688
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/lto.exp.diff?cvsroot=src&r1=1.4&r2=1.5
Comment 4 Sourceware Commits 2013-02-19 03:59:12 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	sandra@sourceware.org	2013-02-19 03:59:09

Modified files:
	ld             : ChangeLog plugin.c 

Log message:
	2013-02-19  Sandra Loosemore  <sandra@codesourcery.com>
	
	PR ld/15146
	
	ld/
	* plugin.c (plugin_notice): Add null check before dereferencing
	pointer.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&r1=1.2558&r2=1.2559
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/plugin.c.diff?cvsroot=src&r1=1.49&r2=1.50
Comment 5 Sourceware Commits 2013-04-03 11:44:43 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	amodra@sourceware.org	2013-04-03 11:44:41

Modified files:
	ld/testsuite   : ChangeLog 
	ld/testsuite/ld-plugin: lto.exp pr12942a.cc 

Log message:
	PR ld/15227
	* ld-plugin/lto.exp (PR ld/12942 (3)): Remove file name and
	line number from regexp.
	(PR ld/15146 (2)): Similarly.
	* ld-plugin/pr12942a.cc (main): Use __builtin_abort.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1704&r2=1.1705
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/lto.exp.diff?cvsroot=src&r1=1.7&r2=1.8
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr12942a.cc.diff?cvsroot=src&r1=1.1&r2=1.2
Comment 6 Sourceware Commits 2020-09-04 07:25:04 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1e3b96fd6cf0c7d018083994ad951ccf92aba582

commit 1e3b96fd6cf0c7d018083994ad951ccf92aba582
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Sep 4 13:54:21 2020 +0930

    Allow plugin syms to mark as-needed shared libs needed
    
    We must tell LTO about symbols in all shared libraries loaded.  That
    means we can't load extra shared libraries after LTO recompilation, at
    least, not those that affect the set of symbols that LTO cares about,
    the IR symbols.
    
    This change will likely result in complaints about --as-needed
    libraries being loaded unnecessarily, but being correct is more
    important than being optimal.  One of the PR15146 tests regresses, and
    while that could be hidden by disabling the missing dso message by
    making it conditional on h->root.non_ir_ref_regular, that would just
    be sweeping a problem under the rug.
    
    bfd/
            PR 15146
            PR 26314
            PR 26530
            * elflink.c (elf_link_add_object_symbols): Do set def_regular
            and ref_regular for IR symbols.  Don't clear dynsym, allowing
            IR symbols to load --as-needed shared libraries, but prevent
            IR symbols from becoming dynamic.
    ld/
            * testsuite/ld-plugin/lto.exp: Don't run pr15146 tests.
            * testsuite/ld-plugin/pr15146.d: Delete.
            * testsuite/ld-plugin/pr15146a.c: Delete.
            * testsuite/ld-plugin/pr15146b.c: Delete.
            * testsuite/ld-plugin/pr15146c.c: Delete.
            * testsuite/ld-plugin/pr15146d.c: Delete.
Comment 7 H.J. Lu 2020-10-29 11:43:24 UTC
*** Bug 26806 has been marked as a duplicate of this bug. ***
Comment 8 Sourceware Commits 2020-11-01 23:14:42 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b1a92c635c1ec10fd703302ce1fc4ab3a8515a04

commit b1a92c635c1ec10fd703302ce1fc4ab3a8515a04
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Oct 30 14:56:35 2020 +1030

    PR26806, Suspected linker bug with LTO
    
    This patch reverts most of git commit 1e3b96fd6cf, so IR symbols are
    again not marked def_regular or ref_regular.  That should be enough to
    stop IR symbols from becoming dynamic.  To mark as-needed shared
    libraries referenced by IR symbols, use the referencing BFD rather
    than the ref flags.
    
    bfd/
            PR 15146
            PR 26314
            PR 26530
            PR 26806
            * elflink.c (elf_link_add_object_symbols): Don't set def/ref flags
            for plugin syms.  Do allow plugin syms to mark as-needed libs.
    ld/
            PR 26806
            * testsuite/ld-plugin/lto-19.h,
            * testsuite/ld-plugin/lto-19a.c,
            * testsuite/ld-plugin/lto-19b.c,
            * testsuite/ld-plugin/lto-19c.c: New test.
            * testsuite/ld-plugin/pr26806.c,
            * testsuite/ld-plugin/pr26806.d: New test.
            * testsuite/ld-plugin/lto.exp: Run them.
Comment 9 Sourceware Commits 2021-02-04 13:19:09 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=37707bd8221c2e3f7676de8bee0c42ce3ab38c77

commit 37707bd8221c2e3f7676de8bee0c42ce3ab38c77
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Feb 4 05:11:37 2021 -0800

    ld: Restore PR ld/15146 tests
    
    commit 7d409ac001cce916661d345bff01ed589991e762
    Author: Alan Modra <amodra@gmail.com>
    Date:   Thu Feb 4 13:56:34 2021 +1030
    
        PR27311, (symbol from plugin): undefined reference, hidden sym
    
    fixed PR ld/15146.  Restore PR ld/15146 tests.
    
            PR ld/15146
            * testsuite/ld-plugin/lto.exp: Run PR ld/15146 tests.
            * testsuite/ld-plugin/pr15146.d: Restored.
            * testsuite/ld-plugin/pr15146a.c: Likewise.
            * testsuite/ld-plugin/pr15146b.c: Likewise.
            * testsuite/ld-plugin/pr15146c.c: Likewise.
            * testsuite/ld-plugin/pr15146d.c: Likewise.
Comment 10 Sourceware Commits 2021-02-04 13:19:22 UTC
The binutils-2_36-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3592ada38da455903b1ad2425c2c64849c4637fe

commit 3592ada38da455903b1ad2425c2c64849c4637fe
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Feb 4 05:11:37 2021 -0800

    ld: Restore PR ld/15146 tests
    
    commit 7d409ac001cce916661d345bff01ed589991e762
    Author: Alan Modra <amodra@gmail.com>
    Date:   Thu Feb 4 13:56:34 2021 +1030
    
        PR27311, (symbol from plugin): undefined reference, hidden sym
    
    fixed PR ld/15146.  Restore PR ld/15146 tests.
    
            PR ld/15146
            * testsuite/ld-plugin/lto.exp: Run PR ld/15146 tests.
            * testsuite/ld-plugin/pr15146.d: Restored.
            * testsuite/ld-plugin/pr15146a.c: Likewise.
            * testsuite/ld-plugin/pr15146b.c: Likewise.
            * testsuite/ld-plugin/pr15146c.c: Likewise.
            * testsuite/ld-plugin/pr15146d.c: Likewise.
    
    (cherry picked from commit 37707bd8221c2e3f7676de8bee0c42ce3ab38c77)
Comment 11 H.J. Lu 2021-02-04 13:20:49 UTC
Fixed for 2.37 and 2.36 branch.  Only 2.36 release doesn't work.