]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ld: Don't define section symbols for excluded sections
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 30 Jul 2026 07:10:36 +0000 (15:10 +0800)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 30 Jul 2026 10:55:11 +0000 (18:55 +0800)
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.

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.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
ld/ldlang.c

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