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