1 /* Support routines for building symbol tables in GDB's internal format.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
3 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
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. */
32 #include "symfile.h" /* Needed for "struct complaint" */
34 #include "complaints.h"
37 /* Ask buildsym.h to define the vars it normally declares `extern'. */
39 #include "buildsym.h" /* Our own declarations */
42 /* For cleanup_undefined_types and finish_global_stabs (somewhat
43 questionable--see comment where we call them). */
44 #include "stabsread.h"
47 compare_line_numbers PARAMS ((const void *, const void *));
49 static struct blockvector *
50 make_blockvector PARAMS ((struct objfile *));
53 /* Initial sizes of data structures. These are realloc'd larger if needed,
54 and realloc'd down to the size actually used, when completed. */
56 #define INITIAL_CONTEXT_STACK_SIZE 10
57 #define INITIAL_LINE_VECTOR_LENGTH 1000
60 /* Complaints about the symbols we have encountered. */
62 struct complaint innerblock_complaint =
63 {"inner block not inside outer block in %s", 0, 0};
65 struct complaint innerblock_anon_complaint =
66 {"inner block not inside outer block", 0, 0};
68 struct complaint blockvector_complaint =
69 {"block at 0x%lx out of order", 0, 0};
72 /* maintain the lists of symbols and blocks */
74 /* Add a symbol to one of the lists of symbols. */
77 add_symbol_to_list (symbol, listhead)
78 struct symbol *symbol;
79 struct pending **listhead;
81 register struct pending *link;
83 /* We keep PENDINGSIZE symbols in each link of the list.
84 If we don't have a link with room in it, add a new link. */
85 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
90 free_pendings = link->next;
94 link = (struct pending *) xmalloc (sizeof (struct pending));
97 link->next = *listhead;
102 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
105 /* Find a symbol named NAME on a LIST. NAME need not be '\0'-terminated;
106 LENGTH is the length of the name. */
109 find_symbol_in_list (list, name, length)
110 struct pending *list;
119 for (j = list->nsyms; --j >= 0; )
121 pp = SYMBOL_NAME (list->symbol[j]);
122 if (*pp == *name && strncmp (pp, name, length) == 0 &&
125 return (list->symbol[j]);
133 /* At end of reading syms, or in case of quit,
134 really free as many `struct pending's as we can easily find. */
138 really_free_pendings (foo)
141 struct pending *next, *next1;
143 struct pending_block *bnext, *bnext1;
146 for (next = free_pendings; next; next = next1)
151 free_pendings = NULL;
153 #if 0 /* Now we make the links in the symbol_obstack, so don't free them. */
154 for (bnext = pending_blocks; bnext; bnext = bnext1)
156 bnext1 = bnext->next;
160 pending_blocks = NULL;
162 for (next = file_symbols; next != NULL; next = next1)
169 for (next = global_symbols; next != NULL; next = next1)
174 global_symbols = NULL;
177 /* Take one of the lists of symbols and make a block from it.
178 Keep the order the symbols have in the list (reversed from the input file).
179 Put the block on the list of pending blocks. */
182 finish_block (symbol, listhead, old_blocks, start, end, objfile)
183 struct symbol *symbol;
184 struct pending **listhead;
185 struct pending_block *old_blocks;
186 CORE_ADDR start, end;
187 struct objfile *objfile;
189 register struct pending *next, *next1;
190 register struct block *block;
191 register struct pending_block *pblock;
192 struct pending_block *opblock;
196 /* Count the length of the list of symbols. */
198 for (next = *listhead, i = 0;
200 i += next->nsyms, next = next->next)
205 block = (struct block *) obstack_alloc (&objfile -> symbol_obstack,
206 (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *))));
208 /* Copy the symbols into the block. */
210 BLOCK_NSYMS (block) = i;
211 for (next = *listhead; next; next = next->next)
213 for (j = next->nsyms - 1; j >= 0; j--)
215 BLOCK_SYM (block, --i) = next->symbol[j];
219 BLOCK_START (block) = start;
220 BLOCK_END (block) = end;
221 /* Superblock filled in when containing block is made */
222 BLOCK_SUPERBLOCK (block) = NULL;
223 BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
225 /* Put the block in as the value of the symbol that names it. */
229 SYMBOL_BLOCK_VALUE (symbol) = block;
230 BLOCK_FUNCTION (block) = symbol;
234 BLOCK_FUNCTION (block) = NULL;
237 /* Now "free" the links of the list, and empty the list. */
239 for (next = *listhead; next; next = next1)
242 next->next = free_pendings;
243 free_pendings = next;
247 /* Install this block as the superblock
248 of all blocks made since the start of this scope
249 that don't have superblocks yet. */
252 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
254 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
257 /* Check to be sure the blocks are nested as we receive them.
258 If the compiler/assembler/linker work, this just burns a small
260 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
261 BLOCK_END (pblock->block) > BLOCK_END (block))
265 complain (&innerblock_complaint,
266 SYMBOL_SOURCE_NAME (symbol));
270 complain (&innerblock_anon_complaint);
272 BLOCK_START (pblock->block) = BLOCK_START (block);
273 BLOCK_END (pblock->block) = BLOCK_END (block);
276 BLOCK_SUPERBLOCK (pblock->block) = block;
281 /* Record this block on the list of all blocks in the file.
282 Put it after opblock, or at the beginning if opblock is 0.
283 This puts the block in the list after all its subblocks. */
285 /* Allocate in the symbol_obstack to save time.
286 It wastes a little space. */
287 pblock = (struct pending_block *)
288 obstack_alloc (&objfile -> symbol_obstack,
289 sizeof (struct pending_block));
290 pblock->block = block;
293 pblock->next = opblock->next;
294 opblock->next = pblock;
298 pblock->next = pending_blocks;
299 pending_blocks = pblock;
303 static struct blockvector *
304 make_blockvector (objfile)
305 struct objfile *objfile;
307 register struct pending_block *next;
308 register struct blockvector *blockvector;
311 /* Count the length of the list of blocks. */
313 for (next = pending_blocks, i = 0; next; next = next->next, i++) {;}
315 blockvector = (struct blockvector *)
316 obstack_alloc (&objfile -> symbol_obstack,
317 (sizeof (struct blockvector)
318 + (i - 1) * sizeof (struct block *)));
320 /* Copy the blocks into the blockvector.
321 This is done in reverse order, which happens to put
322 the blocks into the proper order (ascending starting address).
323 finish_block has hair to insert each block into the list
324 after its subblocks in order to make sure this is true. */
326 BLOCKVECTOR_NBLOCKS (blockvector) = i;
327 for (next = pending_blocks; next; next = next->next)
329 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
332 #if 0 /* Now we make the links in the obstack, so don't free them. */
333 /* Now free the links of the list, and empty the list. */
335 for (next = pending_blocks; next; next = next1)
341 pending_blocks = NULL;
343 #if 1 /* FIXME, shut this off after a while to speed up symbol reading. */
344 /* Some compilers output blocks in the wrong order, but we depend
345 on their being in the right order so we can binary search.
346 Check the order and moan about it. FIXME. */
347 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
349 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
351 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1))
352 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)))
354 complain (&blockvector_complaint,
355 (unsigned long) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)));
361 return (blockvector);
365 /* Start recording information about source code that came from an included
366 (or otherwise merged-in) source file with a different name. NAME is
367 the name of the file (cannot be NULL), DIRNAME is the directory in which
368 it resides (or NULL if not known). */
371 start_subfile (name, dirname)
375 register struct subfile *subfile;
377 /* See if this subfile is already known as a subfile of the
378 current main source file. */
380 for (subfile = subfiles; subfile; subfile = subfile->next)
382 if (STREQ (subfile->name, name))
384 current_subfile = subfile;
389 /* This subfile is not known. Add an entry for it.
390 Make an entry for this subfile in the list of all subfiles
391 of the current main source file. */
393 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
394 subfile->next = subfiles;
396 current_subfile = subfile;
398 /* Save its name and compilation directory name */
399 subfile->name = (name == NULL)? NULL : strdup (name);
400 subfile->dirname = (dirname == NULL) ? NULL : strdup (dirname);
402 /* Initialize line-number recording for this subfile. */
403 subfile->line_vector = NULL;
405 /* Default the source language to whatever can be deduced from
406 the filename. If nothing can be deduced (such as for a C/C++
407 include file with a ".h" extension), then inherit whatever
408 language the previous subfile had. This kludgery is necessary
409 because there is no standard way in some object formats to
410 record the source language. Also, when symtabs are allocated
411 we try to deduce a language then as well, but it is too late
412 for us to use that information while reading symbols, since
413 symtabs aren't allocated until after all the symbols have
414 been processed for a given source file. */
416 subfile->language = deduce_language_from_filename (subfile->name);
417 if (subfile->language == language_unknown &&
418 subfile->next != NULL)
420 subfile->language = subfile->next->language;
423 /* cfront output is a C program, so in most ways it looks like a C
424 program. But to demangle we need to set the language to C++. We
425 can distinguish cfront code by the fact that it has #line
426 directives which specify a file name ending in .C.
428 So if the filename of this subfile ends in .C, then change the language
429 of any pending subfiles from C to C++. We also accept any other C++
430 suffixes accepted by deduce_language_from_filename (in particular,
431 some people use .cxx with cfront). */
437 if (deduce_language_from_filename (subfile->name) == language_cplus)
438 for (s = subfiles; s != NULL; s = s->next)
439 if (s->language == language_c)
440 s->language = language_cplus;
443 /* And patch up this file if necessary. */
444 if (subfile->language == language_c
445 && subfile->next != NULL
446 && subfile->next->language == language_cplus)
448 subfile->language = language_cplus;
452 /* For stabs readers, the first N_SO symbol is assumed to be the source
453 file name, and the subfile struct is initialized using that assumption.
454 If another N_SO symbol is later seen, immediately following the first
455 one, then the first one is assumed to be the directory name and the
456 second one is really the source file name.
458 So we have to patch up the subfile struct by moving the old name value to
459 dirname and remembering the new name. Some sanity checking is performed
460 to ensure that the state of the subfile struct is reasonable and that the
461 old name we are assuming to be a directory name actually is (by checking
462 for a trailing '/'). */
465 patch_subfile_names (subfile, name)
466 struct subfile *subfile;
469 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
470 && subfile->name[strlen(subfile->name)-1] == '/')
472 subfile->dirname = subfile->name;
473 subfile->name = strdup (name);
475 /* Default the source language to whatever can be deduced from
476 the filename. If nothing can be deduced (such as for a C/C++
477 include file with a ".h" extension), then inherit whatever
478 language the previous subfile had. This kludgery is necessary
479 because there is no standard way in some object formats to
480 record the source language. Also, when symtabs are allocated
481 we try to deduce a language then as well, but it is too late
482 for us to use that information while reading symbols, since
483 symtabs aren't allocated until after all the symbols have
484 been processed for a given source file. */
486 subfile->language = deduce_language_from_filename (subfile->name);
487 if (subfile->language == language_unknown &&
488 subfile->next != NULL)
490 subfile->language = subfile->next->language;
496 /* Handle the N_BINCL and N_EINCL symbol types
497 that act like N_SOL for switching source files
498 (different subfiles, as we call them) within one object file,
499 but using a stack rather than in an arbitrary order. */
504 register struct subfile_stack *tem
505 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
507 tem->next = subfile_stack;
509 if (current_subfile == NULL || current_subfile->name == NULL)
513 tem->name = current_subfile->name;
520 register struct subfile_stack *link = subfile_stack;
527 subfile_stack = link->next;
533 /* Add a linetable entry for line number LINE and address PC to the line
534 vector for SUBFILE. */
537 record_line (subfile, line, pc)
538 register struct subfile *subfile;
542 struct linetable_entry *e;
543 /* Ignore the dummy line number in libg.o */
550 /* Make sure line vector exists and is big enough. */
551 if (!subfile->line_vector)
553 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
554 subfile->line_vector = (struct linetable *)
555 xmalloc (sizeof (struct linetable)
556 + subfile->line_vector_length * sizeof (struct linetable_entry));
557 subfile->line_vector->nitems = 0;
560 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
562 subfile->line_vector_length *= 2;
563 subfile->line_vector = (struct linetable *)
564 xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable)
565 + subfile->line_vector_length * sizeof (struct linetable_entry)));
568 e = subfile->line_vector->item + subfile->line_vector->nitems++;
569 e->line = line; e->pc = pc;
573 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
576 compare_line_numbers (ln1p, ln2p)
580 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
581 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
583 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
584 Please keep it that way. */
585 if (ln1->pc < ln2->pc)
588 if (ln1->pc > ln2->pc)
591 /* If pc equal, sort by line. I'm not sure whether this is optimum
592 behavior (see comment at struct linetable in symtab.h). */
593 return ln1->line - ln2->line;
597 /* Start a new symtab for a new source file.
598 Called, for example, when a stabs symbol of type N_SO is seen, or when
599 a DWARF TAG_compile_unit DIE is seen.
600 It indicates the start of data for one original source file. */
603 start_symtab (name, dirname, start_addr)
606 CORE_ADDR start_addr;
609 last_source_file = name;
610 last_source_start_addr = start_addr;
612 global_symbols = NULL;
615 /* Context stack is initially empty. Allocate first one with room for
616 10 levels; reuse it forever afterward. */
617 if (context_stack == NULL)
619 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
620 context_stack = (struct context_stack *)
621 xmalloc (context_stack_size * sizeof (struct context_stack));
623 context_stack_depth = 0;
625 /* Initialize the list of sub source files with one entry
626 for this file (the top-level source file). */
629 current_subfile = NULL;
630 start_subfile (name, dirname);
633 /* Finish the symbol definitions for one main source file,
634 close off all the lexical contexts for that file
635 (creating struct block's for them), then make the struct symtab
636 for that file and put it in the list of all such.
638 END_ADDR is the address of the end of the file's text.
639 SECTION is the section number (in objfile->section_offsets) of
640 the blockvector and linetable.
642 Note that it is possible for end_symtab() to return NULL. In particular,
643 for the DWARF case at least, it will return NULL when it finds a
644 compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This
645 can happen when we link in an object file that was compiled from an empty
646 source file. Returning NULL is probably not the correct thing to do,
647 because then gdb will never know about this empty file (FIXME). */
650 end_symtab (end_addr, sort_pending, sort_linevec, objfile, section)
654 struct objfile *objfile;
657 register struct symtab *symtab = NULL;
658 register struct blockvector *blockvector;
659 register struct subfile *subfile;
660 register struct context_stack *cstk;
661 struct subfile *nextsub;
663 /* Finish the lexical context of the last function in the file;
664 pop the context stack. */
666 if (context_stack_depth > 0)
668 context_stack_depth--;
669 cstk = &context_stack[context_stack_depth];
670 /* Make a block for the local symbols within. */
671 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
672 cstk->start_addr, end_addr, objfile);
674 if (context_stack_depth > 0)
676 /* This is said to happen with SCO. The old coffread.c code
677 simply emptied the context stack, so we do the same. FIXME:
678 Find out why it is happening. This is not believed to happen
679 in most cases (even for coffread.c); it used to be an abort(). */
680 static struct complaint msg =
681 {"Context stack not empty in end_symtab", 0, 0};
683 context_stack_depth = 0;
687 /* It is unfortunate that in xcoff, pending blocks might not be ordered
688 in this stage. Especially, blocks for static functions will show up at
689 the end. We need to sort them, so tools like `find_pc_function' and
690 `find_pc_block' can work reliably. */
692 if (sort_pending && pending_blocks)
694 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
698 struct pending_block *pb, *pbnext;
706 /* swap blocks if unordered! */
708 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block))
710 struct block *tmp = pb->block;
711 pb->block = pbnext->block;
716 pbnext = pbnext->next;
721 /* Cleanup any undefined types that have been left hanging around
722 (this needs to be done before the finish_blocks so that
723 file_symbols is still good).
725 Both cleanup_undefined_types and finish_global_stabs are stabs
726 specific, but harmless for other symbol readers, since on gdb
727 startup or when finished reading stabs, the state is set so these
728 are no-ops. FIXME: Is this handled right in case of QUIT? Can
729 we make this cleaner? */
731 cleanup_undefined_types ();
732 finish_global_stabs (objfile);
734 if (pending_blocks == NULL
735 && file_symbols == NULL
736 && global_symbols == NULL)
738 /* Ignore symtabs that have no functions with real debugging info */
743 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
744 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
746 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
748 blockvector = make_blockvector (objfile);
751 #ifdef PROCESS_LINENUMBER_HOOK
752 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
755 /* Now create the symtab objects proper, one for each subfile. */
756 /* (The main file is the last one on the chain.) */
758 for (subfile = subfiles; subfile; subfile = nextsub)
760 int linetablesize = 0;
761 /* If we have blocks of symbols, make a symtab.
762 Otherwise, just ignore this file and any line number info in it. */
766 if (subfile->line_vector)
768 linetablesize = sizeof (struct linetable) +
769 subfile->line_vector->nitems * sizeof (struct linetable_entry);
771 /* I think this is artifact from before it went on the obstack.
772 I doubt we'll need the memory between now and when we
773 free it later in this function. */
774 /* First, shrink the linetable to make more memory. */
775 subfile->line_vector = (struct linetable *)
776 xrealloc ((char *) subfile->line_vector, linetablesize);
778 /* If sort_linevec is false, we might want just check to make
779 sure they are sorted and complain() if not, as a way of
780 tracking down compilers/symbol readers which don't get
781 them sorted right. */
784 qsort (subfile->line_vector->item,
785 subfile->line_vector->nitems,
786 sizeof (struct linetable_entry), compare_line_numbers);
789 /* Now, allocate a symbol table. */
790 symtab = allocate_symtab (subfile->name, objfile);
792 /* Fill in its components. */
793 symtab->blockvector = blockvector;
794 if (subfile->line_vector)
796 /* Reallocate the line table on the symbol obstack */
797 symtab->linetable = (struct linetable *)
798 obstack_alloc (&objfile -> symbol_obstack, linetablesize);
799 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
803 symtab->linetable = NULL;
805 symtab->block_line_section = section;
806 if (subfile->dirname)
808 /* Reallocate the dirname on the symbol obstack */
809 symtab->dirname = (char *)
810 obstack_alloc (&objfile -> symbol_obstack,
811 strlen (subfile -> dirname) + 1);
812 strcpy (symtab->dirname, subfile->dirname);
816 symtab->dirname = NULL;
818 symtab->free_code = free_linetable;
819 symtab->free_ptr = NULL;
821 /* Use whatever language we have been using for this subfile,
822 not the one that was deduced in allocate_symtab from the
823 filename. We already did our own deducing when we created
824 the subfile, and we may have altered our opinion of what
825 language it is from things we found in the symbols. */
826 symtab->language = subfile->language;
828 /* All symtabs for the main file and the subfiles share a
829 blockvector, so we need to clear primary for everything but
834 if (subfile->name != NULL)
836 free ((PTR) subfile->name);
838 if (subfile->dirname != NULL)
840 free ((PTR) subfile->dirname);
842 if (subfile->line_vector != NULL)
844 free ((PTR) subfile->line_vector);
847 nextsub = subfile->next;
851 /* Set this for the main source file. */
857 last_source_file = NULL;
858 current_subfile = NULL;
864 /* Push a context block. Args are an identifying nesting level (checkable
865 when you pop it), and the starting PC address of this context. */
867 struct context_stack *
868 push_context (desc, valu)
872 register struct context_stack *new;
874 if (context_stack_depth == context_stack_size)
876 context_stack_size *= 2;
877 context_stack = (struct context_stack *)
878 xrealloc ((char *) context_stack,
879 (context_stack_size * sizeof (struct context_stack)));
882 new = &context_stack[context_stack_depth++];
884 new->locals = local_symbols;
885 new->old_blocks = pending_blocks;
886 new->start_addr = valu;
889 local_symbols = NULL;
895 /* Compute a small integer hash code for the given name. */
901 register char *p = name;
902 register int total = p[0];
917 /* Ensure result is positive. */
920 total += (1000 << 6);
922 return (total % HASHSIZE);
926 /* Initialize anything that needs initializing when starting to read
927 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
933 free_pendings = NULL;
935 global_symbols = NULL;
936 pending_blocks = NULL;
939 /* Initialize anything that needs initializing when a completely new
940 symbol file is specified (not just adding some symbols from another
941 file, e.g. a shared library). */
949 /* Initializer for this module */
952 _initialize_buildsym ()