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 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This module provides subroutines used for creating and adding to
23 the symbol table. These routines are called from various symbol-
24 file-reading routines.
26 Routines to support specific debugging information formats (stabs,
27 DWARF, etc) belong somewhere else. */
31 #include "gdb_obstack.h"
36 #include "complaints.h"
37 #include "gdb_string.h"
38 #include "expression.h" /* For "enum exp_opcode" used by... */
39 #include "language.h" /* For "local_hex_string" */
41 #include "filenames.h" /* For DOSish file names */
43 #include "demangle.h" /* Needed by SYMBOL_INIT_DEMANGLED_NAME. */
44 /* Ask buildsym.h to define the vars it normally declares `extern'. */
47 #include "buildsym.h" /* Our own declarations */
50 /* For cleanup_undefined_types and finish_global_stabs (somewhat
51 questionable--see comment where we call them). */
53 #include "stabsread.h"
55 /* List of free `struct pending' structures for reuse. */
57 static struct pending
*free_pendings
;
59 /* Non-zero if symtab has line number info. This prevents an
60 otherwise empty symtab from being tossed. */
62 static int have_line_numbers
;
64 static int compare_line_numbers (const void *ln1p
, const void *ln2p
);
67 /* Initial sizes of data structures. These are realloc'd larger if
68 needed, and realloc'd down to the size actually used, when
71 #define INITIAL_CONTEXT_STACK_SIZE 10
72 #define INITIAL_LINE_VECTOR_LENGTH 1000
75 /* Complaints about the symbols we have encountered. */
77 struct deprecated_complaint block_end_complaint
=
78 {"block end address less than block start address in %s (patched it)", 0, 0};
80 struct deprecated_complaint anon_block_end_complaint
=
81 {"block end address 0x%lx less than block start address 0x%lx (patched it)", 0, 0};
83 struct deprecated_complaint innerblock_complaint
=
84 {"inner block not inside outer block in %s", 0, 0};
86 struct deprecated_complaint innerblock_anon_complaint
=
87 {"inner block (0x%lx-0x%lx) not inside outer block (0x%lx-0x%lx)", 0, 0};
89 struct deprecated_complaint blockvector_complaint
=
90 {"block at %s out of order", 0, 0};
92 /* maintain the lists of symbols and blocks */
94 /* Add a pending list to free_pendings. */
96 add_free_pendings (struct pending
*list
)
98 register struct pending
*link
= list
;
102 while (link
->next
) link
= link
->next
;
103 link
->next
= free_pendings
;
104 free_pendings
= list
;
108 /* Add a symbol to one of the lists of symbols. */
111 add_symbol_to_list (struct symbol
*symbol
, struct pending
**listhead
)
113 register struct pending
*link
;
115 /* If this is an alias for another symbol, don't add it. */
116 if (symbol
->ginfo
.name
&& symbol
->ginfo
.name
[0] == '#')
119 /* We keep PENDINGSIZE symbols in each link of the list. If we
120 don't have a link with room in it, add a new link. */
121 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
125 link
= free_pendings
;
126 free_pendings
= link
->next
;
130 link
= (struct pending
*) xmalloc (sizeof (struct pending
));
133 link
->next
= *listhead
;
138 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
141 /* Find a symbol named NAME on a LIST. NAME need not be
142 '\0'-terminated; LENGTH is the length of the name. */
145 find_symbol_in_list (struct pending
*list
, char *name
, int length
)
152 for (j
= list
->nsyms
; --j
>= 0;)
154 pp
= SYMBOL_NAME (list
->symbol
[j
]);
155 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0 &&
158 return (list
->symbol
[j
]);
166 /* At end of reading syms, or in case of quit, really free as many
167 `struct pending's as we can easily find. */
171 really_free_pendings (PTR dummy
)
173 struct pending
*next
, *next1
;
175 for (next
= free_pendings
; next
; next
= next1
)
178 xfree ((void *) next
);
180 free_pendings
= NULL
;
182 free_pending_blocks ();
184 for (next
= file_symbols
; next
!= NULL
; next
= next1
)
187 xfree ((void *) next
);
191 for (next
= global_symbols
; next
!= NULL
; next
= next1
)
194 xfree ((void *) next
);
196 global_symbols
= NULL
;
199 free_macro_table (pending_macros
);
202 /* This function is called to discard any pending blocks. */
205 free_pending_blocks (void)
207 #if 0 /* Now we make the links in the
208 symbol_obstack, so don't free
210 struct pending_block
*bnext
, *bnext1
;
212 for (bnext
= pending_blocks
; bnext
; bnext
= bnext1
)
214 bnext1
= bnext
->next
;
215 xfree ((void *) bnext
);
218 pending_blocks
= NULL
;
221 /* Take one of the lists of symbols and make a block from it. Keep
222 the order the symbols have in the list (reversed from the input
223 file). Put the block on the list of pending blocks. */
226 finish_block (struct symbol
*symbol
, struct pending
**listhead
,
227 struct pending_block
*old_blocks
,
228 CORE_ADDR start
, CORE_ADDR end
,
229 struct objfile
*objfile
)
231 register struct pending
*next
, *next1
;
232 register struct block
*block
;
233 register struct pending_block
*pblock
;
234 struct pending_block
*opblock
;
238 /* Count the length of the list of symbols. */
240 for (next
= *listhead
, i
= 0;
242 i
+= next
->nsyms
, next
= next
->next
)
247 /* Copy the symbols into the block. */
251 block
= (struct block
*)
252 obstack_alloc (&objfile
->symbol_obstack
,
253 (sizeof (struct block
) +
254 ((i
- 1) * sizeof (struct symbol
*))));
255 BLOCK_NSYMS (block
) = i
;
256 for (next
= *listhead
; next
; next
= next
->next
)
257 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
259 BLOCK_SYM (block
, --i
) = next
->symbol
[j
];
264 int htab_size
= BLOCK_HASHTABLE_SIZE (i
);
266 block
= (struct block
*)
267 obstack_alloc (&objfile
->symbol_obstack
,
268 (sizeof (struct block
) +
269 ((htab_size
- 1) * sizeof (struct symbol
*))));
270 for (j
= 0; j
< htab_size
; j
++)
272 BLOCK_BUCKET (block
, j
) = 0;
274 BLOCK_BUCKETS (block
) = htab_size
;
275 for (next
= *listhead
; next
; next
= next
->next
)
277 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
280 unsigned int hash_index
;
281 const char *name
= SYMBOL_DEMANGLED_NAME (next
->symbol
[j
]);
283 name
= SYMBOL_NAME (next
->symbol
[j
]);
284 hash_index
= msymbol_hash_iw (name
);
285 hash_index
= hash_index
% BLOCK_BUCKETS (block
);
286 sym
= BLOCK_BUCKET (block
, hash_index
);
287 BLOCK_BUCKET (block
, hash_index
) = next
->symbol
[j
];
288 next
->symbol
[j
]->hash_next
= sym
;
293 BLOCK_START (block
) = start
;
294 BLOCK_END (block
) = end
;
295 /* Superblock filled in when containing block is made */
296 BLOCK_SUPERBLOCK (block
) = NULL
;
298 BLOCK_GCC_COMPILED (block
) = processing_gcc_compilation
;
300 /* Put the block in as the value of the symbol that names it. */
304 struct type
*ftype
= SYMBOL_TYPE (symbol
);
305 SYMBOL_BLOCK_VALUE (symbol
) = block
;
306 BLOCK_FUNCTION (block
) = symbol
;
307 BLOCK_HASHTABLE (block
) = 0;
309 if (TYPE_NFIELDS (ftype
) <= 0)
311 /* No parameter type information is recorded with the
312 function's type. Set that from the type of the
313 parameter symbols. */
314 int nparams
= 0, iparams
;
316 ALL_BLOCK_SYMBOLS (block
, i
, sym
)
318 switch (SYMBOL_CLASS (sym
))
323 case LOC_REGPARM_ADDR
:
324 case LOC_BASEREG_ARG
:
337 case LOC_CONST_BYTES
:
340 case LOC_OPTIMIZED_OUT
:
347 TYPE_NFIELDS (ftype
) = nparams
;
348 TYPE_FIELDS (ftype
) = (struct field
*)
349 TYPE_ALLOC (ftype
, nparams
* sizeof (struct field
));
351 for (i
= iparams
= 0; iparams
< nparams
; i
++)
353 sym
= BLOCK_SYM (block
, i
);
354 switch (SYMBOL_CLASS (sym
))
359 case LOC_REGPARM_ADDR
:
360 case LOC_BASEREG_ARG
:
362 TYPE_FIELD_TYPE (ftype
, iparams
) = SYMBOL_TYPE (sym
);
363 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
375 case LOC_CONST_BYTES
:
378 case LOC_OPTIMIZED_OUT
:
388 BLOCK_FUNCTION (block
) = NULL
;
389 BLOCK_HASHTABLE (block
) = 1;
392 /* Now "free" the links of the list, and empty the list. */
394 for (next
= *listhead
; next
; next
= next1
)
397 next
->next
= free_pendings
;
398 free_pendings
= next
;
403 /* Check to be sure that the blocks have an end address that is
404 greater than starting address */
406 if (BLOCK_END (block
) < BLOCK_START (block
))
410 complain (&block_end_complaint
, SYMBOL_SOURCE_NAME (symbol
));
414 complain (&anon_block_end_complaint
, BLOCK_END (block
), BLOCK_START (block
));
416 /* Better than nothing */
417 BLOCK_END (block
) = BLOCK_START (block
);
421 /* Install this block as the superblock of all blocks made since the
422 start of this scope that don't have superblocks yet. */
425 for (pblock
= pending_blocks
;
426 pblock
&& pblock
!= old_blocks
;
427 pblock
= pblock
->next
)
429 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
432 /* Check to be sure the blocks are nested as we receive
433 them. If the compiler/assembler/linker work, this just
434 burns a small amount of time. */
435 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
) ||
436 BLOCK_END (pblock
->block
) > BLOCK_END (block
))
440 complain (&innerblock_complaint
,
441 SYMBOL_SOURCE_NAME (symbol
));
445 complain (&innerblock_anon_complaint
, BLOCK_START (pblock
->block
),
446 BLOCK_END (pblock
->block
), BLOCK_START (block
),
449 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
))
450 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
451 if (BLOCK_END (pblock
->block
) > BLOCK_END (block
))
452 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
455 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
460 record_pending_block (objfile
, block
, opblock
);
463 /* Record BLOCK on the list of all blocks in the file. Put it after
464 OPBLOCK, or at the beginning if opblock is NULL. This puts the
465 block in the list after all its subblocks.
467 Allocate the pending block struct in the symbol_obstack to save
468 time. This wastes a little space. FIXME: Is it worth it? */
471 record_pending_block (struct objfile
*objfile
, struct block
*block
,
472 struct pending_block
*opblock
)
474 register struct pending_block
*pblock
;
476 pblock
= (struct pending_block
*)
477 obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct pending_block
));
478 pblock
->block
= block
;
481 pblock
->next
= opblock
->next
;
482 opblock
->next
= pblock
;
486 pblock
->next
= pending_blocks
;
487 pending_blocks
= pblock
;
491 /* OBSOLETE Note that this is only used in this file and in dstread.c, which */
492 /* OBSOLETE should be fixed to not need direct access to this function. When */
493 /* OBSOLETE that is done, it can be made static again. */
495 static struct blockvector
*
496 make_blockvector (struct objfile
*objfile
)
498 register struct pending_block
*next
;
499 register struct blockvector
*blockvector
;
502 /* Count the length of the list of blocks. */
504 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++)
508 blockvector
= (struct blockvector
*)
509 obstack_alloc (&objfile
->symbol_obstack
,
510 (sizeof (struct blockvector
)
511 + (i
- 1) * sizeof (struct block
*)));
513 /* Copy the blocks into the blockvector. This is done in reverse
514 order, which happens to put the blocks into the proper order
515 (ascending starting address). finish_block has hair to insert
516 each block into the list after its subblocks in order to make
517 sure this is true. */
519 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
520 for (next
= pending_blocks
; next
; next
= next
->next
)
522 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
525 #if 0 /* Now we make the links in the
526 obstack, so don't free them. */
527 /* Now free the links of the list, and empty the list. */
529 for (next
= pending_blocks
; next
; next
= next1
)
535 pending_blocks
= NULL
;
537 #if 1 /* FIXME, shut this off after a while
538 to speed up symbol reading. */
539 /* Some compilers output blocks in the wrong order, but we depend on
540 their being in the right order so we can binary search. Check the
541 order and moan about it. FIXME. */
542 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
544 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
546 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
- 1))
547 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
)))
550 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector
, i
));
552 complain (&blockvector_complaint
,
553 local_hex_string ((LONGEST
) start
));
559 return (blockvector
);
562 /* Start recording information about source code that came from an
563 included (or otherwise merged-in) source file with a different
564 name. NAME is the name of the file (cannot be NULL), DIRNAME is
565 the directory in which it resides (or NULL if not known). */
568 start_subfile (char *name
, char *dirname
)
570 register struct subfile
*subfile
;
572 /* See if this subfile is already known as a subfile of the current
575 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
577 if (FILENAME_CMP (subfile
->name
, name
) == 0)
579 current_subfile
= subfile
;
584 /* This subfile is not known. Add an entry for it. Make an entry
585 for this subfile in the list of all subfiles of the current main
588 subfile
= (struct subfile
*) xmalloc (sizeof (struct subfile
));
589 memset ((char *) subfile
, 0, sizeof (struct subfile
));
590 subfile
->next
= subfiles
;
592 current_subfile
= subfile
;
594 /* Save its name and compilation directory name */
595 subfile
->name
= (name
== NULL
) ? NULL
: savestring (name
, strlen (name
));
597 (dirname
== NULL
) ? NULL
: savestring (dirname
, strlen (dirname
));
599 /* Initialize line-number recording for this subfile. */
600 subfile
->line_vector
= NULL
;
602 /* Default the source language to whatever can be deduced from the
603 filename. If nothing can be deduced (such as for a C/C++ include
604 file with a ".h" extension), then inherit whatever language the
605 previous subfile had. This kludgery is necessary because there
606 is no standard way in some object formats to record the source
607 language. Also, when symtabs are allocated we try to deduce a
608 language then as well, but it is too late for us to use that
609 information while reading symbols, since symtabs aren't allocated
610 until after all the symbols have been processed for a given
613 subfile
->language
= deduce_language_from_filename (subfile
->name
);
614 if (subfile
->language
== language_unknown
&&
615 subfile
->next
!= NULL
)
617 subfile
->language
= subfile
->next
->language
;
620 /* Initialize the debug format string to NULL. We may supply it
621 later via a call to record_debugformat. */
622 subfile
->debugformat
= NULL
;
624 /* cfront output is a C program, so in most ways it looks like a C
625 program. But to demangle we need to set the language to C++. We
626 can distinguish cfront code by the fact that it has #line
627 directives which specify a file name ending in .C.
629 So if the filename of this subfile ends in .C, then change the
630 language of any pending subfiles from C to C++. We also accept
631 any other C++ suffixes accepted by deduce_language_from_filename
632 (in particular, some people use .cxx with cfront). */
633 /* Likewise for f2c. */
638 enum language sublang
= deduce_language_from_filename (subfile
->name
);
640 if (sublang
== language_cplus
|| sublang
== language_fortran
)
641 for (s
= subfiles
; s
!= NULL
; s
= s
->next
)
642 if (s
->language
== language_c
)
643 s
->language
= sublang
;
646 /* And patch up this file if necessary. */
647 if (subfile
->language
== language_c
648 && subfile
->next
!= NULL
649 && (subfile
->next
->language
== language_cplus
650 || subfile
->next
->language
== language_fortran
))
652 subfile
->language
= subfile
->next
->language
;
656 /* For stabs readers, the first N_SO symbol is assumed to be the
657 source file name, and the subfile struct is initialized using that
658 assumption. If another N_SO symbol is later seen, immediately
659 following the first one, then the first one is assumed to be the
660 directory name and the second one is really the source file name.
662 So we have to patch up the subfile struct by moving the old name
663 value to dirname and remembering the new name. Some sanity
664 checking is performed to ensure that the state of the subfile
665 struct is reasonable and that the old name we are assuming to be a
666 directory name actually is (by checking for a trailing '/'). */
669 patch_subfile_names (struct subfile
*subfile
, char *name
)
671 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
672 && subfile
->name
[strlen (subfile
->name
) - 1] == '/')
674 subfile
->dirname
= subfile
->name
;
675 subfile
->name
= savestring (name
, strlen (name
));
676 last_source_file
= name
;
678 /* Default the source language to whatever can be deduced from
679 the filename. If nothing can be deduced (such as for a C/C++
680 include file with a ".h" extension), then inherit whatever
681 language the previous subfile had. This kludgery is
682 necessary because there is no standard way in some object
683 formats to record the source language. Also, when symtabs
684 are allocated we try to deduce a language then as well, but
685 it is too late for us to use that information while reading
686 symbols, since symtabs aren't allocated until after all the
687 symbols have been processed for a given source file. */
689 subfile
->language
= deduce_language_from_filename (subfile
->name
);
690 if (subfile
->language
== language_unknown
&&
691 subfile
->next
!= NULL
)
693 subfile
->language
= subfile
->next
->language
;
698 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
699 switching source files (different subfiles, as we call them) within
700 one object file, but using a stack rather than in an arbitrary
706 register struct subfile_stack
*tem
707 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
709 tem
->next
= subfile_stack
;
711 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
713 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
715 tem
->name
= current_subfile
->name
;
722 register struct subfile_stack
*link
= subfile_stack
;
726 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
729 subfile_stack
= link
->next
;
730 xfree ((void *) link
);
734 /* Add a linetable entry for line number LINE and address PC to the
735 line vector for SUBFILE. */
738 record_line (register struct subfile
*subfile
, int line
, CORE_ADDR pc
)
740 struct linetable_entry
*e
;
741 /* Ignore the dummy line number in libg.o */
748 /* Make sure line vector exists and is big enough. */
749 if (!subfile
->line_vector
)
751 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
752 subfile
->line_vector
= (struct linetable
*)
753 xmalloc (sizeof (struct linetable
)
754 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
755 subfile
->line_vector
->nitems
= 0;
756 have_line_numbers
= 1;
759 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
761 subfile
->line_vector_length
*= 2;
762 subfile
->line_vector
= (struct linetable
*)
763 xrealloc ((char *) subfile
->line_vector
,
764 (sizeof (struct linetable
)
765 + (subfile
->line_vector_length
766 * sizeof (struct linetable_entry
))));
769 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
771 e
->pc
= ADDR_BITS_REMOVE(pc
);
774 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
777 compare_line_numbers (const void *ln1p
, const void *ln2p
)
779 struct linetable_entry
*ln1
= (struct linetable_entry
*) ln1p
;
780 struct linetable_entry
*ln2
= (struct linetable_entry
*) ln2p
;
782 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
783 Please keep it that way. */
784 if (ln1
->pc
< ln2
->pc
)
787 if (ln1
->pc
> ln2
->pc
)
790 /* If pc equal, sort by line. I'm not sure whether this is optimum
791 behavior (see comment at struct linetable in symtab.h). */
792 return ln1
->line
- ln2
->line
;
795 /* Start a new symtab for a new source file. Called, for example,
796 when a stabs symbol of type N_SO is seen, or when a DWARF
797 TAG_compile_unit DIE is seen. It indicates the start of data for
798 one original source file. */
801 start_symtab (char *name
, char *dirname
, CORE_ADDR start_addr
)
804 last_source_file
= name
;
805 last_source_start_addr
= start_addr
;
807 global_symbols
= NULL
;
809 have_line_numbers
= 0;
811 /* Context stack is initially empty. Allocate first one with room
812 for 10 levels; reuse it forever afterward. */
813 if (context_stack
== NULL
)
815 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
816 context_stack
= (struct context_stack
*)
817 xmalloc (context_stack_size
* sizeof (struct context_stack
));
819 context_stack_depth
= 0;
821 /* Initialize the list of sub source files with one entry for this
822 file (the top-level source file). */
825 current_subfile
= NULL
;
826 start_subfile (name
, dirname
);
829 /* Finish the symbol definitions for one main source file, close off
830 all the lexical contexts for that file (creating struct block's for
831 them), then make the struct symtab for that file and put it in the
834 END_ADDR is the address of the end of the file's text. SECTION is
835 the section number (in objfile->section_offsets) of the blockvector
838 Note that it is possible for end_symtab() to return NULL. In
839 particular, for the DWARF case at least, it will return NULL when
840 it finds a compilation unit that has exactly one DIE, a
841 TAG_compile_unit DIE. This can happen when we link in an object
842 file that was compiled from an empty source file. Returning NULL
843 is probably not the correct thing to do, because then gdb will
844 never know about this empty file (FIXME). */
847 end_symtab (CORE_ADDR end_addr
, struct objfile
*objfile
, int section
)
849 register struct symtab
*symtab
= NULL
;
850 register struct blockvector
*blockvector
;
851 register struct subfile
*subfile
;
852 register struct context_stack
*cstk
;
853 struct subfile
*nextsub
;
855 /* Finish the lexical context of the last function in the file; pop
856 the context stack. */
858 if (context_stack_depth
> 0)
860 cstk
= pop_context ();
861 /* Make a block for the local symbols within. */
862 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
863 cstk
->start_addr
, end_addr
, objfile
);
865 if (context_stack_depth
> 0)
867 /* This is said to happen with SCO. The old coffread.c
868 code simply emptied the context stack, so we do the
869 same. FIXME: Find out why it is happening. This is not
870 believed to happen in most cases (even for coffread.c);
871 it used to be an abort(). */
872 static struct deprecated_complaint msg
=
873 {"Context stack not empty in end_symtab", 0, 0};
875 context_stack_depth
= 0;
879 /* Reordered executables may have out of order pending blocks; if
880 OBJF_REORDERED is true, then sort the pending blocks. */
881 if ((objfile
->flags
& OBJF_REORDERED
) && pending_blocks
)
883 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
887 struct pending_block
*pb
, *pbnext
;
895 /* swap blocks if unordered! */
897 if (BLOCK_START (pb
->block
) < BLOCK_START (pbnext
->block
))
899 struct block
*tmp
= pb
->block
;
900 pb
->block
= pbnext
->block
;
905 pbnext
= pbnext
->next
;
911 /* Cleanup any undefined types that have been left hanging around
912 (this needs to be done before the finish_blocks so that
913 file_symbols is still good).
915 Both cleanup_undefined_types and finish_global_stabs are stabs
916 specific, but harmless for other symbol readers, since on gdb
917 startup or when finished reading stabs, the state is set so these
918 are no-ops. FIXME: Is this handled right in case of QUIT? Can
919 we make this cleaner? */
921 cleanup_undefined_types ();
922 finish_global_stabs (objfile
);
924 if (pending_blocks
== NULL
925 && file_symbols
== NULL
926 && global_symbols
== NULL
927 && have_line_numbers
== 0
928 && pending_macros
== NULL
)
930 /* Ignore symtabs that have no functions with real debugging
936 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
938 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
940 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
942 blockvector
= make_blockvector (objfile
);
945 #ifndef PROCESS_LINENUMBER_HOOK
946 #define PROCESS_LINENUMBER_HOOK()
948 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
950 /* Now create the symtab objects proper, one for each subfile. */
951 /* (The main file is the last one on the chain.) */
953 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
955 int linetablesize
= 0;
958 /* If we have blocks of symbols, make a symtab. Otherwise, just
959 ignore this file and any line number info in it. */
962 if (subfile
->line_vector
)
964 linetablesize
= sizeof (struct linetable
) +
965 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
967 /* I think this is artifact from before it went on the
968 obstack. I doubt we'll need the memory between now
969 and when we free it later in this function. */
970 /* First, shrink the linetable to make more memory. */
971 subfile
->line_vector
= (struct linetable
*)
972 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
975 /* Like the pending blocks, the line table may be
976 scrambled in reordered executables. Sort it if
977 OBJF_REORDERED is true. */
978 if (objfile
->flags
& OBJF_REORDERED
)
979 qsort (subfile
->line_vector
->item
,
980 subfile
->line_vector
->nitems
,
981 sizeof (struct linetable_entry
), compare_line_numbers
);
984 /* Now, allocate a symbol table. */
985 symtab
= allocate_symtab (subfile
->name
, objfile
);
987 /* Fill in its components. */
988 symtab
->blockvector
= blockvector
;
989 symtab
->macro_table
= pending_macros
;
990 if (subfile
->line_vector
)
992 /* Reallocate the line table on the symbol obstack */
993 symtab
->linetable
= (struct linetable
*)
994 obstack_alloc (&objfile
->symbol_obstack
, linetablesize
);
995 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
999 symtab
->linetable
= NULL
;
1001 symtab
->block_line_section
= section
;
1002 if (subfile
->dirname
)
1004 /* Reallocate the dirname on the symbol obstack */
1005 symtab
->dirname
= (char *)
1006 obstack_alloc (&objfile
->symbol_obstack
,
1007 strlen (subfile
->dirname
) + 1);
1008 strcpy (symtab
->dirname
, subfile
->dirname
);
1012 symtab
->dirname
= NULL
;
1014 symtab
->free_code
= free_linetable
;
1015 symtab
->free_ptr
= NULL
;
1017 /* Use whatever language we have been using for this
1018 subfile, not the one that was deduced in allocate_symtab
1019 from the filename. We already did our own deducing when
1020 we created the subfile, and we may have altered our
1021 opinion of what language it is from things we found in
1023 symtab
->language
= subfile
->language
;
1025 /* Save the debug format string (if any) in the symtab */
1026 if (subfile
->debugformat
!= NULL
)
1028 symtab
->debugformat
= obsavestring (subfile
->debugformat
,
1029 strlen (subfile
->debugformat
),
1030 &objfile
->symbol_obstack
);
1033 /* All symtabs for the main file and the subfiles share a
1034 blockvector, so we need to clear primary for everything
1035 but the main file. */
1037 symtab
->primary
= 0;
1039 if (subfile
->name
!= NULL
)
1041 xfree ((void *) subfile
->name
);
1043 if (subfile
->dirname
!= NULL
)
1045 xfree ((void *) subfile
->dirname
);
1047 if (subfile
->line_vector
!= NULL
)
1049 xfree ((void *) subfile
->line_vector
);
1051 if (subfile
->debugformat
!= NULL
)
1053 xfree ((void *) subfile
->debugformat
);
1056 nextsub
= subfile
->next
;
1057 xfree ((void *) subfile
);
1060 /* Set this for the main source file. */
1063 symtab
->primary
= 1;
1066 last_source_file
= NULL
;
1067 current_subfile
= NULL
;
1068 pending_macros
= NULL
;
1073 /* Push a context block. Args are an identifying nesting level
1074 (checkable when you pop it), and the starting PC address of this
1077 struct context_stack
*
1078 push_context (int desc
, CORE_ADDR valu
)
1080 register struct context_stack
*new;
1082 if (context_stack_depth
== context_stack_size
)
1084 context_stack_size
*= 2;
1085 context_stack
= (struct context_stack
*)
1086 xrealloc ((char *) context_stack
,
1087 (context_stack_size
* sizeof (struct context_stack
)));
1090 new = &context_stack
[context_stack_depth
++];
1092 new->locals
= local_symbols
;
1093 new->params
= param_symbols
;
1094 new->old_blocks
= pending_blocks
;
1095 new->start_addr
= valu
;
1098 local_symbols
= NULL
;
1099 param_symbols
= NULL
;
1105 /* Compute a small integer hash code for the given name. */
1108 hashname (char *name
)
1110 return (hash(name
,strlen(name
)) % HASHSIZE
);
1115 record_debugformat (char *format
)
1117 current_subfile
->debugformat
= savestring (format
, strlen (format
));
1120 /* Merge the first symbol list SRCLIST into the second symbol list
1121 TARGETLIST by repeated calls to add_symbol_to_list(). This
1122 procedure "frees" each link of SRCLIST by adding it to the
1123 free_pendings list. Caller must set SRCLIST to a null list after
1124 calling this function.
1129 merge_symbol_lists (struct pending
**srclist
, struct pending
**targetlist
)
1133 if (!srclist
|| !*srclist
)
1136 /* Merge in elements from current link. */
1137 for (i
= 0; i
< (*srclist
)->nsyms
; i
++)
1138 add_symbol_to_list ((*srclist
)->symbol
[i
], targetlist
);
1140 /* Recurse on next. */
1141 merge_symbol_lists (&(*srclist
)->next
, targetlist
);
1143 /* "Free" the current link. */
1144 (*srclist
)->next
= free_pendings
;
1145 free_pendings
= (*srclist
);
1148 /* Initialize anything that needs initializing when starting to read a
1149 fresh piece of a symbol file, e.g. reading in the stuff
1150 corresponding to a psymtab. */
1153 buildsym_init (void)
1155 free_pendings
= NULL
;
1156 file_symbols
= NULL
;
1157 global_symbols
= NULL
;
1158 pending_blocks
= NULL
;
1159 pending_macros
= NULL
;
1162 /* Initialize anything that needs initializing when a completely new
1163 symbol file is specified (not just adding some symbols from another
1164 file, e.g. a shared library). */
1167 buildsym_new_init (void)