1 diff --git a/bfd/elflink.c b/bfd/elflink.c
2 index 1f6c1a0..8daebe5 100644
5 @@ -11721,10 +11721,15 @@
7 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
9 - if ((h->root.type == bfd_link_hash_defined
10 - || h->root.type == bfd_link_hash_defweak)
11 - && !h->root.u.def.section->gc_mark
12 - && !(h->root.u.def.section->owner->flags & DYNAMIC))
13 + if (((h->root.type == bfd_link_hash_defined
14 + || h->root.type == bfd_link_hash_defweak)
15 + && !h->root.u.def.section->gc_mark
16 + && (!(h->root.u.def.section->owner->flags & DYNAMIC)
17 + || (h->plt.refcount <= 0
18 + && h->got.refcount <= 0)))
19 + || (h->root.type == bfd_link_hash_undefined
20 + && h->plt.refcount <= 0
21 + && h->got.refcount <= 0))
23 struct elf_gc_sweep_symbol_info *inf =
24 (struct elf_gc_sweep_symbol_info *) data;
26 diff --git a/ld/testsuite/ld-elf/pr13177.d b/ld/testsuite/ld-elf/pr13177.d
28 index 0000000..b58623a
30 +++ b/ld/testsuite/ld-elf/pr13177.d
33 +#ld: --gc-sections -shared
34 +#readelf: -s -D --wide
35 +#notarget: arc-*-* d30v-*-* dlx-*-* i960-*-* or32-*-* pj*-*-*
36 +#notarget: hppa64-*-* i370-*-* i860-*-* ia64-*-* mep-*-* mn10200-*-*
37 +# generic linker targets don't support --gc-sections, nor do a bunch of others
41 +.*: 0+0 +0 +NOTYPE +GLOBAL +DEFAULT +UND bar
43 diff --git a/ld/testsuite/ld-elf/pr13177.s b/ld/testsuite/ld-elf/pr13177.s
45 index 0000000..acc36ab
47 +++ b/ld/testsuite/ld-elf/pr13177.s
49 + .section .text.foo,"ax",%progbits
51 + .type foo, %function
54 + .section .text.opt_out,"ax",%progbits
55 + .type opt_out, %function