Back out change to signals.exp (test_handle_all_print): Add setup_xfail for "alpha...
[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
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++;
6b14af2b
FF
250 break;
251 case LOC_UNDEF:
252 case LOC_CONST:
253 case LOC_STATIC:
254 case LOC_REGISTER:
255 case LOC_LOCAL:
256 case LOC_TYPEDEF:
257 case LOC_LABEL:
258 case LOC_BLOCK:
259 case LOC_CONST_BYTES:
260 case LOC_LOCAL_ARG:
261 case LOC_BASEREG:
262 case LOC_BASEREG_ARG:
b1027aa4 263 case LOC_UNRESOLVED:
6b14af2b
FF
264 case LOC_OPTIMIZED_OUT:
265 default:
266 break;
27202b6a
PB
267 }
268 }
269 if (nparams > 0)
270 {
271 TYPE_NFIELDS (ftype) = nparams;
272 TYPE_FIELDS (ftype) = (struct field *)
273 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
274
275 for (i = iparams = 0; iparams < nparams; i++)
276 {
277 sym = BLOCK_SYM (block, i);
278 switch (SYMBOL_CLASS (sym))
279 {
280 case LOC_ARG:
281 case LOC_REF_ARG:
282 case LOC_REGPARM:
283 case LOC_REGPARM_ADDR:
284 TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
285 iparams++;
6b14af2b
FF
286 break;
287 case LOC_UNDEF:
288 case LOC_CONST:
289 case LOC_STATIC:
290 case LOC_REGISTER:
291 case LOC_LOCAL:
292 case LOC_TYPEDEF:
293 case LOC_LABEL:
294 case LOC_BLOCK:
295 case LOC_CONST_BYTES:
296 case LOC_LOCAL_ARG:
297 case LOC_BASEREG:
298 case LOC_BASEREG_ARG:
b1027aa4 299 case LOC_UNRESOLVED:
6b14af2b
FF
300 case LOC_OPTIMIZED_OUT:
301 default:
302 break;
27202b6a
PB
303 }
304 }
305 }
306 }
c0302457
JG
307 }
308 else
d07734e3
FF
309 {
310 BLOCK_FUNCTION (block) = NULL;
311 }
c0302457
JG
312
313 /* Now "free" the links of the list, and empty the list. */
314
315 for (next = *listhead; next; next = next1)
316 {
317 next1 = next->next;
318 next->next = free_pendings;
319 free_pendings = next;
320 }
d07734e3 321 *listhead = NULL;
c0302457
JG
322
323 /* Install this block as the superblock
324 of all blocks made since the start of this scope
325 that don't have superblocks yet. */
326
d07734e3 327 opblock = NULL;
c0302457
JG
328 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
329 {
d07734e3
FF
330 if (BLOCK_SUPERBLOCK (pblock->block) == NULL)
331 {
c0302457 332#if 1
d07734e3
FF
333 /* Check to be sure the blocks are nested as we receive them.
334 If the compiler/assembler/linker work, this just burns a small
335 amount of time. */
336 if (BLOCK_START (pblock->block) < BLOCK_START (block) ||
337 BLOCK_END (pblock->block) > BLOCK_END (block))
338 {
339 if (symbol)
340 {
2e4964ad
FF
341 complain (&innerblock_complaint,
342 SYMBOL_SOURCE_NAME (symbol));
d07734e3
FF
343 }
344 else
345 {
51b80b00 346 complain (&innerblock_anon_complaint);
d07734e3
FF
347 }
348 BLOCK_START (pblock->block) = BLOCK_START (block);
349 BLOCK_END (pblock->block) = BLOCK_END (block);
350 }
c0302457 351#endif
d07734e3
FF
352 BLOCK_SUPERBLOCK (pblock->block) = block;
353 }
c0302457
JG
354 opblock = pblock;
355 }
356
357 /* Record this block on the list of all blocks in the file.
358 Put it after opblock, or at the beginning if opblock is 0.
359 This puts the block in the list after all its subblocks. */
360
361 /* Allocate in the symbol_obstack to save time.
362 It wastes a little space. */
363 pblock = (struct pending_block *)
1ab3bf1b 364 obstack_alloc (&objfile -> symbol_obstack,
c0302457
JG
365 sizeof (struct pending_block));
366 pblock->block = block;
367 if (opblock)
368 {
369 pblock->next = opblock->next;
370 opblock->next = pblock;
371 }
372 else
373 {
374 pblock->next = pending_blocks;
375 pending_blocks = pblock;
376 }
377}
378
1ab3bf1b
JG
379static struct blockvector *
380make_blockvector (objfile)
c438b3af 381 struct objfile *objfile;
c0302457
JG
382{
383 register struct pending_block *next;
384 register struct blockvector *blockvector;
385 register int i;
386
387 /* Count the length of the list of blocks. */
388
d07734e3 389 for (next = pending_blocks, i = 0; next; next = next->next, i++) {;}
c0302457
JG
390
391 blockvector = (struct blockvector *)
1ab3bf1b 392 obstack_alloc (&objfile -> symbol_obstack,
c0302457
JG
393 (sizeof (struct blockvector)
394 + (i - 1) * sizeof (struct block *)));
395
396 /* Copy the blocks into the blockvector.
397 This is done in reverse order, which happens to put
398 the blocks into the proper order (ascending starting address).
399 finish_block has hair to insert each block into the list
400 after its subblocks in order to make sure this is true. */
401
402 BLOCKVECTOR_NBLOCKS (blockvector) = i;
d07734e3
FF
403 for (next = pending_blocks; next; next = next->next)
404 {
405 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
406 }
c0302457
JG
407
408#if 0 /* Now we make the links in the obstack, so don't free them. */
409 /* Now free the links of the list, and empty the list. */
410
411 for (next = pending_blocks; next; next = next1)
412 {
413 next1 = next->next;
414 free (next);
415 }
416#endif
d07734e3 417 pending_blocks = NULL;
c0302457
JG
418
419#if 1 /* FIXME, shut this off after a while to speed up symbol reading. */
420 /* Some compilers output blocks in the wrong order, but we depend
421 on their being in the right order so we can binary search.
422 Check the order and moan about it. FIXME. */
423 if (BLOCKVECTOR_NBLOCKS (blockvector) > 1)
d07734e3
FF
424 {
425 for (i = 1; i < BLOCKVECTOR_NBLOCKS (blockvector); i++)
426 {
427 if (BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i-1))
428 > BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)))
429 {
833e0d94
JK
430
431 /* FIXME-32x64: loses if CORE_ADDR doesn't fit in a
432 long. Possible solutions include a version of
433 complain which takes a callback, a
434 sprintf_address_numeric to match
435 print_address_numeric, or a way to set up a GDB_FILE
436 * which causes sprintf rather than fprintf to be
437 called. */
438
d07734e3 439 complain (&blockvector_complaint,
5573d7d4 440 (unsigned long) BLOCK_START(BLOCKVECTOR_BLOCK (blockvector, i)));
d07734e3
FF
441 }
442 }
c0302457
JG
443 }
444#endif
445
d07734e3 446 return (blockvector);
c0302457 447}
d07734e3 448
c0302457 449\f
4137c5fc 450/* Start recording information about source code that came from an included
c438b3af
JK
451 (or otherwise merged-in) source file with a different name. NAME is
452 the name of the file (cannot be NULL), DIRNAME is the directory in which
453 it resides (or NULL if not known). */
c0302457
JG
454
455void
4137c5fc
JG
456start_subfile (name, dirname)
457 char *name;
458 char *dirname;
459{
460 register struct subfile *subfile;
461
462 /* See if this subfile is already known as a subfile of the
463 current main source file. */
464
465 for (subfile = subfiles; subfile; subfile = subfile->next)
466 {
2e4964ad 467 if (STREQ (subfile->name, name))
4137c5fc
JG
468 {
469 current_subfile = subfile;
470 return;
471 }
472 }
473
474 /* This subfile is not known. Add an entry for it.
475 Make an entry for this subfile in the list of all subfiles
476 of the current main source file. */
477
478 subfile = (struct subfile *) xmalloc (sizeof (struct subfile));
479 subfile->next = subfiles;
480 subfiles = subfile;
481 current_subfile = subfile;
482
483 /* Save its name and compilation directory name */
bb6247c6
JK
484 subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
485 subfile->dirname =
486 (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
4137c5fc
JG
487
488 /* Initialize line-number recording for this subfile. */
d07734e3 489 subfile->line_vector = NULL;
2e4964ad
FF
490
491 /* Default the source language to whatever can be deduced from
492 the filename. If nothing can be deduced (such as for a C/C++
493 include file with a ".h" extension), then inherit whatever
494 language the previous subfile had. This kludgery is necessary
495 because there is no standard way in some object formats to
496 record the source language. Also, when symtabs are allocated
497 we try to deduce a language then as well, but it is too late
498 for us to use that information while reading symbols, since
499 symtabs aren't allocated until after all the symbols have
500 been processed for a given source file. */
501
502 subfile->language = deduce_language_from_filename (subfile->name);
503 if (subfile->language == language_unknown &&
504 subfile->next != NULL)
505 {
506 subfile->language = subfile->next->language;
507 }
56ad756a
JK
508
509 /* cfront output is a C program, so in most ways it looks like a C
510 program. But to demangle we need to set the language to C++. We
511 can distinguish cfront code by the fact that it has #line
512 directives which specify a file name ending in .C.
513
514 So if the filename of this subfile ends in .C, then change the language
8b05f64a
JK
515 of any pending subfiles from C to C++. We also accept any other C++
516 suffixes accepted by deduce_language_from_filename (in particular,
517 some people use .cxx with cfront). */
21af55c9 518 /* Likewise for f2c. */
56ad756a
JK
519
520 if (subfile->name)
521 {
56ad756a 522 struct subfile *s;
21af55c9 523 enum language sublang = deduce_language_from_filename (subfile->name);
56ad756a 524
21af55c9 525 if (sublang == language_cplus || sublang == language_fortran)
56ad756a
JK
526 for (s = subfiles; s != NULL; s = s->next)
527 if (s->language == language_c)
21af55c9 528 s->language = sublang;
56ad756a
JK
529 }
530
531 /* And patch up this file if necessary. */
532 if (subfile->language == language_c
533 && subfile->next != NULL
21af55c9
JK
534 && (subfile->next->language == language_cplus
535 || subfile->next->language == language_fortran))
56ad756a 536 {
21af55c9 537 subfile->language = subfile->next->language;
56ad756a 538 }
4137c5fc 539}
d07734e3 540
3416d90b
FF
541/* For stabs readers, the first N_SO symbol is assumed to be the source
542 file name, and the subfile struct is initialized using that assumption.
543 If another N_SO symbol is later seen, immediately following the first
544 one, then the first one is assumed to be the directory name and the
545 second one is really the source file name.
546
547 So we have to patch up the subfile struct by moving the old name value to
548 dirname and remembering the new name. Some sanity checking is performed
549 to ensure that the state of the subfile struct is reasonable and that the
550 old name we are assuming to be a directory name actually is (by checking
551 for a trailing '/'). */
552
553void
554patch_subfile_names (subfile, name)
555 struct subfile *subfile;
556 char *name;
557{
558 if (subfile != NULL && subfile->dirname == NULL && subfile->name != NULL
559 && subfile->name[strlen(subfile->name)-1] == '/')
560 {
561 subfile->dirname = subfile->name;
bb6247c6 562 subfile->name = savestring (name, strlen (name));
b9e58503 563 last_source_file = name;
2e4964ad
FF
564
565 /* Default the source language to whatever can be deduced from
566 the filename. If nothing can be deduced (such as for a C/C++
567 include file with a ".h" extension), then inherit whatever
568 language the previous subfile had. This kludgery is necessary
569 because there is no standard way in some object formats to
570 record the source language. Also, when symtabs are allocated
571 we try to deduce a language then as well, but it is too late
572 for us to use that information while reading symbols, since
573 symtabs aren't allocated until after all the symbols have
574 been processed for a given source file. */
575
576 subfile->language = deduce_language_from_filename (subfile->name);
577 if (subfile->language == language_unknown &&
578 subfile->next != NULL)
579 {
580 subfile->language = subfile->next->language;
581 }
3416d90b
FF
582 }
583}
584
4137c5fc 585\f
a048c8f5
JG
586/* Handle the N_BINCL and N_EINCL symbol types
587 that act like N_SOL for switching source files
588 (different subfiles, as we call them) within one object file,
589 but using a stack rather than in an arbitrary order. */
590
591void
592push_subfile ()
593{
594 register struct subfile_stack *tem
595 = (struct subfile_stack *) xmalloc (sizeof (struct subfile_stack));
596
597 tem->next = subfile_stack;
598 subfile_stack = tem;
d07734e3
FF
599 if (current_subfile == NULL || current_subfile->name == NULL)
600 {
601 abort ();
602 }
a048c8f5 603 tem->name = current_subfile->name;
a048c8f5
JG
604}
605
606char *
607pop_subfile ()
608{
609 register char *name;
610 register struct subfile_stack *link = subfile_stack;
611
d07734e3
FF
612 if (link == NULL)
613 {
614 abort ();
615 }
a048c8f5
JG
616 name = link->name;
617 subfile_stack = link->next;
84ffdec2 618 free ((PTR)link);
d07734e3 619 return (name);
a048c8f5 620}
d07734e3 621
a048c8f5 622\f
be7d4f3f
JK
623/* Add a linetable entry for line number LINE and address PC to the line
624 vector for SUBFILE. */
4137c5fc
JG
625
626void
627record_line (subfile, line, pc)
628 register struct subfile *subfile;
c0302457
JG
629 int line;
630 CORE_ADDR pc;
631{
632 struct linetable_entry *e;
633 /* Ignore the dummy line number in libg.o */
634
635 if (line == 0xffff)
d07734e3
FF
636 {
637 return;
638 }
c0302457 639
4137c5fc 640 /* Make sure line vector exists and is big enough. */
d07734e3
FF
641 if (!subfile->line_vector)
642 {
643 subfile->line_vector_length = INITIAL_LINE_VECTOR_LENGTH;
644 subfile->line_vector = (struct linetable *)
4137c5fc
JG
645 xmalloc (sizeof (struct linetable)
646 + subfile->line_vector_length * sizeof (struct linetable_entry));
d07734e3
FF
647 subfile->line_vector->nitems = 0;
648 }
c0302457 649
4137c5fc 650 if (subfile->line_vector->nitems + 1 >= subfile->line_vector_length)
c0302457 651 {
4137c5fc
JG
652 subfile->line_vector_length *= 2;
653 subfile->line_vector = (struct linetable *)
1ab3bf1b 654 xrealloc ((char *) subfile->line_vector, (sizeof (struct linetable)
d07734e3 655 + subfile->line_vector_length * sizeof (struct linetable_entry)));
c0302457
JG
656 }
657
4137c5fc 658 e = subfile->line_vector->item + subfile->line_vector->nitems++;
c0302457
JG
659 e->line = line; e->pc = pc;
660}
4137c5fc
JG
661
662
663/* Needed in order to sort line tables from IBM xcoff files. Sigh! */
664
1ab3bf1b
JG
665static int
666compare_line_numbers (ln1p, ln2p)
667 const PTR ln1p;
668 const PTR ln2p;
4137c5fc 669{
c438b3af
JK
670 struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
671 struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
672
673 /* Note: this code does not assume that CORE_ADDRs can fit in ints.
674 Please keep it that way. */
675 if (ln1->pc < ln2->pc)
676 return -1;
677
678 if (ln1->pc > ln2->pc)
679 return 1;
680
681 /* If pc equal, sort by line. I'm not sure whether this is optimum
682 behavior (see comment at struct linetable in symtab.h). */
683 return ln1->line - ln2->line;
4137c5fc 684}
1ab3bf1b 685
c0302457
JG
686\f
687/* Start a new symtab for a new source file.
d07734e3
FF
688 Called, for example, when a stabs symbol of type N_SO is seen, or when
689 a DWARF TAG_compile_unit DIE is seen.
690 It indicates the start of data for one original source file. */
c0302457
JG
691
692void
693start_symtab (name, dirname, start_addr)
694 char *name;
695 char *dirname;
696 CORE_ADDR start_addr;
697{
698
699 last_source_file = name;
700 last_source_start_addr = start_addr;
d07734e3
FF
701 file_symbols = NULL;
702 global_symbols = NULL;
c0302457
JG
703 within_function = 0;
704
a048c8f5
JG
705 /* Context stack is initially empty. Allocate first one with room for
706 10 levels; reuse it forever afterward. */
d07734e3
FF
707 if (context_stack == NULL)
708 {
709 context_stack_size = INITIAL_CONTEXT_STACK_SIZE;
710 context_stack = (struct context_stack *)
711 xmalloc (context_stack_size * sizeof (struct context_stack));
712 }
c0302457
JG
713 context_stack_depth = 0;
714
c0302457
JG
715 /* Initialize the list of sub source files with one entry
716 for this file (the top-level source file). */
717
d07734e3
FF
718 subfiles = NULL;
719 current_subfile = NULL;
c0302457
JG
720 start_subfile (name, dirname);
721}
722
723/* Finish the symbol definitions for one main source file,
724 close off all the lexical contexts for that file
725 (creating struct block's for them), then make the struct symtab
726 for that file and put it in the list of all such.
727
7b5d9650 728 END_ADDR is the address of the end of the file's text.
3c02636b
JK
729 SECTION is the section number (in objfile->section_offsets) of
730 the blockvector and linetable.
7b5d9650
FF
731
732 Note that it is possible for end_symtab() to return NULL. In particular,
733 for the DWARF case at least, it will return NULL when it finds a
734 compilation unit that has exactly one DIE, a TAG_compile_unit DIE. This
735 can happen when we link in an object file that was compiled from an empty
736 source file. Returning NULL is probably not the correct thing to do,
737 because then gdb will never know about this empty file (FIXME). */
c0302457
JG
738
739struct symtab *
436d4143 740end_symtab (end_addr, objfile, section)
c0302457 741 CORE_ADDR end_addr;
a048c8f5 742 struct objfile *objfile;
3c02636b 743 int section;
c0302457 744{
fee933f1 745 register struct symtab *symtab = NULL;
c0302457
JG
746 register struct blockvector *blockvector;
747 register struct subfile *subfile;
d07734e3 748 register struct context_stack *cstk;
c0302457
JG
749 struct subfile *nextsub;
750
751 /* Finish the lexical context of the last function in the file;
752 pop the context stack. */
753
754 if (context_stack_depth > 0)
755 {
c0302457
JG
756 context_stack_depth--;
757 cstk = &context_stack[context_stack_depth];
758 /* Make a block for the local symbols within. */
759 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
1ab3bf1b 760 cstk->start_addr, end_addr, objfile);
a048c8f5 761
a048c8f5 762 if (context_stack_depth > 0)
d07734e3 763 {
f91b837d
JK
764 /* This is said to happen with SCO. The old coffread.c code
765 simply emptied the context stack, so we do the same. FIXME:
766 Find out why it is happening. This is not believed to happen
767 in most cases (even for coffread.c); it used to be an abort(). */
768 static struct complaint msg =
769 {"Context stack not empty in end_symtab", 0, 0};
770 complain (&msg);
771 context_stack_depth = 0;
d07734e3 772 }
c0302457
JG
773 }
774
436d4143
JL
775 /* Reordered executables may have out of order pending blocks; if
776 OBJF_REORDERED is true, then sort the pending blocks. */
777 if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
d07734e3 778 {
436d4143
JL
779 /* FIXME! Remove this horrid bubble sort and use qsort!!!
780 It'd be a whole lot easier if they weren't in a linked list!!! */
d07734e3
FF
781 int swapped;
782 do
783 {
784 struct pending_block *pb, *pbnext;
785
786 pb = pending_blocks;
787 pbnext = pb->next;
788 swapped = 0;
789
790 while (pbnext)
791 {
792 /* swap blocks if unordered! */
793
794 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block))
795 {
796 struct block *tmp = pb->block;
797 pb->block = pbnext->block;
798 pbnext->block = tmp;
799 swapped = 1;
800 }
801 pb = pbnext;
802 pbnext = pbnext->next;
803 }
804 } while (swapped);
805 }
4137c5fc 806
c0302457
JG
807 /* Cleanup any undefined types that have been left hanging around
808 (this needs to be done before the finish_blocks so that
d07734e3 809 file_symbols is still good).
c438b3af
JK
810
811 Both cleanup_undefined_types and finish_global_stabs are stabs
812 specific, but harmless for other symbol readers, since on gdb
813 startup or when finished reading stabs, the state is set so these
814 are no-ops. FIXME: Is this handled right in case of QUIT? Can
815 we make this cleaner? */
816
c0302457 817 cleanup_undefined_types ();
d07734e3 818 finish_global_stabs (objfile);
c0302457 819
d07734e3
FF
820 if (pending_blocks == NULL
821 && file_symbols == NULL
822 && global_symbols == NULL)
823 {
824 /* Ignore symtabs that have no functions with real debugging info */
825 blockvector = NULL;
826 }
827 else
828 {
829 /* Define the STATIC_BLOCK & GLOBAL_BLOCK, and build the blockvector. */
830 finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr,
831 objfile);
832 finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
833 objfile);
834 blockvector = make_blockvector (objfile);
835 }
c0302457 836
818de002 837#ifdef PROCESS_LINENUMBER_HOOK
9b280a7f 838 PROCESS_LINENUMBER_HOOK (); /* Needed for xcoff. */
818de002
PB
839#endif
840
c0302457
JG
841 /* Now create the symtab objects proper, one for each subfile. */
842 /* (The main file is the last one on the chain.) */
843
844 for (subfile = subfiles; subfile; subfile = nextsub)
845 {
fee933f1 846 int linetablesize = 0;
a048c8f5
JG
847 /* If we have blocks of symbols, make a symtab.
848 Otherwise, just ignore this file and any line number info in it. */
d07734e3
FF
849 symtab = NULL;
850 if (blockvector)
851 {
852 if (subfile->line_vector)
853 {
d07734e3
FF
854 linetablesize = sizeof (struct linetable) +
855 subfile->line_vector->nitems * sizeof (struct linetable_entry);
c438b3af
JK
856#if 0
857 /* I think this is artifact from before it went on the obstack.
858 I doubt we'll need the memory between now and when we
859 free it later in this function. */
860 /* First, shrink the linetable to make more memory. */
d07734e3
FF
861 subfile->line_vector = (struct linetable *)
862 xrealloc ((char *) subfile->line_vector, linetablesize);
c438b3af 863#endif
d07734e3 864
436d4143
JL
865 /* Like the pending blocks, the line table may be scrambled
866 in reordered executables. Sort it if OBJF_REORDERED is
867 true. */
868 if (objfile->flags & OBJF_REORDERED)
d07734e3
FF
869 qsort (subfile->line_vector->item,
870 subfile->line_vector->nitems,
871 sizeof (struct linetable_entry), compare_line_numbers);
872 }
873
874 /* Now, allocate a symbol table. */
875 symtab = allocate_symtab (subfile->name, objfile);
4137c5fc 876
d07734e3
FF
877 /* Fill in its components. */
878 symtab->blockvector = blockvector;
879 if (subfile->line_vector)
880 {
881 /* Reallocate the line table on the symbol obstack */
882 symtab->linetable = (struct linetable *)
883 obstack_alloc (&objfile -> symbol_obstack, linetablesize);
884 memcpy (symtab->linetable, subfile->line_vector, linetablesize);
885 }
886 else
887 {
888 symtab->linetable = NULL;
889 }
3c02636b 890 symtab->block_line_section = section;
8275e802
FF
891 if (subfile->dirname)
892 {
893 /* Reallocate the dirname on the symbol obstack */
894 symtab->dirname = (char *)
895 obstack_alloc (&objfile -> symbol_obstack,
896 strlen (subfile -> dirname) + 1);
897 strcpy (symtab->dirname, subfile->dirname);
898 }
899 else
900 {
901 symtab->dirname = NULL;
902 }
d07734e3
FF
903 symtab->free_code = free_linetable;
904 symtab->free_ptr = NULL;
2b5a8d9c 905
2e4964ad
FF
906 /* Use whatever language we have been using for this subfile,
907 not the one that was deduced in allocate_symtab from the
908 filename. We already did our own deducing when we created
909 the subfile, and we may have altered our opinion of what
910 language it is from things we found in the symbols. */
911 symtab->language = subfile->language;
912
3c02636b
JK
913 /* All symtabs for the main file and the subfiles share a
914 blockvector, so we need to clear primary for everything but
915 the main file. */
2b5a8d9c 916
3c02636b 917 symtab->primary = 0;
d07734e3 918 }
3416d90b
FF
919 if (subfile->name != NULL)
920 {
921 free ((PTR) subfile->name);
922 }
923 if (subfile->dirname != NULL)
924 {
925 free ((PTR) subfile->dirname);
926 }
927 if (subfile->line_vector != NULL)
d07734e3 928 {
3416d90b 929 free ((PTR) subfile->line_vector);
d07734e3 930 }
4137c5fc 931
c0302457 932 nextsub = subfile->next;
84ffdec2 933 free ((PTR)subfile);
c0302457
JG
934 }
935
3c02636b 936 /* Set this for the main source file. */
1eeba686 937 if (symtab)
d07734e3 938 {
3c02636b 939 symtab->primary = 1;
d07734e3 940 }
2b5a8d9c 941
d07734e3
FF
942 last_source_file = NULL;
943 current_subfile = NULL;
4137c5fc 944
d07734e3 945 return (symtab);
c0302457 946}
a048c8f5
JG
947
948
949/* Push a context block. Args are an identifying nesting level (checkable
950 when you pop it), and the starting PC address of this context. */
951
952struct context_stack *
953push_context (desc, valu)
954 int desc;
955 CORE_ADDR valu;
956{
957 register struct context_stack *new;
958
959 if (context_stack_depth == context_stack_size)
960 {
961 context_stack_size *= 2;
962 context_stack = (struct context_stack *)
1ab3bf1b
JG
963 xrealloc ((char *) context_stack,
964 (context_stack_size * sizeof (struct context_stack)));
a048c8f5
JG
965 }
966
967 new = &context_stack[context_stack_depth++];
968 new->depth = desc;
969 new->locals = local_symbols;
970 new->old_blocks = pending_blocks;
971 new->start_addr = valu;
d07734e3 972 new->name = NULL;
a048c8f5 973
d07734e3 974 local_symbols = NULL;
a048c8f5 975
d07734e3 976 return (new);
a048c8f5 977}
d07734e3 978
48f075eb
SS
979\f
980/* Compute a small integer hash code for the given name. */
981
982int
983hashname (name)
984 char *name;
985{
986 register char *p = name;
987 register int total = p[0];
988 register int c;
989
990 c = p[1];
991 total += c << 2;
992 if (c)
993 {
994 c = p[2];
995 total += c << 4;
996 if (c)
997 {
998 total += p[3] << 6;
999 }
1000 }
1001
1002 /* Ensure result is positive. */
1003 if (total < 0)
1004 {
1005 total += (1000 << 6);
1006 }
1007 return (total % HASHSIZE);
1008}
1009
c0302457
JG
1010\f
1011/* Initialize anything that needs initializing when starting to read
1012 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
1013 to a psymtab. */
1014
1015void
1016buildsym_init ()
1017{
d07734e3
FF
1018 free_pendings = NULL;
1019 file_symbols = NULL;
1020 global_symbols = NULL;
1021 pending_blocks = NULL;
c0302457
JG
1022}
1023
1024/* Initialize anything that needs initializing when a completely new
1025 symbol file is specified (not just adding some symbols from another
1026 file, e.g. a shared library). */
1027
1028void
1029buildsym_new_init ()
1030{
c0302457
JG
1031 buildsym_init ();
1032}
1033
d07734e3 1034/* Initializer for this module */
095db7ce 1035
c0302457
JG
1036void
1037_initialize_buildsym ()
1038{
c0302457 1039}
This page took 0.242196 seconds and 4 git commands to generate.