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