PR 34204 dlltool SEGVs with --exclude-symbols

Alan Modra amodra@gmail.com
Wed Jun 10 06:25:50 GMT 2026


This fixes a segfault introduced by commit 45a7f5a29de7 which didn't
take into account that there was a use of leading_underscore in option
processing.

	PR 34204
	* dlltool.c (struct string_list): Make string a flexible array
	member.
	(add_excludes): Adjust to suit.  Extract code adding underscore
	and informing..
	(underscore_excludes): ..to here.  New function.
	(main): Call underscore_excludes.

diff --git a/binutils/dlltool.c b/binutils/dlltool.c
index f4ee894de1b..67839632a43 100644
--- a/binutils/dlltool.c
+++ b/binutils/dlltool.c
@@ -789,7 +789,7 @@ export_type;
 struct string_list
 {
   struct string_list *next;
-  char *string;
+  char string[];
 };
 
 static struct string_list *excludes;
@@ -1460,23 +1460,37 @@ add_excludes (const char *new_excludes)
   exclude_string = strtok (local_copy, ",:");
   for (; exclude_string; exclude_string = strtok (NULL, ",:"))
     {
-      struct string_list *new_exclude = xmalloc (sizeof (*new_exclude));
-      /* Don't add a leading underscore for fastcall symbols.  */
-      if (*exclude_string == '@')
-	new_exclude->string = xstrdup (exclude_string);
-      else
-	new_exclude->string = xasprintf ("%s%s", leading_underscore,
-					 exclude_string);
+      size_t len = strlen (exclude_string);
+      /* Allocate extra byte for possible underscore.  */
+      struct string_list *new_exclude = xmalloc (sizeof (*new_exclude)
+						 + len + 2);
+      memcpy (new_exclude->string, exclude_string, len + 1);
       new_exclude->next = excludes;
       excludes = new_exclude;
-
-      /* xgettext:c-format */
-      inform (_("Excluding symbol: %s"), exclude_string);
     }
 
   free (local_copy);
 }
 
+/* Prefix symbols on the excludes list with an underscore.  */
+
+static void
+underscore_excludes (void)
+{
+  for (struct string_list *ex = excludes; ex; ex = ex->next)
+    {
+      /* Don't add a leading underscore for fastcall symbols.  */
+      if (*ex->string != '@' && *leading_underscore)
+	{
+	  size_t len = strlen (ex->string);
+	  memmove (ex->string + 1, ex->string, len + 1);
+	  *ex->string = *leading_underscore;
+	}
+      /* xgettext:c-format */
+      inform (_("Excluding symbol: %s"), ex->string);
+    }
+}
+
 /* See if STRING is on the list of symbols to exclude.  */
 
 static bool
@@ -4065,6 +4079,8 @@ main (int ac, char **av)
   if (do_default_excludes)
     set_default_excludes ();
 
+  underscore_excludes ();
+
   if (def_file)
     process_def_file (def_file);
 

-- 
Alan Modra


More information about the Binutils mailing list