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