[RFC PATCH 10/17] aarch64: give the field size of relocations wider than their howto

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


Some AArch64 relocations access more octets at r_offset than their
howto says, so the relocation offset check needs the real size:

- R_AARCH64_TLS_DTPREL64 (P32_TLS_DTPREL for ILP32) is an 8-octet
  field, for ILP32 too, since gas accepts %dtprel only in .xword or
  .dword data.  It is stored with bfd_put_64, but its howto size is 4.
- R_AARCH64_TLSDESC_CALL, and for LP64 R_AARCH64_TLSDESC_LDR and
  R_AARCH64_TLSDESC_ADD, have no dst_mask, but TLS relaxation
  rewrites their instruction, four octets.  ILP32 has no TLSDESC_LDR
  or TLSDESC_ADD relocation.

bfd/
	* elfnn-aarch64.c (elfNN_aarch64_reloc_field_size): New function.
	(elf_backend_reloc_field_size): Define.

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

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfnn-aarch64.c                           | 30 +++++++++++++++++++
 ld/testsuite/ld-aarch64/aarch64-elf.exp       |  4 +++
 ld/testsuite/ld-aarch64/reloc-field-dtprel.d  |  3 ++
 ld/testsuite/ld-aarch64/reloc-field-dtprel.s  | 12 ++++++++
 .../ld-aarch64/reloc-field-tlsdesc-add.d      |  3 ++
 .../ld-aarch64/reloc-field-tlsdesc-add.s      |  8 +++++
 .../ld-aarch64/reloc-field-tlsdesc-call.d     |  3 ++
 .../ld-aarch64/reloc-field-tlsdesc-call.s     | 12 ++++++++
 8 files changed, 75 insertions(+)
 create mode 100644 ld/testsuite/ld-aarch64/reloc-field-dtprel.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-field-dtprel.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.s

diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index dbb29549df..bbe51a4e0f 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -10761,6 +10761,36 @@ static const struct elf_size_info elfNN_aarch64_size_info =
   bfd_elfNN_swap_reloca_out
 };
 
+/* TLS_DTPREL is an 8-octet field, for ILP32 too, as the assembler
+   accepts %dtprel only in .xword or .dword data.  It is stored with
+   bfd_put_64, although its howto size is 4.  TLSDESC_CALL, and for
+   LP64 TLSDESC_LDR and TLSDESC_ADD, have no dst_mask, but TLS
+   relaxation rewrites their instruction.  */
+
+static bool
+elfNN_aarch64_reloc_field_size (const bfd *abfd ATTRIBUTE_UNUSED,
+				unsigned int r_type,
+				bool rel_p ATTRIBUTE_UNUSED,
+				unsigned int *size)
+{
+  switch (r_type)
+    {
+    case AARCH64_R (TLS_DTPREL):
+      *size = 8;
+      return true;
+#if ARCH_SIZE == 64
+    case AARCH64_R (TLSDESC_LDR):
+    case AARCH64_R (TLSDESC_ADD):
+#endif
+    case AARCH64_R (TLSDESC_CALL):
+      *size = 4;
+      return true;
+    default:
+      return false;
+    }
+}
+#define elf_backend_reloc_field_size elfNN_aarch64_reloc_field_size
+
 #define ELF_ARCH			bfd_arch_aarch64
 #define ELF_TARGET_ID			AARCH64_ELF_DATA
 #define ELF_MACHINE_CODE		EM_AARCH64
diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp
index f4941b0151..5ed1d8ed3d 100644
--- a/ld/testsuite/ld-aarch64/aarch64-elf.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp
@@ -445,6 +445,10 @@ if { [supports_dt_relr] } {
   run_dump_test "relr-got-shared-ilp32"
 }
 
+run_dump_test_lp64 "reloc-field-dtprel"
+run_dump_test_lp64 "reloc-field-tlsdesc-call"
+run_dump_test_lp64 "reloc-field-tlsdesc-add"
+
 if { ![skip_sframe_tests] } {
   run_dump_test "sframe-simple-1"
   run_dump_test "sframe-command-line-1"
diff --git a/ld/testsuite/ld-aarch64/reloc-field-dtprel.d b/ld/testsuite/ld-aarch64/reloc-field-dtprel.d
new file mode 100644
index 0000000000..78a2db3c59
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-field-dtprel.d
@@ -0,0 +1,3 @@
+#name: AArch64 TLS_DTPREL field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x8 > 0x4\) for section `\.probe'
diff --git a/ld/testsuite/ld-aarch64/reloc-field-dtprel.s b/ld/testsuite/ld-aarch64/reloc-field-dtprel.s
new file mode 100644
index 0000000000..cf340a2b1f
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-field-dtprel.s
@@ -0,0 +1,12 @@
+	.text
+	.globl _start
+_start:
+	ret
+	.section .tdata,"awT",%progbits
+tv:
+	.word 0
+	.section .probe,"",%progbits
+	.word 0
+	/* R_AARCH64_TLS_DTPREL stores 8 octets, past the end of this
+	   4-octet non-alloc section.  */
+	.reloc 0, R_AARCH64_TLS_DTPREL64, tv
diff --git a/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.d b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.d
new file mode 100644
index 0000000000..a24eb6416d
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.d
@@ -0,0 +1,3 @@
+#name: AArch64 TLSDESC_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-aarch64/reloc-field-tlsdesc-add.s b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.s
new file mode 100644
index 0000000000..38a111e048
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.s
@@ -0,0 +1,8 @@
+	.text
+	.globl _start
+_start:
+	.section .probe,"",%progbits
+	.byte 0, 0
+	/* R_AARCH64_TLSDESC_ADD marks a 4-octet instruction, which TLS
+	   relaxation rewrites; it would end past this 2-octet section.  */
+	.reloc 0, R_AARCH64_TLSDESC_ADD, _start
diff --git a/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.d b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.d
new file mode 100644
index 0000000000..ff8cc25409
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.d
@@ -0,0 +1,3 @@
+#name: AArch64 TLSDESC_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-aarch64/reloc-field-tlsdesc-call.s b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.s
new file mode 100644
index 0000000000..ab7bc0d072
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.s
@@ -0,0 +1,12 @@
+	.text
+	.globl _start
+_start:
+	ret
+	.section .tdata,"awT",%progbits
+tv:
+	.word 0
+	.section .probe,"",%progbits
+	.hword 0
+	/* R_AARCH64_TLSDESC_CALL marks a 4-octet instruction, which TLS
+	   relaxation rewrites, past the end of this 2-octet section.  */
+	.reloc 0, R_AARCH64_TLSDESC_CALL, tv
-- 
2.55.0




More information about the Binutils mailing list