Update checked-in copy for convenience. (FSF releases build their own.)
[deliverable/binutils-gdb.git] / gdb / mipsread.c
CommitLineData
bd5635a1
RP
1/* Read a symbol table in MIPS' format (Third-Eye).
2 Copyright (C) 1986, 1987, 1989-1991 Free Software Foundation, Inc.
3 Contributed by Alessandro Forin (af@cs.cmu.edu) at CMU
4
5This file is part of GDB.
6
b8c50f09 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
b8c50f09
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
b8c50f09 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
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
b8c50f09
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 20
3eaebb75
SG
21/* This module provides three functions: mipscoff_symfile_init,
22 which initializes to read a symbol file; mipscoff_new_init, which
23 discards existing cached information when all symbols are being
24 discarded; and mipscoff_symfile_read, which reads a symbol table
25 from a file.
26
27 mipscoff_symfile_read only does the minimum work necessary for letting the
28 user "name" things symbolically; it does not read the entire symtab.
29 Instead, it reads the external and static symbols and puts them in partial
30 symbol tables. When more extensive information is requested of a
31 file, the corresponding partial symbol table is mutated into a full
32 fledged symbol table by going back and reading the symbols
33 for real. mipscoff_psymtab_to_symtab() is called indirectly through
34 a pointer in the psymtab to do this. */
35
bd5635a1
RP
36#include <stdio.h>
37#include "param.h"
38#include "obstack.h"
39#include <sys/param.h>
40#include <sys/file.h>
41#include <sys/stat.h>
42#include "defs.h"
43#include "symtab.h"
44#include "gdbcore.h"
45#include "symfile.h"
46#ifdef CMUCS
47#include <mips/syms.h>
31ef19fc 48#else /* not CMUCS */
101f259c
JG
49#include <symconst.h>
50#include <sym.h>
31ef19fc 51#endif /* not CMUCS */
bd5635a1 52
a048c8f5 53#include "coff-mips.h"
bd5635a1
RP
54
55struct coff_exec {
b8c50f09
JG
56 struct external_filehdr f;
57 struct external_aouthdr a;
bd5635a1 58};
bd5635a1 59
4a35d6e9
FF
60/* Each partial symbol table entry contains a pointer to private data for the
61 read_symtab() function to use when expanding a partial symbol table entry
62 to a full symbol table entry.
63
64 For mipsread this structure contains the index of the FDR that this psymtab
65 represents and a pointer to the symbol table header HDRR from the symbol
a048c8f5 66 file that the psymtab was created from. */
4a35d6e9
FF
67
68#define FDR_IDX(p) (((struct symloc *)((p)->read_symtab_private))->fdr_idx)
69#define CUR_HDR(p) (((struct symloc *)((p)->read_symtab_private))->cur_hdr)
70
71struct symloc {
72 int fdr_idx;
73 HDRR *cur_hdr;
74};
75
bd5635a1
RP
76/* Things we import explicitly from other modules */
77
78extern int info_verbose;
79extern struct block *block_for_pc();
80extern void sort_symtab_syms();
81
3eaebb75
SG
82/* Various complaints about symbol reading that don't abort the process */
83
bd5635a1
RP
84struct complaint unknown_ext_complaint =
85 {"unknown external symbol %s", 0, 0};
86
101f259c
JG
87struct complaint unknown_sym_complaint =
88 {"unknown local symbol %s", 0, 0};
89
90struct complaint unknown_st_complaint =
91 {"with type %d", 0, 0};
92
3eaebb75
SG
93struct complaint block_overflow_complaint =
94 {"block containing %s overfilled", 0, 0};
95
96struct complaint basic_type_complaint =
97 {"cannot map MIPS basic type 0x%x", 0, 0};
98
99struct complaint unknown_type_qual_complaint =
100 {"unknown type qualifier 0x%x", 0, 0};
101
102struct complaint array_bitsize_complaint =
103 {"size of array target type not known, assuming %d bits", 0, 0};
104
105struct complaint array_parse_complaint =
106 {"array type with strange relative symbol", 0, 0};
107
108/* Macros and extra defs */
109
110/* Already-parsed symbols are marked specially */
bd5635a1
RP
111
112#define stParsed stType
113
114/* Puns: hard to find whether -g was used and how */
115
116#define MIN_GLEVEL GLEVEL_0
117#define compare_glevel(a,b) \
118 (((a) == GLEVEL_3) ? ((b) < GLEVEL_3) : \
119 ((b) == GLEVEL_3) ? -1 : (int)((b) - (a)))
120
3eaebb75 121/* When looking at .o files, avoid tripping over bad addresses */
bd5635a1
RP
122
123#define SAFE_TEXT_ADDR 0x400000
124#define SAFE_DATA_ADDR 0x10000000
125
126#define UNSAFE_DATA_ADDR(p) ((unsigned)p < SAFE_DATA_ADDR || (unsigned)p > 2*SAFE_DATA_ADDR)
e072c738
JG
127\f
128/* Things that really are local to this module */
129
130/* GDB symtable for the current compilation unit */
131
132static struct symtab *cur_stab;
133
3eaebb75
SG
134/* MIPS symtab header for the current file */
135
136static HDRR *cur_hdr;
137
e072c738
JG
138/* Pointer to current file decriptor record, and its index */
139
140static FDR *cur_fdr;
141static int cur_fd;
142
143/* Index of current symbol */
144
145static int cur_sdx;
146
147/* Note how much "debuggable" this image is. We would like
148 to see at least one FDR with full symbols */
149
150static max_gdbinfo;
151static max_glevel;
152
153/* When examining .o files, report on undefined symbols */
154
155static int n_undef_symbols, n_undef_labels, n_undef_vars, n_undef_procs;
156
157/* Extra builtin types */
158
159struct type *builtin_type_complex;
160struct type *builtin_type_double_complex;
161struct type *builtin_type_fixed_dec;
162struct type *builtin_type_float_dec;
163struct type *builtin_type_string;
164
165/* Template types */
166
167static struct type *builtin_type_ptr;
168static struct type *builtin_type_struct;
169static struct type *builtin_type_union;
170static struct type *builtin_type_enum;
171static struct type *builtin_type_range;
172static struct type *builtin_type_set;
bd5635a1 173
3eaebb75 174/* Forward declarations */
e072c738
JG
175
176static struct symbol *new_symbol();
177static struct type *new_type();
178static struct field *new_field();
179static struct block *new_block();
180static struct symtab *new_symtab();
181static struct linetable *new_linetable();
182static struct blockvector *new_bvect();
183
184static struct type *parse_type();
185static struct type *make_type();
186static struct symbol *mylookup_symbol();
187static struct block *shrink_block();
188
189static int compare_symtabs();
190static int compare_psymtabs();
191static int compare_blocks();
192
193static struct partial_symtab *new_psymtab();
e072c738
JG
194static struct partial_symtab *parse_fdr();
195static int compare_psymbols();
196
3eaebb75
SG
197static void psymtab_to_symtab_1();
198static void add_block();
199static void add_symbol();
200static int add_line();
e072c738
JG
201static void reorder_symtabs();
202static void reorder_psymtabs();
3eaebb75 203static void shrink_linetable();
bd5635a1
RP
204\f
205/* Things we export to other modules */
206
bd5635a1 207/* Address bounds for the signal trampoline in inferior, if any */
101f259c 208/* FIXME: Nothing really seems to use this. Why is it here? */
bd5635a1
RP
209
210CORE_ADDR sigtramp_address, sigtramp_end;
211
bd5635a1
RP
212/* The entry point (starting address) of the file, if it is an executable. */
213
bd5635a1
RP
214extern CORE_ADDR startup_file_start; /* From blockframe.c */
215extern CORE_ADDR startup_file_end; /* From blockframe.c */
216
217void
218mipscoff_new_init()
219{
3eaebb75
SG
220 /* If we have a file symbol header lying around, blow it away. */
221 if (cur_hdr)
222 free ((char *)cur_hdr);
223 cur_hdr = 0;
bd5635a1
RP
224}
225
226void
227mipscoff_symfile_init (sf)
228 struct sym_fns *sf;
229{
bd5635a1 230 sf->sym_private = NULL;
bd5635a1
RP
231}
232
233void
234mipscoff_symfile_read(sf, addr, mainline)
235 struct sym_fns *sf;
236 CORE_ADDR addr;
237 int mainline;
238{
239 struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
a048c8f5 240 bfd *abfd = sf->objfile->obfd;
bd5635a1
RP
241 char *name = bfd_get_filename (abfd);
242 int desc;
243 register int val;
bd5635a1
RP
244 int symtab_offset;
245 int stringtab_offset;
246
e5fa3486
JG
247 /* Initialize a variable that we couldn't do at _initialize_ time. */
248 builtin_type_ptr = lookup_pointer_type (builtin_type_void);
249
bd5635a1
RP
250/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
251 desc = fileno ((FILE *)(abfd->iostream)); /* Raw file descriptor */
bd5635a1
RP
252/* End of warning */
253
3eaebb75 254 /* Position to read the symbol table. */
bd5635a1
RP
255 val = lseek (desc, (long)symtab_offset, 0);
256 if (val < 0)
257 perror_with_name (name);
258
259 init_misc_bunches ();
260 make_cleanup (discard_misc_bunches, 0);
261
262 /* Now that the executable file is positioned at symbol table,
263 process it and define symbols accordingly. */
264
a048c8f5 265 read_mips_symtab(sf->objfile, desc);
bd5635a1 266
bd5635a1
RP
267 /* Go over the misc symbol bunches and install them in vector. */
268
3eaebb75 269 condense_misc_bunches (!mainline);
bd5635a1
RP
270}
271
bd5635a1
RP
272/* Exported procedure: Allocate zeroed memory */
273
0c4d2cc2
JG
274char *
275xzalloc(size)
bd5635a1
RP
276{
277 char *p = xmalloc(size);
278
279 bzero(p, size);
280 return p;
281}
282
283/* Exported procedure: Builds a symtab from the PST partial one.
284 Restores the environment in effect when PST was created, delegates
285 most of the work to an ancillary procedure, and sorts
286 and reorders the symtab list at the end */
287
3eaebb75 288static void
bd5635a1
RP
289mipscoff_psymtab_to_symtab(pst)
290 struct partial_symtab *pst;
291{
292 struct symtab *ret;
293 int i;
294
295 if (!pst)
296 return;
297
298 if (info_verbose) {
299 printf_filtered("Reading in symbols for %s...", pst->filename);
300 fflush(stdout);
301 }
302 /* Restore the header and list of pending typedefs */
4a35d6e9 303 cur_hdr = CUR_HDR(pst);
bd5635a1 304
3eaebb75 305 psymtab_to_symtab_1(pst, pst->filename);
bd5635a1
RP
306
307 reorder_symtabs();
308
bd5635a1
RP
309 if (info_verbose)
310 printf_filtered("done.\n");
311}
312
313/* Exported procedure: Is PC in the signal trampoline code */
314
b8c50f09
JG
315int
316in_sigtramp(pc, name)
bd5635a1 317 CORE_ADDR pc;
b8c50f09 318 char *name;
bd5635a1
RP
319{
320 if (sigtramp_address == 0)
321 fixup_sigtramp();
322 return (pc >= sigtramp_address && pc < sigtramp_end);
323}
bd5635a1
RP
324\f
325/* File-level interface functions */
326
b8c50f09
JG
327/* Read the symtab information from file FSYM into memory. Also,
328 return address just past end of our text segment in *END_OF_TEXT_SEGP. */
bd5635a1
RP
329
330static
b8c50f09
JG
331read_the_mips_symtab(abfd, fsym, end_of_text_segp)
332 bfd *abfd;
333 int fsym;
334 CORE_ADDR *end_of_text_segp;
bd5635a1
RP
335{
336 int stsize, st_hdrsize;
337 unsigned st_filptr;
338 HDRR st_hdr;
b8c50f09
JG
339 /* Header for executable/object file we read symbols from */
340 struct coff_exec filhdr;
341
342 /* We get here with DESC pointing to the symtab header. But we need
343 * other info from the initial headers */
344 lseek(fsym, 0L, 0);
345 myread(fsym, &filhdr, sizeof filhdr);
346
347 if (end_of_text_segp)
348 *end_of_text_segp =
349 bfd_h_get_32 (abfd, filhdr.a.text_start) +
350 bfd_h_get_32 (abfd, filhdr.a.tsize);
bd5635a1
RP
351
352 /* Find and read the symbol table header */
b8c50f09
JG
353 st_hdrsize = bfd_h_get_32 (abfd, filhdr.f.f_nsyms);
354 st_filptr = bfd_h_get_32 (abfd, filhdr.f.f_symptr);
bd5635a1
RP
355 if (st_filptr == 0)
356 return 0;
357
358 lseek(fsym, st_filptr, L_SET);
b8c50f09
JG
359 if (st_hdrsize > sizeof (st_hdr)) /* Profanity check */
360 abort();
bd5635a1
RP
361 if (read(fsym, &st_hdr, st_hdrsize) != st_hdrsize)
362 goto readerr;
363
364 /* Find out how large the symbol table is */
365 stsize = (st_hdr.cbExtOffset - (st_filptr + st_hdrsize))
366 + st_hdr.iextMax * cbEXTR;
367
368 /* Allocate space for the symbol table. Read it in. */
369 cur_hdr = (HDRR *) xmalloc(stsize + st_hdrsize);
370
371 bcopy(&st_hdr, cur_hdr, st_hdrsize);
372 if (read(fsym, (char *) cur_hdr + st_hdrsize, stsize) != stsize)
373 goto readerr;
374
375 /* Fixup file_pointers in it */
376 fixup_symtab(cur_hdr, (char *) cur_hdr + st_hdrsize,
377 st_filptr + st_hdrsize);
378
379 return;
380readerr:
c9bd6710 381 error("Short read on %s", bfd_get_filename (abfd));
bd5635a1
RP
382}
383
384
385/* Turn all file-relative pointers in the symtab described by HDR
386 into memory pointers, given that the symtab itself is located
387 at DATA in memory and F_PTR in the file. */
388
389static
390fixup_symtab( hdr, data, f_ptr)
391 HDRR *hdr;
392 char *data;
393{
394 int f_idx, s_idx;
395 FDR *fh;
396 SYMR *sh;
397 OPTR *op;
398 PDR *pr;
399 EXTR *esh;
400
401 /*
402 * These fields are useless (and empty) by now:
403 * hdr->cbDnOffset, hdr->cbOptOffset
404 * We use them for other internal purposes.
405 */
406 hdr->cbDnOffset = 0;
407 hdr->cbOptOffset = 0;
408
409#define FIX(off) \
410 if (hdr->off) hdr->off = (unsigned int)data + (hdr->off - f_ptr);
411
412 FIX(cbLineOffset);
413 FIX(cbPdOffset);
414 FIX(cbSymOffset);
415 FIX(cbOptOffset);
416 FIX(cbAuxOffset);
417 FIX(cbSsOffset);
418 FIX(cbSsExtOffset);
419 FIX(cbFdOffset);
420 FIX(cbRfdOffset);
421 FIX(cbExtOffset);
422#undef FIX
423
424
425 /*
426 * Fix all string pointers inside the symtab, and
427 * the FDR records. Also fix other miscellany.
428 */
429 for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
430 register unsigned code_offset;
431
432 /* Header itself, and strings */
433 fh = (FDR *) (hdr->cbFdOffset) + f_idx;
434 fh->issBase += hdr->cbSsOffset;
435 if (fh->rss != -1)
436 fh->rss = (long)fh->rss + fh->issBase;
437 for (s_idx = 0; s_idx < fh->csym; s_idx++) {
438 sh = (SYMR*)(hdr->cbSymOffset) + fh->isymBase + s_idx;
439 sh->iss = (long) sh->iss + fh->issBase;
440 sh->reserved = 0;
441 }
442
443 cur_fd = f_idx;
444
445 /* Local symbols */
446 fh->isymBase = (int)((SYMR*)(hdr->cbSymOffset)+fh->isymBase);
447
448 /* cannot fix fh->ipdFirst because it is a short */
449#define IPDFIRST(h,fh) \
450 ((long)h->cbPdOffset + fh->ipdFirst * sizeof(PDR))
451
452 /* Optional symbols (actually used for partial_symtabs) */
453 fh->ioptBase = 0;
454 fh->copt = 0;
455
456 /* Aux symbols */
457 if (fh->caux)
458 fh->iauxBase = hdr->cbAuxOffset + fh->iauxBase * sizeof(AUXU);
459 /* Relative file descriptor table */
460 fh->rfdBase = hdr->cbRfdOffset + fh->rfdBase * sizeof(RFDT);
461
462 /* Line numbers */
463 if (fh->cbLine)
464 fh->cbLineOffset += hdr->cbLineOffset;
465
466 /* Procedure symbols. (XXX This should be done later) */
467 code_offset = fh->adr;
468 for (s_idx = 0; s_idx < fh->cpd; s_idx++) {
469 unsigned name, only_ext;
470
471 pr = (PDR*)(IPDFIRST(hdr,fh)) + s_idx;
472
473 /* Simple rule to find files linked "-x" */
474 only_ext = fh->rss == -1;
475 if (only_ext) {
476 if (pr->isym == -1) {
477 /* static function */
478 sh = (SYMR*)-1;
479 } else {
480 /* external */
481 name = hdr->cbExtOffset + pr->isym * sizeof(EXTR);
482 sh = &((EXTR*)name)->asym;
483 }
484 } else {
485 /* Full symbols */
486 sh = (SYMR*)fh->isymBase + pr->isym;
487 /* Included code ? */
488 if (s_idx == 0 && pr->adr != 0)
489 code_offset -= pr->adr;
490 }
491
492 /* Turn index into a pointer */
493 pr->isym = (long)sh;
494
495 /* Fix line numbers */
496 pr->cbLineOffset += fh->cbLineOffset;
497
498 /* Relocate address */
499 if (!only_ext)
500 pr->adr += code_offset;
501 }
502 }
503
504 /* External symbols: fix string */
505 for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
506 esh = (EXTR*)(hdr->cbExtOffset) + s_idx;
507 esh->asym.iss = esh->asym.iss + hdr->cbSsExtOffset;
508 }
509}
510
511
512/* Find a file descriptor given its index RF relative to a file CF */
513
3eaebb75
SG
514static FDR *
515get_rfd (cf, rf)
516 int cf, rf;
bd5635a1
RP
517{
518 register FDR *f;
519
520 f = (FDR *) (cur_hdr->cbFdOffset) + cf;
521 /* Object files do not have the RFD table, all refs are absolute */
522 if (f->rfdBase == 0)
523 return (FDR *) (cur_hdr->cbFdOffset) + rf;
524 cf = *((pRFDT) f->rfdBase + rf);
525 return (FDR *) (cur_hdr->cbFdOffset) + cf;
526}
527
528/* Return a safer print NAME for a file descriptor */
529
3eaebb75
SG
530static char *
531fdr_name(name)
bd5635a1
RP
532 char *name;
533{
534 if (name == (char *) -1)
535 return "<stripped file>";
536 if (UNSAFE_DATA_ADDR(name))
537 return "<NFY>";
538 return name;
539}
540
541
542/* Read in and parse the symtab of the file DESC. INCREMENTAL says
b8c50f09
JG
543 whether we are adding to the general symtab or not.
544 FIXME: INCREMENTAL is currently always zero, though it should not be. */
bd5635a1
RP
545
546static
a048c8f5
JG
547read_mips_symtab (objfile, desc)
548 struct objfile *objfile;
b8c50f09 549 int desc;
bd5635a1 550{
b8c50f09 551 CORE_ADDR end_of_text_seg;
bd5635a1 552
a048c8f5 553 read_the_mips_symtab(objfile->obfd, desc, &end_of_text_seg);
bd5635a1 554
a048c8f5 555 parse_partial_symbols(end_of_text_seg, objfile);
bd5635a1
RP
556
557 /*
558 * Check to make sure file was compiled with -g.
559 * If not, warn the user of this limitation.
560 */
561 if (compare_glevel(max_glevel, GLEVEL_2) < 0) {
562 if (max_gdbinfo == 0)
c9bd6710
JG
563 printf (
564"\n%s not compiled with -g, debugging support is limited.\n",
a048c8f5 565 objfile->name);
c9bd6710
JG
566 printf(
567"You should compile with -g2 or -g3 for best debugging support.\n");
bd5635a1
RP
568 fflush(stdout);
569 }
bd5635a1 570}
bd5635a1
RP
571\f
572/* Local utilities */
573
bd5635a1
RP
574/* Map of FDR indexes to partial symtabs */
575
576static struct pst_map {
577 struct partial_symtab *pst; /* the psymtab proper */
578 int n_globals; /* globals it exports */
101f259c 579 int n_statics; /* statics (locals) it contains */
bd5635a1
RP
580} * fdr_to_pst;
581
582
583/* Utility stack, used to nest procedures and blocks properly.
584 It is a doubly linked list, to avoid too many alloc/free.
585 Since we might need it quite a few times it is NOT deallocated
586 after use. */
587
588static struct parse_stack {
589 struct parse_stack *next, *prev;
590 struct symtab *cur_st; /* Current symtab */
591 struct block *cur_block; /* Block in it */
592 int blocktype; /* What are we parsing */
593 int maxsyms; /* Max symbols in this block */
594 struct type *cur_type; /* Type we parse fields for */
595 int procadr; /* Start addres of this procedure */
596 int numargs; /* Its argument count */
597} *top_stack; /* Top stack ptr */
598
599
600/* Enter a new lexical context */
601
602static push_parse_stack()
603{
604 struct parse_stack *new;
605
606 /* Reuse frames if possible */
607 if (top_stack && top_stack->prev)
608 new = top_stack->prev;
609 else
610 new = (struct parse_stack *) xzalloc(sizeof(struct parse_stack));
611 /* Initialize new frame with previous content */
612 if (top_stack) {
613 register struct parse_stack *prev = new->prev;
614
615 *new = *top_stack;
616 top_stack->prev = new;
617 new->prev = prev;
618 new->next = top_stack;
619 }
620 top_stack = new;
621}
622
623/* Exit a lexical context */
624
625static pop_parse_stack()
626{
627 if (!top_stack)
628 return;
629 if (top_stack->next)
630 top_stack = top_stack->next;
631}
632
633
634/* Cross-references might be to things we haven't looked at
635 yet, e.g. type references. To avoid too many type
636 duplications we keep a quick fixup table, an array
637 of lists of references indexed by file descriptor */
638
639static struct pending {
640 struct pending *next; /* link */
641 SYMR *s; /* the symbol */
642 struct type *t; /* its partial type descriptor */
643} **pending_list;
644
645
646/* Check whether we already saw symbol SH in file FH as undefined */
647
648static
649struct pending *is_pending_symbol(fh, sh)
650 FDR *fh;
651 SYMR *sh;
652{
653 int f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
654 register struct pending *p;
655
656 /* Linear search is ok, list is typically no more than 10 deep */
657 for (p = pending_list[f_idx]; p; p = p->next)
658 if (p->s == sh)
659 break;
660 return p;
661}
662
663/* Check whether we already saw type T in file FH as undefined */
664
665static
666struct pending *is_pending_type(fh, t)
667 FDR *fh;
668 struct type *t;
669{
670 int f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
671 register struct pending *p;
672
673 for (p = pending_list[f_idx]; p; p = p->next)
674 if (p->t == t)
675 break;
676 return p;
677}
678
679/* Add a new undef symbol SH of type T */
680
681static
682add_pending(fh, sh, t)
683 FDR *fh;
684 SYMR *sh;
685 struct type *t;
686{
687 int f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
688 struct pending *p = is_pending_symbol(fh, sh);
689
690 /* Make sure we do not make duplicates */
691 if (!p) {
692 p = (struct pending *) xmalloc(sizeof(*p));
693 p->s = sh;
694 p->t = t;
695 p->next = pending_list[f_idx];
696 pending_list[f_idx] = p;
697 }
698 sh->reserved = 1; /* for quick check */
699}
700
701/* Throw away undef entries when done with file index F_IDX */
702
703static
704free_pending(f_idx)
705{
706 register struct pending *p, *q;
707
708 for (p = pending_list[f_idx]; p; p = q) {
709 q = p->next;
710 free(p);
711 }
712 pending_list[f_idx] = 0;
713}
714
715/* The number of args to a procedure is not explicit in the symtab,
716 this is the list of all those we know of.
717 This makes parsing more reasonable and avoids extra passes */
718
719static struct numarg {
720 struct numarg *next; /* link */
721 unsigned adr; /* procedure's start address */
722 unsigned num; /* arg count */
723} *numargs_list;
724
725/* Record that the procedure at ADR takes NUM arguments. */
726
727static
728got_numargs(adr,num)
729{
730 struct numarg *n = (struct numarg *) xmalloc(sizeof(struct numarg));
731
732 n->adr = adr;
733 n->num = num;
734 n->next = numargs_list;
735 numargs_list = n;
736}
737
738/* See if we know how many arguments the procedure at ADR takes */
739
740static
741lookup_numargs(adr)
742{
743 struct numarg *n = numargs_list;
744
745 while (n && n->adr != adr)
746 n = n->next;
747 return (n) ? n->num : -1;
748}
749
750/* Release storage when done with this file */
751
3eaebb75 752static void
bd5635a1
RP
753free_numargs()
754{
755 struct numarg *n = numargs_list, *m;
756
757 while (n) {
758 m = n->next;
759 free(n);
760 n = m;
761 }
762 numargs_list = 0;
763}
764
765\f
766/* Parsing Routines proper. */
767
768/* Parse a single symbol. Mostly just make up a GDB symbol for it.
769 For blocks, procedures and types we open a new lexical context.
770 This is basically just a big switch on the symbol's type */
771
3eaebb75 772static void
bd5635a1
RP
773parse_symbol(sh, ax)
774 SYMR *sh;
775 AUXU *ax;
776{
777 struct symbol *s;
778 struct block *b;
779 struct type *t;
780 struct field *f;
781 /* When a symbol is cross-referenced from other files/symbols
782 we mark it explicitly */
783 int pend = (sh->reserved == 1);
784 enum address_class class;
785
786 switch (sh->st) {
787
788 case stNil:
789 break;
790
3eaebb75 791 case stGlobal: /* external symbol, goes into global block */
bd5635a1 792 class = LOC_STATIC;
d219db01
JG
793 b = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st),
794 GLOBAL_BLOCK);
101f259c
JG
795 s = new_symbol(sh->iss);
796 SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
bd5635a1
RP
797 goto data;
798
3eaebb75 799 case stStatic: /* static data, goes into current block. */
bd5635a1
RP
800 class = LOC_STATIC;
801 b = top_stack->cur_block;
101f259c
JG
802 s = new_symbol(sh->iss);
803 SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
bd5635a1
RP
804 goto data;
805
3eaebb75 806 case stLocal: /* local variable, goes into current block */
bd5635a1
RP
807 if (sh->sc == scRegister) {
808 class = LOC_REGISTER;
809 if (sh->value > 31)
810 sh->value += 6;
811 } else
812 class = LOC_LOCAL;
813 b = top_stack->cur_block;
101f259c
JG
814 s = new_symbol(sh->iss);
815 SYMBOL_VALUE(s) = sh->value;
bd5635a1
RP
816
817data: /* Common code for symbols describing data */
bd5635a1
RP
818 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
819 SYMBOL_CLASS(s) = class;
820 add_symbol(s, b);
821
822 /* Type could be missing in a number of cases */
823 if (sh->sc == scUndefined || sh->sc == scNil ||
824 sh->index == 0xfffff)
825 SYMBOL_TYPE(s) = builtin_type_int; /* undefined? */
826 else
827 SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
828 /* Value of a data symbol is its memory address */
bd5635a1
RP
829 break;
830
3eaebb75 831 case stParam: /* arg to procedure, goes into current block */
bd5635a1
RP
832 max_gdbinfo++;
833 top_stack->numargs++;
834 s = new_symbol(sh->iss);
835 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
836 if (sh->sc == scRegister) {
837 SYMBOL_CLASS(s) = LOC_REGPARM;
838 if (sh->value > 31)
839 sh->value += 6;
840 } else
841 SYMBOL_CLASS(s) = LOC_ARG;
842 SYMBOL_VALUE(s) = sh->value;
843 SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
844 add_symbol(s, top_stack->cur_block);
0c4d2cc2
JG
845#if 0
846 /* FIXME: This has not been tested. See dbxread.c */
847 /* Add the type of this parameter to the function/procedure
848 type of this block. */
849 add_param_to_type(&top_stack->cur_block->function->type,s);
850#endif
bd5635a1
RP
851 break;
852
3eaebb75 853 case stLabel: /* label, goes into current block */
bd5635a1
RP
854 s = new_symbol(sh->iss);
855 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; /* so that it can be used */
856 SYMBOL_CLASS(s) = LOC_LABEL; /* but not misused */
101f259c 857 SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
bd5635a1
RP
858 SYMBOL_TYPE(s) = builtin_type_int;
859 add_symbol(s, top_stack->cur_block);
860 break;
861
0c4d2cc2 862 case stProc: /* Procedure, usually goes into global block */
3eaebb75 863 case stStaticProc: /* Static procedure, goes into current block */
bd5635a1
RP
864 s = new_symbol(sh->iss);
865 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
866 SYMBOL_CLASS(s) = LOC_BLOCK;
867 /* Type of the return value */
868 if (sh->sc == scUndefined || sh->sc == scNil)
869 t = builtin_type_int;
870 else
871 t = parse_type(ax + sh->index, sh, 0);
0c4d2cc2
JG
872 b = top_stack->cur_block;
873 if (sh->st == stProc) {
874 struct blockvector *bv = BLOCKVECTOR(top_stack->cur_st);
875 /* The next test should normally be true,
876 but provides a hook for nested functions
877 (which we don't want to make global). */
878 if (b == BLOCKVECTOR_BLOCK(bv, STATIC_BLOCK))
879 b = BLOCKVECTOR_BLOCK(bv, GLOBAL_BLOCK);
880 }
881 add_symbol(s, b);
bd5635a1
RP
882
883 /* Make a type for the procedure itself */
0c4d2cc2
JG
884#if 0
885 /* FIXME: This has not been tested yet! See dbxread.c */
886 /* Generate a template for the type of this function. The
887 types of the arguments will be added as we read the symbol
888 table. */
889 bcopy(SYMBOL_TYPE(s),lookup_function_type(t),sizeof(struct type));
890#else
bd5635a1 891 SYMBOL_TYPE(s) = lookup_function_type (t);
0c4d2cc2 892#endif
bd5635a1
RP
893
894 /* Create and enter a new lexical context */
895 b = new_block(top_stack->maxsyms);
896 SYMBOL_BLOCK_VALUE(s) = b;
897 BLOCK_FUNCTION(b) = s;
898 BLOCK_START(b) = BLOCK_END(b) = sh->value;
899 BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
900 add_block(b, top_stack->cur_st);
901
902 /* Not if we only have partial info */
903 if (sh->sc == scUndefined || sh->sc == scNil)
904 break;
905
906 push_parse_stack();
907 top_stack->cur_block = b;
908 top_stack->blocktype = sh->st;
909 top_stack->cur_type = SYMBOL_TYPE(s);
910 top_stack->procadr = sh->value;
911 top_stack->numargs = 0;
912
913 sh->value = (long) SYMBOL_TYPE(s);
914 break;
915
916 case stBlock: /* Either a lexical block, or some type */
917 push_parse_stack();
918 top_stack->blocktype = stBlock;
919 if (sh->sc == scInfo) { /* structure/union/enum def */
920 s = new_symbol(sh->iss);
921 SYMBOL_NAMESPACE(s) = STRUCT_NAMESPACE;
922 SYMBOL_CLASS(s) = LOC_TYPEDEF;
923 SYMBOL_VALUE(s) = 0;
924 add_symbol(s, top_stack->cur_block);
925 /* If this type was expected, use its partial definition */
926 if (pend) {
927 t = is_pending_symbol(cur_fdr, sh)->t;
928 } else {
929 /* Uhmm, can`t decide yet. Smash later */
930 t = new_type(sh->iss);
931 TYPE_CODE(t) = TYPE_CODE_UNDEF;
932 add_pending(cur_fdr, sh, t);
933 }
934 SYMBOL_TYPE(s) = t;
935 /* make this the current type */
936 top_stack->cur_type = t;
937 TYPE_LENGTH(t) = sh->value;
938 /* Mark that symbol has a type, and say which one */
939 sh->value = (long) t;
940 } else {
941 /* beginnning of (code) block. Value of symbol
942 is the displacement from procedure start */
943 b = new_block(top_stack->maxsyms);
944 BLOCK_START(b) = sh->value + top_stack->procadr;
945 BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
946 top_stack->cur_block = b;
947 add_block(b, top_stack->cur_st);
948 }
949 break;
950
951 case stEnd: /* end (of anything) */
952 if (sh->sc == scInfo) {
953 /* Finished with type */
954 top_stack->cur_type = 0;
955 } else if (sh->sc == scText &&
956 (top_stack->blocktype == stProc ||
957 top_stack->blocktype == stStaticProc)) {
958 /* Finished with procedure */
959 struct blockvector *bv = BLOCKVECTOR(top_stack->cur_st);
960 struct block *b;
961 int i;
962
963 BLOCK_END(top_stack->cur_block) += sh->value; /* size */
964 got_numargs(top_stack->procadr, top_stack->numargs);
965 /* Reallocate symbols, saving memory */
966 b = shrink_block(top_stack->cur_block, top_stack->cur_st);
967
968 /* f77 emits proc-level with address bounds==[0,0],
969 So look for such child blocks, and patch them. */
970 for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++) {
971 struct block *b_bad = BLOCKVECTOR_BLOCK(bv,i);
972 if (BLOCK_SUPERBLOCK(b_bad) == b
973 && BLOCK_START(b_bad) == top_stack->procadr
974 && BLOCK_END(b_bad) == top_stack->procadr) {
975 BLOCK_START(b_bad) = BLOCK_START(b);
976 BLOCK_END(b_bad) = BLOCK_END(b);
977 }
978 }
961b4908
JK
979 if (entry_point < BLOCK_END(b)
980 && entry_point >= BLOCK_START(b)) {
981 startup_file_start = BLOCK_START(b);
982 startup_file_end = BLOCK_END(b);
983 }
bd5635a1
RP
984 } else if (sh->sc == scText && top_stack->blocktype == stBlock) {
985 /* End of (code) block. The value of the symbol
986 is the displacement from the procedure`s start
987 address of the end of this block. */
988 BLOCK_END(top_stack->cur_block) = sh->value + top_stack->procadr;
989 (void) shrink_block(top_stack->cur_block, top_stack->cur_st);
990 }
991 pop_parse_stack(); /* restore previous lexical context */
992 break;
993
994 case stMember: /* member of struct/union/enum.. */
995 f = new_field(top_stack->cur_type, sh->iss);
996 f->bitpos = sh->value;
997 f->type = parse_type(ax + sh->index, sh, &f->bitsize);
998 break;
999
1000 case stTypedef: /* type definition */
1001 s = new_symbol(sh->iss);
1002 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
1003 SYMBOL_CLASS(s) = LOC_TYPEDEF;
1004 SYMBOL_BLOCK_VALUE(s) = top_stack->cur_block;
1005 add_symbol(s, top_stack->cur_block);
1006 SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
1007 sh->value = (long) SYMBOL_TYPE(s);
1008 break;
1009
1010 case stFile: /* file name */
1011 push_parse_stack();
1012 top_stack->blocktype = sh->st;
1013 break;
1014
1015 /* I`ve never seen these for C */
1016 case stRegReloc:
1017 break; /* register relocation */
1018 case stForward:
1019 break; /* forwarding address */
1020 case stConstant:
1021 break; /* constant */
1022 default:
1023 error("Unknown symbol type %x.", sh->st);
1024 }
1025 sh->st = stParsed;
1026}
1027
1028/* Parse the type information provided in the AX entries for
1029 the symbol SH. Return the bitfield size in BS, in case. */
1030
1031static struct type *parse_type(ax, sh, bs)
1032 AUXU *ax;
1033 SYMR *sh;
1034 int *bs;
1035{
1036 /* Null entries in this map are treated specially */
1037 static struct type **map_bt[] =
1038 {
1039 &builtin_type_void, /* btNil */
1040 0, /* btAdr */
1041 &builtin_type_char, /* btChar */
1042 &builtin_type_unsigned_char, /* btUChar */
1043 &builtin_type_short, /* btShort */
1044 &builtin_type_unsigned_short, /* btUShort */
1045 &builtin_type_int, /* btInt */
1046 &builtin_type_unsigned_int, /* btUInt */
1047 &builtin_type_long, /* btLong */
1048 &builtin_type_unsigned_long, /* btULong */
1049 &builtin_type_float, /* btFloat */
1050 &builtin_type_double, /* btDouble */
1051 0, /* btStruct */
1052 0, /* btUnion */
1053 0, /* btEnum */
1054 0, /* btTypedef */
1055 0, /* btRange */
1056 0, /* btSet */
1057 &builtin_type_complex, /* btComplex */
1058 &builtin_type_double_complex, /* btDComplex */
1059 0, /* btIndirect */
1060 &builtin_type_fixed_dec, /* btFixedDec */
1061 &builtin_type_float_dec, /* btFloatDec */
1062 &builtin_type_string, /* btString */
1063 0, /* btBit */
1064 0, /* btPicture */
1065 &builtin_type_void, /* btVoid */
1066 };
1067
1068 TIR *t;
1069 struct type *tp = 0, *tp1;
1070 char *fmt = "%s";
1071
1072 /* Procedures start off by one */
1073 if (sh->st == stProc || sh->st == stStaticProc)
1074 ax++;
1075
1076 /* Undefined ? Should not happen */
1077 if (ax->rndx.rfd == 0xfff) {
1078 return builtin_type_void;
1079 }
1080
1081 /* Use aux as a type information record, map its basic type */
1082 t = &ax->ti;
1083 if (t->bt > 26 || t->bt == btPicture) {
3eaebb75 1084 complain (&basic_type_complaint, t->bt);
bd5635a1
RP
1085 return builtin_type_int;
1086 }
1087 if (map_bt[t->bt])
1088 tp = *map_bt[t->bt];
1089 else {
1090 /* Cannot use builtin types, use templates */
1091 tp = make_type(TYPE_CODE_VOID, 0, 0, 0);
1092 switch (t->bt) {
1093 case btAdr:
1094 *tp = *builtin_type_ptr;
1095 break;
1096 case btStruct:
1097 *tp = *builtin_type_struct;
1098 fmt = "struct %s";
1099 break;
1100 case btUnion:
1101 *tp = *builtin_type_union;
1102 fmt = "union %s";
1103 break;
1104 case btEnum:
1105 *tp = *builtin_type_enum;
1106 fmt = "enum %s";
1107 break;
1108 case btRange:
1109 *tp = *builtin_type_range;
1110 break;
1111 case btSet:
1112 *tp = *builtin_type_set;
1113 fmt = "set %s";
1114 break;
1115 }
1116 }
1117
1118 /* Move on to next aux */
1119 ax++;
1120 if (t->continued) {
1121 /* This is the way it would work if the compiler worked */
1122 register TIR *t1 = t;
1123 while (t1->continued)
1124 ax++;
1125 }
1126
1127 /* For bitfields all we need is the width */
1128 if (t->fBitfield) {
1129 *bs = ax->width;
1130 return tp;
1131 }
1132
1133 /* All these types really point to some (common) MIPS type
1134 definition, and only the type-qualifiers fully identify
1135 them. We`ll make the same effort at sharing */
1136 if (t->bt == btIndirect ||
1137 t->bt == btStruct ||
1138 t->bt == btUnion ||
1139 t->bt == btEnum ||
1140 t->bt == btTypedef ||
1141 t->bt == btRange ||
1142 t->bt == btSet) {
1143 char name[256], *pn;
1144
1145 /* Try to cross reference this type */
1146 tp1 = tp;
1147 ax += cross_ref(ax, &tp1, &pn);
1148 /* SOMEONE OUGHT TO FIX DBXREAD TO DROP "STRUCT" */
1149 sprintf(name, fmt, pn);
1150
1151 /* reading .o file ? */
1152 if (UNSAFE_DATA_ADDR(tp1))
1153 tp1 = tp;
1154 if (TYPE_CODE(tp1) == TYPE_CODE_UNDEF) {
1155 /*
1156 * Type was incompletely defined, now we know.
1157 */
1158 TYPE_CODE(tp1) = TYPE_CODE(tp);
1159 TYPE_NAME(tp1) = obsavestring(name, strlen(name));
1160 if (TYPE_CODE(tp1) == TYPE_CODE_ENUM) {
1161 int i;
1162
1163 for (i = 0; i < TYPE_NFIELDS(tp1); i++)
1164 make_enum_constant(&TYPE_FIELD(tp1,i), tp1);
1165 }
1166 }
1167 if (tp1 != tp) {
1168 /* found as cross ref, rid of our template */
1169 if ((TYPE_FLAGS(tp) & TYPE_FLAG_PERM) == 0)
1170 free(tp);
1171 tp = tp1;
1172 /* stupid idea of prepending "struct" to type names */
1173 if (t->bt == btStruct && !index(TYPE_NAME(tp), ' ')) {
1174 sprintf(name, fmt, TYPE_NAME(tp));
1175 TYPE_NAME(tp) = obsavestring(name, strlen(name));
1176 }
1177 } else
1178 TYPE_NAME(tp) = savestring(name, strlen(name));
1179 }
1180
1181 /* Deal with range types */
1182 if (t->bt == btRange) {
1183 struct field *f;
1184
1185 f = new_field(tp, "Low");
1186 f->bitpos = ax->dnLow;
1187 ax++;
1188 f = new_field(tp, "High");
1189 f->bitpos = ax->dnHigh;
1190 ax++;
1191 }
1192
1193 /* Parse all the type qualifiers now. If there are more
1194 than 6 the game will continue in the next aux */
1195
1196#define PARSE_TQ(tq) \
1197 if (t->tq != tqNil) ax += upgrade_type(&tp, t->tq, ax, sh);
1198
1199again: PARSE_TQ(tq0);
1200 PARSE_TQ(tq1);
1201 PARSE_TQ(tq2);
1202 PARSE_TQ(tq3);
1203 PARSE_TQ(tq4);
1204 PARSE_TQ(tq5);
1205#undef PARSE_TQ
1206
1207 if (t->continued) {
1208 t++;
1209 goto again;
1210 }
1211 return tp;
1212}
1213
1214/* Make up a complex type from a basic one. Type is passed by
1215 reference in TPP and side-effected as necessary. The type
1216 qualifier TQ says how to handle the aux symbols at AX for
1217 the symbol SX we are currently analyzing.
1218 Returns the number of aux symbols we parsed. */
1219
3eaebb75 1220static int
bd5635a1
RP
1221upgrade_type(tpp, tq, ax, sh)
1222 struct type **tpp;
1223 AUXU *ax;
1224 SYMR *sh;
1225{
3eaebb75 1226 int off;
bd5635a1
RP
1227 struct type *t;
1228
3eaebb75
SG
1229 /* Used in array processing */
1230 int rf, id;
1231 FDR *fh;
1232 struct field *f;
1233 SYMR ss;
1234 int lower, upper;
1235
1236 switch (tq) {
1237 case tqPtr:
bd5635a1 1238 t = lookup_pointer_type (*tpp);
3eaebb75
SG
1239 *tpp = t;
1240 return 0;
1241
1242 case tqProc:
bd5635a1 1243 t = lookup_function_type (*tpp);
3eaebb75
SG
1244 *tpp = t;
1245 return 0;
bd5635a1 1246
3eaebb75
SG
1247 case tqArray:
1248 off = 0;
bd5635a1
RP
1249 t = make_type(TYPE_CODE_ARRAY, 0, 0, 0);
1250 TYPE_TARGET_TYPE(t) = *tpp;
1251
3eaebb75 1252 /* Determine and record the domain type (type of index) */
bd5635a1 1253 id = ax->rndx.index;
3eaebb75
SG
1254 rf = ax->rndx.rfd;
1255 if (rf == 0xfff) {
1256 rf = (++ax)->isym;
1257 off++;
1258 }
bd5635a1 1259 fh = get_rfd(cur_fd, rf);
3eaebb75 1260 f = new_field(t, (char *)0);
bd5635a1
RP
1261 bzero(&ss, sizeof ss);
1262/* XXX */ f->type = parse_type(fh->iauxBase + id * sizeof(AUXU),
1263 &ss, &f->bitsize);
1264
3eaebb75 1265 if (off == 0) {
bd5635a1
RP
1266 /*
1267 * This seems to be a pointer to the end of the Block defining
1268 * the type. Why it is here is magic for me, and I have no
1269 * good use for it anyways.
1270 */
3eaebb75
SG
1271 /* This used to occur because cross_ref returned
1272 the wrong result (ax pointed wrong). FIXME,
1273 delete this code in a while. -- gnu@cygnus jul91 */
1274 complain (&array_parse_complaint, 0);
bd5635a1
RP
1275 off++;
1276 id = (++ax)->rndx.index;
1277 if ((rf = ax->rndx.rfd) == 0xfff)
1278 rf = (++ax)->isym, off++;
1279 }
3eaebb75
SG
1280 lower = (++ax)->dnLow;
1281 upper = (++ax)->dnHigh;
1282 rf = (++ax)->width; /* bit size of array element */
1283
1284 /* Check whether supplied array element bit size matches
1285 the known size of the element type. If this complaint
1286 ends up not happening, we can remove this code. It's
1287 here because we aren't sure we understand this *&%&$
1288 symbol format. */
bd5635a1 1289 id = TYPE_LENGTH(TYPE_TARGET_TYPE(t)) << 3; /* bitsize */
bd5635a1
RP
1290 if (id == 0) {
1291 /* Most likely an undefined type */
3eaebb75 1292 id = rf;
bd5635a1
RP
1293 TYPE_LENGTH(TYPE_TARGET_TYPE(t)) = id >> 3;
1294 }
3eaebb75
SG
1295 if (id != rf)
1296 complain (&array_bitsize_complaint, rf);
1297
1298 TYPE_LENGTH(t) = (upper < 0) ? 0 :
1299 (upper - lower + 1) * (rf >> 3);
1300 *tpp = t;
1301 return 4 + off;
bd5635a1 1302
3eaebb75
SG
1303 case tqVol:
1304 /* Volatile -- currently ignored */
1305 return 0;
1306
1307 default:
1308 complain (&unknown_type_qual_complaint, tq);
1309 return 0;
1310 }
bd5635a1
RP
1311}
1312
1313
1314/* Parse a procedure descriptor record PR. Note that the procedure
1315 is parsed _after_ the local symbols, now we just make up the
1316 extra information we need into a special symbol that we insert
1317 in the procedure's main block. Note also that images that
1318 have been partially stripped (ld -x) have been deprived
1319 of local symbols, and we have to cope with them here.
1320 The procedure's code ends at BOUND */
1321
1322static
1323parse_procedure(pr, bound)
1324 PDR *pr;
1325{
1326 struct symbol *s, *i;
1327 SYMR *sh = (SYMR*)pr->isym;
1328 struct block *b;
1329 struct mips_extra_func_info *e;
1330 char name[100];
1331 char *sh_name;
1332
1333 /* Reuse the MIPS record */
1334 e = (struct mips_extra_func_info *) pr;
1335 e->numargs = lookup_numargs(pr->adr);
1336
1337 /* Make up our special symbol */
1338 i = new_symbol(".gdbinfo.");
1339 SYMBOL_VALUE(i) = (int)e;
1340 SYMBOL_NAMESPACE(i) = LABEL_NAMESPACE;
1341 SYMBOL_CLASS(i) = LOC_CONST;
1342 SYMBOL_TYPE(i) = builtin_type_void;
1343
1344 /* Make up a name for static procedures. Sigh. */
1345 if (sh == (SYMR*)-1) {
1346 sprintf(name,".static_procedure@%x",pr->adr);
1347 sh_name = savestring(name, strlen(name));
1348 s = NULL;
1349 }
1350 else {
1351 sh_name = (char*)sh->iss;
1352 s = mylookup_symbol(sh_name, top_stack->cur_block,
1353 VAR_NAMESPACE, LOC_BLOCK);
1354 }
1355 if (s != 0) {
1356 b = SYMBOL_BLOCK_VALUE(s);
1357 } else {
1358 s = new_symbol(sh_name);
1359 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
1360 SYMBOL_CLASS(s) = LOC_BLOCK;
1361 /* Donno its type, hope int is ok */
1362 SYMBOL_TYPE(s) = lookup_function_type (builtin_type_int);
1363 add_symbol(s, top_stack->cur_block);
1364 /* Wont have symbols for this one */
1365 b = new_block(2);
1366 SYMBOL_BLOCK_VALUE(s) = b;
1367 BLOCK_FUNCTION(b) = s;
1368 BLOCK_START(b) = pr->adr;
1369 BLOCK_END(b) = bound;
1370 BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
1371 add_block(b, top_stack->cur_st);
1372 }
1373 e->isym = (long)s;
1374 add_symbol(i,b);
1375}
1376
1377/* Parse the external symbol ES. Just call parse_symbol() after
1378 making sure we know where the aux are for it. For procedures,
1379 parsing of the PDRs has already provided all the needed
1380 information, we only parse them if SKIP_PROCEDURES is false,
101f259c
JG
1381 and only if this causes no symbol duplication.
1382
1383 This routine clobbers top_stack->cur_block and ->cur_st. */
bd5635a1
RP
1384
1385static
1386parse_external(es, skip_procedures)
1387 EXTR *es;
1388{
1389 AUXU *ax;
1390
1391 if (es->ifd != ifdNil) {
1392 cur_fd = es->ifd;
1393 cur_fdr = (FDR*)(cur_hdr->cbFdOffset) + cur_fd;
1394 ax = (AUXU*)cur_fdr->iauxBase;
1395 } else {
1396 cur_fdr = (FDR*)(cur_hdr->cbFdOffset);
1397 ax = 0;
1398 }
1399 top_stack->cur_st = cur_stab;
d219db01
JG
1400 top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st),
1401 GLOBAL_BLOCK);
bd5635a1
RP
1402
1403 /* Reading .o files */
1404 if (es->asym.sc == scUndefined || es->asym.sc == scNil) {
1405 char *what;
1406 switch (es->asym.st) {
1407 case stStaticProc:
3eaebb75
SG
1408 case stProc: what = "procedure"; n_undef_procs++; break;
1409 case stGlobal: what = "variable"; n_undef_vars++; break;
1410 case stLabel: what = "label"; n_undef_labels++; break;
1411 default : what = "symbol"; break;
bd5635a1
RP
1412 }
1413 n_undef_symbols++;
1414 if (info_verbose)
3eaebb75 1415 printf_filtered("Warning: %s `%s' is undefined (in %s)\n", what,
bd5635a1
RP
1416 es->asym.iss, fdr_name(cur_fdr->rss));
1417 return;
1418 }
1419
1420 switch (es->asym.st) {
1421 case stProc:
1422 /* If we have full symbols we do not need more */
1423 if (skip_procedures)
1424 return;
1425 if (mylookup_symbol (es->asym.iss, top_stack->cur_block,
1426 VAR_NAMESPACE, LOC_BLOCK))
1427 break;
1428 /* fall through */
1429 case stGlobal:
1430 case stLabel:
1431 /*
1432 * Note that the case of a symbol with indexNil
1433 * must be handled anyways by parse_symbol().
1434 */
1435 parse_symbol(&es->asym, ax);
1436 break;
1437 default:
1438 break;
1439 }
1440}
1441
1442/* Parse the line number info for file descriptor FH into
1443 GDB's linetable LT. MIPS' encoding requires a little bit
1444 of magic to get things out. Note also that MIPS' line
1445 numbers can go back and forth, apparently we can live
1446 with that and do not need to reorder our linetables */
1447
1448static
1449parse_lines(fh, lt)
1450 FDR *fh;
1451 struct linetable *lt;
1452{
3eaebb75 1453 unsigned char *base = (unsigned char*)fh->cbLineOffset;
bd5635a1
RP
1454 int i, j, k;
1455 int delta, count, lineno = 0;
1456 PDR *pr;
1457
1458 if (base == 0)
1459 return;
1460
1461 /* Scan by procedure descriptors */
1462 i = 0; j = 0, k = 0;
1463 for (pr = (PDR*)IPDFIRST(cur_hdr,fh); j < fh->cpd; j++, pr++) {
1464 int l, halt;
1465
1466 /* No code for this one */
1467 if (pr->iline == ilineNil ||
1468 pr->lnLow == -1 || pr->lnHigh == -1)
1469 continue;
1470 /*
1471 * Aurgh! To know where to stop expanding we
1472 * must look-ahead.
1473 */
1474 for (l = 1; l < (fh->cpd - j); l++)
1475 if (pr[l].iline != -1)
1476 break;
1477 if (l == (fh->cpd - j))
1478 halt = fh->cline;
1479 else
1480 halt = pr[l].iline;
1481 /*
1482 * When procedures are moved around the linenumbers
1483 * are attributed to the next procedure up
1484 */
1485 if (pr->iline >= halt) continue;
1486
3eaebb75 1487 base = (unsigned char*)pr->cbLineOffset;
bd5635a1
RP
1488 l = pr->adr >> 2; /* in words */
1489 halt += (pr->adr >> 2) - pr->iline;
1490 for (lineno = pr->lnLow; l < halt;) {
1491 count = *base & 0x0f;
1492 delta = *base++ >> 4;
3eaebb75
SG
1493 if (delta >= 8)
1494 delta -= 16;
bd5635a1 1495 if (delta == -8) {
3eaebb75 1496 delta = (base[0] << 8) | base[1];
4a35d6e9
FF
1497 if (delta >= 0x8000)
1498 delta -= 0x10000;
bd5635a1
RP
1499 base += 2;
1500 }
1501 lineno += delta;/* first delta is 0 */
1502 k = add_line(lt, lineno, l, k);
1503 l += count + 1;
1504 }
1505 }
1506}
1507
1508
1509/* Parse the symbols of the file described by FH, whose index is F_IDX.
1510 BOUND is the highest core address of this file's procedures */
1511
1512static
1513parse_one_file(fh, f_idx, bound)
1514 FDR *fh;
1515{
1516 register int s_idx;
1517 SYMR *sh;
1518 PDR *pr;
1519
1520 /* Parse local symbols first */
1521
1522 for (s_idx = 0; s_idx < fh->csym; s_idx++) {
1523 sh = (SYMR *) (fh->isymBase) + s_idx;
1524 cur_sdx = s_idx;
1525 parse_symbol(sh, fh->iauxBase);
1526 }
1527
1528 /* Procedures next, note we need to look-ahead to
1529 find out where the procedure's code ends */
1530
1531 for (s_idx = 0; s_idx < fh->cpd-1; s_idx++) {
1532 pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx;
1533 parse_procedure(pr, pr[1].adr); /* next proc up */
1534 }
1535 if (fh->cpd) {
1536 pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx;
1537 parse_procedure(pr, bound); /* next file up */
1538 }
1539
1540 /* Linenumbers. At the end, check if we can save memory */
1541 parse_lines(fh, LINETABLE(cur_stab));
1542 if (LINETABLE(cur_stab)->nitems < fh->cline)
1543 shrink_linetable(cur_stab);
1544}
101f259c
JG
1545\f
1546/* Master parsing procedure for first-pass reading of file symbols
1547 into a partial_symtab.
bd5635a1 1548
3eaebb75 1549 Parses the symtab described by the global symbolic header CUR_HDR.
101f259c
JG
1550 END_OF_TEXT_SEG gives the address just after the text segment for
1551 the symtab we are reading. */
bd5635a1 1552
bd5635a1 1553static
a048c8f5 1554parse_partial_symbols(end_of_text_seg, objfile)
3eaebb75 1555 int end_of_text_seg;
a048c8f5 1556 struct objfile *objfile;
bd5635a1 1557{
101f259c 1558 int f_idx, s_idx, h_max, stat_idx;
3eaebb75 1559 HDRR *hdr;
bd5635a1 1560 /* Running pointers */
101f259c
JG
1561 FDR *fh;
1562 RFDT *rh;
1563 register EXTR *esh;
1564 register SYMR *sh;
1565 struct partial_symtab *pst;
bd5635a1
RP
1566
1567 /*
1568 * Big plan:
1569 *
101f259c 1570 * Only parse the Local and External symbols, and the Relative FDR.
bd5635a1 1571 * Fixup enough of the loader symtab to be able to use it.
0c4d2cc2 1572 * Allocate space only for the file's portions we need to
bd5635a1
RP
1573 * look at. (XXX)
1574 */
1575
3eaebb75 1576 hdr = cur_hdr;
bd5635a1
RP
1577 max_gdbinfo = 0;
1578 max_glevel = MIN_GLEVEL;
1579
1580 /* Allocate the map FDR -> PST.
1581 Minor hack: -O3 images might claim some global data belongs
1582 to FDR -1. We`ll go along with that */
1583 fdr_to_pst = (struct pst_map *)xzalloc((hdr->ifdMax+1) * sizeof *fdr_to_pst);
1584 fdr_to_pst++;
1585 {
a048c8f5 1586 struct partial_symtab * pst = new_psymtab("", objfile);
bd5635a1 1587 fdr_to_pst[-1].pst = pst;
4a35d6e9 1588 FDR_IDX(pst) = -1;
bd5635a1
RP
1589 }
1590
1591 /* Now scan the FDRs, mostly for dependencies */
1592 for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++)
a048c8f5 1593 (void) parse_fdr(f_idx, 1, objfile);
bd5635a1
RP
1594
1595 /* Take a good guess at how many symbols we might ever need */
1596 h_max = hdr->iextMax;
1597
1598 /* Parse externals: two passes because they can be ordered
101f259c
JG
1599 in any way, but gdb likes to have them segregated by their
1600 source file. */
bd5635a1 1601
101f259c 1602 /* Pass 1 over external syms: Presize and partition the list */
bd5635a1
RP
1603 for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
1604 esh = (EXTR *) (hdr->cbExtOffset) + s_idx;
1605 fdr_to_pst[esh->ifd].n_globals++;
1606 }
1607
1608 if (global_psymbols.list) {
101f259c
JG
1609 int origsize = global_psymbols.next - global_psymbols.list;
1610
bd5635a1 1611 global_psymbols.list = (struct partial_symbol *)
101f259c
JG
1612 xrealloc (global_psymbols.list,
1613 (h_max + origsize) * sizeof(struct partial_symbol));
1614 global_psymbols.next = global_psymbols.list + origsize;
1615 global_psymbols.size = h_max + origsize;
bd5635a1
RP
1616 } else {
1617 global_psymbols.list = (struct partial_symbol *)
101f259c 1618 xmalloc (h_max * sizeof(struct partial_symbol));
bd5635a1 1619 global_psymbols.next = global_psymbols.list;
101f259c 1620 global_psymbols.size = h_max;
bd5635a1
RP
1621 }
1622
101f259c
JG
1623 /* Pass 1.5 over files: partition out global symbol space */
1624 s_idx = global_psymbols.next - global_psymbols.list;
bd5635a1
RP
1625 for (f_idx = -1; f_idx < hdr->ifdMax; f_idx++) {
1626 fdr_to_pst[f_idx].pst->globals_offset = s_idx;
1627 s_idx += fdr_to_pst[f_idx].n_globals;
1628 }
1629
101f259c
JG
1630 /* Pass 1.6 over files: partition out static symbol space.
1631 Note that this loop starts at 0, not at -1. */
1632 stat_idx = static_psymbols.next - static_psymbols.list;
1633 for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
1634 fdr_to_pst[f_idx].pst->statics_offset = stat_idx;
1635 fh = f_idx + (FDR *)(hdr->cbFdOffset);
1636 stat_idx += fh->csym;
1637 }
1638
1639 /* Now that we know its max size, allocate static symbol list */
1640 if (static_psymbols.list) {
1641 int origsize = static_psymbols.next - static_psymbols.list;
1642
1643 static_psymbols.list = (struct partial_symbol *)
1644 xrealloc (static_psymbols.list,
1645 stat_idx * sizeof(struct partial_symbol));
1646 static_psymbols.next = static_psymbols.list + origsize;
1647 static_psymbols.size = stat_idx;
1648 } else {
1649 static_psymbols.list = (struct partial_symbol *)
1650 xmalloc (stat_idx * sizeof(struct partial_symbol));
1651 static_psymbols.next = static_psymbols.list;
1652 static_psymbols.size = stat_idx;
1653 }
1654
1655 /* Pass 2 over external syms: fill in external symbols */
bd5635a1
RP
1656 for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
1657 register struct partial_symbol *p;
1658 enum misc_function_type misc_type = mf_text;
1659 esh = (EXTR *) (hdr->cbExtOffset) + s_idx;
1660
1661 if (esh->asym.sc == scUndefined || esh->asym.sc == scNil)
1662 continue;
101f259c
JG
1663
1664 /* Locate the psymtab and the preallocated psymbol. */
1665 pst = fdr_to_pst[esh->ifd].pst;
1666 p = global_psymbols.list + pst->globals_offset +
1667 pst->n_global_syms++;
1668 SYMBOL_NAME(p) = (char *)(esh->asym.iss);
bd5635a1
RP
1669 SYMBOL_NAMESPACE(p) = VAR_NAMESPACE;
1670
1671 switch (esh->asym.st) {
1672 case stProc:
1673 SYMBOL_CLASS(p) = LOC_BLOCK;
101f259c 1674 SYMBOL_VALUE(p) = esh->asym.value;
bd5635a1
RP
1675 break;
1676 case stGlobal:
1677 SYMBOL_CLASS(p) = LOC_STATIC;
101f259c 1678 SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)esh->asym.value;
bd5635a1
RP
1679 misc_type = mf_data;
1680 break;
1681 case stLabel:
1682 SYMBOL_CLASS(p) = LOC_LABEL;
101f259c 1683 SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)esh->asym.value;
bd5635a1
RP
1684 break;
1685 default:
1686 misc_type = mf_unknown;
1687 complain (&unknown_ext_complaint, SYMBOL_NAME(p));
1688 }
1689 prim_record_misc_function (SYMBOL_NAME(p),
1690 SYMBOL_VALUE(p),
1691 misc_type);
1692 }
1693
101f259c
JG
1694 /* Pass 3 over files, over local syms: fill in static symbols */
1695 for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
1696 fh = f_idx + (FDR *)(cur_hdr->cbFdOffset);
1697 pst = fdr_to_pst[f_idx].pst;
4a35d6e9 1698 pst->texthigh = pst->textlow;
101f259c
JG
1699
1700 for (s_idx = 0; s_idx < fh->csym; ) {
1701 register struct partial_symbol *p;
1702
1703 sh = s_idx + (SYMR *) fh->isymBase;
1704
1705 if (sh->sc == scUndefined || sh->sc == scNil) {
1706 /* FIXME, premature? */
1707 s_idx++;
1708 continue;
1709 }
1710
1711 /* Locate the preallocated psymbol. */
1712 p = static_psymbols.list + pst->statics_offset +
1713 pst->n_static_syms;
1714 SYMBOL_NAME(p) = (char *)(sh->iss);
1715 SYMBOL_VALUE(p) = sh->value;
1716 SYMBOL_NAMESPACE(p) = VAR_NAMESPACE;
1717
1718 switch (sh->st) {
1719 case stProc: /* Asm labels apparently */
1720 case stStaticProc: /* Function */
1721 SYMBOL_CLASS(p) = LOC_BLOCK;
1722 pst->n_static_syms++; /* Use gdb symbol */
1723 /* Skip over procedure to next one. */
1724 s_idx = (sh->index + (AUXU *)fh->iauxBase)
1725 ->isym;
4a35d6e9
FF
1726 {
1727 long high;
1728 long procaddr = sh->value;
1729
1730 sh = s_idx + (SYMR *) fh->isymBase - 1;
1731 if (sh->st != stEnd)
1732 continue;
1733 high = procaddr + sh->value;
1734 if (high > pst->texthigh)
1735 pst->texthigh = high;
1736 }
101f259c
JG
1737 continue;
1738 case stStatic: /* Variable */
1739 SYMBOL_CLASS(p) = LOC_STATIC;
1740 SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)sh->value;
1741 break;
1742 case stTypedef: /* Typedef */
1743 SYMBOL_CLASS(p) = LOC_TYPEDEF;
1744 break;
1745 case stConstant: /* Constant decl */
1746 SYMBOL_CLASS(p) = LOC_CONST;
1747 break;
4a35d6e9
FF
1748 case stBlock: /* { }, str, un, enum*/
1749 if (sh->sc == scInfo) {
1750 SYMBOL_NAMESPACE(p) = STRUCT_NAMESPACE;
1751 SYMBOL_CLASS(p) = LOC_TYPEDEF;
1752 pst->n_static_syms++;
1753 }
101f259c
JG
1754 /* Skip over the block */
1755 s_idx = sh->index;
1756 continue;
1757 case stFile: /* File headers */
1758 case stLabel: /* Labels */
1759 case stEnd: /* Ends of files */
1760 goto skip;
1761 default:
1762 complain (&unknown_sym_complaint, SYMBOL_NAME(p));
1763 complain (&unknown_st_complaint, sh->st);
1764 s_idx++;
1765 continue;
1766 }
1767 pst->n_static_syms++; /* Use this gdb symbol */
1768 skip:
1769 s_idx++; /* Go to next file symbol */
1770#if 0
1771/* We don't usually record static syms, but some we seem to. chk dbxread. */
1772/*FIXME*/ prim_record_misc_function (SYMBOL_NAME(p),
1773 SYMBOL_VALUE(p),
1774 misc_type);
1775#endif
1776 }
1777 }
bd5635a1 1778
4a35d6e9 1779 /* The array (of lists) of globals must be sorted. */
3eaebb75
SG
1780 reorder_psymtabs();
1781
4a35d6e9 1782 /* Now sort the global psymbols. */
bd5635a1
RP
1783 for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
1784 struct partial_symtab *pst = fdr_to_pst[f_idx].pst;
1785 if (pst->n_global_syms > 1)
1786 qsort (global_psymbols.list + pst->globals_offset,
1787 pst->n_global_syms, sizeof (struct partial_symbol),
1788 compare_psymbols);
bd5635a1
RP
1789 }
1790
1791 /* Mark the last code address, and remember it for later */
b8c50f09 1792 hdr->cbDnOffset = end_of_text_seg;
bd5635a1 1793
bd5635a1
RP
1794 free(&fdr_to_pst[-1]);
1795 fdr_to_pst = 0;
1796}
1797
1798
1799/* Do the initial analisys of the F_IDX-th file descriptor.
1800 Allocates a partial symtab for it, and builds the list
1801 of dependent files by recursion. LEV says at which level
1802 of recursion we are called (to pretty up debug traces) */
1803
1804static struct partial_symtab *
a048c8f5 1805parse_fdr(f_idx, lev, objfile)
bd5635a1 1806 int f_idx;
a048c8f5
JG
1807 int lev;
1808 struct objfile *objfile;
bd5635a1
RP
1809{
1810 register FDR *fh;
1811 register struct partial_symtab *pst;
1812 int s_idx, s_id0;
1813
1814 fh = (FDR *) (cur_hdr->cbFdOffset) + f_idx;
1815
1816 /* Use this to indicate into which symtab this file was parsed */
1817 if (fh->ioptBase)
1818 return (struct partial_symtab *) fh->ioptBase;
1819
1820 /* Debuggability level */
1821 if (compare_glevel(max_glevel, fh->glevel) < 0)
1822 max_glevel = fh->glevel;
1823
1824 /* Make a new partial_symtab */
a048c8f5 1825 pst = new_psymtab(fh->rss, objfile);
bd5635a1
RP
1826 if (fh->cpd == 0){
1827 pst->textlow = 0;
1828 pst->texthigh = 0;
1829 } else {
1830 pst->textlow = fh->adr;
1831 pst->texthigh = fh->cpd; /* To be fixed later */
1832 }
bd5635a1 1833
101f259c 1834 /* Make everything point to everything. */
4a35d6e9 1835 FDR_IDX(pst) = f_idx;
bd5635a1
RP
1836 fdr_to_pst[f_idx].pst = pst;
1837 fh->ioptBase = (int)pst;
1838
1839 /* Analyze its dependencies */
1840 if (fh->crfd <= 1)
1841 return pst;
1842
1843 s_id0 = 0;
1844 if (fh->cpd == 0) { /* If there are no functions defined here ... */
1845 /* ...then presumably a .h file: drop reverse depends .h->.c */
1846 for (; s_id0 < fh->crfd; s_id0++) {
1847 RFDT *rh = (RFDT *) (fh->rfdBase) + s_id0;
1848 if (*rh == f_idx) {
1849 s_id0++; /* Skip self-dependency */
1850 break;
1851 }
1852 }
1853 }
1854 pst->number_of_dependencies = fh->crfd - s_id0;
1855 pst->dependencies = (struct partial_symtab **)
1856 obstack_alloc (psymbol_obstack,
3eaebb75
SG
1857 pst->number_of_dependencies *
1858 sizeof (struct partial_symtab *));
bd5635a1
RP
1859 for (s_idx = s_id0; s_idx < fh->crfd; s_idx++) {
1860 RFDT *rh = (RFDT *) (fh->rfdBase) + s_idx;
1861
a048c8f5 1862 pst->dependencies[s_idx-s_id0] = parse_fdr(*rh, lev+1, objfile);
bd5635a1
RP
1863 }
1864
1865 return pst;
1866}
1867
1868
1869/* Ancillary function to psymtab_to_symtab(). Does all the work
1870 for turning the partial symtab PST into a symtab, recurring
3eaebb75
SG
1871 first on all dependent psymtabs. The argument FILENAME is
1872 only passed so we can see in debug stack traces what file
1873 is being read. */
bd5635a1 1874
e072c738 1875static void
3eaebb75 1876psymtab_to_symtab_1(pst, filename)
bd5635a1 1877 struct partial_symtab *pst;
3eaebb75 1878 char *filename;
bd5635a1
RP
1879{
1880 int i, f_max;
1881 struct symtab *st;
1882 FDR *fh;
1883
1884 if (pst->readin)
1885 return;
1886 pst->readin = 1;
1887
1888 pending_list = (struct pending **) cur_hdr->cbOptOffset;
1889 if (pending_list == 0) {
1890 pending_list = (struct pending **)
1891 xzalloc(cur_hdr->ifdMax * sizeof(struct pending *));
1892 cur_hdr->cbOptOffset = (int)pending_list;
1893 }
1894
1895 /* How many symbols will we need */
3eaebb75 1896 /* FIXME, this does not count enum values. */
bd5635a1 1897 f_max = pst->n_global_syms + pst->n_static_syms;
4a35d6e9 1898 if (FDR_IDX(pst) == -1) {
bd5635a1 1899 fh = 0;
a048c8f5 1900 st = new_symtab ("unknown", f_max, 0, pst->objfile);
bd5635a1 1901 } else {
4a35d6e9 1902 fh = (FDR *) (cur_hdr->cbFdOffset) + FDR_IDX(pst);
bd5635a1 1903 f_max += fh->csym + fh->cpd;
a048c8f5
JG
1904 st = new_symtab (pst->filename, 2 * f_max, 2 * fh->cline,
1905 pst->objfile);
bd5635a1
RP
1906 }
1907
3eaebb75
SG
1908 /* Read in all partial symbtabs on which this one is dependent.
1909 NOTE that we do have circular dependencies, sigh. We solved
1910 that by setting pst->readin before this point. */
1911
bd5635a1
RP
1912 for (i = 0; i < pst->number_of_dependencies; i++)
1913 if (!pst->dependencies[i]->readin) {
3eaebb75
SG
1914 /* Inform about additional files to be read in. */
1915 if (info_verbose)
1916 {
1917 fputs_filtered (" ", stdout);
1918 wrap_here ("");
1919 fputs_filtered ("and ", stdout);
1920 wrap_here ("");
1921 printf_filtered ("%s...",
1922 pst->dependencies[i]->filename);
1923 wrap_here (""); /* Flush output */
1924 fflush (stdout);
1925 }
1926 /* We only pass the filename for debug purposes */
1927 psymtab_to_symtab_1(pst->dependencies[i],
1928 pst->dependencies[i]->filename);
bd5635a1
RP
1929 }
1930
1931 /* Now read the symbols for this symtab */
1932
4a35d6e9 1933 cur_fd = FDR_IDX(pst);
bd5635a1
RP
1934 cur_fdr = fh;
1935 cur_stab = st;
1936
1937 /* Get a new lexical context */
1938
1939 push_parse_stack();
1940 top_stack->cur_st = cur_stab;
d219db01 1941 top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(cur_stab),
101f259c 1942 STATIC_BLOCK);
bd5635a1
RP
1943 BLOCK_START(top_stack->cur_block) = fh ? fh->adr : 0;
1944 BLOCK_END(top_stack->cur_block) = 0;
1945 top_stack->blocktype = stFile;
3eaebb75 1946 top_stack->maxsyms = 2*f_max;
bd5635a1
RP
1947 top_stack->cur_type = 0;
1948 top_stack->procadr = 0;
1949 top_stack->numargs = 0;
1950
1951 /* Parse locals and procedures */
1952 if (fh)
1953 parse_one_file(fh, cur_fd, (cur_fd == (cur_hdr->ifdMax - 1)) ?
1954 cur_hdr->cbDnOffset : fh[1].adr);
1955
1956 /* .. and our share of externals.
101f259c
JG
1957 XXX use the global list to speed up things here. how ?
1958 FIXME, Maybe quit once we have found the right number of ext's? */
1959 /* parse_external clobbers top_stack->cur_block and ->cur_st here. */
bd5635a1
RP
1960 top_stack->blocktype = stFile;
1961 top_stack->maxsyms = cur_hdr->isymMax + cur_hdr->ipdMax + cur_hdr->iextMax;
1962 for (i = 0; i < cur_hdr->iextMax; i++) {
1963 register EXTR *esh = (EXTR *) (cur_hdr->cbExtOffset) + i;
1964 if (esh->ifd == cur_fd)
1965 parse_external(esh, 1);
1966 }
1967
1968 /* If there are undefined, tell the user */
1969 if (n_undef_symbols) {
1970 printf_filtered("File %s contains %d unresolved references:",
1971 st->filename, n_undef_symbols);
1972 printf_filtered("\n\t%4d variables\n\t%4d procedures\n\t%4d labels\n",
1973 n_undef_vars, n_undef_procs, n_undef_labels);
1974 n_undef_symbols = n_undef_labels = n_undef_vars = n_undef_procs = 0;
1975 }
1976
1977 pop_parse_stack();
1978
1979 /*
1980 * Sort the symbol table now, we are done adding symbols to it.
1981 */
1982 sort_symtab_syms(st);
bd5635a1 1983
101f259c
JG
1984 /* Now link the psymtab and the symtab. */
1985 pst->symtab = st;
1986}
bd5635a1
RP
1987\f
1988/* Ancillary parsing procedures. */
1989
1990/* Lookup the type at relative index RN. Return it in TPP
1991 if found and in any event come up with its name PNAME.
1992 Return value says how many aux symbols we ate */
1993
1994static
1995cross_ref(rn, tpp, pname)
1996 RNDXR *rn;
1997 struct type **tpp;
1998 char **pname;
1999{
2000 unsigned rf;
2001
2002 /* Escape index means 'the next one' */
2003 if (rn->rfd == 0xfff)
2004 rf = *(unsigned *) (rn + 1);
2005 else
2006 rf = rn->rfd;
2007
2008 if (rf == -1) {
2009 /* Ooops */
2010 *pname = "<undefined>";
2011 } else {
2012 /*
2013 * Find the relative file descriptor and the symbol in it
2014 */
2015 FDR *fh = get_rfd(cur_fd, rf);
2016 SYMR *sh;
2017 struct type *t;
2018
2019 /*
2020 * If we have processed this symbol then we left a forwarding
2021 * pointer to the corresponding GDB symbol. If not, we`ll put
2022 * it in a list of pending symbols, to be processed later when
2023 * the file f will be. In any event, we collect the name for
2024 * the type here. Which is why we made a first pass at
2025 * strings.
2026 */
2027 sh = (SYMR *) (fh->isymBase) + rn->index;
2028
2029 /* Careful, we might be looking at .o files */
2030 *pname = (UNSAFE_DATA_ADDR(sh->iss)) ? "<undefined>" :
2031 (char *) sh->iss;
2032
2033 /* Have we parsed it ? */
2034 if ((!UNSAFE_DATA_ADDR(sh->value)) && (sh->st == stParsed)) {
2035 t = (struct type *) sh->value;
2036 *tpp = t;
2037 } else {
2038 struct pending *p;
2039
2040 /* Avoid duplicates */
2041 p = is_pending_symbol(fh, sh);
2042
2043 if (p)
2044 *tpp = p->t;
2045 else
2046 add_pending(fh, sh, *tpp);
2047 }
2048 }
3eaebb75
SG
2049
2050 /* We used one auxent normally, two if we got a "next one" rf. */
2051 return (rn->rfd == 0xfff? 2: 1);
bd5635a1
RP
2052}
2053
2054
2055/* Quick&dirty lookup procedure, to avoid the MI ones that require
2056 keeping the symtab sorted */
2057
2058static struct symbol *
2059mylookup_symbol (name, block, namespace, class)
2060 char *name;
2061 register struct block *block;
2062 enum namespace namespace;
2063 enum address_class class;
2064{
2065 register int bot, top, inc;
2066 register struct symbol *sym;
2067
2068 bot = 0;
2069 top = BLOCK_NSYMS(block);
2070 inc = name[0];
2071 while (bot < top) {
2072 sym = BLOCK_SYM(block, bot);
2073 if (SYMBOL_NAME(sym)[0] == inc
2074 && SYMBOL_NAMESPACE(sym) == namespace
2075 && SYMBOL_CLASS(sym) == class
2076 && !strcmp(SYMBOL_NAME(sym), name))
2077 return sym;
2078 bot++;
2079 }
2080 if (block = BLOCK_SUPERBLOCK (block))
2081 return mylookup_symbol (name, block, namespace, class);
2082 return 0;
2083}
2084
2085
3eaebb75
SG
2086/* Add a new symbol S to a block B.
2087 Infrequently, we will need to reallocate the block to make it bigger.
2088 We only detect this case when adding to top_stack->cur_block, since
2089 that's the only time we know how big the block is. FIXME. */
bd5635a1 2090
3eaebb75 2091static void
bd5635a1
RP
2092add_symbol(s,b)
2093 struct symbol *s;
2094 struct block *b;
2095{
3eaebb75
SG
2096 int nsyms = BLOCK_NSYMS(b)++;
2097 struct block *origb;
2098 struct parse_stack *stackp;
2099
bd5635a1 2100 if (b == top_stack->cur_block &&
3eaebb75
SG
2101 nsyms >= top_stack->maxsyms) {
2102 complain (&block_overflow_complaint, s->name);
2103 /* In this case shrink_block is actually grow_block, since
2104 BLOCK_NSYMS(b) is larger than its current size. */
2105 origb = b;
2106 b = shrink_block (top_stack->cur_block, top_stack->cur_st);
2107
2108 /* Now run through the stack replacing pointers to the
2109 original block. shrink_block has already done this
2110 for the blockvector and BLOCK_FUNCTION. */
2111 for (stackp = top_stack; stackp; stackp = stackp->next) {
2112 if (stackp->cur_block == origb) {
2113 stackp->cur_block = b;
2114 stackp->maxsyms = BLOCK_NSYMS (b);
2115 }
2116 }
2117 }
2118 BLOCK_SYM(b,nsyms) = s;
bd5635a1
RP
2119}
2120
2121/* Add a new block B to a symtab S */
2122
3eaebb75 2123static void
bd5635a1
RP
2124add_block(b,s)
2125 struct block *b;
2126 struct symtab *s;
2127{
2128 struct blockvector *bv = BLOCKVECTOR(s);
2129
2130 bv = (struct blockvector *)xrealloc(bv, sizeof(struct blockvector) +
2131 BLOCKVECTOR_NBLOCKS(bv) * sizeof(bv->block));
2132 if (bv != BLOCKVECTOR(s))
2133 BLOCKVECTOR(s) = bv;
2134
2135 BLOCKVECTOR_BLOCK(bv, BLOCKVECTOR_NBLOCKS(bv)++) = b;
2136}
2137
2138/* Add a new linenumber entry (LINENO,ADR) to a linevector LT.
2139 MIPS' linenumber encoding might need more than one byte
2140 to describe it, LAST is used to detect these continuation lines */
2141
3eaebb75 2142static int
bd5635a1
RP
2143add_line(lt, lineno, adr, last)
2144 struct linetable *lt;
3eaebb75 2145 int lineno;
bd5635a1 2146 CORE_ADDR adr;
3eaebb75 2147 int last;
bd5635a1
RP
2148{
2149 if (last == 0)
2150 last = -2; /* make sure we record first line */
2151
2152 if (last == lineno) /* skip continuation lines */
2153 return lineno;
2154
2155 lt->item[lt->nitems].line = lineno;
2156 lt->item[lt->nitems++].pc = adr << 2;
2157 return lineno;
2158}
2159
2160
2161\f
2162/* Comparison functions, used when sorting things */
2163
2164/* Symtabs must be ordered viz the code segments they cover */
2165
2166static int
2167compare_symtabs( s1, s2)
2168 struct symtab **s1, **s2;
2169{
2170 /* "most specific" first */
2171
2172 register struct block *b1, *b2;
d219db01
JG
2173 b1 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s1),GLOBAL_BLOCK);
2174 b2 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s2),GLOBAL_BLOCK);
bd5635a1
RP
2175 if (BLOCK_END(b1) == BLOCK_END(b2))
2176 return BLOCK_START(b1) - BLOCK_START(b2);
2177 return BLOCK_END(b1) - BLOCK_END(b2);
2178}
2179
2180
2181/* Partial Symtabs, same */
2182
2183static int
2184compare_psymtabs( s1, s2)
2185 struct partial_symtab **s1, **s2;
2186{
2187 /* Perf twist: put the ones with no code at the end */
2188
2189 register int a = (*s1)->textlow;
2190 register int b = (*s2)->textlow;
2191 if (a == 0)
2192 return b;
2193 if (b == 0)
2194 return -a;
2195 return a - b;
2196}
2197
2198
2199/* Partial symbols are compared lexicog by their print names */
2200
2201static int
2202compare_psymbols (s1, s2)
2203 register struct partial_symbol *s1, *s2;
2204{
2205 register char
2206 *st1 = SYMBOL_NAME(s1),
2207 *st2 = SYMBOL_NAME(s2);
2208
2209 return (st1[0] - st2[0] ? st1[0] - st2[0] :
2210 strcmp(st1 + 1, st2 + 1));
2211}
2212
2213/* Blocks with a smaller low bound should come first */
2214
2215static int compare_blocks(b1,b2)
2216 struct block **b1, **b2;
2217{
2218 register int addr_diff;
2219
2220 addr_diff = (BLOCK_START((*b1))) - (BLOCK_START((*b2)));
2221 if (addr_diff == 0)
2222 return (BLOCK_END((*b1))) - (BLOCK_END((*b2)));
2223 return addr_diff;
2224}
2225
2226\f
2227/* Sorting and reordering procedures */
2228
2229/* Sort the blocks of a symtab S.
2230 Reorder the blocks in the blockvector by code-address,
2231 as required by some MI search routines */
2232
e072c738 2233static void
bd5635a1
RP
2234sort_blocks(s)
2235 struct symtab *s;
2236{
2237 struct blockvector *bv = BLOCKVECTOR(s);
2238
2239 if (BLOCKVECTOR_NBLOCKS(bv) <= 2) {
2240 /* Cosmetic */
d219db01
JG
2241 if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) == 0)
2242 BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = 0;
101f259c
JG
2243 if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) == 0)
2244 BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = 0;
bd5635a1
RP
2245 return;
2246 }
2247 /*
2248 * This is very unfortunate: normally all functions are compiled in
2249 * the order they are found, but if the file is compiled -O3 things
2250 * are very different. It would be nice to find a reliable test
2251 * to detect -O3 images in advance.
2252 */
2253 if (BLOCKVECTOR_NBLOCKS(bv) > 3)
d219db01
JG
2254 qsort(&BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK),
2255 BLOCKVECTOR_NBLOCKS(bv) - FIRST_LOCAL_BLOCK,
bd5635a1
RP
2256 sizeof(struct block *),
2257 compare_blocks);
2258
2259 {
2260 register CORE_ADDR high = 0;
2261 register int i, j = BLOCKVECTOR_NBLOCKS(bv);
2262
d219db01 2263 for (i = FIRST_LOCAL_BLOCK; i < j; i++)
bd5635a1
RP
2264 if (high < BLOCK_END(BLOCKVECTOR_BLOCK(bv,i)))
2265 high = BLOCK_END(BLOCKVECTOR_BLOCK(bv,i));
d219db01 2266 BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = high;
bd5635a1
RP
2267 }
2268
d219db01
JG
2269 BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) =
2270 BLOCK_START(BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK));
bd5635a1 2271
d219db01
JG
2272 BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) =
2273 BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK));
2274 BLOCK_END (BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) =
2275 BLOCK_END (BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK));
bd5635a1
RP
2276}
2277
2278/* Sort the symtab list, as required by some search procedures.
2279 We want files ordered to make them look right to users, and for
2280 searching (see block_for_pc). */
2281
e072c738 2282static void
bd5635a1
RP
2283reorder_symtabs()
2284{
2285 register int i;
2286 struct symtab *stab;
e072c738
JG
2287 register struct symtab **all_symtabs;
2288 register int symtab_count;
2289
2290 if (!symtab_list)
2291 return;
bd5635a1
RP
2292
2293 /* Create an array of pointers to all the symtabs. */
e072c738
JG
2294 for (symtab_count = 0, stab = symtab_list;
2295 stab;
2296 symtab_count++, stab = stab->next) {
2297 obstack_grow (psymbol_obstack, &stab, sizeof (stab));
2298 /* FIXME: Only sort blocks for new symtabs ??? */
2299 sort_blocks(stab);
bd5635a1
RP
2300 }
2301
e072c738
JG
2302 all_symtabs = (struct symtab **)
2303 obstack_base (psymbol_obstack);
2304 qsort((char *)all_symtabs, symtab_count,
bd5635a1
RP
2305 sizeof(struct symtab *), compare_symtabs);
2306
2307 /* Re-construct the symtab list, but now it is sorted. */
e072c738 2308 for (i = 0; i < symtab_count-1; i++)
bd5635a1
RP
2309 all_symtabs[i]->next = all_symtabs[i+1];
2310 all_symtabs[i]->next = 0;
2311 symtab_list = all_symtabs[0];
e072c738 2312
bd5635a1
RP
2313 obstack_free (psymbol_obstack, all_symtabs);
2314}
2315
e072c738
JG
2316/* Sort the partial symtab list, as required by some search procedures.
2317 PC lookups stop at the first psymtab such that textlow <= PC < texthigh */
bd5635a1 2318
e072c738
JG
2319static void
2320reorder_psymtabs()
bd5635a1
RP
2321{
2322 register int i;
e072c738 2323 register int all_psymtabs_count;
bd5635a1 2324 struct partial_symtab *pstab;
e072c738
JG
2325 struct partial_symtab **all_psymtabs;
2326
2327 if (!partial_symtab_list)
2328 return;
bd5635a1 2329
bd5635a1 2330 /* Create an array of pointers to all the partial_symtabs. */
e072c738
JG
2331
2332 for (all_psymtabs_count = 0, pstab = partial_symtab_list;
2333 pstab;
2334 all_psymtabs_count++, pstab = pstab->next)
2335 obstack_grow (psymbol_obstack, &pstab, sizeof (pstab));
2336
2337 all_psymtabs = (struct partial_symtab **)
2338 obstack_base (psymbol_obstack);
2339
2340 qsort((char *)all_psymtabs, all_psymtabs_count,
bd5635a1
RP
2341 sizeof(struct partial_symtab *), compare_psymtabs);
2342
2343 /* Re-construct the partial_symtab_list, but now it is sorted. */
2344
2345 for (i = 0; i < all_psymtabs_count-1; i++)
2346 all_psymtabs[i]->next = all_psymtabs[i+1];
2347 all_psymtabs[i]->next = 0;
2348 partial_symtab_list = all_psymtabs[0];
2349
2350 obstack_free (psymbol_obstack, all_psymtabs);
2351}
bd5635a1
RP
2352\f
2353/* Constructor/restructor/destructor procedures */
2354
2355/* Allocate a new symtab for NAME. Needs an estimate of how many symbols
2356 MAXSYMS and linenumbers MAXLINES we'll put in it */
2357
2358static
2359struct symtab *
a048c8f5 2360new_symtab(name, maxsyms, maxlines, objfile)
bd5635a1
RP
2361 char *name;
2362{
a048c8f5 2363 struct symtab *s = allocate_symtab (name, objfile);
bd5635a1
RP
2364
2365 LINETABLE(s) = new_linetable(maxlines);
2366
bd5635a1
RP
2367 /* All symtabs must have at least two blocks */
2368 BLOCKVECTOR(s) = new_bvect(2);
d219db01
JG
2369 BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK) = new_block(maxsyms);
2370 BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), STATIC_BLOCK) = new_block(maxsyms);
2371 BLOCK_SUPERBLOCK( BLOCKVECTOR_BLOCK(BLOCKVECTOR(s),STATIC_BLOCK)) =
2372 BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK);
bd5635a1
RP
2373
2374 s->free_code = free_linetable;
2375
2376 /* Link the new symtab into the list of such. */
2377 s->next = symtab_list;
2378 symtab_list = s;
2379
bd5635a1
RP
2380 return s;
2381}
2382
bd5635a1
RP
2383/* Allocate a new partial_symtab NAME */
2384
2385static struct partial_symtab *
a048c8f5 2386new_psymtab(name, objfile)
bd5635a1 2387 char *name;
a048c8f5 2388 struct objfile *objfile;
bd5635a1
RP
2389{
2390 struct partial_symtab *pst;
2391
2392 pst = (struct partial_symtab *)
2393 obstack_alloc (psymbol_obstack, sizeof (*pst));
2394 bzero (pst, sizeof (*pst));
2395
2396 if (name == (char*)-1) /* FIXME -- why not null here? */
2397 pst->filename = "<no name>";
2398 else
2399 pst->filename = name;
2400
a048c8f5
JG
2401 /* Chain it to its object file */
2402 pst->objfile = objfile;
2403 pst->objfile_chain = sym_objfile->psymtabs;
2404 sym_objfile->psymtabs = pst;
2405
bd5635a1
RP
2406 pst->next = partial_symtab_list;
2407 partial_symtab_list = pst;
bd5635a1 2408
0c4d2cc2 2409 /* Keep a backpointer to the file's symbols */
4a35d6e9
FF
2410 pst->read_symtab_private = (char *) obstack_alloc (psymbol_obstack,
2411 sizeof (struct symloc));
2412 CUR_HDR(pst) = cur_hdr;
bd5635a1
RP
2413
2414 /* The way to turn this into a symtab is to call... */
2415 pst->read_symtab = mipscoff_psymtab_to_symtab;
2416
2417 return pst;
2418}
2419
2420
bd5635a1
RP
2421/* Allocate a linetable array of the given SIZE */
2422
2423static
2424struct linetable *new_linetable(size)
2425{
2426 struct linetable *l;
2427
2428 size = size * sizeof(l->item) + sizeof(struct linetable);
2429 l = (struct linetable *)xmalloc(size);
2430 l->nitems = 0;
2431 return l;
2432}
2433
2434/* Oops, too big. Shrink it. This was important with the 2.4 linetables,
2435 I am not so sure about the 3.4 ones */
2436
3eaebb75
SG
2437static void
2438shrink_linetable(s)
bd5635a1
RP
2439 struct symtab *s;
2440{
2441 struct linetable *l = new_linetable(LINETABLE(s)->nitems);
2442
2443 bcopy(LINETABLE(s), l,
2444 LINETABLE(s)->nitems * sizeof(l->item) + sizeof(struct linetable));
2445 free (LINETABLE(s));
2446 LINETABLE(s) = l;
2447}
2448
2449/* Allocate and zero a new blockvector of NBLOCKS blocks. */
2450
2451static
3eaebb75
SG
2452struct blockvector *
2453new_bvect(nblocks)
bd5635a1
RP
2454{
2455 struct blockvector *bv;
2456 int size;
2457
2458 size = sizeof(struct blockvector) + nblocks * sizeof(struct block*);
2459 bv = (struct blockvector *) xzalloc(size);
2460
2461 BLOCKVECTOR_NBLOCKS(bv) = nblocks;
2462
2463 return bv;
2464}
2465
2466/* Allocate and zero a new block of MAXSYMS symbols */
2467
2468static
3eaebb75
SG
2469struct block *
2470new_block(maxsyms)
bd5635a1
RP
2471{
2472 int size = sizeof(struct block) + (maxsyms-1) * sizeof(struct symbol *);
2473 struct block *b = (struct block *)xzalloc(size);
2474
2475 return b;
2476}
2477
3eaebb75
SG
2478/* Ooops, too big. Shrink block B in symtab S to its minimal size.
2479 Shrink_block can also be used by add_symbol to grow a block. */
bd5635a1
RP
2480
2481static struct block *
2482shrink_block(b, s)
2483 struct block *b;
2484 struct symtab *s;
2485{
2486 struct block *new;
2487 struct blockvector *bv = BLOCKVECTOR(s);
2488 int i;
2489
3eaebb75 2490 /* Just reallocate it and fix references to the old one */
bd5635a1 2491
3eaebb75 2492 new = (struct block *) xrealloc ((char *)b, sizeof(struct block) +
bd5635a1
RP
2493 (BLOCK_NSYMS(b)-1) * sizeof(struct symbol *));
2494
bd5635a1
RP
2495 /* Should chase pointers to old one. Fortunately, that`s just
2496 the block`s function and inferior blocks */
3eaebb75
SG
2497 if (BLOCK_FUNCTION(new) && SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(new)) == b)
2498 SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(new)) = new;
bd5635a1
RP
2499 for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++)
2500 if (BLOCKVECTOR_BLOCK(bv,i) == b)
2501 BLOCKVECTOR_BLOCK(bv,i) = new;
2502 else if (BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) == b)
2503 BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) = new;
bd5635a1
RP
2504 return new;
2505}
2506
2507/* Create a new symbol with printname NAME */
2508
2509static
2510struct symbol *
2511new_symbol(name)
2512 char *name;
2513{
2514 struct symbol *s = (struct symbol *)
2515 obstack_alloc (symbol_obstack, sizeof (struct symbol));
2516
2517 bzero (s, sizeof (*s));
2518 SYMBOL_NAME(s) = name;
2519 return s;
2520}
2521
2522/* Create a new type with printname NAME */
2523
2524static
2525struct type *
2526new_type(name)
2527 char *name;
2528{
2529 struct type *t = (struct type *)
2530 obstack_alloc (symbol_obstack, sizeof (struct type));
2531
2532 bzero (t, sizeof (*t));
4a35d6e9 2533 TYPE_VPTR_FIELDNO (t) = -1;
bd5635a1
RP
2534 TYPE_NAME(t) = name;
2535 return t;
2536}
2537
2538/* Create and initialize a new type with printname NAME.
2539 CODE and LENGTH are the initial info we put in,
2540 UNS says whether the type is unsigned or not. */
2541
2542static
2543struct type *
2544make_type(code, length, uns, name)
2545 enum type_code code;
2546 int length, uns;
2547 char *name;
2548{
2549 register struct type *type;
2550
2551 type = (struct type *) xzalloc(sizeof(struct type));
2552 TYPE_CODE(type) = code;
2553 TYPE_LENGTH(type) = length;
2554 TYPE_FLAGS(type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2555 TYPE_NAME(type) = name;
4a35d6e9 2556 TYPE_VPTR_FIELDNO (type) = -1;
bd5635a1
RP
2557
2558 return type;
2559}
2560
2561/* Allocate a new field named NAME to the type TYPE */
2562
2563static
3eaebb75
SG
2564struct field *
2565new_field(type,name)
bd5635a1
RP
2566 struct type *type;
2567 char *name;
2568{
2569 struct field *f;
2570
2571 /* Fields are kept in an array */
2572 if (TYPE_NFIELDS(type))
2573 TYPE_FIELDS(type) = (struct field*)xrealloc(TYPE_FIELDS(type),
2574 (TYPE_NFIELDS(type)+1) * sizeof(struct field));
2575 else
3eaebb75
SG
2576 TYPE_FIELDS(type) = (struct field*)xzalloc(sizeof(struct field));
2577 f = &(TYPE_FIELD(type,TYPE_NFIELDS(type)));
2578 TYPE_NFIELDS(type)++;
bd5635a1 2579 bzero(f, sizeof(struct field));
3eaebb75 2580 f->name = name; /* Whether or not NAME is zero, this works. */
bd5635a1
RP
2581 return f;
2582}
2583
2584/* Make an enum constant for a member F of an enumerated type T */
2585
2586static
2587make_enum_constant(f,t)
2588 struct field *f;
2589 struct type *t;
2590{
2591 struct symbol *s;
2592 /*
2593 * This is awful, but that`s the way it is supposed to be
2594 * (BTW, no need to free the real 'type', it's a builtin)
2595 */
2596 f->type = (struct type *) f->bitpos;
2597
2598 s = new_symbol(f->name);
2599 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
2600 SYMBOL_CLASS(s) = LOC_CONST;
2601 SYMBOL_TYPE(s) = t;
2602 SYMBOL_VALUE(s) = f->bitpos;
2603 add_symbol(s, top_stack->cur_block);
2604}
2605
2606
2607\f
2608/* Things used for calling functions in the inferior.
2609 These functions are exported to our companion
2610 mips-dep.c file and are here because they play
2611 with the symbol-table explicitly. */
2612
2613#if 0
2614/* Need to make a new symbol on the fly for the dummy
2615 frame we put on the stack. Which goes in the.. */
2616
2617static struct symtab *dummy_symtab;
2618
2619/* Make up a dummy symbol for the code we put at END_PC,
2620 of size SIZE, invoking a function with NARGS arguments
2621 and using a frame of FRAMESIZE bytes */
2622
2623mips_create_dummy_symbol(end_pc, size, nargs, framesize)
2624{
2625 struct block *bl;
2626 struct symbol *g;
2627 struct mips_extra_func_info *gdbinfo;
2628
2629 /* Allocate symtab if not done already */
2630 if (dummy_symtab == 0)
2631 dummy_symtab = new_symtab(".dummy_symtab.", 100, 0);
2632
2633 /* Make a new block. Only needs one symbol */
2634 bl = new_block(1);
2635 BLOCK_START(bl) = end_pc - size;
2636 BLOCK_END(bl) = end_pc;
2637
d219db01
JG
2638 BLOCK_SUPERBLOCK(bl) =
2639 BLOCKVECTOR_BLOCK(BLOCKVECTOR(dummy_symtab),GLOBAL_BLOCK);
bd5635a1
RP
2640 add_block(bl, dummy_symtab);
2641 sort_blocks(dummy_symtab);
2642
2643 BLOCK_FUNCTION(bl) = new_symbol("??");
2644 SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(bl)) = bl;
2645 g = new_symbol(".gdbinfo.");
2646 BLOCK_SYM(bl,BLOCK_NSYMS(bl)++) = g;
2647
2648 SYMBOL_NAMESPACE(g) = LABEL_NAMESPACE;
2649 SYMBOL_CLASS(g) = LOC_CONST;
2650 SYMBOL_TYPE(g) = builtin_type_void;
2651 gdbinfo = (struct mips_extra_func_info *)
2652 xzalloc(sizeof(struct mips_extra_func_info));
2653
2654 SYMBOL_VALUE(g) = (long) gdbinfo;
2655
2656 gdbinfo->numargs = nargs;
2657 gdbinfo->framesize = framesize;
2658 gdbinfo->framereg = 29;
2659 gdbinfo->pcreg = 31;
2660 gdbinfo->regmask = -2;
2661 gdbinfo->regoffset = -4;
2662 gdbinfo->fregmask = 0; /* XXX */
2663 gdbinfo->fregoffset = 0; /* XXX */
2664}
2665
2666/* We just returned from the dummy code at END_PC, drop its symbol */
2667
2668mips_destroy_dummy_symbol(end_pc)
2669{
2670 struct block *bl;
2671 struct blockvector *bv = BLOCKVECTOR(dummy_symtab);
2672 int i;
2673
2674 bl = block_for_pc(end_pc);
2675 free(BLOCK_FUNCTION(bl));
2676 free(SYMBOL_VALUE(BLOCK_SYM(bl,0)));
2677 free(BLOCK_SYM(bl,0));
2678
d219db01 2679 for (i = FIRST_LOCAL_BLOCK; i < BLOCKVECTOR_NBLOCKS(bv); i++)
bd5635a1
RP
2680 if (BLOCKVECTOR_BLOCK(bv,i) == bl)
2681 break;
2682 for (; i < BLOCKVECTOR_NBLOCKS(bv) - 1; i++)
2683 BLOCKVECTOR_BLOCK(bv,i) = BLOCKVECTOR_BLOCK(bv,i+1);
2684 BLOCKVECTOR_NBLOCKS(bv)--;
2685 sort_blocks(dummy_symtab);
2686 free(bl);
2687}
2688#endif
2689
2690/* Sigtramp: make sure we have all the necessary information
2691 about the signal trampoline code. Since the official code
2692 from MIPS does not do so, we make up that information ourselves.
2693 If they fix the library (unlikely) this code will neutralize itself. */
2694
2695static
2696fixup_sigtramp()
2697{
2698 struct symbol *s;
2699 struct symtab *st;
2700 struct block *b, *b0;
2701
2702 sigtramp_address = -1;
2703
2704 /* We know it is sold as sigvec */
2705 s = lookup_symbol("sigvec", 0, VAR_NAMESPACE, 0, NULL);
2706
2707 /* Most programs do not play with signals */
2708 if (s == 0)
2709 return;
2710
2711 b0 = SYMBOL_BLOCK_VALUE(s);
2712
2713 /* A label of sigvec, to be more precise */
2714 s = lookup_symbol("sigtramp", b0, VAR_NAMESPACE, 0, NULL);
2715
2716 /* But maybe this program uses its own version of sigvec */
2717 if (s == 0)
2718 return;
2719
2720 sigtramp_address = SYMBOL_VALUE(s);
2721 sigtramp_end = sigtramp_address + 0x88; /* black magic */
2722
2723 /* Did we or MIPSco fix the library ? */
2724 if (SYMBOL_CLASS(s) == LOC_BLOCK)
2725 return;
2726
2727 /* But what symtab does it live in ? */
2728 st = find_pc_symtab(SYMBOL_VALUE(s));
2729
2730 /*
2731 * Ok, there goes the fix: turn it into a procedure, with all the
2732 * needed info. Note we make it a nested procedure of sigvec,
2733 * which is the way the (assembly) code is actually written.
2734 */
2735 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
2736 SYMBOL_CLASS(s) = LOC_BLOCK;
2737 SYMBOL_TYPE(s) = make_type(TYPE_CODE_FUNC, 4, 0, 0);
2738 TYPE_TARGET_TYPE(SYMBOL_TYPE(s)) = builtin_type_void;
2739
2740 /* Need a block to allocate .gdbinfo. in */
2741 b = new_block(1);
2742 SYMBOL_BLOCK_VALUE(s) = b;
2743 BLOCK_START(b) = sigtramp_address;
2744 BLOCK_END(b) = sigtramp_end;
2745 BLOCK_FUNCTION(b) = s;
2746 BLOCK_SUPERBLOCK(b) = BLOCK_SUPERBLOCK(b0);
2747 add_block(b, st);
2748 sort_blocks(st);
2749
2750 /* Make a .gdbinfo. for it */
2751 {
2752 struct mips_extra_func_info *e =
2753 (struct mips_extra_func_info *)
2754 xzalloc(sizeof(struct mips_extra_func_info));
2755
2756 e->numargs = 0; /* the kernel thinks otherwise */
2757 /* align_longword(sigcontext + SIGFRAME) */
2758 e->framesize = 0x150;
2759 e->framereg = SP_REGNUM;
2760 e->pcreg = 31;
2761 e->regmask = -2;
2762 e->regoffset = -(41 * sizeof(int));
2763 e->fregmask = -1;
2764 e->fregoffset = -(37 * sizeof(int));
2765 e->isym = (long)s;
2766
2767 s = new_symbol(".gdbinfo.");
2768 SYMBOL_VALUE(s) = (int) e;
2769 SYMBOL_NAMESPACE(s) = LABEL_NAMESPACE;
2770 SYMBOL_CLASS(s) = LOC_CONST;
2771 SYMBOL_TYPE(s) = builtin_type_void;
2772 }
2773
2774 BLOCK_SYM(b,BLOCK_NSYMS(b)++) = s;
2775}
bd5635a1
RP
2776\f
2777/* Initialization */
2778
2779static struct sym_fns ecoff_sym_fns = {"ecoff", 5,
2780 mipscoff_new_init, mipscoff_symfile_init,
3eaebb75 2781 mipscoff_symfile_read};
bd5635a1
RP
2782
2783_initialize_mipsread ()
2784{
2785 add_symtab_fns (&ecoff_sym_fns);
2786
bd5635a1
RP
2787 /* Missing basic types */
2788 builtin_type_string = make_type(TYPE_CODE_PASCAL_ARRAY,
2789 1, 0, "string");
2790 builtin_type_complex = make_type(TYPE_CODE_FLT,
2791 2 * sizeof(float), 0, "complex");
2792 builtin_type_double_complex = make_type(TYPE_CODE_FLT,
2793 2 * sizeof(double), 0, "double_complex");
2794 builtin_type_fixed_dec = make_type(TYPE_CODE_INT, sizeof(int),
2795 0, "fixed_decimal");
2796 builtin_type_float_dec = make_type(TYPE_CODE_FLT, sizeof(double),
2797 0, "floating_decimal");
2798
2799 /* Templates types */
bd5635a1
RP
2800 builtin_type_struct = make_type(TYPE_CODE_STRUCT, 0, 0, 0);
2801 builtin_type_union = make_type(TYPE_CODE_UNION, 0, 0, 0);
2802 builtin_type_enum = make_type(TYPE_CODE_ENUM, 0, 0, 0);
2803 builtin_type_range = make_type(TYPE_CODE_RANGE, 0, 0, 0);
2804 builtin_type_set = make_type(TYPE_CODE_SET, 0, 0, 0);
e5fa3486
JG
2805
2806 /* We can't do this now because builtin_type_void may not
2807 be set yet. Do it at symbol reading time. */
2808 /* builtin_type_ptr = lookup_pointer_type (builtin_type_void); */
bd5635a1 2809}
This page took 0.15676 seconds and 4 git commands to generate.