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