1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986-2014 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 "complaints.h"
34 #include "expression.h" /* For "enum exp_opcode" used by... */
36 #include "filenames.h" /* For DOSish file names. */
38 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
40 #include "cp-support.h"
41 #include "dictionary.h"
44 /* Ask buildsym.h to define the vars it normally declares `extern'. */
47 #include "buildsym.h" /* Our own declarations. */
50 /* For cleanup_undefined_stabs_types and finish_global_stabs (somewhat
51 questionable--see comment where we call them). */
53 #include "stabsread.h"
55 /* The objfile we are currently reading debug info from. */
57 static struct objfile *buildsym_objfile;
59 /* The compilation directory. */
61 static char *buildsym_comp_dir;
63 /* List of subfiles. */
65 static struct subfile *subfiles;
67 /* The "main" subfile.
68 In C this is the ".c" file (and similarly for other languages).
69 This becomes the "primary" symtab of the compilation unit. */
71 static struct subfile *main_subfile;
73 /* List of free `struct pending' structures for reuse. */
75 static struct pending *free_pendings;
77 /* Non-zero if symtab has line number info. This prevents an
78 otherwise empty symtab from being tossed. */
80 static int have_line_numbers;
82 /* The mutable address map for the compilation unit whose symbols
83 we're currently reading. The symtabs' shared blockvector will
84 point to a fixed copy of this. */
85 static struct addrmap *pending_addrmap;
87 /* The obstack on which we allocate pending_addrmap.
88 If pending_addrmap is NULL, this is uninitialized; otherwise, it is
89 initialized (and holds pending_addrmap). */
90 static struct obstack pending_addrmap_obstack;
92 /* Non-zero if we recorded any ranges in the addrmap that are
93 different from those in the blockvector already. We set this to
94 zero when we start processing a symfile, and if it's still zero at
95 the end, then we just toss the addrmap. */
96 static int pending_addrmap_interesting;
98 /* An obstack used for allocating pending blocks. */
100 static struct obstack pending_block_obstack;
102 /* List of blocks already made (lexical contexts already closed).
103 This is used at the end to make the blockvector. */
107 struct pending_block *next;
111 /* Pointer to the head of a linked list of symbol blocks which have
112 already been finalized (lexical contexts already closed) and which
113 are just waiting to be built into a blockvector when finalizing the
114 associated symtab. */
116 static struct pending_block *pending_blocks;
120 struct subfile_stack *next;
124 static struct subfile_stack *subfile_stack;
126 /* The macro table for the compilation unit whose symbols we're
127 currently reading. All the symtabs for the CU will point to this. */
128 static struct macro_table *pending_macros;
130 static int compare_line_numbers (const void *ln1p, const void *ln2p);
132 static void record_pending_block (struct objfile *objfile,
134 struct pending_block *opblock);
136 /* Initial sizes of data structures. These are realloc'd larger if
137 needed, and realloc'd down to the size actually used, when
140 #define INITIAL_CONTEXT_STACK_SIZE 10
141 #define INITIAL_LINE_VECTOR_LENGTH 1000
144 /* Maintain the lists of symbols and blocks. */
146 /* Add a symbol to one of the lists of symbols. */
149 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
151 struct pending *link;
153 /* If this is an alias for another symbol, don't add it. */
154 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
157 /* We keep PENDINGSIZE symbols in each link of the list. If we
158 don't have a link with room in it, add a new link. */
159 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
163 link = free_pendings;
164 free_pendings = link->next;
168 link = (struct pending *) xmalloc (sizeof (struct pending));
171 link->next = *listhead;
176 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
179 /* Find a symbol named NAME on a LIST. NAME need not be
180 '\0'-terminated; LENGTH is the length of the name. */
183 find_symbol_in_list (struct pending *list, char *name, int length)
190 for (j = list->nsyms; --j >= 0;)
192 pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
193 if (*pp == *name && strncmp (pp, name, length) == 0
194 && pp[length] == '\0')
196 return (list->symbol[j]);
204 /* At end of reading syms, or in case of quit, really free as many
205 `struct pending's as we can easily find. */
208 really_free_pendings (void *dummy)
210 struct pending *next, *next1;
212 for (next = free_pendings; next; next = next1)
215 xfree ((void *) next);
217 free_pendings = NULL;
219 free_pending_blocks ();
221 for (next = file_symbols; next != NULL; next = next1)
224 xfree ((void *) next);
228 for (next = global_symbols; next != NULL; next = next1)
231 xfree ((void *) next);
233 global_symbols = NULL;
236 free_macro_table (pending_macros);
240 obstack_free (&pending_addrmap_obstack, NULL);
241 pending_addrmap = NULL;
245 /* This function is called to discard any pending blocks. */
248 free_pending_blocks (void)
250 if (pending_blocks != NULL)
252 obstack_free (&pending_block_obstack, NULL);
253 pending_blocks = NULL;
257 /* Take one of the lists of symbols and make a block from it. Keep
258 the order the symbols have in the list (reversed from the input
259 file). Put the block on the list of pending blocks. */
261 static struct block *
262 finish_block_internal (struct symbol *symbol, struct pending **listhead,
263 struct pending_block *old_blocks,
264 CORE_ADDR start, CORE_ADDR end,
265 int is_global, int expandable)
267 struct objfile *objfile = buildsym_objfile;
268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
269 struct pending *next, *next1;
271 struct pending_block *pblock;
272 struct pending_block *opblock;
275 ? allocate_global_block (&objfile->objfile_obstack)
276 : allocate_block (&objfile->objfile_obstack));
280 BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
287 BLOCK_DICT (block) = dict_create_hashed_expandable ();
288 dict_add_pending (BLOCK_DICT (block), *listhead);
293 dict_create_hashed (&objfile->objfile_obstack, *listhead);
297 BLOCK_START (block) = start;
298 BLOCK_END (block) = end;
300 /* Put the block in as the value of the symbol that names it. */
304 struct type *ftype = SYMBOL_TYPE (symbol);
305 struct dict_iterator iter;
306 SYMBOL_BLOCK_VALUE (symbol) = block;
307 BLOCK_FUNCTION (block) = symbol;
309 if (TYPE_NFIELDS (ftype) <= 0)
311 /* No parameter type information is recorded with the
312 function's type. Set that from the type of the
313 parameter symbols. */
314 int nparams = 0, iparams;
317 /* Here we want to directly access the dictionary, because
318 we haven't fully initialized the block yet. */
319 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
321 if (SYMBOL_IS_ARGUMENT (sym))
326 TYPE_NFIELDS (ftype) = nparams;
327 TYPE_FIELDS (ftype) = (struct field *)
328 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
331 /* Here we want to directly access the dictionary, because
332 we haven't fully initialized the block yet. */
333 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
335 if (iparams == nparams)
338 if (SYMBOL_IS_ARGUMENT (sym))
340 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
341 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
350 BLOCK_FUNCTION (block) = NULL;
353 /* Now "free" the links of the list, and empty the list. */
355 for (next = *listhead; next; next = next1)
358 next->next = free_pendings;
359 free_pendings = next;
363 /* Check to be sure that the blocks have an end address that is
364 greater than starting address. */
366 if (BLOCK_END (block) < BLOCK_START (block))
370 complaint (&symfile_complaints,
371 _("block end address less than block "
372 "start address in %s (patched it)"),
373 SYMBOL_PRINT_NAME (symbol));
377 complaint (&symfile_complaints,
378 _("block end address %s less than block "
379 "start address %s (patched it)"),
380 paddress (gdbarch, BLOCK_END (block)),
381 paddress (gdbarch, BLOCK_START (block)));
383 /* Better than nothing. */
384 BLOCK_END (block) = BLOCK_START (block);
387 /* Install this block as the superblock of all blocks made since the
388 start of this scope that don't have superblocks yet. */
391 for (pblock = pending_blocks;
392 pblock && pblock != old_blocks;
393 pblock = pblock->next)
395 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
397 /* Check to be sure the blocks are nested as we receive
398 them. If the compiler/assembler/linker work, this just
399 burns a small amount of time.
401 Skip blocks which correspond to a function; they're not
402 physically nested inside this other blocks, only
404 if (BLOCK_FUNCTION (pblock->block) == NULL
405 && (BLOCK_START (pblock->block) < BLOCK_START (block)
406 || BLOCK_END (pblock->block) > BLOCK_END (block)))
410 complaint (&symfile_complaints,
411 _("inner block not inside outer block in %s"),
412 SYMBOL_PRINT_NAME (symbol));
416 complaint (&symfile_complaints,
417 _("inner block (%s-%s) not "
418 "inside outer block (%s-%s)"),
419 paddress (gdbarch, BLOCK_START (pblock->block)),
420 paddress (gdbarch, BLOCK_END (pblock->block)),
421 paddress (gdbarch, BLOCK_START (block)),
422 paddress (gdbarch, BLOCK_END (block)));
424 if (BLOCK_START (pblock->block) < BLOCK_START (block))
425 BLOCK_START (pblock->block) = BLOCK_START (block);
426 if (BLOCK_END (pblock->block) > BLOCK_END (block))
427 BLOCK_END (pblock->block) = BLOCK_END (block);
429 BLOCK_SUPERBLOCK (pblock->block) = block;
434 block_set_using (block, using_directives, &objfile->objfile_obstack);
435 using_directives = NULL;
437 record_pending_block (objfile, block, opblock);
443 finish_block (struct symbol *symbol, struct pending **listhead,
444 struct pending_block *old_blocks,
445 CORE_ADDR start, CORE_ADDR end)
447 return finish_block_internal (symbol, listhead, old_blocks,
451 /* Record BLOCK on the list of all blocks in the file. Put it after
452 OPBLOCK, or at the beginning if opblock is NULL. This puts the
453 block in the list after all its subblocks.
455 Allocate the pending block struct in the objfile_obstack to save
456 time. This wastes a little space. FIXME: Is it worth it? */
459 record_pending_block (struct objfile *objfile, struct block *block,
460 struct pending_block *opblock)
462 struct pending_block *pblock;
464 if (pending_blocks == NULL)
465 obstack_init (&pending_block_obstack);
467 pblock = (struct pending_block *)
468 obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
469 pblock->block = block;
472 pblock->next = opblock->next;
473 opblock->next = pblock;
477 pblock->next = pending_blocks;
478 pending_blocks = pblock;
483 /* Record that the range of addresses from START to END_INCLUSIVE
484 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
485 addresses must be set already. You must apply this function to all
486 BLOCK's children before applying it to BLOCK.
488 If a call to this function complicates the picture beyond that
489 already provided by BLOCK_START and BLOCK_END, then we create an
490 address map for the block. */
492 record_block_range (struct block *block,
493 CORE_ADDR start, CORE_ADDR end_inclusive)
495 /* If this is any different from the range recorded in the block's
496 own BLOCK_START and BLOCK_END, then note that the address map has
497 become interesting. Note that even if this block doesn't have
498 any "interesting" ranges, some later block might, so we still
499 need to record this block in the addrmap. */
500 if (start != BLOCK_START (block)
501 || end_inclusive + 1 != BLOCK_END (block))
502 pending_addrmap_interesting = 1;
504 if (! pending_addrmap)
506 obstack_init (&pending_addrmap_obstack);
507 pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
510 addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
514 static struct blockvector *
515 make_blockvector (struct objfile *objfile)
517 struct pending_block *next;
518 struct blockvector *blockvector;
521 /* Count the length of the list of blocks. */
523 for (next = pending_blocks, i = 0; next; next = next->next, i++)
527 blockvector = (struct blockvector *)
528 obstack_alloc (&objfile->objfile_obstack,
529 (sizeof (struct blockvector)
530 + (i - 1) * sizeof (struct block *)));
532 /* Copy the blocks into the blockvector. This is done in reverse
533 order, which happens to put the blocks into the proper order
534 (ascending starting address). finish_block has hair to insert
535 each block into the list after its subblocks in order to make
536 sure this is true. */
538 BLOCKVECTOR_NBLOCKS (blockvector) = i;
539 for (next = pending_blocks; next; next = next->next)
541 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
544 free_pending_blocks ();
546 /* If we needed an address map for this symtab, record it in the
548 if (pending_addrmap && pending_addrmap_interesting)
549 BLOCKVECTOR_MAP (blockvector)
550 = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
552 BLOCKVECTOR_MAP (blockvector) = 0;
554 /* Some compilers output blocks in the wrong order, but we depend on
555 their being in the right order so we can binary search. Check the
556 order and moan about it.
557 Note: Remember that the first two blocks are the global and static
558 blocks. We could special case that fact and begin checking at block 2.
559 To avoid making that assumption we do not. */
560 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
562 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
564 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
565 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
568 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
570 complaint (&symfile_complaints, _("block at %s out of order"),
571 hex_string ((LONGEST) start));
576 return (blockvector);
579 /* Start recording information about source code that came from an
580 included (or otherwise merged-in) source file with a different
581 name. NAME is the name of the file (cannot be NULL). */
584 start_subfile (const char *name)
586 struct subfile *subfile;
588 /* See if this subfile is already known as a subfile of the current
591 for (subfile = subfiles; subfile; subfile = subfile->next)
595 /* If NAME is an absolute path, and this subfile is not, then
596 attempt to create an absolute path to compare. */
597 if (IS_ABSOLUTE_PATH (name)
598 && !IS_ABSOLUTE_PATH (subfile->name)
599 && subfile->dirname != NULL)
600 subfile_name = concat (subfile->dirname, SLASH_STRING,
601 subfile->name, (char *) NULL);
603 subfile_name = subfile->name;
605 if (FILENAME_CMP (subfile_name, name) == 0)
607 current_subfile = subfile;
608 if (subfile_name != subfile->name)
609 xfree (subfile_name);
612 if (subfile_name != subfile->name)
613 xfree (subfile_name);
616 /* This subfile is not known. Add an entry for it. Make an entry
617 for this subfile in the list of all subfiles of the current main
620 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
621 memset ((char *) subfile, 0, sizeof (struct subfile));
622 subfile->next = subfiles;
624 current_subfile = subfile;
626 /* Save its name and compilation directory name. */
627 subfile->name = xstrdup (name);
629 = (buildsym_comp_dir == NULL) ? NULL : xstrdup (buildsym_comp_dir);
631 /* Initialize line-number recording for this subfile. */
632 subfile->line_vector = NULL;
634 /* Default the source language to whatever can be deduced from the
635 filename. If nothing can be deduced (such as for a C/C++ include
636 file with a ".h" extension), then inherit whatever language the
637 previous subfile had. This kludgery is necessary because there
638 is no standard way in some object formats to record the source
639 language. Also, when symtabs are allocated we try to deduce a
640 language then as well, but it is too late for us to use that
641 information while reading symbols, since symtabs aren't allocated
642 until after all the symbols have been processed for a given
645 subfile->language = deduce_language_from_filename (subfile->name);
646 if (subfile->language == language_unknown
647 && subfile->next != NULL)
649 subfile->language = subfile->next->language;
652 /* Initialize the debug format string to NULL. We may supply it
653 later via a call to record_debugformat. */
654 subfile->debugformat = NULL;
656 /* Similarly for the producer. */
657 subfile->producer = NULL;
659 /* If the filename of this subfile ends in .C, then change the
660 language of any pending subfiles from C to C++. We also accept
661 any other C++ suffixes accepted by deduce_language_from_filename. */
662 /* Likewise for f2c. */
667 enum language sublang = deduce_language_from_filename (subfile->name);
669 if (sublang == language_cplus || sublang == language_fortran)
670 for (s = subfiles; s != NULL; s = s->next)
671 if (s->language == language_c)
672 s->language = sublang;
675 /* And patch up this file if necessary. */
676 if (subfile->language == language_c
677 && subfile->next != NULL
678 && (subfile->next->language == language_cplus
679 || subfile->next->language == language_fortran))
681 subfile->language = subfile->next->language;
685 /* Delete the subfiles list. */
688 free_subfiles_list (void)
690 struct subfile *subfile, *nextsub;
692 for (subfile = subfiles; subfile != NULL; subfile = nextsub)
694 nextsub = subfile->next;
695 xfree (subfile->name);
696 xfree (subfile->dirname);
697 xfree (subfile->line_vector);
701 current_subfile = NULL;
705 /* For stabs readers, the first N_SO symbol is assumed to be the
706 source file name, and the subfile struct is initialized using that
707 assumption. If another N_SO symbol is later seen, immediately
708 following the first one, then the first one is assumed to be the
709 directory name and the second one is really the source file name.
711 So we have to patch up the subfile struct by moving the old name
712 value to dirname and remembering the new name. Some sanity
713 checking is performed to ensure that the state of the subfile
714 struct is reasonable and that the old name we are assuming to be a
715 directory name actually is (by checking for a trailing '/'). */
718 patch_subfile_names (struct subfile *subfile, char *name)
720 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
721 && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
723 /* With correct debug info, buildsym_comp_dir should be NULL since
724 subfile->dirname is NULL. However, don't assume this. */
725 xfree (buildsym_comp_dir);
726 buildsym_comp_dir = xstrdup (subfile->name);
728 subfile->dirname = subfile->name;
729 subfile->name = xstrdup (name);
730 set_last_source_file (name);
732 /* Default the source language to whatever can be deduced from
733 the filename. If nothing can be deduced (such as for a C/C++
734 include file with a ".h" extension), then inherit whatever
735 language the previous subfile had. This kludgery is
736 necessary because there is no standard way in some object
737 formats to record the source language. Also, when symtabs
738 are allocated we try to deduce a language then as well, but
739 it is too late for us to use that information while reading
740 symbols, since symtabs aren't allocated until after all the
741 symbols have been processed for a given source file. */
743 subfile->language = deduce_language_from_filename (subfile->name);
744 if (subfile->language == language_unknown
745 && subfile->next != NULL)
747 subfile->language = subfile->next->language;
752 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
753 switching source files (different subfiles, as we call them) within
754 one object file, but using a stack rather than in an arbitrary
760 struct subfile_stack *tem
761 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
763 tem->next = subfile_stack;
765 if (current_subfile == NULL || current_subfile->name == NULL)
767 internal_error (__FILE__, __LINE__,
768 _("failed internal consistency check"));
770 tem->name = current_subfile->name;
777 struct subfile_stack *link = subfile_stack;
781 internal_error (__FILE__, __LINE__,
782 _("failed internal consistency check"));
785 subfile_stack = link->next;
786 xfree ((void *) link);
790 /* Add a linetable entry for line number LINE and address PC to the
791 line vector for SUBFILE. */
794 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
796 struct linetable_entry *e;
798 /* Ignore the dummy line number in libg.o */
804 /* Make sure line vector exists and is big enough. */
805 if (!subfile->line_vector)
807 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
808 subfile->line_vector = (struct linetable *)
809 xmalloc (sizeof (struct linetable)
810 + subfile->line_vector_length * sizeof (struct linetable_entry));
811 subfile->line_vector->nitems = 0;
812 have_line_numbers = 1;
815 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
817 subfile->line_vector_length *= 2;
818 subfile->line_vector = (struct linetable *)
819 xrealloc ((char *) subfile->line_vector,
820 (sizeof (struct linetable)
821 + (subfile->line_vector_length
822 * sizeof (struct linetable_entry))));
825 /* Normally, we treat lines as unsorted. But the end of sequence
826 marker is special. We sort line markers at the same PC by line
827 number, so end of sequence markers (which have line == 0) appear
828 first. This is right if the marker ends the previous function,
829 and there is no padding before the next function. But it is
830 wrong if the previous line was empty and we are now marking a
831 switch to a different subfile. We must leave the end of sequence
832 marker at the end of this group of lines, not sort the empty line
833 to after the marker. The easiest way to accomplish this is to
834 delete any empty lines from our table, if they are followed by
835 end of sequence markers. All we lose is the ability to set
836 breakpoints at some lines which contain no instructions
838 if (line == 0 && subfile->line_vector->nitems > 0)
840 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
841 while (subfile->line_vector->nitems > 0 && e->pc == pc)
844 subfile->line_vector->nitems--;
848 e = subfile->line_vector->item + subfile->line_vector->nitems++;
853 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
856 compare_line_numbers (const void *ln1p, const void *ln2p)
858 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
859 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
861 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
862 Please keep it that way. */
863 if (ln1->pc < ln2->pc)
866 if (ln1->pc > ln2->pc)
869 /* If pc equal, sort by line. I'm not sure whether this is optimum
870 behavior (see comment at struct linetable in symtab.h). */
871 return ln1->line - ln2->line;
874 /* Return the macro table.
875 Initialize it if this is the first use.
876 It is only valid to call this between calls to start_symtab and the
877 end_symtab* functions. */
880 get_macro_table (const char *comp_dir)
882 struct objfile *objfile = buildsym_objfile;
884 if (! pending_macros)
885 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
886 objfile->per_bfd->macro_cache,
888 return pending_macros;
891 /* Start a new symtab for a new source file in OBJFILE. Called, for example,
892 when a stabs symbol of type N_SO is seen, or when a DWARF
893 TAG_compile_unit DIE is seen. It indicates the start of data for
894 one original source file.
896 NAME is the name of the file (cannot be NULL). COMP_DIR is the directory in
897 which the file was compiled (or NULL if not known). START_ADDR is the
898 lowest address of objects in the file (or 0 if not known). */
901 start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
902 CORE_ADDR start_addr)
904 buildsym_objfile = objfile;
905 buildsym_comp_dir = comp_dir != NULL ? xstrdup (comp_dir) : NULL;
906 restart_symtab (start_addr);
907 set_last_source_file (name);
908 start_subfile (name);
909 /* Save this so that we don't have to go looking for it at the end
910 of the subfiles list. */
911 main_subfile = current_subfile;
914 /* Restart compilation for a symtab.
915 This is used when a symtab is built from multiple sources.
916 The symtab is first built with start_symtab and then for each additional
917 piece call restart_symtab. */
920 restart_symtab (CORE_ADDR start_addr)
922 set_last_source_file (NULL);
923 last_source_start_addr = start_addr;
925 global_symbols = NULL;
927 have_line_numbers = 0;
929 /* Context stack is initially empty. Allocate first one with room
930 for 10 levels; reuse it forever afterward. */
931 if (context_stack == NULL)
933 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
934 context_stack = (struct context_stack *)
935 xmalloc (context_stack_size * sizeof (struct context_stack));
937 context_stack_depth = 0;
939 /* We shouldn't have any address map at this point. */
940 gdb_assert (! pending_addrmap);
942 /* Reset the sub source files list. The list should already be empty,
943 but free it anyway in case some code didn't finish cleaning up after
945 free_subfiles_list ();
948 /* Subroutine of end_symtab to simplify it. Look for a subfile that
949 matches the main source file's basename. If there is only one, and
950 if the main source file doesn't have any symbol or line number
951 information, then copy this file's symtab and line_vector to the
952 main source file's subfile and discard the other subfile. This can
953 happen because of a compiler bug or from the user playing games
954 with #line or from things like a distributed build system that
955 manipulates the debug info. */
958 watch_main_source_file_lossage (void)
960 struct subfile *subfile;
962 /* We have to watch for mainsub == NULL here. It's a quirk of
963 end_symtab, it can return NULL so there may not be a main subfile. */
964 if (main_subfile == NULL)
967 /* If the main source file doesn't have any line number or symbol
968 info, look for an alias in another subfile. */
970 if (main_subfile->line_vector == NULL
971 && main_subfile->symtab == NULL)
973 const char *mainbase = lbasename (main_subfile->name);
975 struct subfile *prevsub;
976 struct subfile *mainsub_alias = NULL;
977 struct subfile *prev_mainsub_alias = NULL;
980 for (subfile = subfiles;
981 /* Stop before we get to the last one. */
983 subfile = subfile->next)
985 if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
988 mainsub_alias = subfile;
989 prev_mainsub_alias = prevsub;
996 gdb_assert (mainsub_alias != NULL && mainsub_alias != main_subfile);
998 /* Found a match for the main source file.
999 Copy its line_vector and symtab to the main subfile
1000 and then discard it. */
1002 main_subfile->line_vector = mainsub_alias->line_vector;
1003 main_subfile->line_vector_length = mainsub_alias->line_vector_length;
1004 main_subfile->symtab = mainsub_alias->symtab;
1006 if (prev_mainsub_alias == NULL)
1007 subfiles = mainsub_alias->next;
1009 prev_mainsub_alias->next = mainsub_alias->next;
1010 xfree (mainsub_alias->name);
1011 xfree (mainsub_alias->dirname);
1012 xfree (mainsub_alias);
1017 /* Helper function for qsort. Parameters are `struct block *' pointers,
1018 function sorts them in descending order by their BLOCK_START. */
1021 block_compar (const void *ap, const void *bp)
1023 const struct block *a = *(const struct block **) ap;
1024 const struct block *b = *(const struct block **) bp;
1026 return ((BLOCK_START (b) > BLOCK_START (a))
1027 - (BLOCK_START (b) < BLOCK_START (a)));
1030 /* Reset globals used to build symtabs. */
1033 reset_symtab_globals (void)
1035 buildsym_objfile = NULL;
1036 xfree (buildsym_comp_dir);
1037 buildsym_comp_dir = NULL;
1038 set_last_source_file (NULL);
1039 free_subfiles_list ();
1040 pending_macros = NULL;
1041 if (pending_addrmap)
1043 obstack_free (&pending_addrmap_obstack, NULL);
1044 pending_addrmap = NULL;
1048 /* Implementation of the first part of end_symtab. It allows modifying
1049 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
1050 If the returned value is NULL there is no blockvector created for
1051 this symtab (you still must call end_symtab_from_static_block).
1053 END_ADDR is the same as for end_symtab: the address of the end of the
1056 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
1059 If REQUIRED is non-zero, then a symtab is created even if it does
1060 not contain any symbols. */
1063 end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
1065 struct objfile *objfile = buildsym_objfile;
1067 /* Finish the lexical context of the last function in the file; pop
1068 the context stack. */
1070 if (context_stack_depth > 0)
1072 struct context_stack *cstk = pop_context ();
1074 /* Make a block for the local symbols within. */
1075 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1076 cstk->start_addr, end_addr);
1078 if (context_stack_depth > 0)
1080 /* This is said to happen with SCO. The old coffread.c
1081 code simply emptied the context stack, so we do the
1082 same. FIXME: Find out why it is happening. This is not
1083 believed to happen in most cases (even for coffread.c);
1084 it used to be an abort(). */
1085 complaint (&symfile_complaints,
1086 _("Context stack not empty in end_symtab"));
1087 context_stack_depth = 0;
1091 /* Reordered executables may have out of order pending blocks; if
1092 OBJF_REORDERED is true, then sort the pending blocks. */
1094 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
1097 struct pending_block *pb;
1098 struct block **barray, **bp;
1099 struct cleanup *back_to;
1101 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1104 barray = xmalloc (sizeof (*barray) * count);
1105 back_to = make_cleanup (xfree, barray);
1108 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1111 qsort (barray, count, sizeof (*barray), block_compar);
1114 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1117 do_cleanups (back_to);
1120 /* Cleanup any undefined types that have been left hanging around
1121 (this needs to be done before the finish_blocks so that
1122 file_symbols is still good).
1124 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
1125 specific, but harmless for other symbol readers, since on gdb
1126 startup or when finished reading stabs, the state is set so these
1127 are no-ops. FIXME: Is this handled right in case of QUIT? Can
1128 we make this cleaner? */
1130 cleanup_undefined_stabs_types (objfile);
1131 finish_global_stabs (objfile);
1134 && pending_blocks == NULL
1135 && file_symbols == NULL
1136 && global_symbols == NULL
1137 && have_line_numbers == 0
1138 && pending_macros == NULL)
1140 /* Ignore symtabs that have no functions with real debugging info. */
1145 /* Define the STATIC_BLOCK. */
1146 return finish_block_internal (NULL, &file_symbols, NULL,
1147 last_source_start_addr, end_addr,
1152 /* Subroutine of end_symtab_from_static_block to simplify it.
1153 Handle the "no blockvector" case.
1154 When this happens there is nothing to record, so just free up
1155 any memory we allocated while reading debug info. */
1158 end_symtab_without_blockvector (void)
1160 struct objfile *objfile = buildsym_objfile;
1161 struct subfile *subfile;
1163 /* Since we are ignoring these subfiles, we also need
1164 to unlink the associated empty symtab that we created.
1165 Otherwise, we can run into trouble because various parts
1166 such as the block-vector are uninitialized whereas
1167 the rest of the code assumes that they are.
1169 We can only unlink the symtab. We can't free it because
1170 it was allocated on the objfile obstack. */
1172 for (subfile = subfiles; subfile != NULL; subfile = subfile->next)
1174 if (subfile->symtab)
1178 if (objfile->symtabs == subfile->symtab)
1179 objfile->symtabs = objfile->symtabs->next;
1181 ALL_OBJFILE_SYMTABS (objfile, s)
1182 if (s->next == subfile->symtab)
1184 s->next = s->next->next;
1187 subfile->symtab = NULL;
1192 /* Subroutine of end_symtab_from_static_block to simplify it.
1193 Handle the "have blockvector" case.
1194 See end_symtab_from_static_block for a description of the arguments. */
1196 static struct symtab *
1197 end_symtab_with_blockvector (struct block *static_block,
1198 int section, int expandable)
1200 struct objfile *objfile = buildsym_objfile;
1201 struct symtab *symtab;
1202 struct blockvector *blockvector;
1203 struct subfile *subfile;
1206 gdb_assert (static_block != NULL);
1207 gdb_assert (subfiles != NULL);
1209 end_addr = BLOCK_END (static_block);
1211 /* Create the GLOBAL_BLOCK and build the blockvector. */
1212 finish_block_internal (NULL, &global_symbols, NULL,
1213 last_source_start_addr, end_addr,
1215 blockvector = make_blockvector (objfile);
1217 /* Read the line table if it has to be read separately.
1218 This is only used by xcoffread.c. */
1219 if (objfile->sf->sym_read_linetable != NULL)
1220 objfile->sf->sym_read_linetable (objfile);
1222 /* Handle the case where the debug info specifies a different path
1223 for the main source file. It can cause us to lose track of its
1224 line number information. */
1225 watch_main_source_file_lossage ();
1227 /* Now create the symtab objects proper, one for each subfile. */
1229 for (subfile = subfiles; subfile != NULL; subfile = subfile->next)
1231 int linetablesize = 0;
1233 if (subfile->line_vector)
1235 linetablesize = sizeof (struct linetable) +
1236 subfile->line_vector->nitems * sizeof (struct linetable_entry);
1238 /* Like the pending blocks, the line table may be
1239 scrambled in reordered executables. Sort it if
1240 OBJF_REORDERED is true. */
1241 if (objfile->flags & OBJF_REORDERED)
1242 qsort (subfile->line_vector->item,
1243 subfile->line_vector->nitems,
1244 sizeof (struct linetable_entry), compare_line_numbers);
1247 /* Allocate a symbol table if necessary. */
1248 if (subfile->symtab == NULL)
1249 subfile->symtab = allocate_symtab (subfile->name, objfile);
1250 symtab = subfile->symtab;
1252 /* Fill in its components. */
1253 symtab->blockvector = blockvector;
1254 symtab->macro_table = pending_macros;
1255 if (subfile->line_vector)
1257 /* Reallocate the line table on the symbol obstack. */
1258 SYMTAB_LINETABLE (symtab) = (struct linetable *)
1259 obstack_alloc (&objfile->objfile_obstack, linetablesize);
1260 memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
1265 SYMTAB_LINETABLE (symtab) = NULL;
1267 symtab->block_line_section = section;
1268 if (subfile->dirname)
1270 /* Reallocate the dirname on the symbol obstack. */
1271 SYMTAB_DIRNAME (symtab) =
1272 obstack_copy0 (&objfile->objfile_obstack,
1274 strlen (subfile->dirname));
1278 SYMTAB_DIRNAME (symtab) = NULL;
1281 /* Use whatever language we have been using for this
1282 subfile, not the one that was deduced in allocate_symtab
1283 from the filename. We already did our own deducing when
1284 we created the subfile, and we may have altered our
1285 opinion of what language it is from things we found in
1287 symtab->language = subfile->language;
1289 /* Save the debug format string (if any) in the symtab. */
1290 symtab->debugformat = subfile->debugformat;
1292 /* Similarly for the producer. */
1293 symtab->producer = subfile->producer;
1295 /* All symtabs for the main file and the subfiles share a
1296 blockvector, so we need to clear primary for everything
1297 but the main file. */
1298 set_symtab_primary (symtab, 0);
1301 /* The main source file is the primary symtab. */
1302 gdb_assert (main_subfile->symtab != NULL);
1303 symtab = main_subfile->symtab;
1304 set_symtab_primary (symtab, 1);
1306 struct block *b = BLOCKVECTOR_BLOCK (symtab->blockvector, GLOBAL_BLOCK);
1308 set_block_symtab (b, symtab);
1311 /* Default any symbols without a specified symtab to the primary symtab. */
1315 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1317 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1319 struct dict_iterator iter;
1321 /* Inlined functions may have symbols not in the global or
1322 static symbol lists. */
1323 if (BLOCK_FUNCTION (block) != NULL)
1324 if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
1325 SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
1327 /* Note that we only want to fix up symbols from the local
1328 blocks, not blocks coming from included symtabs. That is why
1329 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1330 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
1331 if (SYMBOL_SYMTAB (sym) == NULL)
1332 SYMBOL_SYMTAB (sym) = symtab;
1339 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1340 as value returned by end_symtab_get_static_block.
1342 SECTION is the same as for end_symtab: the section number
1343 (in objfile->section_offsets) of the blockvector and linetable.
1345 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1349 end_symtab_from_static_block (struct block *static_block,
1350 int section, int expandable)
1354 if (static_block == NULL)
1356 end_symtab_without_blockvector ();
1360 s = end_symtab_with_blockvector (static_block, section, expandable);
1362 reset_symtab_globals ();
1367 /* Finish the symbol definitions for one main source file, close off
1368 all the lexical contexts for that file (creating struct block's for
1369 them), then make the struct symtab for that file and put it in the
1372 END_ADDR is the address of the end of the file's text. SECTION is
1373 the section number (in objfile->section_offsets) of the blockvector
1376 Note that it is possible for end_symtab() to return NULL. In
1377 particular, for the DWARF case at least, it will return NULL when
1378 it finds a compilation unit that has exactly one DIE, a
1379 TAG_compile_unit DIE. This can happen when we link in an object
1380 file that was compiled from an empty source file. Returning NULL
1381 is probably not the correct thing to do, because then gdb will
1382 never know about this empty file (FIXME).
1384 If you need to modify STATIC_BLOCK before it is finalized you should
1385 call end_symtab_get_static_block and end_symtab_from_static_block
1389 end_symtab (CORE_ADDR end_addr, int section)
1391 struct block *static_block;
1393 static_block = end_symtab_get_static_block (end_addr, 0, 0);
1394 return end_symtab_from_static_block (static_block, section, 0);
1397 /* Same as end_symtab except create a symtab that can be later added to. */
1400 end_expandable_symtab (CORE_ADDR end_addr, int section)
1402 struct block *static_block;
1404 static_block = end_symtab_get_static_block (end_addr, 1, 0);
1405 return end_symtab_from_static_block (static_block, section, 1);
1408 /* Subroutine of augment_type_symtab to simplify it.
1409 Attach SYMTAB to all symbols in PENDING_LIST that don't have one. */
1412 set_missing_symtab (struct pending *pending_list, struct symtab *symtab)
1414 struct pending *pending;
1417 for (pending = pending_list; pending != NULL; pending = pending->next)
1419 for (i = 0; i < pending->nsyms; ++i)
1421 if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
1422 SYMBOL_SYMTAB (pending->symbol[i]) = symtab;
1427 /* Same as end_symtab, but for the case where we're adding more symbols
1428 to an existing symtab that is known to contain only type information.
1429 This is the case for DWARF4 Type Units. */
1432 augment_type_symtab (struct symtab *primary_symtab)
1434 const struct blockvector *blockvector = primary_symtab->blockvector;
1436 if (context_stack_depth > 0)
1438 complaint (&symfile_complaints,
1439 _("Context stack not empty in augment_type_symtab"));
1440 context_stack_depth = 0;
1442 if (pending_blocks != NULL)
1443 complaint (&symfile_complaints, _("Blocks in a type symtab"));
1444 if (pending_macros != NULL)
1445 complaint (&symfile_complaints, _("Macro in a type symtab"));
1446 if (have_line_numbers)
1447 complaint (&symfile_complaints,
1448 _("Line numbers recorded in a type symtab"));
1450 if (file_symbols != NULL)
1452 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1454 /* First mark any symbols without a specified symtab as belonging
1455 to the primary symtab. */
1456 set_missing_symtab (file_symbols, primary_symtab);
1458 dict_add_pending (BLOCK_DICT (block), file_symbols);
1461 if (global_symbols != NULL)
1463 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1465 /* First mark any symbols without a specified symtab as belonging
1466 to the primary symtab. */
1467 set_missing_symtab (global_symbols, primary_symtab);
1469 dict_add_pending (BLOCK_DICT (block), global_symbols);
1472 reset_symtab_globals ();
1475 /* Push a context block. Args are an identifying nesting level
1476 (checkable when you pop it), and the starting PC address of this
1479 struct context_stack *
1480 push_context (int desc, CORE_ADDR valu)
1482 struct context_stack *new;
1484 if (context_stack_depth == context_stack_size)
1486 context_stack_size *= 2;
1487 context_stack = (struct context_stack *)
1488 xrealloc ((char *) context_stack,
1489 (context_stack_size * sizeof (struct context_stack)));
1492 new = &context_stack[context_stack_depth++];
1494 new->locals = local_symbols;
1495 new->old_blocks = pending_blocks;
1496 new->start_addr = valu;
1497 new->using_directives = using_directives;
1500 local_symbols = NULL;
1501 using_directives = NULL;
1506 /* Pop a context block. Returns the address of the context block just
1509 struct context_stack *
1512 gdb_assert (context_stack_depth > 0);
1513 return (&context_stack[--context_stack_depth]);
1518 /* Compute a small integer hash code for the given name. */
1521 hashname (const char *name)
1523 return (hash(name,strlen(name)) % HASHSIZE);
1528 record_debugformat (const char *format)
1530 current_subfile->debugformat = format;
1534 record_producer (const char *producer)
1536 current_subfile->producer = producer;
1539 /* Merge the first symbol list SRCLIST into the second symbol list
1540 TARGETLIST by repeated calls to add_symbol_to_list(). This
1541 procedure "frees" each link of SRCLIST by adding it to the
1542 free_pendings list. Caller must set SRCLIST to a null list after
1543 calling this function.
1548 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1552 if (!srclist || !*srclist)
1555 /* Merge in elements from current link. */
1556 for (i = 0; i < (*srclist)->nsyms; i++)
1557 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1559 /* Recurse on next. */
1560 merge_symbol_lists (&(*srclist)->next, targetlist);
1562 /* "Free" the current link. */
1563 (*srclist)->next = free_pendings;
1564 free_pendings = (*srclist);
1568 /* Name of source file whose symbol data we are now processing. This
1569 comes from a symbol of type N_SO for stabs. For Dwarf it comes
1570 from the DW_AT_name attribute of a DW_TAG_compile_unit DIE. */
1572 static char *last_source_file;
1574 /* See buildsym.h. */
1577 set_last_source_file (const char *name)
1579 xfree (last_source_file);
1580 last_source_file = name == NULL ? NULL : xstrdup (name);
1583 /* See buildsym.h. */
1586 get_last_source_file (void)
1588 return last_source_file;
1593 /* Initialize anything that needs initializing when starting to read a
1594 fresh piece of a symbol file, e.g. reading in the stuff
1595 corresponding to a psymtab. */
1598 buildsym_init (void)
1600 free_pendings = NULL;
1601 file_symbols = NULL;
1602 global_symbols = NULL;
1603 pending_blocks = NULL;
1604 pending_macros = NULL;
1605 using_directives = NULL;
1606 subfile_stack = NULL;
1608 /* We shouldn't have any address map at this point. */
1609 gdb_assert (! pending_addrmap);
1610 pending_addrmap_interesting = 0;
1613 /* Initialize anything that needs initializing when a completely new
1614 symbol file is specified (not just adding some symbols from another
1615 file, e.g. a shared library). */
1618 buildsym_new_init (void)