1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
20 #include "buildsym-legacy.h"
22 #include "gdb_obstack.h"
27 #include "complaints.h"
28 #include "expression.h" /* For "enum exp_opcode" used by... */
29 #include "filenames.h" /* For DOSish file names. */
31 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
33 #include "cp-support.h"
34 #include "dictionary.h"
38 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
39 questionable--see comment where we call them). */
41 #include "stabsread.h"
43 /* List of blocks already made (lexical contexts already closed).
44 This is used at the end to make the blockvector. */
48 struct pending_block *next;
52 static int compare_line_numbers (const void *ln1p, const void *ln2p);
54 /* Initial sizes of data structures. These are realloc'd larger if
55 needed, and realloc'd down to the size actually used, when
58 #define INITIAL_LINE_VECTOR_LENGTH 1000
61 buildsym_compunit::buildsym_compunit (struct objfile *objfile_,
63 const char *comp_dir_,
64 enum language language_,
66 : m_objfile (objfile_),
67 m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
68 m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
69 m_language (language_),
70 m_last_source_start_addr (last_addr)
72 /* Allocate the compunit symtab now. The caller needs it to allocate
73 non-primary symtabs. It is also needed by get_macro_table. */
74 m_compunit_symtab = allocate_compunit_symtab (m_objfile, name);
76 /* Build the subfile for NAME (the main source file) so that we can record
77 a pointer to it for later.
78 IMPORTANT: Do not allocate a struct symtab for NAME here.
79 It can happen that the debug info provides a different path to NAME than
80 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
81 that only works if the main_subfile doesn't have a symtab yet. */
83 /* Save this so that we don't have to go looking for it at the end
84 of the subfiles list. */
85 m_main_subfile = m_current_subfile;
88 buildsym_compunit::~buildsym_compunit ()
90 struct subfile *subfile, *nextsub;
92 if (m_pending_macros != nullptr)
93 free_macro_table (m_pending_macros);
95 for (subfile = m_subfiles;
99 nextsub = subfile->next;
100 xfree (subfile->name);
101 xfree (subfile->line_vector);
105 struct pending *next, *next1;
107 for (next = m_file_symbols; next != NULL; next = next1)
110 xfree ((void *) next);
113 for (next = m_global_symbols; next != NULL; next = next1)
116 xfree ((void *) next);
121 buildsym_compunit::get_macro_table ()
123 if (m_pending_macros == nullptr)
124 m_pending_macros = new_macro_table (&m_objfile->per_bfd->storage_obstack,
125 m_objfile->per_bfd->macro_cache,
127 return m_pending_macros;
130 /* Maintain the lists of symbols and blocks. */
132 /* Add a symbol to one of the lists of symbols. */
135 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
137 struct pending *link;
139 /* If this is an alias for another symbol, don't add it. */
140 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
143 /* We keep PENDINGSIZE symbols in each link of the list. If we
144 don't have a link with room in it, add a new link. */
145 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
147 link = XNEW (struct pending);
148 link->next = *listhead;
153 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
156 /* Find a symbol named NAME on a LIST. NAME need not be
157 '\0'-terminated; LENGTH is the length of the name. */
160 find_symbol_in_list (struct pending *list, char *name, int length)
167 for (j = list->nsyms; --j >= 0;)
169 pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
170 if (*pp == *name && strncmp (pp, name, length) == 0
171 && pp[length] == '\0')
173 return (list->symbol[j]);
181 /* Record BLOCK on the list of all blocks in the file. Put it after
182 OPBLOCK, or at the beginning if opblock is NULL. This puts the
183 block in the list after all its subblocks. */
186 buildsym_compunit::record_pending_block (struct block *block,
187 struct pending_block *opblock)
189 struct pending_block *pblock;
191 pblock = XOBNEW (&m_pending_block_obstack, struct pending_block);
192 pblock->block = block;
195 pblock->next = opblock->next;
196 opblock->next = pblock;
200 pblock->next = m_pending_blocks;
201 m_pending_blocks = pblock;
205 /* Take one of the lists of symbols and make a block from it. Keep
206 the order the symbols have in the list (reversed from the input
207 file). Put the block on the list of pending blocks. */
210 buildsym_compunit::finish_block_internal
211 (struct symbol *symbol,
212 struct pending **listhead,
213 struct pending_block *old_blocks,
214 const struct dynamic_prop *static_link,
215 CORE_ADDR start, CORE_ADDR end,
216 int is_global, int expandable)
218 struct gdbarch *gdbarch = get_objfile_arch (m_objfile);
219 struct pending *next, *next1;
221 struct pending_block *pblock;
222 struct pending_block *opblock;
225 ? allocate_global_block (&m_objfile->objfile_obstack)
226 : allocate_block (&m_objfile->objfile_obstack));
231 = dict_create_linear (&m_objfile->objfile_obstack,
232 m_language, *listhead);
238 BLOCK_DICT (block) = dict_create_hashed_expandable (m_language);
239 dict_add_pending (BLOCK_DICT (block), *listhead);
244 dict_create_hashed (&m_objfile->objfile_obstack,
245 m_language, *listhead);
249 BLOCK_START (block) = start;
250 BLOCK_END (block) = end;
252 /* Put the block in as the value of the symbol that names it. */
256 struct type *ftype = SYMBOL_TYPE (symbol);
257 struct dict_iterator iter;
258 SYMBOL_BLOCK_VALUE (symbol) = block;
259 BLOCK_FUNCTION (block) = symbol;
261 if (TYPE_NFIELDS (ftype) <= 0)
263 /* No parameter type information is recorded with the
264 function's type. Set that from the type of the
265 parameter symbols. */
266 int nparams = 0, iparams;
269 /* Here we want to directly access the dictionary, because
270 we haven't fully initialized the block yet. */
271 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
273 if (SYMBOL_IS_ARGUMENT (sym))
278 TYPE_NFIELDS (ftype) = nparams;
279 TYPE_FIELDS (ftype) = (struct field *)
280 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
283 /* Here we want to directly access the dictionary, because
284 we haven't fully initialized the block yet. */
285 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
287 if (iparams == nparams)
290 if (SYMBOL_IS_ARGUMENT (sym))
292 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
293 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
302 BLOCK_FUNCTION (block) = NULL;
305 if (static_link != NULL)
306 objfile_register_static_link (m_objfile, block, static_link);
308 /* Now free the links of the list, and empty the list. */
310 for (next = *listhead; next; next = next1)
317 /* Check to be sure that the blocks have an end address that is
318 greater than starting address. */
320 if (BLOCK_END (block) < BLOCK_START (block))
324 complaint (_("block end address less than block "
325 "start address in %s (patched it)"),
326 SYMBOL_PRINT_NAME (symbol));
330 complaint (_("block end address %s less than block "
331 "start address %s (patched it)"),
332 paddress (gdbarch, BLOCK_END (block)),
333 paddress (gdbarch, BLOCK_START (block)));
335 /* Better than nothing. */
336 BLOCK_END (block) = BLOCK_START (block);
339 /* Install this block as the superblock of all blocks made since the
340 start of this scope that don't have superblocks yet. */
343 for (pblock = m_pending_blocks;
344 pblock && pblock != old_blocks;
345 pblock = pblock->next)
347 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
349 /* Check to be sure the blocks are nested as we receive
350 them. If the compiler/assembler/linker work, this just
351 burns a small amount of time.
353 Skip blocks which correspond to a function; they're not
354 physically nested inside this other blocks, only
356 if (BLOCK_FUNCTION (pblock->block) == NULL
357 && (BLOCK_START (pblock->block) < BLOCK_START (block)
358 || BLOCK_END (pblock->block) > BLOCK_END (block)))
362 complaint (_("inner block not inside outer block in %s"),
363 SYMBOL_PRINT_NAME (symbol));
367 complaint (_("inner block (%s-%s) not "
368 "inside outer block (%s-%s)"),
369 paddress (gdbarch, BLOCK_START (pblock->block)),
370 paddress (gdbarch, BLOCK_END (pblock->block)),
371 paddress (gdbarch, BLOCK_START (block)),
372 paddress (gdbarch, BLOCK_END (block)));
374 if (BLOCK_START (pblock->block) < BLOCK_START (block))
375 BLOCK_START (pblock->block) = BLOCK_START (block);
376 if (BLOCK_END (pblock->block) > BLOCK_END (block))
377 BLOCK_END (pblock->block) = BLOCK_END (block);
379 BLOCK_SUPERBLOCK (pblock->block) = block;
384 block_set_using (block,
386 ? m_global_using_directives
387 : m_local_using_directives),
388 &m_objfile->objfile_obstack);
390 m_global_using_directives = NULL;
392 m_local_using_directives = NULL;
394 record_pending_block (block, opblock);
400 buildsym_compunit::finish_block (struct symbol *symbol,
401 struct pending_block *old_blocks,
402 const struct dynamic_prop *static_link,
403 CORE_ADDR start, CORE_ADDR end)
405 return finish_block_internal (symbol, &m_local_symbols,
406 old_blocks, static_link, start, end, 0, 0);
409 /* Record that the range of addresses from START to END_INCLUSIVE
410 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
411 addresses must be set already. You must apply this function to all
412 BLOCK's children before applying it to BLOCK.
414 If a call to this function complicates the picture beyond that
415 already provided by BLOCK_START and BLOCK_END, then we create an
416 address map for the block. */
418 buildsym_compunit::record_block_range (struct block *block,
420 CORE_ADDR end_inclusive)
422 /* If this is any different from the range recorded in the block's
423 own BLOCK_START and BLOCK_END, then note that the address map has
424 become interesting. Note that even if this block doesn't have
425 any "interesting" ranges, some later block might, so we still
426 need to record this block in the addrmap. */
427 if (start != BLOCK_START (block)
428 || end_inclusive + 1 != BLOCK_END (block))
429 m_pending_addrmap_interesting = true;
431 if (m_pending_addrmap == nullptr)
432 m_pending_addrmap = addrmap_create_mutable (&m_pending_addrmap_obstack);
434 addrmap_set_empty (m_pending_addrmap, start, end_inclusive, block);
438 buildsym_compunit::make_blockvector ()
440 struct pending_block *next;
441 struct blockvector *blockvector;
444 /* Count the length of the list of blocks. */
446 for (next = m_pending_blocks, i = 0; next; next = next->next, i++)
450 blockvector = (struct blockvector *)
451 obstack_alloc (&m_objfile->objfile_obstack,
452 (sizeof (struct blockvector)
453 + (i - 1) * sizeof (struct block *)));
455 /* Copy the blocks into the blockvector. This is done in reverse
456 order, which happens to put the blocks into the proper order
457 (ascending starting address). finish_block has hair to insert
458 each block into the list after its subblocks in order to make
459 sure this is true. */
461 BLOCKVECTOR_NBLOCKS (blockvector) = i;
462 for (next = m_pending_blocks; next; next = next->next)
464 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
467 free_pending_blocks ();
469 /* If we needed an address map for this symtab, record it in the
471 if (m_pending_addrmap != nullptr && m_pending_addrmap_interesting)
472 BLOCKVECTOR_MAP (blockvector)
473 = addrmap_create_fixed (m_pending_addrmap, &m_objfile->objfile_obstack);
475 BLOCKVECTOR_MAP (blockvector) = 0;
477 /* Some compilers output blocks in the wrong order, but we depend on
478 their being in the right order so we can binary search. Check the
479 order and moan about it.
480 Note: Remember that the first two blocks are the global and static
481 blocks. We could special case that fact and begin checking at block 2.
482 To avoid making that assumption we do not. */
483 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
485 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
487 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
488 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
491 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
493 complaint (_("block at %s out of order"),
494 hex_string ((LONGEST) start));
499 return (blockvector);
502 /* Start recording information about source code that came from an
503 included (or otherwise merged-in) source file with a different
504 name. NAME is the name of the file (cannot be NULL). */
507 buildsym_compunit::start_subfile (const char *name)
509 const char *subfile_dirname;
510 struct subfile *subfile;
512 subfile_dirname = m_comp_dir.get ();
514 /* See if this subfile is already registered. */
516 for (subfile = m_subfiles; subfile; subfile = subfile->next)
520 /* If NAME is an absolute path, and this subfile is not, then
521 attempt to create an absolute path to compare. */
522 if (IS_ABSOLUTE_PATH (name)
523 && !IS_ABSOLUTE_PATH (subfile->name)
524 && subfile_dirname != NULL)
525 subfile_name = concat (subfile_dirname, SLASH_STRING,
526 subfile->name, (char *) NULL);
528 subfile_name = subfile->name;
530 if (FILENAME_CMP (subfile_name, name) == 0)
532 m_current_subfile = subfile;
533 if (subfile_name != subfile->name)
534 xfree (subfile_name);
537 if (subfile_name != subfile->name)
538 xfree (subfile_name);
541 /* This subfile is not known. Add an entry for it. */
543 subfile = XNEW (struct subfile);
544 memset (subfile, 0, sizeof (struct subfile));
545 subfile->buildsym_compunit = this;
547 subfile->next = m_subfiles;
548 m_subfiles = subfile;
550 m_current_subfile = subfile;
552 subfile->name = xstrdup (name);
554 /* Initialize line-number recording for this subfile. */
555 subfile->line_vector = NULL;
557 /* Default the source language to whatever can be deduced from the
558 filename. If nothing can be deduced (such as for a C/C++ include
559 file with a ".h" extension), then inherit whatever language the
560 previous subfile had. This kludgery is necessary because there
561 is no standard way in some object formats to record the source
562 language. Also, when symtabs are allocated we try to deduce a
563 language then as well, but it is too late for us to use that
564 information while reading symbols, since symtabs aren't allocated
565 until after all the symbols have been processed for a given
568 subfile->language = deduce_language_from_filename (subfile->name);
569 if (subfile->language == language_unknown
570 && subfile->next != NULL)
572 subfile->language = subfile->next->language;
575 /* If the filename of this subfile ends in .C, then change the
576 language of any pending subfiles from C to C++. We also accept
577 any other C++ suffixes accepted by deduce_language_from_filename. */
578 /* Likewise for f2c. */
583 enum language sublang = deduce_language_from_filename (subfile->name);
585 if (sublang == language_cplus || sublang == language_fortran)
586 for (s = m_subfiles; s != NULL; s = s->next)
587 if (s->language == language_c)
588 s->language = sublang;
591 /* And patch up this file if necessary. */
592 if (subfile->language == language_c
593 && subfile->next != NULL
594 && (subfile->next->language == language_cplus
595 || subfile->next->language == language_fortran))
597 subfile->language = subfile->next->language;
601 /* For stabs readers, the first N_SO symbol is assumed to be the
602 source file name, and the subfile struct is initialized using that
603 assumption. If another N_SO symbol is later seen, immediately
604 following the first one, then the first one is assumed to be the
605 directory name and the second one is really the source file name.
607 So we have to patch up the subfile struct by moving the old name
608 value to dirname and remembering the new name. Some sanity
609 checking is performed to ensure that the state of the subfile
610 struct is reasonable and that the old name we are assuming to be a
611 directory name actually is (by checking for a trailing '/'). */
614 buildsym_compunit::patch_subfile_names (struct subfile *subfile,
618 && m_comp_dir == NULL
619 && subfile->name != NULL
620 && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
622 m_comp_dir.reset (subfile->name);
623 subfile->name = xstrdup (name);
624 set_last_source_file (name);
626 /* Default the source language to whatever can be deduced from
627 the filename. If nothing can be deduced (such as for a C/C++
628 include file with a ".h" extension), then inherit whatever
629 language the previous subfile had. This kludgery is
630 necessary because there is no standard way in some object
631 formats to record the source language. Also, when symtabs
632 are allocated we try to deduce a language then as well, but
633 it is too late for us to use that information while reading
634 symbols, since symtabs aren't allocated until after all the
635 symbols have been processed for a given source file. */
637 subfile->language = deduce_language_from_filename (subfile->name);
638 if (subfile->language == language_unknown
639 && subfile->next != NULL)
641 subfile->language = subfile->next->language;
646 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
647 switching source files (different subfiles, as we call them) within
648 one object file, but using a stack rather than in an arbitrary
652 buildsym_compunit::push_subfile ()
654 gdb_assert (m_current_subfile != NULL);
655 gdb_assert (m_current_subfile->name != NULL);
656 m_subfile_stack.push_back (m_current_subfile->name);
660 buildsym_compunit::pop_subfile ()
662 gdb_assert (!m_subfile_stack.empty ());
663 const char *name = m_subfile_stack.back ();
664 m_subfile_stack.pop_back ();
668 /* Add a linetable entry for line number LINE and address PC to the
669 line vector for SUBFILE. */
672 buildsym_compunit::record_line (struct subfile *subfile, int line,
675 struct linetable_entry *e;
677 /* Ignore the dummy line number in libg.o */
683 /* Make sure line vector exists and is big enough. */
684 if (!subfile->line_vector)
686 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
687 subfile->line_vector = (struct linetable *)
688 xmalloc (sizeof (struct linetable)
689 + subfile->line_vector_length * sizeof (struct linetable_entry));
690 subfile->line_vector->nitems = 0;
691 m_have_line_numbers = true;
694 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
696 subfile->line_vector_length *= 2;
697 subfile->line_vector = (struct linetable *)
698 xrealloc ((char *) subfile->line_vector,
699 (sizeof (struct linetable)
700 + (subfile->line_vector_length
701 * sizeof (struct linetable_entry))));
704 /* Normally, we treat lines as unsorted. But the end of sequence
705 marker is special. We sort line markers at the same PC by line
706 number, so end of sequence markers (which have line == 0) appear
707 first. This is right if the marker ends the previous function,
708 and there is no padding before the next function. But it is
709 wrong if the previous line was empty and we are now marking a
710 switch to a different subfile. We must leave the end of sequence
711 marker at the end of this group of lines, not sort the empty line
712 to after the marker. The easiest way to accomplish this is to
713 delete any empty lines from our table, if they are followed by
714 end of sequence markers. All we lose is the ability to set
715 breakpoints at some lines which contain no instructions
717 if (line == 0 && subfile->line_vector->nitems > 0)
719 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
720 while (subfile->line_vector->nitems > 0 && e->pc == pc)
723 subfile->line_vector->nitems--;
727 e = subfile->line_vector->item + subfile->line_vector->nitems++;
732 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
735 compare_line_numbers (const void *ln1p, const void *ln2p)
737 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
738 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
740 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
741 Please keep it that way. */
742 if (ln1->pc < ln2->pc)
745 if (ln1->pc > ln2->pc)
748 /* If pc equal, sort by line. I'm not sure whether this is optimum
749 behavior (see comment at struct linetable in symtab.h). */
750 return ln1->line - ln2->line;
753 /* Subroutine of end_symtab to simplify it. Look for a subfile that
754 matches the main source file's basename. If there is only one, and
755 if the main source file doesn't have any symbol or line number
756 information, then copy this file's symtab and line_vector to the
757 main source file's subfile and discard the other subfile. This can
758 happen because of a compiler bug or from the user playing games
759 with #line or from things like a distributed build system that
760 manipulates the debug info. This can also happen from an innocent
761 symlink in the paths, we don't canonicalize paths here. */
764 buildsym_compunit::watch_main_source_file_lossage ()
766 struct subfile *mainsub, *subfile;
768 /* Get the main source file. */
769 mainsub = m_main_subfile;
771 /* If the main source file doesn't have any line number or symbol
772 info, look for an alias in another subfile. */
774 if (mainsub->line_vector == NULL
775 && mainsub->symtab == NULL)
777 const char *mainbase = lbasename (mainsub->name);
779 struct subfile *prevsub;
780 struct subfile *mainsub_alias = NULL;
781 struct subfile *prev_mainsub_alias = NULL;
784 for (subfile = m_subfiles;
786 subfile = subfile->next)
788 if (subfile == mainsub)
790 if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
793 mainsub_alias = subfile;
794 prev_mainsub_alias = prevsub;
801 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
803 /* Found a match for the main source file.
804 Copy its line_vector and symtab to the main subfile
805 and then discard it. */
807 mainsub->line_vector = mainsub_alias->line_vector;
808 mainsub->line_vector_length = mainsub_alias->line_vector_length;
809 mainsub->symtab = mainsub_alias->symtab;
811 if (prev_mainsub_alias == NULL)
812 m_subfiles = mainsub_alias->next;
814 prev_mainsub_alias->next = mainsub_alias->next;
815 xfree (mainsub_alias->name);
816 xfree (mainsub_alias);
821 /* Implementation of the first part of end_symtab. It allows modifying
822 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
823 If the returned value is NULL there is no blockvector created for
824 this symtab (you still must call end_symtab_from_static_block).
826 END_ADDR is the same as for end_symtab: the address of the end of the
829 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
832 If REQUIRED is non-zero, then a symtab is created even if it does
833 not contain any symbols. */
836 buildsym_compunit::end_symtab_get_static_block (CORE_ADDR end_addr,
837 int expandable, int required)
839 /* Finish the lexical context of the last function in the file; pop
840 the context stack. */
842 if (!m_context_stack.empty ())
844 struct context_stack cstk = pop_context ();
846 /* Make a block for the local symbols within. */
847 finish_block (cstk.name, cstk.old_blocks, NULL,
848 cstk.start_addr, end_addr);
850 if (!m_context_stack.empty ())
852 /* This is said to happen with SCO. The old coffread.c
853 code simply emptied the context stack, so we do the
854 same. FIXME: Find out why it is happening. This is not
855 believed to happen in most cases (even for coffread.c);
856 it used to be an abort(). */
857 complaint (_("Context stack not empty in end_symtab"));
858 m_context_stack.clear ();
862 /* Reordered executables may have out of order pending blocks; if
863 OBJF_REORDERED is true, then sort the pending blocks. */
865 if ((m_objfile->flags & OBJF_REORDERED) && m_pending_blocks)
867 struct pending_block *pb;
869 std::vector<block *> barray;
871 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
872 barray.push_back (pb->block);
874 /* Sort blocks by start address in descending order. Blocks with the
875 same start address must remain in the original order to preserve
876 inline function caller/callee relationships. */
877 std::stable_sort (barray.begin (), barray.end (),
878 [] (const block *a, const block *b)
880 return BLOCK_START (a) > BLOCK_START (b);
884 for (pb = m_pending_blocks; pb != NULL; pb = pb->next)
885 pb->block = barray[i++];
888 /* Cleanup any undefined types that have been left hanging around
889 (this needs to be done before the finish_blocks so that
890 file_symbols is still good).
892 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
893 specific, but harmless for other symbol readers, since on gdb
894 startup or when finished reading stabs, the state is set so these
895 are no-ops. FIXME: Is this handled right in case of QUIT? Can
896 we make this cleaner? */
898 cleanup_undefined_stabs_types (m_objfile);
899 finish_global_stabs (m_objfile);
902 && m_pending_blocks == NULL
903 && m_file_symbols == NULL
904 && m_global_symbols == NULL
905 && !m_have_line_numbers
906 && m_pending_macros == NULL
907 && m_global_using_directives == NULL)
909 /* Ignore symtabs that have no functions with real debugging info. */
914 /* Define the STATIC_BLOCK. */
915 return finish_block_internal (NULL, get_file_symbols (), NULL, NULL,
916 m_last_source_start_addr,
917 end_addr, 0, expandable);
921 /* Subroutine of end_symtab_from_static_block to simplify it.
922 Handle the "have blockvector" case.
923 See end_symtab_from_static_block for a description of the arguments. */
925 struct compunit_symtab *
926 buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
927 int section, int expandable)
929 struct compunit_symtab *cu = m_compunit_symtab;
930 struct symtab *symtab;
931 struct blockvector *blockvector;
932 struct subfile *subfile;
935 gdb_assert (static_block != NULL);
936 gdb_assert (m_subfiles != NULL);
938 end_addr = BLOCK_END (static_block);
940 /* Create the GLOBAL_BLOCK and build the blockvector. */
941 finish_block_internal (NULL, get_global_symbols (), NULL, NULL,
942 m_last_source_start_addr, end_addr,
944 blockvector = make_blockvector ();
946 /* Read the line table if it has to be read separately.
947 This is only used by xcoffread.c. */
948 if (m_objfile->sf->sym_read_linetable != NULL)
949 m_objfile->sf->sym_read_linetable (m_objfile);
951 /* Handle the case where the debug info specifies a different path
952 for the main source file. It can cause us to lose track of its
953 line number information. */
954 watch_main_source_file_lossage ();
956 /* Now create the symtab objects proper, if not already done,
957 one for each subfile. */
959 for (subfile = m_subfiles;
961 subfile = subfile->next)
963 int linetablesize = 0;
965 if (subfile->line_vector)
967 linetablesize = sizeof (struct linetable) +
968 subfile->line_vector->nitems * sizeof (struct linetable_entry);
970 /* Like the pending blocks, the line table may be
971 scrambled in reordered executables. Sort it if
972 OBJF_REORDERED is true. */
973 if (m_objfile->flags & OBJF_REORDERED)
974 qsort (subfile->line_vector->item,
975 subfile->line_vector->nitems,
976 sizeof (struct linetable_entry), compare_line_numbers);
979 /* Allocate a symbol table if necessary. */
980 if (subfile->symtab == NULL)
981 subfile->symtab = allocate_symtab (cu, subfile->name);
982 symtab = subfile->symtab;
984 /* Fill in its components. */
986 if (subfile->line_vector)
988 /* Reallocate the line table on the symbol obstack. */
989 SYMTAB_LINETABLE (symtab) = (struct linetable *)
990 obstack_alloc (&m_objfile->objfile_obstack, linetablesize);
991 memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
996 SYMTAB_LINETABLE (symtab) = NULL;
999 /* Use whatever language we have been using for this
1000 subfile, not the one that was deduced in allocate_symtab
1001 from the filename. We already did our own deducing when
1002 we created the subfile, and we may have altered our
1003 opinion of what language it is from things we found in
1005 symtab->language = subfile->language;
1008 /* Make sure the symtab of main_subfile is the first in its list. */
1010 struct symtab *main_symtab, *prev_symtab;
1012 main_symtab = m_main_subfile->symtab;
1014 ALL_COMPUNIT_FILETABS (cu, symtab)
1016 if (symtab == main_symtab)
1018 if (prev_symtab != NULL)
1020 prev_symtab->next = main_symtab->next;
1021 main_symtab->next = COMPUNIT_FILETABS (cu);
1022 COMPUNIT_FILETABS (cu) = main_symtab;
1026 prev_symtab = symtab;
1028 gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
1031 /* Fill out the compunit symtab. */
1033 if (m_comp_dir != NULL)
1035 /* Reallocate the dirname on the symbol obstack. */
1036 const char *comp_dir = m_comp_dir.get ();
1037 COMPUNIT_DIRNAME (cu)
1038 = (const char *) obstack_copy0 (&m_objfile->objfile_obstack,
1039 comp_dir, strlen (comp_dir));
1042 /* Save the debug format string (if any) in the symtab. */
1043 COMPUNIT_DEBUGFORMAT (cu) = m_debugformat;
1045 /* Similarly for the producer. */
1046 COMPUNIT_PRODUCER (cu) = m_producer;
1048 COMPUNIT_BLOCKVECTOR (cu) = blockvector;
1050 struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1052 set_block_compunit_symtab (b, cu);
1055 COMPUNIT_BLOCK_LINE_SECTION (cu) = section;
1057 COMPUNIT_MACRO_TABLE (cu) = release_macros ();
1059 /* Default any symbols without a specified symtab to the primary symtab. */
1063 /* The main source file's symtab. */
1064 symtab = COMPUNIT_FILETABS (cu);
1066 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1068 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1070 struct dict_iterator iter;
1072 /* Inlined functions may have symbols not in the global or
1073 static symbol lists. */
1074 if (BLOCK_FUNCTION (block) != NULL)
1075 if (symbol_symtab (BLOCK_FUNCTION (block)) == NULL)
1076 symbol_set_symtab (BLOCK_FUNCTION (block), symtab);
1078 /* Note that we only want to fix up symbols from the local
1079 blocks, not blocks coming from included symtabs. That is why
1080 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1081 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
1082 if (symbol_symtab (sym) == NULL)
1083 symbol_set_symtab (sym, symtab);
1087 add_compunit_symtab_to_objfile (cu);
1092 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1093 as value returned by end_symtab_get_static_block.
1095 SECTION is the same as for end_symtab: the section number
1096 (in objfile->section_offsets) of the blockvector and linetable.
1098 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1101 struct compunit_symtab *
1102 buildsym_compunit::end_symtab_from_static_block (struct block *static_block,
1103 int section, int expandable)
1105 struct compunit_symtab *cu;
1107 if (static_block == NULL)
1109 /* Handle the "no blockvector" case.
1110 When this happens there is nothing to record, so there's nothing
1111 to do: memory will be freed up later.
1113 Note: We won't be adding a compunit to the objfile's list of
1114 compunits, so there's nothing to unchain. However, since each symtab
1115 is added to the objfile's obstack we can't free that space.
1116 We could do better, but this is believed to be a sufficiently rare
1121 cu = end_symtab_with_blockvector (static_block, section, expandable);
1126 /* Finish the symbol definitions for one main source file, close off
1127 all the lexical contexts for that file (creating struct block's for
1128 them), then make the struct symtab for that file and put it in the
1131 END_ADDR is the address of the end of the file's text. SECTION is
1132 the section number (in objfile->section_offsets) of the blockvector
1135 Note that it is possible for end_symtab() to return NULL. In
1136 particular, for the DWARF case at least, it will return NULL when
1137 it finds a compilation unit that has exactly one DIE, a
1138 TAG_compile_unit DIE. This can happen when we link in an object
1139 file that was compiled from an empty source file. Returning NULL
1140 is probably not the correct thing to do, because then gdb will
1141 never know about this empty file (FIXME).
1143 If you need to modify STATIC_BLOCK before it is finalized you should
1144 call end_symtab_get_static_block and end_symtab_from_static_block
1147 struct compunit_symtab *
1148 buildsym_compunit::end_symtab (CORE_ADDR end_addr, int section)
1150 struct block *static_block;
1152 static_block = end_symtab_get_static_block (end_addr, 0, 0);
1153 return end_symtab_from_static_block (static_block, section, 0);
1156 /* Same as end_symtab except create a symtab that can be later added to. */
1158 struct compunit_symtab *
1159 buildsym_compunit::end_expandable_symtab (CORE_ADDR end_addr, int section)
1161 struct block *static_block;
1163 static_block = end_symtab_get_static_block (end_addr, 1, 0);
1164 return end_symtab_from_static_block (static_block, section, 1);
1167 /* Subroutine of augment_type_symtab to simplify it.
1168 Attach the main source file's symtab to all symbols in PENDING_LIST that
1172 set_missing_symtab (struct pending *pending_list,
1173 struct compunit_symtab *cu)
1175 struct pending *pending;
1178 for (pending = pending_list; pending != NULL; pending = pending->next)
1180 for (i = 0; i < pending->nsyms; ++i)
1182 if (symbol_symtab (pending->symbol[i]) == NULL)
1183 symbol_set_symtab (pending->symbol[i], COMPUNIT_FILETABS (cu));
1188 /* Same as end_symtab, but for the case where we're adding more symbols
1189 to an existing symtab that is known to contain only type information.
1190 This is the case for DWARF4 Type Units. */
1193 buildsym_compunit::augment_type_symtab ()
1195 struct compunit_symtab *cust = m_compunit_symtab;
1196 const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);
1198 if (!m_context_stack.empty ())
1199 complaint (_("Context stack not empty in augment_type_symtab"));
1200 if (m_pending_blocks != NULL)
1201 complaint (_("Blocks in a type symtab"));
1202 if (m_pending_macros != NULL)
1203 complaint (_("Macro in a type symtab"));
1204 if (m_have_line_numbers)
1205 complaint (_("Line numbers recorded in a type symtab"));
1207 if (m_file_symbols != NULL)
1209 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1211 /* First mark any symbols without a specified symtab as belonging
1212 to the primary symtab. */
1213 set_missing_symtab (m_file_symbols, cust);
1215 dict_add_pending (BLOCK_DICT (block), m_file_symbols);
1218 if (m_global_symbols != NULL)
1220 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1222 /* First mark any symbols without a specified symtab as belonging
1223 to the primary symtab. */
1224 set_missing_symtab (m_global_symbols, cust);
1226 dict_add_pending (BLOCK_DICT (block),
1231 /* Push a context block. Args are an identifying nesting level
1232 (checkable when you pop it), and the starting PC address of this
1235 struct context_stack *
1236 buildsym_compunit::push_context (int desc, CORE_ADDR valu)
1238 m_context_stack.emplace_back ();
1239 struct context_stack *newobj = &m_context_stack.back ();
1241 newobj->depth = desc;
1242 newobj->locals = m_local_symbols;
1243 newobj->old_blocks = m_pending_blocks;
1244 newobj->start_addr = valu;
1245 newobj->local_using_directives = m_local_using_directives;
1246 newobj->name = NULL;
1248 m_local_symbols = NULL;
1249 m_local_using_directives = NULL;
1254 /* Pop a context block. Returns the address of the context block just
1257 struct context_stack
1258 buildsym_compunit::pop_context ()
1260 gdb_assert (!m_context_stack.empty ());
1261 struct context_stack result = m_context_stack.back ();
1262 m_context_stack.pop_back ();