]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/buildsym.c
sim: ppc: hoist pk.h creation to top-level
[thirdparty/binutils-gdb.git] / gdb / buildsym.c
CommitLineData
c906108c 1/* Support routines for building symbol tables in GDB's internal format.
213516ef 2 Copyright (C) 1986-2023 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 18
c906108c 19#include "defs.h"
d55e5aa6 20#include "buildsym-legacy.h"
4de283e4 21#include "bfd.h"
bf31fd38 22#include "gdbsupport/gdb_obstack.h"
ffaebc19 23#include "gdbsupport/pathstuff.h"
4de283e4
TT
24#include "symtab.h"
25#include "symfile.h"
26#include "objfiles.h"
d55e5aa6 27#include "gdbtypes.h"
4de283e4 28#include "complaints.h"
ef0f16cc
TT
29#include "expression.h"
30#include "filenames.h"
d55e5aa6 31#include "macrotab.h"
ef0f16cc 32#include "demangle.h"
4de283e4
TT
33#include "block.h"
34#include "cp-support.h"
35#include "dictionary.h"
4de283e4 36#include <algorithm>
9219021c 37
0a0edcd5 38/* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
c906108c
SS
39 questionable--see comment where we call them). */
40
41#include "stabsread.h"
42
93eed41f
TT
43/* List of blocks already made (lexical contexts already closed).
44 This is used at the end to make the blockvector. */
45
46struct pending_block
47 {
48 struct pending_block *next;
49 struct block *block;
50 };
51
ab209f6f
TT
52buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
53 const char *name,
54 const char *comp_dir_,
f71ad555 55 const char *name_for_id,
ab209f6f
TT
56 enum language language_,
57 CORE_ADDR last_addr)
cbb09508 58 : m_objfile (objfile_),
ab209f6f 59 m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
ebd4e6d0 60 m_comp_dir (comp_dir_ == nullptr ? "" : comp_dir_),
cbb09508 61 m_language (language_),
ab209f6f
TT
62 m_last_source_start_addr (last_addr)
63{
64 /* Allocate the compunit symtab now. The caller needs it to allocate
65 non-primary symtabs. It is also needed by get_macro_table. */
cbb09508 66 m_compunit_symtab = allocate_compunit_symtab (m_objfile, name);
ab209f6f
TT
67
68 /* Build the subfile for NAME (the main source file) so that we can record
69 a pointer to it for later.
70 IMPORTANT: Do not allocate a struct symtab for NAME here.
71 It can happen that the debug info provides a different path to NAME than
72 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
73 that only works if the main_subfile doesn't have a symtab yet. */
f71ad555 74 start_subfile (name, name_for_id);
ab209f6f
TT
75 /* Save this so that we don't have to go looking for it at the end
76 of the subfiles list. */
cbb09508 77 m_main_subfile = m_current_subfile;
ab209f6f
TT
78}
79
80buildsym_compunit::~buildsym_compunit ()
81{
82 struct subfile *subfile, *nextsub;
83
84 if (m_pending_macros != nullptr)
85 free_macro_table (m_pending_macros);
86
cbb09508 87 for (subfile = m_subfiles;
ab209f6f
TT
88 subfile != NULL;
89 subfile = nextsub)
90 {
91 nextsub = subfile->next;
71bc95ed 92 delete subfile;
ab209f6f
TT
93 }
94
95 struct pending *next, *next1;
96
97 for (next = m_file_symbols; next != NULL; next = next1)
98 {
99 next1 = next->next;
100 xfree ((void *) next);
101 }
102
103 for (next = m_global_symbols; next != NULL; next = next1)
104 {
105 next1 = next->next;
106 xfree ((void *) next);
107 }
108}
109
110struct macro_table *
111buildsym_compunit::get_macro_table ()
112{
113 if (m_pending_macros == nullptr)
cbb09508 114 m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
be1e3d3e 115 &m_objfile->per_bfd->string_cache,
cbb09508 116 m_compunit_symtab);
ab209f6f
TT
117 return m_pending_macros;
118}
119
4a64f543 120/* Maintain the lists of symbols and blocks. */
c906108c 121
93bf33fd 122/* Add a symbol to one of the lists of symbols. */
c906108c
SS
123
124void
125add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
126{
52f0bd74 127 struct pending *link;
c906108c
SS
128
129 /* If this is an alias for another symbol, don't add it. */
4d4eaa30 130 if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#')
c906108c
SS
131 return;
132
4a64f543 133 /* We keep PENDINGSIZE symbols in each link of the list. If we
c906108c
SS
134 don't have a link with room in it, add a new link. */
135 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
136 {
1d376700 137 link = XNEW (struct pending);
c906108c
SS
138 link->next = *listhead;
139 *listhead = link;
140 link->nsyms = 0;
141 }
142
143 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
144}
145
146/* Find a symbol named NAME on a LIST. NAME need not be
147 '\0'-terminated; LENGTH is the length of the name. */
148
149struct symbol *
150find_symbol_in_list (struct pending *list, char *name, int length)
151{
152 int j;
0d5cff50 153 const char *pp;
c906108c
SS
154
155 while (list != NULL)
156 {
157 for (j = list->nsyms; --j >= 0;)
158 {
987012b8 159 pp = list->symbol[j]->linkage_name ();
5aafa1cc
PM
160 if (*pp == *name && strncmp (pp, name, length) == 0
161 && pp[length] == '\0')
c906108c
SS
162 {
163 return (list->symbol[j]);
164 }
165 }
166 list = list->next;
167 }
168 return (NULL);
169}
170
6b213a47
TT
171/* Record BLOCK on the list of all blocks in the file. Put it after
172 OPBLOCK, or at the beginning if opblock is NULL. This puts the
173 block in the list after all its subblocks. */
174
4a2125f5
TT
175void
176buildsym_compunit::record_pending_block (struct block *block,
177 struct pending_block *opblock)
6b213a47
TT
178{
179 struct pending_block *pblock;
180
4a2125f5 181 pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
6b213a47
TT
182 pblock->block = block;
183 if (opblock)
184 {
185 pblock->next = opblock->next;
186 opblock->next = pblock;
187 }
188 else
189 {
4a2125f5
TT
190 pblock->next = m_pending_blocks;
191 m_pending_blocks = pblock;
6b213a47
TT
192 }
193}
194
c906108c
SS
195/* Take one of the lists of symbols and make a block from it. Keep
196 the order the symbols have in the list (reversed from the input
197 file). Put the block on the list of pending blocks. */
198
4a2125f5
TT
199struct block *
200buildsym_compunit::finish_block_internal
201 (struct symbol *symbol,
202 struct pending **listhead,
203 struct pending_block *old_blocks,
204 const struct dynamic_prop *static_link,
205 CORE_ADDR start, CORE_ADDR end,
206 int is_global, int expandable)
c906108c 207{
08feed99 208 struct gdbarch *gdbarch = m_objfile->arch ();
52f0bd74
AC
209 struct pending *next, *next1;
210 struct block *block;
211 struct pending_block *pblock;
c906108c 212 struct pending_block *opblock;
c906108c 213
52255383
TT
214 if (is_global)
215 block = new (&m_objfile->objfile_obstack) global_block;
216 else
217 block = new (&m_objfile->objfile_obstack) struct block;
c906108c 218
261397f8
DJ
219 if (symbol)
220 {
24d74bb5
SM
221 block->set_multidict
222 (mdict_create_linear (&m_objfile->objfile_obstack, *listhead));
261397f8
DJ
223 }
224 else
c906108c 225 {
6d30eef8
DE
226 if (expandable)
227 {
24d74bb5
SM
228 block->set_multidict
229 (mdict_create_hashed_expandable (m_language));
230 mdict_add_pending (block->multidict (), *listhead);
6d30eef8
DE
231 }
232 else
233 {
24d74bb5
SM
234 block->set_multidict
235 (mdict_create_hashed (&m_objfile->objfile_obstack, *listhead));
6d30eef8 236 }
c906108c
SS
237 }
238
4b8791e1
SM
239 block->set_start (start);
240 block->set_end (end);
c906108c 241
c906108c
SS
242 /* Put the block in as the value of the symbol that names it. */
243
244 if (symbol)
245 {
5f9c5a63 246 struct type *ftype = symbol->type ();
4aeddc50 247 symbol->set_value_block (block);
5abbfa98 248 symbol->set_section_index (SECT_OFF_TEXT (m_objfile));
6c00f721 249 block->set_function (symbol);
c906108c 250
1f704f76 251 if (ftype->num_fields () <= 0)
c906108c
SS
252 {
253 /* No parameter type information is recorded with the
254 function's type. Set that from the type of the
4a64f543 255 parameter symbols. */
c906108c 256 int nparams = 0, iparams;
8157b174
TT
257
258 /* Here we want to directly access the dictionary, because
259 we haven't fully initialized the block yet. */
8e8d48f9 260 for (struct symbol *sym : block->multidict_symbols ())
c906108c 261 {
d9743061 262 if (sym->is_argument ())
2a2d4dc3 263 nparams++;
c906108c
SS
264 }
265 if (nparams > 0)
266 {
2774f2da 267 ftype->alloc_fields (nparams);
c906108c 268
de4f826b 269 iparams = 0;
8157b174
TT
270 /* Here we want to directly access the dictionary, because
271 we haven't fully initialized the block yet. */
8e8d48f9 272 for (struct symbol *sym : block->multidict_symbols ())
c906108c 273 {
de4f826b
DC
274 if (iparams == nparams)
275 break;
276
d9743061 277 if (sym->is_argument ())
c906108c 278 {
5f9c5a63 279 ftype->field (iparams).set_type (sym->type ());
321d8b3f 280 ftype->field (iparams).set_is_artificial (false);
c906108c 281 iparams++;
c906108c
SS
282 }
283 }
284 }
285 }
286 }
287 else
6c00f721 288 block->set_function (nullptr);
c906108c 289
63e43d3a 290 if (static_link != NULL)
cbb09508 291 objfile_register_static_link (m_objfile, block, static_link);
63e43d3a 292
1d376700 293 /* Now free the links of the list, and empty the list. */
c906108c
SS
294
295 for (next = *listhead; next; next = next1)
296 {
297 next1 = next->next;
1d376700 298 xfree (next);
c906108c
SS
299 }
300 *listhead = NULL;
301
c906108c 302 /* Check to be sure that the blocks have an end address that is
4a64f543 303 greater than starting address. */
c906108c 304
4b8791e1 305 if (block->end () < block->start ())
c906108c
SS
306 {
307 if (symbol)
308 {
b98664d3 309 complaint (_("block end address less than block "
3e43a32a 310 "start address in %s (patched it)"),
987012b8 311 symbol->print_name ());
c906108c
SS
312 }
313 else
314 {
b98664d3 315 complaint (_("block end address %s less than block "
3e43a32a 316 "start address %s (patched it)"),
4b8791e1
SM
317 paddress (gdbarch, block->end ()),
318 paddress (gdbarch, block->start ()));
c906108c 319 }
4a64f543 320 /* Better than nothing. */
4b8791e1 321 block->set_end (block->start ());
c906108c 322 }
c906108c
SS
323
324 /* Install this block as the superblock of all blocks made since the
325 start of this scope that don't have superblocks yet. */
326
327 opblock = NULL;
4a2125f5 328 for (pblock = m_pending_blocks;
c0219d42
MS
329 pblock && pblock != old_blocks;
330 pblock = pblock->next)
c906108c 331 {
f135fe72 332 if (pblock->block->superblock () == NULL)
c906108c 333 {
c906108c 334 /* Check to be sure the blocks are nested as we receive
4a64f543 335 them. If the compiler/assembler/linker work, this just
14711c82
DJ
336 burns a small amount of time.
337
338 Skip blocks which correspond to a function; they're not
339 physically nested inside this other blocks, only
340 lexically nested. */
6c00f721 341 if (pblock->block->function () == NULL
4b8791e1
SM
342 && (pblock->block->start () < block->start ()
343 || pblock->block->end () > block->end ()))
c906108c
SS
344 {
345 if (symbol)
346 {
b98664d3 347 complaint (_("inner block not inside outer block in %s"),
987012b8 348 symbol->print_name ());
c906108c
SS
349 }
350 else
351 {
b98664d3 352 complaint (_("inner block (%s-%s) not "
3e43a32a 353 "inside outer block (%s-%s)"),
4b8791e1
SM
354 paddress (gdbarch, pblock->block->start ()),
355 paddress (gdbarch, pblock->block->end ()),
356 paddress (gdbarch, block->start ()),
357 paddress (gdbarch, block->end ()));
c906108c 358 }
4b8791e1
SM
359
360 if (pblock->block->start () < block->start ())
361 pblock->block->set_start (block->start ());
362
363 if (pblock->block->end () > block->end ())
364 pblock->block->set_end (block->end ());
c906108c 365 }
f135fe72 366 pblock->block->set_superblock (block);
c906108c
SS
367 }
368 opblock = pblock;
369 }
370
3c45e9f9
TT
371 block->set_using ((is_global
372 ? m_global_using_directives
373 : m_local_using_directives),
374 &m_objfile->objfile_obstack);
22cee43f 375 if (is_global)
4a2125f5 376 m_global_using_directives = NULL;
22cee43f 377 else
4a2125f5 378 m_local_using_directives = NULL;
27aa8d6a 379
6b213a47 380 record_pending_block (block, opblock);
801e3a5b
JB
381
382 return block;
c906108c
SS
383}
384
84a146c9 385struct block *
4a2125f5
TT
386buildsym_compunit::finish_block (struct symbol *symbol,
387 struct pending_block *old_blocks,
388 const struct dynamic_prop *static_link,
389 CORE_ADDR start, CORE_ADDR end)
84a146c9 390{
4a2125f5
TT
391 return finish_block_internal (symbol, &m_local_symbols,
392 old_blocks, static_link, start, end, 0, 0);
84a146c9 393}
de4f826b 394
801e3a5b
JB
395/* Record that the range of addresses from START to END_INCLUSIVE
396 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
397 addresses must be set already. You must apply this function to all
398 BLOCK's children before applying it to BLOCK.
399
400 If a call to this function complicates the picture beyond that
401 already provided by BLOCK_START and BLOCK_END, then we create an
402 address map for the block. */
403void
4a2125f5
TT
404buildsym_compunit::record_block_range (struct block *block,
405 CORE_ADDR start,
406 CORE_ADDR end_inclusive)
801e3a5b
JB
407{
408 /* If this is any different from the range recorded in the block's
409 own BLOCK_START and BLOCK_END, then note that the address map has
410 become interesting. Note that even if this block doesn't have
411 any "interesting" ranges, some later block might, so we still
412 need to record this block in the addrmap. */
4b8791e1
SM
413 if (start != block->start ()
414 || end_inclusive + 1 != block->end ())
4a2125f5 415 m_pending_addrmap_interesting = true;
801e3a5b 416
93b527ef 417 m_pending_addrmap.set_empty (start, end_inclusive, block);
801e3a5b
JB
418}
419
4a2125f5
TT
420struct blockvector *
421buildsym_compunit::make_blockvector ()
c906108c 422{
52f0bd74
AC
423 struct pending_block *next;
424 struct blockvector *blockvector;
425 int i;
c906108c
SS
426
427 /* Count the length of the list of blocks. */
428
4a2125f5 429 for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
5ac04550 430 {
c906108c
SS
431 }
432
433 blockvector = (struct blockvector *)
cbb09508 434 obstack_alloc (&m_objfile->objfile_obstack,
c906108c
SS
435 (sizeof (struct blockvector)
436 + (i - 1) * sizeof (struct block *)));
437
4a64f543 438 /* Copy the blocks into the blockvector. This is done in reverse
c906108c 439 order, which happens to put the blocks into the proper order
4a64f543 440 (ascending starting address). finish_block has hair to insert
c906108c
SS
441 each block into the list after its subblocks in order to make
442 sure this is true. */
443
63d609de 444 blockvector->set_num_blocks (i);
4a2125f5 445 for (next = m_pending_blocks; next; next = next->next)
63d609de 446 blockvector->set_block (--i, next->block);
c906108c 447
4a2125f5 448 free_pending_blocks ();
c906108c 449
801e3a5b
JB
450 /* If we needed an address map for this symtab, record it in the
451 blockvector. */
93b527ef 452 if (m_pending_addrmap_interesting)
414705d1 453 blockvector->set_map
d89120e9 454 (new (&m_objfile->objfile_obstack) addrmap_fixed
93b527ef 455 (&m_objfile->objfile_obstack, &m_pending_addrmap));
801e3a5b 456 else
414705d1 457 blockvector->set_map (nullptr);
4aad0dfc 458
c906108c 459 /* Some compilers output blocks in the wrong order, but we depend on
4a64f543 460 their being in the right order so we can binary search. Check the
4aad0dfc
DE
461 order and moan about it.
462 Note: Remember that the first two blocks are the global and static
463 blocks. We could special case that fact and begin checking at block 2.
464 To avoid making that assumption we do not. */
63d609de 465 if (blockvector->num_blocks () > 1)
c906108c 466 {
63d609de 467 for (i = 1; i < blockvector->num_blocks (); i++)
c906108c 468 {
63d609de
SM
469 if (blockvector->block (i - 1)->start ()
470 > blockvector->block (i)->start ())
c906108c 471 {
59527da0 472 CORE_ADDR start
63d609de 473 = blockvector->block (i)->start ();
c906108c 474
b98664d3 475 complaint (_("block at %s out of order"),
bb599908 476 hex_string ((LONGEST) start));
c906108c
SS
477 }
478 }
479 }
c906108c
SS
480
481 return (blockvector);
482}
f71ad555
SM
483
484/* See buildsym.h. */
c906108c
SS
485
486void
f71ad555 487buildsym_compunit::start_subfile (const char *name, const char *name_for_id)
c906108c 488{
43f3e411
DE
489 /* See if this subfile is already registered. */
490
f71ad555 491 symtab_create_debug_printf ("name = %s, name_for_id = %s", name, name_for_id);
80affb9f 492
71bc95ed 493 for (subfile *subfile = m_subfiles; subfile; subfile = subfile->next)
ee26d001
SM
494 if (FILENAME_CMP (subfile->name_for_id.c_str (), name_for_id) == 0)
495 {
496 symtab_create_debug_printf ("found existing symtab with name_for_id %s",
497 subfile->name_for_id.c_str ());
498 m_current_subfile = subfile;
499 return;
500 }
c906108c 501
43f3e411 502 /* This subfile is not known. Add an entry for it. */
c906108c 503
71bc95ed 504 subfile_up subfile (new struct subfile);
ebd4e6d0 505 subfile->name = name;
f71ad555 506 subfile->name_for_id = name_for_id;
43f3e411 507
71bc95ed 508 m_current_subfile = subfile.get ();
c906108c 509
c906108c
SS
510 /* Default the source language to whatever can be deduced from the
511 filename. If nothing can be deduced (such as for a C/C++ include
512 file with a ".h" extension), then inherit whatever language the
513 previous subfile had. This kludgery is necessary because there
514 is no standard way in some object formats to record the source
515 language. Also, when symtabs are allocated we try to deduce a
516 language then as well, but it is too late for us to use that
517 information while reading symbols, since symtabs aren't allocated
518 until after all the symbols have been processed for a given
4a64f543 519 source file. */
c906108c 520
ebd4e6d0 521 subfile->language = deduce_language_from_filename (subfile->name.c_str ());
71bc95ed
SM
522 if (subfile->language == language_unknown && m_subfiles != nullptr)
523 subfile->language = m_subfiles->language;
c906108c 524
25caa7a8 525 /* If the filename of this subfile ends in .C, then change the
c906108c 526 language of any pending subfiles from C to C++. We also accept
25caa7a8 527 any other C++ suffixes accepted by deduce_language_from_filename. */
c906108c
SS
528 /* Likewise for f2c. */
529
ebd4e6d0 530 if (!subfile->name.empty ())
c906108c
SS
531 {
532 struct subfile *s;
ebd4e6d0 533 language sublang = deduce_language_from_filename (subfile->name.c_str ());
c906108c
SS
534
535 if (sublang == language_cplus || sublang == language_fortran)
cbb09508 536 for (s = m_subfiles; s != NULL; s = s->next)
c906108c
SS
537 if (s->language == language_c)
538 s->language = sublang;
539 }
540
541 /* And patch up this file if necessary. */
542 if (subfile->language == language_c
71bc95ed
SM
543 && m_subfiles != nullptr
544 && (m_subfiles->language == language_cplus
545 || m_subfiles->language == language_fortran))
546 subfile->language = m_subfiles->language;
547
548 /* Link this subfile at the front of the subfile list. */
549 subfile->next = m_subfiles;
550 m_subfiles = subfile.release ();
c906108c
SS
551}
552
553/* For stabs readers, the first N_SO symbol is assumed to be the
554 source file name, and the subfile struct is initialized using that
555 assumption. If another N_SO symbol is later seen, immediately
556 following the first one, then the first one is assumed to be the
557 directory name and the second one is really the source file name.
558
559 So we have to patch up the subfile struct by moving the old name
560 value to dirname and remembering the new name. Some sanity
561 checking is performed to ensure that the state of the subfile
562 struct is reasonable and that the old name we are assuming to be a
4a64f543 563 directory name actually is (by checking for a trailing '/'). */
c906108c
SS
564
565void
4a2125f5
TT
566buildsym_compunit::patch_subfile_names (struct subfile *subfile,
567 const char *name)
c906108c 568{
43f3e411 569 if (subfile != NULL
ebd4e6d0
SM
570 && m_comp_dir.empty ()
571 && !subfile->name.empty ()
572 && IS_DIR_SEPARATOR (subfile->name.back ()))
c906108c 573 {
ebd4e6d0
SM
574 m_comp_dir = std::move (subfile->name);
575 subfile->name = name;
f71ad555 576 subfile->name_for_id = name;
46212e0b 577 set_last_source_file (name);
c906108c
SS
578
579 /* Default the source language to whatever can be deduced from
dda83cd7
SM
580 the filename. If nothing can be deduced (such as for a C/C++
581 include file with a ".h" extension), then inherit whatever
582 language the previous subfile had. This kludgery is
583 necessary because there is no standard way in some object
584 formats to record the source language. Also, when symtabs
585 are allocated we try to deduce a language then as well, but
586 it is too late for us to use that information while reading
587 symbols, since symtabs aren't allocated until after all the
588 symbols have been processed for a given source file. */
c906108c 589
ebd4e6d0
SM
590 subfile->language
591 = deduce_language_from_filename (subfile->name.c_str ());
5aafa1cc
PM
592 if (subfile->language == language_unknown
593 && subfile->next != NULL)
c906108c
SS
594 {
595 subfile->language = subfile->next->language;
596 }
597 }
598}
599\f
600/* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
601 switching source files (different subfiles, as we call them) within
602 one object file, but using a stack rather than in an arbitrary
603 order. */
604
605void
4a2125f5 606buildsym_compunit::push_subfile ()
c906108c 607{
4a2125f5 608 gdb_assert (m_current_subfile != NULL);
ebd4e6d0
SM
609 gdb_assert (!m_current_subfile->name.empty ());
610 m_subfile_stack.push_back (m_current_subfile->name.c_str ());
c906108c
SS
611}
612
8419ee53 613const char *
4a2125f5 614buildsym_compunit::pop_subfile ()
c906108c 615{
4a2125f5
TT
616 gdb_assert (!m_subfile_stack.empty ());
617 const char *name = m_subfile_stack.back ();
618 m_subfile_stack.pop_back ();
8419ee53 619 return name;
c906108c
SS
620}
621\f
622/* Add a linetable entry for line number LINE and address PC to the
623 line vector for SUBFILE. */
624
625void
4a2125f5 626buildsym_compunit::record_line (struct subfile *subfile, int line,
48e0f38c 627 unrelocated_addr pc, linetable_entry_flags flags)
c906108c 628{
558802e4 629 m_have_line_numbers = true;
c906108c 630
a25198bb
BE
631 /* Normally, we treat lines as unsorted. But the end of sequence
632 marker is special. We sort line markers at the same PC by line
633 number, so end of sequence markers (which have line == 0) appear
634 first. This is right if the marker ends the previous function,
635 and there is no padding before the next function. But it is
636 wrong if the previous line was empty and we are now marking a
637 switch to a different subfile. We must leave the end of sequence
638 marker at the end of this group of lines, not sort the empty line
639 to after the marker. The easiest way to accomplish this is to
640 delete any empty lines from our table, if they are followed by
641 end of sequence markers. All we lose is the ability to set
642 breakpoints at some lines which contain no instructions
643 anyway. */
644 if (line == 0)
607ae575 645 {
6b09f134 646 std::optional<int> last_line;
558802e4
SM
647
648 while (!subfile->line_vector_entries.empty ())
607ae575 649 {
558802e4
SM
650 linetable_entry *last = &subfile->line_vector_entries.back ();
651 last_line = last->line;
652
0434c3ef 653 if (last->unrelocated_pc () != pc)
64dc2d4b 654 break;
558802e4
SM
655
656 subfile->line_vector_entries.pop_back ();
607ae575 657 }
e4ad960a
TV
658
659 /* Ignore an end-of-sequence marker marking an empty sequence. */
558802e4 660 if (!last_line.has_value () || *last_line == 0)
e4ad960a 661 return;
607ae575
DJ
662 }
663
558802e4
SM
664 subfile->line_vector_entries.emplace_back ();
665 linetable_entry &e = subfile->line_vector_entries.back ();
666 e.line = line;
667 e.is_stmt = (flags & LEF_IS_STMT) != 0;
0434c3ef 668 e.set_unrelocated_pc (pc);
558802e4 669 e.prologue_end = (flags & LEF_PROLOGUE_END) != 0;
c906108c
SS
670}
671
c906108c 672\f
59dfe8ad 673/* Subroutine of end_compunit_symtab to simplify it. Look for a subfile that
4a64f543
MS
674 matches the main source file's basename. If there is only one, and
675 if the main source file doesn't have any symbol or line number
676 information, then copy this file's symtab and line_vector to the
677 main source file's subfile and discard the other subfile. This can
678 happen because of a compiler bug or from the user playing games
679 with #line or from things like a distributed build system that
43f3e411
DE
680 manipulates the debug info. This can also happen from an innocent
681 symlink in the paths, we don't canonicalize paths here. */
4584e32e 682
4a2125f5
TT
683void
684buildsym_compunit::watch_main_source_file_lossage ()
4584e32e 685{
43f3e411 686 struct subfile *mainsub, *subfile;
4584e32e 687
43f3e411 688 /* Get the main source file. */
cbb09508 689 mainsub = m_main_subfile;
43f3e411 690
4a64f543 691 /* If the main source file doesn't have any line number or symbol
7bab9b58 692 info, look for an alias in another subfile. */
4584e32e 693
558802e4 694 if (mainsub->line_vector_entries.empty ()
43f3e411 695 && mainsub->symtab == NULL)
4584e32e 696 {
ebd4e6d0 697 const char *mainbase = lbasename (mainsub->name.c_str ());
4584e32e
DE
698 int nr_matches = 0;
699 struct subfile *prevsub;
700 struct subfile *mainsub_alias = NULL;
701 struct subfile *prev_mainsub_alias = NULL;
702
703 prevsub = NULL;
cbb09508 704 for (subfile = m_subfiles;
43f3e411 705 subfile != NULL;
4584e32e
DE
706 subfile = subfile->next)
707 {
43f3e411
DE
708 if (subfile == mainsub)
709 continue;
ebd4e6d0 710 if (filename_cmp (lbasename (subfile->name.c_str ()), mainbase) == 0)
4584e32e
DE
711 {
712 ++nr_matches;
713 mainsub_alias = subfile;
714 prev_mainsub_alias = prevsub;
715 }
716 prevsub = subfile;
717 }
718
719 if (nr_matches == 1)
720 {
43f3e411 721 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
4584e32e
DE
722
723 /* Found a match for the main source file.
724 Copy its line_vector and symtab to the main subfile
725 and then discard it. */
726
80affb9f
SM
727 symtab_create_debug_printf ("using subfile %s as the main subfile",
728 mainsub_alias->name.c_str ());
729
558802e4
SM
730 mainsub->line_vector_entries
731 = std::move (mainsub_alias->line_vector_entries);
43f3e411 732 mainsub->symtab = mainsub_alias->symtab;
4584e32e
DE
733
734 if (prev_mainsub_alias == NULL)
cbb09508 735 m_subfiles = mainsub_alias->next;
4584e32e
DE
736 else
737 prev_mainsub_alias->next = mainsub_alias->next;
71bc95ed
SM
738
739 delete mainsub_alias;
4584e32e
DE
740 }
741 }
742}
743
59dfe8ad
SM
744/* Implementation of the first part of end_compunit_symtab. It allows modifying
745 STATIC_BLOCK before it gets finalized by
746 end_compunit_symtab_from_static_block. If the returned value is NULL there
747 is no blockvector created for this symtab (you still must call
748 end_compunit_symtab_from_static_block).
c906108c 749
59dfe8ad
SM
750 END_ADDR is the same as for end_compunit_symtab: the address of the end of
751 the file's text.
c906108c 752
4359dff1 753 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
36586728
TT
754 expandable.
755
756 If REQUIRED is non-zero, then a symtab is created even if it does
757 not contain any symbols. */
6d30eef8 758
4359dff1 759struct block *
59dfe8ad
SM
760buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr,
761 int expandable,
762 int required)
c906108c 763{
c906108c
SS
764 /* Finish the lexical context of the last function in the file; pop
765 the context stack. */
766
4a2125f5 767 if (!m_context_stack.empty ())
c906108c 768 {
a60f3166 769 struct context_stack cstk = pop_context ();
4359dff1 770
c906108c 771 /* Make a block for the local symbols within. */
c233e9c6 772 finish_block (cstk.name, cstk.old_blocks, NULL,
a60f3166 773 cstk.start_addr, end_addr);
c906108c 774
4a2125f5 775 if (!m_context_stack.empty ())
c906108c
SS
776 {
777 /* This is said to happen with SCO. The old coffread.c
778 code simply emptied the context stack, so we do the
779 same. FIXME: Find out why it is happening. This is not
780 believed to happen in most cases (even for coffread.c);
781 it used to be an abort(). */
59dfe8ad 782 complaint (_("Context stack not empty in end_compunit_symtab"));
4a2125f5 783 m_context_stack.clear ();
c906108c
SS
784 }
785 }
786
100e3935
TT
787 /* Executables may have out of order pending blocks; sort the
788 pending blocks. */
789 if (m_pending_blocks != nullptr)
c906108c 790 {
07e7f39f 791 struct pending_block *pb;
c906108c 792
b05628f0 793 std::vector<block *> barray;
c906108c 794
4a2125f5 795 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
b05628f0 796 barray.push_back (pb->block);
07e7f39f 797
5033013f
UW
798 /* Sort blocks by start address in descending order. Blocks with the
799 same start address must remain in the original order to preserve
800 inline function caller/callee relationships. */
801 std::stable_sort (barray.begin (), barray.end (),
802 [] (const block *a, const block *b)
803 {
4b8791e1 804 return a->start () > b->start ();
5033013f 805 });
07e7f39f 806
b05628f0 807 int i = 0;
4a2125f5 808 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
b05628f0 809 pb->block = barray[i++];
c906108c
SS
810 }
811
812 /* Cleanup any undefined types that have been left hanging around
813 (this needs to be done before the finish_blocks so that
814 file_symbols is still good).
c5aa993b 815
0a0edcd5 816 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
c906108c
SS
817 specific, but harmless for other symbol readers, since on gdb
818 startup or when finished reading stabs, the state is set so these
819 are no-ops. FIXME: Is this handled right in case of QUIT? Can
820 we make this cleaner? */
821
cbb09508
KS
822 cleanup_undefined_stabs_types (m_objfile);
823 finish_global_stabs (m_objfile);
c906108c 824
36586728 825 if (!required
4a2125f5
TT
826 && m_pending_blocks == NULL
827 && m_file_symbols == NULL
828 && m_global_symbols == NULL
829 && !m_have_line_numbers
830 && m_pending_macros == NULL
831 && m_global_using_directives == NULL)
c906108c 832 {
4359dff1
JK
833 /* Ignore symtabs that have no functions with real debugging info. */
834 return NULL;
835 }
836 else
837 {
838 /* Define the STATIC_BLOCK. */
e148f09d 839 return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
4a2125f5 840 m_last_source_start_addr,
2c99ee5c 841 end_addr, 0, expandable);
4359dff1
JK
842 }
843}
844
59dfe8ad 845/* Subroutine of end_compunit_symtab_from_static_block to simplify it.
7bab9b58 846 Handle the "have blockvector" case.
59dfe8ad
SM
847 See end_compunit_symtab_from_static_block for a description of the
848 arguments. */
7bab9b58 849
4a2125f5 850struct compunit_symtab *
59dfe8ad 851buildsym_compunit::end_compunit_symtab_with_blockvector
83bad316 852 (struct block *static_block, int expandable)
4359dff1 853{
cbb09508 854 struct compunit_symtab *cu = m_compunit_symtab;
4359dff1
JK
855 struct blockvector *blockvector;
856 struct subfile *subfile;
7bab9b58 857 CORE_ADDR end_addr;
4359dff1 858
7bab9b58 859 gdb_assert (static_block != NULL);
cbb09508 860 gdb_assert (m_subfiles != NULL);
7bab9b58 861
4b8791e1 862 end_addr = static_block->end ();
7bab9b58
DE
863
864 /* Create the GLOBAL_BLOCK and build the blockvector. */
e148f09d 865 finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
4a2125f5 866 m_last_source_start_addr, end_addr,
7bab9b58 867 1, expandable);
43f3e411 868 blockvector = make_blockvector ();
c906108c 869
f56ce883
DE
870 /* Read the line table if it has to be read separately.
871 This is only used by xcoffread.c. */
cbb09508
KS
872 if (m_objfile->sf->sym_read_linetable != NULL)
873 m_objfile->sf->sym_read_linetable (m_objfile);
c906108c 874
4584e32e
DE
875 /* Handle the case where the debug info specifies a different path
876 for the main source file. It can cause us to lose track of its
877 line number information. */
878 watch_main_source_file_lossage ();
879
43f3e411
DE
880 /* Now create the symtab objects proper, if not already done,
881 one for each subfile. */
c906108c 882
cbb09508 883 for (subfile = m_subfiles;
43f3e411
DE
884 subfile != NULL;
885 subfile = subfile->next)
c906108c 886 {
558802e4 887 if (!subfile->line_vector_entries.empty ())
c906108c 888 {
100e3935
TT
889 /* Like the pending blocks, the line table may be scrambled
890 in reordered executables. Sort it. It is important to
891 preserve the order of lines at the same address, as this
892 maintains the inline function caller/callee
3d92a3e3 893 relationships, this is why std::stable_sort is used. */
100e3935 894 std::stable_sort (subfile->line_vector_entries.begin (),
6e6ac32d 895 subfile->line_vector_entries.end ());
7bab9b58 896 }
9182c5bc 897
7bab9b58
DE
898 /* Allocate a symbol table if necessary. */
899 if (subfile->symtab == NULL)
f71ad555
SM
900 subfile->symtab = allocate_symtab (cu, subfile->name.c_str (),
901 subfile->name_for_id.c_str ());
ebd4e6d0 902
5accd1a0 903 struct symtab *symtab = subfile->symtab;
9182c5bc 904
7bab9b58 905 /* Fill in its components. */
43f3e411 906
558802e4 907 if (!subfile->line_vector_entries.empty ())
7bab9b58 908 {
558802e4
SM
909 /* Reallocate the line table on the objfile obstack. */
910 size_t n_entries = subfile->line_vector_entries.size ();
911 size_t entry_array_size = n_entries * sizeof (struct linetable_entry);
912 int linetablesize = sizeof (struct linetable) + entry_array_size;
913
977a0c16
TT
914 struct linetable *new_table
915 = XOBNEWVAR (&m_objfile->objfile_obstack, struct linetable,
916 linetablesize);
558802e4 917
977a0c16
TT
918 new_table->nitems = n_entries;
919 memcpy (new_table->item,
558802e4 920 subfile->line_vector_entries.data (), entry_array_size);
977a0c16
TT
921
922 symtab->set_linetable (new_table);
c906108c 923 }
24be086d 924 else
5b607461 925 symtab->set_linetable (nullptr);
c906108c 926
7bab9b58
DE
927 /* Use whatever language we have been using for this
928 subfile, not the one that was deduced in allocate_symtab
929 from the filename. We already did our own deducing when
930 we created the subfile, and we may have altered our
931 opinion of what language it is from things we found in
932 the symbols. */
1ee2e9f9 933 symtab->set_language (subfile->language);
43f3e411 934 }
c906108c 935
36664835
SM
936 /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
937 cu->set_primary_filetab (m_main_subfile->symtab);
84a146c9 938
0ab9ce85 939 /* Fill out the compunit symtab. */
84a146c9 940
ebd4e6d0 941 if (!m_comp_dir.empty ())
43f3e411
DE
942 {
943 /* Reallocate the dirname on the symbol obstack. */
0d9acb45 944 cu->set_dirname (obstack_strdup (&m_objfile->objfile_obstack,
ebd4e6d0 945 m_comp_dir.c_str ()));
c906108c
SS
946 }
947
43f3e411 948 /* Save the debug format string (if any) in the symtab. */
422f1ea2 949 cu->set_debugformat (m_debugformat);
43f3e411
DE
950
951 /* Similarly for the producer. */
ab5f850e 952 cu->set_producer (m_producer);
43f3e411 953
af39c5c8 954 cu->set_blockvector (blockvector);
7bab9b58 955 {
63d609de 956 struct block *b = blockvector->global_block ();
cb1df416 957
cade9c8a 958 b->set_compunit_symtab (cu);
7bab9b58 959 }
cb1df416 960
10cc645b 961 cu->set_macro_table (release_macros ());
43f3e411 962
7bab9b58
DE
963 /* Default any symbols without a specified symtab to the primary symtab. */
964 {
965 int block_i;
966
43f3e411 967 /* The main source file's symtab. */
510860f2 968 struct symtab *symtab = cu->primary_filetab ();
43f3e411 969
63d609de 970 for (block_i = 0; block_i < blockvector->num_blocks (); block_i++)
7bab9b58 971 {
63d609de 972 struct block *block = blockvector->block (block_i);
7bab9b58
DE
973
974 /* Inlined functions may have symbols not in the global or
975 static symbol lists. */
6c00f721
SM
976 if (block->function () != nullptr
977 && block->function ()->symtab () == nullptr)
978 block->function ()->set_symtab (symtab);
7bab9b58
DE
979
980 /* Note that we only want to fix up symbols from the local
8e8d48f9
TT
981 blocks, not blocks coming from included symtabs. That is
982 why we use an mdict iterator here and not a block
983 iterator. */
984 for (struct symbol *sym : block->multidict_symbols ())
4206d69e
TT
985 if (sym->symtab () == NULL)
986 sym->set_symtab (symtab);
7bab9b58
DE
987 }
988 }
edb3359d 989
43f3e411 990 add_compunit_symtab_to_objfile (cu);
43f3e411
DE
991
992 return cu;
7bab9b58
DE
993}
994
59dfe8ad
SM
995/* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
996 as value returned by end_compunit_symtab_get_static_block.
7bab9b58 997
7bab9b58
DE
998 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
999 expandable. */
1000
43f3e411 1001struct compunit_symtab *
59dfe8ad 1002buildsym_compunit::end_compunit_symtab_from_static_block
83bad316 1003 (struct block *static_block, int expandable)
7bab9b58 1004{
43f3e411 1005 struct compunit_symtab *cu;
7bab9b58
DE
1006
1007 if (static_block == NULL)
1008 {
0ab9ce85
DE
1009 /* Handle the "no blockvector" case.
1010 When this happens there is nothing to record, so there's nothing
1011 to do: memory will be freed up later.
1012
1013 Note: We won't be adding a compunit to the objfile's list of
1014 compunits, so there's nothing to unchain. However, since each symtab
1015 is added to the objfile's obstack we can't free that space.
1016 We could do better, but this is believed to be a sufficiently rare
1017 event. */
43f3e411 1018 cu = NULL;
7bab9b58
DE
1019 }
1020 else
83bad316 1021 cu = end_compunit_symtab_with_blockvector (static_block, expandable);
cb1df416 1022
43f3e411 1023 return cu;
6d30eef8
DE
1024}
1025
4359dff1
JK
1026/* Finish the symbol definitions for one main source file, close off
1027 all the lexical contexts for that file (creating struct block's for
1028 them), then make the struct symtab for that file and put it in the
1029 list of all such.
1030
83bad316 1031 END_ADDR is the address of the end of the file's text.
4359dff1 1032
59dfe8ad 1033 Note that it is possible for end_compunit_symtab() to return NULL. In
4359dff1
JK
1034 particular, for the DWARF case at least, it will return NULL when
1035 it finds a compilation unit that has exactly one DIE, a
1036 TAG_compile_unit DIE. This can happen when we link in an object
1037 file that was compiled from an empty source file. Returning NULL
1038 is probably not the correct thing to do, because then gdb will
1039 never know about this empty file (FIXME).
1040
1041 If you need to modify STATIC_BLOCK before it is finalized you should
59dfe8ad
SM
1042 call end_compunit_symtab_get_static_block and
1043 end_compunit_symtab_from_static_block yourself. */
6d30eef8 1044
43f3e411 1045struct compunit_symtab *
83bad316 1046buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr)
6d30eef8 1047{
4359dff1
JK
1048 struct block *static_block;
1049
59dfe8ad 1050 static_block = end_compunit_symtab_get_static_block (end_addr, 0, 0);
83bad316 1051 return end_compunit_symtab_from_static_block (static_block, 0);
6d30eef8
DE
1052}
1053
59dfe8ad
SM
1054/* Same as end_compunit_symtab except create a symtab that can be later added
1055 to. */
6d30eef8 1056
43f3e411 1057struct compunit_symtab *
83bad316 1058buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr)
6d30eef8 1059{
4359dff1
JK
1060 struct block *static_block;
1061
59dfe8ad 1062 static_block = end_compunit_symtab_get_static_block (end_addr, 1, 0);
83bad316 1063 return end_compunit_symtab_from_static_block (static_block, 1);
6d30eef8
DE
1064}
1065
1066/* Subroutine of augment_type_symtab to simplify it.
43f3e411
DE
1067 Attach the main source file's symtab to all symbols in PENDING_LIST that
1068 don't have one. */
6d30eef8
DE
1069
1070static void
43f3e411
DE
1071set_missing_symtab (struct pending *pending_list,
1072 struct compunit_symtab *cu)
6d30eef8
DE
1073{
1074 struct pending *pending;
1075 int i;
1076
1077 for (pending = pending_list; pending != NULL; pending = pending->next)
801e3a5b 1078 {
6d30eef8
DE
1079 for (i = 0; i < pending->nsyms; ++i)
1080 {
4206d69e
TT
1081 if (pending->symbol[i]->symtab () == NULL)
1082 pending->symbol[i]->set_symtab (cu->primary_filetab ());
6d30eef8 1083 }
801e3a5b 1084 }
6d30eef8 1085}
c906108c 1086
59dfe8ad 1087/* Same as end_compunit_symtab, but for the case where we're adding more symbols
6d30eef8
DE
1088 to an existing symtab that is known to contain only type information.
1089 This is the case for DWARF4 Type Units. */
1090
1091void
4a2125f5 1092buildsym_compunit::augment_type_symtab ()
6d30eef8 1093{
cbb09508 1094 struct compunit_symtab *cust = m_compunit_symtab;
63d609de 1095 struct blockvector *blockvector = cust->blockvector ();
6d30eef8 1096
4a2125f5 1097 if (!m_context_stack.empty ())
a60f3166 1098 complaint (_("Context stack not empty in augment_type_symtab"));
4a2125f5 1099 if (m_pending_blocks != NULL)
b98664d3 1100 complaint (_("Blocks in a type symtab"));
4a2125f5 1101 if (m_pending_macros != NULL)
b98664d3 1102 complaint (_("Macro in a type symtab"));
4a2125f5 1103 if (m_have_line_numbers)
b98664d3 1104 complaint (_("Line numbers recorded in a type symtab"));
6d30eef8 1105
4a2125f5 1106 if (m_file_symbols != NULL)
6d30eef8 1107 {
63d609de 1108 struct block *block = blockvector->static_block ();
6d30eef8
DE
1109
1110 /* First mark any symbols without a specified symtab as belonging
1111 to the primary symtab. */
4a2125f5 1112 set_missing_symtab (m_file_symbols, cust);
6d30eef8 1113
24d74bb5 1114 mdict_add_pending (block->multidict (), m_file_symbols);
6d30eef8
DE
1115 }
1116
4a2125f5 1117 if (m_global_symbols != NULL)
6d30eef8 1118 {
63d609de 1119 struct block *block = blockvector->global_block ();
6d30eef8
DE
1120
1121 /* First mark any symbols without a specified symtab as belonging
1122 to the primary symtab. */
4a2125f5 1123 set_missing_symtab (m_global_symbols, cust);
6d30eef8 1124
24d74bb5 1125 mdict_add_pending (block->multidict (), m_global_symbols);
6d30eef8 1126 }
c906108c
SS
1127}
1128
1129/* Push a context block. Args are an identifying nesting level
1130 (checkable when you pop it), and the starting PC address of this
1131 context. */
1132
1133struct context_stack *
4a2125f5 1134buildsym_compunit::push_context (int desc, CORE_ADDR valu)
c906108c 1135{
4a2125f5
TT
1136 m_context_stack.emplace_back ();
1137 struct context_stack *newobj = &m_context_stack.back ();
c906108c 1138
fe978cb0 1139 newobj->depth = desc;
4a2125f5
TT
1140 newobj->locals = m_local_symbols;
1141 newobj->old_blocks = m_pending_blocks;
fe978cb0 1142 newobj->start_addr = valu;
4a2125f5 1143 newobj->local_using_directives = m_local_using_directives;
fe978cb0 1144 newobj->name = NULL;
c906108c 1145
4a2125f5
TT
1146 m_local_symbols = NULL;
1147 m_local_using_directives = NULL;
c906108c 1148
fe978cb0 1149 return newobj;
c906108c 1150}
0c5e171a 1151
a672ef13 1152/* Pop a context block. Returns the address of the context block just
4a64f543 1153 popped. */
a672ef13 1154
a60f3166 1155struct context_stack
4a2125f5 1156buildsym_compunit::pop_context ()
0c5e171a 1157{
4a2125f5
TT
1158 gdb_assert (!m_context_stack.empty ());
1159 struct context_stack result = m_context_stack.back ();
1160 m_context_stack.pop_back ();
a60f3166 1161 return result;
0c5e171a 1162}