Bug 22649 - -gc-sections preserves hidden symbols that are also visible in dynamic objects
Summary: -gc-sections preserves hidden symbols that are also visible in dynamic objects
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.30
: P2 normal
Target Milestone: 2.30
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-12-29 15:06 UTC by Alexander Monakov
Modified: 2018-03-31 12:41 UTC (History)
2 users (show)

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 Alexander Monakov 2017-12-29 15:06:37 UTC
Consider the follow testcase with two shared libraries:

$ cat > lib1.s
.globl foo
foo:

$ cat > lib2.s
.hidden foo
.hidden bar
.globl foo
.globl bar
foo:
bar:
call foo@PLT

$ gcc -c *.s

$ ld-new -shared -o lib1.so lib1.o

$ ld-new -shared -o lib2.so lib2.o -gc-sections -print-gc-sections
ld-new: Removing unused section '.text' in file 'lib2.o'

$ ld-new -shared -o lib2.so lib2.o -gc-sections -print-gc-sections lib1.so
[no output]

This is surprising: somehow specifying lib1.so on the command line has the effect of pinning "foo" as a GC root.
Comment 1 H.J. Lu 2018-01-11 23:37:45 UTC
Fixed.
Comment 2 Sourceware Commits 2018-01-11 23:38:02 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=d664fd41e15f058aab41b70c567ad09f2fab1115

commit d664fd41e15f058aab41b70c567ad09f2fab1115
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 11 15:36:40 2018 -0800

    Ignore dynamic references on forced local symbols
    
    We should ignore dynamic references on forced local symbols during
    garbage collection since they can never be referenced dynamically.
    
    bfd/
    
    	PR ld/22649
    	* elflink.c (bfd_elf_gc_mark_dynamic_ref_symbol): Ignore dynamic
    	references on forced local symbols.
    
    ld/
    
    	PR ld/22649
    	* testsuite/ld-elf/pr22649-1.s: New file.
    	* testsuite/ld-elf/pr22649-2a.s: Likewise.
    	* testsuite/ld-elf/pr22649-2b.s: Likewise.
    	* testsuite/ld-elf/pr22649.msg: Likewise.
    	* testsuite/ld-elf/shared.exp: Run ld/22649 tests.
Comment 3 Sourceware Commits 2018-01-12 10:47:51 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

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

commit 87e79a6515951fece72ee08871dd6e112b1042ba
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Jan 12 16:58:04 2018 +1030

    Fixes for "Ignore dynamic references on forced local symbols"
    
    PowerPC64 has its own mark_dynamic_ref, which needs the same change as
    made by d664fd41e1 to the generic ELF version.  Some other targets
    discard more than just .data, so allow for that too in expected ld
    messages.
    
    bfd/
    	PR ld/22649
    	* elf64-ppc.c (ppc64_elf_gc_mark_dynamic_ref): Ignore dynamic
    	references on forced local symbols.
    ld/
    	PR ld/22649
    	* testsuite/ld-elf/pr22649.msg: Allow other messages.
    	* testsuite/ld-elf/shared.exp: Check that --gc-sections is
    	supported before running ld/22649 tests.
Comment 4 Sourceware Commits 2018-01-30 01:36:44 UTC
The master branch has been updated by Maciej W. Rozycki <macro@sourceware.org>:

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

commit af667f2218ecf772ac2f8882ceba6a16ddc18ddc
Author: Maciej W. Rozycki <macro@mips.com>
Date:   Tue Jan 30 01:31:23 2018 +0000

    MIPS/LD/testsuite: Correct PR ld/22649 test case failures
    
    Fix commit d664fd41e15f ("Ignore dynamic references on forced local
    symbols") and use alternative test actions and match patterns to
    correctly handle messages like:
    
    .../ld/ld-new: Removing unused section '.reginfo' in file 'tmpdir/pr22649-2b.o'
    
    or:
    
    .../ld/ld-new: Removing unused section '.MIPS.options' in file 'tmpdir/pr22649-2b.o'
    
    produced by LD on MIPS targets, removing:
    
    FAIL: Build pr22649-2c.so
    FAIL: Build pr22649-2d.so
    
    test suite failures and tightening checks made with `pr22649-2a.so' and
    `pr22649-2b.so' test cases.
    
    Keep the original empty action with `pr22649-2c.so' and `pr22649-2d.so'
    links and MIPS/ELF targets though, because for them the linker does not
    garbage-collect the `.reginfo' section.  This is because the section has
    its flags set differently by code in GAS in `md_begin':
    
        if (strncmp (TARGET_OS, "elf", 3) != 0)
          flags |= SEC_ALLOC | SEC_LOAD;
    
    and consequently BFD linker code in `_bfd_elf_gc_mark_extra_sections':
    
    	  else if (((isec->flags & SEC_DEBUGGING) != 0
    		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
    		   && elf_next_in_group (isec) == NULL)
    	    isec->gc_mark = 1;
    
    marks these sections to be kept due to their SEC_ALLOC, SEC_LOAD and
    SEC_RELOC flags all being zero (`.reginfo' sections never have
    relocations attached).
    
    	ld/
    	PR ld/22649
    	* testsuite/ld-elf/pr22649-2ab-mips.msg: New stderr output.
    	* testsuite/ld-elf/pr22649-2cd-mips.msg: New stderr output.
    	* testsuite/ld-elf/shared.exp: Use the new outputs with
    	`mips*-*-*' targets.