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 free `struct pending' structures for reuse. */
60 static struct pending
*free_pendings
;
62 /* Non-zero if symtab has line number info. This prevents an
63 otherwise empty symtab from being tossed. */
65 static int have_line_numbers
;
67 static int compare_line_numbers (const void *ln1p
, const void *ln2p
);
70 /* Initial sizes of data structures. These are realloc'd larger if
71 needed, and realloc'd down to the size actually used, when
74 #define INITIAL_CONTEXT_STACK_SIZE 10
75 #define INITIAL_LINE_VECTOR_LENGTH 1000
78 /* maintain the lists of symbols and blocks */
80 /* Add a pending list to free_pendings. */
82 add_free_pendings (struct pending
*list
)
84 struct pending
*link
= list
;
88 while (link
->next
) link
= link
->next
;
89 link
->next
= free_pendings
;
94 /* Add a symbol to one of the lists of symbols. While we're at it, if
95 we're in the C++ case and don't have full namespace debugging info,
96 check to see if it references an anonymous namespace; if so, add an
97 appropriate using directive. */
100 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
102 struct pending
*link
;
104 /* If this is an alias for another symbol, don't add it. */
105 if (symbol
->ginfo
.name
&& symbol
->ginfo
.name
[0] == '#')
108 /* We keep PENDINGSIZE symbols in each link of the list. If we
109 don't have a link with room in it, add a new link. */
110 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
114 link
= free_pendings
;
115 free_pendings
= link
->next
;
119 link
= (struct pending
*) xmalloc (sizeof (struct pending
));
122 link
->next
= *listhead
;
127 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
129 /* Check to see if we might need to look for a mention of anonymous
132 if (SYMBOL_LANGUAGE (symbol
) == language_cplus
)
133 cp_scan_for_anonymous_namespaces (symbol
);
136 /* Find a symbol named NAME on a LIST. NAME need not be
137 '\0'-terminated; LENGTH is the length of the name. */
140 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
147 for (j
= list
->nsyms
; --j
>= 0;)
149 pp
= DEPRECATED_SYMBOL_NAME (list
->symbol
[j
]);
150 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0 &&
153 return (list
->symbol
[j
]);
161 /* At end of reading syms, or in case of quit, really free as many
162 `struct pending's as we can easily find. */
165 really_free_pendings (void *dummy
)
167 struct pending
*next
, *next1
;
169 for (next
= free_pendings
; next
; next
= next1
)
172 xfree ((void *) next
);
174 free_pendings
= NULL
;
176 free_pending_blocks ();
178 for (next
= file_symbols
; next
!= NULL
; next
= next1
)
181 xfree ((void *) next
);
185 for (next
= global_symbols
; next
!= NULL
; next
= next1
)
188 xfree ((void *) next
);
190 global_symbols
= NULL
;
193 free_macro_table (pending_macros
);
196 /* This function is called to discard any pending blocks. */
199 free_pending_blocks (void)
201 #if 0 /* Now we make the links in the
202 objfile_obstack, so don't free
204 struct pending_block
*bnext
, *bnext1
;
206 for (bnext
= pending_blocks
; bnext
; bnext
= bnext1
)
208 bnext1
= bnext
->next
;
209 xfree ((void *) bnext
);
212 pending_blocks
= NULL
;
215 /* Take one of the lists of symbols and make a block from it. Keep
216 the order the symbols have in the list (reversed from the input
217 file). Put the block on the list of pending blocks. */
220 finish_block (struct symbol
*symbol
, struct pending
**listhead
,
221 struct pending_block
*old_blocks
,
222 CORE_ADDR start
, CORE_ADDR end
,
223 struct objfile
*objfile
)
225 struct pending
*next
, *next1
;
227 struct pending_block
*pblock
;
228 struct pending_block
*opblock
;
230 block
= allocate_block (&objfile
->objfile_obstack
);
234 BLOCK_DICT (block
) = dict_create_linear (&objfile
->objfile_obstack
,
239 BLOCK_DICT (block
) = dict_create_hashed (&objfile
->objfile_obstack
,
243 BLOCK_START (block
) = start
;
244 BLOCK_END (block
) = end
;
245 /* Superblock filled in when containing block is made */
246 BLOCK_SUPERBLOCK (block
) = NULL
;
247 BLOCK_NAMESPACE (block
) = NULL
;
249 BLOCK_GCC_COMPILED (block
) = processing_gcc_compilation
;
251 /* Put the block in as the value of the symbol that names it. */
255 struct type
*ftype
= SYMBOL_TYPE (symbol
);
256 struct dict_iterator iter
;
257 SYMBOL_BLOCK_VALUE (symbol
) = block
;
258 BLOCK_FUNCTION (block
) = symbol
;
260 if (TYPE_NFIELDS (ftype
) <= 0)
262 /* No parameter type information is recorded with the
263 function's type. Set that from the type of the
264 parameter symbols. */
265 int nparams
= 0, iparams
;
267 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
269 switch (SYMBOL_CLASS (sym
))
274 case LOC_REGPARM_ADDR
:
275 case LOC_BASEREG_ARG
:
277 case LOC_COMPUTED_ARG
:
289 case LOC_CONST_BYTES
:
292 case LOC_OPTIMIZED_OUT
:
300 TYPE_NFIELDS (ftype
) = nparams
;
301 TYPE_FIELDS (ftype
) = (struct field
*)
302 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
305 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
307 if (iparams
== nparams
)
310 switch (SYMBOL_CLASS (sym
))
315 case LOC_REGPARM_ADDR
:
316 case LOC_BASEREG_ARG
:
318 case LOC_COMPUTED_ARG
:
319 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
320 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
332 case LOC_CONST_BYTES
:
335 case LOC_OPTIMIZED_OUT
:
344 /* If we're in the C++ case, set the block's scope. */
345 if (SYMBOL_LANGUAGE (symbol
) == language_cplus
)
347 cp_set_block_scope (symbol
, block
, &objfile
->objfile_obstack
);
352 BLOCK_FUNCTION (block
) = NULL
;
355 /* Now "free" the links of the list, and empty the list. */
357 for (next
= *listhead
; next
; next
= next1
)
360 next
->next
= free_pendings
;
361 free_pendings
= next
;
366 /* Check to be sure that the blocks have an end address that is
367 greater than starting address */
369 if (BLOCK_END (block
) < BLOCK_START (block
))
373 complaint (&symfile_complaints
,
374 _("block end address less than block start address in %s (patched it)"),
375 SYMBOL_PRINT_NAME (symbol
));
379 complaint (&symfile_complaints
,
380 _("block end address 0x%s less than block start address 0x%s (patched it)"),
381 paddr_nz (BLOCK_END (block
)), paddr_nz (BLOCK_START (block
)));
383 /* Better than nothing */
384 BLOCK_END (block
) = BLOCK_START (block
);
388 /* Install this block as the superblock of all blocks made since the
389 start of this scope that don't have superblocks yet. */
392 for (pblock
= pending_blocks
;
393 pblock
&& pblock
!= old_blocks
;
394 pblock
= pblock
->next
)
396 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
399 /* Check to be sure the blocks are nested as we receive
400 them. If the compiler/assembler/linker work, this just
401 burns a small amount of time.
403 Skip blocks which correspond to a function; they're not
404 physically nested inside this other blocks, only
406 if (BLOCK_FUNCTION (pblock
->block
) == NULL
407 && (BLOCK_START (pblock
->block
) < BLOCK_START (block
)
408 || BLOCK_END (pblock
->block
) > BLOCK_END (block
)))
412 complaint (&symfile_complaints
,
413 _("inner block not inside outer block in %s"),
414 SYMBOL_PRINT_NAME (symbol
));
418 complaint (&symfile_complaints
,
419 _("inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)"),
420 paddr_nz (BLOCK_START (pblock
->block
)),
421 paddr_nz (BLOCK_END (pblock
->block
)),
422 paddr_nz (BLOCK_START (block
)),
423 paddr_nz (BLOCK_END (block
)));
425 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
426 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
427 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
428 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
431 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
436 record_pending_block (objfile
, block
, opblock
);
440 /* Record BLOCK on the list of all blocks in the file. Put it after
441 OPBLOCK, or at the beginning if opblock is NULL. This puts the
442 block in the list after all its subblocks.
444 Allocate the pending block struct in the objfile_obstack to save
445 time. This wastes a little space. FIXME: Is it worth it? */
448 record_pending_block (struct objfile
*objfile
, struct block
*block
,
449 struct pending_block
*opblock
)
451 struct pending_block
*pblock
;
453 pblock
= (struct pending_block
*)
454 obstack_alloc (&objfile
->objfile_obstack
, sizeof (struct pending_block
));
455 pblock
->block
= block
;
458 pblock
->next
= opblock
->next
;
459 opblock
->next
= pblock
;
463 pblock
->next
= pending_blocks
;
464 pending_blocks
= pblock
;
468 static struct blockvector
*
469 make_blockvector (struct objfile
*objfile
)
471 struct pending_block
*next
;
472 struct blockvector
*blockvector
;
475 /* Count the length of the list of blocks. */
477 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
481 blockvector
= (struct blockvector
*)
482 obstack_alloc (&objfile
->objfile_obstack
,
483 (sizeof (struct blockvector
)
484 + (i
- 1) * sizeof (struct block
*)));
486 /* Copy the blocks into the blockvector. This is done in reverse
487 order, which happens to put the blocks into the proper order
488 (ascending starting address). finish_block has hair to insert
489 each block into the list after its subblocks in order to make
490 sure this is true. */
492 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
493 for (next
= pending_blocks
; next
; next
= next
->next
)
495 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
498 #if 0 /* Now we make the links in the
499 obstack, so don't free them. */
500 /* Now free the links of the list, and empty the list. */
502 for (next
= pending_blocks
; next
; next
= next1
)
508 pending_blocks
= NULL
;
510 #if 1 /* FIXME, shut this off after a while
511 to speed up symbol reading. */
512 /* Some compilers output blocks in the wrong order, but we depend on
513 their being in the right order so we can binary search. Check the
514 order and moan about it. FIXME. */
515 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
517 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
519 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
520 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
523 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
525 complaint (&symfile_complaints
, _("block at %s out of order"),
526 hex_string ((LONGEST
) start
));
532 return (blockvector
);
535 /* Start recording information about source code that came from an
536 included (or otherwise merged-in) source file with a different
537 name. NAME is the name of the file (cannot be NULL), DIRNAME is
538 the directory in which it resides (or NULL if not known). */
541 start_subfile (char *name
, char *dirname
)
543 struct subfile
*subfile
;
545 /* See if this subfile is already known as a subfile of the current
548 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
552 /* If NAME is an absolute path, and this subfile is not, then
553 attempt to create an absolute path to compare. */
554 if (IS_ABSOLUTE_PATH (name
)
555 && !IS_ABSOLUTE_PATH (subfile
->name
)
556 && subfile
->dirname
!= NULL
)
557 subfile_name
= concat (subfile
->dirname
, SLASH_STRING
,
558 subfile
->name
, NULL
);
560 subfile_name
= subfile
->name
;
562 if (FILENAME_CMP (subfile_name
, name
) == 0)
564 current_subfile
= subfile
;
565 if (subfile_name
!= subfile
->name
)
566 xfree (subfile_name
);
569 if (subfile_name
!= subfile
->name
)
570 xfree (subfile_name
);
573 /* This subfile is not known. Add an entry for it. Make an entry
574 for this subfile in the list of all subfiles of the current main
577 subfile
= (struct subfile
*) xmalloc (sizeof (struct subfile
));
578 memset ((char *) subfile
, 0, sizeof (struct subfile
));
579 subfile
->next
= subfiles
;
581 current_subfile
= subfile
;
583 /* Save its name and compilation directory name */
584 subfile
->name
= (name
== NULL
) ? NULL
: savestring (name
, strlen (name
));
586 (dirname
== NULL
) ? NULL
: savestring (dirname
, strlen (dirname
));
588 /* Initialize line-number recording for this subfile. */
589 subfile
->line_vector
= NULL
;
591 /* Default the source language to whatever can be deduced from the
592 filename. If nothing can be deduced (such as for a C/C++ include
593 file with a ".h" extension), then inherit whatever language the
594 previous subfile had. This kludgery is necessary because there
595 is no standard way in some object formats to record the source
596 language. Also, when symtabs are allocated we try to deduce a
597 language then as well, but it is too late for us to use that
598 information while reading symbols, since symtabs aren't allocated
599 until after all the symbols have been processed for a given
602 subfile
->language
= deduce_language_from_filename (subfile
->name
);
603 if (subfile
->language
== language_unknown
&&
604 subfile
->next
!= NULL
)
606 subfile
->language
= subfile
->next
->language
;
609 /* Initialize the debug format string to NULL. We may supply it
610 later via a call to record_debugformat. */
611 subfile
->debugformat
= NULL
;
613 /* Similarly for the producer. */
614 subfile
->producer
= NULL
;
616 /* If the filename of this subfile ends in .C, then change the
617 language of any pending subfiles from C to C++. We also accept
618 any other C++ suffixes accepted by deduce_language_from_filename. */
619 /* Likewise for f2c. */
624 enum language sublang
= deduce_language_from_filename (subfile
->name
);
626 if (sublang
== language_cplus
|| sublang
== language_fortran
)
627 for (s
= subfiles
; s
!= NULL
; s
= s
->next
)
628 if (s
->language
== language_c
)
629 s
->language
= sublang
;
632 /* And patch up this file if necessary. */
633 if (subfile
->language
== language_c
634 && subfile
->next
!= NULL
635 && (subfile
->next
->language
== language_cplus
636 || subfile
->next
->language
== language_fortran
))
638 subfile
->language
= subfile
->next
->language
;
642 /* For stabs readers, the first N_SO symbol is assumed to be the
643 source file name, and the subfile struct is initialized using that
644 assumption. If another N_SO symbol is later seen, immediately
645 following the first one, then the first one is assumed to be the
646 directory name and the second one is really the source file name.
648 So we have to patch up the subfile struct by moving the old name
649 value to dirname and remembering the new name. Some sanity
650 checking is performed to ensure that the state of the subfile
651 struct is reasonable and that the old name we are assuming to be a
652 directory name actually is (by checking for a trailing '/'). */
655 patch_subfile_names (struct subfile
*subfile
, char *name
)
657 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
658 && subfile
->name
[strlen (subfile
->name
) - 1] == '/')
660 subfile
->dirname
= subfile
->name
;
661 subfile
->name
= savestring (name
, strlen (name
));
662 last_source_file
= name
;
664 /* Default the source language to whatever can be deduced from
665 the filename. If nothing can be deduced (such as for a C/C++
666 include file with a ".h" extension), then inherit whatever
667 language the previous subfile had. This kludgery is
668 necessary because there is no standard way in some object
669 formats to record the source language. Also, when symtabs
670 are allocated we try to deduce a language then as well, but
671 it is too late for us to use that information while reading
672 symbols, since symtabs aren't allocated until after all the
673 symbols have been processed for a given source file. */
675 subfile
->language
= deduce_language_from_filename (subfile
->name
);
676 if (subfile
->language
== language_unknown
&&
677 subfile
->next
!= NULL
)
679 subfile
->language
= subfile
->next
->language
;
684 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
685 switching source files (different subfiles, as we call them) within
686 one object file, but using a stack rather than in an arbitrary
692 struct subfile_stack
*tem
693 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
695 tem
->next
= subfile_stack
;
697 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
699 internal_error (__FILE__
, __LINE__
, _("failed internal consistency check"));
701 tem
->name
= current_subfile
->name
;
708 struct subfile_stack
*link
= subfile_stack
;
712 internal_error (__FILE__
, __LINE__
, _("failed internal consistency check"));
715 subfile_stack
= link
->next
;
716 xfree ((void *) link
);
720 /* Add a linetable entry for line number LINE and address PC to the
721 line vector for SUBFILE. */
724 record_line (struct subfile
*subfile
, int line
, CORE_ADDR pc
)
726 struct linetable_entry
*e
;
727 /* Ignore the dummy line number in libg.o */
734 /* Make sure line vector exists and is big enough. */
735 if (!subfile
->line_vector
)
737 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
738 subfile
->line_vector
= (struct linetable
*)
739 xmalloc (sizeof (struct linetable
)
740 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
741 subfile
->line_vector
->nitems
= 0;
742 have_line_numbers
= 1;
745 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
747 subfile
->line_vector_length
*= 2;
748 subfile
->line_vector
= (struct linetable
*)
749 xrealloc ((char *) subfile
->line_vector
,
750 (sizeof (struct linetable
)
751 + (subfile
->line_vector_length
752 * sizeof (struct linetable_entry
))));
755 pc
= gdbarch_addr_bits_remove (current_gdbarch
, pc
);
757 /* Normally, we treat lines as unsorted. But the end of sequence
758 marker is special. We sort line markers at the same PC by line
759 number, so end of sequence markers (which have line == 0) appear
760 first. This is right if the marker ends the previous function,
761 and there is no padding before the next function. But it is
762 wrong if the previous line was empty and we are now marking a
763 switch to a different subfile. We must leave the end of sequence
764 marker at the end of this group of lines, not sort the empty line
765 to after the marker. The easiest way to accomplish this is to
766 delete any empty lines from our table, if they are followed by
767 end of sequence markers. All we lose is the ability to set
768 breakpoints at some lines which contain no instructions
770 if (line
== 0 && subfile
->line_vector
->nitems
> 0)
772 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
- 1;
773 while (subfile
->line_vector
->nitems
> 0 && e
->pc
== pc
)
776 subfile
->line_vector
->nitems
--;
780 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
785 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
788 compare_line_numbers (const void *ln1p
, const void *ln2p
)
790 struct linetable_entry
*ln1
= (struct linetable_entry
*) ln1p
;
791 struct linetable_entry
*ln2
= (struct linetable_entry
*) ln2p
;
793 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
794 Please keep it that way. */
795 if (ln1
->pc
< ln2
->pc
)
798 if (ln1
->pc
> ln2
->pc
)
801 /* If pc equal, sort by line. I'm not sure whether this is optimum
802 behavior (see comment at struct linetable in symtab.h). */
803 return ln1
->line
- ln2
->line
;
806 /* Start a new symtab for a new source file. Called, for example,
807 when a stabs symbol of type N_SO is seen, or when a DWARF
808 TAG_compile_unit DIE is seen. It indicates the start of data for
809 one original source file. */
812 start_symtab (char *name
, char *dirname
, CORE_ADDR start_addr
)
815 last_source_file
= name
;
816 last_source_start_addr
= start_addr
;
818 global_symbols
= NULL
;
820 have_line_numbers
= 0;
822 /* Context stack is initially empty. Allocate first one with room
823 for 10 levels; reuse it forever afterward. */
824 if (context_stack
== NULL
)
826 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
827 context_stack
= (struct context_stack
*)
828 xmalloc (context_stack_size
* sizeof (struct context_stack
));
830 context_stack_depth
= 0;
832 /* Set up support for C++ namespace support, in case we need it. */
834 cp_initialize_namespace ();
836 /* Initialize the list of sub source files with one entry for this
837 file (the top-level source file). */
840 current_subfile
= NULL
;
841 start_subfile (name
, dirname
);
844 /* Finish the symbol definitions for one main source file, close off
845 all the lexical contexts for that file (creating struct block's for
846 them), then make the struct symtab for that file and put it in the
849 END_ADDR is the address of the end of the file's text. SECTION is
850 the section number (in objfile->section_offsets) of the blockvector
853 Note that it is possible for end_symtab() to return NULL. In
854 particular, for the DWARF case at least, it will return NULL when
855 it finds a compilation unit that has exactly one DIE, a
856 TAG_compile_unit DIE. This can happen when we link in an object
857 file that was compiled from an empty source file. Returning NULL
858 is probably not the correct thing to do, because then gdb will
859 never know about this empty file (FIXME). */
862 end_symtab (CORE_ADDR end_addr
, struct objfile
*objfile
, int section
)
864 struct symtab
*symtab
= NULL
;
865 struct blockvector
*blockvector
;
866 struct subfile
*subfile
;
867 struct context_stack
*cstk
;
868 struct subfile
*nextsub
;
870 /* Finish the lexical context of the last function in the file; pop
871 the context stack. */
873 if (context_stack_depth
> 0)
875 cstk
= pop_context ();
876 /* Make a block for the local symbols within. */
877 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
878 cstk
->start_addr
, end_addr
, objfile
);
880 if (context_stack_depth
> 0)
882 /* This is said to happen with SCO. The old coffread.c
883 code simply emptied the context stack, so we do the
884 same. FIXME: Find out why it is happening. This is not
885 believed to happen in most cases (even for coffread.c);
886 it used to be an abort(). */
887 complaint (&symfile_complaints
,
888 _("Context stack not empty in end_symtab"));
889 context_stack_depth
= 0;
893 /* Reordered executables may have out of order pending blocks; if
894 OBJF_REORDERED is true, then sort the pending blocks. */
895 if ((objfile
->flags
& OBJF_REORDERED
) && pending_blocks
)
897 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
901 struct pending_block
*pb
, *pbnext
;
909 /* swap blocks if unordered! */
911 if (BLOCK_START (pb
->block
) < BLOCK_START (pbnext
->block
))
913 struct block
*tmp
= pb
->block
;
914 pb
->block
= pbnext
->block
;
919 pbnext
= pbnext
->next
;
925 /* Cleanup any undefined types that have been left hanging around
926 (this needs to be done before the finish_blocks so that
927 file_symbols is still good).
929 Both cleanup_undefined_types and finish_global_stabs are stabs
930 specific, but harmless for other symbol readers, since on gdb
931 startup or when finished reading stabs, the state is set so these
932 are no-ops. FIXME: Is this handled right in case of QUIT? Can
933 we make this cleaner? */
935 cleanup_undefined_types ();
936 finish_global_stabs (objfile
);
938 if (pending_blocks
== NULL
939 && file_symbols
== NULL
940 && global_symbols
== NULL
941 && have_line_numbers
== 0
942 && pending_macros
== NULL
)
944 /* Ignore symtabs that have no functions with real debugging
950 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
952 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
954 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
956 blockvector
= make_blockvector (objfile
);
957 cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
),
958 &objfile
->objfile_obstack
);
961 /* Read the line table if it has to be read separately. */
962 if (objfile
->sf
->sym_read_linetable
!= NULL
)
963 objfile
->sf
->sym_read_linetable ();
965 /* Now create the symtab objects proper, one for each subfile. */
966 /* (The main file is the last one on the chain.) */
968 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
970 int linetablesize
= 0;
973 /* If we have blocks of symbols, make a symtab. Otherwise, just
974 ignore this file and any line number info in it. */
977 if (subfile
->line_vector
)
979 linetablesize
= sizeof (struct linetable
) +
980 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
982 /* I think this is artifact from before it went on the
983 obstack. I doubt we'll need the memory between now
984 and when we free it later in this function. */
985 /* First, shrink the linetable to make more memory. */
986 subfile
->line_vector
= (struct linetable
*)
987 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
990 /* Like the pending blocks, the line table may be
991 scrambled in reordered executables. Sort it if
992 OBJF_REORDERED is true. */
993 if (objfile
->flags
& OBJF_REORDERED
)
994 qsort (subfile
->line_vector
->item
,
995 subfile
->line_vector
->nitems
,
996 sizeof (struct linetable_entry
), compare_line_numbers
);
999 /* Now, allocate a symbol table. */
1000 if (subfile
->symtab
== NULL
)
1001 symtab
= allocate_symtab (subfile
->name
, objfile
);
1003 symtab
= subfile
->symtab
;
1005 /* Fill in its components. */
1006 symtab
->blockvector
= blockvector
;
1007 symtab
->macro_table
= pending_macros
;
1008 if (subfile
->line_vector
)
1010 /* Reallocate the line table on the symbol obstack */
1011 symtab
->linetable
= (struct linetable
*)
1012 obstack_alloc (&objfile
->objfile_obstack
, linetablesize
);
1013 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
1017 symtab
->linetable
= NULL
;
1019 symtab
->block_line_section
= section
;
1020 if (subfile
->dirname
)
1022 /* Reallocate the dirname on the symbol obstack */
1023 symtab
->dirname
= (char *)
1024 obstack_alloc (&objfile
->objfile_obstack
,
1025 strlen (subfile
->dirname
) + 1);
1026 strcpy (symtab
->dirname
, subfile
->dirname
);
1030 symtab
->dirname
= NULL
;
1032 symtab
->free_code
= free_linetable
;
1033 symtab
->free_func
= NULL
;
1035 /* Use whatever language we have been using for this
1036 subfile, not the one that was deduced in allocate_symtab
1037 from the filename. We already did our own deducing when
1038 we created the subfile, and we may have altered our
1039 opinion of what language it is from things we found in
1041 symtab
->language
= subfile
->language
;
1043 /* Save the debug format string (if any) in the symtab */
1044 if (subfile
->debugformat
!= NULL
)
1046 symtab
->debugformat
= obsavestring (subfile
->debugformat
,
1047 strlen (subfile
->debugformat
),
1048 &objfile
->objfile_obstack
);
1051 /* Similarly for the producer. */
1052 if (subfile
->producer
!= NULL
)
1053 symtab
->producer
= obsavestring (subfile
->producer
,
1054 strlen (subfile
->producer
),
1055 &objfile
->objfile_obstack
);
1057 /* All symtabs for the main file and the subfiles share a
1058 blockvector, so we need to clear primary for everything
1059 but the main file. */
1061 symtab
->primary
= 0;
1063 if (subfile
->name
!= NULL
)
1065 xfree ((void *) subfile
->name
);
1067 if (subfile
->dirname
!= NULL
)
1069 xfree ((void *) subfile
->dirname
);
1071 if (subfile
->line_vector
!= NULL
)
1073 xfree ((void *) subfile
->line_vector
);
1075 if (subfile
->debugformat
!= NULL
)
1077 xfree ((void *) subfile
->debugformat
);
1079 if (subfile
->producer
!= NULL
)
1080 xfree (subfile
->producer
);
1082 nextsub
= subfile
->next
;
1083 xfree ((void *) subfile
);
1086 /* Set this for the main source file. */
1089 symtab
->primary
= 1;
1092 /* Default any symbols without a specified symtab to the primary
1098 for (block_i
= 0; block_i
< BLOCKVECTOR_NBLOCKS (blockvector
); block_i
++)
1100 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, block_i
);
1102 struct dict_iterator iter
;
1104 for (sym
= dict_iterator_first (BLOCK_DICT (block
), &iter
);
1106 sym
= dict_iterator_next (&iter
))
1107 if (SYMBOL_SYMTAB (sym
) == NULL
)
1108 SYMBOL_SYMTAB (sym
) = symtab
;
1112 last_source_file
= NULL
;
1113 current_subfile
= NULL
;
1114 pending_macros
= NULL
;
1119 /* Push a context block. Args are an identifying nesting level
1120 (checkable when you pop it), and the starting PC address of this
1123 struct context_stack
*
1124 push_context (int desc
, CORE_ADDR valu
)
1126 struct context_stack
*new;
1128 if (context_stack_depth
== context_stack_size
)
1130 context_stack_size
*= 2;
1131 context_stack
= (struct context_stack
*)
1132 xrealloc ((char *) context_stack
,
1133 (context_stack_size
* sizeof (struct context_stack
)));
1136 new = &context_stack
[context_stack_depth
++];
1138 new->locals
= local_symbols
;
1139 new->params
= param_symbols
;
1140 new->old_blocks
= pending_blocks
;
1141 new->start_addr
= valu
;
1144 local_symbols
= NULL
;
1145 param_symbols
= NULL
;
1150 /* Pop a context block. Returns the address of the context block just
1153 struct context_stack
*
1156 gdb_assert (context_stack_depth
> 0);
1157 return (&context_stack
[--context_stack_depth
]);
1162 /* Compute a small integer hash code for the given name. */
1165 hashname (char *name
)
1167 return (hash(name
,strlen(name
)) % HASHSIZE
);
1172 record_debugformat (char *format
)
1174 current_subfile
->debugformat
= savestring (format
, strlen (format
));
1178 record_producer (const char *producer
)
1180 /* The producer is not always provided in the debugging info.
1181 Do nothing if PRODUCER is NULL. */
1182 if (producer
== NULL
)
1185 current_subfile
->producer
= savestring (producer
, strlen (producer
));
1188 /* Merge the first symbol list SRCLIST into the second symbol list
1189 TARGETLIST by repeated calls to add_symbol_to_list(). This
1190 procedure "frees" each link of SRCLIST by adding it to the
1191 free_pendings list. Caller must set SRCLIST to a null list after
1192 calling this function.
1197 merge_symbol_lists (struct pending
**srclist
, struct pending
**targetlist
)
1201 if (!srclist
|| !*srclist
)
1204 /* Merge in elements from current link. */
1205 for (i
= 0; i
< (*srclist
)->nsyms
; i
++)
1206 add_symbol_to_list ((*srclist
)->symbol
[i
], targetlist
);
1208 /* Recurse on next. */
1209 merge_symbol_lists (&(*srclist
)->next
, targetlist
);
1211 /* "Free" the current link. */
1212 (*srclist
)->next
= free_pendings
;
1213 free_pendings
= (*srclist
);
1216 /* Initialize anything that needs initializing when starting to read a
1217 fresh piece of a symbol file, e.g. reading in the stuff
1218 corresponding to a psymtab. */
1221 buildsym_init (void)
1223 free_pendings
= NULL
;
1224 file_symbols
= NULL
;
1225 global_symbols
= NULL
;
1226 pending_blocks
= NULL
;
1227 pending_macros
= NULL
;
1230 /* Initialize anything that needs initializing when a completely new
1231 symbol file is specified (not just adding some symbols from another
1232 file, e.g. a shared library). */
1235 buildsym_new_init (void)