[binutils-gdb] ld: Check input section garbage collection error

H.J. Lu hjl@sourceware.org
Thu Aug 6 01:41:15 GMT 2026


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

commit a692a633d407995a5ce87e84a0b1032e9ac51360
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 5 16:43:05 2026 +0800

    ld: Check input section garbage collection error
    
    The ELF backend gc_mark_extra_sections function may return false for
    error and bfd_gc_sections may return false on invalid input:
    
    ld: pr34448-bug_18.o: bad reloc symbol index (0xf2000005 >= 0x13) for offset 0x4 in section `.text.get_tls[get_tls]'
    
    Change bfd_elf_gc_sections to return false if gc_mark_extra_sections
    return false.  Change lang_gc_sections to check bfd_gc_sections return
    and report the fatal error.
    
    bfd/
            PR ld/34448
            * elflink.c (bfd_elf_gc_sections): Return false if
            gc_mark_extra_sections return false.
    
    ld/
            PR ld/34448
            * ldlang.c (lang_gc_sections): Check bfd_gc_sections return and
            report the fatal error.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>

Diff:
---
 bfd/elflink.c | 3 ++-
 ld/ldlang.c   | 5 +++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 11da9a744e2..cc29b99ba56 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -14764,7 +14764,8 @@ bfd_elf_gc_sections (bfd *obfd, struct bfd_link_info *info)
     }
 
   /* Allow the backend to mark additional target specific sections.  */
-  obed->gc_mark_extra_sections (info, gc_mark_hook);
+  if (!obed->gc_mark_extra_sections (info, gc_mark_hook))
+    return false;
 
   /* ... and mark SEC_EXCLUDE for those that go.  */
   return elf_gc_sweep (obfd, info);
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 886c49a1861..65494acca00 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8165,8 +8165,9 @@ lang_gc_sections (void)
 	}
     }
 
-  if (link_info.gc_sections)
-    bfd_gc_sections (link_info.output_bfd, &link_info);
+  if (link_info.gc_sections
+      && !bfd_gc_sections (link_info.output_bfd, &link_info))
+    fatal (_("%P: --gc-sections failed: %E\n"));
 }
 
 /* Worker for lang_find_relro_sections_1.  */


More information about the Binutils-cvs mailing list