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