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