[PATCH 0/3] bfd: fix AIX after-allocation crashes on foreign and retargeted inputs

Harshit Kumar harshit7kr@gmail.com
Fri Nov 13 07:33:25 GMT 2026


Gentle ping.

This series fixes two crashes in bfd_xcoff_build_dynamic_sections, which
every AIX emulation runs after allocation:

  1/3  a foreign (non-XCOFF) input leaves the linkage, TOC and descriptor
       sections NULL, and they are dereferenced;
  2/3  a linker script that retargets the output to a generic ELF format
       makes the function read xcoff_hash_table fields from a plain
       bfd_link_hash_table.
  3/3  adds ld-powerpc/aix-retarget.exp (8/8 pass with the series, 8/8
       fail without).

It still applies cleanly to current master (b6272d0e9071).  The same
retargeted-output pattern was fixed for the AVR, M68HC1x and VxWorks
emulations in ed80917c3b8 and a3e26a5b700.

Happy to rework it if you would prefer a different approach.

Thanks,
Harshit


On Sat, 24 Oct 2026 at 15:28, Harshit Kumar <harshit7kr@gmail.com> wrote:
>
> bfd_xcoff_build_dynamic_sections runs after allocation for every AIX
> emulation.  It assumes that the input created the XCOFF linkage, TOC and
> descriptor sections, and that the link hash table is an XCOFF one.
> Neither is guaranteed: ld reports an incompatible architecture with
> an error in lang_check, but because the error is non-fatal (%X),
> execution continues into ldemul_after_allocation.  Additionally, a
> linker script can retarget the output to a generic ELF format with a
> plain bfd_link_hash_table.  Both paths previously crashed before the
> link could terminate.
>
> Base commit: 2c5ab4463fbfa424e6677549ee6086ed532ac714
>
> The two code patches are independent and can be applied in either order
> (verified with git am on the base commit).  The test patch depends on
> both.
>
> Summary of changes:
>
> 1. bfd: do not dereference absent XCOFF magic sections
>    When a foreign input is linked, none of the magic sections are
>    created (a nearby FIXME in xcoff_link_create_extra_sections
>    acknowledges that XCOFF output with no XCOFF input is not handled
>    here), leaving linkage_section, toc_section, and descriptor_section
>    NULL.  Each section is now checked for NULL before reading its size
>    (sec != NULL && sec->size != 0).
>
> 2. bfd: decline a non-XCOFF output in build_dynamic_sections
>    A script can retarget the output to a generic ELF target (such as
>    elf32-little), which allocates a generic bfd_link_hash_table.
>    Touching xcoff_hash_table performs an out-of-bounds read into
>    adjacent heap memory.  The function now returns early when
>    output_bfd is not XCOFF, matching bfd_xcoff_size_dynamic_sections.
>
> 3. ld: add AIX after-allocation regression test
>    Adds ld-powerpc/aix-retarget.exp, which links an embedded x86-64
>    relocatable object across all four AIX emulations, with and without
>    a retargeting script.  Asserts that ld exits 1 cleanly with the
>    architecture mismatch diagnostic, leaves no output, and does not
>    crash.  Passes 8/8 patched, fails 8/8 unpatched.
>
> Audit of magic sections:
> A full grep across bfd/xcofflink.c confirmed no other unguarded
> dereferences of the magic sections remain.  _bfd_xcoff_bfd_final_link
> already guards each with "!= NULL", bfd_xcoff_size_stubs skips
> non-XCOFF inputs, and other sites use pointer equality.
>
> Testing: x86_64-pc-linux-gnu host, --enable-targets=all all-ld build;
> aix-retarget.exp passes 8/8 patched and fails 8/8 unpatched; full
> ld-powerpc suite passes 15/15; AddressSanitizer reports no issues.
>
> Harshit Kumar (3):
>   bfd: do not dereference absent XCOFF magic sections
>   bfd: decline a non-XCOFF output in build_dynamic_sections
>   ld: add AIX after-allocation regression test
>
>  bfd/xcofflink.c                          |  14 +-
>  ld/testsuite/ld-powerpc/aix-retarget.exp | 165 +++++++++++++++++++++++
>  2 files changed, 175 insertions(+), 4 deletions(-)
>  create mode 100644 ld/testsuite/ld-powerpc/aix-retarget.exp
>
> --
> 2.55.0
>
>


More information about the Binutils mailing list