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