[1/3] bfd: do not dereference absent XCOFF magic sections

Message ID 20261024095819.1905597-2-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
  bfd_xcoff_build_dynamic_sections reads linkage_section, toc_section and
descriptor_section out of the link hash table and dereferences each to
test sec->size.  These magic sections are created inside
xcoff_link_create_extra_sections (called by xcoff_link_add_symbols),
and only when the input's xvec matches the output's.  When a foreign
input is linked, none of them are created, so all three remain NULL.
(A nearby FIXME in xcoff_link_create_extra_sections acknowledges that
producing an XCOFF output with no XCOFF input is not handled here.)

That happens with a simple command line.  ld reports an incompatible-
architecture error in lang_check, but because the error is non-fatal
(%X), execution continues into the after-allocation hook with the magic
sections unset.  The subsequent dereference then crashes:

  $ ld -maix5rs6 -o out sc.o
  ld: i386:x86-64 architecture of input file `sc.o' is incompatible
      with rs6000:6000 output
  Segmentation fault

Check each magic section for NULL before reading its size, using
sec != NULL && sec->size != 0 to match _bfd_xcoff_bfd_final_link.

bfd/
	* xcofflink.c (bfd_xcoff_build_dynamic_sections): Check each
	magic section for NULL before reading its size.

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

Patch

diff --git a/bfd/xcofflink.c b/bfd/xcofflink.c
index f155d1c3..10b1b64a 100644
--- a/bfd/xcofflink.c
+++ b/bfd/xcofflink.c
@@ -4141,9 +4141,10 @@  bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
       && !xcoff_build_loader_section (ldinfo))
     return false;
 
-  /* Allocate space for the magic sections.  */
+  /* Allocate space for the magic sections.  They are created only
+     when an XCOFF input is added, so any of them may be NULL.  */
   sec = xcoff_hash_table (info)->linkage_section;
-  if (sec->size > 0)
+  if (sec != NULL && sec->size != 0)
     {
       sec->contents = bfd_zalloc (output_bfd, sec->size);
       if (sec->contents == NULL)
@@ -4151,7 +4152,7 @@  bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
       sec->alloced = 1;
     }
   sec = xcoff_hash_table (info)->toc_section;
-  if (sec->size > 0)
+  if (sec != NULL && sec->size != 0)
     {
       sec->contents = bfd_zalloc (output_bfd, sec->size);
       if (sec->contents == NULL)
@@ -4159,7 +4160,7 @@  bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
       sec->alloced = 1;
     }
   sec = xcoff_hash_table (info)->descriptor_section;
-  if (sec->size > 0)
+  if (sec != NULL && sec->size != 0)
     {
       sec->contents = bfd_zalloc (output_bfd, sec->size);
       if (sec->contents == NULL)