5078935fefebb98e2eb2615bb904857fb0bcae54
[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, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
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.
25
26 Routines to support specific debugging information formats (stabs,
27 DWARF, etc) belong somewhere else. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "obstack.h"
32 #include "symtab.h"
33 #include "symfile.h" /* Needed for "struct complaint" */
34 #include "objfiles.h"
35 #include "gdbtypes.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" */
40 #include "bcache.h"
41 #include "filenames.h" /* For DOSish file names */
42 #include "macrotab.h"
43 /* Ask buildsym.h to define the vars it normally declares `extern'. */
44 #define EXTERN
45 /**/
46 #include "buildsym.h" /* Our own declarations */
47 #undef EXTERN
48
49 /* For cleanup_undefined_types and finish_global_stabs (somewhat
50 questionable--see comment where we call them). */
51
52 #include "stabsread.h"
53
54 /* List of free `struct pending' structures for reuse. */
55
56 static struct pending *free_pendings;
57
58 /* Non-zero if symtab has line number info. This prevents an
59 otherwise empty symtab from being tossed. */
60
61 static int have_line_numbers;
62 \f
63 static int compare_line_numbers (const void *ln1p, const void *ln2p);
64 \f
65
66 /* Initial sizes of data structures. These are realloc'd larger if
67 needed, and realloc'd down to the size actually used, when
68 completed. */
69
70 #define INITIAL_CONTEXT_STACK_SIZE 10
71 #define INITIAL_LINE_VECTOR_LENGTH 1000
72 \f
73
74 /* Complaints about the symbols we have encountered. */
75
76 struct complaint block_end_complaint =
77 {"block end address less than block start address in %s (patched it)", 0, 0};
78
79 struct complaint anon_block_end_complaint =
80 {"block end address 0x%lx less than block start address 0x%lx (patched it)", 0, 0};
81
82 struct complaint innerblock_complaint =
83 {"inner block not inside outer block in %s", 0, 0};
84
85 struct complaint innerblock_anon_complaint =
86 {"inner block (0x%lx-0x%lx) not inside outer block (0x%lx-0x%lx)", 0, 0};
87
88 struct complaint blockvector_complaint =
89 {"block at %s out of order", 0, 0};
90 \f
91 /* maintain the lists of symbols and blocks */
92
93 /* Add a pending list to free_pendings. */
94 void
95 add_free_pendings (struct pending *list)
96 {
97 register struct pending *link = list;
98
99 if (list)
100 {
101 while (link->next) link = link->next;
102 link->next = free_pendings;
103 free_pendings = list;
104 }
105 }
106
107 /* Add a symbol to one of the lists of symbols. */
108
109 void
110 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
111 {
112 register struct pending *link;
113
114 /* If this is an alias for another symbol, don't add it. */
115 if (symbol->ginfo.name && symbol->ginfo.name[0] == '#')
116 return;
117
118 /* We keep PENDINGSIZE symbols in each link of the list. If we
119 don't have a link with room in it, add a new link. */
120 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
121 {
122 if (free_pendings)
123 {
124 link = free_pendings;
125 free_pendings = link->next;
126 }
127 else
128 {
129 link = (struct pending *) xmalloc (sizeof (struct pending));
130 }
131
132 link->next = *listhead;
133 *listhead = link;
134 link->nsyms = 0;
135 }
136
137 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
138 }
139
140 /* Find a symbol named NAME on a LIST. NAME need not be
141 '\0'-terminated; LENGTH is the length of the name. */
142
143 struct symbol *
144 find_symbol_in_list (struct pending *list, char *name, int length)
145 {
146 int j;
147 char *pp;
148
149 while (list != NULL)
150 {
151 for (j = list->nsyms; --j >= 0;)
152 {
153 pp = SYMBOL_NAME (list->symbol[j]);
154 if (*pp == *name && strncmp (pp, name, length) == 0 &&
155 pp[length] == '\0')
156 {
157 return (list->symbol[j]);
158 }
159 }
160 list = list->next;
161 }
162 return (NULL);
163 }
164
165 /* At end of reading syms, or in case of quit, really free as many
166 `struct pending's as we can easily find. */
167
168 /* ARGSUSED */
169 void
170 really_free_pendings (PTR dummy)
171 {
172 struct pending *next, *next1;
173
174 for (next = free_pendings; next; next = next1)
175 {
176 next1 = next->next;
177 xfree ((void *) next);
178 }
179 free_pendings = NULL;
180
181 free_pending_blocks ();
182
183 for (next = file_symbols; next != NULL; next = next1)
184 {
185 next1 = next->next;
186 xfree ((void *) next);
187 }
188 file_symbols = NULL;
189
190 for (next = global_symbols; next != NULL; next = next1)
191 {
192 next1 = next->next;
193 xfree ((void *) next);
194 }
195 global_symbols = NULL;
196
197 if (pending_macros)
198 free_macro_table (pending_macros);
199 }
200
201 /* This function is called to discard any pending blocks. */
202
203 void
204 free_pending_blocks (void)
205 {
206 #if 0 /* Now we make the links in the
207 symbol_obstack, so don't free
208 them. */
209 struct pending_block *bnext, *bnext1;
210
211 for (bnext = pending_blocks; bnext; bnext = bnext1)
212 {
213 bnext1 = bnext->next;
214 xfree ((void *) bnext);
215 }
216 #endif
217 pending_blocks = NULL;
218 }
219
220 /* Take one of the lists of symbols and make a block from it. Keep
221 the order the symbols have in the list (reversed from the input
222 file). Put the block on the list of pending blocks. */
223
224 void
225 finish_block (struct symbol *symbol, struct pending **listhead,
226 struct pending_block *old_blocks,
227 CORE_ADDR start, CORE_ADDR end,
228 struct objfile *objfile)
229 {
230 register struct pending *next, *next1;
231 register struct block *block;
232 register struct pending_block *pblock;
233 struct pending_block *opblock;
234 register int i;
235 register int j;
236
237 /* Count the length of the list of symbols. */
238
239 for (next = *listhead, i = 0;
240 next;
241 i += next->nsyms, next = next->next)
242 {
243 /* EMPTY */ ;
244 }
245
246 block = (struct block *) obstack_alloc (&objfile->symbol_obstack,
247 (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *))));
248
249 /* Copy the symbols into the block. */
250
251 BLOCK_NSYMS (block) = i;
252 for (next = *listhead; next; next = next->next)
253 {
254 for (j = next->nsyms - 1; j >= 0; j--)
255 {
256 BLOCK_SYM (block, --i) = next->symbol[j];
257 }
258 }
259
260 BLOCK_START (block) = start;
261 BLOCK_END (block) = end;
262 /* Superblock filled in when containing block is made */
263 BLOCK_SUPERBLOCK (block) = NULL;
264
265 BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
266
267 /* Put the block in as the value of the symbol that names it. */
268
269 if (symbol)
270 {
271 struct type *ftype = SYMBOL_TYPE (symbol);
272 SYMBOL_BLOCK_VALUE (symbol) = block;
273 BLOCK_FUNCTION (block) = symbol;
274
275 if (TYPE_NFIELDS (ftype) <= 0)
276 {
277 /* No parameter type information is recorded with the
278 function's type. Set that from the type of the
279 parameter symbols. */
280 int nparams = 0, iparams;
281 struct symbol *sym;
282 ALL_BLOCK_SYMBOLS (block, i, sym)
283 {
284 switch (SYMBOL_CLASS (sym))
285 {
286 case LOC_ARG:
287 case LOC_REF_ARG:
288 case LOC_REGPARM:
289 case LOC_REGPARM_ADDR:
290 case LOC_BASEREG_ARG:
291 case LOC_LOCAL_ARG:
292 nparams++;
293 break;
294 case LOC_UNDEF:
295 case LOC_CONST:
296 case LOC_STATIC:
297 case LOC_INDIRECT:
298 case LOC_REGISTER:
299 case LOC_LOCAL:
300 case LOC_TYPEDEF:
301 case LOC_LABEL:
302 case LOC_BLOCK:
303 case LOC_CONST_BYTES:
304 case LOC_BASEREG:
305 case LOC_UNRESOLVED:
306 case LOC_OPTIMIZED_OUT:
307 default:
308 break;
309 }
310 }
311 if (nparams > 0)
312 {
313 TYPE_NFIELDS (ftype) = nparams;
314 TYPE_FIELDS (ftype) = (struct field *)
315 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
316
317 for (i = iparams = 0; iparams < nparams; i++)
318 {
319 sym = BLOCK_SYM (block, i);
320 switch (SYMBOL_CLASS (sym))
321 {
322 case LOC_ARG:
323 case LOC_REF_ARG:
324 case LOC_REGPARM:
325 case LOC_REGPARM_ADDR:
326 case LOC_BASEREG_ARG:
327 case LOC_LOCAL_ARG:
328 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
329 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
330 iparams++;
331 break;
332 case LOC_UNDEF:
333 case LOC_CONST:
334 case LOC_STATIC:
335 case LOC_INDIRECT:
336 case LOC_REGISTER:
337 case LOC_LOCAL:
338 case LOC_TYPEDEF:
339 case LOC_LABEL:
340 case LOC_BLOCK:
341 case LOC_CONST_BYTES:
342 case LOC_BASEREG:
343 case LOC_UNRESOLVED:
344 case LOC_OPTIMIZED_OUT:
345 default:
346 break;
347 }
348 }
349 }
350 }
351 }
352 else
353 {
354 BLOCK_FUNCTION (block) = NULL;
355 }
356
357 /* Now "free" the links of the list, and empty the list. */
358
359 for (next = *listhead; next; next = next1)
360 {
361 next1 = next->next;
362 next->next = free_pendings;
363 free_pendings = next;
364 }
365 *listhead = NULL;
366
367 #if 1
368 /* Check to be sure that the blocks have an end address that is
369 greater than starting address */
370
371 if (BLOCK_END (block) < BLOCK_START (block))
372 {
373 if (symbol)
374 {
375 complain (&block_end_complaint, SYMBOL_SOURCE_NAME (symbol));
376 }
377 else
378 {
379 complain (&anon_block_end_complaint, BLOCK_END (block), BLOCK_START (block));
380 }
381 /* Better than nothing */
382 BLOCK_END (block) = BLOCK_START (block);
383 }
384 #endif
385
386 /* Install this block as the superblock of all blocks made since the
387 start of this scope that don't have superblocks yet. */
388
389 opblock = NULL;
390 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
391 {
392 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
393 {
394 #if 1
395 /* Check to be sure the blocks are nested as we receive
396 them. If the compiler/assembler/linker work, this just
397 burns a small amount of time. */
398 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
399 BLOCK_END (pblock->block) > BLOCK_END (block))
400 {
401 if (symbol)
402 {
403 complain (&innerblock_complaint,
404 SYMBOL_SOURCE_NAME (symbol));
405 }
406 else
407 {
408 complain (&innerblock_anon_complaint, BLOCK_START (pblock->block),
409 BLOCK_END (pblock->block), BLOCK_START (block),
410 BLOCK_END (block));
411 }
412 if (BLOCK_START (pblock->block) < BLOCK_START (block))
413 BLOCK_START (pblock->block) = BLOCK_START (block);
414 if (BLOCK_END (pblock->block) > BLOCK_END (block))
415 BLOCK_END (pblock->block) = BLOCK_END (block);
416 }
417 #endif
418 BLOCK_SUPERBLOCK (pblock->block) = block;
419 }
420 opblock = pblock;
421 }
422
423 record_pending_block (objfile, block, opblock);
424 }
425
426 /* Record BLOCK on the list of all blocks in the file. Put it after
427 OPBLOCK, or at the beginning if opblock is NULL. This puts the
428 block in the list after all its subblocks.
429
430 Allocate the pending block struct in the symbol_obstack to save
431 time. This wastes a little space. FIXME: Is it worth it? */
432
433 void
434 record_pending_block (struct objfile *objfile, struct block *block,
435 struct pending_block *opblock)
436 {
437 register struct pending_block *pblock;
438
439 pblock = (struct pending_block *)
440 obstack_alloc (&objfile->symbol_obstack, sizeof (struct pending_block));
441 pblock->block = block;
442 if (opblock)
443 {
444 pblock->next = opblock->next;
445 opblock->next = pblock;
446 }
447 else
448 {
449 pblock->next = pending_blocks;
450 pending_blocks = pblock;
451 }
452 }
453
454 /* Note that this is only used in this file and in dstread.c, which
455 should be fixed to not need direct access to this function. When
456 that is done, it can be made static again. */
457
458 struct blockvector *
459 make_blockvector (struct objfile *objfile)
460 {
461 register struct pending_block *next;
462 register struct blockvector *blockvector;
463 register int i;
464
465 /* Count the length of the list of blocks. */
466
467 for (next = pending_blocks, i = 0; next; next = next->next, i++)
468 {;
469 }
470
471 blockvector = (struct blockvector *)
472 obstack_alloc (&objfile->symbol_obstack,
473 (sizeof (struct blockvector)
474 + (i - 1) * sizeof (struct block *)));
475
476 /* Copy the blocks into the blockvector. This is done in reverse
477 order, which happens to put the blocks into the proper order
478 (ascending starting address). finish_block has hair to insert
479 each block into the list after its subblocks in order to make
480 sure this is true. */
481
482 BLOCKVECTOR_NBLOCKS (blockvector) = i;
483 for (next = pending_blocks; next; next = next->next)
484 {
485 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
486 }
487
488 #if 0 /* Now we make the links in the
489 obstack, so don't free them. */
490 /* Now free the links of the list, and empty the list. */
491
492 for (next = pending_blocks; next; next = next1)
493 {
494 next1 = next->next;
495 xfree (next);
496 }
497 #endif
498 pending_blocks = NULL;
499
500 #if 1 /* FIXME, shut this off after a while
501 to speed up symbol reading. */
502 /* Some compilers output blocks in the wrong order, but we depend on
503 their being in the right order so we can binary search. Check the
504 order and moan about it. FIXME. */
505 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
506 {
507 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
508 {
509 if (BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i - 1))
510 > BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i)))
511 {
512 CORE_ADDR start
513 = BLOCK_START (BLOCKVECTOR_BLOCK (blockvector, i));
514
515 complain (&blockvector_complaint,
516 local_hex_string ((LONGEST) start));
517 }
518 }
519 }
520 #endif
521
522 return (blockvector);
523 }
524 \f
525 /* Start recording information about source code that came from an
526 included (or otherwise merged-in) source file with a different
527 name. NAME is the name of the file (cannot be NULL), DIRNAME is
528 the directory in which it resides (or NULL if not known). */
529
530 void
531 start_subfile (char *name, char *dirname)
532 {
533 register struct subfile *subfile;
534
535 /* See if this subfile is already known as a subfile of the current
536 main source file. */
537
538 for (subfile = subfiles; subfile; subfile = subfile->next)
539 {
540 if (FILENAME_CMP (subfile->name, name) == 0)
541 {
542 current_subfile = subfile;
543 return;
544 }
545 }
546
547 /* This subfile is not known. Add an entry for it. Make an entry
548 for this subfile in the list of all subfiles of the current main
549 source file. */
550
551 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
552 memset ((char *) subfile, 0, sizeof (struct subfile));
553 subfile->next = subfiles;
554 subfiles = subfile;
555 current_subfile = subfile;
556
557 /* Save its name and compilation directory name */
558 subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
559 subfile->dirname =
560 (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
561
562 /* Initialize line-number recording for this subfile. */
563 subfile->line_vector = NULL;
564
565 /* Default the source language to whatever can be deduced from the
566 filename. If nothing can be deduced (such as for a C/C++ include
567 file with a ".h" extension), then inherit whatever language the
568 previous subfile had. This kludgery is necessary because there
569 is no standard way in some object formats to record the source
570 language. Also, when symtabs are allocated we try to deduce a
571 language then as well, but it is too late for us to use that
572 information while reading symbols, since symtabs aren't allocated
573 until after all the symbols have been processed for a given
574 source file. */
575
576 subfile->language = deduce_language_from_filename (subfile->name);
577 if (subfile->language == language_unknown &&
578 subfile->next != NULL)
579 {
580 subfile->language = subfile->next->language;
581 }
582
583 /* Initialize the debug format string to NULL. We may supply it
584 later via a call to record_debugformat. */
585 subfile->debugformat = NULL;
586
587 /* cfront output is a C program, so in most ways it looks like a C
588 program. But to demangle we need to set the language to C++. We
589 can distinguish cfront code by the fact that it has #line
590 directives which specify a file name ending in .C.
591
592 So if the filename of this subfile ends in .C, then change the
593 language of any pending subfiles from C to C++. We also accept
594 any other C++ suffixes accepted by deduce_language_from_filename
595 (in particular, some people use .cxx with cfront). */
596 /* Likewise for f2c. */
597
598 if (subfile->name)
599 {
600 struct subfile *s;
601 enum language sublang = deduce_language_from_filename (subfile->name);
602
603 if (sublang == language_cplus || sublang == language_fortran)
604 for (s = subfiles; s != NULL; s = s->next)
605 if (s->language == language_c)
606 s->language = sublang;
607 }
608
609 /* And patch up this file if necessary. */
610 if (subfile->language == language_c
611 && subfile->next != NULL
612 && (subfile->next->language == language_cplus
613 || subfile->next->language == language_fortran))
614 {
615 subfile->language = subfile->next->language;
616 }
617 }
618
619 /* For stabs readers, the first N_SO symbol is assumed to be the
620 source file name, and the subfile struct is initialized using that
621 assumption. If another N_SO symbol is later seen, immediately
622 following the first one, then the first one is assumed to be the
623 directory name and the second one is really the source file name.
624
625 So we have to patch up the subfile struct by moving the old name
626 value to dirname and remembering the new name. Some sanity
627 checking is performed to ensure that the state of the subfile
628 struct is reasonable and that the old name we are assuming to be a
629 directory name actually is (by checking for a trailing '/'). */
630
631 void
632 patch_subfile_names (struct subfile *subfile, char *name)
633 {
634 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
635 && subfile->name[strlen (subfile->name) - 1] == '/')
636 {
637 subfile->dirname = subfile->name;
638 subfile->name = savestring (name, strlen (name));
639 last_source_file = name;
640
641 /* Default the source language to whatever can be deduced from
642 the filename. If nothing can be deduced (such as for a C/C++
643 include file with a ".h" extension), then inherit whatever
644 language the previous subfile had. This kludgery is
645 necessary because there is no standard way in some object
646 formats to record the source language. Also, when symtabs
647 are allocated we try to deduce a language then as well, but
648 it is too late for us to use that information while reading
649 symbols, since symtabs aren't allocated until after all the
650 symbols have been processed for a given source file. */
651
652 subfile->language = deduce_language_from_filename (subfile->name);
653 if (subfile->language == language_unknown &&
654 subfile->next != NULL)
655 {
656 subfile->language = subfile->next->language;
657 }
658 }
659 }
660 \f
661 /* Handle the N_BINCL and N_EINCL symbol types that act like N_SOL for
662 switching source files (different subfiles, as we call them) within
663 one object file, but using a stack rather than in an arbitrary
664 order. */
665
666 void
667 push_subfile (void)
668 {
669 register struct subfile_stack *tem
670 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
671
672 tem->next = subfile_stack;
673 subfile_stack = tem;
674 if (current_subfile == NULL || current_subfile->name == NULL)
675 {
676 internal_error (__FILE__, __LINE__, "failed internal consistency check");
677 }
678 tem->name = current_subfile->name;
679 }
680
681 char *
682 pop_subfile (void)
683 {
684 register char *name;
685 register struct subfile_stack *link = subfile_stack;
686
687 if (link == NULL)
688 {
689 internal_error (__FILE__, __LINE__, "failed internal consistency check");
690 }
691 name = link->name;
692 subfile_stack = link->next;
693 xfree ((void *) link);
694 return (name);
695 }
696 \f
697 /* Add a linetable entry for line number LINE and address PC to the
698 line vector for SUBFILE. */
699
700 void
701 record_line (register struct subfile *subfile, int line, CORE_ADDR pc)
702 {
703 struct linetable_entry *e;
704 /* Ignore the dummy line number in libg.o */
705
706 if (line == 0xffff)
707 {
708 return;
709 }
710
711 /* Make sure line vector exists and is big enough. */
712 if (!subfile->line_vector)
713 {
714 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
715 subfile->line_vector = (struct linetable *)
716 xmalloc (sizeof (struct linetable)
717 + subfile->line_vector_length * sizeof (struct linetable_entry));
718 subfile->line_vector->nitems = 0;
719 have_line_numbers = 1;
720 }
721
722 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
723 {
724 subfile->line_vector_length *= 2;
725 subfile->line_vector = (struct linetable *)
726 xrealloc ((char *) subfile->line_vector,
727 (sizeof (struct linetable)
728 + (subfile->line_vector_length
729 * sizeof (struct linetable_entry))));
730 }
731
732 e = subfile->line_vector->item + subfile->line_vector->nitems++;
733 e->line = line;
734 e->pc = ADDR_BITS_REMOVE(pc);
735 }
736
737 /* Needed in order to sort line tables from IBM xcoff files. Sigh! */
738
739 static int
740 compare_line_numbers (const void *ln1p, const void *ln2p)
741 {
742 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
743 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
744
745 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
746 Please keep it that way. */
747 if (ln1->pc < ln2->pc)
748 return -1;
749
750 if (ln1->pc > ln2->pc)
751 return 1;
752
753 /* If pc equal, sort by line. I'm not sure whether this is optimum
754 behavior (see comment at struct linetable in symtab.h). */
755 return ln1->line - ln2->line;
756 }
757 \f
758 /* Start a new symtab for a new source file. Called, for example,
759 when a stabs symbol of type N_SO is seen, or when a DWARF
760 TAG_compile_unit DIE is seen. It indicates the start of data for
761 one original source file. */
762
763 void
764 start_symtab (char *name, char *dirname, CORE_ADDR start_addr)
765 {
766
767 last_source_file = name;
768 last_source_start_addr = start_addr;
769 file_symbols = NULL;
770 global_symbols = NULL;
771 within_function = 0;
772 have_line_numbers = 0;
773
774 /* Context stack is initially empty. Allocate first one with room
775 for 10 levels; reuse it forever afterward. */
776 if (context_stack == NULL)
777 {
778 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
779 context_stack = (struct context_stack *)
780 xmalloc (context_stack_size * sizeof (struct context_stack));
781 }
782 context_stack_depth = 0;
783
784 /* Initialize the list of sub source files with one entry for this
785 file (the top-level source file). */
786
787 subfiles = NULL;
788 current_subfile = NULL;
789 start_subfile (name, dirname);
790 }
791
792 /* Finish the symbol definitions for one main source file, close off
793 all the lexical contexts for that file (creating struct block's for
794 them), then make the struct symtab for that file and put it in the
795 list of all such.
796
797 END_ADDR is the address of the end of the file's text. SECTION is
798 the section number (in objfile->section_offsets) of the blockvector
799 and linetable.
800
801 Note that it is possible for end_symtab() to return NULL. In
802 particular, for the DWARF case at least, it will return NULL when
803 it finds a compilation unit that has exactly one DIE, a
804 TAG_compile_unit DIE. This can happen when we link in an object
805 file that was compiled from an empty source file. Returning NULL
806 is probably not the correct thing to do, because then gdb will
807 never know about this empty file (FIXME). */
808
809 struct symtab *
810 end_symtab (CORE_ADDR end_addr, struct objfile *objfile, int section)
811 {
812 register struct symtab *symtab = NULL;
813 register struct blockvector *blockvector;
814 register struct subfile *subfile;
815 register struct context_stack *cstk;
816 struct subfile *nextsub;
817
818 /* Finish the lexical context of the last function in the file; pop
819 the context stack. */
820
821 if (context_stack_depth > 0)
822 {
823 cstk = pop_context ();
824 /* Make a block for the local symbols within. */
825 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
826 cstk->start_addr, end_addr, objfile);
827
828 if (context_stack_depth > 0)
829 {
830 /* This is said to happen with SCO. The old coffread.c
831 code simply emptied the context stack, so we do the
832 same. FIXME: Find out why it is happening. This is not
833 believed to happen in most cases (even for coffread.c);
834 it used to be an abort(). */
835 static struct complaint msg =
836 {"Context stack not empty in end_symtab", 0, 0};
837 complain (&msg);
838 context_stack_depth = 0;
839 }
840 }
841
842 /* Reordered executables may have out of order pending blocks; if
843 OBJF_REORDERED is true, then sort the pending blocks. */
844 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
845 {
846 /* FIXME! Remove this horrid bubble sort and use merge sort!!! */
847 int swapped;
848 do
849 {
850 struct pending_block *pb, *pbnext;
851
852 pb = pending_blocks;
853 pbnext = pb->next;
854 swapped = 0;
855
856 while (pbnext)
857 {
858 /* swap blocks if unordered! */
859
860 if (BLOCK_START (pb->block) < BLOCK_START (pbnext->block))
861 {
862 struct block *tmp = pb->block;
863 pb->block = pbnext->block;
864 pbnext->block = tmp;
865 swapped = 1;
866 }
867 pb = pbnext;
868 pbnext = pbnext->next;
869 }
870 }
871 while (swapped);
872 }
873
874 /* Cleanup any undefined types that have been left hanging around
875 (this needs to be done before the finish_blocks so that
876 file_symbols is still good).
877
878 Both cleanup_undefined_types and finish_global_stabs are stabs
879 specific, but harmless for other symbol readers, since on gdb
880 startup or when finished reading stabs, the state is set so these
881 are no-ops. FIXME: Is this handled right in case of QUIT? Can
882 we make this cleaner? */
883
884 cleanup_undefined_types ();
885 finish_global_stabs (objfile);
886
887 if (pending_blocks == NULL
888 && file_symbols == NULL
889 && global_symbols == NULL
890 && have_line_numbers == 0
891 && pending_macros == NULL)
892 {
893 /* Ignore symtabs that have no functions with real debugging
894 info. */
895 blockvector = NULL;
896 }
897 else
898 {
899 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the
900 blockvector. */
901 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
902 objfile);
903 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
904 objfile);
905 blockvector = make_blockvector (objfile);
906 }
907
908 #ifndef PROCESS_LINENUMBER_HOOK
909 #define PROCESS_LINENUMBER_HOOK()
910 #endif
911 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
912
913 /* Now create the symtab objects proper, one for each subfile. */
914 /* (The main file is the last one on the chain.) */
915
916 for (subfile = subfiles; subfile; subfile = nextsub)
917 {
918 int linetablesize = 0;
919 symtab = NULL;
920
921 /* If we have blocks of symbols, make a symtab. Otherwise, just
922 ignore this file and any line number info in it. */
923 if (blockvector)
924 {
925 if (subfile->line_vector)
926 {
927 linetablesize = sizeof (struct linetable) +
928 subfile->line_vector->nitems * sizeof (struct linetable_entry);
929 #if 0
930 /* I think this is artifact from before it went on the
931 obstack. I doubt we'll need the memory between now
932 and when we free it later in this function. */
933 /* First, shrink the linetable to make more memory. */
934 subfile->line_vector = (struct linetable *)
935 xrealloc ((char *) subfile->line_vector, linetablesize);
936 #endif
937
938 /* Like the pending blocks, the line table may be
939 scrambled in reordered executables. Sort it if
940 OBJF_REORDERED is true. */
941 if (objfile->flags & OBJF_REORDERED)
942 qsort (subfile->line_vector->item,
943 subfile->line_vector->nitems,
944 sizeof (struct linetable_entry), compare_line_numbers);
945 }
946
947 /* Now, allocate a symbol table. */
948 symtab = allocate_symtab (subfile->name, objfile);
949
950 /* Fill in its components. */
951 symtab->blockvector = blockvector;
952 symtab->macro_table = pending_macros;
953 if (subfile->line_vector)
954 {
955 /* Reallocate the line table on the symbol obstack */
956 symtab->linetable = (struct linetable *)
957 obstack_alloc (&objfile->symbol_obstack, linetablesize);
958 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
959 }
960 else
961 {
962 symtab->linetable = NULL;
963 }
964 symtab->block_line_section = section;
965 if (subfile->dirname)
966 {
967 /* Reallocate the dirname on the symbol obstack */
968 symtab->dirname = (char *)
969 obstack_alloc (&objfile->symbol_obstack,
970 strlen (subfile->dirname) + 1);
971 strcpy (symtab->dirname, subfile->dirname);
972 }
973 else
974 {
975 symtab->dirname = NULL;
976 }
977 symtab->free_code = free_linetable;
978 symtab->free_ptr = NULL;
979
980 /* Use whatever language we have been using for this
981 subfile, not the one that was deduced in allocate_symtab
982 from the filename. We already did our own deducing when
983 we created the subfile, and we may have altered our
984 opinion of what language it is from things we found in
985 the symbols. */
986 symtab->language = subfile->language;
987
988 /* Save the debug format string (if any) in the symtab */
989 if (subfile->debugformat != NULL)
990 {
991 symtab->debugformat = obsavestring (subfile->debugformat,
992 strlen (subfile->debugformat),
993 &objfile->symbol_obstack);
994 }
995
996 /* All symtabs for the main file and the subfiles share a
997 blockvector, so we need to clear primary for everything
998 but the main file. */
999
1000 symtab->primary = 0;
1001 }
1002 if (subfile->name != NULL)
1003 {
1004 xfree ((void *) subfile->name);
1005 }
1006 if (subfile->dirname != NULL)
1007 {
1008 xfree ((void *) subfile->dirname);
1009 }
1010 if (subfile->line_vector != NULL)
1011 {
1012 xfree ((void *) subfile->line_vector);
1013 }
1014 if (subfile->debugformat != NULL)
1015 {
1016 xfree ((void *) subfile->debugformat);
1017 }
1018
1019 nextsub = subfile->next;
1020 xfree ((void *) subfile);
1021 }
1022
1023 /* Set this for the main source file. */
1024 if (symtab)
1025 {
1026 symtab->primary = 1;
1027 }
1028
1029 last_source_file = NULL;
1030 current_subfile = NULL;
1031 pending_macros = NULL;
1032
1033 return symtab;
1034 }
1035
1036 /* Push a context block. Args are an identifying nesting level
1037 (checkable when you pop it), and the starting PC address of this
1038 context. */
1039
1040 struct context_stack *
1041 push_context (int desc, CORE_ADDR valu)
1042 {
1043 register struct context_stack *new;
1044
1045 if (context_stack_depth == context_stack_size)
1046 {
1047 context_stack_size *= 2;
1048 context_stack = (struct context_stack *)
1049 xrealloc ((char *) context_stack,
1050 (context_stack_size * sizeof (struct context_stack)));
1051 }
1052
1053 new = &context_stack[context_stack_depth++];
1054 new->depth = desc;
1055 new->locals = local_symbols;
1056 new->params = param_symbols;
1057 new->old_blocks = pending_blocks;
1058 new->start_addr = valu;
1059 new->name = NULL;
1060
1061 local_symbols = NULL;
1062 param_symbols = NULL;
1063
1064 return new;
1065 }
1066 \f
1067
1068 /* Compute a small integer hash code for the given name. */
1069
1070 int
1071 hashname (char *name)
1072 {
1073 return (hash(name,strlen(name)) % HASHSIZE);
1074 }
1075 \f
1076
1077 void
1078 record_debugformat (char *format)
1079 {
1080 current_subfile->debugformat = savestring (format, strlen (format));
1081 }
1082
1083 /* Merge the first symbol list SRCLIST into the second symbol list
1084 TARGETLIST by repeated calls to add_symbol_to_list(). This
1085 procedure "frees" each link of SRCLIST by adding it to the
1086 free_pendings list. Caller must set SRCLIST to a null list after
1087 calling this function.
1088
1089 Void return. */
1090
1091 void
1092 merge_symbol_lists (struct pending **srclist, struct pending **targetlist)
1093 {
1094 register int i;
1095
1096 if (!srclist || !*srclist)
1097 return;
1098
1099 /* Merge in elements from current link. */
1100 for (i = 0; i < (*srclist)->nsyms; i++)
1101 add_symbol_to_list ((*srclist)->symbol[i], targetlist);
1102
1103 /* Recurse on next. */
1104 merge_symbol_lists (&(*srclist)->next, targetlist);
1105
1106 /* "Free" the current link. */
1107 (*srclist)->next = free_pendings;
1108 free_pendings = (*srclist);
1109 }
1110 \f
1111 /* Initialize anything that needs initializing when starting to read a
1112 fresh piece of a symbol file, e.g. reading in the stuff
1113 corresponding to a psymtab. */
1114
1115 void
1116 buildsym_init (void)
1117 {
1118 free_pendings = NULL;
1119 file_symbols = NULL;
1120 global_symbols = NULL;
1121 pending_blocks = NULL;
1122 pending_macros = NULL;
1123 }
1124
1125 /* Initialize anything that needs initializing when a completely new
1126 symbol file is specified (not just adding some symbols from another
1127 file, e.g. a shared library). */
1128
1129 void
1130 buildsym_new_init (void)
1131 {
1132 buildsym_init ();
1133 }
This page took 0.070132 seconds and 4 git commands to generate.