1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2013 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/>. */
19 /* This module provides subroutines used for creating and adding to
20 the symbol table. These routines are called from various symbol-
21 file-reading routines.
23 Routines to support specific debugging information formats (stabs,
24 DWARF, etc) belong somewhere else. */
28 #include "gdb_obstack.h"
33 #include "gdb_assert.h"
34 #include "complaints.h"
35 #include "gdb_string.h"
36 #include "expression.h" /* For "enum exp_opcode" used by... */
38 #include "filenames.h" /* For DOSish file names. */
40 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
42 #include "cp-support.h"
43 #include "dictionary.h"
46 /* Ask buildsym.h to define the vars it normally declares `extern'. */
49 #include "buildsym.h" /* Our own declarations. */
52 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
53 questionable--see comment where we call them). */
55 #include "stabsread.h"
57 /* List of subfiles. */
59 static struct subfile *subfiles;
61 /* List of free `struct pending' structures for reuse. */
63 static struct pending *free_pendings;
65 /* Non-zero if symtab has line number info. This prevents an
66 otherwise empty symtab from being tossed. */
68 static int have_line_numbers;
70 /* The mutable address map for the compilation unit whose symbols
71 we're currently reading. The symtabs' shared blockvector will
72 point to a fixed copy of this. */
73 static struct addrmap *pending_addrmap;
75 /* The obstack on which we allocate pending_addrmap.
76 If pending_addrmap is NULL, this is uninitialized; otherwise, it is
77 initialized (and holds pending_addrmap). */
78 static struct obstack pending_addrmap_obstack;
80 /* Non-zero if we recorded any ranges in the addrmap that are
81 different from those in the blockvector already. We set this to
82 zero when we start processing a symfile, and if it's still zero at
83 the end, then we just toss the addrmap. */
84 static int pending_addrmap_interesting;
86 /* An obstack used for allocating pending blocks. */
88 static struct obstack pending_block_obstack;
90 /* List of blocks already made (lexical contexts already closed).
91 This is used at the end to make the blockvector. */
95 struct pending_block *next;
99 /* Pointer to the head of a linked list of symbol blocks which have
100 already been finalized (lexical contexts already closed) and which
101 are just waiting to be built into a blockvector when finalizing the
102 associated symtab. */
104 static struct pending_block *pending_blocks;
106 static int compare_line_numbers (const void *ln1p, const void *ln2p);
108 static void record_pending_block (struct objfile *objfile,
110 struct pending_block *opblock);
113 /* Initial sizes of data structures. These are realloc'd larger if
114 needed, and realloc'd down to the size actually used, when
117 #define INITIAL_CONTEXT_STACK_SIZE 10
118 #define INITIAL_LINE_VECTOR_LENGTH 1000
121 /* Maintain the lists of symbols and blocks. */
123 /* Add a symbol to one of the lists of symbols. */
126 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
128 struct pending *link;
130 /* If this is an alias for another symbol, don't add it. */
131 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
134 /* We keep PENDINGSIZE symbols in each link of the list. If we
135 don't have a link with room in it, add a new link. */
136 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
140 link = free_pendings;
141 free_pendings = link->next;
145 link = (struct pending *) xmalloc (sizeof (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 /* At end of reading syms, or in case of quit, really free as many
182 `struct pending's as we can easily find. */
185 really_free_pendings (void *dummy)
187 struct pending *next, *next1;
189 for (next = free_pendings; next; next = next1)
192 xfree ((void *) next);
194 free_pendings = NULL;
196 free_pending_blocks ();
198 for (next = file_symbols; next != NULL; next = next1)
201 xfree ((void *) next);
205 for (next = global_symbols; next != NULL; next = next1)
208 xfree ((void *) next);
210 global_symbols = NULL;
213 free_macro_table (pending_macros);
217 obstack_free (&pending_addrmap_obstack, NULL);
218 pending_addrmap = NULL;
222 /* This function is called to discard any pending blocks. */
225 free_pending_blocks (void)
227 if (pending_blocks != NULL)
229 obstack_free (&pending_block_obstack, NULL);
230 pending_blocks = NULL;
234 /* Take one of the lists of symbols and make a block from it. Keep
235 the order the symbols have in the list (reversed from the input
236 file). Put the block on the list of pending blocks. */
238 static struct block *
239 finish_block_internal (struct symbol *symbol, struct pending **listhead,
240 struct pending_block *old_blocks,
241 CORE_ADDR start, CORE_ADDR end,
242 struct objfile *objfile,
243 int is_global, int expandable)
245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
246 struct pending *next, *next1;
248 struct pending_block *pblock;
249 struct pending_block *opblock;
252 ? allocate_global_block (&objfile->objfile_obstack)
253 : allocate_block (&objfile->objfile_obstack));
257 BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
264 BLOCK_DICT (block) = dict_create_hashed_expandable ();
265 dict_add_pending (BLOCK_DICT (block), *listhead);
270 dict_create_hashed (&objfile->objfile_obstack, *listhead);
274 BLOCK_START (block) = start;
275 BLOCK_END (block) = end;
277 /* Put the block in as the value of the symbol that names it. */
281 struct type *ftype = SYMBOL_TYPE (symbol);
282 struct dict_iterator iter;
283 SYMBOL_BLOCK_VALUE (symbol) = block;
284 BLOCK_FUNCTION (block) = symbol;
286 if (TYPE_NFIELDS (ftype) <= 0)
288 /* No parameter type information is recorded with the
289 function's type. Set that from the type of the
290 parameter symbols. */
291 int nparams = 0, iparams;
294 /* Here we want to directly access the dictionary, because
295 we haven't fully initialized the block yet. */
296 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
298 if (SYMBOL_IS_ARGUMENT (sym))
303 TYPE_NFIELDS (ftype) = nparams;
304 TYPE_FIELDS (ftype) = (struct field *)
305 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
308 /* Here we want to directly access the dictionary, because
309 we haven't fully initialized the block yet. */
310 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
312 if (iparams == nparams)
315 if (SYMBOL_IS_ARGUMENT (sym))
317 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
318 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
327 BLOCK_FUNCTION (block) = NULL;
330 /* Now "free" the links of the list, and empty the list. */
332 for (next = *listhead; next; next = next1)
335 next->next = free_pendings;
336 free_pendings = next;
340 /* Check to be sure that the blocks have an end address that is
341 greater than starting address. */
343 if (BLOCK_END (block) < BLOCK_START (block))
347 complaint (&symfile_complaints,
348 _("block end address less than block "
349 "start address in %s (patched it)"),
350 SYMBOL_PRINT_NAME (symbol));
354 complaint (&symfile_complaints,
355 _("block end address %s less than block "
356 "start address %s (patched it)"),
357 paddress (gdbarch, BLOCK_END (block)),
358 paddress (gdbarch, BLOCK_START (block)));
360 /* Better than nothing. */
361 BLOCK_END (block) = BLOCK_START (block);
364 /* Install this block as the superblock of all blocks made since the
365 start of this scope that don't have superblocks yet. */
368 for (pblock = pending_blocks;
369 pblock && pblock != old_blocks;
370 pblock = pblock->next)
372 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
374 /* Check to be sure the blocks are nested as we receive
375 them. If the compiler/assembler/linker work, this just
376 burns a small amount of time.
378 Skip blocks which correspond to a function; they're not
379 physically nested inside this other blocks, only
381 if (BLOCK_FUNCTION (pblock->block) == NULL
382 && (BLOCK_START (pblock->block) < BLOCK_START (block)
383 || BLOCK_END (pblock->block) > BLOCK_END (block)))
387 complaint (&symfile_complaints,
388 _("inner block not inside outer block in %s"),
389 SYMBOL_PRINT_NAME (symbol));
393 complaint (&symfile_complaints,
394 _("inner block (%s-%s) not "
395 "inside outer block (%s-%s)"),
396 paddress (gdbarch, BLOCK_START (pblock->block)),
397 paddress (gdbarch, BLOCK_END (pblock->block)),
398 paddress (gdbarch, BLOCK_START (block)),
399 paddress (gdbarch, BLOCK_END (block)));
401 if (BLOCK_START (pblock->block) < BLOCK_START (block))
402 BLOCK_START (pblock->block) = BLOCK_START (block);
403 if (BLOCK_END (pblock->block) > BLOCK_END (block))
404 BLOCK_END (pblock->block) = BLOCK_END (block);
406 BLOCK_SUPERBLOCK (pblock->block) = block;
411 block_set_using (block, using_directives, &objfile->objfile_obstack);
412 using_directives = NULL;
414 record_pending_block (objfile, block, opblock);
420 finish_block (struct symbol *symbol, struct pending **listhead,
421 struct pending_block *old_blocks,
422 CORE_ADDR start, CORE_ADDR end,
423 struct objfile *objfile)
425 return finish_block_internal (symbol, listhead, old_blocks,
426 start, end, objfile, 0, 0);
429 /* Record BLOCK on the list of all blocks in the file. Put it after
430 OPBLOCK, or at the beginning if opblock is NULL. This puts the
431 block in the list after all its subblocks.
433 Allocate the pending block struct in the objfile_obstack to save
434 time. This wastes a little space. FIXME: Is it worth it? */
437 record_pending_block (struct objfile *objfile, struct block *block,
438 struct pending_block *opblock)
440 struct pending_block *pblock;
442 if (pending_blocks == NULL)
443 obstack_init (&pending_block_obstack);
445 pblock = (struct pending_block *)
446 obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
447 pblock->block = block;
450 pblock->next = opblock->next;
451 opblock->next = pblock;
455 pblock->next = pending_blocks;
456 pending_blocks = pblock;
461 /* Record that the range of addresses from START to END_INCLUSIVE
462 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
463 addresses must be set already. You must apply this function to all
464 BLOCK's children before applying it to BLOCK.
466 If a call to this function complicates the picture beyond that
467 already provided by BLOCK_START and BLOCK_END, then we create an
468 address map for the block. */
470 record_block_range (struct block *block,
471 CORE_ADDR start, CORE_ADDR end_inclusive)
473 /* If this is any different from the range recorded in the block's
474 own BLOCK_START and BLOCK_END, then note that the address map has
475 become interesting. Note that even if this block doesn't have
476 any "interesting" ranges, some later block might, so we still
477 need to record this block in the addrmap. */
478 if (start != BLOCK_START (block)
479 || end_inclusive + 1 != BLOCK_END (block))
480 pending_addrmap_interesting = 1;
482 if (! pending_addrmap)
484 obstack_init (&pending_addrmap_obstack);
485 pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
488 addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
492 static struct blockvector *
493 make_blockvector (struct objfile *objfile)
495 struct pending_block *next;
496 struct blockvector *blockvector;
499 /* Count the length of the list of blocks. */
501 for (next = pending_blocks, i = 0; next; next = next->next, i++)
505 blockvector = (struct blockvector *)
506 obstack_alloc (&objfile->objfile_obstack,
507 (sizeof (struct blockvector)
508 + (i - 1) * sizeof (struct block *)));
510 /* Copy the blocks into the blockvector. This is done in reverse
511 order, which happens to put the blocks into the proper order
512 (ascending starting address). finish_block has hair to insert
513 each block into the list after its subblocks in order to make
514 sure this is true. */
516 BLOCKVECTOR_NBLOCKS (blockvector) = i;
517 for (next = pending_blocks; next; next = next->next)
519 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
522 free_pending_blocks ();
524 /* If we needed an address map for this symtab, record it in the
526 if (pending_addrmap && pending_addrmap_interesting)
527 BLOCKVECTOR_MAP (blockvector)
528 = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
530 BLOCKVECTOR_MAP (blockvector) = 0;
532 /* Some compilers output blocks in the wrong order, but we depend on
533 their being in the right order so we can binary search. Check the
534 order and moan about it.
535 Note: Remember that the first two blocks are the global and static
536 blocks. We could special case that fact and begin checking at block 2.
537 To avoid making that assumption we do not. */
538 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
540 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
542 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
543 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
546 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
548 complaint (&symfile_complaints, _("block at %s out of order"),
549 hex_string ((LONGEST) start));
554 return (blockvector);
557 /* Start recording information about source code that came from an
558 included (or otherwise merged-in) source file with a different
559 name. NAME is the name of the file (cannot be NULL), DIRNAME is
560 the directory in which the file was compiled (or NULL if not
564 start_subfile (const char *name, const char *dirname)
566 struct subfile *subfile;
568 /* See if this subfile is already known as a subfile of the current
571 for (subfile = subfiles; subfile; subfile = subfile->next)
575 /* If NAME is an absolute path, and this subfile is not, then
576 attempt to create an absolute path to compare. */
577 if (IS_ABSOLUTE_PATH (name)
578 && !IS_ABSOLUTE_PATH (subfile->name)
579 && subfile->dirname != NULL)
580 subfile_name = concat (subfile->dirname, SLASH_STRING,
581 subfile->name, (char *) NULL);
583 subfile_name = subfile->name;
585 if (FILENAME_CMP (subfile_name, name) == 0)
587 current_subfile = subfile;
588 if (subfile_name != subfile->name)
589 xfree (subfile_name);
592 if (subfile_name != subfile->name)
593 xfree (subfile_name);
596 /* This subfile is not known. Add an entry for it. Make an entry
597 for this subfile in the list of all subfiles of the current main
600 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
601 memset ((char *) subfile, 0, sizeof (struct subfile));
602 subfile->next = subfiles;
604 current_subfile = subfile;
606 /* Save its name and compilation directory name. */
607 subfile->name = xstrdup (name);
608 subfile->dirname = (dirname == NULL) ? NULL : xstrdup (dirname);
610 /* Initialize line-number recording for this subfile. */
611 subfile->line_vector = NULL;
613 /* Default the source language to whatever can be deduced from the
614 filename. If nothing can be deduced (such as for a C/C++ include
615 file with a ".h" extension), then inherit whatever language the
616 previous subfile had. This kludgery is necessary because there
617 is no standard way in some object formats to record the source
618 language. Also, when symtabs are allocated we try to deduce a
619 language then as well, but it is too late for us to use that
620 information while reading symbols, since symtabs aren't allocated
621 until after all the symbols have been processed for a given
624 subfile->language = deduce_language_from_filename (subfile->name);
625 if (subfile->language == language_unknown
626 && subfile->next != NULL)
628 subfile->language = subfile->next->language;
631 /* Initialize the debug format string to NULL. We may supply it
632 later via a call to record_debugformat. */
633 subfile->debugformat = NULL;
635 /* Similarly for the producer. */
636 subfile->producer = NULL;
638 /* If the filename of this subfile ends in .C, then change the
639 language of any pending subfiles from C to C++. We also accept
640 any other C++ suffixes accepted by deduce_language_from_filename. */
641 /* Likewise for f2c. */
646 enum language sublang = deduce_language_from_filename (subfile->name);
648 if (sublang == language_cplus || sublang == language_fortran)
649 for (s = subfiles; s != NULL; s = s->next)
650 if (s->language == language_c)
651 s->language = sublang;
654 /* And patch up this file if necessary. */
655 if (subfile->language == language_c
656 && subfile->next != NULL
657 && (subfile->next->language == language_cplus
658 || subfile->next->language == language_fortran))
660 subfile->language = subfile->next->language;
664 /* For stabs readers, the first N_SO symbol is assumed to be the
665 source file name, and the subfile struct is initialized using that
666 assumption. If another N_SO symbol is later seen, immediately
667 following the first one, then the first one is assumed to be the
668 directory name and the second one is really the source file name.
670 So we have to patch up the subfile struct by moving the old name
671 value to dirname and remembering the new name. Some sanity
672 checking is performed to ensure that the state of the subfile
673 struct is reasonable and that the old name we are assuming to be a
674 directory name actually is (by checking for a trailing '/'). */
677 patch_subfile_names (struct subfile *subfile, char *name)
679 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
680 && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
682 subfile->dirname = subfile->name;
683 subfile->name = xstrdup (name);
684 last_source_file = name;
686 /* Default the source language to whatever can be deduced from
687 the filename. If nothing can be deduced (such as for a C/C++
688 include file with a ".h" extension), then inherit whatever
689 language the previous subfile had. This kludgery is
690 necessary because there is no standard way in some object
691 formats to record the source language. Also, when symtabs
692 are allocated we try to deduce a language then as well, but
693 it is too late for us to use that information while reading
694 symbols, since symtabs aren't allocated until after all the
695 symbols have been processed for a given source file. */
697 subfile->language = deduce_language_from_filename (subfile->name);
698 if (subfile->language == language_unknown
699 && subfile->next != NULL)
701 subfile->language = subfile->next->language;
706 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
707 switching source files (different subfiles, as we call them) within
708 one object file, but using a stack rather than in an arbitrary
714 struct subfile_stack *tem
715 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
717 tem->next = subfile_stack;
719 if (current_subfile == NULL || current_subfile->name == NULL)
721 internal_error (__FILE__, __LINE__,
722 _("failed internal consistency check"));
724 tem->name = current_subfile->name;
731 struct subfile_stack *link = subfile_stack;
735 internal_error (__FILE__, __LINE__,
736 _("failed internal consistency check"));
739 subfile_stack = link->next;
740 xfree ((void *) link);
744 /* Add a linetable entry for line number LINE and address PC to the
745 line vector for SUBFILE. */
748 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
750 struct linetable_entry *e;
752 /* Ignore the dummy line number in libg.o */
758 /* Make sure line vector exists and is big enough. */
759 if (!subfile->line_vector)
761 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
762 subfile->line_vector = (struct linetable *)
763 xmalloc (sizeof (struct linetable)
764 + subfile->line_vector_length * sizeof (struct linetable_entry));
765 subfile->line_vector->nitems = 0;
766 have_line_numbers = 1;
769 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
771 subfile->line_vector_length *= 2;
772 subfile->line_vector = (struct linetable *)
773 xrealloc ((char *) subfile->line_vector,
774 (sizeof (struct linetable)
775 + (subfile->line_vector_length
776 * sizeof (struct linetable_entry))));
779 /* Normally, we treat lines as unsorted. But the end of sequence
780 marker is special. We sort line markers at the same PC by line
781 number, so end of sequence markers (which have line == 0) appear
782 first. This is right if the marker ends the previous function,
783 and there is no padding before the next function. But it is
784 wrong if the previous line was empty and we are now marking a
785 switch to a different subfile. We must leave the end of sequence
786 marker at the end of this group of lines, not sort the empty line
787 to after the marker. The easiest way to accomplish this is to
788 delete any empty lines from our table, if they are followed by
789 end of sequence markers. All we lose is the ability to set
790 breakpoints at some lines which contain no instructions
792 if (line == 0 && subfile->line_vector->nitems > 0)
794 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
795 while (subfile->line_vector->nitems > 0 && e->pc == pc)
798 subfile->line_vector->nitems--;
802 e = subfile->line_vector->item + subfile->line_vector->nitems++;
807 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
810 compare_line_numbers (const void *ln1p, const void *ln2p)
812 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
813 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
815 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
816 Please keep it that way. */
817 if (ln1->pc < ln2->pc)
820 if (ln1->pc > ln2->pc)
823 /* If pc equal, sort by line. I'm not sure whether this is optimum
824 behavior (see comment at struct linetable in symtab.h). */
825 return ln1->line - ln2->line;
828 /* Start a new symtab for a new source file. Called, for example,
829 when a stabs symbol of type N_SO is seen, or when a DWARF
830 TAG_compile_unit DIE is seen. It indicates the start of data for
831 one original source file.
833 NAME is the name of the file (cannot be NULL). DIRNAME is the directory in
834 which the file was compiled (or NULL if not known). START_ADDR is the
835 lowest address of objects in the file (or 0 if not known). */
838 start_symtab (char *name, char *dirname, CORE_ADDR start_addr)
840 restart_symtab (start_addr);
841 last_source_file = name;
842 start_subfile (name, dirname);
845 /* Restart compilation for a symtab.
846 This is used when a symtab is built from multiple sources.
847 The symtab is first built with start_symtab and then for each additional
848 piece call restart_symtab. */
851 restart_symtab (CORE_ADDR start_addr)
853 last_source_file = NULL;
854 last_source_start_addr = start_addr;
856 global_symbols = NULL;
858 have_line_numbers = 0;
860 /* Context stack is initially empty. Allocate first one with room
861 for 10 levels; reuse it forever afterward. */
862 if (context_stack == NULL)
864 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
865 context_stack = (struct context_stack *)
866 xmalloc (context_stack_size * sizeof (struct context_stack));
868 context_stack_depth = 0;
870 /* We shouldn't have any address map at this point. */
871 gdb_assert (! pending_addrmap);
873 /* Initialize the list of sub source files with one entry for this
874 file (the top-level source file). */
876 current_subfile = NULL;
879 /* Subroutine of end_symtab to simplify it. Look for a subfile that
880 matches the main source file's basename. If there is only one, and
881 if the main source file doesn't have any symbol or line number
882 information, then copy this file's symtab and line_vector to the
883 main source file's subfile and discard the other subfile. This can
884 happen because of a compiler bug or from the user playing games
885 with #line or from things like a distributed build system that
886 manipulates the debug info. */
889 watch_main_source_file_lossage (void)
891 struct subfile *mainsub, *subfile;
893 /* Find the main source file.
894 This loop could be eliminated if start_symtab saved it for us. */
896 for (subfile = subfiles; subfile; subfile = subfile->next)
898 /* The main subfile is guaranteed to be the last one. */
899 if (subfile->next == NULL)
903 /* If the main source file doesn't have any line number or symbol
904 info, look for an alias in another subfile.
906 We have to watch for mainsub == NULL here. It's a quirk of
907 end_symtab, it can return NULL so there may not be a main
911 && mainsub->line_vector == NULL
912 && mainsub->symtab == NULL)
914 const char *mainbase = lbasename (mainsub->name);
916 struct subfile *prevsub;
917 struct subfile *mainsub_alias = NULL;
918 struct subfile *prev_mainsub_alias = NULL;
921 for (subfile = subfiles;
922 /* Stop before we get to the last one. */
924 subfile = subfile->next)
926 if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
929 mainsub_alias = subfile;
930 prev_mainsub_alias = prevsub;
937 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
939 /* Found a match for the main source file.
940 Copy its line_vector and symtab to the main subfile
941 and then discard it. */
943 mainsub->line_vector = mainsub_alias->line_vector;
944 mainsub->line_vector_length = mainsub_alias->line_vector_length;
945 mainsub->symtab = mainsub_alias->symtab;
947 if (prev_mainsub_alias == NULL)
948 subfiles = mainsub_alias->next;
950 prev_mainsub_alias->next = mainsub_alias->next;
951 xfree (mainsub_alias);
956 /* Helper function for qsort. Parameters are `struct block *' pointers,
957 function sorts them in descending order by their BLOCK_START. */
960 block_compar (const void *ap, const void *bp)
962 const struct block *a = *(const struct block **) ap;
963 const struct block *b = *(const struct block **) bp;
965 return ((BLOCK_START (b) > BLOCK_START (a))
966 - (BLOCK_START (b) < BLOCK_START (a)));
969 /* Reset globals used to build symtabs. */
972 reset_symtab_globals (void)
974 last_source_file = NULL;
975 current_subfile = NULL;
976 pending_macros = NULL;
979 obstack_free (&pending_addrmap_obstack, NULL);
980 pending_addrmap = NULL;
984 /* Implementation of the first part of end_symtab. It allows modifying
985 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
986 If the returned value is NULL there is no blockvector created for
987 this symtab (you still must call end_symtab_from_static_block).
989 END_ADDR is the same as for end_symtab: the address of the end of the
992 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
995 If REQUIRED is non-zero, then a symtab is created even if it does
996 not contain any symbols. */
999 end_symtab_get_static_block (CORE_ADDR end_addr, struct objfile *objfile,
1000 int expandable, int required)
1002 /* Finish the lexical context of the last function in the file; pop
1003 the context stack. */
1005 if (context_stack_depth > 0)
1007 struct context_stack *cstk = pop_context ();
1009 /* Make a block for the local symbols within. */
1010 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1011 cstk->start_addr, end_addr, objfile);
1013 if (context_stack_depth > 0)
1015 /* This is said to happen with SCO. The old coffread.c
1016 code simply emptied the context stack, so we do the
1017 same. FIXME: Find out why it is happening. This is not
1018 believed to happen in most cases (even for coffread.c);
1019 it used to be an abort(). */
1020 complaint (&symfile_complaints,
1021 _("Context stack not empty in end_symtab"));
1022 context_stack_depth = 0;
1026 /* Reordered executables may have out of order pending blocks; if
1027 OBJF_REORDERED is true, then sort the pending blocks. */
1029 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
1032 struct pending_block *pb;
1033 struct block **barray, **bp;
1034 struct cleanup *back_to;
1036 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1039 barray = xmalloc (sizeof (*barray) * count);
1040 back_to = make_cleanup (xfree, barray);
1043 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1046 qsort (barray, count, sizeof (*barray), block_compar);
1049 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1052 do_cleanups (back_to);
1055 /* Cleanup any undefined types that have been left hanging around
1056 (this needs to be done before the finish_blocks so that
1057 file_symbols is still good).
1059 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
1060 specific, but harmless for other symbol readers, since on gdb
1061 startup or when finished reading stabs, the state is set so these
1062 are no-ops. FIXME: Is this handled right in case of QUIT? Can
1063 we make this cleaner? */
1065 cleanup_undefined_stabs_types (objfile);
1066 finish_global_stabs (objfile);
1069 && pending_blocks == NULL
1070 && file_symbols == NULL
1071 && global_symbols == NULL
1072 && have_line_numbers == 0
1073 && pending_macros == NULL)
1075 /* Ignore symtabs that have no functions with real debugging info. */
1080 /* Define the STATIC_BLOCK. */
1081 return finish_block_internal (NULL, &file_symbols, NULL,
1082 last_source_start_addr, end_addr, objfile,
1087 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1088 as value returned by end_symtab_get_static_block.
1090 SECTION is the same as for end_symtab: the section number
1091 (in objfile->section_offsets) of the blockvector and linetable.
1093 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1097 end_symtab_from_static_block (struct block *static_block,
1098 struct objfile *objfile, int section,
1101 struct symtab *symtab = NULL;
1102 struct blockvector *blockvector;
1103 struct subfile *subfile;
1104 struct subfile *nextsub;
1106 if (static_block == NULL)
1108 /* Ignore symtabs that have no functions with real debugging info. */
1113 CORE_ADDR end_addr = BLOCK_END (static_block);
1115 /* Define after STATIC_BLOCK also GLOBAL_BLOCK, and build the
1117 finish_block_internal (NULL, &global_symbols, NULL,
1118 last_source_start_addr, end_addr, objfile,
1120 blockvector = make_blockvector (objfile);
1123 /* Read the line table if it has to be read separately. */
1124 if (objfile->sf->sym_read_linetable != NULL)
1125 objfile->sf->sym_read_linetable ();
1127 /* Handle the case where the debug info specifies a different path
1128 for the main source file. It can cause us to lose track of its
1129 line number information. */
1130 watch_main_source_file_lossage ();
1132 /* Now create the symtab objects proper, one for each subfile. */
1133 /* (The main file is the last one on the chain.) */
1135 for (subfile = subfiles; subfile; subfile = nextsub)
1137 int linetablesize = 0;
1140 /* If we have blocks of symbols, make a symtab. Otherwise, just
1141 ignore this file and any line number info in it. */
1144 if (subfile->line_vector)
1146 linetablesize = sizeof (struct linetable) +
1147 subfile->line_vector->nitems * sizeof (struct linetable_entry);
1149 /* Like the pending blocks, the line table may be
1150 scrambled in reordered executables. Sort it if
1151 OBJF_REORDERED is true. */
1152 if (objfile->flags & OBJF_REORDERED)
1153 qsort (subfile->line_vector->item,
1154 subfile->line_vector->nitems,
1155 sizeof (struct linetable_entry), compare_line_numbers);
1158 /* Now, allocate a symbol table. */
1159 if (subfile->symtab == NULL)
1160 symtab = allocate_symtab (subfile->name, objfile);
1162 symtab = subfile->symtab;
1164 /* Fill in its components. */
1165 symtab->blockvector = blockvector;
1166 symtab->macro_table = pending_macros;
1167 if (subfile->line_vector)
1169 /* Reallocate the line table on the symbol obstack. */
1170 symtab->linetable = (struct linetable *)
1171 obstack_alloc (&objfile->objfile_obstack, linetablesize);
1172 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
1176 symtab->linetable = NULL;
1178 symtab->block_line_section = section;
1179 if (subfile->dirname)
1181 /* Reallocate the dirname on the symbol obstack. */
1182 symtab->dirname = (char *)
1183 obstack_alloc (&objfile->objfile_obstack,
1184 strlen (subfile->dirname) + 1);
1185 strcpy (symtab->dirname, subfile->dirname);
1189 symtab->dirname = NULL;
1192 /* Use whatever language we have been using for this
1193 subfile, not the one that was deduced in allocate_symtab
1194 from the filename. We already did our own deducing when
1195 we created the subfile, and we may have altered our
1196 opinion of what language it is from things we found in
1198 symtab->language = subfile->language;
1200 /* Save the debug format string (if any) in the symtab. */
1201 symtab->debugformat = subfile->debugformat;
1203 /* Similarly for the producer. */
1204 symtab->producer = subfile->producer;
1206 /* All symtabs for the main file and the subfiles share a
1207 blockvector, so we need to clear primary for everything
1208 but the main file. */
1210 symtab->primary = 0;
1214 if (subfile->symtab)
1216 /* Since we are ignoring that subfile, we also need
1217 to unlink the associated empty symtab that we created.
1218 Otherwise, we can run into trouble because various parts
1219 such as the block-vector are uninitialized whereas
1220 the rest of the code assumes that they are.
1222 We can only unlink the symtab because it was allocated
1223 on the objfile obstack. */
1226 if (objfile->symtabs == subfile->symtab)
1227 objfile->symtabs = objfile->symtabs->next;
1229 ALL_OBJFILE_SYMTABS (objfile, s)
1230 if (s->next == subfile->symtab)
1232 s->next = s->next->next;
1235 subfile->symtab = NULL;
1238 if (subfile->name != NULL)
1240 xfree ((void *) subfile->name);
1242 if (subfile->dirname != NULL)
1244 xfree ((void *) subfile->dirname);
1246 if (subfile->line_vector != NULL)
1248 xfree ((void *) subfile->line_vector);
1251 nextsub = subfile->next;
1252 xfree ((void *) subfile);
1255 /* Set this for the main source file. */
1258 symtab->primary = 1;
1260 if (symtab->blockvector)
1262 struct block *b = BLOCKVECTOR_BLOCK (symtab->blockvector,
1265 set_block_symtab (b, symtab);
1269 /* Default any symbols without a specified symtab to the primary
1275 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1277 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1279 struct dict_iterator iter;
1281 /* Inlined functions may have symbols not in the global or
1282 static symbol lists. */
1283 if (BLOCK_FUNCTION (block) != NULL)
1284 if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
1285 SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
1287 /* Note that we only want to fix up symbols from the local
1288 blocks, not blocks coming from included symtabs. That is why
1289 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1290 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
1291 if (SYMBOL_SYMTAB (sym) == NULL)
1292 SYMBOL_SYMTAB (sym) = symtab;
1296 reset_symtab_globals ();
1301 /* Finish the symbol definitions for one main source file, close off
1302 all the lexical contexts for that file (creating struct block's for
1303 them), then make the struct symtab for that file and put it in the
1306 END_ADDR is the address of the end of the file's text. SECTION is
1307 the section number (in objfile->section_offsets) of the blockvector
1310 Note that it is possible for end_symtab() to return NULL. In
1311 particular, for the DWARF case at least, it will return NULL when
1312 it finds a compilation unit that has exactly one DIE, a
1313 TAG_compile_unit DIE. This can happen when we link in an object
1314 file that was compiled from an empty source file. Returning NULL
1315 is probably not the correct thing to do, because then gdb will
1316 never know about this empty file (FIXME).
1318 If you need to modify STATIC_BLOCK before it is finalized you should
1319 call end_symtab_get_static_block and end_symtab_from_static_block
1323 end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
1325 struct block *static_block;
1327 static_block = end_symtab_get_static_block (end_addr, objfile, 0, 0);
1328 return end_symtab_from_static_block (static_block, objfile, section, 0);
1331 /* Same as end_symtab except create a symtab that can be later added to. */
1334 end_expandable_symtab (CORE_ADDR end_addr, struct objfile *objfile,
1337 struct block *static_block;
1339 static_block = end_symtab_get_static_block (end_addr, objfile, 1, 0);
1340 return end_symtab_from_static_block (static_block, objfile, section, 1);
1343 /* Subroutine of augment_type_symtab to simplify it.
1344 Attach SYMTAB to all symbols in PENDING_LIST that don't have one. */
1347 set_missing_symtab (struct pending *pending_list, struct symtab *symtab)
1349 struct pending *pending;
1352 for (pending = pending_list; pending != NULL; pending = pending->next)
1354 for (i = 0; i < pending->nsyms; ++i)
1356 if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
1357 SYMBOL_SYMTAB (pending->symbol[i]) = symtab;
1362 /* Same as end_symtab, but for the case where we're adding more symbols
1363 to an existing symtab that is known to contain only type information.
1364 This is the case for DWARF4 Type Units. */
1367 augment_type_symtab (struct objfile *objfile, struct symtab *primary_symtab)
1369 struct blockvector *blockvector = primary_symtab->blockvector;
1372 if (context_stack_depth > 0)
1374 complaint (&symfile_complaints,
1375 _("Context stack not empty in augment_type_symtab"));
1376 context_stack_depth = 0;
1378 if (pending_blocks != NULL)
1379 complaint (&symfile_complaints, _("Blocks in a type symtab"));
1380 if (pending_macros != NULL)
1381 complaint (&symfile_complaints, _("Macro in a type symtab"));
1382 if (have_line_numbers)
1383 complaint (&symfile_complaints,
1384 _("Line numbers recorded in a type symtab"));
1386 if (file_symbols != NULL)
1388 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1390 /* First mark any symbols without a specified symtab as belonging
1391 to the primary symtab. */
1392 set_missing_symtab (file_symbols, primary_symtab);
1394 dict_add_pending (BLOCK_DICT (block), file_symbols);
1397 if (global_symbols != NULL)
1399 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1401 /* First mark any symbols without a specified symtab as belonging
1402 to the primary symtab. */
1403 set_missing_symtab (global_symbols, primary_symtab);
1405 dict_add_pending (BLOCK_DICT (block), global_symbols);
1408 reset_symtab_globals ();
1411 /* Push a context block. Args are an identifying nesting level
1412 (checkable when you pop it), and the starting PC address of this
1415 struct context_stack *
1416 push_context (int desc, CORE_ADDR valu)
1418 struct context_stack *new;
1420 if (context_stack_depth == context_stack_size)
1422 context_stack_size *= 2;
1423 context_stack = (struct context_stack *)
1424 xrealloc ((char *) context_stack,
1425 (context_stack_size * sizeof (struct context_stack)));
1428 new = &context_stack[context_stack_depth++];
1430 new->locals = local_symbols;
1431 new->old_blocks = pending_blocks;
1432 new->start_addr = valu;
1433 new->using_directives = using_directives;
1436 local_symbols = NULL;
1437 using_directives = NULL;
1442 /* Pop a context block. Returns the address of the context block just
1445 struct context_stack *
1448 gdb_assert (context_stack_depth > 0);
1449 return (&context_stack[--context_stack_depth]);
1454 /* Compute a small integer hash code for the given name. */
1457 hashname (const char *name)
1459 return (hash(name,strlen(name)) % HASHSIZE);
1464 record_debugformat (const char *format)
1466 current_subfile->debugformat = format;
1470 record_producer (const char *producer)
1472 current_subfile->producer = producer;
1475 /* Merge the first symbol list SRCLIST into the second symbol list
1476 TARGETLIST by repeated calls to add_symbol_to_list(). This
1477 procedure "frees" each link of SRCLIST by adding it to the
1478 free_pendings list. Caller must set SRCLIST to a null list after
1479 calling this function.
1484 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1488 if (!srclist || !*srclist)
1491 /* Merge in elements from current link. */
1492 for (i = 0; i < (*srclist)->nsyms; i++)
1493 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1495 /* Recurse on next. */
1496 merge_symbol_lists (&(*srclist)->next, targetlist);
1498 /* "Free" the current link. */
1499 (*srclist)->next = free_pendings;
1500 free_pendings = (*srclist);
1503 /* Initialize anything that needs initializing when starting to read a
1504 fresh piece of a symbol file, e.g. reading in the stuff
1505 corresponding to a psymtab. */
1508 buildsym_init (void)
1510 free_pendings = NULL;
1511 file_symbols = NULL;
1512 global_symbols = NULL;
1513 pending_blocks = NULL;
1514 pending_macros = NULL;
1515 using_directives = NULL;
1517 /* We shouldn't have any address map at this point. */
1518 gdb_assert (! pending_addrmap);
1519 pending_addrmap_interesting = 0;
1522 /* Initialize anything that needs initializing when a completely new
1523 symbol file is specified (not just adding some symbols from another
1524 file, e.g. a shared library). */
1527 buildsym_new_init (void)