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