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