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