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