]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/buildsym.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / buildsym.c
index fae3d5ef4ce0459da4f25feb4b35e50c1ae57f4a..a963219a0d29e9ec5259370b542451ef4f4fae96 100644 (file)
@@ -1,5 +1,5 @@
 /* Support routines for building symbol tables in GDB's internal format.
-   Copyright (C) 1986-2022 Free Software Foundation, Inc.
+   Copyright (C) 1986-2024 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "objfiles.h"
 #include "gdbtypes.h"
 #include "complaints.h"
-#include "expression.h"                /* For "enum exp_opcode" used by...  */
-#include "filenames.h"         /* For DOSish file names.  */
+#include "expression.h"
+#include "filenames.h"
 #include "macrotab.h"
-#include "demangle.h"          /* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
+#include "demangle.h"
 #include "block.h"
 #include "cp-support.h"
 #include "dictionary.h"
@@ -52,6 +52,7 @@ struct pending_block
 buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
                                      const char *name,
                                      const char *comp_dir_,
+                                     const char *name_for_id,
                                      enum language language_,
                                      CORE_ADDR last_addr)
   : m_objfile (objfile_),
@@ -70,7 +71,7 @@ buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
      It can happen that the debug info provides a different path to NAME than
      DIRNAME,NAME.  We cope with this in watch_main_source_file_lossage but
      that only works if the main_subfile doesn't have a symtab yet.  */
-  start_subfile (name);
+  start_subfile (name, name_for_id);
   /* Save this so that we don't have to go looking for it at the end
      of the subfiles list.  */
   m_main_subfile = m_current_subfile;
@@ -210,9 +211,10 @@ buildsym_compunit::finish_block_internal
   struct pending_block *pblock;
   struct pending_block *opblock;
 
-  block = (is_global
-          ? allocate_global_block (&m_objfile->objfile_obstack)
-          : allocate_block (&m_objfile->objfile_obstack));
+  if (is_global)
+    block = new (&m_objfile->objfile_obstack) global_block;
+  else
+    block = new (&m_objfile->objfile_obstack) struct block;
 
   if (symbol)
     {
@@ -242,8 +244,8 @@ buildsym_compunit::finish_block_internal
   if (symbol)
     {
       struct type *ftype = symbol->type ();
-      struct mdict_iterator miter;
       symbol->set_value_block (block);
+      symbol->set_section_index (SECT_OFF_TEXT (m_objfile));
       block->set_function (symbol);
 
       if (ftype->num_fields () <= 0)
@@ -252,26 +254,22 @@ buildsym_compunit::finish_block_internal
             function's type.  Set that from the type of the
             parameter symbols.  */
          int nparams = 0, iparams;
-         struct symbol *sym;
 
          /* Here we want to directly access the dictionary, because
             we haven't fully initialized the block yet.  */
-         ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
+         for (struct symbol *sym : block->multidict_symbols ())
            {
              if (sym->is_argument ())
                nparams++;
            }
          if (nparams > 0)
            {
-             ftype->set_num_fields (nparams);
-             ftype->set_fields
-               ((struct field *)
-                TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
+             ftype->alloc_fields (nparams);
 
              iparams = 0;
              /* Here we want to directly access the dictionary, because
                 we haven't fully initialized the block yet.  */
-             ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
+             for (struct symbol *sym : block->multidict_symbols ())
                {
                  if (iparams == nparams)
                    break;
@@ -279,7 +277,7 @@ buildsym_compunit::finish_block_internal
                  if (sym->is_argument ())
                    {
                      ftype->field (iparams).set_type (sym->type ());
-                     TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
+                     ftype->field (iparams).set_is_artificial (false);
                      iparams++;
                    }
                }
@@ -370,11 +368,10 @@ buildsym_compunit::finish_block_internal
       opblock = pblock;
     }
 
-  block_set_using (block,
-                  (is_global
-                   ? m_global_using_directives
-                   : m_local_using_directives),
-                  &m_objfile->objfile_obstack);
+  block->set_using ((is_global
+                    ? m_global_using_directives
+                    : m_local_using_directives),
+                   &m_objfile->objfile_obstack);
   if (is_global)
     m_global_using_directives = NULL;
   else
@@ -483,49 +480,30 @@ buildsym_compunit::make_blockvector ()
 
   return (blockvector);
 }
-\f
-/* Start recording information about source code that came from an
-   included (or otherwise merged-in) source file with a different
-   name.  NAME is the name of the file (cannot be NULL).  */
+
+/* See buildsym.h.  */
 
 void
-buildsym_compunit::start_subfile (const char *name)
+buildsym_compunit::start_subfile (const char *name, const char *name_for_id)
 {
   /* See if this subfile is already registered.  */
 
-  symtab_create_debug_printf ("name = %s", name);
+  symtab_create_debug_printf ("name = %s, name_for_id = %s", name, name_for_id);
 
   for (subfile *subfile = m_subfiles; subfile; subfile = subfile->next)
-    {
-      std::string subfile_name_holder;
-      const char *subfile_name;
-
-      /* If NAME is an absolute path, and this subfile is not, then
-        attempt to create an absolute path to compare.  */
-      if (IS_ABSOLUTE_PATH (name)
-         && !IS_ABSOLUTE_PATH (subfile->name)
-         && !m_comp_dir.empty ())
-       {
-         subfile_name_holder = path_join (m_comp_dir.c_str (),
-                                          subfile->name.c_str ());
-         subfile_name = subfile_name_holder.c_str ();
-       }
-      else
-       subfile_name = subfile->name.c_str ();
-
-      if (FILENAME_CMP (subfile_name, name) == 0)
-       {
-         symtab_create_debug_printf ("found existing symtab with name %s (%s)",
-                                     subfile->name.c_str (), subfile_name);
-         m_current_subfile = subfile;
-         return;
-       }
-    }
+    if (FILENAME_CMP (subfile->name_for_id.c_str (), name_for_id) == 0)
+      {
+       symtab_create_debug_printf ("found existing symtab with name_for_id %s",
+                                   subfile->name_for_id.c_str ());
+       m_current_subfile = subfile;
+       return;
+      }
 
   /* This subfile is not known.  Add an entry for it.  */
 
   subfile_up subfile (new struct subfile);
   subfile->name = name;
+  subfile->name_for_id = name_for_id;
 
   m_current_subfile = subfile.get ();
 
@@ -595,6 +573,7 @@ buildsym_compunit::patch_subfile_names (struct subfile *subfile,
     {
       m_comp_dir = std::move (subfile->name);
       subfile->name = name;
+      subfile->name_for_id = name;
       set_last_source_file (name);
 
       /* Default the source language to whatever can be deduced from
@@ -645,7 +624,7 @@ buildsym_compunit::pop_subfile ()
 
 void
 buildsym_compunit::record_line (struct subfile *subfile, int line,
-                               CORE_ADDR pc, linetable_entry_flags flags)
+                               unrelocated_addr pc, linetable_entry_flags flags)
 {
   m_have_line_numbers = true;
 
@@ -664,14 +643,14 @@ buildsym_compunit::record_line (struct subfile *subfile, int line,
      anyway.  */
   if (line == 0)
     {
-      gdb::optional<int> last_line;
+      std::optional<int> last_line;
 
       while (!subfile->line_vector_entries.empty ())
        {
          linetable_entry *last = &subfile->line_vector_entries.back ();
          last_line = last->line;
 
-         if (last->pc != pc)
+         if (last->unrelocated_pc () != pc)
            break;
 
          subfile->line_vector_entries.pop_back ();
@@ -686,8 +665,9 @@ buildsym_compunit::record_line (struct subfile *subfile, int line,
   linetable_entry &e = subfile->line_vector_entries.back ();
   e.line = line;
   e.is_stmt = (flags & LEF_IS_STMT) != 0;
-  e.pc = pc;
+  e.set_unrelocated_pc (pc);
   e.prologue_end = (flags & LEF_PROLOGUE_END) != 0;
+  e.epilogue_begin = (flags & LEF_EPILOGUE_BEGIN) != 0;
 }
 
 \f
@@ -805,10 +785,9 @@ buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr,
        }
     }
 
-  /* Reordered executables may have out of order pending blocks; if
-     OBJF_REORDERED is true, then sort the pending blocks.  */
-
-  if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks)
+  /* Executables may have out of order pending blocks; sort the
+     pending blocks.  */
+  if (m_pending_blocks != nullptr)
     {
       struct pending_block *pb;
 
@@ -871,7 +850,7 @@ buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr,
 
 struct compunit_symtab *
 buildsym_compunit::end_compunit_symtab_with_blockvector
-  (struct block *static_block, int section, int expandable)
+  (struct block *static_block, int expandable)
 {
   struct compunit_symtab *cu = m_compunit_symtab;
   struct blockvector *blockvector;
@@ -908,31 +887,19 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
     {
       if (!subfile->line_vector_entries.empty ())
        {
-         const auto lte_is_less_than
-           = [] (const linetable_entry &ln1,
-                 const linetable_entry &ln2) -> bool
-             {
-               if (ln1.pc == ln2.pc
-                   && ((ln1.line == 0) != (ln2.line == 0)))
-                 return ln1.line == 0;
-
-               return (ln1.pc < ln2.pc);
-             };
-
-         /* Like the pending blocks, the line table may be scrambled in
-            reordered executables.  Sort it if OBJF_REORDERED is true.  It
-            is important to preserve the order of lines at the same
-            address, as this maintains the inline function caller/callee
+         /* Like the pending blocks, the line table may be scrambled
+            in reordered executables.  Sort it.  It is important to
+            preserve the order of lines at the same address, as this
+            maintains the inline function caller/callee
             relationships, this is why std::stable_sort is used.  */
-         if (m_objfile->flags & OBJF_REORDERED)
-           std::stable_sort (subfile->line_vector_entries.begin (),
-                             subfile->line_vector_entries.end (),
-                             lte_is_less_than);
+         std::stable_sort (subfile->line_vector_entries.begin (),
+                           subfile->line_vector_entries.end ());
        }
 
       /* Allocate a symbol table if necessary.  */
       if (subfile->symtab == NULL)
-       subfile->symtab = allocate_symtab (cu, subfile->name.c_str ());
+       subfile->symtab = allocate_symtab (cu, subfile->name.c_str (),
+                                          subfile->name_for_id.c_str ());
 
       struct symtab *symtab = subfile->symtab;
 
@@ -945,13 +912,15 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
          size_t entry_array_size = n_entries * sizeof (struct linetable_entry);
          int linetablesize = sizeof (struct linetable) + entry_array_size;
 
-         symtab->set_linetable
-           (XOBNEWVAR (&m_objfile->objfile_obstack, struct linetable,
-                       linetablesize));
+         struct linetable *new_table
+           XOBNEWVAR (&m_objfile->objfile_obstack, struct linetable,
+                        linetablesize);
 
-         symtab->linetable ()->nitems = n_entries;
-         memcpy (symtab->linetable ()->item,
+         new_table->nitems = n_entries;
+         memcpy (new_table->item,
                  subfile->line_vector_entries.data (), entry_array_size);
+
+         symtab->set_linetable (new_table);
        }
       else
        symtab->set_linetable (nullptr);
@@ -987,11 +956,9 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
   {
     struct block *b = blockvector->global_block ();
 
-    set_block_compunit_symtab (b, cu);
+    b->set_compunit_symtab (cu);
   }
 
-  cu->set_block_line_section (section);
-
   cu->set_macro_table (release_macros ());
 
   /* Default any symbols without a specified symtab to the primary symtab.  */
@@ -1004,8 +971,6 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
     for (block_i = 0; block_i < blockvector->num_blocks (); block_i++)
       {
        struct block *block = blockvector->block (block_i);
-       struct symbol *sym;
-       struct mdict_iterator miter;
 
        /* Inlined functions may have symbols not in the global or
           static symbol lists.  */
@@ -1014,9 +979,10 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
            block->function ()->set_symtab (symtab);
 
        /* Note that we only want to fix up symbols from the local
-          blocks, not blocks coming from included symtabs.  That is why
-          we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
-       ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
+          blocks, not blocks coming from included symtabs.  That is
+          why we use an mdict iterator here and not a block
+          iterator.  */
+       for (struct symbol *sym : block->multidict_symbols ())
          if (sym->symtab () == NULL)
            sym->set_symtab (symtab);
       }
@@ -1030,15 +996,12 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
 /* Implementation of the second part of end_compunit_symtab.  Pass STATIC_BLOCK
    as value returned by end_compunit_symtab_get_static_block.
 
-   SECTION is the same as for end_compunit_symtab: the section number
-   (in objfile->section_offsets) of the blockvector and linetable.
-
    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
    expandable.  */
 
 struct compunit_symtab *
 buildsym_compunit::end_compunit_symtab_from_static_block
-  (struct block *static_block, int section, int expandable)
+  (struct block *static_block, int expandable)
 {
   struct compunit_symtab *cu;
 
@@ -1056,7 +1019,7 @@ buildsym_compunit::end_compunit_symtab_from_static_block
       cu = NULL;
     }
   else
-    cu = end_compunit_symtab_with_blockvector (static_block, section, expandable);
+    cu = end_compunit_symtab_with_blockvector (static_block, expandable);
 
   return cu;
 }
@@ -1066,9 +1029,7 @@ buildsym_compunit::end_compunit_symtab_from_static_block
    them), then make the struct symtab for that file and put it in the
    list of all such.
 
-   END_ADDR is the address of the end of the file's text.  SECTION is
-   the section number (in objfile->section_offsets) of the blockvector
-   and linetable.
+   END_ADDR is the address of the end of the file's text.
 
    Note that it is possible for end_compunit_symtab() to return NULL.  In
    particular, for the DWARF case at least, it will return NULL when
@@ -1083,24 +1044,24 @@ buildsym_compunit::end_compunit_symtab_from_static_block
    end_compunit_symtab_from_static_block yourself.  */
 
 struct compunit_symtab *
-buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr, int section)
+buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr)
 {
   struct block *static_block;
 
   static_block = end_compunit_symtab_get_static_block (end_addr, 0, 0);
-  return end_compunit_symtab_from_static_block (static_block, section, 0);
+  return end_compunit_symtab_from_static_block (static_block, 0);
 }
 
 /* Same as end_compunit_symtab except create a symtab that can be later added
    to.  */
 
 struct compunit_symtab *
-buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
+buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr)
 {
   struct block *static_block;
 
   static_block = end_compunit_symtab_get_static_block (end_addr, 1, 0);
-  return end_compunit_symtab_from_static_block (static_block, section, 1);
+  return end_compunit_symtab_from_static_block (static_block, 1);
 }
 
 /* Subroutine of augment_type_symtab to simplify it.