[PATCH 2/2] aarch64: bound the TLS relaxation spans and sequence length

Harshit Kumar harshit7kr@gmail.com
Tue Nov 24 07:31:27 GMT 2026


elfNN_aarch64_tls_relax rewrites a fixed instruction sequence at the
relocation offset.  The sequence is up to 20 octets, wider than the
howto size, and the GD and TLSDESC transitions read the following one
or two relocations.  A crafted object could place such a relocation
near the end of a short section, or leave the sequence truncated, and
the linker wrote past the section contents or read past the relocation
array.  Previously only BFD_ASSERT covered this, which the default
handler reports and then continues out of bounds.

Check the written span against the section limit, and check that the
relocations the sequence needs are present, before each transition.
On a bad object stop the transition and fail the link.

bfd/
	* elfnn-aarch64.c (elfNN_aarch64_tls_relax): Add relend
	parameter.  Check the written span and the relocation sequence
	length before each TLS transition, and return
	bfd_reloc_outofrange if either fails.
	(elfNN_aarch64_relocate_section): Pass relend, and fail the link
	on bfd_reloc_outofrange.

ld/
	* testsuite/ld-aarch64/reloc-tls-span.s: New test.
	* testsuite/ld-aarch64/reloc-tls-span.d: New test.
	* testsuite/ld-aarch64/reloc-tls-seq.s: New test.
	* testsuite/ld-aarch64/reloc-tls-seq.d: New test.
	* testsuite/ld-aarch64/aarch64-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfnn-aarch64.c                      | 59 +++++++++++++++++++++++-
 ld/testsuite/ld-aarch64/aarch64-elf.exp  |  2 +
 ld/testsuite/ld-aarch64/reloc-tls-seq.d  |  4 ++
 ld/testsuite/ld-aarch64/reloc-tls-seq.s  | 13 ++++++
 ld/testsuite/ld-aarch64/reloc-tls-span.d |  4 ++
 ld/testsuite/ld-aarch64/reloc-tls-span.s | 13 ++++++
 6 files changed, 93 insertions(+), 2 deletions(-)
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-seq.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-seq.s
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-span.d
 create mode 100644 ld/testsuite/ld-aarch64/reloc-tls-span.s

diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index c6643fe2c5..3438283c6c 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -6714,15 +6714,52 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 			 bfd *input_bfd, asection *input_section,
 			 bfd_byte *contents, Elf_Internal_Rela *rel,
 			 struct elf_link_hash_entry *h,
-			 struct bfd_link_info *info)
+			 struct bfd_link_info *info,
+			 const Elf_Internal_Rela *relend)
 {
   bool local_exec = bfd_link_executable (info)
     && SYMBOL_REFERENCES_LOCAL (info, h);
   unsigned int r_type = ELFNN_R_TYPE (rel->r_info);
   unsigned long insn;
+  bfd_vma limit = bfd_get_section_limit_octets (input_bfd, input_section);
+  reloc_howto_type *howto
+    = elfNN_aarch64_howto_from_type (input_bfd, r_type);
+  const char *name = howto != NULL ? howto->name : "?";
 
   BFD_ASSERT (globals && input_bfd && contents && rel);
 
+/* A TLS relaxation rewrites a sequence described by the N relocations
+   that follow REL, so those relocations must be present.  A truncated
+   sequence in a bad object would otherwise be read past the end of the
+   relocation array.  */
+#define TLS_RELAX_NEED(n)						\
+  do									\
+    {									\
+      if (relend - rel <= (n))						\
+	{								\
+	  _bfd_error_handler						\
+	    /* xgettext:c-format */					\
+	    (_("%pB(%pA+%#" PRIx64 "): "				\
+	       "truncated TLS relaxation sequence"),			\
+	     input_bfd, input_section, (uint64_t) rel->r_offset);	\
+	  bfd_set_error (bfd_error_bad_value);				\
+	  return bfd_reloc_outofrange;					\
+	}								\
+    }									\
+  while (0)
+
+/* The transition rewrites a fixed instruction sequence at OFF that may be
+   wider than the howto size, so the whole SPAN must lie inside the
+   section.  On a bad object stop the transition and fail the link.  */
+#define TLS_RELAX_SPAN(off, span)					\
+  do									\
+    {									\
+      if (!aarch64_reloc_offset_in_range (input_bfd, input_section,	\
+					  name, (off), (span), limit))	\
+	return bfd_reloc_outofrange;					\
+    }									\
+  while (0)
+
   switch (elfNN_aarch64_bfd_reloc_from_type (input_bfd, r_type))
     {
     case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
@@ -6756,6 +6793,8 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
       break;
 
     case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
+      TLS_RELAX_NEED (2);
+      TLS_RELAX_SPAN (rel->r_offset, 12);
       if (local_exec)
 	{
 	  /* Tiny TLSDESC->LE relaxation:
@@ -6798,6 +6837,8 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 	}
 
     case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
+      TLS_RELAX_NEED (1);
+      TLS_RELAX_SPAN (rel->r_offset, 12);
       if (local_exec)
 	{
 	  /* Tiny GD->LE relaxation:
@@ -6846,6 +6887,8 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 
 #if ARCH_SIZE == 64
     case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
+      TLS_RELAX_NEED (2);
+      TLS_RELAX_SPAN (rel->r_offset, 20);
       BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (TLSGD_MOVW_G0_NC));
       BFD_ASSERT (rel->r_offset + 12 == rel[2].r_offset);
       BFD_ASSERT (ELFNN_R_TYPE (rel[2].r_info) == AARCH64_R (CALL26));
@@ -6915,6 +6958,7 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 	}
 
     case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
+      TLS_RELAX_NEED (1);
       if (local_exec)
 	{
 	  /* GD->LE relaxation
@@ -6928,6 +6972,7 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 	  BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
 	  rel[1].r_info = ELFNN_R_INFO (STN_UNDEF, R_AARCH64_NONE);
 
+	  TLS_RELAX_SPAN (rel->r_offset, 12);
 	  bfd_putl32 (movk_R0, contents + rel->r_offset);
 	  bfd_putl32 (0xd53bd041, contents + rel->r_offset + 4);
 	  bfd_putl32 (add_R0_R0_R1, contents + rel->r_offset + 8);
@@ -6951,6 +6996,7 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 	  /* We choose to fixup the BL and NOP instructions using the
 	     offset from the second relocation to allow flexibility in
 	     scheduling instructions between the ADD and BL.  */
+	  TLS_RELAX_SPAN (rel[1].r_offset, 8);
 	  bfd_putl32 (ldr_R0_x0, contents + rel->r_offset);
 	  bfd_putl32 (0xd53bd041, contents + rel[1].r_offset);
 	  bfd_putl32 (add_R0_R0_R1, contents + rel[1].r_offset + 4);
@@ -7055,6 +7101,8 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 	 Where R is x for lp64 mode, and w for ilp32 mode.  */
       if (local_exec)
 	{
+	  TLS_RELAX_NEED (1);
+	  TLS_RELAX_SPAN (rel->r_offset, 8);
 	  BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
 	  BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
 	  /* No need of CALL26 relocation for tls_get_addr.  */
@@ -7085,6 +7133,8 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
 	 Where R is x for lp64 mode, and w for ilp32 mode.  */
       if (local_exec)
 	{
+	  TLS_RELAX_NEED (1);
+	  TLS_RELAX_SPAN (rel->r_offset, 8);
 	  BFD_ASSERT (rel->r_offset + 4 == rel[1].r_offset);
 	  BFD_ASSERT (ELFNN_R_TYPE (rel[1].r_info) == AARCH64_R (CALL26));
 	  /* No need of CALL26 relocation for tls_get_addr.  */
@@ -7100,6 +7150,8 @@ elfNN_aarch64_tls_relax (struct elf_aarch64_link_hash_table *globals,
       return bfd_reloc_continue;
     }
 
+#undef TLS_RELAX_SPAN
+#undef TLS_RELAX_NEED
   return bfd_reloc_ok;
 }
 
@@ -7277,7 +7329,10 @@ elfNN_aarch64_relocate_section (struct bfd_link_info *info,
 	  BFD_ASSERT (howto != NULL);
 	  r_type = howto->type;
 	  r = elfNN_aarch64_tls_relax (globals, input_bfd, input_section,
-				       contents, rel, h, info);
+				       contents, rel, h, info, relend);
+	  /* Only a bad object, already reported, gives this.  */
+	  if (r == bfd_reloc_outofrange)
+	    return false;
 	  unresolved_reloc = 0;
 	}
       else
diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp
index fb5dbccaef..4c4945498f 100644
--- a/ld/testsuite/ld-aarch64/aarch64-elf.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp
@@ -337,6 +337,8 @@ run_dump_test_lp64 "reloc-offset-bounds-r"
 run_dump_test_lp64 "reloc-offset-fit"
 run_dump_test_lp64 "reloc-offset-marker"
 run_dump_test_lp64 "reloc-tls-dtprel"
+run_dump_test_lp64 "reloc-tls-span"
+run_dump_test_lp64 "reloc-tls-seq"
 
 # ifunc tests
 run_dump_test "ifunc-1"
diff --git a/ld/testsuite/ld-aarch64/reloc-tls-seq.d b/ld/testsuite/ld-aarch64/reloc-tls-seq.d
new file mode 100644
index 0000000000..713941d5b7
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-tls-seq.d
@@ -0,0 +1,4 @@
+#name: AArch64 truncated TLS relaxation sequence is rejected
+#source: reloc-tls-seq.s
+#ld:
+#error: .*truncated TLS relaxation sequence
diff --git a/ld/testsuite/ld-aarch64/reloc-tls-seq.s b/ld/testsuite/ld-aarch64/reloc-tls-seq.s
new file mode 100644
index 0000000000..6d98b05df0
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-tls-seq.s
@@ -0,0 +1,13 @@
+	.text
+	.globl _start
+_start:
+	nop
+	nop
+	nop
+	/* A GD sequence relocation with no following CALL26 relocation:
+	   the relaxation would read past the relocation array.  */
+	.reloc 0, R_AARCH64_TLSGD_ADD_LO12_NC, tv
+	.section .tbss,"awT",%nobits
+	.align 2
+tv:
+	.zero 4
diff --git a/ld/testsuite/ld-aarch64/reloc-tls-span.d b/ld/testsuite/ld-aarch64/reloc-tls-span.d
new file mode 100644
index 0000000000..0f569db138
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-tls-span.d
@@ -0,0 +1,4 @@
+#name: AArch64 TLS relaxation span outside the section is rejected
+#source: reloc-tls-span.s
+#ld:
+#error: .*bad reloc offset \(0 \+ 0xc > 0x8\) for R_AARCH64_TLSGD_ADD_LO12_NC in section `\.text'
diff --git a/ld/testsuite/ld-aarch64/reloc-tls-span.s b/ld/testsuite/ld-aarch64/reloc-tls-span.s
new file mode 100644
index 0000000000..81801ea29f
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/reloc-tls-span.s
@@ -0,0 +1,13 @@
+	.text
+	.globl _start
+_start:
+	nop
+	nop
+	/* GD->LE relaxation rewrites a three-instruction (12-octet)
+	   sequence from offset 0, past the end of this 8-octet section.  */
+	.reloc 0, R_AARCH64_TLSGD_ADD_LO12_NC, tv
+	.reloc 4, R_AARCH64_CALL26, __tls_get_addr
+	.section .tbss,"awT",%nobits
+	.align 2
+tv:
+	.zero 4
-- 
2.55.0




More information about the Binutils mailing list