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