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" /* For "enum exp_opcode" used by... */
30#include "filenames.h" /* For DOSish file names. */
31#include "macrotab.h"
32#include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
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 *
112{
113 if (m_pending_macros == nullptr)
117 return m_pending_macros;
118}
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 block = (is_global
217
218 if (symbol)
219 {
222 }
223 else
224 {
225 if (expandable)
226 {
229 mdict_add_pending (block->multidict (), *listhead);
230 }
231 else
232 {
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 struct mdict_iterator miter;
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 struct symbol *sym;
257
258 /* Here we want to directly access the dictionary, because
259 we haven't fully initialized the block yet. */
260 ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
261 {
262 if (sym->is_argument ())
263 nparams++;
264 }
265 if (nparams > 0)
266 {
267 ftype->set_num_fields (nparams);
268 ftype->set_fields
269 ((struct field *)
270 TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
271
272 iparams = 0;
273 /* Here we want to directly access the dictionary, because
274 we haven't fully initialized the block yet. */
275 ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
276 {
277 if (iparams == nparams)
278 break;
279
280 if (sym->is_argument ())
281 {
282 ftype->field (iparams).set_type (sym->type ());
283 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
284 iparams++;
285 }
286 }
287 }
288 }
289 }
290 else
291 block->set_function (nullptr);
292
293 if (static_link != NULL)
295
296 /* Now free the links of the list, and empty the list. */
297
298 for (next = *listhead; next; next = next1)
299 {
300 next1 = next->next;
301 xfree (next);
302 }
303 *listhead = NULL;
304
305 /* Check to be sure that the blocks have an end address that is
306 greater than starting address. */
307
308 if (block->end () < block->start ())
309 {
310 if (symbol)
311 {
312 complaint (_("block end address less than block "
313 "start address in %s (patched it)"),
314 symbol->print_name ());
315 }
316 else
317 {
318 complaint (_("block end address %s less than block "
319 "start address %s (patched it)"),
320 paddress (gdbarch, block->end ()),
321 paddress (gdbarch, block->start ()));
322 }
323 /* Better than nothing. */
324 block->set_end (block->start ());
325 }
326
327 /* Install this block as the superblock of all blocks made since the
328 start of this scope that don't have superblocks yet. */
329
330 opblock = NULL;
331 for (pblock = m_pending_blocks;
332 pblock && pblock != old_blocks;
333 pblock = pblock->next)
334 {
335 if (pblock->block->superblock () == NULL)
336 {
337 /* Check to be sure the blocks are nested as we receive
338 them. If the compiler/assembler/linker work, this just
339 burns a small amount of time.
340
341 Skip blocks which correspond to a function; they're not
342 physically nested inside this other blocks, only
343 lexically nested. */
344 if (pblock->block->function () == NULL
345 && (pblock->block->start () < block->start ()
346 || pblock->block->end () > block->end ()))
347 {
348 if (symbol)
349 {
350 complaint (_("inner block not inside outer block in %s"),
351 symbol->print_name ());
352 }
353 else
354 {
355 complaint (_("inner block (%s-%s) not "
356 "inside outer block (%s-%s)"),
357 paddress (gdbarch, pblock->block->start ()),
358 paddress (gdbarch, pblock->block->end ()),
360 paddress (gdbarch, block->end ()));
361 }
362
363 if (pblock->block->start () < block->start ())
364 pblock->block->set_start (block->start ());
365
366 if (pblock->block->end () > block->end ())
367 pblock->block->set_end (block->end ());
368 }
369 pblock->block->set_superblock (block);
370 }
371 opblock = pblock;
372 }
373
375 (is_global
379 if (is_global)
381 else
383
384 record_pending_block (block, opblock);
385
386 return block;
387}
388
389struct block *
391 struct pending_block *old_blocks,
392 const struct dynamic_prop *static_link,
393 CORE_ADDR start, CORE_ADDR end)
394{
396 old_blocks, static_link, start, end, 0, 0);
397}
398
399/* Record that the range of addresses from START to END_INCLUSIVE
400 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
401 addresses must be set already. You must apply this function to all
402 BLOCK's children before applying it to BLOCK.
403
404 If a call to this function complicates the picture beyond that
405 already provided by BLOCK_START and BLOCK_END, then we create an
406 address map for the block. */
407void
409 CORE_ADDR start,
410 CORE_ADDR end_inclusive)
411{
412 /* If this is any different from the range recorded in the block's
413 own BLOCK_START and BLOCK_END, then note that the address map has
414 become interesting. Note that even if this block doesn't have
415 any "interesting" ranges, some later block might, so we still
416 need to record this block in the addrmap. */
417 if (start != block->start ()
418 || end_inclusive + 1 != block->end ())
420
421 m_pending_addrmap.set_empty (start, end_inclusive, block);
422}
423
424struct blockvector *
426{
427 struct pending_block *next;
428 struct blockvector *blockvector;
429 int i;
430
431 /* Count the length of the list of blocks. */
432
433 for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
434 {
435 }
436
437 blockvector = (struct blockvector *)
438 obstack_alloc (&m_objfile->objfile_obstack,
439 (sizeof (struct blockvector)
440 + (i - 1) * sizeof (struct block *)));
441
442 /* Copy the blocks into the blockvector. This is done in reverse
443 order, which happens to put the blocks into the proper order
444 (ascending starting address). finish_block has hair to insert
445 each block into the list after its subblocks in order to make
446 sure this is true. */
447
449 for (next = m_pending_blocks; next; next = next->next)
450 blockvector->set_block (--i, next->block);
451
453
454 /* If we needed an address map for this symtab, record it in the
455 blockvector. */
460 else
461 blockvector->set_map (nullptr);
462
463 /* Some compilers output blocks in the wrong order, but we depend on
464 their being in the right order so we can binary search. Check the
465 order and moan about it.
466 Note: Remember that the first two blocks are the global and static
467 blocks. We could special case that fact and begin checking at block 2.
468 To avoid making that assumption we do not. */
469 if (blockvector->num_blocks () > 1)
470 {
471 for (i = 1; i < blockvector->num_blocks (); i++)
472 {
473 if (blockvector->block (i - 1)->start ()
474 > blockvector->block (i)->start ())
475 {
476 CORE_ADDR start
477 = blockvector->block (i)->start ();
478
479 complaint (_("block at %s out of order"),
480 hex_string ((LONGEST) start));
481 }
482 }
483 }
484
485 return (blockvector);
486}
487
488/* See buildsym.h. */
489
490void
491buildsym_compunit::start_subfile (const char *name, const char *name_for_id)
492{
493 /* See if this subfile is already registered. */
494
495 symtab_create_debug_printf ("name = %s, name_for_id = %s", name, name_for_id);
496
498 if (FILENAME_CMP (subfile->name_for_id.c_str (), name_for_id) == 0)
499 {
500 symtab_create_debug_printf ("found existing symtab with name_for_id %s",
501 subfile->name_for_id.c_str ());
503 return;
504 }
505
506 /* This subfile is not known. Add an entry for it. */
507
508 subfile_up subfile (new struct subfile);
509 subfile->name = name;
510 subfile->name_for_id = name_for_id;
511
512 m_current_subfile = subfile.get ();
513
514 /* Default the source language to whatever can be deduced from the
515 filename. If nothing can be deduced (such as for a C/C++ include
516 file with a ".h" extension), then inherit whatever language the
517 previous subfile had. This kludgery is necessary because there
518 is no standard way in some object formats to record the source
519 language. Also, when symtabs are allocated we try to deduce a
520 language then as well, but it is too late for us to use that
521 information while reading symbols, since symtabs aren't allocated
522 until after all the symbols have been processed for a given
523 source file. */
524
526 if (subfile->language == language_unknown && m_subfiles != nullptr)
528
529 /* If the filename of this subfile ends in .C, then change the
530 language of any pending subfiles from C to C++. We also accept
531 any other C++ suffixes accepted by deduce_language_from_filename. */
532 /* Likewise for f2c. */
533
534 if (!subfile->name.empty ())
535 {
536 struct subfile *s;
538
539 if (sublang == language_cplus || sublang == language_fortran)
540 for (s = m_subfiles; s != NULL; s = s->next)
541 if (s->language == language_c)
542 s->language = sublang;
543 }
544
545 /* And patch up this file if necessary. */
547 && m_subfiles != nullptr
551
552 /* Link this subfile at the front of the subfile list. */
554 m_subfiles = subfile.release ();
555}
556
557/* For stabs readers, the first N_SO symbol is assumed to be the
558 source file name, and the subfile struct is initialized using that
559 assumption. If another N_SO symbol is later seen, immediately
560 following the first one, then the first one is assumed to be the
561 directory name and the second one is really the source file name.
562
563 So we have to patch up the subfile struct by moving the old name
564 value to dirname and remembering the new name. Some sanity
565 checking is performed to ensure that the state of the subfile
566 struct is reasonable and that the old name we are assuming to be a
567 directory name actually is (by checking for a trailing '/'). */
568
569void
571 const char *name)
572{
573 if (subfile != NULL
574 && m_comp_dir.empty ()
575 && !subfile->name.empty ()
576 && IS_DIR_SEPARATOR (subfile->name.back ()))
577 {
578 m_comp_dir = std::move (subfile->name);
579 subfile->name = name;
582
583 /* Default the source language to whatever can be deduced from
584 the filename. If nothing can be deduced (such as for a C/C++
585 include file with a ".h" extension), then inherit whatever
586 language the previous subfile had. This kludgery is
587 necessary because there is no standard way in some object
588 formats to record the source language. Also, when symtabs
589 are allocated we try to deduce a language then as well, but
590 it is too late for us to use that information while reading
591 symbols, since symtabs aren't allocated until after all the
592 symbols have been processed for a given source file. */
593
597 && subfile->next != NULL)
598 {
600 }
601 }
602}
603
604/* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
605 switching source files (different subfiles, as we call them) within
606 one object file, but using a stack rather than in an arbitrary
607 order. */
608
609void
611{
612 gdb_assert (m_current_subfile != NULL);
613 gdb_assert (!m_current_subfile->name.empty ());
614 m_subfile_stack.push_back (m_current_subfile->name.c_str ());
615}
616
617const char *
619{
620 gdb_assert (!m_subfile_stack.empty ());
621 const char *name = m_subfile_stack.back ();
622 m_subfile_stack.pop_back ();
623 return name;
624}
625
626/* Add a linetable entry for line number LINE and address PC to the
627 line vector for SUBFILE. */
628
629void
631 CORE_ADDR pc, linetable_entry_flags flags)
632{
633 m_have_line_numbers = true;
634
635 /* Normally, we treat lines as unsorted. But the end of sequence
636 marker is special. We sort line markers at the same PC by line
637 number, so end of sequence markers (which have line == 0) appear
638 first. This is right if the marker ends the previous function,
639 and there is no padding before the next function. But it is
640 wrong if the previous line was empty and we are now marking a
641 switch to a different subfile. We must leave the end of sequence
642 marker at the end of this group of lines, not sort the empty line
643 to after the marker. The easiest way to accomplish this is to
644 delete any empty lines from our table, if they are followed by
645 end of sequence markers. All we lose is the ability to set
646 breakpoints at some lines which contain no instructions
647 anyway. */
648 if (line == 0)
649 {
650 gdb::optional<int> last_line;
651
652 while (!subfile->line_vector_entries.empty ())
653 {
655 last_line = last->line;
656
657 if (last->pc != pc)
658 break;
659
660 subfile->line_vector_entries.pop_back ();
661 }
662
663 /* Ignore an end-of-sequence marker marking an empty sequence. */
664 if (!last_line.has_value () || *last_line == 0)
665 return;
666 }
667
668 subfile->line_vector_entries.emplace_back ();
670 e.line = line;
671 e.is_stmt = (flags & LEF_IS_STMT) != 0;
672 e.pc = pc;
674}
675
676
677/* Subroutine of end_compunit_symtab to simplify it. Look for a subfile that
678 matches the main source file's basename. If there is only one, and
679 if the main source file doesn't have any symbol or line number
680 information, then copy this file's symtab and line_vector to the
681 main source file's subfile and discard the other subfile. This can
682 happen because of a compiler bug or from the user playing games
683 with #line or from things like a distributed build system that
684 manipulates the debug info. This can also happen from an innocent
685 symlink in the paths, we don't canonicalize paths here. */
686
687void
689{
690 struct subfile *mainsub, *subfile;
691
692 /* Get the main source file. */
693 mainsub = m_main_subfile;
694
695 /* If the main source file doesn't have any line number or symbol
696 info, look for an alias in another subfile. */
697
698 if (mainsub->line_vector_entries.empty ()
699 && mainsub->symtab == NULL)
700 {
701 const char *mainbase = lbasename (mainsub->name.c_str ());
702 int nr_matches = 0;
703 struct subfile *prevsub;
704 struct subfile *mainsub_alias = NULL;
705 struct subfile *prev_mainsub_alias = NULL;
706
707 prevsub = NULL;
708 for (subfile = m_subfiles;
709 subfile != NULL;
711 {
712 if (subfile == mainsub)
713 continue;
714 if (filename_cmp (lbasename (subfile->name.c_str ()), mainbase) == 0)
715 {
716 ++nr_matches;
717 mainsub_alias = subfile;
718 prev_mainsub_alias = prevsub;
719 }
720 prevsub = subfile;
721 }
722
723 if (nr_matches == 1)
724 {
725 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
726
727 /* Found a match for the main source file.
728 Copy its line_vector and symtab to the main subfile
729 and then discard it. */
730
731 symtab_create_debug_printf ("using subfile %s as the main subfile",
732 mainsub_alias->name.c_str ());
733
734 mainsub->line_vector_entries
735 = std::move (mainsub_alias->line_vector_entries);
736 mainsub->symtab = mainsub_alias->symtab;
737
738 if (prev_mainsub_alias == NULL)
739 m_subfiles = mainsub_alias->next;
740 else
741 prev_mainsub_alias->next = mainsub_alias->next;
742
743 delete mainsub_alias;
744 }
745 }
746}
747
748/* Implementation of the first part of end_compunit_symtab. It allows modifying
749 STATIC_BLOCK before it gets finalized by
750 end_compunit_symtab_from_static_block. If the returned value is NULL there
751 is no blockvector created for this symtab (you still must call
752 end_compunit_symtab_from_static_block).
753
754 END_ADDR is the same as for end_compunit_symtab: the address of the end of
755 the file's text.
756
757 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
758 expandable.
759
760 If REQUIRED is non-zero, then a symtab is created even if it does
761 not contain any symbols. */
762
763struct block *
765 int expandable,
766 int required)
767{
768 /* Finish the lexical context of the last function in the file; pop
769 the context stack. */
770
771 if (!m_context_stack.empty ())
772 {
773 struct context_stack cstk = pop_context ();
774
775 /* Make a block for the local symbols within. */
776 finish_block (cstk.name, cstk.old_blocks, NULL,
777 cstk.start_addr, end_addr);
778
779 if (!m_context_stack.empty ())
780 {
781 /* This is said to happen with SCO. The old coffread.c
782 code simply emptied the context stack, so we do the
783 same. FIXME: Find out why it is happening. This is not
784 believed to happen in most cases (even for coffread.c);
785 it used to be an abort(). */
786 complaint (_("Context stack not empty in end_compunit_symtab"));
787 m_context_stack.clear ();
788 }
789 }
790
791 /* Reordered executables may have out of order pending blocks; if
792 OBJF_REORDERED is true, then sort the pending blocks. */
793
795 {
796 struct pending_block *pb;
797
798 std::vector<block *> barray;
799
800 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
801 barray.push_back (pb->block);
802
803 /* Sort blocks by start address in descending order. Blocks with the
804 same start address must remain in the original order to preserve
805 inline function caller/callee relationships. */
806 std::stable_sort (barray.begin (), barray.end (),
807 [] (const block *a, const block *b)
808 {
809 return a->start () > b->start ();
810 });
811
812 int i = 0;
813 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
814 pb->block = barray[i++];
815 }
816
817 /* Cleanup any undefined types that have been left hanging around
818 (this needs to be done before the finish_blocks so that
819 file_symbols is still good).
820
821 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
822 specific, but harmless for other symbol readers, since on gdb
823 startup or when finished reading stabs, the state is set so these
824 are no-ops. FIXME: Is this handled right in case of QUIT? Can
825 we make this cleaner? */
826
829
830 if (!required
831 && m_pending_blocks == NULL
832 && m_file_symbols == NULL
833 && m_global_symbols == NULL
835 && m_pending_macros == NULL
836 && m_global_using_directives == NULL)
837 {
838 /* Ignore symtabs that have no functions with real debugging info. */
839 return NULL;
840 }
841 else
842 {
843 /* Define the STATIC_BLOCK. */
844 return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
846 end_addr, 0, expandable);
847 }
848}
849
850/* Subroutine of end_compunit_symtab_from_static_block to simplify it.
851 Handle the "have blockvector" case.
852 See end_compunit_symtab_from_static_block for a description of the
853 arguments. */
854
855struct compunit_symtab *
857 (struct block *static_block, int section, int expandable)
858{
860 struct blockvector *blockvector;
861 struct subfile *subfile;
862 CORE_ADDR end_addr;
863
864 gdb_assert (static_block != NULL);
865 gdb_assert (m_subfiles != NULL);
866
867 end_addr = static_block->end ();
868
869 /* Create the GLOBAL_BLOCK and build the blockvector. */
870 finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
871 m_last_source_start_addr, end_addr,
872 1, expandable);
874
875 /* Read the line table if it has to be read separately.
876 This is only used by xcoffread.c. */
877 if (m_objfile->sf->sym_read_linetable != NULL)
879
880 /* Handle the case where the debug info specifies a different path
881 for the main source file. It can cause us to lose track of its
882 line number information. */
884
885 /* Now create the symtab objects proper, if not already done,
886 one for each subfile. */
887
888 for (subfile = m_subfiles;
889 subfile != NULL;
891 {
892 if (!subfile->line_vector_entries.empty ())
893 {
894 const auto lte_is_less_than
895 = [] (const linetable_entry &ln1,
896 const linetable_entry &ln2) -> bool
897 {
898 if (ln1.pc == ln2.pc
899 && ((ln1.line == 0) != (ln2.line == 0)))
900 return ln1.line == 0;
901
902 return (ln1.pc < ln2.pc);
903 };
904
905 /* Like the pending blocks, the line table may be scrambled in
906 reordered executables. Sort it if OBJF_REORDERED is true. It
907 is important to preserve the order of lines at the same
908 address, as this maintains the inline function caller/callee
909 relationships, this is why std::stable_sort is used. */
911 std::stable_sort (subfile->line_vector_entries.begin (),
913 lte_is_less_than);
914 }
915
916 /* Allocate a symbol table if necessary. */
917 if (subfile->symtab == NULL)
918 subfile->symtab = allocate_symtab (cu, subfile->name.c_str (),
919 subfile->name_for_id.c_str ());
920
921 struct symtab *symtab = subfile->symtab;
922
923 /* Fill in its components. */
924
925 if (!subfile->line_vector_entries.empty ())
926 {
927 /* Reallocate the line table on the objfile obstack. */
928 size_t n_entries = subfile->line_vector_entries.size ();
929 size_t entry_array_size = n_entries * sizeof (struct linetable_entry);
930 int linetablesize = sizeof (struct linetable) + entry_array_size;
931
933 (XOBNEWVAR (&m_objfile->objfile_obstack, struct linetable,
934 linetablesize));
935
936 symtab->linetable ()->nitems = n_entries;
937 memcpy (symtab->linetable ()->item,
938 subfile->line_vector_entries.data (), entry_array_size);
939 }
940 else
941 symtab->set_linetable (nullptr);
942
943 /* Use whatever language we have been using for this
944 subfile, not the one that was deduced in allocate_symtab
945 from the filename. We already did our own deducing when
946 we created the subfile, and we may have altered our
947 opinion of what language it is from things we found in
948 the symbols. */
950 }
951
952 /* Make sure the filetab of main_subfile is the primary filetab of the CU. */
954
955 /* Fill out the compunit symtab. */
956
957 if (!m_comp_dir.empty ())
958 {
959 /* Reallocate the dirname on the symbol obstack. */
960 cu->set_dirname (obstack_strdup (&m_objfile->objfile_obstack,
961 m_comp_dir.c_str ()));
962 }
963
964 /* Save the debug format string (if any) in the symtab. */
966
967 /* Similarly for the producer. */
969
971 {
972 struct block *b = blockvector->global_block ();
973
975 }
976
977 cu->set_block_line_section (section);
978
980
981 /* Default any symbols without a specified symtab to the primary symtab. */
982 {
983 int block_i;
984
985 /* The main source file's symtab. */
986 struct symtab *symtab = cu->primary_filetab ();
987
988 for (block_i = 0; block_i < blockvector->num_blocks (); block_i++)
989 {
990 struct block *block = blockvector->block (block_i);
991 struct symbol *sym;
992 struct mdict_iterator miter;
993
994 /* Inlined functions may have symbols not in the global or
995 static symbol lists. */
996 if (block->function () != nullptr
997 && block->function ()->symtab () == nullptr)
999
1000 /* Note that we only want to fix up symbols from the local
1001 blocks, not blocks coming from included symtabs. That is why
1002 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1003 ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
1004 if (sym->symtab () == NULL)
1005 sym->set_symtab (symtab);
1006 }
1007 }
1008
1010
1011 return cu;
1012}
1013
1014/* Implementation of the second part of end_compunit_symtab. Pass STATIC_BLOCK
1015 as value returned by end_compunit_symtab_get_static_block.
1016
1017 SECTION is the same as for end_compunit_symtab: the section number
1018 (in objfile->section_offsets) of the blockvector and linetable.
1019
1020 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1021 expandable. */
1022
1023struct compunit_symtab *
1025 (struct block *static_block, int section, int expandable)
1026{
1027 struct compunit_symtab *cu;
1028
1029 if (static_block == NULL)
1030 {
1031 /* Handle the "no blockvector" case.
1032 When this happens there is nothing to record, so there's nothing
1033 to do: memory will be freed up later.
1034
1035 Note: We won't be adding a compunit to the objfile's list of
1036 compunits, so there's nothing to unchain. However, since each symtab
1037 is added to the objfile's obstack we can't free that space.
1038 We could do better, but this is believed to be a sufficiently rare
1039 event. */
1040 cu = NULL;
1041 }
1042 else
1043 cu = end_compunit_symtab_with_blockvector (static_block, section, expandable);
1044
1045 return cu;
1046}
1047
1048/* Finish the symbol definitions for one main source file, close off
1049 all the lexical contexts for that file (creating struct block's for
1050 them), then make the struct symtab for that file and put it in the
1051 list of all such.
1052
1053 END_ADDR is the address of the end of the file's text. SECTION is
1054 the section number (in objfile->section_offsets) of the blockvector
1055 and linetable.
1056
1057 Note that it is possible for end_compunit_symtab() to return NULL. In
1058 particular, for the DWARF case at least, it will return NULL when
1059 it finds a compilation unit that has exactly one DIE, a
1060 TAG_compile_unit DIE. This can happen when we link in an object
1061 file that was compiled from an empty source file. Returning NULL
1062 is probably not the correct thing to do, because then gdb will
1063 never know about this empty file (FIXME).
1064
1065 If you need to modify STATIC_BLOCK before it is finalized you should
1066 call end_compunit_symtab_get_static_block and
1067 end_compunit_symtab_from_static_block yourself. */
1068
1069struct compunit_symtab *
1070buildsym_compunit::end_compunit_symtab (CORE_ADDR end_addr, int section)
1071{
1072 struct block *static_block;
1073
1074 static_block = end_compunit_symtab_get_static_block (end_addr, 0, 0);
1075 return end_compunit_symtab_from_static_block (static_block, section, 0);
1076}
1077
1078/* Same as end_compunit_symtab except create a symtab that can be later added
1079 to. */
1080
1081struct compunit_symtab *
1082buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
1083{
1084 struct block *static_block;
1085
1086 static_block = end_compunit_symtab_get_static_block (end_addr, 1, 0);
1087 return end_compunit_symtab_from_static_block (static_block, section, 1);
1088}
1089
1090/* Subroutine of augment_type_symtab to simplify it.
1091 Attach the main source file's symtab to all symbols in PENDING_LIST that
1092 don't have one. */
1093
1094static void
1096 struct compunit_symtab *cu)
1097{
1098 struct pending *pending;
1099 int i;
1100
1101 for (pending = pending_list; pending != NULL; pending = pending->next)
1102 {
1103 for (i = 0; i < pending->nsyms; ++i)
1104 {
1105 if (pending->symbol[i]->symtab () == NULL)
1107 }
1108 }
1109}
1110
1111/* Same as end_compunit_symtab, but for the case where we're adding more symbols
1112 to an existing symtab that is known to contain only type information.
1113 This is the case for DWARF4 Type Units. */
1114
1115void
1117{
1118 struct compunit_symtab *cust = m_compunit_symtab;
1119 struct blockvector *blockvector = cust->blockvector ();
1120
1121 if (!m_context_stack.empty ())
1122 complaint (_("Context stack not empty in augment_type_symtab"));
1123 if (m_pending_blocks != NULL)
1124 complaint (_("Blocks in a type symtab"));
1125 if (m_pending_macros != NULL)
1126 complaint (_("Macro in a type symtab"));
1128 complaint (_("Line numbers recorded in a type symtab"));
1129
1130 if (m_file_symbols != NULL)
1131 {
1132 struct block *block = blockvector->static_block ();
1133
1134 /* First mark any symbols without a specified symtab as belonging
1135 to the primary symtab. */
1137
1139 }
1140
1141 if (m_global_symbols != NULL)
1142 {
1143 struct block *block = blockvector->global_block ();
1144
1145 /* First mark any symbols without a specified symtab as belonging
1146 to the primary symtab. */
1148
1150 }
1151}
1152
1153/* Push a context block. Args are an identifying nesting level
1154 (checkable when you pop it), and the starting PC address of this
1155 context. */
1156
1157struct context_stack *
1158buildsym_compunit::push_context (int desc, CORE_ADDR valu)
1159{
1160 m_context_stack.emplace_back ();
1161 struct context_stack *newobj = &m_context_stack.back ();
1162
1163 newobj->depth = desc;
1164 newobj->locals = m_local_symbols;
1165 newobj->old_blocks = m_pending_blocks;
1166 newobj->start_addr = valu;
1168 newobj->name = NULL;
1169
1170 m_local_symbols = NULL;
1172
1173 return newobj;
1174}
1175
1176/* Pop a context block. Returns the address of the context block just
1177 popped. */
1178
1179struct context_stack
1181{
1182 gdb_assert (!m_context_stack.empty ());
1183 struct context_stack result = m_context_stack.back ();
1184 m_context_stack.pop_back ();
1185 return result;
1186}
const char *const name
void xfree(void *)
struct block * allocate_block(struct obstack *obstack)
Definition block.c:397
void block_set_using(struct block *block, struct using_direct *using_decl, struct obstack *obstack)
Definition block.c:338
struct block * allocate_global_block(struct obstack *obstack)
Definition block.c:407
void set_block_compunit_symtab(struct block *block, struct compunit_symtab *cu)
Definition block.c:417
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:1095
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:214
struct multidictionary * mdict_create_hashed_expandable(enum language language)
Definition dictionary.c:977
struct multidictionary * mdict_create_hashed(struct obstack *obstack, const struct pending *symbol_list)
Definition dictionary.c:948
struct multidictionary * mdict_create_linear(struct obstack *obstack, const struct pending *symbol_list)
Definition dictionary.c:993
void mdict_add_pending(struct multidictionary *mdict, const struct pending *symbol_list)
#define ALL_DICT_SYMBOLS(dict, iter, sym)
Definition dictionary.h:174
#define TYPE_FIELD_ARTIFICIAL(thistype, n)
Definition gdbtypes.h:2122
#define TYPE_ALLOC(t, size)
Definition gdbtypes.h:2406
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:1862
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:478
@ OBJF_REORDERED
void objfile_register_static_link(struct objfile *objfile, const struct block *block, const struct dynamic_prop *static_link)
Definition objfiles.c:199
void finish_global_stabs(struct objfile *objfile)
Definition stabsread.c:4694
void cleanup_undefined_stabs_types(struct objfile *objfile)
Definition stabsread.c:4506
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
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: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:271
struct block * static_block()
Definition block.h:302
int num_blocks() const
Definition block.h:290
void set_map(addrmap *map)
Definition block.h:318
void set_block(int i, struct block *block)
Definition block.h:279
struct block * global_block()
Definition block.h:294
void set_num_blocks(int num_blocks)
Definition block.h:286
struct objfile * m_objfile
Definition buildsym.h:358
void push_subfile()
Definition buildsym.c:610
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:415
CORE_ADDR m_last_source_start_addr
Definition buildsym.h:402
void start_subfile(const char *name, const char *name_for_id)
Definition buildsym.c:491
struct pending * m_local_symbols
Definition buildsym.h:446
const char * m_debugformat
Definition buildsym.h:383
bool m_have_line_numbers
Definition buildsym.h:397
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
void record_pending_block(struct block *block, struct pending_block *opblock)
Definition buildsym.c:176
struct pending * m_file_symbols
Definition buildsym.h:440
std::string m_comp_dir
Definition buildsym.h:375
struct using_direct * m_local_using_directives
Definition buildsym.h:408
void watch_main_source_file_lossage()
Definition buildsym.c:688
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:390
struct pending * m_global_symbols
Definition buildsym.h:443
struct subfile * m_subfiles
Definition buildsym.h:364
struct blockvector * make_blockvector()
Definition buildsym.c:425
struct compunit_symtab * end_compunit_symtab_with_blockvector(struct block *static_block, int section, int expandable)
Definition buildsym.c:857
auto_obstack m_pending_block_obstack
Definition buildsym.h:431
struct using_direct * m_global_using_directives
Definition buildsym.h:411
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
void set_last_source_file(const char *name)
Definition buildsym.h:183
struct compunit_symtab * end_compunit_symtab_from_static_block(struct block *static_block, int section, int expandable)
Definition buildsym.c:1025
struct compunit_symtab * m_compunit_symtab
Definition buildsym.h:386
struct compunit_symtab * end_compunit_symtab(CORE_ADDR end_addr, int section)
Definition buildsym.c:1070
struct block * end_compunit_symtab_get_static_block(CORE_ADDR end_addr, int expandable, int required)
Definition buildsym.c:764
struct addrmap_mutable m_pending_addrmap
Definition buildsym.h:422
void augment_type_symtab()
Definition buildsym.c:1116
struct pending ** get_file_symbols()
Definition buildsym.h:302
bool m_pending_addrmap_interesting
Definition buildsym.h:428
void record_line(struct subfile *subfile, int line, CORE_ADDR pc, linetable_entry_flags flags)
Definition buildsym.c:630
std::vector< const char * > m_subfile_stack
Definition buildsym.h:405
struct subfile * m_current_subfile
Definition buildsym.h:417
const char * m_producer
Definition buildsym.h:379
void free_pending_blocks()
Definition buildsym.h:205
struct pending_block * m_pending_blocks
Definition buildsym.h:437
enum language m_language
Definition buildsym.h:389
struct subfile * m_main_subfile
Definition buildsym.h:367
struct context_stack pop_context()
Definition buildsym.c:1180
struct pending ** get_global_symbols()
Definition buildsym.h:307
struct compunit_symtab * end_expandable_symtab(CORE_ADDR end_addr, int section)
Definition buildsym.c:1082
void patch_subfile_names(struct subfile *subfile, const char *name)
Definition buildsym.c:570
const char * pop_subfile()
Definition buildsym.c:618
void record_block_range(struct block *block, CORE_ADDR start, CORE_ADDR end_inclusive)
Definition buildsym.c:408
struct macro_table * m_pending_macros
Definition buildsym.h:393
struct context_stack * push_context(int desc, CORE_ADDR valu)
Definition buildsym.c:1158
symtab * primary_filetab() const
Definition symtab.c:397
void set_block_line_section(int block_line_section)
Definition symtab.h:1793
void set_blockvector(struct blockvector *blockvector)
Definition symtab.h:1783
struct blockvector * blockvector()
Definition symtab.h:1773
void set_macro_table(struct macro_table *macro_table)
Definition symtab.h:1823
void set_dirname(const char *dirname)
Definition symtab.h:1768
void set_debugformat(const char *debugformat)
Definition symtab.h:1748
void set_producer(const char *producer)
Definition symtab.h:1758
void set_primary_filetab(symtab *primary_filetab)
Definition symtab.c:369
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:564
const char * print_name() const
Definition symtab.h:474
const char * linkage_name() const
Definition symtab.h:459
Definition symtab.h:1548
unsigned is_stmt
Definition symtab.h:1553
int line
Definition symtab.h:1550
CORE_ADDR pc
Definition symtab.h:1560
bool prologue_end
Definition symtab.h:1557
int nitems
Definition symtab.h:1582
struct linetable_entry item[1]
Definition symtab.h:1587
auto_obstack storage_obstack
Definition objfiles.h:265
gdb::bcache string_cache
Definition objfiles.h:269
const struct sym_fns * sf
Definition objfiles.h:681
struct gdbarch * arch() const
Definition objfiles.h:482
struct objfile_per_bfd_storage * per_bfd
Definition objfiles.h:657
objfile_flags flags
Definition objfiles.h:637
auto_obstack objfile_obstack
Definition objfiles.h:673
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:1285
void set_value_block(const block *block)
Definition symtab.h:1353
bool is_argument() const
Definition symtab.h:1260
struct symtab * symtab
Definition symtab.h:1414
void set_symtab(struct symtab *symtab)
Definition symtab.c:6505
void set_linetable(struct linetable *linetable)
Definition symtab.h:1618
struct linetable * linetable() const
Definition symtab.h:1613
void set_language(enum language language)
Definition symtab.h:1628
struct field & field(int idx) const
Definition gdbtypes.h:983
void set_num_fields(int num_fields)
Definition gdbtypes.h:971
int num_fields() const
Definition gdbtypes.h:965
void set_fields(struct field *fields)
Definition gdbtypes.h:990
enum language deduce_language_from_filename(const char *filename)
Definition symfile.c:2759
void add_compunit_symtab_to_objfile(struct compunit_symtab *cu)
Definition symfile.c:2851
struct symtab * allocate_symtab(struct compunit_symtab *cust, const char *filename, const char *filename_for_id)
Definition symfile.c:2778
struct compunit_symtab * allocate_compunit_symtab(struct objfile *objfile, const char *name)
Definition symfile.c:2825
#define symtab_create_debug_printf(fmt,...)
Definition symtab.h:2617
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition utils.c:3114