[PATCH v3 5/8] rx: check relaxation delete range, propagate failures

Harshit Kumar harshit7kr@gmail.com
Sat Nov 7 07:51:00 GMT 2026


Validate deletion range in elf32_rx_relax_delete_bytes using
_bfd_elf_relax_delete_range_ok and drop redundant BFD_ASSERT.  Wrap
SNIPNR and SNIP macros in do-while(0) blocks.  Propagate deletion return
status in elf32_rx_relax_section and branch to error_return on error.
Add DejaGnu regression test ld-rx/rx-relax-underflow.exp.

Note that crafted inputs with out-of-range relocation offsets can
still fault during pre-deletion opcode inspection or rewriting before
the deletion helper is reached; those pre-deletion bounds issues will be
addressed separately.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
v3:
- Document intentional goto error_return in SNIP/SNIPNR macros.
- Convert rx-relax-underflow.exp to use ld_relax_underflow_test.

 bfd/elf32-rx.c                            | 38 +++++++++---
 ld/testsuite/ld-rx/rx-relax-underflow.exp | 71 +++++++++++++++++++++++
 2 files changed, 100 insertions(+), 9 deletions(-)
 create mode 100644 ld/testsuite/ld-rx/rx-relax-underflow.exp

diff --git a/bfd/elf32-rx.c b/bfd/elf32-rx.c
index c3c4bd90..03abc04e 100644
--- a/bfd/elf32-rx.c
+++ b/bfd/elf32-rx.c
@@ -1626,7 +1626,8 @@ elf32_rx_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
   if (alignment_rel)
     toaddr = alignment_rel->r_offset;
 
-  BFD_ASSERT (toaddr > addr);
+  if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, toaddr))
+    return false;
 
   /* Actually delete the bytes.  */
   memmove (contents + addr, contents + addr + count,
@@ -2230,8 +2231,12 @@ elf32_rx_relax_section (bfd *abfd,
 	  nbytes /= alignment;
 	  nbytes *= alignment;
 
-	  elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
-				       erel->r_offset == sec->size, internal_relocs);
+	  if (! elf32_rx_relax_delete_bytes (abfd, sec,
+					     erel->r_offset - nbytes,
+					     nbytes, next_alignment,
+					     erel->r_offset == sec->size,
+					     internal_relocs))
+	    goto error_return;
 	  *again = true;
 
 	  continue;
@@ -2269,11 +2274,25 @@ elf32_rx_relax_section (bfd *abfd,
       pcrel = symval - pc + srel->r_addend; \
       nrelocs --;
 
-#define SNIPNR(offset, nbytes) \
-      elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
-#define SNIP(offset, nbytes, newtype) \
-	SNIPNR (offset, nbytes);						\
-	srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
+      /* Delete bytes within an instruction pattern; on range check
+	 failure, abort relaxation and branch to error_return.  */
+#define SNIPNR(offset, nbytes)						\
+  do									\
+    {									\
+      if (! elf32_rx_relax_delete_bytes (abfd, sec,			\
+					 (insn - contents) + offset,	\
+					 nbytes, next_alignment, 0,	\
+					 internal_relocs))		\
+	goto error_return;						\
+    }									\
+  while (0)
+#define SNIP(offset, nbytes, newtype)					\
+  do									\
+    {									\
+      SNIPNR (offset, nbytes);						\
+      srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype); \
+    }									\
+  while (0)
 
       /* The order of these bit tests must match the order that the
 	 relocs appear in.  Since we sorted those by offset, we can
@@ -3084,7 +3103,8 @@ elf32_rx_relax_section (bfd *abfd,
     {
       bool ignored;
 
-      elf32_rx_relax_section (abfd, sec, link_info, &ignored, true);
+      if (! elf32_rx_relax_section (abfd, sec, link_info, &ignored, true))
+	goto error_return;
     }
 
   return true;
diff --git a/ld/testsuite/ld-rx/rx-relax-underflow.exp b/ld/testsuite/ld-rx/rx-relax-underflow.exp
new file mode 100644
index 00000000..d106fadc
--- /dev/null
+++ b/ld/testsuite/ld-rx/rx-relax-underflow.exp
@@ -0,0 +1,71 @@
+# Expect script for RX ELF linker relaxation range tests.
+# Copyright (C) 2026 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+# Valid control case:
+# Encodes ELF32 RX object with .text (16 bytes: 8B code + 8B padding),
+# .rela.text with two R_RX_RH_RELAX relocations (alignment at offset 0 and
+# alignment end at offset 8), .symtab, .strtab, and .shstrtab.
+# Alignment relaxation calls elf32_rx_relax_delete_bytes(addr=0, count=8)
+# with toaddr=16.
+# Range check validates offs <= limit, count <= limit - offs,
+# and limit <= sec->size.
+# .text shrinks from 16 bytes (0x10) to 8 bytes.
+set ctrl_valid_hex {
+    7f454c460101010000000000000000000100ad00010000000000000000000000
+    c800000000000000340000000000280006000500000000000000000000000000
+    00000000000000002d01000003000010080000002d0100000000002000000000
+    0000000000000000000000000100000000000000000000000000010008000000
+    000000000000000012000100004c616c69676e005f737461727400002e746578
+    74002e72656c612e74657874002e73796d746162002e737472746162002e7368
+    7374727461620000000000000000000000000000000000000000000000000000
+    0000000000000000000000000000000001000000010000000600000000000000
+    3400000010000000000000000000000004000000000000000700000004000000
+    400000000000000044000000180000000300000001000000040000000c000000
+    120000000200000000000000000000005c000000300000000400000002000000
+    04000000100000001a0000000300000000000000000000008c0000000f000000
+    0000000000000000010000000000000022000000030000000000000000000000
+    9b0000002c00000000000000000000000100000000000000
+}
+
+# Malformed test case:
+# Truncates .text sh_size to 4 bytes while retaining R_RX_RH_RELAX
+# alignment relocations at offsets 0 and 8.
+# With no subsequent alignment relocation, next_alignment is NULL,
+# so toaddr defaults to sec->size (4).
+# Relaxation attempts to delete 8 bytes at offs=0 with limit=4.
+# Since count (8) > limit - offs (4), the range check rejects the
+# deletion.
+set underflow_hex {
+    7f454c460101010000000000000000000100ad00010000000000000000000000
+    bc0000000000000034000000000028000600050000000000000000002d010000
+    03000010080000002d0100000000002000000000000000000000000000000000
+    0100000000000000000000000000010008000000000000000000000012000100
+    004c616c69676e005f737461727400002e74657874002e72656c612e74657874
+    002e73796d746162002e737472746162002e7368737472746162000000000000
+    0000000000000000000000000000000000000000000000000000000000000000
+    0000000001000000010000000600000000000000340000000400000000000000
+    0000000004000000000000000700000004000000400000000000000038000000
+    180000000300000001000000040000000c000000120000000200000000000000
+    000000005000000030000000040000000200000004000000100000001a000000
+    030000000000000000000000800000000f000000000000000000000001000000
+    00000000220000000300000000000000000000008f0000002c00000000000000
+    000000000100000000000000
+}
+
+ld_relax_underflow_test "elf32rx" "-m elf32rx --relax -e _start" "RX" \
+    $ctrl_valid_hex "8" $underflow_hex
-- 
2.55.0




More information about the Binutils mailing list