]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Don't return "(null)" from bfd_elf_sym_name
authorAlan Modra <amodra@gmail.com>
Tue, 1 Oct 2024 04:38:08 +0000 (14:08 +0930)
committerAlan Modra <amodra@gmail.com>
Thu, 3 Oct 2024 03:01:45 +0000 (12:31 +0930)
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.

bfd/coffgen.c
bfd/ecoff.c
bfd/elf.c
bfd/pef.c
bfd/syms.c
ld/ldlang.c

index cc1c655738b3bb990ee879a5044aae7900e82bf8..ff382a7e9c9f1bc0a1839f1b66732218e558c9d3 100644 (file)
@@ -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);
        }
     }
 }
index 5ee7ffaf4898c7c562590ca9fa6ea5d9de56da30..93b93f39be1304190926c15f58dbe8616a5b41bb 100644 (file)
@@ -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)
index c882a66ab5c2a8050c1f79e863c9dac4b55a2fff..7d3d2063130bd3d5bd988207ca68b15f76ac85c8 100644 (file)
--- 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;
     }
index f330b92e82103e15cee3c77805400f99ab7b71a8..324adb33d695c32a048d5fc346375a19553db984 100644 (file)
--- 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;
index b370a3375d91591d51e50175a4be67ff38a504ae..816296b68a3a702d6d54216621c74cb6bcb1b58a 100644 (file)
@@ -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>";
 }
 
 /*
index 7f9e3d2b119b57015149dbbc15a7167ac6e36af1..343c4de53f48fbe2ffa3e86a0cd50aa19cba9e65 100644 (file)
@@ -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;