xref: /netbsd/external/gpl3/gdb/dist/gdb/buildsym.c (revision 1424dfb3)
166e63ce3Schristos /* Support routines for building symbol tables in GDB's internal format.
2*1424dfb3Schristos    Copyright (C) 1986-2020 Free Software Foundation, Inc.
366e63ce3Schristos 
466e63ce3Schristos    This file is part of GDB.
566e63ce3Schristos 
666e63ce3Schristos    This program is free software; you can redistribute it and/or modify
766e63ce3Schristos    it under the terms of the GNU General Public License as published by
866e63ce3Schristos    the Free Software Foundation; either version 3 of the License, or
966e63ce3Schristos    (at your option) any later version.
1066e63ce3Schristos 
1166e63ce3Schristos    This program is distributed in the hope that it will be useful,
1266e63ce3Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1366e63ce3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1466e63ce3Schristos    GNU General Public License for more details.
1566e63ce3Schristos 
1666e63ce3Schristos    You should have received a copy of the GNU General Public License
1766e63ce3Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1866e63ce3Schristos 
1966e63ce3Schristos #include "defs.h"
2007163879Schristos #include "buildsym-legacy.h"
2166e63ce3Schristos #include "bfd.h"
2266e63ce3Schristos #include "gdb_obstack.h"
2366e63ce3Schristos #include "symtab.h"
2466e63ce3Schristos #include "symfile.h"
2566e63ce3Schristos #include "objfiles.h"
2666e63ce3Schristos #include "gdbtypes.h"
2766e63ce3Schristos #include "complaints.h"
2866e63ce3Schristos #include "expression.h"		/* For "enum exp_opcode" used by...  */
2966e63ce3Schristos #include "filenames.h"		/* For DOSish file names.  */
3066e63ce3Schristos #include "macrotab.h"
3166e63ce3Schristos #include "demangle.h"		/* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
3266e63ce3Schristos #include "block.h"
3366e63ce3Schristos #include "cp-support.h"
3466e63ce3Schristos #include "dictionary.h"
3566e63ce3Schristos #include "addrmap.h"
3607163879Schristos #include <algorithm>
3766e63ce3Schristos 
3848596154Schristos /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
3966e63ce3Schristos    questionable--see comment where we call them).  */
4066e63ce3Schristos 
4166e63ce3Schristos #include "stabsread.h"
4266e63ce3Schristos 
4348596154Schristos /* List of blocks already made (lexical contexts already closed).
4448596154Schristos    This is used at the end to make the blockvector.  */
4548596154Schristos 
4648596154Schristos struct pending_block
4748596154Schristos   {
4848596154Schristos     struct pending_block *next;
4948596154Schristos     struct block *block;
5048596154Schristos   };
5148596154Schristos 
5266e63ce3Schristos /* Initial sizes of data structures.  These are realloc'd larger if
5366e63ce3Schristos    needed, and realloc'd down to the size actually used, when
5466e63ce3Schristos    completed.  */
5566e63ce3Schristos 
5666e63ce3Schristos #define	INITIAL_LINE_VECTOR_LENGTH	1000
5766e63ce3Schristos 
5866e63ce3Schristos 
buildsym_compunit(struct objfile * objfile_,const char * name,const char * comp_dir_,enum language language_,CORE_ADDR last_addr)5907163879Schristos buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
6007163879Schristos 				      const char *name,
6107163879Schristos 				      const char *comp_dir_,
6207163879Schristos 				      enum language language_,
6307163879Schristos 				      CORE_ADDR last_addr)
6407163879Schristos   : m_objfile (objfile_),
6507163879Schristos     m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
6607163879Schristos     m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
6707163879Schristos     m_language (language_),
6807163879Schristos     m_last_source_start_addr (last_addr)
6907163879Schristos {
7007163879Schristos   /* Allocate the compunit symtab now.  The caller needs it to allocate
7107163879Schristos      non-primary symtabs.  It is also needed by get_macro_table.  */
7207163879Schristos   m_compunit_symtab = allocate_compunit_symtab (m_objfile, name);
7307163879Schristos 
7407163879Schristos   /* Build the subfile for NAME (the main source file) so that we can record
7507163879Schristos      a pointer to it for later.
7607163879Schristos      IMPORTANT: Do not allocate a struct symtab for NAME here.
7707163879Schristos      It can happen that the debug info provides a different path to NAME than
7807163879Schristos      DIRNAME,NAME.  We cope with this in watch_main_source_file_lossage but
7907163879Schristos      that only works if the main_subfile doesn't have a symtab yet.  */
8007163879Schristos   start_subfile (name);
8107163879Schristos   /* Save this so that we don't have to go looking for it at the end
8207163879Schristos      of the subfiles list.  */
8307163879Schristos   m_main_subfile = m_current_subfile;
8407163879Schristos }
8507163879Schristos 
~buildsym_compunit()8607163879Schristos buildsym_compunit::~buildsym_compunit ()
8707163879Schristos {
8807163879Schristos   struct subfile *subfile, *nextsub;
8907163879Schristos 
9007163879Schristos   if (m_pending_macros != nullptr)
9107163879Schristos     free_macro_table (m_pending_macros);
9207163879Schristos 
9307163879Schristos   for (subfile = m_subfiles;
9407163879Schristos        subfile != NULL;
9507163879Schristos        subfile = nextsub)
9607163879Schristos     {
9707163879Schristos       nextsub = subfile->next;
9807163879Schristos       xfree (subfile->name);
9907163879Schristos       xfree (subfile->line_vector);
10007163879Schristos       xfree (subfile);
10107163879Schristos     }
10207163879Schristos 
10307163879Schristos   struct pending *next, *next1;
10407163879Schristos 
10507163879Schristos   for (next = m_file_symbols; next != NULL; next = next1)
10607163879Schristos     {
10707163879Schristos       next1 = next->next;
10807163879Schristos       xfree ((void *) next);
10907163879Schristos     }
11007163879Schristos 
11107163879Schristos   for (next = m_global_symbols; next != NULL; next = next1)
11207163879Schristos     {
11307163879Schristos       next1 = next->next;
11407163879Schristos       xfree ((void *) next);
11507163879Schristos     }
11607163879Schristos }
11707163879Schristos 
11807163879Schristos struct macro_table *
get_macro_table()11907163879Schristos buildsym_compunit::get_macro_table ()
12007163879Schristos {
12107163879Schristos   if (m_pending_macros == nullptr)
12207163879Schristos     m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
123*1424dfb3Schristos 					&m_objfile->per_bfd->string_cache,
12407163879Schristos 					m_compunit_symtab);
12507163879Schristos   return m_pending_macros;
12607163879Schristos }
12707163879Schristos 
12866e63ce3Schristos /* Maintain the lists of symbols and blocks.  */
12966e63ce3Schristos 
13048596154Schristos /* Add a symbol to one of the lists of symbols.  */
13166e63ce3Schristos 
13266e63ce3Schristos void
add_symbol_to_list(struct symbol * symbol,struct pending ** listhead)13366e63ce3Schristos add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
13466e63ce3Schristos {
13566e63ce3Schristos   struct pending *link;
13666e63ce3Schristos 
13766e63ce3Schristos   /* If this is an alias for another symbol, don't add it.  */
138*1424dfb3Schristos   if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#')
13966e63ce3Schristos     return;
14066e63ce3Schristos 
14166e63ce3Schristos   /* We keep PENDINGSIZE symbols in each link of the list.  If we
14266e63ce3Schristos      don't have a link with room in it, add a new link.  */
14366e63ce3Schristos   if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
14466e63ce3Schristos     {
145c03b94e9Schristos       link = XNEW (struct pending);
14666e63ce3Schristos       link->next = *listhead;
14766e63ce3Schristos       *listhead = link;
14866e63ce3Schristos       link->nsyms = 0;
14966e63ce3Schristos     }
15066e63ce3Schristos 
15166e63ce3Schristos   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
15266e63ce3Schristos }
15366e63ce3Schristos 
15466e63ce3Schristos /* Find a symbol named NAME on a LIST.  NAME need not be
15566e63ce3Schristos    '\0'-terminated; LENGTH is the length of the name.  */
15666e63ce3Schristos 
15766e63ce3Schristos struct symbol *
find_symbol_in_list(struct pending * list,char * name,int length)15866e63ce3Schristos find_symbol_in_list (struct pending *list, char *name, int length)
15966e63ce3Schristos {
16066e63ce3Schristos   int j;
16148596154Schristos   const char *pp;
16266e63ce3Schristos 
16366e63ce3Schristos   while (list != NULL)
16466e63ce3Schristos     {
16566e63ce3Schristos       for (j = list->nsyms; --j >= 0;)
16666e63ce3Schristos 	{
167*1424dfb3Schristos 	  pp = list->symbol[j]->linkage_name ();
16866e63ce3Schristos 	  if (*pp == *name && strncmp (pp, name, length) == 0
16966e63ce3Schristos 	      && pp[length] == '\0')
17066e63ce3Schristos 	    {
17166e63ce3Schristos 	      return (list->symbol[j]);
17266e63ce3Schristos 	    }
17366e63ce3Schristos 	}
17466e63ce3Schristos       list = list->next;
17566e63ce3Schristos     }
17666e63ce3Schristos   return (NULL);
17766e63ce3Schristos }
17866e63ce3Schristos 
17907163879Schristos /* Record BLOCK on the list of all blocks in the file.  Put it after
18007163879Schristos    OPBLOCK, or at the beginning if opblock is NULL.  This puts the
18107163879Schristos    block in the list after all its subblocks.  */
18266e63ce3Schristos 
18366e63ce3Schristos void
record_pending_block(struct block * block,struct pending_block * opblock)18407163879Schristos buildsym_compunit::record_pending_block (struct block *block,
18507163879Schristos 					 struct pending_block *opblock)
18666e63ce3Schristos {
18707163879Schristos   struct pending_block *pblock;
18866e63ce3Schristos 
18907163879Schristos   pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
19007163879Schristos   pblock->block = block;
19107163879Schristos   if (opblock)
19266e63ce3Schristos     {
19307163879Schristos       pblock->next = opblock->next;
19407163879Schristos       opblock->next = pblock;
19566e63ce3Schristos     }
19607163879Schristos   else
19766e63ce3Schristos     {
19807163879Schristos       pblock->next = m_pending_blocks;
19907163879Schristos       m_pending_blocks = pblock;
20066e63ce3Schristos     }
20148596154Schristos }
20266e63ce3Schristos 
20366e63ce3Schristos /* Take one of the lists of symbols and make a block from it.  Keep
20466e63ce3Schristos    the order the symbols have in the list (reversed from the input
20566e63ce3Schristos    file).  Put the block on the list of pending blocks.  */
20666e63ce3Schristos 
20707163879Schristos struct block *
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)20807163879Schristos buildsym_compunit::finish_block_internal
20907163879Schristos     (struct symbol *symbol,
210c03b94e9Schristos      struct pending **listhead,
21166e63ce3Schristos      struct pending_block *old_blocks,
212c03b94e9Schristos      const struct dynamic_prop *static_link,
21366e63ce3Schristos      CORE_ADDR start, CORE_ADDR end,
21448596154Schristos      int is_global, int expandable)
21566e63ce3Schristos {
216*1424dfb3Schristos   struct gdbarch *gdbarch = m_objfile->arch ();
21766e63ce3Schristos   struct pending *next, *next1;
21866e63ce3Schristos   struct block *block;
21966e63ce3Schristos   struct pending_block *pblock;
22066e63ce3Schristos   struct pending_block *opblock;
22166e63ce3Schristos 
22248596154Schristos   block = (is_global
22307163879Schristos 	   ? allocate_global_block (&m_objfile->objfile_obstack)
22407163879Schristos 	   : allocate_block (&m_objfile->objfile_obstack));
22566e63ce3Schristos 
22666e63ce3Schristos   if (symbol)
22766e63ce3Schristos     {
22807163879Schristos       BLOCK_MULTIDICT (block)
22907163879Schristos 	= mdict_create_linear (&m_objfile->objfile_obstack, *listhead);
23066e63ce3Schristos     }
23166e63ce3Schristos   else
23266e63ce3Schristos     {
23348596154Schristos       if (expandable)
23448596154Schristos 	{
23507163879Schristos 	  BLOCK_MULTIDICT (block) = mdict_create_hashed_expandable (m_language);
23607163879Schristos 	  mdict_add_pending (BLOCK_MULTIDICT (block), *listhead);
23748596154Schristos 	}
23848596154Schristos       else
23948596154Schristos 	{
24007163879Schristos 	  BLOCK_MULTIDICT (block) =
24107163879Schristos 	    mdict_create_hashed (&m_objfile->objfile_obstack, *listhead);
24248596154Schristos 	}
24366e63ce3Schristos     }
24466e63ce3Schristos 
24566e63ce3Schristos   BLOCK_START (block) = start;
24666e63ce3Schristos   BLOCK_END (block) = end;
24766e63ce3Schristos 
24866e63ce3Schristos   /* Put the block in as the value of the symbol that names it.  */
24966e63ce3Schristos 
25066e63ce3Schristos   if (symbol)
25166e63ce3Schristos     {
25266e63ce3Schristos       struct type *ftype = SYMBOL_TYPE (symbol);
25307163879Schristos       struct mdict_iterator miter;
25466e63ce3Schristos       SYMBOL_BLOCK_VALUE (symbol) = block;
25566e63ce3Schristos       BLOCK_FUNCTION (block) = symbol;
25666e63ce3Schristos 
257*1424dfb3Schristos       if (ftype->num_fields () <= 0)
25866e63ce3Schristos 	{
25966e63ce3Schristos 	  /* No parameter type information is recorded with the
26066e63ce3Schristos 	     function's type.  Set that from the type of the
26166e63ce3Schristos 	     parameter symbols.  */
26266e63ce3Schristos 	  int nparams = 0, iparams;
26366e63ce3Schristos 	  struct symbol *sym;
26448596154Schristos 
26548596154Schristos 	  /* Here we want to directly access the dictionary, because
26648596154Schristos 	     we haven't fully initialized the block yet.  */
26707163879Schristos 	  ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
26866e63ce3Schristos 	    {
26966e63ce3Schristos 	      if (SYMBOL_IS_ARGUMENT (sym))
27066e63ce3Schristos 		nparams++;
27166e63ce3Schristos 	    }
27266e63ce3Schristos 	  if (nparams > 0)
27366e63ce3Schristos 	    {
274*1424dfb3Schristos 	      ftype->set_num_fields (nparams);
275*1424dfb3Schristos 	      ftype->set_fields
276*1424dfb3Schristos 		((struct field *)
277*1424dfb3Schristos 		 TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
27866e63ce3Schristos 
27966e63ce3Schristos 	      iparams = 0;
28048596154Schristos 	      /* Here we want to directly access the dictionary, because
28148596154Schristos 		 we haven't fully initialized the block yet.  */
28207163879Schristos 	      ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
28366e63ce3Schristos 		{
28466e63ce3Schristos 		  if (iparams == nparams)
28566e63ce3Schristos 		    break;
28666e63ce3Schristos 
28766e63ce3Schristos 		  if (SYMBOL_IS_ARGUMENT (sym))
28866e63ce3Schristos 		    {
289*1424dfb3Schristos 		      ftype->field (iparams).set_type (SYMBOL_TYPE (sym));
29066e63ce3Schristos 		      TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
29166e63ce3Schristos 		      iparams++;
29266e63ce3Schristos 		    }
29366e63ce3Schristos 		}
29466e63ce3Schristos 	    }
29566e63ce3Schristos 	}
29666e63ce3Schristos     }
29766e63ce3Schristos   else
29866e63ce3Schristos     {
29966e63ce3Schristos       BLOCK_FUNCTION (block) = NULL;
30066e63ce3Schristos     }
30166e63ce3Schristos 
302c03b94e9Schristos   if (static_link != NULL)
30307163879Schristos     objfile_register_static_link (m_objfile, block, static_link);
304c03b94e9Schristos 
30507163879Schristos   /* Now free the links of the list, and empty the list.  */
30666e63ce3Schristos 
30766e63ce3Schristos   for (next = *listhead; next; next = next1)
30866e63ce3Schristos     {
30966e63ce3Schristos       next1 = next->next;
31007163879Schristos       xfree (next);
31166e63ce3Schristos     }
31266e63ce3Schristos   *listhead = NULL;
31366e63ce3Schristos 
31466e63ce3Schristos   /* Check to be sure that the blocks have an end address that is
31566e63ce3Schristos      greater than starting address.  */
31666e63ce3Schristos 
31766e63ce3Schristos   if (BLOCK_END (block) < BLOCK_START (block))
31866e63ce3Schristos     {
31966e63ce3Schristos       if (symbol)
32066e63ce3Schristos 	{
32107163879Schristos 	  complaint (_("block end address less than block "
32266e63ce3Schristos 		       "start address in %s (patched it)"),
323*1424dfb3Schristos 		     symbol->print_name ());
32466e63ce3Schristos 	}
32566e63ce3Schristos       else
32666e63ce3Schristos 	{
32707163879Schristos 	  complaint (_("block end address %s less than block "
32866e63ce3Schristos 		       "start address %s (patched it)"),
32966e63ce3Schristos 		     paddress (gdbarch, BLOCK_END (block)),
33066e63ce3Schristos 		     paddress (gdbarch, BLOCK_START (block)));
33166e63ce3Schristos 	}
33266e63ce3Schristos       /* Better than nothing.  */
33366e63ce3Schristos       BLOCK_END (block) = BLOCK_START (block);
33466e63ce3Schristos     }
33566e63ce3Schristos 
33666e63ce3Schristos   /* Install this block as the superblock of all blocks made since the
33766e63ce3Schristos      start of this scope that don't have superblocks yet.  */
33866e63ce3Schristos 
33966e63ce3Schristos   opblock = NULL;
34007163879Schristos   for (pblock = m_pending_blocks;
34166e63ce3Schristos        pblock && pblock != old_blocks;
34266e63ce3Schristos        pblock = pblock->next)
34366e63ce3Schristos     {
34466e63ce3Schristos       if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
34566e63ce3Schristos 	{
34666e63ce3Schristos 	  /* Check to be sure the blocks are nested as we receive
34766e63ce3Schristos 	     them.  If the compiler/assembler/linker work, this just
34866e63ce3Schristos 	     burns a small amount of time.
34966e63ce3Schristos 
35066e63ce3Schristos 	     Skip blocks which correspond to a function; they're not
35166e63ce3Schristos 	     physically nested inside this other blocks, only
35266e63ce3Schristos 	     lexically nested.  */
35366e63ce3Schristos 	  if (BLOCK_FUNCTION (pblock->block) == NULL
35466e63ce3Schristos 	      && (BLOCK_START (pblock->block) < BLOCK_START (block)
35566e63ce3Schristos 		  || BLOCK_END (pblock->block) > BLOCK_END (block)))
35666e63ce3Schristos 	    {
35766e63ce3Schristos 	      if (symbol)
35866e63ce3Schristos 		{
35907163879Schristos 		  complaint (_("inner block not inside outer block in %s"),
360*1424dfb3Schristos 			     symbol->print_name ());
36166e63ce3Schristos 		}
36266e63ce3Schristos 	      else
36366e63ce3Schristos 		{
36407163879Schristos 		  complaint (_("inner block (%s-%s) not "
36566e63ce3Schristos 			       "inside outer block (%s-%s)"),
36666e63ce3Schristos 			     paddress (gdbarch, BLOCK_START (pblock->block)),
36766e63ce3Schristos 			     paddress (gdbarch, BLOCK_END (pblock->block)),
36866e63ce3Schristos 			     paddress (gdbarch, BLOCK_START (block)),
36966e63ce3Schristos 			     paddress (gdbarch, BLOCK_END (block)));
37066e63ce3Schristos 		}
37166e63ce3Schristos 	      if (BLOCK_START (pblock->block) < BLOCK_START (block))
37266e63ce3Schristos 		BLOCK_START (pblock->block) = BLOCK_START (block);
37366e63ce3Schristos 	      if (BLOCK_END (pblock->block) > BLOCK_END (block))
37466e63ce3Schristos 		BLOCK_END (pblock->block) = BLOCK_END (block);
37566e63ce3Schristos 	    }
37666e63ce3Schristos 	  BLOCK_SUPERBLOCK (pblock->block) = block;
37766e63ce3Schristos 	}
37866e63ce3Schristos       opblock = pblock;
37966e63ce3Schristos     }
38066e63ce3Schristos 
381c03b94e9Schristos   block_set_using (block,
382c03b94e9Schristos 		   (is_global
38307163879Schristos 		    ? m_global_using_directives
38407163879Schristos 		    : m_local_using_directives),
38507163879Schristos 		   &m_objfile->objfile_obstack);
386c03b94e9Schristos   if (is_global)
38707163879Schristos     m_global_using_directives = NULL;
388c03b94e9Schristos   else
38907163879Schristos     m_local_using_directives = NULL;
39066e63ce3Schristos 
39107163879Schristos   record_pending_block (block, opblock);
39266e63ce3Schristos 
39366e63ce3Schristos   return block;
39466e63ce3Schristos }
39566e63ce3Schristos 
39648596154Schristos struct block *
finish_block(struct symbol * symbol,struct pending_block * old_blocks,const struct dynamic_prop * static_link,CORE_ADDR start,CORE_ADDR end)39707163879Schristos buildsym_compunit::finish_block (struct symbol *symbol,
39848596154Schristos 				 struct pending_block *old_blocks,
399c03b94e9Schristos 				 const struct dynamic_prop *static_link,
40026a53354Schristos 				 CORE_ADDR start, CORE_ADDR end)
40148596154Schristos {
40207163879Schristos   return finish_block_internal (symbol, &m_local_symbols,
40307163879Schristos 				old_blocks, static_link, start, end, 0, 0);
40448596154Schristos }
40566e63ce3Schristos 
40666e63ce3Schristos /* Record that the range of addresses from START to END_INCLUSIVE
40766e63ce3Schristos    (inclusive, like it says) belongs to BLOCK.  BLOCK's start and end
40866e63ce3Schristos    addresses must be set already.  You must apply this function to all
40966e63ce3Schristos    BLOCK's children before applying it to BLOCK.
41066e63ce3Schristos 
41166e63ce3Schristos    If a call to this function complicates the picture beyond that
41266e63ce3Schristos    already provided by BLOCK_START and BLOCK_END, then we create an
41366e63ce3Schristos    address map for the block.  */
41466e63ce3Schristos void
record_block_range(struct block * block,CORE_ADDR start,CORE_ADDR end_inclusive)41507163879Schristos buildsym_compunit::record_block_range (struct block *block,
41607163879Schristos 				       CORE_ADDR start,
41707163879Schristos 				       CORE_ADDR end_inclusive)
41866e63ce3Schristos {
41966e63ce3Schristos   /* If this is any different from the range recorded in the block's
42066e63ce3Schristos      own BLOCK_START and BLOCK_END, then note that the address map has
42166e63ce3Schristos      become interesting.  Note that even if this block doesn't have
42266e63ce3Schristos      any "interesting" ranges, some later block might, so we still
42366e63ce3Schristos      need to record this block in the addrmap.  */
42466e63ce3Schristos   if (start != BLOCK_START (block)
42566e63ce3Schristos       || end_inclusive + 1 != BLOCK_END (block))
42607163879Schristos     m_pending_addrmap_interesting = true;
42766e63ce3Schristos 
42807163879Schristos   if (m_pending_addrmap == nullptr)
42907163879Schristos     m_pending_addrmap = addrmap_create_mutable (&m_pending_addrmap_obstack);
43007163879Schristos 
43107163879Schristos   addrmap_set_empty (m_pending_addrmap, start, end_inclusive, block);
43266e63ce3Schristos }
43366e63ce3Schristos 
43407163879Schristos struct blockvector *
make_blockvector()43507163879Schristos buildsym_compunit::make_blockvector ()
43666e63ce3Schristos {
43766e63ce3Schristos   struct pending_block *next;
43866e63ce3Schristos   struct blockvector *blockvector;
43966e63ce3Schristos   int i;
44066e63ce3Schristos 
44166e63ce3Schristos   /* Count the length of the list of blocks.  */
44266e63ce3Schristos 
44307163879Schristos   for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
44407163879Schristos     {
44566e63ce3Schristos     }
44666e63ce3Schristos 
44766e63ce3Schristos   blockvector = (struct blockvector *)
44807163879Schristos     obstack_alloc (&m_objfile->objfile_obstack,
44966e63ce3Schristos 		   (sizeof (struct blockvector)
45066e63ce3Schristos 		    + (i - 1) * sizeof (struct block *)));
45166e63ce3Schristos 
45266e63ce3Schristos   /* Copy the blocks into the blockvector.  This is done in reverse
45366e63ce3Schristos      order, which happens to put the blocks into the proper order
45466e63ce3Schristos      (ascending starting address).  finish_block has hair to insert
45566e63ce3Schristos      each block into the list after its subblocks in order to make
45666e63ce3Schristos      sure this is true.  */
45766e63ce3Schristos 
45866e63ce3Schristos   BLOCKVECTOR_NBLOCKS (blockvector) = i;
45907163879Schristos   for (next = m_pending_blocks; next; next = next->next)
46066e63ce3Schristos     {
46166e63ce3Schristos       BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
46266e63ce3Schristos     }
46366e63ce3Schristos 
46466e63ce3Schristos   free_pending_blocks ();
46566e63ce3Schristos 
46666e63ce3Schristos   /* If we needed an address map for this symtab, record it in the
46766e63ce3Schristos      blockvector.  */
46807163879Schristos   if (m_pending_addrmap != nullptr && m_pending_addrmap_interesting)
46966e63ce3Schristos     BLOCKVECTOR_MAP (blockvector)
47007163879Schristos       = addrmap_create_fixed (m_pending_addrmap, &m_objfile->objfile_obstack);
47166e63ce3Schristos   else
47266e63ce3Schristos     BLOCKVECTOR_MAP (blockvector) = 0;
47366e63ce3Schristos 
47466e63ce3Schristos   /* Some compilers output blocks in the wrong order, but we depend on
47566e63ce3Schristos      their being in the right order so we can binary search.  Check the
47648596154Schristos      order and moan about it.
47748596154Schristos      Note: Remember that the first two blocks are the global and static
47848596154Schristos      blocks.  We could special case that fact and begin checking at block 2.
47948596154Schristos      To avoid making that assumption we do not.  */
48066e63ce3Schristos   if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
48166e63ce3Schristos     {
48266e63ce3Schristos       for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
48366e63ce3Schristos 	{
48466e63ce3Schristos 	  if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
48566e63ce3Schristos 	      > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
48666e63ce3Schristos 	    {
48766e63ce3Schristos 	      CORE_ADDR start
48866e63ce3Schristos 		= BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
48966e63ce3Schristos 
49007163879Schristos 	      complaint (_("block at %s out of order"),
49166e63ce3Schristos 			 hex_string ((LONGEST) start));
49266e63ce3Schristos 	    }
49366e63ce3Schristos 	}
49466e63ce3Schristos     }
49566e63ce3Schristos 
49666e63ce3Schristos   return (blockvector);
49766e63ce3Schristos }
49866e63ce3Schristos 
49966e63ce3Schristos /* Start recording information about source code that came from an
50066e63ce3Schristos    included (or otherwise merged-in) source file with a different
50126a53354Schristos    name.  NAME is the name of the file (cannot be NULL).  */
50266e63ce3Schristos 
50366e63ce3Schristos void
start_subfile(const char * name)50407163879Schristos buildsym_compunit::start_subfile (const char *name)
50566e63ce3Schristos {
50626a53354Schristos   const char *subfile_dirname;
50766e63ce3Schristos   struct subfile *subfile;
50866e63ce3Schristos 
50907163879Schristos   subfile_dirname = m_comp_dir.get ();
51026a53354Schristos 
51126a53354Schristos   /* See if this subfile is already registered.  */
51226a53354Schristos 
51307163879Schristos   for (subfile = m_subfiles; subfile; subfile = subfile->next)
51466e63ce3Schristos     {
51566e63ce3Schristos       char *subfile_name;
51666e63ce3Schristos 
51766e63ce3Schristos       /* If NAME is an absolute path, and this subfile is not, then
51866e63ce3Schristos 	 attempt to create an absolute path to compare.  */
51966e63ce3Schristos       if (IS_ABSOLUTE_PATH (name)
52066e63ce3Schristos 	  && !IS_ABSOLUTE_PATH (subfile->name)
52126a53354Schristos 	  && subfile_dirname != NULL)
52226a53354Schristos 	subfile_name = concat (subfile_dirname, SLASH_STRING,
52366e63ce3Schristos 			       subfile->name, (char *) NULL);
52466e63ce3Schristos       else
52566e63ce3Schristos 	subfile_name = subfile->name;
52666e63ce3Schristos 
52766e63ce3Schristos       if (FILENAME_CMP (subfile_name, name) == 0)
52866e63ce3Schristos 	{
52907163879Schristos 	  m_current_subfile = subfile;
53066e63ce3Schristos 	  if (subfile_name != subfile->name)
53166e63ce3Schristos 	    xfree (subfile_name);
53266e63ce3Schristos 	  return;
53366e63ce3Schristos 	}
53466e63ce3Schristos       if (subfile_name != subfile->name)
53566e63ce3Schristos 	xfree (subfile_name);
53666e63ce3Schristos     }
53766e63ce3Schristos 
53826a53354Schristos   /* This subfile is not known.  Add an entry for it.  */
53966e63ce3Schristos 
540c03b94e9Schristos   subfile = XNEW (struct subfile);
54126a53354Schristos   memset (subfile, 0, sizeof (struct subfile));
54207163879Schristos   subfile->buildsym_compunit = this;
54326a53354Schristos 
54407163879Schristos   subfile->next = m_subfiles;
54507163879Schristos   m_subfiles = subfile;
54626a53354Schristos 
54707163879Schristos   m_current_subfile = subfile;
54866e63ce3Schristos 
54948596154Schristos   subfile->name = xstrdup (name);
55066e63ce3Schristos 
55166e63ce3Schristos   /* Initialize line-number recording for this subfile.  */
55266e63ce3Schristos   subfile->line_vector = NULL;
55366e63ce3Schristos 
55466e63ce3Schristos   /* Default the source language to whatever can be deduced from the
55566e63ce3Schristos      filename.  If nothing can be deduced (such as for a C/C++ include
55666e63ce3Schristos      file with a ".h" extension), then inherit whatever language the
55766e63ce3Schristos      previous subfile had.  This kludgery is necessary because there
55866e63ce3Schristos      is no standard way in some object formats to record the source
55966e63ce3Schristos      language.  Also, when symtabs are allocated we try to deduce a
56066e63ce3Schristos      language then as well, but it is too late for us to use that
56166e63ce3Schristos      information while reading symbols, since symtabs aren't allocated
56266e63ce3Schristos      until after all the symbols have been processed for a given
56366e63ce3Schristos      source file.  */
56466e63ce3Schristos 
56566e63ce3Schristos   subfile->language = deduce_language_from_filename (subfile->name);
56666e63ce3Schristos   if (subfile->language == language_unknown
56766e63ce3Schristos       && subfile->next != NULL)
56866e63ce3Schristos     {
56966e63ce3Schristos       subfile->language = subfile->next->language;
57066e63ce3Schristos     }
57166e63ce3Schristos 
57266e63ce3Schristos   /* If the filename of this subfile ends in .C, then change the
57366e63ce3Schristos      language of any pending subfiles from C to C++.  We also accept
57466e63ce3Schristos      any other C++ suffixes accepted by deduce_language_from_filename.  */
57566e63ce3Schristos   /* Likewise for f2c.  */
57666e63ce3Schristos 
57766e63ce3Schristos   if (subfile->name)
57866e63ce3Schristos     {
57966e63ce3Schristos       struct subfile *s;
58066e63ce3Schristos       enum language sublang = deduce_language_from_filename (subfile->name);
58166e63ce3Schristos 
58266e63ce3Schristos       if (sublang == language_cplus || sublang == language_fortran)
58307163879Schristos 	for (s = m_subfiles; s != NULL; s = s->next)
58466e63ce3Schristos 	  if (s->language == language_c)
58566e63ce3Schristos 	    s->language = sublang;
58666e63ce3Schristos     }
58766e63ce3Schristos 
58866e63ce3Schristos   /* And patch up this file if necessary.  */
58966e63ce3Schristos   if (subfile->language == language_c
59066e63ce3Schristos       && subfile->next != NULL
59166e63ce3Schristos       && (subfile->next->language == language_cplus
59266e63ce3Schristos 	  || subfile->next->language == language_fortran))
59366e63ce3Schristos     {
59466e63ce3Schristos       subfile->language = subfile->next->language;
59566e63ce3Schristos     }
59666e63ce3Schristos }
59766e63ce3Schristos 
59866e63ce3Schristos /* For stabs readers, the first N_SO symbol is assumed to be the
59966e63ce3Schristos    source file name, and the subfile struct is initialized using that
60066e63ce3Schristos    assumption.  If another N_SO symbol is later seen, immediately
60166e63ce3Schristos    following the first one, then the first one is assumed to be the
60266e63ce3Schristos    directory name and the second one is really the source file name.
60366e63ce3Schristos 
60466e63ce3Schristos    So we have to patch up the subfile struct by moving the old name
60566e63ce3Schristos    value to dirname and remembering the new name.  Some sanity
60666e63ce3Schristos    checking is performed to ensure that the state of the subfile
60766e63ce3Schristos    struct is reasonable and that the old name we are assuming to be a
60866e63ce3Schristos    directory name actually is (by checking for a trailing '/').  */
60966e63ce3Schristos 
61066e63ce3Schristos void
patch_subfile_names(struct subfile * subfile,const char * name)61107163879Schristos buildsym_compunit::patch_subfile_names (struct subfile *subfile,
61207163879Schristos 					const char *name)
61366e63ce3Schristos {
61426a53354Schristos   if (subfile != NULL
61507163879Schristos       && m_comp_dir == NULL
61626a53354Schristos       && subfile->name != NULL
61766e63ce3Schristos       && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
61866e63ce3Schristos     {
61907163879Schristos       m_comp_dir.reset (subfile->name);
62066e63ce3Schristos       subfile->name = xstrdup (name);
62148596154Schristos       set_last_source_file (name);
62266e63ce3Schristos 
62366e63ce3Schristos       /* Default the source language to whatever can be deduced from
62466e63ce3Schristos          the filename.  If nothing can be deduced (such as for a C/C++
62566e63ce3Schristos          include file with a ".h" extension), then inherit whatever
62666e63ce3Schristos          language the previous subfile had.  This kludgery is
62766e63ce3Schristos          necessary because there is no standard way in some object
62866e63ce3Schristos          formats to record the source language.  Also, when symtabs
62966e63ce3Schristos          are allocated we try to deduce a language then as well, but
63066e63ce3Schristos          it is too late for us to use that information while reading
63166e63ce3Schristos          symbols, since symtabs aren't allocated until after all the
63266e63ce3Schristos          symbols have been processed for a given source file.  */
63366e63ce3Schristos 
63466e63ce3Schristos       subfile->language = deduce_language_from_filename (subfile->name);
63566e63ce3Schristos       if (subfile->language == language_unknown
63666e63ce3Schristos 	  && subfile->next != NULL)
63766e63ce3Schristos 	{
63866e63ce3Schristos 	  subfile->language = subfile->next->language;
63966e63ce3Schristos 	}
64066e63ce3Schristos     }
64166e63ce3Schristos }
64266e63ce3Schristos 
64366e63ce3Schristos /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
64466e63ce3Schristos    switching source files (different subfiles, as we call them) within
64566e63ce3Schristos    one object file, but using a stack rather than in an arbitrary
64666e63ce3Schristos    order.  */
64766e63ce3Schristos 
64866e63ce3Schristos void
push_subfile()64907163879Schristos buildsym_compunit::push_subfile ()
65066e63ce3Schristos {
65107163879Schristos   gdb_assert (m_current_subfile != NULL);
65207163879Schristos   gdb_assert (m_current_subfile->name != NULL);
65307163879Schristos   m_subfile_stack.push_back (m_current_subfile->name);
65466e63ce3Schristos }
65566e63ce3Schristos 
65607163879Schristos const char *
pop_subfile()65707163879Schristos buildsym_compunit::pop_subfile ()
65866e63ce3Schristos {
65907163879Schristos   gdb_assert (!m_subfile_stack.empty ());
66007163879Schristos   const char *name = m_subfile_stack.back ();
66107163879Schristos   m_subfile_stack.pop_back ();
66207163879Schristos   return name;
66366e63ce3Schristos }
66466e63ce3Schristos 
66566e63ce3Schristos /* Add a linetable entry for line number LINE and address PC to the
66666e63ce3Schristos    line vector for SUBFILE.  */
66766e63ce3Schristos 
66866e63ce3Schristos void
record_line(struct subfile * subfile,int line,CORE_ADDR pc,bool is_stmt)66907163879Schristos buildsym_compunit::record_line (struct subfile *subfile, int line,
670*1424dfb3Schristos 				CORE_ADDR pc, bool is_stmt)
67166e63ce3Schristos {
67266e63ce3Schristos   struct linetable_entry *e;
67366e63ce3Schristos 
67466e63ce3Schristos   /* Make sure line vector exists and is big enough.  */
67566e63ce3Schristos   if (!subfile->line_vector)
67666e63ce3Schristos     {
67766e63ce3Schristos       subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
67866e63ce3Schristos       subfile->line_vector = (struct linetable *)
67966e63ce3Schristos 	xmalloc (sizeof (struct linetable)
68066e63ce3Schristos 	   + subfile->line_vector_length * sizeof (struct linetable_entry));
68166e63ce3Schristos       subfile->line_vector->nitems = 0;
68207163879Schristos       m_have_line_numbers = true;
68366e63ce3Schristos     }
68466e63ce3Schristos 
685*1424dfb3Schristos   if (subfile->line_vector->nitems >= subfile->line_vector_length)
68666e63ce3Schristos     {
68766e63ce3Schristos       subfile->line_vector_length *= 2;
68866e63ce3Schristos       subfile->line_vector = (struct linetable *)
68966e63ce3Schristos 	xrealloc ((char *) subfile->line_vector,
69066e63ce3Schristos 		  (sizeof (struct linetable)
69166e63ce3Schristos 		   + (subfile->line_vector_length
69266e63ce3Schristos 		      * sizeof (struct linetable_entry))));
69366e63ce3Schristos     }
69466e63ce3Schristos 
69566e63ce3Schristos   /* Normally, we treat lines as unsorted.  But the end of sequence
69666e63ce3Schristos      marker is special.  We sort line markers at the same PC by line
69766e63ce3Schristos      number, so end of sequence markers (which have line == 0) appear
69866e63ce3Schristos      first.  This is right if the marker ends the previous function,
69966e63ce3Schristos      and there is no padding before the next function.  But it is
70066e63ce3Schristos      wrong if the previous line was empty and we are now marking a
70166e63ce3Schristos      switch to a different subfile.  We must leave the end of sequence
70266e63ce3Schristos      marker at the end of this group of lines, not sort the empty line
70366e63ce3Schristos      to after the marker.  The easiest way to accomplish this is to
70466e63ce3Schristos      delete any empty lines from our table, if they are followed by
70566e63ce3Schristos      end of sequence markers.  All we lose is the ability to set
70666e63ce3Schristos      breakpoints at some lines which contain no instructions
70766e63ce3Schristos      anyway.  */
708*1424dfb3Schristos   if (line == 0)
709*1424dfb3Schristos     {
710*1424dfb3Schristos       while (subfile->line_vector->nitems > 0)
71166e63ce3Schristos 	{
71266e63ce3Schristos 	  e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
713*1424dfb3Schristos 	  if (e->pc != pc)
714*1424dfb3Schristos 	    break;
71566e63ce3Schristos 	  subfile->line_vector->nitems--;
71666e63ce3Schristos 	}
71766e63ce3Schristos     }
71866e63ce3Schristos 
71966e63ce3Schristos   e = subfile->line_vector->item + subfile->line_vector->nitems++;
72066e63ce3Schristos   e->line = line;
721*1424dfb3Schristos   e->is_stmt = is_stmt ? 1 : 0;
72266e63ce3Schristos   e->pc = pc;
72366e63ce3Schristos }
72466e63ce3Schristos 
72566e63ce3Schristos 
72666e63ce3Schristos /* Subroutine of end_symtab to simplify it.  Look for a subfile that
72766e63ce3Schristos    matches the main source file's basename.  If there is only one, and
72866e63ce3Schristos    if the main source file doesn't have any symbol or line number
72966e63ce3Schristos    information, then copy this file's symtab and line_vector to the
73066e63ce3Schristos    main source file's subfile and discard the other subfile.  This can
73166e63ce3Schristos    happen because of a compiler bug or from the user playing games
73266e63ce3Schristos    with #line or from things like a distributed build system that
73326a53354Schristos    manipulates the debug info.  This can also happen from an innocent
73426a53354Schristos    symlink in the paths, we don't canonicalize paths here.  */
73566e63ce3Schristos 
73607163879Schristos void
watch_main_source_file_lossage()73707163879Schristos buildsym_compunit::watch_main_source_file_lossage ()
73866e63ce3Schristos {
73966e63ce3Schristos   struct subfile *mainsub, *subfile;
74066e63ce3Schristos 
74126a53354Schristos   /* Get the main source file.  */
74207163879Schristos   mainsub = m_main_subfile;
74366e63ce3Schristos 
74466e63ce3Schristos   /* If the main source file doesn't have any line number or symbol
74526a53354Schristos      info, look for an alias in another subfile.  */
74666e63ce3Schristos 
74726a53354Schristos   if (mainsub->line_vector == NULL
74866e63ce3Schristos       && mainsub->symtab == NULL)
74966e63ce3Schristos     {
75066e63ce3Schristos       const char *mainbase = lbasename (mainsub->name);
75166e63ce3Schristos       int nr_matches = 0;
75266e63ce3Schristos       struct subfile *prevsub;
75366e63ce3Schristos       struct subfile *mainsub_alias = NULL;
75466e63ce3Schristos       struct subfile *prev_mainsub_alias = NULL;
75566e63ce3Schristos 
75666e63ce3Schristos       prevsub = NULL;
75707163879Schristos       for (subfile = m_subfiles;
75826a53354Schristos 	   subfile != NULL;
75966e63ce3Schristos 	   subfile = subfile->next)
76066e63ce3Schristos 	{
76126a53354Schristos 	  if (subfile == mainsub)
76226a53354Schristos 	    continue;
76366e63ce3Schristos 	  if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
76466e63ce3Schristos 	    {
76566e63ce3Schristos 	      ++nr_matches;
76666e63ce3Schristos 	      mainsub_alias = subfile;
76766e63ce3Schristos 	      prev_mainsub_alias = prevsub;
76866e63ce3Schristos 	    }
76966e63ce3Schristos 	  prevsub = subfile;
77066e63ce3Schristos 	}
77166e63ce3Schristos 
77266e63ce3Schristos       if (nr_matches == 1)
77366e63ce3Schristos 	{
77466e63ce3Schristos 	  gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
77566e63ce3Schristos 
77666e63ce3Schristos 	  /* Found a match for the main source file.
77766e63ce3Schristos 	     Copy its line_vector and symtab to the main subfile
77866e63ce3Schristos 	     and then discard it.  */
77966e63ce3Schristos 
78066e63ce3Schristos 	  mainsub->line_vector = mainsub_alias->line_vector;
78166e63ce3Schristos 	  mainsub->line_vector_length = mainsub_alias->line_vector_length;
78266e63ce3Schristos 	  mainsub->symtab = mainsub_alias->symtab;
78366e63ce3Schristos 
78466e63ce3Schristos 	  if (prev_mainsub_alias == NULL)
78507163879Schristos 	    m_subfiles = mainsub_alias->next;
78666e63ce3Schristos 	  else
78766e63ce3Schristos 	    prev_mainsub_alias->next = mainsub_alias->next;
78826a53354Schristos 	  xfree (mainsub_alias->name);
78966e63ce3Schristos 	  xfree (mainsub_alias);
79066e63ce3Schristos 	}
79166e63ce3Schristos     }
79266e63ce3Schristos }
79366e63ce3Schristos 
79448596154Schristos /* Implementation of the first part of end_symtab.  It allows modifying
79548596154Schristos    STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
79648596154Schristos    If the returned value is NULL there is no blockvector created for
79748596154Schristos    this symtab (you still must call end_symtab_from_static_block).
79848596154Schristos 
79948596154Schristos    END_ADDR is the same as for end_symtab: the address of the end of the
80048596154Schristos    file's text.
80148596154Schristos 
80248596154Schristos    If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
80348596154Schristos    expandable.
80448596154Schristos 
80548596154Schristos    If REQUIRED is non-zero, then a symtab is created even if it does
80648596154Schristos    not contain any symbols.  */
80748596154Schristos 
80848596154Schristos struct block *
end_symtab_get_static_block(CORE_ADDR end_addr,int expandable,int required)80907163879Schristos buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr,
81007163879Schristos 						int expandable, int required)
81148596154Schristos {
81266e63ce3Schristos   /* Finish the lexical context of the last function in the file; pop
81366e63ce3Schristos      the context stack.  */
81466e63ce3Schristos 
81507163879Schristos   if (!m_context_stack.empty ())
81666e63ce3Schristos     {
81707163879Schristos       struct context_stack cstk = pop_context ();
81848596154Schristos 
81966e63ce3Schristos       /* Make a block for the local symbols within.  */
82007163879Schristos       finish_block (cstk.name, cstk.old_blocks, NULL,
82107163879Schristos 		    cstk.start_addr, end_addr);
82266e63ce3Schristos 
82307163879Schristos       if (!m_context_stack.empty ())
82466e63ce3Schristos 	{
82566e63ce3Schristos 	  /* This is said to happen with SCO.  The old coffread.c
82666e63ce3Schristos 	     code simply emptied the context stack, so we do the
82766e63ce3Schristos 	     same.  FIXME: Find out why it is happening.  This is not
82866e63ce3Schristos 	     believed to happen in most cases (even for coffread.c);
82966e63ce3Schristos 	     it used to be an abort().  */
83007163879Schristos 	  complaint (_("Context stack not empty in end_symtab"));
83107163879Schristos 	  m_context_stack.clear ();
83266e63ce3Schristos 	}
83366e63ce3Schristos     }
83466e63ce3Schristos 
83566e63ce3Schristos   /* Reordered executables may have out of order pending blocks; if
83666e63ce3Schristos      OBJF_REORDERED is true, then sort the pending blocks.  */
83748596154Schristos 
83807163879Schristos   if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks)
83966e63ce3Schristos     {
84066e63ce3Schristos       struct pending_block *pb;
84166e63ce3Schristos 
84207163879Schristos       std::vector<block *> barray;
84366e63ce3Schristos 
84407163879Schristos       for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
84507163879Schristos 	barray.push_back (pb->block);
84666e63ce3Schristos 
84707163879Schristos       /* Sort blocks by start address in descending order.  Blocks with the
84807163879Schristos 	 same start address must remain in the original order to preserve
84907163879Schristos 	 inline function caller/callee relationships.  */
85007163879Schristos       std::stable_sort (barray.begin (), barray.end (),
85107163879Schristos 			[] (const block *a, const block *b)
85207163879Schristos 			{
85307163879Schristos 			  return BLOCK_START (a) > BLOCK_START (b);
85407163879Schristos 			});
85566e63ce3Schristos 
85607163879Schristos       int i = 0;
85707163879Schristos       for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
85807163879Schristos 	pb->block = barray[i++];
85966e63ce3Schristos     }
86066e63ce3Schristos 
86166e63ce3Schristos   /* Cleanup any undefined types that have been left hanging around
86266e63ce3Schristos      (this needs to be done before the finish_blocks so that
86366e63ce3Schristos      file_symbols is still good).
86466e63ce3Schristos 
86548596154Schristos      Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
86666e63ce3Schristos      specific, but harmless for other symbol readers, since on gdb
86766e63ce3Schristos      startup or when finished reading stabs, the state is set so these
86866e63ce3Schristos      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
86966e63ce3Schristos      we make this cleaner?  */
87066e63ce3Schristos 
87107163879Schristos   cleanup_undefined_stabs_types (m_objfile);
87207163879Schristos   finish_global_stabs (m_objfile);
87366e63ce3Schristos 
87448596154Schristos   if (!required
87507163879Schristos       && m_pending_blocks == NULL
87607163879Schristos       && m_file_symbols == NULL
87707163879Schristos       && m_global_symbols == NULL
87807163879Schristos       && !m_have_line_numbers
87907163879Schristos       && m_pending_macros == NULL
88007163879Schristos       && m_global_using_directives == NULL)
88166e63ce3Schristos     {
88248596154Schristos       /* Ignore symtabs that have no functions with real debugging info.  */
88348596154Schristos       return NULL;
88448596154Schristos     }
88548596154Schristos   else
88648596154Schristos     {
88748596154Schristos       /* Define the STATIC_BLOCK.  */
88807163879Schristos       return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
88907163879Schristos 				    m_last_source_start_addr,
89007163879Schristos 				    end_addr, 0, expandable);
89148596154Schristos     }
89248596154Schristos }
89348596154Schristos 
89426a53354Schristos /* Subroutine of end_symtab_from_static_block to simplify it.
89526a53354Schristos    Handle the "have blockvector" case.
89626a53354Schristos    See end_symtab_from_static_block for a description of the arguments.  */
89748596154Schristos 
89807163879Schristos struct compunit_symtab *
end_symtab_with_blockvector(struct block * static_block,int section,int expandable)89907163879Schristos buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
90026a53354Schristos 						int section, int expandable)
90148596154Schristos {
90207163879Schristos   struct compunit_symtab *cu = m_compunit_symtab;
90348596154Schristos   struct blockvector *blockvector;
90448596154Schristos   struct subfile *subfile;
90526a53354Schristos   CORE_ADDR end_addr;
90648596154Schristos 
90726a53354Schristos   gdb_assert (static_block != NULL);
90807163879Schristos   gdb_assert (m_subfiles != NULL);
90948596154Schristos 
91026a53354Schristos   end_addr = BLOCK_END (static_block);
91126a53354Schristos 
91226a53354Schristos   /* Create the GLOBAL_BLOCK and build the blockvector.  */
91307163879Schristos   finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
91407163879Schristos 			 m_last_source_start_addr, end_addr,
91548596154Schristos 			 1, expandable);
91626a53354Schristos   blockvector = make_blockvector ();
91766e63ce3Schristos 
9187af5a897Schristos   /* Read the line table if it has to be read separately.
9197af5a897Schristos      This is only used by xcoffread.c.  */
92007163879Schristos   if (m_objfile->sf->sym_read_linetable != NULL)
92107163879Schristos     m_objfile->sf->sym_read_linetable (m_objfile);
92266e63ce3Schristos 
92366e63ce3Schristos   /* Handle the case where the debug info specifies a different path
92466e63ce3Schristos      for the main source file.  It can cause us to lose track of its
92566e63ce3Schristos      line number information.  */
92666e63ce3Schristos   watch_main_source_file_lossage ();
92766e63ce3Schristos 
92826a53354Schristos   /* Now create the symtab objects proper, if not already done,
92926a53354Schristos      one for each subfile.  */
93066e63ce3Schristos 
93107163879Schristos   for (subfile = m_subfiles;
93226a53354Schristos        subfile != NULL;
93326a53354Schristos        subfile = subfile->next)
93466e63ce3Schristos     {
93566e63ce3Schristos       int linetablesize = 0;
93666e63ce3Schristos 
93766e63ce3Schristos       if (subfile->line_vector)
93866e63ce3Schristos 	{
93966e63ce3Schristos 	  linetablesize = sizeof (struct linetable) +
94066e63ce3Schristos 	    subfile->line_vector->nitems * sizeof (struct linetable_entry);
94166e63ce3Schristos 
942*1424dfb3Schristos 	  const auto lte_is_less_than
943*1424dfb3Schristos 	    = [] (const linetable_entry &ln1,
944*1424dfb3Schristos 		  const linetable_entry &ln2) -> bool
945*1424dfb3Schristos 	      {
946*1424dfb3Schristos 		if (ln1.pc == ln2.pc
947*1424dfb3Schristos 		    && ((ln1.line == 0) != (ln2.line == 0)))
948*1424dfb3Schristos 		  return ln1.line == 0;
949*1424dfb3Schristos 
950*1424dfb3Schristos 		return (ln1.pc < ln2.pc);
951*1424dfb3Schristos 	      };
952*1424dfb3Schristos 
953*1424dfb3Schristos 	  /* Like the pending blocks, the line table may be scrambled in
954*1424dfb3Schristos 	     reordered executables.  Sort it if OBJF_REORDERED is true.  It
955*1424dfb3Schristos 	     is important to preserve the order of lines at the same
956*1424dfb3Schristos 	     address, as this maintains the inline function caller/callee
957*1424dfb3Schristos 	     relationships, this is why std::stable_sort is used.  */
95807163879Schristos 	  if (m_objfile->flags & OBJF_REORDERED)
959*1424dfb3Schristos 	    std::stable_sort (subfile->line_vector->item,
960*1424dfb3Schristos 			      subfile->line_vector->item
961*1424dfb3Schristos 			      + subfile->line_vector->nitems,
962*1424dfb3Schristos 			      lte_is_less_than);
96366e63ce3Schristos 	}
96466e63ce3Schristos 
96526a53354Schristos       /* Allocate a symbol table if necessary.  */
96666e63ce3Schristos       if (subfile->symtab == NULL)
96726a53354Schristos 	subfile->symtab = allocate_symtab (cu, subfile->name);
96807163879Schristos       struct symtab *symtab = subfile->symtab;
96966e63ce3Schristos 
97066e63ce3Schristos       /* Fill in its components.  */
97126a53354Schristos 
97266e63ce3Schristos       if (subfile->line_vector)
97366e63ce3Schristos 	{
97466e63ce3Schristos 	  /* Reallocate the line table on the symbol obstack.  */
97526a53354Schristos 	  SYMTAB_LINETABLE (symtab) = (struct linetable *)
97607163879Schristos 	    obstack_alloc (&m_objfile->objfile_obstack, linetablesize);
97726a53354Schristos 	  memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
97826a53354Schristos 		  linetablesize);
97966e63ce3Schristos 	}
98066e63ce3Schristos       else
98166e63ce3Schristos 	{
98226a53354Schristos 	  SYMTAB_LINETABLE (symtab) = NULL;
98366e63ce3Schristos 	}
98466e63ce3Schristos 
98566e63ce3Schristos       /* Use whatever language we have been using for this
98666e63ce3Schristos 	 subfile, not the one that was deduced in allocate_symtab
98766e63ce3Schristos 	 from the filename.  We already did our own deducing when
98866e63ce3Schristos 	 we created the subfile, and we may have altered our
98966e63ce3Schristos 	 opinion of what language it is from things we found in
99066e63ce3Schristos 	 the symbols.  */
99166e63ce3Schristos       symtab->language = subfile->language;
99266e63ce3Schristos     }
99366e63ce3Schristos 
99426a53354Schristos   /* Make sure the symtab of main_subfile is the first in its list.  */
99566e63ce3Schristos   {
99626a53354Schristos     struct symtab *main_symtab, *prev_symtab;
99726a53354Schristos 
99807163879Schristos     main_symtab = m_main_subfile->symtab;
99926a53354Schristos     prev_symtab = NULL;
100007163879Schristos     for (symtab *symtab : compunit_filetabs (cu))
100126a53354Schristos       {
100226a53354Schristos 	if (symtab == main_symtab)
100326a53354Schristos 	  {
100426a53354Schristos 	    if (prev_symtab != NULL)
100526a53354Schristos 	      {
100626a53354Schristos 		prev_symtab->next = main_symtab->next;
100726a53354Schristos 		main_symtab->next = COMPUNIT_FILETABS (cu);
100826a53354Schristos 		COMPUNIT_FILETABS (cu) = main_symtab;
100926a53354Schristos 	      }
101066e63ce3Schristos 	    break;
101166e63ce3Schristos 	  }
101226a53354Schristos 	prev_symtab = symtab;
101366e63ce3Schristos       }
101426a53354Schristos     gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
101566e63ce3Schristos   }
101666e63ce3Schristos 
101726a53354Schristos   /* Fill out the compunit symtab.  */
101866e63ce3Schristos 
101907163879Schristos   if (m_comp_dir != NULL)
102066e63ce3Schristos     {
102126a53354Schristos       /* Reallocate the dirname on the symbol obstack.  */
102207163879Schristos       const char *comp_dir = m_comp_dir.get ();
1023*1424dfb3Schristos       COMPUNIT_DIRNAME (cu) = obstack_strdup (&m_objfile->objfile_obstack,
1024*1424dfb3Schristos 					      comp_dir);
102526a53354Schristos     }
102648596154Schristos 
102726a53354Schristos   /* Save the debug format string (if any) in the symtab.  */
102807163879Schristos   COMPUNIT_DEBUGFORMAT (cu) = m_debugformat;
102926a53354Schristos 
103026a53354Schristos   /* Similarly for the producer.  */
103107163879Schristos   COMPUNIT_PRODUCER (cu) = m_producer;
103226a53354Schristos 
103326a53354Schristos   COMPUNIT_BLOCKVECTOR (cu) = blockvector;
103448596154Schristos   {
103526a53354Schristos     struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
103648596154Schristos 
103726a53354Schristos     set_block_compunit_symtab (b, cu);
103866e63ce3Schristos   }
103966e63ce3Schristos 
104026a53354Schristos   COMPUNIT_BLOCK_LINE_SECTION (cu) = section;
104126a53354Schristos 
104207163879Schristos   COMPUNIT_MACRO_TABLE (cu) = release_macros ();
104326a53354Schristos 
104426a53354Schristos   /* Default any symbols without a specified symtab to the primary symtab.  */
104566e63ce3Schristos   {
104666e63ce3Schristos     int block_i;
104766e63ce3Schristos 
104826a53354Schristos     /* The main source file's symtab.  */
104907163879Schristos     struct symtab *symtab = COMPUNIT_FILETABS (cu);
105026a53354Schristos 
105166e63ce3Schristos     for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
105266e63ce3Schristos       {
105366e63ce3Schristos 	struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
105466e63ce3Schristos 	struct symbol *sym;
105507163879Schristos 	struct mdict_iterator miter;
105666e63ce3Schristos 
105766e63ce3Schristos 	/* Inlined functions may have symbols not in the global or
105866e63ce3Schristos 	   static symbol lists.  */
105966e63ce3Schristos 	if (BLOCK_FUNCTION (block) != NULL)
106026a53354Schristos 	  if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
106126a53354Schristos 	    symbol_set_symtab (BLOCK_FUNCTION (block), symtab);
106266e63ce3Schristos 
106348596154Schristos 	/* Note that we only want to fix up symbols from the local
106448596154Schristos 	   blocks, not blocks coming from included symtabs.  That is why
106548596154Schristos 	   we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
106607163879Schristos 	ALL_DICT_SYMBOLS (BLOCK_MULTIDICT (block), miter, sym)
106726a53354Schristos 	  if (symbol_symtab (sym) == NULL)
106826a53354Schristos 	    symbol_set_symtab (sym, symtab);
106966e63ce3Schristos       }
107066e63ce3Schristos   }
107166e63ce3Schristos 
107226a53354Schristos   add_compunit_symtab_to_objfile (cu);
107326a53354Schristos 
107426a53354Schristos   return cu;
107526a53354Schristos }
107626a53354Schristos 
107726a53354Schristos /* Implementation of the second part of end_symtab.  Pass STATIC_BLOCK
107826a53354Schristos    as value returned by end_symtab_get_static_block.
107926a53354Schristos 
108026a53354Schristos    SECTION is the same as for end_symtab: the section number
108126a53354Schristos    (in objfile->section_offsets) of the blockvector and linetable.
108226a53354Schristos 
108326a53354Schristos    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
108426a53354Schristos    expandable.  */
108526a53354Schristos 
108626a53354Schristos struct compunit_symtab *
end_symtab_from_static_block(struct block * static_block,int section,int expandable)108707163879Schristos buildsym_compunit::end_symtab_from_static_block (struct block *static_block,
108826a53354Schristos 						 int section, int expandable)
108926a53354Schristos {
109026a53354Schristos   struct compunit_symtab *cu;
109126a53354Schristos 
109226a53354Schristos   if (static_block == NULL)
109326a53354Schristos     {
109426a53354Schristos       /* Handle the "no blockvector" case.
109526a53354Schristos 	 When this happens there is nothing to record, so there's nothing
109626a53354Schristos 	 to do: memory will be freed up later.
109726a53354Schristos 
109826a53354Schristos 	 Note: We won't be adding a compunit to the objfile's list of
109926a53354Schristos 	 compunits, so there's nothing to unchain.  However, since each symtab
110026a53354Schristos 	 is added to the objfile's obstack we can't free that space.
110126a53354Schristos 	 We could do better, but this is believed to be a sufficiently rare
110226a53354Schristos 	 event.  */
110326a53354Schristos       cu = NULL;
110426a53354Schristos     }
110526a53354Schristos   else
110626a53354Schristos     cu = end_symtab_with_blockvector (static_block, section, expandable);
110726a53354Schristos 
110826a53354Schristos   return cu;
110966e63ce3Schristos }
111066e63ce3Schristos 
111148596154Schristos /* Finish the symbol definitions for one main source file, close off
111248596154Schristos    all the lexical contexts for that file (creating struct block's for
111348596154Schristos    them), then make the struct symtab for that file and put it in the
111448596154Schristos    list of all such.
111548596154Schristos 
111648596154Schristos    END_ADDR is the address of the end of the file's text.  SECTION is
111748596154Schristos    the section number (in objfile->section_offsets) of the blockvector
111848596154Schristos    and linetable.
111948596154Schristos 
112048596154Schristos    Note that it is possible for end_symtab() to return NULL.  In
112148596154Schristos    particular, for the DWARF case at least, it will return NULL when
112248596154Schristos    it finds a compilation unit that has exactly one DIE, a
112348596154Schristos    TAG_compile_unit DIE.  This can happen when we link in an object
112448596154Schristos    file that was compiled from an empty source file.  Returning NULL
112548596154Schristos    is probably not the correct thing to do, because then gdb will
112648596154Schristos    never know about this empty file (FIXME).
112748596154Schristos 
112848596154Schristos    If you need to modify STATIC_BLOCK before it is finalized you should
112948596154Schristos    call end_symtab_get_static_block and end_symtab_from_static_block
113048596154Schristos    yourself.  */
113148596154Schristos 
113226a53354Schristos struct compunit_symtab *
end_symtab(CORE_ADDR end_addr,int section)113307163879Schristos buildsym_compunit::end_symtab (CORE_ADDR end_addr, int section)
113448596154Schristos {
113548596154Schristos   struct block *static_block;
113648596154Schristos 
113726a53354Schristos   static_block = end_symtab_get_static_block (end_addr, 0, 0);
113826a53354Schristos   return end_symtab_from_static_block (static_block, section, 0);
113948596154Schristos }
114048596154Schristos 
114148596154Schristos /* Same as end_symtab except create a symtab that can be later added to.  */
114248596154Schristos 
114326a53354Schristos struct compunit_symtab *
end_expandable_symtab(CORE_ADDR end_addr,int section)114407163879Schristos buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
114548596154Schristos {
114648596154Schristos   struct block *static_block;
114748596154Schristos 
114826a53354Schristos   static_block = end_symtab_get_static_block (end_addr, 1, 0);
114926a53354Schristos   return end_symtab_from_static_block (static_block, section, 1);
115048596154Schristos }
115148596154Schristos 
115248596154Schristos /* Subroutine of augment_type_symtab to simplify it.
115326a53354Schristos    Attach the main source file's symtab to all symbols in PENDING_LIST that
115426a53354Schristos    don't have one.  */
115548596154Schristos 
115648596154Schristos static void
set_missing_symtab(struct pending * pending_list,struct compunit_symtab * cu)115726a53354Schristos set_missing_symtab (struct pending *pending_list,
115826a53354Schristos 		    struct compunit_symtab *cu)
115948596154Schristos {
116048596154Schristos   struct pending *pending;
116148596154Schristos   int i;
116248596154Schristos 
116348596154Schristos   for (pending = pending_list; pending != NULL; pending = pending->next)
116448596154Schristos     {
116548596154Schristos       for (i = 0; i < pending->nsyms; ++i)
116648596154Schristos 	{
116726a53354Schristos 	  if (symbol_symtab (pending->symbol[i]) == NULL)
116826a53354Schristos 	    symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu));
116948596154Schristos 	}
117048596154Schristos     }
117148596154Schristos }
117248596154Schristos 
117348596154Schristos /* Same as end_symtab, but for the case where we're adding more symbols
117448596154Schristos    to an existing symtab that is known to contain only type information.
117548596154Schristos    This is the case for DWARF4 Type Units.  */
117648596154Schristos 
117748596154Schristos void
augment_type_symtab()117807163879Schristos buildsym_compunit::augment_type_symtab ()
117948596154Schristos {
118007163879Schristos   struct compunit_symtab *cust = m_compunit_symtab;
118126a53354Schristos   const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);
118248596154Schristos 
118307163879Schristos   if (!m_context_stack.empty ())
118407163879Schristos     complaint (_("Context stack not empty in augment_type_symtab"));
118507163879Schristos   if (m_pending_blocks != NULL)
118607163879Schristos     complaint (_("Blocks in a type symtab"));
118707163879Schristos   if (m_pending_macros != NULL)
118807163879Schristos     complaint (_("Macro in a type symtab"));
118907163879Schristos   if (m_have_line_numbers)
119007163879Schristos     complaint (_("Line numbers recorded in a type symtab"));
119148596154Schristos 
119207163879Schristos   if (m_file_symbols != NULL)
119348596154Schristos     {
119448596154Schristos       struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
119548596154Schristos 
119648596154Schristos       /* First mark any symbols without a specified symtab as belonging
119748596154Schristos 	 to the primary symtab.  */
119807163879Schristos       set_missing_symtab (m_file_symbols, cust);
119948596154Schristos 
120007163879Schristos       mdict_add_pending (BLOCK_MULTIDICT (block), m_file_symbols);
120148596154Schristos     }
120248596154Schristos 
120307163879Schristos   if (m_global_symbols != NULL)
120448596154Schristos     {
120548596154Schristos       struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
120648596154Schristos 
120748596154Schristos       /* First mark any symbols without a specified symtab as belonging
120848596154Schristos 	 to the primary symtab.  */
120907163879Schristos       set_missing_symtab (m_global_symbols, cust);
121048596154Schristos 
121107163879Schristos       mdict_add_pending (BLOCK_MULTIDICT (block),
121207163879Schristos 			m_global_symbols);
121348596154Schristos     }
121448596154Schristos }
121548596154Schristos 
121666e63ce3Schristos /* Push a context block.  Args are an identifying nesting level
121766e63ce3Schristos    (checkable when you pop it), and the starting PC address of this
121866e63ce3Schristos    context.  */
121966e63ce3Schristos 
122066e63ce3Schristos struct context_stack *
push_context(int desc,CORE_ADDR valu)122107163879Schristos buildsym_compunit::push_context (int desc, CORE_ADDR valu)
122266e63ce3Schristos {
122307163879Schristos   m_context_stack.emplace_back ();
122407163879Schristos   struct context_stack *newobj = &m_context_stack.back ();
122566e63ce3Schristos 
1226ed6a76a9Schristos   newobj->depth = desc;
122707163879Schristos   newobj->locals = m_local_symbols;
122807163879Schristos   newobj->old_blocks = m_pending_blocks;
1229ed6a76a9Schristos   newobj->start_addr = valu;
123007163879Schristos   newobj->local_using_directives = m_local_using_directives;
1231ed6a76a9Schristos   newobj->name = NULL;
123266e63ce3Schristos 
123307163879Schristos   m_local_symbols = NULL;
123407163879Schristos   m_local_using_directives = NULL;
123566e63ce3Schristos 
1236ed6a76a9Schristos   return newobj;
123766e63ce3Schristos }
123866e63ce3Schristos 
123966e63ce3Schristos /* Pop a context block.  Returns the address of the context block just
124066e63ce3Schristos    popped.  */
124166e63ce3Schristos 
124207163879Schristos struct context_stack
pop_context()124307163879Schristos buildsym_compunit::pop_context ()
124466e63ce3Schristos {
124507163879Schristos   gdb_assert (!m_context_stack.empty ());
124607163879Schristos   struct context_stack result = m_context_stack.back ();
124707163879Schristos   m_context_stack.pop_back ();
124807163879Schristos   return result;
124966e63ce3Schristos }
1250