[binutils-gdb] PR 33473 SEGV in _bfd_elf_gc_mark_debug_special_section_group

Alan Modra amodra@sourceware.org
Mon Nov 24 21:33:51 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4e397ea9446b3dca3cbc85b6c0e0158042548f19

commit 4e397ea9446b3dca3cbc85b6c0e0158042548f19
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Nov 24 18:46:35 2025 +1030

    PR 33473 SEGV in _bfd_elf_gc_mark_debug_special_section_group
    
    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:
---
 bfd/elf.c     |  2 +-
 bfd/elflink.c | 11 +++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

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 96497ac1c33..60a0a0efd88 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -14192,7 +14192,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;
@@ -14201,19 +14201,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);
     }
 }


More information about the Binutils-cvs mailing list