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