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