PR27128, nm -P portable output format regression

Alan Modra amodra@gmail.com
Wed Dec 30 11:50:54 GMT 2020


https://sourceware.org/pipermail/binutils/2020-March/000233.html
appended a symbol version string for nm -D, but got it wrong for
--format=posix and --format=sysv.  This patch fixes that problem, and
also prints versions for demangled names.

Should we print versions when demangling?  I'm inclined to think they
are useful, but am open to argument.

	PR 27128
	* nm.c (print_symname): Append version string to symbol name
	before printing the lot under control of "form".  Append version
	to demangled names too.

diff --git a/binutils/nm.c b/binutils/nm.c
index 2946bd6904..e77828eeff 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -407,21 +407,17 @@ static void
 print_symname (const char *form, struct extended_symbol_info *info,
 	       const char *name, bfd *abfd)
 {
+  char *alloc = NULL;
+
   if (name == NULL)
     name = info->sinfo->name;
   if (do_demangle && *name)
     {
-      char *res = bfd_demangle (abfd, name, demangle_flags);
-
-      if (res != NULL)
-	{
-	  printf (form, res);
-	  free (res);
-	  return;
-	}
+      alloc = bfd_demangle (abfd, name, demangle_flags);
+      if (alloc != NULL)
+	name = alloc;
     }
 
-  printf (form, name);
   if (info != NULL && info->elfinfo)
     {
       const char *version_string;
@@ -431,11 +427,17 @@ print_symname (const char *form, struct extended_symbol_info *info,
 	= bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol,
 					 FALSE, &hidden);
       if (version_string && version_string[0])
-	printf ("%s%s",
-	       (hidden || bfd_is_und_section (info->elfinfo->symbol.section)
-		? "@" : "@@"),
-	       version_string);
+	{
+	  const char *at = "@@";
+	  if (hidden || bfd_is_und_section (info->elfinfo->symbol.section))
+	    at = "@";
+	  alloc = reconcat (alloc, name, at, version_string, NULL);
+	  if (alloc != NULL)
+	    name = alloc;
+	}
     }
+  printf (form, name);
+  free (alloc);
 }
 
 static void

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list