[binutils-gdb] ld: refuse the link when no stub file was created
Jan Beulich
jbeulich@sourceware.org
Mon Nov 9 10:13:03 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ed80917c3b8871249186d6152f52df806b010079
commit ed80917c3b8871249186d6152f52df806b010079
Author: Harshit Kumar <harshit7kr@gmail.com>
Date: Mon Nov 9 11:09:12 2026 +0100
ld: refuse the link when no stub file was created
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>
Diff:
---
ld/emultempl/avrelf.em | 13 ++-
ld/emultempl/m68hc1xelf.em | 11 ++-
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(-)
diff --git a/ld/emultempl/avrelf.em b/ld/emultempl/avrelf.em
index fa403f8fe1f..bec6b25fefa 100644
--- a/ld/emultempl/avrelf.em
+++ b/ld/emultempl/avrelf.em
@@ -117,15 +117,22 @@ avr_elf_after_open_output (void)
{
flagword flags;
- if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
+ ldelf_after_open_output ();
+
+ /* ldelf_after_open_output only creates the stub file when the output
+ backend wants one. A linker script may still select an ELF output
+ format whose backend does not, in which case there is nowhere to
+ link AVR trampolines into and we cannot proceed. Test the state we
+ are about to use rather than the output flavour, which is ELF in
+ exactly this case. */
+
+ if (stub_file == NULL)
{
fatal (_("%P: error: cannot change output format "
"whilst linking %s binaries\n"), "AVR");
return;
}
- ldelf_after_open_output ();
-
/* Now we add the stub section. */
flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
diff --git a/ld/emultempl/m68hc1xelf.em b/ld/emultempl/m68hc1xelf.em
index b3c4277103d..25d40124e09 100644
--- a/ld/emultempl/m68hc1xelf.em
+++ b/ld/emultempl/m68hc1xelf.em
@@ -142,10 +142,17 @@ m68hc11elf_after_open_output (void)
{
ldelf_after_open_output ();
+ /* ldelf_after_open_output only creates the stub file when the output
+ backend wants one. If it did not, there is nowhere to link the
+ m68hc11 stub sections into, and m68hc11elf_after_allocation
+ dereferences stub_file unconditionally. Refuse the link here, as
+ every other emulation with a stub file does, rather than warn and
+ continue into that dereference. */
+
if (stub_file == NULL)
{
- einfo (_("%X%P: changing output format whilst linking "
- "is not supported\n"));
+ fatal (_("%P: error: cannot change output format "
+ "whilst linking %s binaries\n"), "M68HC11");
return;
}
}
diff --git a/ld/testsuite/ld-x86-64/stub-file-retarget.exp b/ld/testsuite/ld-x86-64/stub-file-retarget.exp
new file mode 100644
index 00000000000..fa36acf663e
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/stub-file-retarget.exp
@@ -0,0 +1,124 @@
+# Expect script for the stub_file NULL dereference after OUTPUT_FORMAT
+# retargeting.
+#
+# 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/>.
+
+# ld/ldelf.c creates the global stub_file only when the output backend
+# wants a stub bfd. The AVR and m68hc1x emulations dereference it
+# unconditionally. A linker script can retarget the output to an ELF
+# format whose backend does not want a stub bfd, so the emulation must
+# notice that stub_file is NULL and refuse the link.
+#
+# Before the fix both emulations segfaulted on an ordinary executable
+# link: no sanitizer, no unusual build, and no malformed input. AVR
+# tested the output flavour, which is ELF in exactly the failing case,
+# and m68hc1x warned with einfo and then carried on into the
+# dereference.
+
+# The linker scripts are written to the local tmpdir, which a remote
+# host cannot see.
+if { [is_remote host] } {
+ return
+}
+
+if { ![ld_assemble $as "--64 $srcdir/$subdir/stub-file-retarget.s" \
+ tmpdir/stub-file-retarget.o] } {
+ return
+}
+
+proc write_retarget_script { path format } {
+ set fp [open $path w]
+ puts $fp "OUTPUT_FORMAT(\"$format\")"
+ puts $fp "SECTIONS"
+ puts $fp "{"
+ puts $fp " .text : { *(.text) }"
+ puts $fp "}"
+ close $fp
+}
+
+# Check whether TARGET is among the linker's supported targets.
+proc ld_supports_target { target } {
+ global ld
+ set cmd_output [remote_exec host $ld "--help"]
+ set text [lindex $cmd_output 1]
+ if { [regexp -line {supported targets: ([^\r\n]+)} $text -> targets] } {
+ return [expr {[lsearch -exact [split $targets] $target] >= 0}]
+ }
+ return 0
+}
+
+# The generic ELF backends report the ELF flavour but do not want a
+# stub bfd, which is the case under test. A build without them cannot
+# run these tests, so probe supported targets first.
+set retarget_formats {}
+foreach format { elf32-little elf64-little elf32-big elf64-big } {
+ if { [ld_supports_target $format] } {
+ lappend retarget_formats $format
+ }
+}
+
+if { [llength $retarget_formats] == 0 } {
+ unsupported "No generic ELF output format is available for the retarget tests."
+ return
+}
+
+# Each case links with -m<emul> and a script that retargets the output.
+# The link is expected to fail, because the object is not an <emul>
+# object, and it must fail with the emulation's refusal message rather
+# than a crash. binutils' run_host_cmd returns the output text only,
+# so a segfaulting linker is caught by the absence of that message: the
+# link dies before printing one.
+#
+# Note: m68hc1xelf.em is shared by the m68hc11 and m68hc12 emulations and
+# always names M68HC11 in this refusal message.
+set stub_file_cases {
+ { avr1 "cannot change output format whilst linking AVR binaries" }
+ { avr6 "cannot change output format whilst linking AVR binaries" }
+ { avrxmega7 "cannot change output format whilst linking AVR binaries" }
+ { m68hc11elf "cannot change output format whilst linking M68HC11 binaries" }
+ { m68hc12elf "cannot change output format whilst linking M68HC11 binaries" }
+}
+
+foreach format $retarget_formats {
+ set script tmpdir/stub-file-retarget-$format.t
+ write_retarget_script $script $format
+
+ foreach emul_case $stub_file_cases {
+ set emul [lindex $emul_case 0]
+ set expected [lindex $emul_case 1]
+ set test "$emul refuses a retargeted $format output"
+
+ if { ![ld_supports_emul $emul] } {
+ unsupported "$test (emulation not supported)"
+ continue
+ }
+
+ set out tmpdir/stub-file-retarget-$emul.out
+ catch { file delete -force $out }
+ set got [run_host_cmd "$ld" \
+ "-m$emul -T $script -o $out tmpdir/stub-file-retarget.o"]
+
+ if { [string match "*$expected*" $got] } {
+ pass $test
+ } else {
+ fail $test
+ verbose -log "expected the refusal: $expected"
+ verbose -log "linker output was: $got"
+ }
+ }
+}
diff --git a/ld/testsuite/ld-x86-64/stub-file-retarget.s b/ld/testsuite/ld-x86-64/stub-file-retarget.s
new file mode 100644
index 00000000000..afb61dc65e6
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/stub-file-retarget.s
@@ -0,0 +1,14 @@
+/* Trivial object used by stub-file-retarget.exp.
+
+ The object is deliberately not an AVR or m68hc11 object. The bugs
+ under test happen before the input architecture matters: the
+ emulation hook dereferences a NULL stub_file, and the link dies in
+ the hook. An x86-64 object keeps the test buildable by any
+ assembler. */
+
+ .text
+ .globl _start
+ .type _start, @function
+_start:
+ nop
+ .size _start, .-_start
More information about the Binutils-cvs
mailing list