[PATCH]: coff/pe: fix handling of weak defined symbols for --gc-sections

Tristan Gingold gingold@adacore.com
Fri Mar 31 13:36:00 GMT 2017


Hello,

this patch handles the coff/pe specific way to represent a potential 
definition for a weak symbol in gc-section.
There is a link in the undefined weak symbol to the definition.  So the 
section for the definition must be keep if there is a reference to the
undefined symbol.

No regression on i686-pc-mingw32
Ok to commit ?

Regards,
Tristan.

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index bb851c7..e2c0103 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,9 @@
+2017-03-31  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-03-30  Pip Cet  <pipcet@gmail.com>

  	* elf32-wasm32.c: Add relocation code, two relocs.
diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index 3c60ed4..18f423b 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 84ef78e..e0fd169 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,9 @@
+2017-03-31  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-03-28  Hans-Peter Nilsson  <hp@axis.com>

  	PR ld/16044
diff --git a/ld/testsuite/ld-pe/pe.exp b/ld/testsuite/ld-pe/pe.exp
index 8d20056..b69f36a 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 0000000..57a8565
--- /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 0000000..ddce4fb
--- /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



More information about the Binutils mailing list