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