Remove unneeded AUX register symbols.
[deliverable/binutils-gdb.git] / gdb / buildsym.c
index 4aeb6ac826090dd156f48fe178e37d1621d14945..56be8ac929ec9c70ed9c4e277076a2bc1791293e 100644 (file)
@@ -1,5 +1,5 @@
 /* Support routines for building symbol tables in GDB's internal format.
-   Copyright (C) 1986-2014 Free Software Foundation, Inc.
+   Copyright (C) 1986-2016 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    file-reading routines.
 
    Routines to support specific debugging information formats (stabs,
-   DWARF, etc) belong somewhere else.  */
+   DWARF, etc) belong somewhere else.
+
+   The basic way this module is used is as follows:
+
+   buildsym_init ();
+   cleanups = make_cleanup (really_free_pendings, NULL);
+   cust = start_symtab (...);
+   ... read debug info ...
+   cust = end_symtab (...);
+   do_cleanups (cleanups);
+
+   The compunit symtab pointer ("cust") is returned from both start_symtab
+   and end_symtab to simplify the debug info readers.
+
+   There are minor variations on this, e.g., dwarf2read.c splits end_symtab
+   into two calls: end_symtab_get_static_block, end_symtab_from_static_block,
+   but all debug info readers follow this basic flow.
+
+   Reading DWARF Type Units is another variation:
+
+   buildsym_init ();
+   cleanups = make_cleanup (really_free_pendings, NULL);
+   cust = start_symtab (...);
+   ... read debug info ...
+   cust = end_expandable_symtab (...);
+   do_cleanups (cleanups);
+
+   And then reading subsequent Type Units within the containing "Comp Unit"
+   will use a second flow:
+
+   buildsym_init ();
+   cleanups = make_cleanup (really_free_pendings, NULL);
+   cust = restart_symtab (...);
+   ... read debug info ...
+   cust = augment_type_symtab (...);
+   do_cleanups (cleanups);
+
+   dbxread.c and xcoffread.c use another variation:
+
+   buildsym_init ();
+   cleanups = make_cleanup (really_free_pendings, NULL);
+   cust = start_symtab (...);
+   ... read debug info ...
+   cust = end_symtab (...);
+   ... start_symtab + read + end_symtab repeated ...
+   do_cleanups (cleanups);
+*/
 
 #include "defs.h"
 #include "bfd.h"
@@ -146,6 +192,8 @@ static struct subfile_stack *subfile_stack;
    currently reading.  */
 static struct macro_table *pending_macros;
 
+static void free_buildsym_compunit (void);
+
 static int compare_line_numbers (const void *ln1p, const void *ln2p);
 
 static void record_pending_block (struct objfile *objfile,
@@ -184,7 +232,7 @@ add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
        }
       else
        {
-         link = (struct pending *) xmalloc (sizeof (struct pending));
+         link = XNEW (struct pending);
        }
 
       link->next = *listhead;
@@ -220,8 +268,12 @@ find_symbol_in_list (struct pending *list, char *name, int length)
   return (NULL);
 }
 
-/* At end of reading syms, or in case of quit, really free as many
-   `struct pending's as we can easily find.  */
+/* At end of reading syms, or in case of quit, ensure everything associated
+   with building symtabs is freed.  This is intended to be registered as a
+   cleanup before doing psymtab->symtab expansion.
+
+   N.B. This is *not* intended to be used when building psymtabs.  Some debug
+   info readers call this anyway, which is harmless if confusing.  */
 
 void
 really_free_pendings (void *dummy)
@@ -253,12 +305,13 @@ really_free_pendings (void *dummy)
 
   if (pending_macros)
     free_macro_table (pending_macros);
+  pending_macros = NULL;
 
   if (pending_addrmap)
-    {
-      obstack_free (&pending_addrmap_obstack, NULL);
-      pending_addrmap = NULL;
-    }
+    obstack_free (&pending_addrmap_obstack, NULL);
+  pending_addrmap = NULL;
+
+  free_buildsym_compunit ();
 }
 
 /* This function is called to discard any pending blocks.  */
@@ -278,8 +331,10 @@ free_pending_blocks (void)
    file).  Put the block on the list of pending blocks.  */
 
 static struct block *
-finish_block_internal (struct symbol *symbol, struct pending **listhead,
+finish_block_internal (struct symbol *symbol,
+                      struct pending **listhead,
                       struct pending_block *old_blocks,
+                      const struct dynamic_prop *static_link,
                       CORE_ADDR start, CORE_ADDR end,
                       int is_global, int expandable)
 {
@@ -369,6 +424,9 @@ finish_block_internal (struct symbol *symbol, struct pending **listhead,
       BLOCK_FUNCTION (block) = NULL;
     }
 
+  if (static_link != NULL)
+    objfile_register_static_link (objfile, block, static_link);
+
   /* Now "free" the links of the list, and empty the list.  */
 
   for (next = *listhead; next; next = next1)
@@ -450,8 +508,15 @@ finish_block_internal (struct symbol *symbol, struct pending **listhead,
       opblock = pblock;
     }
 
-  block_set_using (block, using_directives, &objfile->objfile_obstack);
-  using_directives = NULL;
+  block_set_using (block,
+                  (is_global
+                   ? global_using_directives
+                   : local_using_directives),
+                  &objfile->objfile_obstack);
+  if (is_global)
+    global_using_directives = NULL;
+  else
+    local_using_directives = NULL;
 
   record_pending_block (objfile, block, opblock);
 
@@ -459,11 +524,13 @@ finish_block_internal (struct symbol *symbol, struct pending **listhead,
 }
 
 struct block *
-finish_block (struct symbol *symbol, struct pending **listhead,
+finish_block (struct symbol *symbol,
+             struct pending **listhead,
              struct pending_block *old_blocks,
+             const struct dynamic_prop *static_link,
              CORE_ADDR start, CORE_ADDR end)
 {
-  return finish_block_internal (symbol, listhead, old_blocks,
+  return finish_block_internal (symbol, listhead, old_blocks, static_link,
                                start, end, 0, 0);
 }
 
@@ -483,8 +550,7 @@ record_pending_block (struct objfile *objfile, struct block *block,
   if (pending_blocks == NULL)
     obstack_init (&pending_block_obstack);
 
-  pblock = (struct pending_block *)
-    obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
+  pblock = XOBNEW (&pending_block_obstack, struct pending_block);
   pblock->block = block;
   if (opblock)
     {
@@ -638,7 +704,7 @@ start_subfile (const char *name)
 
   /* This subfile is not known.  Add an entry for it.  */
 
-  subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
+  subfile = XNEW (struct subfile);
   memset (subfile, 0, sizeof (struct subfile));
   subfile->buildsym_compunit = buildsym_compunit;
 
@@ -706,8 +772,7 @@ start_buildsym_compunit (struct objfile *objfile, const char *comp_dir)
 {
   struct buildsym_compunit *bscu;
 
-  bscu = (struct buildsym_compunit *)
-    xmalloc (sizeof (struct buildsym_compunit));
+  bscu = XNEW (struct buildsym_compunit);
   memset (bscu, 0, sizeof (struct buildsym_compunit));
 
   bscu->objfile = objfile;
@@ -744,6 +809,7 @@ free_buildsym_compunit (void)
   xfree (buildsym_compunit->comp_dir);
   xfree (buildsym_compunit);
   buildsym_compunit = NULL;
+  current_subfile = NULL;
 }
 
 /* For stabs readers, the first N_SO symbol is assumed to be the
@@ -798,8 +864,7 @@ patch_subfile_names (struct subfile *subfile, char *name)
 void
 push_subfile (void)
 {
-  struct subfile_stack *tem
-    = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
+  struct subfile_stack *tem = XNEW (struct subfile_stack);
 
   tem->next = subfile_stack;
   subfile_stack = tem;
@@ -943,6 +1008,34 @@ get_macro_table (void)
   return pending_macros;
 }
 \f
+/* Init state to prepare for building a symtab.
+   Note: This can't be done in buildsym_init because dbxread.c and xcoffread.c
+   can call start_symtab+end_symtab multiple times after one call to
+   buildsym_init.  */
+
+static void
+prepare_for_building (const char *name, CORE_ADDR start_addr)
+{
+  set_last_source_file (name);
+  last_source_start_addr = start_addr;
+
+  local_symbols = NULL;
+  local_using_directives = NULL;
+  within_function = 0;
+  have_line_numbers = 0;
+
+  context_stack_depth = 0;
+
+  /* These should have been reset either by successful completion of building
+     a symtab, or by the really_free_pendings cleanup.  */
+  gdb_assert (file_symbols == NULL);
+  gdb_assert (global_symbols == NULL);
+  gdb_assert (global_using_directives == NULL);
+  gdb_assert (pending_macros == NULL);
+  gdb_assert (pending_addrmap == NULL);
+  gdb_assert (current_subfile == NULL);
+}
+
 /* Start a new symtab for a new source file in OBJFILE.  Called, for example,
    when a stabs symbol of type N_SO is seen, or when a DWARF
    TAG_compile_unit DIE is seen.  It indicates the start of data for
@@ -956,11 +1049,11 @@ struct compunit_symtab *
 start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
              CORE_ADDR start_addr)
 {
-  restart_symtab (start_addr);
+  prepare_for_building (name, start_addr);
 
   buildsym_compunit = start_buildsym_compunit (objfile, comp_dir);
 
-  /* Allocate the primary symtab now.  The caller needs it to allocate
+  /* Allocate the compunit symtab now.  The caller needs it to allocate
      non-primary symtabs.  It is also needed by get_macro_table.  */
   buildsym_compunit->compunit_symtab = allocate_compunit_symtab (objfile,
                                                                 name);
@@ -976,43 +1069,27 @@ start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
      of the subfiles list.  */
   buildsym_compunit->main_subfile = current_subfile;
 
-  set_last_source_file (name);
-
   return buildsym_compunit->compunit_symtab;
 }
 
 /* Restart compilation for a symtab.
+   CUST is the result of end_expandable_symtab.
+   NAME, START_ADDR are the source file we are resuming with.
+
    This is used when a symtab is built from multiple sources.
-   The symtab is first built with start_symtab and then for each additional
-   piece call restart_symtab.  */
+   The symtab is first built with start_symtab/end_expandable_symtab
+   and then for each additional piece call restart_symtab/augment_*_symtab.
+   Note: At the moment there is only augment_type_symtab.  */
 
 void
-restart_symtab (CORE_ADDR start_addr)
+restart_symtab (struct compunit_symtab *cust,
+               const char *name, CORE_ADDR start_addr)
 {
-  set_last_source_file (NULL);
-  last_source_start_addr = start_addr;
-  file_symbols = NULL;
-  global_symbols = NULL;
-  within_function = 0;
-  have_line_numbers = 0;
-
-  /* Context stack is initially empty.  Allocate first one with room
-     for 10 levels; reuse it forever afterward.  */
-  if (context_stack == NULL)
-    {
-      context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
-      context_stack = (struct context_stack *)
-       xmalloc (context_stack_size * sizeof (struct context_stack));
-    }
-  context_stack_depth = 0;
-
-  /* We shouldn't have any address map at this point.  */
-  gdb_assert (! pending_addrmap);
+  prepare_for_building (name, start_addr);
 
-  /* Reset the sub source files list.  The list should already be empty,
-     but free it anyway in case some code didn't finish cleaning up after
-     an error.  */
-  free_buildsym_compunit ();
+  buildsym_compunit = start_buildsym_compunit (COMPUNIT_OBJFILE (cust),
+                                              COMPUNIT_DIRNAME (cust));
+  buildsym_compunit->compunit_symtab = cust;
 }
 
 /* Subroutine of end_symtab to simplify it.  Look for a subfile that
@@ -1101,19 +1178,32 @@ block_compar (const void *ap, const void *bp)
          - (BLOCK_START (b) < BLOCK_START (a)));
 }
 
-/* Reset globals used to build symtabs.  */
+/* Reset state after a successful building of a symtab.
+   This exists because dbxread.c and xcoffread.c can call
+   start_symtab+end_symtab multiple times after one call to buildsym_init,
+   and before the really_free_pendings cleanup is called.
+   We keep the free_pendings list around for dbx/xcoff sake.  */
 
 static void
 reset_symtab_globals (void)
 {
   set_last_source_file (NULL);
-  free_buildsym_compunit ();
+
+  local_symbols = NULL;
+  local_using_directives = NULL;
+  file_symbols = NULL;
+  global_symbols = NULL;
+  global_using_directives = NULL;
+
+  /* We don't free pending_macros here because if the symtab was successfully
+     built then ownership was transferred to the symtab.  */
   pending_macros = NULL;
+
   if (pending_addrmap)
-    {
-      obstack_free (&pending_addrmap_obstack, NULL);
-      pending_addrmap = NULL;
-    }
+    obstack_free (&pending_addrmap_obstack, NULL);
+  pending_addrmap = NULL;
+
+  free_buildsym_compunit ();
 }
 
 /* Implementation of the first part of end_symtab.  It allows modifying
@@ -1143,7 +1233,7 @@ end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
       struct context_stack *cstk = pop_context ();
 
       /* Make a block for the local symbols within.  */
-      finish_block (cstk->name, &local_symbols, cstk->old_blocks,
+      finish_block (cstk->name, &local_symbols, cstk->old_blocks, NULL,
                    cstk->start_addr, end_addr);
 
       if (context_stack_depth > 0)
@@ -1172,7 +1262,7 @@ end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
       for (pb = pending_blocks; pb != NULL; pb = pb->next)
        count++;
 
-      barray = xmalloc (sizeof (*barray) * count);
+      barray = XNEWVEC (struct block *, count);
       back_to = make_cleanup (xfree, barray);
 
       bp = barray;
@@ -1206,7 +1296,8 @@ end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
       && file_symbols == NULL
       && global_symbols == NULL
       && have_line_numbers == 0
-      && pending_macros == NULL)
+      && pending_macros == NULL
+      && global_using_directives == NULL)
     {
       /* Ignore symtabs that have no functions with real debugging info.  */
       return NULL;
@@ -1214,29 +1305,12 @@ end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
   else
     {
       /* Define the STATIC_BLOCK.  */
-      return finish_block_internal (NULL, &file_symbols, NULL,
+      return finish_block_internal (NULL, &file_symbols, NULL, NULL,
                                    last_source_start_addr, end_addr,
                                    0, expandable);
     }
 }
 
-/* Subroutine of end_symtab_from_static_block to simplify it.
-   Handle the "no blockvector" case.
-   When this happens there is nothing to record, so just free up
-   any memory we allocated while reading debug info.  */
-
-static void
-end_symtab_without_blockvector (void)
-{
-  /* Free up all the subfiles.
-     We won't be adding a compunit to the objfile's list of compunits,
-     so there's nothing to unchain.  However, since each symtab
-     is added to the objfile's obstack we can't free that space.
-     We could do better, but this is believed to be a sufficiently rare
-     event.  */
-  free_buildsym_compunit ();
-}
-
 /* Subroutine of end_symtab_from_static_block to simplify it.
    Handle the "have blockvector" case.
    See end_symtab_from_static_block for a description of the arguments.  */
@@ -1259,7 +1333,7 @@ end_symtab_with_blockvector (struct block *static_block,
   end_addr = BLOCK_END (static_block);
 
   /* Create the GLOBAL_BLOCK and build the blockvector.  */
-  finish_block_internal (NULL, &global_symbols, NULL,
+  finish_block_internal (NULL, &global_symbols, NULL, NULL,
                         last_source_start_addr, end_addr,
                         1, expandable);
   blockvector = make_blockvector ();
@@ -1349,15 +1423,15 @@ end_symtab_with_blockvector (struct block *static_block,
     gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
   }
 
-  /* Fill out the primary symtab.  */
+  /* Fill out the compunit symtab.  */
 
   if (buildsym_compunit->comp_dir != NULL)
     {
       /* Reallocate the dirname on the symbol obstack.  */
       COMPUNIT_DIRNAME (cu)
-       = obstack_copy0 (&objfile->objfile_obstack,
-                        buildsym_compunit->comp_dir,
-                        strlen (buildsym_compunit->comp_dir));
+       = (const char *) obstack_copy0 (&objfile->objfile_obstack,
+                                       buildsym_compunit->comp_dir,
+                                       strlen (buildsym_compunit->comp_dir));
     }
 
   /* Save the debug format string (if any) in the symtab.  */
@@ -1393,20 +1467,19 @@ end_symtab_with_blockvector (struct block *static_block,
        /* Inlined functions may have symbols not in the global or
           static symbol lists.  */
        if (BLOCK_FUNCTION (block) != NULL)
-         if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
-           SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
+         if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
+           symbol_set_symtab (BLOCK_FUNCTION (block), 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_DICT (block), iter, sym)
-         if (SYMBOL_SYMTAB (sym) == NULL)
-           SYMBOL_SYMTAB (sym) = symtab;
+         if (symbol_symtab (sym) == NULL)
+           symbol_set_symtab (sym, symtab);
       }
   }
 
   add_compunit_symtab_to_objfile (cu);
-  free_buildsym_compunit ();
 
   return cu;
 }
@@ -1428,7 +1501,15 @@ end_symtab_from_static_block (struct block *static_block,
 
   if (static_block == NULL)
     {
-      end_symtab_without_blockvector ();
+      /* Handle the "no blockvector" case.
+        When this happens there is nothing to record, so there's nothing
+        to do: memory will be freed up later.
+
+        Note: We won't be adding a compunit to the objfile's list of
+        compunits, so there's nothing to unchain.  However, since each symtab
+        is added to the objfile's obstack we can't free that space.
+        We could do better, but this is believed to be a sufficiently rare
+        event.  */
       cu = NULL;
     }
   else
@@ -1495,8 +1576,8 @@ set_missing_symtab (struct pending *pending_list,
     {
       for (i = 0; i < pending->nsyms; ++i)
        {
-         if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
-           SYMBOL_SYMTAB (pending->symbol[i]) = COMPUNIT_FILETABS (cu);
+         if (symbol_symtab (pending->symbol[i]) == NULL)
+           symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu));
        }
     }
 }
@@ -1506,8 +1587,9 @@ set_missing_symtab (struct pending *pending_list,
    This is the case for DWARF4 Type Units.  */
 
 void
-augment_type_symtab (struct compunit_symtab *cust)
+augment_type_symtab (void)
 {
+  struct compunit_symtab *cust = buildsym_compunit->compunit_symtab;
   const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);
 
   if (context_stack_depth > 0)
@@ -1556,7 +1638,7 @@ augment_type_symtab (struct compunit_symtab *cust)
 struct context_stack *
 push_context (int desc, CORE_ADDR valu)
 {
-  struct context_stack *new;
+  struct context_stack *newobj;
 
   if (context_stack_depth == context_stack_size)
     {
@@ -1566,18 +1648,18 @@ push_context (int desc, CORE_ADDR valu)
                  (context_stack_size * sizeof (struct context_stack)));
     }
 
-  new = &context_stack[context_stack_depth++];
-  new->depth = desc;
-  new->locals = local_symbols;
-  new->old_blocks = pending_blocks;
-  new->start_addr = valu;
-  new->using_directives = using_directives;
-  new->name = NULL;
+  newobj = &context_stack[context_stack_depth++];
+  newobj->depth = desc;
+  newobj->locals = local_symbols;
+  newobj->old_blocks = pending_blocks;
+  newobj->start_addr = valu;
+  newobj->local_using_directives = local_using_directives;
+  newobj->name = NULL;
 
   local_symbols = NULL;
-  using_directives = NULL;
+  local_using_directives = NULL;
 
-  return new;
+  return newobj;
 }
 
 /* Pop a context block.  Returns the address of the context block just
@@ -1674,17 +1756,28 @@ get_last_source_file (void)
 void
 buildsym_init (void)
 {
-  free_pendings = NULL;
-  file_symbols = NULL;
-  global_symbols = NULL;
-  pending_blocks = NULL;
-  pending_macros = NULL;
-  using_directives = NULL;
   subfile_stack = NULL;
 
-  /* We shouldn't have any address map at this point.  */
-  gdb_assert (! pending_addrmap);
   pending_addrmap_interesting = 0;
+
+  /* Context stack is initially empty.  Allocate first one with room
+     for a few levels; reuse it forever afterward.  */
+  if (context_stack == NULL)
+    {
+      context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
+      context_stack = XNEWVEC (struct context_stack, context_stack_size);
+    }
+
+  /* Ensure the really_free_pendings cleanup was called after
+     the last time.  */
+  gdb_assert (free_pendings == NULL);
+  gdb_assert (pending_blocks == NULL);
+  gdb_assert (file_symbols == NULL);
+  gdb_assert (global_symbols == NULL);
+  gdb_assert (global_using_directives == NULL);
+  gdb_assert (pending_macros == NULL);
+  gdb_assert (pending_addrmap == NULL);
+  gdb_assert (buildsym_compunit == NULL);
 }
 
 /* Initialize anything that needs initializing when a completely new
This page took 0.031545 seconds and 4 git commands to generate.