Re: [PATCH v2] ld: Check input section garbage collection error

"H.J. Lu" <[email protected]>
Newsgroups gmane.comp.gnu.binutils
Message-ID <CAMe9rOqs0fpVVX515FpFqhL2kmgowN4T83tPs_zStiTDJKX5=g@mail.gmail.com>
On Thu, Aug 6, 2026 at 9:06 AM Alan Modra <[email protected]> wrote:
>
> On Thu, Aug 06, 2026 at 07:34:43AM +0800, H.J. Lu wrote:
> > diff --git a/bfd/elflink.c b/bfd/elflink.c
> > index 31c652b17c9..343fc880cda 100644
> > --- a/bfd/elflink.c
> > +++ b/bfd/elflink.c
> > @@ -14776,7 +14776,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..f8e9d2496a4 100644
> > --- a/ld/ldlang.c
> > +++ b/ld/ldlang.c
> > @@ -8165,8 +8165,10 @@ 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)
> > +      && bfd_get_error () == bfd_error_bad_value)
>
> OK without the bfd_get_error test.  If there is some condition under
> which bfd_gc_sections returns false where we'd like the linker to
> continue, then we'll fix bfd_gc_sections to return true.  As far as I
> can see with a quick scan over the code, bfd_elf_gc_sections returns
> false only on an error reading relocs, and I think all of the possible
> false returns from elf_link_read_relocs_from_section ought to cause a
> fatal error.

Fixed.   This is the patch I am checking in.

> > +    fatal (_("%P: --gc-sections failed: %E\n"));
> >  }
> >
> >  /* Worker for lang_find_relro_sections_1.  */
> > --
> > 2.55.0
> >
>
>
> --
> Alan Modra



-- 
H.J.
0001-ld-Check-input-section-garbage-collection-error.patch (text/x-patch, 1.9 KB)
From 76323447462ebd8f3e89739c1761d899e282bed7 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <[email protected]>
Date: Wed, 5 Aug 2026 16:43:05 +0800
Subject: [PATCH] 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 <[email protected]>
---
 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 31c652b17c9..343fc880cda 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -14776,7 +14776,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.  */
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.