[binutils-gdb] ld: Don't define section symbols for excluded sections

"H.J. Lu via Binutils-cvs" <[email protected]> Thu, 30 Jul 2026 10:57:30 +0000 (GMT)
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dd1268210b6f6=
eddd267a4ebe4b7d36b802af628d

commit d1268210b6f6eddd267a4ebe4b7d36b802af628d
Author: H.J. Lu <[email protected]>
Date:   Thu Jul 30 15:10:36 2026 +0800

    ld: Don't define section symbols for excluded sections
   =20
    When the SEC_EXCLUDE bit is set on a section, for example, sections with
    the SHF_EXCLUDE flag bit set in ELF input, the contents of the section
    are excluded by the linker for non-relocatable output.  Define  __start,
    __stop, .startof. and .sizeof. symbols for relocatable link or if the
    SEC_EXCLUDE bit on the section is cleared.
   =20
            PR ld/34448
            * ldlang.c (lang_init_start_stop): Call lang_define_start_stop
            for relocatable link or if the SEC_EXCLUDE bit on the section
            is cleared.
   =20
    Signed-off-by: H.J. Lu <[email protected]>

Diff:
---
 ld/ldlang.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index b04ba113aaa..886c49a1861 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -7283,28 +7283,31 @@ lang_init_start_stop (void)
=20
   for (abfd =3D link_info.input_bfds; abfd !=3D NULL; abfd =3D abfd->link.=
next)
     for (s =3D abfd->sections; s !=3D NULL; s =3D s->next)
-      {
-	const char *ps;
-	const char *secname =3D s->name;
+      if (bfd_link_relocatable (&link_info)
+	  || (s->flags & SEC_EXCLUDE) =3D=3D 0)
+	{
+	  const char *ps;
+	  const char *secname =3D s->name;
=20
-	for (ps =3D secname; *ps !=3D '\0'; ps++)
-	  if (!ISALNUM ((unsigned char) *ps) && *ps !=3D '_')
-	    break;
-	if (*ps =3D=3D '\0')
-	  {
-	    char *symbol =3D (char *) xmalloc (10 + strlen (secname));
+	  for (ps =3D secname; *ps !=3D '\0'; ps++)
+	    if (!ISALNUM ((unsigned char) *ps) && *ps !=3D '_')
+	      break;
+	  if (*ps =3D=3D '\0')
+	    {
+	      char *symbol =3D (char *) xmalloc (10 + strlen (secname));
=20
-	    symbol[0] =3D leading_char;
-	    sprintf (symbol + (leading_char !=3D 0), "__start_%s", secname);
-	    lang_define_start_stop (symbol, s);
+	      symbol[0] =3D leading_char;
+	      sprintf (symbol + (leading_char !=3D 0), "__start_%s",
+		       secname);
+	      lang_define_start_stop (symbol, s);
=20
-	    symbol[1] =3D leading_char;
-	    memcpy (symbol + 1 + (leading_char !=3D 0), "__stop", 6);
-	    lang_define_start_stop (symbol + 1, s);
+	      symbol[1] =3D leading_char;
+	      memcpy (symbol + 1 + (leading_char !=3D 0), "__stop", 6);
+	      lang_define_start_stop (symbol + 1, s);
=20
-	    free (symbol);
-	  }
-      }
+	      free (symbol);
+	    }
+	}
 }
=20
 /* Iterate over start_stop_syms.  */