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