pe/coff: handle weak defined symbol for gc-sections.
authorTristan Gingold <gingold@adacore.com>
Fri, 31 Mar 2017 13:25:18 +0000 (15:25 +0200)
committerTristan Gingold <gingold@adacore.com>
Fri, 7 Apr 2017 08:03:17 +0000 (10:03 +0200)
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
bfd/coffgen.c
ld/ChangeLog
ld/testsuite/ld-pe/pe.exp
ld/testsuite/ld-pe/weakdef-1.d [new file with mode: 0644]
ld/testsuite/ld-pe/weakdef-1.s [new file with mode: 0644]

index a1ab3883f00b040326428f1d25c929ba89d6a119..d912e1aaa1449bc35b6374b61693cac9e098d2c1 100644 (file)
@@ -1,3 +1,9 @@
+2017-04-07  gingold  <gingold@gingold-Precision-7510>
+
+       * 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  <amodra@gmail.com>
 
        * po/SRC-POTFILES.in: Regenerate.
index 3c60ed438f6ca349a35d34c132a91e32643289f7..18f423ba89f8e5ba92d380e75f893e538838e91f 100644 (file)
@@ -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
index 1bd90a27f03b5a6741ca8c8e2bee7a9776ad89fe..1d2f43b167e579c5de6e7eb139bb05a0921cddaa 100644 (file)
@@ -1,3 +1,9 @@
+2017-04-07  gingold  <gingold@gingold-Precision-7510>
+
+       * 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  <amodra@gmail.com>
 
        * testsuite/ld-elf/mbind1a.d: Remove matches for PT_LOAD segments.
index 8d200566e33e6a5793fff51453e593aab4fad381..b69f36ac389d85fc59482e381a3b558b3354a3f3 100644 (file)
@@ -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 (file)
index 0000000..57a8565
--- /dev/null
@@ -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 (file)
index 0000000..ddce4fb
--- /dev/null
@@ -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