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 /* Put the block in as the value of the symbol that names it. */
253 struct type
*ftype
= SYMBOL_TYPE (symbol
);
254 struct dict_iterator iter
;
255 SYMBOL_BLOCK_VALUE (symbol
) = block
;
256 BLOCK_FUNCTION (block
) = symbol
;
258 if (TYPE_NFIELDS (ftype
) <= 0)
260 /* No parameter type information is recorded with the
261 function's type. Set that from the type of the
262 parameter symbols. */
263 int nparams
= 0, iparams
;
265 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
267 switch (SYMBOL_CLASS (sym
))
272 case LOC_REGPARM_ADDR
:
273 case LOC_BASEREG_ARG
:
275 case LOC_COMPUTED_ARG
:
287 case LOC_CONST_BYTES
:
290 case LOC_OPTIMIZED_OUT
:
298 TYPE_NFIELDS (ftype
) = nparams
;
299 TYPE_FIELDS (ftype
) = (struct field
*)
300 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
303 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
305 if (iparams
== nparams
)
308 switch (SYMBOL_CLASS (sym
))
313 case LOC_REGPARM_ADDR
:
314 case LOC_BASEREG_ARG
:
316 case LOC_COMPUTED_ARG
:
317 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
318 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
330 case LOC_CONST_BYTES
:
333 case LOC_OPTIMIZED_OUT
:
342 /* If we're in the C++ case, set the block's scope. */
343 if (SYMBOL_LANGUAGE (symbol
) == language_cplus
)
345 cp_set_block_scope (symbol
, block
, &objfile
->objfile_obstack
);
350 BLOCK_FUNCTION (block
) = NULL
;
353 /* Now "free" the links of the list, and empty the list. */
355 for (next
= *listhead
; next
; next
= next1
)
358 next
->next
= free_pendings
;
359 free_pendings
= next
;
364 /* Check to be sure that the blocks have an end address that is
365 greater than starting address */
367 if (BLOCK_END (block
) < BLOCK_START (block
))
371 complaint (&symfile_complaints
,
372 _("block end address less than block start address in %s (patched it)"),
373 SYMBOL_PRINT_NAME (symbol
));
377 complaint (&symfile_complaints
,
378 _("block end address 0x%s less than block start address 0x%s (patched it)"),
379 paddr_nz (BLOCK_END (block
)), paddr_nz (BLOCK_START (block
)));
381 /* Better than nothing */
382 BLOCK_END (block
) = BLOCK_START (block
);
386 /* Install this block as the superblock of all blocks made since the
387 start of this scope that don't have superblocks yet. */
390 for (pblock
= pending_blocks
;
391 pblock
&& pblock
!= old_blocks
;
392 pblock
= pblock
->next
)
394 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
397 /* Check to be sure the blocks are nested as we receive
398 them. If the compiler/assembler/linker work, this just
399 burns a small amount of time.
401 Skip blocks which correspond to a function; they're not
402 physically nested inside this other blocks, only
404 if (BLOCK_FUNCTION (pblock
->block
) == NULL
405 && (BLOCK_START (pblock
->block
) < BLOCK_START (block
)
406 || BLOCK_END (pblock
->block
) > BLOCK_END (block
)))
410 complaint (&symfile_complaints
,
411 _("inner block not inside outer block in %s"),
412 SYMBOL_PRINT_NAME (symbol
));
416 complaint (&symfile_complaints
,
417 _("inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)"),
418 paddr_nz (BLOCK_START (pblock
->block
)),
419 paddr_nz (BLOCK_END (pblock
->block
)),
420 paddr_nz (BLOCK_START (block
)),
421 paddr_nz (BLOCK_END (block
)));
423 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
424 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
425 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
426 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
429 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
434 record_pending_block (objfile
, block
, opblock
);
438 /* Record BLOCK on the list of all blocks in the file. Put it after
439 OPBLOCK, or at the beginning if opblock is NULL. This puts the
440 block in the list after all its subblocks.
442 Allocate the pending block struct in the objfile_obstack to save
443 time. This wastes a little space. FIXME: Is it worth it? */
446 record_pending_block (struct objfile
*objfile
, struct block
*block
,
447 struct pending_block
*opblock
)
449 struct pending_block
*pblock
;
451 pblock
= (struct pending_block
*)
452 obstack_alloc (&objfile
->objfile_obstack
, sizeof (struct pending_block
));
453 pblock
->block
= block
;
456 pblock
->next
= opblock
->next
;
457 opblock
->next
= pblock
;
461 pblock
->next
= pending_blocks
;
462 pending_blocks
= pblock
;
466 static struct blockvector
*
467 make_blockvector (struct objfile
*objfile
)
469 struct pending_block
*next
;
470 struct blockvector
*blockvector
;
473 /* Count the length of the list of blocks. */
475 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
479 blockvector
= (struct blockvector
*)
480 obstack_alloc (&objfile
->objfile_obstack
,
481 (sizeof (struct blockvector
)
482 + (i
- 1) * sizeof (struct block
*)));
484 /* Copy the blocks into the blockvector. This is done in reverse
485 order, which happens to put the blocks into the proper order
486 (ascending starting address). finish_block has hair to insert
487 each block into the list after its subblocks in order to make
488 sure this is true. */
490 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
491 for (next
= pending_blocks
; next
; next
= next
->next
)
493 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
496 #if 0 /* Now we make the links in the
497 obstack, so don't free them. */
498 /* Now free the links of the list, and empty the list. */
500 for (next
= pending_blocks
; next
; next
= next1
)
506 pending_blocks
= NULL
;
508 #if 1 /* FIXME, shut this off after a while
509 to speed up symbol reading. */
510 /* Some compilers output blocks in the wrong order, but we depend on
511 their being in the right order so we can binary search. Check the
512 order and moan about it. FIXME. */
513 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
515 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
517 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
518 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
521 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
523 complaint (&symfile_complaints
, _("block at %s out of order"),
524 hex_string ((LONGEST
) start
));
530 return (blockvector
);
533 /* Start recording information about source code that came from an
534 included (or otherwise merged-in) source file with a different
535 name. NAME is the name of the file (cannot be NULL), DIRNAME is
536 the directory in which it resides (or NULL if not known). */
539 start_subfile (char *name
, char *dirname
)
541 struct subfile
*subfile
;
543 /* See if this subfile is already known as a subfile of the current
546 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
550 /* If NAME is an absolute path, and this subfile is not, then
551 attempt to create an absolute path to compare. */
552 if (IS_ABSOLUTE_PATH (name
)
553 && !IS_ABSOLUTE_PATH (subfile
->name
)
554 && subfile
->dirname
!= NULL
)
555 subfile_name
= concat (subfile
->dirname
, SLASH_STRING
,
556 subfile
->name
, NULL
);
558 subfile_name
= subfile
->name
;
560 if (FILENAME_CMP (subfile_name
, name
) == 0)
562 current_subfile
= subfile
;
563 if (subfile_name
!= subfile
->name
)
564 xfree (subfile_name
);
567 if (subfile_name
!= subfile
->name
)
568 xfree (subfile_name
);
571 /* This subfile is not known. Add an entry for it. Make an entry
572 for this subfile in the list of all subfiles of the current main
575 subfile
= (struct subfile
*) xmalloc (sizeof (struct subfile
));
576 memset ((char *) subfile
, 0, sizeof (struct subfile
));
577 subfile
->next
= subfiles
;
579 current_subfile
= subfile
;
581 /* Save its name and compilation directory name */
582 subfile
->name
= (name
== NULL
) ? NULL
: savestring (name
, strlen (name
));
584 (dirname
== NULL
) ? NULL
: savestring (dirname
, strlen (dirname
));
586 /* Initialize line-number recording for this subfile. */
587 subfile
->line_vector
= NULL
;
589 /* Default the source language to whatever can be deduced from the
590 filename. If nothing can be deduced (such as for a C/C++ include
591 file with a ".h" extension), then inherit whatever language the
592 previous subfile had. This kludgery is necessary because there
593 is no standard way in some object formats to record the source
594 language. Also, when symtabs are allocated we try to deduce a
595 language then as well, but it is too late for us to use that
596 information while reading symbols, since symtabs aren't allocated
597 until after all the symbols have been processed for a given
600 subfile
->language
= deduce_language_from_filename (subfile
->name
);
601 if (subfile
->language
== language_unknown
&&
602 subfile
->next
!= NULL
)
604 subfile
->language
= subfile
->next
->language
;
607 /* Initialize the debug format string to NULL. We may supply it
608 later via a call to record_debugformat. */
609 subfile
->debugformat
= NULL
;
611 /* Similarly for the producer. */
612 subfile
->producer
= NULL
;
614 /* If the filename of this subfile ends in .C, then change the
615 language of any pending subfiles from C to C++. We also accept
616 any other C++ suffixes accepted by deduce_language_from_filename. */
617 /* Likewise for f2c. */
622 enum language sublang
= deduce_language_from_filename (subfile
->name
);
624 if (sublang
== language_cplus
|| sublang
== language_fortran
)
625 for (s
= subfiles
; s
!= NULL
; s
= s
->next
)
626 if (s
->language
== language_c
)
627 s
->language
= sublang
;
630 /* And patch up this file if necessary. */
631 if (subfile
->language
== language_c
632 && subfile
->next
!= NULL
633 && (subfile
->next
->language
== language_cplus
634 || subfile
->next
->language
== language_fortran
))
636 subfile
->language
= subfile
->next
->language
;
640 /* For stabs readers, the first N_SO symbol is assumed to be the
641 source file name, and the subfile struct is initialized using that
642 assumption. If another N_SO symbol is later seen, immediately
643 following the first one, then the first one is assumed to be the
644 directory name and the second one is really the source file name.
646 So we have to patch up the subfile struct by moving the old name
647 value to dirname and remembering the new name. Some sanity
648 checking is performed to ensure that the state of the subfile
649 struct is reasonable and that the old name we are assuming to be a
650 directory name actually is (by checking for a trailing '/'). */
653 patch_subfile_names (struct subfile
*subfile
, char *name
)
655 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
656 && subfile
->name
[strlen (subfile
->name
) - 1] == '/')
658 subfile
->dirname
= subfile
->name
;
659 subfile
->name
= savestring (name
, strlen (name
));
660 last_source_file
= name
;
662 /* Default the source language to whatever can be deduced from
663 the filename. If nothing can be deduced (such as for a C/C++
664 include file with a ".h" extension), then inherit whatever
665 language the previous subfile had. This kludgery is
666 necessary because there is no standard way in some object
667 formats to record the source language. Also, when symtabs
668 are allocated we try to deduce a language then as well, but
669 it is too late for us to use that information while reading
670 symbols, since symtabs aren't allocated until after all the
671 symbols have been processed for a given source file. */
673 subfile
->language
= deduce_language_from_filename (subfile
->name
);
674 if (subfile
->language
== language_unknown
&&
675 subfile
->next
!= NULL
)
677 subfile
->language
= subfile
->next
->language
;
682 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
683 switching source files (different subfiles, as we call them) within
684 one object file, but using a stack rather than in an arbitrary
690 struct subfile_stack
*tem
691 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
693 tem
->next
= subfile_stack
;
695 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
697 internal_error (__FILE__
, __LINE__
, _("failed internal consistency check"));
699 tem
->name
= current_subfile
->name
;
706 struct subfile_stack
*link
= subfile_stack
;
710 internal_error (__FILE__
, __LINE__
, _("failed internal consistency check"));
713 subfile_stack
= link
->next
;
714 xfree ((void *) link
);
718 /* Add a linetable entry for line number LINE and address PC to the
719 line vector for SUBFILE. */
722 record_line (struct subfile
*subfile
, int line
, CORE_ADDR pc
)
724 struct linetable_entry
*e
;
725 /* Ignore the dummy line number in libg.o */
732 /* Make sure line vector exists and is big enough. */
733 if (!subfile
->line_vector
)
735 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
736 subfile
->line_vector
= (struct linetable
*)
737 xmalloc (sizeof (struct linetable
)
738 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
739 subfile
->line_vector
->nitems
= 0;
740 have_line_numbers
= 1;
743 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
745 subfile
->line_vector_length
*= 2;
746 subfile
->line_vector
= (struct linetable
*)
747 xrealloc ((char *) subfile
->line_vector
,
748 (sizeof (struct linetable
)
749 + (subfile
->line_vector_length
750 * sizeof (struct linetable_entry
))));
753 pc
= gdbarch_addr_bits_remove (current_gdbarch
, pc
);
755 /* Normally, we treat lines as unsorted. But the end of sequence
756 marker is special. We sort line markers at the same PC by line
757 number, so end of sequence markers (which have line == 0) appear
758 first. This is right if the marker ends the previous function,
759 and there is no padding before the next function. But it is
760 wrong if the previous line was empty and we are now marking a
761 switch to a different subfile. We must leave the end of sequence
762 marker at the end of this group of lines, not sort the empty line
763 to after the marker. The easiest way to accomplish this is to
764 delete any empty lines from our table, if they are followed by
765 end of sequence markers. All we lose is the ability to set
766 breakpoints at some lines which contain no instructions
768 if (line
== 0 && subfile
->line_vector
->nitems
> 0)
770 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
- 1;
771 while (subfile
->line_vector
->nitems
> 0 && e
->pc
== pc
)
774 subfile
->line_vector
->nitems
--;
778 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
783 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
786 compare_line_numbers (const void *ln1p
, const void *ln2p
)
788 struct linetable_entry
*ln1
= (struct linetable_entry
*) ln1p
;
789 struct linetable_entry
*ln2
= (struct linetable_entry
*) ln2p
;
791 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
792 Please keep it that way. */
793 if (ln1
->pc
< ln2
->pc
)
796 if (ln1
->pc
> ln2
->pc
)
799 /* If pc equal, sort by line. I'm not sure whether this is optimum
800 behavior (see comment at struct linetable in symtab.h). */
801 return ln1
->line
- ln2
->line
;
804 /* Start a new symtab for a new source file. Called, for example,
805 when a stabs symbol of type N_SO is seen, or when a DWARF
806 TAG_compile_unit DIE is seen. It indicates the start of data for
807 one original source file. */
810 start_symtab (char *name
, char *dirname
, CORE_ADDR start_addr
)
813 last_source_file
= name
;
814 last_source_start_addr
= start_addr
;
816 global_symbols
= NULL
;
818 have_line_numbers
= 0;
820 /* Context stack is initially empty. Allocate first one with room
821 for 10 levels; reuse it forever afterward. */
822 if (context_stack
== NULL
)
824 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
825 context_stack
= (struct context_stack
*)
826 xmalloc (context_stack_size
* sizeof (struct context_stack
));
828 context_stack_depth
= 0;
830 /* Set up support for C++ namespace support, in case we need it. */
832 cp_initialize_namespace ();
834 /* Initialize the list of sub source files with one entry for this
835 file (the top-level source file). */
838 current_subfile
= NULL
;
839 start_subfile (name
, dirname
);
842 /* Finish the symbol definitions for one main source file, close off
843 all the lexical contexts for that file (creating struct block's for
844 them), then make the struct symtab for that file and put it in the
847 END_ADDR is the address of the end of the file's text. SECTION is
848 the section number (in objfile->section_offsets) of the blockvector
851 Note that it is possible for end_symtab() to return NULL. In
852 particular, for the DWARF case at least, it will return NULL when
853 it finds a compilation unit that has exactly one DIE, a
854 TAG_compile_unit DIE. This can happen when we link in an object
855 file that was compiled from an empty source file. Returning NULL
856 is probably not the correct thing to do, because then gdb will
857 never know about this empty file (FIXME). */
860 end_symtab (CORE_ADDR end_addr
, struct objfile
*objfile
, int section
)
862 struct symtab
*symtab
= NULL
;
863 struct blockvector
*blockvector
;
864 struct subfile
*subfile
;
865 struct context_stack
*cstk
;
866 struct subfile
*nextsub
;
868 /* Finish the lexical context of the last function in the file; pop
869 the context stack. */
871 if (context_stack_depth
> 0)
873 cstk
= pop_context ();
874 /* Make a block for the local symbols within. */
875 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
876 cstk
->start_addr
, end_addr
, objfile
);
878 if (context_stack_depth
> 0)
880 /* This is said to happen with SCO. The old coffread.c
881 code simply emptied the context stack, so we do the
882 same. FIXME: Find out why it is happening. This is not
883 believed to happen in most cases (even for coffread.c);
884 it used to be an abort(). */
885 complaint (&symfile_complaints
,
886 _("Context stack not empty in end_symtab"));
887 context_stack_depth
= 0;
891 /* Reordered executables may have out of order pending blocks; if
892 OBJF_REORDERED is true, then sort the pending blocks. */
893 if ((objfile
->flags
& OBJF_REORDERED
) && pending_blocks
)
895 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
899 struct pending_block
*pb
, *pbnext
;
907 /* swap blocks if unordered! */
909 if (BLOCK_START (pb
->block
) < BLOCK_START (pbnext
->block
))
911 struct block
*tmp
= pb
->block
;
912 pb
->block
= pbnext
->block
;
917 pbnext
= pbnext
->next
;
923 /* Cleanup any undefined types that have been left hanging around
924 (this needs to be done before the finish_blocks so that
925 file_symbols is still good).
927 Both cleanup_undefined_types and finish_global_stabs are stabs
928 specific, but harmless for other symbol readers, since on gdb
929 startup or when finished reading stabs, the state is set so these
930 are no-ops. FIXME: Is this handled right in case of QUIT? Can
931 we make this cleaner? */
933 cleanup_undefined_types ();
934 finish_global_stabs (objfile
);
936 if (pending_blocks
== NULL
937 && file_symbols
== NULL
938 && global_symbols
== NULL
939 && have_line_numbers
== 0
940 && pending_macros
== NULL
)
942 /* Ignore symtabs that have no functions with real debugging
948 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
950 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
952 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
954 blockvector
= make_blockvector (objfile
);
955 cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector
, STATIC_BLOCK
),
956 &objfile
->objfile_obstack
);
959 /* Read the line table if it has to be read separately. */
960 if (objfile
->sf
->sym_read_linetable
!= NULL
)
961 objfile
->sf
->sym_read_linetable ();
963 /* Now create the symtab objects proper, one for each subfile. */
964 /* (The main file is the last one on the chain.) */
966 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
968 int linetablesize
= 0;
971 /* If we have blocks of symbols, make a symtab. Otherwise, just
972 ignore this file and any line number info in it. */
975 if (subfile
->line_vector
)
977 linetablesize
= sizeof (struct linetable
) +
978 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
980 /* I think this is artifact from before it went on the
981 obstack. I doubt we'll need the memory between now
982 and when we free it later in this function. */
983 /* First, shrink the linetable to make more memory. */
984 subfile
->line_vector
= (struct linetable
*)
985 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
988 /* Like the pending blocks, the line table may be
989 scrambled in reordered executables. Sort it if
990 OBJF_REORDERED is true. */
991 if (objfile
->flags
& OBJF_REORDERED
)
992 qsort (subfile
->line_vector
->item
,
993 subfile
->line_vector
->nitems
,
994 sizeof (struct linetable_entry
), compare_line_numbers
);
997 /* Now, allocate a symbol table. */
998 if (subfile
->symtab
== NULL
)
999 symtab
= allocate_symtab (subfile
->name
, objfile
);
1001 symtab
= subfile
->symtab
;
1003 /* Fill in its components. */
1004 symtab
->blockvector
= blockvector
;
1005 symtab
->macro_table
= pending_macros
;
1006 if (subfile
->line_vector
)
1008 /* Reallocate the line table on the symbol obstack */
1009 symtab
->linetable
= (struct linetable
*)
1010 obstack_alloc (&objfile
->objfile_obstack
, linetablesize
);
1011 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
1015 symtab
->linetable
= NULL
;
1017 symtab
->block_line_section
= section
;
1018 if (subfile
->dirname
)
1020 /* Reallocate the dirname on the symbol obstack */
1021 symtab
->dirname
= (char *)
1022 obstack_alloc (&objfile
->objfile_obstack
,
1023 strlen (subfile
->dirname
) + 1);
1024 strcpy (symtab
->dirname
, subfile
->dirname
);
1028 symtab
->dirname
= NULL
;
1030 symtab
->free_code
= free_linetable
;
1031 symtab
->free_func
= NULL
;
1033 /* Use whatever language we have been using for this
1034 subfile, not the one that was deduced in allocate_symtab
1035 from the filename. We already did our own deducing when
1036 we created the subfile, and we may have altered our
1037 opinion of what language it is from things we found in
1039 symtab
->language
= subfile
->language
;
1041 /* Save the debug format string (if any) in the symtab */
1042 if (subfile
->debugformat
!= NULL
)
1044 symtab
->debugformat
= obsavestring (subfile
->debugformat
,
1045 strlen (subfile
->debugformat
),
1046 &objfile
->objfile_obstack
);
1049 /* Similarly for the producer. */
1050 if (subfile
->producer
!= NULL
)
1051 symtab
->producer
= obsavestring (subfile
->producer
,
1052 strlen (subfile
->producer
),
1053 &objfile
->objfile_obstack
);
1055 /* All symtabs for the main file and the subfiles share a
1056 blockvector, so we need to clear primary for everything
1057 but the main file. */
1059 symtab
->primary
= 0;
1061 if (subfile
->name
!= NULL
)
1063 xfree ((void *) subfile
->name
);
1065 if (subfile
->dirname
!= NULL
)
1067 xfree ((void *) subfile
->dirname
);
1069 if (subfile
->line_vector
!= NULL
)
1071 xfree ((void *) subfile
->line_vector
);
1073 if (subfile
->debugformat
!= NULL
)
1075 xfree ((void *) subfile
->debugformat
);
1077 if (subfile
->producer
!= NULL
)
1078 xfree (subfile
->producer
);
1080 nextsub
= subfile
->next
;
1081 xfree ((void *) subfile
);
1084 /* Set this for the main source file. */
1087 symtab
->primary
= 1;
1090 /* Default any symbols without a specified symtab to the primary
1096 for (block_i
= 0; block_i
< BLOCKVECTOR_NBLOCKS (blockvector
); block_i
++)
1098 struct block
*block
= BLOCKVECTOR_BLOCK (blockvector
, block_i
);
1100 struct dict_iterator iter
;
1102 for (sym
= dict_iterator_first (BLOCK_DICT (block
), &iter
);
1104 sym
= dict_iterator_next (&iter
))
1105 if (SYMBOL_SYMTAB (sym
) == NULL
)
1106 SYMBOL_SYMTAB (sym
) = symtab
;
1110 last_source_file
= NULL
;
1111 current_subfile
= NULL
;
1112 pending_macros
= NULL
;
1117 /* Push a context block. Args are an identifying nesting level
1118 (checkable when you pop it), and the starting PC address of this
1121 struct context_stack
*
1122 push_context (int desc
, CORE_ADDR valu
)
1124 struct context_stack
*new;
1126 if (context_stack_depth
== context_stack_size
)
1128 context_stack_size
*= 2;
1129 context_stack
= (struct context_stack
*)
1130 xrealloc ((char *) context_stack
,
1131 (context_stack_size
* sizeof (struct context_stack
)));
1134 new = &context_stack
[context_stack_depth
++];
1136 new->locals
= local_symbols
;
1137 new->params
= param_symbols
;
1138 new->old_blocks
= pending_blocks
;
1139 new->start_addr
= valu
;
1142 local_symbols
= NULL
;
1143 param_symbols
= NULL
;
1148 /* Pop a context block. Returns the address of the context block just
1151 struct context_stack
*
1154 gdb_assert (context_stack_depth
> 0);
1155 return (&context_stack
[--context_stack_depth
]);
1160 /* Compute a small integer hash code for the given name. */
1163 hashname (char *name
)
1165 return (hash(name
,strlen(name
)) % HASHSIZE
);
1170 record_debugformat (char *format
)
1172 current_subfile
->debugformat
= savestring (format
, strlen (format
));
1176 record_producer (const char *producer
)
1178 /* The producer is not always provided in the debugging info.
1179 Do nothing if PRODUCER is NULL. */
1180 if (producer
== NULL
)
1183 current_subfile
->producer
= savestring (producer
, strlen (producer
));
1186 /* Merge the first symbol list SRCLIST into the second symbol list
1187 TARGETLIST by repeated calls to add_symbol_to_list(). This
1188 procedure "frees" each link of SRCLIST by adding it to the
1189 free_pendings list. Caller must set SRCLIST to a null list after
1190 calling this function.
1195 merge_symbol_lists (struct pending
**srclist
, struct pending
**targetlist
)
1199 if (!srclist
|| !*srclist
)
1202 /* Merge in elements from current link. */
1203 for (i
= 0; i
< (*srclist
)->nsyms
; i
++)
1204 add_symbol_to_list ((*srclist
)->symbol
[i
], targetlist
);
1206 /* Recurse on next. */
1207 merge_symbol_lists (&(*srclist
)->next
, targetlist
);
1209 /* "Free" the current link. */
1210 (*srclist
)->next
= free_pendings
;
1211 free_pendings
= (*srclist
);
1214 /* Initialize anything that needs initializing when starting to read a
1215 fresh piece of a symbol file, e.g. reading in the stuff
1216 corresponding to a psymtab. */
1219 buildsym_init (void)
1221 free_pendings
= NULL
;
1222 file_symbols
= NULL
;
1223 global_symbols
= NULL
;
1224 pending_blocks
= NULL
;
1225 pending_macros
= NULL
;
1228 /* Initialize anything that needs initializing when a completely new
1229 symbol file is specified (not just adding some symbols from another
1230 file, e.g. a shared library). */
1233 buildsym_new_init (void)