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