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