PR 33473 SEGV in _bfd_elf_gc_mark_debug_special_section_group

Alan Modra amodra@gmail.com
Mon Nov 24 11:26:23 GMT 2025


The code that faulted made the assumption that a group section always
had at least one valid member.  Fix that assumption.  Also fail if all
entries in a SHT_GROUP section are invalid.  (An empty group will not
result in a call to process_sht_group_entries.)

	PR 33473
	* elflink.x (_bfd_elf_gc_mark_debug_special_section_group): Don't
	segfault on empty group.
	* elf.c (process_sht_group_entries): Return false if all
	entries are invalid.

diff --git a/bfd/elf.c b/bfd/elf.c
index d51552e2a2d..33c2d269a9c 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -790,7 +790,7 @@ process_sht_group_entries (bfd *abfd,
     }
 
   free (contents);
-  return true;
+  return last_elt != NULL;
 }
 
 bool
diff --git a/bfd/elflink.c b/bfd/elflink.c
index ec3ad9735fe..81e4ee3da69 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -14198,7 +14198,7 @@ _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
   /* First scan to see if group contains any section other than debug
      and special section.  */
   ssec = msec = elf_next_in_group (grp);
-  do
+  while (msec != NULL)
     {
       if ((msec->flags & SEC_DEBUGGING) == 0)
 	is_debug_grp = false;
@@ -14207,19 +14207,22 @@ _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
 	is_special_grp = false;
 
       msec = elf_next_in_group (msec);
+      if (msec == ssec)
+	break;
     }
-  while (msec != ssec);
 
   /* If this is a pure debug section group or pure special section group,
      keep all sections in this group.  */
   if (is_debug_grp || is_special_grp)
     {
-      do
+      msec = ssec;
+      while (msec != NULL)
 	{
 	  msec->gc_mark = 1;
 	  msec = elf_next_in_group (msec);
+	  if (msec == ssec)
+	    break;
 	}
-      while (msec != ssec);
     }
 }
 

-- 
Alan Modra


More information about the Binutils mailing list