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