119370Spst/* Support routines for building symbol tables in GDB's internal format.
298944Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3130803Smarcel   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4130803Smarcel   Free Software Foundation, Inc.
519370Spst
698944Sobrien   This file is part of GDB.
719370Spst
898944Sobrien   This program is free software; you can redistribute it and/or modify
998944Sobrien   it under the terms of the GNU General Public License as published by
1098944Sobrien   the Free Software Foundation; either version 2 of the License, or
1198944Sobrien   (at your option) any later version.
1219370Spst
1398944Sobrien   This program is distributed in the hope that it will be useful,
1498944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698944Sobrien   GNU General Public License for more details.
1719370Spst
1898944Sobrien   You should have received a copy of the GNU General Public License
1998944Sobrien   along with this program; if not, write to the Free Software
2098944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198944Sobrien   Boston, MA 02111-1307, USA.  */
2219370Spst
2319370Spst/* This module provides subroutines used for creating and adding to
2419370Spst   the symbol table.  These routines are called from various symbol-
2519370Spst   file-reading routines.
2619370Spst
2719370Spst   Routines to support specific debugging information formats (stabs,
2819370Spst   DWARF, etc) belong somewhere else. */
2919370Spst
3019370Spst#include "defs.h"
3119370Spst#include "bfd.h"
32130803Smarcel#include "gdb_obstack.h"
3319370Spst#include "symtab.h"
34130803Smarcel#include "symfile.h"
3519370Spst#include "objfiles.h"
3619370Spst#include "gdbtypes.h"
37130803Smarcel#include "gdb_assert.h"
3819370Spst#include "complaints.h"
3919370Spst#include "gdb_string.h"
4098944Sobrien#include "expression.h"		/* For "enum exp_opcode" used by... */
41130803Smarcel#include "language.h"		/* For "local_hex_string" */
4298944Sobrien#include "bcache.h"
4398944Sobrien#include "filenames.h"		/* For DOSish file names */
44130803Smarcel#include "macrotab.h"
45130803Smarcel#include "demangle.h"		/* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
46130803Smarcel#include "block.h"
47130803Smarcel#include "cp-support.h"
48130803Smarcel#include "dictionary.h"
49130803Smarcel
5019370Spst/* Ask buildsym.h to define the vars it normally declares `extern'.  */
5198944Sobrien#define	EXTERN
5298944Sobrien/**/
5319370Spst#include "buildsym.h"		/* Our own declarations */
5419370Spst#undef	EXTERN
5519370Spst
5619370Spst/* For cleanup_undefined_types and finish_global_stabs (somewhat
5719370Spst   questionable--see comment where we call them).  */
5846283Sdfr
5919370Spst#include "stabsread.h"
6019370Spst
6146283Sdfr/* List of free `struct pending' structures for reuse.  */
6219370Spst
6346283Sdfrstatic struct pending *free_pendings;
6419370Spst
6546283Sdfr/* Non-zero if symtab has line number info.  This prevents an
6646283Sdfr   otherwise empty symtab from being tossed.  */
6746283Sdfr
6846283Sdfrstatic int have_line_numbers;
6919370Spst
7046283Sdfrstatic int compare_line_numbers (const void *ln1p, const void *ln2p);
7146283Sdfr
7219370Spst
7346283Sdfr/* Initial sizes of data structures.  These are realloc'd larger if
7446283Sdfr   needed, and realloc'd down to the size actually used, when
7546283Sdfr   completed.  */
7646283Sdfr
7719370Spst#define	INITIAL_CONTEXT_STACK_SIZE	10
7819370Spst#define	INITIAL_LINE_VECTOR_LENGTH	1000
7946283Sdfr
8019370Spst
8119370Spst/* maintain the lists of symbols and blocks */
8219370Spst
8398944Sobrien/* Add a pending list to free_pendings. */
8498944Sobrienvoid
8598944Sobrienadd_free_pendings (struct pending *list)
8698944Sobrien{
87130803Smarcel  struct pending *link = list;
8898944Sobrien
8998944Sobrien  if (list)
9098944Sobrien    {
9198944Sobrien      while (link->next) link = link->next;
9298944Sobrien      link->next = free_pendings;
9398944Sobrien      free_pendings = list;
9498944Sobrien    }
9598944Sobrien}
9698944Sobrien
97130803Smarcel/* Add a symbol to one of the lists of symbols.  While we're at it, if
98130803Smarcel   we're in the C++ case and don't have full namespace debugging info,
99130803Smarcel   check to see if it references an anonymous namespace; if so, add an
100130803Smarcel   appropriate using directive.  */
10119370Spst
10219370Spstvoid
10346283Sdfradd_symbol_to_list (struct symbol *symbol, struct pending **listhead)
10419370Spst{
105130803Smarcel  struct pending *link;
10646283Sdfr
10746283Sdfr  /* If this is an alias for another symbol, don't add it.  */
10846283Sdfr  if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
10946283Sdfr    return;
11046283Sdfr
11146283Sdfr  /* We keep PENDINGSIZE symbols in each link of the list. If we
11246283Sdfr     don't have a link with room in it, add a new link.  */
11319370Spst  if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
11419370Spst    {
11519370Spst      if (free_pendings)
11619370Spst	{
11719370Spst	  link = free_pendings;
11819370Spst	  free_pendings = link->next;
11919370Spst	}
12019370Spst      else
12119370Spst	{
12219370Spst	  link = (struct pending *) xmalloc (sizeof (struct pending));
12319370Spst	}
12419370Spst
12519370Spst      link->next = *listhead;
12619370Spst      *listhead = link;
12719370Spst      link->nsyms = 0;
12819370Spst    }
12919370Spst
13019370Spst  (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
131130803Smarcel
132130803Smarcel  /* Check to see if we might need to look for a mention of anonymous
133130803Smarcel     namespaces.  */
134130803Smarcel
135130803Smarcel  if (SYMBOL_LANGUAGE (symbol) == language_cplus)
136130803Smarcel    cp_scan_for_anonymous_namespaces (symbol);
13719370Spst}
13819370Spst
13946283Sdfr/* Find a symbol named NAME on a LIST.  NAME need not be
14046283Sdfr   '\0'-terminated; LENGTH is the length of the name.  */
14119370Spst
14219370Spststruct symbol *
14346283Sdfrfind_symbol_in_list (struct pending *list, char *name, int length)
14419370Spst{
14519370Spst  int j;
14619370Spst  char *pp;
14719370Spst
14819370Spst  while (list != NULL)
14919370Spst    {
15046283Sdfr      for (j = list->nsyms; --j >= 0;)
15119370Spst	{
152130803Smarcel	  pp = DEPRECATED_SYMBOL_NAME (list->symbol[j]);
15319370Spst	  if (*pp == *name && strncmp (pp, name, length) == 0 &&
15419370Spst	      pp[length] == '\0')
15519370Spst	    {
15619370Spst	      return (list->symbol[j]);
15719370Spst	    }
15819370Spst	}
15919370Spst      list = list->next;
16019370Spst    }
16119370Spst  return (NULL);
16219370Spst}
16319370Spst
16446283Sdfr/* At end of reading syms, or in case of quit, really free as many
16546283Sdfr   `struct pending's as we can easily find. */
16619370Spst
16719370Spstvoid
168130803Smarcelreally_free_pendings (void *dummy)
16919370Spst{
17019370Spst  struct pending *next, *next1;
17119370Spst
17219370Spst  for (next = free_pendings; next; next = next1)
17319370Spst    {
17419370Spst      next1 = next->next;
17598944Sobrien      xfree ((void *) next);
17619370Spst    }
17719370Spst  free_pendings = NULL;
17819370Spst
17946283Sdfr  free_pending_blocks ();
18019370Spst
18119370Spst  for (next = file_symbols; next != NULL; next = next1)
18219370Spst    {
18319370Spst      next1 = next->next;
18498944Sobrien      xfree ((void *) next);
18519370Spst    }
18619370Spst  file_symbols = NULL;
18719370Spst
18819370Spst  for (next = global_symbols; next != NULL; next = next1)
18919370Spst    {
19019370Spst      next1 = next->next;
19198944Sobrien      xfree ((void *) next);
19219370Spst    }
19319370Spst  global_symbols = NULL;
194130803Smarcel
195130803Smarcel  if (pending_macros)
196130803Smarcel    free_macro_table (pending_macros);
19719370Spst}
19819370Spst
19946283Sdfr/* This function is called to discard any pending blocks. */
20019370Spst
20119370Spstvoid
20246283Sdfrfree_pending_blocks (void)
20319370Spst{
20446283Sdfr#if 0				/* Now we make the links in the
205130803Smarcel				   objfile_obstack, so don't free
20646283Sdfr				   them.  */
20746283Sdfr  struct pending_block *bnext, *bnext1;
20846283Sdfr
20946283Sdfr  for (bnext = pending_blocks; bnext; bnext = bnext1)
21046283Sdfr    {
21146283Sdfr      bnext1 = bnext->next;
21298944Sobrien      xfree ((void *) bnext);
21346283Sdfr    }
21446283Sdfr#endif
21546283Sdfr  pending_blocks = NULL;
21646283Sdfr}
21746283Sdfr
21846283Sdfr/* Take one of the lists of symbols and make a block from it.  Keep
21946283Sdfr   the order the symbols have in the list (reversed from the input
22046283Sdfr   file).  Put the block on the list of pending blocks.  */
22146283Sdfr
22246283Sdfrvoid
22346283Sdfrfinish_block (struct symbol *symbol, struct pending **listhead,
22446283Sdfr	      struct pending_block *old_blocks,
22546283Sdfr	      CORE_ADDR start, CORE_ADDR end,
22646283Sdfr	      struct objfile *objfile)
22746283Sdfr{
228130803Smarcel  struct pending *next, *next1;
229130803Smarcel  struct block *block;
230130803Smarcel  struct pending_block *pblock;
23119370Spst  struct pending_block *opblock;
23219370Spst
233130803Smarcel  block = allocate_block (&objfile->objfile_obstack);
23419370Spst
235130803Smarcel  if (symbol)
23619370Spst    {
237130803Smarcel      BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
238130803Smarcel					       *listhead);
23919370Spst    }
240130803Smarcel  else
24119370Spst    {
242130803Smarcel      BLOCK_DICT (block) = dict_create_hashed (&objfile->objfile_obstack,
243130803Smarcel					       *listhead);
24419370Spst    }
24519370Spst
24619370Spst  BLOCK_START (block) = start;
24719370Spst  BLOCK_END (block) = end;
24846283Sdfr  /* Superblock filled in when containing block is made */
24919370Spst  BLOCK_SUPERBLOCK (block) = NULL;
250130803Smarcel  BLOCK_NAMESPACE (block) = NULL;
25146283Sdfr
25219370Spst  BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
25319370Spst
25419370Spst  /* Put the block in as the value of the symbol that names it.  */
25519370Spst
25619370Spst  if (symbol)
25719370Spst    {
25819370Spst      struct type *ftype = SYMBOL_TYPE (symbol);
259130803Smarcel      struct dict_iterator iter;
26019370Spst      SYMBOL_BLOCK_VALUE (symbol) = block;
26119370Spst      BLOCK_FUNCTION (block) = symbol;
26219370Spst
26319370Spst      if (TYPE_NFIELDS (ftype) <= 0)
26419370Spst	{
26546283Sdfr	  /* No parameter type information is recorded with the
26646283Sdfr	     function's type.  Set that from the type of the
26746283Sdfr	     parameter symbols. */
26819370Spst	  int nparams = 0, iparams;
26919370Spst	  struct symbol *sym;
270130803Smarcel	  ALL_BLOCK_SYMBOLS (block, iter, sym)
27119370Spst	    {
27219370Spst	      switch (SYMBOL_CLASS (sym))
27319370Spst		{
27419370Spst		case LOC_ARG:
27519370Spst		case LOC_REF_ARG:
27619370Spst		case LOC_REGPARM:
27719370Spst		case LOC_REGPARM_ADDR:
27846283Sdfr		case LOC_BASEREG_ARG:
27946283Sdfr		case LOC_LOCAL_ARG:
280130803Smarcel		case LOC_COMPUTED_ARG:
28119370Spst		  nparams++;
28219370Spst		  break;
28319370Spst		case LOC_UNDEF:
28419370Spst		case LOC_CONST:
28519370Spst		case LOC_STATIC:
28646283Sdfr		case LOC_INDIRECT:
28719370Spst		case LOC_REGISTER:
28819370Spst		case LOC_LOCAL:
28919370Spst		case LOC_TYPEDEF:
29019370Spst		case LOC_LABEL:
29119370Spst		case LOC_BLOCK:
29219370Spst		case LOC_CONST_BYTES:
29319370Spst		case LOC_BASEREG:
29419370Spst		case LOC_UNRESOLVED:
29519370Spst		case LOC_OPTIMIZED_OUT:
296130803Smarcel		case LOC_COMPUTED:
29719370Spst		default:
29819370Spst		  break;
29919370Spst		}
30019370Spst	    }
30119370Spst	  if (nparams > 0)
30219370Spst	    {
30319370Spst	      TYPE_NFIELDS (ftype) = nparams;
30419370Spst	      TYPE_FIELDS (ftype) = (struct field *)
30519370Spst		TYPE_ALLOC (ftype, nparams * sizeof (struct field));
30646283Sdfr
307130803Smarcel	      iparams = 0;
308130803Smarcel	      ALL_BLOCK_SYMBOLS (block, iter, sym)
30919370Spst		{
310130803Smarcel		  if (iparams == nparams)
311130803Smarcel		    break;
312130803Smarcel
31319370Spst		  switch (SYMBOL_CLASS (sym))
31419370Spst		    {
31519370Spst		    case LOC_ARG:
31619370Spst		    case LOC_REF_ARG:
31719370Spst		    case LOC_REGPARM:
31819370Spst		    case LOC_REGPARM_ADDR:
31946283Sdfr		    case LOC_BASEREG_ARG:
32046283Sdfr		    case LOC_LOCAL_ARG:
321130803Smarcel		    case LOC_COMPUTED_ARG:
32219370Spst		      TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
32398944Sobrien		      TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
32419370Spst		      iparams++;
32519370Spst		      break;
32619370Spst		    case LOC_UNDEF:
32719370Spst		    case LOC_CONST:
32819370Spst		    case LOC_STATIC:
32946283Sdfr		    case LOC_INDIRECT:
33019370Spst		    case LOC_REGISTER:
33119370Spst		    case LOC_LOCAL:
33219370Spst		    case LOC_TYPEDEF:
33319370Spst		    case LOC_LABEL:
33419370Spst		    case LOC_BLOCK:
33519370Spst		    case LOC_CONST_BYTES:
33619370Spst		    case LOC_BASEREG:
33719370Spst		    case LOC_UNRESOLVED:
33819370Spst		    case LOC_OPTIMIZED_OUT:
339130803Smarcel		    case LOC_COMPUTED:
34019370Spst		    default:
34119370Spst		      break;
34219370Spst		    }
34319370Spst		}
34419370Spst	    }
34519370Spst	}
346130803Smarcel
347130803Smarcel      /* If we're in the C++ case, set the block's scope.  */
348130803Smarcel      if (SYMBOL_LANGUAGE (symbol) == language_cplus)
349130803Smarcel	{
350130803Smarcel	  cp_set_block_scope (symbol, block, &objfile->objfile_obstack);
351130803Smarcel	}
35219370Spst    }
35319370Spst  else
35419370Spst    {
35519370Spst      BLOCK_FUNCTION (block) = NULL;
35619370Spst    }
35719370Spst
35819370Spst  /* Now "free" the links of the list, and empty the list.  */
35919370Spst
36019370Spst  for (next = *listhead; next; next = next1)
36119370Spst    {
36219370Spst      next1 = next->next;
36319370Spst      next->next = free_pendings;
36419370Spst      free_pendings = next;
36519370Spst    }
36619370Spst  *listhead = NULL;
36719370Spst
36846283Sdfr#if 1
36946283Sdfr  /* Check to be sure that the blocks have an end address that is
37046283Sdfr     greater than starting address */
37119370Spst
37246283Sdfr  if (BLOCK_END (block) < BLOCK_START (block))
37346283Sdfr    {
37446283Sdfr      if (symbol)
37546283Sdfr	{
376130803Smarcel	  complaint (&symfile_complaints,
377130803Smarcel		     "block end address less than block start address in %s (patched it)",
378130803Smarcel		     SYMBOL_PRINT_NAME (symbol));
37946283Sdfr	}
38046283Sdfr      else
38146283Sdfr	{
382130803Smarcel	  complaint (&symfile_complaints,
383130803Smarcel		     "block end address 0x%s less than block start address 0x%s (patched it)",
384130803Smarcel		     paddr_nz (BLOCK_END (block)), paddr_nz (BLOCK_START (block)));
38546283Sdfr	}
38646283Sdfr      /* Better than nothing */
38746283Sdfr      BLOCK_END (block) = BLOCK_START (block);
38846283Sdfr    }
38946283Sdfr#endif
39046283Sdfr
39146283Sdfr  /* Install this block as the superblock of all blocks made since the
39246283Sdfr     start of this scope that don't have superblocks yet.  */
39346283Sdfr
39419370Spst  opblock = NULL;
395130803Smarcel  for (pblock = pending_blocks;
396130803Smarcel       pblock && pblock != old_blocks;
397130803Smarcel       pblock = pblock->next)
39819370Spst    {
39919370Spst      if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
40019370Spst	{
40119370Spst#if 1
40246283Sdfr	  /* Check to be sure the blocks are nested as we receive
40346283Sdfr	     them. If the compiler/assembler/linker work, this just
40446283Sdfr	     burns a small amount of time.  */
40519370Spst	  if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
40646283Sdfr	      BLOCK_END (pblock->block) > BLOCK_END (block))
40719370Spst	    {
40819370Spst	      if (symbol)
40919370Spst		{
410130803Smarcel		  complaint (&symfile_complaints,
411130803Smarcel			     "inner block not inside outer block in %s",
412130803Smarcel			     SYMBOL_PRINT_NAME (symbol));
41319370Spst		}
41419370Spst	      else
41519370Spst		{
416130803Smarcel		  complaint (&symfile_complaints,
417130803Smarcel			     "inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)",
418130803Smarcel			     paddr_nz (BLOCK_START (pblock->block)),
419130803Smarcel			     paddr_nz (BLOCK_END (pblock->block)),
420130803Smarcel			     paddr_nz (BLOCK_START (block)),
421130803Smarcel			     paddr_nz (BLOCK_END (block)));
42219370Spst		}
42346283Sdfr	      if (BLOCK_START (pblock->block) < BLOCK_START (block))
42446283Sdfr		BLOCK_START (pblock->block) = BLOCK_START (block);
42546283Sdfr	      if (BLOCK_END (pblock->block) > BLOCK_END (block))
42646283Sdfr		BLOCK_END (pblock->block) = BLOCK_END (block);
42719370Spst	    }
42819370Spst#endif
42919370Spst	  BLOCK_SUPERBLOCK (pblock->block) = block;
43019370Spst	}
43119370Spst      opblock = pblock;
43219370Spst    }
43319370Spst
43446283Sdfr  record_pending_block (objfile, block, opblock);
43546283Sdfr}
43619370Spst
437130803Smarcel
43846283Sdfr/* Record BLOCK on the list of all blocks in the file.  Put it after
43946283Sdfr   OPBLOCK, or at the beginning if opblock is NULL.  This puts the
44046283Sdfr   block in the list after all its subblocks.
44146283Sdfr
442130803Smarcel   Allocate the pending block struct in the objfile_obstack to save
44346283Sdfr   time.  This wastes a little space.  FIXME: Is it worth it?  */
44446283Sdfr
44546283Sdfrvoid
44646283Sdfrrecord_pending_block (struct objfile *objfile, struct block *block,
44746283Sdfr		      struct pending_block *opblock)
44846283Sdfr{
449130803Smarcel  struct pending_block *pblock;
45046283Sdfr
45119370Spst  pblock = (struct pending_block *)
452130803Smarcel    obstack_alloc (&objfile->objfile_obstack, sizeof (struct pending_block));
45319370Spst  pblock->block = block;
45419370Spst  if (opblock)
45519370Spst    {
45619370Spst      pblock->next = opblock->next;
45719370Spst      opblock->next = pblock;
45819370Spst    }
45919370Spst  else
46019370Spst    {
46119370Spst      pblock->next = pending_blocks;
46219370Spst      pending_blocks = pblock;
46319370Spst    }
46419370Spst}
46519370Spst
466130803Smarcelstatic struct blockvector *
46746283Sdfrmake_blockvector (struct objfile *objfile)
46819370Spst{
469130803Smarcel  struct pending_block *next;
470130803Smarcel  struct blockvector *blockvector;
471130803Smarcel  int i;
47219370Spst
47319370Spst  /* Count the length of the list of blocks.  */
47419370Spst
47546283Sdfr  for (next = pending_blocks, i = 0; next; next = next->next, i++)
47646283Sdfr    {;
47746283Sdfr    }
47819370Spst
47919370Spst  blockvector = (struct blockvector *)
480130803Smarcel    obstack_alloc (&objfile->objfile_obstack,
48119370Spst		   (sizeof (struct blockvector)
48219370Spst		    + (i - 1) * sizeof (struct block *)));
48319370Spst
48446283Sdfr  /* Copy the blocks into the blockvector. This is done in reverse
48546283Sdfr     order, which happens to put the blocks into the proper order
48646283Sdfr     (ascending starting address). finish_block has hair to insert
48746283Sdfr     each block into the list after its subblocks in order to make
48846283Sdfr     sure this is true.  */
48919370Spst
49019370Spst  BLOCKVECTOR_NBLOCKS (blockvector) = i;
49119370Spst  for (next = pending_blocks; next; next = next->next)
49219370Spst    {
49319370Spst      BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
49419370Spst    }
49519370Spst
49646283Sdfr#if 0				/* Now we make the links in the
49746283Sdfr				   obstack, so don't free them.  */
49819370Spst  /* Now free the links of the list, and empty the list.  */
49919370Spst
50019370Spst  for (next = pending_blocks; next; next = next1)
50119370Spst    {
50219370Spst      next1 = next->next;
50398944Sobrien      xfree (next);
50419370Spst    }
50519370Spst#endif
50619370Spst  pending_blocks = NULL;
50719370Spst
50846283Sdfr#if 1				/* FIXME, shut this off after a while
50946283Sdfr				   to speed up symbol reading.  */
51046283Sdfr  /* Some compilers output blocks in the wrong order, but we depend on
51146283Sdfr     their being in the right order so we can binary search. Check the
51246283Sdfr     order and moan about it.  FIXME.  */
51319370Spst  if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
51419370Spst    {
51519370Spst      for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
51619370Spst	{
51746283Sdfr	  if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
51846283Sdfr	      > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
51919370Spst	    {
52098944Sobrien	      CORE_ADDR start
52198944Sobrien		= BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
52219370Spst
523130803Smarcel	      complaint (&symfile_complaints, "block at %s out of order",
524130803Smarcel			 local_hex_string ((LONGEST) start));
52519370Spst	    }
52619370Spst	}
52719370Spst    }
52819370Spst#endif
52919370Spst
53019370Spst  return (blockvector);
53119370Spst}
53219370Spst
53346283Sdfr/* Start recording information about source code that came from an
53446283Sdfr   included (or otherwise merged-in) source file with a different
53546283Sdfr   name.  NAME is the name of the file (cannot be NULL), DIRNAME is
53646283Sdfr   the directory in which it resides (or NULL if not known).  */
53719370Spst
53819370Spstvoid
53946283Sdfrstart_subfile (char *name, char *dirname)
54019370Spst{
541130803Smarcel  struct subfile *subfile;
54219370Spst
54346283Sdfr  /* See if this subfile is already known as a subfile of the current
54446283Sdfr     main source file.  */
54519370Spst
54619370Spst  for (subfile = subfiles; subfile; subfile = subfile->next)
54719370Spst    {
54898944Sobrien      if (FILENAME_CMP (subfile->name, name) == 0)
54919370Spst	{
55019370Spst	  current_subfile = subfile;
55119370Spst	  return;
55219370Spst	}
55319370Spst    }
55419370Spst
55546283Sdfr  /* This subfile is not known.  Add an entry for it. Make an entry
55646283Sdfr     for this subfile in the list of all subfiles of the current main
55746283Sdfr     source file.  */
55819370Spst
55919370Spst  subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
56098944Sobrien  memset ((char *) subfile, 0, sizeof (struct subfile));
56119370Spst  subfile->next = subfiles;
56219370Spst  subfiles = subfile;
56319370Spst  current_subfile = subfile;
56419370Spst
56519370Spst  /* Save its name and compilation directory name */
56619370Spst  subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
56719370Spst  subfile->dirname =
56819370Spst    (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
56946283Sdfr
57019370Spst  /* Initialize line-number recording for this subfile.  */
57119370Spst  subfile->line_vector = NULL;
57219370Spst
57346283Sdfr  /* Default the source language to whatever can be deduced from the
57446283Sdfr     filename.  If nothing can be deduced (such as for a C/C++ include
57546283Sdfr     file with a ".h" extension), then inherit whatever language the
57646283Sdfr     previous subfile had.  This kludgery is necessary because there
57746283Sdfr     is no standard way in some object formats to record the source
57846283Sdfr     language.  Also, when symtabs are allocated we try to deduce a
57946283Sdfr     language then as well, but it is too late for us to use that
58046283Sdfr     information while reading symbols, since symtabs aren't allocated
58146283Sdfr     until after all the symbols have been processed for a given
58246283Sdfr     source file. */
58319370Spst
58419370Spst  subfile->language = deduce_language_from_filename (subfile->name);
58519370Spst  if (subfile->language == language_unknown &&
58619370Spst      subfile->next != NULL)
58719370Spst    {
58819370Spst      subfile->language = subfile->next->language;
58919370Spst    }
59019370Spst
59146283Sdfr  /* Initialize the debug format string to NULL.  We may supply it
59246283Sdfr     later via a call to record_debugformat. */
59346283Sdfr  subfile->debugformat = NULL;
59446283Sdfr
595130803Smarcel  /* If the filename of this subfile ends in .C, then change the
59646283Sdfr     language of any pending subfiles from C to C++.  We also accept
597130803Smarcel     any other C++ suffixes accepted by deduce_language_from_filename.  */
59819370Spst  /* Likewise for f2c.  */
59919370Spst
60019370Spst  if (subfile->name)
60119370Spst    {
60219370Spst      struct subfile *s;
60319370Spst      enum language sublang = deduce_language_from_filename (subfile->name);
60419370Spst
60519370Spst      if (sublang == language_cplus || sublang == language_fortran)
60619370Spst	for (s = subfiles; s != NULL; s = s->next)
60719370Spst	  if (s->language == language_c)
60819370Spst	    s->language = sublang;
60919370Spst    }
61019370Spst
61119370Spst  /* And patch up this file if necessary.  */
61219370Spst  if (subfile->language == language_c
61319370Spst      && subfile->next != NULL
61419370Spst      && (subfile->next->language == language_cplus
61519370Spst	  || subfile->next->language == language_fortran))
61619370Spst    {
61719370Spst      subfile->language = subfile->next->language;
61819370Spst    }
61919370Spst}
62019370Spst
62146283Sdfr/* For stabs readers, the first N_SO symbol is assumed to be the
62246283Sdfr   source file name, and the subfile struct is initialized using that
62346283Sdfr   assumption.  If another N_SO symbol is later seen, immediately
62446283Sdfr   following the first one, then the first one is assumed to be the
62546283Sdfr   directory name and the second one is really the source file name.
62619370Spst
62746283Sdfr   So we have to patch up the subfile struct by moving the old name
62846283Sdfr   value to dirname and remembering the new name.  Some sanity
62946283Sdfr   checking is performed to ensure that the state of the subfile
63046283Sdfr   struct is reasonable and that the old name we are assuming to be a
63146283Sdfr   directory name actually is (by checking for a trailing '/'). */
63219370Spst
63319370Spstvoid
63446283Sdfrpatch_subfile_names (struct subfile *subfile, char *name)
63519370Spst{
63619370Spst  if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
63746283Sdfr      && subfile->name[strlen (subfile->name) - 1] == '/')
63819370Spst    {
63919370Spst      subfile->dirname = subfile->name;
64019370Spst      subfile->name = savestring (name, strlen (name));
64119370Spst      last_source_file = name;
64219370Spst
64319370Spst      /* Default the source language to whatever can be deduced from
64446283Sdfr         the filename.  If nothing can be deduced (such as for a C/C++
64546283Sdfr         include file with a ".h" extension), then inherit whatever
64646283Sdfr         language the previous subfile had.  This kludgery is
64746283Sdfr         necessary because there is no standard way in some object
64846283Sdfr         formats to record the source language.  Also, when symtabs
64946283Sdfr         are allocated we try to deduce a language then as well, but
65046283Sdfr         it is too late for us to use that information while reading
65146283Sdfr         symbols, since symtabs aren't allocated until after all the
65246283Sdfr         symbols have been processed for a given source file. */
65319370Spst
65419370Spst      subfile->language = deduce_language_from_filename (subfile->name);
65519370Spst      if (subfile->language == language_unknown &&
65619370Spst	  subfile->next != NULL)
65719370Spst	{
65819370Spst	  subfile->language = subfile->next->language;
65919370Spst	}
66019370Spst    }
66119370Spst}
66219370Spst
66346283Sdfr/* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
66446283Sdfr   switching source files (different subfiles, as we call them) within
66546283Sdfr   one object file, but using a stack rather than in an arbitrary
66646283Sdfr   order.  */
66719370Spst
66819370Spstvoid
66946283Sdfrpush_subfile (void)
67019370Spst{
671130803Smarcel  struct subfile_stack *tem
67246283Sdfr  = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
67319370Spst
67419370Spst  tem->next = subfile_stack;
67519370Spst  subfile_stack = tem;
67619370Spst  if (current_subfile == NULL || current_subfile->name == NULL)
67719370Spst    {
67898944Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
67919370Spst    }
68019370Spst  tem->name = current_subfile->name;
68119370Spst}
68219370Spst
68319370Spstchar *
68446283Sdfrpop_subfile (void)
68519370Spst{
686130803Smarcel  char *name;
687130803Smarcel  struct subfile_stack *link = subfile_stack;
68819370Spst
68919370Spst  if (link == NULL)
69019370Spst    {
69198944Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
69219370Spst    }
69319370Spst  name = link->name;
69419370Spst  subfile_stack = link->next;
69598944Sobrien  xfree ((void *) link);
69619370Spst  return (name);
69719370Spst}
69819370Spst
69946283Sdfr/* Add a linetable entry for line number LINE and address PC to the
70046283Sdfr   line vector for SUBFILE.  */
70119370Spst
70219370Spstvoid
703130803Smarcelrecord_line (struct subfile *subfile, int line, CORE_ADDR pc)
70419370Spst{
70519370Spst  struct linetable_entry *e;
70619370Spst  /* Ignore the dummy line number in libg.o */
70719370Spst
70819370Spst  if (line == 0xffff)
70919370Spst    {
71019370Spst      return;
71119370Spst    }
71219370Spst
71319370Spst  /* Make sure line vector exists and is big enough.  */
71419370Spst  if (!subfile->line_vector)
71519370Spst    {
71619370Spst      subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
71719370Spst      subfile->line_vector = (struct linetable *)
71819370Spst	xmalloc (sizeof (struct linetable)
71998944Sobrien	   + subfile->line_vector_length * sizeof (struct linetable_entry));
72019370Spst      subfile->line_vector->nitems = 0;
72146283Sdfr      have_line_numbers = 1;
72219370Spst    }
72319370Spst
72419370Spst  if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
72519370Spst    {
72619370Spst      subfile->line_vector_length *= 2;
72719370Spst      subfile->line_vector = (struct linetable *)
72846283Sdfr	xrealloc ((char *) subfile->line_vector,
72946283Sdfr		  (sizeof (struct linetable)
73046283Sdfr		   + (subfile->line_vector_length
73146283Sdfr		      * sizeof (struct linetable_entry))));
73219370Spst    }
73319370Spst
73419370Spst  e = subfile->line_vector->item + subfile->line_vector->nitems++;
73546283Sdfr  e->line = line;
73698944Sobrien  e->pc = ADDR_BITS_REMOVE(pc);
73719370Spst}
73819370Spst
73919370Spst/* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */
74019370Spst
74119370Spststatic int
74246283Sdfrcompare_line_numbers (const void *ln1p, const void *ln2p)
74319370Spst{
74419370Spst  struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
74519370Spst  struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
74619370Spst
74719370Spst  /* Note: this code does not assume that CORE_ADDRs can fit in ints.
74819370Spst     Please keep it that way.  */
74919370Spst  if (ln1->pc < ln2->pc)
75019370Spst    return -1;
75119370Spst
75219370Spst  if (ln1->pc > ln2->pc)
75319370Spst    return 1;
75419370Spst
75519370Spst  /* If pc equal, sort by line.  I'm not sure whether this is optimum
75619370Spst     behavior (see comment at struct linetable in symtab.h).  */
75719370Spst  return ln1->line - ln2->line;
75819370Spst}
75919370Spst
76046283Sdfr/* Start a new symtab for a new source file.  Called, for example,
76146283Sdfr   when a stabs symbol of type N_SO is seen, or when a DWARF
76246283Sdfr   TAG_compile_unit DIE is seen.  It indicates the start of data for
76346283Sdfr   one original source file.  */
76419370Spst
76519370Spstvoid
76646283Sdfrstart_symtab (char *name, char *dirname, CORE_ADDR start_addr)
76719370Spst{
76819370Spst
76919370Spst  last_source_file = name;
77019370Spst  last_source_start_addr = start_addr;
77119370Spst  file_symbols = NULL;
77219370Spst  global_symbols = NULL;
77319370Spst  within_function = 0;
77446283Sdfr  have_line_numbers = 0;
77519370Spst
77646283Sdfr  /* Context stack is initially empty.  Allocate first one with room
77746283Sdfr     for 10 levels; reuse it forever afterward.  */
77819370Spst  if (context_stack == NULL)
77919370Spst    {
78019370Spst      context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
78119370Spst      context_stack = (struct context_stack *)
78219370Spst	xmalloc (context_stack_size * sizeof (struct context_stack));
78319370Spst    }
78419370Spst  context_stack_depth = 0;
78519370Spst
786130803Smarcel  /* Set up support for C++ namespace support, in case we need it.  */
787130803Smarcel
788130803Smarcel  cp_initialize_namespace ();
789130803Smarcel
79046283Sdfr  /* Initialize the list of sub source files with one entry for this
79146283Sdfr     file (the top-level source file).  */
79219370Spst
79319370Spst  subfiles = NULL;
79419370Spst  current_subfile = NULL;
79519370Spst  start_subfile (name, dirname);
79619370Spst}
79719370Spst
79846283Sdfr/* Finish the symbol definitions for one main source file, close off
79946283Sdfr   all the lexical contexts for that file (creating struct block's for
80046283Sdfr   them), then make the struct symtab for that file and put it in the
80146283Sdfr   list of all such.
80219370Spst
80346283Sdfr   END_ADDR is the address of the end of the file's text.  SECTION is
80446283Sdfr   the section number (in objfile->section_offsets) of the blockvector
80546283Sdfr   and linetable.
80619370Spst
80746283Sdfr   Note that it is possible for end_symtab() to return NULL.  In
80846283Sdfr   particular, for the DWARF case at least, it will return NULL when
80946283Sdfr   it finds a compilation unit that has exactly one DIE, a
81046283Sdfr   TAG_compile_unit DIE.  This can happen when we link in an object
81146283Sdfr   file that was compiled from an empty source file.  Returning NULL
81246283Sdfr   is probably not the correct thing to do, because then gdb will
81346283Sdfr   never know about this empty file (FIXME). */
81419370Spst
81519370Spststruct symtab *
81646283Sdfrend_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
81719370Spst{
818130803Smarcel  struct symtab *symtab = NULL;
819130803Smarcel  struct blockvector *blockvector;
820130803Smarcel  struct subfile *subfile;
821130803Smarcel  struct context_stack *cstk;
82219370Spst  struct subfile *nextsub;
82319370Spst
82446283Sdfr  /* Finish the lexical context of the last function in the file; pop
82546283Sdfr     the context stack.  */
82619370Spst
82719370Spst  if (context_stack_depth > 0)
82819370Spst    {
82946283Sdfr      cstk = pop_context ();
83019370Spst      /* Make a block for the local symbols within.  */
83119370Spst      finish_block (cstk->name, &local_symbols, cstk->old_blocks,
83219370Spst		    cstk->start_addr, end_addr, objfile);
83319370Spst
83419370Spst      if (context_stack_depth > 0)
83519370Spst	{
83646283Sdfr	  /* This is said to happen with SCO.  The old coffread.c
83746283Sdfr	     code simply emptied the context stack, so we do the
83846283Sdfr	     same.  FIXME: Find out why it is happening.  This is not
83946283Sdfr	     believed to happen in most cases (even for coffread.c);
84046283Sdfr	     it used to be an abort().  */
841130803Smarcel	  complaint (&symfile_complaints,
842130803Smarcel	             "Context stack not empty in end_symtab");
84319370Spst	  context_stack_depth = 0;
84419370Spst	}
84519370Spst    }
84619370Spst
84719370Spst  /* Reordered executables may have out of order pending blocks; if
84819370Spst     OBJF_REORDERED is true, then sort the pending blocks.  */
84919370Spst  if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
85019370Spst    {
85146283Sdfr      /* FIXME!  Remove this horrid bubble sort and use merge sort!!! */
85219370Spst      int swapped;
85319370Spst      do
85419370Spst	{
85519370Spst	  struct pending_block *pb, *pbnext;
85646283Sdfr
85719370Spst	  pb = pending_blocks;
85819370Spst	  pbnext = pb->next;
85919370Spst	  swapped = 0;
86019370Spst
86119370Spst	  while (pbnext)
86219370Spst	    {
86319370Spst	      /* swap blocks if unordered! */
86446283Sdfr
86546283Sdfr	      if (BLOCK_START (pb->block) < BLOCK_START (pbnext->block))
86619370Spst		{
86719370Spst		  struct block *tmp = pb->block;
86819370Spst		  pb->block = pbnext->block;
86919370Spst		  pbnext->block = tmp;
87019370Spst		  swapped = 1;
87119370Spst		}
87219370Spst	      pb = pbnext;
87319370Spst	      pbnext = pbnext->next;
87419370Spst	    }
87546283Sdfr	}
87646283Sdfr      while (swapped);
87719370Spst    }
87819370Spst
87919370Spst  /* Cleanup any undefined types that have been left hanging around
88019370Spst     (this needs to be done before the finish_blocks so that
88119370Spst     file_symbols is still good).
88298944Sobrien
88319370Spst     Both cleanup_undefined_types and finish_global_stabs are stabs
88419370Spst     specific, but harmless for other symbol readers, since on gdb
88519370Spst     startup or when finished reading stabs, the state is set so these
88619370Spst     are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
88719370Spst     we make this cleaner?  */
88819370Spst
88919370Spst  cleanup_undefined_types ();
89019370Spst  finish_global_stabs (objfile);
89119370Spst
89219370Spst  if (pending_blocks == NULL
89319370Spst      && file_symbols == NULL
89446283Sdfr      && global_symbols == NULL
895130803Smarcel      && have_line_numbers == 0
896130803Smarcel      && pending_macros == NULL)
89719370Spst    {
89846283Sdfr      /* Ignore symtabs that have no functions with real debugging
89946283Sdfr         info.  */
90019370Spst      blockvector = NULL;
90119370Spst    }
90219370Spst  else
90319370Spst    {
90446283Sdfr      /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
90546283Sdfr         blockvector.  */
90619370Spst      finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
90719370Spst		    objfile);
90819370Spst      finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
90919370Spst		    objfile);
91019370Spst      blockvector = make_blockvector (objfile);
911130803Smarcel      cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK),
912130803Smarcel			     &objfile->objfile_obstack);
91319370Spst    }
91419370Spst
91546283Sdfr#ifndef PROCESS_LINENUMBER_HOOK
91646283Sdfr#define PROCESS_LINENUMBER_HOOK()
91719370Spst#endif
91846283Sdfr  PROCESS_LINENUMBER_HOOK ();	/* Needed for xcoff. */
91919370Spst
92019370Spst  /* Now create the symtab objects proper, one for each subfile.  */
92119370Spst  /* (The main file is the last one on the chain.)  */
92219370Spst
92319370Spst  for (subfile = subfiles; subfile; subfile = nextsub)
92419370Spst    {
92519370Spst      int linetablesize = 0;
92619370Spst      symtab = NULL;
92746283Sdfr
92846283Sdfr      /* If we have blocks of symbols, make a symtab. Otherwise, just
92946283Sdfr         ignore this file and any line number info in it.  */
93019370Spst      if (blockvector)
93119370Spst	{
93219370Spst	  if (subfile->line_vector)
93319370Spst	    {
93419370Spst	      linetablesize = sizeof (struct linetable) +
93546283Sdfr	        subfile->line_vector->nitems * sizeof (struct linetable_entry);
93619370Spst#if 0
93746283Sdfr	      /* I think this is artifact from before it went on the
93846283Sdfr	         obstack. I doubt we'll need the memory between now
93946283Sdfr	         and when we free it later in this function.  */
94019370Spst	      /* First, shrink the linetable to make more memory.  */
94119370Spst	      subfile->line_vector = (struct linetable *)
94219370Spst		xrealloc ((char *) subfile->line_vector, linetablesize);
94319370Spst#endif
94419370Spst
94546283Sdfr	      /* Like the pending blocks, the line table may be
94646283Sdfr	         scrambled in reordered executables.  Sort it if
94746283Sdfr	         OBJF_REORDERED is true.  */
94819370Spst	      if (objfile->flags & OBJF_REORDERED)
94919370Spst		qsort (subfile->line_vector->item,
95019370Spst		       subfile->line_vector->nitems,
95198944Sobrien		     sizeof (struct linetable_entry), compare_line_numbers);
95219370Spst	    }
95319370Spst
95419370Spst	  /* Now, allocate a symbol table.  */
95519370Spst	  symtab = allocate_symtab (subfile->name, objfile);
95619370Spst
95719370Spst	  /* Fill in its components.  */
95819370Spst	  symtab->blockvector = blockvector;
959130803Smarcel          symtab->macro_table = pending_macros;
96019370Spst	  if (subfile->line_vector)
96119370Spst	    {
96219370Spst	      /* Reallocate the line table on the symbol obstack */
96346283Sdfr	      symtab->linetable = (struct linetable *)
964130803Smarcel		obstack_alloc (&objfile->objfile_obstack, linetablesize);
96519370Spst	      memcpy (symtab->linetable, subfile->line_vector, linetablesize);
96619370Spst	    }
96719370Spst	  else
96819370Spst	    {
96919370Spst	      symtab->linetable = NULL;
97019370Spst	    }
97119370Spst	  symtab->block_line_section = section;
97219370Spst	  if (subfile->dirname)
97319370Spst	    {
97419370Spst	      /* Reallocate the dirname on the symbol obstack */
97519370Spst	      symtab->dirname = (char *)
976130803Smarcel		obstack_alloc (&objfile->objfile_obstack,
97746283Sdfr			       strlen (subfile->dirname) + 1);
97819370Spst	      strcpy (symtab->dirname, subfile->dirname);
97919370Spst	    }
98019370Spst	  else
98119370Spst	    {
98219370Spst	      symtab->dirname = NULL;
98319370Spst	    }
98419370Spst	  symtab->free_code = free_linetable;
985130803Smarcel	  symtab->free_func = NULL;
98619370Spst
98746283Sdfr	  /* Use whatever language we have been using for this
98846283Sdfr	     subfile, not the one that was deduced in allocate_symtab
98946283Sdfr	     from the filename.  We already did our own deducing when
99046283Sdfr	     we created the subfile, and we may have altered our
99146283Sdfr	     opinion of what language it is from things we found in
99246283Sdfr	     the symbols. */
99319370Spst	  symtab->language = subfile->language;
99419370Spst
99546283Sdfr	  /* Save the debug format string (if any) in the symtab */
99646283Sdfr	  if (subfile->debugformat != NULL)
99746283Sdfr	    {
99846283Sdfr	      symtab->debugformat = obsavestring (subfile->debugformat,
99998944Sobrien					      strlen (subfile->debugformat),
1000130803Smarcel						  &objfile->objfile_obstack);
100146283Sdfr	    }
100246283Sdfr
100319370Spst	  /* All symtabs for the main file and the subfiles share a
100446283Sdfr	     blockvector, so we need to clear primary for everything
100546283Sdfr	     but the main file.  */
100619370Spst
100719370Spst	  symtab->primary = 0;
100819370Spst	}
100919370Spst      if (subfile->name != NULL)
101019370Spst	{
101198944Sobrien	  xfree ((void *) subfile->name);
101219370Spst	}
101319370Spst      if (subfile->dirname != NULL)
101419370Spst	{
101598944Sobrien	  xfree ((void *) subfile->dirname);
101619370Spst	}
101719370Spst      if (subfile->line_vector != NULL)
101819370Spst	{
101998944Sobrien	  xfree ((void *) subfile->line_vector);
102019370Spst	}
102146283Sdfr      if (subfile->debugformat != NULL)
102246283Sdfr	{
102398944Sobrien	  xfree ((void *) subfile->debugformat);
102446283Sdfr	}
102519370Spst
102619370Spst      nextsub = subfile->next;
102798944Sobrien      xfree ((void *) subfile);
102819370Spst    }
102919370Spst
103019370Spst  /* Set this for the main source file.  */
103119370Spst  if (symtab)
103219370Spst    {
103319370Spst      symtab->primary = 1;
103419370Spst    }
103519370Spst
103619370Spst  last_source_file = NULL;
103719370Spst  current_subfile = NULL;
1038130803Smarcel  pending_macros = NULL;
103919370Spst
104046283Sdfr  return symtab;
104119370Spst}
104219370Spst
104346283Sdfr/* Push a context block.  Args are an identifying nesting level
104446283Sdfr   (checkable when you pop it), and the starting PC address of this
104546283Sdfr   context.  */
104619370Spst
104719370Spststruct context_stack *
104846283Sdfrpush_context (int desc, CORE_ADDR valu)
104919370Spst{
1050130803Smarcel  struct context_stack *new;
105119370Spst
105219370Spst  if (context_stack_depth == context_stack_size)
105319370Spst    {
105419370Spst      context_stack_size *= 2;
105519370Spst      context_stack = (struct context_stack *)
105619370Spst	xrealloc ((char *) context_stack,
105798944Sobrien		  (context_stack_size * sizeof (struct context_stack)));
105819370Spst    }
105919370Spst
106019370Spst  new = &context_stack[context_stack_depth++];
106119370Spst  new->depth = desc;
106219370Spst  new->locals = local_symbols;
106346283Sdfr  new->params = param_symbols;
106419370Spst  new->old_blocks = pending_blocks;
106519370Spst  new->start_addr = valu;
106619370Spst  new->name = NULL;
106719370Spst
106819370Spst  local_symbols = NULL;
106946283Sdfr  param_symbols = NULL;
107019370Spst
107146283Sdfr  return new;
107219370Spst}
1073130803Smarcel
1074130803Smarcel/* Pop a context block.  Returns the address of the context block just
1075130803Smarcel   popped. */
1076130803Smarcel
1077130803Smarcelstruct context_stack *
1078130803Smarcelpop_context (void)
1079130803Smarcel{
1080130803Smarcel  gdb_assert (context_stack_depth > 0);
1081130803Smarcel  return (&context_stack[--context_stack_depth]);
1082130803Smarcel}
1083130803Smarcel
108419370Spst
108598944Sobrien
108619370Spst/* Compute a small integer hash code for the given name. */
108719370Spst
108819370Spstint
108946283Sdfrhashname (char *name)
109019370Spst{
109198944Sobrien    return (hash(name,strlen(name)) % HASHSIZE);
109219370Spst}
109346283Sdfr
109419370Spst
109546283Sdfrvoid
109646283Sdfrrecord_debugformat (char *format)
109746283Sdfr{
109846283Sdfr  current_subfile->debugformat = savestring (format, strlen (format));
109946283Sdfr}
110046283Sdfr
110146283Sdfr/* Merge the first symbol list SRCLIST into the second symbol list
110246283Sdfr   TARGETLIST by repeated calls to add_symbol_to_list().  This
110346283Sdfr   procedure "frees" each link of SRCLIST by adding it to the
110446283Sdfr   free_pendings list.  Caller must set SRCLIST to a null list after
110546283Sdfr   calling this function.
110646283Sdfr
110746283Sdfr   Void return. */
110846283Sdfr
110946283Sdfrvoid
111046283Sdfrmerge_symbol_lists (struct pending **srclist, struct pending **targetlist)
111146283Sdfr{
1112130803Smarcel  int i;
111346283Sdfr
111446283Sdfr  if (!srclist || !*srclist)
111546283Sdfr    return;
111646283Sdfr
111746283Sdfr  /* Merge in elements from current link.  */
111846283Sdfr  for (i = 0; i < (*srclist)->nsyms; i++)
111946283Sdfr    add_symbol_to_list ((*srclist)->symbol[i], targetlist);
112046283Sdfr
112146283Sdfr  /* Recurse on next.  */
112246283Sdfr  merge_symbol_lists (&(*srclist)->next, targetlist);
112346283Sdfr
112446283Sdfr  /* "Free" the current link.  */
112546283Sdfr  (*srclist)->next = free_pendings;
112646283Sdfr  free_pendings = (*srclist);
112746283Sdfr}
112819370Spst
112946283Sdfr/* Initialize anything that needs initializing when starting to read a
113046283Sdfr   fresh piece of a symbol file, e.g. reading in the stuff
113146283Sdfr   corresponding to a psymtab.  */
113219370Spst
113319370Spstvoid
113498944Sobrienbuildsym_init (void)
113519370Spst{
113619370Spst  free_pendings = NULL;
113719370Spst  file_symbols = NULL;
113819370Spst  global_symbols = NULL;
113919370Spst  pending_blocks = NULL;
1140130803Smarcel  pending_macros = NULL;
114119370Spst}
114219370Spst
114319370Spst/* Initialize anything that needs initializing when a completely new
114419370Spst   symbol file is specified (not just adding some symbols from another
114519370Spst   file, e.g. a shared library).  */
114619370Spst
114719370Spstvoid
114898944Sobrienbuildsym_new_init (void)
114919370Spst{
115019370Spst  buildsym_init ();
115119370Spst}
1152