1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* This module provides subroutines used for creating and adding to
22 the symbol table. These routines are called from various symbol-
23 file-reading routines.
25 Routines to support specific debugging information formats (stabs,
26 DWARF, etc) belong somewhere else. */
30 #include "gdb_obstack.h"
35 #include "gdb_assert.h"
36 #include "complaints.h"
37 #include "gdb_string.h"
38 #include "expression.h" /* For "enum exp_opcode" used by... */
40 #include "filenames.h" /* For DOSish file names */
42 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
44 #include "cp-support.h"
45 #include "dictionary.h"
47 /* Ask buildsym.h to define the vars it normally declares `extern'. */
50 #include "buildsym.h" /* Our own declarations */
53 /* For cleanup_undefined_types and finish_global_stabs (somewhat
54 questionable--see comment where we call them). */
56 #include "stabsread.h"
58 /* List of subfiles. */
60 static struct subfile *subfiles;
62 /* List of free `struct pending' structures for reuse. */
64 static struct pending *free_pendings;
66 /* Non-zero if symtab has line number info. This prevents an
67 otherwise empty symtab from being tossed. */
69 static int have_line_numbers;
71 static int compare_line_numbers (const void *ln1p, const void *ln2p);
74 /* Initial sizes of data structures. These are realloc'd larger if
75 needed, and realloc'd down to the size actually used, when
78 #define INITIAL_CONTEXT_STACK_SIZE 10
79 #define INITIAL_LINE_VECTOR_LENGTH 1000
82 /* maintain the lists of symbols and blocks */
84 /* Add a pending list to free_pendings. */
86 add_free_pendings (struct pending *list)
88 struct pending *link = list;
92 while (link->next) link = link->next;
93 link->next = free_pendings;
98 /* Add a symbol to one of the lists of symbols. While we're at it, if
99 we're in the C++ case and don't have full namespace debugging info,
100 check to see if it references an anonymous namespace; if so, add an
101 appropriate using directive. */
104 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
106 struct pending *link;
108 /* If this is an alias for another symbol, don't add it. */
109 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
112 /* We keep PENDINGSIZE symbols in each link of the list. If we
113 don't have a link with room in it, add a new link. */
114 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
118 link = free_pendings;
119 free_pendings = link->next;
123 link = (struct pending *) xmalloc (sizeof (struct pending));
126 link->next = *listhead;
131 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
133 /* Check to see if we might need to look for a mention of anonymous
136 if (SYMBOL_LANGUAGE (symbol) == language_cplus)
137 cp_scan_for_anonymous_namespaces (symbol);
140 /* Find a symbol named NAME on a LIST. NAME need not be
141 '\0'-terminated; LENGTH is the length of the name. */
144 find_symbol_in_list (struct pending *list, char *name, int length)
151 for (j = list->nsyms; --j >= 0;)
153 pp = DEPRECATED_SYMBOL_NAME (list->symbol[j]);
154 if (*pp == *name && strncmp (pp, name, length) == 0 &&
157 return (list->symbol[j]);
165 /* At end of reading syms, or in case of quit, really free as many
166 `struct pending's as we can easily find. */
169 really_free_pendings (void *dummy)
171 struct pending *next, *next1;
173 for (next = free_pendings; next; next = next1)
176 xfree ((void *) next);
178 free_pendings = NULL;
180 free_pending_blocks ();
182 for (next = file_symbols; next != NULL; next = next1)
185 xfree ((void *) next);
189 for (next = global_symbols; next != NULL; next = next1)
192 xfree ((void *) next);
194 global_symbols = NULL;
197 free_macro_table (pending_macros);
200 /* This function is called to discard any pending blocks. */
203 free_pending_blocks (void)
205 /* The links are made in the objfile_obstack, so we only need to
206 reset PENDING_BLOCKS. */
207 pending_blocks = NULL;
210 /* Take one of the lists of symbols and make a block from it. Keep
211 the order the symbols have in the list (reversed from the input
212 file). Put the block on the list of pending blocks. */
215 finish_block (struct symbol *symbol, struct pending **listhead,
216 struct pending_block *old_blocks,
217 CORE_ADDR start, CORE_ADDR end,
218 struct objfile *objfile)
220 struct pending *next, *next1;
222 struct pending_block *pblock;
223 struct pending_block *opblock;
225 block = allocate_block (&objfile->objfile_obstack);
229 BLOCK_DICT (block) = dict_create_linear (&objfile->objfile_obstack,
234 BLOCK_DICT (block) = dict_create_hashed (&objfile->objfile_obstack,
238 BLOCK_START (block) = start;
239 BLOCK_END (block) = end;
240 /* Superblock filled in when containing block is made */
241 BLOCK_SUPERBLOCK (block) = NULL;
242 BLOCK_NAMESPACE (block) = NULL;
244 /* Put the block in as the value of the symbol that names it. */
248 struct type *ftype = SYMBOL_TYPE (symbol);
249 struct dict_iterator iter;
250 SYMBOL_BLOCK_VALUE (symbol) = block;
251 BLOCK_FUNCTION (block) = symbol;
253 if (TYPE_NFIELDS (ftype) <= 0)
255 /* No parameter type information is recorded with the
256 function's type. Set that from the type of the
257 parameter symbols. */
258 int nparams = 0, iparams;
260 ALL_BLOCK_SYMBOLS (block, iter, sym)
262 switch (SYMBOL_CLASS (sym))
267 case LOC_REGPARM_ADDR:
268 case LOC_BASEREG_ARG:
270 case LOC_COMPUTED_ARG:
282 case LOC_CONST_BYTES:
285 case LOC_OPTIMIZED_OUT:
293 TYPE_NFIELDS (ftype) = nparams;
294 TYPE_FIELDS (ftype) = (struct field *)
295 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
298 ALL_BLOCK_SYMBOLS (block, iter, sym)
300 if (iparams == nparams)
303 switch (SYMBOL_CLASS (sym))
308 case LOC_REGPARM_ADDR:
309 case LOC_BASEREG_ARG:
311 case LOC_COMPUTED_ARG:
312 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
313 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
325 case LOC_CONST_BYTES:
328 case LOC_OPTIMIZED_OUT:
337 /* If we're in the C++ case, set the block's scope. */
338 if (SYMBOL_LANGUAGE (symbol) == language_cplus)
340 cp_set_block_scope (symbol, block, &objfile->objfile_obstack);
345 BLOCK_FUNCTION (block) = NULL;
348 /* Now "free" the links of the list, and empty the list. */
350 for (next = *listhead; next; next = next1)
353 next->next = free_pendings;
354 free_pendings = next;
358 /* Check to be sure that the blocks have an end address that is
359 greater than starting address */
361 if (BLOCK_END (block) < BLOCK_START (block))
365 complaint (&symfile_complaints,
366 _("block end address less than block start address in %s (patched it)"),
367 SYMBOL_PRINT_NAME (symbol));
371 complaint (&symfile_complaints,
372 _("block end address 0x%s less than block start address 0x%s (patched it)"),
373 paddr_nz (BLOCK_END (block)), paddr_nz (BLOCK_START (block)));
375 /* Better than nothing */
376 BLOCK_END (block) = BLOCK_START (block);
379 /* Install this block as the superblock of all blocks made since the
380 start of this scope that don't have superblocks yet. */
383 for (pblock = pending_blocks;
384 pblock && pblock != old_blocks;
385 pblock = pblock->next)
387 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
389 /* Check to be sure the blocks are nested as we receive
390 them. If the compiler/assembler/linker work, this just
391 burns a small amount of time.
393 Skip blocks which correspond to a function; they're not
394 physically nested inside this other blocks, only
396 if (BLOCK_FUNCTION (pblock->block) == NULL
397 && (BLOCK_START (pblock->block) < BLOCK_START (block)
398 || BLOCK_END (pblock->block) > BLOCK_END (block)))
402 complaint (&symfile_complaints,
403 _("inner block not inside outer block in %s"),
404 SYMBOL_PRINT_NAME (symbol));
408 complaint (&symfile_complaints,
409 _("inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)"),
410 paddr_nz (BLOCK_START (pblock->block)),
411 paddr_nz (BLOCK_END (pblock->block)),
412 paddr_nz (BLOCK_START (block)),
413 paddr_nz (BLOCK_END (block)));
415 if (BLOCK_START (pblock->block) < BLOCK_START (block))
416 BLOCK_START (pblock->block) = BLOCK_START (block);
417 if (BLOCK_END (pblock->block) > BLOCK_END (block))
418 BLOCK_END (pblock->block) = BLOCK_END (block);
420 BLOCK_SUPERBLOCK (pblock->block) = block;
425 record_pending_block (objfile, block, opblock);
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 pblock = (struct pending_block *)
443 obstack_alloc (&objfile->objfile_obstack, sizeof (struct pending_block));
444 pblock->block = block;
447 pblock->next = opblock->next;
448 opblock->next = pblock;
452 pblock->next = pending_blocks;
453 pending_blocks = pblock;
457 static struct blockvector *
458 make_blockvector (struct objfile *objfile)
460 struct pending_block *next;
461 struct blockvector *blockvector;
464 /* Count the length of the list of blocks. */
466 for (next = pending_blocks, i = 0; next; next = next->next, i++)
470 blockvector = (struct blockvector *)
471 obstack_alloc (&objfile->objfile_obstack,
472 (sizeof (struct blockvector)
473 + (i - 1) * sizeof (struct block *)));
475 /* Copy the blocks into the blockvector. This is done in reverse
476 order, which happens to put the blocks into the proper order
477 (ascending starting address). finish_block has hair to insert
478 each block into the list after its subblocks in order to make
479 sure this is true. */
481 BLOCKVECTOR_NBLOCKS (blockvector) = i;
482 for (next = pending_blocks; next; next = next->next)
484 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
487 free_pending_blocks ();
489 /* Some compilers output blocks in the wrong order, but we depend on
490 their being in the right order so we can binary search. Check the
491 order and moan about it. */
492 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
494 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
496 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
497 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
500 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
502 complaint (&symfile_complaints, _("block at %s out of order"),
503 hex_string ((LONGEST) start));
508 return (blockvector);
511 /* Start recording information about source code that came from an
512 included (or otherwise merged-in) source file with a different
513 name. NAME is the name of the file (cannot be NULL), DIRNAME is
514 the directory in which the file was compiled (or NULL if not known). */
517 start_subfile (char *name, char *dirname)
519 struct subfile *subfile;
521 /* See if this subfile is already known as a subfile of the current
524 for (subfile = subfiles; subfile; subfile = subfile->next)
528 /* If NAME is an absolute path, and this subfile is not, then
529 attempt to create an absolute path to compare. */
530 if (IS_ABSOLUTE_PATH (name)
531 && !IS_ABSOLUTE_PATH (subfile->name)
532 && subfile->dirname != NULL)
533 subfile_name = concat (subfile->dirname, SLASH_STRING,
534 subfile->name, NULL);
536 subfile_name = subfile->name;
538 if (FILENAME_CMP (subfile_name, name) == 0)
540 current_subfile = subfile;
541 if (subfile_name != subfile->name)
542 xfree (subfile_name);
545 if (subfile_name != subfile->name)
546 xfree (subfile_name);
549 /* This subfile is not known. Add an entry for it. Make an entry
550 for this subfile in the list of all subfiles of the current main
553 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
554 memset ((char *) subfile, 0, sizeof (struct subfile));
555 subfile->next = subfiles;
557 current_subfile = subfile;
559 /* Save its name and compilation directory name */
560 subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
562 (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
564 /* Initialize line-number recording for this subfile. */
565 subfile->line_vector = NULL;
567 /* Default the source language to whatever can be deduced from the
568 filename. If nothing can be deduced (such as for a C/C++ include
569 file with a ".h" extension), then inherit whatever language the
570 previous subfile had. This kludgery is necessary because there
571 is no standard way in some object formats to record the source
572 language. Also, when symtabs are allocated we try to deduce a
573 language then as well, but it is too late for us to use that
574 information while reading symbols, since symtabs aren't allocated
575 until after all the symbols have been processed for a given
578 subfile->language = deduce_language_from_filename (subfile->name);
579 if (subfile->language == language_unknown &&
580 subfile->next != NULL)
582 subfile->language = subfile->next->language;
585 /* Initialize the debug format string to NULL. We may supply it
586 later via a call to record_debugformat. */
587 subfile->debugformat = NULL;
589 /* Similarly for the producer. */
590 subfile->producer = NULL;
592 /* If the filename of this subfile ends in .C, then change the
593 language of any pending subfiles from C to C++. We also accept
594 any other C++ suffixes accepted by deduce_language_from_filename. */
595 /* Likewise for f2c. */
600 enum language sublang = deduce_language_from_filename (subfile->name);
602 if (sublang == language_cplus || sublang == language_fortran)
603 for (s = subfiles; s != NULL; s = s->next)
604 if (s->language == language_c)
605 s->language = sublang;
608 /* And patch up this file if necessary. */
609 if (subfile->language == language_c
610 && subfile->next != NULL
611 && (subfile->next->language == language_cplus
612 || subfile->next->language == language_fortran))
614 subfile->language = subfile->next->language;
618 /* For stabs readers, the first N_SO symbol is assumed to be the
619 source file name, and the subfile struct is initialized using that
620 assumption. If another N_SO symbol is later seen, immediately
621 following the first one, then the first one is assumed to be the
622 directory name and the second one is really the source file name.
624 So we have to patch up the subfile struct by moving the old name
625 value to dirname and remembering the new name. Some sanity
626 checking is performed to ensure that the state of the subfile
627 struct is reasonable and that the old name we are assuming to be a
628 directory name actually is (by checking for a trailing '/'). */
631 patch_subfile_names (struct subfile *subfile, char *name)
633 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
634 && subfile->name[strlen (subfile->name) - 1] == '/')
636 subfile->dirname = subfile->name;
637 subfile->name = savestring (name, strlen (name));
638 last_source_file = name;
640 /* Default the source language to whatever can be deduced from
641 the filename. If nothing can be deduced (such as for a C/C++
642 include file with a ".h" extension), then inherit whatever
643 language the previous subfile had. This kludgery is
644 necessary because there is no standard way in some object
645 formats to record the source language. Also, when symtabs
646 are allocated we try to deduce a language then as well, but
647 it is too late for us to use that information while reading
648 symbols, since symtabs aren't allocated until after all the
649 symbols have been processed for a given source file. */
651 subfile->language = deduce_language_from_filename (subfile->name);
652 if (subfile->language == language_unknown &&
653 subfile->next != NULL)
655 subfile->language = subfile->next->language;
660 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
661 switching source files (different subfiles, as we call them) within
662 one object file, but using a stack rather than in an arbitrary
668 struct subfile_stack *tem
669 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
671 tem->next = subfile_stack;
673 if (current_subfile == NULL || current_subfile->name == NULL)
675 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
677 tem->name = current_subfile->name;
684 struct subfile_stack *link = subfile_stack;
688 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
691 subfile_stack = link->next;
692 xfree ((void *) link);
696 /* Add a linetable entry for line number LINE and address PC to the
697 line vector for SUBFILE. */
700 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
702 struct linetable_entry *e;
703 /* Ignore the dummy line number in libg.o */
710 /* Make sure line vector exists and is big enough. */
711 if (!subfile->line_vector)
713 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
714 subfile->line_vector = (struct linetable *)
715 xmalloc (sizeof (struct linetable)
716 + subfile->line_vector_length * sizeof (struct linetable_entry));
717 subfile->line_vector->nitems = 0;
718 have_line_numbers = 1;
721 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
723 subfile->line_vector_length *= 2;
724 subfile->line_vector = (struct linetable *)
725 xrealloc ((char *) subfile->line_vector,
726 (sizeof (struct linetable)
727 + (subfile->line_vector_length
728 * sizeof (struct linetable_entry))));
731 pc = gdbarch_addr_bits_remove (current_gdbarch, pc);
733 /* Normally, we treat lines as unsorted. But the end of sequence
734 marker is special. We sort line markers at the same PC by line
735 number, so end of sequence markers (which have line == 0) appear
736 first. This is right if the marker ends the previous function,
737 and there is no padding before the next function. But it is
738 wrong if the previous line was empty and we are now marking a
739 switch to a different subfile. We must leave the end of sequence
740 marker at the end of this group of lines, not sort the empty line
741 to after the marker. The easiest way to accomplish this is to
742 delete any empty lines from our table, if they are followed by
743 end of sequence markers. All we lose is the ability to set
744 breakpoints at some lines which contain no instructions
746 if (line == 0 && subfile->line_vector->nitems > 0)
748 e = subfile->line_vector->item + subfile->line_vector->nitems - 1;
749 while (subfile->line_vector->nitems > 0 && e->pc == pc)
752 subfile->line_vector->nitems--;
756 e = subfile->line_vector->item + subfile->line_vector->nitems++;
761 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
764 compare_line_numbers (const void *ln1p, const void *ln2p)
766 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
767 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
769 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
770 Please keep it that way. */
771 if (ln1->pc < ln2->pc)
774 if (ln1->pc > ln2->pc)
777 /* If pc equal, sort by line. I'm not sure whether this is optimum
778 behavior (see comment at struct linetable in symtab.h). */
779 return ln1->line - ln2->line;
782 /* Start a new symtab for a new source file. Called, for example,
783 when a stabs symbol of type N_SO is seen, or when a DWARF
784 TAG_compile_unit DIE is seen. It indicates the start of data for
785 one original source file.
787 NAME is the name of the file (cannot be NULL). DIRNAME is the directory in
788 which the file was compiled (or NULL if not known). START_ADDR is the
789 lowest address of objects in the file (or 0 if not known). */
792 start_symtab (char *name, char *dirname, CORE_ADDR start_addr)
794 last_source_file = name;
795 last_source_start_addr = start_addr;
797 global_symbols = NULL;
799 have_line_numbers = 0;
801 /* Context stack is initially empty. Allocate first one with room
802 for 10 levels; reuse it forever afterward. */
803 if (context_stack == NULL)
805 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
806 context_stack = (struct context_stack *)
807 xmalloc (context_stack_size * sizeof (struct context_stack));
809 context_stack_depth = 0;
811 /* Set up support for C++ namespace support, in case we need it. */
813 cp_initialize_namespace ();
815 /* Initialize the list of sub source files with one entry for this
816 file (the top-level source file). */
819 current_subfile = NULL;
820 start_subfile (name, dirname);
823 /* Finish the symbol definitions for one main source file, close off
824 all the lexical contexts for that file (creating struct block's for
825 them), then make the struct symtab for that file and put it in the
828 END_ADDR is the address of the end of the file's text. SECTION is
829 the section number (in objfile->section_offsets) of the blockvector
832 Note that it is possible for end_symtab() to return NULL. In
833 particular, for the DWARF case at least, it will return NULL when
834 it finds a compilation unit that has exactly one DIE, a
835 TAG_compile_unit DIE. This can happen when we link in an object
836 file that was compiled from an empty source file. Returning NULL
837 is probably not the correct thing to do, because then gdb will
838 never know about this empty file (FIXME). */
841 end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
843 struct symtab *symtab = NULL;
844 struct blockvector *blockvector;
845 struct subfile *subfile;
846 struct context_stack *cstk;
847 struct subfile *nextsub;
849 /* Finish the lexical context of the last function in the file; pop
850 the context stack. */
852 if (context_stack_depth > 0)
854 cstk = pop_context ();
855 /* Make a block for the local symbols within. */
856 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
857 cstk->start_addr, end_addr, objfile);
859 if (context_stack_depth > 0)
861 /* This is said to happen with SCO. The old coffread.c
862 code simply emptied the context stack, so we do the
863 same. FIXME: Find out why it is happening. This is not
864 believed to happen in most cases (even for coffread.c);
865 it used to be an abort(). */
866 complaint (&symfile_complaints,
867 _("Context stack not empty in end_symtab"));
868 context_stack_depth = 0;
872 /* Reordered executables may have out of order pending blocks; if
873 OBJF_REORDERED is true, then sort the pending blocks. */
874 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
876 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
880 struct pending_block *pb, *pbnext;
888 /* swap blocks if unordered! */
890 if (BLOCK_START (pb->block) < BLOCK_START (pbnext->block))
892 struct block *tmp = pb->block;
893 pb->block = pbnext->block;
898 pbnext = pbnext->next;
904 /* Cleanup any undefined types that have been left hanging around
905 (this needs to be done before the finish_blocks so that
906 file_symbols is still good).
908 Both cleanup_undefined_types and finish_global_stabs are stabs
909 specific, but harmless for other symbol readers, since on gdb
910 startup or when finished reading stabs, the state is set so these
911 are no-ops. FIXME: Is this handled right in case of QUIT? Can
912 we make this cleaner? */
914 cleanup_undefined_types ();
915 finish_global_stabs (objfile);
917 if (pending_blocks == NULL
918 && file_symbols == NULL
919 && global_symbols == NULL
920 && have_line_numbers == 0
921 && pending_macros == NULL)
923 /* Ignore symtabs that have no functions with real debugging
929 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
931 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
933 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
935 blockvector = make_blockvector (objfile);
936 cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK),
937 &objfile->objfile_obstack);
940 /* Read the line table if it has to be read separately. */
941 if (objfile->sf->sym_read_linetable != NULL)
942 objfile->sf->sym_read_linetable ();
944 /* Now create the symtab objects proper, one for each subfile. */
945 /* (The main file is the last one on the chain.) */
947 for (subfile = subfiles; subfile; subfile = nextsub)
949 int linetablesize = 0;
952 /* If we have blocks of symbols, make a symtab. Otherwise, just
953 ignore this file and any line number info in it. */
956 if (subfile->line_vector)
958 linetablesize = sizeof (struct linetable) +
959 subfile->line_vector->nitems * sizeof (struct linetable_entry);
961 /* Like the pending blocks, the line table may be
962 scrambled in reordered executables. Sort it if
963 OBJF_REORDERED is true. */
964 if (objfile->flags & OBJF_REORDERED)
965 qsort (subfile->line_vector->item,
966 subfile->line_vector->nitems,
967 sizeof (struct linetable_entry), compare_line_numbers);
970 /* Now, allocate a symbol table. */
971 if (subfile->symtab == NULL)
972 symtab = allocate_symtab (subfile->name, objfile);
974 symtab = subfile->symtab;
976 /* Fill in its components. */
977 symtab->blockvector = blockvector;
978 symtab->macro_table = pending_macros;
979 if (subfile->line_vector)
981 /* Reallocate the line table on the symbol obstack */
982 symtab->linetable = (struct linetable *)
983 obstack_alloc (&objfile->objfile_obstack, linetablesize);
984 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
988 symtab->linetable = NULL;
990 symtab->block_line_section = section;
991 if (subfile->dirname)
993 /* Reallocate the dirname on the symbol obstack */
994 symtab->dirname = (char *)
995 obstack_alloc (&objfile->objfile_obstack,
996 strlen (subfile->dirname) + 1);
997 strcpy (symtab->dirname, subfile->dirname);
1001 symtab->dirname = NULL;
1003 symtab->free_code = free_linetable;
1004 symtab->free_func = NULL;
1006 /* Use whatever language we have been using for this
1007 subfile, not the one that was deduced in allocate_symtab
1008 from the filename. We already did our own deducing when
1009 we created the subfile, and we may have altered our
1010 opinion of what language it is from things we found in
1012 symtab->language = subfile->language;
1014 /* Save the debug format string (if any) in the symtab */
1015 if (subfile->debugformat != NULL)
1017 symtab->debugformat = obsavestring (subfile->debugformat,
1018 strlen (subfile->debugformat),
1019 &objfile->objfile_obstack);
1022 /* Similarly for the producer. */
1023 if (subfile->producer != NULL)
1024 symtab->producer = obsavestring (subfile->producer,
1025 strlen (subfile->producer),
1026 &objfile->objfile_obstack);
1028 /* All symtabs for the main file and the subfiles share a
1029 blockvector, so we need to clear primary for everything
1030 but the main file. */
1032 symtab->primary = 0;
1034 if (subfile->name != NULL)
1036 xfree ((void *) subfile->name);
1038 if (subfile->dirname != NULL)
1040 xfree ((void *) subfile->dirname);
1042 if (subfile->line_vector != NULL)
1044 xfree ((void *) subfile->line_vector);
1046 if (subfile->debugformat != NULL)
1048 xfree ((void *) subfile->debugformat);
1050 if (subfile->producer != NULL)
1051 xfree (subfile->producer);
1053 nextsub = subfile->next;
1054 xfree ((void *) subfile);
1057 /* Set this for the main source file. */
1060 symtab->primary = 1;
1063 /* Default any symbols without a specified symtab to the primary
1069 for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
1071 struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
1073 struct dict_iterator iter;
1075 for (sym = dict_iterator_first (BLOCK_DICT (block), &iter);
1077 sym = dict_iterator_next (&iter))
1078 if (SYMBOL_SYMTAB (sym) == NULL)
1079 SYMBOL_SYMTAB (sym) = symtab;
1083 last_source_file = NULL;
1084 current_subfile = NULL;
1085 pending_macros = NULL;
1090 /* Push a context block. Args are an identifying nesting level
1091 (checkable when you pop it), and the starting PC address of this
1094 struct context_stack *
1095 push_context (int desc, CORE_ADDR valu)
1097 struct context_stack *new;
1099 if (context_stack_depth == context_stack_size)
1101 context_stack_size *= 2;
1102 context_stack = (struct context_stack *)
1103 xrealloc ((char *) context_stack,
1104 (context_stack_size * sizeof (struct context_stack)));
1107 new = &context_stack[context_stack_depth++];
1109 new->locals = local_symbols;
1110 new->params = param_symbols;
1111 new->old_blocks = pending_blocks;
1112 new->start_addr = valu;
1115 local_symbols = NULL;
1116 param_symbols = NULL;
1121 /* Pop a context block. Returns the address of the context block just
1124 struct context_stack *
1127 gdb_assert (context_stack_depth > 0);
1128 return (&context_stack[--context_stack_depth]);
1133 /* Compute a small integer hash code for the given name. */
1136 hashname (char *name)
1138 return (hash(name,strlen(name)) % HASHSIZE);
1143 record_debugformat (char *format)
1145 current_subfile->debugformat = savestring (format, strlen (format));
1149 record_producer (const char *producer)
1151 /* The producer is not always provided in the debugging info.
1152 Do nothing if PRODUCER is NULL. */
1153 if (producer == NULL)
1156 current_subfile->producer = savestring (producer, strlen (producer));
1159 /* Merge the first symbol list SRCLIST into the second symbol list
1160 TARGETLIST by repeated calls to add_symbol_to_list(). This
1161 procedure "frees" each link of SRCLIST by adding it to the
1162 free_pendings list. Caller must set SRCLIST to a null list after
1163 calling this function.
1168 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1172 if (!srclist || !*srclist)
1175 /* Merge in elements from current link. */
1176 for (i = 0; i < (*srclist)->nsyms; i++)
1177 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1179 /* Recurse on next. */
1180 merge_symbol_lists (&(*srclist)->next, targetlist);
1182 /* "Free" the current link. */
1183 (*srclist)->next = free_pendings;
1184 free_pendings = (*srclist);
1187 /* Initialize anything that needs initializing when starting to read a
1188 fresh piece of a symbol file, e.g. reading in the stuff
1189 corresponding to a psymtab. */
1192 buildsym_init (void)
1194 free_pendings = NULL;
1195 file_symbols = NULL;
1196 global_symbols = NULL;
1197 pending_blocks = NULL;
1198 pending_macros = NULL;
1201 /* Initialize anything that needs initializing when a completely new
1202 symbol file is specified (not just adding some symbols from another
1203 file, e.g. a shared library). */
1206 buildsym_new_init (void)