[PATCH v2 2/8] bfd: check relaxation delete ranges in simple ELF backends
Jan Beulich
jbeulich@suse.com
Fri Nov 6 14:04:00 GMT 2026
On 16.10.2026 14:36, Harshit Kumar wrote:
> --- a/bfd/elf32-v850.c
> +++ b/bfd/elf32-v850.c
> @@ -3225,8 +3225,12 @@ 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);
> + (size_t) (toaddr - addr - count));
What use is this addition of a cast? If the result ends up too large for
the site_t parameter or memmove(), the cast only possibly hides that from
(silences it for) sanitizers. And ...
> memset (contents + toaddr-count, 0, count);
... the subtraction here would then similarly too large for something
added to a pointer.
> --- /dev/null
> +++ b/ld/testsuite/ld-crx/crx-relax-underflow.exp
> @@ -0,0 +1,94 @@
> +# 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/>.
> +
> +if { [is_remote host] } {
> + return
> +}
> +
> +if { ![ld_supports_emul "elf32crx"] } {
> + return
> +}
> +
> +set tmpdir tmpdir
> +
> +proc crx_relax_underflow_test {} {
> + global ld
> + global tmpdir
> +
> + # 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 addr <= toaddr, count <= toaddr - addr,
> + # and toaddr <= 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
> + }
> + set ctrl_obj "$tmpdir/ctrl-crx-valid.o"
> + ld_write_hex_object $ctrl_obj $ctrl_valid_hex
> +
> + # Malformed test case:
> + # Same object except .text sh_size is truncated to 6 bytes.
> + # Relaxation attempts to delete 2 bytes at addr=6 (r_offset 4 + 2)
> + # with toaddr=6.
> + # Since count (2) > toaddr - addr (0), the range check rejects the link.
> + set underflow_hex {
> + 7f454c4601010100000000000000000001007200010000000000000000000000
> + b400000000000000340000000000280006000500000000007031000004000000
> + 0601000000000000000000000000000000000000000000000100000004000000
> + 000000000000010009000000000000000000000012000100004c6272616e6368
> + 005f737461727400002e74657874002e72656c612e74657874002e73796d7461
> + 62002e737472746162002e736873747274616200000000000000000000000000
> + 0000000000000000000000000000000000000000000000000000000001000000
> + 0100000006000000000000003400000006000000000000000000000004000000
> + 00000000070000000400000040000000000000003c0000000c00000003000000
> + 01000000040000000c0000001200000002000000000000000000000048000000
> + 30000000040000000200000004000000100000001a0000000300000000000000
> + 0000000078000000100000000000000000000000010000000000000022000000
> + 030000000000000000000000880000002c000000000000000000000001000000
> + 00000000
> + }
> + set underflow_obj "$tmpdir/oob-crx-underflow.o"
> + ld_write_hex_object $underflow_obj $underflow_hex
> +
> + set opts "-m elf32crx -e _start"
> +
> + ld_test_relax_valid $ld $opts $ctrl_obj \
> + "$tmpdir/ctrl-crx-valid" "CRX relax valid relaxation" "6"
> +
> + ld_test_relax_underflow $ld $opts $underflow_obj \
> + "$tmpdir/oob-crx-underflow" "CRX relax underflow rejection"
> +}
> +
> +crx_relax_underflow_test
Despite your adding of a few library helpers, there's still quite a bit of
redundancy in the testcases added here plus the ones added by later
patches. I wonder if this can't be further abstracted, with just the hex
blobs customized per-target.
Jan
More information about the Binutils
mailing list