[PATCH v2 0/2] ld: fix retargeted-output stub-file and hash-table accesses
Harshit Kumar
harshit7kr@gmail.com
Fri Nov 6 14:28:26 GMT 2026
A linker script can retarget the output BFD with OUTPUT_FORMAT. The
AVR, m68hc1x and VxWorks emulation hooks run after that and assume the
output backend created a stub file or an ELF link hash table. The
generic elf32-gen/elf64-gen backends do neither, so the hooks either
dereference a NULL stub_file or read past a generic hash table.
Base commit: a729341631f5ddea915cebe722e4ec198ff96c38
The two patches are independent. They are sent together because they
share the same retargeting trigger and the same review area. I checked
the other emulation hooks in ld/emultempl/ and ld/*.c for the same
patterns (stub_file, elf_hash_table, bfd_target_elf_flavour) and found
no other unguarded sites.
1. ld: refuse the link when no stub file was created
AVR and m68hc1x dereference stub_file unconditionally. AVR tested
the output flavour, which is ELF in the failing case; m68hc1x warned
and then continued into the dereference. Both now refuse the link.
ldelf_after_open_output is called before the check, which safely
returns early for non-ELF outputs. Refusing here also ensures the
two later unguarded stub_file uses in avrelf.em are not reached.
`ld-x86-64/stub-file-retarget.exp` covers five emulations in four
generic ELF formats: 20/20 fail on the unpatched tree (linker crash)
and 20/20 pass on the patched tree (clean refusal message). The
test is placed in ld-x86-64/ because it uses an x86-64 relocatable
object as the foreign input to test retargeting across generic ELF
formats.
2. ld: guard the vxworks ELF link hash table read
`vxworks_after_open` reads `dynamic_sections_created` after checking
only the output flavour. A generic ELF output allocates a generic
bfd_link_hash_table, which does not contain the elf_link_hash_table
fields. The patch tests `is_elf_hash_table` first, matching the
equivalent guards in ld/ldelf.c and bfd/elflink.c.
This patch has no testsuite test: the only plain-build symptom is a
false warning whose appearance depends on an uninitialised heap byte
and the input object. With AddressSanitizer, all seven VxWorks
emulations report a heap-buffer-overflow in `vxworks_after_open`
before the fix and no sanitizer report after it.
Changes in v2:
- Patch 1: Unchanged from v1; resent for completeness as the series
is not yet applied.
- Patch 2: Dropped the explanatory comment in vxworks.em (Jan).
The commit message covers the reasoning.
Harshit Kumar (2):
ld: refuse the link when no stub file was created
ld: guard the vxworks ELF link hash table read
ld/emultempl/avrelf.em | 13 +-
ld/emultempl/m68hc1xelf.em | 11 +-
ld/emultempl/vxworks.em | 1 +
ld/testsuite/ld-x86-64/stub-file-retarget.exp | 124 ++++++++++++++++++
ld/testsuite/ld-x86-64/stub-file-retarget.s | 14 ++
5 files changed, 158 insertions(+), 5 deletions(-)
create mode 100644 ld/testsuite/ld-x86-64/stub-file-retarget.exp
create mode 100644 ld/testsuite/ld-x86-64/stub-file-retarget.s
--
2.55.0
More information about the Binutils
mailing list