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