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