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