[PATCH v3 7/8] rl78: check relaxation delete range, propagate failures

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


Validate deletion range in elf32_rl78_relax_delete_bytes using
_bfd_elf_relax_delete_range_ok.  Wrap SNIPNR and SNIP macros in
do-while(0) blocks.  Propagate deletion return status in
rl78_elf_relax_section and branch to error_return on failure.
Add DejaGnu regression test ld-rl78/rl78-relax-underflow.exp.

Note that crafted inputs with out-of-range relocation offsets can
still fault during pre-deletion opcode rewriting in
rl78_elf_relax_section 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 rl78-relax-underflow.exp to use ld_relax_underflow_test.

 bfd/elf32-rl78.c                              | 31 +++++--
 ld/testsuite/ld-rl78/rl78-relax-underflow.exp | 83 +++++++++++++++++++
 2 files changed, 108 insertions(+), 6 deletions(-)
 create mode 100644 ld/testsuite/ld-rl78/rl78-relax-underflow.exp

diff --git a/bfd/elf32-rl78.c b/bfd/elf32-rl78.c
index b684b891..7e4332f1 100644
--- a/bfd/elf32-rl78.c
+++ b/bfd/elf32-rl78.c
@@ -1695,6 +1695,9 @@ elf32_rl78_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count
 
   irelend = irel + sec->reloc_count;
 
+  if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, toaddr))
+    return false;
+
   /* Actually delete the bytes.  */
   memmove (contents + addr, contents + addr + count,
 	   (size_t) (toaddr - addr - count));
@@ -2253,8 +2256,11 @@ rl78_elf_relax_section (bfd *abfd,
 	  nbytes /= alignment;
 	  nbytes *= alignment;
 
-	  elf32_rl78_relax_delete_bytes (abfd, sec, erel->r_offset - nbytes, nbytes,
-					 next_alignment, erel->r_offset == sec->size);
+	  if (! elf32_rl78_relax_delete_bytes (abfd, sec,
+					       erel->r_offset - nbytes,
+					       nbytes, next_alignment,
+					       erel->r_offset == sec->size))
+	    goto error_return;
 	  *again = true;
 
 	  continue;
@@ -2293,12 +2299,25 @@ rl78_elf_relax_section (bfd *abfd,
       pcrel = symval - pc + srel->r_addend;		\
       nrelocs --;
 
-#define SNIPNR(offset, nbytes) \
-	elf32_rl78_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
+      /* Delete bytes within an instruction pattern; on range check
+	 failure, abort relaxation and branch to error_return.  */
+#define SNIPNR(offset, nbytes)						\
+  do									\
+    {									\
+      if (! elf32_rl78_relax_delete_bytes (abfd, sec,			\
+					   (insn - contents) + offset,	\
+					   nbytes, next_alignment, 0))	\
+	goto error_return;						\
+    }									\
+  while (0)
 
 #define SNIP(offset, nbytes, newtype)					\
-	SNIPNR (offset, nbytes);					\
-	srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), 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
diff --git a/ld/testsuite/ld-rl78/rl78-relax-underflow.exp b/ld/testsuite/ld-rl78/rl78-relax-underflow.exp
new file mode 100644
index 00000000..da90fc54
--- /dev/null
+++ b/ld/testsuite/ld-rl78/rl78-relax-underflow.exp
@@ -0,0 +1,83 @@
+# Expect script for RL78 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 RL78 object with .text (6 bytes: nop,
+# br $!target opcode 0xee, nop, target: nop), .rela.text with
+# R_RL78_RH_RELAX (type 45) at offset 1 and R_RL78_DIR16S_PCREL
+# (type 10) at offset 2 referencing target (offset 5), .symtab,
+# .strtab, and .shstrtab.
+# Branch relaxation converts 3B long branch to 2B short branch
+# (0xef), calling elf32_rl78_relax_delete_bytes with addr=3,
+# count=1, toaddr=6.
+# Range check validates offs <= limit, count <= limit - offs,
+# and limit <= sec->size.
+# .text shrinks from 6 bytes to 5 bytes.
+set ctrl_valid_hex {
+    7f454c460101010000000000000000000100c500010000000000000000000000
+    140100000000000034000000000028000800070000ee00000000000000000000
+    0000000000000000000000000000000000000000000000000300010000000000
+    0000000000000000030003000000000000000000000000000300040001000000
+    0500000000000000000001000800000000000000000000001000000015000000
+    00000000000000001000010000746172676574005f5f726c37385f6162735f5f
+    005f737461727400010000002d00000011000000020000000a04000000000000
+    002e73796d746162002e737472746162002e7368737472746162002e72656c61
+    2e74657874002e64617461002e62737300000000000000000000000000000000
+    0000000000000000000000000000000000000000000000000000000020000000
+    0100000006000000000000003400000006000000000000000000000001000000
+    000000001b000000040000004000000000000000c80000001800000005000000
+    01000000040000000c000000260000000100000003000000000000003a000000
+    00000000000000000000000001000000000000002c0000000800000003000000
+    000000003a000000000000000000000000000000010000000000000001000000
+    0200000000000000000000003c00000070000000060000000500000004000000
+    1000000009000000030000000000000000000000ac0000001c00000000000000
+    00000000010000000000000011000000030000000000000000000000e0000000
+    3100000000000000000000000100000000000000
+}
+
+# Malformed test case:
+# Truncates .text sh_size to 3 bytes (0x03) in section header.
+# Instruction to relax is a 3-byte branch starting at offset 1.
+# Relaxation attempts to delete 1 byte at offs=3 with limit=3.
+# Since count (1) > limit - offs (3 - 3 = 0), the check fails
+# and branches to error_return, cleanly rejecting the link with
+# status 1.
+set underflow_hex {
+    7f454c460101010000000000000000000100c500010000000000000000000000
+    140100000000000034000000000028000800070000ee00000000000000000000
+    0000000000000000000000000000000000000000000000000300010000000000
+    0000000000000000030003000000000000000000000000000300040001000000
+    0500000000000000000001000800000000000000000000001000000015000000
+    00000000000000001000010000746172676574005f5f726c37385f6162735f5f
+    005f737461727400010000002d00000011000000020000000a04000000000000
+    002e73796d746162002e737472746162002e7368737472746162002e72656c61
+    2e74657874002e64617461002e62737300000000000000000000000000000000
+    0000000000000000000000000000000000000000000000000000000020000000
+    0100000006000000000000003400000003000000000000000000000001000000
+    000000001b000000040000004000000000000000c80000001800000005000000
+    01000000040000000c000000260000000100000003000000000000003a000000
+    00000000000000000000000001000000000000002c0000000800000003000000
+    000000003a000000000000000000000000000000010000000000000001000000
+    0200000000000000000000003c00000070000000060000000500000004000000
+    1000000009000000030000000000000000000000ac0000001c00000000000000
+    00000000010000000000000011000000030000000000000000000000e0000000
+    3100000000000000000000000100000000000000
+}
+
+ld_relax_underflow_test "elf32rl78" "-m elf32rl78 --relax -e _start" "RL78" \
+    $ctrl_valid_hex "5" $underflow_hex
-- 
2.55.0




More information about the Binutils mailing list