[binutils-gdb] ld: do not read ELF fields from a generic link hash table

Jan Beulich jbeulich@sourceware.org
Fri Nov 6 13:16:01 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f6d98c0bc403261fafba98b233c985068cf78c05

commit f6d98c0bc403261fafba98b233c985068cf78c05
Author: Harshit Kumar <harshit7kr@gmail.com>
Date:   Fri Nov 6 14:13:40 2026 +0100

    ld: do not read ELF fields from a generic link hash table
    
    PR ld/34874
    
    ldelf_after_open_output assumes that an ELF output BFD implies an ELF
    link hash table.  A linker script can select an ELF flavour that uses
    the generic hash table, for example OUTPUT_FORMAT("elf64-nfp").  The
    hook then reads hash_table_id, which is past the end of the generic
    table (AddressSanitizer, heap-buffer-overflow, READ of size 4).
    
    Test is_elf_hash_table before reading the table, as other uses of
    link_info.hash in ld do.  compatible_format in bfd/elflink.c does the
    same before it reads the ELF fields.
    
    Extend ld-x86-64/arch-confusion.exp to cover the generic hash table,
    using elf64-nfp and, when that is not built, elf32-little.
    
    Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>

Diff:
---
 ld/ldelf.c                                |  5 +++
 ld/testsuite/ld-x86-64/arch-confusion.exp | 64 ++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/ld/ldelf.c b/ld/ldelf.c
index d3c24543d61..5f4cb2ea712 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -78,6 +78,11 @@ ldelf_after_open_output (void)
   if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
     return;
 
+  /* An ELF output does not imply an ELF link hash table: a linker
+     script can select an ELF flavour that uses a generic one.  */
+  if (!is_elf_hash_table (link_info.hash))
+    return;
+
   struct elf_link_hash_table *htab = elf_hash_table (&link_info);
   if (elf_object_id (link_info.output_bfd) != elf_hash_table_id (htab))
     return;
diff --git a/ld/testsuite/ld-x86-64/arch-confusion.exp b/ld/testsuite/ld-x86-64/arch-confusion.exp
index 7238044ce02..d390345ec3f 100644
--- a/ld/testsuite/ld-x86-64/arch-confusion.exp
+++ b/ld/testsuite/ld-x86-64/arch-confusion.exp
@@ -19,8 +19,8 @@
 
 # A linker script can retarget the output BFD away from x86 with
 # OUTPUT_FORMAT.  The x86 emulation still calls
-# bfd_elf_linker_x86_set_options.  That function must not mistake the
-# foreign link hash table for a struct elf_x86_link_hash_table.
+# bfd_elf_linker_x86_set_options, and ldelf_after_open_output runs too.
+# Neither may mistake a foreign link hash table for the one it expects.
 
 # The linker scripts below are written to the local tmpdir, which a
 # remote host cannot see.
@@ -84,6 +84,14 @@ set arch_confusion_elf_targets {
     {elf32-bfin           bfin}
 }
 
+# Generic output formats to try, as {format architecture}.  Both names
+# have to be valid, as above.  The empty architecture is left to the
+# format's own default.
+set arch_confusion_generic_targets {
+    {elf64-nfp            nfp}
+    {elf32-little         {}}
+}
+
 if { ![ld_assemble $as "--64 $srcdir/$subdir/arch-confusion.s" \
 	 tmpdir/arch-confusion.o] } {
     unsupported "$test (could not assemble the input)"
@@ -145,8 +153,60 @@ if { !$found } {
     unsupported "$test (foreign ELF output format)"
 }
 
+# Some ELF flavours use the generic link hash table rather than an ELF
+# one.  ldelf_after_open_output runs for them too, and must not read ELF
+# fields out of the generic table.  elf64-nfp enforces its machine, so
+# OUTPUT_ARCH is needed there, or ld rejects the script before the hook
+# runs.
+#
+# The result differs by format, and a clean link is accepted.  elf64-nfp
+# is a real port with its own machine and rejects the x86-64 input with
+# the architecture diagnostic.  A generic backend such as elf32-little
+# accepts any input machine, so there is no diagnostic and the link
+# simply has to produce its output.  Either way a sanitizer report from
+# the out of bounds read, or any other diagnostic, is a failure.  As
+# above, a candidate this build lacks is logged and skipped, and the test
+# is unsupported only when no candidate is available.
+set found 0
+foreach target $arch_confusion_generic_targets {
+    set format [lindex $target 0]
+    set arch [lindex $target 1]
+
+    arch_confusion_script tmpdir/arch-confusion-generic.t $format $arch
+    set output [arch_confusion_link tmpdir/arch-confusion-generic.t \
+		    tmpdir/arch-confusion.o tmpdir/arch-confusion-generic.out]
+
+    if { [regexp "target $format not found" $output]
+	 || [regexp "cannot represent machine" $output] } {
+	verbose -log "no $format target in this build, skipping it"
+	continue
+    }
+
+    set found 1
+
+    # ld removes its output file when the link fails, so an empty
+    # diagnostic together with the file present means the link really
+    # did complete.
+    if { [regexp "architecture of input file .* is incompatible with" \
+		 $output] } {
+	pass "$test (generic link hash table, output format $format)"
+    } elseif { $output eq "" \
+	       && [file exists tmpdir/arch-confusion-generic.out] } {
+	pass "$test (generic link hash table, output format $format)"
+    } else {
+	fail "$test (generic link hash table, output format $format: $output)"
+    }
+    break
+}
+
+if { !$found } {
+    unsupported "$test (generic link hash table, output format)"
+}
+
 catch { file delete -force tmpdir/arch-confusion-binary.t }
 catch { file delete -force tmpdir/arch-confusion-binary.out }
 catch { file delete -force tmpdir/arch-confusion-elf.t }
 catch { file delete -force tmpdir/arch-confusion-elf.out }
+catch { file delete -force tmpdir/arch-confusion-generic.t }
+catch { file delete -force tmpdir/arch-confusion-generic.out }
 catch { file delete -force tmpdir/arch-confusion.o }


More information about the Binutils-cvs mailing list