[PATCH 1/2] ld: refuse the link when no stub file was created
Jan Beulich
jbeulich@suse.com
Fri Nov 6 13:27:34 GMT 2026
On 24.10.2026 17:22, Harshit Kumar wrote:
> ldelf_after_open_output creates the global stub_file only when the
> output backend wants a stub BFD. Generic elf*-gen backends define
> no stub support and report want_stub_bfd = 0.
>
> When a linker script uses OUTPUT_FORMAT to select a generic ELF
> flavour, the output BFD reports the ELF flavour but no stub file is
> created, leaving stub_file == NULL. Both the AVR and M68HC emulation
> hooks then dereference stub_file:
>
> - In avr_elf_after_open_output, the check tested whether the output
> flavour was ELF. Because generic ELF outputs are ELF, this check
> passed and the function dereferenced stub_file->the_bfd. Call
> ldelf_after_open_output first (which safely returns early if the
> output is non-ELF) and refuse the link when stub_file is NULL.
> Refusing here also ensures the two later unguarded stub_file uses in
> avrelf.em (in avr_elf_set_global_bfd_parameters and avr_finish) are
> never reached.
> - In m68hc11elf_after_open_output, the hook warned with einfo and
> returned, allowing the link to continue into
> m68hc11elf_after_allocation where stub_file->the_bfd was dereferenced.
> Refuse the link with fatal instead. (The file is shared with
> m68hc12elf and names M68HC11 in the diagnostic.)
>
> Reproducer:
> OUTPUT_FORMAT("elf32-little")
> SECTIONS { .text : { *(.text) } }
>
> Linking an x86-64 object with 'ld -mavr1 -T script' or 'ld -mm68hc11elf'
> crashes with SIGSEGV (exit status 139) on unpatched trees. With this
> change, the link is refused with status 1.
>
> Regression test: ld/testsuite/ld-x86-64/stub-file-retarget.exp.
>
> ld/
> * emultempl/avrelf.em (avr_elf_after_open_output): Call
> ldelf_after_open_output before testing stub_file. Refuse the link
> when stub_file is NULL.
> * emultempl/m68hc1xelf.em (m68hc11elf_after_open_output): Use fatal
> rather than einfo when stub_file is NULL.
> * testsuite/ld-x86-64/stub-file-retarget.exp: New test.
> * testsuite/ld-x86-64/stub-file-retarget.s: New test source.
>
> Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
Okay, seeing that ...
> ---
> ld/emultempl/avrelf.em | 13 +-
> ld/emultempl/m68hc1xelf.em | 11 +-
... target maintainers haven't expressed any concerns.
Jan
> ld/testsuite/ld-x86-64/stub-file-retarget.exp | 124 ++++++++++++++++++
> ld/testsuite/ld-x86-64/stub-file-retarget.s | 14 ++
> 4 files changed, 157 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
More information about the Binutils
mailing list