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