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