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

"H.J. Lu" <[email protected]>
Newsgroups gmane.comp.gnu.binutils
Message-ID <CAMe9rOo4xgHGW1WKYCM3fu_cgk1_PUbSYg-z1nnJ-OXQv5STUQ@mail.gmail.com>
On Thu, Aug 6, 2026 at 7:04 AM Alan Modra <[email protected]> wrote:
>
> On Thu, Aug 06, 2026 at 05:24:14AM +0800, H.J. Lu wrote:
> > On Wed, Aug 5, 2026 at 6:40 PM Jan Beulich <[email protected]> wrote:
> > >
> > > On 05.08.2026 12:32, H.J. Lu wrote:
> > > > On Wed, Aug 5, 2026 at 4:59 PM Jan Beulich <[email protected]> wrote:
> > > >>
> > > >> On 05.08.2026 10:50, H.J. Lu wrote:
> > > >>> 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]'
> > > >>>
> > > >>> and set the BFD error to bfd_error_bad_value.  Check bfd_gc_sections
> > > >>> return and report the fatal error if the BFD error is bfd_error_bad_value.
> > > >>
> > > >> And what guarantees that bfd_error_bad_value isn't also set for other kinds
> > > >> of errors, from about anywhere in the library? Does bfd_gc_sections()
> > > >> returning false even reliably set the error indicator? That's far from
> > > >
> > > > If it isn't the case, it is a bug.
> > > >
> > > >> obvious when looking at e.g. bfd_elf_gc_sections(), so I can't exclude the
> > > >> possibility of bfd_error_bad_value having been on record before the call,
> > > >> and it simply not getting changed.
> > > >
> > > > Isn't it a bug?
> > >
> > > I don't know, as I don't know enough of the history of libbfd. The value could
> > > be meant to be errno-like, i.e. you may need to clear it at certain points
> > > (especially if there's a mix of functions storing into and not storing into
> > > the field).
> >
> > When a bfd function returns an error, the BFD error should be set
> > appropriately.  If it isn't the case, it is a BFD bug.
>
> The original commits 303b4cc64fa8 and 71b012a810fd omitted
> checking the return from bfd_gc_sections, but I'm inclined to think
> that any false return from bfd_gc_sections should result in an error
> exit from ld, displaying the bfd error with %E.  We also should not be
> ignoring a false return from gc_mark_extra_sections in
> bfd_elf_gc_sections.
>
> --
> Alan Modra

Here is the v2 patch to check gc_mark_extra_sections error return.
With

commit 44fab92685fd1f4bc02cc6668237883ed0d9789d
Author: H.J. Lu <[email protected]>
Date:   Thu Jul 30 15:18:42 2026 +0800

    ld: Don't treated the fatal error as warning

the fatal error won't be ignored by -w.

-- 
H.J.
---
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]'

and set the BFD error to bfd_error_bad_value.

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 if the BFD error is bfd_error_bad_value.

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 if the BFD error is bfd_error_bad_value.
v2-0001-ld-Check-input-section-garbage-collection-error.patch (text/x-patch, 2.1 KB)
From a311b4e8150b6f24bd5d99eee525513624754c9a Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <[email protected]>
Date: Wed, 5 Aug 2026 16:43:05 +0800
Subject: [PATCH v2] 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]'

and set the BFD error to bfd_error_bad_value.

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 if the BFD error is bfd_error_bad_value.

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 if the BFD error is bfd_error_bad_value.

Signed-off-by: H.J. Lu <[email protected]>
---
 bfd/elflink.c | 3 ++-
 ld/ldlang.c   | 6 ++++--
 2 files changed, 6 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..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)
+    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.