From 1fd6d111905e76dfa7af9fbee5229f3f1d360b4c Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Fri, 31 Mar 2017 15:25:18 +0200 Subject: [PATCH] pe/coff: handle weak defined symbol for gc-sections. bfd/ * coffgen.c (_bfd_coff_gc_mark_hook): Handle PE weak external symbols with a definition. (_bfd_coff_gc_mark_extra_sections): Fix typo. ld/ * testsuite/ld-pe/pe.exp: New test. * testsuite/ld-pe/weakdef-1.s: New test source. * testsuite/ld-pe/weakdef-1.d: New test. --- bfd/ChangeLog | 6 ++++++ bfd/coffgen.c | 18 ++++++++++++++++-- ld/ChangeLog | 6 ++++++ ld/testsuite/ld-pe/pe.exp | 2 ++ ld/testsuite/ld-pe/weakdef-1.d | 8 ++++++++ ld/testsuite/ld-pe/weakdef-1.s | 19 +++++++++++++++++++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 ld/testsuite/ld-pe/weakdef-1.d create mode 100644 ld/testsuite/ld-pe/weakdef-1.s diff --git a/bfd/ChangeLog b/bfd/ChangeLog index a1ab3883f00..d912e1aaa14 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,9 @@ +2017-04-07 gingold + + * coffgen.c (_bfd_coff_gc_mark_hook): Handle PE weak + external symbols with a definition. + (_bfd_coff_gc_mark_extra_sections): Fix typo. + 2017-04-07 Alan Modra * po/SRC-POTFILES.in: Regenerate. diff --git a/bfd/coffgen.c b/bfd/coffgen.c index 3c60ed438f6..18f423ba89f 100644 --- a/bfd/coffgen.c +++ b/bfd/coffgen.c @@ -2785,8 +2785,22 @@ _bfd_coff_gc_mark_hook (asection *sec, case bfd_link_hash_common: return h->root.u.c.p->section; - case bfd_link_hash_undefined: case bfd_link_hash_undefweak: + if (h->symbol_class == C_NT_WEAK && h->numaux == 1) + { + /* PE weak externals. A weak symbol may include an auxiliary + record indicating that if the weak symbol is not resolved, + another external symbol is used instead. */ + struct coff_link_hash_entry *h2 = + h->auxbfd->tdata.coff_obj_data->sym_hashes[ + h->aux->x_sym.x_tagndx.l]; + + if (h2 && h2->root.type != bfd_link_hash_undefined) + return h2->root.u.def.section; + } + break; + + case bfd_link_hash_undefined: default: break; } @@ -2897,7 +2911,7 @@ _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info, asection *isec; bfd_boolean some_kept; - if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) + if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour) continue; /* Ensure all linker created sections are kept, and see whether diff --git a/ld/ChangeLog b/ld/ChangeLog index 1bd90a27f03..1d2f43b167e 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,9 @@ +2017-04-07 gingold + + * testsuite/ld-pe/pe.exp: New test. + * testsuite/ld-pe/weakdef-1.s: New test source. + * testsuite/ld-pe/weakdef-1.d: New test. + 2017-04-07 Alan Modra * testsuite/ld-elf/mbind1a.d: Remove matches for PT_LOAD segments. diff --git a/ld/testsuite/ld-pe/pe.exp b/ld/testsuite/ld-pe/pe.exp index 8d200566e33..b69f36ac389 100644 --- a/ld/testsuite/ld-pe/pe.exp +++ b/ld/testsuite/ld-pe/pe.exp @@ -76,6 +76,8 @@ run_dump_test "longsecn-5" run_dump_test "orphan" run_dump_test "orphan_nu" +run_dump_test "weakdef-1" + run_dump_test "pr19803" set pr19803_dll { { "PR 19803: not exporting swept symbols" diff --git a/ld/testsuite/ld-pe/weakdef-1.d b/ld/testsuite/ld-pe/weakdef-1.d new file mode 100644 index 00000000000..57a85656ea5 --- /dev/null +++ b/ld/testsuite/ld-pe/weakdef-1.d @@ -0,0 +1,8 @@ +#source: weakdef-1.s +#target: i*86-*-cygwin* i*86-*-pe i*86-*-mingw* +#ld: -e _start --gc-sections +#objdump: -d + +#... + 401003: a1 00 20 40 00 mov 0x402000,%eax +#pass diff --git a/ld/testsuite/ld-pe/weakdef-1.s b/ld/testsuite/ld-pe/weakdef-1.s new file mode 100644 index 00000000000..ddce4fbd60b --- /dev/null +++ b/ld/testsuite/ld-pe/weakdef-1.s @@ -0,0 +1,19 @@ + .weak _wsym + .section .data$wsym,"w" + .align 4 +_wsym: + .long 1 + + .section .text$start,"x" + .globl _start + .def _start; .scl 2; .type 32; .endef +_start: + pushl %ebp + movl %esp, %ebp + movl _wsym, %eax + testl %eax, %eax + sete %al + movzbl %al, %eax + nop + popl %ebp + ret -- 2.30.2