* mdebugread.c (parse_symbol): Use new variable
[deliverable/binutils-gdb.git] / gdb / buildsym.c
CommitLineData
d07734e3 1/* Support routines for building symbol tables in GDB's internal format.
b9e58503 2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1995
93297ea0 3 Free Software Foundation, Inc.
c0302457
JG
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/* This module provides subroutines used for creating and adding to
22 the symbol table. These routines are called from various symbol-
d07734e3 23 file-reading routines.
c0302457 24
d07734e3
FF
25 Routines to support specific debugging information formats (stabs,
26 DWARF, etc) belong somewhere else. */
c0302457
JG
27
28#include "defs.h"
d07734e3 29#include "bfd.h"
c0302457
JG
30#include "obstack.h"
31#include "symtab.h"
c0302457 32#include "symfile.h" /* Needed for "struct complaint" */
5e2e79f8 33#include "objfiles.h"
27202b6a 34#include "gdbtypes.h"
51b80b00 35#include "complaints.h"
c0302457 36#include <string.h>
c0302457
JG
37
38/* Ask buildsym.h to define the vars it normally declares `extern'. */
39#define EXTERN /**/
40#include "buildsym.h" /* Our own declarations */
41#undef EXTERN
42
100f92e2
JK
43/* For cleanup_undefined_types and finish_global_stabs (somewhat
44 questionable--see comment where we call them). */
45#include "stabsread.h"
46
1ab3bf1b
JG
47static int
48compare_line_numbers PARAMS ((const void *, const void *));
49
50static struct blockvector *
51make_blockvector PARAMS ((struct objfile *));
52
1ab3bf1b 53\f
4137c5fc
JG
54/* Initial sizes of data structures. These are realloc'd larger if needed,
55 and realloc'd down to the size actually used, when completed. */
56
57#define INITIAL_CONTEXT_STACK_SIZE 10
4137c5fc 58#define INITIAL_LINE_VECTOR_LENGTH 1000
d07734e3 59
c0302457
JG
60\f
61/* Complaints about the symbols we have encountered. */
62
63struct complaint innerblock_complaint =
64 {"inner block not inside outer block in %s", 0, 0};
65
76512886
JG
66struct complaint innerblock_anon_complaint =
67 {"inner block not inside outer block", 0, 0};
68
c0302457 69struct complaint blockvector_complaint =
5573d7d4 70 {"block at 0x%lx out of order", 0, 0};
c0302457 71
c0302457
JG
72\f
73/* maintain the lists of symbols and blocks */
74
75/* Add a symbol to one of the lists of symbols. */
d07734e3 76
c0302457
JG
77void
78add_symbol_to_list (symbol, listhead)
79 struct symbol *symbol;
80 struct pending **listhead;
81{
d07734e3
FF
82 register struct pending *link;
83
c0302457
JG
84 /* We keep PENDINGSIZE symbols in each link of the list.
85 If we don't have a link with room in it, add a new link. */
d07734e3 86 if (*listhead == NULL || (*listhead)->nsyms == PENDINGSIZE)
c0302457 87 {
c0302457
JG
88 if (free_pendings)
89 {
90 link = free_pendings;
91 free_pendings = link->next;
92 }
93 else
d07734e3
FF
94 {
95 link = (struct pending *) xmalloc (sizeof (struct pending));
96 }
c0302457
JG
97
98 link->next = *listhead;
99 *listhead = link;
100 link->nsyms = 0;
101 }
102
103 (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
104}
105
c438b3af
JK
106/* Find a symbol named NAME on a LIST. NAME need not be '\0'-terminated;
107 LENGTH is the length of the name. */
d07734e3 108
a048c8f5
JG
109struct symbol *
110find_symbol_in_list (list, name, length)
111 struct pending *list;
112 char *name;
113 int length;
114{
115 int j;
d07734e3 116 char *pp;
a048c8f5 117
d07734e3
FF
118 while (list != NULL)
119 {
120 for (j = list->nsyms; --j >= 0; )
121 {
122 pp = SYMBOL_NAME (list->symbol[j]);
123 if (*pp == *name && strncmp (pp, name, length) == 0 &&
124 pp[length] == '\0')
125 {
126 return (list->symbol[j]);
127 }
128 }
129 list = list->next;
a048c8f5 130 }
d07734e3 131 return (NULL);
a048c8f5
JG
132}
133
c0302457 134/* At end of reading syms, or in case of quit,
d07734e3 135 really free as many `struct pending's as we can easily find. */
c0302457
JG
136
137/* ARGSUSED */
138void
139really_free_pendings (foo)
140 int foo;
141{
142 struct pending *next, *next1;
143#if 0
144 struct pending_block *bnext, *bnext1;
145#endif
146
147 for (next = free_pendings; next; next = next1)
148 {
149 next1 = next->next;
84ffdec2 150 free ((PTR)next);
c0302457 151 }
d07734e3 152 free_pendings = NULL;
c0302457
JG
153
154#if 0 /* Now we make the links in the symbol_obstack, so don't free them. */
155 for (bnext = pending_blocks; bnext; bnext = bnext1)
156 {
157 bnext1 = bnext->next;
84ffdec2 158 free ((PTR)bnext);
c0302457
JG
159 }
160#endif
d07734e3 161 pending_blocks = NULL;
c0302457 162
d07734e3 163 for (next = file_symbols; next != NULL; next = next1)
c0302457
JG
164 {
165 next1 = next->next;
84ffdec2 166 free ((PTR)next);
c0302457 167 }
d07734e3 168 file_symbols = NULL;
c0302457 169
d07734e3 170 for (next = global_symbols; next != NULL; next = next1)
c0302457
JG
171 {
172 next1 = next->next;
84ffdec2 173 free ((PTR)next);
c0302457 174 }
d07734e3 175 global_symbols = NULL;
c0302457
JG
176}
177
178/* Take one of the lists of symbols and make a block from it.
179 Keep the order the symbols have in the list (reversed from the input file).
180 Put the block on the list of pending blocks. */
181
182void
1ab3bf1b 183finish_block (symbol, listhead, old_blocks, start, end, objfile)
c0302457
JG
184 struct symbol *symbol;
185 struct pending **listhead;
186 struct pending_block *old_blocks;
187 CORE_ADDR start, end;
1ab3bf1b 188 struct objfile *objfile;
c0302457
JG
189{
190 register struct pending *next, *next1;
191 register struct block *block;
192 register struct pending_block *pblock;
193 struct pending_block *opblock;
194 register int i;
d07734e3 195 register int j;
c0302457
JG
196
197 /* Count the length of the list of symbols. */
198
cd46ffad
FF
199 for (next = *listhead, i = 0;
200 next;
201 i += next->nsyms, next = next->next)
d07734e3
FF
202 {
203 /*EMPTY*/;
204 }
c0302457 205
1ab3bf1b 206 block = (struct block *) obstack_alloc (&objfile -> symbol_obstack,
a048c8f5 207 (sizeof (struct block) + ((i - 1) * sizeof (struct symbol *))));
c0302457
JG
208
209 /* Copy the symbols into the block. */
210
211 BLOCK_NSYMS (block) = i;
212 for (next = *listhead; next; next = next->next)
213 {
c0302457 214 for (j = next->nsyms - 1; j >= 0; j--)
d07734e3
FF
215 {
216 BLOCK_SYM (block, --i) = next->symbol[j];
217 }
c0302457
JG
218 }
219
220 BLOCK_START (block) = start;
221 BLOCK_END (block) = end;
d07734e3
FF
222 /* Superblock filled in when containing block is made */
223 BLOCK_SUPERBLOCK (block) = NULL;
c0302457
JG
224 BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
225
226 /* Put the block in as the value of the symbol that names it. */
227
228 if (symbol)
229 {
27202b6a 230 struct type *ftype = SYMBOL_TYPE (symbol);
c0302457
JG
231 SYMBOL_BLOCK_VALUE (symbol) = block;
232 BLOCK_FUNCTION (block) = symbol;
27202b6a
PB
233
234 if (TYPE_NFIELDS (ftype) <= 0)
235 {
236 /* No parameter type information is recorded with the function's
237 type. Set that from the type of the parameter symbols. */
238 int nparams = 0, iparams;
239 struct symbol *sym;
240 for (i = 0; i < BLOCK_NSYMS (block); i++)
241 {
242 sym = BLOCK_SYM (block, i);
243 switch (SYMBOL_CLASS (sym))
244 {
245 case LOC_ARG:
246 case LOC_REF_ARG:
247 case LOC_REGPARM:
248 case LOC_REGPARM_ADDR:
249 nparams++;
250 }
251 }
252 if (nparams > 0)
253 {
254 TYPE_NFIELDS (ftype) = nparams;
255 TYPE_FIELDS (ftype) = (struct field *)
256 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
257
258 for (i = iparams = 0; iparams < nparams; i++)
259 {
260 sym = BLOCK_SYM (block, i);
261 switch (SYMBOL_CLASS (sym))
262 {
263 case LOC_ARG:
264 case LOC_REF_ARG:
265 case LOC_REGPARM:
266 case LOC_REGPARM_ADDR:
267 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
268 iparams++;
269 }
270 }
271 }
272 }
c0302457
JG
273 }
274 else
d07734e3
FF
275 {
276 BLOCK_FUNCTION (block) = NULL;
277 }
c0302457
JG
278
279 /* Now "free" the links of the list, and empty the list. */
280
281 for (next = *listhead; next; next = next1)
282 {
283 next1 = next->next;
284 next->next = free_pendings;
285 free_pendings = next;
286 }
d07734e3 287 *listhead = NULL;
c0302457
JG
288
289 /* Install this block as the superblock
290 of all blocks made since the start of this scope
291 that don't have superblocks yet. */
292
d07734e3 293 opblock = NULL;
c0302457
JG
294 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
295 {
d07734e3
FF
296 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
297 {
c0302457 298#if 1
d07734e3
FF
299 /* Check to be sure the blocks are nested as we receive them.
300 If the compiler/assembler/linker work, this just burns a small
301 amount of time. */
302 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
303 BLOCK_END (pblock->block) > BLOCK_END (block))
304 {
305 if (symbol)
306 {
2e4964ad
FF
307 complain (&innerblock_complaint,
308 SYMBOL_SOURCE_NAME (symbol));
d07734e3
FF
309 }
310 else
311 {
51b80b00 312 complain (&innerblock_anon_complaint);
d07734e3
FF
313 }
314 BLOCK_START (pblock->block) = BLOCK_START (block);
315 BLOCK_END (pblock->block) = BLOCK_END (block);
316 }
c0302457 317#endif
d07734e3
FF
318 BLOCK_SUPERBLOCK (pblock->block) = block;
319 }
c0302457
JG
320 opblock = pblock;
321 }
322
323 /* Record this block on the list of all blocks in the file.
324 Put it after opblock, or at the beginning if opblock is 0.
325 This puts the block in the list after all its subblocks. */
326
327 /* Allocate in the symbol_obstack to save time.
328 It wastes a little space. */
329 pblock = (struct pending_block *)
1ab3bf1b 330 obstack_alloc (&objfile -> symbol_obstack,
c0302457
JG
331 sizeof (struct pending_block));
332 pblock->block = block;
333 if (opblock)
334 {
335 pblock->next = opblock->next;
336 opblock->next = pblock;
337 }
338 else
339 {
340 pblock->next = pending_blocks;
341 pending_blocks = pblock;
342 }
343}
344
1ab3bf1b
JG
345static struct blockvector *
346make_blockvector (objfile)
c438b3af 347 struct objfile *objfile;
c0302457
JG
348{
349 register struct pending_block *next;
350 register struct blockvector *blockvector;
351 register int i;
352
353 /* Count the length of the list of blocks. */
354
d07734e3 355 for (next = pending_blocks, i = 0; next; next = next->next, i++) {;}
c0302457
JG
356
357 blockvector = (struct blockvector *)
1ab3bf1b 358 obstack_alloc (&objfile -> symbol_obstack,
c0302457
JG
359 (sizeof (struct blockvector)
360 + (i - 1) * sizeof (struct block *)));
361
362 /* Copy the blocks into the blockvector.
363 This is done in reverse order, which happens to put
364 the blocks into the proper order (ascending starting address).
365 finish_block has hair to insert each block into the list
366 after its subblocks in order to make sure this is true. */
367
368 BLOCKVECTOR_NBLOCKS (blockvector) = i;
d07734e3
FF
369 for (next = pending_blocks; next; next = next->next)
370 {
371 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
372 }
c0302457
JG
373
374#if 0 /* Now we make the links in the obstack, so don't free them. */
375 /* Now free the links of the list, and empty the list. */
376
377 for (next = pending_blocks; next; next = next1)
378 {
379 next1 = next->next;
380 free (next);
381 }
382#endif
d07734e3 383 pending_blocks = NULL;
c0302457
JG
384
385#if 1 /* FIXME, shut this off after a while to speed up symbol reading. */
386 /* Some compilers output blocks in the wrong order, but we depend
387 on their being in the right order so we can binary search.
388 Check the order and moan about it. FIXME. */
389 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
d07734e3
FF
390 {
391 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
392 {
393 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1))
394 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)))
395 {
833e0d94
JK
396
397 /* FIXME-32x64: loses if CORE_ADDR doesn't fit in a
398 long. Possible solutions include a version of
399 complain which takes a callback, a
400 sprintf_address_numeric to match
401 print_address_numeric, or a way to set up a GDB_FILE
402 * which causes sprintf rather than fprintf to be
403 called. */
404
d07734e3 405 complain (&blockvector_complaint,
5573d7d4 406 (unsigned long) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)));
d07734e3
FF
407 }
408 }
c0302457
JG
409 }
410#endif
411
d07734e3 412 return (blockvector);
c0302457 413}
d07734e3 414
c0302457 415\f
4137c5fc 416/* Start recording information about source code that came from an included
c438b3af
JK
417 (or otherwise merged-in) source file with a different name. NAME is
418 the name of the file (cannot be NULL), DIRNAME is the directory in which
419 it resides (or NULL if not known). */
c0302457
JG
420
421void
4137c5fc
JG
422start_subfile (name, dirname)
423 char *name;
424 char *dirname;
425{
426 register struct subfile *subfile;
427
428 /* See if this subfile is already known as a subfile of the
429 current main source file. */
430
431 for (subfile = subfiles; subfile; subfile = subfile->next)
432 {
2e4964ad 433 if (STREQ (subfile->name, name))
4137c5fc
JG
434 {
435 current_subfile = subfile;
436 return;
437 }
438 }
439
440 /* This subfile is not known. Add an entry for it.
441 Make an entry for this subfile in the list of all subfiles
442 of the current main source file. */
443
444 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
445 subfile->next = subfiles;
446 subfiles = subfile;
447 current_subfile = subfile;
448
449 /* Save its name and compilation directory name */
bb6247c6
JK
450 subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
451 subfile->dirname =
452 (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
4137c5fc
JG
453
454 /* Initialize line-number recording for this subfile. */
d07734e3 455 subfile->line_vector = NULL;
2e4964ad
FF
456
457 /* Default the source language to whatever can be deduced from
458 the filename. If nothing can be deduced (such as for a C/C++
459 include file with a ".h" extension), then inherit whatever
460 language the previous subfile had. This kludgery is necessary
461 because there is no standard way in some object formats to
462 record the source language. Also, when symtabs are allocated
463 we try to deduce a language then as well, but it is too late
464 for us to use that information while reading symbols, since
465 symtabs aren't allocated until after all the symbols have
466 been processed for a given source file. */
467
468 subfile->language = deduce_language_from_filename (subfile->name);
469 if (subfile->language == language_unknown &&
470 subfile->next != NULL)
471 {
472 subfile->language = subfile->next->language;
473 }
56ad756a
JK
474
475 /* cfront output is a C program, so in most ways it looks like a C
476 program. But to demangle we need to set the language to C++. We
477 can distinguish cfront code by the fact that it has #line
478 directives which specify a file name ending in .C.
479
480 So if the filename of this subfile ends in .C, then change the language
8b05f64a
JK
481 of any pending subfiles from C to C++. We also accept any other C++
482 suffixes accepted by deduce_language_from_filename (in particular,
483 some people use .cxx with cfront). */
21af55c9 484 /* Likewise for f2c. */
56ad756a
JK
485
486 if (subfile->name)
487 {
56ad756a 488 struct subfile *s;
21af55c9 489 enum language sublang = deduce_language_from_filename (subfile->name);
56ad756a 490
21af55c9 491 if (sublang == language_cplus || sublang == language_fortran)
56ad756a
JK
492 for (s = subfiles; s != NULL; s = s->next)
493 if (s->language == language_c)
21af55c9 494 s->language = sublang;
56ad756a
JK
495 }
496
497 /* And patch up this file if necessary. */
498 if (subfile->language == language_c
499 && subfile->next != NULL
21af55c9
JK
500 && (subfile->next->language == language_cplus
501 || subfile->next->language == language_fortran))
56ad756a 502 {
21af55c9 503 subfile->language = subfile->next->language;
56ad756a 504 }
4137c5fc 505}
d07734e3 506
3416d90b
FF
507/* For stabs readers, the first N_SO symbol is assumed to be the source
508 file name, and the subfile struct is initialized using that assumption.
509 If another N_SO symbol is later seen, immediately following the first
510 one, then the first one is assumed to be the directory name and the
511 second one is really the source file name.
512
513 So we have to patch up the subfile struct by moving the old name value to
514 dirname and remembering the new name. Some sanity checking is performed
515 to ensure that the state of the subfile struct is reasonable and that the
516 old name we are assuming to be a directory name actually is (by checking
517 for a trailing '/'). */
518
519void
520patch_subfile_names (subfile, name)
521 struct subfile *subfile;
522 char *name;
523{
524 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
525 && subfile->name[strlen(subfile->name)-1] == '/')
526 {
527 subfile->dirname = subfile->name;
bb6247c6 528 subfile->name = savestring (name, strlen (name));
b9e58503 529 last_source_file = name;
2e4964ad
FF
530
531 /* Default the source language to whatever can be deduced from
532 the filename. If nothing can be deduced (such as for a C/C++
533 include file with a ".h" extension), then inherit whatever
534 language the previous subfile had. This kludgery is necessary
535 because there is no standard way in some object formats to
536 record the source language. Also, when symtabs are allocated
537 we try to deduce a language then as well, but it is too late
538 for us to use that information while reading symbols, since
539 symtabs aren't allocated until after all the symbols have
540 been processed for a given source file. */
541
542 subfile->language = deduce_language_from_filename (subfile->name);
543 if (subfile->language == language_unknown &&
544 subfile->next != NULL)
545 {
546 subfile->language = subfile->next->language;
547 }
3416d90b
FF
548 }
549}
550
4137c5fc 551\f
a048c8f5
JG
552/* Handle the N_BINCL and N_EINCL symbol types
553 that act like N_SOL for switching source files
554 (different subfiles, as we call them) within one object file,
555 but using a stack rather than in an arbitrary order. */
556
557void
558push_subfile ()
559{
560 register struct subfile_stack *tem
561 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
562
563 tem->next = subfile_stack;
564 subfile_stack = tem;
d07734e3
FF
565 if (current_subfile == NULL || current_subfile->name == NULL)
566 {
567 abort ();
568 }
a048c8f5 569 tem->name = current_subfile->name;
a048c8f5
JG
570}
571
572char *
573pop_subfile ()
574{
575 register char *name;
576 register struct subfile_stack *link = subfile_stack;
577
d07734e3
FF
578 if (link == NULL)
579 {
580 abort ();
581 }
a048c8f5
JG
582 name = link->name;
583 subfile_stack = link->next;
84ffdec2 584 free ((PTR)link);
d07734e3 585 return (name);
a048c8f5 586}
d07734e3 587
a048c8f5 588\f
be7d4f3f
JK
589/* Add a linetable entry for line number LINE and address PC to the line
590 vector for SUBFILE. */
4137c5fc
JG
591
592void
593record_line (subfile, line, pc)
594 register struct subfile *subfile;
c0302457
JG
595 int line;
596 CORE_ADDR pc;
597{
598 struct linetable_entry *e;
599 /* Ignore the dummy line number in libg.o */
600
601 if (line == 0xffff)
d07734e3
FF
602 {
603 return;
604 }
c0302457 605
4137c5fc 606 /* Make sure line vector exists and is big enough. */
d07734e3
FF
607 if (!subfile->line_vector)
608 {
609 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
610 subfile->line_vector = (struct linetable *)
4137c5fc
JG
611 xmalloc (sizeof (struct linetable)
612 + subfile->line_vector_length * sizeof (struct linetable_entry));
d07734e3
FF
613 subfile->line_vector->nitems = 0;
614 }
c0302457 615
4137c5fc 616 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
c0302457 617 {
4137c5fc
JG
618 subfile->line_vector_length *= 2;
619 subfile->line_vector = (struct linetable *)
1ab3bf1b 620 xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable)
d07734e3 621 + subfile->line_vector_length * sizeof (struct linetable_entry)));
c0302457
JG
622 }
623
4137c5fc 624 e = subfile->line_vector->item + subfile->line_vector->nitems++;
c0302457
JG
625 e->line = line; e->pc = pc;
626}
4137c5fc
JG
627
628
629/* Needed in order to sort line tables from IBM xcoff files. Sigh! */
630
1ab3bf1b
JG
631static int
632compare_line_numbers (ln1p, ln2p)
633 const PTR ln1p;
634 const PTR ln2p;
4137c5fc 635{
c438b3af
JK
636 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
637 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
638
639 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
640 Please keep it that way. */
641 if (ln1->pc < ln2->pc)
642 return -1;
643
644 if (ln1->pc > ln2->pc)
645 return 1;
646
647 /* If pc equal, sort by line. I'm not sure whether this is optimum
648 behavior (see comment at struct linetable in symtab.h). */
649 return ln1->line - ln2->line;
4137c5fc 650}
1ab3bf1b 651
c0302457
JG
652\f
653/* Start a new symtab for a new source file.
d07734e3
FF
654 Called, for example, when a stabs symbol of type N_SO is seen, or when
655 a DWARF TAG_compile_unit DIE is seen.
656 It indicates the start of data for one original source file. */
c0302457
JG
657
658void
659start_symtab (name, dirname, start_addr)
660 char *name;
661 char *dirname;
662 CORE_ADDR start_addr;
663{
664
665 last_source_file = name;
666 last_source_start_addr = start_addr;
d07734e3
FF
667 file_symbols = NULL;
668 global_symbols = NULL;
c0302457
JG
669 within_function = 0;
670
a048c8f5
JG
671 /* Context stack is initially empty. Allocate first one with room for
672 10 levels; reuse it forever afterward. */
d07734e3
FF
673 if (context_stack == NULL)
674 {
675 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
676 context_stack = (struct context_stack *)
677 xmalloc (context_stack_size * sizeof (struct context_stack));
678 }
c0302457
JG
679 context_stack_depth = 0;
680
c0302457
JG
681 /* Initialize the list of sub source files with one entry
682 for this file (the top-level source file). */
683
d07734e3
FF
684 subfiles = NULL;
685 current_subfile = NULL;
c0302457
JG
686 start_subfile (name, dirname);
687}
688
689/* Finish the symbol definitions for one main source file,
690 close off all the lexical contexts for that file
691 (creating struct block's for them), then make the struct symtab
692 for that file and put it in the list of all such.
693
7b5d9650 694 END_ADDR is the address of the end of the file's text.
3c02636b
JK
695 SECTION is the section number (in objfile->section_offsets) of
696 the blockvector and linetable.
7b5d9650
FF
697
698 Note that it is possible for end_symtab() to return NULL. In particular,
699 for the DWARF case at least, it will return NULL when it finds a
700 compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This
701 can happen when we link in an object file that was compiled from an empty
702 source file. Returning NULL is probably not the correct thing to do,
703 because then gdb will never know about this empty file (FIXME). */
c0302457
JG
704
705struct symtab *
3c02636b 706end_symtab (end_addr, sort_pending, sort_linevec, objfile, section)
c0302457 707 CORE_ADDR end_addr;
4137c5fc
JG
708 int sort_pending;
709 int sort_linevec;
a048c8f5 710 struct objfile *objfile;
3c02636b 711 int section;
c0302457 712{
fee933f1 713 register struct symtab *symtab = NULL;
c0302457
JG
714 register struct blockvector *blockvector;
715 register struct subfile *subfile;
d07734e3 716 register struct context_stack *cstk;
c0302457
JG
717 struct subfile *nextsub;
718
719 /* Finish the lexical context of the last function in the file;
720 pop the context stack. */
721
722 if (context_stack_depth > 0)
723 {
c0302457
JG
724 context_stack_depth--;
725 cstk = &context_stack[context_stack_depth];
726 /* Make a block for the local symbols within. */
727 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1ab3bf1b 728 cstk->start_addr, end_addr, objfile);
a048c8f5 729
a048c8f5 730 if (context_stack_depth > 0)
d07734e3 731 {
f91b837d
JK
732 /* This is said to happen with SCO. The old coffread.c code
733 simply emptied the context stack, so we do the same. FIXME:
734 Find out why it is happening. This is not believed to happen
735 in most cases (even for coffread.c); it used to be an abort(). */
736 static struct complaint msg =
737 {"Context stack not empty in end_symtab", 0, 0};
738 complain (&msg);
739 context_stack_depth = 0;
d07734e3 740 }
c0302457
JG
741 }
742
9b280a7f 743 /* It is unfortunate that in xcoff, pending blocks might not be ordered
4137c5fc
JG
744 in this stage. Especially, blocks for static functions will show up at
745 the end. We need to sort them, so tools like `find_pc_function' and
746 `find_pc_block' can work reliably. */
d07734e3
FF
747
748 if (sort_pending && pending_blocks)
749 {
750 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
751 int swapped;
752 do
753 {
754 struct pending_block *pb, *pbnext;
755
756 pb = pending_blocks;
757 pbnext = pb->next;
758 swapped = 0;
759
760 while (pbnext)
761 {
762 /* swap blocks if unordered! */
763
764 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block))
765 {
766 struct block *tmp = pb->block;
767 pb->block = pbnext->block;
768 pbnext->block = tmp;
769 swapped = 1;
770 }
771 pb = pbnext;
772 pbnext = pbnext->next;
773 }
774 } while (swapped);
775 }
4137c5fc 776
c0302457
JG
777 /* Cleanup any undefined types that have been left hanging around
778 (this needs to be done before the finish_blocks so that
d07734e3 779 file_symbols is still good).
c438b3af
JK
780
781 Both cleanup_undefined_types and finish_global_stabs are stabs
782 specific, but harmless for other symbol readers, since on gdb
783 startup or when finished reading stabs, the state is set so these
784 are no-ops. FIXME: Is this handled right in case of QUIT? Can
785 we make this cleaner? */
786
c0302457 787 cleanup_undefined_types ();
d07734e3 788 finish_global_stabs (objfile);
c0302457 789
d07734e3
FF
790 if (pending_blocks == NULL
791 && file_symbols == NULL
792 && global_symbols == NULL)
793 {
794 /* Ignore symtabs that have no functions with real debugging info */
795 blockvector = NULL;
796 }
797 else
798 {
799 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
800 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
801 objfile);
802 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
803 objfile);
804 blockvector = make_blockvector (objfile);
805 }
c0302457 806
818de002 807#ifdef PROCESS_LINENUMBER_HOOK
9b280a7f 808 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
818de002
PB
809#endif
810
c0302457
JG
811 /* Now create the symtab objects proper, one for each subfile. */
812 /* (The main file is the last one on the chain.) */
813
814 for (subfile = subfiles; subfile; subfile = nextsub)
815 {
fee933f1 816 int linetablesize = 0;
a048c8f5
JG
817 /* If we have blocks of symbols, make a symtab.
818 Otherwise, just ignore this file and any line number info in it. */
d07734e3
FF
819 symtab = NULL;
820 if (blockvector)
821 {
822 if (subfile->line_vector)
823 {
d07734e3
FF
824 linetablesize = sizeof (struct linetable) +
825 subfile->line_vector->nitems * sizeof (struct linetable_entry);
c438b3af
JK
826#if 0
827 /* I think this is artifact from before it went on the obstack.
828 I doubt we'll need the memory between now and when we
829 free it later in this function. */
830 /* First, shrink the linetable to make more memory. */
d07734e3
FF
831 subfile->line_vector = (struct linetable *)
832 xrealloc ((char *) subfile->line_vector, linetablesize);
c438b3af
JK
833#endif
834 /* If sort_linevec is false, we might want just check to make
835 sure they are sorted and complain() if not, as a way of
836 tracking down compilers/symbol readers which don't get
837 them sorted right. */
d07734e3
FF
838
839 if (sort_linevec)
840 qsort (subfile->line_vector->item,
841 subfile->line_vector->nitems,
842 sizeof (struct linetable_entry), compare_line_numbers);
843 }
844
845 /* Now, allocate a symbol table. */
846 symtab = allocate_symtab (subfile->name, objfile);
4137c5fc 847
d07734e3
FF
848 /* Fill in its components. */
849 symtab->blockvector = blockvector;
850 if (subfile->line_vector)
851 {
852 /* Reallocate the line table on the symbol obstack */
853 symtab->linetable = (struct linetable *)
854 obstack_alloc (&objfile -> symbol_obstack, linetablesize);
855 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
856 }
857 else
858 {
859 symtab->linetable = NULL;
860 }
3c02636b 861 symtab->block_line_section = section;
8275e802
FF
862 if (subfile->dirname)
863 {
864 /* Reallocate the dirname on the symbol obstack */
865 symtab->dirname = (char *)
866 obstack_alloc (&objfile -> symbol_obstack,
867 strlen (subfile -> dirname) + 1);
868 strcpy (symtab->dirname, subfile->dirname);
869 }
870 else
871 {
872 symtab->dirname = NULL;
873 }
d07734e3
FF
874 symtab->free_code = free_linetable;
875 symtab->free_ptr = NULL;
2b5a8d9c 876
2e4964ad
FF
877 /* Use whatever language we have been using for this subfile,
878 not the one that was deduced in allocate_symtab from the
879 filename. We already did our own deducing when we created
880 the subfile, and we may have altered our opinion of what
881 language it is from things we found in the symbols. */
882 symtab->language = subfile->language;
883
3c02636b
JK
884 /* All symtabs for the main file and the subfiles share a
885 blockvector, so we need to clear primary for everything but
886 the main file. */
2b5a8d9c 887
3c02636b 888 symtab->primary = 0;
d07734e3 889 }
3416d90b
FF
890 if (subfile->name != NULL)
891 {
892 free ((PTR) subfile->name);
893 }
894 if (subfile->dirname != NULL)
895 {
896 free ((PTR) subfile->dirname);
897 }
898 if (subfile->line_vector != NULL)
d07734e3 899 {
3416d90b 900 free ((PTR) subfile->line_vector);
d07734e3 901 }
4137c5fc 902
c0302457 903 nextsub = subfile->next;
84ffdec2 904 free ((PTR)subfile);
c0302457
JG
905 }
906
3c02636b 907 /* Set this for the main source file. */
1eeba686 908 if (symtab)
d07734e3 909 {
3c02636b 910 symtab->primary = 1;
d07734e3 911 }
2b5a8d9c 912
d07734e3
FF
913 last_source_file = NULL;
914 current_subfile = NULL;
4137c5fc 915
d07734e3 916 return (symtab);
c0302457 917}
a048c8f5
JG
918
919
920/* Push a context block. Args are an identifying nesting level (checkable
921 when you pop it), and the starting PC address of this context. */
922
923struct context_stack *
924push_context (desc, valu)
925 int desc;
926 CORE_ADDR valu;
927{
928 register struct context_stack *new;
929
930 if (context_stack_depth == context_stack_size)
931 {
932 context_stack_size *= 2;
933 context_stack = (struct context_stack *)
1ab3bf1b
JG
934 xrealloc ((char *) context_stack,
935 (context_stack_size * sizeof (struct context_stack)));
a048c8f5
JG
936 }
937
938 new = &context_stack[context_stack_depth++];
939 new->depth = desc;
940 new->locals = local_symbols;
941 new->old_blocks = pending_blocks;
942 new->start_addr = valu;
d07734e3 943 new->name = NULL;
a048c8f5 944
d07734e3 945 local_symbols = NULL;
a048c8f5 946
d07734e3 947 return (new);
a048c8f5 948}
d07734e3 949
48f075eb
SS
950\f
951/* Compute a small integer hash code for the given name. */
952
953int
954hashname (name)
955 char *name;
956{
957 register char *p = name;
958 register int total = p[0];
959 register int c;
960
961 c = p[1];
962 total += c << 2;
963 if (c)
964 {
965 c = p[2];
966 total += c << 4;
967 if (c)
968 {
969 total += p[3] << 6;
970 }
971 }
972
973 /* Ensure result is positive. */
974 if (total < 0)
975 {
976 total += (1000 << 6);
977 }
978 return (total % HASHSIZE);
979}
980
c0302457
JG
981\f
982/* Initialize anything that needs initializing when starting to read
983 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
984 to a psymtab. */
985
986void
987buildsym_init ()
988{
d07734e3
FF
989 free_pendings = NULL;
990 file_symbols = NULL;
991 global_symbols = NULL;
992 pending_blocks = NULL;
c0302457
JG
993}
994
995/* Initialize anything that needs initializing when a completely new
996 symbol file is specified (not just adding some symbols from another
997 file, e.g. a shared library). */
998
999void
1000buildsym_new_init ()
1001{
c0302457
JG
1002 buildsym_init ();
1003}
1004
d07734e3 1005/* Initializer for this module */
095db7ce 1006
c0302457
JG
1007void
1008_initialize_buildsym ()
1009{
c0302457 1010}
This page took 0.20961 seconds and 4 git commands to generate.