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