[PATCH] RISC-V: Don't partially relax TLS LE address sequences [PR ld/34957]

Shivam Gupta shivam98.tkg@gmail.com
Fri Nov 27 04:48:39 GMT 2026


When R_RISCV_TPREL_HI20 / R_RISCV_TPREL_ADD are marked with
R_RISCV_RELAX but a R_RISCV_TPREL_LO12_I/S for the same symbol and
addend is not, ld deleted the lui/add while leaving the consumer using
its original base register, reviving a dead incoming value.

Only delete the producers when every TPREL_LO12 consumer of the same
symbol+addend in the section is itself relaxable.

	PR ld/34957
bfd/
	* elfnn-riscv.c (riscv_tls_le_has_unrelaxable_lo): New.
	(_bfd_riscv_relax_section): Use it before relaxing
	R_RISCV_TPREL_HI20 and R_RISCV_TPREL_ADD.
ld/
	* testsuite/ld-riscv-elf/tlsle-partial-relax.s: New test.
	* testsuite/ld-riscv-elf/tlsle-partial-relax.d: New test.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run it.

Signed-off-by: Shivam Gupta <Shivam98.tkg@gmail.com>
---
 bfd/elfnn-riscv.c                             | 35 +++++++++++++++++++
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |  1 +
 .../ld-riscv-elf/tlsle-partial-relax.d        | 16 +++++++++
 .../ld-riscv-elf/tlsle-partial-relax.s        | 30 ++++++++++++++++
 4 files changed, 82 insertions(+)
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsle-partial-relax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/tlsle-partial-relax.s

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 4c4ddbb78f7..36c5e126eab 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -7032,6 +7032,31 @@ bfd_elfNN_riscv_init_relax_passes (struct bfd_link_info *info)
   return htab->num_relax_passes;
 }
 
+/* Return true if SEC has an R_RISCV_TPREL_LO12_I/S for the same symbol
+   and addend as REL that is not paired with R_RISCV_RELAX.  Such a
+   consumer keeps its original base register, so deleting the lui/add
+   that produce that register would change program semantics.  */
+static bool
+riscv_tls_le_has_unrelaxable_lo (asection *sec, Elf_Internal_Rela *relocs,
+				 Elf_Internal_Rela *rel)
+{
+  for (unsigned int i = 0; i < sec->reloc_count; i++)
+    {
+      Elf_Internal_Rela *r = relocs + i;
+      int type = ELFNN_R_TYPE (r->r_info);
+
+      if ((type == R_RISCV_TPREL_LO12_I || type == R_RISCV_TPREL_LO12_S)
+	  && ELFNN_R_SYM (r->r_info) == ELFNN_R_SYM (rel->r_info)
+	  && r->r_addend == rel->r_addend)
+	{
+	  unsigned int j = i;
+	  if (!riscv_relax_skip_paired_relax (sec, relocs, &j))
+	    return true;
+	}
+    }
+  return false;
+}
+
 /* Relax a section, using the pass that info->relax_pass selects.  */
 
 static bool
@@ -7124,6 +7149,16 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
       if (relax_func == NULL)
 	continue;
 
+      /* Don't delete a TLS LE address producer (lui/add) if a
+	 R_RISCV_TPREL_LO12_I/S for the same symbol+addend in this
+	 section is not relaxable; it would keep using the base
+	 register those instructions computed.  */
+      if (relax_func == _bfd_riscv_relax_tls_le
+	  && (ELFNN_R_TYPE (rel->r_info) == R_RISCV_TPREL_HI20
+	      || ELFNN_R_TYPE (rel->r_info) == R_RISCV_TPREL_ADD)
+	  && riscv_tls_le_has_unrelaxable_lo (sec, relocs, rel))
+	continue;
+
       data->relocs = relocs;
 
       /* Read this BFD's contents if we haven't done so already.  */
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index 786439f4598..59e1677dd7e 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -575,5 +575,6 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "tlsdesc-le-norelax"
     run_dump_test "tlsdesc-unrelaxed"
     run_dump_test "tlsdesc-unrelaxed-le"
+    run_dump_test "tlsle-partial-relax"
     run_dump_test "absolute-no-relative"
 }
diff --git a/ld/testsuite/ld-riscv-elf/tlsle-partial-relax.d b/ld/testsuite/ld-riscv-elf/tlsle-partial-relax.d
new file mode 100644
index 00000000000..e19cd0aaf58
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsle-partial-relax.d
@@ -0,0 +1,16 @@
+#source: tlsle-partial-relax.s
+#as: -march=rv64i
+#ld: -melf64lriscv
+#objdump: -d
+
+#...
+0+[0-9a-f]+ <partial>:
+.*:[ 	]+[0-9a-f]+[ 	]+lui[ 	]+a0,0x0
+.*:[ 	]+[0-9a-f]+[ 	]+add[ 	]+a0,a0,tp
+.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a0,0\(a0\).*
+.*:[ 	]+[0-9a-f]+[ 	]+ret
+
+0+[0-9a-f]+ <full>:
+.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a1,8\(tp\).*
+.*:[ 	]+[0-9a-f]+[ 	]+ret
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/tlsle-partial-relax.s b/ld/testsuite/ld-riscv-elf/tlsle-partial-relax.s
new file mode 100644
index 00000000000..5d8753845ea
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsle-partial-relax.s
@@ -0,0 +1,30 @@
+	.section .tdata,"awT",@progbits
+	.align 3
+tvar:	.dword 7
+tvar2:	.dword 9
+
+	.text
+	.globl _start
+_start:
+	ret
+
+	# LO12 consumer is not relaxable: lui/add must be kept.
+	.option push
+	.option relax
+partial:
+	lui	a0, %tprel_hi(tvar)
+	add	a0, a0, tp, %tprel_add(tvar)
+	.option norelax
+	ld	a0, %tprel_lo(tvar)(a0)
+	.option pop
+	ret
+
+	# Fully marked group: still relaxed to a tp-relative load.
+	.option push
+	.option relax
+full:
+	lui	a1, %tprel_hi(tvar2)
+	add	a1, a1, tp, %tprel_add(tvar2)
+	ld	a1, %tprel_lo(tvar2)(a1)
+	.option pop
+	ret
-- 
2.43.0



More information about the Binutils mailing list