[binutils-gdb] Don't return "(null)" from bfd_elf_sym_name

Alan Modra amodra@sourceware.org
Thu Oct 3 04:37:28 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=68bbe118337939aa0b52e007a7415c8a157579a1

commit 68bbe118337939aa0b52e007a7415c8a157579a1
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Oct 1 14:08:08 2024 +0930

    Don't return "(null)" from bfd_elf_sym_name
    
    A NULL return from bfd_elf_string_from_elf_section indicates an error.
    That shouldn't be masked by bfd_elf_sym_name but rather passed up to
    callers such as group_signature.  If we want to print "(null)" then
    that should be done at a higher level.  That's what this patch does,
    except that I chose to print "<null>" instead, like readelf.  If we
    see "(null)" we're probably passing a NULL to printf.  I haven't
    changed aoutx.h or pdp11.c print_symbol functions because they already
    handle NULL names by omitting the name.  I also haven't changed
    mach-o.c, mmo.c, som.c, srec.c, tekhex.c, vms-alpha.c and
    wasm-module.c print_symbol function because it looks like they will
    never have NULL symbol names.
    
    bfd/
            * elf.c (bfd_elf_sym_name): Don't turn a NULL name into a
            pointer to "(null)".
            (bfd_elf_print_symbol): Print "<null>" for NULL symbol names.
            * coffgen.c (coff_print_symbol): Likewise.
            * ecoff.c (_bfd_ecoff_print_symbol): Likewise.
            * pef.c (bfd_pef_print_symbol): Likewise.
            * syms.c (bfd_symbol_info): Return "<null>" in symbol_info.name
            if symbol name is NULL.
    ld/
            * ldlang.c (ld_is_local_symbol): Don't check for "(null)"
            symbol name.

Diff:
---
 bfd/coffgen.c | 12 +++++++-----
 bfd/ecoff.c   |  5 +++--
 bfd/elf.c     | 19 +++++++++----------
 bfd/pef.c     |  7 ++++---
 bfd/syms.c    |  2 +-
 ld/ldlang.c   |  3 ---
 6 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index cc1c655738b..ff382a7e9c9 100644
--- a/bfd/coffgen.c
+++ b/bfd/coffgen.c
@@ -2161,11 +2161,12 @@ coff_print_symbol (bfd *abfd,
 		   bfd_print_symbol_type how)
 {
   FILE * file = (FILE *) filep;
+  const char *symname = symbol->name ? symbol->name : "<null>";
 
   switch (how)
     {
     case bfd_print_symbol_name:
-      fprintf (file, "%s", symbol->name);
+      fprintf (file, "%s", symname);
       break;
 
     case bfd_print_symbol_more:
@@ -2189,7 +2190,7 @@ coff_print_symbol (bfd *abfd,
 	  if (combined < obj_raw_syments (abfd)
 	      || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
 	    {
-	      fprintf (file, _("<corrupt info> %s"), symbol->name);
+	      fprintf (file, _("<corrupt info> %s"), symname);
 	      break;
 	    }
 
@@ -2207,7 +2208,7 @@ coff_print_symbol (bfd *abfd,
 		   combined->u.syment.n_sclass,
 		   combined->u.syment.n_numaux);
 	  bfd_fprintf_vma (abfd, file, val);
-	  fprintf (file, " %s", symbol->name);
+	  fprintf (file, " %s", symname);
 
 	  for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
 	    {
@@ -2297,7 +2298,8 @@ coff_print_symbol (bfd *abfd,
 
 	  if (l)
 	    {
-	      fprintf (file, "\n%s :", l->u.sym->name);
+	      fprintf (file, "\n%s :",
+		       l->u.sym->name ? l->u.sym->name : "<null>");
 	      l++;
 	      while (l->line_number)
 		{
@@ -2317,7 +2319,7 @@ coff_print_symbol (bfd *abfd,
 		   symbol->section->name,
 		   coffsymbol (symbol)->native ? "n" : "g",
 		   coffsymbol (symbol)->lineno ? "l" : " ",
-		   symbol->name);
+		   symname);
 	}
     }
 }
diff --git a/bfd/ecoff.c b/bfd/ecoff.c
index 5ee7ffaf489..93b93f39be1 100644
--- a/bfd/ecoff.c
+++ b/bfd/ecoff.c
@@ -1452,11 +1452,12 @@ _bfd_ecoff_print_symbol (bfd *abfd,
   const struct ecoff_debug_swap * const debug_swap
     = &ecoff_backend (abfd)->debug_swap;
   FILE *file = (FILE *)filep;
+  const char *symname = symbol->name ? symbol->name : "<null>";
 
   switch (how)
     {
     case bfd_print_symbol_name:
-      fprintf (file, "%s", symbol->name);
+      fprintf (file, "%s", symname);
       break;
     case bfd_print_symbol_more:
       if (ecoffsymbol (symbol)->local)
@@ -1526,7 +1527,7 @@ _bfd_ecoff_print_symbol (bfd *abfd,
 		 (unsigned) ecoff_ext.asym.sc,
 		 (unsigned) ecoff_ext.asym.index,
 		 jmptbl, cobol_main, weakext,
-		 symbol->name);
+		 symname);
 
 	if (ecoffsymbol (symbol)->fdr != NULL
 	    && ecoff_ext.asym.index != indexNil)
diff --git a/bfd/elf.c b/bfd/elf.c
index c882a66ab5c..7d3d2063130 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -549,9 +549,7 @@ bfd_elf_sym_name (bfd *abfd,
     }
 
   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
-  if (name == NULL)
-    name = "(null)";
-  else if (sym_sec && *name == '\0')
+  if (sym_sec && name && *name == '\0')
     name = bfd_section_name (sym_sec);
 
   return name;
@@ -2314,10 +2312,12 @@ bfd_elf_print_symbol (bfd *abfd,
 		      bfd_print_symbol_type how)
 {
   FILE *file = (FILE *) filep;
+  const char *symname = symbol->name ? symbol->name : "<null>";
+
   switch (how)
     {
     case bfd_print_symbol_name:
-      fprintf (file, "%s", symbol->name);
+      fprintf (file, "%s", symname);
       break;
     case bfd_print_symbol_more:
       fprintf (file, "elf ");
@@ -2340,11 +2340,10 @@ bfd_elf_print_symbol (bfd *abfd,
 	if (bed->elf_backend_print_symbol_all)
 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
 
-	if (name == NULL)
-	  {
-	    name = symbol->name;
-	    bfd_print_symbol_vandf (abfd, file, symbol);
-	  }
+	if (name != NULL)
+	  symname = name;
+	else
+	  bfd_print_symbol_vandf (abfd, file, symbol);
 
 	fprintf (file, " %s\t", section_name);
 	/* Print the "other" value for a symbol.  For common symbols,
@@ -2391,7 +2390,7 @@ bfd_elf_print_symbol (bfd *abfd,
 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
 	  }
 
-	fprintf (file, " %s", name);
+	fprintf (file, " %s", symname);
       }
       break;
     }
diff --git a/bfd/pef.c b/bfd/pef.c
index f330b92e821..324adb33d69 100644
--- a/bfd/pef.c
+++ b/bfd/pef.c
@@ -210,16 +210,17 @@ bfd_pef_print_symbol (bfd *abfd,
 		      bfd_print_symbol_type how)
 {
   FILE *file = (FILE *) afile;
+  const char *symname = symbol->name ? symbol->name : "<null>";
 
   switch (how)
     {
     case bfd_print_symbol_name:
-      fprintf (file, "%s", symbol->name);
+      fprintf (file, "%s", symname);
       break;
     default:
       bfd_print_symbol_vandf (abfd, (void *) file, symbol);
-      fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
-      if (startswith (symbol->name, "__traceback_"))
+      fprintf (file, " %-5s %s", symbol->section->name, symname);
+      if (startswith (symname, "__traceback_"))
 	{
 	  unsigned char *buf;
 	  size_t offset = symbol->value + 4;
diff --git a/bfd/syms.c b/bfd/syms.c
index b370a3375d9..816296b68a3 100644
--- a/bfd/syms.c
+++ b/bfd/syms.c
@@ -777,7 +777,7 @@ bfd_symbol_info (asymbol *symbol, symbol_info *ret)
   else
     ret->value = symbol->value + symbol->section->vma;
 
-  ret->name = symbol->name;
+  ret->name = symbol->name ? symbol->name : "<null>";
 }
 
 /*
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 7f9e3d2b119..343c4de53f4 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -4895,9 +4895,6 @@ ld_is_local_symbol (asymbol * sym)
   if (name == NULL || *name == 0)
     return false;
 
-  if (strcmp (name, "(null)") == 0)
-    return false;
-
   /* Skip .Lxxx and such like.  */
   if (bfd_is_local_label (link_info.output_bfd, sym))
     return false;


More information about the Binutils-cvs mailing list