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