[2/3] bfd: decline a non-XCOFF output in build_dynamic_sections

Message ID 20261024095819.1905597-3-harshit7kr@gmail.com
State New
Headers
Series bfd: fix AIX after-allocation crashes on foreign and retargeted inputs |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed

Commit Message

Harshit Kumar Oct. 24, 2026, 9:58 a.m. UTC
  The AIX emulations allow a linker script to select a non-XCOFF output
format via OUTPUT_FORMAT (such as elf32-little).  When that happens,
the link hash table is a generic bfd_link_hash_table, not the larger
xcoff_link_hash_table.

bfd_xcoff_build_dynamic_sections runs in ldemul_after_allocation and
assumes an xcoff_link_hash_table.  Accessing xcoff_hash_table (info)
performs an out-of-bounds read into adjacent heap memory (which
AddressSanitizer reported as a heap-use-after-free because an adjacent
heap region was freed).

Return true immediately when output_bfd does not have XCOFF flavour,
matching the existing guard in bfd_xcoff_size_dynamic_sections.

bfd/
	* xcofflink.c (bfd_xcoff_build_dynamic_sections): Return early
	when output_bfd flavour is not XCOFF.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/xcofflink.c | 5 +++++
 1 file changed, 5 insertions(+)
  

Patch

diff --git a/bfd/xcofflink.c b/bfd/xcofflink.c
index 10b1b64a..e8838c94 100644
--- a/bfd/xcofflink.c
+++ b/bfd/xcofflink.c
@@ -4135,6 +4135,11 @@  bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
   bfd *sub;
   asection *sec;
 
+  /* The AIX emulations allow a linker script to select a non-XCOFF
+     output format, which uses a generic hash table.  */
+  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
+    return true;
+
   ldinfo = &(xcoff_hash_table (info)->ldinfo);
 
   if (xcoff_hash_table (info)->loader_section