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