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.
Fixed.
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.
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.
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.