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