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. */
46 /* Ask buildsym.h to define the vars it normally declares `extern'. */
49 #include "buildsym.h" /* Our own declarations */
52 /* For cleanup_undefined_types and finish_global_stabs (somewhat
53 questionable--see comment where we call them). */
55 #include "stabsread.h"
57 /* List of free `struct pending' structures for reuse. */
59 static struct pending
*free_pendings
;
61 /* Non-zero if symtab has line number info. This prevents an
62 otherwise empty symtab from being tossed. */
64 static int have_line_numbers
;
66 static int compare_line_numbers (const void *ln1p
, const void *ln2p
);
69 /* Initial sizes of data structures. These are realloc'd larger if
70 needed, and realloc'd down to the size actually used, when
73 #define INITIAL_CONTEXT_STACK_SIZE 10
74 #define INITIAL_LINE_VECTOR_LENGTH 1000
77 /* maintain the lists of symbols and blocks */
79 /* Add a pending list to free_pendings. */
81 add_free_pendings (struct pending
*list
)
83 register struct pending
*link
= list
;
87 while (link
->next
) link
= link
->next
;
88 link
->next
= free_pendings
;
93 /* Add a symbol to one of the lists of symbols. */
96 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
98 register struct pending
*link
;
100 /* If this is an alias for another symbol, don't add it. */
101 if (symbol
->ginfo
.name
&& symbol
->ginfo
.name
[0] == '#')
104 /* We keep PENDINGSIZE symbols in each link of the list. If we
105 don't have a link with room in it, add a new link. */
106 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
110 link
= free_pendings
;
111 free_pendings
= link
->next
;
115 link
= (struct pending
*) xmalloc (sizeof (struct pending
));
118 link
->next
= *listhead
;
123 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
126 /* Find a symbol named NAME on a LIST. NAME need not be
127 '\0'-terminated; LENGTH is the length of the name. */
130 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
137 for (j
= list
->nsyms
; --j
>= 0;)
139 pp
= SYMBOL_NAME (list
->symbol
[j
]);
140 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0 &&
143 return (list
->symbol
[j
]);
151 /* At end of reading syms, or in case of quit, really free as many
152 `struct pending's as we can easily find. */
156 really_free_pendings (void *dummy
)
158 struct pending
*next
, *next1
;
160 for (next
= free_pendings
; next
; next
= next1
)
163 xfree ((void *) next
);
165 free_pendings
= NULL
;
167 free_pending_blocks ();
169 for (next
= file_symbols
; next
!= NULL
; next
= next1
)
172 xfree ((void *) next
);
176 for (next
= global_symbols
; next
!= NULL
; next
= next1
)
179 xfree ((void *) next
);
181 global_symbols
= NULL
;
184 free_macro_table (pending_macros
);
187 /* This function is called to discard any pending blocks. */
190 free_pending_blocks (void)
192 #if 0 /* Now we make the links in the
193 symbol_obstack, so don't free
195 struct pending_block
*bnext
, *bnext1
;
197 for (bnext
= pending_blocks
; bnext
; bnext
= bnext1
)
199 bnext1
= bnext
->next
;
200 xfree ((void *) bnext
);
203 pending_blocks
= NULL
;
206 /* Take one of the lists of symbols and make a block from it. Keep
207 the order the symbols have in the list (reversed from the input
208 file). Put the block on the list of pending blocks. */
211 finish_block (struct symbol
*symbol
, struct pending
**listhead
,
212 struct pending_block
*old_blocks
,
213 CORE_ADDR start
, CORE_ADDR end
,
214 struct objfile
*objfile
)
216 register struct pending
*next
, *next1
;
217 register struct block
*block
;
218 register struct pending_block
*pblock
;
219 struct pending_block
*opblock
;
223 /* Count the length of the list of symbols. */
225 for (next
= *listhead
, i
= 0;
227 i
+= next
->nsyms
, next
= next
->next
)
232 /* Copy the symbols into the block. */
236 block
= (struct block
*)
237 obstack_alloc (&objfile
->symbol_obstack
,
238 (sizeof (struct block
) +
239 ((i
- 1) * sizeof (struct symbol
*))));
240 BLOCK_NSYMS (block
) = i
;
241 for (next
= *listhead
; next
; next
= next
->next
)
242 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
244 BLOCK_SYM (block
, --i
) = next
->symbol
[j
];
249 int htab_size
= BLOCK_HASHTABLE_SIZE (i
);
251 block
= (struct block
*)
252 obstack_alloc (&objfile
->symbol_obstack
,
253 (sizeof (struct block
) +
254 ((htab_size
- 1) * sizeof (struct symbol
*))));
255 for (j
= 0; j
< htab_size
; j
++)
257 BLOCK_BUCKET (block
, j
) = 0;
259 BLOCK_BUCKETS (block
) = htab_size
;
260 for (next
= *listhead
; next
; next
= next
->next
)
262 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
265 unsigned int hash_index
;
266 const char *name
= SYMBOL_DEMANGLED_NAME (next
->symbol
[j
]);
268 name
= SYMBOL_NAME (next
->symbol
[j
]);
269 hash_index
= msymbol_hash_iw (name
);
270 hash_index
= hash_index
% BLOCK_BUCKETS (block
);
271 sym
= BLOCK_BUCKET (block
, hash_index
);
272 BLOCK_BUCKET (block
, hash_index
) = next
->symbol
[j
];
273 next
->symbol
[j
]->hash_next
= sym
;
278 BLOCK_START (block
) = start
;
279 BLOCK_END (block
) = end
;
280 /* Superblock filled in when containing block is made */
281 BLOCK_SUPERBLOCK (block
) = NULL
;
283 BLOCK_GCC_COMPILED (block
) = processing_gcc_compilation
;
285 /* Put the block in as the value of the symbol that names it. */
289 struct type
*ftype
= SYMBOL_TYPE (symbol
);
290 SYMBOL_BLOCK_VALUE (symbol
) = block
;
291 BLOCK_FUNCTION (block
) = symbol
;
292 BLOCK_HASHTABLE (block
) = 0;
294 if (TYPE_NFIELDS (ftype
) <= 0)
296 /* No parameter type information is recorded with the
297 function's type. Set that from the type of the
298 parameter symbols. */
299 int nparams
= 0, iparams
;
301 ALL_BLOCK_SYMBOLS (block
, i
, sym
)
303 switch (SYMBOL_CLASS (sym
))
308 case LOC_REGPARM_ADDR
:
309 case LOC_BASEREG_ARG
:
322 case LOC_CONST_BYTES
:
325 case LOC_OPTIMIZED_OUT
:
332 TYPE_NFIELDS (ftype
) = nparams
;
333 TYPE_FIELDS (ftype
) = (struct field
*)
334 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
336 for (i
= iparams
= 0; iparams
< nparams
; i
++)
338 sym
= BLOCK_SYM (block
, i
);
339 switch (SYMBOL_CLASS (sym
))
344 case LOC_REGPARM_ADDR
:
345 case LOC_BASEREG_ARG
:
347 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
348 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
360 case LOC_CONST_BYTES
:
363 case LOC_OPTIMIZED_OUT
:
373 BLOCK_FUNCTION (block
) = NULL
;
374 BLOCK_HASHTABLE (block
) = 1;
377 /* Now "free" the links of the list, and empty the list. */
379 for (next
= *listhead
; next
; next
= next1
)
382 next
->next
= free_pendings
;
383 free_pendings
= next
;
388 /* Check to be sure that the blocks have an end address that is
389 greater than starting address */
391 if (BLOCK_END (block
) < BLOCK_START (block
))
395 complaint (&symfile_complaints
,
396 "block end address less than block start address in %s (patched it)",
397 SYMBOL_SOURCE_NAME (symbol
));
401 complaint (&symfile_complaints
,
402 "block end address 0x%s less than block start address 0x%s (patched it)",
403 paddr_nz (BLOCK_END (block
)), paddr_nz (BLOCK_START (block
)));
405 /* Better than nothing */
406 BLOCK_END (block
) = BLOCK_START (block
);
410 /* Install this block as the superblock of all blocks made since the
411 start of this scope that don't have superblocks yet. */
414 for (pblock
= pending_blocks
;
415 pblock
&& pblock
!= old_blocks
;
416 pblock
= pblock
->next
)
418 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
421 /* Check to be sure the blocks are nested as we receive
422 them. If the compiler/assembler/linker work, this just
423 burns a small amount of time. */
424 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
) ||
425 BLOCK_END (pblock
->block
) > BLOCK_END (block
))
429 complaint (&symfile_complaints
,
430 "inner block not inside outer block in %s",
431 SYMBOL_SOURCE_NAME (symbol
));
435 complaint (&symfile_complaints
,
436 "inner block (0x%s-0x%s) not inside outer block (0x%s-0x%s)",
437 paddr_nz (BLOCK_START (pblock
->block
)),
438 paddr_nz (BLOCK_END (pblock
->block
)),
439 paddr_nz (BLOCK_START (block
)),
440 paddr_nz (BLOCK_END (block
)));
442 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
443 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
444 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
445 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
448 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
453 record_pending_block (objfile
, block
, opblock
);
456 /* Record BLOCK on the list of all blocks in the file. Put it after
457 OPBLOCK, or at the beginning if opblock is NULL. This puts the
458 block in the list after all its subblocks.
460 Allocate the pending block struct in the symbol_obstack to save
461 time. This wastes a little space. FIXME: Is it worth it? */
464 record_pending_block (struct objfile
*objfile
, struct block
*block
,
465 struct pending_block
*opblock
)
467 register struct pending_block
*pblock
;
469 pblock
= (struct pending_block
*)
470 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct pending_block
));
471 pblock
->block
= block
;
474 pblock
->next
= opblock
->next
;
475 opblock
->next
= pblock
;
479 pblock
->next
= pending_blocks
;
480 pending_blocks
= pblock
;
484 static struct blockvector
*
485 make_blockvector (struct objfile
*objfile
)
487 register struct pending_block
*next
;
488 register struct blockvector
*blockvector
;
491 /* Count the length of the list of blocks. */
493 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
497 blockvector
= (struct blockvector
*)
498 obstack_alloc (&objfile
->symbol_obstack
,
499 (sizeof (struct blockvector
)
500 + (i
- 1) * sizeof (struct block
*)));
502 /* Copy the blocks into the blockvector. This is done in reverse
503 order, which happens to put the blocks into the proper order
504 (ascending starting address). finish_block has hair to insert
505 each block into the list after its subblocks in order to make
506 sure this is true. */
508 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
509 for (next
= pending_blocks
; next
; next
= next
->next
)
511 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
514 #if 0 /* Now we make the links in the
515 obstack, so don't free them. */
516 /* Now free the links of the list, and empty the list. */
518 for (next
= pending_blocks
; next
; next
= next1
)
524 pending_blocks
= NULL
;
526 #if 1 /* FIXME, shut this off after a while
527 to speed up symbol reading. */
528 /* Some compilers output blocks in the wrong order, but we depend on
529 their being in the right order so we can binary search. Check the
530 order and moan about it. FIXME. */
531 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
533 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
535 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
536 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
539 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
541 complaint (&symfile_complaints
, "block at %s out of order",
542 local_hex_string ((LONGEST
) start
));
548 return (blockvector
);
551 /* Start recording information about source code that came from an
552 included (or otherwise merged-in) source file with a different
553 name. NAME is the name of the file (cannot be NULL), DIRNAME is
554 the directory in which it resides (or NULL if not known). */
557 start_subfile (char *name
, char *dirname
)
559 register struct subfile
*subfile
;
561 /* See if this subfile is already known as a subfile of the current
564 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
566 if (FILENAME_CMP (subfile
->name
, name
) == 0)
568 current_subfile
= subfile
;
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 #if 0 /* OBSOLETE CFront */
614 // OBSOLETE /* cfront output is a C program, so in most ways it looks like a C
615 // OBSOLETE program. But to demangle we need to set the language to C++. We
616 // OBSOLETE can distinguish cfront code by the fact that it has #line
617 // OBSOLETE directives which specify a file name ending in .C. */
618 #endif /* OBSOLETE CFront */
620 /* If the filename of this subfile ends in .C, then change the
621 language of any pending subfiles from C to C++. We also accept
622 any other C++ suffixes accepted by deduce_language_from_filename. */
623 /* OBSOLETE (in particular, some people use .cxx with cfront). */
624 /* Likewise for f2c. */
629 enum language sublang
= deduce_language_from_filename (subfile
->name
);
631 if (sublang
== language_cplus
|| sublang
== language_fortran
)
632 for (s
= subfiles
; s
!= NULL
; s
= s
->next
)
633 if (s
->language
== language_c
)
634 s
->language
= sublang
;
637 /* And patch up this file if necessary. */
638 if (subfile
->language
== language_c
639 && subfile
->next
!= NULL
640 && (subfile
->next
->language
== language_cplus
641 || subfile
->next
->language
== language_fortran
))
643 subfile
->language
= subfile
->next
->language
;
647 /* For stabs readers, the first N_SO symbol is assumed to be the
648 source file name, and the subfile struct is initialized using that
649 assumption. If another N_SO symbol is later seen, immediately
650 following the first one, then the first one is assumed to be the
651 directory name and the second one is really the source file name.
653 So we have to patch up the subfile struct by moving the old name
654 value to dirname and remembering the new name. Some sanity
655 checking is performed to ensure that the state of the subfile
656 struct is reasonable and that the old name we are assuming to be a
657 directory name actually is (by checking for a trailing '/'). */
660 patch_subfile_names (struct subfile
*subfile
, char *name
)
662 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
663 && subfile
->name
[strlen (subfile
->name
) - 1] == '/')
665 subfile
->dirname
= subfile
->name
;
666 subfile
->name
= savestring (name
, strlen (name
));
667 last_source_file
= name
;
669 /* Default the source language to whatever can be deduced from
670 the filename. If nothing can be deduced (such as for a C/C++
671 include file with a ".h" extension), then inherit whatever
672 language the previous subfile had. This kludgery is
673 necessary because there is no standard way in some object
674 formats to record the source language. Also, when symtabs
675 are allocated we try to deduce a language then as well, but
676 it is too late for us to use that information while reading
677 symbols, since symtabs aren't allocated until after all the
678 symbols have been processed for a given source file. */
680 subfile
->language
= deduce_language_from_filename (subfile
->name
);
681 if (subfile
->language
== language_unknown
&&
682 subfile
->next
!= NULL
)
684 subfile
->language
= subfile
->next
->language
;
689 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
690 switching source files (different subfiles, as we call them) within
691 one object file, but using a stack rather than in an arbitrary
697 register struct subfile_stack
*tem
698 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
700 tem
->next
= subfile_stack
;
702 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
704 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
706 tem
->name
= current_subfile
->name
;
713 register struct subfile_stack
*link
= subfile_stack
;
717 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
720 subfile_stack
= link
->next
;
721 xfree ((void *) link
);
725 /* Add a linetable entry for line number LINE and address PC to the
726 line vector for SUBFILE. */
729 record_line (register struct subfile
*subfile
, int line
, CORE_ADDR pc
)
731 struct linetable_entry
*e
;
732 /* Ignore the dummy line number in libg.o */
739 /* Make sure line vector exists and is big enough. */
740 if (!subfile
->line_vector
)
742 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
743 subfile
->line_vector
= (struct linetable
*)
744 xmalloc (sizeof (struct linetable
)
745 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
746 subfile
->line_vector
->nitems
= 0;
747 have_line_numbers
= 1;
750 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
752 subfile
->line_vector_length
*= 2;
753 subfile
->line_vector
= (struct linetable
*)
754 xrealloc ((char *) subfile
->line_vector
,
755 (sizeof (struct linetable
)
756 + (subfile
->line_vector_length
757 * sizeof (struct linetable_entry
))));
760 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
762 e
->pc
= ADDR_BITS_REMOVE(pc
);
765 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
768 compare_line_numbers (const void *ln1p
, const void *ln2p
)
770 struct linetable_entry
*ln1
= (struct linetable_entry
*) ln1p
;
771 struct linetable_entry
*ln2
= (struct linetable_entry
*) ln2p
;
773 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
774 Please keep it that way. */
775 if (ln1
->pc
< ln2
->pc
)
778 if (ln1
->pc
> ln2
->pc
)
781 /* If pc equal, sort by line. I'm not sure whether this is optimum
782 behavior (see comment at struct linetable in symtab.h). */
783 return ln1
->line
- ln2
->line
;
786 /* Start a new symtab for a new source file. Called, for example,
787 when a stabs symbol of type N_SO is seen, or when a DWARF
788 TAG_compile_unit DIE is seen. It indicates the start of data for
789 one original source file. */
792 start_symtab (char *name
, char *dirname
, CORE_ADDR start_addr
)
795 last_source_file
= name
;
796 last_source_start_addr
= start_addr
;
798 global_symbols
= NULL
;
800 have_line_numbers
= 0;
802 /* Context stack is initially empty. Allocate first one with room
803 for 10 levels; reuse it forever afterward. */
804 if (context_stack
== NULL
)
806 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
807 context_stack
= (struct context_stack
*)
808 xmalloc (context_stack_size
* sizeof (struct context_stack
));
810 context_stack_depth
= 0;
812 /* Initialize the list of sub source files with one entry for this
813 file (the top-level source file). */
816 current_subfile
= NULL
;
817 start_subfile (name
, dirname
);
820 /* Finish the symbol definitions for one main source file, close off
821 all the lexical contexts for that file (creating struct block's for
822 them), then make the struct symtab for that file and put it in the
825 END_ADDR is the address of the end of the file's text. SECTION is
826 the section number (in objfile->section_offsets) of the blockvector
829 Note that it is possible for end_symtab() to return NULL. In
830 particular, for the DWARF case at least, it will return NULL when
831 it finds a compilation unit that has exactly one DIE, a
832 TAG_compile_unit DIE. This can happen when we link in an object
833 file that was compiled from an empty source file. Returning NULL
834 is probably not the correct thing to do, because then gdb will
835 never know about this empty file (FIXME). */
838 end_symtab (CORE_ADDR end_addr
, struct objfile
*objfile
, int section
)
840 register struct symtab
*symtab
= NULL
;
841 register struct blockvector
*blockvector
;
842 register struct subfile
*subfile
;
843 register struct context_stack
*cstk
;
844 struct subfile
*nextsub
;
846 /* Finish the lexical context of the last function in the file; pop
847 the context stack. */
849 if (context_stack_depth
> 0)
851 cstk
= pop_context ();
852 /* Make a block for the local symbols within. */
853 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
854 cstk
->start_addr
, end_addr
, objfile
);
856 if (context_stack_depth
> 0)
858 /* This is said to happen with SCO. The old coffread.c
859 code simply emptied the context stack, so we do the
860 same. FIXME: Find out why it is happening. This is not
861 believed to happen in most cases (even for coffread.c);
862 it used to be an abort(). */
863 complaint (&symfile_complaints
,
864 "Context stack not empty in end_symtab");
865 context_stack_depth
= 0;
869 /* Reordered executables may have out of order pending blocks; if
870 OBJF_REORDERED is true, then sort the pending blocks. */
871 if ((objfile
->flags
& OBJF_REORDERED
) && pending_blocks
)
873 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
877 struct pending_block
*pb
, *pbnext
;
885 /* swap blocks if unordered! */
887 if (BLOCK_START (pb
->block
) < BLOCK_START (pbnext
->block
))
889 struct block
*tmp
= pb
->block
;
890 pb
->block
= pbnext
->block
;
895 pbnext
= pbnext
->next
;
901 /* Cleanup any undefined types that have been left hanging around
902 (this needs to be done before the finish_blocks so that
903 file_symbols is still good).
905 Both cleanup_undefined_types and finish_global_stabs are stabs
906 specific, but harmless for other symbol readers, since on gdb
907 startup or when finished reading stabs, the state is set so these
908 are no-ops. FIXME: Is this handled right in case of QUIT? Can
909 we make this cleaner? */
911 cleanup_undefined_types ();
912 finish_global_stabs (objfile
);
914 if (pending_blocks
== NULL
915 && file_symbols
== NULL
916 && global_symbols
== NULL
917 && have_line_numbers
== 0
918 && pending_macros
== NULL
)
920 /* Ignore symtabs that have no functions with real debugging
926 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
928 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
930 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
932 blockvector
= make_blockvector (objfile
);
935 #ifndef PROCESS_LINENUMBER_HOOK
936 #define PROCESS_LINENUMBER_HOOK()
938 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
940 /* Now create the symtab objects proper, one for each subfile. */
941 /* (The main file is the last one on the chain.) */
943 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
945 int linetablesize
= 0;
948 /* If we have blocks of symbols, make a symtab. Otherwise, just
949 ignore this file and any line number info in it. */
952 if (subfile
->line_vector
)
954 linetablesize
= sizeof (struct linetable
) +
955 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
957 /* I think this is artifact from before it went on the
958 obstack. I doubt we'll need the memory between now
959 and when we free it later in this function. */
960 /* First, shrink the linetable to make more memory. */
961 subfile
->line_vector
= (struct linetable
*)
962 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
965 /* Like the pending blocks, the line table may be
966 scrambled in reordered executables. Sort it if
967 OBJF_REORDERED is true. */
968 if (objfile
->flags
& OBJF_REORDERED
)
969 qsort (subfile
->line_vector
->item
,
970 subfile
->line_vector
->nitems
,
971 sizeof (struct linetable_entry
), compare_line_numbers
);
974 /* Now, allocate a symbol table. */
975 symtab
= allocate_symtab (subfile
->name
, objfile
);
977 /* Fill in its components. */
978 symtab
->blockvector
= blockvector
;
979 symtab
->macro_table
= pending_macros
;
980 if (subfile
->line_vector
)
982 /* Reallocate the line table on the symbol obstack */
983 symtab
->linetable
= (struct linetable
*)
984 obstack_alloc (&objfile
->symbol_obstack
, linetablesize
);
985 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
989 symtab
->linetable
= NULL
;
991 symtab
->block_line_section
= section
;
992 if (subfile
->dirname
)
994 /* Reallocate the dirname on the symbol obstack */
995 symtab
->dirname
= (char *)
996 obstack_alloc (&objfile
->symbol_obstack
,
997 strlen (subfile
->dirname
) + 1);
998 strcpy (symtab
->dirname
, subfile
->dirname
);
1002 symtab
->dirname
= NULL
;
1004 symtab
->free_code
= free_linetable
;
1005 symtab
->free_ptr
= NULL
;
1007 /* Use whatever language we have been using for this
1008 subfile, not the one that was deduced in allocate_symtab
1009 from the filename. We already did our own deducing when
1010 we created the subfile, and we may have altered our
1011 opinion of what language it is from things we found in
1013 symtab
->language
= subfile
->language
;
1015 /* Save the debug format string (if any) in the symtab */
1016 if (subfile
->debugformat
!= NULL
)
1018 symtab
->debugformat
= obsavestring (subfile
->debugformat
,
1019 strlen (subfile
->debugformat
),
1020 &objfile
->symbol_obstack
);
1023 /* All symtabs for the main file and the subfiles share a
1024 blockvector, so we need to clear primary for everything
1025 but the main file. */
1027 symtab
->primary
= 0;
1029 if (subfile
->name
!= NULL
)
1031 xfree ((void *) subfile
->name
);
1033 if (subfile
->dirname
!= NULL
)
1035 xfree ((void *) subfile
->dirname
);
1037 if (subfile
->line_vector
!= NULL
)
1039 xfree ((void *) subfile
->line_vector
);
1041 if (subfile
->debugformat
!= NULL
)
1043 xfree ((void *) subfile
->debugformat
);
1046 nextsub
= subfile
->next
;
1047 xfree ((void *) subfile
);
1050 /* Set this for the main source file. */
1053 symtab
->primary
= 1;
1056 last_source_file
= NULL
;
1057 current_subfile
= NULL
;
1058 pending_macros
= NULL
;
1063 /* Push a context block. Args are an identifying nesting level
1064 (checkable when you pop it), and the starting PC address of this
1067 struct context_stack
*
1068 push_context (int desc
, CORE_ADDR valu
)
1070 register struct context_stack
*new;
1072 if (context_stack_depth
== context_stack_size
)
1074 context_stack_size
*= 2;
1075 context_stack
= (struct context_stack
*)
1076 xrealloc ((char *) context_stack
,
1077 (context_stack_size
* sizeof (struct context_stack
)));
1080 new = &context_stack
[context_stack_depth
++];
1082 new->locals
= local_symbols
;
1083 new->params
= param_symbols
;
1084 new->old_blocks
= pending_blocks
;
1085 new->start_addr
= valu
;
1088 local_symbols
= NULL
;
1089 param_symbols
= NULL
;
1094 /* Pop a context block. Returns the address of the context block just
1097 struct context_stack
*
1100 gdb_assert (context_stack_depth
> 0);
1101 return (&context_stack
[--context_stack_depth
]);
1106 /* Compute a small integer hash code for the given name. */
1109 hashname (char *name
)
1111 return (hash(name
,strlen(name
)) % HASHSIZE
);
1116 record_debugformat (char *format
)
1118 current_subfile
->debugformat
= savestring (format
, strlen (format
));
1121 /* Merge the first symbol list SRCLIST into the second symbol list
1122 TARGETLIST by repeated calls to add_symbol_to_list(). This
1123 procedure "frees" each link of SRCLIST by adding it to the
1124 free_pendings list. Caller must set SRCLIST to a null list after
1125 calling this function.
1130 merge_symbol_lists (struct pending
**srclist
, struct pending
**targetlist
)
1134 if (!srclist
|| !*srclist
)
1137 /* Merge in elements from current link. */
1138 for (i
= 0; i
< (*srclist
)->nsyms
; i
++)
1139 add_symbol_to_list ((*srclist
)->symbol
[i
], targetlist
);
1141 /* Recurse on next. */
1142 merge_symbol_lists (&(*srclist
)->next
, targetlist
);
1144 /* "Free" the current link. */
1145 (*srclist
)->next
= free_pendings
;
1146 free_pendings
= (*srclist
);
1149 /* Initialize anything that needs initializing when starting to read a
1150 fresh piece of a symbol file, e.g. reading in the stuff
1151 corresponding to a psymtab. */
1154 buildsym_init (void)
1156 free_pendings
= NULL
;
1157 file_symbols
= NULL
;
1158 global_symbols
= NULL
;
1159 pending_blocks
= NULL
;
1160 pending_macros
= NULL
;
1163 /* Initialize anything that needs initializing when a completely new
1164 symbol file is specified (not just adding some symbols from another
1165 file, e.g. a shared library). */
1168 buildsym_new_init (void)