]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/buildsym.c
gdb sim testing, set gdb_protocol to "sim"
[thirdparty/binutils-gdb.git] / gdb / buildsym.c
1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "buildsym-legacy.h"
20 #include "bfd.h"
21 #include "gdbsupport/gdb_obstack.h"
22 #include "gdbsupport/pathstuff.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "gdbtypes.h"
27 #include "complaints.h"
28 #include "expression.h"
29 #include "filenames.h"
30 #include "macrotab.h"
31 #include "demangle.h"
32 #include "block.h"
33 #include "cp-support.h"
34 #include "dictionary.h"
35 #include <algorithm>
36
37 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
38 questionable--see comment where we call them). */
39
40 #include "stabsread.h"
41
42 /* List of blocks already made (lexical contexts already closed).
43 This is used at the end to make the blockvector. */
44
45 struct pending_block
46 {
47 struct pending_block *next;
48 struct block *block;
49 };
50
51 buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
52 const char *name,
53 const char *comp_dir_,
54 const char *name_for_id,
55 enum language language_,
56 CORE_ADDR last_addr)
57 : m_objfile (objfile_),
58 m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
59 m_comp_dir (comp_dir_ == nullptr ? "" : comp_dir_),
60 m_language (language_),
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. */
65 m_compunit_symtab = allocate_compunit_symtab (m_objfile, name);
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. */
73 start_subfile (name, name_for_id);
74 /* Save this so that we don't have to go looking for it at the end
75 of the subfiles list. */
76 m_main_subfile = m_current_subfile;
77 }
78
79 buildsym_compunit::~buildsym_compunit ()
80 {
81 struct subfile *subfile, *nextsub;
82
83 if (m_pending_macros != nullptr)
84 free_macro_table (m_pending_macros);
85
86 for (subfile = m_subfiles;
87 subfile != NULL;
88 subfile = nextsub)
89 {
90 nextsub = subfile->next;
91 delete subfile;
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
109 struct macro_table *
110 buildsym_compunit::get_macro_table ()
111 {
112 if (m_pending_macros == nullptr)
113 m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
114 &m_objfile->per_bfd->string_cache,
115 m_compunit_symtab);
116 return m_pending_macros;
117 }
118
119 /* Maintain the lists of symbols and blocks. */
120
121 /* Add a symbol to one of the lists of symbols. */
122
123 void
124 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
125 {
126 struct pending *link;
127
128 /* If this is an alias for another symbol, don't add it. */
129 if (symbol->linkage_name () && symbol->linkage_name ()[0] == '#')
130 return;
131
132 /* We keep PENDINGSIZE symbols in each link of the list. If we
133 don't have a link with room in it, add a new link. */
134 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
135 {
136 link = XNEW (struct pending);
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
148 struct symbol *
149 find_symbol_in_list (struct pending *list, char *name, int length)
150 {
151 int j;
152 const char *pp;
153
154 while (list != NULL)
155 {
156 for (j = list->nsyms; --j >= 0;)
157 {
158 pp = list->symbol[j]->linkage_name ();
159 if (*pp == *name && strncmp (pp, name, length) == 0
160 && pp[length] == '\0')
161 {
162 return (list->symbol[j]);
163 }
164 }
165 list = list->next;
166 }
167 return (NULL);
168 }
169
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
174 void
175 buildsym_compunit::record_pending_block (struct block *block,
176 struct pending_block *opblock)
177 {
178 struct pending_block *pblock;
179
180 pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
181 pblock->block = block;
182 if (opblock)
183 {
184 pblock->next = opblock->next;
185 opblock->next = pblock;
186 }
187 else
188 {
189 pblock->next = m_pending_blocks;
190 m_pending_blocks = pblock;
191 }
192 }
193
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
198 struct block *
199 buildsym_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)
206 {
207 struct gdbarch *gdbarch = m_objfile->arch ();
208 struct pending *next, *next1;
209 struct block *block;
210 struct pending_block *pblock;
211 struct pending_block *opblock;
212
213 if (is_global)
214 block = new (&m_objfile->objfile_obstack) global_block;
215 else
216 block = new (&m_objfile->objfile_obstack) struct block;
217
218 if (symbol)
219 {
220 block->set_multidict
221 (mdict_create_linear (&m_objfile->objfile_obstack, *listhead));
222 }
223 else
224 {
225 if (expandable)
226 {
227 block->set_multidict
228 (mdict_create_hashed_expandable (m_language));
229 mdict_add_pending (block->multidict (), *listhead);
230 }
231 else
232 {
233 block->set_multidict
234 (mdict_create_hashed (&m_objfile->objfile_obstack, *listhead));
235 }
236 }
237
238 block->set_start (start);
239 block->set_end (end);
240
241 /* Put the block in as the value of the symbol that names it. */
242
243 if (symbol)
244 {
245 struct type *ftype = symbol->type ();
246 symbol->set_value_block (block);
247 symbol->set_section_index (SECT_OFF_TEXT (m_objfile));
248 block->set_function (symbol);
249
250 if (ftype->num_fields () <= 0)
251 {
252 /* No parameter type information is recorded with the
253 function's type. Set that from the type of the
254 parameter symbols. */
255 int nparams = 0, iparams;
256
257 /* Here we want to directly access the dictionary, because
258 we haven't fully initialized the block yet. */
259 for (struct symbol *sym : block->multidict_symbols ())
260 {
261 if (sym->is_argument ())
262 nparams++;
263 }
264 if (nparams > 0)
265 {
266 ftype->alloc_fields (nparams);
267
268 iparams = 0;
269 /* Here we want to directly access the dictionary, because
270 we haven't fully initialized the block yet. */
271 for (struct symbol *sym : block->multidict_symbols ())
272 {
273 if (iparams == nparams)
274 break;
275
276 if (sym->is_argument ())
277 {
278 ftype->field (iparams).set_type (sym->type ());
279 ftype->field (iparams).set_is_artificial (false);
280 iparams++;
281 }
282 }
283 }
284 }
285 }
286 else
287 block->set_function (nullptr);
288
289 if (static_link != NULL)
290 objfile_register_static_link (m_objfile, block, static_link);
291
292 /* Now free the links of the list, and empty the list. */
293
294 for (next = *listhead; next; next = next1)
295 {
296 next1 = next->next;
297 xfree (next);
298 }
299 *listhead = NULL;
300
301 /* Check to be sure that the blocks have an end address that is
302 greater than starting address. */
303
304 if (block->end () < block->start ())
305 {
306 if (symbol)
307 {
308 complaint (_("block end address less than block "
309 "start address in %s (patched it)"),
310 symbol->print_name ());
311 }
312 else
313 {
314 complaint (_("block end address %s less than block "
315 "start address %s (patched it)"),
316 paddress (gdbarch, block->end ()),
317 paddress (gdbarch, block->start ()));
318 }
319 /* Better than nothing. */
320 block->set_end (block->start ());
321 }
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;
327 for (pblock = m_pending_blocks;
328 pblock && pblock != old_blocks;
329 pblock = pblock->next)
330 {
331 if (pblock->block->superblock () == NULL)
332 {
333 /* Check to be sure the blocks are nested as we receive
334 them. If the compiler/assembler/linker work, this just
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. */
340 if (pblock->block->function () == NULL
341 && (pblock->block->start () < block->start ()
342 || pblock->block->end () > block->end ()))
343 {
344 if (symbol)
345 {
346 complaint (_("inner block not inside outer block in %s"),
347 symbol->print_name ());
348 }
349 else
350 {
351 complaint (_("inner block (%s-%s) not "
352 "inside outer block (%s-%s)"),
353 paddress (gdbarch, pblock->block->start ()),
354 paddress (gdbarch, pblock->block->end ()),
355 paddress (gdbarch, block->start ()),
356 paddress (gdbarch, block->end ()));
357 }
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 ());
364 }
365 pblock->block->set_superblock (block);
366 }
367 opblock = pblock;
368 }
369
370 block->set_using ((is_global
371 ? m_global_using_directives
372 : m_local_using_directives),
373 &m_objfile->objfile_obstack);
374 if (is_global)
375 m_global_using_directives = NULL;
376 else
377 m_local_using_directives = NULL;
378
379 record_pending_block (block, opblock);
380
381 return block;
382 }
383
384 struct block *
385 buildsym_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)
389 {
390 return finish_block_internal (symbol, &m_local_symbols,
391 old_blocks, static_link, start, end, 0, 0);
392 }
393
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. */
402 void
403 buildsym_compunit::record_block_range (struct block *block,
404 CORE_ADDR start,
405 CORE_ADDR end_inclusive)
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. */
412 if (start != block->start ()
413 || end_inclusive + 1 != block->end ())
414 m_pending_addrmap_interesting = true;
415
416 m_pending_addrmap.set_empty (start, end_inclusive, block);
417 }
418
419 struct blockvector *
420 buildsym_compunit::make_blockvector ()
421 {
422 struct pending_block *next;
423 struct blockvector *blockvector;
424 int i;
425
426 /* Count the length of the list of blocks. */
427
428 for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
429 {
430 }
431
432 blockvector = (struct blockvector *)
433 obstack_alloc (&m_objfile->objfile_obstack,
434 (sizeof (struct blockvector)
435 + (i - 1) * sizeof (struct block *)));
436
437 /* Copy the blocks into the blockvector. This is done in reverse
438 order, which happens to put the blocks into the proper order
439 (ascending starting address). finish_block has hair to insert
440 each block into the list after its subblocks in order to make
441 sure this is true. */
442
443 blockvector->set_num_blocks (i);
444 for (next = m_pending_blocks; next; next = next->next)
445 blockvector->set_block (--i, next->block);
446
447 free_pending_blocks ();
448
449 /* If we needed an address map for this symtab, record it in the
450 blockvector. */
451 if (m_pending_addrmap_interesting)
452 blockvector->set_map
453 (new (&m_objfile->objfile_obstack) addrmap_fixed
454 (&m_objfile->objfile_obstack, &m_pending_addrmap));
455 else
456 blockvector->set_map (nullptr);
457
458 /* Some compilers output blocks in the wrong order, but we depend on
459 their being in the right order so we can binary search. Check the
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. */
464 if (blockvector->num_blocks () > 1)
465 {
466 for (i = 1; i < blockvector->num_blocks (); i++)
467 {
468 if (blockvector->block (i - 1)->start ()
469 > blockvector->block (i)->start ())
470 {
471 CORE_ADDR start
472 = blockvector->block (i)->start ();
473
474 complaint (_("block at %s out of order"),
475 hex_string ((LONGEST) start));
476 }
477 }
478 }
479
480 return (blockvector);
481 }
482
483 /* See buildsym.h. */
484
485 void
486 buildsym_compunit::start_subfile (const char *name, const char *name_for_id)
487 {
488 /* See if this subfile is already registered. */
489
490 symtab_create_debug_printf ("name = %s, name_for_id = %s", name, name_for_id);
491
492 for (subfile *subfile = m_subfiles; subfile; subfile = subfile->next)
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 }
500
501 /* This subfile is not known. Add an entry for it. */
502
503 subfile_up subfile (new struct subfile);
504 subfile->name = name;
505 subfile->name_for_id = name_for_id;
506
507 m_current_subfile = subfile.get ();
508
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
518 source file. */
519
520 subfile->language = deduce_language_from_filename (subfile->name.c_str ());
521 if (subfile->language == language_unknown && m_subfiles != nullptr)
522 subfile->language = m_subfiles->language;
523
524 /* If the filename of this subfile ends in .C, then change the
525 language of any pending subfiles from C to C++. We also accept
526 any other C++ suffixes accepted by deduce_language_from_filename. */
527 /* Likewise for f2c. */
528
529 if (!subfile->name.empty ())
530 {
531 struct subfile *s;
532 language sublang = deduce_language_from_filename (subfile->name.c_str ());
533
534 if (sublang == language_cplus || sublang == language_fortran)
535 for (s = m_subfiles; s != NULL; s = s->next)
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
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 ();
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
562 directory name actually is (by checking for a trailing '/'). */
563
564 void
565 buildsym_compunit::patch_subfile_names (struct subfile *subfile,
566 const char *name)
567 {
568 if (subfile != NULL
569 && m_comp_dir.empty ()
570 && !subfile->name.empty ()
571 && IS_DIR_SEPARATOR (subfile->name.back ()))
572 {
573 m_comp_dir = std::move (subfile->name);
574 subfile->name = name;
575 subfile->name_for_id = name;
576 set_last_source_file (name);
577
578 /* Default the source language to whatever can be deduced from
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. */
588
589 subfile->language
590 = deduce_language_from_filename (subfile->name.c_str ());
591 if (subfile->language == language_unknown
592 && subfile->next != NULL)
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
604 void
605 buildsym_compunit::push_subfile ()
606 {
607 gdb_assert (m_current_subfile != NULL);
608 gdb_assert (!m_current_subfile->name.empty ());
609 m_subfile_stack.push_back (m_current_subfile->name.c_str ());
610 }
611
612 const char *
613 buildsym_compunit::pop_subfile ()
614 {
615 gdb_assert (!m_subfile_stack.empty ());
616 const char *name = m_subfile_stack.back ();
617 m_subfile_stack.pop_back ();
618 return name;
619 }
620 \f
621 /* Add a linetable entry for line number LINE and address PC to the
622 line vector for SUBFILE. */
623
624 void
625 buildsym_compunit::record_line (struct subfile *subfile, int line,
626 unrelocated_addr pc, linetable_entry_flags flags)
627 {
628 m_have_line_numbers = true;
629
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)
644 {
645 std::optional<int> last_line;
646
647 while (!subfile->line_vector_entries.empty ())
648 {
649 linetable_entry *last = &subfile->line_vector_entries.back ();
650 last_line = last->line;
651
652 if (last->unrelocated_pc () != pc)
653 break;
654
655 subfile->line_vector_entries.pop_back ();
656 }
657
658 /* Ignore an end-of-sequence marker marking an empty sequence. */
659 if (!last_line.has_value () || *last_line == 0)
660 return;
661 }
662
663 linetable_entry &e = subfile->line_vector_entries.emplace_back ();
664 e.line = line;
665 e.is_stmt = (flags & LEF_IS_STMT) != 0;
666 e.set_unrelocated_pc (pc);
667 e.prologue_end = (flags & LEF_PROLOGUE_END) != 0;
668 e.epilogue_begin = (flags & LEF_EPILOGUE_BEGIN) != 0;
669 }
670
671 \f
672 /* Subroutine of end_compunit_symtab to simplify it. Look for a subfile that
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
679 manipulates the debug info. This can also happen from an innocent
680 symlink in the paths, we don't canonicalize paths here. */
681
682 void
683 buildsym_compunit::watch_main_source_file_lossage ()
684 {
685 struct subfile *mainsub, *subfile;
686
687 /* Get the main source file. */
688 mainsub = m_main_subfile;
689
690 /* If the main source file doesn't have any line number or symbol
691 info, look for an alias in another subfile. */
692
693 if (mainsub->line_vector_entries.empty ()
694 && mainsub->symtab == NULL)
695 {
696 const char *mainbase = lbasename (mainsub->name.c_str ());
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;
703 for (subfile = m_subfiles;
704 subfile != NULL;
705 subfile = subfile->next)
706 {
707 if (subfile == mainsub)
708 continue;
709 if (filename_cmp (lbasename (subfile->name.c_str ()), mainbase) == 0)
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 {
720 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
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
726 symtab_create_debug_printf ("using subfile %s as the main subfile",
727 mainsub_alias->name.c_str ());
728
729 mainsub->line_vector_entries
730 = std::move (mainsub_alias->line_vector_entries);
731 mainsub->symtab = mainsub_alias->symtab;
732
733 if (prev_mainsub_alias == NULL)
734 m_subfiles = mainsub_alias->next;
735 else
736 prev_mainsub_alias->next = mainsub_alias->next;
737
738 delete mainsub_alias;
739 }
740 }
741 }
742
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).
748
749 END_ADDR is the same as for end_compunit_symtab: the address of the end of
750 the file's text.
751
752 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
753 expandable.
754
755 If REQUIRED is non-zero, then a symtab is created even if it does
756 not contain any symbols. */
757
758 struct block *
759 buildsym_compunit::end_compunit_symtab_get_static_block (CORE_ADDR end_addr,
760 int expandable,
761 int required)
762 {
763 /* Finish the lexical context of the last function in the file; pop
764 the context stack. */
765
766 if (!m_context_stack.empty ())
767 {
768 struct context_stack cstk = pop_context ();
769
770 /* Make a block for the local symbols within. */
771 finish_block (cstk.name, cstk.old_blocks, NULL,
772 cstk.start_addr, end_addr);
773
774 if (!m_context_stack.empty ())
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(). */
781 complaint (_("Context stack not empty in end_compunit_symtab"));
782 m_context_stack.clear ();
783 }
784 }
785
786 /* Executables may have out of order pending blocks; sort the
787 pending blocks. */
788 if (m_pending_blocks != nullptr)
789 {
790 struct pending_block *pb;
791
792 std::vector<block *> barray;
793
794 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
795 barray.push_back (pb->block);
796
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 {
803 return a->start () > b->start ();
804 });
805
806 int i = 0;
807 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
808 pb->block = barray[i++];
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).
814
815 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
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
821 cleanup_undefined_stabs_types (m_objfile);
822 finish_global_stabs (m_objfile);
823
824 if (!required
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)
831 {
832 /* Ignore symtabs that have no functions with real debugging info. */
833 return NULL;
834 }
835 else
836 {
837 /* Define the STATIC_BLOCK. */
838 return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
839 m_last_source_start_addr,
840 end_addr, 0, expandable);
841 }
842 }
843
844 /* Subroutine of end_compunit_symtab_from_static_block to simplify it.
845 Handle the "have blockvector" case.
846 See end_compunit_symtab_from_static_block for a description of the
847 arguments. */
848
849 struct compunit_symtab *
850 buildsym_compunit::end_compunit_symtab_with_blockvector
851 (struct block *static_block, int expandable)
852 {
853 struct compunit_symtab *cu = m_compunit_symtab;
854 struct blockvector *blockvector;
855 struct subfile *subfile;
856 CORE_ADDR end_addr;
857
858 gdb_assert (static_block != NULL);
859 gdb_assert (m_subfiles != NULL);
860
861 end_addr = static_block->end ();
862
863 /* Create the GLOBAL_BLOCK and build the blockvector. */
864 finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
865 m_last_source_start_addr, end_addr,
866 1, expandable);
867 blockvector = make_blockvector ();
868
869 /* Read the line table if it has to be read separately.
870 This is only used by xcoffread.c. */
871 if (m_objfile->sf->sym_read_linetable != NULL)
872 m_objfile->sf->sym_read_linetable (m_objfile);
873
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
879 /* Now create the symtab objects proper, if not already done,
880 one for each subfile. */
881
882 for (subfile = m_subfiles;
883 subfile != NULL;
884 subfile = subfile->next)
885 {
886 if (!subfile->line_vector_entries.empty ())
887 {
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
892 relationships, this is why std::stable_sort is used. */
893 std::stable_sort (subfile->line_vector_entries.begin (),
894 subfile->line_vector_entries.end ());
895 }
896
897 /* Allocate a symbol table if necessary. */
898 if (subfile->symtab == NULL)
899 subfile->symtab = allocate_symtab (cu, subfile->name.c_str (),
900 subfile->name_for_id.c_str ());
901
902 struct symtab *symtab = subfile->symtab;
903
904 /* Fill in its components. */
905
906 if (!subfile->line_vector_entries.empty ())
907 {
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
913 struct linetable *new_table
914 = XOBNEWVAR (&m_objfile->objfile_obstack, struct linetable,
915 linetablesize);
916
917 new_table->nitems = n_entries;
918 memcpy (new_table->item,
919 subfile->line_vector_entries.data (), entry_array_size);
920
921 symtab->set_linetable (new_table);
922 }
923 else
924 symtab->set_linetable (nullptr);
925
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. */
932 symtab->set_language (subfile->language);
933 }
934
935 /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
936 cu->set_primary_filetab (m_main_subfile->symtab);
937
938 /* Fill out the compunit symtab. */
939
940 if (!m_comp_dir.empty ())
941 {
942 /* Reallocate the dirname on the symbol obstack. */
943 cu->set_dirname (obstack_strdup (&m_objfile->objfile_obstack,
944 m_comp_dir.c_str ()));
945 }
946
947 /* Save the debug format string (if any) in the symtab. */
948 cu->set_debugformat (m_debugformat);
949
950 /* Similarly for the producer. */
951 cu->set_producer (m_producer);
952
953 cu->set_blockvector (blockvector);
954 {
955 struct block *b = blockvector->global_block ();
956
957 b->set_compunit_symtab (cu);
958 }
959
960 cu->set_macro_table (release_macros ());
961
962 /* Default any symbols without a specified symtab to the primary symtab. */
963 {
964 int block_i;
965
966 /* The main source file's symtab. */
967 struct symtab *symtab = cu->primary_filetab ();
968
969 for (block_i = 0; block_i < blockvector->num_blocks (); block_i++)
970 {
971 struct block *block = blockvector->block (block_i);
972
973 /* Inlined functions may have symbols not in the global or
974 static symbol lists. */
975 if (block->function () != nullptr
976 && block->function ()->symtab () == nullptr)
977 block->function ()->set_symtab (symtab);
978
979 /* Note that we only want to fix up symbols from the local
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 ())
984 if (sym->symtab () == NULL)
985 sym->set_symtab (symtab);
986 }
987 }
988
989 add_compunit_symtab_to_objfile (cu);
990
991 return cu;
992 }
993
994 /* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
995 as value returned by end_compunit_symtab_get_static_block.
996
997 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
998 expandable. */
999
1000 struct compunit_symtab *
1001 buildsym_compunit::end_compunit_symtab_from_static_block
1002 (struct block *static_block, int expandable)
1003 {
1004 struct compunit_symtab *cu;
1005
1006 if (static_block == NULL)
1007 {
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. */
1017 cu = NULL;
1018 }
1019 else
1020 cu = end_compunit_symtab_with_blockvector (static_block, expandable);
1021
1022 return cu;
1023 }
1024
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
1030 END_ADDR is the address of the end of the file's text.
1031
1032 Note that it is possible for end_compunit_symtab() to return NULL. In
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
1041 call end_compunit_symtab_get_static_block and
1042 end_compunit_symtab_from_static_block yourself. */
1043
1044 struct compunit_symtab *
1045 buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr)
1046 {
1047 struct block *static_block;
1048
1049 static_block = end_compunit_symtab_get_static_block (end_addr, 0, 0);
1050 return end_compunit_symtab_from_static_block (static_block, 0);
1051 }
1052
1053 /* Same as end_compunit_symtab except create a symtab that can be later added
1054 to. */
1055
1056 struct compunit_symtab *
1057 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr)
1058 {
1059 struct block *static_block;
1060
1061 static_block = end_compunit_symtab_get_static_block (end_addr, 1, 0);
1062 return end_compunit_symtab_from_static_block (static_block, 1);
1063 }
1064
1065 /* Subroutine of augment_type_symtab to simplify it.
1066 Attach the main source file's symtab to all symbols in PENDING_LIST that
1067 don't have one. */
1068
1069 static void
1070 set_missing_symtab (struct pending *pending_list,
1071 struct compunit_symtab *cu)
1072 {
1073 struct pending *pending;
1074 int i;
1075
1076 for (pending = pending_list; pending != NULL; pending = pending->next)
1077 {
1078 for (i = 0; i < pending->nsyms; ++i)
1079 {
1080 if (pending->symbol[i]->symtab () == NULL)
1081 pending->symbol[i]->set_symtab (cu->primary_filetab ());
1082 }
1083 }
1084 }
1085
1086 /* Same as end_compunit_symtab, but for the case where we're adding more symbols
1087 to an existing symtab that is known to contain only type information.
1088 This is the case for DWARF4 Type Units. */
1089
1090 void
1091 buildsym_compunit::augment_type_symtab ()
1092 {
1093 struct compunit_symtab *cust = m_compunit_symtab;
1094 struct blockvector *blockvector = cust->blockvector ();
1095
1096 if (!m_context_stack.empty ())
1097 complaint (_("Context stack not empty in augment_type_symtab"));
1098 if (m_pending_blocks != NULL)
1099 complaint (_("Blocks in a type symtab"));
1100 if (m_pending_macros != NULL)
1101 complaint (_("Macro in a type symtab"));
1102 if (m_have_line_numbers)
1103 complaint (_("Line numbers recorded in a type symtab"));
1104
1105 if (m_file_symbols != NULL)
1106 {
1107 struct block *block = blockvector->static_block ();
1108
1109 /* First mark any symbols without a specified symtab as belonging
1110 to the primary symtab. */
1111 set_missing_symtab (m_file_symbols, cust);
1112
1113 mdict_add_pending (block->multidict (), m_file_symbols);
1114 }
1115
1116 if (m_global_symbols != NULL)
1117 {
1118 struct block *block = blockvector->global_block ();
1119
1120 /* First mark any symbols without a specified symtab as belonging
1121 to the primary symtab. */
1122 set_missing_symtab (m_global_symbols, cust);
1123
1124 mdict_add_pending (block->multidict (), m_global_symbols);
1125 }
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
1132 struct context_stack *
1133 buildsym_compunit::push_context (int desc, CORE_ADDR valu)
1134 {
1135 struct context_stack *newobj = &m_context_stack.emplace_back ();
1136
1137 newobj->depth = desc;
1138 newobj->locals = m_local_symbols;
1139 newobj->old_blocks = m_pending_blocks;
1140 newobj->start_addr = valu;
1141 newobj->local_using_directives = m_local_using_directives;
1142 newobj->name = NULL;
1143
1144 m_local_symbols = NULL;
1145 m_local_using_directives = NULL;
1146
1147 return newobj;
1148 }
1149
1150 /* Pop a context block. Returns the address of the context block just
1151 popped. */
1152
1153 struct context_stack
1154 buildsym_compunit::pop_context ()
1155 {
1156 gdb_assert (!m_context_stack.empty ());
1157 struct context_stack result = m_context_stack.back ();
1158 m_context_stack.pop_back ();
1159 return result;
1160 }