Fix gdb.fortran "failed to extract expected results" errors
[binutils-gdb.git] / gdb / dbxread.c
index a6f44d5a5649288fad52f8f6c22c086353fb51aa..165040dd7ce4fd7ecc29204f03b0c5ff72daf382 100644 (file)
@@ -1,5 +1,5 @@
 /* Read dbx symbol tables and convert to internal format, for GDB.
-   Copyright (C) 1986-2021 Free Software Foundation, Inc.
+   Copyright (C) 1986-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    for real.  dbx_psymtab_to_symtab() is the function that does this */
 
 #include "defs.h"
-#if defined(__CYGNUSCLIB__)
-#include <sys/types.h>
-#include <fcntl.h>
-#endif
 
-#include "gdb_obstack.h"
+#include "gdbsupport/gdb_obstack.h"
 #include <sys/stat.h>
 #include "symtab.h"
 #include "breakpoint.h"
@@ -545,7 +541,9 @@ dbx_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
   /* Read stabs data from executable file and define symbols.  */
 
-  psymtab_storage *partial_symtabs = objfile->partial_symtabs.get ();
+  psymbol_functions *psf = new psymbol_functions ();
+  psymtab_storage *partial_symtabs = psf->get_partial_symtabs ().get ();
+  objfile->qf.emplace_front (psf);
   read_dbx_symtab (reader, partial_symtabs, objfile);
 
   /* Install any minimal symbols that have been collected as the current
@@ -1432,10 +1430,10 @@ read_dbx_symtab (minimal_symbol_reader &reader,
          if (!p)
            continue;           /* Not a debugging symbol.   */
 
-         sym_len = 0;
+         sym_len = 0;
          sym_name = NULL;      /* pacify "gcc -Werror" */
-         if (psymtab_language == language_cplus)
-           {
+         if (psymtab_language == language_cplus)
+           {
              std::string name (namestring, p - namestring);
              gdb::unique_xmalloc_ptr<char> new_name
                = cp_canonicalize_string (name.c_str ());
@@ -1447,11 +1445,11 @@ read_dbx_symtab (minimal_symbol_reader &reader,
                }
            }
 
-         if (sym_len == 0)
-           {
-             sym_name = namestring;
-             sym_len = p - namestring;
-           }
+         if (sym_len == 0)
+           {
+             sym_name = namestring;
+             sym_len = p - namestring;
+           }
 
          /* Main processing section for debugging symbols which
             the initial read through the symbol tables needs to worry
@@ -1463,23 +1461,33 @@ read_dbx_symtab (minimal_symbol_reader &reader,
          switch (p[1])
            {
            case 'S':
-             pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
-                               VAR_DOMAIN, LOC_STATIC,
-                               data_sect_index,
-                               psymbol_placement::STATIC,
-                               nlist.n_value, psymtab_language,
-                               partial_symtabs, objfile);
+             if (pst != nullptr)
+               pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
+                                 VAR_DOMAIN, LOC_STATIC,
+                                 data_sect_index,
+                                 psymbol_placement::STATIC,
+                                 nlist.n_value, psymtab_language,
+                                 partial_symtabs, objfile);
+             else
+               complaint (_("static `%*s' appears to be defined "
+                            "outside of all compilation units"),
+                          sym_len, sym_name);
              continue;
 
            case 'G':
              /* The addresses in these entries are reported to be
                 wrong.  See the code that reads 'G's for symtabs.  */
-             pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
-                               VAR_DOMAIN, LOC_STATIC,
-                               data_sect_index,
-                               psymbol_placement::GLOBAL,
-                               nlist.n_value, psymtab_language,
-                               partial_symtabs, objfile);
+             if (pst != nullptr)
+               pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
+                                 VAR_DOMAIN, LOC_STATIC,
+                                 data_sect_index,
+                                 psymbol_placement::GLOBAL,
+                                 nlist.n_value, psymtab_language,
+                                 partial_symtabs, objfile);
+             else
+               complaint (_("global `%*s' appears to be defined "
+                            "outside of all compilation units"),
+                          sym_len, sym_name);
              continue;
 
            case 'T':
@@ -1493,19 +1501,30 @@ read_dbx_symtab (minimal_symbol_reader &reader,
                  || (p == namestring + 1
                      && namestring[0] != ' '))
                {
-                 pst->add_psymbol (gdb::string_view (sym_name, sym_len),
-                                   true, STRUCT_DOMAIN, LOC_TYPEDEF, -1,
-                                   psymbol_placement::STATIC,
-                                   0, psymtab_language,
-                                   partial_symtabs, objfile);
+                 if (pst != nullptr)
+                   pst->add_psymbol (gdb::string_view (sym_name, sym_len),
+                                     true, STRUCT_DOMAIN, LOC_TYPEDEF, -1,
+                                     psymbol_placement::STATIC,
+                                     0, psymtab_language,
+                                     partial_symtabs, objfile);
+                 else
+                   complaint (_("enum, struct, or union `%*s' appears "
+                                "to be defined outside of all "
+                                "compilation units"),
+                              sym_len, sym_name);
                  if (p[2] == 't')
                    {
                      /* Also a typedef with the same name.  */
-                     pst->add_psymbol (gdb::string_view (sym_name, sym_len),
-                                       true, VAR_DOMAIN, LOC_TYPEDEF, -1,
-                                       psymbol_placement::STATIC,
-                                       0, psymtab_language,
-                                       partial_symtabs, objfile);
+                     if (pst != nullptr)
+                       pst->add_psymbol (gdb::string_view (sym_name, sym_len),
+                                         true, VAR_DOMAIN, LOC_TYPEDEF, -1,
+                                         psymbol_placement::STATIC,
+                                         0, psymtab_language,
+                                         partial_symtabs, objfile);
+                     else
+                       complaint (_("typedef `%*s' appears to be defined "
+                                    "outside of all compilation units"),
+                                  sym_len, sym_name);
                      p += 1;
                    }
                }
@@ -1514,11 +1533,16 @@ read_dbx_symtab (minimal_symbol_reader &reader,
            case 't':
              if (p != namestring)      /* a name is there, not just :T...  */
                {
-                 pst->add_psymbol (gdb::string_view (sym_name, sym_len),
-                                   true, VAR_DOMAIN, LOC_TYPEDEF, -1,
-                                   psymbol_placement::STATIC,
-                                   0, psymtab_language,
-                                   partial_symtabs, objfile);
+                 if (pst != nullptr)
+                   pst->add_psymbol (gdb::string_view (sym_name, sym_len),
+                                     true, VAR_DOMAIN, LOC_TYPEDEF, -1,
+                                     psymbol_placement::STATIC,
+                                     0, psymtab_language,
+                                     partial_symtabs, objfile);
+                 else
+                   complaint (_("typename `%*s' appears to be defined "
+                                "outside of all compilation units"),
+                              sym_len, sym_name);
                }
            check_enum:
              /* If this is an enumerated type, we need to
@@ -1576,11 +1600,16 @@ read_dbx_symtab (minimal_symbol_reader &reader,
                        ;
                      /* Note that the value doesn't matter for
                         enum constants in psymtabs, just in symtabs.  */
-                     pst->add_psymbol (gdb::string_view (p, q - p), true,
-                                       VAR_DOMAIN, LOC_CONST, -1,
-                                       psymbol_placement::STATIC, 0,
-                                       psymtab_language,
-                                       partial_symtabs, objfile);
+                     if (pst != nullptr)
+                       pst->add_psymbol (gdb::string_view (p, q - p), true,
+                                         VAR_DOMAIN, LOC_CONST, -1,
+                                         psymbol_placement::STATIC, 0,
+                                         psymtab_language,
+                                         partial_symtabs, objfile);
+                     else
+                       complaint (_("enum constant `%*s' appears to be defined "
+                                    "outside of all compilation units"),
+                                  ((int) (q - p)), p);
                      /* Point past the name.  */
                      p = q;
                      /* Skip over the value.  */
@@ -1595,23 +1624,24 @@ read_dbx_symtab (minimal_symbol_reader &reader,
 
            case 'c':
              /* Constant, e.g. from "const" in Pascal.  */
-             pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
-                               VAR_DOMAIN, LOC_CONST, -1,
-                               psymbol_placement::STATIC, 0,
-                               psymtab_language,
-                               partial_symtabs, objfile);
+             if (pst != nullptr)
+               pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
+                                 VAR_DOMAIN, LOC_CONST, -1,
+                                 psymbol_placement::STATIC, 0,
+                                 psymtab_language,
+                                 partial_symtabs, objfile);
+             else
+               complaint (_("constant `%*s' appears to be defined "
+                            "outside of all compilation units"),
+                          sym_len, sym_name);
+
              continue;
 
            case 'f':
              if (! pst)
                {
-                 int name_len = p - namestring;
-                 char *name = (char *) xmalloc (name_len + 1);
-
-                 memcpy (name, namestring, name_len);
-                 name[name_len] = '\0';
-                 function_outside_compilation_unit_complaint (name);
-                 xfree (name);
+                 std::string name (namestring, (p - namestring));
+                 function_outside_compilation_unit_complaint (name.c_str ());
                }
              /* Kludges for ELF/STABS with Sun ACC.  */
              last_function_name = namestring;
@@ -1651,12 +1681,13 @@ read_dbx_symtab (minimal_symbol_reader &reader,
                  pst->set_text_low (nlist.n_value);
                  textlow_not_set = 0;
                }
-             pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
-                               VAR_DOMAIN, LOC_BLOCK,
-                               SECT_OFF_TEXT (objfile),
-                               psymbol_placement::STATIC,
-                               nlist.n_value, psymtab_language,
-                               partial_symtabs, objfile);
+             if (pst != nullptr)
+               pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
+                                 VAR_DOMAIN, LOC_BLOCK,
+                                 SECT_OFF_TEXT (objfile),
+                                 psymbol_placement::STATIC,
+                                 nlist.n_value, psymtab_language,
+                                 partial_symtabs, objfile);
              continue;
 
              /* Global functions were ignored here, but now they
@@ -1665,13 +1696,8 @@ read_dbx_symtab (minimal_symbol_reader &reader,
            case 'F':
              if (! pst)
                {
-                 int name_len = p - namestring;
-                 char *name = (char *) xmalloc (name_len + 1);
-
-                 memcpy (name, namestring, name_len);
-                 name[name_len] = '\0';
-                 function_outside_compilation_unit_complaint (name);
-                 xfree (name);
+                 std::string name (namestring, (p - namestring));
+                 function_outside_compilation_unit_complaint (name.c_str ());
                }
              /* Kludges for ELF/STABS with Sun ACC.  */
              last_function_name = namestring;
@@ -1711,12 +1737,13 @@ read_dbx_symtab (minimal_symbol_reader &reader,
                  pst->set_text_low (nlist.n_value);
                  textlow_not_set = 0;
                }
-             pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
-                               VAR_DOMAIN, LOC_BLOCK,
-                               SECT_OFF_TEXT (objfile),
-                               psymbol_placement::GLOBAL,
-                               nlist.n_value, psymtab_language,
-                               partial_symtabs, objfile);
+             if (pst != nullptr)
+               pst->add_psymbol (gdb::string_view (sym_name, sym_len), true,
+                                 VAR_DOMAIN, LOC_BLOCK,
+                                 SECT_OFF_TEXT (objfile),
+                                 psymbol_placement::GLOBAL,
+                                 nlist.n_value, psymtab_language,
+                                 partial_symtabs, objfile);
              continue;
 
              /* Two things show up here (hopefully); static symbols of
@@ -1915,7 +1942,7 @@ start_psymtab (psymtab_storage *partial_symtabs, struct objfile *objfile,
               const char *filename, CORE_ADDR textlow, int ldsymoff)
 {
   legacy_psymtab *result = new legacy_psymtab (filename, partial_symtabs,
-                                              objfile, textlow);
+                                              objfile->per_bfd, textlow);
 
   result->read_symtab_private =
     XOBNEW (&objfile->objfile_obstack, struct symloc);
@@ -2038,7 +2065,7 @@ dbx_end_psymtab (struct objfile *objfile, psymtab_storage *partial_symtabs,
   for (i = 0; i < num_includes; i++)
     {
       legacy_psymtab *subpst =
-       new legacy_psymtab (include_list[i], partial_symtabs, objfile);
+       new legacy_psymtab (include_list[i], partial_symtabs, objfile->per_bfd);
 
       subpst->read_symtab_private =
        XOBNEW (&objfile->objfile_obstack, struct symloc);
@@ -2398,10 +2425,10 @@ process_one_symbol (int type, int desc, CORE_ADDR valu, const char *name,
          struct block *block;
 
          if (outermost_context_p ())
-           {
+           {
              lbrac_mismatch_complaint (symnum);
-             break;
-           }
+             break;
+           }
 
          /* The following check is added before recording line 0 at
             end of function so as to handle hand-generated stabs