constify some blockvector APIs
[external/binutils.git] / gdb / buildsym.c
1 /* Support routines for building symbol tables in GDB's internal format.
2    Copyright (C) 1986-2014 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* This module provides subroutines used for creating and adding to
20    the symbol table.  These routines are called from various symbol-
21    file-reading routines.
22
23    Routines to support specific debugging information formats (stabs,
24    DWARF, etc) belong somewhere else.  */
25
26 #include "defs.h"
27 #include "bfd.h"
28 #include "gdb_obstack.h"
29 #include "symtab.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "gdbtypes.h"
33 #include "gdb_assert.h"
34 #include "complaints.h"
35 #include <string.h>
36 #include "expression.h"         /* For "enum exp_opcode" used by...  */
37 #include "bcache.h"
38 #include "filenames.h"          /* For DOSish file names.  */
39 #include "macrotab.h"
40 #include "demangle.h"           /* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
41 #include "block.h"
42 #include "cp-support.h"
43 #include "dictionary.h"
44 #include "addrmap.h"
45
46 /* Ask buildsym.h to define the vars it normally declares `extern'.  */
47 #define EXTERN
48 /**/
49 #include "buildsym.h"           /* Our own declarations.  */
50 #undef  EXTERN
51
52 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
53    questionable--see comment where we call them).  */
54
55 #include "stabsread.h"
56
57 /* List of subfiles.  */
58
59 static struct subfile *subfiles;
60
61 /* List of free `struct pending' structures for reuse.  */
62
63 static struct pending *free_pendings;
64
65 /* Non-zero if symtab has line number info.  This prevents an
66    otherwise empty symtab from being tossed.  */
67
68 static int have_line_numbers;
69
70 /* The mutable address map for the compilation unit whose symbols
71    we're currently reading.  The symtabs' shared blockvector will
72    point to a fixed copy of this.  */
73 static struct addrmap *pending_addrmap;
74
75 /* The obstack on which we allocate pending_addrmap.
76    If pending_addrmap is NULL, this is uninitialized; otherwise, it is
77    initialized (and holds pending_addrmap).  */
78 static struct obstack pending_addrmap_obstack;
79
80 /* Non-zero if we recorded any ranges in the addrmap that are
81    different from those in the blockvector already.  We set this to
82    zero when we start processing a symfile, and if it's still zero at
83    the end, then we just toss the addrmap.  */
84 static int pending_addrmap_interesting;
85
86 /* An obstack used for allocating pending blocks.  */
87
88 static struct obstack pending_block_obstack;
89
90 /* List of blocks already made (lexical contexts already closed).
91    This is used at the end to make the blockvector.  */
92
93 struct pending_block
94   {
95     struct pending_block *next;
96     struct block *block;
97   };
98
99 /* Pointer to the head of a linked list of symbol blocks which have
100    already been finalized (lexical contexts already closed) and which
101    are just waiting to be built into a blockvector when finalizing the
102    associated symtab.  */
103
104 static struct pending_block *pending_blocks;
105
106 struct subfile_stack
107   {
108     struct subfile_stack *next;
109     char *name;
110   };
111
112 static struct subfile_stack *subfile_stack;
113
114 /* The macro table for the compilation unit whose symbols we're
115    currently reading.  All the symtabs for the CU will point to this.  */
116 static struct macro_table *pending_macros;
117
118 static int compare_line_numbers (const void *ln1p, const void *ln2p);
119
120 static void record_pending_block (struct objfile *objfile,
121                                   struct block *block,
122                                   struct pending_block *opblock);
123
124 /* Initial sizes of data structures.  These are realloc'd larger if
125    needed, and realloc'd down to the size actually used, when
126    completed.  */
127
128 #define INITIAL_CONTEXT_STACK_SIZE      10
129 #define INITIAL_LINE_VECTOR_LENGTH      1000
130 \f
131
132 /* Maintain the lists of symbols and blocks.  */
133
134 /* Add a symbol to one of the lists of symbols.  */
135
136 void
137 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
138 {
139   struct pending *link;
140
141   /* If this is an alias for another symbol, don't add it.  */
142   if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
143     return;
144
145   /* We keep PENDINGSIZE symbols in each link of the list.  If we
146      don't have a link with room in it, add a new link.  */
147   if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
148     {
149       if (free_pendings)
150         {
151           link = free_pendings;
152           free_pendings = link->next;
153         }
154       else
155         {
156           link = (struct pending *) xmalloc (sizeof (struct pending));
157         }
158
159       link->next = *listhead;
160       *listhead = link;
161       link->nsyms = 0;
162     }
163
164   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
165 }
166
167 /* Find a symbol named NAME on a LIST.  NAME need not be
168    '\0'-terminated; LENGTH is the length of the name.  */
169
170 struct symbol *
171 find_symbol_in_list (struct pending *list, char *name, int length)
172 {
173   int j;
174   const char *pp;
175
176   while (list != NULL)
177     {
178       for (j = list->nsyms; --j >= 0;)
179         {
180           pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
181           if (*pp == *name && strncmp (pp, name, length) == 0
182               && pp[length] == '\0')
183             {
184               return (list->symbol[j]);
185             }
186         }
187       list = list->next;
188     }
189   return (NULL);
190 }
191
192 /* At end of reading syms, or in case of quit, really free as many
193    `struct pending's as we can easily find.  */
194
195 void
196 really_free_pendings (void *dummy)
197 {
198   struct pending *next, *next1;
199
200   for (next = free_pendings; next; next = next1)
201     {
202       next1 = next->next;
203       xfree ((void *) next);
204     }
205   free_pendings = NULL;
206
207   free_pending_blocks ();
208
209   for (next = file_symbols; next != NULL; next = next1)
210     {
211       next1 = next->next;
212       xfree ((void *) next);
213     }
214   file_symbols = NULL;
215
216   for (next = global_symbols; next != NULL; next = next1)
217     {
218       next1 = next->next;
219       xfree ((void *) next);
220     }
221   global_symbols = NULL;
222
223   if (pending_macros)
224     free_macro_table (pending_macros);
225
226   if (pending_addrmap)
227     {
228       obstack_free (&pending_addrmap_obstack, NULL);
229       pending_addrmap = NULL;
230     }
231 }
232
233 /* This function is called to discard any pending blocks.  */
234
235 void
236 free_pending_blocks (void)
237 {
238   if (pending_blocks != NULL)
239     {
240       obstack_free (&pending_block_obstack, NULL);
241       pending_blocks = NULL;
242     }
243 }
244
245 /* Take one of the lists of symbols and make a block from it.  Keep
246    the order the symbols have in the list (reversed from the input
247    file).  Put the block on the list of pending blocks.  */
248
249 static struct block *
250 finish_block_internal (struct symbol *symbol, struct pending **listhead,
251                        struct pending_block *old_blocks,
252                        CORE_ADDR start, CORE_ADDR end,
253                        struct objfile *objfile,
254                        int is_global, int expandable)
255 {
256   struct gdbarch *gdbarch = get_objfile_arch (objfile);
257   struct pending *next, *next1;
258   struct block *block;
259   struct pending_block *pblock;
260   struct pending_block *opblock;
261
262   block = (is_global
263            ? allocate_global_block (&objfile->objfile_obstack)
264            : allocate_block (&objfile->objfile_obstack));
265
266   if (symbol)
267     {
268       BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
269                                                *listhead);
270     }
271   else
272     {
273       if (expandable)
274         {
275           BLOCK_DICT (block) = dict_create_hashed_expandable ();
276           dict_add_pending (BLOCK_DICT (block), *listhead);
277         }
278       else
279         {
280           BLOCK_DICT (block) =
281             dict_create_hashed (&objfile->objfile_obstack, *listhead);
282         }
283     }
284
285   BLOCK_START (block) = start;
286   BLOCK_END (block) = end;
287
288   /* Put the block in as the value of the symbol that names it.  */
289
290   if (symbol)
291     {
292       struct type *ftype = SYMBOL_TYPE (symbol);
293       struct dict_iterator iter;
294       SYMBOL_BLOCK_VALUE (symbol) = block;
295       BLOCK_FUNCTION (block) = symbol;
296
297       if (TYPE_NFIELDS (ftype) <= 0)
298         {
299           /* No parameter type information is recorded with the
300              function's type.  Set that from the type of the
301              parameter symbols.  */
302           int nparams = 0, iparams;
303           struct symbol *sym;
304
305           /* Here we want to directly access the dictionary, because
306              we haven't fully initialized the block yet.  */
307           ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
308             {
309               if (SYMBOL_IS_ARGUMENT (sym))
310                 nparams++;
311             }
312           if (nparams > 0)
313             {
314               TYPE_NFIELDS (ftype) = nparams;
315               TYPE_FIELDS (ftype) = (struct field *)
316                 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
317
318               iparams = 0;
319               /* Here we want to directly access the dictionary, because
320                  we haven't fully initialized the block yet.  */
321               ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
322                 {
323                   if (iparams == nparams)
324                     break;
325
326                   if (SYMBOL_IS_ARGUMENT (sym))
327                     {
328                       TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
329                       TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
330                       iparams++;
331                     }
332                 }
333             }
334         }
335     }
336   else
337     {
338       BLOCK_FUNCTION (block) = NULL;
339     }
340
341   /* Now "free" the links of the list, and empty the list.  */
342
343   for (next = *listhead; next; next = next1)
344     {
345       next1 = next->next;
346       next->next = free_pendings;
347       free_pendings = next;
348     }
349   *listhead = NULL;
350
351   /* Check to be sure that the blocks have an end address that is
352      greater than starting address.  */
353
354   if (BLOCK_END (block) < BLOCK_START (block))
355     {
356       if (symbol)
357         {
358           complaint (&symfile_complaints,
359                      _("block end address less than block "
360                        "start address in %s (patched it)"),
361                      SYMBOL_PRINT_NAME (symbol));
362         }
363       else
364         {
365           complaint (&symfile_complaints,
366                      _("block end address %s less than block "
367                        "start address %s (patched it)"),
368                      paddress (gdbarch, BLOCK_END (block)),
369                      paddress (gdbarch, BLOCK_START (block)));
370         }
371       /* Better than nothing.  */
372       BLOCK_END (block) = BLOCK_START (block);
373     }
374
375   /* Install this block as the superblock of all blocks made since the
376      start of this scope that don't have superblocks yet.  */
377
378   opblock = NULL;
379   for (pblock = pending_blocks; 
380        pblock && pblock != old_blocks; 
381        pblock = pblock->next)
382     {
383       if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
384         {
385           /* Check to be sure the blocks are nested as we receive
386              them.  If the compiler/assembler/linker work, this just
387              burns a small amount of time.
388
389              Skip blocks which correspond to a function; they're not
390              physically nested inside this other blocks, only
391              lexically nested.  */
392           if (BLOCK_FUNCTION (pblock->block) == NULL
393               && (BLOCK_START (pblock->block) < BLOCK_START (block)
394                   || BLOCK_END (pblock->block) > BLOCK_END (block)))
395             {
396               if (symbol)
397                 {
398                   complaint (&symfile_complaints,
399                              _("inner block not inside outer block in %s"),
400                              SYMBOL_PRINT_NAME (symbol));
401                 }
402               else
403                 {
404                   complaint (&symfile_complaints,
405                              _("inner block (%s-%s) not "
406                                "inside outer block (%s-%s)"),
407                              paddress (gdbarch, BLOCK_START (pblock->block)),
408                              paddress (gdbarch, BLOCK_END (pblock->block)),
409                              paddress (gdbarch, BLOCK_START (block)),
410                              paddress (gdbarch, BLOCK_END (block)));
411                 }
412               if (BLOCK_START (pblock->block) < BLOCK_START (block))
413                 BLOCK_START (pblock->block) = BLOCK_START (block);
414               if (BLOCK_END (pblock->block) > BLOCK_END (block))
415                 BLOCK_END (pblock->block) = BLOCK_END (block);
416             }
417           BLOCK_SUPERBLOCK (pblock->block) = block;
418         }
419       opblock = pblock;
420     }
421
422   block_set_using (block, using_directives, &objfile->objfile_obstack);
423   using_directives = NULL;
424
425   record_pending_block (objfile, block, opblock);
426
427   return block;
428 }
429
430 struct block *
431 finish_block (struct symbol *symbol, struct pending **listhead,
432               struct pending_block *old_blocks,
433               CORE_ADDR start, CORE_ADDR end,
434               struct objfile *objfile)
435 {
436   return finish_block_internal (symbol, listhead, old_blocks,
437                                 start, end, objfile, 0, 0);
438 }
439
440 /* Record BLOCK on the list of all blocks in the file.  Put it after
441    OPBLOCK, or at the beginning if opblock is NULL.  This puts the
442    block in the list after all its subblocks.
443
444    Allocate the pending block struct in the objfile_obstack to save
445    time.  This wastes a little space.  FIXME: Is it worth it?  */
446
447 static void
448 record_pending_block (struct objfile *objfile, struct block *block,
449                       struct pending_block *opblock)
450 {
451   struct pending_block *pblock;
452
453   if (pending_blocks == NULL)
454     obstack_init (&pending_block_obstack);
455
456   pblock = (struct pending_block *)
457     obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
458   pblock->block = block;
459   if (opblock)
460     {
461       pblock->next = opblock->next;
462       opblock->next = pblock;
463     }
464   else
465     {
466       pblock->next = pending_blocks;
467       pending_blocks = pblock;
468     }
469 }
470
471
472 /* Record that the range of addresses from START to END_INCLUSIVE
473    (inclusive, like it says) belongs to BLOCK.  BLOCK's start and end
474    addresses must be set already.  You must apply this function to all
475    BLOCK's children before applying it to BLOCK.
476
477    If a call to this function complicates the picture beyond that
478    already provided by BLOCK_START and BLOCK_END, then we create an
479    address map for the block.  */
480 void
481 record_block_range (struct block *block,
482                     CORE_ADDR start, CORE_ADDR end_inclusive)
483 {
484   /* If this is any different from the range recorded in the block's
485      own BLOCK_START and BLOCK_END, then note that the address map has
486      become interesting.  Note that even if this block doesn't have
487      any "interesting" ranges, some later block might, so we still
488      need to record this block in the addrmap.  */
489   if (start != BLOCK_START (block)
490       || end_inclusive + 1 != BLOCK_END (block))
491     pending_addrmap_interesting = 1;
492
493   if (! pending_addrmap)
494     {
495       obstack_init (&pending_addrmap_obstack);
496       pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
497     }
498
499   addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
500 }
501
502
503 static struct blockvector *
504 make_blockvector (struct objfile *objfile)
505 {
506   struct pending_block *next;
507   struct blockvector *blockvector;
508   int i;
509
510   /* Count the length of the list of blocks.  */
511
512   for (next = pending_blocks, i = 0; next; next = next->next, i++)
513     {;
514     }
515
516   blockvector = (struct blockvector *)
517     obstack_alloc (&objfile->objfile_obstack,
518                    (sizeof (struct blockvector)
519                     + (i - 1) * sizeof (struct block *)));
520
521   /* Copy the blocks into the blockvector.  This is done in reverse
522      order, which happens to put the blocks into the proper order
523      (ascending starting address).  finish_block has hair to insert
524      each block into the list after its subblocks in order to make
525      sure this is true.  */
526
527   BLOCKVECTOR_NBLOCKS (blockvector) = i;
528   for (next = pending_blocks; next; next = next->next)
529     {
530       BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
531     }
532
533   free_pending_blocks ();
534
535   /* If we needed an address map for this symtab, record it in the
536      blockvector.  */
537   if (pending_addrmap && pending_addrmap_interesting)
538     BLOCKVECTOR_MAP (blockvector)
539       = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
540   else
541     BLOCKVECTOR_MAP (blockvector) = 0;
542
543   /* Some compilers output blocks in the wrong order, but we depend on
544      their being in the right order so we can binary search.  Check the
545      order and moan about it.
546      Note: Remember that the first two blocks are the global and static
547      blocks.  We could special case that fact and begin checking at block 2.
548      To avoid making that assumption we do not.  */
549   if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
550     {
551       for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
552         {
553           if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
554               > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
555             {
556               CORE_ADDR start
557                 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
558
559               complaint (&symfile_complaints, _("block at %s out of order"),
560                          hex_string ((LONGEST) start));
561             }
562         }
563     }
564
565   return (blockvector);
566 }
567 \f
568 /* Start recording information about source code that came from an
569    included (or otherwise merged-in) source file with a different
570    name.  NAME is the name of the file (cannot be NULL), DIRNAME is
571    the directory in which the file was compiled (or NULL if not
572    known).  */
573
574 void
575 start_subfile (const char *name, const char *dirname)
576 {
577   struct subfile *subfile;
578
579   /* See if this subfile is already known as a subfile of the current
580      main source file.  */
581
582   for (subfile = subfiles; subfile; subfile = subfile->next)
583     {
584       char *subfile_name;
585
586       /* If NAME is an absolute path, and this subfile is not, then
587          attempt to create an absolute path to compare.  */
588       if (IS_ABSOLUTE_PATH (name)
589           && !IS_ABSOLUTE_PATH (subfile->name)
590           && subfile->dirname != NULL)
591         subfile_name = concat (subfile->dirname, SLASH_STRING,
592                                subfile->name, (char *) NULL);
593       else
594         subfile_name = subfile->name;
595
596       if (FILENAME_CMP (subfile_name, name) == 0)
597         {
598           current_subfile = subfile;
599           if (subfile_name != subfile->name)
600             xfree (subfile_name);
601           return;
602         }
603       if (subfile_name != subfile->name)
604         xfree (subfile_name);
605     }
606
607   /* This subfile is not known.  Add an entry for it.  Make an entry
608      for this subfile in the list of all subfiles of the current main
609      source file.  */
610
611   subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
612   memset ((char *) subfile, 0, sizeof (struct subfile));
613   subfile->next = subfiles;
614   subfiles = subfile;
615   current_subfile = subfile;
616
617   /* Save its name and compilation directory name.  */
618   subfile->name = xstrdup (name);
619   subfile->dirname = (dirname == NULL) ? NULL : xstrdup (dirname);
620
621   /* Initialize line-number recording for this subfile.  */
622   subfile->line_vector = NULL;
623
624   /* Default the source language to whatever can be deduced from the
625      filename.  If nothing can be deduced (such as for a C/C++ include
626      file with a ".h" extension), then inherit whatever language the
627      previous subfile had.  This kludgery is necessary because there
628      is no standard way in some object formats to record the source
629      language.  Also, when symtabs are allocated we try to deduce a
630      language then as well, but it is too late for us to use that
631      information while reading symbols, since symtabs aren't allocated
632      until after all the symbols have been processed for a given
633      source file.  */
634
635   subfile->language = deduce_language_from_filename (subfile->name);
636   if (subfile->language == language_unknown
637       && subfile->next != NULL)
638     {
639       subfile->language = subfile->next->language;
640     }
641
642   /* Initialize the debug format string to NULL.  We may supply it
643      later via a call to record_debugformat.  */
644   subfile->debugformat = NULL;
645
646   /* Similarly for the producer.  */
647   subfile->producer = NULL;
648
649   /* If the filename of this subfile ends in .C, then change the
650      language of any pending subfiles from C to C++.  We also accept
651      any other C++ suffixes accepted by deduce_language_from_filename.  */
652   /* Likewise for f2c.  */
653
654   if (subfile->name)
655     {
656       struct subfile *s;
657       enum language sublang = deduce_language_from_filename (subfile->name);
658
659       if (sublang == language_cplus || sublang == language_fortran)
660         for (s = subfiles; s != NULL; s = s->next)
661           if (s->language == language_c)
662             s->language = sublang;
663     }
664
665   /* And patch up this file if necessary.  */
666   if (subfile->language == language_c
667       && subfile->next != NULL
668       && (subfile->next->language == language_cplus
669           || subfile->next->language == language_fortran))
670     {
671       subfile->language = subfile->next->language;
672     }
673 }
674
675 /* For stabs readers, the first N_SO symbol is assumed to be the
676    source file name, and the subfile struct is initialized using that
677    assumption.  If another N_SO symbol is later seen, immediately
678    following the first one, then the first one is assumed to be the
679    directory name and the second one is really the source file name.
680
681    So we have to patch up the subfile struct by moving the old name
682    value to dirname and remembering the new name.  Some sanity
683    checking is performed to ensure that the state of the subfile
684    struct is reasonable and that the old name we are assuming to be a
685    directory name actually is (by checking for a trailing '/').  */
686
687 void
688 patch_subfile_names (struct subfile *subfile, char *name)
689 {
690   if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
691       && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
692     {
693       subfile->dirname = subfile->name;
694       subfile->name = xstrdup (name);
695       set_last_source_file (name);
696
697       /* Default the source language to whatever can be deduced from
698          the filename.  If nothing can be deduced (such as for a C/C++
699          include file with a ".h" extension), then inherit whatever
700          language the previous subfile had.  This kludgery is
701          necessary because there is no standard way in some object
702          formats to record the source language.  Also, when symtabs
703          are allocated we try to deduce a language then as well, but
704          it is too late for us to use that information while reading
705          symbols, since symtabs aren't allocated until after all the
706          symbols have been processed for a given source file.  */
707
708       subfile->language = deduce_language_from_filename (subfile->name);
709       if (subfile->language == language_unknown
710           && subfile->next != NULL)
711         {
712           subfile->language = subfile->next->language;
713         }
714     }
715 }
716 \f
717 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
718    switching source files (different subfiles, as we call them) within
719    one object file, but using a stack rather than in an arbitrary
720    order.  */
721
722 void
723 push_subfile (void)
724 {
725   struct subfile_stack *tem
726     = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
727
728   tem->next = subfile_stack;
729   subfile_stack = tem;
730   if (current_subfile == NULL || current_subfile->name == NULL)
731     {
732       internal_error (__FILE__, __LINE__, 
733                       _("failed internal consistency check"));
734     }
735   tem->name = current_subfile->name;
736 }
737
738 char *
739 pop_subfile (void)
740 {
741   char *name;
742   struct subfile_stack *link = subfile_stack;
743
744   if (link == NULL)
745     {
746       internal_error (__FILE__, __LINE__,
747                       _("failed internal consistency check"));
748     }
749   name = link->name;
750   subfile_stack = link->next;
751   xfree ((void *) link);
752   return (name);
753 }
754 \f
755 /* Add a linetable entry for line number LINE and address PC to the
756    line vector for SUBFILE.  */
757
758 void
759 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
760 {
761   struct linetable_entry *e;
762
763   /* Ignore the dummy line number in libg.o */
764   if (line == 0xffff)
765     {
766       return;
767     }
768
769   /* Make sure line vector exists and is big enough.  */
770   if (!subfile->line_vector)
771     {
772       subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
773       subfile->line_vector = (struct linetable *)
774         xmalloc (sizeof (struct linetable)
775            + subfile->line_vector_length * sizeof (struct linetable_entry));
776       subfile->line_vector->nitems = 0;
777       have_line_numbers = 1;
778     }
779
780   if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
781     {
782       subfile->line_vector_length *= 2;
783       subfile->line_vector = (struct linetable *)
784         xrealloc ((char *) subfile->line_vector,
785                   (sizeof (struct linetable)
786                    + (subfile->line_vector_length
787                       * sizeof (struct linetable_entry))));
788     }
789
790   /* Normally, we treat lines as unsorted.  But the end of sequence
791      marker is special.  We sort line markers at the same PC by line
792      number, so end of sequence markers (which have line == 0) appear
793      first.  This is right if the marker ends the previous function,
794      and there is no padding before the next function.  But it is
795      wrong if the previous line was empty and we are now marking a
796      switch to a different subfile.  We must leave the end of sequence
797      marker at the end of this group of lines, not sort the empty line
798      to after the marker.  The easiest way to accomplish this is to
799      delete any empty lines from our table, if they are followed by
800      end of sequence markers.  All we lose is the ability to set
801      breakpoints at some lines which contain no instructions
802      anyway.  */
803   if (line == 0 && subfile->line_vector->nitems > 0)
804     {
805       e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
806       while (subfile->line_vector->nitems > 0 && e->pc == pc)
807         {
808           e--;
809           subfile->line_vector->nitems--;
810         }
811     }
812
813   e = subfile->line_vector->item + subfile->line_vector->nitems++;
814   e->line = line;
815   e->pc = pc;
816 }
817
818 /* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */
819
820 static int
821 compare_line_numbers (const void *ln1p, const void *ln2p)
822 {
823   struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
824   struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
825
826   /* Note: this code does not assume that CORE_ADDRs can fit in ints.
827      Please keep it that way.  */
828   if (ln1->pc < ln2->pc)
829     return -1;
830
831   if (ln1->pc > ln2->pc)
832     return 1;
833
834   /* If pc equal, sort by line.  I'm not sure whether this is optimum
835      behavior (see comment at struct linetable in symtab.h).  */
836   return ln1->line - ln2->line;
837 }
838 \f
839 /* Return the macro table.
840    Initialize it if this is the first use.  */
841
842 struct macro_table *
843 get_macro_table (struct objfile *objfile, const char *comp_dir)
844 {
845   if (! pending_macros)
846     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
847                                       objfile->per_bfd->macro_cache,
848                                       comp_dir);
849   return pending_macros;
850 }
851 \f
852 /* Start a new symtab for a new source file.  Called, for example,
853    when a stabs symbol of type N_SO is seen, or when a DWARF
854    TAG_compile_unit DIE is seen.  It indicates the start of data for
855    one original source file.
856
857    NAME is the name of the file (cannot be NULL).  DIRNAME is the directory in
858    which the file was compiled (or NULL if not known).  START_ADDR is the
859    lowest address of objects in the file (or 0 if not known).  */
860
861 void
862 start_symtab (const char *name, const char *dirname, CORE_ADDR start_addr)
863 {
864   restart_symtab (start_addr);
865   set_last_source_file (name);
866   start_subfile (name, dirname);
867 }
868
869 /* Restart compilation for a symtab.
870    This is used when a symtab is built from multiple sources.
871    The symtab is first built with start_symtab and then for each additional
872    piece call restart_symtab.  */
873
874 void
875 restart_symtab (CORE_ADDR start_addr)
876 {
877   set_last_source_file (NULL);
878   last_source_start_addr = start_addr;
879   file_symbols = NULL;
880   global_symbols = NULL;
881   within_function = 0;
882   have_line_numbers = 0;
883
884   /* Context stack is initially empty.  Allocate first one with room
885      for 10 levels; reuse it forever afterward.  */
886   if (context_stack == NULL)
887     {
888       context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
889       context_stack = (struct context_stack *)
890         xmalloc (context_stack_size * sizeof (struct context_stack));
891     }
892   context_stack_depth = 0;
893
894   /* We shouldn't have any address map at this point.  */
895   gdb_assert (! pending_addrmap);
896
897   /* Initialize the list of sub source files with one entry for this
898      file (the top-level source file).  */
899   subfiles = NULL;
900   current_subfile = NULL;
901 }
902
903 /* Subroutine of end_symtab to simplify it.  Look for a subfile that
904    matches the main source file's basename.  If there is only one, and
905    if the main source file doesn't have any symbol or line number
906    information, then copy this file's symtab and line_vector to the
907    main source file's subfile and discard the other subfile.  This can
908    happen because of a compiler bug or from the user playing games
909    with #line or from things like a distributed build system that
910    manipulates the debug info.  */
911
912 static void
913 watch_main_source_file_lossage (void)
914 {
915   struct subfile *mainsub, *subfile;
916
917   /* Find the main source file.
918      This loop could be eliminated if start_symtab saved it for us.  */
919   mainsub = NULL;
920   for (subfile = subfiles; subfile; subfile = subfile->next)
921     {
922       /* The main subfile is guaranteed to be the last one.  */
923       if (subfile->next == NULL)
924         mainsub = subfile;
925     }
926
927   /* If the main source file doesn't have any line number or symbol
928      info, look for an alias in another subfile.
929
930      We have to watch for mainsub == NULL here.  It's a quirk of
931      end_symtab, it can return NULL so there may not be a main
932      subfile.  */
933
934   if (mainsub
935       && mainsub->line_vector == NULL
936       && mainsub->symtab == NULL)
937     {
938       const char *mainbase = lbasename (mainsub->name);
939       int nr_matches = 0;
940       struct subfile *prevsub;
941       struct subfile *mainsub_alias = NULL;
942       struct subfile *prev_mainsub_alias = NULL;
943
944       prevsub = NULL;
945       for (subfile = subfiles;
946            /* Stop before we get to the last one.  */
947            subfile->next;
948            subfile = subfile->next)
949         {
950           if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
951             {
952               ++nr_matches;
953               mainsub_alias = subfile;
954               prev_mainsub_alias = prevsub;
955             }
956           prevsub = subfile;
957         }
958
959       if (nr_matches == 1)
960         {
961           gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
962
963           /* Found a match for the main source file.
964              Copy its line_vector and symtab to the main subfile
965              and then discard it.  */
966
967           mainsub->line_vector = mainsub_alias->line_vector;
968           mainsub->line_vector_length = mainsub_alias->line_vector_length;
969           mainsub->symtab = mainsub_alias->symtab;
970
971           if (prev_mainsub_alias == NULL)
972             subfiles = mainsub_alias->next;
973           else
974             prev_mainsub_alias->next = mainsub_alias->next;
975           xfree (mainsub_alias);
976         }
977     }
978 }
979
980 /* Helper function for qsort.  Parameters are `struct block *' pointers,
981    function sorts them in descending order by their BLOCK_START.  */
982
983 static int
984 block_compar (const void *ap, const void *bp)
985 {
986   const struct block *a = *(const struct block **) ap;
987   const struct block *b = *(const struct block **) bp;
988
989   return ((BLOCK_START (b) > BLOCK_START (a))
990           - (BLOCK_START (b) < BLOCK_START (a)));
991 }
992
993 /* Reset globals used to build symtabs.  */
994
995 static void
996 reset_symtab_globals (void)
997 {
998   set_last_source_file (NULL);
999   current_subfile = NULL;
1000   pending_macros = NULL;
1001   if (pending_addrmap)
1002     {
1003       obstack_free (&pending_addrmap_obstack, NULL);
1004       pending_addrmap = NULL;
1005     }
1006 }
1007
1008 /* Implementation of the first part of end_symtab.  It allows modifying
1009    STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
1010    If the returned value is NULL there is no blockvector created for
1011    this symtab (you still must call end_symtab_from_static_block).
1012
1013    END_ADDR is the same as for end_symtab: the address of the end of the
1014    file's text.
1015
1016    If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
1017    expandable.
1018
1019    If REQUIRED is non-zero, then a symtab is created even if it does
1020    not contain any symbols.  */
1021
1022 struct block *
1023 end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
1024                              int expandable, int required)
1025 {
1026   /* Finish the lexical context of the last function in the file; pop
1027      the context stack.  */
1028
1029   if (context_stack_depth > 0)
1030     {
1031       struct context_stack *cstk = pop_context ();
1032
1033       /* Make a block for the local symbols within.  */
1034       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1035                     cstk->start_addr, end_addr, objfile);
1036
1037       if (context_stack_depth > 0)
1038         {
1039           /* This is said to happen with SCO.  The old coffread.c
1040              code simply emptied the context stack, so we do the
1041              same.  FIXME: Find out why it is happening.  This is not
1042              believed to happen in most cases (even for coffread.c);
1043              it used to be an abort().  */
1044           complaint (&symfile_complaints,
1045                      _("Context stack not empty in end_symtab"));
1046           context_stack_depth = 0;
1047         }
1048     }
1049
1050   /* Reordered executables may have out of order pending blocks; if
1051      OBJF_REORDERED is true, then sort the pending blocks.  */
1052
1053   if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
1054     {
1055       unsigned count = 0;
1056       struct pending_block *pb;
1057       struct block **barray, **bp;
1058       struct cleanup *back_to;
1059
1060       for (pb = pending_blocks; pb != NULL; pb = pb->next)
1061         count++;
1062
1063       barray = xmalloc (sizeof (*barray) * count);
1064       back_to = make_cleanup (xfree, barray);
1065
1066       bp = barray;
1067       for (pb = pending_blocks; pb != NULL; pb = pb->next)
1068         *bp++ = pb->block;
1069
1070       qsort (barray, count, sizeof (*barray), block_compar);
1071
1072       bp = barray;
1073       for (pb = pending_blocks; pb != NULL; pb = pb->next)
1074         pb->block = *bp++;
1075
1076       do_cleanups (back_to);
1077     }
1078
1079   /* Cleanup any undefined types that have been left hanging around
1080      (this needs to be done before the finish_blocks so that
1081      file_symbols is still good).
1082
1083      Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
1084      specific, but harmless for other symbol readers, since on gdb
1085      startup or when finished reading stabs, the state is set so these
1086      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
1087      we make this cleaner?  */
1088
1089   cleanup_undefined_stabs_types (objfile);
1090   finish_global_stabs (objfile);
1091
1092   if (!required
1093       && pending_blocks == NULL
1094       && file_symbols == NULL
1095       && global_symbols == NULL
1096       && have_line_numbers == 0
1097       && pending_macros == NULL)
1098     {
1099       /* Ignore symtabs that have no functions with real debugging info.  */
1100       return NULL;
1101     }
1102   else
1103     {
1104       /* Define the STATIC_BLOCK.  */
1105       return finish_block_internal (NULL, &file_symbols, NULL,
1106                                     last_source_start_addr, end_addr, objfile,
1107                                     0, expandable);
1108     }
1109 }
1110
1111 /* Implementation of the second part of end_symtab.  Pass STATIC_BLOCK
1112    as value returned by end_symtab_get_static_block.
1113
1114    SECTION is the same as for end_symtab: the section number
1115    (in objfile->section_offsets) of the blockvector and linetable.
1116
1117    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1118    expandable.  */
1119
1120 struct symtab *
1121 end_symtab_from_static_block (struct block *static_block,
1122                               struct objfile *objfile, int section,
1123                               int expandable)
1124 {
1125   struct symtab *symtab = NULL;
1126   struct blockvector *blockvector;
1127   struct subfile *subfile;
1128   struct subfile *nextsub;
1129
1130   if (static_block == NULL)
1131     {
1132       /* Ignore symtabs that have no functions with real debugging info.  */
1133       blockvector = NULL;
1134     }
1135   else
1136     {
1137       CORE_ADDR end_addr = BLOCK_END (static_block);
1138
1139       /* Define after STATIC_BLOCK also GLOBAL_BLOCK, and build the
1140          blockvector.  */
1141       finish_block_internal (NULL, &global_symbols, NULL,
1142                              last_source_start_addr, end_addr, objfile,
1143                              1, expandable);
1144       blockvector = make_blockvector (objfile);
1145     }
1146
1147   /* Read the line table if it has to be read separately.
1148      This is only used by xcoffread.c.  */
1149   if (objfile->sf->sym_read_linetable != NULL)
1150     objfile->sf->sym_read_linetable (objfile);
1151
1152   /* Handle the case where the debug info specifies a different path
1153      for the main source file.  It can cause us to lose track of its
1154      line number information.  */
1155   watch_main_source_file_lossage ();
1156
1157   /* Now create the symtab objects proper, one for each subfile.  */
1158   /* (The main file is the last one on the chain.)  */
1159
1160   for (subfile = subfiles; subfile; subfile = nextsub)
1161     {
1162       int linetablesize = 0;
1163       symtab = NULL;
1164
1165       /* If we have blocks of symbols, make a symtab.  Otherwise, just
1166          ignore this file and any line number info in it.  */
1167       if (blockvector)
1168         {
1169           if (subfile->line_vector)
1170             {
1171               linetablesize = sizeof (struct linetable) +
1172                 subfile->line_vector->nitems * sizeof (struct linetable_entry);
1173
1174               /* Like the pending blocks, the line table may be
1175                  scrambled in reordered executables.  Sort it if
1176                  OBJF_REORDERED is true.  */
1177               if (objfile->flags & OBJF_REORDERED)
1178                 qsort (subfile->line_vector->item,
1179                        subfile->line_vector->nitems,
1180                      sizeof (struct linetable_entry), compare_line_numbers);
1181             }
1182
1183           /* Now, allocate a symbol table.  */
1184           if (subfile->symtab == NULL)
1185             symtab = allocate_symtab (subfile->name, objfile);
1186           else
1187             symtab = subfile->symtab;
1188
1189           /* Fill in its components.  */
1190           symtab->blockvector = blockvector;
1191           symtab->macro_table = pending_macros;
1192           if (subfile->line_vector)
1193             {
1194               /* Reallocate the line table on the symbol obstack.  */
1195               symtab->linetable = (struct linetable *)
1196                 obstack_alloc (&objfile->objfile_obstack, linetablesize);
1197               memcpy (symtab->linetable, subfile->line_vector, linetablesize);
1198             }
1199           else
1200             {
1201               symtab->linetable = NULL;
1202             }
1203           symtab->block_line_section = section;
1204           if (subfile->dirname)
1205             {
1206               /* Reallocate the dirname on the symbol obstack.  */
1207               symtab->dirname =
1208                 obstack_copy0 (&objfile->objfile_obstack,
1209                                subfile->dirname,
1210                                strlen (subfile->dirname));
1211             }
1212           else
1213             {
1214               symtab->dirname = NULL;
1215             }
1216
1217           /* Use whatever language we have been using for this
1218              subfile, not the one that was deduced in allocate_symtab
1219              from the filename.  We already did our own deducing when
1220              we created the subfile, and we may have altered our
1221              opinion of what language it is from things we found in
1222              the symbols.  */
1223           symtab->language = subfile->language;
1224
1225           /* Save the debug format string (if any) in the symtab.  */
1226           symtab->debugformat = subfile->debugformat;
1227
1228           /* Similarly for the producer.  */
1229           symtab->producer = subfile->producer;
1230
1231           /* All symtabs for the main file and the subfiles share a
1232              blockvector, so we need to clear primary for everything
1233              but the main file.  */
1234           set_symtab_primary (symtab, 0);
1235         }
1236       else
1237         {
1238           if (subfile->symtab)
1239             {
1240               /* Since we are ignoring that subfile, we also need
1241                  to unlink the associated empty symtab that we created.
1242                  Otherwise, we can run into trouble because various parts
1243                  such as the block-vector are uninitialized whereas
1244                  the rest of the code assumes that they are.
1245                  
1246                  We can only unlink the symtab because it was allocated
1247                  on the objfile obstack.  */
1248               struct symtab *s;
1249
1250               if (objfile->symtabs == subfile->symtab)
1251                 objfile->symtabs = objfile->symtabs->next;
1252               else
1253                 ALL_OBJFILE_SYMTABS (objfile, s)
1254                   if (s->next == subfile->symtab)
1255                     {
1256                       s->next = s->next->next;
1257                       break;
1258                     }
1259               subfile->symtab = NULL;
1260             }
1261         }
1262       if (subfile->name != NULL)
1263         {
1264           xfree ((void *) subfile->name);
1265         }
1266       if (subfile->dirname != NULL)
1267         {
1268           xfree ((void *) subfile->dirname);
1269         }
1270       if (subfile->line_vector != NULL)
1271         {
1272           xfree ((void *) subfile->line_vector);
1273         }
1274
1275       nextsub = subfile->next;
1276       xfree ((void *) subfile);
1277     }
1278
1279   /* Set this for the main source file.  */
1280   if (symtab)
1281     {
1282       set_symtab_primary (symtab, 1);
1283
1284       if (symtab->blockvector)
1285         {
1286           struct block *b = BLOCKVECTOR_BLOCK (symtab->blockvector,
1287                                                GLOBAL_BLOCK);
1288
1289           set_block_symtab (b, symtab);
1290         }
1291     }
1292
1293   /* Default any symbols without a specified symtab to the primary
1294      symtab.  */
1295   if (blockvector)
1296     {
1297       int block_i;
1298
1299       for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1300         {
1301           struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1302           struct symbol *sym;
1303           struct dict_iterator iter;
1304
1305           /* Inlined functions may have symbols not in the global or
1306              static symbol lists.  */
1307           if (BLOCK_FUNCTION (block) != NULL)
1308             if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
1309               SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
1310
1311           /* Note that we only want to fix up symbols from the local
1312              blocks, not blocks coming from included symtabs.  That is why
1313              we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
1314           ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
1315             if (SYMBOL_SYMTAB (sym) == NULL)
1316               SYMBOL_SYMTAB (sym) = symtab;
1317         }
1318     }
1319
1320   reset_symtab_globals ();
1321
1322   return symtab;
1323 }
1324
1325 /* Finish the symbol definitions for one main source file, close off
1326    all the lexical contexts for that file (creating struct block's for
1327    them), then make the struct symtab for that file and put it in the
1328    list of all such.
1329
1330    END_ADDR is the address of the end of the file's text.  SECTION is
1331    the section number (in objfile->section_offsets) of the blockvector
1332    and linetable.
1333
1334    Note that it is possible for end_symtab() to return NULL.  In
1335    particular, for the DWARF case at least, it will return NULL when
1336    it finds a compilation unit that has exactly one DIE, a
1337    TAG_compile_unit DIE.  This can happen when we link in an object
1338    file that was compiled from an empty source file.  Returning NULL
1339    is probably not the correct thing to do, because then gdb will
1340    never know about this empty file (FIXME).
1341
1342    If you need to modify STATIC_BLOCK before it is finalized you should
1343    call end_symtab_get_static_block and end_symtab_from_static_block
1344    yourself.  */
1345
1346 struct symtab *
1347 end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
1348 {
1349   struct block *static_block;
1350
1351   static_block = end_symtab_get_static_block (end_addr, objfile, 0, 0);
1352   return end_symtab_from_static_block (static_block, objfile, section, 0);
1353 }
1354
1355 /* Same as end_symtab except create a symtab that can be later added to.  */
1356
1357 struct symtab *
1358 end_expandable_symtab (CORE_ADDR end_addr, struct objfile *objfile,
1359                        int section)
1360 {
1361   struct block *static_block;
1362
1363   static_block = end_symtab_get_static_block (end_addr, objfile, 1, 0);
1364   return end_symtab_from_static_block (static_block, objfile, section, 1);
1365 }
1366
1367 /* Subroutine of augment_type_symtab to simplify it.
1368    Attach SYMTAB to all symbols in PENDING_LIST that don't have one.  */
1369
1370 static void
1371 set_missing_symtab (struct pending *pending_list, struct symtab *symtab)
1372 {
1373   struct pending *pending;
1374   int i;
1375
1376   for (pending = pending_list; pending != NULL; pending = pending->next)
1377     {
1378       for (i = 0; i < pending->nsyms; ++i)
1379         {
1380           if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
1381             SYMBOL_SYMTAB (pending->symbol[i]) = symtab;
1382         }
1383     }
1384 }
1385
1386 /* Same as end_symtab, but for the case where we're adding more symbols
1387    to an existing symtab that is known to contain only type information.
1388    This is the case for DWARF4 Type Units.  */
1389
1390 void
1391 augment_type_symtab (struct objfile *objfile, struct symtab *primary_symtab)
1392 {
1393   const struct blockvector *blockvector = primary_symtab->blockvector;
1394
1395   if (context_stack_depth > 0)
1396     {
1397       complaint (&symfile_complaints,
1398                  _("Context stack not empty in augment_type_symtab"));
1399       context_stack_depth = 0;
1400     }
1401   if (pending_blocks != NULL)
1402     complaint (&symfile_complaints, _("Blocks in a type symtab"));
1403   if (pending_macros != NULL)
1404     complaint (&symfile_complaints, _("Macro in a type symtab"));
1405   if (have_line_numbers)
1406     complaint (&symfile_complaints,
1407                _("Line numbers recorded in a type symtab"));
1408
1409   if (file_symbols != NULL)
1410     {
1411       struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1412
1413       /* First mark any symbols without a specified symtab as belonging
1414          to the primary symtab.  */
1415       set_missing_symtab (file_symbols, primary_symtab);
1416
1417       dict_add_pending (BLOCK_DICT (block), file_symbols);
1418     }
1419
1420   if (global_symbols != NULL)
1421     {
1422       struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1423
1424       /* First mark any symbols without a specified symtab as belonging
1425          to the primary symtab.  */
1426       set_missing_symtab (global_symbols, primary_symtab);
1427
1428       dict_add_pending (BLOCK_DICT (block), global_symbols);
1429     }
1430
1431   reset_symtab_globals ();
1432 }
1433
1434 /* Push a context block.  Args are an identifying nesting level
1435    (checkable when you pop it), and the starting PC address of this
1436    context.  */
1437
1438 struct context_stack *
1439 push_context (int desc, CORE_ADDR valu)
1440 {
1441   struct context_stack *new;
1442
1443   if (context_stack_depth == context_stack_size)
1444     {
1445       context_stack_size *= 2;
1446       context_stack = (struct context_stack *)
1447         xrealloc ((char *) context_stack,
1448                   (context_stack_size * sizeof (struct context_stack)));
1449     }
1450
1451   new = &context_stack[context_stack_depth++];
1452   new->depth = desc;
1453   new->locals = local_symbols;
1454   new->old_blocks = pending_blocks;
1455   new->start_addr = valu;
1456   new->using_directives = using_directives;
1457   new->name = NULL;
1458
1459   local_symbols = NULL;
1460   using_directives = NULL;
1461
1462   return new;
1463 }
1464
1465 /* Pop a context block.  Returns the address of the context block just
1466    popped.  */
1467
1468 struct context_stack *
1469 pop_context (void)
1470 {
1471   gdb_assert (context_stack_depth > 0);
1472   return (&context_stack[--context_stack_depth]);
1473 }
1474
1475 \f
1476
1477 /* Compute a small integer hash code for the given name.  */
1478
1479 int
1480 hashname (const char *name)
1481 {
1482     return (hash(name,strlen(name)) % HASHSIZE);
1483 }
1484 \f
1485
1486 void
1487 record_debugformat (const char *format)
1488 {
1489   current_subfile->debugformat = format;
1490 }
1491
1492 void
1493 record_producer (const char *producer)
1494 {
1495   current_subfile->producer = producer;
1496 }
1497
1498 /* Merge the first symbol list SRCLIST into the second symbol list
1499    TARGETLIST by repeated calls to add_symbol_to_list().  This
1500    procedure "frees" each link of SRCLIST by adding it to the
1501    free_pendings list.  Caller must set SRCLIST to a null list after
1502    calling this function.
1503
1504    Void return.  */
1505
1506 void
1507 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1508 {
1509   int i;
1510
1511   if (!srclist || !*srclist)
1512     return;
1513
1514   /* Merge in elements from current link.  */
1515   for (i = 0; i < (*srclist)->nsyms; i++)
1516     add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1517
1518   /* Recurse on next.  */
1519   merge_symbol_lists (&(*srclist)->next, targetlist);
1520
1521   /* "Free" the current link.  */
1522   (*srclist)->next = free_pendings;
1523   free_pendings = (*srclist);
1524 }
1525 \f
1526
1527 /* Name of source file whose symbol data we are now processing.  This
1528    comes from a symbol of type N_SO for stabs.  For Dwarf it comes
1529    from the DW_AT_name attribute of a DW_TAG_compile_unit DIE.  */
1530
1531 static char *last_source_file;
1532
1533 /* See buildsym.h.  */
1534
1535 void
1536 set_last_source_file (const char *name)
1537 {
1538   xfree (last_source_file);
1539   last_source_file = name == NULL ? NULL : xstrdup (name);
1540 }
1541
1542 /* See buildsym.h.  */
1543
1544 const char *
1545 get_last_source_file (void)
1546 {
1547   return last_source_file;
1548 }
1549
1550 \f
1551
1552 /* Initialize anything that needs initializing when starting to read a
1553    fresh piece of a symbol file, e.g. reading in the stuff
1554    corresponding to a psymtab.  */
1555
1556 void
1557 buildsym_init (void)
1558 {
1559   free_pendings = NULL;
1560   file_symbols = NULL;
1561   global_symbols = NULL;
1562   pending_blocks = NULL;
1563   pending_macros = NULL;
1564   using_directives = NULL;
1565   subfile_stack = NULL;
1566
1567   /* We shouldn't have any address map at this point.  */
1568   gdb_assert (! pending_addrmap);
1569   pending_addrmap_interesting = 0;
1570 }
1571
1572 /* Initialize anything that needs initializing when a completely new
1573    symbol file is specified (not just adding some symbols from another
1574    file, e.g. a shared library).  */
1575
1576 void
1577 buildsym_new_init (void)
1578 {
1579   buildsym_init ();
1580 }