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