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