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