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