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