[RFC PATCH 00/17] bfd: check that ELF relocation fields lie inside their section
Harshit Kumar
harshit7kr@gmail.com
Mon Nov 16 18:43:16 GMT 2026
Hi Alan, Jan, all,
While fixing relocation offset bounds one backend at a time (RISC-V,
PR ld/34987, posted on 11-11), I found that many ELF backends apply
relocations at contents + r_offset without checking that the field
lies inside the section. i386 and x86-64 check this for themselves.
Before sending more per-backend patches, I'd like your view on doing
the check once, for every ELF backend.
Patch 9 checks the relocations in elf_link_read_relocs_from_section,
next to the symbol index check, so check_relocs, relaxation, gc and
relocate_section only see relocations whose field fits. The field
size comes from the howto; relocations with no dst_mask are not
checked. Patch 1 adds a hook that gives the size where the howto does
not, and a flag for MIPS's composed relocations. The rest are one
patch per target:
2-6: keep the check from rejecting valid nds32, MIPS, RX, MSP430
and MN10300 objects;
7-8: MIPS ELF64 and hppa, whose howtos the default cannot use;
10-17: AArch64, ARM, RISC-V, MIPS, RX, LoongArch, SPARC and s390
relocations whose backend accesses more than the howto says.
Patches 1-8 change nothing on their own; 9 needs them, and 10-17
need 9. 1, 5 (MSP430 has no maintainer) and 9 need a global
maintainer's ack, 4 and 14 need Nick's (RX), and the others their
target maintainers'.
An AddressSanitizer probe of every relocation type of every ELF
backend found most of what 10-17 fix. It also found 54 types on 18
other, mostly embedded, backends that access more than the check
allows, and some that read before r_offset; I have left those until
the approach is agreed, and can send the list. The check does not
replace backend checks of what lies beside the field (the rest of a
TLS sequence, ULEB128, alpha GPDISP).
Testing, on master 5678a7bfce: make check-ld with and without the
check on 47 configurations changes only the new tests; clang-built
objects for ten targets link to identical output with and without it;
each patch builds with --enable-targets=all and -Werror. The cost is
at most 16% more instructions on an ld -r of 400,000 relocations, with
no measurable difference in time.
Questions:
1. Is relocation read time the right place, or would you prefer
elf_link_input_bfd, before relocate_section only?
2. The default looks the howto up through elf_info_to_howto with
bfd's messages suppressed (as bfd_check_format_matches does), so
an unknown type is still reported once, by the backend. Is that
acceptable, or should every backend provide the hook?
3. A failure is an error, as in i386 and x86-64. Would you rather
have a warning for a release?
4. If this goes in, should the i386 and x86-64 checks go, and should
I drop the generic part of the RISC-V series?
Thanks,
Harshit
Harshit Kumar (17):
bfd: add ELF hooks for a relocation field size check
nds32: give position markers no relocation field
mips: check only the last of composed relocations
rx: give operand relocations no field
msp430: give SYM_DIFF relocations no field
mn10300: give SYM_DIFF relocations no field
mips: give the field size of ELF64 relocations
hppa: give the field size of relocations
bfd: check that relocation fields lie inside their section
aarch64: give the field size of relocations wider than their howto
arm: give the field size of relocations wider than their howto
riscv: give the field size of TLSDESC_CALL and TPREL_ADD
mips: give the field size of JALR
rx: give the field size of relocations wider than their howto
loongarch: give the field size of TLS relocations with no field
sparc: give the field size of relocations with no field
s390: give the field size of TLS relocations with no field
bfd/bfd.c | 21 ++
bfd/elf-bfd.h | 27 +++
bfd/elf-hppa.h | 25 +++
bfd/elf-m10300.c | 17 ++
bfd/elf32-arm.c | 33 +++
bfd/elf32-hppa.c | 1 +
bfd/elf32-mips.c | 2 +
bfd/elf32-msp430.c | 20 +-
bfd/elf32-nds32.c | 25 +++
bfd/elf32-rx.c | 40 ++++
bfd/elf32-s390.c | 26 +++
bfd/elf32-sparc.c | 1 +
bfd/elf64-hppa.c | 1 +
bfd/elf64-mips.c | 49 ++++-
bfd/elf64-s390.c | 23 ++
bfd/elf64-sparc.c | 2 +
bfd/elflink.c | 198 +++++++++++++++++-
bfd/elfn32-mips.c | 2 +
bfd/elfnn-aarch64.c | 30 +++
bfd/elfnn-loongarch.c | 23 ++
bfd/elfnn-riscv.c | 16 ++
bfd/elfxx-mips.c | 27 +++
bfd/elfxx-mips.h | 2 +
bfd/elfxx-sparc.c | 26 +++
bfd/elfxx-sparc.h | 2 +
bfd/elfxx-target.h | 9 +
bfd/libbfd.h | 2 +
ld/testsuite/ld-aarch64/aarch64-elf.exp | 4 +
ld/testsuite/ld-aarch64/reloc-field-dtprel.d | 3 +
ld/testsuite/ld-aarch64/reloc-field-dtprel.s | 12 ++
.../ld-aarch64/reloc-field-tlsdesc-add.d | 3 +
.../ld-aarch64/reloc-field-tlsdesc-add.s | 8 +
.../ld-aarch64/reloc-field-tlsdesc-call.d | 3 +
.../ld-aarch64/reloc-field-tlsdesc-call.s | 12 ++
ld/testsuite/ld-arm/arm-elf.exp | 2 +
ld/testsuite/ld-arm/reloc-field-bf.d | 4 +
ld/testsuite/ld-arm/reloc-field-bf.s | 11 +
ld/testsuite/ld-arm/reloc-field-tls-descseq.d | 3 +
ld/testsuite/ld-arm/reloc-field-tls-descseq.s | 8 +
.../ld-elf/reloc-field-rx-dir32-rev.d | 4 +
.../ld-elf/reloc-field-rx-dir32-rev.s | 8 +
ld/testsuite/ld-elf/reloc-offset-1.d | 8 +
ld/testsuite/ld-elf/reloc-offset-1.s | 4 +
ld/testsuite/ld-elf/reloc-offset-2.d | 8 +
ld/testsuite/ld-elf/reloc-offset-2.s | 4 +
ld/testsuite/ld-elf/reloc-offset-3.d | 8 +
ld/testsuite/ld-elf/reloc-offset-3.s | 4 +
ld/testsuite/ld-elf/reloc-offset-diff.d | 8 +
ld/testsuite/ld-elf/reloc-offset-diff.s | 6 +
.../ld-loongarch-elf/ld-loongarch-elf.exp | 1 +
.../reloc-field-tls-desc-call.d | 3 +
.../reloc-field-tls-desc-call.s | 8 +
ld/testsuite/ld-mips-elf/mips-elf.exp | 6 +
.../ld-mips-elf/reloc-field-compose-n32.d | 8 +
.../ld-mips-elf/reloc-field-compose-n64.d | 8 +
.../ld-mips-elf/reloc-field-compose.s | 6 +
ld/testsuite/ld-mips-elf/reloc-field-jalr.d | 3 +
ld/testsuite/ld-mips-elf/reloc-field-jalr.s | 8 +
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 2 +
.../ld-riscv-elf/reloc-field-tlsdesc-call.d | 4 +
.../ld-riscv-elf/reloc-field-tlsdesc-call.s | 17 ++
.../ld-riscv-elf/reloc-field-tprel-add.d | 3 +
.../ld-riscv-elf/reloc-field-tprel-add.s | 8 +
ld/testsuite/ld-s390/reloc-field-tls-gdcall.d | 3 +
ld/testsuite/ld-s390/reloc-field-tls-gdcall.s | 8 +
ld/testsuite/ld-s390/s390.exp | 1 +
ld/testsuite/ld-sparc/reloc-field-wdisp16.d | 3 +
ld/testsuite/ld-sparc/reloc-field-wdisp16.s | 8 +
ld/testsuite/ld-sparc/sparc.exp | 2 +
69 files changed, 887 insertions(+), 8 deletions(-)
create mode 100644 ld/testsuite/ld-aarch64/reloc-field-dtprel.d
create mode 100644 ld/testsuite/ld-aarch64/reloc-field-dtprel.s
create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.d
create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-add.s
create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.d
create mode 100644 ld/testsuite/ld-aarch64/reloc-field-tlsdesc-call.s
create mode 100644 ld/testsuite/ld-arm/reloc-field-bf.d
create mode 100644 ld/testsuite/ld-arm/reloc-field-bf.s
create mode 100644 ld/testsuite/ld-arm/reloc-field-tls-descseq.d
create mode 100644 ld/testsuite/ld-arm/reloc-field-tls-descseq.s
create mode 100644 ld/testsuite/ld-elf/reloc-field-rx-dir32-rev.d
create mode 100644 ld/testsuite/ld-elf/reloc-field-rx-dir32-rev.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-1.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-1.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-2.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-2.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-3.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-3.s
create mode 100644 ld/testsuite/ld-elf/reloc-offset-diff.d
create mode 100644 ld/testsuite/ld-elf/reloc-offset-diff.s
create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.d
create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-field-tls-desc-call.s
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-compose-n32.d
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-compose-n64.d
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-compose.s
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-jalr.d
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-jalr.s
create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.d
create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tlsdesc-call.s
create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.d
create mode 100644 ld/testsuite/ld-riscv-elf/reloc-field-tprel-add.s
create mode 100644 ld/testsuite/ld-s390/reloc-field-tls-gdcall.d
create mode 100644 ld/testsuite/ld-s390/reloc-field-tls-gdcall.s
create mode 100644 ld/testsuite/ld-sparc/reloc-field-wdisp16.d
create mode 100644 ld/testsuite/ld-sparc/reloc-field-wdisp16.s
base-commit: 5678a7bfceb3a6ea18bcde1ae68f7e96c43c998b
--
2.55.0
More information about the Binutils
mailing list