ld: refuse the link when no stub file was created
Prevents linker crashes by refusing links when stub files are unexpectedly NULL.
This commit fixes a null pointer dereference in the linker’s AVR and M68HC11 emulation hooks by adding checks for a valid stub_file. Previously, when a generic ELF output format was selected via OUTPUT_FORMAT, no stub file was created, leading to crashes in emulation-specific code that expected one. The linker now correctly refuses the link in such cases.
In Details
The ldelf_after_open_output function creates stub_file only if the output backend requires it. For generic ELF outputs selected via OUTPUT_FORMAT, want_stub_bfd is false, resulting in stub_file being NULL. The AVR and M68HC11 emulation hooks (avr_elf_after_open_output, m68hc11elf_after_open_output) would then dereference stub_file->the_bfd, causing a crash. This patch ensures ldelf_after_open_output is called first and explicitly checks for stub_file == NULL, refusing the link if it's not created.
- stub file
- A temporary file or data structure used by the linker during the linking process, particularly for handling dynamic linking or specific emulation requirements.
- OUTPUT_FORMAT
- A linker script command that specifies the output file format.
- AVR
- A family of microcontrollers from Atmel (now Microchip Technology), often targeted by embedded toolchains.
- M68HC11
- A specific microcontroller from Motorola (now Freescale/NXP), popular in embedded systems.
- NULL pointer dereference
- An error that occurs when a program tries to access memory through a pointer that is NULL (points to nothing), typically causing a crash.