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