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