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