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