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

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


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

- R_ARM_THM_BF16, BF12 and BF18 read and write both halfwords of a
  32-bit Thumb instruction, four octets, but their howto size is 2.
- R_ARM_LDR_PC_G0 reads and writes a 32-bit ARM instruction, but its
  howto size is 1.
- R_ARM_TLS_DESCSEQ (four octets), and R_ARM_THM_TLS_DESCSEQ and
  R_ARM_THM_ALU_ABS_G0_NC to G3_NC (two), have no dst_mask, but TLS
  relaxation or elf32_arm_final_link_relocate rewrites their
  instruction.

bfd/
	* elf32-arm.c (elf32_arm_reloc_field_size): New function.
	(elf_backend_reloc_field_size): Define.

ld/
	* testsuite/ld-arm/reloc-field-bf.d: New test.
	* testsuite/ld-arm/reloc-field-bf.s: New file.
	* testsuite/ld-arm/reloc-field-tls-descseq.d: New test.
	* testsuite/ld-arm/reloc-field-tls-descseq.s: New file.
	* testsuite/ld-arm/arm-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elf32-arm.c                               | 33 +++++++++++++++++++
 ld/testsuite/ld-arm/arm-elf.exp               |  2 ++
 ld/testsuite/ld-arm/reloc-field-bf.d          |  4 +++
 ld/testsuite/ld-arm/reloc-field-bf.s          | 11 +++++++
 ld/testsuite/ld-arm/reloc-field-tls-descseq.d |  3 ++
 ld/testsuite/ld-arm/reloc-field-tls-descseq.s |  8 +++++
 6 files changed, 61 insertions(+)
 create mode 100644 ld/testsuite/ld-arm/reloc-field-bf.d
 create mode 100644 ld/testsuite/ld-arm/reloc-field-bf.s
 create mode 100644 ld/testsuite/ld-arm/reloc-field-tls-descseq.d
 create mode 100644 ld/testsuite/ld-arm/reloc-field-tls-descseq.s

diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index 17963aa9cc..aa35867ab9 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -20144,6 +20144,39 @@ elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
 #undef  elf_backend_copy_special_section_fields
 #define elf_backend_copy_special_section_fields elf32_arm_copy_special_section_fields
 
+/* The R_ARM_THM_BF* relocations and R_ARM_LDR_PC_G0 read and write a
+   whole 32-bit instruction, although their howto size is smaller.
+   R_ARM_THM_ALU_ABS_G*_NC and the TLS_DESCSEQ relocations have no
+   dst_mask, but their instruction is rewritten.  */
+
+static bool
+elf32_arm_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_ARM_THM_BF16:
+    case R_ARM_THM_BF12:
+    case R_ARM_THM_BF18:
+    case R_ARM_LDR_PC_G0:
+    case R_ARM_TLS_DESCSEQ:
+      *size = 4;
+      return true;
+    case R_ARM_THM_TLS_DESCSEQ:
+    case R_ARM_THM_ALU_ABS_G0_NC:
+    case R_ARM_THM_ALU_ABS_G1_NC:
+    case R_ARM_THM_ALU_ABS_G2_NC:
+    case R_ARM_THM_ALU_ABS_G3_NC:
+      *size = 2;
+      return true;
+    default:
+      return false;
+    }
+}
+#define elf_backend_reloc_field_size elf32_arm_reloc_field_size
+
 #define ELF_ARCH			bfd_arch_arm
 #define ELF_TARGET_ID			ARM_ELF_DATA
 #define ELF_MACHINE_CODE		EM_ARM
diff --git a/ld/testsuite/ld-arm/arm-elf.exp b/ld/testsuite/ld-arm/arm-elf.exp
index f50c0c1795..9fee2f8776 100644
--- a/ld/testsuite/ld-arm/arm-elf.exp
+++ b/ld/testsuite/ld-arm/arm-elf.exp
@@ -1295,6 +1295,8 @@ run_dump_test "non-contiguous-arm4"
 run_dump_test "non-contiguous-arm5"
 run_dump_test "non-contiguous-arm6"
 run_dump_test "non-contiguous-arm7"
+run_dump_test "reloc-field-bf"
+run_dump_test "reloc-field-tls-descseq"
 
 if { [check_shared_lib_support] } {
     run_dump_test "thumb-plt"
diff --git a/ld/testsuite/ld-arm/reloc-field-bf.d b/ld/testsuite/ld-arm/reloc-field-bf.d
new file mode 100644
index 0000000000..85c8743562
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-field-bf.d
@@ -0,0 +1,4 @@
+#name: ARM THM_BF16 field size for the relocation offset check
+#as: -march=armv8.1-m.main
+#ld:
+#error: .*bad reloc offset \(0x2 \+ 0x4 > 0x4\) for section `\.text'
diff --git a/ld/testsuite/ld-arm/reloc-field-bf.s b/ld/testsuite/ld-arm/reloc-field-bf.s
new file mode 100644
index 0000000000..35a56242d2
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-field-bf.s
@@ -0,0 +1,11 @@
+	.syntax unified
+	.thumb
+	.text
+	.globl _start
+	.type _start, %function
+_start:
+	.hword 0xbf00, 0xbf00
+	/* R_ARM_THM_BF16 reads and writes both halfwords of a 32-bit
+	   Thumb instruction, four octets from offset 2, past this 4-octet
+	   section.  Its howto size is two.  */
+	.reloc 2, R_ARM_THM_BF16, _start
diff --git a/ld/testsuite/ld-arm/reloc-field-tls-descseq.d b/ld/testsuite/ld-arm/reloc-field-tls-descseq.d
new file mode 100644
index 0000000000..c35365377a
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-field-tls-descseq.d
@@ -0,0 +1,3 @@
+#name: ARM TLS_DESCSEQ field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x4 > 0x2\) for section `\.probe'
diff --git a/ld/testsuite/ld-arm/reloc-field-tls-descseq.s b/ld/testsuite/ld-arm/reloc-field-tls-descseq.s
new file mode 100644
index 0000000000..9c8b8ec498
--- /dev/null
+++ b/ld/testsuite/ld-arm/reloc-field-tls-descseq.s
@@ -0,0 +1,8 @@
+	.text
+	.globl _start
+_start:
+	.section .probe,"",%progbits
+	.byte 0, 0
+	/* R_ARM_TLS_DESCSEQ marks a 4-octet instruction, which TLS
+	   relaxation rewrites; it would end past this 2-octet section.  */
+	.reloc 0, R_ARM_TLS_DESCSEQ, _start
-- 
2.55.0




More information about the Binutils mailing list