alpha: handle discarded got section

Message ID 15b821d8532c3e0c3d4c35f9f3f543684596e986.1778457849.git.amodra@gmail.com
State New
Headers
Series alpha: handle discarded got section |

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

Alan Modra May 11, 2026, 5:11 a.m. UTC
  This fixes testsuite failures I noticed when changing dynobj to be
linker created, but I think the bug could be triggered by suitably
crafted linker scripts without any dynobj changes.

	* elf64-alpha.c (elf64_alpha_final_link): Don't attempt to
	write out a got section that has been discarded.
  

Patch

diff --git a/bfd/elf64-alpha.c b/bfd/elf64-alpha.c
index 0a63ca0bbdb..91cc7bcaec8 100644
--- a/bfd/elf64-alpha.c
+++ b/bfd/elf64-alpha.c
@@ -5248,28 +5248,28 @@  elf64_alpha_final_link (bfd *abfd, struct bfd_link_info *info)
   /* Now write out the computed sections.  */
 
   /* The .got subsections...  */
-  {
-    bfd *i, *dynobj = elf_hash_table(info)->dynobj;
-    for (i = htab->got_list;
-	 i != NULL;
-	 i = alpha_elf_tdata(i)->got_link_next)
-      {
-	asection *sgot;
+  bfd *i, *dynobj = elf_hash_table(info)->dynobj;
+  for (i = htab->got_list;
+       i != NULL;
+       i = alpha_elf_tdata(i)->got_link_next)
+    {
+      asection *sgot;
 
-	/* elf_bfd_final_link already did everything in dynobj.  */
-	if (i == dynobj)
-	  continue;
+      /* elf_bfd_final_link already did everything in dynobj.  */
+      if (i == dynobj)
+	continue;
 
-	sgot = alpha_elf_tdata(i)->got;
-	if (! bfd_set_section_contents (abfd, sgot->output_section,
+      sgot = alpha_elf_tdata(i)->got;
+      if (sgot != NULL
+	  && !bfd_is_abs_section (sgot->output_section)
+	  && !bfd_set_section_contents (abfd, sgot->output_section,
 					sgot->contents,
 					(file_ptr) sgot->output_offset,
 					sgot->size))
-	  return false;
-      }
-  }
+	return false;
+    }
 
-  if (mdebug_sec != (asection *) NULL)
+  if (mdebug_sec != NULL)
     {
       BFD_ASSERT (abfd->output_has_begun);
       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,