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