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 /* Buildsym's counterpart to struct compunit_symtab.
56 TODO(dje): Move all related global state into here. */
58 struct buildsym_compunit
60 /* The objfile we're reading debug info from. */
61 struct objfile *objfile;
63 /* List of subfiles (source files).
64 Files are added to the front of the list.
65 This is important mostly for the language determination hacks we use,
66 which iterate over previously added files. */
67 struct subfile *subfiles;
69 /* The subfile of the main source file. */
70 struct subfile *main_subfile;
72 /* E.g., DW_AT_comp_dir if DWARF. Space for this is malloc'd. */
75 /* Space for this is not malloc'd, and is assumed to have at least
76 the same lifetime as objfile. */
79 /* Space for this is not malloc'd, and is assumed to have at least
80 the same lifetime as objfile. */
81 const char *debugformat;
83 /* The compunit we are building. */
84 struct compunit_symtab *compunit_symtab;
87 /* The work-in-progress of the compunit we are building.
88 This is created first, before any subfiles by start_symtab. */
90 static struct buildsym_compunit *buildsym_compunit;
92 /* List of free `struct pending' structures for reuse. */
94 static struct pending *free_pendings;
96 /* Non-zero if symtab has line number info. This prevents an
97 otherwise empty symtab from being tossed. */
99 static int have_line_numbers;
101 /* The mutable address map for the compilation unit whose symbols
102 we're currently reading. The symtabs' shared blockvector will
103 point to a fixed copy of this. */
104 static struct addrmap *pending_addrmap;
106 /* The obstack on which we allocate pending_addrmap.
107 If pending_addrmap is NULL, this is uninitialized; otherwise, it is
108 initialized (and holds pending_addrmap). */
109 static struct obstack pending_addrmap_obstack;
111 /* Non-zero if we recorded any ranges in the addrmap that are
112 different from those in the blockvector already. We set this to
113 zero when we start processing a symfile, and if it's still zero at
114 the end, then we just toss the addrmap. */
115 static int pending_addrmap_interesting;
117 /* An obstack used for allocating pending blocks. */
119 static struct obstack pending_block_obstack;
121 /* List of blocks already made (lexical contexts already closed).
122 This is used at the end to make the blockvector. */
126 struct pending_block *next;
130 /* Pointer to the head of a linked list of symbol blocks which have
131 already been finalized (lexical contexts already closed) and which
132 are just waiting to be built into a blockvector when finalizing the
133 associated symtab. */
135 static struct pending_block *pending_blocks;
139 struct subfile_stack *next;
143 static struct subfile_stack *subfile_stack;
145 /* The macro table for the compilation unit whose symbols we're
146 currently reading. */
147 static struct macro_table *pending_macros;
149 static int compare_line_numbers (const void *ln1p, const void *ln2p);
151 static void record_pending_block (struct objfile *objfile,
153 struct pending_block *opblock);
155 /* Initial sizes of data structures. These are realloc'd larger if
156 needed, and realloc'd down to the size actually used, when
159 #define INITIAL_CONTEXT_STACK_SIZE 10
160 #define INITIAL_LINE_VECTOR_LENGTH 1000
163 /* Maintain the lists of symbols and blocks. */
165 /* Add a symbol to one of the lists of symbols. */
168 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
170 struct pending *link;
172 /* If this is an alias for another symbol, don't add it. */
173 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
176 /* We keep PENDINGSIZE symbols in each link of the list. If we
177 don't have a link with room in it, add a new link. */
178 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
182 link = free_pendings;
183 free_pendings = link->next;
187 link = (struct pending *) xmalloc (sizeof (struct pending));
190 link->next = *listhead;
195 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
198 /* Find a symbol named NAME on a LIST. NAME need not be
199 '\0'-terminated; LENGTH is the length of the name. */
202 find_symbol_in_list (struct pending *list, char *name, int length)
209 for (j = list->nsyms; --j >= 0;)
211 pp = SYMBOL_LINKAGE_NAME (list->symbol[j]);
212 if (*pp == *name && strncmp (pp, name, length) == 0
213 && pp[length] == '\0')
215 return (list->symbol[j]);
223 /* At end of reading syms, or in case of quit, really free as many
224 `struct pending's as we can easily find. */
227 really_free_pendings (void *dummy)
229 struct pending *next, *next1;
231 for (next = free_pendings; next; next = next1)
234 xfree ((void *) next);
236 free_pendings = NULL;
238 free_pending_blocks ();
240 for (next = file_symbols; next != NULL; next = next1)
243 xfree ((void *) next);
247 for (next = global_symbols; next != NULL; next = next1)
250 xfree ((void *) next);
252 global_symbols = NULL;
255 free_macro_table (pending_macros);
259 obstack_free (&pending_addrmap_obstack, NULL);
260 pending_addrmap = NULL;
264 /* This function is called to discard any pending blocks. */
267 free_pending_blocks (void)
269 if (pending_blocks != NULL)
271 obstack_free (&pending_block_obstack, NULL);
272 pending_blocks = NULL;
276 /* Take one of the lists of symbols and make a block from it. Keep
277 the order the symbols have in the list (reversed from the input
278 file). Put the block on the list of pending blocks. */
280 static struct block *
281 finish_block_internal (struct symbol *symbol, struct pending **listhead,
282 struct pending_block *old_blocks,
283 CORE_ADDR start, CORE_ADDR end,
284 int is_global, int expandable)
286 struct objfile *objfile = buildsym_compunit->objfile;
287 struct gdbarch *gdbarch = get_objfile_arch (objfile);
288 struct pending *next, *next1;
290 struct pending_block *pblock;
291 struct pending_block *opblock;
294 ? allocate_global_block (&objfile->objfile_obstack)
295 : allocate_block (&objfile->objfile_obstack));
299 BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
306 BLOCK_DICT (block) = dict_create_hashed_expandable ();
307 dict_add_pending (BLOCK_DICT (block), *listhead);
312 dict_create_hashed (&objfile->objfile_obstack, *listhead);
316 BLOCK_START (block) = start;
317 BLOCK_END (block) = end;
319 /* Put the block in as the value of the symbol that names it. */
323 struct type *ftype = SYMBOL_TYPE (symbol);
324 struct dict_iterator iter;
325 SYMBOL_BLOCK_VALUE (symbol) = block;
326 BLOCK_FUNCTION (block) = symbol;
328 if (TYPE_NFIELDS (ftype) <= 0)
330 /* No parameter type information is recorded with the
331 function's type. Set that from the type of the
332 parameter symbols. */
333 int nparams = 0, iparams;
336 /* Here we want to directly access the dictionary, because
337 we haven't fully initialized the block yet. */
338 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
340 if (SYMBOL_IS_ARGUMENT (sym))
345 TYPE_NFIELDS (ftype) = nparams;
346 TYPE_FIELDS (ftype) = (struct field *)
347 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
350 /* Here we want to directly access the dictionary, because
351 we haven't fully initialized the block yet. */
352 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
354 if (iparams == nparams)
357 if (SYMBOL_IS_ARGUMENT (sym))
359 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
360 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
369 BLOCK_FUNCTION (block) = NULL;
372 /* Now "free" the links of the list, and empty the list. */
374 for (next = *listhead; next; next = next1)
377 next->next = free_pendings;
378 free_pendings = next;
382 /* Check to be sure that the blocks have an end address that is
383 greater than starting address. */
385 if (BLOCK_END (block) < BLOCK_START (block))
389 complaint (&symfile_complaints,
390 _("block end address less than block "
391 "start address in %s (patched it)"),
392 SYMBOL_PRINT_NAME (symbol));
396 complaint (&symfile_complaints,
397 _("block end address %s less than block "
398 "start address %s (patched it)"),
399 paddress (gdbarch, BLOCK_END (block)),
400 paddress (gdbarch, BLOCK_START (block)));
402 /* Better than nothing. */
403 BLOCK_END (block) = BLOCK_START (block);
406 /* Install this block as the superblock of all blocks made since the
407 start of this scope that don't have superblocks yet. */
410 for (pblock = pending_blocks;
411 pblock && pblock != old_blocks;
412 pblock = pblock->next)
414 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
416 /* Check to be sure the blocks are nested as we receive
417 them. If the compiler/assembler/linker work, this just
418 burns a small amount of time.
420 Skip blocks which correspond to a function; they're not
421 physically nested inside this other blocks, only
423 if (BLOCK_FUNCTION (pblock->block) == NULL
424 && (BLOCK_START (pblock->block) < BLOCK_START (block)
425 || BLOCK_END (pblock->block) > BLOCK_END (block)))
429 complaint (&symfile_complaints,
430 _("inner block not inside outer block in %s"),
431 SYMBOL_PRINT_NAME (symbol));
435 complaint (&symfile_complaints,
436 _("inner block (%s-%s) not "
437 "inside outer block (%s-%s)"),
438 paddress (gdbarch, BLOCK_START (pblock->block)),
439 paddress (gdbarch, BLOCK_END (pblock->block)),
440 paddress (gdbarch, BLOCK_START (block)),
441 paddress (gdbarch, BLOCK_END (block)));
443 if (BLOCK_START (pblock->block) < BLOCK_START (block))
444 BLOCK_START (pblock->block) = BLOCK_START (block);
445 if (BLOCK_END (pblock->block) > BLOCK_END (block))
446 BLOCK_END (pblock->block) = BLOCK_END (block);
448 BLOCK_SUPERBLOCK (pblock->block) = block;
453 block_set_using (block, using_directives, &objfile->objfile_obstack);
454 using_directives = NULL;
456 record_pending_block (objfile, block, opblock);
462 finish_block (struct symbol *symbol, struct pending **listhead,
463 struct pending_block *old_blocks,
464 CORE_ADDR start, CORE_ADDR end)
466 return finish_block_internal (symbol, listhead, old_blocks,
470 /* Record BLOCK on the list of all blocks in the file. Put it after
471 OPBLOCK, or at the beginning if opblock is NULL. This puts the
472 block in the list after all its subblocks.
474 Allocate the pending block struct in the objfile_obstack to save
475 time. This wastes a little space. FIXME: Is it worth it? */
478 record_pending_block (struct objfile *objfile, struct block *block,
479 struct pending_block *opblock)
481 struct pending_block *pblock;
483 if (pending_blocks == NULL)
484 obstack_init (&pending_block_obstack);
486 pblock = (struct pending_block *)
487 obstack_alloc (&pending_block_obstack, sizeof (struct pending_block));
488 pblock->block = block;
491 pblock->next = opblock->next;
492 opblock->next = pblock;
496 pblock->next = pending_blocks;
497 pending_blocks = pblock;
502 /* Record that the range of addresses from START to END_INCLUSIVE
503 (inclusive, like it says) belongs to BLOCK. BLOCK's start and end
504 addresses must be set already. You must apply this function to all
505 BLOCK's children before applying it to BLOCK.
507 If a call to this function complicates the picture beyond that
508 already provided by BLOCK_START and BLOCK_END, then we create an
509 address map for the block. */
511 record_block_range (struct block *block,
512 CORE_ADDR start, CORE_ADDR end_inclusive)
514 /* If this is any different from the range recorded in the block's
515 own BLOCK_START and BLOCK_END, then note that the address map has
516 become interesting. Note that even if this block doesn't have
517 any "interesting" ranges, some later block might, so we still
518 need to record this block in the addrmap. */
519 if (start != BLOCK_START (block)
520 || end_inclusive + 1 != BLOCK_END (block))
521 pending_addrmap_interesting = 1;
523 if (! pending_addrmap)
525 obstack_init (&pending_addrmap_obstack);
526 pending_addrmap = addrmap_create_mutable (&pending_addrmap_obstack);
529 addrmap_set_empty (pending_addrmap, start, end_inclusive, block);
532 static struct blockvector *
533 make_blockvector (void)
535 struct objfile *objfile = buildsym_compunit->objfile;
536 struct pending_block *next;
537 struct blockvector *blockvector;
540 /* Count the length of the list of blocks. */
542 for (next = pending_blocks, i = 0; next; next = next->next, i++)
546 blockvector = (struct blockvector *)
547 obstack_alloc (&objfile->objfile_obstack,
548 (sizeof (struct blockvector)
549 + (i - 1) * sizeof (struct block *)));
551 /* Copy the blocks into the blockvector. This is done in reverse
552 order, which happens to put the blocks into the proper order
553 (ascending starting address). finish_block has hair to insert
554 each block into the list after its subblocks in order to make
555 sure this is true. */
557 BLOCKVECTOR_NBLOCKS (blockvector) = i;
558 for (next = pending_blocks; next; next = next->next)
560 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
563 free_pending_blocks ();
565 /* If we needed an address map for this symtab, record it in the
567 if (pending_addrmap && pending_addrmap_interesting)
568 BLOCKVECTOR_MAP (blockvector)
569 = addrmap_create_fixed (pending_addrmap, &objfile->objfile_obstack);
571 BLOCKVECTOR_MAP (blockvector) = 0;
573 /* Some compilers output blocks in the wrong order, but we depend on
574 their being in the right order so we can binary search. Check the
575 order and moan about it.
576 Note: Remember that the first two blocks are the global and static
577 blocks. We could special case that fact and begin checking at block 2.
578 To avoid making that assumption we do not. */
579 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
581 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
583 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
584 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
587 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
589 complaint (&symfile_complaints, _("block at %s out of order"),
590 hex_string ((LONGEST) start));
595 return (blockvector);
598 /* Start recording information about source code that came from an
599 included (or otherwise merged-in) source file with a different
600 name. NAME is the name of the file (cannot be NULL). */
603 start_subfile (const char *name)
605 const char *subfile_dirname;
606 struct subfile *subfile;
608 gdb_assert (buildsym_compunit != NULL);
610 subfile_dirname = buildsym_compunit->comp_dir;
612 /* See if this subfile is already registered. */
614 for (subfile = buildsym_compunit->subfiles; subfile; subfile = subfile->next)
618 /* If NAME is an absolute path, and this subfile is not, then
619 attempt to create an absolute path to compare. */
620 if (IS_ABSOLUTE_PATH (name)
621 && !IS_ABSOLUTE_PATH (subfile->name)
622 && subfile_dirname != NULL)
623 subfile_name = concat (subfile_dirname, SLASH_STRING,
624 subfile->name, (char *) NULL);
626 subfile_name = subfile->name;
628 if (FILENAME_CMP (subfile_name, name) == 0)
630 current_subfile = subfile;
631 if (subfile_name != subfile->name)
632 xfree (subfile_name);
635 if (subfile_name != subfile->name)
636 xfree (subfile_name);
639 /* This subfile is not known. Add an entry for it. */
641 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
642 memset (subfile, 0, sizeof (struct subfile));
643 subfile->buildsym_compunit = buildsym_compunit;
645 subfile->next = buildsym_compunit->subfiles;
646 buildsym_compunit->subfiles = subfile;
648 current_subfile = subfile;
650 subfile->name = xstrdup (name);
652 /* Initialize line-number recording for this subfile. */
653 subfile->line_vector = NULL;
655 /* Default the source language to whatever can be deduced from the
656 filename. If nothing can be deduced (such as for a C/C++ include
657 file with a ".h" extension), then inherit whatever language the
658 previous subfile had. This kludgery is necessary because there
659 is no standard way in some object formats to record the source
660 language. Also, when symtabs are allocated we try to deduce a
661 language then as well, but it is too late for us to use that
662 information while reading symbols, since symtabs aren't allocated
663 until after all the symbols have been processed for a given
666 subfile->language = deduce_language_from_filename (subfile->name);
667 if (subfile->language == language_unknown
668 && subfile->next != NULL)
670 subfile->language = subfile->next->language;
673 /* If the filename of this subfile ends in .C, then change the
674 language of any pending subfiles from C to C++. We also accept
675 any other C++ suffixes accepted by deduce_language_from_filename. */
676 /* Likewise for f2c. */
681 enum language sublang = deduce_language_from_filename (subfile->name);
683 if (sublang == language_cplus || sublang == language_fortran)
684 for (s = buildsym_compunit->subfiles; s != NULL; s = s->next)
685 if (s->language == language_c)
686 s->language = sublang;
689 /* And patch up this file if necessary. */
690 if (subfile->language == language_c
691 && subfile->next != NULL
692 && (subfile->next->language == language_cplus
693 || subfile->next->language == language_fortran))
695 subfile->language = subfile->next->language;
699 /* Start recording information about a primary source file (IOW, not an
700 included source file).
701 COMP_DIR is the directory in which the compilation unit was compiled
702 (or NULL if not known). */
704 static struct buildsym_compunit *
705 start_buildsym_compunit (struct objfile *objfile, const char *comp_dir)
707 struct buildsym_compunit *bscu;
709 bscu = (struct buildsym_compunit *)
710 xmalloc (sizeof (struct buildsym_compunit));
711 memset (bscu, 0, sizeof (struct buildsym_compunit));
713 bscu->objfile = objfile;
714 bscu->comp_dir = (comp_dir == NULL) ? NULL : xstrdup (comp_dir);
716 /* Initialize the debug format string to NULL. We may supply it
717 later via a call to record_debugformat. */
718 bscu->debugformat = NULL;
720 /* Similarly for the producer. */
721 bscu->producer = NULL;
726 /* Delete the buildsym compunit. */
729 free_buildsym_compunit (void)
731 struct subfile *subfile, *nextsub;
733 if (buildsym_compunit == NULL)
735 for (subfile = buildsym_compunit->subfiles;
739 nextsub = subfile->next;
740 xfree (subfile->name);
741 xfree (subfile->line_vector);
744 xfree (buildsym_compunit->comp_dir);
745 xfree (buildsym_compunit);
746 buildsym_compunit = NULL;
749 /* For stabs readers, the first N_SO symbol is assumed to be the
750 source file name, and the subfile struct is initialized using that
751 assumption. If another N_SO symbol is later seen, immediately
752 following the first one, then the first one is assumed to be the
753 directory name and the second one is really the source file name.
755 So we have to patch up the subfile struct by moving the old name
756 value to dirname and remembering the new name. Some sanity
757 checking is performed to ensure that the state of the subfile
758 struct is reasonable and that the old name we are assuming to be a
759 directory name actually is (by checking for a trailing '/'). */
762 patch_subfile_names (struct subfile *subfile, char *name)
765 && buildsym_compunit->comp_dir == NULL
766 && subfile->name != NULL
767 && IS_DIR_SEPARATOR (subfile->name[strlen (subfile->name) - 1]))
769 buildsym_compunit->comp_dir = subfile->name;
770 subfile->name = xstrdup (name);
771 set_last_source_file (name);
773 /* Default the source language to whatever can be deduced from
774 the filename. If nothing can be deduced (such as for a C/C++
775 include file with a ".h" extension), then inherit whatever
776 language the previous subfile had. This kludgery is
777 necessary because there is no standard way in some object
778 formats to record the source language. Also, when symtabs
779 are allocated we try to deduce a language then as well, but
780 it is too late for us to use that information while reading
781 symbols, since symtabs aren't allocated until after all the
782 symbols have been processed for a given source file. */
784 subfile->language = deduce_language_from_filename (subfile->name);
785 if (subfile->language == language_unknown
786 && subfile->next != NULL)
788 subfile->language = subfile->next->language;
793 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
794 switching source files (different subfiles, as we call them) within
795 one object file, but using a stack rather than in an arbitrary
801 struct subfile_stack *tem
802 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
804 tem->next = subfile_stack;
806 if (current_subfile == NULL || current_subfile->name == NULL)
808 internal_error (__FILE__, __LINE__,
809 _("failed internal consistency check"));
811 tem->name = current_subfile->name;
818 struct subfile_stack *link = subfile_stack;
822 internal_error (__FILE__, __LINE__,
823 _("failed internal consistency check"));
826 subfile_stack = link->next;
827 xfree ((void *) link);
831 /* Add a linetable entry for line number LINE and address PC to the
832 line vector for SUBFILE. */
835 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
837 struct linetable_entry *e;
839 /* Ignore the dummy line number in libg.o */
845 /* Make sure line vector exists and is big enough. */
846 if (!subfile->line_vector)
848 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
849 subfile->line_vector = (struct linetable *)
850 xmalloc (sizeof (struct linetable)
851 + subfile->line_vector_length * sizeof (struct linetable_entry));
852 subfile->line_vector->nitems = 0;
853 have_line_numbers = 1;
856 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
858 subfile->line_vector_length *= 2;
859 subfile->line_vector = (struct linetable *)
860 xrealloc ((char *) subfile->line_vector,
861 (sizeof (struct linetable)
862 + (subfile->line_vector_length
863 * sizeof (struct linetable_entry))));
866 /* Normally, we treat lines as unsorted. But the end of sequence
867 marker is special. We sort line markers at the same PC by line
868 number, so end of sequence markers (which have line == 0) appear
869 first. This is right if the marker ends the previous function,
870 and there is no padding before the next function. But it is
871 wrong if the previous line was empty and we are now marking a
872 switch to a different subfile. We must leave the end of sequence
873 marker at the end of this group of lines, not sort the empty line
874 to after the marker. The easiest way to accomplish this is to
875 delete any empty lines from our table, if they are followed by
876 end of sequence markers. All we lose is the ability to set
877 breakpoints at some lines which contain no instructions
879 if (line == 0 && subfile->line_vector->nitems > 0)
881 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
882 while (subfile->line_vector->nitems > 0 && e->pc == pc)
885 subfile->line_vector->nitems--;
889 e = subfile->line_vector->item + subfile->line_vector->nitems++;
894 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
897 compare_line_numbers (const void *ln1p, const void *ln2p)
899 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
900 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
902 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
903 Please keep it that way. */
904 if (ln1->pc < ln2->pc)
907 if (ln1->pc > ln2->pc)
910 /* If pc equal, sort by line. I'm not sure whether this is optimum
911 behavior (see comment at struct linetable in symtab.h). */
912 return ln1->line - ln2->line;
915 /* See buildsym.h. */
917 struct compunit_symtab *
918 buildsym_compunit_symtab (void)
920 gdb_assert (buildsym_compunit != NULL);
922 return buildsym_compunit->compunit_symtab;
925 /* See buildsym.h. */
928 get_macro_table (void)
930 struct objfile *objfile;
932 gdb_assert (buildsym_compunit != NULL);
934 objfile = buildsym_compunit->objfile;
936 if (! pending_macros)
938 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
939 objfile->per_bfd->macro_cache,
940 buildsym_compunit->compunit_symtab);
943 return pending_macros;
946 /* Start a new symtab for a new source file in OBJFILE. Called, for example,
947 when a stabs symbol of type N_SO is seen, or when a DWARF
948 TAG_compile_unit DIE is seen. It indicates the start of data for
949 one original source file.
951 NAME is the name of the file (cannot be NULL). COMP_DIR is the directory in
952 which the file was compiled (or NULL if not known). START_ADDR is the
953 lowest address of objects in the file (or 0 if not known). */
955 struct compunit_symtab *
956 start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
957 CORE_ADDR start_addr)
959 restart_symtab (start_addr);
961 buildsym_compunit = start_buildsym_compunit (objfile, comp_dir);
963 /* Allocate the primary symtab now. The caller needs it to allocate
964 non-primary symtabs. It is also needed by get_macro_table. */
965 buildsym_compunit->compunit_symtab = allocate_compunit_symtab (objfile,
968 /* Build the subfile for NAME (the main source file) so that we can record
969 a pointer to it for later.
970 IMPORTANT: Do not allocate a struct symtab for NAME here.
971 It can happen that the debug info provides a different path to NAME than
972 DIRNAME,NAME. We cope with this in watch_main_source_file_lossage but
973 that only works if the main_subfile doesn't have a symtab yet. */
974 start_subfile (name);
975 /* Save this so that we don't have to go looking for it at the end
976 of the subfiles list. */
977 buildsym_compunit->main_subfile = current_subfile;
979 set_last_source_file (name);
981 return buildsym_compunit->compunit_symtab;
984 /* Restart compilation for a symtab.
985 This is used when a symtab is built from multiple sources.
986 The symtab is first built with start_symtab and then for each additional
987 piece call restart_symtab. */
990 restart_symtab (CORE_ADDR start_addr)
992 set_last_source_file (NULL);
993 last_source_start_addr = start_addr;
995 global_symbols = NULL;
997 have_line_numbers = 0;
999 /* Context stack is initially empty. Allocate first one with room
1000 for 10 levels; reuse it forever afterward. */
1001 if (context_stack == NULL)
1003 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
1004 context_stack = (struct context_stack *)
1005 xmalloc (context_stack_size * sizeof (struct context_stack));
1007 context_stack_depth = 0;
1009 /* We shouldn't have any address map at this point. */
1010 gdb_assert (! pending_addrmap);
1012 /* Reset the sub source files list. The list should already be empty,
1013 but free it anyway in case some code didn't finish cleaning up after
1015 free_buildsym_compunit ();
1018 /* Subroutine of end_symtab to simplify it. Look for a subfile that
1019 matches the main source file's basename. If there is only one, and
1020 if the main source file doesn't have any symbol or line number
1021 information, then copy this file's symtab and line_vector to the
1022 main source file's subfile and discard the other subfile. This can
1023 happen because of a compiler bug or from the user playing games
1024 with #line or from things like a distributed build system that
1025 manipulates the debug info. This can also happen from an innocent
1026 symlink in the paths, we don't canonicalize paths here. */
1029 watch_main_source_file_lossage (void)
1031 struct subfile *mainsub, *subfile;
1033 /* We have to watch for buildsym_compunit == NULL here. It's a quirk of
1034 end_symtab, it can return NULL so there may not be a main subfile. */
1035 if (buildsym_compunit == NULL)
1038 /* Get the main source file. */
1039 mainsub = buildsym_compunit->main_subfile;
1041 /* If the main source file doesn't have any line number or symbol
1042 info, look for an alias in another subfile. */
1044 if (mainsub->line_vector == NULL
1045 && mainsub->symtab == NULL)
1047 const char *mainbase = lbasename (mainsub->name);
1049 struct subfile *prevsub;
1050 struct subfile *mainsub_alias = NULL;
1051 struct subfile *prev_mainsub_alias = NULL;
1054 for (subfile = buildsym_compunit->subfiles;
1056 subfile = subfile->next)
1058 if (subfile == mainsub)
1060 if (filename_cmp (lbasename (subfile->name), mainbase) == 0)
1063 mainsub_alias = subfile;
1064 prev_mainsub_alias = prevsub;
1069 if (nr_matches == 1)
1071 gdb_assert (mainsub_alias != NULL && mainsub_alias != mainsub);
1073 /* Found a match for the main source file.
1074 Copy its line_vector and symtab to the main subfile
1075 and then discard it. */
1077 mainsub->line_vector = mainsub_alias->line_vector;
1078 mainsub->line_vector_length = mainsub_alias->line_vector_length;
1079 mainsub->symtab = mainsub_alias->symtab;
1081 if (prev_mainsub_alias == NULL)
1082 buildsym_compunit->subfiles = mainsub_alias->next;
1084 prev_mainsub_alias->next = mainsub_alias->next;
1085 xfree (mainsub_alias->name);
1086 xfree (mainsub_alias);
1091 /* Helper function for qsort. Parameters are `struct block *' pointers,
1092 function sorts them in descending order by their BLOCK_START. */
1095 block_compar (const void *ap, const void *bp)
1097 const struct block *a = *(const struct block **) ap;
1098 const struct block *b = *(const struct block **) bp;
1100 return ((BLOCK_START (b) > BLOCK_START (a))
1101 - (BLOCK_START (b) < BLOCK_START (a)));
1104 /* Reset globals used to build symtabs. */
1107 reset_symtab_globals (void)
1109 set_last_source_file (NULL);
1110 free_buildsym_compunit ();
1111 pending_macros = NULL;
1112 if (pending_addrmap)
1114 obstack_free (&pending_addrmap_obstack, NULL);
1115 pending_addrmap = NULL;
1119 /* Implementation of the first part of end_symtab. It allows modifying
1120 STATIC_BLOCK before it gets finalized by end_symtab_from_static_block.
1121 If the returned value is NULL there is no blockvector created for
1122 this symtab (you still must call end_symtab_from_static_block).
1124 END_ADDR is the same as for end_symtab: the address of the end of the
1127 If EXPANDABLE is non-zero the STATIC_BLOCK dictionary is made
1130 If REQUIRED is non-zero, then a symtab is created even if it does
1131 not contain any symbols. */
1134 end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
1136 struct objfile *objfile = buildsym_compunit->objfile;
1138 /* Finish the lexical context of the last function in the file; pop
1139 the context stack. */
1141 if (context_stack_depth > 0)
1143 struct context_stack *cstk = pop_context ();
1145 /* Make a block for the local symbols within. */
1146 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1147 cstk->start_addr, end_addr);
1149 if (context_stack_depth > 0)
1151 /* This is said to happen with SCO. The old coffread.c
1152 code simply emptied the context stack, so we do the
1153 same. FIXME: Find out why it is happening. This is not
1154 believed to happen in most cases (even for coffread.c);
1155 it used to be an abort(). */
1156 complaint (&symfile_complaints,
1157 _("Context stack not empty in end_symtab"));
1158 context_stack_depth = 0;
1162 /* Reordered executables may have out of order pending blocks; if
1163 OBJF_REORDERED is true, then sort the pending blocks. */
1165 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
1168 struct pending_block *pb;
1169 struct block **barray, **bp;
1170 struct cleanup *back_to;
1172 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1175 barray = xmalloc (sizeof (*barray) * count);
1176 back_to = make_cleanup (xfree, barray);
1179 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1182 qsort (barray, count, sizeof (*barray), block_compar);
1185 for (pb = pending_blocks; pb != NULL; pb = pb->next)
1188 do_cleanups (back_to);
1191 /* Cleanup any undefined types that have been left hanging around
1192 (this needs to be done before the finish_blocks so that
1193 file_symbols is still good).
1195 Both cleanup_undefined_stabs_types and finish_global_stabs are stabs
1196 specific, but harmless for other symbol readers, since on gdb
1197 startup or when finished reading stabs, the state is set so these
1198 are no-ops. FIXME: Is this handled right in case of QUIT? Can
1199 we make this cleaner? */
1201 cleanup_undefined_stabs_types (objfile);
1202 finish_global_stabs (objfile);
1205 && pending_blocks == NULL
1206 && file_symbols == NULL
1207 && global_symbols == NULL
1208 && have_line_numbers == 0
1209 && pending_macros == NULL)
1211 /* Ignore symtabs that have no functions with real debugging info. */
1216 /* Define the STATIC_BLOCK. */
1217 return finish_block_internal (NULL, &file_symbols, NULL,
1218 last_source_start_addr, end_addr,
1223 /* Subroutine of end_symtab_from_static_block to simplify it.
1224 Handle the "no blockvector" case.
1225 When this happens there is nothing to record, so just free up
1226 any memory we allocated while reading debug info. */
1229 end_symtab_without_blockvector (void)
1231 /* Free up all the subfiles.
1232 We won't be adding a compunit to the objfile's list of compunits,
1233 so there's nothing to unchain. However, since each symtab
1234 is added to the objfile's obstack we can't free that space.
1235 We could do better, but this is believed to be a sufficiently rare
1237 free_buildsym_compunit ();
1240 /* Subroutine of end_symtab_from_static_block to simplify it.
1241 Handle the "have blockvector" case.
1242 See end_symtab_from_static_block for a description of the arguments. */
1244 static struct compunit_symtab *
1245 end_symtab_with_blockvector (struct block *static_block,
1246 int section, int expandable)
1248 struct objfile *objfile = buildsym_compunit->objfile;
1249 struct compunit_symtab *cu = buildsym_compunit->compunit_symtab;
1250 struct symtab *symtab;
1251 struct blockvector *blockvector;
1252 struct subfile *subfile;
1255 gdb_assert (static_block != NULL);
1256 gdb_assert (buildsym_compunit != NULL);
1257 gdb_assert (buildsym_compunit->subfiles != NULL);
1259 end_addr = BLOCK_END (static_block);
1261 /* Create the GLOBAL_BLOCK and build the blockvector. */
1262 finish_block_internal (NULL, &global_symbols, NULL,
1263 last_source_start_addr, end_addr,
1265 blockvector = make_blockvector ();
1267 /* Read the line table if it has to be read separately.
1268 This is only used by xcoffread.c. */
1269 if (objfile->sf->sym_read_linetable != NULL)
1270 objfile->sf->sym_read_linetable (objfile);
1272 /* Handle the case where the debug info specifies a different path
1273 for the main source file. It can cause us to lose track of its
1274 line number information. */
1275 watch_main_source_file_lossage ();
1277 /* Now create the symtab objects proper, if not already done,
1278 one for each subfile. */
1280 for (subfile = buildsym_compunit->subfiles;
1282 subfile = subfile->next)
1284 int linetablesize = 0;
1286 if (subfile->line_vector)
1288 linetablesize = sizeof (struct linetable) +
1289 subfile->line_vector->nitems * sizeof (struct linetable_entry);
1291 /* Like the pending blocks, the line table may be
1292 scrambled in reordered executables. Sort it if
1293 OBJF_REORDERED is true. */
1294 if (objfile->flags & OBJF_REORDERED)
1295 qsort (subfile->line_vector->item,
1296 subfile->line_vector->nitems,
1297 sizeof (struct linetable_entry), compare_line_numbers);
1300 /* Allocate a symbol table if necessary. */
1301 if (subfile->symtab == NULL)
1302 subfile->symtab = allocate_symtab (cu, subfile->name);
1303 symtab = subfile->symtab;
1305 /* Fill in its components. */
1307 if (subfile->line_vector)
1309 /* Reallocate the line table on the symbol obstack. */
1310 SYMTAB_LINETABLE (symtab) = (struct linetable *)
1311 obstack_alloc (&objfile->objfile_obstack, linetablesize);
1312 memcpy (SYMTAB_LINETABLE (symtab), subfile->line_vector,
1317 SYMTAB_LINETABLE (symtab) = NULL;
1320 /* Use whatever language we have been using for this
1321 subfile, not the one that was deduced in allocate_symtab
1322 from the filename. We already did our own deducing when
1323 we created the subfile, and we may have altered our
1324 opinion of what language it is from things we found in
1326 symtab->language = subfile->language;
1329 /* Make sure the symtab of main_subfile is the first in its list. */
1331 struct symtab *main_symtab, *prev_symtab;
1333 main_symtab = buildsym_compunit->main_subfile->symtab;
1335 ALL_COMPUNIT_FILETABS (cu, symtab)
1337 if (symtab == main_symtab)
1339 if (prev_symtab != NULL)
1341 prev_symtab->next = main_symtab->next;
1342 main_symtab->next = COMPUNIT_FILETABS (cu);
1343 COMPUNIT_FILETABS (cu) = main_symtab;
1347 prev_symtab = symtab;
1349 gdb_assert (main_symtab == COMPUNIT_FILETABS (cu));
1352 /* Fill out the primary symtab. */
1354 if (buildsym_compunit->comp_dir != NULL)
1356 /* Reallocate the dirname on the symbol obstack. */
1357 COMPUNIT_DIRNAME (cu)
1358 = obstack_copy0 (&objfile->objfile_obstack,
1359 buildsym_compunit->comp_dir,
1360 strlen (buildsym_compunit->comp_dir));
1363 /* Save the debug format string (if any) in the symtab. */
1364 COMPUNIT_DEBUGFORMAT (cu) = buildsym_compunit->debugformat;
1366 /* Similarly for the producer. */
1367 COMPUNIT_PRODUCER (cu) = buildsym_compunit->producer;
1369 COMPUNIT_BLOCKVECTOR (cu) = blockvector;
1371 struct block *b = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1373 set_block_compunit_symtab (b, cu);
1376 COMPUNIT_BLOCK_LINE_SECTION (cu) = section;
1378 COMPUNIT_MACRO_TABLE (cu) = pending_macros;
1380 /* Default any symbols without a specified symtab to the primary symtab. */
1384 /* The main source file's symtab. */
1385 symtab = COMPUNIT_FILETABS (cu);
1387 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1389 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1391 struct dict_iterator iter;
1393 /* Inlined functions may have symbols not in the global or
1394 static symbol lists. */
1395 if (BLOCK_FUNCTION (block) != NULL)
1396 if (SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) == NULL)
1397 SYMBOL_SYMTAB (BLOCK_FUNCTION (block)) = symtab;
1399 /* Note that we only want to fix up symbols from the local
1400 blocks, not blocks coming from included symtabs. That is why
1401 we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS. */
1402 ALL_DICT_SYMBOLS (BLOCK_DICT (block), iter, sym)
1403 if (SYMBOL_SYMTAB (sym) == NULL)
1404 SYMBOL_SYMTAB (sym) = symtab;
1408 add_compunit_symtab_to_objfile (cu);
1409 free_buildsym_compunit ();
1414 /* Implementation of the second part of end_symtab. Pass STATIC_BLOCK
1415 as value returned by end_symtab_get_static_block.
1417 SECTION is the same as for end_symtab: the section number
1418 (in objfile->section_offsets) of the blockvector and linetable.
1420 If EXPANDABLE is non-zero the GLOBAL_BLOCK dictionary is made
1423 struct compunit_symtab *
1424 end_symtab_from_static_block (struct block *static_block,
1425 int section, int expandable)
1427 struct compunit_symtab *cu;
1429 if (static_block == NULL)
1431 end_symtab_without_blockvector ();
1435 cu = end_symtab_with_blockvector (static_block, section, expandable);
1437 reset_symtab_globals ();
1442 /* Finish the symbol definitions for one main source file, close off
1443 all the lexical contexts for that file (creating struct block's for
1444 them), then make the struct symtab for that file and put it in the
1447 END_ADDR is the address of the end of the file's text. SECTION is
1448 the section number (in objfile->section_offsets) of the blockvector
1451 Note that it is possible for end_symtab() to return NULL. In
1452 particular, for the DWARF case at least, it will return NULL when
1453 it finds a compilation unit that has exactly one DIE, a
1454 TAG_compile_unit DIE. This can happen when we link in an object
1455 file that was compiled from an empty source file. Returning NULL
1456 is probably not the correct thing to do, because then gdb will
1457 never know about this empty file (FIXME).
1459 If you need to modify STATIC_BLOCK before it is finalized you should
1460 call end_symtab_get_static_block and end_symtab_from_static_block
1463 struct compunit_symtab *
1464 end_symtab (CORE_ADDR end_addr, int section)
1466 struct block *static_block;
1468 static_block = end_symtab_get_static_block (end_addr, 0, 0);
1469 return end_symtab_from_static_block (static_block, section, 0);
1472 /* Same as end_symtab except create a symtab that can be later added to. */
1474 struct compunit_symtab *
1475 end_expandable_symtab (CORE_ADDR end_addr, int section)
1477 struct block *static_block;
1479 static_block = end_symtab_get_static_block (end_addr, 1, 0);
1480 return end_symtab_from_static_block (static_block, section, 1);
1483 /* Subroutine of augment_type_symtab to simplify it.
1484 Attach the main source file's symtab to all symbols in PENDING_LIST that
1488 set_missing_symtab (struct pending *pending_list,
1489 struct compunit_symtab *cu)
1491 struct pending *pending;
1494 for (pending = pending_list; pending != NULL; pending = pending->next)
1496 for (i = 0; i < pending->nsyms; ++i)
1498 if (SYMBOL_SYMTAB (pending->symbol[i]) == NULL)
1499 SYMBOL_SYMTAB (pending->symbol[i]) = COMPUNIT_FILETABS (cu);
1504 /* Same as end_symtab, but for the case where we're adding more symbols
1505 to an existing symtab that is known to contain only type information.
1506 This is the case for DWARF4 Type Units. */
1509 augment_type_symtab (struct compunit_symtab *cust)
1511 const struct blockvector *blockvector = COMPUNIT_BLOCKVECTOR (cust);
1513 if (context_stack_depth > 0)
1515 complaint (&symfile_complaints,
1516 _("Context stack not empty in augment_type_symtab"));
1517 context_stack_depth = 0;
1519 if (pending_blocks != NULL)
1520 complaint (&symfile_complaints, _("Blocks in a type symtab"));
1521 if (pending_macros != NULL)
1522 complaint (&symfile_complaints, _("Macro in a type symtab"));
1523 if (have_line_numbers)
1524 complaint (&symfile_complaints,
1525 _("Line numbers recorded in a type symtab"));
1527 if (file_symbols != NULL)
1529 struct block *block = BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK);
1531 /* First mark any symbols without a specified symtab as belonging
1532 to the primary symtab. */
1533 set_missing_symtab (file_symbols, cust);
1535 dict_add_pending (BLOCK_DICT (block), file_symbols);
1538 if (global_symbols != NULL)
1540 struct block *block = BLOCKVECTOR_BLOCK (blockvector, GLOBAL_BLOCK);
1542 /* First mark any symbols without a specified symtab as belonging
1543 to the primary symtab. */
1544 set_missing_symtab (global_symbols, cust);
1546 dict_add_pending (BLOCK_DICT (block), global_symbols);
1549 reset_symtab_globals ();
1552 /* Push a context block. Args are an identifying nesting level
1553 (checkable when you pop it), and the starting PC address of this
1556 struct context_stack *
1557 push_context (int desc, CORE_ADDR valu)
1559 struct context_stack *new;
1561 if (context_stack_depth == context_stack_size)
1563 context_stack_size *= 2;
1564 context_stack = (struct context_stack *)
1565 xrealloc ((char *) context_stack,
1566 (context_stack_size * sizeof (struct context_stack)));
1569 new = &context_stack[context_stack_depth++];
1571 new->locals = local_symbols;
1572 new->old_blocks = pending_blocks;
1573 new->start_addr = valu;
1574 new->using_directives = using_directives;
1577 local_symbols = NULL;
1578 using_directives = NULL;
1583 /* Pop a context block. Returns the address of the context block just
1586 struct context_stack *
1589 gdb_assert (context_stack_depth > 0);
1590 return (&context_stack[--context_stack_depth]);
1595 /* Compute a small integer hash code for the given name. */
1598 hashname (const char *name)
1600 return (hash(name,strlen(name)) % HASHSIZE);
1605 record_debugformat (const char *format)
1607 buildsym_compunit->debugformat = format;
1611 record_producer (const char *producer)
1613 buildsym_compunit->producer = producer;
1616 /* Merge the first symbol list SRCLIST into the second symbol list
1617 TARGETLIST by repeated calls to add_symbol_to_list(). This
1618 procedure "frees" each link of SRCLIST by adding it to the
1619 free_pendings list. Caller must set SRCLIST to a null list after
1620 calling this function.
1625 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1629 if (!srclist || !*srclist)
1632 /* Merge in elements from current link. */
1633 for (i = 0; i < (*srclist)->nsyms; i++)
1634 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1636 /* Recurse on next. */
1637 merge_symbol_lists (&(*srclist)->next, targetlist);
1639 /* "Free" the current link. */
1640 (*srclist)->next = free_pendings;
1641 free_pendings = (*srclist);
1645 /* Name of source file whose symbol data we are now processing. This
1646 comes from a symbol of type N_SO for stabs. For Dwarf it comes
1647 from the DW_AT_name attribute of a DW_TAG_compile_unit DIE. */
1649 static char *last_source_file;
1651 /* See buildsym.h. */
1654 set_last_source_file (const char *name)
1656 xfree (last_source_file);
1657 last_source_file = name == NULL ? NULL : xstrdup (name);
1660 /* See buildsym.h. */
1663 get_last_source_file (void)
1665 return last_source_file;
1670 /* Initialize anything that needs initializing when starting to read a
1671 fresh piece of a symbol file, e.g. reading in the stuff
1672 corresponding to a psymtab. */
1675 buildsym_init (void)
1677 free_pendings = NULL;
1678 file_symbols = NULL;
1679 global_symbols = NULL;
1680 pending_blocks = NULL;
1681 pending_macros = NULL;
1682 using_directives = NULL;
1683 subfile_stack = NULL;
1685 /* We shouldn't have any address map at this point. */
1686 gdb_assert (! pending_addrmap);
1687 pending_addrmap_interesting = 0;
1690 /* Initialize anything that needs initializing when a completely new
1691 symbol file is specified (not just adding some symbols from another
1692 file, e.g. a shared library). */
1695 buildsym_new_init (void)