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