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