[PATCH] s390: Do not force symbol dynamic unless it is undefined weak

Jens Remus jremus@linux.ibm.com
Thu Nov 12 11:56:55 GMT 2026


Linking a PIE with a global symbol whose only GOT reference is in a
discarded section fails with the assertion ((h->got.offset & 1) != 0) in
finish_dynamic_symbol.  The GOT entry is accounted for by check_relocs,
but never initialized, as relocate_section is not called for discarded
sections.

The assertion is reached because allocate_dynrelocs makes symbols
dynamic (by calling bfd_elf_link_record_dynamic_symbol) that do not need
to be.  This applies to every symbol with a PLT or GOT reference, and in
non-PIC links to every symbol with dynamic relocations.  A symbol forced
dynamic this way has a dynindx, so finish_dynamic_symbol is called for
it and emits a R_390_RELATIVE relocation for the uninitialized GOT
entry.

Forcing such symbols dynamic is also wrong independent of discarded
sections.  A global symbol that is not meant to be exported gets
published in .dynsym as GLOBAL DEFAULT merely because it has a GOT or
PLT reference.  The dynamic linker's interposition rules then let it
shadow a symbol of the same name in all other loaded DSOs.  For example,
a PIE that defines its own "time" or "times" would interpose the weak
"time" and "times" exported by Glibc, although it never intended to
export them.  Any weak symbol exported by Glibc is affected the same
way.  See the comment of GENERATE_RELATIVE_RELOC_P in x86 commit
f70656b2604e ("x86: Add GENERATE_RELATIVE_RELOC_P").

Align with x86-64 commit 9e9821ddd80c ("x86-64: Force symbol dynamic if
it isn't undefined weak"), RISC-V commit 9e10fcf71c11 ("RISC-V: Fix the
assert fail when linking discarded sections under -pie for got"), and
other targets such as aarch64:

In allocate_dynrelocs only call bfd_elf_link_record_dynamic_symbol for
undefined weak symbols.  The generic code in elf_link_add_object_symbols
only makes symbols dynamic if linking a shared library or if a DSO
defines or references it (def_dynamic/ref_dynamic).  For the dynamic
linker to resolve undefined weak symbols, they must also be forced
dynamic.

As such symbols are no longer forced dynamic, finish_dynamic_symbol is
no longer called for them and thus no longer emits the R_390_RELATIVE
relocation for their GOT entry.  Emit it in relocate_section for a
symbol that is not dynamic, not forced local, and not undefined weak in
a PIC link.  This is where the GOT entry is initialized.  Record this
in the new no_finish_dynamic_symbol flag and abort if
finish_dynamic_symbol is called for such a symbol.

Any .rela.dyn entries reserved for the discarded GOT references remain
as R_390_NONE.  Counting the GOT and PLT references after /DISCARD/, as
x86-64 does by splitting check_relocs into check_relocs and scan_relocs
with commit 3747999c6bd4 ("ld: Extract _bfd_elf_link_iterate_on_relocs"),
would avoid them.

Add linker tests for references to symbols in discarded sections,
inspired by the RISC-V discard-* tests.  The PDE test passes without
dynamic relocations.  The PIE test covers the above assertion and
documents the superfluous R_390_NONE relocations.  The shared library
test additionally documents the superfluous R_390_GLOB_DAT relocation
for the GOT reference in a discarded section.

bfd/
	* elf64-s390.c (elf_s390_link_hash_entry): Add
	no_finish_dynamic_symbol.
	(link_hash_newfunc): Initialize no_finish_dynamic_symbol to 0.
	(allocate_dynrelocs): If a symbol isn't undefined weak, do not
	make it dynamic.
	(elf_s390_relocate_section): If a symbol isn't dynamic in PIC,
	set no_finish_dynamic_symbol and generate R_390_RELATIVE
	relocation for GOT reference.  Factor out generation of
	R_390_RELATIVE relocation for GOT entries.
	(elf_s390_finish_dynamic_symbol): Abort if
	no_finish_dynamic_symbol is set.

ld/testsuite/
	* ld-s390/s390.exp: Run new discard-* tests and GOT reference
	in PIE test.
	* ld-s390/discard.ld: New linker script.
	* ld-s390/discard.s: New test source.
	* ld-s390/discard-pde.d: New test for discarded sections in
	a position-dependent executable (PDE).
	* ld-s390/discard-pie.d: New test for discarded sections in
	a position-independent executable (PIE).
	* ld-s390/discard-shared.d: New test for discarded sections in
	a shared library.
	* ld-s390/gotreloc-2.s: New test source.
	* ld-s390/gotreloc_64-pie-1.rd: New test for GOT references in
	PIE.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
---

Notes (jremus):
    The plan is to address the superfluous dynamic R_390_NONE and
    R_390_GLOB_DAT relocations with a future patch, implementing x86-64's
    solution using check_relocs and scan_relocs from x86 commit 3747999c6bd4
    ("ld: Extract _bfd_elf_link_iterate_on_relocs").

 bfd/elf64-s390.c                          | 82 ++++++++++++++++-------
 ld/testsuite/ld-s390/discard-pde.d        |  6 ++
 ld/testsuite/ld-s390/discard-pie.d        | 20 ++++++
 ld/testsuite/ld-s390/discard-shared.d     | 16 +++++
 ld/testsuite/ld-s390/discard.ld           | 14 ++++
 ld/testsuite/ld-s390/discard.s            | 61 +++++++++++++++++
 ld/testsuite/ld-s390/gotreloc-2.s         | 11 +++
 ld/testsuite/ld-s390/gotreloc_64-pie-1.rd | 11 +++
 ld/testsuite/ld-s390/s390.exp             | 11 +++
 9 files changed, 208 insertions(+), 24 deletions(-)
 create mode 100644 ld/testsuite/ld-s390/discard-pde.d
 create mode 100644 ld/testsuite/ld-s390/discard-pie.d
 create mode 100644 ld/testsuite/ld-s390/discard-shared.d
 create mode 100644 ld/testsuite/ld-s390/discard.ld
 create mode 100644 ld/testsuite/ld-s390/discard.s
 create mode 100644 ld/testsuite/ld-s390/gotreloc-2.s
 create mode 100644 ld/testsuite/ld-s390/gotreloc_64-pie-1.rd

diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 23482e2646ef..89783c90b5bc 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -657,6 +657,9 @@ struct elf_s390_link_hash_entry
 #define GOT_TLS_IE_NLT	4	/* Initial Exec, no literal pool entry.  */
   unsigned char tls_type;
 
+  /* Don't call finish_dynamic_symbol on this symbol.  */
+  unsigned int no_finish_dynamic_symbol : 1;
+
   /* For pointer equality reasons we might need to change the symbol
      type from STT_GNU_IFUNC to STT_FUNC together with its value and
      section entry.  So after alloc_dynrelocs only these values should
@@ -784,6 +787,7 @@ link_hash_newfunc (struct bfd_hash_entry *entry,
       eh = (struct elf_s390_link_hash_entry *) entry;
       eh->gotplt_refcount = 0;
       eh->tls_type = GOT_UNKNOWN;
+      eh->no_finish_dynamic_symbol = 0;
       eh->ifunc_resolver_address = 0;
       eh->ifunc_resolver_section = NULL;
     }
@@ -1762,7 +1766,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h,
       /* Make sure this symbol is output as a dynamic symbol.
 	 Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
-	  && !h->forced_local)
+	  && !h->forced_local
+	  && h->root.type == bfd_link_hash_undefweak)
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return false;
@@ -1844,7 +1849,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h,
       /* Make sure this symbol is output as a dynamic symbol.
 	 Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
-	  && !h->forced_local)
+	  && !h->forced_local
+	  && h->root.type == bfd_link_hash_undefweak)
 	{
 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
 	    return false;
@@ -1933,7 +1939,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h,
 	  /* Make sure this symbol is output as a dynamic symbol.
 	     Undefined weak syms won't yet be marked as dynamic.  */
 	  if (h->dynindx == -1
-	      && !h->forced_local)
+	      && !h->forced_local
+	      && h->root.type == bfd_link_hash_undefweak)
 	    {
 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
 		return false;
@@ -2333,6 +2340,8 @@ elf_s390_relocate_section (struct bfd_link_info *info,
       bfd_reloc_status_type r;
       int tls_type;
       bool resolved_to_zero;
+      bool relative_reloc;
+      bool got_to_larl;
       bool relax;
 
       r_type = ELF64_R_TYPE (rel->r_info);
@@ -2495,6 +2504,8 @@ elf_s390_relocate_section (struct bfd_link_info *info,
 	  if (htab->elf.sgot == NULL)
 	    abort ();
 
+	  relative_reloc = false;
+	  got_to_larl = false;
 	  if (h != NULL)
 	    {
 	      bool dyn;
@@ -2545,9 +2556,10 @@ elf_s390_relocate_section (struct bfd_link_info *info,
 		     least significant bit to record whether we have
 		     initialized it already.
 
-		     When doing a dynamic link, we create a .rel.got
+		     When doing a dynamic link, we create a .rela.got
 		     relocation entry to initialize the value.  This
-		     is done in the finish_dynamic_symbol routine.  */
+		     is done in the finish_dynamic_symbol routine, or
+		     below for symbols that are not dynamic.  */
 		  if ((off & 1) != 0)
 		    off &= ~1;
 		  else
@@ -2555,6 +2567,17 @@ elf_s390_relocate_section (struct bfd_link_info *info,
 		      bfd_put_64 (info->output_bfd, relocation,
 				  htab->elf.sgot->contents + off);
 		      h->got.offset |= 1;
+
+		      /* If this symbol isn't dynamic in PIC,
+			 generate R_390_RELATIVE reloc below.  */
+		      if (h->dynindx == -1
+			  && !h->forced_local
+			  && h->root.type != bfd_link_hash_undefweak
+			  && bfd_link_pic (info))
+			{
+			  elf_s390_hash_entry (h)->no_finish_dynamic_symbol = 1;
+			  relative_reloc = true;
+			}
 		    }
 
 		  /* When turning a GOT slot dereference into a direct
@@ -2596,7 +2619,7 @@ elf_s390_relocate_section (struct bfd_link_info *info,
 		      relocation = h->root.u.def.value
 			+ h->root.u.def.section->output_section->vma
 			+ h->root.u.def.section->output_offset;
-		      goto do_relocation;
+		      got_to_larl = true;
 		    }
 		}
 	      else
@@ -2618,31 +2641,39 @@ elf_s390_relocate_section (struct bfd_link_info *info,
 		{
 		  bfd_put_64 (info->output_bfd, relocation,
 			      htab->elf.sgot->contents + off);
+		  local_got_offsets[r_symndx] |= 1;
 
 		  if (bfd_link_pic (info))
-		    {
-		      asection *s;
-		      Elf_Internal_Rela outrel;
-		      bfd_byte *loc;
+		    relative_reloc = true;
+		}
+	    }
 
-		      s = htab->elf.srelgot;
-		      if (s == NULL)
-			abort ();
+	  /* Generate a R_390_RELATIVE reloc for the GOT entry.  For
+	     dynamic symbols this is done in finish_dynamic_symbol.  */
+	  if (relative_reloc)
+	    {
+	      asection *s;
+	      Elf_Internal_Rela outrel;
+	      bfd_byte *loc;
 
-		      outrel.r_offset = (htab->elf.sgot->output_section->vma
-					 + htab->elf.sgot->output_offset
-					 + off);
-		      outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
-		      outrel.r_addend = relocation;
-		      loc = s->contents;
-		      loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
-		      bfd_elf64_swap_reloca_out (info->output_bfd, &outrel, loc);
-		    }
+	      s = htab->elf.srelgot;
+	      if (s == NULL)
+		abort ();
 
-		  local_got_offsets[r_symndx] |= 1;
-		}
+	      outrel.r_offset = (htab->elf.sgot->output_section->vma
+				 + htab->elf.sgot->output_offset
+				 + off);
+	      outrel.r_info = ELF64_R_INFO (0, R_390_RELATIVE);
+	      outrel.r_addend = relocation;
+	      loc = s->contents;
+	      loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
+	      bfd_elf64_swap_reloca_out (info->output_bfd, &outrel, loc);
 	    }
 
+	  /* The GOT slot load has been turned into a direct reference.  */
+	  if (got_to_larl)
+	    goto do_relocation;
+
 	  if (off >= (bfd_vma) -2)
 	    abort ();
 
@@ -3639,6 +3670,9 @@ elf_s390_finish_dynamic_symbol (struct bfd_link_info *info,
   struct elf_s390_link_hash_table *htab;
   struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h;
 
+  if (eh->no_finish_dynamic_symbol)
+    abort ();
+
   htab = elf_s390_hash_table (info);
 
   if (h->plt.offset != (bfd_vma) -1)
diff --git a/ld/testsuite/ld-s390/discard-pde.d b/ld/testsuite/ld-s390/discard-pde.d
new file mode 100644
index 000000000000..8fc7b005227b
--- /dev/null
+++ b/ld/testsuite/ld-s390/discard-pde.d
@@ -0,0 +1,6 @@
+#source: discard.s
+#as: -m64
+#ld: -melf64_s390 -T discard.ld
+#readelf: -rW
+
+There are no relocations in this file.
diff --git a/ld/testsuite/ld-s390/discard-pie.d b/ld/testsuite/ld-s390/discard-pie.d
new file mode 100644
index 000000000000..23c774f69f49
--- /dev/null
+++ b/ld/testsuite/ld-s390/discard-pie.d
@@ -0,0 +1,20 @@
+#source: discard.s
+#as: -m64
+#ld: -pie -melf64_s390 -T discard.ld
+#readelf: -rW
+
+# FIXME: Superfluous GOT entries and dynamic relocations (R_390_NONE)
+# for references in discarded sections.  The GOT entry of a symbol
+# referenced only from a discarded section is accounted by check_relocs,
+# but never initialized by relocate_section.
+# The proper fix is to split check_relocs into a pre- and post-/DISCARD/
+# phase as x86-64 does with check_relocs and scan_relocs.
+
+Relocation section '\.rela\.dyn' at offset 0x[0-9a-f]+ contains 6 entries:
+    Offset             Info             Type               Symbol's Value  Symbol's Name \+ Addend
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
diff --git a/ld/testsuite/ld-s390/discard-shared.d b/ld/testsuite/ld-s390/discard-shared.d
new file mode 100644
index 000000000000..5e4548b911cc
--- /dev/null
+++ b/ld/testsuite/ld-s390/discard-shared.d
@@ -0,0 +1,16 @@
+#source: discard.s
+#as: -m64
+#ld: -shared -melf64_s390 -T discard.ld
+#readelf: -rW
+
+# FIXME: Superfluous GOT entries and dynamic relocations (R_390_NONE and
+# R_390_GLOB_DAT) for references in discarded sections.  See discard-pie.
+
+Relocation section '\.rela\.dyn' at offset 0x[0-9a-f]+ contains 6 entries:
+    Offset             Info             Type               Symbol's Value  Symbol's Name \+ Addend
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000000000  0000000000000000 R_390_NONE                                0
+0000000000020020  000000030000000a R_390_GLOB_DAT         000000000001000c sym_global \+ 0
diff --git a/ld/testsuite/ld-s390/discard.ld b/ld/testsuite/ld-s390/discard.ld
new file mode 100644
index 000000000000..471a5aed88f9
--- /dev/null
+++ b/ld/testsuite/ld-s390/discard.ld
@@ -0,0 +1,14 @@
+OUTPUT_ARCH(s390:64-bit)
+ENTRY(_start)
+SECTIONS
+{
+  /DISCARD/ : { *(.discard.*) }
+
+  . = 0x10000;
+  .text : { *(.text) }
+  . = 0x20000;
+  .got     : { *(.got) }
+  .got.plt : { *(.got.plt) }
+  . = 0x30000;
+  .data : { *(.data) *(.data.*) }
+}
diff --git a/ld/testsuite/ld-s390/discard.s b/ld/testsuite/ld-s390/discard.s
new file mode 100644
index 000000000000..c9ad385003b0
--- /dev/null
+++ b/ld/testsuite/ld-s390/discard.s
@@ -0,0 +1,61 @@
+# Test references in discarded sections.
+
+.text
+.align 8
+.global _start
+_start:
+	nop
+
+sym_local:
+	nop
+
+.global sym_hidden
+.hidden sym_hidden
+sym_hidden:
+	nop
+
+.global sym_global
+sym_global:
+	nop
+
+.global sym_global_abs
+.set sym_global_abs, 42
+
+.global sym_weak_undef
+.weak sym_weak_undef
+
+.section .discard.got_local,"ax"
+	lgrl	%r0, sym_local@GOTENT
+
+.section .discard.got_global,"ax"
+	lgrl	%r0, sym_global@GOTENT
+
+.section .discard.local,"a"
+.align 8
+discard_local:
+	.quad	sym_local
+
+.section .discard.hidden,"a"
+.align 8
+discard_hidden:
+	.quad	sym_hidden
+
+.section .discard.global,"a"
+.align 8
+discard_global:
+	.quad	sym_global
+
+.section .discard.global_abs,"a"
+.align 8
+discard_global_abs:
+	.quad	sym_global_abs
+
+.section .discard.weak_undef,"a"
+.align 8
+discard_weak_undef:
+	.quad	sym_weak_undef
+
+.section .discard._DYNAMIC,"a"
+.align 8
+discard_DYNAMIC:
+	.quad	_DYNAMIC
diff --git a/ld/testsuite/ld-s390/gotreloc-2.s b/ld/testsuite/ld-s390/gotreloc-2.s
new file mode 100644
index 000000000000..58c03db127d1
--- /dev/null
+++ b/ld/testsuite/ld-s390/gotreloc-2.s
@@ -0,0 +1,11 @@
+.text
+	.globl	foo2
+foo2:
+	lgrl	%r1,hidden_sym@GOTENT
+	lg	%r1,hidden_sym@GOT(%r12)
+
+.data
+	.globl	hidden_sym
+	.hidden	hidden_sym
+hidden_sym:
+	.quad	0x456
diff --git a/ld/testsuite/ld-s390/gotreloc_64-pie-1.rd b/ld/testsuite/ld-s390/gotreloc_64-pie-1.rd
new file mode 100644
index 000000000000..d8d78f33f3cc
--- /dev/null
+++ b/ld/testsuite/ld-s390/gotreloc_64-pie-1.rd
@@ -0,0 +1,11 @@
+Relocation section '\.rela\.dyn' at offset 0x[0-9a-f]+ contains 4 entries:
+ +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ +R_390_RELATIVE +[0-9a-f]+
+[0-9a-f]+ +[0-9a-f]+ +R_390_RELATIVE +[0-9a-f]+
+[0-9a-f]+ +[0-9a-f]+ +R_390_RELATIVE +[0-9a-f]+
+[0-9a-f]+ +[0-9a-f]+ +R_390_RELATIVE +[0-9a-f]+
+
+Symbol table '\.dynsym' contains 2 entries:
+ +Num: +Value +Size Type +Bind +Vis +Ndx Name
+ +0: 0+ +0 NOTYPE +LOCAL +DEFAULT +UND *
+ +1: [0-9a-f]+ +0 SECTION +LOCAL +DEFAULT +[0-9]+ \.text
diff --git a/ld/testsuite/ld-s390/s390.exp b/ld/testsuite/ld-s390/s390.exp
index 80536e2e39a0..30683e6d94f8 100644
--- a/ld/testsuite/ld-s390/s390.exp
+++ b/ld/testsuite/ld-s390/s390.exp
@@ -127,6 +127,11 @@ set s390xtests {
      "-m64" {gotreloc-1.s}
      {{objdump -dzrj.text gotreloc_64-no-pie-1b.dd}}
      "gotreloc_64-1b"}
+    {"GOT: PIE does not export symbols with GOT reference"
+     "-melf64_s390 -pie -e foo" ""
+     "-m64" {gotreloc-1.s gotreloc-2.s}
+     {{readelf "-rW --dyn-syms" gotreloc_64-pie-1.rd}}
+     "gotreloc_64-pie-1"}
     {"PLT: offset test"
      "-shared -m elf64_s390 -dT pltoffset-1.ld --no-error-rwx-segments" ""
      "-m64" {pltoffset-1.s}
@@ -184,11 +189,17 @@ if { [istarget "s390-*-*"] || [istarget "s390x-*-*"] } {
 }
 
 if [istarget "s390x-*-*"] {
+    global NOPIE_LDFLAGS
+
     run_ld_link_tests $s390xtests
 
     run_dump_test "weakundef-1b"
     run_dump_test "weakundef-2b"
 
+    run_dump_test "discard-pde" [list [list ld $NOPIE_LDFLAGS]]
+    run_dump_test "discard-pie"
+    run_dump_test "discard-shared"
+
     if { ![skip_sframe_tests] } {
 	run_dump_test "sframe-simple-1"
 	run_dump_test "sframe-plt-1"
-- 
2.53.0



More information about the Binutils mailing list