[PATCH] ld: add PE/COFF and XCOFF64 aux symbol overrun tests

Harshit Kumar harshit7kr@gmail.com
Sat Nov 7 07:35:58 GMT 2026


Hi Alan,

Quick follow-up on the test-only patch I sent last month.

It adds i386 PE/COFF (ld-pe/coff-aux.exp) and 64-bit AIX
(xcoff64_aux_test) aux-symbol overrun tests for 335d76afde5. It still
applies cleanly to master.

If the existing 32-bit XCOFF tests from 335d76afde5 are sufficient and
this patch isn't needed, no worries; I just wanted to check.

Thanks,
Harshit

On Thu, 8 Oct 2026 at 17:43, Harshit Kumar <harshit7kr@gmail.com> wrote:
>
> This patch adds regression test coverage for commit 335d76afde5.
> Commit 335d76afde5 validates symbol table bounds before the linker
> walks symbols in cofflink.c and xcofflink.c. The original commit
> added regression tests for 32-bit XCOFF only.
>
> This change adds 9 new tests (6 malformed and 3 positive controls),
> bringing the xcoff-aux and coff-aux tests to 16 in total:
>
> 1. New test script ld/testsuite/ld-pe/coff-aux.exp tests i386 PE/COFF
> links when i386pe emulation is supported (for example in configurations
> with --enable-targets=i386-pe). It embeds an 86-byte minimal seed and
> a 104-byte exact-fit seed with a valid auxiliary record.
>
> 2. Procedure xcoff64_aux_test in ld/testsuite/ld-powerpc/xcoff-aux.exp
> tests AIX 64-bit target links (-b64 -e 0) using an embedded 378-byte
> seed object.
>
> Tested against unpatched baseline commit 335d76afde5^:
> - All 6 malformed tests fail on the unpatched baseline:
>   - 4 by crash or assertion:
>     - i386 PE/COFF n_numaux = 5 trips a BFD assertion failure in
>       cofflink.c followed by a heap corruption abort (SIGABRT).
>     - i386 PE/COFF n_numaux = 255 crashes with malloc invalid size abort.
>     - XCOFF64 n_numaux = 6 trips wrong auxtype warnings and an assertion
>       failure in xcofflink.c.
>     - XCOFF64 n_numaux = 255 crashes with malloc invalid size abort.
>   - 2 because ld exits 0 where the test requires status 1:
>     - i386 PE/COFF 86-byte n_numaux = 1 and 104-byte n_numaux = 2 both
>       exit 0 on unpatched ld. Patched ld cleanly rejects both with status 1.
> - All 3 new positive controls link cleanly with status 0 on both builds.
>
> On the patched build, all 9 new tests pass cleanly alongside the 7
> existing XCOFF32 tests (16 passes total).
>
> ld/
>         * testsuite/ld-pe/coff-aux.exp: New test.
>         * testsuite/ld-powerpc/xcoff-aux.exp (xcoff64_aux_test): New proc.
>         Call xcoff64_aux_test.
>
> Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
> ---
> This patch provides follow-up testsuite coverage for commit 335d76afde5
> ("bfd: validate COFF and XCOFF symbol tables before the linker walks
> them").
>
> The new i386 PE/COFF test script executes only when i386pe emulation
> is supported (e.g. configurations with --enable-targets=i386-pe).
>
>  ld/testsuite/ld-pe/coff-aux.exp       | 245 ++++++++++++++++++++++++++
>  ld/testsuite/ld-powerpc/xcoff-aux.exp |  89 ++++++++++
>  2 files changed, 334 insertions(+)
>  create mode 100644 ld/testsuite/ld-pe/coff-aux.exp
>
> diff --git a/ld/testsuite/ld-pe/coff-aux.exp b/ld/testsuite/ld-pe/coff-aux.exp
> new file mode 100644
> index 00000000000..5247860bd46
> --- /dev/null
> +++ b/ld/testsuite/ld-pe/coff-aux.exp
> @@ -0,0 +1,245 @@
> +# Expect script for i386 PE/COFF auxiliary symbol count overrun regression tests.
> +#
> +# 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; see the file COPYING3. If not,
> +# see <http://www.gnu.org/licenses/>.
> +
> +# Skip on remote host configurations as objects are generated in local tmpdir.
> +if { [is_remote host] } {
> +    return
> +}
> +
> +# Only run on targets that support i386pe emulation.
> +if { ![ld_supports_emul "i386pe"] } {
> +    return
> +}
> +
> +set tmpdir tmpdir
> +
> +# Write binary data from a hex string to destination file.
> +proc coff_aux_write_hex { hex dst } {
> +    regsub -all {\s+} $hex "" clean_hex
> +    set f [open $dst "wb"]
> +    fconfigure $f -translation binary
> +    puts -nonewline $f [binary format "H*" $clean_hex]
> +    close $f
> +}
> +
> +# Read a single unsigned byte from a file.
> +proc coff_aux_read_byte { file off } {
> +    set f [open $file "rb"]
> +    fconfigure $f -translation binary
> +    seek $f $off
> +    set b [read $f 1]
> +    close $f
> +    binary scan $b "c" val
> +    return [expr {$val & 0xff}]
> +}
> +
> +# Mutate a single byte in an object file using native Tcl.
> +proc coff_aux_mutate_byte { src dst off val } {
> +    file copy -force $src $dst
> +    set f [open $dst "r+b"]
> +    fconfigure $f -translation binary
> +    seek $f $off
> +    puts -nonewline $f [binary format "c" $val]
> +    close $f
> +}
> +
> +# Run LD over OBJ, writing OUTFILE, and return the pair {status output}.
> +proc coff_aux_run_ld_capture { ld opts obj outfile } {
> +    set cmd "$ld $opts -o $outfile $obj 2>&1; echo COFF_EXIT:\$?"
> +    set out [lindex [remote_exec host [concat sh -c [list $cmd]]] 1]
> +    set status -1
> +    if { [regexp {COFF_EXIT:(\d+)} $out -> status] } {
> +       regsub {COFF_EXIT:\d+\s*$} $out "" out
> +    }
> +    return [list $status $out]
> +}
> +
> +# Check whether output indicates a crash or abort.
> +proc coff_aux_crashed { out } {
> +    foreach pat { "*AddressSanitizer*" "*LeakSanitizer*" \
> +                 "*Segmentation fault*" "*core dumped*" "*SIGABRT*" \
> +                 "*SIGSEGV*" "*free(): invalid size*" "*malloc(): *" \
> +                 "*corrupted size vs. prev_size*" "*assertion fail*" \
> +                 "*Aborted*" } {
> +       if { [string match $pat $out] } {
> +           return 1
> +       }
> +    }
> +    return 0
> +}
> +
> +# Expect ld to reject a malformed input cleanly with status 1.
> +proc coff_aux_expect_clean_rejection { ld opts obj bin what } {
> +    file delete -force $bin
> +    set result [coff_aux_run_ld_capture $ld $opts $obj $bin]
> +    set status [lindex $result 0]
> +    set out [lindex $result 1]
> +    if { $status == 1
> +        && ![file exists $bin]
> +        && ![coff_aux_crashed $out]
> +        && [string match "*has missing aux entries*" $out] } {
> +       pass "i386 PE/COFF aux $what: rejected cleanly"
> +    } else {
> +       fail "i386 PE/COFF aux $what: expected status 1, no crash and no output file,\
> +             got status $status, output file [expr {[file exists $bin] ? \
> +             {exists} : {absent}}]: $out"
> +    }
> +    file delete -force $bin
> +}
> +
> +proc coff_aux_test {} {
> +    global ld tmpdir
> +
> +    set emul "-m i386pe"
> +
> +    # Embedded hex representation of a minimal 86-byte i386 PE/COFF seed object.
> +    # Layout:
> +    #   Header: magic 0x014c (i386), 1 section (.text), f_symptr = 64 (0x0040),
> +    #           f_nsyms = 1.
> +    #   Section: .text (size 4, file offset 60, contains ret opcode 0xc3).
> +    #   Symbol 0: 'entry' (value 0, scnum 1, type 0, class 2 C_EXT, n_numaux = 0).
> +    #             Offset 64. n_sclass at offset 80, n_numaux at offset 81.
> +    #   String table: length 4 (empty) at offset 82.
> +    set seed86_hex {
> +       4c010100000000004000000001000000000000002e7465787400000000000000
> +       00000000040000003c00000000000000000000000000000020000060c3000000
> +       656e7472790000000000000001000000020004000000
> +    }
> +    set seed86 "$tmpdir/coff-aux-seed86.o"
> +    coff_aux_write_hex $seed86_hex $seed86
> +
> +    set seed86_size [file size $seed86]
> +    if { $seed86_size != 86 } {
> +       fail "i386 PE/COFF aux: invalid seed size $seed86_size (expected 86)"
> +       return
> +    }
> +
> +    if { [coff_aux_read_byte $seed86 80] != 2 || [coff_aux_read_byte $seed86 81] != 0 } {
> +       fail "i386 PE/COFF aux: 86-byte seed layout mismatch"
> +       return
> +    }
> +
> +    # 1. Positive control: unmutated 86-byte seed must link cleanly.
> +    set ctrl86_bin "$tmpdir/coff-aux-ctrl86.o"
> +    file delete -force $ctrl86_bin
> +    set result [coff_aux_run_ld_capture $ld "$emul -r" $seed86 $ctrl86_bin]
> +    set status [lindex $result 0]
> +    set out [lindex $result 1]
> +    if { $status != 0 || [coff_aux_crashed $out] || ![file exists $ctrl86_bin] } {
> +       fail "i386 PE/COFF aux positive control: unmutated 86-byte seed did not link cleanly (status $status): $out"
> +       return
> +    }
> +    pass "i386 PE/COFF aux positive control: unmutated 86-byte seed links cleanly"
> +    file delete -force $ctrl86_bin
> +
> +    # 2. 1-slot boundary overrun test: mutate entry n_numaux from 0 to 1.
> +    # With f_nsyms = 1, symbol 0 claiming 1 aux entry requires 2 slots,
> +    # overrunning the table by exactly 1 slot past table end.
> +    set bad_boundary_1 "$tmpdir/coff-aux-boundary-1.o"
> +    if { [catch {coff_aux_mutate_byte $seed86 $bad_boundary_1 81 1} err] } {
> +       fail "i386 PE/COFF aux 1-slot boundary: could not mutate: $err"
> +       return
> +    }
> +    coff_aux_expect_clean_rejection $ld "$emul -r" $bad_boundary_1 \
> +       "$tmpdir/coff-aux-boundary-1.bin" \
> +       "1-slot boundary overrun (entry n_numaux = 1)"
> +
> +    # 3. Multi-slot boundary test: mutate entry n_numaux from 0 to 5.
> +    # On an unpatched linker, ld trips a BFD assertion failure in cofflink.c
> +    # followed by glibc heap corruption (free(): invalid size / SIGABRT)
> +    # and leaves a corrupt output file.
> +    set bad_boundary_5 "$tmpdir/coff-aux-boundary-5.o"
> +    if { [catch {coff_aux_mutate_byte $seed86 $bad_boundary_5 81 5} err] } {
> +       fail "i386 PE/COFF aux boundary: could not mutate: $err"
> +       return
> +    }
> +    coff_aux_expect_clean_rejection $ld "$emul -r" $bad_boundary_5 \
> +       "$tmpdir/coff-aux-boundary-5.bin" \
> +       "boundary overrun (entry n_numaux = 5)"
> +
> +    # 4. Inflated overrun test: mutate entry n_numaux from 0 to 255.
> +    # On an unpatched linker, ld crashes with glibc malloc corruption
> +    # (malloc(): invalid size / SIGABRT) and leaves a corrupt output file.
> +    set bad_overrun "$tmpdir/coff-aux-overrun.o"
> +    if { [catch {coff_aux_mutate_byte $seed86 $bad_overrun 81 255} err] } {
> +       fail "i386 PE/COFF aux overrun: could not mutate: $err"
> +       return
> +    }
> +    coff_aux_expect_clean_rejection $ld "$emul -r" $bad_overrun \
> +       "$tmpdir/coff-aux-overrun.bin" \
> +       "inflated overrun (entry n_numaux = 255)"
> +
> +    # Embedded hex representation of an exact-fit 104-byte i386 PE/COFF seed object.
> +    # Layout:
> +    #   Header: magic 0x014c, 1 section (.text), f_symptr = 64 (0x0040),
> +    #           f_nsyms = 2 (1 main symbol + 1 valid auxiliary entry).
> +    #   Section: .text (size 4, file offset 60, ret opcode 0xc3).
> +    #   Symbol 0: 'entry' (value 0, scnum 1, type 0, class 2 C_EXT, n_numaux = 1).
> +    #             Offset 64. n_sclass at offset 80, n_numaux at offset 81.
> +    #   Aux entry: 18 zero bytes at offset 82..99.
> +    #   String table: length 4 (empty) at offset 100.
> +    set seed104_hex {
> +       4c010100000000004000000002000000000000002e7465787400000000000000
> +       00000000040000003c00000000000000000000000000000020000060c3000000
> +       656e747279000000000000000100000002010000000000000000000000000000
> +       0000000004000000
> +    }
> +    set seed104 "$tmpdir/coff-aux-seed104.o"
> +    coff_aux_write_hex $seed104_hex $seed104
> +
> +    set seed104_size [file size $seed104]
> +    if { $seed104_size != 104 } {
> +       fail "i386 PE/COFF aux: invalid seed size $seed104_size (expected 104)"
> +       return
> +    }
> +
> +    if { [coff_aux_read_byte $seed104 80] != 2 || [coff_aux_read_byte $seed104 81] != 1 } {
> +       fail "i386 PE/COFF aux: 104-byte seed layout mismatch"
> +       return
> +    }
> +
> +    # 5. Positive control: unmutated 104-byte seed carrying a valid aux entry
> +    # must link cleanly.
> +    set ctrl104_bin "$tmpdir/coff-aux-ctrl104.o"
> +    file delete -force $ctrl104_bin
> +    set result [coff_aux_run_ld_capture $ld "$emul -r" $seed104 $ctrl104_bin]
> +    set status [lindex $result 0]
> +    set out [lindex $result 1]
> +    if { $status != 0 || [coff_aux_crashed $out] || ![file exists $ctrl104_bin] } {
> +       fail "i386 PE/COFF aux positive control: unmutated 104-byte seed did not link cleanly (status $status): $out"
> +       return
> +    }
> +    pass "i386 PE/COFF aux positive control: unmutated 104-byte seed with aux entry links cleanly"
> +    file delete -force $ctrl104_bin
> +
> +    # 6. Boundary overrun test with aux entry: mutate entry n_numaux from 1 to 2.
> +    # With f_nsyms = 2, symbol 0 claiming 2 aux entries requires 3 slots,
> +    # overrunning the table by exactly 1 slot past table end.
> +    set bad_boundary_exact "$tmpdir/coff-aux-boundary-exact.o"
> +    if { [catch {coff_aux_mutate_byte $seed104 $bad_boundary_exact 81 2} err] } {
> +       fail "i386 PE/COFF aux boundary with aux entry: could not mutate: $err"
> +       return
> +    }
> +    coff_aux_expect_clean_rejection $ld "$emul -r" $bad_boundary_exact \
> +       "$tmpdir/coff-aux-boundary-exact.bin" \
> +       "boundary overrun with aux entry (entry n_numaux = 2)"
> +}
> +
> +coff_aux_test
> diff --git a/ld/testsuite/ld-powerpc/xcoff-aux.exp b/ld/testsuite/ld-powerpc/xcoff-aux.exp
> index 631186682e7..e8a5bc8a371 100644
> --- a/ld/testsuite/ld-powerpc/xcoff-aux.exp
> +++ b/ld/testsuite/ld-powerpc/xcoff-aux.exp
> @@ -369,4 +369,93 @@ proc xcoff_aux_test {} {
>
>  }
>
> +# Exercise 64-bit XCOFF auxiliary symbol bounds checking.
> +proc xcoff64_aux_test {} {
> +    global ld tmpdir
> +
> +    set emul ""
> +    if { [ld_supports_emul "aix5rs6"] } {
> +       set emul "-m aix5rs6"
> +    }
> +
> +    # Embedded hex representation of a valid 378-byte XCOFF64 seed object.
> +    # Layout:
> +    #   Header: magic 0x01f7 (XCOFF64), 3 sections (.text, .data, .bss),
> +    #           f_symptr = 248 (0x00f8), f_nsyms = 6.
> +    #   Symbols: 3 symbols with 1 auxiliary entry each = 6 slots of 18 bytes:
> +    #     Slot 0..1: '.file' (class 103 C_FILE, n_numaux = 1) -> nx at off 265
> +    #     Slot 2..3: '.text' (class 107 C_HIDEXT, n_numaux = 1)
> +    #     Slot 4..5: 'entry' (class 2 C_EXT, n_numaux = 1)
> +    set seed64_hex {
> +       01f700030000000000000000000000f800000204000000062e74657874000000
> +       00000000000000000000000000000000000000000000000400000000000000f0
> +       0000000000000000000000000000000000000000000000000000002000000000
> +       2e64617461000000000000000000000800000000000000080000000000000000
> +       0000000000000000000000000000000000000000000000000000000000000000
> +       00000040000000002e6273730000000000000000000000080000000000000008
> +       0000000000000000000000000000000000000000000000000000000000000000
> +       000000000000000000000080000000004e800020000000000000000000000000
> +       00000004fffe00006701736565642e630000000000000000000000fc00000000
> +       000000000000000a000100006b010000000400000000000011000000000000fb
> +       0000000000000000000000100001000002010000000200000000000002000000
> +       000000fb000000162e66696c65002e7465787400656e74727900
> +    }
> +    regsub -all {\s+} $seed64_hex "" clean64_hex
> +    set seed64 "$tmpdir/xcoff64-aux-seed.o"
> +    set f [open $seed64 "wb"]
> +    fconfigure $f -translation binary
> +    puts -nonewline $f [binary format "H*" $clean64_hex]
> +    close $f
> +
> +    set seed64_size [file size $seed64]
> +    if { $seed64_size != 378 } {
> +       fail "XCOFF64 aux: invalid seed size $seed64_size (expected 378)"
> +       return
> +    }
> +
> +    # Verify .file n_numaux at offset 265 before testing.
> +    if { [read_byte $seed64 265] != 1 } {
> +       unresolved "XCOFF64 aux test: seed layout does not match expected offset"
> +       return
> +    }
> +
> +    # 8. Positive control: unmutated 64-bit seed must link cleanly.
> +    set ctrl_bin "$tmpdir/xcoff64-aux-control"
> +    file delete -force $ctrl_bin
> +    set result [run_ld_capture $ld "$emul -b64 -e 0" $seed64 $ctrl_bin]
> +    set status [lindex $result 0]
> +    set out [lindex $result 1]
> +    if { $status != 0 || [crashed $out] || ![file exists $ctrl_bin] } {
> +       fail "XCOFF64 aux positive control: unmutated seed did not link cleanly (status $status): $out"
> +       return
> +    }
> +    pass "XCOFF64 aux positive control: unmutated seed links cleanly"
> +    file delete -force $ctrl_bin
> +
> +    # 9. Boundary overrun: mutate .file n_numaux from 1 to 6 (exceeds table end).
> +    # On an unpatched linker, ld trips auxtype warnings and a BFD assertion
> +    # failure in xcofflink.c.
> +    set bad_boundary "$tmpdir/xcoff64-aux-boundary.o"
> +    if { [catch {mutate_byte $seed64 $bad_boundary 265 6} err] } {
> +       fail "XCOFF64 aux boundary: could not mutate: $err"
> +       return
> +    }
> +    expect_clean_rejection $ld "$emul -b64 -e 0" $bad_boundary \
> +       "$tmpdir/xcoff64-aux-boundary" \
> +       "64-bit non-csect boundary, 1 slot past table end (.file n_numaux = 6)"
> +
> +    # 10. Inflated overrun: mutate .file n_numaux from 1 to 255.
> +    # On an unpatched linker, ld crashes with glibc heap corruption
> +    # (malloc(): invalid size / SIGABRT) and leaves a corrupt output file.
> +    set bad_overrun "$tmpdir/xcoff64-aux-overrun.o"
> +    if { [catch {mutate_byte $seed64 $bad_overrun 265 255} err] } {
> +       fail "XCOFF64 aux overrun: could not mutate: $err"
> +       return
> +    }
> +    expect_clean_rejection $ld "$emul -b64 -e 0" $bad_overrun \
> +       "$tmpdir/xcoff64-aux-overrun" \
> +       "64-bit non-csect inflated overrun (.file n_numaux = 255)"
> +}
> +
>  xcoff_aux_test
> +xcoff64_aux_test
> --
> 2.55.0
>
>


More information about the Binutils mailing list