[RFC PATCH 15/17] loongarch: give the field size of TLS relocations with no field

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


R_LARCH_TLS_DESC_LD, R_LARCH_TLS_DESC_CALL and R_LARCH_TLS_LE_ADD_R
have no dst_mask, so the relocation offset check skips them.  But
loongarch_tls_perform_trans writes a NOP over the instruction of the
first two, and loongarch_relax_tls_le reads the instruction of the
third.  In a section too small for the instruction, ld writes past
the end of the section contents.  Give them a field size of 4.

bfd/
	* elfnn-loongarch.c (loongarch_elf_reloc_field_size): New
	function.
	(elf_backend_reloc_field_size): Define.

ld/
	* testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.d: New test.
	* testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.s: New file.
	* testsuite/ld-loongarch-elf/ld-loongarch-elf.exp: Run it.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfnn-loongarch.c                         | 23 +++++++++++++++++++
 .../ld-loongarch-elf/ld-loongarch-elf.exp     |  1 +
 .../reloc-field-tls-desc-call.d               |  3 +++
 .../reloc-field-tls-desc-call.s               |  8 +++++++
 4 files changed, 35 insertions(+)
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.s

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 3860ae81be..4e49c533f4 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -7248,6 +7248,29 @@ elfNN_loongarch_size_aligns (bfd *output_bfd,
   return true;
 }
 
+/* R_LARCH_TLS_DESC_LD, R_LARCH_TLS_DESC_CALL and R_LARCH_TLS_LE_ADD_R
+   have no dst_mask, but TLS transitions and relaxation read or rewrite
+   their 4-octet instruction.  */
+
+static bool
+loongarch_elf_reloc_field_size (const bfd *abfd ATTRIBUTE_UNUSED,
+				unsigned int r_type,
+				bool rel_p ATTRIBUTE_UNUSED,
+				unsigned int *size)
+{
+  switch (r_type)
+    {
+    case R_LARCH_TLS_DESC_LD:
+    case R_LARCH_TLS_DESC_CALL:
+    case R_LARCH_TLS_LE_ADD_R:
+      *size = 4;
+      return true;
+    default:
+      return false;
+    }
+}
+#define elf_backend_reloc_field_size loongarch_elf_reloc_field_size
+
 #define TARGET_LITTLE_SYM loongarch_elfNN_vec
 #define TARGET_LITTLE_NAME "elfNN-loongarch"
 #define ELF_ARCH bfd_arch_loongarch
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index b8e335bf77..e55aff00e7 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -21,6 +21,7 @@
 
 if [istarget loongarch*-*-*] {
   run_dump_test "dtprel"
+  run_dump_test "reloc-field-tls-desc-call"
 }
 
 if [istarget "loongarch64-*-*"] {
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.d b/ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.d
new file mode 100644
index 0000000000..d7c38f663d
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.d
@@ -0,0 +1,3 @@
+#name: LoongArch TLS_DESC_CALL field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x4 > 0x2\) for section `\.probe'
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.s b/ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.s
new file mode 100644
index 0000000000..8a4b0c15ff
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.s
@@ -0,0 +1,8 @@
+	.text
+	.globl _start
+_start:
+	.section .probe,"",@progbits
+	.byte 0, 0
+	/* R_LARCH_TLS_DESC_CALL marks a 4-octet instruction, which a TLS
+	   transition rewrites; it would end past this 2-octet section.  */
+	.reloc 0, R_LARCH_TLS_DESC_CALL, _start
-- 
2.55.0




More information about the Binutils mailing list