1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This module provides subroutines used for creating and adding to
24 the symbol table. These routines are called from various symbol-
25 file-reading routines.
27 Routines to support specific debugging information formats (stabs,
28 DWARF, etc) belong somewhere else. */
32 #include "gdb_obstack.h"
37 #include "gdb_assert.h"
38 #include "complaints.h"
39 #include "gdb_string.h"
40 #include "expression.h" /* For "enum exp_opcode" used by... */
41 #include "language.h" /* For "local_hex_string" */
43 #include "filenames.h" /* For DOSish file names */
45 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
47 #include "cp-support.h"
48 #include "dictionary.h"
50 /* Ask buildsym.h to define the vars it normally declares `extern'. */
53 #include "buildsym.h" /* Our own declarations */
56 /* For cleanup_undefined_types and finish_global_stabs (somewhat
57 questionable--see comment where we call them). */
59 #include "stabsread.h"
61 /* List of free `struct pending' structures for reuse. */
63 static struct pending *free_pendings;
65 /* Non-zero if symtab has line number info. This prevents an
66 otherwise empty symtab from being tossed. */
68 static int have_line_numbers;
70 static int compare_line_numbers (const void *ln1p, const void *ln2p);
73 /* Initial sizes of data structures. These are realloc'd larger if
74 needed, and realloc'd down to the size actually used, when
77 #define INITIAL_CONTEXT_STACK_SIZE 10
78 #define INITIAL_LINE_VECTOR_LENGTH 1000
81 /* maintain the lists of symbols and blocks */
83 /* Add a pending list to free_pendings. */
85 add_free_pendings (struct pending *list)
87 register struct pending *link = list;
91 while (link->next) link = link->next;
92 link->next = free_pendings;
97 /* Add a symbol to one of the lists of symbols. While we're at it, if
98 we're in the C++ case and don't have full namespace debugging info,
99 check to see if it references an anonymous namespace; if so, add an
100 appropriate using directive. */
103 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
105 register struct pending *link;
107 /* If this is an alias for another symbol, don't add it. */
108 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
111 /* We keep PENDINGSIZE symbols in each link of the list. If we
112 don't have a link with room in it, add a new link. */
113 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
117 link = free_pendings;
118 free_pendings = link->next;
122 link = (struct pending *) xmalloc (sizeof (struct pending));
125 link->next = *listhead;
130 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
132 /* Check to see if we might need to look for a mention of anonymous
135 if (SYMBOL_LANGUAGE (symbol) == language_cplus)
136 cp_scan_for_anonymous_namespaces (symbol);
139 /* Find a symbol named NAME on a LIST. NAME need not be
140 '\0'-terminated; LENGTH is the length of the name. */
143 find_symbol_in_list (struct pending *list, char *name, int length)
150 for (j = list->nsyms; --j >= 0;)
152 pp = DEPRECATED_SYMBOL_NAME (list->symbol[j]);
153 if (*pp == *name && strncmp (pp, name, length) == 0 &&
156 return (list->symbol[j]);
164 /* At end of reading syms, or in case of quit, really free as many
165 `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 #if 0 /* Now we make the links in the
206 symbol_obstack, so don't free
208 struct pending_block *bnext, *bnext1;
210 for (bnext = pending_blocks; bnext; bnext = bnext1)
212 bnext1 = bnext->next;
213 xfree ((void *) bnext);
216 pending_blocks = NULL;
219 /* Take one of the lists of symbols and make a block from it. Keep
220 the order the symbols have in the list (reversed from the input
221 file). Put the block on the list of pending blocks. */
224 finish_block (struct symbol *symbol, struct pending **listhead,
225 struct pending_block *old_blocks,
226 CORE_ADDR start, CORE_ADDR end,
227 struct objfile *objfile)
229 register struct pending *next, *next1;
230 register struct block *block;
231 register struct pending_block *pblock;
232 struct pending_block *opblock;
234 /* Initialize the block's dictionary. */
238 block = (struct block *)
239 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block));
240 BLOCK_DICT (block) = dict_create_linear (&objfile->symbol_obstack,
245 block = (struct block *)
246 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block));
247 BLOCK_DICT (block) = dict_create_hashed (&objfile->symbol_obstack,
251 BLOCK_START (block) = start;
252 BLOCK_END (block) = end;
253 /* Superblock filled in when containing block is made */
254 BLOCK_SUPERBLOCK (block) = NULL;
255 BLOCK_NAMESPACE (block) = NULL;
257 BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
259 /* Put the block in as the value of the symbol that names it. */
263 struct type *ftype = SYMBOL_TYPE (symbol);
264 struct dict_iterator iter;
265 SYMBOL_BLOCK_VALUE (symbol) = block;
266 BLOCK_FUNCTION (block) = symbol;
268 if (TYPE_NFIELDS (ftype) <= 0)
270 /* No parameter type information is recorded with the
271 function's type. Set that from the type of the
272 parameter symbols. */
273 int nparams = 0, iparams;
275 ALL_BLOCK_SYMBOLS (block, iter, sym)
277 switch (SYMBOL_CLASS (sym))
282 case LOC_REGPARM_ADDR:
283 case LOC_BASEREG_ARG:
285 case LOC_COMPUTED_ARG:
297 case LOC_CONST_BYTES:
300 case LOC_OPTIMIZED_OUT:
308 TYPE_NFIELDS (ftype) = nparams;
309 TYPE_FIELDS (ftype) = (struct field *)
310 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
313 ALL_BLOCK_SYMBOLS (block, iter, sym)
315 if (iparams == nparams)
318 switch (SYMBOL_CLASS (sym))
323 case LOC_REGPARM_ADDR:
324 case LOC_BASEREG_ARG:
326 case LOC_COMPUTED_ARG:
327 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
328 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
340 case LOC_CONST_BYTES:
343 case LOC_OPTIMIZED_OUT:
352 /* If we're in the C++ case, set the block's scope. */
353 if (SYMBOL_LANGUAGE (symbol) == language_cplus)
355 cp_set_block_scope (symbol, block, &objfile->symbol_obstack);
360 BLOCK_FUNCTION (block) = NULL;
363 /* Now "free" the links of the list, and empty the list. */
365 for (next = *listhead; next; next = next1)
368 next->next = free_pendings;
369 free_pendings = next;
374 /* Check to be sure that the blocks have an end address that is
375 greater than starting address */
377 if (BLOCK_END (block) < BLOCK_START (block))
381 complaint (&symfile_complaints,
382 "block end address less than block start address in %s (patched it)",
383 SYMBOL_PRINT_NAME (symbol));
387 complaint (&symfile_complaints,
388 "block end address 0x%s less than block start address 0x%s (patched it)",
389 paddr_nz (BLOCK_END (block)), paddr_nz (BLOCK_START (block)));
391 /* Better than nothing */
392 BLOCK_END (block) = BLOCK_START (block);
396 /* Install this block as the superblock of all blocks made since the
397 start of this scope that don't have superblocks yet. */
400 for (pblock = pending_blocks;
401 pblock && pblock != old_blocks;
402 pblock = pblock->next)
404 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
407 /* Check to be sure the blocks are nested as we receive
408 them. If the compiler/assembler/linker work, this just
409 burns a small amount of time. */
410 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
411 BLOCK_END (pblock->block) > BLOCK_END (block))
415 complaint (&symfile_complaints,
416 "inner block not inside outer block in %s",
417 SYMBOL_PRINT_NAME (symbol));
421 complaint (&symfile_complaints,
422 "inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)",
423 paddr_nz (BLOCK_START (pblock->block)),
424 paddr_nz (BLOCK_END (pblock->block)),
425 paddr_nz (BLOCK_START (block)),
426 paddr_nz (BLOCK_END (block)));
428 if (BLOCK_START (pblock->block) < BLOCK_START (block))
429 BLOCK_START (pblock->block) = BLOCK_START (block);
430 if (BLOCK_END (pblock->block) > BLOCK_END (block))
431 BLOCK_END (pblock->block) = BLOCK_END (block);
434 BLOCK_SUPERBLOCK (pblock->block) = block;
439 record_pending_block (objfile, block, opblock);
443 /* Record BLOCK on the list of all blocks in the file. Put it after
444 OPBLOCK, or at the beginning if opblock is NULL. This puts the
445 block in the list after all its subblocks.
447 Allocate the pending block struct in the symbol_obstack to save
448 time. This wastes a little space. FIXME: Is it worth it? */
451 record_pending_block (struct objfile *objfile, struct block *block,
452 struct pending_block *opblock)
454 register struct pending_block *pblock;
456 pblock = (struct pending_block *)
457 obstack_alloc (&objfile->symbol_obstack, sizeof (struct pending_block));
458 pblock->block = block;
461 pblock->next = opblock->next;
462 opblock->next = pblock;
466 pblock->next = pending_blocks;
467 pending_blocks = pblock;
471 static struct blockvector *
472 make_blockvector (struct objfile *objfile)
474 register struct pending_block *next;
475 register struct blockvector *blockvector;
478 /* Count the length of the list of blocks. */
480 for (next = pending_blocks, i = 0; next; next = next->next, i++)
484 blockvector = (struct blockvector *)
485 obstack_alloc (&objfile->symbol_obstack,
486 (sizeof (struct blockvector)
487 + (i - 1) * sizeof (struct block *)));
489 /* Copy the blocks into the blockvector. This is done in reverse
490 order, which happens to put the blocks into the proper order
491 (ascending starting address). finish_block has hair to insert
492 each block into the list after its subblocks in order to make
493 sure this is true. */
495 BLOCKVECTOR_NBLOCKS (blockvector) = i;
496 for (next = pending_blocks; next; next = next->next)
498 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
501 #if 0 /* Now we make the links in the
502 obstack, so don't free them. */
503 /* Now free the links of the list, and empty the list. */
505 for (next = pending_blocks; next; next = next1)
511 pending_blocks = NULL;
513 #if 1 /* FIXME, shut this off after a while
514 to speed up symbol reading. */
515 /* Some compilers output blocks in the wrong order, but we depend on
516 their being in the right order so we can binary search. Check the
517 order and moan about it. FIXME. */
518 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
520 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
522 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
523 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
526 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
528 complaint (&symfile_complaints, "block at %s out of order",
529 local_hex_string ((LONGEST) start));
535 return (blockvector);
538 /* Start recording information about source code that came from an
539 included (or otherwise merged-in) source file with a different
540 name. NAME is the name of the file (cannot be NULL), DIRNAME is
541 the directory in which it resides (or NULL if not known). */
544 start_subfile (char *name, char *dirname)
546 register struct subfile *subfile;
548 /* See if this subfile is already known as a subfile of the current
551 for (subfile = subfiles; subfile; subfile = subfile->next)
553 if (FILENAME_CMP (subfile->name, name) == 0)
555 current_subfile = subfile;
560 /* This subfile is not known. Add an entry for it. Make an entry
561 for this subfile in the list of all subfiles of the current main
564 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
565 memset ((char *) subfile, 0, sizeof (struct subfile));
566 subfile->next = subfiles;
568 current_subfile = subfile;
570 /* Save its name and compilation directory name */
571 subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
573 (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
575 /* Initialize line-number recording for this subfile. */
576 subfile->line_vector = NULL;
578 /* Default the source language to whatever can be deduced from the
579 filename. If nothing can be deduced (such as for a C/C++ include
580 file with a ".h" extension), then inherit whatever language the
581 previous subfile had. This kludgery is necessary because there
582 is no standard way in some object formats to record the source
583 language. Also, when symtabs are allocated we try to deduce a
584 language then as well, but it is too late for us to use that
585 information while reading symbols, since symtabs aren't allocated
586 until after all the symbols have been processed for a given
589 subfile->language = deduce_language_from_filename (subfile->name);
590 if (subfile->language == language_unknown &&
591 subfile->next != NULL)
593 subfile->language = subfile->next->language;
596 /* Initialize the debug format string to NULL. We may supply it
597 later via a call to record_debugformat. */
598 subfile->debugformat = NULL;
600 #if 0 /* OBSOLETE CFront */
601 // OBSOLETE /* cfront output is a C program, so in most ways it looks like a C
602 // OBSOLETE program. But to demangle we need to set the language to C++. We
603 // OBSOLETE can distinguish cfront code by the fact that it has #line
604 // OBSOLETE directives which specify a file name ending in .C. */
605 #endif /* OBSOLETE CFront */
607 /* If the filename of this subfile ends in .C, then change the
608 language of any pending subfiles from C to C++. We also accept
609 any other C++ suffixes accepted by deduce_language_from_filename. */
610 /* OBSOLETE (in particular, some people use .cxx with cfront). */
611 /* Likewise for f2c. */
616 enum language sublang = deduce_language_from_filename (subfile->name);
618 if (sublang == language_cplus || sublang == language_fortran)
619 for (s = subfiles; s != NULL; s = s->next)
620 if (s->language == language_c)
621 s->language = sublang;
624 /* And patch up this file if necessary. */
625 if (subfile->language == language_c
626 && subfile->next != NULL
627 && (subfile->next->language == language_cplus
628 || subfile->next->language == language_fortran))
630 subfile->language = subfile->next->language;
634 /* For stabs readers, the first N_SO symbol is assumed to be the
635 source file name, and the subfile struct is initialized using that
636 assumption. If another N_SO symbol is later seen, immediately
637 following the first one, then the first one is assumed to be the
638 directory name and the second one is really the source file name.
640 So we have to patch up the subfile struct by moving the old name
641 value to dirname and remembering the new name. Some sanity
642 checking is performed to ensure that the state of the subfile
643 struct is reasonable and that the old name we are assuming to be a
644 directory name actually is (by checking for a trailing '/'). */
647 patch_subfile_names (struct subfile *subfile, char *name)
649 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
650 && subfile->name[strlen (subfile->name) - 1] == '/')
652 subfile->dirname = subfile->name;
653 subfile->name = savestring (name, strlen (name));
654 last_source_file = name;
656 /* Default the source language to whatever can be deduced from
657 the filename. If nothing can be deduced (such as for a C/C++
658 include file with a ".h" extension), then inherit whatever
659 language the previous subfile had. This kludgery is
660 necessary because there is no standard way in some object
661 formats to record the source language. Also, when symtabs
662 are allocated we try to deduce a language then as well, but
663 it is too late for us to use that information while reading
664 symbols, since symtabs aren't allocated until after all the
665 symbols have been processed for a given source file. */
667 subfile->language = deduce_language_from_filename (subfile->name);
668 if (subfile->language == language_unknown &&
669 subfile->next != NULL)
671 subfile->language = subfile->next->language;
676 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
677 switching source files (different subfiles, as we call them) within
678 one object file, but using a stack rather than in an arbitrary
684 register struct subfile_stack *tem
685 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
687 tem->next = subfile_stack;
689 if (current_subfile == NULL || current_subfile->name == NULL)
691 internal_error (__FILE__, __LINE__, "failed internal consistency check");
693 tem->name = current_subfile->name;
700 register struct subfile_stack *link = subfile_stack;
704 internal_error (__FILE__, __LINE__, "failed internal consistency check");
707 subfile_stack = link->next;
708 xfree ((void *) link);
712 /* Add a linetable entry for line number LINE and address PC to the
713 line vector for SUBFILE. */
716 record_line (register struct subfile *subfile, int line, CORE_ADDR pc)
718 struct linetable_entry *e;
719 /* Ignore the dummy line number in libg.o */
726 /* Make sure line vector exists and is big enough. */
727 if (!subfile->line_vector)
729 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
730 subfile->line_vector = (struct linetable *)
731 xmalloc (sizeof (struct linetable)
732 + subfile->line_vector_length * sizeof (struct linetable_entry));
733 subfile->line_vector->nitems = 0;
734 have_line_numbers = 1;
737 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
739 subfile->line_vector_length *= 2;
740 subfile->line_vector = (struct linetable *)
741 xrealloc ((char *) subfile->line_vector,
742 (sizeof (struct linetable)
743 + (subfile->line_vector_length
744 * sizeof (struct linetable_entry))));
747 e = subfile->line_vector->item + subfile->line_vector->nitems++;
749 e->pc = ADDR_BITS_REMOVE(pc);
752 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
755 compare_line_numbers (const void *ln1p, const void *ln2p)
757 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
758 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
760 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
761 Please keep it that way. */
762 if (ln1->pc < ln2->pc)
765 if (ln1->pc > ln2->pc)
768 /* If pc equal, sort by line. I'm not sure whether this is optimum
769 behavior (see comment at struct linetable in symtab.h). */
770 return ln1->line - ln2->line;
773 /* Start a new symtab for a new source file. Called, for example,
774 when a stabs symbol of type N_SO is seen, or when a DWARF
775 TAG_compile_unit DIE is seen. It indicates the start of data for
776 one original source file. */
779 start_symtab (char *name, char *dirname, CORE_ADDR start_addr)
782 last_source_file = name;
783 last_source_start_addr = start_addr;
785 global_symbols = NULL;
787 have_line_numbers = 0;
789 /* Context stack is initially empty. Allocate first one with room
790 for 10 levels; reuse it forever afterward. */
791 if (context_stack == NULL)
793 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
794 context_stack = (struct context_stack *)
795 xmalloc (context_stack_size * sizeof (struct context_stack));
797 context_stack_depth = 0;
799 /* Set up support for C++ namespace support, in case we need it. */
801 cp_initialize_namespace ();
803 /* Initialize the list of sub source files with one entry for this
804 file (the top-level source file). */
807 current_subfile = NULL;
808 start_subfile (name, dirname);
811 /* Finish the symbol definitions for one main source file, close off
812 all the lexical contexts for that file (creating struct block's for
813 them), then make the struct symtab for that file and put it in the
816 END_ADDR is the address of the end of the file's text. SECTION is
817 the section number (in objfile->section_offsets) of the blockvector
820 Note that it is possible for end_symtab() to return NULL. In
821 particular, for the DWARF case at least, it will return NULL when
822 it finds a compilation unit that has exactly one DIE, a
823 TAG_compile_unit DIE. This can happen when we link in an object
824 file that was compiled from an empty source file. Returning NULL
825 is probably not the correct thing to do, because then gdb will
826 never know about this empty file (FIXME). */
829 end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
831 register struct symtab *symtab = NULL;
832 register struct blockvector *blockvector;
833 register struct subfile *subfile;
834 register struct context_stack *cstk;
835 struct subfile *nextsub;
837 /* Finish the lexical context of the last function in the file; pop
838 the context stack. */
840 if (context_stack_depth > 0)
842 cstk = pop_context ();
843 /* Make a block for the local symbols within. */
844 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
845 cstk->start_addr, end_addr, objfile);
847 if (context_stack_depth > 0)
849 /* This is said to happen with SCO. The old coffread.c
850 code simply emptied the context stack, so we do the
851 same. FIXME: Find out why it is happening. This is not
852 believed to happen in most cases (even for coffread.c);
853 it used to be an abort(). */
854 complaint (&symfile_complaints,
855 "Context stack not empty in end_symtab");
856 context_stack_depth = 0;
860 /* Reordered executables may have out of order pending blocks; if
861 OBJF_REORDERED is true, then sort the pending blocks. */
862 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
864 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
868 struct pending_block *pb, *pbnext;
876 /* swap blocks if unordered! */
878 if (BLOCK_START (pb->block) < BLOCK_START (pbnext->block))
880 struct block *tmp = pb->block;
881 pb->block = pbnext->block;
886 pbnext = pbnext->next;
892 /* Cleanup any undefined types that have been left hanging around
893 (this needs to be done before the finish_blocks so that
894 file_symbols is still good).
896 Both cleanup_undefined_types and finish_global_stabs are stabs
897 specific, but harmless for other symbol readers, since on gdb
898 startup or when finished reading stabs, the state is set so these
899 are no-ops. FIXME: Is this handled right in case of QUIT? Can
900 we make this cleaner? */
902 cleanup_undefined_types ();
903 finish_global_stabs (objfile);
905 if (pending_blocks == NULL
906 && file_symbols == NULL
907 && global_symbols == NULL
908 && have_line_numbers == 0
909 && pending_macros == NULL)
911 /* Ignore symtabs that have no functions with real debugging
917 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
919 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
921 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
923 blockvector = make_blockvector (objfile);
924 cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK),
925 &objfile->symbol_obstack);
928 #ifndef PROCESS_LINENUMBER_HOOK
929 #define PROCESS_LINENUMBER_HOOK()
931 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
933 /* Now create the symtab objects proper, one for each subfile. */
934 /* (The main file is the last one on the chain.) */
936 for (subfile = subfiles; subfile; subfile = nextsub)
938 int linetablesize = 0;
941 /* If we have blocks of symbols, make a symtab. Otherwise, just
942 ignore this file and any line number info in it. */
945 if (subfile->line_vector)
947 linetablesize = sizeof (struct linetable) +
948 subfile->line_vector->nitems * sizeof (struct linetable_entry);
950 /* I think this is artifact from before it went on the
951 obstack. I doubt we'll need the memory between now
952 and when we free it later in this function. */
953 /* First, shrink the linetable to make more memory. */
954 subfile->line_vector = (struct linetable *)
955 xrealloc ((char *) subfile->line_vector, linetablesize);
958 /* Like the pending blocks, the line table may be
959 scrambled in reordered executables. Sort it if
960 OBJF_REORDERED is true. */
961 if (objfile->flags & OBJF_REORDERED)
962 qsort (subfile->line_vector->item,
963 subfile->line_vector->nitems,
964 sizeof (struct linetable_entry), compare_line_numbers);
967 /* Now, allocate a symbol table. */
968 symtab = allocate_symtab (subfile->name, objfile);
970 /* Fill in its components. */
971 symtab->blockvector = blockvector;
972 symtab->macro_table = pending_macros;
973 if (subfile->line_vector)
975 /* Reallocate the line table on the symbol obstack */
976 symtab->linetable = (struct linetable *)
977 obstack_alloc (&objfile->symbol_obstack, linetablesize);
978 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
982 symtab->linetable = NULL;
984 symtab->block_line_section = section;
985 if (subfile->dirname)
987 /* Reallocate the dirname on the symbol obstack */
988 symtab->dirname = (char *)
989 obstack_alloc (&objfile->symbol_obstack,
990 strlen (subfile->dirname) + 1);
991 strcpy (symtab->dirname, subfile->dirname);
995 symtab->dirname = NULL;
997 symtab->free_code = free_linetable;
998 symtab->free_func = NULL;
1000 /* Use whatever language we have been using for this
1001 subfile, not the one that was deduced in allocate_symtab
1002 from the filename. We already did our own deducing when
1003 we created the subfile, and we may have altered our
1004 opinion of what language it is from things we found in
1006 symtab->language = subfile->language;
1008 /* Save the debug format string (if any) in the symtab */
1009 if (subfile->debugformat != NULL)
1011 symtab->debugformat = obsavestring (subfile->debugformat,
1012 strlen (subfile->debugformat),
1013 &objfile->symbol_obstack);
1016 /* All symtabs for the main file and the subfiles share a
1017 blockvector, so we need to clear primary for everything
1018 but the main file. */
1020 symtab->primary = 0;
1022 if (subfile->name != NULL)
1024 xfree ((void *) subfile->name);
1026 if (subfile->dirname != NULL)
1028 xfree ((void *) subfile->dirname);
1030 if (subfile->line_vector != NULL)
1032 xfree ((void *) subfile->line_vector);
1034 if (subfile->debugformat != NULL)
1036 xfree ((void *) subfile->debugformat);
1039 nextsub = subfile->next;
1040 xfree ((void *) subfile);
1043 /* Set this for the main source file. */
1046 symtab->primary = 1;
1049 last_source_file = NULL;
1050 current_subfile = NULL;
1051 pending_macros = NULL;
1056 /* Push a context block. Args are an identifying nesting level
1057 (checkable when you pop it), and the starting PC address of this
1060 struct context_stack *
1061 push_context (int desc, CORE_ADDR valu)
1063 register struct context_stack *new;
1065 if (context_stack_depth == context_stack_size)
1067 context_stack_size *= 2;
1068 context_stack = (struct context_stack *)
1069 xrealloc ((char *) context_stack,
1070 (context_stack_size * sizeof (struct context_stack)));
1073 new = &context_stack[context_stack_depth++];
1075 new->locals = local_symbols;
1076 new->params = param_symbols;
1077 new->old_blocks = pending_blocks;
1078 new->start_addr = valu;
1081 local_symbols = NULL;
1082 param_symbols = NULL;
1087 /* Pop a context block. Returns the address of the context block just
1090 struct context_stack *
1093 gdb_assert (context_stack_depth > 0);
1094 return (&context_stack[--context_stack_depth]);
1099 /* Compute a small integer hash code for the given name. */
1102 hashname (char *name)
1104 return (hash(name,strlen(name)) % HASHSIZE);
1109 record_debugformat (char *format)
1111 current_subfile->debugformat = savestring (format, strlen (format));
1114 /* Merge the first symbol list SRCLIST into the second symbol list
1115 TARGETLIST by repeated calls to add_symbol_to_list(). This
1116 procedure "frees" each link of SRCLIST by adding it to the
1117 free_pendings list. Caller must set SRCLIST to a null list after
1118 calling this function.
1123 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1127 if (!srclist || !*srclist)
1130 /* Merge in elements from current link. */
1131 for (i = 0; i < (*srclist)->nsyms; i++)
1132 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1134 /* Recurse on next. */
1135 merge_symbol_lists (&(*srclist)->next, targetlist);
1137 /* "Free" the current link. */
1138 (*srclist)->next = free_pendings;
1139 free_pendings = (*srclist);
1142 /* Initialize anything that needs initializing when starting to read a
1143 fresh piece of a symbol file, e.g. reading in the stuff
1144 corresponding to a psymtab. */
1147 buildsym_init (void)
1149 free_pendings = NULL;
1150 file_symbols = NULL;
1151 global_symbols = NULL;
1152 pending_blocks = NULL;
1153 pending_macros = NULL;
1156 /* Initialize anything that needs initializing when a completely new
1157 symbol file is specified (not just adding some symbols from another
1158 file, e.g. a shared library). */
1161 buildsym_new_init (void)