From 02ba2ff2dd6c58cb657cdbab4c33339b40ce1689 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 5 Aug 2026 16:43:05 +0800
Subject: [PATCH] ld: Check bfd_gc_sections return

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.

	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 <hjl.tools@gmail.com>
---
 ld/ldlang.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

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

