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