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 */
43 compare_line_numbers
PARAMS ((const void *, const void *));
45 static struct blockvector
*
46 make_blockvector
PARAMS ((struct objfile
*));
49 /* Initial sizes of data structures. These are realloc'd larger if needed,
50 and realloc'd down to the size actually used, when completed. */
52 #define INITIAL_CONTEXT_STACK_SIZE 10
53 #define INITIAL_LINE_VECTOR_LENGTH 1000
56 /* Complaints about the symbols we have encountered. */
58 struct complaint innerblock_complaint
=
59 {"inner block not inside outer block in %s", 0, 0};
61 struct complaint innerblock_anon_complaint
=
62 {"inner block not inside outer block", 0, 0};
64 struct complaint blockvector_complaint
=
65 {"block at 0x%x out of order", 0, 0};
68 /* maintain the lists of symbols and blocks */
70 /* Add a symbol to one of the lists of symbols. */
73 add_symbol_to_list (symbol
, listhead
)
74 struct symbol
*symbol
;
75 struct pending
**listhead
;
77 register struct pending
*link
;
79 /* We keep PENDINGSIZE symbols in each link of the list.
80 If we don't have a link with room in it, add a new link. */
81 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
86 free_pendings
= link
->next
;
90 link
= (struct pending
*) xmalloc (sizeof (struct pending
));
93 link
->next
= *listhead
;
98 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
101 /* Find a symbol on a pending list. */
104 find_symbol_in_list (list
, name
, length
)
105 struct pending
*list
;
114 for (j
= list
->nsyms
; --j
>= 0; )
116 pp
= SYMBOL_NAME (list
->symbol
[j
]);
117 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0 &&
120 return (list
->symbol
[j
]);
128 /* At end of reading syms, or in case of quit,
129 really free as many `struct pending's as we can easily find. */
133 really_free_pendings (foo
)
136 struct pending
*next
, *next1
;
138 struct pending_block
*bnext
, *bnext1
;
141 for (next
= free_pendings
; next
; next
= next1
)
146 free_pendings
= NULL
;
148 #if 0 /* Now we make the links in the symbol_obstack, so don't free them. */
149 for (bnext
= pending_blocks
; bnext
; bnext
= bnext1
)
151 bnext1
= bnext
->next
;
155 pending_blocks
= NULL
;
157 for (next
= file_symbols
; next
!= NULL
; next
= next1
)
164 for (next
= global_symbols
; next
!= NULL
; next
= next1
)
169 global_symbols
= NULL
;
172 /* Take one of the lists of symbols and make a block from it.
173 Keep the order the symbols have in the list (reversed from the input file).
174 Put the block on the list of pending blocks. */
177 finish_block (symbol
, listhead
, old_blocks
, start
, end
, objfile
)
178 struct symbol
*symbol
;
179 struct pending
**listhead
;
180 struct pending_block
*old_blocks
;
181 CORE_ADDR start
, end
;
182 struct objfile
*objfile
;
184 register struct pending
*next
, *next1
;
185 register struct block
*block
;
186 register struct pending_block
*pblock
;
187 struct pending_block
*opblock
;
191 /* Count the length of the list of symbols. */
193 for (next
= *listhead
, i
= 0;
195 i
+= next
->nsyms
, next
= next
->next
)
200 block
= (struct block
*) obstack_alloc (&objfile
-> symbol_obstack
,
201 (sizeof (struct block
) + ((i
- 1) * sizeof (struct symbol
*))));
203 /* Copy the symbols into the block. */
205 BLOCK_NSYMS (block
) = i
;
206 for (next
= *listhead
; next
; next
= next
->next
)
208 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
210 BLOCK_SYM (block
, --i
) = next
->symbol
[j
];
214 BLOCK_START (block
) = start
;
215 BLOCK_END (block
) = end
;
216 /* Superblock filled in when containing block is made */
217 BLOCK_SUPERBLOCK (block
) = NULL
;
218 BLOCK_GCC_COMPILED (block
) = processing_gcc_compilation
;
220 /* Put the block in as the value of the symbol that names it. */
224 SYMBOL_BLOCK_VALUE (symbol
) = block
;
225 BLOCK_FUNCTION (block
) = symbol
;
229 BLOCK_FUNCTION (block
) = NULL
;
232 /* Now "free" the links of the list, and empty the list. */
234 for (next
= *listhead
; next
; next
= next1
)
237 next
->next
= free_pendings
;
238 free_pendings
= next
;
242 /* Install this block as the superblock
243 of all blocks made since the start of this scope
244 that don't have superblocks yet. */
247 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
249 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
252 /* Check to be sure the blocks are nested as we receive them.
253 If the compiler/assembler/linker work, this just burns a small
255 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
) ||
256 BLOCK_END (pblock
->block
) > BLOCK_END (block
))
260 complain (&innerblock_complaint
, SYMBOL_NAME (symbol
));
264 complain (&innerblock_anon_complaint
);
266 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
267 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
270 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
275 /* Record this block on the list of all blocks in the file.
276 Put it after opblock, or at the beginning if opblock is 0.
277 This puts the block in the list after all its subblocks. */
279 /* Allocate in the symbol_obstack to save time.
280 It wastes a little space. */
281 pblock
= (struct pending_block
*)
282 obstack_alloc (&objfile
-> symbol_obstack
,
283 sizeof (struct pending_block
));
284 pblock
->block
= block
;
287 pblock
->next
= opblock
->next
;
288 opblock
->next
= pblock
;
292 pblock
->next
= pending_blocks
;
293 pending_blocks
= pblock
;
297 static struct blockvector
*
298 make_blockvector (objfile
)
299 struct objfile
*objfile
;
301 register struct pending_block
*next
;
302 register struct blockvector
*blockvector
;
305 /* Count the length of the list of blocks. */
307 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++) {;}
309 blockvector
= (struct blockvector
*)
310 obstack_alloc (&objfile
-> symbol_obstack
,
311 (sizeof (struct blockvector
)
312 + (i
- 1) * sizeof (struct block
*)));
314 /* Copy the blocks into the blockvector.
315 This is done in reverse order, which happens to put
316 the blocks into the proper order (ascending starting address).
317 finish_block has hair to insert each block into the list
318 after its subblocks in order to make sure this is true. */
320 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
321 for (next
= pending_blocks
; next
; next
= next
->next
)
323 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
326 #if 0 /* Now we make the links in the obstack, so don't free them. */
327 /* Now free the links of the list, and empty the list. */
329 for (next
= pending_blocks
; next
; next
= next1
)
335 pending_blocks
= NULL
;
337 #if 1 /* FIXME, shut this off after a while to speed up symbol reading. */
338 /* Some compilers output blocks in the wrong order, but we depend
339 on their being in the right order so we can binary search.
340 Check the order and moan about it. FIXME. */
341 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
343 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
345 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector
, i
-1))
346 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector
, i
)))
348 complain (&blockvector_complaint
,
349 BLOCK_START(BLOCKVECTOR_BLOCK (blockvector
, i
)));
355 return (blockvector
);
359 /* Start recording information about source code that came from an included
360 (or otherwise merged-in) source file with a different name. */
363 start_subfile (name
, dirname
)
367 register struct subfile
*subfile
;
369 /* See if this subfile is already known as a subfile of the
370 current main source file. */
372 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
374 if (!strcmp (subfile
->name
, name
))
376 current_subfile
= subfile
;
381 /* This subfile is not known. Add an entry for it.
382 Make an entry for this subfile in the list of all subfiles
383 of the current main source file. */
385 subfile
= (struct subfile
*) xmalloc (sizeof (struct subfile
));
386 subfile
->next
= subfiles
;
388 current_subfile
= subfile
;
390 /* Save its name and compilation directory name */
391 subfile
->name
= strdup (name
);
392 subfile
->dirname
= (dirname
== NULL
) ? NULL
: strdup (dirname
);
394 /* Initialize line-number recording for this subfile. */
395 subfile
->line_vector
= NULL
;
398 /* For stabs readers, the first N_SO symbol is assumed to be the source
399 file name, and the subfile struct is initialized using that assumption.
400 If another N_SO symbol is later seen, immediately following the first
401 one, then the first one is assumed to be the directory name and the
402 second one is really the source file name.
404 So we have to patch up the subfile struct by moving the old name value to
405 dirname and remembering the new name. Some sanity checking is performed
406 to ensure that the state of the subfile struct is reasonable and that the
407 old name we are assuming to be a directory name actually is (by checking
408 for a trailing '/'). */
411 patch_subfile_names (subfile
, name
)
412 struct subfile
*subfile
;
415 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
416 && subfile
->name
[strlen(subfile
->name
)-1] == '/')
418 subfile
->dirname
= subfile
->name
;
419 subfile
->name
= strdup (name
);
424 /* Handle the N_BINCL and N_EINCL symbol types
425 that act like N_SOL for switching source files
426 (different subfiles, as we call them) within one object file,
427 but using a stack rather than in an arbitrary order. */
432 register struct subfile_stack
*tem
433 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
435 tem
->next
= subfile_stack
;
437 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
441 tem
->name
= current_subfile
->name
;
448 register struct subfile_stack
*link
= subfile_stack
;
455 subfile_stack
= link
->next
;
461 /* Manage the vector of line numbers for each subfile. */
464 record_line (subfile
, line
, pc
)
465 register struct subfile
*subfile
;
469 struct linetable_entry
*e
;
470 /* Ignore the dummy line number in libg.o */
477 /* Make sure line vector exists and is big enough. */
478 if (!subfile
->line_vector
)
480 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
481 subfile
->line_vector
= (struct linetable
*)
482 xmalloc (sizeof (struct linetable
)
483 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
484 subfile
->line_vector
->nitems
= 0;
487 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
489 subfile
->line_vector_length
*= 2;
490 subfile
->line_vector
= (struct linetable
*)
491 xrealloc ((char *) subfile
->line_vector
, (sizeof (struct linetable
)
492 + subfile
->line_vector_length
* sizeof (struct linetable_entry
)));
495 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
496 e
->line
= line
; e
->pc
= pc
;
500 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
503 compare_line_numbers (ln1p
, ln2p
)
507 return (((struct linetable_entry
*) ln1p
) -> line
-
508 ((struct linetable_entry
*) ln2p
) -> line
);
512 /* Start a new symtab for a new source file.
513 Called, for example, when a stabs symbol of type N_SO is seen, or when
514 a DWARF TAG_compile_unit DIE is seen.
515 It indicates the start of data for one original source file. */
518 start_symtab (name
, dirname
, start_addr
)
521 CORE_ADDR start_addr
;
524 last_source_file
= name
;
525 last_source_start_addr
= start_addr
;
527 global_symbols
= NULL
;
530 /* Context stack is initially empty. Allocate first one with room for
531 10 levels; reuse it forever afterward. */
532 if (context_stack
== NULL
)
534 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
535 context_stack
= (struct context_stack
*)
536 xmalloc (context_stack_size
* sizeof (struct context_stack
));
538 context_stack_depth
= 0;
540 /* Initialize the list of sub source files with one entry
541 for this file (the top-level source file). */
544 current_subfile
= NULL
;
545 start_subfile (name
, dirname
);
548 /* Finish the symbol definitions for one main source file,
549 close off all the lexical contexts for that file
550 (creating struct block's for them), then make the struct symtab
551 for that file and put it in the list of all such.
553 END_ADDR is the address of the end of the file's text.
555 Note that it is possible for end_symtab() to return NULL. In particular,
556 for the DWARF case at least, it will return NULL when it finds a
557 compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This
558 can happen when we link in an object file that was compiled from an empty
559 source file. Returning NULL is probably not the correct thing to do,
560 because then gdb will never know about this empty file (FIXME). */
563 end_symtab (end_addr
, sort_pending
, sort_linevec
, objfile
)
567 struct objfile
*objfile
;
569 register struct symtab
*symtab
;
570 register struct blockvector
*blockvector
;
571 register struct subfile
*subfile
;
572 register struct context_stack
*cstk
;
573 struct subfile
*nextsub
;
575 /* Finish the lexical context of the last function in the file;
576 pop the context stack. */
578 if (context_stack_depth
> 0)
580 context_stack_depth
--;
581 cstk
= &context_stack
[context_stack_depth
];
582 /* Make a block for the local symbols within. */
583 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
584 cstk
->start_addr
, end_addr
, objfile
);
586 /* Debug: if context stack still has something in it,
587 we are in trouble. */
588 if (context_stack_depth
> 0)
594 /* It is unfortunate that in xcoff, pending blocks might not be ordered
595 in this stage. Especially, blocks for static functions will show up at
596 the end. We need to sort them, so tools like `find_pc_function' and
597 `find_pc_block' can work reliably. */
599 if (sort_pending
&& pending_blocks
)
601 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
605 struct pending_block
*pb
, *pbnext
;
613 /* swap blocks if unordered! */
615 if (BLOCK_START(pb
->block
) < BLOCK_START(pbnext
->block
))
617 struct block
*tmp
= pb
->block
;
618 pb
->block
= pbnext
->block
;
623 pbnext
= pbnext
->next
;
628 /* Cleanup any undefined types that have been left hanging around
629 (this needs to be done before the finish_blocks so that
630 file_symbols is still good).
631 FIXME: Stabs specific. */
632 cleanup_undefined_types ();
633 finish_global_stabs (objfile
);
635 if (pending_blocks
== NULL
636 && file_symbols
== NULL
637 && global_symbols
== NULL
)
639 /* Ignore symtabs that have no functions with real debugging info */
644 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
645 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
647 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
649 blockvector
= make_blockvector (objfile
);
652 #ifdef PROCESS_LINENUMBER_HOOK
653 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
656 /* Now create the symtab objects proper, one for each subfile. */
657 /* (The main file is the last one on the chain.) */
659 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
662 /* If we have blocks of symbols, make a symtab.
663 Otherwise, just ignore this file and any line number info in it. */
667 if (subfile
->line_vector
)
669 /* First, shrink the linetable to make more memory. */
670 linetablesize
= sizeof (struct linetable
) +
671 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
672 subfile
->line_vector
= (struct linetable
*)
673 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
676 qsort (subfile
->line_vector
->item
,
677 subfile
->line_vector
->nitems
,
678 sizeof (struct linetable_entry
), compare_line_numbers
);
681 /* Now, allocate a symbol table. */
682 symtab
= allocate_symtab (subfile
->name
, objfile
);
684 /* Fill in its components. */
685 symtab
->blockvector
= blockvector
;
686 if (subfile
->line_vector
)
688 /* Reallocate the line table on the symbol obstack */
689 symtab
->linetable
= (struct linetable
*)
690 obstack_alloc (&objfile
-> symbol_obstack
, linetablesize
);
691 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
695 symtab
->linetable
= NULL
;
697 if (subfile
->dirname
)
699 /* Reallocate the dirname on the symbol obstack */
700 symtab
->dirname
= (char *)
701 obstack_alloc (&objfile
-> symbol_obstack
,
702 strlen (subfile
-> dirname
) + 1);
703 strcpy (symtab
->dirname
, subfile
->dirname
);
707 symtab
->dirname
= NULL
;
709 symtab
->free_code
= free_linetable
;
710 symtab
->free_ptr
= NULL
;
712 #ifdef IBM6000_TARGET
713 /* In case we need to duplicate symbol tables (to represent include
714 files), and in case our system needs relocation, we want to
715 relocate the main symbol table node only (for the main file,
716 not for the include files). */
718 symtab
->nonreloc
= TRUE
;
721 if (subfile
->name
!= NULL
)
723 free ((PTR
) subfile
->name
);
725 if (subfile
->dirname
!= NULL
)
727 free ((PTR
) subfile
->dirname
);
729 if (subfile
->line_vector
!= NULL
)
731 free ((PTR
) subfile
->line_vector
);
734 nextsub
= subfile
->next
;
738 #ifdef IBM6000_TARGET
739 /* all include symbol tables are non-relocatable, except the main source
743 symtab
->nonreloc
= FALSE
;
747 last_source_file
= NULL
;
748 current_subfile
= NULL
;
754 /* Push a context block. Args are an identifying nesting level (checkable
755 when you pop it), and the starting PC address of this context. */
757 struct context_stack
*
758 push_context (desc
, valu
)
762 register struct context_stack
*new;
764 if (context_stack_depth
== context_stack_size
)
766 context_stack_size
*= 2;
767 context_stack
= (struct context_stack
*)
768 xrealloc ((char *) context_stack
,
769 (context_stack_size
* sizeof (struct context_stack
)));
772 new = &context_stack
[context_stack_depth
++];
774 new->locals
= local_symbols
;
775 new->old_blocks
= pending_blocks
;
776 new->start_addr
= valu
;
779 local_symbols
= NULL
;
785 /* Initialize anything that needs initializing when starting to read
786 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
792 free_pendings
= NULL
;
794 global_symbols
= NULL
;
795 pending_blocks
= NULL
;
798 /* Initialize anything that needs initializing when a completely new
799 symbol file is specified (not just adding some symbols from another
800 file, e.g. a shared library). */
808 /* Initializer for this module */
811 _initialize_buildsym ()