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" */
36 /* Ask buildsym.h to define the vars it normally declares `extern'. */
38 #include "buildsym.h" /* Our own declarations */
42 compare_line_numbers
PARAMS ((const void *, const void *));
44 static struct blockvector
*
45 make_blockvector
PARAMS ((struct objfile
*));
48 /* Initial sizes of data structures. These are realloc'd larger if needed,
49 and realloc'd down to the size actually used, when completed. */
51 #define INITIAL_CONTEXT_STACK_SIZE 10
52 #define INITIAL_LINE_VECTOR_LENGTH 1000
55 /* Complaints about the symbols we have encountered. */
57 struct complaint innerblock_complaint
=
58 {"inner block not inside outer block in %s", 0, 0};
60 struct complaint innerblock_anon_complaint
=
61 {"inner block not inside outer block", 0, 0};
63 struct complaint blockvector_complaint
=
64 {"block at 0x%x out of order", 0, 0};
67 /* maintain the lists of symbols and blocks */
69 /* Add a symbol to one of the lists of symbols. */
72 add_symbol_to_list (symbol
, listhead
)
73 struct symbol
*symbol
;
74 struct pending
**listhead
;
76 register struct pending
*link
;
78 /* We keep PENDINGSIZE symbols in each link of the list.
79 If we don't have a link with room in it, add a new link. */
80 if (*listhead
== NULL
|| (*listhead
)->nsyms
== PENDINGSIZE
)
85 free_pendings
= link
->next
;
89 link
= (struct pending
*) xmalloc (sizeof (struct pending
));
92 link
->next
= *listhead
;
97 (*listhead
)->symbol
[(*listhead
)->nsyms
++] = symbol
;
100 /* Find a symbol on a pending list. */
103 find_symbol_in_list (list
, name
, length
)
104 struct pending
*list
;
113 for (j
= list
->nsyms
; --j
>= 0; )
115 pp
= SYMBOL_NAME (list
->symbol
[j
]);
116 if (*pp
== *name
&& strncmp (pp
, name
, length
) == 0 &&
119 return (list
->symbol
[j
]);
127 /* At end of reading syms, or in case of quit,
128 really free as many `struct pending's as we can easily find. */
132 really_free_pendings (foo
)
135 struct pending
*next
, *next1
;
137 struct pending_block
*bnext
, *bnext1
;
140 for (next
= free_pendings
; next
; next
= next1
)
145 free_pendings
= NULL
;
147 #if 0 /* Now we make the links in the symbol_obstack, so don't free them. */
148 for (bnext
= pending_blocks
; bnext
; bnext
= bnext1
)
150 bnext1
= bnext
->next
;
154 pending_blocks
= NULL
;
156 for (next
= file_symbols
; next
!= NULL
; next
= next1
)
163 for (next
= global_symbols
; next
!= NULL
; next
= next1
)
168 global_symbols
= NULL
;
171 /* Take one of the lists of symbols and make a block from it.
172 Keep the order the symbols have in the list (reversed from the input file).
173 Put the block on the list of pending blocks. */
176 finish_block (symbol
, listhead
, old_blocks
, start
, end
, objfile
)
177 struct symbol
*symbol
;
178 struct pending
**listhead
;
179 struct pending_block
*old_blocks
;
180 CORE_ADDR start
, end
;
181 struct objfile
*objfile
;
183 register struct pending
*next
, *next1
;
184 register struct block
*block
;
185 register struct pending_block
*pblock
;
186 struct pending_block
*opblock
;
190 /* Count the length of the list of symbols. */
192 for (next
= *listhead
, i
= 0;
194 i
+= next
->nsyms
, next
= next
->next
)
199 block
= (struct block
*) obstack_alloc (&objfile
-> symbol_obstack
,
200 (sizeof (struct block
) + ((i
- 1) * sizeof (struct symbol
*))));
202 /* Copy the symbols into the block. */
204 BLOCK_NSYMS (block
) = i
;
205 for (next
= *listhead
; next
; next
= next
->next
)
207 for (j
= next
->nsyms
- 1; j
>= 0; j
--)
209 BLOCK_SYM (block
, --i
) = next
->symbol
[j
];
213 BLOCK_START (block
) = start
;
214 BLOCK_END (block
) = end
;
215 /* Superblock filled in when containing block is made */
216 BLOCK_SUPERBLOCK (block
) = NULL
;
217 BLOCK_GCC_COMPILED (block
) = processing_gcc_compilation
;
219 /* Put the block in as the value of the symbol that names it. */
223 SYMBOL_BLOCK_VALUE (symbol
) = block
;
224 BLOCK_FUNCTION (block
) = symbol
;
228 BLOCK_FUNCTION (block
) = NULL
;
231 /* Now "free" the links of the list, and empty the list. */
233 for (next
= *listhead
; next
; next
= next1
)
236 next
->next
= free_pendings
;
237 free_pendings
= next
;
241 /* Install this block as the superblock
242 of all blocks made since the start of this scope
243 that don't have superblocks yet. */
246 for (pblock
= pending_blocks
; pblock
!= old_blocks
; pblock
= pblock
->next
)
248 if (BLOCK_SUPERBLOCK (pblock
->block
) == NULL
)
251 /* Check to be sure the blocks are nested as we receive them.
252 If the compiler/assembler/linker work, this just burns a small
254 if (BLOCK_START (pblock
->block
) < BLOCK_START (block
) ||
255 BLOCK_END (pblock
->block
) > BLOCK_END (block
))
259 complain (&innerblock_complaint
, SYMBOL_NAME (symbol
));
263 complain (&innerblock_anon_complaint
, 0);
265 BLOCK_START (pblock
->block
) = BLOCK_START (block
);
266 BLOCK_END (pblock
->block
) = BLOCK_END (block
);
269 BLOCK_SUPERBLOCK (pblock
->block
) = block
;
274 /* Record this block on the list of all blocks in the file.
275 Put it after opblock, or at the beginning if opblock is 0.
276 This puts the block in the list after all its subblocks. */
278 /* Allocate in the symbol_obstack to save time.
279 It wastes a little space. */
280 pblock
= (struct pending_block
*)
281 obstack_alloc (&objfile
-> symbol_obstack
,
282 sizeof (struct pending_block
));
283 pblock
->block
= block
;
286 pblock
->next
= opblock
->next
;
287 opblock
->next
= pblock
;
291 pblock
->next
= pending_blocks
;
292 pending_blocks
= pblock
;
296 static struct blockvector
*
297 make_blockvector (objfile
)
298 struct objfile
*objfile
;
300 register struct pending_block
*next
;
301 register struct blockvector
*blockvector
;
304 /* Count the length of the list of blocks. */
306 for (next
= pending_blocks
, i
= 0; next
; next
= next
->next
, i
++) {;}
308 blockvector
= (struct blockvector
*)
309 obstack_alloc (&objfile
-> symbol_obstack
,
310 (sizeof (struct blockvector
)
311 + (i
- 1) * sizeof (struct block
*)));
313 /* Copy the blocks into the blockvector.
314 This is done in reverse order, which happens to put
315 the blocks into the proper order (ascending starting address).
316 finish_block has hair to insert each block into the list
317 after its subblocks in order to make sure this is true. */
319 BLOCKVECTOR_NBLOCKS (blockvector
) = i
;
320 for (next
= pending_blocks
; next
; next
= next
->next
)
322 BLOCKVECTOR_BLOCK (blockvector
, --i
) = next
->block
;
325 #if 0 /* Now we make the links in the obstack, so don't free them. */
326 /* Now free the links of the list, and empty the list. */
328 for (next
= pending_blocks
; next
; next
= next1
)
334 pending_blocks
= NULL
;
336 #if 1 /* FIXME, shut this off after a while to speed up symbol reading. */
337 /* Some compilers output blocks in the wrong order, but we depend
338 on their being in the right order so we can binary search.
339 Check the order and moan about it. FIXME. */
340 if (BLOCKVECTOR_NBLOCKS (blockvector
) > 1)
342 for (i
= 1; i
< BLOCKVECTOR_NBLOCKS (blockvector
); i
++)
344 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector
, i
-1))
345 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector
, i
)))
347 complain (&blockvector_complaint
,
348 (char *) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector
, i
)));
354 return (blockvector
);
358 /* Start recording information about source code that came from an included
359 (or otherwise merged-in) source file with a different name. */
362 start_subfile (name
, dirname
)
366 register struct subfile
*subfile
;
368 /* See if this subfile is already known as a subfile of the
369 current main source file. */
371 for (subfile
= subfiles
; subfile
; subfile
= subfile
->next
)
373 if (!strcmp (subfile
->name
, name
))
375 current_subfile
= subfile
;
380 /* This subfile is not known. Add an entry for it.
381 Make an entry for this subfile in the list of all subfiles
382 of the current main source file. */
384 subfile
= (struct subfile
*) xmalloc (sizeof (struct subfile
));
385 subfile
->next
= subfiles
;
387 current_subfile
= subfile
;
389 /* Save its name and compilation directory name */
390 subfile
->name
= strdup (name
);
391 subfile
->dirname
= (dirname
== NULL
) ? NULL
: strdup (dirname
);
393 /* Initialize line-number recording for this subfile. */
394 subfile
->line_vector
= NULL
;
397 /* For stabs readers, the first N_SO symbol is assumed to be the source
398 file name, and the subfile struct is initialized using that assumption.
399 If another N_SO symbol is later seen, immediately following the first
400 one, then the first one is assumed to be the directory name and the
401 second one is really the source file name.
403 So we have to patch up the subfile struct by moving the old name value to
404 dirname and remembering the new name. Some sanity checking is performed
405 to ensure that the state of the subfile struct is reasonable and that the
406 old name we are assuming to be a directory name actually is (by checking
407 for a trailing '/'). */
410 patch_subfile_names (subfile
, name
)
411 struct subfile
*subfile
;
414 if (subfile
!= NULL
&& subfile
->dirname
== NULL
&& subfile
->name
!= NULL
415 && subfile
->name
[strlen(subfile
->name
)-1] == '/')
417 subfile
->dirname
= subfile
->name
;
418 subfile
->name
= strdup (name
);
423 /* Handle the N_BINCL and N_EINCL symbol types
424 that act like N_SOL for switching source files
425 (different subfiles, as we call them) within one object file,
426 but using a stack rather than in an arbitrary order. */
431 register struct subfile_stack
*tem
432 = (struct subfile_stack
*) xmalloc (sizeof (struct subfile_stack
));
434 tem
->next
= subfile_stack
;
436 if (current_subfile
== NULL
|| current_subfile
->name
== NULL
)
440 tem
->name
= current_subfile
->name
;
447 register struct subfile_stack
*link
= subfile_stack
;
454 subfile_stack
= link
->next
;
460 /* Manage the vector of line numbers for each subfile. */
463 record_line (subfile
, line
, pc
)
464 register struct subfile
*subfile
;
468 struct linetable_entry
*e
;
469 /* Ignore the dummy line number in libg.o */
476 /* Make sure line vector exists and is big enough. */
477 if (!subfile
->line_vector
)
479 subfile
->line_vector_length
= INITIAL_LINE_VECTOR_LENGTH
;
480 subfile
->line_vector
= (struct linetable
*)
481 xmalloc (sizeof (struct linetable
)
482 + subfile
->line_vector_length
* sizeof (struct linetable_entry
));
483 subfile
->line_vector
->nitems
= 0;
486 if (subfile
->line_vector
->nitems
+ 1 >= subfile
->line_vector_length
)
488 subfile
->line_vector_length
*= 2;
489 subfile
->line_vector
= (struct linetable
*)
490 xrealloc ((char *) subfile
->line_vector
, (sizeof (struct linetable
)
491 + subfile
->line_vector_length
* sizeof (struct linetable_entry
)));
494 e
= subfile
->line_vector
->item
+ subfile
->line_vector
->nitems
++;
495 e
->line
= line
; e
->pc
= pc
;
499 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
502 compare_line_numbers (ln1p
, ln2p
)
506 return (((struct linetable_entry
*) ln1p
) -> line
-
507 ((struct linetable_entry
*) ln2p
) -> line
);
511 /* Start a new symtab for a new source file.
512 Called, for example, when a stabs symbol of type N_SO is seen, or when
513 a DWARF TAG_compile_unit DIE is seen.
514 It indicates the start of data for one original source file. */
517 start_symtab (name
, dirname
, start_addr
)
520 CORE_ADDR start_addr
;
523 last_source_file
= name
;
524 last_source_start_addr
= start_addr
;
526 global_symbols
= NULL
;
529 /* Context stack is initially empty. Allocate first one with room for
530 10 levels; reuse it forever afterward. */
531 if (context_stack
== NULL
)
533 context_stack_size
= INITIAL_CONTEXT_STACK_SIZE
;
534 context_stack
= (struct context_stack
*)
535 xmalloc (context_stack_size
* sizeof (struct context_stack
));
537 context_stack_depth
= 0;
539 /* Initialize the list of sub source files with one entry
540 for this file (the top-level source file). */
543 current_subfile
= NULL
;
544 start_subfile (name
, dirname
);
547 /* Finish the symbol definitions for one main source file,
548 close off all the lexical contexts for that file
549 (creating struct block's for them), then make the struct symtab
550 for that file and put it in the list of all such.
552 END_ADDR is the address of the end of the file's text. */
555 end_symtab (end_addr
, sort_pending
, sort_linevec
, objfile
)
559 struct objfile
*objfile
;
561 register struct symtab
*symtab
;
562 register struct blockvector
*blockvector
;
563 register struct subfile
*subfile
;
564 register struct context_stack
*cstk
;
565 struct subfile
*nextsub
;
567 /* Finish the lexical context of the last function in the file;
568 pop the context stack. */
570 if (context_stack_depth
> 0)
572 context_stack_depth
--;
573 cstk
= &context_stack
[context_stack_depth
];
574 /* Make a block for the local symbols within. */
575 finish_block (cstk
->name
, &local_symbols
, cstk
->old_blocks
,
576 cstk
->start_addr
, end_addr
, objfile
);
578 /* Debug: if context stack still has something in it,
579 we are in trouble. */
580 if (context_stack_depth
> 0)
586 /* It is unfortunate that in aixcoff, pending blocks might not be ordered
587 in this stage. Especially, blocks for static functions will show up at
588 the end. We need to sort them, so tools like `find_pc_function' and
589 `find_pc_block' can work reliably. */
591 if (sort_pending
&& pending_blocks
)
593 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
597 struct pending_block
*pb
, *pbnext
;
605 /* swap blocks if unordered! */
607 if (BLOCK_START(pb
->block
) < BLOCK_START(pbnext
->block
))
609 struct block
*tmp
= pb
->block
;
610 pb
->block
= pbnext
->block
;
615 pbnext
= pbnext
->next
;
620 /* Cleanup any undefined types that have been left hanging around
621 (this needs to be done before the finish_blocks so that
622 file_symbols is still good).
623 FIXME: Stabs specific. */
624 cleanup_undefined_types ();
625 finish_global_stabs (objfile
);
627 if (pending_blocks
== NULL
628 && file_symbols
== NULL
629 && global_symbols
== NULL
)
631 /* Ignore symtabs that have no functions with real debugging info */
636 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
637 finish_block (0, &file_symbols
, 0, last_source_start_addr
, end_addr
,
639 finish_block (0, &global_symbols
, 0, last_source_start_addr
, end_addr
,
641 blockvector
= make_blockvector (objfile
);
644 #ifdef PROCESS_LINENUMBER_HOOK
645 PROCESS_LINENUMBER_HOOK (); /* Needed for aixcoff. */
648 /* Now create the symtab objects proper, one for each subfile. */
649 /* (The main file is the last one on the chain.) */
651 for (subfile
= subfiles
; subfile
; subfile
= nextsub
)
654 /* If we have blocks of symbols, make a symtab.
655 Otherwise, just ignore this file and any line number info in it. */
659 if (subfile
->line_vector
)
661 /* First, shrink the linetable to make more memory. */
662 linetablesize
= sizeof (struct linetable
) +
663 subfile
->line_vector
->nitems
* sizeof (struct linetable_entry
);
664 subfile
->line_vector
= (struct linetable
*)
665 xrealloc ((char *) subfile
->line_vector
, linetablesize
);
668 qsort (subfile
->line_vector
->item
,
669 subfile
->line_vector
->nitems
,
670 sizeof (struct linetable_entry
), compare_line_numbers
);
673 /* Now, allocate a symbol table. */
674 symtab
= allocate_symtab (subfile
->name
, objfile
);
676 /* Fill in its components. */
677 symtab
->blockvector
= blockvector
;
678 if (subfile
->line_vector
)
680 /* Reallocate the line table on the symbol obstack */
681 symtab
->linetable
= (struct linetable
*)
682 obstack_alloc (&objfile
-> symbol_obstack
, linetablesize
);
683 memcpy (symtab
->linetable
, subfile
->line_vector
, linetablesize
);
687 symtab
->linetable
= NULL
;
689 if (subfile
->dirname
)
691 /* Reallocate the dirname on the symbol obstack */
692 symtab
->dirname
= (char *)
693 obstack_alloc (&objfile
-> symbol_obstack
,
694 strlen (subfile
-> dirname
) + 1);
695 strcpy (symtab
->dirname
, subfile
->dirname
);
699 symtab
->dirname
= NULL
;
701 symtab
->free_code
= free_linetable
;
702 symtab
->free_ptr
= NULL
;
704 #ifdef IBM6000_TARGET
705 /* In case we need to duplicate symbol tables (to represent include
706 files), and in case our system needs relocation, we want to
707 relocate the main symbol table node only (for the main file,
708 not for the include files). */
710 symtab
->nonreloc
= TRUE
;
713 if (subfile
->name
!= NULL
)
715 free ((PTR
) subfile
->name
);
717 if (subfile
->dirname
!= NULL
)
719 free ((PTR
) subfile
->dirname
);
721 if (subfile
->line_vector
!= NULL
)
723 free ((PTR
) subfile
->line_vector
);
726 nextsub
= subfile
->next
;
730 #ifdef IBM6000_TARGET
731 /* all include symbol tables are non-relocatable, except the main source
735 symtab
->nonreloc
= FALSE
;
739 last_source_file
= NULL
;
740 current_subfile
= NULL
;
746 /* Push a context block. Args are an identifying nesting level (checkable
747 when you pop it), and the starting PC address of this context. */
749 struct context_stack
*
750 push_context (desc
, valu
)
754 register struct context_stack
*new;
756 if (context_stack_depth
== context_stack_size
)
758 context_stack_size
*= 2;
759 context_stack
= (struct context_stack
*)
760 xrealloc ((char *) context_stack
,
761 (context_stack_size
* sizeof (struct context_stack
)));
764 new = &context_stack
[context_stack_depth
++];
766 new->locals
= local_symbols
;
767 new->old_blocks
= pending_blocks
;
768 new->start_addr
= valu
;
771 local_symbols
= NULL
;
777 /* Initialize anything that needs initializing when starting to read
778 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
784 free_pendings
= NULL
;
786 global_symbols
= NULL
;
787 pending_blocks
= NULL
;
790 /* Initialize anything that needs initializing when a completely new
791 symbol file is specified (not just adding some symbols from another
792 file, e.g. a shared library). */
800 /* Initializer for this module */
803 _initialize_buildsym ()