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