208d207760725dac13525c7e3bdd450f4b87b5ed
[deliverable/binutils-gdb.git] / gdb / buildsym.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
20
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.
24
25 Routines to support specific debugging information formats (stabs,
26 DWARF, etc) belong somewhere else. */
27
28 #include "defs.h"
29 #include "bfd.h"
30 #include "obstack.h"
31 #include "symtab.h"
32 #include "symfile.h" /* Needed for "struct complaint" */
33 #include "objfiles.h"
34 #include <string.h>
35
36 /* Ask buildsym.h to define the vars it normally declares `extern'. */
37 #define EXTERN /**/
38 #include "buildsym.h" /* Our own declarations */
39 #undef EXTERN
40
41 static int
42 compare_line_numbers PARAMS ((const void *, const void *));
43
44 static struct blockvector *
45 make_blockvector PARAMS ((struct objfile *));
46
47 \f
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. */
50
51 #define INITIAL_CONTEXT_STACK_SIZE 10
52 #define INITIAL_LINE_VECTOR_LENGTH 1000
53
54 \f
55 /* Complaints about the symbols we have encountered. */
56
57 struct complaint innerblock_complaint =
58 {"inner block not inside outer block in %s", 0, 0};
59
60 struct complaint innerblock_anon_complaint =
61 {"inner block not inside outer block", 0, 0};
62
63 struct complaint blockvector_complaint =
64 {"block at 0x%x out of order", 0, 0};
65
66 \f
67 /* maintain the lists of symbols and blocks */
68
69 /* Add a symbol to one of the lists of symbols. */
70
71 void
72 add_symbol_to_list (symbol, listhead)
73 struct symbol *symbol;
74 struct pending **listhead;
75 {
76 register struct pending *link;
77
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)
81 {
82 if (free_pendings)
83 {
84 link = free_pendings;
85 free_pendings = link->next;
86 }
87 else
88 {
89 link = (struct pending *) xmalloc (sizeof (struct pending));
90 }
91
92 link->next = *listhead;
93 *listhead = link;
94 link->nsyms = 0;
95 }
96
97 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
98 }
99
100 /* Find a symbol on a pending list. */
101
102 struct symbol *
103 find_symbol_in_list (list, name, length)
104 struct pending *list;
105 char *name;
106 int length;
107 {
108 int j;
109 char *pp;
110
111 while (list != NULL)
112 {
113 for (j = list->nsyms; --j >= 0; )
114 {
115 pp = SYMBOL_NAME (list->symbol[j]);
116 if (*pp == *name && strncmp (pp, name, length) == 0 &&
117 pp[length] == '\0')
118 {
119 return (list->symbol[j]);
120 }
121 }
122 list = list->next;
123 }
124 return (NULL);
125 }
126
127 /* At end of reading syms, or in case of quit,
128 really free as many `struct pending's as we can easily find. */
129
130 /* ARGSUSED */
131 void
132 really_free_pendings (foo)
133 int foo;
134 {
135 struct pending *next, *next1;
136 #if 0
137 struct pending_block *bnext, *bnext1;
138 #endif
139
140 for (next = free_pendings; next; next = next1)
141 {
142 next1 = next->next;
143 free ((PTR)next);
144 }
145 free_pendings = NULL;
146
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)
149 {
150 bnext1 = bnext->next;
151 free ((PTR)bnext);
152 }
153 #endif
154 pending_blocks = NULL;
155
156 for (next = file_symbols; next != NULL; next = next1)
157 {
158 next1 = next->next;
159 free ((PTR)next);
160 }
161 file_symbols = NULL;
162
163 for (next = global_symbols; next != NULL; next = next1)
164 {
165 next1 = next->next;
166 free ((PTR)next);
167 }
168 global_symbols = NULL;
169 }
170
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. */
174
175 void
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;
182 {
183 register struct pending *next, *next1;
184 register struct block *block;
185 register struct pending_block *pblock;
186 struct pending_block *opblock;
187 register int i;
188 register int j;
189
190 /* Count the length of the list of symbols. */
191
192 for (next = *listhead, i = 0;
193 next;
194 i += next->nsyms, next = next->next)
195 {
196 /*EMPTY*/;
197 }
198
199 block = (struct block *) obstack_alloc (&objfile -> symbol_obstack,
200 (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *))));
201
202 /* Copy the symbols into the block. */
203
204 BLOCK_NSYMS (block) = i;
205 for (next = *listhead; next; next = next->next)
206 {
207 for (j = next->nsyms - 1; j >= 0; j--)
208 {
209 BLOCK_SYM (block, --i) = next->symbol[j];
210 }
211 }
212
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;
218
219 /* Put the block in as the value of the symbol that names it. */
220
221 if (symbol)
222 {
223 SYMBOL_BLOCK_VALUE (symbol) = block;
224 BLOCK_FUNCTION (block) = symbol;
225 }
226 else
227 {
228 BLOCK_FUNCTION (block) = NULL;
229 }
230
231 /* Now "free" the links of the list, and empty the list. */
232
233 for (next = *listhead; next; next = next1)
234 {
235 next1 = next->next;
236 next->next = free_pendings;
237 free_pendings = next;
238 }
239 *listhead = NULL;
240
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. */
244
245 opblock = NULL;
246 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
247 {
248 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
249 {
250 #if 1
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
253 amount of time. */
254 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
255 BLOCK_END (pblock->block) > BLOCK_END (block))
256 {
257 if (symbol)
258 {
259 complain (&innerblock_complaint, SYMBOL_NAME (symbol));
260 }
261 else
262 {
263 complain (&innerblock_anon_complaint, 0);
264 }
265 BLOCK_START (pblock->block) = BLOCK_START (block);
266 BLOCK_END (pblock->block) = BLOCK_END (block);
267 }
268 #endif
269 BLOCK_SUPERBLOCK (pblock->block) = block;
270 }
271 opblock = pblock;
272 }
273
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. */
277
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;
284 if (opblock)
285 {
286 pblock->next = opblock->next;
287 opblock->next = pblock;
288 }
289 else
290 {
291 pblock->next = pending_blocks;
292 pending_blocks = pblock;
293 }
294 }
295
296 static struct blockvector *
297 make_blockvector (objfile)
298 struct objfile *objfile;
299 {
300 register struct pending_block *next;
301 register struct blockvector *blockvector;
302 register int i;
303
304 /* Count the length of the list of blocks. */
305
306 for (next = pending_blocks, i = 0; next; next = next->next, i++) {;}
307
308 blockvector = (struct blockvector *)
309 obstack_alloc (&objfile -> symbol_obstack,
310 (sizeof (struct blockvector)
311 + (i - 1) * sizeof (struct block *)));
312
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. */
318
319 BLOCKVECTOR_NBLOCKS (blockvector) = i;
320 for (next = pending_blocks; next; next = next->next)
321 {
322 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
323 }
324
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. */
327
328 for (next = pending_blocks; next; next = next1)
329 {
330 next1 = next->next;
331 free (next);
332 }
333 #endif
334 pending_blocks = NULL;
335
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)
341 {
342 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
343 {
344 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1))
345 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)))
346 {
347 complain (&blockvector_complaint,
348 (char *) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)));
349 }
350 }
351 }
352 #endif
353
354 return (blockvector);
355 }
356
357 \f
358 /* Start recording information about source code that came from an included
359 (or otherwise merged-in) source file with a different name. */
360
361 void
362 start_subfile (name, dirname)
363 char *name;
364 char *dirname;
365 {
366 register struct subfile *subfile;
367
368 /* See if this subfile is already known as a subfile of the
369 current main source file. */
370
371 for (subfile = subfiles; subfile; subfile = subfile->next)
372 {
373 if (!strcmp (subfile->name, name))
374 {
375 current_subfile = subfile;
376 return;
377 }
378 }
379
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. */
383
384 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
385 subfile->next = subfiles;
386 subfiles = subfile;
387 current_subfile = subfile;
388
389 /* Save its name and compilation directory name */
390 subfile->name = strdup (name);
391 if (dirname == NULL)
392 {
393 subfile->dirname = NULL;
394 }
395 else
396 {
397 subfile->dirname = strdup (dirname);
398 }
399
400 /* Initialize line-number recording for this subfile. */
401 subfile->line_vector = NULL;
402 }
403
404 \f
405 /* Handle the N_BINCL and N_EINCL symbol types
406 that act like N_SOL for switching source files
407 (different subfiles, as we call them) within one object file,
408 but using a stack rather than in an arbitrary order. */
409
410 void
411 push_subfile ()
412 {
413 register struct subfile_stack *tem
414 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
415
416 tem->next = subfile_stack;
417 subfile_stack = tem;
418 if (current_subfile == NULL || current_subfile->name == NULL)
419 {
420 abort ();
421 }
422 tem->name = current_subfile->name;
423 }
424
425 char *
426 pop_subfile ()
427 {
428 register char *name;
429 register struct subfile_stack *link = subfile_stack;
430
431 if (link == NULL)
432 {
433 abort ();
434 }
435 name = link->name;
436 subfile_stack = link->next;
437 free ((PTR)link);
438 return (name);
439 }
440
441 \f
442 /* Manage the vector of line numbers for each subfile. */
443
444 void
445 record_line (subfile, line, pc)
446 register struct subfile *subfile;
447 int line;
448 CORE_ADDR pc;
449 {
450 struct linetable_entry *e;
451 /* Ignore the dummy line number in libg.o */
452
453 if (line == 0xffff)
454 {
455 return;
456 }
457
458 /* Make sure line vector exists and is big enough. */
459 if (!subfile->line_vector)
460 {
461 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
462 subfile->line_vector = (struct linetable *)
463 xmalloc (sizeof (struct linetable)
464 + subfile->line_vector_length * sizeof (struct linetable_entry));
465 subfile->line_vector->nitems = 0;
466 }
467
468 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
469 {
470 subfile->line_vector_length *= 2;
471 subfile->line_vector = (struct linetable *)
472 xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable)
473 + subfile->line_vector_length * sizeof (struct linetable_entry)));
474 }
475
476 e = subfile->line_vector->item + subfile->line_vector->nitems++;
477 e->line = line; e->pc = pc;
478 }
479
480
481 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
482
483 static int
484 compare_line_numbers (ln1p, ln2p)
485 const PTR ln1p;
486 const PTR ln2p;
487 {
488 return (((struct linetable_entry *) ln1p) -> line -
489 ((struct linetable_entry *) ln2p) -> line);
490 }
491
492 \f
493 /* Start a new symtab for a new source file.
494 Called, for example, when a stabs symbol of type N_SO is seen, or when
495 a DWARF TAG_compile_unit DIE is seen.
496 It indicates the start of data for one original source file. */
497
498 void
499 start_symtab (name, dirname, start_addr)
500 char *name;
501 char *dirname;
502 CORE_ADDR start_addr;
503 {
504
505 last_source_file = name;
506 last_source_start_addr = start_addr;
507 file_symbols = NULL;
508 global_symbols = NULL;
509 within_function = 0;
510
511 /* Context stack is initially empty. Allocate first one with room for
512 10 levels; reuse it forever afterward. */
513 if (context_stack == NULL)
514 {
515 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
516 context_stack = (struct context_stack *)
517 xmalloc (context_stack_size * sizeof (struct context_stack));
518 }
519 context_stack_depth = 0;
520
521 /* Initialize the list of sub source files with one entry
522 for this file (the top-level source file). */
523
524 subfiles = NULL;
525 current_subfile = NULL;
526 start_subfile (name, dirname);
527 }
528
529 /* Finish the symbol definitions for one main source file,
530 close off all the lexical contexts for that file
531 (creating struct block's for them), then make the struct symtab
532 for that file and put it in the list of all such.
533
534 END_ADDR is the address of the end of the file's text. */
535
536 struct symtab *
537 end_symtab (end_addr, sort_pending, sort_linevec, objfile)
538 CORE_ADDR end_addr;
539 int sort_pending;
540 int sort_linevec;
541 struct objfile *objfile;
542 {
543 register struct symtab *symtab;
544 register struct blockvector *blockvector;
545 register struct subfile *subfile;
546 register struct context_stack *cstk;
547 struct subfile *nextsub;
548
549 /* Finish the lexical context of the last function in the file;
550 pop the context stack. */
551
552 if (context_stack_depth > 0)
553 {
554 context_stack_depth--;
555 cstk = &context_stack[context_stack_depth];
556 /* Make a block for the local symbols within. */
557 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
558 cstk->start_addr, end_addr, objfile);
559
560 /* Debug: if context stack still has something in it,
561 we are in trouble. */
562 if (context_stack_depth > 0)
563 {
564 abort ();
565 }
566 }
567
568 /* It is unfortunate that in aixcoff, pending blocks might not be ordered
569 in this stage. Especially, blocks for static functions will show up at
570 the end. We need to sort them, so tools like `find_pc_function' and
571 `find_pc_block' can work reliably. */
572
573 if (sort_pending && pending_blocks)
574 {
575 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
576 int swapped;
577 do
578 {
579 struct pending_block *pb, *pbnext;
580
581 pb = pending_blocks;
582 pbnext = pb->next;
583 swapped = 0;
584
585 while (pbnext)
586 {
587 /* swap blocks if unordered! */
588
589 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block))
590 {
591 struct block *tmp = pb->block;
592 pb->block = pbnext->block;
593 pbnext->block = tmp;
594 swapped = 1;
595 }
596 pb = pbnext;
597 pbnext = pbnext->next;
598 }
599 } while (swapped);
600 }
601
602 /* Cleanup any undefined types that have been left hanging around
603 (this needs to be done before the finish_blocks so that
604 file_symbols is still good).
605 FIXME: Stabs specific. */
606 cleanup_undefined_types ();
607 finish_global_stabs (objfile);
608
609 if (pending_blocks == NULL
610 && file_symbols == NULL
611 && global_symbols == NULL)
612 {
613 /* Ignore symtabs that have no functions with real debugging info */
614 blockvector = NULL;
615 }
616 else
617 {
618 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
619 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
620 objfile);
621 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
622 objfile);
623 blockvector = make_blockvector (objfile);
624 }
625
626 #ifdef PROCESS_LINENUMBER_HOOK
627 PROCESS_LINENUMBER_HOOK (); /* Needed for aixcoff. */
628 #endif
629
630 /* Now create the symtab objects proper, one for each subfile. */
631 /* (The main file is the last one on the chain.) */
632
633 for (subfile = subfiles; subfile; subfile = nextsub)
634 {
635 int linetablesize;
636 /* If we have blocks of symbols, make a symtab.
637 Otherwise, just ignore this file and any line number info in it. */
638 symtab = NULL;
639 if (blockvector)
640 {
641 if (subfile->line_vector)
642 {
643 /* First, shrink the linetable to make more memory. */
644 linetablesize = sizeof (struct linetable) +
645 subfile->line_vector->nitems * sizeof (struct linetable_entry);
646 subfile->line_vector = (struct linetable *)
647 xrealloc ((char *) subfile->line_vector, linetablesize);
648
649 if (sort_linevec)
650 qsort (subfile->line_vector->item,
651 subfile->line_vector->nitems,
652 sizeof (struct linetable_entry), compare_line_numbers);
653 }
654
655 /* Now, allocate a symbol table. */
656 symtab = allocate_symtab (subfile->name, objfile);
657
658 /* Fill in its components. */
659 symtab->blockvector = blockvector;
660 if (subfile->line_vector)
661 {
662 /* Reallocate the line table on the symbol obstack */
663 symtab->linetable = (struct linetable *)
664 obstack_alloc (&objfile -> symbol_obstack, linetablesize);
665 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
666 }
667 else
668 {
669 symtab->linetable = NULL;
670 }
671 if (subfile->dirname)
672 {
673 /* Reallocate the dirname on the symbol obstack */
674 symtab->dirname = (char *)
675 obstack_alloc (&objfile -> symbol_obstack,
676 strlen (subfile -> dirname) + 1);
677 strcpy (symtab->dirname, subfile->dirname);
678 }
679 else
680 {
681 symtab->dirname = NULL;
682 }
683 symtab->free_code = free_linetable;
684 symtab->free_ptr = NULL;
685
686 #ifdef IBM6000_TARGET
687 /* In case we need to duplicate symbol tables (to represent include
688 files), and in case our system needs relocation, we want to
689 relocate the main symbol table node only (for the main file,
690 not for the include files). */
691
692 symtab->nonreloc = TRUE;
693 #endif
694 }
695 if (subfile->line_vector)
696 {
697 free ((PTR)subfile->line_vector);
698 }
699
700 nextsub = subfile->next;
701 free ((PTR)subfile);
702 }
703
704 #ifdef IBM6000_TARGET
705 /* all include symbol tables are non-relocatable, except the main source
706 file's. */
707 if (symtab)
708 {
709 symtab->nonreloc = FALSE;
710 }
711 #endif
712
713 last_source_file = NULL;
714 current_subfile = NULL;
715
716 return (symtab);
717 }
718
719
720 /* Push a context block. Args are an identifying nesting level (checkable
721 when you pop it), and the starting PC address of this context. */
722
723 struct context_stack *
724 push_context (desc, valu)
725 int desc;
726 CORE_ADDR valu;
727 {
728 register struct context_stack *new;
729
730 if (context_stack_depth == context_stack_size)
731 {
732 context_stack_size *= 2;
733 context_stack = (struct context_stack *)
734 xrealloc ((char *) context_stack,
735 (context_stack_size * sizeof (struct context_stack)));
736 }
737
738 new = &context_stack[context_stack_depth++];
739 new->depth = desc;
740 new->locals = local_symbols;
741 new->old_blocks = pending_blocks;
742 new->start_addr = valu;
743 new->name = NULL;
744
745 local_symbols = NULL;
746
747 return (new);
748 }
749
750 \f
751 /* Initialize anything that needs initializing when starting to read
752 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
753 to a psymtab. */
754
755 void
756 buildsym_init ()
757 {
758 free_pendings = NULL;
759 file_symbols = NULL;
760 global_symbols = NULL;
761 pending_blocks = NULL;
762 }
763
764 /* Initialize anything that needs initializing when a completely new
765 symbol file is specified (not just adding some symbols from another
766 file, e.g. a shared library). */
767
768 void
769 buildsym_new_init ()
770 {
771 buildsym_init ();
772 }
773
774 /* Initializer for this module */
775
776 void
777 _initialize_buildsym ()
778 {
779 }
This page took 0.044447 seconds and 4 git commands to generate.