Add many methods to buildsym_compunit
[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 /* Reset state after a successful building of a symtab.  */
1244
1245 static void
1246 reset_symtab_globals (void)
1247 {
1248   free_buildsym_compunit ();
1249 }
1250
1251 /* Implementation of the first part of end_symtab.  It allows modifying
1252    STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
1253    If the returned value is NULL there is no blockvector created for
1254    this symtab (you still must call end_symtab_from_static_block).
1255
1256    END_ADDR is the same as for end_symtab: the address of the end of the
1257    file's text.
1258
1259    If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
1260    expandable.
1261
1262    If REQUIRED is non-zero, then a symtab is created even if it does
1263    not contain any symbols.  */
1264
1265 struct block *
1266 buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr,
1267                                                 int expandable, int required)
1268 {
1269   /* Finish the lexical context of the last function in the file; pop
1270      the context stack.  */
1271
1272   if (!m_context_stack.empty ())
1273     {
1274       struct context_stack cstk = pop_context ();
1275
1276       /* Make a block for the local symbols within.  */
1277       finish_block (cstk.name, cstk.old_blocks, NULL,
1278                     cstk.start_addr, end_addr);
1279
1280       if (!m_context_stack.empty ())
1281         {
1282           /* This is said to happen with SCO.  The old coffread.c
1283              code simply emptied the context stack, so we do the
1284              same.  FIXME: Find out why it is happening.  This is not
1285              believed to happen in most cases (even for coffread.c);
1286              it used to be an abort().  */
1287           complaint (_("Context stack not empty in end_symtab"));
1288           m_context_stack.clear ();
1289         }
1290     }
1291
1292   /* Reordered executables may have out of order pending blocks; if
1293      OBJF_REORDERED is true, then sort the pending blocks.  */
1294
1295   if ((objfile->flags & OBJF_REORDERED) && m_pending_blocks)
1296     {
1297       struct pending_block *pb;
1298
1299       std::vector<block *> barray;
1300
1301       for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
1302         barray.push_back (pb->block);
1303
1304       /* Sort blocks by start address in descending order.  Blocks with the
1305          same start address must remain in the original order to preserve
1306          inline function caller/callee relationships.  */
1307       std::stable_sort (barray.begin (), barray.end (),
1308                         [] (const block *a, const block *b)
1309                         {
1310                           return BLOCK_START (a) > BLOCK_START (b);
1311                         });
1312
1313       int i = 0;
1314       for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
1315         pb->block = barray[i++];
1316     }
1317
1318   /* Cleanup any undefined types that have been left hanging around
1319      (this needs to be done before the finish_blocks so that
1320      file_symbols is still good).
1321
1322      Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
1323      specific, but harmless for other symbol readers, since on gdb
1324      startup or when finished reading stabs, the state is set so these
1325      are no-ops.  FIXME: Is this handled right in case of QUIT?  Can
1326      we make this cleaner?  */
1327
1328   cleanup_undefined_stabs_types (objfile);
1329   finish_global_stabs (objfile);
1330
1331   if (!required
1332       && m_pending_blocks == NULL
1333       && m_file_symbols == NULL
1334       && m_global_symbols == NULL
1335       && !m_have_line_numbers
1336       && m_pending_macros == NULL
1337       && m_global_using_directives == NULL)
1338     {
1339       /* Ignore symtabs that have no functions with real debugging info.  */
1340       return NULL;
1341     }
1342   else
1343     {
1344       /* Define the STATIC_BLOCK.  */
1345       return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
1346                                     m_last_source_start_addr,
1347                                     end_addr, 0, expandable);
1348     }
1349 }
1350
1351 /* Subroutine of end_symtab_from_static_block to simplify it.
1352    Handle the "have blockvector" case.
1353    See end_symtab_from_static_block for a description of the arguments.  */
1354
1355 struct compunit_symtab *
1356 buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
1357                                                 int section, int expandable)
1358 {
1359   struct compunit_symtab *cu = compunit_symtab;
1360   struct symtab *symtab;
1361   struct blockvector *blockvector;
1362   struct subfile *subfile;
1363   CORE_ADDR end_addr;
1364
1365   gdb_assert (static_block != NULL);
1366   gdb_assert (subfiles != NULL);
1367
1368   end_addr = BLOCK_END (static_block);
1369
1370   /* Create the GLOBAL_BLOCK and build the blockvector.  */
1371   finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
1372                          m_last_source_start_addr, end_addr,
1373                          1, expandable);
1374   blockvector = make_blockvector ();
1375
1376   /* Read the line table if it has to be read separately.
1377      This is only used by xcoffread.c.  */
1378   if (objfile->sf->sym_read_linetable != NULL)
1379     objfile->sf->sym_read_linetable (objfile);
1380
1381   /* Handle the case where the debug info specifies a different path
1382      for the main source file.  It can cause us to lose track of its
1383      line number information.  */
1384   watch_main_source_file_lossage ();
1385
1386   /* Now create the symtab objects proper, if not already done,
1387      one for each subfile.  */
1388
1389   for (subfile = subfiles;
1390        subfile != NULL;
1391        subfile = subfile->next)
1392     {
1393       int linetablesize = 0;
1394
1395       if (subfile->line_vector)
1396         {
1397           linetablesize = sizeof (struct linetable) +
1398             subfile->line_vector->nitems * sizeof (struct linetable_entry);
1399
1400           /* Like the pending blocks, the line table may be
1401              scrambled in reordered executables.  Sort it if
1402              OBJF_REORDERED is true.  */
1403           if (objfile->flags & OBJF_REORDERED)
1404             qsort (subfile->line_vector->item,
1405                    subfile->line_vector->nitems,
1406                    sizeof (struct linetable_entry), compare_line_numbers);
1407         }
1408
1409       /* Allocate a symbol table if necessary.  */
1410       if (subfile->symtab == NULL)
1411         subfile->symtab = allocate_symtab (cu, subfile->name);
1412       symtab = subfile->symtab;
1413
1414       /* Fill in its components.  */
1415
1416       if (subfile->line_vector)
1417         {
1418           /* Reallocate the line table on the symbol obstack.  */
1419           SYMTAB_LINETABLE (symtab) = (struct linetable *)
1420             obstack_alloc (&objfile->objfile_obstack, linetablesize);
1421           memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
1422                   linetablesize);
1423         }
1424       else
1425         {
1426           SYMTAB_LINETABLE (symtab) = NULL;
1427         }
1428
1429       /* Use whatever language we have been using for this
1430          subfile, not the one that was deduced in allocate_symtab
1431          from the filename.  We already did our own deducing when
1432          we created the subfile, and we may have altered our
1433          opinion of what language it is from things we found in
1434          the symbols.  */
1435       symtab->language = subfile->language;
1436     }
1437
1438   /* Make sure the symtab of main_subfile is the first in its list.  */
1439   {
1440     struct symtab *main_symtab, *prev_symtab;
1441
1442     main_symtab = main_subfile->symtab;
1443     prev_symtab = NULL;
1444     ALL_COMPUNIT_FILETABS (cu, symtab)
1445       {
1446         if (symtab == main_symtab)
1447           {
1448             if (prev_symtab != NULL)
1449               {
1450                 prev_symtab->next = main_symtab->next;
1451                 main_symtab->next = COMPUNIT_FILETABS (cu);
1452                 COMPUNIT_FILETABS (cu) = main_symtab;
1453               }
1454             break;
1455           }
1456         prev_symtab = symtab;
1457       }
1458     gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
1459   }
1460
1461   /* Fill out the compunit symtab.  */
1462
1463   if (comp_dir != NULL)
1464     {
1465       /* Reallocate the dirname on the symbol obstack.  */
1466       const char *comp_dir = this->comp_dir.get ();
1467       COMPUNIT_DIRNAME (cu)
1468         = (const char *) obstack_copy0 (&objfile->objfile_obstack,
1469                                         comp_dir, strlen (comp_dir));
1470     }
1471
1472   /* Save the debug format string (if any) in the symtab.  */
1473   COMPUNIT_DEBUGFORMAT (cu) = debugformat;
1474
1475   /* Similarly for the producer.  */
1476   COMPUNIT_PRODUCER (cu) = producer;
1477
1478   COMPUNIT_BLOCKVECTOR (cu) = blockvector;
1479   {
1480     struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1481
1482     set_block_compunit_symtab (b, cu);
1483   }
1484
1485   COMPUNIT_BLOCK_LINE_SECTION (cu) = section;
1486
1487   COMPUNIT_MACRO_TABLE (cu) = release_macros ();
1488
1489   /* Default any symbols without a specified symtab to the primary symtab.  */
1490   {
1491     int block_i;
1492
1493     /* The main source file's symtab.  */
1494     symtab = COMPUNIT_FILETABS (cu);
1495
1496     for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1497       {
1498         struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1499         struct symbol *sym;
1500         struct dict_iterator iter;
1501
1502         /* Inlined functions may have symbols not in the global or
1503            static symbol lists.  */
1504         if (BLOCK_FUNCTION (block) != NULL)
1505           if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
1506             symbol_set_symtab (BLOCK_FUNCTION (block), symtab);
1507
1508         /* Note that we only want to fix up symbols from the local
1509            blocks, not blocks coming from included symtabs.  That is why
1510            we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
1511         ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
1512           if (symbol_symtab (sym) == NULL)
1513             symbol_set_symtab (sym, symtab);
1514       }
1515   }
1516
1517   add_compunit_symtab_to_objfile (cu);
1518
1519   return cu;
1520 }
1521
1522 /* Implementation of the second part of end_symtab.  Pass STATIC_BLOCK
1523    as value returned by end_symtab_get_static_block.
1524
1525    SECTION is the same as for end_symtab: the section number
1526    (in objfile->section_offsets) of the blockvector and linetable.
1527
1528    If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1529    expandable.  */
1530
1531 struct compunit_symtab *
1532 buildsym_compunit::end_symtab_from_static_block (struct block *static_block,
1533                                                  int section, int expandable)
1534 {
1535   struct compunit_symtab *cu;
1536
1537   if (static_block == NULL)
1538     {
1539       /* Handle the "no blockvector" case.
1540          When this happens there is nothing to record, so there's nothing
1541          to do: memory will be freed up later.
1542
1543          Note: We won't be adding a compunit to the objfile's list of
1544          compunits, so there's nothing to unchain.  However, since each symtab
1545          is added to the objfile's obstack we can't free that space.
1546          We could do better, but this is believed to be a sufficiently rare
1547          event.  */
1548       cu = NULL;
1549     }
1550   else
1551     cu = end_symtab_with_blockvector (static_block, section, expandable);
1552
1553   reset_symtab_globals ();
1554
1555   return cu;
1556 }
1557
1558 /* Finish the symbol definitions for one main source file, close off
1559    all the lexical contexts for that file (creating struct block's for
1560    them), then make the struct symtab for that file and put it in the
1561    list of all such.
1562
1563    END_ADDR is the address of the end of the file's text.  SECTION is
1564    the section number (in objfile->section_offsets) of the blockvector
1565    and linetable.
1566
1567    Note that it is possible for end_symtab() to return NULL.  In
1568    particular, for the DWARF case at least, it will return NULL when
1569    it finds a compilation unit that has exactly one DIE, a
1570    TAG_compile_unit DIE.  This can happen when we link in an object
1571    file that was compiled from an empty source file.  Returning NULL
1572    is probably not the correct thing to do, because then gdb will
1573    never know about this empty file (FIXME).
1574
1575    If you need to modify STATIC_BLOCK before it is finalized you should
1576    call end_symtab_get_static_block and end_symtab_from_static_block
1577    yourself.  */
1578
1579 struct compunit_symtab *
1580 buildsym_compunit::end_symtab (CORE_ADDR end_addr, int section)
1581 {
1582   struct block *static_block;
1583
1584   static_block = end_symtab_get_static_block (end_addr, 0, 0);
1585   return end_symtab_from_static_block (static_block, section, 0);
1586 }
1587
1588 /* Same as end_symtab except create a symtab that can be later added to.  */
1589
1590 struct compunit_symtab *
1591 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
1592 {
1593   struct block *static_block;
1594
1595   static_block = end_symtab_get_static_block (end_addr, 1, 0);
1596   return end_symtab_from_static_block (static_block, section, 1);
1597 }
1598
1599 /* Subroutine of augment_type_symtab to simplify it.
1600    Attach the main source file's symtab to all symbols in PENDING_LIST that
1601    don't have one.  */
1602
1603 static void
1604 set_missing_symtab (struct pending *pending_list,
1605                     struct compunit_symtab *cu)
1606 {
1607   struct pending *pending;
1608   int i;
1609
1610   for (pending = pending_list; pending != NULL; pending = pending->next)
1611     {
1612       for (i = 0; i < pending->nsyms; ++i)
1613         {
1614           if (symbol_symtab (pending->symbol[i]) == NULL)
1615             symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu));
1616         }
1617     }
1618 }
1619
1620 /* Same as end_symtab, but for the case where we're adding more symbols
1621    to an existing symtab that is known to contain only type information.
1622    This is the case for DWARF4 Type Units.  */
1623
1624 void
1625 buildsym_compunit::augment_type_symtab ()
1626 {
1627   struct compunit_symtab *cust = compunit_symtab;
1628   const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);
1629
1630   if (!m_context_stack.empty ())
1631     complaint (_("Context stack not empty in augment_type_symtab"));
1632   if (m_pending_blocks != NULL)
1633     complaint (_("Blocks in a type symtab"));
1634   if (m_pending_macros != NULL)
1635     complaint (_("Macro in a type symtab"));
1636   if (m_have_line_numbers)
1637     complaint (_("Line numbers recorded in a type symtab"));
1638
1639   if (m_file_symbols != NULL)
1640     {
1641       struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1642
1643       /* First mark any symbols without a specified symtab as belonging
1644          to the primary symtab.  */
1645       set_missing_symtab (m_file_symbols, cust);
1646
1647       dict_add_pending (BLOCK_DICT (block), m_file_symbols);
1648     }
1649
1650   if (m_global_symbols != NULL)
1651     {
1652       struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1653
1654       /* First mark any symbols without a specified symtab as belonging
1655          to the primary symtab.  */
1656       set_missing_symtab (m_global_symbols, cust);
1657
1658       dict_add_pending (BLOCK_DICT (block),
1659                         m_global_symbols);
1660     }
1661
1662   reset_symtab_globals ();
1663 }
1664
1665 /* Push a context block.  Args are an identifying nesting level
1666    (checkable when you pop it), and the starting PC address of this
1667    context.  */
1668
1669 struct context_stack *
1670 buildsym_compunit::push_context (int desc, CORE_ADDR valu)
1671 {
1672   m_context_stack.emplace_back ();
1673   struct context_stack *newobj = &m_context_stack.back ();
1674
1675   newobj->depth = desc;
1676   newobj->locals = m_local_symbols;
1677   newobj->old_blocks = m_pending_blocks;
1678   newobj->start_addr = valu;
1679   newobj->local_using_directives = m_local_using_directives;
1680   newobj->name = NULL;
1681
1682   m_local_symbols = NULL;
1683   m_local_using_directives = NULL;
1684
1685   return newobj;
1686 }
1687
1688 /* Pop a context block.  Returns the address of the context block just
1689    popped.  */
1690
1691 struct context_stack
1692 buildsym_compunit::pop_context ()
1693 {
1694   gdb_assert (!m_context_stack.empty ());
1695   struct context_stack result = m_context_stack.back ();
1696   m_context_stack.pop_back ();
1697   return result;
1698 }
1699
1700 \f
1701
1702 void
1703 record_debugformat (const char *format)
1704 {
1705   buildsym_compunit->record_debugformat (format);
1706 }
1707
1708 void
1709 record_producer (const char *producer)
1710 {
1711   buildsym_compunit->record_producer (producer);
1712 }
1713
1714 \f
1715
1716 /* See buildsym.h.  */
1717
1718 void
1719 set_last_source_file (const char *name)
1720 {
1721   gdb_assert (buildsym_compunit != nullptr || name == nullptr);
1722   if (buildsym_compunit != nullptr)
1723     buildsym_compunit->set_last_source_file (name);
1724 }
1725
1726 /* See buildsym.h.  */
1727
1728 const char *
1729 get_last_source_file ()
1730 {
1731   if (buildsym_compunit == nullptr)
1732     return nullptr;
1733   return buildsym_compunit->get_last_source_file ();
1734 }
1735
1736 /* See buildsym.h.  */
1737
1738 void
1739 set_last_source_start_addr (CORE_ADDR addr)
1740 {
1741   gdb_assert (buildsym_compunit != nullptr);
1742   buildsym_compunit->set_last_source_start_addr (addr);
1743 }
1744
1745 /* See buildsym.h.  */
1746
1747 CORE_ADDR
1748 get_last_source_start_addr ()
1749 {
1750   gdb_assert (buildsym_compunit != nullptr);
1751   return buildsym_compunit->get_last_source_start_addr ();
1752 }
1753
1754 /* See buildsym.h.  */
1755
1756 struct using_direct **
1757 get_local_using_directives ()
1758 {
1759   gdb_assert (buildsym_compunit != nullptr);
1760   return buildsym_compunit->get_local_using_directives ();
1761 }
1762
1763 /* See buildsym.h.  */
1764
1765 void
1766 set_local_using_directives (struct using_direct *new_local)
1767 {
1768   gdb_assert (buildsym_compunit != nullptr);
1769   buildsym_compunit->set_local_using_directives (new_local);
1770 }
1771
1772 /* See buildsym.h.  */
1773
1774 struct using_direct **
1775 get_global_using_directives ()
1776 {
1777   gdb_assert (buildsym_compunit != nullptr);
1778   return buildsym_compunit->get_global_using_directives ();
1779 }
1780
1781 /* See buildsym.h.  */
1782
1783 bool
1784 outermost_context_p ()
1785 {
1786   gdb_assert (buildsym_compunit != nullptr);
1787   return buildsym_compunit->outermost_context_p ();
1788 }
1789
1790 /* See buildsym.h.  */
1791
1792 struct context_stack *
1793 get_current_context_stack ()
1794 {
1795   gdb_assert (buildsym_compunit != nullptr);
1796   return buildsym_compunit->get_current_context_stack ();
1797 }
1798
1799 /* See buildsym.h.  */
1800
1801 int
1802 get_context_stack_depth ()
1803 {
1804   gdb_assert (buildsym_compunit != nullptr);
1805   return buildsym_compunit->get_context_stack_depth ();
1806 }
1807
1808 /* See buildsym.h.  */
1809
1810 struct subfile *
1811 get_current_subfile ()
1812 {
1813   gdb_assert (buildsym_compunit != nullptr);
1814   return buildsym_compunit->get_current_subfile ();
1815 }
1816
1817 /* See buildsym.h.  */
1818
1819 struct pending **
1820 get_local_symbols ()
1821 {
1822   gdb_assert (buildsym_compunit != nullptr);
1823   return buildsym_compunit->get_local_symbols ();
1824 }
1825
1826 /* See buildsym.h.  */
1827
1828 struct pending **
1829 get_file_symbols ()
1830 {
1831   gdb_assert (buildsym_compunit != nullptr);
1832   return buildsym_compunit->get_file_symbols ();
1833 }
1834
1835 /* See buildsym.h.  */
1836
1837 struct pending **
1838 get_global_symbols ()
1839 {
1840   gdb_assert (buildsym_compunit != nullptr);
1841   return buildsym_compunit->get_global_symbols ();
1842 }
1843
1844 void
1845 start_subfile (const char *name)
1846 {
1847   gdb_assert (buildsym_compunit != nullptr);
1848   buildsym_compunit->start_subfile (name);
1849 }
1850
1851 void
1852 patch_subfile_names (struct subfile *subfile, const char *name)
1853 {
1854   gdb_assert (buildsym_compunit != nullptr);
1855   buildsym_compunit->patch_subfile_names (subfile, name);
1856 }
1857
1858 void
1859 push_subfile ()
1860 {
1861   gdb_assert (buildsym_compunit != nullptr);
1862   buildsym_compunit->push_subfile ();
1863 }
1864
1865 const char *
1866 pop_subfile ()
1867 {
1868   gdb_assert (buildsym_compunit != nullptr);
1869   return buildsym_compunit->pop_subfile ();
1870 }
1871
1872 struct block *
1873 end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
1874 {
1875   gdb_assert (buildsym_compunit != nullptr);
1876   return buildsym_compunit->end_symtab_get_static_block (end_addr, expandable,
1877                                                          required);
1878 }
1879
1880 struct compunit_symtab *
1881 end_symtab_from_static_block (struct block *static_block,
1882                               int section, int expandable)
1883 {
1884   gdb_assert (buildsym_compunit != nullptr);
1885   struct compunit_symtab *result
1886     = buildsym_compunit->end_symtab_from_static_block (static_block,
1887                                                        section, expandable);
1888   reset_symtab_globals ();
1889   return result;
1890 }
1891
1892 struct compunit_symtab *
1893 end_symtab (CORE_ADDR end_addr, int section)
1894 {
1895   gdb_assert (buildsym_compunit != nullptr);
1896   struct compunit_symtab *result
1897     = buildsym_compunit->end_symtab (end_addr, section);
1898   reset_symtab_globals ();
1899   return result;
1900 }
1901
1902 struct compunit_symtab *
1903 end_expandable_symtab (CORE_ADDR end_addr, int section)
1904 {
1905   gdb_assert (buildsym_compunit != nullptr);
1906   struct compunit_symtab *result
1907     = buildsym_compunit->end_expandable_symtab (end_addr, section);
1908   reset_symtab_globals ();
1909   return result;
1910 }
1911
1912 void
1913 augment_type_symtab ()
1914 {
1915   gdb_assert (buildsym_compunit != nullptr);
1916   buildsym_compunit->augment_type_symtab ();
1917   reset_symtab_globals ();
1918 }
1919
1920 struct context_stack *
1921 push_context (int desc, CORE_ADDR valu)
1922 {
1923   gdb_assert (buildsym_compunit != nullptr);
1924   return buildsym_compunit->push_context (desc, valu);
1925 }
1926
1927 struct context_stack
1928 pop_context ()
1929 {
1930   gdb_assert (buildsym_compunit != nullptr);
1931   return buildsym_compunit->pop_context ();
1932 }
1933
1934 struct block *
1935 finish_block (struct symbol *symbol, struct pending_block *old_blocks,
1936               const struct dynamic_prop *static_link,
1937               CORE_ADDR start, CORE_ADDR end)
1938 {
1939   gdb_assert (buildsym_compunit != nullptr);
1940   return buildsym_compunit->finish_block (symbol, old_blocks, static_link,
1941                                           start, end);
1942 }
1943
1944 void
1945 record_block_range (struct block *block, CORE_ADDR start,
1946                     CORE_ADDR end_inclusive)
1947 {
1948   gdb_assert (buildsym_compunit != nullptr);
1949   buildsym_compunit->record_block_range (block, start, end_inclusive);
1950 }
1951
1952 void
1953 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
1954 {
1955   gdb_assert (buildsym_compunit != nullptr);
1956   buildsym_compunit->record_line (subfile, line, pc);
1957 }