symtab.h (SYMTAB_BLOCKVECTOR): Renamed from BLOCKVECTOR. All uses updated.
[external/binutils.git] / gdb / mdebugread.c
1 /* Read a symbol table in ECOFF format (Third-Eye).
2
3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5    Original version contributed by Alessandro Forin (af@cs.cmu.edu) at
6    CMU.  Major work by Per Bothner, John Gilmore and Ian Lance Taylor
7    at Cygnus Support.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 "filenames.h"
47 #include "objfiles.h"
48 #include "gdb_obstack.h"
49 #include "buildsym.h"
50 #include "stabsread.h"
51 #include "complaints.h"
52 #include "demangle.h"
53 #include "gdb-demangle.h"
54 #include "block.h"
55 #include "dictionary.h"
56 #include "mdebugread.h"
57 #include <sys/stat.h>
58 #include "psympriv.h"
59 #include "source.h"
60
61 #include "bfd.h"
62
63 #include "coff/ecoff.h"         /* COFF-like aspects of ecoff files.  */
64
65 #include "libaout.h"            /* Private BFD a.out information.  */
66 #include "aout/aout64.h"
67 #include "aout/stab_gnu.h"      /* STABS information.  */
68
69 #include "expression.h"
70
71 extern void _initialize_mdebugread (void);
72
73 /* Provide a way to test if we have both ECOFF and ELF symbol tables.
74    We use this define in order to know whether we should override a 
75    symbol's ECOFF section with its ELF section.  This is necessary in 
76    case the symbol's ELF section could not be represented in ECOFF.  */
77 #define ECOFF_IN_ELF(bfd) (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
78                            && bfd_get_section_by_name (bfd, ".mdebug") != NULL)
79
80 /* The objfile we are currently reading.  */
81
82 static struct objfile *mdebugread_objfile;
83
84 \f
85
86 /* We put a pointer to this structure in the read_symtab_private field
87    of the psymtab.  */
88
89 struct symloc
90   {
91     /* Index of the FDR that this psymtab represents.  */
92     int fdr_idx;
93     /* The BFD that the psymtab was created from.  */
94     bfd *cur_bfd;
95     const struct ecoff_debug_swap *debug_swap;
96     struct ecoff_debug_info *debug_info;
97     struct mdebug_pending **pending_list;
98     /* Pointer to external symbols for this file.  */
99     EXTR *extern_tab;
100     /* Size of extern_tab.  */
101     int extern_count;
102     enum language pst_language;
103   };
104
105 #define PST_PRIVATE(p) ((struct symloc *)(p)->read_symtab_private)
106 #define FDR_IDX(p) (PST_PRIVATE(p)->fdr_idx)
107 #define CUR_BFD(p) (PST_PRIVATE(p)->cur_bfd)
108 #define DEBUG_SWAP(p) (PST_PRIVATE(p)->debug_swap)
109 #define DEBUG_INFO(p) (PST_PRIVATE(p)->debug_info)
110 #define PENDING_LIST(p) (PST_PRIVATE(p)->pending_list)
111
112 #define SC_IS_TEXT(sc) ((sc) == scText \
113                    || (sc) == scRConst \
114                    || (sc) == scInit \
115                    || (sc) == scFini)
116 #define SC_IS_DATA(sc) ((sc) == scData \
117                    || (sc) == scSData \
118                    || (sc) == scRData \
119                    || (sc) == scPData \
120                    || (sc) == scXData)
121 #define SC_IS_COMMON(sc) ((sc) == scCommon || (sc) == scSCommon)
122 #define SC_IS_BSS(sc) ((sc) == scBss)
123 #define SC_IS_SBSS(sc) ((sc) == scSBss)
124 #define SC_IS_UNDEF(sc) ((sc) == scUndefined || (sc) == scSUndefined)
125 \f
126 /* Various complaints about symbol reading that don't abort the process.  */
127 static void
128 index_complaint (const char *arg1)
129 {
130   complaint (&symfile_complaints, _("bad aux index at symbol %s"), arg1);
131 }
132
133 static void
134 unknown_ext_complaint (const char *arg1)
135 {
136   complaint (&symfile_complaints, _("unknown external symbol %s"), arg1);
137 }
138
139 static void
140 basic_type_complaint (int arg1, const char *arg2)
141 {
142   complaint (&symfile_complaints, _("cannot map ECOFF basic type 0x%x for %s"),
143              arg1, arg2);
144 }
145
146 static void
147 bad_tag_guess_complaint (const char *arg1)
148 {
149   complaint (&symfile_complaints,
150              _("guessed tag type of %s incorrectly"), arg1);
151 }
152
153 static void
154 bad_rfd_entry_complaint (const char *arg1, int arg2, int arg3)
155 {
156   complaint (&symfile_complaints, _("bad rfd entry for %s: file %d, index %d"),
157              arg1, arg2, arg3);
158 }
159
160 static void
161 unexpected_type_code_complaint (const char *arg1)
162 {
163   complaint (&symfile_complaints, _("unexpected type code for %s"), arg1);
164 }
165
166 /* Macros and extra defs.  */
167
168 /* Puns: hard to find whether -g was used and how.  */
169
170 #define MIN_GLEVEL GLEVEL_0
171 #define compare_glevel(a,b)                                     \
172         (((a) == GLEVEL_3) ? ((b) < GLEVEL_3) :                 \
173          ((b) == GLEVEL_3) ? -1 : (int)((b) - (a)))
174 \f
175 /* Things that really are local to this module.  */
176
177 /* Remember what we deduced to be the source language of this psymtab.  */
178
179 static enum language psymtab_language = language_unknown;
180
181 /* Current BFD.  */
182
183 static bfd *cur_bfd;
184
185 /* How to parse debugging information for CUR_BFD.  */
186
187 static const struct ecoff_debug_swap *debug_swap;
188
189 /* Pointers to debugging information for CUR_BFD.  */
190
191 static struct ecoff_debug_info *debug_info;
192
193 /* Pointer to current file decriptor record, and its index.  */
194
195 static FDR *cur_fdr;
196 static int cur_fd;
197
198 /* Index of current symbol.  */
199
200 static int cur_sdx;
201
202 /* Note how much "debuggable" this image is.  We would like
203    to see at least one FDR with full symbols.  */
204
205 static int max_gdbinfo;
206 static int max_glevel;
207
208 /* When examining .o files, report on undefined symbols.  */
209
210 static int n_undef_symbols, n_undef_labels, n_undef_vars, n_undef_procs;
211
212 /* Pseudo symbol to use when putting stabs into the symbol table.  */
213
214 static char stabs_symbol[] = STABS_SYMBOL;
215
216 /* Nonzero if we have seen ecoff debugging info for a file.  */
217
218 static int found_ecoff_debugging_info;
219
220 /* Forward declarations.  */
221
222 static int upgrade_type (int, struct type **, int, union aux_ext *,
223                          int, char *);
224
225 static void parse_partial_symbols (struct objfile *);
226
227 static int has_opaque_xref (FDR *, SYMR *);
228
229 static int cross_ref (int, union aux_ext *, struct type **, enum type_code,
230                       char **, int, char *);
231
232 static struct symbol *new_symbol (char *);
233
234 static struct type *new_type (char *);
235
236 enum block_type { FUNCTION_BLOCK, NON_FUNCTION_BLOCK };
237
238 static struct block *new_block (enum block_type);
239
240 static struct symtab *new_symtab (const char *, int, struct objfile *);
241
242 static struct linetable *new_linetable (int);
243
244 static struct blockvector *new_bvect (int);
245
246 static struct type *parse_type (int, union aux_ext *, unsigned int, int *,
247                                 int, char *);
248
249 static struct symbol *mylookup_symbol (char *, const struct block *,
250                                        domain_enum, enum address_class);
251
252 static void sort_blocks (struct symtab *);
253
254 static struct partial_symtab *new_psymtab (char *, struct objfile *);
255
256 static void psymtab_to_symtab_1 (struct objfile *objfile,
257                                  struct partial_symtab *, const char *);
258
259 static void add_block (struct block *, struct symtab *);
260
261 static void add_symbol (struct symbol *, struct symtab *, struct block *);
262
263 static int add_line (struct linetable *, int, CORE_ADDR, int);
264
265 static struct linetable *shrink_linetable (struct linetable *);
266
267 static void handle_psymbol_enumerators (struct objfile *, FDR *, int,
268                                         CORE_ADDR);
269
270 static char *mdebug_next_symbol_text (struct objfile *);
271 \f
272 /* Exported procedure: Builds a symtab from the partial symtab SELF.
273    Restores the environment in effect when SELF was created, delegates
274    most of the work to an ancillary procedure, and sorts
275    and reorders the symtab list at the end.  SELF is not NULL.  */
276
277 static void
278 mdebug_read_symtab (struct partial_symtab *self, struct objfile *objfile)
279 {
280   if (info_verbose)
281     {
282       printf_filtered (_("Reading in symbols for %s..."), self->filename);
283       gdb_flush (gdb_stdout);
284     }
285
286   next_symbol_text_func = mdebug_next_symbol_text;
287
288   psymtab_to_symtab_1 (objfile, self, self->filename);
289
290   /* Match with global symbols.  This only needs to be done once,
291      after all of the symtabs and dependencies have been read in.  */
292   scan_file_globals (objfile);
293
294   if (info_verbose)
295     printf_filtered (_("done.\n"));
296 }
297 \f
298 /* File-level interface functions.  */
299
300 /* Find a file descriptor given its index RF relative to a file CF.  */
301
302 static FDR *
303 get_rfd (int cf, int rf)
304 {
305   FDR *fdrs;
306   FDR *f;
307   RFDT rfd;
308
309   fdrs = debug_info->fdr;
310   f = fdrs + cf;
311   /* Object files do not have the RFD table, all refs are absolute.  */
312   if (f->rfdBase == 0)
313     return fdrs + rf;
314   (*debug_swap->swap_rfd_in) (cur_bfd,
315                               ((char *) debug_info->external_rfd
316                                + ((f->rfdBase + rf)
317                                   * debug_swap->external_rfd_size)),
318                               &rfd);
319   return fdrs + rfd;
320 }
321
322 /* Return a safer print NAME for a file descriptor.  */
323
324 static char *
325 fdr_name (FDR *f)
326 {
327   if (f->rss == -1)
328     return "<stripped file>";
329   if (f->rss == 0)
330     return "<NFY>";
331   return debug_info->ss + f->issBase + f->rss;
332 }
333
334
335 /* Read in and parse the symtab of the file OBJFILE.  Symbols from
336    different sections are relocated via the SECTION_OFFSETS.  */
337
338 void
339 mdebug_build_psymtabs (struct objfile *objfile,
340                        const struct ecoff_debug_swap *swap,
341                        struct ecoff_debug_info *info)
342 {
343   cur_bfd = objfile->obfd;
344   debug_swap = swap;
345   debug_info = info;
346
347   stabsread_new_init ();
348   buildsym_new_init ();
349   free_header_files ();
350   init_header_files ();
351         
352   /* Make sure all the FDR information is swapped in.  */
353   if (info->fdr == (FDR *) NULL)
354     {
355       char *fdr_src;
356       char *fdr_end;
357       FDR *fdr_ptr;
358
359       info->fdr = (FDR *) obstack_alloc (&objfile->objfile_obstack,
360                                          (info->symbolic_header.ifdMax
361                                           * sizeof (FDR)));
362       fdr_src = info->external_fdr;
363       fdr_end = (fdr_src
364                  + info->symbolic_header.ifdMax * swap->external_fdr_size);
365       fdr_ptr = info->fdr;
366       for (; fdr_src < fdr_end; fdr_src += swap->external_fdr_size, fdr_ptr++)
367         (*swap->swap_fdr_in) (objfile->obfd, fdr_src, fdr_ptr);
368     }
369
370   parse_partial_symbols (objfile);
371
372 #if 0
373   /* Check to make sure file was compiled with -g.  If not, warn the
374      user of this limitation.  */
375   if (compare_glevel (max_glevel, GLEVEL_2) < 0)
376     {
377       if (max_gdbinfo == 0)
378         printf_unfiltered (_("\n%s not compiled with -g, "
379                              "debugging support is limited.\n"),
380                            objfile->name);
381       printf_unfiltered (_("You should compile with -g2 or "
382                            "-g3 for best debugging support.\n"));
383       gdb_flush (gdb_stdout);
384     }
385 #endif
386 }
387 \f
388 /* Local utilities */
389
390 /* Map of FDR indexes to partial symtabs.  */
391
392 struct pst_map
393 {
394   struct partial_symtab *pst;   /* the psymtab proper */
395   long n_globals;               /* exported globals (external symbols) */
396   long globals_offset;          /* cumulative */
397 };
398
399
400 /* Utility stack, used to nest procedures and blocks properly.
401    It is a doubly linked list, to avoid too many alloc/free.
402    Since we might need it quite a few times it is NOT deallocated
403    after use.  */
404
405 static struct parse_stack
406   {
407     struct parse_stack *next, *prev;
408     struct symtab *cur_st;      /* Current symtab.  */
409     struct block *cur_block;    /* Block in it.  */
410
411     /* What are we parsing.  stFile, or stBlock are for files and
412        blocks.  stProc or stStaticProc means we have seen the start of a
413        procedure, but not the start of the block within in.  When we see
414        the start of that block, we change it to stNil, without pushing a
415        new block, i.e. stNil means both a procedure and a block.  */
416
417     int blocktype;
418
419     struct type *cur_type;      /* Type we parse fields for.  */
420     int cur_field;              /* Field number in cur_type.  */
421     CORE_ADDR procadr;          /* Start addres of this procedure.  */
422     int numargs;                /* Its argument count.  */
423   }
424
425  *top_stack;                    /* Top stack ptr */
426
427
428 /* Enter a new lexical context.  */
429
430 static void
431 push_parse_stack (void)
432 {
433   struct parse_stack *new;
434
435   /* Reuse frames if possible.  */
436   if (top_stack && top_stack->prev)
437     new = top_stack->prev;
438   else
439     new = (struct parse_stack *) xzalloc (sizeof (struct parse_stack));
440   /* Initialize new frame with previous content.  */
441   if (top_stack)
442     {
443       struct parse_stack *prev = new->prev;
444
445       *new = *top_stack;
446       top_stack->prev = new;
447       new->prev = prev;
448       new->next = top_stack;
449     }
450   top_stack = new;
451 }
452
453 /* Exit a lexical context.  */
454
455 static void
456 pop_parse_stack (void)
457 {
458   if (!top_stack)
459     return;
460   if (top_stack->next)
461     top_stack = top_stack->next;
462 }
463
464
465 /* Cross-references might be to things we haven't looked at
466    yet, e.g. type references.  To avoid too many type
467    duplications we keep a quick fixup table, an array
468    of lists of references indexed by file descriptor.  */
469
470 struct mdebug_pending
471 {
472   struct mdebug_pending *next;  /* link */
473   char *s;                      /* the unswapped symbol */
474   struct type *t;               /* its partial type descriptor */
475 };
476
477
478 /* The pending information is kept for an entire object file.  We
479    allocate the pending information table when we create the partial
480    symbols, and we store a pointer to the single table in each
481    psymtab.  */
482
483 static struct mdebug_pending **pending_list;
484
485 /* Check whether we already saw symbol SH in file FH.  */
486
487 static struct mdebug_pending *
488 is_pending_symbol (FDR *fh, char *sh)
489 {
490   int f_idx = fh - debug_info->fdr;
491   struct mdebug_pending *p;
492
493   /* Linear search is ok, list is typically no more than 10 deep.  */
494   for (p = pending_list[f_idx]; p; p = p->next)
495     if (p->s == sh)
496       break;
497   return p;
498 }
499
500 /* Add a new symbol SH of type T.  */
501
502 static void
503 add_pending (FDR *fh, char *sh, struct type *t)
504 {
505   int f_idx = fh - debug_info->fdr;
506   struct mdebug_pending *p = is_pending_symbol (fh, sh);
507
508   /* Make sure we do not make duplicates.  */
509   if (!p)
510     {
511       p = ((struct mdebug_pending *)
512            obstack_alloc (&mdebugread_objfile->objfile_obstack,
513                           sizeof (struct mdebug_pending)));
514       p->s = sh;
515       p->t = t;
516       p->next = pending_list[f_idx];
517       pending_list[f_idx] = p;
518     }
519 }
520 \f
521
522 /* Parsing Routines proper.  */
523
524 /* Parse a single symbol.  Mostly just make up a GDB symbol for it.
525    For blocks, procedures and types we open a new lexical context.
526    This is basically just a big switch on the symbol's type.  Argument
527    AX is the base pointer of aux symbols for this file (fh->iauxBase).
528    EXT_SH points to the unswapped symbol, which is needed for struct,
529    union, etc., types; it is NULL for an EXTR.  BIGEND says whether
530    aux symbols are big-endian or little-endian.  Return count of
531    SYMR's handled (normally one).  */
532
533 static int
534 mdebug_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
535 {
536   return gdbarch_ecoff_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
537 }
538
539 static const struct symbol_register_ops mdebug_register_funcs = {
540   mdebug_reg_to_regnum
541 };
542
543 /* The "aclass" indices for computed symbols.  */
544
545 static int mdebug_register_index;
546 static int mdebug_regparm_index;
547
548 static int
549 parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
550               struct section_offsets *section_offsets, struct objfile *objfile)
551 {
552   struct gdbarch *gdbarch = get_objfile_arch (objfile);
553   const bfd_size_type external_sym_size = debug_swap->external_sym_size;
554   void (*const swap_sym_in) (bfd *, void *, SYMR *) = debug_swap->swap_sym_in;
555   char *name;
556   struct symbol *s;
557   struct block *b;
558   struct mdebug_pending *pend;
559   struct type *t;
560   struct field *f;
561   int count = 1;
562   enum address_class class;
563   TIR tir;
564   long svalue = sh->value;
565   int bitsize;
566
567   if (ext_sh == (char *) NULL)
568     name = debug_info->ssext + sh->iss;
569   else
570     name = debug_info->ss + cur_fdr->issBase + sh->iss;
571
572   switch (sh->sc)
573     {
574     case scText:
575     case scRConst:
576       /* Do not relocate relative values.
577          The value of a stEnd symbol is the displacement from the
578          corresponding start symbol value.
579          The value of a stBlock symbol is the displacement from the
580          procedure address.  */
581       if (sh->st != stEnd && sh->st != stBlock)
582         sh->value += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
583       break;
584     case scData:
585     case scSData:
586     case scRData:
587     case scPData:
588     case scXData:
589       sh->value += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
590       break;
591     case scBss:
592     case scSBss:
593       sh->value += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
594       break;
595     }
596
597   switch (sh->st)
598     {
599     case stNil:
600       break;
601
602     case stGlobal:              /* External symbol, goes into global block.  */
603       class = LOC_STATIC;
604       b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (top_stack->cur_st),
605                              GLOBAL_BLOCK);
606       s = new_symbol (name);
607       SYMBOL_VALUE_ADDRESS (s) = (CORE_ADDR) sh->value;
608       goto data;
609
610     case stStatic:              /* Static data, goes into current block.  */
611       class = LOC_STATIC;
612       b = top_stack->cur_block;
613       s = new_symbol (name);
614       if (SC_IS_COMMON (sh->sc))
615         {
616           /* It is a FORTRAN common block.  At least for SGI Fortran the
617              address is not in the symbol; we need to fix it later in
618              scan_file_globals.  */
619           int bucket = hashname (SYMBOL_LINKAGE_NAME (s));
620           SYMBOL_VALUE_CHAIN (s) = global_sym_chain[bucket];
621           global_sym_chain[bucket] = s;
622         }
623       else
624         SYMBOL_VALUE_ADDRESS (s) = (CORE_ADDR) sh->value;
625       goto data;
626
627     case stLocal:               /* Local variable, goes into current block.  */
628       b = top_stack->cur_block;
629       s = new_symbol (name);
630       SYMBOL_VALUE (s) = svalue;
631       if (sh->sc == scRegister)
632         class = mdebug_register_index;
633       else
634         class = LOC_LOCAL;
635
636     data:                       /* Common code for symbols describing data.  */
637       SYMBOL_DOMAIN (s) = VAR_DOMAIN;
638       SYMBOL_ACLASS_INDEX (s) = class;
639       add_symbol (s, top_stack->cur_st, b);
640
641       /* Type could be missing if file is compiled without debugging info.  */
642       if (SC_IS_UNDEF (sh->sc)
643           || sh->sc == scNil || sh->index == indexNil)
644         SYMBOL_TYPE (s) = objfile_type (objfile)->nodebug_data_symbol;
645       else
646         SYMBOL_TYPE (s) = parse_type (cur_fd, ax, sh->index, 0, bigend, name);
647       /* Value of a data symbol is its memory address.  */
648       break;
649
650     case stParam:               /* Arg to procedure, goes into current
651                                    block.  */
652       max_gdbinfo++;
653       found_ecoff_debugging_info = 1;
654       top_stack->numargs++;
655
656       /* Special GNU C++ name.  */
657       if (is_cplus_marker (name[0]) && name[1] == 't' && name[2] == 0)
658         name = "this";          /* FIXME, not alloc'd in obstack.  */
659       s = new_symbol (name);
660
661       SYMBOL_DOMAIN (s) = VAR_DOMAIN;
662       SYMBOL_IS_ARGUMENT (s) = 1;
663       switch (sh->sc)
664         {
665         case scRegister:
666           /* Pass by value in register.  */
667           SYMBOL_ACLASS_INDEX (s) = mdebug_register_index;
668           break;
669         case scVar:
670           /* Pass by reference on stack.  */
671           SYMBOL_ACLASS_INDEX (s) = LOC_REF_ARG;
672           break;
673         case scVarRegister:
674           /* Pass by reference in register.  */
675           SYMBOL_ACLASS_INDEX (s) = mdebug_regparm_index;
676           break;
677         default:
678           /* Pass by value on stack.  */
679           SYMBOL_ACLASS_INDEX (s) = LOC_ARG;
680           break;
681         }
682       SYMBOL_VALUE (s) = svalue;
683       SYMBOL_TYPE (s) = parse_type (cur_fd, ax, sh->index, 0, bigend, name);
684       add_symbol (s, top_stack->cur_st, top_stack->cur_block);
685       break;
686
687     case stLabel:               /* label, goes into current block.  */
688       s = new_symbol (name);
689       SYMBOL_DOMAIN (s) = VAR_DOMAIN;   /* So that it can be used */
690       SYMBOL_ACLASS_INDEX (s) = LOC_LABEL;      /* but not misused.  */
691       SYMBOL_VALUE_ADDRESS (s) = (CORE_ADDR) sh->value;
692       SYMBOL_TYPE (s) = objfile_type (objfile)->builtin_int;
693       add_symbol (s, top_stack->cur_st, top_stack->cur_block);
694       break;
695
696     case stProc:        /* Procedure, usually goes into global block.  */
697     case stStaticProc:  /* Static procedure, goes into current block.  */
698       /* For stProc symbol records, we need to check the storage class
699          as well, as only (stProc, scText) entries represent "real"
700          procedures - See the Compaq document titled "Object File /
701          Symbol Table Format Specification" for more information.
702          If the storage class is not scText, we discard the whole block
703          of symbol records for this stProc.  */
704       if (sh->st == stProc && sh->sc != scText)
705         {
706           char *ext_tsym = ext_sh;
707           int keep_counting = 1;
708           SYMR tsym;
709
710           while (keep_counting)
711             {
712               ext_tsym += external_sym_size;
713               (*swap_sym_in) (cur_bfd, ext_tsym, &tsym);
714               count++;
715               switch (tsym.st)
716                 {
717                   case stParam:
718                     break;
719                   case stEnd:
720                     keep_counting = 0;
721                     break;
722                   default:
723                     complaint (&symfile_complaints,
724                                _("unknown symbol type 0x%x"), sh->st);
725                     break;
726                 }
727             }
728           break;
729         }
730       s = new_symbol (name);
731       SYMBOL_DOMAIN (s) = VAR_DOMAIN;
732       SYMBOL_ACLASS_INDEX (s) = LOC_BLOCK;
733       /* Type of the return value.  */
734       if (SC_IS_UNDEF (sh->sc) || sh->sc == scNil)
735         t = objfile_type (objfile)->builtin_int;
736       else
737         {
738           t = parse_type (cur_fd, ax, sh->index + 1, 0, bigend, name);
739           if (strcmp (name, "malloc") == 0
740               && TYPE_CODE (t) == TYPE_CODE_VOID)
741             {
742               /* I don't know why, but, at least under Alpha GNU/Linux,
743                  when linking against a malloc without debugging
744                  symbols, its read as a function returning void---this
745                  is bad because it means we cannot call functions with
746                  string arguments interactively; i.e., "call
747                  printf("howdy\n")" would fail with the error message
748                  "program has no memory available".  To avoid this, we
749                  patch up the type and make it void*
750                  instead. (davidm@azstarnet.com).  */
751               t = make_pointer_type (t, NULL);
752             }
753         }
754       b = top_stack->cur_block;
755       if (sh->st == stProc)
756         {
757           const struct blockvector *bv
758             = SYMTAB_BLOCKVECTOR (top_stack->cur_st);
759
760           /* The next test should normally be true, but provides a
761              hook for nested functions (which we don't want to make
762              global).  */
763           if (b == BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))
764             b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
765           /* Irix 5 sometimes has duplicate names for the same
766              function.  We want to add such names up at the global
767              level, not as a nested function.  */
768           else if (sh->value == top_stack->procadr)
769             b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
770         }
771       add_symbol (s, top_stack->cur_st, b);
772
773       /* Make a type for the procedure itself.  */
774       SYMBOL_TYPE (s) = lookup_function_type (t);
775
776       /* All functions in C++ have prototypes.  For C we don't have enough
777          information in the debug info.  */
778       if (SYMBOL_LANGUAGE (s) == language_cplus)
779         TYPE_PROTOTYPED (SYMBOL_TYPE (s)) = 1;
780
781       /* Create and enter a new lexical context.  */
782       b = new_block (FUNCTION_BLOCK);
783       SYMBOL_BLOCK_VALUE (s) = b;
784       BLOCK_FUNCTION (b) = s;
785       BLOCK_START (b) = BLOCK_END (b) = sh->value;
786       BLOCK_SUPERBLOCK (b) = top_stack->cur_block;
787       add_block (b, top_stack->cur_st);
788
789       /* Not if we only have partial info.  */
790       if (SC_IS_UNDEF (sh->sc) || sh->sc == scNil)
791         break;
792
793       push_parse_stack ();
794       top_stack->cur_block = b;
795       top_stack->blocktype = sh->st;
796       top_stack->cur_type = SYMBOL_TYPE (s);
797       top_stack->cur_field = -1;
798       top_stack->procadr = sh->value;
799       top_stack->numargs = 0;
800       break;
801
802       /* Beginning of code for structure, union, and enum definitions.
803          They all share a common set of local variables, defined here.  */
804       {
805         enum type_code type_code;
806         char *ext_tsym;
807         int nfields;
808         long max_value;
809         struct field *f;
810
811     case stStruct:              /* Start a block defining a struct type.  */
812         type_code = TYPE_CODE_STRUCT;
813         goto structured_common;
814
815     case stUnion:               /* Start a block defining a union type.  */
816         type_code = TYPE_CODE_UNION;
817         goto structured_common;
818
819     case stEnum:                /* Start a block defining an enum type.  */
820         type_code = TYPE_CODE_ENUM;
821         goto structured_common;
822
823     case stBlock:               /* Either a lexical block, or some type.  */
824         if (sh->sc != scInfo && !SC_IS_COMMON (sh->sc))
825           goto case_stBlock_code;       /* Lexical block */
826
827         type_code = TYPE_CODE_UNDEF;    /* We have a type.  */
828
829         /* Common code for handling struct, union, enum, and/or as-yet-
830            unknown-type blocks of info about structured data.  `type_code'
831            has been set to the proper TYPE_CODE, if we know it.  */
832       structured_common:
833         found_ecoff_debugging_info = 1;
834         push_parse_stack ();
835         top_stack->blocktype = stBlock;
836
837         /* First count the number of fields and the highest value.  */
838         nfields = 0;
839         max_value = 0;
840         for (ext_tsym = ext_sh + external_sym_size;
841              ;
842              ext_tsym += external_sym_size)
843           {
844             SYMR tsym;
845
846             (*swap_sym_in) (cur_bfd, ext_tsym, &tsym);
847
848             switch (tsym.st)
849               {
850               case stEnd:
851                 /* C++ encodes class types as structures where there the
852                    methods are encoded as stProc.  The scope of stProc
853                    symbols also ends with stEnd, thus creating a risk of
854                    taking the wrong stEnd symbol record as the end of
855                    the current struct, which would cause GDB to undercount
856                    the real number of fields in this struct.  To make sure
857                    we really reached the right stEnd symbol record, we
858                    check the associated name, and match it against the
859                    struct name.  Since method names are mangled while
860                    the class name is not, there is no risk of having a
861                    method whose name is identical to the class name
862                    (in particular constructor method names are different
863                    from the class name).  There is therefore no risk that
864                    this check stops the count on the StEnd of a method.
865                    
866                    Also, assume that we're really at the end when tsym.iss
867                    is 0 (issNull).  */
868                 if (tsym.iss == issNull
869                     || strcmp (debug_info->ss + cur_fdr->issBase + tsym.iss,
870                                name) == 0)
871                   goto end_of_fields;
872                 break;
873
874               case stMember:
875                 if (nfields == 0 && type_code == TYPE_CODE_UNDEF)
876                   {
877                     /* If the type of the member is Nil (or Void),
878                        without qualifiers, assume the tag is an
879                        enumeration.
880                        Alpha cc -migrate enums are recognized by a zero
881                        index and a zero symbol value.
882                        DU 4.0 cc enums are recognized by a member type of
883                        btEnum without qualifiers and a zero symbol value.  */
884                     if (tsym.index == indexNil
885                         || (tsym.index == 0 && sh->value == 0))
886                       type_code = TYPE_CODE_ENUM;
887                     else
888                       {
889                         (*debug_swap->swap_tir_in) (bigend,
890                                                     &ax[tsym.index].a_ti,
891                                                     &tir);
892                         if ((tir.bt == btNil || tir.bt == btVoid
893                              || (tir.bt == btEnum && sh->value == 0))
894                             && tir.tq0 == tqNil)
895                           type_code = TYPE_CODE_ENUM;
896                       }
897                   }
898                 nfields++;
899                 if (tsym.value > max_value)
900                   max_value = tsym.value;
901                 break;
902
903               case stBlock:
904               case stUnion:
905               case stEnum:
906               case stStruct:
907                 {
908 #if 0
909                   /* This is a no-op; is it trying to tell us something
910                      we should be checking?  */
911                   if (tsym.sc == scVariant);    /*UNIMPLEMENTED */
912 #endif
913                   if (tsym.index != 0)
914                     {
915                       /* This is something like a struct within a
916                          struct.  Skip over the fields of the inner
917                          struct.  The -1 is because the for loop will
918                          increment ext_tsym.  */
919                       ext_tsym = ((char *) debug_info->external_sym
920                                   + ((cur_fdr->isymBase + tsym.index - 1)
921                                      * external_sym_size));
922                     }
923                 }
924                 break;
925
926               case stTypedef:
927                 /* mips cc puts out a typedef for struct x if it is not yet
928                    defined when it encounters
929                    struct y { struct x *xp; };
930                    Just ignore it.  */
931                 break;
932
933               case stIndirect:
934                 /* Irix5 cc puts out a stIndirect for struct x if it is not
935                    yet defined when it encounters
936                    struct y { struct x *xp; };
937                    Just ignore it.  */
938                 break;
939
940               default:
941                 complaint (&symfile_complaints,
942                            _("declaration block contains "
943                              "unhandled symbol type %d"),
944                            tsym.st);
945               }
946           }
947       end_of_fields:
948
949         /* In an stBlock, there is no way to distinguish structs,
950            unions, and enums at this point.  This is a bug in the
951            original design (that has been fixed with the recent
952            addition of the stStruct, stUnion, and stEnum symbol
953            types.)  The way you can tell is if/when you see a variable
954            or field of that type.  In that case the variable's type
955            (in the AUX table) says if the type is struct, union, or
956            enum, and points back to the stBlock here.  So you can
957            patch the tag kind up later - but only if there actually is
958            a variable or field of that type.
959
960            So until we know for sure, we will guess at this point.
961            The heuristic is:
962            If the first member has index==indexNil or a void type,
963            assume we have an enumeration.
964            Otherwise, if there is more than one member, and all
965            the members have offset 0, assume we have a union.
966            Otherwise, assume we have a struct.
967
968            The heuristic could guess wrong in the case of of an
969            enumeration with no members or a union with one (or zero)
970            members, or when all except the last field of a struct have
971            width zero.  These are uncommon and/or illegal situations,
972            and in any case guessing wrong probably doesn't matter
973            much.
974
975            But if we later do find out we were wrong, we fixup the tag
976            kind.  Members of an enumeration must be handled
977            differently from struct/union fields, and that is harder to
978            patch up, but luckily we shouldn't need to.  (If there are
979            any enumeration members, we can tell for sure it's an enum
980            here.)  */
981
982         if (type_code == TYPE_CODE_UNDEF)
983           {
984             if (nfields > 1 && max_value == 0)
985               type_code = TYPE_CODE_UNION;
986             else
987               type_code = TYPE_CODE_STRUCT;
988           }
989
990         /* Create a new type or use the pending type.  */
991         pend = is_pending_symbol (cur_fdr, ext_sh);
992         if (pend == (struct mdebug_pending *) NULL)
993           {
994             t = new_type (NULL);
995             add_pending (cur_fdr, ext_sh, t);
996           }
997         else
998           t = pend->t;
999
1000         /* Do not set the tag name if it is a compiler generated tag name
1001            (.Fxx or .xxfake or empty) for unnamed struct/union/enums.
1002            Alpha cc puts out an sh->iss of zero for those.  */
1003         if (sh->iss == 0 || name[0] == '.' || name[0] == '\0')
1004           TYPE_TAG_NAME (t) = NULL;
1005         else
1006           TYPE_TAG_NAME (t) = obconcat (&mdebugread_objfile->objfile_obstack,
1007                                         name, (char *) NULL);
1008
1009         TYPE_CODE (t) = type_code;
1010         TYPE_LENGTH (t) = sh->value;
1011         TYPE_NFIELDS (t) = nfields;
1012         TYPE_FIELDS (t) = f = ((struct field *)
1013                                TYPE_ALLOC (t,
1014                                            nfields * sizeof (struct field)));
1015
1016         if (type_code == TYPE_CODE_ENUM)
1017           {
1018             int unsigned_enum = 1;
1019
1020             /* This is a non-empty enum.  */
1021
1022             /* DEC c89 has the number of enumerators in the sh.value field,
1023                not the type length, so we have to compensate for that
1024                incompatibility quirk.
1025                This might do the wrong thing for an enum with one or two
1026                enumerators and gcc -gcoff -fshort-enums, but these cases
1027                are hopefully rare enough.
1028                Alpha cc -migrate has a sh.value field of zero, we adjust
1029                that too.  */
1030             if (TYPE_LENGTH (t) == TYPE_NFIELDS (t)
1031                 || TYPE_LENGTH (t) == 0)
1032               TYPE_LENGTH (t) = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
1033             for (ext_tsym = ext_sh + external_sym_size;
1034                  ;
1035                  ext_tsym += external_sym_size)
1036               {
1037                 SYMR tsym;
1038                 struct symbol *enum_sym;
1039
1040                 (*swap_sym_in) (cur_bfd, ext_tsym, &tsym);
1041
1042                 if (tsym.st != stMember)
1043                   break;
1044
1045                 SET_FIELD_ENUMVAL (*f, tsym.value);
1046                 FIELD_TYPE (*f) = t;
1047                 FIELD_NAME (*f) = debug_info->ss + cur_fdr->issBase + tsym.iss;
1048                 FIELD_BITSIZE (*f) = 0;
1049
1050                 enum_sym = allocate_symbol (mdebugread_objfile);
1051                 SYMBOL_SET_LINKAGE_NAME
1052                   (enum_sym,
1053                    obstack_copy0 (&mdebugread_objfile->objfile_obstack,
1054                                   f->name, strlen (f->name)));
1055                 SYMBOL_ACLASS_INDEX (enum_sym) = LOC_CONST;
1056                 SYMBOL_TYPE (enum_sym) = t;
1057                 SYMBOL_DOMAIN (enum_sym) = VAR_DOMAIN;
1058                 SYMBOL_VALUE (enum_sym) = tsym.value;
1059                 if (SYMBOL_VALUE (enum_sym) < 0)
1060                   unsigned_enum = 0;
1061                 add_symbol (enum_sym, top_stack->cur_st, top_stack->cur_block);
1062
1063                 /* Skip the stMembers that we've handled.  */
1064                 count++;
1065                 f++;
1066               }
1067             if (unsigned_enum)
1068               TYPE_UNSIGNED (t) = 1;
1069           }
1070         /* Make this the current type.  */
1071         top_stack->cur_type = t;
1072         top_stack->cur_field = 0;
1073
1074         /* Do not create a symbol for alpha cc unnamed structs.  */
1075         if (sh->iss == 0)
1076           break;
1077
1078         /* gcc puts out an empty struct for an opaque struct definitions,
1079            do not create a symbol for it either.  */
1080         if (TYPE_NFIELDS (t) == 0)
1081           {
1082             TYPE_STUB (t) = 1;
1083             break;
1084           }
1085
1086         s = new_symbol (name);
1087         SYMBOL_DOMAIN (s) = STRUCT_DOMAIN;
1088         SYMBOL_ACLASS_INDEX (s) = LOC_TYPEDEF;
1089         SYMBOL_VALUE (s) = 0;
1090         SYMBOL_TYPE (s) = t;
1091         add_symbol (s, top_stack->cur_st, top_stack->cur_block);
1092         break;
1093
1094         /* End of local variables shared by struct, union, enum, and
1095            block (as yet unknown struct/union/enum) processing.  */
1096       }
1097
1098     case_stBlock_code:
1099       found_ecoff_debugging_info = 1;
1100       /* Beginnning of (code) block.  Value of symbol
1101          is the displacement from procedure start.  */
1102       push_parse_stack ();
1103
1104       /* Do not start a new block if this is the outermost block of a
1105          procedure.  This allows the LOC_BLOCK symbol to point to the
1106          block with the local variables, so funcname::var works.  */
1107       if (top_stack->blocktype == stProc
1108           || top_stack->blocktype == stStaticProc)
1109         {
1110           top_stack->blocktype = stNil;
1111           break;
1112         }
1113
1114       top_stack->blocktype = stBlock;
1115       b = new_block (NON_FUNCTION_BLOCK);
1116       BLOCK_START (b) = sh->value + top_stack->procadr;
1117       BLOCK_SUPERBLOCK (b) = top_stack->cur_block;
1118       top_stack->cur_block = b;
1119       add_block (b, top_stack->cur_st);
1120       break;
1121
1122     case stEnd:         /* end (of anything) */
1123       if (sh->sc == scInfo || SC_IS_COMMON (sh->sc))
1124         {
1125           /* Finished with type */
1126           top_stack->cur_type = 0;
1127         }
1128       else if (sh->sc == scText &&
1129                (top_stack->blocktype == stProc ||
1130                 top_stack->blocktype == stStaticProc))
1131         {
1132           /* Finished with procedure */
1133           const struct blockvector *bv
1134             = SYMTAB_BLOCKVECTOR (top_stack->cur_st);
1135           struct mdebug_extra_func_info *e;
1136           struct block *b = top_stack->cur_block;
1137           struct type *ftype = top_stack->cur_type;
1138           int i;
1139
1140           BLOCK_END (top_stack->cur_block) += sh->value;        /* size */
1141
1142           /* Make up special symbol to contain procedure specific info.  */
1143           s = new_symbol (MDEBUG_EFI_SYMBOL_NAME);
1144           SYMBOL_DOMAIN (s) = LABEL_DOMAIN;
1145           SYMBOL_ACLASS_INDEX (s) = LOC_CONST;
1146           SYMBOL_TYPE (s) = objfile_type (mdebugread_objfile)->builtin_void;
1147           e = ((struct mdebug_extra_func_info *)
1148                obstack_alloc (&mdebugread_objfile->objfile_obstack,
1149                               sizeof (struct mdebug_extra_func_info)));
1150           memset (e, 0, sizeof (struct mdebug_extra_func_info));
1151           SYMBOL_VALUE_BYTES (s) = (gdb_byte *) e;
1152           e->numargs = top_stack->numargs;
1153           e->pdr.framereg = -1;
1154           add_symbol (s, top_stack->cur_st, top_stack->cur_block);
1155
1156           /* f77 emits proc-level with address bounds==[0,0],
1157              So look for such child blocks, and patch them.  */
1158           for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); i++)
1159             {
1160               struct block *b_bad = BLOCKVECTOR_BLOCK (bv, i);
1161
1162               if (BLOCK_SUPERBLOCK (b_bad) == b
1163                   && BLOCK_START (b_bad) == top_stack->procadr
1164                   && BLOCK_END (b_bad) == top_stack->procadr)
1165                 {
1166                   BLOCK_START (b_bad) = BLOCK_START (b);
1167                   BLOCK_END (b_bad) = BLOCK_END (b);
1168                 }
1169             }
1170
1171           if (TYPE_NFIELDS (ftype) <= 0)
1172             {
1173               /* No parameter type information is recorded with the function's
1174                  type.  Set that from the type of the parameter symbols.  */
1175               int nparams = top_stack->numargs;
1176               int iparams;
1177               struct symbol *sym;
1178
1179               if (nparams > 0)
1180                 {
1181                   struct block_iterator iter;
1182
1183                   TYPE_NFIELDS (ftype) = nparams;
1184                   TYPE_FIELDS (ftype) = (struct field *)
1185                     TYPE_ALLOC (ftype, nparams * sizeof (struct field));
1186
1187                   iparams = 0;
1188                   ALL_BLOCK_SYMBOLS (b, iter, sym)
1189                     {
1190                       if (iparams == nparams)
1191                         break;
1192
1193                       if (SYMBOL_IS_ARGUMENT (sym))
1194                         {
1195                           TYPE_FIELD_TYPE (ftype, iparams) = SYMBOL_TYPE (sym);
1196                           TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
1197                           iparams++;
1198                         }
1199                     }
1200                 }
1201             }
1202         }
1203       else if (sh->sc == scText && top_stack->blocktype == stBlock)
1204         {
1205           /* End of (code) block.  The value of the symbol is the
1206              displacement from the procedure`s start address of the
1207              end of this block.  */
1208           BLOCK_END (top_stack->cur_block) = sh->value + top_stack->procadr;
1209         }
1210       else if (sh->sc == scText && top_stack->blocktype == stNil)
1211         {
1212           /* End of outermost block.  Pop parse stack and ignore.  The
1213              following stEnd of stProc will take care of the block.  */
1214           ;
1215         }
1216       else if (sh->sc == scText && top_stack->blocktype == stFile)
1217         {
1218           /* End of file.  Pop parse stack and ignore.  Higher
1219              level code deals with this.  */
1220           ;
1221         }
1222       else
1223         complaint (&symfile_complaints,
1224                    _("stEnd with storage class %d not handled"), sh->sc);
1225
1226       pop_parse_stack ();       /* Restore previous lexical context.  */
1227       break;
1228
1229     case stMember:              /* member of struct or union */
1230       f = &TYPE_FIELDS (top_stack->cur_type)[top_stack->cur_field++];
1231       FIELD_NAME (*f) = name;
1232       SET_FIELD_BITPOS (*f, sh->value);
1233       bitsize = 0;
1234       FIELD_TYPE (*f) = parse_type (cur_fd, ax, sh->index,
1235                                     &bitsize, bigend, name);
1236       FIELD_BITSIZE (*f) = bitsize;
1237       break;
1238
1239     case stIndirect:            /* forward declaration on Irix5 */
1240       /* Forward declarations from Irix5 cc are handled by cross_ref,
1241          skip them.  */
1242       break;
1243
1244     case stTypedef:             /* type definition */
1245       found_ecoff_debugging_info = 1;
1246
1247       /* Typedefs for forward declarations and opaque structs from alpha cc
1248          are handled by cross_ref, skip them.  */
1249       if (sh->iss == 0)
1250         break;
1251
1252       /* Parse the type or use the pending type.  */
1253       pend = is_pending_symbol (cur_fdr, ext_sh);
1254       if (pend == (struct mdebug_pending *) NULL)
1255         {
1256           t = parse_type (cur_fd, ax, sh->index, (int *) NULL, bigend, name);
1257           add_pending (cur_fdr, ext_sh, t);
1258         }
1259       else
1260         t = pend->t;
1261
1262       /* Mips cc puts out a typedef with the name of the struct for forward
1263          declarations.  These should not go into the symbol table and
1264          TYPE_NAME should not be set for them.
1265          They can't be distinguished from an intentional typedef to
1266          the same name however:
1267          x.h:
1268          struct x { int ix; int jx; };
1269          struct xx;
1270          x.c:
1271          typedef struct x x;
1272          struct xx {int ixx; int jxx; };
1273          generates a cross referencing stTypedef for x and xx.
1274          The user visible effect of this is that the type of a pointer
1275          to struct foo sometimes is given as `foo *' instead of `struct foo *'.
1276          The problem is fixed with alpha cc and Irix5 cc.  */
1277
1278       /* However if the typedef cross references to an opaque aggregate, it
1279          is safe to omit it from the symbol table.  */
1280
1281       if (has_opaque_xref (cur_fdr, sh))
1282         break;
1283       s = new_symbol (name);
1284       SYMBOL_DOMAIN (s) = VAR_DOMAIN;
1285       SYMBOL_ACLASS_INDEX (s) = LOC_TYPEDEF;
1286       SYMBOL_BLOCK_VALUE (s) = top_stack->cur_block;
1287       SYMBOL_TYPE (s) = t;
1288       add_symbol (s, top_stack->cur_st, top_stack->cur_block);
1289
1290       /* Incomplete definitions of structs should not get a name.  */
1291       if (TYPE_NAME (SYMBOL_TYPE (s)) == NULL
1292           && (TYPE_NFIELDS (SYMBOL_TYPE (s)) != 0
1293               || (TYPE_CODE (SYMBOL_TYPE (s)) != TYPE_CODE_STRUCT
1294                   && TYPE_CODE (SYMBOL_TYPE (s)) != TYPE_CODE_UNION)))
1295         {
1296           if (TYPE_CODE (SYMBOL_TYPE (s)) == TYPE_CODE_PTR
1297               || TYPE_CODE (SYMBOL_TYPE (s)) == TYPE_CODE_FUNC)
1298             {
1299               /* If we are giving a name to a type such as "pointer to
1300                  foo" or "function returning foo", we better not set
1301                  the TYPE_NAME.  If the program contains "typedef char
1302                  *caddr_t;", we don't want all variables of type char
1303                  * to print as caddr_t.  This is not just a
1304                  consequence of GDB's type management; CC and GCC (at
1305                  least through version 2.4) both output variables of
1306                  either type char * or caddr_t with the type
1307                  refering to the stTypedef symbol for caddr_t.  If a future
1308                  compiler cleans this up it GDB is not ready for it
1309                  yet, but if it becomes ready we somehow need to
1310                  disable this check (without breaking the PCC/GCC2.4
1311                  case).
1312
1313                  Sigh.
1314
1315                  Fortunately, this check seems not to be necessary
1316                  for anything except pointers or functions.  */
1317             }
1318           else
1319             TYPE_NAME (SYMBOL_TYPE (s)) = SYMBOL_LINKAGE_NAME (s);
1320         }
1321       break;
1322
1323     case stFile:                /* file name */
1324       push_parse_stack ();
1325       top_stack->blocktype = sh->st;
1326       break;
1327
1328       /* I`ve never seen these for C */
1329     case stRegReloc:
1330       break;                    /* register relocation */
1331     case stForward:
1332       break;                    /* forwarding address */
1333     case stConstant:
1334       break;                    /* constant */
1335     default:
1336       complaint (&symfile_complaints, _("unknown symbol type 0x%x"), sh->st);
1337       break;
1338     }
1339
1340   return count;
1341 }
1342
1343 /* Basic types.  */
1344
1345 static const struct objfile_data *basic_type_data;
1346
1347 static struct type *
1348 basic_type (int bt, struct objfile *objfile)
1349 {
1350   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1351   struct type **map_bt = objfile_data (objfile, basic_type_data);
1352   struct type *tp;
1353
1354   if (bt >= btMax)
1355     return NULL;
1356
1357   if (!map_bt)
1358     {
1359       map_bt = OBSTACK_CALLOC (&objfile->objfile_obstack,
1360                                btMax, struct type *);
1361       set_objfile_data (objfile, basic_type_data, map_bt);
1362     }
1363
1364   if (map_bt[bt])
1365     return map_bt[bt];
1366
1367   switch (bt)
1368     {
1369     case btNil:
1370       tp = objfile_type (objfile)->builtin_void;
1371       break;
1372
1373     case btAdr:
1374       tp = init_type (TYPE_CODE_PTR, 4, TYPE_FLAG_UNSIGNED,
1375                       "adr_32", objfile);
1376       TYPE_TARGET_TYPE (tp) = objfile_type (objfile)->builtin_void;
1377       break;
1378
1379     case btChar:
1380       tp = init_type (TYPE_CODE_INT, 1, 0,
1381                       "char", objfile);
1382       break;
1383
1384     case btUChar:
1385       tp = init_type (TYPE_CODE_INT, 1, TYPE_FLAG_UNSIGNED,
1386                       "unsigned char", objfile);
1387       break;
1388
1389     case btShort:
1390       tp = init_type (TYPE_CODE_INT, 2, 0,
1391                       "short", objfile);
1392       break;
1393
1394     case btUShort:
1395       tp = init_type (TYPE_CODE_INT, 2, TYPE_FLAG_UNSIGNED,
1396                       "unsigned short", objfile);
1397       break;
1398
1399     case btInt:
1400       tp = init_type (TYPE_CODE_INT, 4, 0,
1401                       "int", objfile);
1402       break;
1403
1404    case btUInt:
1405       tp = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
1406                       "unsigned int", objfile);
1407       break;
1408
1409     case btLong:
1410       tp = init_type (TYPE_CODE_INT, 4, 0,
1411                       "long", objfile);
1412       break;
1413
1414     case btULong:
1415       tp = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
1416                       "unsigned long", objfile);
1417       break;
1418
1419     case btFloat:
1420       tp = init_type (TYPE_CODE_FLT,
1421                       gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT, 0,
1422                       "float", objfile);
1423       break;
1424
1425     case btDouble:
1426       tp = init_type (TYPE_CODE_FLT,
1427                       gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT, 0,
1428                       "double", objfile);
1429       break;
1430
1431     case btComplex:
1432       tp = init_type (TYPE_CODE_COMPLEX,
1433                       2 * gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT, 0,
1434                       "complex", objfile);
1435       TYPE_TARGET_TYPE (tp) = basic_type (btFloat, objfile);
1436       break;
1437
1438     case btDComplex:
1439       tp = init_type (TYPE_CODE_COMPLEX,
1440                       2 * gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT, 0,
1441                       "double complex", objfile);
1442       TYPE_TARGET_TYPE (tp) = basic_type (btDouble, objfile);
1443       break;
1444
1445     case btFixedDec:
1446       /* We use TYPE_CODE_INT to print these as integers.  Does this do any
1447          good?  Would we be better off with TYPE_CODE_ERROR?  Should
1448          TYPE_CODE_ERROR print things in hex if it knows the size?  */
1449       tp = init_type (TYPE_CODE_INT,
1450                       gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT, 0,
1451                       "fixed decimal", objfile);
1452       break;
1453
1454     case btFloatDec:
1455       tp = init_type (TYPE_CODE_ERROR,
1456                       gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT, 0,
1457                       "floating decimal", objfile);
1458       break;
1459
1460     case btString:
1461       /* Is a "string" the way btString means it the same as TYPE_CODE_STRING?
1462          FIXME.  */
1463       tp = init_type (TYPE_CODE_STRING, 1, 0,
1464                       "string", objfile);
1465       break;
1466
1467     case btVoid:
1468       tp = objfile_type (objfile)->builtin_void;
1469       break;
1470
1471     case btLong64:
1472       tp = init_type (TYPE_CODE_INT, 8, 0,
1473                       "long", objfile);
1474       break;
1475
1476     case btULong64:
1477       tp = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
1478                       "unsigned long", objfile);
1479       break;
1480
1481     case btLongLong64:
1482       tp = init_type (TYPE_CODE_INT, 8, 0,
1483                       "long long", objfile);
1484       break;
1485
1486     case btULongLong64:
1487       tp = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
1488                       "unsigned long long", objfile);
1489       break;
1490
1491     case btAdr64:
1492       tp = init_type (TYPE_CODE_PTR, 8, TYPE_FLAG_UNSIGNED,
1493                       "adr_64", objfile);
1494       TYPE_TARGET_TYPE (tp) = objfile_type (objfile)->builtin_void;
1495       break;
1496
1497     case btInt64:
1498       tp = init_type (TYPE_CODE_INT, 8, 0,
1499                       "int", objfile);
1500       break;
1501
1502     case btUInt64:
1503       tp = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
1504                       "unsigned int", objfile);
1505       break;
1506
1507     default:
1508       tp = NULL;
1509       break;
1510     }
1511
1512   map_bt[bt] = tp;
1513   return tp;
1514 }
1515
1516 /* Parse the type information provided in the raw AX entries for
1517    the symbol SH.  Return the bitfield size in BS, in case.
1518    We must byte-swap the AX entries before we use them; BIGEND says whether
1519    they are big-endian or little-endian (from fh->fBigendian).  */
1520
1521 static struct type *
1522 parse_type (int fd, union aux_ext *ax, unsigned int aux_index, int *bs,
1523             int bigend, char *sym_name)
1524 {
1525   TIR t[1];
1526   struct type *tp = 0;
1527   enum type_code type_code = TYPE_CODE_UNDEF;
1528
1529   /* Handle undefined types, they have indexNil.  */
1530   if (aux_index == indexNil)
1531     return basic_type (btInt, mdebugread_objfile);
1532
1533   /* Handle corrupt aux indices.  */
1534   if (aux_index >= (debug_info->fdr + fd)->caux)
1535     {
1536       index_complaint (sym_name);
1537       return basic_type (btInt, mdebugread_objfile);
1538     }
1539   ax += aux_index;
1540
1541   /* Use aux as a type information record, map its basic type.  */
1542   (*debug_swap->swap_tir_in) (bigend, &ax->a_ti, t);
1543   tp = basic_type (t->bt, mdebugread_objfile);
1544   if (tp == NULL)
1545     {
1546       /* Cannot use builtin types -- build our own.  */
1547       switch (t->bt)
1548         {
1549         case btStruct:
1550           type_code = TYPE_CODE_STRUCT;
1551           break;
1552         case btUnion:
1553           type_code = TYPE_CODE_UNION;
1554           break;
1555         case btEnum:
1556           type_code = TYPE_CODE_ENUM;
1557           break;
1558         case btRange:
1559           type_code = TYPE_CODE_RANGE;
1560           break;
1561         case btSet:
1562           type_code = TYPE_CODE_SET;
1563           break;
1564         case btIndirect:
1565           /* alpha cc -migrate uses this for typedefs.  The true type will
1566              be obtained by crossreferencing below.  */
1567           type_code = TYPE_CODE_ERROR;
1568           break;
1569         case btTypedef:
1570           /* alpha cc uses this for typedefs.  The true type will be
1571              obtained by crossreferencing below.  */
1572           type_code = TYPE_CODE_ERROR;
1573           break;
1574         default:
1575           basic_type_complaint (t->bt, sym_name);
1576           return basic_type (btInt, mdebugread_objfile);
1577         }
1578     }
1579
1580   /* Move on to next aux.  */
1581   ax++;
1582
1583   if (t->fBitfield)
1584     {
1585       int width = AUX_GET_WIDTH (bigend, ax);
1586
1587       /* Inhibit core dumps if TIR is corrupted.  */
1588       if (bs == (int *) NULL)
1589         {
1590           /* Alpha cc -migrate encodes char and unsigned char types
1591              as short and unsigned short types with a field width of 8.
1592              Enum types also have a field width which we ignore for now.  */
1593           if (t->bt == btShort && width == 8)
1594             tp = basic_type (btChar, mdebugread_objfile);
1595           else if (t->bt == btUShort && width == 8)
1596             tp = basic_type (btUChar, mdebugread_objfile);
1597           else if (t->bt == btEnum)
1598             ;
1599           else
1600             complaint (&symfile_complaints,
1601                        _("can't handle TIR fBitfield for %s"),
1602                        sym_name);
1603         }
1604       else
1605         *bs = width;
1606       ax++;
1607     }
1608
1609   /* A btIndirect entry cross references to an aux entry containing
1610      the type.  */
1611   if (t->bt == btIndirect)
1612     {
1613       RNDXR rn[1];
1614       int rf;
1615       FDR *xref_fh;
1616       int xref_fd;
1617
1618       (*debug_swap->swap_rndx_in) (bigend, &ax->a_rndx, rn);
1619       ax++;
1620       if (rn->rfd == 0xfff)
1621         {
1622           rf = AUX_GET_ISYM (bigend, ax);
1623           ax++;
1624         }
1625       else
1626         rf = rn->rfd;
1627
1628       if (rf == -1)
1629         {
1630           complaint (&symfile_complaints,
1631                      _("unable to cross ref btIndirect for %s"), sym_name);
1632           return basic_type (btInt, mdebugread_objfile);
1633         }
1634       xref_fh = get_rfd (fd, rf);
1635       xref_fd = xref_fh - debug_info->fdr;
1636       tp = parse_type (xref_fd, debug_info->external_aux + xref_fh->iauxBase,
1637                     rn->index, (int *) NULL, xref_fh->fBigendian, sym_name);
1638     }
1639
1640   /* All these types really point to some (common) MIPS type
1641      definition, and only the type-qualifiers fully identify
1642      them.  We'll make the same effort at sharing.  */
1643   if (t->bt == btStruct ||
1644       t->bt == btUnion ||
1645       t->bt == btEnum ||
1646
1647   /* btSet (I think) implies that the name is a tag name, not a typedef
1648      name.  This apparently is a MIPS extension for C sets.  */
1649       t->bt == btSet)
1650     {
1651       char *name;
1652
1653       /* Try to cross reference this type, build new type on failure.  */
1654       ax += cross_ref (fd, ax, &tp, type_code, &name, bigend, sym_name);
1655       if (tp == (struct type *) NULL)
1656         tp = init_type (type_code, 0, 0, (char *) NULL, mdebugread_objfile);
1657
1658       /* DEC c89 produces cross references to qualified aggregate types,
1659          dereference them.  */
1660       while (TYPE_CODE (tp) == TYPE_CODE_PTR
1661              || TYPE_CODE (tp) == TYPE_CODE_ARRAY)
1662         tp = TYPE_TARGET_TYPE (tp);
1663
1664       /* Make sure that TYPE_CODE(tp) has an expected type code.
1665          Any type may be returned from cross_ref if file indirect entries
1666          are corrupted.  */
1667       if (TYPE_CODE (tp) != TYPE_CODE_STRUCT
1668           && TYPE_CODE (tp) != TYPE_CODE_UNION
1669           && TYPE_CODE (tp) != TYPE_CODE_ENUM)
1670         {
1671           unexpected_type_code_complaint (sym_name);
1672         }
1673       else
1674         {
1675           /* Usually, TYPE_CODE(tp) is already type_code.  The main
1676              exception is if we guessed wrong re struct/union/enum.
1677              But for struct vs. union a wrong guess is harmless, so
1678              don't complain().  */
1679           if ((TYPE_CODE (tp) == TYPE_CODE_ENUM
1680                && type_code != TYPE_CODE_ENUM)
1681               || (TYPE_CODE (tp) != TYPE_CODE_ENUM
1682                   && type_code == TYPE_CODE_ENUM))
1683             {
1684               bad_tag_guess_complaint (sym_name);
1685             }
1686
1687           if (TYPE_CODE (tp) != type_code)
1688             {
1689               TYPE_CODE (tp) = type_code;
1690             }
1691
1692           /* Do not set the tag name if it is a compiler generated tag name
1693              (.Fxx or .xxfake or empty) for unnamed struct/union/enums.  */
1694           if (name[0] == '.' || name[0] == '\0')
1695             TYPE_TAG_NAME (tp) = NULL;
1696           else if (TYPE_TAG_NAME (tp) == NULL
1697                    || strcmp (TYPE_TAG_NAME (tp), name) != 0)
1698             TYPE_TAG_NAME (tp)
1699               = obstack_copy0 (&mdebugread_objfile->objfile_obstack,
1700                                name, strlen (name));
1701         }
1702     }
1703
1704   /* All these types really point to some (common) MIPS type
1705      definition, and only the type-qualifiers fully identify
1706      them.  We'll make the same effort at sharing.
1707      FIXME: We are not doing any guessing on range types.  */
1708   if (t->bt == btRange)
1709     {
1710       char *name;
1711
1712       /* Try to cross reference this type, build new type on failure.  */
1713       ax += cross_ref (fd, ax, &tp, type_code, &name, bigend, sym_name);
1714       if (tp == (struct type *) NULL)
1715         tp = init_type (type_code, 0, 0, (char *) NULL, mdebugread_objfile);
1716
1717       /* Make sure that TYPE_CODE(tp) has an expected type code.
1718          Any type may be returned from cross_ref if file indirect entries
1719          are corrupted.  */
1720       if (TYPE_CODE (tp) != TYPE_CODE_RANGE)
1721         {
1722           unexpected_type_code_complaint (sym_name);
1723         }
1724       else
1725         {
1726           /* Usually, TYPE_CODE(tp) is already type_code.  The main
1727              exception is if we guessed wrong re struct/union/enum.  */
1728           if (TYPE_CODE (tp) != type_code)
1729             {
1730               bad_tag_guess_complaint (sym_name);
1731               TYPE_CODE (tp) = type_code;
1732             }
1733           if (TYPE_NAME (tp) == NULL
1734               || strcmp (TYPE_NAME (tp), name) != 0)
1735             TYPE_NAME (tp)
1736               = obstack_copy0 (&mdebugread_objfile->objfile_obstack,
1737                                name, strlen (name));
1738         }
1739     }
1740   if (t->bt == btTypedef)
1741     {
1742       char *name;
1743
1744       /* Try to cross reference this type, it should succeed.  */
1745       ax += cross_ref (fd, ax, &tp, type_code, &name, bigend, sym_name);
1746       if (tp == (struct type *) NULL)
1747         {
1748           complaint (&symfile_complaints,
1749                      _("unable to cross ref btTypedef for %s"), sym_name);
1750           tp = basic_type (btInt, mdebugread_objfile);
1751         }
1752     }
1753
1754   /* Deal with range types.  */
1755   if (t->bt == btRange)
1756     {
1757       TYPE_NFIELDS (tp) = 0;
1758       TYPE_RANGE_DATA (tp) = ((struct range_bounds *)
1759                           TYPE_ZALLOC (tp, sizeof (struct range_bounds)));
1760       TYPE_LOW_BOUND (tp) = AUX_GET_DNLOW (bigend, ax);
1761       ax++;
1762       TYPE_HIGH_BOUND (tp) = AUX_GET_DNHIGH (bigend, ax);
1763       ax++;
1764     }
1765
1766   /* Parse all the type qualifiers now.  If there are more
1767      than 6 the game will continue in the next aux.  */
1768
1769   while (1)
1770     {
1771 #define PARSE_TQ(tq) \
1772       if (t->tq != tqNil) \
1773         ax += upgrade_type(fd, &tp, t->tq, ax, bigend, sym_name); \
1774       else \
1775         break;
1776
1777       PARSE_TQ (tq0);
1778       PARSE_TQ (tq1);
1779       PARSE_TQ (tq2);
1780       PARSE_TQ (tq3);
1781       PARSE_TQ (tq4);
1782       PARSE_TQ (tq5);
1783 #undef  PARSE_TQ
1784
1785       /* mips cc 2.x and gcc never put out continued aux entries.  */
1786       if (!t->continued)
1787         break;
1788
1789       (*debug_swap->swap_tir_in) (bigend, &ax->a_ti, t);
1790       ax++;
1791     }
1792
1793   /* Complain for illegal continuations due to corrupt aux entries.  */
1794   if (t->continued)
1795     complaint (&symfile_complaints,
1796                _("illegal TIR continued for %s"), sym_name);
1797
1798   return tp;
1799 }
1800
1801 /* Make up a complex type from a basic one.  Type is passed by
1802    reference in TPP and side-effected as necessary.  The type
1803    qualifier TQ says how to handle the aux symbols at AX for
1804    the symbol SX we are currently analyzing.  BIGEND says whether
1805    aux symbols are big-endian or little-endian.
1806    Returns the number of aux symbols we parsed.  */
1807
1808 static int
1809 upgrade_type (int fd, struct type **tpp, int tq, union aux_ext *ax, int bigend,
1810               char *sym_name)
1811 {
1812   int off;
1813   struct type *t;
1814
1815   /* Used in array processing.  */
1816   int rf, id;
1817   FDR *fh;
1818   struct type *range;
1819   struct type *indx;
1820   int lower, upper;
1821   RNDXR rndx;
1822
1823   switch (tq)
1824     {
1825     case tqPtr:
1826       t = lookup_pointer_type (*tpp);
1827       *tpp = t;
1828       return 0;
1829
1830     case tqProc:
1831       t = lookup_function_type (*tpp);
1832       *tpp = t;
1833       return 0;
1834
1835     case tqArray:
1836       off = 0;
1837
1838       /* Determine and record the domain type (type of index).  */
1839       (*debug_swap->swap_rndx_in) (bigend, &ax->a_rndx, &rndx);
1840       id = rndx.index;
1841       rf = rndx.rfd;
1842       if (rf == 0xfff)
1843         {
1844           ax++;
1845           rf = AUX_GET_ISYM (bigend, ax);
1846           off++;
1847         }
1848       fh = get_rfd (fd, rf);
1849
1850       indx = parse_type (fh - debug_info->fdr,
1851                          debug_info->external_aux + fh->iauxBase,
1852                          id, (int *) NULL, bigend, sym_name);
1853
1854       /* The bounds type should be an integer type, but might be anything
1855          else due to corrupt aux entries.  */
1856       if (TYPE_CODE (indx) != TYPE_CODE_INT)
1857         {
1858           complaint (&symfile_complaints,
1859                      _("illegal array index type for %s, assuming int"),
1860                      sym_name);
1861           indx = objfile_type (mdebugread_objfile)->builtin_int;
1862         }
1863
1864       /* Get the bounds, and create the array type.  */
1865       ax++;
1866       lower = AUX_GET_DNLOW (bigend, ax);
1867       ax++;
1868       upper = AUX_GET_DNHIGH (bigend, ax);
1869       ax++;
1870       rf = AUX_GET_WIDTH (bigend, ax);  /* bit size of array element */
1871
1872       range = create_static_range_type ((struct type *) NULL, indx,
1873                                         lower, upper);
1874
1875       t = create_array_type ((struct type *) NULL, *tpp, range);
1876
1877       /* We used to fill in the supplied array element bitsize
1878          here if the TYPE_LENGTH of the target type was zero.
1879          This happens for a `pointer to an array of anonymous structs',
1880          but in this case the array element bitsize is also zero,
1881          so nothing is gained.
1882          And we used to check the TYPE_LENGTH of the target type against
1883          the supplied array element bitsize.
1884          gcc causes a mismatch for `pointer to array of object',
1885          since the sdb directives it uses do not have a way of
1886          specifying the bitsize, but it does no harm (the
1887          TYPE_LENGTH should be correct) and we should be able to
1888          ignore the erroneous bitsize from the auxiliary entry safely.
1889          dbx seems to ignore it too.  */
1890
1891       /* TYPE_TARGET_STUB now takes care of the zero TYPE_LENGTH problem.  */
1892       if (TYPE_LENGTH (*tpp) == 0)
1893         TYPE_TARGET_STUB (t) = 1;
1894
1895       *tpp = t;
1896       return 4 + off;
1897
1898     case tqVol:
1899       /* Volatile -- currently ignored */
1900       return 0;
1901
1902     case tqConst:
1903       /* Const -- currently ignored */
1904       return 0;
1905
1906     default:
1907       complaint (&symfile_complaints, _("unknown type qualifier 0x%x"), tq);
1908       return 0;
1909     }
1910 }
1911
1912
1913 /* Parse a procedure descriptor record PR.  Note that the procedure is
1914    parsed _after_ the local symbols, now we just insert the extra
1915    information we need into a MDEBUG_EFI_SYMBOL_NAME symbol that has
1916    already been placed in the procedure's main block.  Note also that
1917    images that have been partially stripped (ld -x) have been deprived
1918    of local symbols, and we have to cope with them here.  FIRST_OFF is
1919    the offset of the first procedure for this FDR; we adjust the
1920    address by this amount, but I don't know why.  SEARCH_SYMTAB is the symtab
1921    to look for the function which contains the MDEBUG_EFI_SYMBOL_NAME symbol
1922    in question, or NULL to use top_stack->cur_block.  */
1923
1924 static void
1925 parse_procedure (PDR *pr, struct symtab *search_symtab,
1926                  struct partial_symtab *pst)
1927 {
1928   struct symbol *s, *i;
1929   const struct block *b;
1930   char *sh_name;
1931
1932   /* Simple rule to find files linked "-x".  */
1933   if (cur_fdr->rss == -1)
1934     {
1935       if (pr->isym == -1)
1936         {
1937           /* Static procedure at address pr->adr.  Sigh.  */
1938           /* FIXME-32x64.  assuming pr->adr fits in long.  */
1939           complaint (&symfile_complaints,
1940                      _("can't handle PDR for static proc at 0x%lx"),
1941                      (unsigned long) pr->adr);
1942           return;
1943         }
1944       else
1945         {
1946           /* external */
1947           EXTR she;
1948
1949           (*debug_swap->swap_ext_in) (cur_bfd,
1950                                       ((char *) debug_info->external_ext
1951                                        + (pr->isym
1952                                           * debug_swap->external_ext_size)),
1953                                       &she);
1954           sh_name = debug_info->ssext + she.asym.iss;
1955         }
1956     }
1957   else
1958     {
1959       /* Full symbols */
1960       SYMR sh;
1961
1962       (*debug_swap->swap_sym_in) (cur_bfd,
1963                                   ((char *) debug_info->external_sym
1964                                    + ((cur_fdr->isymBase + pr->isym)
1965                                       * debug_swap->external_sym_size)),
1966                                   &sh);
1967       sh_name = debug_info->ss + cur_fdr->issBase + sh.iss;
1968     }
1969
1970   if (search_symtab != NULL)
1971     {
1972 #if 0
1973       /* This loses both in the case mentioned (want a static, find a global),
1974          but also if we are looking up a non-mangled name which happens to
1975          match the name of a mangled function.  */
1976       /* We have to save the cur_fdr across the call to lookup_symbol.
1977          If the pdr is for a static function and if a global function with
1978          the same name exists, lookup_symbol will eventually read in the symtab
1979          for the global function and clobber cur_fdr.  */
1980       FDR *save_cur_fdr = cur_fdr;
1981
1982       s = lookup_symbol (sh_name, NULL, VAR_DOMAIN, 0);
1983       cur_fdr = save_cur_fdr;
1984 #else
1985       s = mylookup_symbol
1986         (sh_name,
1987          BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (search_symtab), STATIC_BLOCK),
1988          VAR_DOMAIN,
1989          LOC_BLOCK);
1990 #endif
1991     }
1992   else
1993     s = mylookup_symbol (sh_name, top_stack->cur_block,
1994                          VAR_DOMAIN, LOC_BLOCK);
1995
1996   if (s != 0)
1997     {
1998       b = SYMBOL_BLOCK_VALUE (s);
1999     }
2000   else
2001     {
2002       complaint (&symfile_complaints, _("PDR for %s, but no symbol"), sh_name);
2003 #if 1
2004       return;
2005 #else
2006 /* FIXME -- delete.  We can't do symbol allocation now; it's all done.  */
2007       s = new_symbol (sh_name);
2008       SYMBOL_DOMAIN (s) = VAR_DOMAIN;
2009       SYMBOL_CLASS (s) = LOC_BLOCK;
2010       /* Donno its type, hope int is ok.  */
2011       SYMBOL_TYPE (s)
2012         = lookup_function_type (objfile_type (pst->objfile)->builtin_int);
2013       add_symbol (s, top_stack->cur_st, top_stack->cur_block);
2014       /* Won't have symbols for this one.  */
2015       b = new_block (2);
2016       SYMBOL_BLOCK_VALUE (s) = b;
2017       BLOCK_FUNCTION (b) = s;
2018       BLOCK_START (b) = pr->adr;
2019       /* BOUND used to be the end of procedure's text, but the
2020          argument is no longer passed in.  */
2021       BLOCK_END (b) = bound;
2022       BLOCK_SUPERBLOCK (b) = top_stack->cur_block;
2023       add_block (b, top_stack->cur_st);
2024 #endif
2025     }
2026
2027   i = mylookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, LABEL_DOMAIN, LOC_CONST);
2028
2029   if (i)
2030     {
2031       struct mdebug_extra_func_info *e;
2032       
2033       e = (struct mdebug_extra_func_info *) SYMBOL_VALUE_BYTES (i);
2034       e->pdr = *pr;
2035
2036       /* GDB expects the absolute function start address for the
2037          procedure descriptor in e->pdr.adr.
2038          As the address in the procedure descriptor is usually relative,
2039          we would have to relocate e->pdr.adr with cur_fdr->adr and
2040          ANOFFSET (pst->section_offsets, SECT_OFF_TEXT (pst->objfile)).
2041          Unfortunately cur_fdr->adr and e->pdr.adr are both absolute
2042          in shared libraries on some systems, and on other systems
2043          e->pdr.adr is sometimes offset by a bogus value.
2044          To work around these problems, we replace e->pdr.adr with
2045          the start address of the function.  */
2046       e->pdr.adr = BLOCK_START (b);
2047     }
2048
2049   /* It would be reasonable that functions that have been compiled
2050      without debugging info have a btNil type for their return value,
2051      and functions that are void and are compiled with debugging info
2052      have btVoid.
2053      gcc and DEC f77 put out btNil types for both cases, so btNil is mapped
2054      to TYPE_CODE_VOID in parse_type to get the `compiled with debugging info'
2055      case right.
2056      The glevel field in cur_fdr could be used to determine the presence
2057      of debugging info, but GCC doesn't always pass the -g switch settings
2058      to the assembler and GAS doesn't set the glevel field from the -g switch
2059      settings.
2060      To work around these problems, the return value type of a TYPE_CODE_VOID
2061      function is adjusted accordingly if no debugging info was found in the
2062      compilation unit.  */
2063
2064   if (processing_gcc_compilation == 0
2065       && found_ecoff_debugging_info == 0
2066       && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (s))) == TYPE_CODE_VOID)
2067     SYMBOL_TYPE (s) = objfile_type (mdebugread_objfile)->nodebug_text_symbol;
2068 }
2069
2070 /* Parse the external symbol ES.  Just call parse_symbol() after
2071    making sure we know where the aux are for it.
2072    BIGEND says whether aux entries are big-endian or little-endian.
2073
2074    This routine clobbers top_stack->cur_block and ->cur_st.  */
2075
2076 static void parse_external (EXTR *, int, struct section_offsets *,
2077                             struct objfile *);
2078
2079 static void
2080 parse_external (EXTR *es, int bigend, struct section_offsets *section_offsets,
2081                 struct objfile *objfile)
2082 {
2083   union aux_ext *ax;
2084
2085   if (es->ifd != ifdNil)
2086     {
2087       cur_fd = es->ifd;
2088       cur_fdr = debug_info->fdr + cur_fd;
2089       ax = debug_info->external_aux + cur_fdr->iauxBase;
2090     }
2091   else
2092     {
2093       cur_fdr = debug_info->fdr;
2094       ax = 0;
2095     }
2096
2097   /* Reading .o files */
2098   if (SC_IS_UNDEF (es->asym.sc) || es->asym.sc == scNil)
2099     {
2100       char *what;
2101       switch (es->asym.st)
2102         {
2103         case stNil:
2104           /* These are generated for static symbols in .o files,
2105              ignore them.  */
2106           return;
2107         case stStaticProc:
2108         case stProc:
2109           what = "procedure";
2110           n_undef_procs++;
2111           break;
2112         case stGlobal:
2113           what = "variable";
2114           n_undef_vars++;
2115           break;
2116         case stLabel:
2117           what = "label";
2118           n_undef_labels++;
2119           break;
2120         default:
2121           what = "symbol";
2122           break;
2123         }
2124       n_undef_symbols++;
2125       /* FIXME:  Turn this into a complaint?  */
2126       if (info_verbose)
2127         printf_filtered (_("Warning: %s `%s' is undefined (in %s)\n"),
2128                          what, debug_info->ssext + es->asym.iss,
2129                          fdr_name (cur_fdr));
2130       return;
2131     }
2132
2133   switch (es->asym.st)
2134     {
2135     case stProc:
2136     case stStaticProc:
2137       /* There is no need to parse the external procedure symbols.
2138          If they are from objects compiled without -g, their index will
2139          be indexNil, and the symbol definition from the minimal symbol
2140          is preferrable (yielding a function returning int instead of int).
2141          If the index points to a local procedure symbol, the local
2142          symbol already provides the correct type.
2143          Note that the index of the external procedure symbol points
2144          to the local procedure symbol in the local symbol table, and
2145          _not_ to the auxiliary symbol info.  */
2146       break;
2147     case stGlobal:
2148     case stLabel:
2149       /* Global common symbols are resolved by the runtime loader,
2150          ignore them.  */
2151       if (SC_IS_COMMON (es->asym.sc))
2152         break;
2153
2154       /* Note that the case of a symbol with indexNil must be handled
2155          anyways by parse_symbol().  */
2156       parse_symbol (&es->asym, ax, (char *) NULL,
2157                     bigend, section_offsets, objfile);
2158       break;
2159     default:
2160       break;
2161     }
2162 }
2163
2164 /* Parse the line number info for file descriptor FH into
2165    GDB's linetable LT.  MIPS' encoding requires a little bit
2166    of magic to get things out.  Note also that MIPS' line
2167    numbers can go back and forth, apparently we can live
2168    with that and do not need to reorder our linetables.  */
2169
2170 static void parse_lines (FDR *, PDR *, struct linetable *, int,
2171                          struct partial_symtab *, CORE_ADDR);
2172
2173 static void
2174 parse_lines (FDR *fh, PDR *pr, struct linetable *lt, int maxlines,
2175              struct partial_symtab *pst, CORE_ADDR lowest_pdr_addr)
2176 {
2177   unsigned char *base;
2178   int j, k;
2179   int delta, count, lineno = 0;
2180
2181   if (fh->cbLine == 0)
2182     return;
2183
2184   /* Scan by procedure descriptors.  */
2185   k = 0;
2186   for (j = 0; j < fh->cpd; j++, pr++)
2187     {
2188       CORE_ADDR l;
2189       CORE_ADDR adr;
2190       unsigned char *halt;
2191
2192       /* No code for this one.  */
2193       if (pr->iline == ilineNil ||
2194           pr->lnLow == -1 || pr->lnHigh == -1)
2195         continue;
2196
2197       /* Determine start and end address of compressed line bytes for
2198          this procedure.  */
2199       base = debug_info->line + fh->cbLineOffset;
2200       if (j != (fh->cpd - 1))
2201         halt = base + pr[1].cbLineOffset;
2202       else
2203         halt = base + fh->cbLine;
2204       base += pr->cbLineOffset;
2205
2206       adr = pst->textlow + pr->adr - lowest_pdr_addr;
2207
2208       l = adr >> 2;             /* in words */
2209       for (lineno = pr->lnLow; base < halt;)
2210         {
2211           count = *base & 0x0f;
2212           delta = *base++ >> 4;
2213           if (delta >= 8)
2214             delta -= 16;
2215           if (delta == -8)
2216             {
2217               delta = (base[0] << 8) | base[1];
2218               if (delta >= 0x8000)
2219                 delta -= 0x10000;
2220               base += 2;
2221             }
2222           lineno += delta;      /* first delta is 0 */
2223
2224           /* Complain if the line table overflows.  Could happen
2225              with corrupt binaries.  */
2226           if (lt->nitems >= maxlines)
2227             {
2228               complaint (&symfile_complaints,
2229                          _("guessed size of linetable for %s incorrectly"),
2230                          fdr_name (fh));
2231               break;
2232             }
2233           k = add_line (lt, lineno, l, k);
2234           l += count + 1;
2235         }
2236     }
2237 }
2238 \f
2239 static void
2240 function_outside_compilation_unit_complaint (const char *arg1)
2241 {
2242   complaint (&symfile_complaints,
2243              _("function `%s' appears to be defined "
2244                "outside of all compilation units"),
2245              arg1);
2246 }
2247
2248 /* Use the STORAGE_CLASS to compute which section the given symbol
2249    belongs to, and then records this new minimal symbol.  */
2250
2251 static void
2252 record_minimal_symbol (const char *name, const CORE_ADDR address,
2253                        enum minimal_symbol_type ms_type, int storage_class,
2254                        struct objfile *objfile)
2255 {
2256   int section;
2257
2258   switch (storage_class)
2259     {
2260       case scText:
2261         section = SECT_OFF_TEXT (objfile);
2262         break;
2263       case scData:
2264         section = SECT_OFF_DATA (objfile);
2265         break;
2266       case scBss:
2267         section = SECT_OFF_BSS (objfile);
2268         break;
2269       case scSData:
2270         section = get_section_index (objfile, ".sdata");
2271         break;
2272       case scSBss:
2273         section = get_section_index (objfile, ".sbss");
2274         break;
2275       case scRData:
2276         section = get_section_index (objfile, ".rdata");
2277         break;
2278       case scInit:
2279         section = get_section_index (objfile, ".init");
2280         break;
2281       case scXData:
2282         section = get_section_index (objfile, ".xdata");
2283         break;
2284       case scPData:
2285         section = get_section_index (objfile, ".pdata");
2286         break;
2287       case scFini:
2288         section = get_section_index (objfile, ".fini");
2289         break;
2290       case scRConst:
2291         section = get_section_index (objfile, ".rconst");
2292         break;
2293 #ifdef scTlsData
2294       case scTlsData:
2295         section = get_section_index (objfile, ".tlsdata");
2296         break;
2297 #endif
2298 #ifdef scTlsBss
2299       case scTlsBss:
2300         section = get_section_index (objfile, ".tlsbss");
2301         break;
2302 #endif
2303       default:
2304         /* This kind of symbol is not associated to a section.  */
2305         section = -1;
2306     }
2307
2308   prim_record_minimal_symbol_and_info (name, address, ms_type,
2309                                        section, objfile);
2310 }
2311
2312 /* Master parsing procedure for first-pass reading of file symbols
2313    into a partial_symtab.  */
2314
2315 static void
2316 parse_partial_symbols (struct objfile *objfile)
2317 {
2318   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2319   const bfd_size_type external_sym_size = debug_swap->external_sym_size;
2320   const bfd_size_type external_rfd_size = debug_swap->external_rfd_size;
2321   const bfd_size_type external_ext_size = debug_swap->external_ext_size;
2322   void (*const swap_ext_in) (bfd *, void *, EXTR *) = debug_swap->swap_ext_in;
2323   void (*const swap_sym_in) (bfd *, void *, SYMR *) = debug_swap->swap_sym_in;
2324   void (*const swap_rfd_in) (bfd *, void *, RFDT *) = debug_swap->swap_rfd_in;
2325   int f_idx, s_idx;
2326   HDRR *hdr = &debug_info->symbolic_header;
2327   /* Running pointers */
2328   FDR *fh;
2329   char *ext_out;
2330   char *ext_out_end;
2331   EXTR *ext_block;
2332   EXTR *ext_in;
2333   EXTR *ext_in_end;
2334   SYMR sh;
2335   struct partial_symtab *pst;
2336   int textlow_not_set = 1;
2337   int past_first_source_file = 0;
2338
2339   /* List of current psymtab's include files.  */
2340   const char **psymtab_include_list;
2341   int includes_allocated;
2342   int includes_used;
2343   EXTR *extern_tab;
2344   struct pst_map *fdr_to_pst;
2345   /* Index within current psymtab dependency list.  */
2346   struct partial_symtab **dependency_list;
2347   int dependencies_used, dependencies_allocated;
2348   struct cleanup *old_chain;
2349   char *name;
2350   enum language prev_language;
2351   asection *text_sect;
2352   int relocatable = 0;
2353
2354   /* Irix 5.2 shared libraries have a fh->adr field of zero, but
2355      the shared libraries are prelinked at a high memory address.
2356      We have to adjust the start address of the object file for this case,
2357      by setting it to the start address of the first procedure in the file.
2358      But we should do no adjustments if we are debugging a .o file, where
2359      the text section (and fh->adr) really starts at zero.  */
2360   text_sect = bfd_get_section_by_name (cur_bfd, ".text");
2361   if (text_sect != NULL
2362       && (bfd_get_section_flags (cur_bfd, text_sect) & SEC_RELOC))
2363     relocatable = 1;
2364
2365   extern_tab = (EXTR *) obstack_alloc (&objfile->objfile_obstack,
2366                                        sizeof (EXTR) * hdr->iextMax);
2367
2368   includes_allocated = 30;
2369   includes_used = 0;
2370   psymtab_include_list = (const char **) alloca (includes_allocated *
2371                                                  sizeof (const char *));
2372   next_symbol_text_func = mdebug_next_symbol_text;
2373
2374   dependencies_allocated = 30;
2375   dependencies_used = 0;
2376   dependency_list =
2377     (struct partial_symtab **) alloca (dependencies_allocated *
2378                                        sizeof (struct partial_symtab *));
2379
2380   set_last_source_file (NULL);
2381
2382   /*
2383    * Big plan:
2384    *
2385    * Only parse the Local and External symbols, and the Relative FDR.
2386    * Fixup enough of the loader symtab to be able to use it.
2387    * Allocate space only for the file's portions we need to
2388    * look at.  (XXX)
2389    */
2390
2391   max_gdbinfo = 0;
2392   max_glevel = MIN_GLEVEL;
2393
2394   /* Allocate the map FDR -> PST.
2395      Minor hack: -O3 images might claim some global data belongs
2396      to FDR -1.  We`ll go along with that.  */
2397   fdr_to_pst = (struct pst_map *)
2398     xzalloc ((hdr->ifdMax + 1) * sizeof *fdr_to_pst);
2399   old_chain = make_cleanup (xfree, fdr_to_pst);
2400   fdr_to_pst++;
2401   {
2402     struct partial_symtab *pst = new_psymtab ("", objfile);
2403
2404     fdr_to_pst[-1].pst = pst;
2405     FDR_IDX (pst) = -1;
2406   }
2407
2408   /* Allocate the global pending list.  */
2409   pending_list =
2410     ((struct mdebug_pending **)
2411      obstack_alloc (&objfile->objfile_obstack,
2412                     hdr->ifdMax * sizeof (struct mdebug_pending *)));
2413   memset (pending_list, 0,
2414           hdr->ifdMax * sizeof (struct mdebug_pending *));
2415
2416   /* Pass 0 over external syms: swap them in.  */
2417   ext_block = (EXTR *) xmalloc (hdr->iextMax * sizeof (EXTR));
2418   make_cleanup (xfree, ext_block);
2419
2420   ext_out = (char *) debug_info->external_ext;
2421   ext_out_end = ext_out + hdr->iextMax * external_ext_size;
2422   ext_in = ext_block;
2423   for (; ext_out < ext_out_end; ext_out += external_ext_size, ext_in++)
2424     (*swap_ext_in) (cur_bfd, ext_out, ext_in);
2425
2426   /* Pass 1 over external syms: Presize and partition the list.  */
2427   ext_in = ext_block;
2428   ext_in_end = ext_in + hdr->iextMax;
2429   for (; ext_in < ext_in_end; ext_in++)
2430     {
2431       /* See calls to complain below.  */
2432       if (ext_in->ifd >= -1
2433           && ext_in->ifd < hdr->ifdMax
2434           && ext_in->asym.iss >= 0
2435           && ext_in->asym.iss < hdr->issExtMax)
2436         fdr_to_pst[ext_in->ifd].n_globals++;
2437     }
2438
2439   /* Pass 1.5 over files:  partition out global symbol space.  */
2440   s_idx = 0;
2441   for (f_idx = -1; f_idx < hdr->ifdMax; f_idx++)
2442     {
2443       fdr_to_pst[f_idx].globals_offset = s_idx;
2444       s_idx += fdr_to_pst[f_idx].n_globals;
2445       fdr_to_pst[f_idx].n_globals = 0;
2446     }
2447
2448   /* ECOFF in ELF:
2449
2450      For ECOFF in ELF, we skip the creation of the minimal symbols.
2451      The ECOFF symbols should be a subset of the Elf symbols, and the 
2452      section information of the elf symbols will be more accurate.
2453      FIXME!  What about Irix 5's native linker?
2454
2455      By default, Elf sections which don't exist in ECOFF 
2456      get put in ECOFF's absolute section by the gnu linker.
2457      Since absolute sections don't get relocated, we 
2458      end up calculating an address different from that of 
2459      the symbol's minimal symbol (created earlier from the
2460      Elf symtab).
2461
2462      To fix this, either :
2463      1) don't create the duplicate symbol
2464      (assumes ECOFF symtab is a subset of the ELF symtab;
2465      assumes no side-effects result from ignoring ECOFF symbol)
2466      2) create it, only if lookup for existing symbol in ELF's minimal 
2467      symbols fails
2468      (inefficient; 
2469      assumes no side-effects result from ignoring ECOFF symbol)
2470      3) create it, but lookup ELF's minimal symbol and use it's section
2471      during relocation, then modify "uniqify" phase to merge and 
2472      eliminate the duplicate symbol
2473      (highly inefficient)
2474
2475      I've implemented #1 here...
2476      Skip the creation of the minimal symbols based on the ECOFF 
2477      symbol table.  */
2478
2479   /* Pass 2 over external syms: fill in external symbols.  */
2480   ext_in = ext_block;
2481   ext_in_end = ext_in + hdr->iextMax;
2482   for (; ext_in < ext_in_end; ext_in++)
2483     {
2484       enum minimal_symbol_type ms_type = mst_text;
2485       CORE_ADDR svalue = ext_in->asym.value;
2486
2487       /* The Irix 5 native tools seem to sometimes generate bogus
2488          external symbols.  */
2489       if (ext_in->ifd < -1 || ext_in->ifd >= hdr->ifdMax)
2490         {
2491           complaint (&symfile_complaints,
2492                      _("bad ifd for external symbol: %d (max %ld)"),
2493                      ext_in->ifd, hdr->ifdMax);
2494           continue;
2495         }
2496       if (ext_in->asym.iss < 0 || ext_in->asym.iss >= hdr->issExtMax)
2497         {
2498           complaint (&symfile_complaints,
2499                      _("bad iss for external symbol: %ld (max %ld)"),
2500                      ext_in->asym.iss, hdr->issExtMax);
2501           continue;
2502         }
2503
2504       extern_tab[fdr_to_pst[ext_in->ifd].globals_offset
2505                  + fdr_to_pst[ext_in->ifd].n_globals++] = *ext_in;
2506
2507
2508       if (SC_IS_UNDEF (ext_in->asym.sc) || ext_in->asym.sc == scNil)
2509         continue;
2510
2511
2512       /* Pass 3 over files, over local syms: fill in static symbols.  */
2513       name = debug_info->ssext + ext_in->asym.iss;
2514
2515       /* Process ECOFF Symbol Types and Storage Classes.  */
2516       switch (ext_in->asym.st)
2517         {
2518         case stProc:
2519           /* Beginnning of Procedure */
2520           break;
2521         case stStaticProc:
2522           /* Load time only static procs */
2523           ms_type = mst_file_text;
2524           break;
2525         case stGlobal:
2526           /* External symbol */
2527           if (SC_IS_COMMON (ext_in->asym.sc))
2528             {
2529               /* The value of a common symbol is its size, not its address.
2530                  Ignore it.  */
2531               continue;
2532             }
2533           else if (SC_IS_DATA (ext_in->asym.sc))
2534             {
2535               ms_type = mst_data;
2536             }
2537           else if (SC_IS_BSS (ext_in->asym.sc))
2538             {
2539               ms_type = mst_bss;
2540             }
2541           else if (SC_IS_SBSS (ext_in->asym.sc))
2542             {
2543               ms_type = mst_bss;
2544             }
2545           else
2546             ms_type = mst_abs;
2547           break;
2548         case stLabel:
2549           /* Label */
2550
2551           /* On certain platforms, some extra label symbols can be
2552              generated by the linker.  One possible usage for this kind
2553              of symbols is to represent the address of the begining of a
2554              given section.  For instance, on Tru64 5.1, the address of
2555              the _ftext label is the start address of the .text section.
2556
2557              The storage class of these symbols is usually directly
2558              related to the section to which the symbol refers.  For
2559              instance, on Tru64 5.1, the storage class for the _fdata
2560              label is scData, refering to the .data section.
2561
2562              It is actually possible that the section associated to the
2563              storage class of the label does not exist.  On True64 5.1
2564              for instance, the libm.so shared library does not contain
2565              any .data section, although it contains a _fpdata label
2566              which storage class is scData...  Since these symbols are
2567              usually useless for the debugger user anyway, we just
2568              discard these symbols.  */
2569           
2570           if (SC_IS_TEXT (ext_in->asym.sc))
2571             {
2572               if (objfile->sect_index_text == -1)
2573                 continue;
2574                 
2575               ms_type = mst_file_text;
2576             }
2577           else if (SC_IS_DATA (ext_in->asym.sc))
2578             {
2579               if (objfile->sect_index_data == -1)
2580                 continue;
2581
2582               ms_type = mst_file_data;
2583             }
2584           else if (SC_IS_BSS (ext_in->asym.sc))
2585             {
2586               if (objfile->sect_index_bss == -1)
2587                 continue;
2588
2589               ms_type = mst_file_bss;
2590             }
2591           else if (SC_IS_SBSS (ext_in->asym.sc))
2592             {
2593               const int sbss_sect_index = get_section_index (objfile, ".sbss");
2594
2595               if (sbss_sect_index == -1)
2596                 continue;
2597
2598               ms_type = mst_file_bss;
2599             }
2600           else
2601             ms_type = mst_abs;
2602           break;
2603         case stLocal:
2604         case stNil:
2605           /* The alpha has the section start addresses in stLocal symbols
2606              whose name starts with a `.'.  Skip those but complain for all
2607              other stLocal symbols.
2608              Irix6 puts the section start addresses in stNil symbols, skip
2609              those too.  */
2610           if (name[0] == '.')
2611             continue;
2612           /* Fall through.  */
2613         default:
2614           ms_type = mst_unknown;
2615           unknown_ext_complaint (name);
2616         }
2617       if (!ECOFF_IN_ELF (cur_bfd))
2618         record_minimal_symbol (name, svalue, ms_type, ext_in->asym.sc,
2619                                objfile);
2620     }
2621
2622   /* Pass 3 over files, over local syms: fill in static symbols.  */
2623   for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++)
2624     {
2625       struct partial_symtab *save_pst;
2626       EXTR *ext_ptr;
2627       CORE_ADDR textlow;
2628
2629       cur_fdr = fh = debug_info->fdr + f_idx;
2630
2631       if (fh->csym == 0)
2632         {
2633           fdr_to_pst[f_idx].pst = NULL;
2634           continue;
2635         }
2636
2637       /* Determine the start address for this object file from the
2638          file header and relocate it, except for Irix 5.2 zero fh->adr.  */
2639       if (fh->cpd)
2640         {
2641           textlow = fh->adr;
2642           if (relocatable || textlow != 0)
2643             textlow += ANOFFSET (objfile->section_offsets,
2644                                  SECT_OFF_TEXT (objfile));
2645         }
2646       else
2647         textlow = 0;
2648       pst = start_psymtab_common (objfile, objfile->section_offsets,
2649                                   fdr_name (fh),
2650                                   textlow,
2651                                   objfile->global_psymbols.next,
2652                                   objfile->static_psymbols.next);
2653       pst->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
2654                                                 sizeof (struct symloc));
2655       memset (pst->read_symtab_private, 0, sizeof (struct symloc));
2656
2657       save_pst = pst;
2658       FDR_IDX (pst) = f_idx;
2659       CUR_BFD (pst) = cur_bfd;
2660       DEBUG_SWAP (pst) = debug_swap;
2661       DEBUG_INFO (pst) = debug_info;
2662       PENDING_LIST (pst) = pending_list;
2663
2664       /* The way to turn this into a symtab is to call...  */
2665       pst->read_symtab = mdebug_read_symtab;
2666
2667       /* Set up language for the pst.
2668          The language from the FDR is used if it is unambigious (e.g. cfront
2669          with native cc and g++ will set the language to C).
2670          Otherwise we have to deduce the language from the filename.
2671          Native ecoff has every header file in a separate FDR, so
2672          deduce_language_from_filename will return language_unknown for
2673          a header file, which is not what we want.
2674          But the FDRs for the header files are after the FDR for the source
2675          file, so we can assign the language of the source file to the
2676          following header files.  Then we save the language in the private
2677          pst data so that we can reuse it when building symtabs.  */
2678       prev_language = psymtab_language;
2679
2680       switch (fh->lang)
2681         {
2682         case langCplusplusV2:
2683           psymtab_language = language_cplus;
2684           break;
2685         default:
2686           psymtab_language = deduce_language_from_filename (fdr_name (fh));
2687           break;
2688         }
2689       if (psymtab_language == language_unknown)
2690         psymtab_language = prev_language;
2691       PST_PRIVATE (pst)->pst_language = psymtab_language;
2692
2693       pst->texthigh = pst->textlow;
2694
2695       /* For stabs-in-ecoff files, the second symbol must be @stab.
2696          This symbol is emitted by mips-tfile to signal that the
2697          current object file uses encapsulated stabs instead of mips
2698          ecoff for local symbols.  (It is the second symbol because
2699          the first symbol is the stFile used to signal the start of a
2700          file).  */
2701       processing_gcc_compilation = 0;
2702       if (fh->csym >= 2)
2703         {
2704           (*swap_sym_in) (cur_bfd,
2705                           ((char *) debug_info->external_sym
2706                            + (fh->isymBase + 1) * external_sym_size),
2707                           &sh);
2708           if (strcmp (debug_info->ss + fh->issBase + sh.iss,
2709                       stabs_symbol) == 0)
2710             processing_gcc_compilation = 2;
2711         }
2712
2713       if (processing_gcc_compilation != 0)
2714         {
2715           for (cur_sdx = 2; cur_sdx < fh->csym; cur_sdx++)
2716             {
2717               int type_code;
2718               const char *namestring;
2719
2720               (*swap_sym_in) (cur_bfd,
2721                               (((char *) debug_info->external_sym)
2722                             + (fh->isymBase + cur_sdx) * external_sym_size),
2723                               &sh);
2724               type_code = ECOFF_UNMARK_STAB (sh.index);
2725               if (!ECOFF_IS_STAB (&sh))
2726                 {
2727                   if (sh.st == stProc || sh.st == stStaticProc)
2728                     {
2729                       CORE_ADDR procaddr;
2730                       long isym;
2731
2732                       if (sh.st == stStaticProc)
2733                         {
2734                           namestring = debug_info->ss + fh->issBase + sh.iss;
2735                           record_minimal_symbol (namestring, sh.value,
2736                                                  mst_file_text, sh.sc,
2737                                                  objfile);
2738                         }
2739                       sh.value += ANOFFSET (objfile->section_offsets,
2740                                             SECT_OFF_TEXT (objfile));
2741                       procaddr = sh.value;
2742
2743                       isym = AUX_GET_ISYM (fh->fBigendian,
2744                                            (debug_info->external_aux
2745                                             + fh->iauxBase
2746                                             + sh.index));
2747                       (*swap_sym_in) (cur_bfd,
2748                                       ((char *) debug_info->external_sym
2749                                        + ((fh->isymBase + isym - 1)
2750                                           * external_sym_size)),
2751                                       &sh);
2752                       if (sh.st == stEnd)
2753                         {
2754                           CORE_ADDR high = procaddr + sh.value;
2755
2756                           /* Kludge for Irix 5.2 zero fh->adr.  */
2757                           if (!relocatable
2758                           && (pst->textlow == 0 || procaddr < pst->textlow))
2759                             pst->textlow = procaddr;
2760                           if (high > pst->texthigh)
2761                             pst->texthigh = high;
2762                         }
2763                     }
2764                   else if (sh.st == stStatic)
2765                     {
2766                       switch (sh.sc)
2767                         {
2768                         case scUndefined:
2769                         case scSUndefined:
2770                         case scNil:
2771                         case scAbs:
2772                           break;
2773
2774                         case scData:
2775                         case scSData:
2776                         case scRData:
2777                         case scPData:
2778                         case scXData:
2779                           namestring = debug_info->ss + fh->issBase + sh.iss;
2780                           record_minimal_symbol (namestring, sh.value,
2781                                                  mst_file_data, sh.sc,
2782                                                  objfile);
2783                           sh.value += ANOFFSET (objfile->section_offsets,
2784                                                 SECT_OFF_DATA (objfile));
2785                           break;
2786
2787                         default:
2788                           /* FIXME!  Shouldn't this use cases for bss, 
2789                              then have the default be abs?  */
2790                           namestring = debug_info->ss + fh->issBase + sh.iss;
2791                           record_minimal_symbol (namestring, sh.value,
2792                                                  mst_file_bss, sh.sc,
2793                                                  objfile);
2794                           sh.value += ANOFFSET (objfile->section_offsets,
2795                                                 SECT_OFF_BSS (objfile));
2796                           break;
2797                         }
2798                     }
2799                   continue;
2800                 }
2801               /* Handle stabs continuation.  */
2802               {
2803                 char *stabstring = debug_info->ss + fh->issBase + sh.iss;
2804                 int len = strlen (stabstring);
2805
2806                 while (stabstring[len - 1] == '\\')
2807                   {
2808                     SYMR sh2;
2809                     char *stabstring1 = stabstring;
2810                     char *stabstring2;
2811                     int len2;
2812
2813                     /* Ignore continuation char from 1st string.  */
2814                     len--;
2815
2816                     /* Read next stabstring.  */
2817                     cur_sdx++;
2818                     (*swap_sym_in) (cur_bfd,
2819                                     (((char *) debug_info->external_sym)
2820                                      + (fh->isymBase + cur_sdx)
2821                                      * external_sym_size),
2822                                     &sh2);
2823                     stabstring2 = debug_info->ss + fh->issBase + sh2.iss;
2824                     len2 = strlen (stabstring2);
2825
2826                     /* Concatinate stabstring2 with stabstring1.  */
2827                     if (stabstring
2828                      && stabstring != debug_info->ss + fh->issBase + sh.iss)
2829                       stabstring = xrealloc (stabstring, len + len2 + 1);
2830                     else
2831                       {
2832                         stabstring = xmalloc (len + len2 + 1);
2833                         strcpy (stabstring, stabstring1);
2834                       }
2835                     strcpy (stabstring + len, stabstring2);
2836                     len += len2;
2837                   }
2838
2839                 switch (type_code)
2840                   {
2841                     char *p;
2842
2843                     /* Standard, external, non-debugger, symbols.  */
2844
2845                   case N_TEXT | N_EXT:
2846                   case N_NBTEXT | N_EXT:
2847                     sh.value += ANOFFSET (objfile->section_offsets,
2848                                           SECT_OFF_TEXT (objfile));
2849                     goto record_it;
2850
2851                   case N_DATA | N_EXT:
2852                   case N_NBDATA | N_EXT:
2853                     sh.value += ANOFFSET (objfile->section_offsets,
2854                                           SECT_OFF_DATA (objfile));
2855                     goto record_it;
2856
2857                   case N_BSS:
2858                   case N_BSS | N_EXT:
2859                   case N_NBBSS | N_EXT:
2860                   case N_SETV | N_EXT:          /* FIXME, is this in BSS?  */
2861                     sh.value += ANOFFSET (objfile->section_offsets,
2862                                           SECT_OFF_BSS (objfile));
2863                     goto record_it;
2864
2865                   case N_ABS | N_EXT:
2866                   record_it:
2867                     continue;
2868
2869                   /* Standard, local, non-debugger, symbols.  */
2870
2871                   case N_NBTEXT:
2872
2873                     /* We need to be able to deal with both N_FN or
2874                        N_TEXT, because we have no way of knowing
2875                        whether the sys-supplied ld or GNU ld was used
2876                        to make the executable.  Sequents throw in
2877                        another wrinkle -- they renumbered N_FN.  */
2878
2879                   case N_FN:
2880                   case N_FN_SEQ:
2881                   case N_TEXT:
2882                     continue;
2883
2884                   case N_DATA:
2885                     sh.value += ANOFFSET (objfile->section_offsets,
2886                                           SECT_OFF_DATA (objfile));
2887                     goto record_it;
2888
2889                   case N_UNDF | N_EXT:
2890                     continue;           /* Just undefined, not COMMON.  */
2891
2892                   case N_UNDF:
2893                     continue;
2894
2895                     /* Lots of symbol types we can just ignore.  */
2896
2897                   case N_ABS:
2898                   case N_NBDATA:
2899                   case N_NBBSS:
2900                     continue;
2901
2902                     /* Keep going . . .  */
2903
2904                     /*
2905                      * Special symbol types for GNU
2906                      */
2907                   case N_INDR:
2908                   case N_INDR | N_EXT:
2909                   case N_SETA:
2910                   case N_SETA | N_EXT:
2911                   case N_SETT:
2912                   case N_SETT | N_EXT:
2913                   case N_SETD:
2914                   case N_SETD | N_EXT:
2915                   case N_SETB:
2916                   case N_SETB | N_EXT:
2917                   case N_SETV:
2918                     continue;
2919
2920                     /*
2921                      * Debugger symbols
2922                      */
2923
2924                   case N_SO:
2925                     {
2926                       CORE_ADDR valu;
2927                       static int prev_so_symnum = -10;
2928                       static int first_so_symnum;
2929                       const char *p;
2930                       int prev_textlow_not_set;
2931
2932                       valu = sh.value + ANOFFSET (objfile->section_offsets,
2933                                                   SECT_OFF_TEXT (objfile));
2934
2935                       prev_textlow_not_set = textlow_not_set;
2936
2937                       /* A zero value is probably an indication for the
2938                          SunPRO 3.0 compiler.  end_psymtab explicitly tests
2939                          for zero, so don't relocate it.  */
2940
2941                       if (sh.value == 0
2942                           && gdbarch_sofun_address_maybe_missing (gdbarch))
2943                         {
2944                           textlow_not_set = 1;
2945                           valu = 0;
2946                         }
2947                       else
2948                         textlow_not_set = 0;
2949
2950                       past_first_source_file = 1;
2951
2952                       if (prev_so_symnum != symnum - 1)
2953                         {               /* Here if prev stab wasn't N_SO.  */
2954                           first_so_symnum = symnum;
2955
2956                           if (pst)
2957                             {
2958                               pst = (struct partial_symtab *) 0;
2959                               includes_used = 0;
2960                               dependencies_used = 0;
2961                             }
2962                         }
2963
2964                       prev_so_symnum = symnum;
2965
2966                       /* End the current partial symtab and start a
2967                          new one.  */
2968
2969                       /* SET_NAMESTRING ();*/
2970                       namestring = stabstring;
2971
2972                       /* Null name means end of .o file.  Don't start a new
2973                          one.  */
2974                       if (*namestring == '\000')
2975                         continue;
2976
2977                       /* Some compilers (including gcc) emit a pair of
2978                          initial N_SOs.  The first one is a directory name;
2979                          the second the file name.  If pst exists, is
2980                          empty, and has a filename ending in '/', we assume
2981                          the previous N_SO was a directory name.  */
2982                       p = lbasename (namestring);
2983                       if (p != namestring && *p == '\000')
2984                         continue;               /* Simply ignore directory
2985                                                    name SOs.  */
2986
2987                       /* Some other compilers (C++ ones in particular) emit
2988                          useless SOs for non-existant .c files.  We ignore
2989                          all subsequent SOs that immediately follow the
2990                          first.  */
2991
2992                       if (!pst)
2993                         pst = save_pst;
2994                       continue;
2995                     }
2996
2997                   case N_BINCL:
2998                     continue;
2999
3000                   case N_SOL:
3001                     {
3002                       enum language tmp_language;
3003
3004                       /* Mark down an include file in the current psymtab.  */
3005
3006                       /* SET_NAMESTRING (); */
3007                       namestring = stabstring;
3008
3009                       tmp_language
3010                         = deduce_language_from_filename (namestring);
3011
3012                       /* Only change the psymtab's language if we've
3013                          learned something useful (eg. tmp_language is not
3014                          language_unknown).  In addition, to match what
3015                          start_subfile does, never change from C++ to
3016                          C.  */
3017                       if (tmp_language != language_unknown
3018                           && (tmp_language != language_c
3019                               || psymtab_language != language_cplus))
3020                         psymtab_language = tmp_language;
3021
3022                       /* In C++, one may expect the same filename to come
3023                          round many times, when code is coming alternately
3024                          from the main file and from inline functions in
3025                          other files.  So I check to see if this is a file
3026                          we've seen before -- either the main source file,
3027                          or a previously included file.
3028
3029                          This seems to be a lot of time to be spending on
3030                          N_SOL, but things like "break c-exp.y:435" need to
3031                          work (I suppose the psymtab_include_list could be
3032                          hashed or put in a binary tree, if profiling shows
3033                          this is a major hog).  */
3034                       if (pst && filename_cmp (namestring, pst->filename) == 0)
3035                         continue;
3036
3037                       {
3038                         int i;
3039
3040                         for (i = 0; i < includes_used; i++)
3041                           if (filename_cmp (namestring,
3042                                             psymtab_include_list[i]) == 0)
3043                             {
3044                               i = -1;
3045                               break;
3046                             }
3047                         if (i == -1)
3048                           continue;
3049                       }
3050
3051                       psymtab_include_list[includes_used++] = namestring;
3052                       if (includes_used >= includes_allocated)
3053                         {
3054                           const char **orig = psymtab_include_list;
3055
3056                           psymtab_include_list = (const char **)
3057                             alloca ((includes_allocated *= 2) *
3058                                     sizeof (const char *));
3059                           memcpy (psymtab_include_list, orig,
3060                                   includes_used * sizeof (const char *));
3061                         }
3062                       continue;
3063                     }
3064                   case N_LSYM:      /* Typedef or automatic variable.  */
3065                   case N_STSYM:     /* Data seg var -- static  */
3066                   case N_LCSYM:     /* BSS      "  */
3067                   case N_ROSYM:     /* Read-only data seg var -- static.  */
3068                   case N_NBSTS:     /* Gould nobase.  */
3069                   case N_NBLCS:     /* symbols.  */
3070                   case N_FUN:
3071                   case N_GSYM:      /* Global (extern) variable; can be
3072                                        data or bss (sigh FIXME).  */
3073
3074                     /* Following may probably be ignored; I'll leave them here
3075                        for now (until I do Pascal and Modula 2 extensions).  */
3076
3077                   case N_PC:        /* I may or may not need this; I
3078                                        suspect not.  */
3079                   case N_M2C:       /* I suspect that I can ignore this
3080                                        here.  */
3081                   case N_SCOPE:     /* Same.  */
3082
3083                     /*    SET_NAMESTRING (); */
3084                     namestring = stabstring;
3085                     p = (char *) strchr (namestring, ':');
3086                     if (!p)
3087                       continue;     /* Not a debugging symbol.  */
3088
3089
3090
3091                     /* Main processing section for debugging symbols which
3092                        the initial read through the symbol tables needs to
3093                        worry about.  If we reach this point, the symbol
3094                        which we are considering is definitely one we are
3095                        interested in.  p must also contain the (valid)
3096                        index into the namestring which indicates the
3097                        debugging type symbol.  */
3098
3099                     switch (p[1])
3100                       {
3101                       case 'S':
3102                         sh.value += ANOFFSET (objfile->section_offsets,
3103                                               SECT_OFF_DATA (objfile));
3104
3105                         if (gdbarch_static_transform_name_p (gdbarch))
3106                           namestring = gdbarch_static_transform_name
3107                                          (gdbarch, namestring);
3108
3109                         add_psymbol_to_list (namestring, p - namestring, 1,
3110                                              VAR_DOMAIN, LOC_STATIC,
3111                                              &objfile->static_psymbols,
3112                                              0, sh.value,
3113                                              psymtab_language, objfile);
3114                         continue;
3115                       case 'G':
3116                         sh.value += ANOFFSET (objfile->section_offsets,
3117                                               SECT_OFF_DATA (objfile));
3118                         /* The addresses in these entries are reported
3119                            to be wrong.  See the code that reads 'G's
3120                            for symtabs.  */
3121                         add_psymbol_to_list (namestring, p - namestring, 1,
3122                                              VAR_DOMAIN, LOC_STATIC,
3123                                              &objfile->global_psymbols,
3124                                              0, sh.value,
3125                                              psymtab_language, objfile);
3126                         continue;
3127
3128                       case 'T':
3129                         /* When a 'T' entry is defining an anonymous enum, it
3130                            may have a name which is the empty string, or a
3131                            single space.  Since they're not really defining a
3132                            symbol, those shouldn't go in the partial symbol
3133                            table.  We do pick up the elements of such enums at
3134                            'check_enum:', below.  */
3135                         if (p >= namestring + 2
3136                             || (p == namestring + 1
3137                                 && namestring[0] != ' '))
3138                           {
3139                             add_psymbol_to_list (namestring, p - namestring, 1,
3140                                                  STRUCT_DOMAIN, LOC_TYPEDEF,
3141                                                  &objfile->static_psymbols,
3142                                                  sh.value, 0,
3143                                                  psymtab_language, objfile);
3144                             if (p[2] == 't')
3145                               {
3146                                 /* Also a typedef with the same name.  */
3147                                 add_psymbol_to_list (namestring,
3148                                                      p - namestring, 1,
3149                                                      VAR_DOMAIN, LOC_TYPEDEF,
3150                                                      &objfile->static_psymbols,
3151                                                      sh.value, 0,
3152                                                      psymtab_language,
3153                                                      objfile);
3154                                 p += 1;
3155                               }
3156                           }
3157                         goto check_enum;
3158                       case 't':
3159                         if (p != namestring)    /* a name is there, not
3160                                                    just :T...  */
3161                           {
3162                             add_psymbol_to_list (namestring, p - namestring, 1,
3163                                                  VAR_DOMAIN, LOC_TYPEDEF,
3164                                                  &objfile->static_psymbols,
3165                                                  sh.value, 0,
3166                                                  psymtab_language, objfile);
3167                           }
3168                       check_enum:
3169                         /* If this is an enumerated type, we need to add
3170                            all the enum constants to the partial symbol
3171                            table.  This does not cover enums without names,
3172                            e.g. "enum {a, b} c;" in C, but fortunately
3173                            those are rare.  There is no way for GDB to find
3174                            those from the enum type without spending too
3175                            much time on it.  Thus to solve this problem,
3176                            the compiler needs to put out the enum in a
3177                            nameless type.  GCC2 does this.  */
3178
3179                         /* We are looking for something of the form
3180                            <name> ":" ("t" | "T") [<number> "="] "e"
3181                            {<constant> ":" <value> ","} ";".  */
3182
3183                         /* Skip over the colon and the 't' or 'T'.  */
3184                         p += 2;
3185                         /* This type may be given a number.  Also, numbers
3186                            can come in pairs like (0,26).  Skip over it.  */
3187                         while ((*p >= '0' && *p <= '9')
3188                                || *p == '(' || *p == ',' || *p == ')'
3189                                || *p == '=')
3190                           p++;
3191
3192                         if (*p++ == 'e')
3193                           {
3194                             /* The aix4 compiler emits extra crud before
3195                                the members.  */
3196                             if (*p == '-')
3197                               {
3198                                 /* Skip over the type (?).  */
3199                                 while (*p != ':')
3200                                   p++;
3201
3202                                 /* Skip over the colon.  */
3203                                 p++;
3204                               }
3205
3206                             /* We have found an enumerated type.  */
3207                             /* According to comments in read_enum_type
3208                                a comma could end it instead of a semicolon.
3209                                I don't know where that happens.
3210                                Accept either.  */
3211                             while (*p && *p != ';' && *p != ',')
3212                               {
3213                                 char *q;
3214
3215                                 /* Check for and handle cretinous dbx
3216                                    symbol name continuation!  */
3217                                 if (*p == '\\' || (*p == '?' && p[1] == '\0'))
3218                                   p = next_symbol_text (objfile);
3219
3220                                 /* Point to the character after the name
3221                                    of the enum constant.  */
3222                                 for (q = p; *q && *q != ':'; q++)
3223                                   ;
3224                                 /* Note that the value doesn't matter for
3225                                    enum constants in psymtabs, just in
3226                                    symtabs.  */
3227                                 add_psymbol_to_list (p, q - p, 1,
3228                                                      VAR_DOMAIN, LOC_CONST,
3229                                                      &objfile->static_psymbols,
3230                                                      0, 0, psymtab_language,
3231                                                      objfile);
3232                                 /* Point past the name.  */
3233                                 p = q;
3234                                 /* Skip over the value.  */
3235                                 while (*p && *p != ',')
3236                                   p++;
3237                                 /* Advance past the comma.  */
3238                                 if (*p)
3239                                   p++;
3240                               }
3241                           }
3242                         continue;
3243                       case 'c':
3244                         /* Constant, e.g. from "const" in Pascal.  */
3245                         add_psymbol_to_list (namestring, p - namestring, 1,
3246                                              VAR_DOMAIN, LOC_CONST,
3247                                              &objfile->static_psymbols,
3248                                              sh.value, 0, psymtab_language,
3249                                              objfile);
3250                         continue;
3251
3252                       case 'f':
3253                         if (! pst)
3254                           {
3255                             int name_len = p - namestring;
3256                             char *name = xmalloc (name_len + 1);
3257
3258                             memcpy (name, namestring, name_len);
3259                             name[name_len] = '\0';
3260                             function_outside_compilation_unit_complaint (name);
3261                             xfree (name);
3262                           }
3263                         sh.value += ANOFFSET (objfile->section_offsets,
3264                                               SECT_OFF_TEXT (objfile));
3265                         add_psymbol_to_list (namestring, p - namestring, 1,
3266                                              VAR_DOMAIN, LOC_BLOCK,
3267                                              &objfile->static_psymbols,
3268                                              0, sh.value,
3269                                              psymtab_language, objfile);
3270                         continue;
3271
3272                         /* Global functions were ignored here, but now they
3273                            are put into the global psymtab like one would
3274                            expect.  They're also in the minimal symbol
3275                            table.  */
3276                       case 'F':
3277                         if (! pst)
3278                           {
3279                             int name_len = p - namestring;
3280                             char *name = xmalloc (name_len + 1);
3281
3282                             memcpy (name, namestring, name_len);
3283                             name[name_len] = '\0';
3284                             function_outside_compilation_unit_complaint (name);
3285                             xfree (name);
3286                           }
3287                         sh.value += ANOFFSET (objfile->section_offsets,
3288                                               SECT_OFF_TEXT (objfile));
3289                         add_psymbol_to_list (namestring, p - namestring, 1,
3290                                              VAR_DOMAIN, LOC_BLOCK,
3291                                              &objfile->global_psymbols,
3292                                              0, sh.value,
3293                                              psymtab_language, objfile);
3294                         continue;
3295
3296                         /* Two things show up here (hopefully); static
3297                            symbols of local scope (static used inside
3298                            braces) or extensions of structure symbols.  We
3299                            can ignore both.  */
3300                       case 'V':
3301                       case '(':
3302                       case '0':
3303                       case '1':
3304                       case '2':
3305                       case '3':
3306                       case '4':
3307                       case '5':
3308                       case '6':
3309                       case '7':
3310                       case '8':
3311                       case '9':
3312                       case '-':
3313                       case '#':         /* For symbol identification (used
3314                                            in live ranges).  */
3315                         continue;
3316
3317                       case ':':
3318                         /* It is a C++ nested symbol.  We don't need to
3319                            record it (I don't think); if we try to look up
3320                            foo::bar::baz, then symbols for the symtab
3321                            containing foo should get read in, I think.  */
3322                         /* Someone says sun cc puts out symbols like
3323                            /foo/baz/maclib::/usr/local/bin/maclib,
3324                            which would get here with a symbol type of ':'.  */
3325                         continue;
3326
3327                       default:
3328                         /* Unexpected symbol descriptor.  The second and
3329                            subsequent stabs of a continued stab can show up
3330                            here.  The question is whether they ever can
3331                            mimic a normal stab--it would be nice if not,
3332                            since we certainly don't want to spend the time
3333                            searching to the end of every string looking for
3334                            a backslash.  */
3335
3336                         complaint (&symfile_complaints,
3337                                    _("unknown symbol descriptor `%c'"), p[1]);
3338
3339                         /* Ignore it; perhaps it is an extension that we don't
3340                            know about.  */
3341                         continue;
3342                       }
3343
3344                   case N_EXCL:
3345                     continue;
3346
3347                   case N_ENDM:
3348                     /* Solaris 2 end of module, finish current partial
3349                        symbol table.  END_PSYMTAB will set
3350                        pst->texthigh to the proper value, which is
3351                        necessary if a module compiled without
3352                        debugging info follows this module.  */
3353                     if (pst
3354                         && gdbarch_sofun_address_maybe_missing (gdbarch))
3355                       {
3356                         pst = (struct partial_symtab *) 0;
3357                         includes_used = 0;
3358                         dependencies_used = 0;
3359                       }
3360                     continue;
3361
3362                   case N_RBRAC:
3363                     if (sh.value > save_pst->texthigh)
3364                       save_pst->texthigh = sh.value;
3365                     continue;
3366                   case N_EINCL:
3367                   case N_DSLINE:
3368                   case N_BSLINE:
3369                   case N_SSYM:          /* Claim: Structure or union
3370                                            element.  Hopefully, I can
3371                                            ignore this.  */
3372                   case N_ENTRY:         /* Alternate entry point; can
3373                                            ignore.  */
3374                   case N_MAIN:          /* Can definitely ignore this.   */
3375                   case N_CATCH:         /* These are GNU C++ extensions.  */
3376                   case N_EHDECL:        /* that can safely be ignored here.  */
3377                   case N_LENG:
3378                   case N_BCOMM:
3379                   case N_ECOMM:
3380                   case N_ECOML:
3381                   case N_FNAME:
3382                   case N_SLINE:
3383                   case N_RSYM:
3384                   case N_PSYM:
3385                   case N_LBRAC:
3386                   case N_NSYMS:         /* Ultrix 4.0: symbol count */
3387                   case N_DEFD:                  /* GNU Modula-2 */
3388                   case N_ALIAS:         /* SunPro F77: alias name, ignore
3389                                            for now.  */
3390
3391                   case N_OBJ:           /* Useless types from Solaris.  */
3392                   case N_OPT:
3393                     /* These symbols aren't interesting; don't worry about
3394                        them.  */
3395
3396                     continue;
3397
3398                   default:
3399                     /* If we haven't found it yet, ignore it.  It's
3400                        probably some new type we don't know about yet.  */
3401                     complaint (&symfile_complaints,
3402                                _("unknown symbol type %s"),
3403                                hex_string (type_code)); /* CUR_SYMBOL_TYPE */
3404                     continue;
3405                   }
3406                 if (stabstring
3407                     && stabstring != debug_info->ss + fh->issBase + sh.iss)
3408                   xfree (stabstring);
3409               }
3410               /* end - Handle continuation */
3411             }
3412         }
3413       else
3414         {
3415           for (cur_sdx = 0; cur_sdx < fh->csym;)
3416             {
3417               char *name;
3418               enum address_class class;
3419               CORE_ADDR minsym_value;
3420
3421               (*swap_sym_in) (cur_bfd,
3422                               ((char *) debug_info->external_sym
3423                                + ((fh->isymBase + cur_sdx)
3424                                   * external_sym_size)),
3425                               &sh);
3426
3427               if (ECOFF_IS_STAB (&sh))
3428                 {
3429                   cur_sdx++;
3430                   continue;
3431                 }
3432
3433               /* Non absolute static symbols go into the minimal table.  */
3434               if (SC_IS_UNDEF (sh.sc) || sh.sc == scNil
3435                   || (sh.index == indexNil
3436                       && (sh.st != stStatic || sh.sc == scAbs)))
3437                 {
3438                   /* FIXME, premature?  */
3439                   cur_sdx++;
3440                   continue;
3441                 }
3442
3443               name = debug_info->ss + fh->issBase + sh.iss;
3444
3445               minsym_value = sh.value;
3446
3447               switch (sh.sc)
3448                 {
3449                 case scText:
3450                 case scRConst:
3451                   /* The value of a stEnd symbol is the displacement from the
3452                      corresponding start symbol value, do not relocate it.  */
3453                   if (sh.st != stEnd)
3454                     sh.value += ANOFFSET (objfile->section_offsets,
3455                                           SECT_OFF_TEXT (objfile));
3456                   break;
3457                 case scData:
3458                 case scSData:
3459                 case scRData:
3460                 case scPData:
3461                 case scXData:
3462                   sh.value += ANOFFSET (objfile->section_offsets,
3463                                         SECT_OFF_DATA (objfile));
3464                   break;
3465                 case scBss:
3466                 case scSBss:
3467                   sh.value += ANOFFSET (objfile->section_offsets,
3468                                         SECT_OFF_BSS (objfile));
3469                   break;
3470                 }
3471
3472               switch (sh.st)
3473                 {
3474                   CORE_ADDR high;
3475                   CORE_ADDR procaddr;
3476                   int new_sdx;
3477
3478                 case stStaticProc:
3479                   prim_record_minimal_symbol_and_info (name, minsym_value,
3480                                                        mst_file_text,
3481                                                        SECT_OFF_TEXT (objfile),
3482                                                        objfile);
3483
3484                   /* FALLTHROUGH */
3485
3486                 case stProc:
3487                   /* Ignore all parameter symbol records.  */
3488                   if (sh.index >= hdr->iauxMax)
3489                     {
3490                       /* Should not happen, but does when cross-compiling
3491                          with the MIPS compiler.  FIXME -- pull later.  */
3492                       index_complaint (name);
3493                       new_sdx = cur_sdx + 1;    /* Don't skip at all.  */
3494                     }
3495                   else
3496                     new_sdx = AUX_GET_ISYM (fh->fBigendian,
3497                                             (debug_info->external_aux
3498                                              + fh->iauxBase
3499                                              + sh.index));
3500
3501                   if (new_sdx <= cur_sdx)
3502                     {
3503                       /* This should not happen either... FIXME.  */
3504                       complaint (&symfile_complaints,
3505                                  _("bad proc end in aux found from symbol %s"),
3506                                  name);
3507                       new_sdx = cur_sdx + 1;    /* Don't skip backward.  */
3508                     }
3509
3510                   /* For stProc symbol records, we need to check the
3511                      storage class as well, as only (stProc, scText)
3512                      entries represent "real" procedures - See the
3513                      Compaq document titled "Object File / Symbol Table
3514                      Format Specification" for more information.  If the
3515                      storage class is not scText, we discard the whole
3516                      block of symbol records for this stProc.  */
3517                   if (sh.st == stProc && sh.sc != scText)
3518                     goto skip;
3519
3520                   /* Usually there is a local and a global stProc symbol
3521                      for a function.  This means that the function name
3522                      has already been entered into the mimimal symbol table
3523                      while processing the global symbols in pass 2 above.
3524                      One notable exception is the PROGRAM name from
3525                      f77 compiled executables, it is only put out as
3526                      local stProc symbol, and a global MAIN__ stProc symbol
3527                      points to it.  It doesn't matter though, as gdb is
3528                      still able to find the PROGRAM name via the partial
3529                      symbol table, and the MAIN__ symbol via the minimal
3530                      symbol table.  */
3531                   if (sh.st == stProc)
3532                     add_psymbol_to_list (name, strlen (name), 1,
3533                                          VAR_DOMAIN, LOC_BLOCK,
3534                                          &objfile->global_psymbols,
3535                                     0, sh.value, psymtab_language, objfile);
3536                   else
3537                     add_psymbol_to_list (name, strlen (name), 1,
3538                                          VAR_DOMAIN, LOC_BLOCK,
3539                                          &objfile->static_psymbols,
3540                                     0, sh.value, psymtab_language, objfile);
3541
3542                   procaddr = sh.value;
3543
3544                   cur_sdx = new_sdx;
3545                   (*swap_sym_in) (cur_bfd,
3546                                   ((char *) debug_info->external_sym
3547                                    + ((fh->isymBase + cur_sdx - 1)
3548                                       * external_sym_size)),
3549                                   &sh);
3550                   if (sh.st != stEnd)
3551                     continue;
3552
3553                   /* Kludge for Irix 5.2 zero fh->adr.  */
3554                   if (!relocatable
3555                       && (pst->textlow == 0 || procaddr < pst->textlow))
3556                     pst->textlow = procaddr;
3557
3558                   high = procaddr + sh.value;
3559                   if (high > pst->texthigh)
3560                     pst->texthigh = high;
3561                   continue;
3562
3563                 case stStatic:  /* Variable */
3564                   if (SC_IS_DATA (sh.sc))
3565                     prim_record_minimal_symbol_and_info (name, minsym_value,
3566                                                          mst_file_data,
3567                                                          SECT_OFF_DATA (objfile),
3568                                                          objfile);
3569                   else
3570                     prim_record_minimal_symbol_and_info (name, minsym_value,
3571                                                          mst_file_bss,
3572                                                          SECT_OFF_BSS (objfile),
3573                                                          objfile);
3574                   class = LOC_STATIC;
3575                   break;
3576
3577                 case stIndirect:        /* Irix5 forward declaration */
3578                   /* Skip forward declarations from Irix5 cc.  */
3579                   goto skip;
3580
3581                 case stTypedef: /* Typedef */
3582                   /* Skip typedefs for forward declarations and opaque
3583                      structs from alpha and mips cc.  */
3584                   if (sh.iss == 0 || has_opaque_xref (fh, &sh))
3585                     goto skip;
3586                   class = LOC_TYPEDEF;
3587                   break;
3588
3589                 case stConstant:        /* Constant decl */
3590                   class = LOC_CONST;
3591                   break;
3592
3593                 case stUnion:
3594                 case stStruct:
3595                 case stEnum:
3596                 case stBlock:   /* { }, str, un, enum */
3597                   /* Do not create a partial symbol for cc unnamed aggregates
3598                      and gcc empty aggregates.  */
3599                   if ((sh.sc == scInfo
3600                        || SC_IS_COMMON (sh.sc))
3601                       && sh.iss != 0
3602                       && sh.index != cur_sdx + 2)
3603                     {
3604                       add_psymbol_to_list (name, strlen (name), 1,
3605                                            STRUCT_DOMAIN, LOC_TYPEDEF,
3606                                            &objfile->static_psymbols,
3607                                            0, (CORE_ADDR) 0,
3608                                            psymtab_language, objfile);
3609                     }
3610                   handle_psymbol_enumerators (objfile, fh, sh.st, sh.value);
3611
3612                   /* Skip over the block.  */
3613                   new_sdx = sh.index;
3614                   if (new_sdx <= cur_sdx)
3615                     {
3616                       /* This happens with the Ultrix kernel.  */
3617                       complaint (&symfile_complaints,
3618                                  _("bad aux index at block symbol %s"), name);
3619                       new_sdx = cur_sdx + 1;    /* Don't skip backward.  */
3620                     }
3621                   cur_sdx = new_sdx;
3622                   continue;
3623
3624                 case stFile:    /* File headers */
3625                 case stLabel:   /* Labels */
3626                 case stEnd:     /* Ends of files */
3627                   goto skip;
3628
3629                 case stLocal:   /* Local variables */
3630                   /* Normally these are skipped because we skip over
3631                      all blocks we see.  However, these can occur
3632                      as visible symbols in a .h file that contains code.  */
3633                   goto skip;
3634
3635                 default:
3636                   /* Both complaints are valid:  one gives symbol name,
3637                      the other the offending symbol type.  */
3638                   complaint (&symfile_complaints, _("unknown local symbol %s"),
3639                              name);
3640                   complaint (&symfile_complaints, _("with type %d"), sh.st);
3641                   cur_sdx++;
3642                   continue;
3643                 }
3644               /* Use this gdb symbol.  */
3645               add_psymbol_to_list (name, strlen (name), 1,
3646                                    VAR_DOMAIN, class,
3647                                    &objfile->static_psymbols,
3648                                    0, sh.value, psymtab_language, objfile);
3649             skip:
3650               cur_sdx++;        /* Go to next file symbol.  */
3651             }
3652
3653           /* Now do enter the external symbols.  */
3654           ext_ptr = &extern_tab[fdr_to_pst[f_idx].globals_offset];
3655           cur_sdx = fdr_to_pst[f_idx].n_globals;
3656           PST_PRIVATE (save_pst)->extern_count = cur_sdx;
3657           PST_PRIVATE (save_pst)->extern_tab = ext_ptr;
3658           for (; --cur_sdx >= 0; ext_ptr++)
3659             {
3660               enum address_class class;
3661               SYMR *psh;
3662               char *name;
3663               CORE_ADDR svalue;
3664
3665               if (ext_ptr->ifd != f_idx)
3666                 internal_error (__FILE__, __LINE__,
3667                                 _("failed internal consistency check"));
3668               psh = &ext_ptr->asym;
3669
3670               /* Do not add undefined symbols to the partial symbol table.  */
3671               if (SC_IS_UNDEF (psh->sc) || psh->sc == scNil)
3672                 continue;
3673
3674               svalue = psh->value;
3675               switch (psh->sc)
3676                 {
3677                 case scText:
3678                 case scRConst:
3679                   svalue += ANOFFSET (objfile->section_offsets,
3680                                       SECT_OFF_TEXT (objfile));
3681                   break;
3682                 case scData:
3683                 case scSData:
3684                 case scRData:
3685                 case scPData:
3686                 case scXData:
3687                   svalue += ANOFFSET (objfile->section_offsets,
3688                                       SECT_OFF_DATA (objfile));
3689                   break;
3690                 case scBss:
3691                 case scSBss:
3692                   svalue += ANOFFSET (objfile->section_offsets,
3693                                       SECT_OFF_BSS (objfile));
3694                   break;
3695                 }
3696
3697               switch (psh->st)
3698                 {
3699                 case stNil:
3700                   /* These are generated for static symbols in .o files,
3701                      ignore them.  */
3702                   continue;
3703                 case stProc:
3704                 case stStaticProc:
3705                   /* External procedure symbols have been entered
3706                      into the minimal symbol table in pass 2 above.
3707                      Ignore them, as parse_external will ignore them too.  */
3708                   continue;
3709                 case stLabel:
3710                   class = LOC_LABEL;
3711                   break;
3712                 default:
3713                   unknown_ext_complaint (debug_info->ssext + psh->iss);
3714                   /* Fall through, pretend it's global.  */
3715                 case stGlobal:
3716                   /* Global common symbols are resolved by the runtime loader,
3717                      ignore them.  */
3718                   if (SC_IS_COMMON (psh->sc))
3719                     continue;
3720
3721                   class = LOC_STATIC;
3722                   break;
3723                 }
3724               name = debug_info->ssext + psh->iss;
3725               add_psymbol_to_list (name, strlen (name), 1,
3726                                    VAR_DOMAIN, class,
3727                                    &objfile->global_psymbols,
3728                                    0, svalue,
3729                                    psymtab_language, objfile);
3730             }
3731         }
3732
3733       /* Link pst to FDR.  end_psymtab returns NULL if the psymtab was
3734          empty and put on the free list.  */
3735       fdr_to_pst[f_idx].pst = end_psymtab (objfile, save_pst,
3736                                         psymtab_include_list, includes_used,
3737                                            -1, save_pst->texthigh,
3738                        dependency_list, dependencies_used, textlow_not_set);
3739       includes_used = 0;
3740       dependencies_used = 0;
3741
3742       /* The objfile has its functions reordered if this partial symbol
3743          table overlaps any other partial symbol table.
3744          We cannot assume a reordered objfile if a partial symbol table
3745          is contained within another partial symbol table, as partial symbol
3746          tables for include files with executable code are contained
3747          within the partial symbol table for the including source file,
3748          and we do not want to flag the objfile reordered for these cases.
3749
3750          This strategy works well for Irix-5.2 shared libraries, but we
3751          might have to use a more elaborate (and slower) algorithm for
3752          other cases.  */
3753       save_pst = fdr_to_pst[f_idx].pst;
3754       if (save_pst != NULL
3755           && save_pst->textlow != 0
3756           && !(objfile->flags & OBJF_REORDERED))
3757         {
3758           ALL_OBJFILE_PSYMTABS (objfile, pst)
3759           {
3760             if (save_pst != pst
3761                 && save_pst->textlow >= pst->textlow
3762                 && save_pst->textlow < pst->texthigh
3763                 && save_pst->texthigh > pst->texthigh)
3764               {
3765                 objfile->flags |= OBJF_REORDERED;
3766                 break;
3767               }
3768           }
3769         }
3770     }
3771
3772   /* Now scan the FDRs for dependencies.  */
3773   for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++)
3774     {
3775       fh = f_idx + debug_info->fdr;
3776       pst = fdr_to_pst[f_idx].pst;
3777
3778       if (pst == (struct partial_symtab *) NULL)
3779         continue;
3780
3781       /* This should catch stabs-in-ecoff.  */
3782       if (fh->crfd <= 1)
3783         continue;
3784
3785       /* Skip the first file indirect entry as it is a self dependency for
3786          source files or a reverse .h -> .c dependency for header files.  */
3787       pst->number_of_dependencies = 0;
3788       pst->dependencies =
3789         ((struct partial_symtab **)
3790          obstack_alloc (&objfile->objfile_obstack,
3791                         ((fh->crfd - 1)
3792                          * sizeof (struct partial_symtab *))));
3793       for (s_idx = 1; s_idx < fh->crfd; s_idx++)
3794         {
3795           RFDT rh;
3796
3797           (*swap_rfd_in) (cur_bfd,
3798                           ((char *) debug_info->external_rfd
3799                            + (fh->rfdBase + s_idx) * external_rfd_size),
3800                           &rh);
3801           if (rh < 0 || rh >= hdr->ifdMax)
3802             {
3803               complaint (&symfile_complaints, _("bad file number %ld"), rh);
3804               continue;
3805             }
3806
3807           /* Skip self dependencies of header files.  */
3808           if (rh == f_idx)
3809             continue;
3810
3811           /* Do not add to dependeny list if psymtab was empty.  */
3812           if (fdr_to_pst[rh].pst == (struct partial_symtab *) NULL)
3813             continue;
3814           pst->dependencies[pst->number_of_dependencies++]
3815             = fdr_to_pst[rh].pst;
3816         }
3817     }
3818
3819   /* Remove the dummy psymtab created for -O3 images above, if it is
3820      still empty, to enable the detection of stripped executables.  */
3821   if (objfile->psymtabs->next == NULL
3822       && objfile->psymtabs->number_of_dependencies == 0
3823       && objfile->psymtabs->n_global_syms == 0
3824       && objfile->psymtabs->n_static_syms == 0)
3825     objfile->psymtabs = NULL;
3826   do_cleanups (old_chain);
3827 }
3828
3829 /* If the current psymbol has an enumerated type, we need to add
3830    all the enum constants to the partial symbol table.  */
3831
3832 static void
3833 handle_psymbol_enumerators (struct objfile *objfile, FDR *fh, int stype,
3834                             CORE_ADDR svalue)
3835 {
3836   const bfd_size_type external_sym_size = debug_swap->external_sym_size;
3837   void (*const swap_sym_in) (bfd *, void *, SYMR *) = debug_swap->swap_sym_in;
3838   char *ext_sym = ((char *) debug_info->external_sym
3839                    + ((fh->isymBase + cur_sdx + 1) * external_sym_size));
3840   SYMR sh;
3841   TIR tir;
3842
3843   switch (stype)
3844     {
3845     case stEnum:
3846       break;
3847
3848     case stBlock:
3849       /* It is an enumerated type if the next symbol entry is a stMember
3850          and its auxiliary index is indexNil or its auxiliary entry
3851          is a plain btNil or btVoid.
3852          Alpha cc -migrate enums are recognized by a zero index and
3853          a zero symbol value.
3854          DU 4.0 cc enums are recognized by a member type of btEnum without
3855          qualifiers and a zero symbol value.  */
3856       (*swap_sym_in) (cur_bfd, ext_sym, &sh);
3857       if (sh.st != stMember)
3858         return;
3859
3860       if (sh.index == indexNil
3861           || (sh.index == 0 && svalue == 0))
3862         break;
3863       (*debug_swap->swap_tir_in) (fh->fBigendian,
3864                                   &(debug_info->external_aux
3865                                     + fh->iauxBase + sh.index)->a_ti,
3866                                   &tir);
3867       if ((tir.bt != btNil
3868            && tir.bt != btVoid
3869            && (tir.bt != btEnum || svalue != 0))
3870           || tir.tq0 != tqNil)
3871         return;
3872       break;
3873
3874     default:
3875       return;
3876     }
3877
3878   for (;;)
3879     {
3880       char *name;
3881
3882       (*swap_sym_in) (cur_bfd, ext_sym, &sh);
3883       if (sh.st != stMember)
3884         break;
3885       name = debug_info->ss + cur_fdr->issBase + sh.iss;
3886
3887       /* Note that the value doesn't matter for enum constants
3888          in psymtabs, just in symtabs.  */
3889       add_psymbol_to_list (name, strlen (name), 1,
3890                            VAR_DOMAIN, LOC_CONST,
3891                            &objfile->static_psymbols, 0,
3892                            (CORE_ADDR) 0, psymtab_language, objfile);
3893       ext_sym += external_sym_size;
3894     }
3895 }
3896
3897 /* Get the next symbol.  OBJFILE is unused.  */
3898
3899 static char *
3900 mdebug_next_symbol_text (struct objfile *objfile)
3901 {
3902   SYMR sh;
3903
3904   cur_sdx++;
3905   (*debug_swap->swap_sym_in) (cur_bfd,
3906                               ((char *) debug_info->external_sym
3907                                + ((cur_fdr->isymBase + cur_sdx)
3908                                   * debug_swap->external_sym_size)),
3909                               &sh);
3910   return debug_info->ss + cur_fdr->issBase + sh.iss;
3911 }
3912
3913 /* Ancillary function to psymtab_to_symtab().  Does all the work
3914    for turning the partial symtab PST into a symtab, recurring
3915    first on all dependent psymtabs.  The argument FILENAME is
3916    only passed so we can see in debug stack traces what file
3917    is being read.
3918
3919    This function has a split personality, based on whether the
3920    symbol table contains ordinary ecoff symbols, or stabs-in-ecoff.
3921    The flow of control and even the memory allocation differs.  FIXME.  */
3922
3923 static void
3924 psymtab_to_symtab_1 (struct objfile *objfile,
3925                      struct partial_symtab *pst, const char *filename)
3926 {
3927   bfd_size_type external_sym_size;
3928   bfd_size_type external_pdr_size;
3929   void (*swap_sym_in) (bfd *, void *, SYMR *);
3930   void (*swap_pdr_in) (bfd *, void *, PDR *);
3931   int i;
3932   struct symtab *st = NULL;
3933   FDR *fh;
3934   struct linetable *lines;
3935   CORE_ADDR lowest_pdr_addr = 0;
3936   int last_symtab_ended = 0;
3937
3938   if (pst->readin)
3939     return;
3940   pst->readin = 1;
3941
3942   /* Read in all partial symbtabs on which this one is dependent.
3943      NOTE that we do have circular dependencies, sigh.  We solved
3944      that by setting pst->readin before this point.  */
3945
3946   for (i = 0; i < pst->number_of_dependencies; i++)
3947     if (!pst->dependencies[i]->readin)
3948       {
3949         /* Inform about additional files to be read in.  */
3950         if (info_verbose)
3951           {
3952             fputs_filtered (" ", gdb_stdout);
3953             wrap_here ("");
3954             fputs_filtered ("and ", gdb_stdout);
3955             wrap_here ("");
3956             printf_filtered ("%s...",
3957                              pst->dependencies[i]->filename);
3958             wrap_here ("");     /* Flush output */
3959             gdb_flush (gdb_stdout);
3960           }
3961         /* We only pass the filename for debug purposes.  */
3962         psymtab_to_symtab_1 (objfile, pst->dependencies[i],
3963                              pst->dependencies[i]->filename);
3964       }
3965
3966   /* Do nothing if this is a dummy psymtab.  */
3967
3968   if (pst->n_global_syms == 0 && pst->n_static_syms == 0
3969       && pst->textlow == 0 && pst->texthigh == 0)
3970     return;
3971
3972   /* Now read the symbols for this symtab.  */
3973
3974   cur_bfd = CUR_BFD (pst);
3975   debug_swap = DEBUG_SWAP (pst);
3976   debug_info = DEBUG_INFO (pst);
3977   pending_list = PENDING_LIST (pst);
3978   external_sym_size = debug_swap->external_sym_size;
3979   external_pdr_size = debug_swap->external_pdr_size;
3980   swap_sym_in = debug_swap->swap_sym_in;
3981   swap_pdr_in = debug_swap->swap_pdr_in;
3982   mdebugread_objfile = objfile;
3983   cur_fd = FDR_IDX (pst);
3984   fh = ((cur_fd == -1)
3985         ? (FDR *) NULL
3986         : debug_info->fdr + cur_fd);
3987   cur_fdr = fh;
3988
3989   /* See comment in parse_partial_symbols about the @stabs sentinel.  */
3990   processing_gcc_compilation = 0;
3991   if (fh != (FDR *) NULL && fh->csym >= 2)
3992     {
3993       SYMR sh;
3994
3995       (*swap_sym_in) (cur_bfd,
3996                       ((char *) debug_info->external_sym
3997                        + (fh->isymBase + 1) * external_sym_size),
3998                       &sh);
3999       if (strcmp (debug_info->ss + fh->issBase + sh.iss,
4000                   stabs_symbol) == 0)
4001         {
4002           /* We indicate that this is a GCC compilation so that certain
4003              features will be enabled in stabsread/dbxread.  */
4004           processing_gcc_compilation = 2;
4005         }
4006     }
4007
4008   if (processing_gcc_compilation != 0)
4009     {
4010       struct gdbarch *gdbarch = get_objfile_arch (objfile);
4011
4012       /* This symbol table contains stabs-in-ecoff entries.  */
4013
4014       /* Parse local symbols first.  */
4015
4016       if (fh->csym <= 2)        /* FIXME, this blows psymtab->symtab ptr.  */
4017         {
4018           mdebugread_objfile = NULL;
4019           return;
4020         }
4021       for (cur_sdx = 2; cur_sdx < fh->csym; cur_sdx++)
4022         {
4023           SYMR sh;
4024           char *name;
4025           CORE_ADDR valu;
4026
4027           (*swap_sym_in) (cur_bfd,
4028                           (((char *) debug_info->external_sym)
4029                            + (fh->isymBase + cur_sdx) * external_sym_size),
4030                           &sh);
4031           name = debug_info->ss + fh->issBase + sh.iss;
4032           valu = sh.value;
4033           /* XXX This is a hack.  It will go away!  */
4034           if (ECOFF_IS_STAB (&sh) || (name[0] == '#'))
4035             {
4036               int type_code = ECOFF_UNMARK_STAB (sh.index);
4037
4038               /* We should never get non N_STAB symbols here, but they
4039                  should be harmless, so keep process_one_symbol from
4040                  complaining about them.  */
4041               if (type_code & N_STAB)
4042                 {
4043                   /* If we found a trailing N_SO with no name, process
4044                      it here instead of in process_one_symbol, so we
4045                      can keep a handle to its symtab.  The symtab
4046                      would otherwise be ended twice, once in
4047                      process_one_symbol, and once after this loop.  */
4048                   if (type_code == N_SO
4049                       && get_last_source_file ()
4050                       && previous_stab_code != (unsigned char) N_SO
4051                       && *name == '\000')
4052                     {
4053                       valu += ANOFFSET (pst->section_offsets,
4054                                         SECT_OFF_TEXT (objfile));
4055                       previous_stab_code = N_SO;
4056                       st = end_symtab (valu, SECT_OFF_TEXT (objfile));
4057                       end_stabs ();
4058                       last_symtab_ended = 1;
4059                     }
4060                   else
4061                     {
4062                       last_symtab_ended = 0;
4063                       process_one_symbol (type_code, 0, valu, name,
4064                                           pst->section_offsets, objfile);
4065                     }
4066                 }
4067               /* Similarly a hack.  */
4068               else if (name[0] == '#')
4069                 {
4070                   process_one_symbol (N_SLINE, 0, valu, name,
4071                                       pst->section_offsets, objfile);
4072                 }
4073               if (type_code == N_FUN)
4074                 {
4075                   /* Make up special symbol to contain
4076                      procedure specific info.  */
4077                   struct mdebug_extra_func_info *e =
4078                     ((struct mdebug_extra_func_info *)
4079                      obstack_alloc (&mdebugread_objfile->objfile_obstack,
4080                                     sizeof (struct mdebug_extra_func_info)));
4081                   struct symbol *s = new_symbol (MDEBUG_EFI_SYMBOL_NAME);
4082
4083                   memset (e, 0, sizeof (struct mdebug_extra_func_info));
4084                   SYMBOL_DOMAIN (s) = LABEL_DOMAIN;
4085                   SYMBOL_ACLASS_INDEX (s) = LOC_CONST;
4086                   SYMBOL_TYPE (s) = objfile_type (objfile)->builtin_void;
4087                   SYMBOL_VALUE_BYTES (s) = (gdb_byte *) e;
4088                   e->pdr.framereg = -1;
4089                   add_symbol_to_list (s, &local_symbols);
4090                 }
4091             }
4092           else if (sh.st == stLabel)
4093             {
4094               if (sh.index == indexNil)
4095                 {
4096                   /* This is what the gcc2_compiled and __gnu_compiled_*
4097                      show up as.  So don't complain.  */
4098                   ;
4099                 }
4100               else
4101                 {
4102                   /* Handle encoded stab line number.  */
4103                   valu += ANOFFSET (pst->section_offsets,
4104                                     SECT_OFF_TEXT (objfile));
4105                   record_line (current_subfile, sh.index,
4106                                gdbarch_addr_bits_remove (gdbarch, valu));
4107                 }
4108             }
4109           else if (sh.st == stProc || sh.st == stStaticProc
4110                    || sh.st == stStatic || sh.st == stEnd)
4111             /* These are generated by gcc-2.x, do not complain.  */
4112             ;
4113           else
4114             complaint (&symfile_complaints,
4115                        _("unknown stabs symbol %s"), name);
4116         }
4117
4118       if (! last_symtab_ended)
4119         {
4120           st = end_symtab (pst->texthigh, SECT_OFF_TEXT (objfile));
4121           end_stabs ();
4122         }
4123
4124       /* There used to be a call to sort_blocks here, but this should not
4125          be necessary for stabs symtabs.  And as sort_blocks modifies the
4126          start address of the GLOBAL_BLOCK to the FIRST_LOCAL_BLOCK,
4127          it did the wrong thing if the first procedure in a file was
4128          generated via asm statements.  */
4129
4130       /* Fill in procedure info next.  */
4131       if (fh->cpd > 0)
4132         {
4133           PDR *pr_block;
4134           struct cleanup *old_chain;
4135           char *pdr_ptr;
4136           char *pdr_end;
4137           PDR *pdr_in;
4138           PDR *pdr_in_end;
4139
4140           pr_block = (PDR *) xmalloc (fh->cpd * sizeof (PDR));
4141           old_chain = make_cleanup (xfree, pr_block);
4142
4143           pdr_ptr = ((char *) debug_info->external_pdr
4144                      + fh->ipdFirst * external_pdr_size);
4145           pdr_end = pdr_ptr + fh->cpd * external_pdr_size;
4146           pdr_in = pr_block;
4147           for (;
4148                pdr_ptr < pdr_end;
4149                pdr_ptr += external_pdr_size, pdr_in++)
4150             {
4151               (*swap_pdr_in) (cur_bfd, pdr_ptr, pdr_in);
4152
4153               /* Determine lowest PDR address, the PDRs are not always
4154                  sorted.  */
4155               if (pdr_in == pr_block)
4156                 lowest_pdr_addr = pdr_in->adr;
4157               else if (pdr_in->adr < lowest_pdr_addr)
4158                 lowest_pdr_addr = pdr_in->adr;
4159             }
4160
4161           pdr_in = pr_block;
4162           pdr_in_end = pdr_in + fh->cpd;
4163           for (; pdr_in < pdr_in_end; pdr_in++)
4164             parse_procedure (pdr_in, st, pst);
4165
4166           do_cleanups (old_chain);
4167         }
4168     }
4169   else
4170     {
4171       /* This symbol table contains ordinary ecoff entries.  */
4172
4173       int maxlines, size;
4174       EXTR *ext_ptr;
4175
4176       if (fh == 0)
4177         {
4178           maxlines = 0;
4179           st = new_symtab ("unknown", 0, objfile);
4180         }
4181       else
4182         {
4183           maxlines = 2 * fh->cline;
4184           st = new_symtab (pst->filename, maxlines, objfile);
4185
4186           /* The proper language was already determined when building
4187              the psymtab, use it.  */
4188           st->language = PST_PRIVATE (pst)->pst_language;
4189         }
4190
4191       psymtab_language = st->language;
4192
4193       lines = SYMTAB_LINETABLE (st);
4194
4195       /* Get a new lexical context.  */
4196
4197       push_parse_stack ();
4198       top_stack->cur_st = st;
4199       top_stack->cur_block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (st),
4200                                                 STATIC_BLOCK);
4201       BLOCK_START (top_stack->cur_block) = pst->textlow;
4202       BLOCK_END (top_stack->cur_block) = 0;
4203       top_stack->blocktype = stFile;
4204       top_stack->cur_type = 0;
4205       top_stack->procadr = 0;
4206       top_stack->numargs = 0;
4207       found_ecoff_debugging_info = 0;
4208
4209       if (fh)
4210         {
4211           char *sym_ptr;
4212           char *sym_end;
4213
4214           /* Parse local symbols first.  */
4215           sym_ptr = ((char *) debug_info->external_sym
4216                      + fh->isymBase * external_sym_size);
4217           sym_end = sym_ptr + fh->csym * external_sym_size;
4218           while (sym_ptr < sym_end)
4219             {
4220               SYMR sh;
4221               int c;
4222
4223               (*swap_sym_in) (cur_bfd, sym_ptr, &sh);
4224               c = parse_symbol (&sh,
4225                                 debug_info->external_aux + fh->iauxBase,
4226                                 sym_ptr, fh->fBigendian,
4227                                 pst->section_offsets, objfile);
4228               sym_ptr += c * external_sym_size;
4229             }
4230
4231           /* Linenumbers.  At the end, check if we can save memory.
4232              parse_lines has to look ahead an arbitrary number of PDR
4233              structures, so we swap them all first.  */
4234           if (fh->cpd > 0)
4235             {
4236               PDR *pr_block;
4237               struct cleanup *old_chain;
4238               char *pdr_ptr;
4239               char *pdr_end;
4240               PDR *pdr_in;
4241               PDR *pdr_in_end;
4242
4243               pr_block = (PDR *) xmalloc (fh->cpd * sizeof (PDR));
4244
4245               old_chain = make_cleanup (xfree, pr_block);
4246
4247               pdr_ptr = ((char *) debug_info->external_pdr
4248                          + fh->ipdFirst * external_pdr_size);
4249               pdr_end = pdr_ptr + fh->cpd * external_pdr_size;
4250               pdr_in = pr_block;
4251               for (;
4252                    pdr_ptr < pdr_end;
4253                    pdr_ptr += external_pdr_size, pdr_in++)
4254                 {
4255                   (*swap_pdr_in) (cur_bfd, pdr_ptr, pdr_in);
4256
4257                   /* Determine lowest PDR address, the PDRs are not always
4258                      sorted.  */
4259                   if (pdr_in == pr_block)
4260                     lowest_pdr_addr = pdr_in->adr;
4261                   else if (pdr_in->adr < lowest_pdr_addr)
4262                     lowest_pdr_addr = pdr_in->adr;
4263                 }
4264
4265               parse_lines (fh, pr_block, lines, maxlines,
4266                            pst, lowest_pdr_addr);
4267               if (lines->nitems < fh->cline)
4268                 lines = shrink_linetable (lines);
4269
4270               /* Fill in procedure info next.  */
4271               pdr_in = pr_block;
4272               pdr_in_end = pdr_in + fh->cpd;
4273               for (; pdr_in < pdr_in_end; pdr_in++)
4274                 parse_procedure (pdr_in, 0, pst);
4275
4276               do_cleanups (old_chain);
4277             }
4278         }
4279
4280       size = lines->nitems;
4281       if (size > 1)
4282         --size;
4283       SYMTAB_LINETABLE (st)
4284         = obstack_copy (&mdebugread_objfile->objfile_obstack,
4285                         lines,
4286                         (sizeof (struct linetable)
4287                          + size * sizeof (lines->item)));
4288       xfree (lines);
4289
4290       /* .. and our share of externals.
4291          XXX use the global list to speed up things here.  How?
4292          FIXME, Maybe quit once we have found the right number of ext's?  */
4293       top_stack->cur_st = st;
4294       top_stack->cur_block
4295         = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (top_stack->cur_st),
4296                              GLOBAL_BLOCK);
4297       top_stack->blocktype = stFile;
4298
4299       ext_ptr = PST_PRIVATE (pst)->extern_tab;
4300       for (i = PST_PRIVATE (pst)->extern_count; --i >= 0; ext_ptr++)
4301         parse_external (ext_ptr, fh->fBigendian,
4302                         pst->section_offsets, objfile);
4303
4304       /* If there are undefined symbols, tell the user.
4305          The alpha has an undefined symbol for every symbol that is
4306          from a shared library, so tell the user only if verbose is on.  */
4307       if (info_verbose && n_undef_symbols)
4308         {
4309           printf_filtered (_("File %s contains %d unresolved references:"),
4310                            symtab_to_filename_for_display (st),
4311                            n_undef_symbols);
4312           printf_filtered ("\n\t%4d variables\n\t%4d "
4313                            "procedures\n\t%4d labels\n",
4314                            n_undef_vars, n_undef_procs, n_undef_labels);
4315           n_undef_symbols = n_undef_labels = n_undef_vars = n_undef_procs = 0;
4316
4317         }
4318       pop_parse_stack ();
4319
4320       set_symtab_primary (st, 1);
4321
4322       sort_blocks (st);
4323     }
4324
4325   /* Now link the psymtab and the symtab.  */
4326   pst->symtab = st;
4327
4328   mdebugread_objfile = NULL;
4329 }
4330 \f
4331 /* Ancillary parsing procedures.  */
4332
4333 /* Return 1 if the symbol pointed to by SH has a cross reference
4334    to an opaque aggregate type, else 0.  */
4335
4336 static int
4337 has_opaque_xref (FDR *fh, SYMR *sh)
4338 {
4339   TIR tir;
4340   union aux_ext *ax;
4341   RNDXR rn[1];
4342   unsigned int rf;
4343
4344   if (sh->index == indexNil)
4345     return 0;
4346
4347   ax = debug_info->external_aux + fh->iauxBase + sh->index;
4348   (*debug_swap->swap_tir_in) (fh->fBigendian, &ax->a_ti, &tir);
4349   if (tir.bt != btStruct && tir.bt != btUnion && tir.bt != btEnum)
4350     return 0;
4351
4352   ax++;
4353   (*debug_swap->swap_rndx_in) (fh->fBigendian, &ax->a_rndx, rn);
4354   if (rn->rfd == 0xfff)
4355     rf = AUX_GET_ISYM (fh->fBigendian, ax + 1);
4356   else
4357     rf = rn->rfd;
4358   if (rf != -1)
4359     return 0;
4360   return 1;
4361 }
4362
4363 /* Lookup the type at relative index RN.  Return it in TPP
4364    if found and in any event come up with its name PNAME.
4365    BIGEND says whether aux symbols are big-endian or not (from fh->fBigendian).
4366    Return value says how many aux symbols we ate.  */
4367
4368 static int
4369 cross_ref (int fd, union aux_ext *ax, struct type **tpp,
4370            enum type_code type_code,
4371            /* Use to alloc new type if none is found.  */
4372            char **pname, int bigend, char *sym_name)
4373 {
4374   RNDXR rn[1];
4375   unsigned int rf;
4376   int result = 1;
4377   FDR *fh;
4378   char *esh;
4379   SYMR sh;
4380   int xref_fd;
4381   struct mdebug_pending *pend;
4382
4383   *tpp = (struct type *) NULL;
4384
4385   (*debug_swap->swap_rndx_in) (bigend, &ax->a_rndx, rn);
4386
4387   /* Escape index means 'the next one'.  */
4388   if (rn->rfd == 0xfff)
4389     {
4390       result++;
4391       rf = AUX_GET_ISYM (bigend, ax + 1);
4392     }
4393   else
4394     {
4395       rf = rn->rfd;
4396     }
4397
4398   /* mips cc uses a rf of -1 for opaque struct definitions.
4399      Set TYPE_FLAG_STUB for these types so that check_typedef will
4400      resolve them if the struct gets defined in another compilation unit.  */
4401   if (rf == -1)
4402     {
4403       *pname = "<undefined>";
4404       *tpp = init_type (type_code, 0, TYPE_FLAG_STUB,
4405                         (char *) NULL, mdebugread_objfile);
4406       return result;
4407     }
4408
4409   /* mips cc uses an escaped rn->index of 0 for struct return types
4410      of procedures that were compiled without -g.  These will always remain
4411      undefined.  */
4412   if (rn->rfd == 0xfff && rn->index == 0)
4413     {
4414       *pname = "<undefined>";
4415       return result;
4416     }
4417
4418   /* Find the relative file descriptor and the symbol in it.  */
4419   fh = get_rfd (fd, rf);
4420   xref_fd = fh - debug_info->fdr;
4421
4422   if (rn->index >= fh->csym)
4423     {
4424       /* File indirect entry is corrupt.  */
4425       *pname = "<illegal>";
4426       bad_rfd_entry_complaint (sym_name, xref_fd, rn->index);
4427       return result;
4428     }
4429
4430   /* If we have processed this symbol then we left a forwarding
4431      pointer to the type in the pending list.  If not, we`ll put
4432      it in a list of pending types, to be processed later when
4433      the file will be.  In any event, we collect the name for the
4434      type here.  */
4435
4436   esh = ((char *) debug_info->external_sym
4437          + ((fh->isymBase + rn->index)
4438             * debug_swap->external_sym_size));
4439   (*debug_swap->swap_sym_in) (cur_bfd, esh, &sh);
4440
4441   /* Make sure that this type of cross reference can be handled.  */
4442   if ((sh.sc != scInfo
4443        || (sh.st != stBlock && sh.st != stTypedef && sh.st != stIndirect
4444            && sh.st != stStruct && sh.st != stUnion
4445            && sh.st != stEnum))
4446       && (sh.st != stBlock || !SC_IS_COMMON (sh.sc)))
4447     {
4448       /* File indirect entry is corrupt.  */
4449       *pname = "<illegal>";
4450       bad_rfd_entry_complaint (sym_name, xref_fd, rn->index);
4451       return result;
4452     }
4453
4454   *pname = debug_info->ss + fh->issBase + sh.iss;
4455
4456   pend = is_pending_symbol (fh, esh);
4457   if (pend)
4458     *tpp = pend->t;
4459   else
4460     {
4461       /* We have not yet seen this type.  */
4462
4463       if ((sh.iss == 0 && sh.st == stTypedef) || sh.st == stIndirect)
4464         {
4465           TIR tir;
4466
4467           /* alpha cc puts out a stTypedef with a sh.iss of zero for
4468              two cases:
4469              a) forward declarations of structs/unions/enums which are not
4470              defined in this compilation unit.
4471              For these the type will be void.  This is a bad design decision
4472              as cross referencing across compilation units is impossible
4473              due to the missing name.
4474              b) forward declarations of structs/unions/enums/typedefs which
4475              are defined later in this file or in another file in the same
4476              compilation unit.  Irix5 cc uses a stIndirect symbol for this.
4477              Simply cross reference those again to get the true type.
4478              The forward references are not entered in the pending list and
4479              in the symbol table.  */
4480
4481           (*debug_swap->swap_tir_in) (bigend,
4482                                       &(debug_info->external_aux
4483                                         + fh->iauxBase + sh.index)->a_ti,
4484                                       &tir);
4485           if (tir.tq0 != tqNil)
4486             complaint (&symfile_complaints,
4487                        _("illegal tq0 in forward typedef for %s"), sym_name);
4488           switch (tir.bt)
4489             {
4490             case btVoid:
4491               *tpp = init_type (type_code, 0, 0, (char *) NULL,
4492                                 mdebugread_objfile);
4493               *pname = "<undefined>";
4494               break;
4495
4496             case btStruct:
4497             case btUnion:
4498             case btEnum:
4499               cross_ref (xref_fd,
4500                          (debug_info->external_aux
4501                           + fh->iauxBase + sh.index + 1),
4502                          tpp, type_code, pname,
4503                          fh->fBigendian, sym_name);
4504               break;
4505
4506             case btTypedef:
4507               /* Follow a forward typedef.  This might recursively
4508                  call cross_ref till we get a non typedef'ed type.
4509                  FIXME: This is not correct behaviour, but gdb currently
4510                  cannot handle typedefs without type copying.  Type
4511                  copying is impossible as we might have mutual forward
4512                  references between two files and the copied type would not
4513                  get filled in when we later parse its definition.  */
4514               *tpp = parse_type (xref_fd,
4515                                  debug_info->external_aux + fh->iauxBase,
4516                                  sh.index,
4517                                  (int *) NULL,
4518                                  fh->fBigendian,
4519                                  debug_info->ss + fh->issBase + sh.iss);
4520               add_pending (fh, esh, *tpp);
4521               break;
4522
4523             default:
4524               complaint (&symfile_complaints,
4525                          _("illegal bt %d in forward typedef for %s"), tir.bt,
4526                          sym_name);
4527               *tpp = init_type (type_code, 0, 0, (char *) NULL,
4528                                 mdebugread_objfile);
4529               break;
4530             }
4531           return result;
4532         }
4533       else if (sh.st == stTypedef)
4534         {
4535           /* Parse the type for a normal typedef.  This might recursively call
4536              cross_ref till we get a non typedef'ed type.
4537              FIXME: This is not correct behaviour, but gdb currently
4538              cannot handle typedefs without type copying.  But type copying is
4539              impossible as we might have mutual forward references between
4540              two files and the copied type would not get filled in when
4541              we later parse its definition.   */
4542           *tpp = parse_type (xref_fd,
4543                              debug_info->external_aux + fh->iauxBase,
4544                              sh.index,
4545                              (int *) NULL,
4546                              fh->fBigendian,
4547                              debug_info->ss + fh->issBase + sh.iss);
4548         }
4549       else
4550         {
4551           /* Cross reference to a struct/union/enum which is defined
4552              in another file in the same compilation unit but that file
4553              has not been parsed yet.
4554              Initialize the type only, it will be filled in when
4555              it's definition is parsed.  */
4556           *tpp = init_type (type_code, 0, 0, (char *) NULL, mdebugread_objfile);
4557         }
4558       add_pending (fh, esh, *tpp);
4559     }
4560
4561   /* We used one auxent normally, two if we got a "next one" rf.  */
4562   return result;
4563 }
4564
4565
4566 /* Quick&dirty lookup procedure, to avoid the MI ones that require
4567    keeping the symtab sorted.  */
4568
4569 static struct symbol *
4570 mylookup_symbol (char *name, const struct block *block,
4571                  domain_enum domain, enum address_class class)
4572 {
4573   struct block_iterator iter;
4574   int inc;
4575   struct symbol *sym;
4576
4577   inc = name[0];
4578   ALL_BLOCK_SYMBOLS (block, iter, sym)
4579     {
4580       if (SYMBOL_LINKAGE_NAME (sym)[0] == inc
4581           && SYMBOL_DOMAIN (sym) == domain
4582           && SYMBOL_CLASS (sym) == class
4583           && strcmp (SYMBOL_LINKAGE_NAME (sym), name) == 0)
4584         return sym;
4585     }
4586
4587   block = BLOCK_SUPERBLOCK (block);
4588   if (block)
4589     return mylookup_symbol (name, block, domain, class);
4590   return 0;
4591 }
4592
4593
4594 /* Add a new symbol S to a block B.  */
4595
4596 static void
4597 add_symbol (struct symbol *s, struct symtab *symtab, struct block *b)
4598 {
4599   SYMBOL_SYMTAB (s) = symtab;
4600   dict_add_symbol (BLOCK_DICT (b), s);
4601 }
4602
4603 /* Add a new block B to a symtab S.  */
4604
4605 static void
4606 add_block (struct block *b, struct symtab *s)
4607 {
4608   /* Cast away "const", but that's ok because we're building the
4609      symtab and blockvector here.  */
4610   struct blockvector *bv = (struct blockvector *) SYMTAB_BLOCKVECTOR (s);
4611
4612   bv = (struct blockvector *) xrealloc ((void *) bv,
4613                                         (sizeof (struct blockvector)
4614                                          + BLOCKVECTOR_NBLOCKS (bv)
4615                                          * sizeof (bv->block)));
4616   if (bv != SYMTAB_BLOCKVECTOR (s))
4617     SYMTAB_BLOCKVECTOR (s) = bv;
4618
4619   BLOCKVECTOR_BLOCK (bv, BLOCKVECTOR_NBLOCKS (bv)++) = b;
4620 }
4621
4622 /* Add a new linenumber entry (LINENO,ADR) to a linevector LT.
4623    MIPS' linenumber encoding might need more than one byte
4624    to describe it, LAST is used to detect these continuation lines.
4625
4626    Combining lines with the same line number seems like a bad idea.
4627    E.g: There could be a line number entry with the same line number after the
4628    prologue and GDB should not ignore it (this is a better way to find
4629    a prologue than mips_skip_prologue).
4630    But due to the compressed line table format there are line number entries
4631    for the same line which are needed to bridge the gap to the next
4632    line number entry.  These entries have a bogus address info with them
4633    and we are unable to tell them from intended duplicate line number
4634    entries.
4635    This is another reason why -ggdb debugging format is preferable.  */
4636
4637 static int
4638 add_line (struct linetable *lt, int lineno, CORE_ADDR adr, int last)
4639 {
4640   /* DEC c89 sometimes produces zero linenos which confuse gdb.
4641      Change them to something sensible.  */
4642   if (lineno == 0)
4643     lineno = 1;
4644   if (last == 0)
4645     last = -2;                  /* Make sure we record first line.  */
4646
4647   if (last == lineno)           /* Skip continuation lines.  */
4648     return lineno;
4649
4650   lt->item[lt->nitems].line = lineno;
4651   lt->item[lt->nitems++].pc = adr << 2;
4652   return lineno;
4653 }
4654 \f
4655 /* Sorting and reordering procedures.  */
4656
4657 /* Blocks with a smaller low bound should come first.  */
4658
4659 static int
4660 compare_blocks (const void *arg1, const void *arg2)
4661 {
4662   LONGEST addr_diff;
4663   struct block **b1 = (struct block **) arg1;
4664   struct block **b2 = (struct block **) arg2;
4665
4666   addr_diff = (BLOCK_START ((*b1))) - (BLOCK_START ((*b2)));
4667   if (addr_diff == 0)
4668     return (BLOCK_END ((*b2))) - (BLOCK_END ((*b1)));
4669   return addr_diff;
4670 }
4671
4672 /* Sort the blocks of a symtab S.
4673    Reorder the blocks in the blockvector by code-address,
4674    as required by some MI search routines.  */
4675
4676 static void
4677 sort_blocks (struct symtab *s)
4678 {
4679   /* We have to cast away const here, but this is ok because we're
4680      constructing the blockvector in this code.  */
4681   struct blockvector *bv = (struct blockvector *) SYMTAB_BLOCKVECTOR (s);
4682
4683   if (BLOCKVECTOR_NBLOCKS (bv) <= FIRST_LOCAL_BLOCK)
4684     {
4685       /* Cosmetic */
4686       if (BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) == 0)
4687         BLOCK_START (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) = 0;
4688       if (BLOCK_END (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)) == 0)
4689         BLOCK_START (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)) = 0;
4690       return;
4691     }
4692   /*
4693    * This is very unfortunate: normally all functions are compiled in
4694    * the order they are found, but if the file is compiled -O3 things
4695    * are very different.  It would be nice to find a reliable test
4696    * to detect -O3 images in advance.
4697    */
4698   if (BLOCKVECTOR_NBLOCKS (bv) > FIRST_LOCAL_BLOCK + 1)
4699     qsort (&BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK),
4700            BLOCKVECTOR_NBLOCKS (bv) - FIRST_LOCAL_BLOCK,
4701            sizeof (struct block *),
4702            compare_blocks);
4703
4704   {
4705     CORE_ADDR high = 0;
4706     int i, j = BLOCKVECTOR_NBLOCKS (bv);
4707
4708     for (i = FIRST_LOCAL_BLOCK; i < j; i++)
4709       if (high < BLOCK_END (BLOCKVECTOR_BLOCK (bv, i)))
4710         high = BLOCK_END (BLOCKVECTOR_BLOCK (bv, i));
4711     BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) = high;
4712   }
4713
4714   BLOCK_START (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK)) =
4715     BLOCK_START (BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK));
4716
4717   BLOCK_START (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)) =
4718     BLOCK_START (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
4719   BLOCK_END (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)) =
4720     BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
4721 }
4722 \f
4723
4724 /* Constructor/restructor/destructor procedures.  */
4725
4726 /* Allocate a new symtab for NAME.  Needs an estimate of how many
4727    linenumbers MAXLINES we'll put in it.  */
4728
4729 static struct symtab *
4730 new_symtab (const char *name, int maxlines, struct objfile *objfile)
4731 {
4732   struct symtab *s = allocate_symtab (name, objfile);
4733   struct blockvector *bv;
4734
4735   SYMTAB_LINETABLE (s) = new_linetable (maxlines);
4736
4737   /* All symtabs must have at least two blocks.  */
4738   bv = new_bvect (2);
4739   BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = new_block (NON_FUNCTION_BLOCK);
4740   BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = new_block (NON_FUNCTION_BLOCK);
4741   BLOCK_SUPERBLOCK (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)) =
4742     BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
4743   SYMTAB_BLOCKVECTOR (s) = bv;
4744
4745   s->debugformat = "ECOFF";
4746   return (s);
4747 }
4748
4749 /* Allocate a new partial_symtab NAME.  */
4750
4751 static struct partial_symtab *
4752 new_psymtab (char *name, struct objfile *objfile)
4753 {
4754   struct partial_symtab *psymtab;
4755
4756   psymtab = allocate_psymtab (name, objfile);
4757   psymtab->section_offsets = objfile->section_offsets;
4758
4759   /* Keep a backpointer to the file's symbols.  */
4760
4761   psymtab->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
4762                                                 sizeof (struct symloc));
4763   memset (psymtab->read_symtab_private, 0, sizeof (struct symloc));
4764   CUR_BFD (psymtab) = cur_bfd;
4765   DEBUG_SWAP (psymtab) = debug_swap;
4766   DEBUG_INFO (psymtab) = debug_info;
4767   PENDING_LIST (psymtab) = pending_list;
4768
4769   /* The way to turn this into a symtab is to call...  */
4770   psymtab->read_symtab = mdebug_read_symtab;
4771   return (psymtab);
4772 }
4773
4774
4775 /* Allocate a linetable array of the given SIZE.  Since the struct
4776    already includes one item, we subtract one when calculating the
4777    proper size to allocate.  */
4778
4779 static struct linetable *
4780 new_linetable (int size)
4781 {
4782   struct linetable *l;
4783
4784   if (size > 1)
4785     --size;
4786   size = size * sizeof (l->item) + sizeof (struct linetable);
4787   l = (struct linetable *) xmalloc (size);
4788   l->nitems = 0;
4789   return l;
4790 }
4791
4792 /* Oops, too big.  Shrink it.  This was important with the 2.4 linetables,
4793    I am not so sure about the 3.4 ones.
4794
4795    Since the struct linetable already includes one item, we subtract one when
4796    calculating the proper size to allocate.  */
4797
4798 static struct linetable *
4799 shrink_linetable (struct linetable *lt)
4800 {
4801   return (struct linetable *) xrealloc ((void *) lt,
4802                                         (sizeof (struct linetable)
4803                                          + ((lt->nitems - 1)
4804                                             * sizeof (lt->item))));
4805 }
4806
4807 /* Allocate and zero a new blockvector of NBLOCKS blocks.  */
4808
4809 static struct blockvector *
4810 new_bvect (int nblocks)
4811 {
4812   struct blockvector *bv;
4813   int size;
4814
4815   size = sizeof (struct blockvector) + nblocks * sizeof (struct block *);
4816   bv = (struct blockvector *) xzalloc (size);
4817
4818   BLOCKVECTOR_NBLOCKS (bv) = nblocks;
4819
4820   return bv;
4821 }
4822
4823 /* Allocate and zero a new block, and set its BLOCK_DICT.  If function
4824    is non-zero, assume the block is associated to a function, and make
4825    sure that the symbols are stored linearly; otherwise, store them
4826    hashed.  */
4827
4828 static struct block *
4829 new_block (enum block_type type)
4830 {
4831   /* FIXME: carlton/2003-09-11: This should use allocate_block to
4832      allocate the block.  Which, in turn, suggests that the block
4833      should be allocated on an obstack.  */
4834   struct block *retval = xzalloc (sizeof (struct block));
4835
4836   if (type == FUNCTION_BLOCK)
4837     BLOCK_DICT (retval) = dict_create_linear_expandable ();
4838   else
4839     BLOCK_DICT (retval) = dict_create_hashed_expandable ();
4840
4841   return retval;
4842 }
4843
4844 /* Create a new symbol with printname NAME.  */
4845
4846 static struct symbol *
4847 new_symbol (char *name)
4848 {
4849   struct symbol *s = allocate_symbol (mdebugread_objfile);
4850
4851   SYMBOL_SET_LANGUAGE (s, psymtab_language,
4852                        &mdebugread_objfile->objfile_obstack);
4853   SYMBOL_SET_NAMES (s, name, strlen (name), 1, mdebugread_objfile);
4854   return s;
4855 }
4856
4857 /* Create a new type with printname NAME.  */
4858
4859 static struct type *
4860 new_type (char *name)
4861 {
4862   struct type *t;
4863
4864   t = alloc_type (mdebugread_objfile);
4865   TYPE_NAME (t) = name;
4866   INIT_CPLUS_SPECIFIC (t);
4867   return t;
4868 }
4869 \f
4870 /* Read ECOFF debugging information from a BFD section.  This is
4871    called from elfread.c.  It parses the section into a
4872    ecoff_debug_info struct, and then lets the rest of the file handle
4873    it as normal.  */
4874
4875 void
4876 elfmdebug_build_psymtabs (struct objfile *objfile,
4877                           const struct ecoff_debug_swap *swap, asection *sec)
4878 {
4879   bfd *abfd = objfile->obfd;
4880   struct ecoff_debug_info *info;
4881   struct cleanup *back_to;
4882
4883   /* FIXME: It's not clear whether we should be getting minimal symbol
4884      information from .mdebug in an ELF file, or whether we will.
4885      Re-initialize the minimal symbol reader in case we do.  */
4886
4887   init_minimal_symbol_collection ();
4888   back_to = make_cleanup_discard_minimal_symbols ();
4889
4890   info = ((struct ecoff_debug_info *)
4891           obstack_alloc (&objfile->objfile_obstack,
4892                          sizeof (struct ecoff_debug_info)));
4893
4894   if (!(*swap->read_debug_info) (abfd, sec, info))
4895     error (_("Error reading ECOFF debugging information: %s"),
4896            bfd_errmsg (bfd_get_error ()));
4897
4898   mdebug_build_psymtabs (objfile, swap, info);
4899
4900   install_minimal_symbols (objfile);
4901   do_cleanups (back_to);
4902 }
4903
4904 void
4905 _initialize_mdebugread (void)
4906 {
4907   basic_type_data = register_objfile_data ();
4908
4909   mdebug_register_index
4910     = register_symbol_register_impl (LOC_REGISTER, &mdebug_register_funcs);
4911   mdebug_regparm_index
4912     = register_symbol_register_impl (LOC_REGPARM_ADDR, &mdebug_register_funcs);
4913 }