Bug 13177

Summary: garbage collector retains zombie references to external libraries
Product: binutils Reporter: Rene <prr>
Component: ldAssignee: unassigned
Status: RESOLVED FIXED    
Severity: normal CC: amodra, hjl.tools
Priority: P2    
Version: 2.22   
Target Milestone: ---   
URL: http://sourceware.org/ml/binutils/2011-09/msg00076.html
Host: Target:
Build: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 13232    

Description Rene 2011-09-11 08:41:31 UTC
If shared library is compiled and linked with garbage collection enabled,
zombie references to external libraries are retained that result future link
failure.
(ld 2.21, gcc 4.5.2, Ubuntu 11.04)

Steps to repro:
* Create file shared.c
--8<--
#include <dlfcn.h>
void opt_out(const char *name)
{
    dlopen(name, RTLD_LAZY);
}
__attribute__ ((visibility ("default"))) int add_two(int number)
{
    return number+2;
}
-->8--

* Create shared library by running
gcc -fpic -shared -ffunction-sections -fdata-sections -Wl,--gc-sections
-fvisibility=hidden -o libtest.so shared.c -static-libgcc
Result: SUCCESS, no errors.

* Verify created library references by running
ldd -r libtest.so
Resukt: SUCCESS. No unreferenced symbols and expected list of library bindings.

* Create file main.c:
--8<--
int add_two(int);
int main()
{
    return add_two(-2);
}
-->8--

* Create a program by running:
gcc -o main main.c -L. -ltest
Expected result: Program compailes and links correctly.
Acttual result: FAIL. ld returns:
  ./libtest.so: undefined reference to 'dlopen' 
  collect2: ld returned 1 exit status
Comment 1 H.J. Lu 2011-09-14 19:15:38 UTC
A patch is posted at

http://sourceware.org/ml/binutils/2011-09/msg00076.html
Comment 2 cvs-commit@gcc.gnu.org 2011-09-16 01:17:19 UTC
CVSROOT:    /cvs/src
Module name:    src
Changes by:    hjl@sourceware.org    2011-09-16 01:17:16

Modified files:
    ld/testsuite   : ChangeLog 
    bfd            : ChangeLog elflink.c 
Added files:
    ld/testsuite/ld-elf: pr13177.d pr13177.s 

Log message:
    Also hide symbols without PLT nor GOT references.

    bfd/

    2011-09-15  H.J. Lu  <hongjiu.lu@intel.com>

    PR ld/13177
    * elflink.c (elf_gc_sweep_symbol): Also hide symbols without PLT
    nor GOT references.

    ld/testsuite/

    2011-09-15  H.J. Lu  <hongjiu.lu@intel.com>

    PR ld/13177
    * ld-elf/pr13177.d: New.
    * ld-elf/pr13177.s: Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1458&r2=1.1459
pr13177.d.diff?cvsroot=src&r1=NONE&r2=1.1">http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr13177.d.diff?cvsroot=src&r1=NONE&r2=1.1
pr13177.s.diff?cvsroot=src&r1=NONE&r2=1.1">http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr13177.s.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5470&r2=1.5471
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elflink.c.diff?cvsroot=src&r1=1.419&r2=1.420
Comment 3 H.J. Lu 2011-09-16 04:25:18 UTC
Fixed.
Comment 4 cvs-commit@gcc.gnu.org 2011-10-18 19:06:54 UTC
CVSROOT:    /cvs/src
Module name:    src
Branch:     binutils-2_22-branch
Changes by:    hjl@sourceware.org    2011-10-18 19:06:50

Modified files:
    ld/testsuite   : ChangeLog 
    bfd            : ChangeLog elflink.c 
Removed files:
    ld/testsuite/ld-elf: pr13177.d pr13177.s 

Log message:
    Revert the fix for PR ld/13177.

    bfd/

    2011-10-18  H.J. Lu  <hongjiu.lu@intel.com>

    PR ld/13177
    * elflink.c (elf_gc_sweep_symbol): Don't hide symbols without PLT
    nor GOT references.

    ld/testsuite/

    2011-10-18  H.J. Lu  <hongjiu.lu@intel.com>

    PR ld/13177
    * ld-elf/pr13177.d: Removed.
    * ld-elf/pr13177.s: Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.1460.2.2&r2=1.1460.2.3
pr13177.d.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.2&r2=NONE">http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr13177.d.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.2&r2=NONE
pr13177.s.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.2&r2=NONE">http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr13177.s.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.2&r2=NONE
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.5473.2.5&r2=1.5473.2.6
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elflink.c.diff?cvsroot=src&only_with_tag=binutils-2_22-branch&r1=1.420.2.2&r2=1.420.2.3
Comment 5 H.J. Lu 2011-10-18 19:08:03 UTC
Patch is reverted on 2.22 branch,
Comment 6 cvs-commit@gcc.gnu.org 2011-10-19 07:17:27 UTC
CVSROOT:    /cvs/src
Module name:    src
Changes by:    amodra@sourceware.org    2011-10-19 07:17:21

Modified files:
    bfd            : ChangeLog elf32-arm.c elf32-bfin.c elf32-cris.c 
                     elf32-frv.c elf32-i386.c elf32-m32r.c 
                     elf32-m68k.c elf32-ppc.c elf32-s390.c 
                     elf32-sh.c elf32-spu.c elf32-tilepro.c 
                     elf32-xtensa.c elf64-alpha.c elf64-ppc.c 
                     elf64-s390.c elf64-sh64.c elf64-x86-64.c 
                     elflink.c elf-m10300.c elfxx-sparc.c 
                     elfxx-tilegx.c 
    ld/testsuite   : ChangeLog 
    ld/testsuite/ld-elf: elf.exp 
    ld/testsuite/ld-gc: gc.exp 
    ld/testsuite/lib: ld-lib.exp 
Added files:
    ld/testsuite/ld-gc: libpersonality.s personality.d personality.s 

Log message:
    PR ld/13177
    bfd/
    * elflink.c (_bfd_elf_gc_mark_rsec): Set symbol "mark".
    (elf_gc_sweep_symbol): Don't test plt/got refcounts, instead test
    "mark".  Hide undefweak too.  Clear def_regular and ref_regular.
    * elf-m10300.c (mn10300_elf_relocate_section): Ignore unresolved
    reloc errors from garbage-collected code.
    * elf32-arm.c (elf32_arm_relocate_section): Likewise.
    * elf32-bfin.c (bfin_relocate_section): Likewise.
    (bfinfdpic_relocate_section): Likewise.
    * elf32-cris.c (cris_elf_relocate_section): Likewise.
    * elf32-frv.c (elf32_frv_relocate_section): Likewise.
    * elf32-i386.c (elf_i386_relocate_section): Likewise.
    * elf32-m32r.c (m32r_elf_relocate_section): Likewise.
    * elf32-m68k.c (elf_m68k_relocate_section): Likewise.
    * elf32-ppc.c (ppc_elf_relocate_section): Likewise.
    * elf32-s390.c (elf_s390_relocate_section): Likewise.
    * elf32-sh.c (sh_elf_relocate_section): Likewise.
    * elf32-spu.c (spu_elf_relocate_section): Likewise.
    * elf32-tilepro.c (tilepro_elf_relocate_section): Likewise.
    * elf32-xtensa.c (elf_xtensa_relocate_section): Likewise.
    * elf64-alpha.c (elf64_alpha_relocate_section): Likewise.
    * elf64-ppc.c (ppc64_elf_relocate_section): Likewise.
    * elf64-s390.c (elf_s390_relocate_section): Likewise.
    * elf64-sh64.c (sh_elf64_relocate_section): Likewise.
    * elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.
    * elfxx-sparc.c (_bfd_sparc_elf_relocate_section): Likewise.
    * elfxx-tilegx.c (tilegx_elf_relocate_section): Likewise.
    ld/testsuite/
    * ld-elf/elf.exp: Move test for shared lib support..
    * lib/ld-lib.exp (check_shared_lib_support): ..to here. Add m68hc1*,
    and s/ms1/mt/.
    (check_gc_sections_available): Match hppa*64 not hppa64.  Comment.
    * ld-gc/libpersonality.s: New.
    * ld-gc/personality.s, * ld-gc/personality.d: New.
    * ld-gc/gc.exp: Run personality test.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5494&r2=1.5495
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-arm.c.diff?cvsroot=src&r1=1.277&r2=1.278
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-bfin.c.diff?cvsroot=src&r1=1.54&r2=1.55
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-cris.c.diff?cvsroot=src&r1=1.116&r2=1.117
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-frv.c.diff?cvsroot=src&r1=1.73&r2=1.74
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-i386.c.diff?cvsroot=src&r1=1.260&r2=1.261
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-m32r.c.diff?cvsroot=src&r1=1.102&r2=1.103
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-m68k.c.diff?cvsroot=src&r1=1.126&r2=1.127
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-ppc.c.diff?cvsroot=src&r1=1.302&r2=1.303
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-s390.c.diff?cvsroot=src&r1=1.108&r2=1.109
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-sh.c.diff?cvsroot=src&r1=1.172&r2=1.173
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-spu.c.diff?cvsroot=src&r1=1.101&r2=1.102
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-tilepro.c.diff?cvsroot=src&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-xtensa.c.diff?cvsroot=src&r1=1.130&r2=1.131
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-alpha.c.diff?cvsroot=src&r1=1.181&r2=1.182
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-ppc.c.diff?cvsroot=src&r1=1.366&r2=1.367
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-s390.c.diff?cvsroot=src&r1=1.108&r2=1.109
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-sh64.c.diff?cvsroot=src&r1=1.87&r2=1.88
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-x86-64.c.diff?cvsroot=src&r1=1.239&r2=1.240
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elflink.c.diff?cvsroot=src&r1=1.427&r2=1.428
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf-m10300.c.diff?cvsroot=src&r1=1.108&r2=1.109
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfxx-sparc.c.diff?cvsroot=src&r1=1.61&r2=1.62
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfxx-tilegx.c.diff?cvsroot=src&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1470&r2=1.1471
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/elf.exp.diff?cvsroot=src&r1=1.30&r2=1.31
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-gc/libpersonality.s.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-gc/personality.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-gc/personality.s.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-gc/gc.exp.diff?cvsroot=src&r1=1.11&r2=1.12
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/lib/ld-lib.exp.diff?cvsroot=src&r1=1.85&r2=1.86
Comment 7 Alan Modra 2012-02-22 03:35:16 UTC
Fixed mainline