xref: /dflybsd-src/contrib/gdb-7/gdb/buildsym.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Support routines for building symbol tables in GDB's internal format.
2*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    This file is part of GDB.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert    (at your option) any later version.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145796c8dcSSimon Schubert    GNU General Public License for more details.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert /* This module provides subroutines used for creating and adding to
205796c8dcSSimon Schubert    the symbol table.  These routines are called from various symbol-
215796c8dcSSimon Schubert    file-reading routines.
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert    Routines to support specific debugging information formats (stabs,
245796c8dcSSimon Schubert    DWARF, etc) belong somewhere else.  */
255796c8dcSSimon Schubert 
265796c8dcSSimon Schubert #include "defs.h"
275796c8dcSSimon Schubert #include "bfd.h"
285796c8dcSSimon Schubert #include "gdb_obstack.h"
295796c8dcSSimon Schubert #include "symtab.h"
305796c8dcSSimon Schubert #include "symfile.h"
315796c8dcSSimon Schubert #include "objfiles.h"
325796c8dcSSimon Schubert #include "gdbtypes.h"
335796c8dcSSimon Schubert #include "gdb_assert.h"
345796c8dcSSimon Schubert #include "complaints.h"
355796c8dcSSimon Schubert #include "gdb_string.h"
365796c8dcSSimon Schubert #include "expression.h"		/* For "enum exp_opcode" used by...  */
375796c8dcSSimon Schubert #include "bcache.h"
38c50c785cSJohn Marino #include "filenames.h"		/* For DOSish file names.  */
395796c8dcSSimon Schubert #include "macrotab.h"
405796c8dcSSimon Schubert #include "demangle.h"		/* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
415796c8dcSSimon Schubert #include "block.h"
425796c8dcSSimon Schubert #include "cp-support.h"
435796c8dcSSimon Schubert #include "dictionary.h"
445796c8dcSSimon Schubert #include "addrmap.h"
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* Ask buildsym.h to define the vars it normally declares `extern'.  */
475796c8dcSSimon Schubert #define	EXTERN
485796c8dcSSimon Schubert /**/
49c50c785cSJohn Marino #include "buildsym.h"		/* Our own declarations.  */
505796c8dcSSimon Schubert #undef	EXTERN
515796c8dcSSimon Schubert 
52*ef5ccd6cSJohn Marino /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
535796c8dcSSimon Schubert    questionable--see comment where we call them).  */
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert #include "stabsread.h"
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert /* List of subfiles.  */
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert static struct subfile *subfiles;
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert /* List of free `struct pending' structures for reuse.  */
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert static struct pending *free_pendings;
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* Non-zero if symtab has line number info.  This prevents an
665796c8dcSSimon Schubert    otherwise empty symtab from being tossed.  */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert static int have_line_numbers;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert /* The mutable address map for the compilation unit whose symbols
715796c8dcSSimon Schubert    we're currently reading.  The symtabs' shared blockvector will
725796c8dcSSimon Schubert    point to a fixed copy of this.  */
735796c8dcSSimon Schubert static struct addrmap *pending_addrmap;
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert /* The obstack on which we allocate pending_addrmap.
765796c8dcSSimon Schubert    If pending_addrmap is NULL, this is uninitialized; otherwise, it is
775796c8dcSSimon Schubert    initialized (and holds pending_addrmap).  */
785796c8dcSSimon Schubert static struct obstack pending_addrmap_obstack;
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert /* Non-zero if we recorded any ranges in the addrmap that are
815796c8dcSSimon Schubert    different from those in the blockvector already.  We set this to
825796c8dcSSimon Schubert    zero when we start processing a symfile, and if it's still zero at
835796c8dcSSimon Schubert    the end, then we just toss the addrmap.  */
845796c8dcSSimon Schubert static int pending_addrmap_interesting;
855796c8dcSSimon Schubert 
86*ef5ccd6cSJohn Marino /* An obstack used for allocating pending blocks.  */
87*ef5ccd6cSJohn Marino 
88*ef5ccd6cSJohn Marino static struct obstack pending_block_obstack;
89*ef5ccd6cSJohn Marino 
90*ef5ccd6cSJohn Marino /* List of blocks already made (lexical contexts already closed).
91*ef5ccd6cSJohn Marino    This is used at the end to make the blockvector.  */
92*ef5ccd6cSJohn Marino 
93*ef5ccd6cSJohn Marino struct pending_block
94*ef5ccd6cSJohn Marino   {
95*ef5ccd6cSJohn Marino     struct pending_block *next;
96*ef5ccd6cSJohn Marino     struct block *block;
97*ef5ccd6cSJohn Marino   };
98*ef5ccd6cSJohn Marino 
99*ef5ccd6cSJohn Marino /* Pointer to the head of a linked list of symbol blocks which have
100*ef5ccd6cSJohn Marino    already been finalized (lexical contexts already closed) and which
101*ef5ccd6cSJohn Marino    are just waiting to be built into a blockvector when finalizing the
102*ef5ccd6cSJohn Marino    associated symtab.  */
103*ef5ccd6cSJohn Marino 
104*ef5ccd6cSJohn Marino static struct pending_block *pending_blocks;
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert static int compare_line_numbers (const void *ln1p, const void *ln2p);
107*ef5ccd6cSJohn Marino 
108*ef5ccd6cSJohn Marino static void record_pending_block (struct objfile *objfile,
109*ef5ccd6cSJohn Marino 				  struct block *block,
110*ef5ccd6cSJohn Marino 				  struct pending_block *opblock);
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert /* Initial sizes of data structures.  These are realloc'd larger if
1145796c8dcSSimon Schubert    needed, and realloc'd down to the size actually used, when
1155796c8dcSSimon Schubert    completed.  */
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert #define	INITIAL_CONTEXT_STACK_SIZE	10
1185796c8dcSSimon Schubert #define	INITIAL_LINE_VECTOR_LENGTH	1000
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert 
121c50c785cSJohn Marino /* Maintain the lists of symbols and blocks.  */
1225796c8dcSSimon Schubert 
123a45ae5f8SJohn Marino /* Add a symbol to one of the lists of symbols.  */
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert void
add_symbol_to_list(struct symbol * symbol,struct pending ** listhead)1265796c8dcSSimon Schubert add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
1275796c8dcSSimon Schubert {
1285796c8dcSSimon Schubert   struct pending *link;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   /* If this is an alias for another symbol, don't add it.  */
1315796c8dcSSimon Schubert   if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
1325796c8dcSSimon Schubert     return;
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert   /* We keep PENDINGSIZE symbols in each link of the list.  If we
1355796c8dcSSimon Schubert      don't have a link with room in it, add a new link.  */
1365796c8dcSSimon Schubert   if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
1375796c8dcSSimon Schubert     {
1385796c8dcSSimon Schubert       if (free_pendings)
1395796c8dcSSimon Schubert 	{
1405796c8dcSSimon Schubert 	  link = free_pendings;
1415796c8dcSSimon Schubert 	  free_pendings = link->next;
1425796c8dcSSimon Schubert 	}
1435796c8dcSSimon Schubert       else
1445796c8dcSSimon Schubert 	{
1455796c8dcSSimon Schubert 	  link = (struct pending *) xmalloc (sizeof (struct pending));
1465796c8dcSSimon Schubert 	}
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert       link->next = *listhead;
1495796c8dcSSimon Schubert       *listhead = link;
1505796c8dcSSimon Schubert       link->nsyms = 0;
1515796c8dcSSimon Schubert     }
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
1545796c8dcSSimon Schubert }
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert /* Find a symbol named NAME on a LIST.  NAME need not be
1575796c8dcSSimon Schubert    '\0'-terminated; LENGTH is the length of the name.  */
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert struct symbol *
find_symbol_in_list(struct pending * list,char * name,int length)1605796c8dcSSimon Schubert find_symbol_in_list (struct pending *list, char *name, int length)
1615796c8dcSSimon Schubert {
1625796c8dcSSimon Schubert   int j;
163*ef5ccd6cSJohn Marino   const char *pp;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert   while (list != NULL)
1665796c8dcSSimon Schubert     {
1675796c8dcSSimon Schubert       for (j = list->nsyms; --j >= 0;)
1685796c8dcSSimon Schubert 	{
1695796c8dcSSimon Schubert 	  pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
170cf7f2e2dSJohn Marino 	  if (*pp == *name && strncmp (pp, name, length) == 0
171cf7f2e2dSJohn Marino 	      && pp[length] == '\0')
1725796c8dcSSimon Schubert 	    {
1735796c8dcSSimon Schubert 	      return (list->symbol[j]);
1745796c8dcSSimon Schubert 	    }
1755796c8dcSSimon Schubert 	}
1765796c8dcSSimon Schubert       list = list->next;
1775796c8dcSSimon Schubert     }
1785796c8dcSSimon Schubert   return (NULL);
1795796c8dcSSimon Schubert }
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert /* At end of reading syms, or in case of quit, really free as many
1825796c8dcSSimon Schubert    `struct pending's as we can easily find.  */
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert void
really_free_pendings(void * dummy)1855796c8dcSSimon Schubert really_free_pendings (void *dummy)
1865796c8dcSSimon Schubert {
1875796c8dcSSimon Schubert   struct pending *next, *next1;
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert   for (next = free_pendings; next; next = next1)
1905796c8dcSSimon Schubert     {
1915796c8dcSSimon Schubert       next1 = next->next;
1925796c8dcSSimon Schubert       xfree ((void *) next);
1935796c8dcSSimon Schubert     }
1945796c8dcSSimon Schubert   free_pendings = NULL;
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert   free_pending_blocks ();
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert   for (next = file_symbols; next != NULL; next = next1)
1995796c8dcSSimon Schubert     {
2005796c8dcSSimon Schubert       next1 = next->next;
2015796c8dcSSimon Schubert       xfree ((void *) next);
2025796c8dcSSimon Schubert     }
2035796c8dcSSimon Schubert   file_symbols = NULL;
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert   for (next = global_symbols; next != NULL; next = next1)
2065796c8dcSSimon Schubert     {
2075796c8dcSSimon Schubert       next1 = next->next;
2085796c8dcSSimon Schubert       xfree ((void *) next);
2095796c8dcSSimon Schubert     }
2105796c8dcSSimon Schubert   global_symbols = NULL;
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert   if (pending_macros)
2135796c8dcSSimon Schubert     free_macro_table (pending_macros);
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert   if (pending_addrmap)
2165796c8dcSSimon Schubert     {
2175796c8dcSSimon Schubert       obstack_free (&pending_addrmap_obstack, NULL);
2185796c8dcSSimon Schubert       pending_addrmap = NULL;
2195796c8dcSSimon Schubert     }
2205796c8dcSSimon Schubert }
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert /* This function is called to discard any pending blocks.  */
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert void
free_pending_blocks(void)2255796c8dcSSimon Schubert free_pending_blocks (void)
2265796c8dcSSimon Schubert {
227*ef5ccd6cSJohn Marino   if (pending_blocks != NULL)
228*ef5ccd6cSJohn Marino     {
229*ef5ccd6cSJohn Marino       obstack_free (&pending_block_obstack, NULL);
2305796c8dcSSimon Schubert       pending_blocks = NULL;
2315796c8dcSSimon Schubert     }
232*ef5ccd6cSJohn Marino }
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert /* Take one of the lists of symbols and make a block from it.  Keep
2355796c8dcSSimon Schubert    the order the symbols have in the list (reversed from the input
2365796c8dcSSimon Schubert    file).  Put the block on the list of pending blocks.  */
2375796c8dcSSimon Schubert 
238*ef5ccd6cSJohn Marino static struct block *
finish_block_internal(struct symbol * symbol,struct pending ** listhead,struct pending_block * old_blocks,CORE_ADDR start,CORE_ADDR end,struct objfile * objfile,int is_global,int expandable)239*ef5ccd6cSJohn Marino finish_block_internal (struct symbol *symbol, struct pending **listhead,
2405796c8dcSSimon Schubert 		       struct pending_block *old_blocks,
2415796c8dcSSimon Schubert 		       CORE_ADDR start, CORE_ADDR end,
242*ef5ccd6cSJohn Marino 		       struct objfile *objfile,
243*ef5ccd6cSJohn Marino 		       int is_global, int expandable)
2445796c8dcSSimon Schubert {
2455796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2465796c8dcSSimon Schubert   struct pending *next, *next1;
2475796c8dcSSimon Schubert   struct block *block;
2485796c8dcSSimon Schubert   struct pending_block *pblock;
2495796c8dcSSimon Schubert   struct pending_block *opblock;
2505796c8dcSSimon Schubert 
251*ef5ccd6cSJohn Marino   block = (is_global
252*ef5ccd6cSJohn Marino 	   ? allocate_global_block (&objfile->objfile_obstack)
253*ef5ccd6cSJohn Marino 	   : allocate_block (&objfile->objfile_obstack));
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert   if (symbol)
2565796c8dcSSimon Schubert     {
2575796c8dcSSimon Schubert       BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
2585796c8dcSSimon Schubert 					       *listhead);
2595796c8dcSSimon Schubert     }
2605796c8dcSSimon Schubert   else
2615796c8dcSSimon Schubert     {
262*ef5ccd6cSJohn Marino       if (expandable)
263*ef5ccd6cSJohn Marino 	{
264*ef5ccd6cSJohn Marino 	  BLOCK_DICT (block) = dict_create_hashed_expandable ();
265*ef5ccd6cSJohn Marino 	  dict_add_pending (BLOCK_DICT (block), *listhead);
266*ef5ccd6cSJohn Marino 	}
267*ef5ccd6cSJohn Marino       else
268*ef5ccd6cSJohn Marino 	{
269*ef5ccd6cSJohn Marino 	  BLOCK_DICT (block) =
270*ef5ccd6cSJohn Marino 	    dict_create_hashed (&objfile->objfile_obstack, *listhead);
271*ef5ccd6cSJohn Marino 	}
2725796c8dcSSimon Schubert     }
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert   BLOCK_START (block) = start;
2755796c8dcSSimon Schubert   BLOCK_END (block) = end;
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert   /* Put the block in as the value of the symbol that names it.  */
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert   if (symbol)
2805796c8dcSSimon Schubert     {
2815796c8dcSSimon Schubert       struct type *ftype = SYMBOL_TYPE (symbol);
2825796c8dcSSimon Schubert       struct dict_iterator iter;
2835796c8dcSSimon Schubert       SYMBOL_BLOCK_VALUE (symbol) = block;
2845796c8dcSSimon Schubert       BLOCK_FUNCTION (block) = symbol;
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert       if (TYPE_NFIELDS (ftype) <= 0)
2875796c8dcSSimon Schubert 	{
2885796c8dcSSimon Schubert 	  /* No parameter type information is recorded with the
2895796c8dcSSimon Schubert 	     function's type.  Set that from the type of the
2905796c8dcSSimon Schubert 	     parameter symbols.  */
2915796c8dcSSimon Schubert 	  int nparams = 0, iparams;
2925796c8dcSSimon Schubert 	  struct symbol *sym;
293*ef5ccd6cSJohn Marino 
294*ef5ccd6cSJohn Marino 	  /* Here we want to directly access the dictionary, because
295*ef5ccd6cSJohn Marino 	     we haven't fully initialized the block yet.  */
296*ef5ccd6cSJohn Marino 	  ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
2975796c8dcSSimon Schubert 	    {
2985796c8dcSSimon Schubert 	      if (SYMBOL_IS_ARGUMENT (sym))
2995796c8dcSSimon Schubert 		nparams++;
3005796c8dcSSimon Schubert 	    }
3015796c8dcSSimon Schubert 	  if (nparams > 0)
3025796c8dcSSimon Schubert 	    {
3035796c8dcSSimon Schubert 	      TYPE_NFIELDS (ftype) = nparams;
3045796c8dcSSimon Schubert 	      TYPE_FIELDS (ftype) = (struct field *)
3055796c8dcSSimon Schubert 		TYPE_ALLOC (ftype, nparams * sizeof (struct field));
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert 	      iparams = 0;
308*ef5ccd6cSJohn Marino 	      /* Here we want to directly access the dictionary, because
309*ef5ccd6cSJohn Marino 		 we haven't fully initialized the block yet.  */
310*ef5ccd6cSJohn Marino 	      ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
3115796c8dcSSimon Schubert 		{
3125796c8dcSSimon Schubert 		  if (iparams == nparams)
3135796c8dcSSimon Schubert 		    break;
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert 		  if (SYMBOL_IS_ARGUMENT (sym))
3165796c8dcSSimon Schubert 		    {
3175796c8dcSSimon Schubert 		      TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
3185796c8dcSSimon Schubert 		      TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3195796c8dcSSimon Schubert 		      iparams++;
3205796c8dcSSimon Schubert 		    }
3215796c8dcSSimon Schubert 		}
3225796c8dcSSimon Schubert 	    }
3235796c8dcSSimon Schubert 	}
3245796c8dcSSimon Schubert     }
3255796c8dcSSimon Schubert   else
3265796c8dcSSimon Schubert     {
3275796c8dcSSimon Schubert       BLOCK_FUNCTION (block) = NULL;
3285796c8dcSSimon Schubert     }
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   /* Now "free" the links of the list, and empty the list.  */
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert   for (next = *listhead; next; next = next1)
3335796c8dcSSimon Schubert     {
3345796c8dcSSimon Schubert       next1 = next->next;
3355796c8dcSSimon Schubert       next->next = free_pendings;
3365796c8dcSSimon Schubert       free_pendings = next;
3375796c8dcSSimon Schubert     }
3385796c8dcSSimon Schubert   *listhead = NULL;
3395796c8dcSSimon Schubert 
3405796c8dcSSimon Schubert   /* Check to be sure that the blocks have an end address that is
341c50c785cSJohn Marino      greater than starting address.  */
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert   if (BLOCK_END (block) < BLOCK_START (block))
3445796c8dcSSimon Schubert     {
3455796c8dcSSimon Schubert       if (symbol)
3465796c8dcSSimon Schubert 	{
3475796c8dcSSimon Schubert 	  complaint (&symfile_complaints,
348c50c785cSJohn Marino 		     _("block end address less than block "
349c50c785cSJohn Marino 		       "start address in %s (patched it)"),
3505796c8dcSSimon Schubert 		     SYMBOL_PRINT_NAME (symbol));
3515796c8dcSSimon Schubert 	}
3525796c8dcSSimon Schubert       else
3535796c8dcSSimon Schubert 	{
3545796c8dcSSimon Schubert 	  complaint (&symfile_complaints,
355c50c785cSJohn Marino 		     _("block end address %s less than block "
356c50c785cSJohn Marino 		       "start address %s (patched it)"),
3575796c8dcSSimon Schubert 		     paddress (gdbarch, BLOCK_END (block)),
3585796c8dcSSimon Schubert 		     paddress (gdbarch, BLOCK_START (block)));
3595796c8dcSSimon Schubert 	}
360c50c785cSJohn Marino       /* Better than nothing.  */
3615796c8dcSSimon Schubert       BLOCK_END (block) = BLOCK_START (block);
3625796c8dcSSimon Schubert     }
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert   /* Install this block as the superblock of all blocks made since the
3655796c8dcSSimon Schubert      start of this scope that don't have superblocks yet.  */
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert   opblock = NULL;
3685796c8dcSSimon Schubert   for (pblock = pending_blocks;
3695796c8dcSSimon Schubert        pblock && pblock != old_blocks;
3705796c8dcSSimon Schubert        pblock = pblock->next)
3715796c8dcSSimon Schubert     {
3725796c8dcSSimon Schubert       if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
3735796c8dcSSimon Schubert 	{
3745796c8dcSSimon Schubert 	  /* Check to be sure the blocks are nested as we receive
3755796c8dcSSimon Schubert 	     them.  If the compiler/assembler/linker work, this just
3765796c8dcSSimon Schubert 	     burns a small amount of time.
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert 	     Skip blocks which correspond to a function; they're not
3795796c8dcSSimon Schubert 	     physically nested inside this other blocks, only
3805796c8dcSSimon Schubert 	     lexically nested.  */
3815796c8dcSSimon Schubert 	  if (BLOCK_FUNCTION (pblock->block) == NULL
3825796c8dcSSimon Schubert 	      && (BLOCK_START (pblock->block) < BLOCK_START (block)
3835796c8dcSSimon Schubert 		  || BLOCK_END (pblock->block) > BLOCK_END (block)))
3845796c8dcSSimon Schubert 	    {
3855796c8dcSSimon Schubert 	      if (symbol)
3865796c8dcSSimon Schubert 		{
3875796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
3885796c8dcSSimon Schubert 			     _("inner block not inside outer block in %s"),
3895796c8dcSSimon Schubert 			     SYMBOL_PRINT_NAME (symbol));
3905796c8dcSSimon Schubert 		}
3915796c8dcSSimon Schubert 	      else
3925796c8dcSSimon Schubert 		{
3935796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
394c50c785cSJohn Marino 			     _("inner block (%s-%s) not "
395c50c785cSJohn Marino 			       "inside outer block (%s-%s)"),
3965796c8dcSSimon Schubert 			     paddress (gdbarch, BLOCK_START (pblock->block)),
3975796c8dcSSimon Schubert 			     paddress (gdbarch, BLOCK_END (pblock->block)),
3985796c8dcSSimon Schubert 			     paddress (gdbarch, BLOCK_START (block)),
3995796c8dcSSimon Schubert 			     paddress (gdbarch, BLOCK_END (block)));
4005796c8dcSSimon Schubert 		}
4015796c8dcSSimon Schubert 	      if (BLOCK_START (pblock->block) < BLOCK_START (block))
4025796c8dcSSimon Schubert 		BLOCK_START (pblock->block) = BLOCK_START (block);
4035796c8dcSSimon Schubert 	      if (BLOCK_END (pblock->block) > BLOCK_END (block))
4045796c8dcSSimon Schubert 		BLOCK_END (pblock->block) = BLOCK_END (block);
4055796c8dcSSimon Schubert 	    }
4065796c8dcSSimon Schubert 	  BLOCK_SUPERBLOCK (pblock->block) = block;
4075796c8dcSSimon Schubert 	}
4085796c8dcSSimon Schubert       opblock = pblock;
4095796c8dcSSimon Schubert     }
4105796c8dcSSimon Schubert 
4115796c8dcSSimon Schubert   block_set_using (block, using_directives, &objfile->objfile_obstack);
412cf7f2e2dSJohn Marino   using_directives = NULL;
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   record_pending_block (objfile, block, opblock);
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert   return block;
4175796c8dcSSimon Schubert }
4185796c8dcSSimon Schubert 
419*ef5ccd6cSJohn Marino struct block *
finish_block(struct symbol * symbol,struct pending ** listhead,struct pending_block * old_blocks,CORE_ADDR start,CORE_ADDR end,struct objfile * objfile)420*ef5ccd6cSJohn Marino finish_block (struct symbol *symbol, struct pending **listhead,
421*ef5ccd6cSJohn Marino 	      struct pending_block *old_blocks,
422*ef5ccd6cSJohn Marino 	      CORE_ADDR start, CORE_ADDR end,
423*ef5ccd6cSJohn Marino 	      struct objfile *objfile)
424*ef5ccd6cSJohn Marino {
425*ef5ccd6cSJohn Marino   return finish_block_internal (symbol, listhead, old_blocks,
426*ef5ccd6cSJohn Marino 				start, end, objfile, 0, 0);
427*ef5ccd6cSJohn Marino }
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert /* Record BLOCK on the list of all blocks in the file.  Put it after
4305796c8dcSSimon Schubert    OPBLOCK, or at the beginning if opblock is NULL.  This puts the
4315796c8dcSSimon Schubert    block in the list after all its subblocks.
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert    Allocate the pending block struct in the objfile_obstack to save
4345796c8dcSSimon Schubert    time.  This wastes a little space.  FIXME: Is it worth it?  */
4355796c8dcSSimon Schubert 
436*ef5ccd6cSJohn Marino static void
record_pending_block(struct objfile * objfile,struct block * block,struct pending_block * opblock)4375796c8dcSSimon Schubert record_pending_block (struct objfile *objfile, struct block *block,
4385796c8dcSSimon Schubert 		      struct pending_block *opblock)
4395796c8dcSSimon Schubert {
4405796c8dcSSimon Schubert   struct pending_block *pblock;
4415796c8dcSSimon Schubert 
442*ef5ccd6cSJohn Marino   if (pending_blocks == NULL)
443*ef5ccd6cSJohn Marino     obstack_init (&pending_block_obstack);
444*ef5ccd6cSJohn Marino 
4455796c8dcSSimon Schubert   pblock = (struct pending_block *)
446*ef5ccd6cSJohn Marino     obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
4475796c8dcSSimon Schubert   pblock->block = block;
4485796c8dcSSimon Schubert   if (opblock)
4495796c8dcSSimon Schubert     {
4505796c8dcSSimon Schubert       pblock->next = opblock->next;
4515796c8dcSSimon Schubert       opblock->next = pblock;
4525796c8dcSSimon Schubert     }
4535796c8dcSSimon Schubert   else
4545796c8dcSSimon Schubert     {
4555796c8dcSSimon Schubert       pblock->next = pending_blocks;
4565796c8dcSSimon Schubert       pending_blocks = pblock;
4575796c8dcSSimon Schubert     }
4585796c8dcSSimon Schubert }
4595796c8dcSSimon Schubert 
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert /* Record that the range of addresses from START to END_INCLUSIVE
4625796c8dcSSimon Schubert    (inclusive, like it says) belongs to BLOCK.  BLOCK's start and end
4635796c8dcSSimon Schubert    addresses must be set already.  You must apply this function to all
4645796c8dcSSimon Schubert    BLOCK's children before applying it to BLOCK.
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert    If a call to this function complicates the picture beyond that
4675796c8dcSSimon Schubert    already provided by BLOCK_START and BLOCK_END, then we create an
4685796c8dcSSimon Schubert    address map for the block.  */
4695796c8dcSSimon Schubert void
record_block_range(struct block * block,CORE_ADDR start,CORE_ADDR end_inclusive)4705796c8dcSSimon Schubert record_block_range (struct block *block,
4715796c8dcSSimon Schubert                     CORE_ADDR start, CORE_ADDR end_inclusive)
4725796c8dcSSimon Schubert {
4735796c8dcSSimon Schubert   /* If this is any different from the range recorded in the block's
4745796c8dcSSimon Schubert      own BLOCK_START and BLOCK_END, then note that the address map has
4755796c8dcSSimon Schubert      become interesting.  Note that even if this block doesn't have
4765796c8dcSSimon Schubert      any "interesting" ranges, some later block might, so we still
4775796c8dcSSimon Schubert      need to record this block in the addrmap.  */
4785796c8dcSSimon Schubert   if (start != BLOCK_START (block)
4795796c8dcSSimon Schubert       || end_inclusive + 1 != BLOCK_END (block))
4805796c8dcSSimon Schubert     pending_addrmap_interesting = 1;
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert   if (! pending_addrmap)
4835796c8dcSSimon Schubert     {
4845796c8dcSSimon Schubert       obstack_init (&pending_addrmap_obstack);
4855796c8dcSSimon Schubert       pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
4865796c8dcSSimon Schubert     }
4875796c8dcSSimon Schubert 
4885796c8dcSSimon Schubert   addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
4895796c8dcSSimon Schubert }
4905796c8dcSSimon Schubert 
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert static struct blockvector *
make_blockvector(struct objfile * objfile)4935796c8dcSSimon Schubert make_blockvector (struct objfile *objfile)
4945796c8dcSSimon Schubert {
4955796c8dcSSimon Schubert   struct pending_block *next;
4965796c8dcSSimon Schubert   struct blockvector *blockvector;
4975796c8dcSSimon Schubert   int i;
4985796c8dcSSimon Schubert 
4995796c8dcSSimon Schubert   /* Count the length of the list of blocks.  */
5005796c8dcSSimon Schubert 
5015796c8dcSSimon Schubert   for (next = pending_blocks, i = 0; next; next = next->next, i++)
5025796c8dcSSimon Schubert     {;
5035796c8dcSSimon Schubert     }
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert   blockvector = (struct blockvector *)
5065796c8dcSSimon Schubert     obstack_alloc (&objfile->objfile_obstack,
5075796c8dcSSimon Schubert 		   (sizeof (struct blockvector)
5085796c8dcSSimon Schubert 		    + (i - 1) * sizeof (struct block *)));
5095796c8dcSSimon Schubert 
5105796c8dcSSimon Schubert   /* Copy the blocks into the blockvector.  This is done in reverse
5115796c8dcSSimon Schubert      order, which happens to put the blocks into the proper order
5125796c8dcSSimon Schubert      (ascending starting address).  finish_block has hair to insert
5135796c8dcSSimon Schubert      each block into the list after its subblocks in order to make
5145796c8dcSSimon Schubert      sure this is true.  */
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert   BLOCKVECTOR_NBLOCKS (blockvector) = i;
5175796c8dcSSimon Schubert   for (next = pending_blocks; next; next = next->next)
5185796c8dcSSimon Schubert     {
5195796c8dcSSimon Schubert       BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
5205796c8dcSSimon Schubert     }
5215796c8dcSSimon Schubert 
5225796c8dcSSimon Schubert   free_pending_blocks ();
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert   /* If we needed an address map for this symtab, record it in the
5255796c8dcSSimon Schubert      blockvector.  */
5265796c8dcSSimon Schubert   if (pending_addrmap && pending_addrmap_interesting)
5275796c8dcSSimon Schubert     BLOCKVECTOR_MAP (blockvector)
5285796c8dcSSimon Schubert       = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
5295796c8dcSSimon Schubert   else
5305796c8dcSSimon Schubert     BLOCKVECTOR_MAP (blockvector) = 0;
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert   /* Some compilers output blocks in the wrong order, but we depend on
5335796c8dcSSimon Schubert      their being in the right order so we can binary search.  Check the
534*ef5ccd6cSJohn Marino      order and moan about it.
535*ef5ccd6cSJohn Marino      Note: Remember that the first two blocks are the global and static
536*ef5ccd6cSJohn Marino      blocks.  We could special case that fact and begin checking at block 2.
537*ef5ccd6cSJohn Marino      To avoid making that assumption we do not.  */
5385796c8dcSSimon Schubert   if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
5395796c8dcSSimon Schubert     {
5405796c8dcSSimon Schubert       for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
5415796c8dcSSimon Schubert 	{
5425796c8dcSSimon Schubert 	  if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
5435796c8dcSSimon Schubert 	      > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
5445796c8dcSSimon Schubert 	    {
5455796c8dcSSimon Schubert 	      CORE_ADDR start
5465796c8dcSSimon Schubert 		= BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
5475796c8dcSSimon Schubert 
5485796c8dcSSimon Schubert 	      complaint (&symfile_complaints, _("block at %s out of order"),
5495796c8dcSSimon Schubert 			 hex_string ((LONGEST) start));
5505796c8dcSSimon Schubert 	    }
5515796c8dcSSimon Schubert 	}
5525796c8dcSSimon Schubert     }
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert   return (blockvector);
5555796c8dcSSimon Schubert }
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert /* Start recording information about source code that came from an
5585796c8dcSSimon Schubert    included (or otherwise merged-in) source file with a different
5595796c8dcSSimon Schubert    name.  NAME is the name of the file (cannot be NULL), DIRNAME is
560c50c785cSJohn Marino    the directory in which the file was compiled (or NULL if not
561c50c785cSJohn Marino    known).  */
5625796c8dcSSimon Schubert 
5635796c8dcSSimon Schubert void
start_subfile(const char * name,const char * dirname)564c50c785cSJohn Marino start_subfile (const char *name, const char *dirname)
5655796c8dcSSimon Schubert {
5665796c8dcSSimon Schubert   struct subfile *subfile;
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert   /* See if this subfile is already known as a subfile of the current
5695796c8dcSSimon Schubert      main source file.  */
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert   for (subfile = subfiles; subfile; subfile = subfile->next)
5725796c8dcSSimon Schubert     {
5735796c8dcSSimon Schubert       char *subfile_name;
5745796c8dcSSimon Schubert 
5755796c8dcSSimon Schubert       /* If NAME is an absolute path, and this subfile is not, then
5765796c8dcSSimon Schubert 	 attempt to create an absolute path to compare.  */
5775796c8dcSSimon Schubert       if (IS_ABSOLUTE_PATH (name)
5785796c8dcSSimon Schubert 	  && !IS_ABSOLUTE_PATH (subfile->name)
5795796c8dcSSimon Schubert 	  && subfile->dirname != NULL)
5805796c8dcSSimon Schubert 	subfile_name = concat (subfile->dirname, SLASH_STRING,
5815796c8dcSSimon Schubert 			       subfile->name, (char *) NULL);
5825796c8dcSSimon Schubert       else
5835796c8dcSSimon Schubert 	subfile_name = subfile->name;
5845796c8dcSSimon Schubert 
5855796c8dcSSimon Schubert       if (FILENAME_CMP (subfile_name, name) == 0)
5865796c8dcSSimon Schubert 	{
5875796c8dcSSimon Schubert 	  current_subfile = subfile;
5885796c8dcSSimon Schubert 	  if (subfile_name != subfile->name)
5895796c8dcSSimon Schubert 	    xfree (subfile_name);
5905796c8dcSSimon Schubert 	  return;
5915796c8dcSSimon Schubert 	}
5925796c8dcSSimon Schubert       if (subfile_name != subfile->name)
5935796c8dcSSimon Schubert 	xfree (subfile_name);
5945796c8dcSSimon Schubert     }
5955796c8dcSSimon Schubert 
5965796c8dcSSimon Schubert   /* This subfile is not known.  Add an entry for it.  Make an entry
5975796c8dcSSimon Schubert      for this subfile in the list of all subfiles of the current main
5985796c8dcSSimon Schubert      source file.  */
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert   subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
6015796c8dcSSimon Schubert   memset ((char *) subfile, 0, sizeof (struct subfile));
6025796c8dcSSimon Schubert   subfile->next = subfiles;
6035796c8dcSSimon Schubert   subfiles = subfile;
6045796c8dcSSimon Schubert   current_subfile = subfile;
6055796c8dcSSimon Schubert 
606c50c785cSJohn Marino   /* Save its name and compilation directory name.  */
607*ef5ccd6cSJohn Marino   subfile->name = xstrdup (name);
6085796c8dcSSimon Schubert   subfile->dirname = (dirname == NULL) ? NULL : xstrdup (dirname);
6095796c8dcSSimon Schubert 
6105796c8dcSSimon Schubert   /* Initialize line-number recording for this subfile.  */
6115796c8dcSSimon Schubert   subfile->line_vector = NULL;
6125796c8dcSSimon Schubert 
6135796c8dcSSimon Schubert   /* Default the source language to whatever can be deduced from the
6145796c8dcSSimon Schubert      filename.  If nothing can be deduced (such as for a C/C++ include
6155796c8dcSSimon Schubert      file with a ".h" extension), then inherit whatever language the
6165796c8dcSSimon Schubert      previous subfile had.  This kludgery is necessary because there
6175796c8dcSSimon Schubert      is no standard way in some object formats to record the source
6185796c8dcSSimon Schubert      language.  Also, when symtabs are allocated we try to deduce a
6195796c8dcSSimon Schubert      language then as well, but it is too late for us to use that
6205796c8dcSSimon Schubert      information while reading symbols, since symtabs aren't allocated
6215796c8dcSSimon Schubert      until after all the symbols have been processed for a given
6225796c8dcSSimon Schubert      source file.  */
6235796c8dcSSimon Schubert 
6245796c8dcSSimon Schubert   subfile->language = deduce_language_from_filename (subfile->name);
625cf7f2e2dSJohn Marino   if (subfile->language == language_unknown
626cf7f2e2dSJohn Marino       && subfile->next != NULL)
6275796c8dcSSimon Schubert     {
6285796c8dcSSimon Schubert       subfile->language = subfile->next->language;
6295796c8dcSSimon Schubert     }
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert   /* Initialize the debug format string to NULL.  We may supply it
6325796c8dcSSimon Schubert      later via a call to record_debugformat.  */
6335796c8dcSSimon Schubert   subfile->debugformat = NULL;
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert   /* Similarly for the producer.  */
6365796c8dcSSimon Schubert   subfile->producer = NULL;
6375796c8dcSSimon Schubert 
6385796c8dcSSimon Schubert   /* If the filename of this subfile ends in .C, then change the
6395796c8dcSSimon Schubert      language of any pending subfiles from C to C++.  We also accept
6405796c8dcSSimon Schubert      any other C++ suffixes accepted by deduce_language_from_filename.  */
6415796c8dcSSimon Schubert   /* Likewise for f2c.  */
6425796c8dcSSimon Schubert 
6435796c8dcSSimon Schubert   if (subfile->name)
6445796c8dcSSimon Schubert     {
6455796c8dcSSimon Schubert       struct subfile *s;
6465796c8dcSSimon Schubert       enum language sublang = deduce_language_from_filename (subfile->name);
6475796c8dcSSimon Schubert 
6485796c8dcSSimon Schubert       if (sublang == language_cplus || sublang == language_fortran)
6495796c8dcSSimon Schubert 	for (s = subfiles; s != NULL; s = s->next)
6505796c8dcSSimon Schubert 	  if (s->language == language_c)
6515796c8dcSSimon Schubert 	    s->language = sublang;
6525796c8dcSSimon Schubert     }
6535796c8dcSSimon Schubert 
6545796c8dcSSimon Schubert   /* And patch up this file if necessary.  */
6555796c8dcSSimon Schubert   if (subfile->language == language_c
6565796c8dcSSimon Schubert       && subfile->next != NULL
6575796c8dcSSimon Schubert       && (subfile->next->language == language_cplus
6585796c8dcSSimon Schubert 	  || subfile->next->language == language_fortran))
6595796c8dcSSimon Schubert     {
6605796c8dcSSimon Schubert       subfile->language = subfile->next->language;
6615796c8dcSSimon Schubert     }
6625796c8dcSSimon Schubert }
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert /* For stabs readers, the first N_SO symbol is assumed to be the
6655796c8dcSSimon Schubert    source file name, and the subfile struct is initialized using that
6665796c8dcSSimon Schubert    assumption.  If another N_SO symbol is later seen, immediately
6675796c8dcSSimon Schubert    following the first one, then the first one is assumed to be the
6685796c8dcSSimon Schubert    directory name and the second one is really the source file name.
6695796c8dcSSimon Schubert 
6705796c8dcSSimon Schubert    So we have to patch up the subfile struct by moving the old name
6715796c8dcSSimon Schubert    value to dirname and remembering the new name.  Some sanity
6725796c8dcSSimon Schubert    checking is performed to ensure that the state of the subfile
6735796c8dcSSimon Schubert    struct is reasonable and that the old name we are assuming to be a
6745796c8dcSSimon Schubert    directory name actually is (by checking for a trailing '/').  */
6755796c8dcSSimon Schubert 
6765796c8dcSSimon Schubert void
patch_subfile_names(struct subfile * subfile,char * name)6775796c8dcSSimon Schubert patch_subfile_names (struct subfile *subfile, char *name)
6785796c8dcSSimon Schubert {
6795796c8dcSSimon Schubert   if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
680c50c785cSJohn Marino       && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
6815796c8dcSSimon Schubert     {
6825796c8dcSSimon Schubert       subfile->dirname = subfile->name;
6835796c8dcSSimon Schubert       subfile->name = xstrdup (name);
684*ef5ccd6cSJohn Marino       set_last_source_file (name);
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert       /* Default the source language to whatever can be deduced from
6875796c8dcSSimon Schubert          the filename.  If nothing can be deduced (such as for a C/C++
6885796c8dcSSimon Schubert          include file with a ".h" extension), then inherit whatever
6895796c8dcSSimon Schubert          language the previous subfile had.  This kludgery is
6905796c8dcSSimon Schubert          necessary because there is no standard way in some object
6915796c8dcSSimon Schubert          formats to record the source language.  Also, when symtabs
6925796c8dcSSimon Schubert          are allocated we try to deduce a language then as well, but
6935796c8dcSSimon Schubert          it is too late for us to use that information while reading
6945796c8dcSSimon Schubert          symbols, since symtabs aren't allocated until after all the
6955796c8dcSSimon Schubert          symbols have been processed for a given source file.  */
6965796c8dcSSimon Schubert 
6975796c8dcSSimon Schubert       subfile->language = deduce_language_from_filename (subfile->name);
698cf7f2e2dSJohn Marino       if (subfile->language == language_unknown
699cf7f2e2dSJohn Marino 	  && subfile->next != NULL)
7005796c8dcSSimon Schubert 	{
7015796c8dcSSimon Schubert 	  subfile->language = subfile->next->language;
7025796c8dcSSimon Schubert 	}
7035796c8dcSSimon Schubert     }
7045796c8dcSSimon Schubert }
7055796c8dcSSimon Schubert 
7065796c8dcSSimon Schubert /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
7075796c8dcSSimon Schubert    switching source files (different subfiles, as we call them) within
7085796c8dcSSimon Schubert    one object file, but using a stack rather than in an arbitrary
7095796c8dcSSimon Schubert    order.  */
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert void
push_subfile(void)7125796c8dcSSimon Schubert push_subfile (void)
7135796c8dcSSimon Schubert {
7145796c8dcSSimon Schubert   struct subfile_stack *tem
7155796c8dcSSimon Schubert     = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert   tem->next = subfile_stack;
7185796c8dcSSimon Schubert   subfile_stack = tem;
7195796c8dcSSimon Schubert   if (current_subfile == NULL || current_subfile->name == NULL)
7205796c8dcSSimon Schubert     {
721c50c785cSJohn Marino       internal_error (__FILE__, __LINE__,
722c50c785cSJohn Marino 		      _("failed internal consistency check"));
7235796c8dcSSimon Schubert     }
7245796c8dcSSimon Schubert   tem->name = current_subfile->name;
7255796c8dcSSimon Schubert }
7265796c8dcSSimon Schubert 
7275796c8dcSSimon Schubert char *
pop_subfile(void)7285796c8dcSSimon Schubert pop_subfile (void)
7295796c8dcSSimon Schubert {
7305796c8dcSSimon Schubert   char *name;
7315796c8dcSSimon Schubert   struct subfile_stack *link = subfile_stack;
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert   if (link == NULL)
7345796c8dcSSimon Schubert     {
735c50c785cSJohn Marino       internal_error (__FILE__, __LINE__,
736c50c785cSJohn Marino 		      _("failed internal consistency check"));
7375796c8dcSSimon Schubert     }
7385796c8dcSSimon Schubert   name = link->name;
7395796c8dcSSimon Schubert   subfile_stack = link->next;
7405796c8dcSSimon Schubert   xfree ((void *) link);
7415796c8dcSSimon Schubert   return (name);
7425796c8dcSSimon Schubert }
7435796c8dcSSimon Schubert 
7445796c8dcSSimon Schubert /* Add a linetable entry for line number LINE and address PC to the
7455796c8dcSSimon Schubert    line vector for SUBFILE.  */
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert void
record_line(struct subfile * subfile,int line,CORE_ADDR pc)7485796c8dcSSimon Schubert record_line (struct subfile *subfile, int line, CORE_ADDR pc)
7495796c8dcSSimon Schubert {
7505796c8dcSSimon Schubert   struct linetable_entry *e;
7515796c8dcSSimon Schubert 
752cf7f2e2dSJohn Marino   /* Ignore the dummy line number in libg.o */
7535796c8dcSSimon Schubert   if (line == 0xffff)
7545796c8dcSSimon Schubert     {
7555796c8dcSSimon Schubert       return;
7565796c8dcSSimon Schubert     }
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert   /* Make sure line vector exists and is big enough.  */
7595796c8dcSSimon Schubert   if (!subfile->line_vector)
7605796c8dcSSimon Schubert     {
7615796c8dcSSimon Schubert       subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
7625796c8dcSSimon Schubert       subfile->line_vector = (struct linetable *)
7635796c8dcSSimon Schubert 	xmalloc (sizeof (struct linetable)
7645796c8dcSSimon Schubert 	   + subfile->line_vector_length * sizeof (struct linetable_entry));
7655796c8dcSSimon Schubert       subfile->line_vector->nitems = 0;
7665796c8dcSSimon Schubert       have_line_numbers = 1;
7675796c8dcSSimon Schubert     }
7685796c8dcSSimon Schubert 
7695796c8dcSSimon Schubert   if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
7705796c8dcSSimon Schubert     {
7715796c8dcSSimon Schubert       subfile->line_vector_length *= 2;
7725796c8dcSSimon Schubert       subfile->line_vector = (struct linetable *)
7735796c8dcSSimon Schubert 	xrealloc ((char *) subfile->line_vector,
7745796c8dcSSimon Schubert 		  (sizeof (struct linetable)
7755796c8dcSSimon Schubert 		   + (subfile->line_vector_length
7765796c8dcSSimon Schubert 		      * sizeof (struct linetable_entry))));
7775796c8dcSSimon Schubert     }
7785796c8dcSSimon Schubert 
7795796c8dcSSimon Schubert   /* Normally, we treat lines as unsorted.  But the end of sequence
7805796c8dcSSimon Schubert      marker is special.  We sort line markers at the same PC by line
7815796c8dcSSimon Schubert      number, so end of sequence markers (which have line == 0) appear
7825796c8dcSSimon Schubert      first.  This is right if the marker ends the previous function,
7835796c8dcSSimon Schubert      and there is no padding before the next function.  But it is
7845796c8dcSSimon Schubert      wrong if the previous line was empty and we are now marking a
7855796c8dcSSimon Schubert      switch to a different subfile.  We must leave the end of sequence
7865796c8dcSSimon Schubert      marker at the end of this group of lines, not sort the empty line
7875796c8dcSSimon Schubert      to after the marker.  The easiest way to accomplish this is to
7885796c8dcSSimon Schubert      delete any empty lines from our table, if they are followed by
7895796c8dcSSimon Schubert      end of sequence markers.  All we lose is the ability to set
7905796c8dcSSimon Schubert      breakpoints at some lines which contain no instructions
7915796c8dcSSimon Schubert      anyway.  */
7925796c8dcSSimon Schubert   if (line == 0 && subfile->line_vector->nitems > 0)
7935796c8dcSSimon Schubert     {
7945796c8dcSSimon Schubert       e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
7955796c8dcSSimon Schubert       while (subfile->line_vector->nitems > 0 && e->pc == pc)
7965796c8dcSSimon Schubert 	{
7975796c8dcSSimon Schubert 	  e--;
7985796c8dcSSimon Schubert 	  subfile->line_vector->nitems--;
7995796c8dcSSimon Schubert 	}
8005796c8dcSSimon Schubert     }
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert   e = subfile->line_vector->item + subfile->line_vector->nitems++;
8035796c8dcSSimon Schubert   e->line = line;
8045796c8dcSSimon Schubert   e->pc = pc;
8055796c8dcSSimon Schubert }
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert /* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */
8085796c8dcSSimon Schubert 
8095796c8dcSSimon Schubert static int
compare_line_numbers(const void * ln1p,const void * ln2p)8105796c8dcSSimon Schubert compare_line_numbers (const void *ln1p, const void *ln2p)
8115796c8dcSSimon Schubert {
8125796c8dcSSimon Schubert   struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
8135796c8dcSSimon Schubert   struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert   /* Note: this code does not assume that CORE_ADDRs can fit in ints.
8165796c8dcSSimon Schubert      Please keep it that way.  */
8175796c8dcSSimon Schubert   if (ln1->pc < ln2->pc)
8185796c8dcSSimon Schubert     return -1;
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert   if (ln1->pc > ln2->pc)
8215796c8dcSSimon Schubert     return 1;
8225796c8dcSSimon Schubert 
8235796c8dcSSimon Schubert   /* If pc equal, sort by line.  I'm not sure whether this is optimum
8245796c8dcSSimon Schubert      behavior (see comment at struct linetable in symtab.h).  */
8255796c8dcSSimon Schubert   return ln1->line - ln2->line;
8265796c8dcSSimon Schubert }
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert /* Start a new symtab for a new source file.  Called, for example,
8295796c8dcSSimon Schubert    when a stabs symbol of type N_SO is seen, or when a DWARF
8305796c8dcSSimon Schubert    TAG_compile_unit DIE is seen.  It indicates the start of data for
8315796c8dcSSimon Schubert    one original source file.
8325796c8dcSSimon Schubert 
8335796c8dcSSimon Schubert    NAME is the name of the file (cannot be NULL).  DIRNAME is the directory in
8345796c8dcSSimon Schubert    which the file was compiled (or NULL if not known).  START_ADDR is the
8355796c8dcSSimon Schubert    lowest address of objects in the file (or 0 if not known).  */
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert void
start_symtab(const char * name,const char * dirname,CORE_ADDR start_addr)838*ef5ccd6cSJohn Marino start_symtab (const char *name, const char *dirname, CORE_ADDR start_addr)
8395796c8dcSSimon Schubert {
840*ef5ccd6cSJohn Marino   restart_symtab (start_addr);
841*ef5ccd6cSJohn Marino   set_last_source_file (name);
842*ef5ccd6cSJohn Marino   start_subfile (name, dirname);
843*ef5ccd6cSJohn Marino }
844*ef5ccd6cSJohn Marino 
845*ef5ccd6cSJohn Marino /* Restart compilation for a symtab.
846*ef5ccd6cSJohn Marino    This is used when a symtab is built from multiple sources.
847*ef5ccd6cSJohn Marino    The symtab is first built with start_symtab and then for each additional
848*ef5ccd6cSJohn Marino    piece call restart_symtab.  */
849*ef5ccd6cSJohn Marino 
850*ef5ccd6cSJohn Marino void
restart_symtab(CORE_ADDR start_addr)851*ef5ccd6cSJohn Marino restart_symtab (CORE_ADDR start_addr)
852*ef5ccd6cSJohn Marino {
853*ef5ccd6cSJohn Marino   set_last_source_file (NULL);
8545796c8dcSSimon Schubert   last_source_start_addr = start_addr;
8555796c8dcSSimon Schubert   file_symbols = NULL;
8565796c8dcSSimon Schubert   global_symbols = NULL;
8575796c8dcSSimon Schubert   within_function = 0;
8585796c8dcSSimon Schubert   have_line_numbers = 0;
8595796c8dcSSimon Schubert 
8605796c8dcSSimon Schubert   /* Context stack is initially empty.  Allocate first one with room
8615796c8dcSSimon Schubert      for 10 levels; reuse it forever afterward.  */
8625796c8dcSSimon Schubert   if (context_stack == NULL)
8635796c8dcSSimon Schubert     {
8645796c8dcSSimon Schubert       context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
8655796c8dcSSimon Schubert       context_stack = (struct context_stack *)
8665796c8dcSSimon Schubert 	xmalloc (context_stack_size * sizeof (struct context_stack));
8675796c8dcSSimon Schubert     }
8685796c8dcSSimon Schubert   context_stack_depth = 0;
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert   /* We shouldn't have any address map at this point.  */
8715796c8dcSSimon Schubert   gdb_assert (! pending_addrmap);
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   /* Initialize the list of sub source files with one entry for this
8745796c8dcSSimon Schubert      file (the top-level source file).  */
8755796c8dcSSimon Schubert   subfiles = NULL;
8765796c8dcSSimon Schubert   current_subfile = NULL;
8775796c8dcSSimon Schubert }
8785796c8dcSSimon Schubert 
879c50c785cSJohn Marino /* Subroutine of end_symtab to simplify it.  Look for a subfile that
880c50c785cSJohn Marino    matches the main source file's basename.  If there is only one, and
881c50c785cSJohn Marino    if the main source file doesn't have any symbol or line number
882c50c785cSJohn Marino    information, then copy this file's symtab and line_vector to the
883c50c785cSJohn Marino    main source file's subfile and discard the other subfile.  This can
884c50c785cSJohn Marino    happen because of a compiler bug or from the user playing games
885c50c785cSJohn Marino    with #line or from things like a distributed build system that
886c50c785cSJohn Marino    manipulates the debug info.  */
8875796c8dcSSimon Schubert 
8885796c8dcSSimon Schubert static void
watch_main_source_file_lossage(void)8895796c8dcSSimon Schubert watch_main_source_file_lossage (void)
8905796c8dcSSimon Schubert {
8915796c8dcSSimon Schubert   struct subfile *mainsub, *subfile;
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert   /* Find the main source file.
8945796c8dcSSimon Schubert      This loop could be eliminated if start_symtab saved it for us.  */
8955796c8dcSSimon Schubert   mainsub = NULL;
8965796c8dcSSimon Schubert   for (subfile = subfiles; subfile; subfile = subfile->next)
8975796c8dcSSimon Schubert     {
8985796c8dcSSimon Schubert       /* The main subfile is guaranteed to be the last one.  */
8995796c8dcSSimon Schubert       if (subfile->next == NULL)
9005796c8dcSSimon Schubert 	mainsub = subfile;
9015796c8dcSSimon Schubert     }
9025796c8dcSSimon Schubert 
903c50c785cSJohn Marino   /* If the main source file doesn't have any line number or symbol
904c50c785cSJohn Marino      info, look for an alias in another subfile.
905c50c785cSJohn Marino 
906c50c785cSJohn Marino      We have to watch for mainsub == NULL here.  It's a quirk of
907c50c785cSJohn Marino      end_symtab, it can return NULL so there may not be a main
908c50c785cSJohn Marino      subfile.  */
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert   if (mainsub
9115796c8dcSSimon Schubert       && mainsub->line_vector == NULL
9125796c8dcSSimon Schubert       && mainsub->symtab == NULL)
9135796c8dcSSimon Schubert     {
9145796c8dcSSimon Schubert       const char *mainbase = lbasename (mainsub->name);
9155796c8dcSSimon Schubert       int nr_matches = 0;
9165796c8dcSSimon Schubert       struct subfile *prevsub;
9175796c8dcSSimon Schubert       struct subfile *mainsub_alias = NULL;
9185796c8dcSSimon Schubert       struct subfile *prev_mainsub_alias = NULL;
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert       prevsub = NULL;
9215796c8dcSSimon Schubert       for (subfile = subfiles;
9225796c8dcSSimon Schubert 	   /* Stop before we get to the last one.  */
9235796c8dcSSimon Schubert 	   subfile->next;
9245796c8dcSSimon Schubert 	   subfile = subfile->next)
9255796c8dcSSimon Schubert 	{
926c50c785cSJohn Marino 	  if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
9275796c8dcSSimon Schubert 	    {
9285796c8dcSSimon Schubert 	      ++nr_matches;
9295796c8dcSSimon Schubert 	      mainsub_alias = subfile;
9305796c8dcSSimon Schubert 	      prev_mainsub_alias = prevsub;
9315796c8dcSSimon Schubert 	    }
9325796c8dcSSimon Schubert 	  prevsub = subfile;
9335796c8dcSSimon Schubert 	}
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert       if (nr_matches == 1)
9365796c8dcSSimon Schubert 	{
9375796c8dcSSimon Schubert 	  gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert 	  /* Found a match for the main source file.
9405796c8dcSSimon Schubert 	     Copy its line_vector and symtab to the main subfile
9415796c8dcSSimon Schubert 	     and then discard it.  */
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert 	  mainsub->line_vector = mainsub_alias->line_vector;
9445796c8dcSSimon Schubert 	  mainsub->line_vector_length = mainsub_alias->line_vector_length;
9455796c8dcSSimon Schubert 	  mainsub->symtab = mainsub_alias->symtab;
9465796c8dcSSimon Schubert 
9475796c8dcSSimon Schubert 	  if (prev_mainsub_alias == NULL)
9485796c8dcSSimon Schubert 	    subfiles = mainsub_alias->next;
9495796c8dcSSimon Schubert 	  else
9505796c8dcSSimon Schubert 	    prev_mainsub_alias->next = mainsub_alias->next;
9515796c8dcSSimon Schubert 	  xfree (mainsub_alias);
9525796c8dcSSimon Schubert 	}
9535796c8dcSSimon Schubert     }
9545796c8dcSSimon Schubert }
9555796c8dcSSimon Schubert 
956*ef5ccd6cSJohn Marino /* Helper function for qsort.  Parameters are `struct block *' pointers,
9575796c8dcSSimon Schubert    function sorts them in descending order by their BLOCK_START.  */
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert static int
block_compar(const void * ap,const void * bp)9605796c8dcSSimon Schubert block_compar (const void *ap, const void *bp)
9615796c8dcSSimon Schubert {
9625796c8dcSSimon Schubert   const struct block *a = *(const struct block **) ap;
9635796c8dcSSimon Schubert   const struct block *b = *(const struct block **) bp;
9645796c8dcSSimon Schubert 
9655796c8dcSSimon Schubert   return ((BLOCK_START (b) > BLOCK_START (a))
9665796c8dcSSimon Schubert 	  - (BLOCK_START (b) < BLOCK_START (a)));
9675796c8dcSSimon Schubert }
9685796c8dcSSimon Schubert 
969*ef5ccd6cSJohn Marino /* Reset globals used to build symtabs.  */
9705796c8dcSSimon Schubert 
971*ef5ccd6cSJohn Marino static void
reset_symtab_globals(void)972*ef5ccd6cSJohn Marino reset_symtab_globals (void)
9735796c8dcSSimon Schubert {
974*ef5ccd6cSJohn Marino   set_last_source_file (NULL);
975*ef5ccd6cSJohn Marino   current_subfile = NULL;
976*ef5ccd6cSJohn Marino   pending_macros = NULL;
977*ef5ccd6cSJohn Marino   if (pending_addrmap)
978*ef5ccd6cSJohn Marino     {
979*ef5ccd6cSJohn Marino       obstack_free (&pending_addrmap_obstack, NULL);
980*ef5ccd6cSJohn Marino       pending_addrmap = NULL;
981*ef5ccd6cSJohn Marino     }
982*ef5ccd6cSJohn Marino }
9835796c8dcSSimon Schubert 
984*ef5ccd6cSJohn Marino /* Implementation of the first part of end_symtab.  It allows modifying
985*ef5ccd6cSJohn Marino    STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
986*ef5ccd6cSJohn Marino    If the returned value is NULL there is no blockvector created for
987*ef5ccd6cSJohn Marino    this symtab (you still must call end_symtab_from_static_block).
988*ef5ccd6cSJohn Marino 
989*ef5ccd6cSJohn Marino    END_ADDR is the same as for end_symtab: the address of the end of the
990*ef5ccd6cSJohn Marino    file's text.
991*ef5ccd6cSJohn Marino 
992*ef5ccd6cSJohn Marino    If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
993*ef5ccd6cSJohn Marino    expandable.
994*ef5ccd6cSJohn Marino 
995*ef5ccd6cSJohn Marino    If REQUIRED is non-zero, then a symtab is created even if it does
996*ef5ccd6cSJohn Marino    not contain any symbols.  */
997*ef5ccd6cSJohn Marino 
998*ef5ccd6cSJohn Marino struct block *
end_symtab_get_static_block(CORE_ADDR end_addr,struct objfile * objfile,int expandable,int required)999*ef5ccd6cSJohn Marino end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
1000*ef5ccd6cSJohn Marino 			     int expandable, int required)
1001*ef5ccd6cSJohn Marino {
10025796c8dcSSimon Schubert   /* Finish the lexical context of the last function in the file; pop
10035796c8dcSSimon Schubert      the context stack.  */
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert   if (context_stack_depth > 0)
10065796c8dcSSimon Schubert     {
1007*ef5ccd6cSJohn Marino       struct context_stack *cstk = pop_context ();
1008*ef5ccd6cSJohn Marino 
10095796c8dcSSimon Schubert       /* Make a block for the local symbols within.  */
10105796c8dcSSimon Schubert       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
10115796c8dcSSimon Schubert 		    cstk->start_addr, end_addr, objfile);
10125796c8dcSSimon Schubert 
10135796c8dcSSimon Schubert       if (context_stack_depth > 0)
10145796c8dcSSimon Schubert 	{
10155796c8dcSSimon Schubert 	  /* This is said to happen with SCO.  The old coffread.c
10165796c8dcSSimon Schubert 	     code simply emptied the context stack, so we do the
10175796c8dcSSimon Schubert 	     same.  FIXME: Find out why it is happening.  This is not
10185796c8dcSSimon Schubert 	     believed to happen in most cases (even for coffread.c);
10195796c8dcSSimon Schubert 	     it used to be an abort().  */
10205796c8dcSSimon Schubert 	  complaint (&symfile_complaints,
10215796c8dcSSimon Schubert 	             _("Context stack not empty in end_symtab"));
10225796c8dcSSimon Schubert 	  context_stack_depth = 0;
10235796c8dcSSimon Schubert 	}
10245796c8dcSSimon Schubert     }
10255796c8dcSSimon Schubert 
10265796c8dcSSimon Schubert   /* Reordered executables may have out of order pending blocks; if
10275796c8dcSSimon Schubert      OBJF_REORDERED is true, then sort the pending blocks.  */
1028*ef5ccd6cSJohn Marino 
10295796c8dcSSimon Schubert   if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
10305796c8dcSSimon Schubert     {
10315796c8dcSSimon Schubert       unsigned count = 0;
10325796c8dcSSimon Schubert       struct pending_block *pb;
10335796c8dcSSimon Schubert       struct block **barray, **bp;
10345796c8dcSSimon Schubert       struct cleanup *back_to;
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert       for (pb = pending_blocks; pb != NULL; pb = pb->next)
10375796c8dcSSimon Schubert 	count++;
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert       barray = xmalloc (sizeof (*barray) * count);
10405796c8dcSSimon Schubert       back_to = make_cleanup (xfree, barray);
10415796c8dcSSimon Schubert 
10425796c8dcSSimon Schubert       bp = barray;
10435796c8dcSSimon Schubert       for (pb = pending_blocks; pb != NULL; pb = pb->next)
10445796c8dcSSimon Schubert 	*bp++ = pb->block;
10455796c8dcSSimon Schubert 
10465796c8dcSSimon Schubert       qsort (barray, count, sizeof (*barray), block_compar);
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert       bp = barray;
10495796c8dcSSimon Schubert       for (pb = pending_blocks; pb != NULL; pb = pb->next)
10505796c8dcSSimon Schubert 	pb->block = *bp++;
10515796c8dcSSimon Schubert 
10525796c8dcSSimon Schubert       do_cleanups (back_to);
10535796c8dcSSimon Schubert     }
10545796c8dcSSimon Schubert 
10555796c8dcSSimon Schubert   /* Cleanup any undefined types that have been left hanging around
10565796c8dcSSimon Schubert      (this needs to be done before the finish_blocks so that
10575796c8dcSSimon Schubert      file_symbols is still good).
10585796c8dcSSimon Schubert 
1059*ef5ccd6cSJohn Marino      Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
10605796c8dcSSimon Schubert      specific, but harmless for other symbol readers, since on gdb
10615796c8dcSSimon Schubert      startup or when finished reading stabs, the state is set so these
10625796c8dcSSimon Schubert      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
10635796c8dcSSimon Schubert      we make this cleaner?  */
10645796c8dcSSimon Schubert 
1065*ef5ccd6cSJohn Marino   cleanup_undefined_stabs_types (objfile);
10665796c8dcSSimon Schubert   finish_global_stabs (objfile);
10675796c8dcSSimon Schubert 
1068*ef5ccd6cSJohn Marino   if (!required
1069*ef5ccd6cSJohn Marino       && pending_blocks == NULL
10705796c8dcSSimon Schubert       && file_symbols == NULL
10715796c8dcSSimon Schubert       && global_symbols == NULL
10725796c8dcSSimon Schubert       && have_line_numbers == 0
10735796c8dcSSimon Schubert       && pending_macros == NULL)
10745796c8dcSSimon Schubert     {
1075*ef5ccd6cSJohn Marino       /* Ignore symtabs that have no functions with real debugging info.  */
1076*ef5ccd6cSJohn Marino       return NULL;
1077*ef5ccd6cSJohn Marino     }
1078*ef5ccd6cSJohn Marino   else
1079*ef5ccd6cSJohn Marino     {
1080*ef5ccd6cSJohn Marino       /* Define the STATIC_BLOCK.  */
1081*ef5ccd6cSJohn Marino       return finish_block_internal (NULL, &file_symbols, NULL,
1082*ef5ccd6cSJohn Marino 				    last_source_start_addr, end_addr, objfile,
1083*ef5ccd6cSJohn Marino 				    0, expandable);
1084*ef5ccd6cSJohn Marino     }
1085*ef5ccd6cSJohn Marino }
1086*ef5ccd6cSJohn Marino 
1087*ef5ccd6cSJohn Marino /* Implementation of the second part of end_symtab.  Pass STATIC_BLOCK
1088*ef5ccd6cSJohn Marino    as value returned by end_symtab_get_static_block.
1089*ef5ccd6cSJohn Marino 
1090*ef5ccd6cSJohn Marino    SECTION is the same as for end_symtab: the section number
1091*ef5ccd6cSJohn Marino    (in objfile->section_offsets) of the blockvector and linetable.
1092*ef5ccd6cSJohn Marino 
1093*ef5ccd6cSJohn Marino    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1094*ef5ccd6cSJohn Marino    expandable.  */
1095*ef5ccd6cSJohn Marino 
1096*ef5ccd6cSJohn Marino struct symtab *
end_symtab_from_static_block(struct block * static_block,struct objfile * objfile,int section,int expandable)1097*ef5ccd6cSJohn Marino end_symtab_from_static_block (struct block *static_block,
1098*ef5ccd6cSJohn Marino 			      struct objfile *objfile, int section,
1099*ef5ccd6cSJohn Marino 			      int expandable)
1100*ef5ccd6cSJohn Marino {
1101*ef5ccd6cSJohn Marino   struct symtab *symtab = NULL;
1102*ef5ccd6cSJohn Marino   struct blockvector *blockvector;
1103*ef5ccd6cSJohn Marino   struct subfile *subfile;
1104*ef5ccd6cSJohn Marino   struct subfile *nextsub;
1105*ef5ccd6cSJohn Marino 
1106*ef5ccd6cSJohn Marino   if (static_block == NULL)
1107*ef5ccd6cSJohn Marino     {
1108*ef5ccd6cSJohn Marino       /* Ignore symtabs that have no functions with real debugging info.  */
11095796c8dcSSimon Schubert       blockvector = NULL;
11105796c8dcSSimon Schubert     }
11115796c8dcSSimon Schubert   else
11125796c8dcSSimon Schubert     {
1113*ef5ccd6cSJohn Marino       CORE_ADDR end_addr = BLOCK_END (static_block);
1114*ef5ccd6cSJohn Marino 
1115*ef5ccd6cSJohn Marino       /* Define after STATIC_BLOCK also GLOBAL_BLOCK, and build the
11165796c8dcSSimon Schubert          blockvector.  */
1117*ef5ccd6cSJohn Marino       finish_block_internal (NULL, &global_symbols, NULL,
1118*ef5ccd6cSJohn Marino 			     last_source_start_addr, end_addr, objfile,
1119*ef5ccd6cSJohn Marino 			     1, expandable);
11205796c8dcSSimon Schubert       blockvector = make_blockvector (objfile);
11215796c8dcSSimon Schubert     }
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert   /* Read the line table if it has to be read separately.  */
11245796c8dcSSimon Schubert   if (objfile->sf->sym_read_linetable != NULL)
11255796c8dcSSimon Schubert     objfile->sf->sym_read_linetable ();
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert   /* Handle the case where the debug info specifies a different path
11285796c8dcSSimon Schubert      for the main source file.  It can cause us to lose track of its
11295796c8dcSSimon Schubert      line number information.  */
11305796c8dcSSimon Schubert   watch_main_source_file_lossage ();
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert   /* Now create the symtab objects proper, one for each subfile.  */
11335796c8dcSSimon Schubert   /* (The main file is the last one on the chain.)  */
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert   for (subfile = subfiles; subfile; subfile = nextsub)
11365796c8dcSSimon Schubert     {
11375796c8dcSSimon Schubert       int linetablesize = 0;
11385796c8dcSSimon Schubert       symtab = NULL;
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert       /* If we have blocks of symbols, make a symtab.  Otherwise, just
11415796c8dcSSimon Schubert          ignore this file and any line number info in it.  */
11425796c8dcSSimon Schubert       if (blockvector)
11435796c8dcSSimon Schubert 	{
11445796c8dcSSimon Schubert 	  if (subfile->line_vector)
11455796c8dcSSimon Schubert 	    {
11465796c8dcSSimon Schubert 	      linetablesize = sizeof (struct linetable) +
11475796c8dcSSimon Schubert 	        subfile->line_vector->nitems * sizeof (struct linetable_entry);
11485796c8dcSSimon Schubert 
11495796c8dcSSimon Schubert 	      /* Like the pending blocks, the line table may be
11505796c8dcSSimon Schubert 	         scrambled in reordered executables.  Sort it if
11515796c8dcSSimon Schubert 	         OBJF_REORDERED is true.  */
11525796c8dcSSimon Schubert 	      if (objfile->flags & OBJF_REORDERED)
11535796c8dcSSimon Schubert 		qsort (subfile->line_vector->item,
11545796c8dcSSimon Schubert 		       subfile->line_vector->nitems,
11555796c8dcSSimon Schubert 		     sizeof (struct linetable_entry), compare_line_numbers);
11565796c8dcSSimon Schubert 	    }
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert 	  /* Now, allocate a symbol table.  */
11595796c8dcSSimon Schubert 	  if (subfile->symtab == NULL)
11605796c8dcSSimon Schubert 	    symtab = allocate_symtab (subfile->name, objfile);
11615796c8dcSSimon Schubert 	  else
11625796c8dcSSimon Schubert 	    symtab = subfile->symtab;
11635796c8dcSSimon Schubert 
11645796c8dcSSimon Schubert 	  /* Fill in its components.  */
11655796c8dcSSimon Schubert 	  symtab->blockvector = blockvector;
11665796c8dcSSimon Schubert           symtab->macro_table = pending_macros;
11675796c8dcSSimon Schubert 	  if (subfile->line_vector)
11685796c8dcSSimon Schubert 	    {
1169c50c785cSJohn Marino 	      /* Reallocate the line table on the symbol obstack.  */
11705796c8dcSSimon Schubert 	      symtab->linetable = (struct linetable *)
11715796c8dcSSimon Schubert 		obstack_alloc (&objfile->objfile_obstack, linetablesize);
11725796c8dcSSimon Schubert 	      memcpy (symtab->linetable, subfile->line_vector, linetablesize);
11735796c8dcSSimon Schubert 	    }
11745796c8dcSSimon Schubert 	  else
11755796c8dcSSimon Schubert 	    {
11765796c8dcSSimon Schubert 	      symtab->linetable = NULL;
11775796c8dcSSimon Schubert 	    }
11785796c8dcSSimon Schubert 	  symtab->block_line_section = section;
11795796c8dcSSimon Schubert 	  if (subfile->dirname)
11805796c8dcSSimon Schubert 	    {
1181c50c785cSJohn Marino 	      /* Reallocate the dirname on the symbol obstack.  */
11825796c8dcSSimon Schubert 	      symtab->dirname = (char *)
11835796c8dcSSimon Schubert 		obstack_alloc (&objfile->objfile_obstack,
11845796c8dcSSimon Schubert 			       strlen (subfile->dirname) + 1);
11855796c8dcSSimon Schubert 	      strcpy (symtab->dirname, subfile->dirname);
11865796c8dcSSimon Schubert 	    }
11875796c8dcSSimon Schubert 	  else
11885796c8dcSSimon Schubert 	    {
11895796c8dcSSimon Schubert 	      symtab->dirname = NULL;
11905796c8dcSSimon Schubert 	    }
11915796c8dcSSimon Schubert 
11925796c8dcSSimon Schubert 	  /* Use whatever language we have been using for this
11935796c8dcSSimon Schubert 	     subfile, not the one that was deduced in allocate_symtab
11945796c8dcSSimon Schubert 	     from the filename.  We already did our own deducing when
11955796c8dcSSimon Schubert 	     we created the subfile, and we may have altered our
11965796c8dcSSimon Schubert 	     opinion of what language it is from things we found in
11975796c8dcSSimon Schubert 	     the symbols.  */
11985796c8dcSSimon Schubert 	  symtab->language = subfile->language;
11995796c8dcSSimon Schubert 
1200c50c785cSJohn Marino 	  /* Save the debug format string (if any) in the symtab.  */
1201a45ae5f8SJohn Marino 	  symtab->debugformat = subfile->debugformat;
12025796c8dcSSimon Schubert 
12035796c8dcSSimon Schubert 	  /* Similarly for the producer.  */
1204a45ae5f8SJohn Marino 	  symtab->producer = subfile->producer;
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert 	  /* All symtabs for the main file and the subfiles share a
12075796c8dcSSimon Schubert 	     blockvector, so we need to clear primary for everything
12085796c8dcSSimon Schubert 	     but the main file.  */
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert 	  symtab->primary = 0;
12115796c8dcSSimon Schubert 	}
12125796c8dcSSimon Schubert       else
12135796c8dcSSimon Schubert         {
12145796c8dcSSimon Schubert           if (subfile->symtab)
12155796c8dcSSimon Schubert             {
12165796c8dcSSimon Schubert               /* Since we are ignoring that subfile, we also need
12175796c8dcSSimon Schubert                  to unlink the associated empty symtab that we created.
1218*ef5ccd6cSJohn Marino                  Otherwise, we can run into trouble because various parts
12195796c8dcSSimon Schubert                  such as the block-vector are uninitialized whereas
12205796c8dcSSimon Schubert                  the rest of the code assumes that they are.
12215796c8dcSSimon Schubert 
12225796c8dcSSimon Schubert                  We can only unlink the symtab because it was allocated
12235796c8dcSSimon Schubert                  on the objfile obstack.  */
12245796c8dcSSimon Schubert               struct symtab *s;
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert               if (objfile->symtabs == subfile->symtab)
12275796c8dcSSimon Schubert                 objfile->symtabs = objfile->symtabs->next;
12285796c8dcSSimon Schubert               else
12295796c8dcSSimon Schubert                 ALL_OBJFILE_SYMTABS (objfile, s)
12305796c8dcSSimon Schubert                   if (s->next == subfile->symtab)
12315796c8dcSSimon Schubert                     {
12325796c8dcSSimon Schubert                       s->next = s->next->next;
12335796c8dcSSimon Schubert                       break;
12345796c8dcSSimon Schubert                     }
12355796c8dcSSimon Schubert               subfile->symtab = NULL;
12365796c8dcSSimon Schubert             }
12375796c8dcSSimon Schubert         }
12385796c8dcSSimon Schubert       if (subfile->name != NULL)
12395796c8dcSSimon Schubert 	{
12405796c8dcSSimon Schubert 	  xfree ((void *) subfile->name);
12415796c8dcSSimon Schubert 	}
12425796c8dcSSimon Schubert       if (subfile->dirname != NULL)
12435796c8dcSSimon Schubert 	{
12445796c8dcSSimon Schubert 	  xfree ((void *) subfile->dirname);
12455796c8dcSSimon Schubert 	}
12465796c8dcSSimon Schubert       if (subfile->line_vector != NULL)
12475796c8dcSSimon Schubert 	{
12485796c8dcSSimon Schubert 	  xfree ((void *) subfile->line_vector);
12495796c8dcSSimon Schubert 	}
12505796c8dcSSimon Schubert 
12515796c8dcSSimon Schubert       nextsub = subfile->next;
12525796c8dcSSimon Schubert       xfree ((void *) subfile);
12535796c8dcSSimon Schubert     }
12545796c8dcSSimon Schubert 
12555796c8dcSSimon Schubert   /* Set this for the main source file.  */
12565796c8dcSSimon Schubert   if (symtab)
12575796c8dcSSimon Schubert     {
12585796c8dcSSimon Schubert       symtab->primary = 1;
1259*ef5ccd6cSJohn Marino 
1260*ef5ccd6cSJohn Marino       if (symtab->blockvector)
1261*ef5ccd6cSJohn Marino 	{
1262*ef5ccd6cSJohn Marino 	  struct block *b = BLOCKVECTOR_BLOCK (symtab->blockvector,
1263*ef5ccd6cSJohn Marino 					       GLOBAL_BLOCK);
1264*ef5ccd6cSJohn Marino 
1265*ef5ccd6cSJohn Marino 	  set_block_symtab (b, symtab);
1266*ef5ccd6cSJohn Marino 	}
12675796c8dcSSimon Schubert     }
12685796c8dcSSimon Schubert 
12695796c8dcSSimon Schubert   /* Default any symbols without a specified symtab to the primary
12705796c8dcSSimon Schubert      symtab.  */
12715796c8dcSSimon Schubert   if (blockvector)
12725796c8dcSSimon Schubert     {
12735796c8dcSSimon Schubert       int block_i;
12745796c8dcSSimon Schubert 
12755796c8dcSSimon Schubert       for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
12765796c8dcSSimon Schubert 	{
12775796c8dcSSimon Schubert 	  struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
12785796c8dcSSimon Schubert 	  struct symbol *sym;
12795796c8dcSSimon Schubert 	  struct dict_iterator iter;
12805796c8dcSSimon Schubert 
1281c50c785cSJohn Marino 	  /* Inlined functions may have symbols not in the global or
1282c50c785cSJohn Marino 	     static symbol lists.  */
12835796c8dcSSimon Schubert 	  if (BLOCK_FUNCTION (block) != NULL)
12845796c8dcSSimon Schubert 	    if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
12855796c8dcSSimon Schubert 	      SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
12865796c8dcSSimon Schubert 
1287*ef5ccd6cSJohn Marino 	  /* Note that we only want to fix up symbols from the local
1288*ef5ccd6cSJohn Marino 	     blocks, not blocks coming from included symtabs.  That is why
1289*ef5ccd6cSJohn Marino 	     we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
1290*ef5ccd6cSJohn Marino 	  ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
12915796c8dcSSimon Schubert 	    if (SYMBOL_SYMTAB (sym) == NULL)
12925796c8dcSSimon Schubert 	      SYMBOL_SYMTAB (sym) = symtab;
12935796c8dcSSimon Schubert 	}
12945796c8dcSSimon Schubert     }
12955796c8dcSSimon Schubert 
1296*ef5ccd6cSJohn Marino   reset_symtab_globals ();
12975796c8dcSSimon Schubert 
12985796c8dcSSimon Schubert   return symtab;
12995796c8dcSSimon Schubert }
13005796c8dcSSimon Schubert 
1301*ef5ccd6cSJohn Marino /* Finish the symbol definitions for one main source file, close off
1302*ef5ccd6cSJohn Marino    all the lexical contexts for that file (creating struct block's for
1303*ef5ccd6cSJohn Marino    them), then make the struct symtab for that file and put it in the
1304*ef5ccd6cSJohn Marino    list of all such.
1305*ef5ccd6cSJohn Marino 
1306*ef5ccd6cSJohn Marino    END_ADDR is the address of the end of the file's text.  SECTION is
1307*ef5ccd6cSJohn Marino    the section number (in objfile->section_offsets) of the blockvector
1308*ef5ccd6cSJohn Marino    and linetable.
1309*ef5ccd6cSJohn Marino 
1310*ef5ccd6cSJohn Marino    Note that it is possible for end_symtab() to return NULL.  In
1311*ef5ccd6cSJohn Marino    particular, for the DWARF case at least, it will return NULL when
1312*ef5ccd6cSJohn Marino    it finds a compilation unit that has exactly one DIE, a
1313*ef5ccd6cSJohn Marino    TAG_compile_unit DIE.  This can happen when we link in an object
1314*ef5ccd6cSJohn Marino    file that was compiled from an empty source file.  Returning NULL
1315*ef5ccd6cSJohn Marino    is probably not the correct thing to do, because then gdb will
1316*ef5ccd6cSJohn Marino    never know about this empty file (FIXME).
1317*ef5ccd6cSJohn Marino 
1318*ef5ccd6cSJohn Marino    If you need to modify STATIC_BLOCK before it is finalized you should
1319*ef5ccd6cSJohn Marino    call end_symtab_get_static_block and end_symtab_from_static_block
1320*ef5ccd6cSJohn Marino    yourself.  */
1321*ef5ccd6cSJohn Marino 
1322*ef5ccd6cSJohn Marino struct symtab *
end_symtab(CORE_ADDR end_addr,struct objfile * objfile,int section)1323*ef5ccd6cSJohn Marino end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
1324*ef5ccd6cSJohn Marino {
1325*ef5ccd6cSJohn Marino   struct block *static_block;
1326*ef5ccd6cSJohn Marino 
1327*ef5ccd6cSJohn Marino   static_block = end_symtab_get_static_block (end_addr, objfile, 0, 0);
1328*ef5ccd6cSJohn Marino   return end_symtab_from_static_block (static_block, objfile, section, 0);
1329*ef5ccd6cSJohn Marino }
1330*ef5ccd6cSJohn Marino 
1331*ef5ccd6cSJohn Marino /* Same as end_symtab except create a symtab that can be later added to.  */
1332*ef5ccd6cSJohn Marino 
1333*ef5ccd6cSJohn Marino struct symtab *
end_expandable_symtab(CORE_ADDR end_addr,struct objfile * objfile,int section)1334*ef5ccd6cSJohn Marino end_expandable_symtab (CORE_ADDR end_addr, struct objfile *objfile,
1335*ef5ccd6cSJohn Marino 		       int section)
1336*ef5ccd6cSJohn Marino {
1337*ef5ccd6cSJohn Marino   struct block *static_block;
1338*ef5ccd6cSJohn Marino 
1339*ef5ccd6cSJohn Marino   static_block = end_symtab_get_static_block (end_addr, objfile, 1, 0);
1340*ef5ccd6cSJohn Marino   return end_symtab_from_static_block (static_block, objfile, section, 1);
1341*ef5ccd6cSJohn Marino }
1342*ef5ccd6cSJohn Marino 
1343*ef5ccd6cSJohn Marino /* Subroutine of augment_type_symtab to simplify it.
1344*ef5ccd6cSJohn Marino    Attach SYMTAB to all symbols in PENDING_LIST that don't have one.  */
1345*ef5ccd6cSJohn Marino 
1346*ef5ccd6cSJohn Marino static void
set_missing_symtab(struct pending * pending_list,struct symtab * symtab)1347*ef5ccd6cSJohn Marino set_missing_symtab (struct pending *pending_list, struct symtab *symtab)
1348*ef5ccd6cSJohn Marino {
1349*ef5ccd6cSJohn Marino   struct pending *pending;
1350*ef5ccd6cSJohn Marino   int i;
1351*ef5ccd6cSJohn Marino 
1352*ef5ccd6cSJohn Marino   for (pending = pending_list; pending != NULL; pending = pending->next)
1353*ef5ccd6cSJohn Marino     {
1354*ef5ccd6cSJohn Marino       for (i = 0; i < pending->nsyms; ++i)
1355*ef5ccd6cSJohn Marino 	{
1356*ef5ccd6cSJohn Marino 	  if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
1357*ef5ccd6cSJohn Marino 	    SYMBOL_SYMTAB (pending->symbol[i]) = symtab;
1358*ef5ccd6cSJohn Marino 	}
1359*ef5ccd6cSJohn Marino     }
1360*ef5ccd6cSJohn Marino }
1361*ef5ccd6cSJohn Marino 
1362*ef5ccd6cSJohn Marino /* Same as end_symtab, but for the case where we're adding more symbols
1363*ef5ccd6cSJohn Marino    to an existing symtab that is known to contain only type information.
1364*ef5ccd6cSJohn Marino    This is the case for DWARF4 Type Units.  */
1365*ef5ccd6cSJohn Marino 
1366*ef5ccd6cSJohn Marino void
augment_type_symtab(struct objfile * objfile,struct symtab * primary_symtab)1367*ef5ccd6cSJohn Marino augment_type_symtab (struct objfile *objfile, struct symtab *primary_symtab)
1368*ef5ccd6cSJohn Marino {
1369*ef5ccd6cSJohn Marino   struct blockvector *blockvector = primary_symtab->blockvector;
1370*ef5ccd6cSJohn Marino   int i;
1371*ef5ccd6cSJohn Marino 
1372*ef5ccd6cSJohn Marino   if (context_stack_depth > 0)
1373*ef5ccd6cSJohn Marino     {
1374*ef5ccd6cSJohn Marino       complaint (&symfile_complaints,
1375*ef5ccd6cSJohn Marino 		 _("Context stack not empty in augment_type_symtab"));
1376*ef5ccd6cSJohn Marino       context_stack_depth = 0;
1377*ef5ccd6cSJohn Marino     }
1378*ef5ccd6cSJohn Marino   if (pending_blocks != NULL)
1379*ef5ccd6cSJohn Marino     complaint (&symfile_complaints, _("Blocks in a type symtab"));
1380*ef5ccd6cSJohn Marino   if (pending_macros != NULL)
1381*ef5ccd6cSJohn Marino     complaint (&symfile_complaints, _("Macro in a type symtab"));
1382*ef5ccd6cSJohn Marino   if (have_line_numbers)
1383*ef5ccd6cSJohn Marino     complaint (&symfile_complaints,
1384*ef5ccd6cSJohn Marino 	       _("Line numbers recorded in a type symtab"));
1385*ef5ccd6cSJohn Marino 
1386*ef5ccd6cSJohn Marino   if (file_symbols != NULL)
1387*ef5ccd6cSJohn Marino     {
1388*ef5ccd6cSJohn Marino       struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1389*ef5ccd6cSJohn Marino 
1390*ef5ccd6cSJohn Marino       /* First mark any symbols without a specified symtab as belonging
1391*ef5ccd6cSJohn Marino 	 to the primary symtab.  */
1392*ef5ccd6cSJohn Marino       set_missing_symtab (file_symbols, primary_symtab);
1393*ef5ccd6cSJohn Marino 
1394*ef5ccd6cSJohn Marino       dict_add_pending (BLOCK_DICT (block), file_symbols);
1395*ef5ccd6cSJohn Marino     }
1396*ef5ccd6cSJohn Marino 
1397*ef5ccd6cSJohn Marino   if (global_symbols != NULL)
1398*ef5ccd6cSJohn Marino     {
1399*ef5ccd6cSJohn Marino       struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1400*ef5ccd6cSJohn Marino 
1401*ef5ccd6cSJohn Marino       /* First mark any symbols without a specified symtab as belonging
1402*ef5ccd6cSJohn Marino 	 to the primary symtab.  */
1403*ef5ccd6cSJohn Marino       set_missing_symtab (global_symbols, primary_symtab);
1404*ef5ccd6cSJohn Marino 
1405*ef5ccd6cSJohn Marino       dict_add_pending (BLOCK_DICT (block), global_symbols);
1406*ef5ccd6cSJohn Marino     }
1407*ef5ccd6cSJohn Marino 
1408*ef5ccd6cSJohn Marino   reset_symtab_globals ();
1409*ef5ccd6cSJohn Marino }
1410*ef5ccd6cSJohn Marino 
14115796c8dcSSimon Schubert /* Push a context block.  Args are an identifying nesting level
14125796c8dcSSimon Schubert    (checkable when you pop it), and the starting PC address of this
14135796c8dcSSimon Schubert    context.  */
14145796c8dcSSimon Schubert 
14155796c8dcSSimon Schubert struct context_stack *
push_context(int desc,CORE_ADDR valu)14165796c8dcSSimon Schubert push_context (int desc, CORE_ADDR valu)
14175796c8dcSSimon Schubert {
14185796c8dcSSimon Schubert   struct context_stack *new;
14195796c8dcSSimon Schubert 
14205796c8dcSSimon Schubert   if (context_stack_depth == context_stack_size)
14215796c8dcSSimon Schubert     {
14225796c8dcSSimon Schubert       context_stack_size *= 2;
14235796c8dcSSimon Schubert       context_stack = (struct context_stack *)
14245796c8dcSSimon Schubert 	xrealloc ((char *) context_stack,
14255796c8dcSSimon Schubert 		  (context_stack_size * sizeof (struct context_stack)));
14265796c8dcSSimon Schubert     }
14275796c8dcSSimon Schubert 
14285796c8dcSSimon Schubert   new = &context_stack[context_stack_depth++];
14295796c8dcSSimon Schubert   new->depth = desc;
14305796c8dcSSimon Schubert   new->locals = local_symbols;
14315796c8dcSSimon Schubert   new->old_blocks = pending_blocks;
14325796c8dcSSimon Schubert   new->start_addr = valu;
14335796c8dcSSimon Schubert   new->using_directives = using_directives;
14345796c8dcSSimon Schubert   new->name = NULL;
14355796c8dcSSimon Schubert 
14365796c8dcSSimon Schubert   local_symbols = NULL;
14375796c8dcSSimon Schubert   using_directives = NULL;
14385796c8dcSSimon Schubert 
14395796c8dcSSimon Schubert   return new;
14405796c8dcSSimon Schubert }
14415796c8dcSSimon Schubert 
14425796c8dcSSimon Schubert /* Pop a context block.  Returns the address of the context block just
14435796c8dcSSimon Schubert    popped.  */
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert struct context_stack *
pop_context(void)14465796c8dcSSimon Schubert pop_context (void)
14475796c8dcSSimon Schubert {
14485796c8dcSSimon Schubert   gdb_assert (context_stack_depth > 0);
14495796c8dcSSimon Schubert   return (&context_stack[--context_stack_depth]);
14505796c8dcSSimon Schubert }
14515796c8dcSSimon Schubert 
14525796c8dcSSimon Schubert 
14535796c8dcSSimon Schubert 
14545796c8dcSSimon Schubert /* Compute a small integer hash code for the given name.  */
14555796c8dcSSimon Schubert 
14565796c8dcSSimon Schubert int
hashname(const char * name)1457*ef5ccd6cSJohn Marino hashname (const char *name)
14585796c8dcSSimon Schubert {
14595796c8dcSSimon Schubert     return (hash(name,strlen(name)) % HASHSIZE);
14605796c8dcSSimon Schubert }
14615796c8dcSSimon Schubert 
14625796c8dcSSimon Schubert 
14635796c8dcSSimon Schubert void
record_debugformat(const char * format)1464a45ae5f8SJohn Marino record_debugformat (const char *format)
14655796c8dcSSimon Schubert {
1466a45ae5f8SJohn Marino   current_subfile->debugformat = format;
14675796c8dcSSimon Schubert }
14685796c8dcSSimon Schubert 
14695796c8dcSSimon Schubert void
record_producer(const char * producer)14705796c8dcSSimon Schubert record_producer (const char *producer)
14715796c8dcSSimon Schubert {
1472a45ae5f8SJohn Marino   current_subfile->producer = producer;
14735796c8dcSSimon Schubert }
14745796c8dcSSimon Schubert 
14755796c8dcSSimon Schubert /* Merge the first symbol list SRCLIST into the second symbol list
14765796c8dcSSimon Schubert    TARGETLIST by repeated calls to add_symbol_to_list().  This
14775796c8dcSSimon Schubert    procedure "frees" each link of SRCLIST by adding it to the
14785796c8dcSSimon Schubert    free_pendings list.  Caller must set SRCLIST to a null list after
14795796c8dcSSimon Schubert    calling this function.
14805796c8dcSSimon Schubert 
14815796c8dcSSimon Schubert    Void return.  */
14825796c8dcSSimon Schubert 
14835796c8dcSSimon Schubert void
merge_symbol_lists(struct pending ** srclist,struct pending ** targetlist)14845796c8dcSSimon Schubert merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
14855796c8dcSSimon Schubert {
14865796c8dcSSimon Schubert   int i;
14875796c8dcSSimon Schubert 
14885796c8dcSSimon Schubert   if (!srclist || !*srclist)
14895796c8dcSSimon Schubert     return;
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert   /* Merge in elements from current link.  */
14925796c8dcSSimon Schubert   for (i = 0; i < (*srclist)->nsyms; i++)
14935796c8dcSSimon Schubert     add_symbol_to_list ((*srclist)->symbol[i], targetlist);
14945796c8dcSSimon Schubert 
14955796c8dcSSimon Schubert   /* Recurse on next.  */
14965796c8dcSSimon Schubert   merge_symbol_lists (&(*srclist)->next, targetlist);
14975796c8dcSSimon Schubert 
14985796c8dcSSimon Schubert   /* "Free" the current link.  */
14995796c8dcSSimon Schubert   (*srclist)->next = free_pendings;
15005796c8dcSSimon Schubert   free_pendings = (*srclist);
15015796c8dcSSimon Schubert }
15025796c8dcSSimon Schubert 
1503*ef5ccd6cSJohn Marino 
1504*ef5ccd6cSJohn Marino /* Name of source file whose symbol data we are now processing.  This
1505*ef5ccd6cSJohn Marino    comes from a symbol of type N_SO for stabs.  For Dwarf it comes
1506*ef5ccd6cSJohn Marino    from the DW_AT_name attribute of a DW_TAG_compile_unit DIE.  */
1507*ef5ccd6cSJohn Marino 
1508*ef5ccd6cSJohn Marino static char *last_source_file;
1509*ef5ccd6cSJohn Marino 
1510*ef5ccd6cSJohn Marino /* See buildsym.h.  */
1511*ef5ccd6cSJohn Marino 
1512*ef5ccd6cSJohn Marino void
set_last_source_file(const char * name)1513*ef5ccd6cSJohn Marino set_last_source_file (const char *name)
1514*ef5ccd6cSJohn Marino {
1515*ef5ccd6cSJohn Marino   xfree (last_source_file);
1516*ef5ccd6cSJohn Marino   last_source_file = name == NULL ? NULL : xstrdup (name);
1517*ef5ccd6cSJohn Marino }
1518*ef5ccd6cSJohn Marino 
1519*ef5ccd6cSJohn Marino /* See buildsym.h.  */
1520*ef5ccd6cSJohn Marino 
1521*ef5ccd6cSJohn Marino const char *
get_last_source_file(void)1522*ef5ccd6cSJohn Marino get_last_source_file (void)
1523*ef5ccd6cSJohn Marino {
1524*ef5ccd6cSJohn Marino   return last_source_file;
1525*ef5ccd6cSJohn Marino }
1526*ef5ccd6cSJohn Marino 
1527*ef5ccd6cSJohn Marino 
1528*ef5ccd6cSJohn Marino 
15295796c8dcSSimon Schubert /* Initialize anything that needs initializing when starting to read a
15305796c8dcSSimon Schubert    fresh piece of a symbol file, e.g. reading in the stuff
15315796c8dcSSimon Schubert    corresponding to a psymtab.  */
15325796c8dcSSimon Schubert 
15335796c8dcSSimon Schubert void
buildsym_init(void)15345796c8dcSSimon Schubert buildsym_init (void)
15355796c8dcSSimon Schubert {
15365796c8dcSSimon Schubert   free_pendings = NULL;
15375796c8dcSSimon Schubert   file_symbols = NULL;
15385796c8dcSSimon Schubert   global_symbols = NULL;
15395796c8dcSSimon Schubert   pending_blocks = NULL;
15405796c8dcSSimon Schubert   pending_macros = NULL;
1541*ef5ccd6cSJohn Marino   using_directives = NULL;
15425796c8dcSSimon Schubert 
15435796c8dcSSimon Schubert   /* We shouldn't have any address map at this point.  */
15445796c8dcSSimon Schubert   gdb_assert (! pending_addrmap);
15455796c8dcSSimon Schubert   pending_addrmap_interesting = 0;
15465796c8dcSSimon Schubert }
15475796c8dcSSimon Schubert 
15485796c8dcSSimon Schubert /* Initialize anything that needs initializing when a completely new
15495796c8dcSSimon Schubert    symbol file is specified (not just adding some symbols from another
15505796c8dcSSimon Schubert    file, e.g. a shared library).  */
15515796c8dcSSimon Schubert 
15525796c8dcSSimon Schubert void
buildsym_new_init(void)15535796c8dcSSimon Schubert buildsym_new_init (void)
15545796c8dcSSimon Schubert {
15555796c8dcSSimon Schubert   buildsym_init ();
15565796c8dcSSimon Schubert }
1557