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