[PATCH v3 2/8] bfd: check relaxation delete ranges in simple ELF backends
Harshit Kumar
harshit7kr@gmail.com
Sat Nov 7 07:50:57 GMT 2026
Validate relaxation deletion bounds in 13 ELF backends using
_bfd_elf_relax_delete_range_ok before memmove: CR16, CRX, FT32,
H8/300, IP2K, M10200, M10300, M32C, MIPS, MSP430, PRU, SH, and V850.
Add shared relaxation test helpers to ld/testsuite/lib/ld-lib.exp
(ld_write_hex_object, ld_relax_run_capture, ld_test_relax_valid,
ld_test_relax_underflow, and ld_relax_underflow_test), and add
DejaGnu regression tests for CRX and H8/300.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
v3:
- Drop redundant (size_t) cast in elf32-v850.c.
- Add ld_relax_underflow_test shared helper in ld-lib.exp.
- Convert crx-relax-underflow.exp and h8300-relax-underflow.exp to use
ld_relax_underflow_test.
- Update H8/300 test case labels to offs/limit.
bfd/elf-m10200.c | 3 +
bfd/elf-m10300.c | 3 +
bfd/elf32-cr16.c | 3 +
bfd/elf32-crx.c | 3 +
bfd/elf32-ft32.c | 3 +
bfd/elf32-h8300.c | 3 +
bfd/elf32-ip2k.c | 3 +
bfd/elf32-m32c.c | 3 +
bfd/elf32-msp430.c | 3 +
bfd/elf32-pru.c | 3 +
bfd/elf32-sh.c | 3 +
bfd/elf32-v850.c | 4 +
bfd/elfxx-mips.c | 3 +
ld/testsuite/ld-crx/crx-relax-underflow.exp | 67 +++++++++++
.../ld-h8300/h8300-relax-underflow.exp | 93 ++++++++++++++++
ld/testsuite/lib/ld-lib.exp | 105 ++++++++++++++++++
16 files changed, 305 insertions(+)
create mode 100644 ld/testsuite/ld-crx/crx-relax-underflow.exp
create mode 100644 ld/testsuite/ld-h8300/h8300-relax-underflow.exp
diff --git a/bfd/elf-m10200.c b/bfd/elf-m10200.c
index 2b2cc3a4..111f85cf 100644
--- a/bfd/elf-m10200.c
+++ b/bfd/elf-m10200.c
@@ -484,6 +484,9 @@ mn10200_elf_relax_delete_bytes (bfd *abfd, asection *sec,
irel = elf_section_data (sec)->relocs;
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));
diff --git a/bfd/elf-m10300.c b/bfd/elf-m10300.c
index 808fd9ac..66570664 100644
--- a/bfd/elf-m10300.c
+++ b/bfd/elf-m10300.c
@@ -2434,6 +2434,9 @@ mn10300_elf_relax_delete_bytes (bfd *abfd,
}
}
+ 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));
diff --git a/bfd/elf32-cr16.c b/bfd/elf32-cr16.c
index a8a5112d..43ccd3f3 100644
--- a/bfd/elf32-cr16.c
+++ b/bfd/elf32-cr16.c
@@ -1230,6 +1230,9 @@ elf32_cr16_relax_delete_bytes (struct bfd_link_info *link_info, bfd *abfd,
irel = elf_section_data (sec)->relocs;
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));
diff --git a/bfd/elf32-crx.c b/bfd/elf32-crx.c
index ca741359..90bee3a0 100644
--- a/bfd/elf32-crx.c
+++ b/bfd/elf32-crx.c
@@ -609,6 +609,9 @@ elf32_crx_relax_delete_bytes (struct bfd_link_info *link_info, bfd *abfd,
irel = elf_section_data (sec)->relocs;
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));
diff --git a/bfd/elf32-ft32.c b/bfd/elf32-ft32.c
index 3e4e6f39..7a7003a4 100644
--- a/bfd/elf32-ft32.c
+++ b/bfd/elf32-ft32.c
@@ -787,6 +787,9 @@ elf32_ft32_relax_delete_bytes (struct bfd_link_info *link_info, bfd * abfd,
irel = elf_section_data (sec)->relocs;
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));
diff --git a/bfd/elf32-h8300.c b/bfd/elf32-h8300.c
index abafaa53..3d7ad509 100644
--- a/bfd/elf32-h8300.c
+++ b/bfd/elf32-h8300.c
@@ -1502,6 +1502,9 @@ elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count)
irel = elf_section_data (sec)->relocs;
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));
diff --git a/bfd/elf32-ip2k.c b/bfd/elf32-ip2k.c
index 40704f86..5173ac2c 100644
--- a/bfd/elf32-ip2k.c
+++ b/bfd/elf32-ip2k.c
@@ -789,6 +789,9 @@ ip2k_elf_relax_delete_bytes (bfd *abfd,
bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
bfd_vma endaddr = sec->size;
+ if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, endaddr))
+ return false;
+
/* Actually delete the bytes. */
memmove (contents + addr, contents + addr + count,
endaddr - addr - count);
diff --git a/bfd/elf32-m32c.c b/bfd/elf32-m32c.c
index 34fb8d1b..fd42de10 100644
--- a/bfd/elf32-m32c.c
+++ b/bfd/elf32-m32c.c
@@ -1978,6 +1978,9 @@ m32c_elf_relax_delete_bytes (bfd *abfd,
irel = elf_section_data (sec)->relocs;
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));
sec->size -= count;
diff --git a/bfd/elf32-msp430.c b/bfd/elf32-msp430.c
index 6948fd3a..bd3b314d 100644
--- a/bfd/elf32-msp430.c
+++ b/bfd/elf32-msp430.c
@@ -1778,6 +1778,9 @@ msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
irel = elf_section_data (sec)->relocs;
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));
diff --git a/bfd/elf32-pru.c b/bfd/elf32-pru.c
index 26bf2e17..434278f1 100644
--- a/bfd/elf32-pru.c
+++ b/bfd/elf32-pru.c
@@ -1140,6 +1140,9 @@ pru_elf_relax_delete_bytes (bfd *abfd,
irel = elf_section_data (sec)->relocs;
irelend = irel + sec->reloc_count;
+ if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, toaddr))
+ return false;
+
/* Actually delete the bytes. */
if (toaddr - addr - count > 0)
memmove (contents + addr, contents + addr + count,
diff --git a/bfd/elf32-sh.c b/bfd/elf32-sh.c
index 684613f4..1a0c0e06 100644
--- a/bfd/elf32-sh.c
+++ b/bfd/elf32-sh.c
@@ -871,6 +871,9 @@ sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma 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,
(size_t) (toaddr - addr - count));
diff --git a/bfd/elf32-v850.c b/bfd/elf32-v850.c
index fe994877..9484f712 100644
--- a/bfd/elf32-v850.c
+++ b/bfd/elf32-v850.c
@@ -3225,6 +3225,10 @@ v850_elf_relax_delete_bytes (bfd *abfd,
fprintf (stderr, "relax_delete: contents: sec: %s %p .. %p %x\n",
sec->name, addr, toaddr, count );
#endif
+
+ if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, toaddr))
+ return false;
+
memmove (contents + addr, contents + addr + count,
toaddr - addr - count);
memset (contents + toaddr-count, 0, count);
diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index 4e8ffa5b..5737bbae 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -13698,6 +13698,9 @@ mips_elf_relax_delete_bytes (bfd *abfd,
irel = elf_section_data (sec)->relocs;
irelend = irel + sec->reloc_count;
+ if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, sec->size))
+ return false;
+
/* Actually delete the bytes. */
memmove (contents + addr, contents + addr + count,
(size_t) (sec->size - addr - count));
diff --git a/ld/testsuite/ld-crx/crx-relax-underflow.exp b/ld/testsuite/ld-crx/crx-relax-underflow.exp
new file mode 100644
index 00000000..0df8ae46
--- /dev/null
+++ b/ld/testsuite/ld-crx/crx-relax-underflow.exp
@@ -0,0 +1,67 @@
+# Expect script for CRX 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 CRX object with .text (8 bytes: 'nop' 2B,
+# 'br _start' 4B, 'nop' 2B), .rela.text (1 relocation R_CRX_REL32 at
+# offset 4 referencing Lbranch), .symtab, .strtab, and .shstrtab.
+# Relaxation converts 4B branch to 2B branch, calling
+# elf32_crx_relax_delete_bytes with addr=6, count=2, toaddr=8.
+# Range check validates offs <= limit, count <= limit - offs,
+# and limit <= sec->size.
+# .text shrinks from 8 bytes to 6 bytes.
+set ctrl_valid_hex {
+ 7f454c4601010100000000000000000001007200010000000000000000000000
+ b400000000000000340000000000280006000500000000007031000004000000
+ 0601000000000000000000000000000000000000000000000100000004000000
+ 000000000000010009000000000000000000000012000100004c6272616e6368
+ 005f737461727400002e74657874002e72656c612e74657874002e73796d7461
+ 62002e737472746162002e736873747274616200000000000000000000000000
+ 0000000000000000000000000000000000000000000000000000000001000000
+ 0100000006000000000000003400000008000000000000000000000004000000
+ 00000000070000000400000040000000000000003c0000000c00000003000000
+ 01000000040000000c0000001200000002000000000000000000000048000000
+ 30000000040000000200000004000000100000001a0000000300000000000000
+ 0000000078000000100000000000000000000000010000000000000022000000
+ 030000000000000000000000880000002c000000000000000000000001000000
+ 00000000
+}
+
+# Malformed test case:
+# Same object except .text sh_size is truncated to 6 bytes.
+# Relaxation attempts to delete 2 bytes at offs=6 with limit=6.
+# Since count (2) > limit - offs (0), the range check rejects the link.
+set underflow_hex {
+ 7f454c4601010100000000000000000001007200010000000000000000000000
+ b400000000000000340000000000280006000500000000007031000004000000
+ 0601000000000000000000000000000000000000000000000100000004000000
+ 000000000000010009000000000000000000000012000100004c6272616e6368
+ 005f737461727400002e74657874002e72656c612e74657874002e73796d7461
+ 62002e737472746162002e736873747274616200000000000000000000000000
+ 0000000000000000000000000000000000000000000000000000000001000000
+ 0100000006000000000000003400000006000000000000000000000004000000
+ 00000000070000000400000040000000000000003c0000000c00000003000000
+ 01000000040000000c0000001200000002000000000000000000000048000000
+ 30000000040000000200000004000000100000001a0000000300000000000000
+ 0000000078000000100000000000000000000000010000000000000022000000
+ 030000000000000000000000880000002c000000000000000000000001000000
+ 00000000
+}
+
+ld_relax_underflow_test "elf32crx" "-m elf32crx -e _start" "CRX" \
+ $ctrl_valid_hex "6" $underflow_hex
diff --git a/ld/testsuite/ld-h8300/h8300-relax-underflow.exp b/ld/testsuite/ld-h8300/h8300-relax-underflow.exp
new file mode 100644
index 00000000..f887ce60
--- /dev/null
+++ b/ld/testsuite/ld-h8300/h8300-relax-underflow.exp
@@ -0,0 +1,93 @@
+# Expect script for H8300 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 H8/300 object with .text (16 bytes), .rela.text with 1
+# relocation R_H8_DIR24R8 (type 62) at offset 4, referencing foo
+# (offset 0), preceded by opcode 0x5a (jmp24) at offset 3, .symtab,
+# .strtab, and .shstrtab.
+# Relaxation converts jmp24 to bra8 (R_H8_PCREL8), calling
+# elf32_h8_relax_delete_bytes(addr=5, count=2) with toaddr=16.
+# Range check validates offs <= limit, count <= limit - offs,
+# and limit <= sec->size.
+# .text shrinks from 16 bytes (0x10) to 14 bytes (0x0e).
+set ctrl_valid_hex {
+ 7f454c460102010000000000000000000001002e000000010000000000000000
+ 000000a4000000000034000000000028000600050000005a0000000000000000
+ 00000000000000040000013e0000000000000000000000000000000000000000
+ 0000000100000000000000000000000100666f6f00002e74657874002e72656c
+ 612e74657874002e73796d746162002e737472746162002e7368737472746162
+ 0000000000000000000000000000000000000000000000000000000000000000
+ 0000000000000000000000000000000100000001000000060000000000000034
+ 0000001000000000000000000000000400000000000000070000000400000000
+ 00000000000000440000000c0000000300000001000000040000000c00000012
+ 0000000200000000000000000000005000000020000000040000000200000004
+ 000000100000001a000000030000000000000000000000700000000500000000
+ 0000000000000001000000000000002200000003000000000000000000000075
+ 0000002c00000000000000000000000100000000
+}
+
+# Malformed test case 1 (offs > limit):
+# .text is 8 bytes with R_H8_DIR24R8 at offset 8 preceded by opcode
+# 0x5a at offset 7.
+# Relaxation calls elf32_h8_relax_delete_bytes(addr=9, count=2)
+# with toaddr=8.
+# Since offs (9) > limit (8), the range check rejects the deletion.
+set underflow_addr_hex {
+ 7f454c460102010000000000000000000001002e000000010000000000000000
+ 0000009c00000000003400000000002800060005000000000000005a00000008
+ 0000013e00000000000000000000000000000000000000000000000100000000
+ 000000000000000100666f6f00002e74657874002e72656c612e74657874002e
+ 73796d746162002e737472746162002e73687374727461620000000000000000
+ 0000000000000000000000000000000000000000000000000000000000000000
+ 0000000000000001000000010000000600000000000000340000000800000000
+ 000000000000000400000000000000070000000400000000000000000000003c
+ 0000000c0000000300000001000000040000000c000000120000000200000000
+ 000000000000004800000020000000040000000200000004000000100000001a
+ 0000000300000000000000000000006800000005000000000000000000000001
+ 00000000000000220000000300000000000000000000006d0000002c00000000
+ 000000000000000100000000
+}
+
+# Malformed test case 2 (count > limit - offs):
+# .text is 8 bytes with R_H8_DIR24R8 at offset 6 preceded by opcode
+# 0x5a at offset 5.
+# Relaxation calls elf32_h8_relax_delete_bytes(addr=7, count=2)
+# with toaddr=8.
+# Since count (2) > limit - offs (8 - 7 = 1), the range check
+# rejects the deletion.
+set underflow_count_hex {
+ 7f454c460102010000000000000000000001002e000000010000000000000000
+ 0000009c0000000000340000000000280006000500000000005a000000000006
+ 0000013e00000000000000000000000000000000000000000000000100000000
+ 000000000000000100666f6f00002e74657874002e72656c612e74657874002e
+ 73796d746162002e737472746162002e73687374727461620000000000000000
+ 0000000000000000000000000000000000000000000000000000000000000000
+ 0000000000000001000000010000000600000000000000340000000800000000
+ 000000000000000400000000000000070000000400000000000000000000003c
+ 0000000c0000000300000001000000040000000c000000120000000200000000
+ 000000000000004800000020000000040000000200000004000000100000001a
+ 0000000300000000000000000000006800000005000000000000000000000001
+ 00000000000000220000000300000000000000000000006d0000002c00000000
+ 000000000000000100000000
+}
+
+ld_relax_underflow_test "h8300elf" "-m h8300elf --relax -e _start" "H8300" \
+ $ctrl_valid_hex "e" \
+ "offs > limit" $underflow_addr_hex \
+ "count > limit - offs" $underflow_count_hex
diff --git a/ld/testsuite/lib/ld-lib.exp b/ld/testsuite/lib/ld-lib.exp
index 25722d86..c5002e46 100644
--- a/ld/testsuite/lib/ld-lib.exp
+++ b/ld/testsuite/lib/ld-lib.exp
@@ -1779,3 +1779,108 @@ proc skip_sframe_tests { } {
return 1
}
+
+# Write binary object file from a hex string representation.
+proc ld_write_hex_object { filename hexdata } {
+ regsub -all {\s+} $hexdata "" compact
+ set f [open $filename w]
+ fconfigure $f -translation binary
+ puts -nonewline $f [binary format H* $compact]
+ close $f
+}
+
+# Run ld with the given options, input object, and output file.
+# Returns a list containing the exit status and combined output.
+proc ld_relax_run_capture { ld opts obj outfile } {
+ set cmd "$ld $opts -o $outfile $obj 2>&1; echo LD_RELAX_EXIT:\$?"
+ set out [lindex [remote_exec host [concat sh -c [list $cmd]]] 1]
+ set status -1
+ if { [regexp {LD_RELAX_EXIT:(\d+)} $out -> status] } {
+ regsub {LD_RELAX_EXIT:\d+\s*$} $out "" out
+ }
+ return [list $status $out]
+}
+
+# Test that linking a valid object with relaxation succeeds (status 0)
+# and shrinks the .text section to the expected size.
+proc ld_test_relax_valid { ld opts obj bin testname exp_size } {
+ global objdump
+ file delete -force $bin
+ set result [ld_relax_run_capture $ld $opts $obj $bin]
+ set status [lindex $result 0]
+ set out [lindex $result 1]
+ if { $status != 0 } {
+ fail "$testname: expected status 0: $out"
+ } else {
+ set od_cmd [list "$objdump -h $bin"]
+ set od_out [lindex [remote_exec host [concat sh -c $od_cmd]] 1]
+ if { [regexp "\\.text\\s+0*${exp_size}\\s" $od_out] } {
+ pass "$testname: linked and shrunk cleanly"
+ } else {
+ fail "$testname: relaxation did not shrink section: $od_out"
+ }
+ }
+ file delete -force $bin
+ file delete -force $obj
+}
+
+# Test that linking a malformed object with relaxation is rejected cleanly
+# with exit status exactly 1 and a "bad relaxation" diagnostic.
+proc ld_test_relax_underflow { ld opts obj bin testname } {
+ file delete -force $bin
+ set result [ld_relax_run_capture $ld $opts $obj $bin]
+ set status [lindex $result 0]
+ set out [lindex $result 1]
+ if { $status == 1 && [string match "*bad relaxation*" $out] } {
+ pass "$testname: rejected cleanly"
+ } else {
+ fail "$testname: expected rejection with status 1: $out"
+ }
+ file delete -force $bin
+ file delete -force $obj
+}
+
+# Run relaxation tests for EMUL: one valid control case (expecting .text
+# shrinkage to EXP_SIZE) and one or more malformed underflow cases
+# (expecting rejection with exit status 1 and "bad relaxation" diagnostic).
+# ARGS is either a single UNDERFLOW_HEX, or alternating SUFFIX HEX pairs.
+proc ld_relax_underflow_test { emul opts prefix ctrl_hex exp_size args } {
+ global ld
+
+ if { [is_remote host] } {
+ return
+ }
+
+ if { ![ld_supports_emul $emul] } {
+ return
+ }
+
+ set tmpdir tmpdir
+ set tag [string tolower $prefix]
+
+ # Valid control case.
+ set ctrl_obj "$tmpdir/ctrl-$tag-valid.o"
+ set ctrl_bin "$tmpdir/ctrl-$tag-valid"
+ ld_write_hex_object $ctrl_obj $ctrl_hex
+ ld_test_relax_valid $ld $opts $ctrl_obj $ctrl_bin \
+ "$prefix relax valid relaxation" $exp_size
+
+ # Malformed underflow cases.
+ if { [llength $args] == 1 } {
+ set hex [lindex $args 0]
+ set title "$prefix relax underflow rejection"
+ set u_obj "$tmpdir/oob-$tag-underflow.o"
+ set u_bin "$tmpdir/oob-$tag-underflow"
+ ld_write_hex_object $u_obj $hex
+ ld_test_relax_underflow $ld $opts $u_obj $u_bin $title
+ } else {
+ foreach {label hex} $args {
+ set title "$prefix relax underflow rejection ($label)"
+ regsub -all {[^a-zA-Z0-9_]} $label "-" safe_label
+ set u_obj "$tmpdir/oob-$tag-underflow-$safe_label.o"
+ set u_bin "$tmpdir/oob-$tag-underflow-$safe_label"
+ ld_write_hex_object $u_obj $hex
+ ld_test_relax_underflow $ld $opts $u_obj $u_bin $title
+ }
+ }
+}
--
2.55.0
More information about the Binutils
mailing list