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