[RFC PATCH 12/17] riscv: give the field size of TLSDESC_CALL and TPREL_ADD

Harshit Kumar harshit7kr@gmail.com
Mon Nov 16 18:43:28 GMT 2026


R_RISCV_TLSDESC_CALL and R_RISCV_TPREL_ADD have a howto size of 0,
but relaxation rewrites the instruction of the first and deletes that
of the second, so the relocation offset check needs the real size,
four octets.

bfd/
	* elfnn-riscv.c (riscv_elf_reloc_field_size): New function.
	(elf_backend_reloc_field_size): Define.

ld/
	* testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.d: New test.
	* testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.s: New file.
	* testsuite/ld-riscv-elf/reloc-field-tprel-add.d: New test.
	* testsuite/ld-riscv-elf/reloc-field-tprel-add.s: New file.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfnn-riscv.c                               | 16 ++++++++++++++++
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp      |  2 ++
 .../ld-riscv-elf/reloc-field-tlsdesc-call.d     |  4 ++++
 .../ld-riscv-elf/reloc-field-tlsdesc-call.s     | 17 +++++++++++++++++
 .../ld-riscv-elf/reloc-field-tprel-add.d        |  3 +++
 .../ld-riscv-elf/reloc-field-tprel-add.s        |  8 ++++++++
 6 files changed, 50 insertions(+)
 create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.d
 create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.s
 create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.d
 create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.s

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 17c714ab2c..a31d05f3d7 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -150,6 +150,22 @@
 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
 
+/* R_RISCV_TLSDESC_CALL and R_RISCV_TPREL_ADD have a zero howto size,
+   but relaxation rewrites or deletes their 4-octet instruction.  */
+
+static bool
+riscv_elf_reloc_field_size (const bfd *abfd ATTRIBUTE_UNUSED,
+			    unsigned int r_type,
+			    bool rel_p ATTRIBUTE_UNUSED,
+			    unsigned int *size)
+{
+  if (r_type != R_RISCV_TLSDESC_CALL && r_type != R_RISCV_TPREL_ADD)
+    return false;
+  *size = 4;
+  return true;
+}
+#define elf_backend_reloc_field_size riscv_elf_reloc_field_size
+
 #define ELF_ARCH			bfd_arch_riscv
 #define ELF_TARGET_ID			RISCV_ELF_DATA
 #define ELF_MACHINE_CODE		EM_RISCV
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index ad12f8d63b..f1d4569ed0 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -412,6 +412,8 @@ if [istarget "riscv*-*-*"] {
     }
     run_dump_test "tls"
     run_dump_test "tlsbin"
+    run_dump_test "reloc-field-tlsdesc-call"
+    run_dump_test "reloc-field-tprel-add"
     run_dump_test "tlsdesc-ie"
     run_dump_test "tlsdesc-ie-dyn"
     run_dump_test "tlsdesc-ie-32"
diff --git a/ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.d b/ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.d
new file mode 100644
index 0000000000..d95252e3f5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.d
@@ -0,0 +1,4 @@
+#name: RISC-V TLSDESC_CALL field size for the relocation offset check
+#ld:
+#target: riscv64*-*-*
+#error: .*bad reloc offset \(0xc \+ 0x4 > 0xc\) for section `\.text'
diff --git a/ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.s b/ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.s
new file mode 100644
index 0000000000..0b04dbf0ea
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.s
@@ -0,0 +1,17 @@
+	.section	.tbss,"awT",@nobits
+	.zero	0x10
+	.globl	sl
+	.type	sl,@object
+	.size	sl,4
+sl:
+	.zero	4
+
+	.text
+	.globl	_start
+	.type	_start,@function
+_start:
+.desc1:
+	auipc	a0, %tlsdesc_hi(sl)
+	ld	t0, %tlsdesc_load_lo(.desc1)(a0)
+	addi	a0, a0, %tlsdesc_add_lo(.desc1)
+	.reloc	12, R_RISCV_TLSDESC_CALL, .desc1
diff --git a/ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.d b/ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.d
new file mode 100644
index 0000000000..fc78b0f06a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.d
@@ -0,0 +1,3 @@
+#name: RISC-V TPREL_ADD field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x4 > 0x2\) for section `\.probe'
diff --git a/ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.s b/ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.s
new file mode 100644
index 0000000000..6c1801d7de
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.s
@@ -0,0 +1,8 @@
+	.text
+	.globl _start
+_start:
+	.section .probe,"",@progbits
+	.byte 0, 0
+	/* R_RISCV_TPREL_ADD marks a 4-octet instruction, which relaxation
+	   deletes; it would end past this 2-octet section.  */
+	.reloc 0, R_RISCV_TPREL_ADD, _start
-- 
2.55.0




More information about the Binutils mailing list