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