]> Git Repo - binutils.git/blob - gdb/mipsread.c
Make structs work on DECstation (sigh)
[binutils.git] / gdb / mipsread.c
1 /* Read a symbol table in MIPS' format (Third-Eye).
2    Copyright 1986, 1987, 1989, 1990, 1991 Free Software Foundation, Inc.
3    Contributed by Alessandro Forin ([email protected]) at CMU.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* This module provides three functions: mipscoff_symfile_init,
22    which initializes to read a symbol file; mipscoff_new_init, which 
23    discards existing cached information when all symbols are being
24    discarded; and mipscoff_symfile_read, which reads a symbol table
25    from a file.
26
27    mipscoff_symfile_read only does the minimum work necessary for letting the
28    user "name" things symbolically; it does not read the entire symtab.
29    Instead, it reads the external and static symbols and puts them in partial
30    symbol tables.  When more extensive information is requested of a
31    file, the corresponding partial symbol table is mutated into a full
32    fledged symbol table by going back and reading the symbols
33    for real.  mipscoff_psymtab_to_symtab() is called indirectly through
34    a pointer in the psymtab to do this.  */
35
36 #include <stdio.h>
37 #include "defs.h"
38 #include "symtab.h"
39 #include "gdbcore.h"
40 #include "symfile.h"
41 #include "obstack.h"
42 #include <sys/param.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #ifdef  CMUCS
46 #include <mips/syms.h>
47 #else /* not CMUCS */
48 #include <symconst.h>
49 #include <sym.h>
50 #endif /* not CMUCS */
51
52 #include "coff-mips.h"
53
54 struct coff_exec {
55         struct external_filehdr f;
56         struct external_aouthdr a;
57 };
58
59 /* Each partial symbol table entry contains a pointer to private data for the
60    read_symtab() function to use when expanding a partial symbol table entry
61    to a full symbol table entry.
62
63    For mipsread this structure contains the index of the FDR that this psymtab
64    represents and a pointer to the symbol table header HDRR from the symbol
65    file that the psymtab was created from.  */
66
67 #define FDR_IDX(p) (((struct symloc *)((p)->read_symtab_private))->fdr_idx)
68 #define CUR_HDR(p) (((struct symloc *)((p)->read_symtab_private))->cur_hdr)
69
70 struct symloc {
71   int fdr_idx;
72   HDRR *cur_hdr;
73 };
74
75 /* Things we import explicitly from other modules */
76
77 extern int           info_verbose;
78 extern struct block *block_for_pc();
79 extern void          sort_symtab_syms();
80
81 /* Various complaints about symbol reading that don't abort the process */
82    
83 struct complaint unknown_ext_complaint = 
84         {"unknown external symbol %s", 0, 0};
85
86 struct complaint unknown_sym_complaint = 
87         {"unknown local symbol %s", 0, 0};
88
89 struct complaint unknown_st_complaint = 
90         {"with type %d", 0, 0};
91
92 struct complaint block_overflow_complaint = 
93         {"block containing %s overfilled", 0, 0};
94
95 struct complaint basic_type_complaint = 
96         {"cannot map MIPS basic type 0x%x", 0, 0};
97
98 struct complaint unknown_type_qual_complaint = 
99         {"unknown type qualifier 0x%x", 0, 0};
100
101 struct complaint array_bitsize_complaint = 
102         {"size of array target type not known, assuming %d bits", 0, 0};
103
104 struct complaint array_parse_complaint = 
105         {"array type with strange relative symbol", 0, 0};
106
107 /* Macros and extra defs */
108
109 /* Already-parsed symbols are marked specially */
110
111 #define stParsed stType
112
113 /* Puns: hard to find whether -g was used and how */
114
115 #define MIN_GLEVEL GLEVEL_0
116 #define compare_glevel(a,b)                                     \
117         (((a) == GLEVEL_3) ? ((b) < GLEVEL_3) :                 \
118          ((b) == GLEVEL_3) ? -1 : (int)((b) - (a)))
119
120 /* When looking at .o files, avoid tripping over bad addresses */
121
122 #define SAFE_TEXT_ADDR 0x400000
123 #define SAFE_DATA_ADDR 0x10000000
124
125 #define UNSAFE_DATA_ADDR(p)     ((unsigned)p < SAFE_DATA_ADDR || (unsigned)p > 2*SAFE_DATA_ADDR)
126 \f
127 /* Things that really are local to this module */
128
129 /* GDB symtable for the current compilation unit */
130
131 static struct symtab *cur_stab;
132
133 /* MIPS symtab header for the current file */
134
135 static HDRR     *cur_hdr;
136
137 /* Pointer to current file decriptor record, and its index */
138
139 static FDR      *cur_fdr;
140 static int       cur_fd;
141
142 /* Index of current symbol */
143
144 static int       cur_sdx;
145
146 /* Note how much "debuggable" this image is.  We would like
147    to see at least one FDR with full symbols */
148
149 static max_gdbinfo;
150 static max_glevel;
151
152 /* When examining .o files, report on undefined symbols */
153
154 static int n_undef_symbols, n_undef_labels, n_undef_vars, n_undef_procs;
155
156 /* Extra builtin types */
157
158 struct type *builtin_type_complex;
159 struct type *builtin_type_double_complex;
160 struct type *builtin_type_fixed_dec;
161 struct type *builtin_type_float_dec;
162 struct type *builtin_type_string;
163
164 /* Forward declarations */
165
166 static struct symbol    *new_symbol();
167 static struct type      *new_type();
168 static struct field     *new_field();
169 static struct block     *new_block();
170 static struct symtab    *new_symtab();
171 static struct linetable *new_linetable();
172 static struct blockvector *new_bvect();
173
174 static struct type      *parse_type();
175 static struct type      *make_type();
176 static struct type      *make_struct_type();
177 static struct symbol    *mylookup_symbol();
178 static struct block     *shrink_block();
179
180 static int compare_symtabs();
181 static int compare_psymtabs();
182 static int compare_blocks();
183
184 static struct partial_symtab *new_psymtab();
185 static struct partial_symtab *parse_fdr();
186 static int compare_psymbols();
187
188 static void psymtab_to_symtab_1();
189 static void add_block();
190 static void add_symbol();
191 static int  add_line();
192 static void reorder_symtabs();
193 static void reorder_psymtabs();
194 static void shrink_linetable();
195 \f
196 /* Things we export to other modules */
197
198 /* Address bounds for the signal trampoline in inferior, if any */
199 /* FIXME:  Nothing really seems to use this.  Why is it here? */
200
201 CORE_ADDR sigtramp_address, sigtramp_end;
202
203 /* The entry point (starting address) of the file, if it is an executable.  */
204
205 extern CORE_ADDR startup_file_start;    /* From blockframe.c */
206 extern CORE_ADDR startup_file_end;      /* From blockframe.c */
207
208 void
209 mipscoff_new_init()
210 {
211   /* If we have a file symbol header lying around, blow it away.  */
212   if (cur_hdr)
213     free ((char *)cur_hdr);
214   cur_hdr = 0;
215 }
216
217 void
218 mipscoff_symfile_init (sf)
219      struct sym_fns *sf;
220 {
221   sf->sym_private = NULL;
222 }
223
224 void
225 mipscoff_symfile_read(sf, addr, mainline)
226      struct sym_fns *sf;
227      CORE_ADDR addr;
228      int mainline;
229 {
230   struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
231   bfd *abfd = sf->objfile->obfd;
232   char *name = bfd_get_filename (abfd);
233   int desc;
234   register int val;
235   int symtab_offset;
236   int stringtab_offset;
237
238 /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
239    desc = fileno ((FILE *)(abfd->iostream));    /* Raw file descriptor */
240 /* End of warning */
241
242   /* Position to read the symbol table.  */
243   val = lseek (desc, (long)symtab_offset, 0);
244   if (val < 0)
245     perror_with_name (name);
246
247   init_misc_bunches ();
248   make_cleanup (discard_misc_bunches, 0);
249
250   /* Now that the executable file is positioned at symbol table,
251      process it and define symbols accordingly.  */
252
253   read_mips_symtab(sf->objfile, desc);
254
255   /* Go over the misc symbol bunches and install them in vector.  */
256
257   condense_misc_bunches (!mainline);
258 }
259   
260 /* Exported procedure: Allocate zeroed memory */
261
262 char *
263 xzalloc(size)
264 {
265         char           *p = xmalloc(size);
266
267         bzero(p, size);
268         return p;
269 }
270
271 /* Exported procedure: Builds a symtab from the PST partial one.
272    Restores the environment in effect when PST was created, delegates
273    most of the work to an ancillary procedure, and sorts
274    and reorders the symtab list at the end */
275
276 static void
277 mipscoff_psymtab_to_symtab(pst)
278         struct partial_symtab *pst;
279 {
280         struct symtab  *ret;
281         int             i;
282
283         if (!pst)
284                 return;
285
286         if (info_verbose) {
287                 printf_filtered("Reading in symbols for %s...", pst->filename);
288                 fflush(stdout);
289         }
290         /* Restore the header and list of pending typedefs */
291         cur_hdr = CUR_HDR(pst);
292
293         psymtab_to_symtab_1(pst, pst->filename);
294
295         reorder_symtabs();
296
297         if (info_verbose)
298                 printf_filtered("done.\n");
299 }
300
301 /* Exported procedure: Is PC in the signal trampoline code */
302
303 int
304 in_sigtramp(pc, name)
305         CORE_ADDR pc;
306         char *name;
307 {
308         if (sigtramp_address == 0)
309                 fixup_sigtramp();
310         return (pc >= sigtramp_address && pc < sigtramp_end);
311 }
312 \f
313 /* File-level interface functions */
314
315 /* Read the symtab information from file FSYM into memory.  Also,
316    return address just past end of our text segment in *END_OF_TEXT_SEGP.  */
317
318 static
319 read_the_mips_symtab(abfd, fsym, end_of_text_segp)
320         bfd             *abfd;
321         int             fsym;
322         CORE_ADDR       *end_of_text_segp;
323 {
324         int             stsize, st_hdrsize;
325         unsigned        st_filptr;
326         HDRR            st_hdr;
327         /* Header for executable/object file we read symbols from */
328         struct coff_exec filhdr;
329
330         /* We get here with DESC pointing to the symtab header. But we need
331          * other info from the initial headers */
332         lseek(fsym, 0L, 0);
333         myread(fsym, &filhdr, sizeof filhdr);
334
335         if (end_of_text_segp)
336                 *end_of_text_segp =
337                         bfd_h_get_32 (abfd, filhdr.a.text_start) +
338                         bfd_h_get_32 (abfd, filhdr.a.tsize);
339
340         /* Find and read the symbol table header */
341         st_hdrsize = bfd_h_get_32 (abfd, filhdr.f.f_nsyms);
342         st_filptr  = bfd_h_get_32 (abfd, filhdr.f.f_symptr);
343         if (st_filptr == 0)
344                 return 0;
345
346         lseek(fsym, st_filptr, L_SET);
347         if (st_hdrsize > sizeof (st_hdr))       /* Profanity check */
348                 abort();
349         if (read(fsym, &st_hdr, st_hdrsize) != st_hdrsize)
350                 goto readerr;
351
352         /* Find out how large the symbol table is */
353         stsize = (st_hdr.cbExtOffset - (st_filptr + st_hdrsize))
354                 + st_hdr.iextMax * cbEXTR;
355
356         /* Allocate space for the symbol table.  Read it in.  */
357         cur_hdr = (HDRR *) xmalloc(stsize + st_hdrsize);
358
359         bcopy(&st_hdr, cur_hdr, st_hdrsize);
360         if (read(fsym, (char *) cur_hdr + st_hdrsize, stsize) != stsize)
361                 goto readerr;
362
363         /* Fixup file_pointers in it */
364         fixup_symtab(cur_hdr, (char *) cur_hdr + st_hdrsize,
365                      st_filptr + st_hdrsize);
366
367         return;
368 readerr:
369         error("Short read on %s", bfd_get_filename (abfd));
370 }
371
372
373 /* Turn all file-relative pointers in the symtab described by HDR
374    into memory pointers, given that the symtab itself is located
375    at DATA in memory and F_PTR in the file. */
376
377 static
378 fixup_symtab( hdr, data, f_ptr)
379         HDRR *hdr;
380         char *data;
381 {
382         int             f_idx, s_idx;
383         FDR            *fh;
384         SYMR           *sh;
385         OPTR           *op;
386         PDR            *pr;
387         EXTR           *esh;
388
389         /*
390          * These fields are useless (and empty) by now:
391          *      hdr->cbDnOffset, hdr->cbOptOffset
392          * We use them for other internal purposes.
393          */
394         hdr->cbDnOffset = 0;
395         hdr->cbOptOffset = 0;
396
397 #define FIX(off) \
398         if (hdr->off) hdr->off = (unsigned int)data + (hdr->off - f_ptr);
399
400         FIX(cbLineOffset);
401         FIX(cbPdOffset);
402         FIX(cbSymOffset);
403         FIX(cbOptOffset);
404         FIX(cbAuxOffset);
405         FIX(cbSsOffset);
406         FIX(cbSsExtOffset);
407         FIX(cbFdOffset);
408         FIX(cbRfdOffset);
409         FIX(cbExtOffset);
410 #undef  FIX
411
412
413         /*
414          * Fix all string pointers inside the symtab, and
415          * the FDR records.  Also fix other miscellany.
416          */
417         for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
418                 register unsigned code_offset;
419
420                 /* Header itself, and strings */
421                 fh = (FDR *) (hdr->cbFdOffset) + f_idx;
422                 fh->issBase += hdr->cbSsOffset;
423                 if (fh->rss != -1)
424                         fh->rss = (long)fh->rss + fh->issBase;
425                 for (s_idx = 0; s_idx < fh->csym; s_idx++) {
426                         sh = (SYMR*)(hdr->cbSymOffset) + fh->isymBase + s_idx;
427                         sh->iss = (long) sh->iss + fh->issBase;
428                         sh->reserved = 0;
429                 }
430
431                 cur_fd = f_idx;
432
433                 /* Local symbols */
434                 fh->isymBase = (int)((SYMR*)(hdr->cbSymOffset)+fh->isymBase);
435
436                 /* cannot fix fh->ipdFirst because it is a short */
437 #define IPDFIRST(h,fh) \
438                 ((long)h->cbPdOffset + fh->ipdFirst * sizeof(PDR))
439
440                 /* Optional symbols (actually used for partial_symtabs) */
441                 fh->ioptBase = 0;
442                 fh->copt = 0;
443
444                 /* Aux symbols */
445                 if (fh->caux)
446                         fh->iauxBase = hdr->cbAuxOffset + fh->iauxBase * sizeof(AUXU);
447                 /* Relative file descriptor table */
448                 fh->rfdBase = hdr->cbRfdOffset + fh->rfdBase * sizeof(RFDT);
449
450                 /* Line numbers */
451                 if (fh->cbLine)
452                         fh->cbLineOffset += hdr->cbLineOffset;
453
454                 /* Procedure symbols.  (XXX This should be done later) */
455                 code_offset = fh->adr;
456                 for (s_idx = 0; s_idx < fh->cpd; s_idx++) {
457                         unsigned name, only_ext;
458
459                         pr = (PDR*)(IPDFIRST(hdr,fh)) + s_idx;
460
461                         /* Simple rule to find files linked "-x" */
462                         only_ext = fh->rss == -1;
463                         if (only_ext) {
464                                 if (pr->isym == -1) {
465                                         /* static function */
466                                         sh = (SYMR*)-1;
467                                 } else {
468                                         /* external */
469                                         name = hdr->cbExtOffset + pr->isym * sizeof(EXTR);
470                                         sh = &((EXTR*)name)->asym;
471                                 }
472                         } else {
473                                 /* Full symbols */
474                                 sh = (SYMR*)fh->isymBase + pr->isym;
475                                 /* Included code ? */
476                                 if (s_idx == 0 && pr->adr != 0)
477                                         code_offset -= pr->adr;
478                         }
479
480                         /* Turn index into a pointer */
481                         pr->isym = (long)sh;
482
483                         /* Fix line numbers */
484                         pr->cbLineOffset += fh->cbLineOffset;
485
486                         /* Relocate address */
487                         if (!only_ext)
488                                 pr->adr += code_offset;
489                 }               
490         }
491
492         /* External symbols: fix string */
493         for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
494                 esh = (EXTR*)(hdr->cbExtOffset) + s_idx;
495                 esh->asym.iss = esh->asym.iss + hdr->cbSsExtOffset;
496         }
497 }
498
499
500 /* Find a file descriptor given its index RF relative to a file CF */
501
502 static FDR *
503 get_rfd (cf, rf)
504         int cf, rf;
505 {
506         register FDR   *f;
507
508         f = (FDR *) (cur_hdr->cbFdOffset) + cf;
509         /* Object files do not have the RFD table, all refs are absolute */
510         if (f->rfdBase == 0)
511                 return (FDR *) (cur_hdr->cbFdOffset) + rf;
512         cf = *((pRFDT) f->rfdBase + rf);
513         return (FDR *) (cur_hdr->cbFdOffset) + cf;
514 }
515
516 /* Return a safer print NAME for a file descriptor */
517
518 static char *
519 fdr_name(name)
520         char *name;
521 {
522         if (name == (char *) -1)
523                 return "<stripped file>";
524         if (UNSAFE_DATA_ADDR(name))
525                 return "<NFY>";
526         return name;
527 }
528
529
530 /* Read in and parse the symtab of the file DESC. INCREMENTAL says
531    whether we are adding to the general symtab or not. 
532    FIXME:  INCREMENTAL is currently always zero, though it should not be. */
533
534 static
535 read_mips_symtab (objfile, desc)
536         struct objfile *objfile;
537         int desc;
538 {
539         CORE_ADDR end_of_text_seg;
540
541         read_the_mips_symtab(objfile->obfd, desc, &end_of_text_seg);
542
543         parse_partial_symbols(end_of_text_seg, objfile);
544
545         /*
546          * Check to make sure file was compiled with -g.
547          * If not, warn the user of this limitation.
548          */
549         if (compare_glevel(max_glevel, GLEVEL_2) < 0) {
550                 if (max_gdbinfo == 0)
551                         printf (
552 "\n%s not compiled with -g, debugging support is limited.\n",
553                                 objfile->name);
554                 printf(
555 "You should compile with -g2 or -g3 for best debugging support.\n");
556                 fflush(stdout);
557         }
558 }
559 \f
560 /* Local utilities */
561
562 /* Map of FDR indexes to partial symtabs */
563
564 static struct pst_map {
565         struct partial_symtab *pst;     /* the psymtab proper */
566         int n_globals;                  /* globals it exports */
567         int n_statics;                  /* statics (locals) it contains */
568 } * fdr_to_pst;
569
570
571 /* Utility stack, used to nest procedures and blocks properly.
572    It is a doubly linked list, to avoid too many alloc/free.
573    Since we might need it quite a few times it is NOT deallocated
574    after use. */
575
576 static struct parse_stack {
577         struct parse_stack      *next, *prev;
578         struct symtab           *cur_st;        /* Current symtab */
579         struct block            *cur_block;     /* Block in it */
580         int                      blocktype;     /* What are we parsing */
581         int                      maxsyms;       /* Max symbols in this block */
582         struct type             *cur_type;      /* Type we parse fields for */
583         int                      procadr;       /* Start addres of this procedure */
584         int                      numargs;       /* Its argument count */
585 } *top_stack;   /* Top stack ptr */
586
587
588 /* Enter a new lexical context */
589
590 static push_parse_stack()
591 {
592         struct parse_stack *new;
593
594         /* Reuse frames if possible */
595         if (top_stack && top_stack->prev)
596                 new = top_stack->prev;
597         else
598                 new = (struct parse_stack *) xzalloc(sizeof(struct parse_stack));
599         /* Initialize new frame with previous content */
600         if (top_stack) {
601                 register struct parse_stack *prev = new->prev;
602
603                 *new = *top_stack;
604                 top_stack->prev = new;
605                 new->prev = prev;
606                 new->next = top_stack;
607         }
608         top_stack = new;
609 }
610
611 /* Exit a lexical context */
612
613 static pop_parse_stack()
614 {
615         if (!top_stack)
616                 return;
617         if (top_stack->next)
618                 top_stack = top_stack->next;
619 }
620
621
622 /* Cross-references might be to things we haven't looked at
623    yet, e.g. type references.  To avoid too many type
624    duplications we keep a quick fixup table, an array
625    of lists of references indexed by file descriptor */
626
627 static struct pending {
628         struct pending  *next;          /* link */
629         SYMR            *s;             /* the symbol */
630         struct type     *t;             /* its partial type descriptor */
631 } **pending_list;
632
633
634 /* Check whether we already saw symbol SH in file FH as undefined */
635
636 static
637 struct pending *is_pending_symbol(fh, sh)
638         FDR *fh;
639         SYMR *sh;
640 {
641         int             f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
642         register struct pending *p;
643
644         /* Linear search is ok, list is typically no more than 10 deep */
645         for (p = pending_list[f_idx]; p; p = p->next)
646                 if (p->s == sh)
647                         break;
648         return p;
649 }
650
651 /* Check whether we already saw type T in file FH as undefined */
652
653 static
654 struct pending *is_pending_type(fh, t)
655         FDR *fh;
656         struct type *t;
657 {
658         int             f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
659         register struct pending *p;
660
661         for (p = pending_list[f_idx]; p; p = p->next)
662                 if (p->t == t)
663                         break;
664         return p;
665 }
666
667 /* Add a new undef symbol SH of type T */
668
669 static
670 add_pending(fh, sh, t)
671         FDR *fh;
672         SYMR *sh;
673         struct type *t;
674 {
675         int             f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
676         struct pending *p = is_pending_symbol(fh, sh);
677
678         /* Make sure we do not make duplicates */
679         if (!p) {
680                 p = (struct pending *) xmalloc(sizeof(*p));
681                 p->s = sh;
682                 p->t = t;
683                 p->next = pending_list[f_idx];
684                 pending_list[f_idx] = p;
685         }
686         sh->reserved = 1;       /* for quick check */
687 }
688
689 /* Throw away undef entries when done with file index F_IDX */
690
691 static
692 free_pending(f_idx)
693 {
694         register struct pending *p, *q;
695
696         for (p = pending_list[f_idx]; p; p = q) {
697                 q = p->next;
698                 free(p);
699         }
700         pending_list[f_idx] = 0;
701 }
702
703 /* The number of args to a procedure is not explicit in the symtab,
704    this is the list of all those we know of.
705    This makes parsing more reasonable and avoids extra passes */
706
707 static struct numarg {
708         struct numarg   *next;          /* link */
709         unsigned         adr;           /* procedure's start address */
710         unsigned         num;           /* arg count */
711 } *numargs_list;
712
713 /* Record that the procedure at ADR takes NUM arguments. */
714
715 static
716 got_numargs(adr,num)
717 {
718         struct numarg  *n = (struct numarg *) xmalloc(sizeof(struct numarg));
719
720         n->adr = adr;
721         n->num = num;
722         n->next = numargs_list;
723         numargs_list = n;
724 }
725
726 /* See if we know how many arguments the procedure at ADR takes */
727
728 static
729 lookup_numargs(adr)
730 {
731         struct numarg  *n = numargs_list;
732
733         while (n && n->adr != adr)
734                 n = n->next;
735         return (n) ? n->num : -1;
736 }
737
738 /* Release storage when done with this file */
739
740 static void
741 free_numargs()
742 {
743         struct numarg  *n = numargs_list, *m;
744
745         while (n) {
746                 m = n->next;
747                 free(n);
748                 n = m;
749         }
750         numargs_list = 0;
751 }
752
753 \f
754 /* Parsing Routines proper. */
755
756 /* Parse a single symbol. Mostly just make up a GDB symbol for it.
757    For blocks, procedures and types we open a new lexical context.
758    This is basically just a big switch on the symbol's type */
759
760 static void
761 parse_symbol(sh, ax)
762         SYMR *sh;
763         AUXU *ax;
764 {
765         struct symbol  *s;
766         struct block   *b;
767         struct type    *t;
768         struct field   *f;
769         /* When a symbol is cross-referenced from other files/symbols
770            we mark it explicitly */
771         int             pend = (sh->reserved == 1);
772         enum address_class class;
773
774         switch (sh->st) {
775
776             case stNil:
777                 break;
778
779             case stGlobal:      /* external symbol, goes into global block */
780                 class = LOC_STATIC;
781                 b = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st),
782                                       GLOBAL_BLOCK);
783                 s = new_symbol(sh->iss);
784                 SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
785                 goto data;
786
787             case stStatic:      /* static data, goes into current block. */
788                 class = LOC_STATIC;
789                 b = top_stack->cur_block;
790                 s = new_symbol(sh->iss);
791                 SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
792                 goto data;
793
794             case stLocal:       /* local variable, goes into current block */
795                 if (sh->sc == scRegister) {
796                         class = LOC_REGISTER;
797                         if (sh->value > 31)
798                                 sh->value += 6;
799                 } else
800                         class = LOC_LOCAL;
801                 b = top_stack->cur_block;
802                 s = new_symbol(sh->iss);
803                 SYMBOL_VALUE(s) = sh->value;
804
805 data:           /* Common code for symbols describing data */
806                 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
807                 SYMBOL_CLASS(s) = class;
808                 add_symbol(s, b);
809
810                 /* Type could be missing in a number of cases */
811                 if (sh->sc == scUndefined || sh->sc == scNil ||
812                     sh->index == 0xfffff)
813                         SYMBOL_TYPE(s) = builtin_type_int;      /* undefined? */
814                 else
815                         SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
816                 /* Value of a data symbol is its memory address */
817                 break;
818
819             case stParam:       /* arg to procedure, goes into current block */
820                 max_gdbinfo++;
821                 top_stack->numargs++;
822                 s = new_symbol(sh->iss);
823                 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
824                 if (sh->sc == scRegister) {
825                         SYMBOL_CLASS(s) = LOC_REGPARM;
826                         if (sh->value > 31)
827                                 sh->value += 6;
828                 } else
829                         SYMBOL_CLASS(s) = LOC_ARG;
830                 SYMBOL_VALUE(s) = sh->value;
831                 SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
832                 add_symbol(s, top_stack->cur_block);
833 #if 0
834                 /* FIXME:  This has not been tested.  See dbxread.c */
835                 /* Add the type of this parameter to the function/procedure
836                    type of this block. */
837                 add_param_to_type(&top_stack->cur_block->function->type,s);
838 #endif
839                 break;
840
841             case stLabel:       /* label, goes into current block */
842                 s = new_symbol(sh->iss);
843                 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;    /* so that it can be used */
844                 SYMBOL_CLASS(s) = LOC_LABEL;            /* but not misused */
845                 SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
846                 SYMBOL_TYPE(s) = builtin_type_int;
847                 add_symbol(s, top_stack->cur_block);
848                 break;
849
850             case stProc:        /* Procedure, usually goes into global block */
851             case stStaticProc:  /* Static procedure, goes into current block */
852                 s = new_symbol(sh->iss);
853                 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
854                 SYMBOL_CLASS(s) = LOC_BLOCK;
855                 /* Type of the return value */
856                 if (sh->sc == scUndefined || sh->sc == scNil)
857                         t = builtin_type_int;
858                 else
859                         t = parse_type(ax + sh->index, sh, 0);
860                 b = top_stack->cur_block;
861                 if (sh->st == stProc) {
862                     struct blockvector *bv = BLOCKVECTOR(top_stack->cur_st);
863                     /* The next test should normally be true,
864                        but provides a hook for nested functions
865                        (which we don't want to make global). */
866                     if (b == BLOCKVECTOR_BLOCK(bv, STATIC_BLOCK))
867                         b = BLOCKVECTOR_BLOCK(bv, GLOBAL_BLOCK);
868                 }
869                 add_symbol(s, b);
870
871                 /* Make a type for the procedure itself */
872 #if 0
873                 /* FIXME:  This has not been tested yet!  See dbxread.c */
874                 /* Generate a template for the type of this function.  The 
875                    types of the arguments will be added as we read the symbol 
876                    table. */
877                 bcopy(SYMBOL_TYPE(s),lookup_function_type(t),sizeof(struct type));
878 #else
879                 SYMBOL_TYPE(s) = lookup_function_type (t);
880 #endif
881
882                 /* Create and enter a new lexical context */
883                 b = new_block(top_stack->maxsyms);
884                 SYMBOL_BLOCK_VALUE(s) = b;
885                 BLOCK_FUNCTION(b) = s;
886                 BLOCK_START(b) = BLOCK_END(b) = sh->value;
887                 BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
888                 add_block(b, top_stack->cur_st);
889
890                 /* Not if we only have partial info */
891                 if (sh->sc == scUndefined || sh->sc == scNil)
892                         break;
893
894                 push_parse_stack();
895                 top_stack->cur_block = b;
896                 top_stack->blocktype = sh->st;
897                 top_stack->cur_type = SYMBOL_TYPE(s);
898                 top_stack->procadr = sh->value;
899                 top_stack->numargs = 0;
900
901                 sh->value = (long) SYMBOL_TYPE(s);
902                 break;
903
904             case stBlock:       /* Either a lexical block, or some type */
905                 push_parse_stack();
906                 top_stack->blocktype = stBlock;
907                 if (sh->sc == scInfo) { /* structure/union/enum def */
908                         s = new_symbol(sh->iss);
909                         SYMBOL_NAMESPACE(s) = STRUCT_NAMESPACE;
910                         SYMBOL_CLASS(s) = LOC_TYPEDEF;
911                         SYMBOL_VALUE(s) = 0;
912                         add_symbol(s, top_stack->cur_block);
913                         /* If this type was expected, use its partial definition */
914                         if (pend) {
915                                 t = is_pending_symbol(cur_fdr, sh)->t;
916                         } else {
917                                 /* Uhmm, can`t decide yet. Smash later */
918                                 t = new_type(sh->iss);
919                                 TYPE_CODE(t) = TYPE_CODE_UNDEF;
920                                 add_pending(cur_fdr, sh, t);
921                         }
922                         SYMBOL_TYPE(s) = t;
923                         /* make this the current type */
924                         top_stack->cur_type = t;
925                         TYPE_LENGTH(t) = sh->value;
926                         /* Mark that symbol has a type, and say which one */
927                         sh->value = (long) t;
928                 } else {
929                         /* beginnning of (code) block. Value of symbol
930                            is the displacement from procedure start */
931                         b = new_block(top_stack->maxsyms);
932                         BLOCK_START(b) = sh->value + top_stack->procadr;
933                         BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
934                         top_stack->cur_block = b;
935                         add_block(b, top_stack->cur_st);
936                 }
937                 break;
938
939             case stEnd:         /* end (of anything) */
940                 if (sh->sc == scInfo) {
941                         /* Finished with type */
942                         top_stack->cur_type = 0;
943                 } else if (sh->sc == scText &&
944                            (top_stack->blocktype == stProc ||
945                             top_stack->blocktype == stStaticProc)) {
946                     /* Finished with procedure */
947                     struct blockvector *bv = BLOCKVECTOR(top_stack->cur_st);
948                     struct block *b;
949                     int i;
950
951                     BLOCK_END(top_stack->cur_block) += sh->value; /* size */
952                     got_numargs(top_stack->procadr, top_stack->numargs);
953                     /* Reallocate symbols, saving memory */
954                     b = shrink_block(top_stack->cur_block, top_stack->cur_st);
955
956                     /* f77 emits proc-level with address bounds==[0,0],
957                        So look for such child blocks, and patch them.  */
958                     for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++) {
959                         struct block *b_bad = BLOCKVECTOR_BLOCK(bv,i);
960                         if (BLOCK_SUPERBLOCK(b_bad) == b
961                          && BLOCK_START(b_bad) == top_stack->procadr
962                          && BLOCK_END(b_bad) == top_stack->procadr) {
963                             BLOCK_START(b_bad) = BLOCK_START(b);
964                             BLOCK_END(b_bad) = BLOCK_END(b);
965                         }
966                     }
967                     if (entry_point < BLOCK_END(b)
968                       && entry_point >= BLOCK_START(b)) {
969                         startup_file_start = BLOCK_START(b);
970                         startup_file_end = BLOCK_END(b);
971                     }
972                 } else if (sh->sc == scText && top_stack->blocktype == stBlock) {
973                         /* End of (code) block. The value of the symbol
974                            is the displacement from the procedure`s start
975                            address of the end of this block. */
976                         BLOCK_END(top_stack->cur_block) = sh->value + top_stack->procadr;
977                         (void) shrink_block(top_stack->cur_block, top_stack->cur_st);
978                 }
979                 pop_parse_stack();      /* restore previous lexical context */
980                 break;
981
982             case stMember:      /* member of struct/union/enum.. */
983                 f = new_field(top_stack->cur_type, sh->iss);
984                 f->bitpos = sh->value;
985                 f->type = parse_type(ax + sh->index, sh, &f->bitsize);
986                 break;
987
988             case stTypedef:     /* type definition */
989                 s = new_symbol(sh->iss);
990                 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
991                 SYMBOL_CLASS(s) = LOC_TYPEDEF;
992                 SYMBOL_BLOCK_VALUE(s) = top_stack->cur_block;
993                 add_symbol(s, top_stack->cur_block);                    
994                 SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
995                 sh->value = (long) SYMBOL_TYPE(s);
996                 break;
997
998             case stFile:        /* file name */
999                 push_parse_stack();
1000                 top_stack->blocktype = sh->st;
1001                 break;
1002
1003                 /* I`ve never seen these for C */
1004             case stRegReloc:
1005                 break;          /* register relocation */
1006             case stForward:
1007                 break;          /* forwarding address */
1008             case stConstant:
1009                 break;          /* constant */
1010             default:
1011                 error("Unknown symbol type %x.", sh->st);
1012         }
1013         sh->st = stParsed;
1014 }
1015
1016 /* Parse the type information provided in the AX entries for
1017    the symbol SH. Return the bitfield size in BS, in case. */
1018
1019 static struct type *parse_type(ax, sh, bs)
1020         AUXU    *ax;
1021         SYMR    *sh;
1022         int     *bs;
1023 {
1024         /* Null entries in this map are treated specially */
1025         static struct type **map_bt[] =
1026         {
1027                  &builtin_type_void,            /* btNil */
1028                  0,                             /* btAdr */
1029                  &builtin_type_char,            /* btChar */
1030                  &builtin_type_unsigned_char,   /* btUChar */
1031                  &builtin_type_short,           /* btShort */
1032                  &builtin_type_unsigned_short,  /* btUShort */
1033                  &builtin_type_int,             /* btInt */
1034                  &builtin_type_unsigned_int,    /* btUInt */
1035                  &builtin_type_long,            /* btLong */
1036                  &builtin_type_unsigned_long,   /* btULong */
1037                  &builtin_type_float,           /* btFloat */
1038                  &builtin_type_double,          /* btDouble */
1039                  0,                             /* btStruct */
1040                  0,                             /* btUnion */
1041                  0,                             /* btEnum */
1042                  0,                             /* btTypedef */
1043                  0,                             /* btRange */
1044                  0,                             /* btSet */
1045                  &builtin_type_complex,         /* btComplex */
1046                  &builtin_type_double_complex,  /* btDComplex */
1047                  0,                             /* btIndirect */
1048                  &builtin_type_fixed_dec,       /* btFixedDec */
1049                  &builtin_type_float_dec,       /* btFloatDec */
1050                  &builtin_type_string,          /* btString */
1051                  0,                             /* btBit */
1052                  0,                             /* btPicture */
1053                  &builtin_type_void,            /* btVoid */
1054         };
1055
1056         TIR            *t;
1057         struct type    *tp = 0, *tp1;
1058         char           *fmt;
1059         int             i;
1060
1061         /* Procedures start off by one */
1062         if (sh->st == stProc || sh->st == stStaticProc)
1063                 ax++;
1064
1065         /* Undefined ? Should not happen */
1066         if (ax->rndx.rfd == 0xfff) {
1067                 return builtin_type_void;
1068         }
1069
1070         /* Use aux as a type information record, map its basic type */
1071         t = &ax->ti;
1072         if (t->bt > (sizeof (map_bt)/sizeof (*map_bt))) {
1073                 complain (&basic_type_complaint, t->bt);
1074                 return builtin_type_int;
1075         }
1076         if (map_bt[t->bt]) {
1077                 tp = *map_bt[t->bt];
1078                 fmt = "%s";
1079         } else {
1080                 /* Cannot use builtin types -- build our own */
1081                 switch (t->bt) {
1082                     case btAdr:
1083                         tp = lookup_pointer_type (builtin_type_void);
1084                         fmt = "%s";
1085                         break;
1086                     case btStruct:
1087                         tp = make_struct_type(TYPE_CODE_STRUCT, 0, 0, 0);
1088                         fmt = "struct %s";
1089                         break;
1090                     case btUnion:
1091                         tp = make_struct_type(TYPE_CODE_UNION, 0, 0, 0);
1092                         fmt = "union %s";
1093                         break;
1094                     case btEnum:
1095                         tp = make_type(TYPE_CODE_ENUM, 0, 0, 0);
1096                         fmt = "enum %s";
1097                         break;
1098                     case btRange:
1099                         tp = make_type(TYPE_CODE_RANGE, 0, 0, 0);
1100                         fmt = "%s";
1101                         break;
1102                     case btSet:
1103                         tp = make_type(TYPE_CODE_SET, 0, 0, 0);
1104                         fmt = "set %s";
1105                         break;
1106                     default:
1107                         complain (&basic_type_complaint, t->bt);
1108                         return builtin_type_int;
1109                 }
1110         }
1111
1112         /* Move on to next aux */
1113         ax++;
1114         if (t->continued) {
1115                 /* This is the way it would work if the compiler worked */
1116                 register TIR *t1 = t;
1117                 while (t1->continued)
1118                         ax++;
1119         }
1120
1121         /* For bitfields all we need is the width */
1122         if (t->fBitfield) {
1123                 *bs = ax->width;
1124                 return tp;
1125         }
1126
1127         /* All these types really point to some (common) MIPS type
1128            definition, and only the type-qualifiers fully identify
1129            them.  We`ll make the same effort at sharing */
1130         if (t->bt == btIndirect ||
1131             t->bt == btStruct ||
1132             t->bt == btUnion ||
1133             t->bt == btEnum ||
1134             t->bt == btTypedef ||
1135             t->bt == btRange ||
1136             t->bt == btSet) {
1137                 char            name[256], *pn;
1138
1139                 /* Try to cross reference this type */
1140                 tp1 = tp;
1141                 ax += cross_ref(ax, &tp1, &pn);
1142                 /* SOMEONE OUGHT TO FIX DBXREAD TO DROP "STRUCT" */
1143                 sprintf(name, fmt, pn);
1144
1145                 /* reading .o file ? */
1146                 if (UNSAFE_DATA_ADDR(tp1))
1147                         tp1 = tp;
1148                 if (TYPE_CODE(tp1) == TYPE_CODE_UNDEF) {
1149                         /*
1150                          * Type was incompletely defined, now we know.
1151                          */
1152                         TYPE_CODE(tp1) = TYPE_CODE(tp);
1153                         TYPE_NAME(tp1) = obsavestring(name, strlen(name));
1154                         TYPE_TYPE_SPECIFIC(tp1) = TYPE_TYPE_SPECIFIC(tp);
1155
1156                         /* Now do cleanup based on the final type.  */
1157                         switch (TYPE_CODE (tp1)) {
1158                         case TYPE_CODE_ENUM:
1159                                 for (i = 0; i < TYPE_NFIELDS(tp1); i++)
1160                                         make_enum_constant(&TYPE_FIELD(tp1,i),
1161                                                            tp1);
1162                                 break;
1163                         }
1164                 }
1165                 if (tp1 != tp) {
1166                         /* found as cross ref, rid of our template */
1167                         if ((TYPE_FLAGS(tp) & TYPE_FLAG_PERM) == 0)
1168                                 free(tp);
1169                         tp = tp1;
1170                         /* stupid idea of prepending "struct" to type names */
1171                         if (t->bt == btStruct && !index(TYPE_NAME(tp), ' ')) {
1172                                 sprintf(name, fmt, TYPE_NAME(tp));
1173                                 TYPE_NAME(tp) = obsavestring(name, strlen(name));
1174                         }
1175                 } else
1176                         TYPE_NAME(tp) = savestring(name, strlen(name));
1177         }
1178
1179         /* Deal with range types */
1180         if (t->bt == btRange) {
1181                 struct field   *f;
1182
1183                 f = new_field(tp, "Low");
1184                 f->bitpos = ax->dnLow;
1185                 ax++;
1186                 f = new_field(tp, "High");
1187                 f->bitpos = ax->dnHigh;
1188                 ax++;
1189         }
1190
1191         /* Parse all the type qualifiers now. If there are more
1192            than 6 the game will continue in the next aux */
1193
1194 #define PARSE_TQ(tq) \
1195         if (t->tq != tqNil) ax += upgrade_type(&tp, t->tq, ax, sh);
1196
1197 again:  PARSE_TQ(tq0);
1198         PARSE_TQ(tq1);
1199         PARSE_TQ(tq2);
1200         PARSE_TQ(tq3);
1201         PARSE_TQ(tq4);
1202         PARSE_TQ(tq5);
1203 #undef  PARSE_TQ
1204
1205         if (t->continued) {
1206                 t++;
1207                 goto again;
1208         }
1209         return tp;
1210 }
1211
1212 /* Make up a complex type from a basic one.  Type is passed by
1213    reference in TPP and side-effected as necessary. The type
1214    qualifier TQ says how to handle the aux symbols at AX for
1215    the symbol SX we are currently analyzing.
1216    Returns the number of aux symbols we parsed. */
1217
1218 static int
1219 upgrade_type(tpp, tq, ax, sh)
1220         struct type   **tpp;
1221         AUXU           *ax;
1222         SYMR           *sh;
1223 {
1224         int             off;
1225         struct type    *t;
1226
1227         /* Used in array processing */
1228         int             rf, id;
1229         FDR            *fh;
1230         struct field   *f;
1231         SYMR            ss;
1232         int             lower, upper;
1233
1234         switch (tq) {
1235         case tqPtr:
1236                 t = lookup_pointer_type (*tpp);
1237                 *tpp = t;
1238                 return 0;
1239
1240         case tqProc:
1241                 t = lookup_function_type (*tpp);
1242                 *tpp = t;
1243                 return 0;
1244
1245         case tqArray:
1246                 off = 0;
1247                 t = make_type(TYPE_CODE_ARRAY, 0, 0, 0);
1248                 TYPE_TARGET_TYPE(t) = *tpp;
1249
1250                 /* Determine and record the domain type (type of index) */
1251                 id = ax->rndx.index;
1252                 rf = ax->rndx.rfd;
1253                 if (rf == 0xfff) {
1254                         rf = (++ax)->isym;
1255                         off++;
1256                 }
1257                 fh = get_rfd(cur_fd, rf);
1258                 f = new_field(t, (char *)0);
1259                 bzero(&ss, sizeof ss);
1260 /* XXX */       f->type = parse_type(fh->iauxBase + id * sizeof(AUXU),
1261                                      &ss, &f->bitsize);
1262
1263                 if (off == 0) {
1264                 /*
1265                  * This seems to be a pointer to the end of the Block defining
1266                  * the type.  Why it is here is magic for me, and I have no
1267                  * good use for it anyways.
1268                  */
1269                         /* This used to occur because cross_ref returned
1270                            the wrong result (ax pointed wrong).  FIXME,
1271                            delete this code in a while.  -- gnu@cygnus jul91 */
1272                         complain (&array_parse_complaint, 0);
1273                         off++;
1274                         id = (++ax)->rndx.index;
1275                         if ((rf = ax->rndx.rfd) == 0xfff)
1276                                 rf = (++ax)->isym, off++;
1277                 }
1278                 lower = (++ax)->dnLow;
1279                 upper = (++ax)->dnHigh;
1280                 rf = (++ax)->width;     /* bit size of array element */
1281
1282                 /* Check whether supplied array element bit size matches
1283                    the known size of the element type.  If this complaint
1284                    ends up not happening, we can remove this code.  It's
1285                    here because we aren't sure we understand this *&%&$
1286                    symbol format.  */
1287                 id = TYPE_LENGTH(TYPE_TARGET_TYPE(t)) << 3; /* bitsize */
1288                 if (id == 0) {
1289                         /* Most likely an undefined type */
1290                         id = rf;
1291                         TYPE_LENGTH(TYPE_TARGET_TYPE(t)) = id >> 3;
1292                 }
1293                 if (id != rf)
1294                         complain (&array_bitsize_complaint, rf);
1295
1296                 TYPE_LENGTH(t) = (upper < 0) ? 0 :
1297                         (upper - lower + 1) * (rf >> 3);
1298                 *tpp = t;
1299                 return 4 + off;
1300
1301         case tqVol:
1302                 /* Volatile -- currently ignored */
1303                 return 0;
1304
1305         default:
1306                 complain (&unknown_type_qual_complaint, tq);
1307                 return 0;
1308         }
1309 }
1310
1311
1312 /* Parse a procedure descriptor record PR.  Note that the procedure
1313    is parsed _after_ the local symbols, now we just make up the
1314    extra information we need into a special symbol that we insert
1315    in the procedure's main block.  Note also that images that
1316    have been partially stripped (ld -x) have been deprived
1317    of local symbols, and we have to cope with them here.
1318    The procedure's code ends at BOUND */
1319
1320 static
1321 parse_procedure(pr, bound)
1322         PDR *pr;
1323 {
1324         struct symbol *s, *i;
1325         SYMR *sh = (SYMR*)pr->isym;
1326         struct block *b;
1327         struct mips_extra_func_info *e;
1328         char name[100];
1329         char *sh_name;
1330
1331         /* Reuse the MIPS record */
1332         e = (struct mips_extra_func_info *) pr;
1333         e->numargs = lookup_numargs(pr->adr);
1334
1335         /* Make up our special symbol */
1336         i = new_symbol(".gdbinfo.");
1337         SYMBOL_VALUE(i) = (int)e;
1338         SYMBOL_NAMESPACE(i) = LABEL_NAMESPACE;
1339         SYMBOL_CLASS(i) = LOC_CONST;
1340         SYMBOL_TYPE(i) = builtin_type_void;
1341
1342         /* Make up a name for static procedures. Sigh. */
1343         if (sh == (SYMR*)-1) {
1344             sprintf(name,".static_procedure@%x",pr->adr);
1345             sh_name = savestring(name, strlen(name));
1346             s = NULL;
1347         }
1348         else {
1349             sh_name = (char*)sh->iss;
1350             s = mylookup_symbol(sh_name, top_stack->cur_block,
1351                                 VAR_NAMESPACE, LOC_BLOCK);
1352         }
1353         if (s != 0) {
1354                 b = SYMBOL_BLOCK_VALUE(s);
1355         } else {
1356                 s = new_symbol(sh_name);
1357                 SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
1358                 SYMBOL_CLASS(s) = LOC_BLOCK;
1359                 /* Donno its type, hope int is ok */
1360                 SYMBOL_TYPE(s) = lookup_function_type (builtin_type_int);
1361                 add_symbol(s, top_stack->cur_block);
1362                 /* Wont have symbols for this one */
1363                 b = new_block(2);
1364                 SYMBOL_BLOCK_VALUE(s) = b;
1365                 BLOCK_FUNCTION(b) = s;
1366                 BLOCK_START(b) = pr->adr;
1367                 BLOCK_END(b) = bound;
1368                 BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
1369                 add_block(b, top_stack->cur_st);
1370         }
1371         e->isym = (long)s;
1372         add_symbol(i,b);
1373 }
1374
1375 /* Parse the external symbol ES. Just call parse_symbol() after
1376    making sure we know where the aux are for it. For procedures,
1377    parsing of the PDRs has already provided all the needed
1378    information, we only parse them if SKIP_PROCEDURES is false,
1379    and only if this causes no symbol duplication.
1380
1381    This routine clobbers top_stack->cur_block and ->cur_st. */
1382
1383 static
1384 parse_external(es, skip_procedures)
1385         EXTR *es;
1386 {
1387         AUXU *ax;
1388
1389         if (es->ifd != ifdNil) {
1390                 cur_fd = es->ifd;
1391                 cur_fdr = (FDR*)(cur_hdr->cbFdOffset) + cur_fd;
1392                 ax = (AUXU*)cur_fdr->iauxBase;
1393         } else {
1394                 cur_fdr = (FDR*)(cur_hdr->cbFdOffset);
1395                 ax = 0;
1396         }
1397         top_stack->cur_st = cur_stab;
1398         top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st),
1399                                                  GLOBAL_BLOCK);
1400
1401         /* Reading .o files */
1402         if (es->asym.sc == scUndefined || es->asym.sc == scNil) {
1403                 char *what;
1404                 switch (es->asym.st) {
1405                 case stStaticProc:
1406                 case stProc:    what = "procedure"; n_undef_procs++;  break;
1407                 case stGlobal:  what = "variable";  n_undef_vars++;   break;
1408                 case stLabel:   what = "label";     n_undef_labels++; break;
1409                 default :       what = "symbol";                      break;
1410                 }
1411                 n_undef_symbols++;
1412                 if (info_verbose)
1413                         printf_filtered("Warning: %s `%s' is undefined (in %s)\n", what,
1414                                 es->asym.iss, fdr_name(cur_fdr->rss));
1415                 return;
1416         }
1417
1418         switch (es->asym.st) {
1419         case stProc:
1420                 /* If we have full symbols we do not need more */
1421                 if (skip_procedures)
1422                         return;
1423                 if (mylookup_symbol (es->asym.iss, top_stack->cur_block,
1424                                         VAR_NAMESPACE, LOC_BLOCK))
1425                         break;
1426                 /* fall through */
1427         case stGlobal:
1428         case stLabel:
1429                 /*
1430                  * Note that the case of a symbol with indexNil
1431                  * must be handled anyways by parse_symbol().
1432                  */
1433                 parse_symbol(&es->asym, ax);
1434                 break;
1435         default:
1436                 break;
1437         }
1438 }
1439
1440 /* Parse the line number info for file descriptor FH into
1441    GDB's linetable LT.  MIPS' encoding requires a little bit
1442    of magic to get things out.  Note also that MIPS' line
1443    numbers can go back and forth, apparently we can live
1444    with that and do not need to reorder our linetables */
1445
1446 static
1447 parse_lines(fh, lt)
1448         FDR *fh;
1449         struct linetable *lt;
1450 {
1451         unsigned char *base = (unsigned char*)fh->cbLineOffset;
1452         int i, j, k;
1453         int delta, count, lineno = 0;
1454         PDR *pr;
1455
1456         if (base == 0)
1457                 return;
1458
1459         /* Scan by procedure descriptors */
1460         i = 0; j = 0, k = 0;
1461         for (pr = (PDR*)IPDFIRST(cur_hdr,fh); j < fh->cpd; j++, pr++) {
1462                 int l, halt;
1463
1464                 /* No code for this one */
1465                 if (pr->iline == ilineNil ||
1466                     pr->lnLow == -1 || pr->lnHigh == -1)
1467                         continue;
1468                 /*
1469                  *      Aurgh! To know where to stop expanding we
1470                  *      must look-ahead.
1471                  */
1472                 for (l = 1; l < (fh->cpd - j); l++)
1473                         if (pr[l].iline != -1)
1474                                 break;
1475                 if (l == (fh->cpd - j))
1476                         halt = fh->cline;
1477                 else
1478                         halt = pr[l].iline;
1479                 /*
1480                  * When procedures are moved around the linenumbers
1481                  * are attributed to the next procedure up
1482                  */
1483                 if (pr->iline >= halt) continue;
1484
1485                 base = (unsigned char*)pr->cbLineOffset;
1486                 l = pr->adr >> 2;       /* in words */
1487                 halt += (pr->adr >> 2) - pr->iline;
1488                 for (lineno = pr->lnLow; l < halt;) {
1489                         count = *base & 0x0f;
1490                         delta = *base++ >> 4;
1491                         if (delta >= 8)
1492                                 delta -= 16;
1493                         if (delta == -8) {
1494                                 delta = (base[0] << 8) | base[1];
1495                                 if (delta >= 0x8000)
1496                                         delta -= 0x10000;
1497                                 base += 2;
1498                         }
1499                         lineno += delta;/* first delta is 0 */
1500                         k = add_line(lt, lineno, l, k);
1501                         l += count + 1;
1502                 }
1503         }
1504 }
1505
1506
1507 /* Parse the symbols of the file described by FH, whose index is F_IDX.
1508    BOUND is the highest core address of this file's procedures */
1509
1510 static
1511 parse_one_file(fh, f_idx, bound)
1512         FDR *fh;
1513 {
1514         register int s_idx;
1515         SYMR *sh;
1516         PDR *pr;
1517
1518         /* Parse local symbols first */
1519
1520         for (s_idx = 0; s_idx < fh->csym; s_idx++) {
1521                 sh = (SYMR *) (fh->isymBase) + s_idx;
1522                 cur_sdx = s_idx;
1523                 parse_symbol(sh, fh->iauxBase);
1524         }
1525
1526         /* Procedures next, note we need to look-ahead to
1527            find out where the procedure's code ends */
1528
1529         for (s_idx = 0; s_idx < fh->cpd-1; s_idx++) {
1530                 pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx;
1531                 parse_procedure(pr, pr[1].adr); /* next proc up */
1532         }
1533         if (fh->cpd) {
1534                 pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx;
1535                 parse_procedure(pr, bound);     /* next file up */
1536         }
1537
1538         /* Linenumbers. At the end, check if we can save memory */
1539         parse_lines(fh, LINETABLE(cur_stab));
1540         if (LINETABLE(cur_stab)->nitems < fh->cline)
1541                 shrink_linetable(cur_stab);
1542 }
1543 \f
1544 /* Master parsing procedure for first-pass reading of file symbols
1545    into a partial_symtab.
1546
1547    Parses the symtab described by the global symbolic header CUR_HDR.
1548    END_OF_TEXT_SEG gives the address just after the text segment for
1549    the symtab we are reading.  */
1550
1551 static
1552 parse_partial_symbols(end_of_text_seg, objfile)
1553         int end_of_text_seg;
1554         struct objfile *objfile;
1555 {
1556         int             f_idx, s_idx, h_max, stat_idx;
1557         HDRR            *hdr;
1558         /* Running pointers */
1559         FDR             *fh;
1560         RFDT            *rh;
1561         register EXTR   *esh;
1562         register SYMR   *sh;
1563         struct partial_symtab *pst;
1564
1565         /*
1566          * Big plan: 
1567          *
1568          * Only parse the Local and External symbols, and the Relative FDR.
1569          * Fixup enough of the loader symtab to be able to use it.
1570          * Allocate space only for the file's portions we need to
1571          * look at. (XXX)
1572          */
1573
1574         hdr = cur_hdr;
1575         max_gdbinfo = 0;
1576         max_glevel = MIN_GLEVEL;
1577
1578         /* Allocate the map FDR -> PST.
1579            Minor hack: -O3 images might claim some global data belongs
1580            to FDR -1. We`ll go along with that */
1581         fdr_to_pst = (struct pst_map *)xzalloc((hdr->ifdMax+1) * sizeof *fdr_to_pst);
1582         fdr_to_pst++;
1583         {
1584                 struct partial_symtab * pst = new_psymtab("", objfile);
1585                 fdr_to_pst[-1].pst = pst;
1586                 FDR_IDX(pst) = -1;
1587         }
1588
1589         /* Now scan the FDRs, mostly for dependencies */
1590         for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++)
1591                 (void) parse_fdr(f_idx, 1, objfile);
1592
1593         /* Take a good guess at how many symbols we might ever need */
1594         h_max = hdr->iextMax;
1595
1596         /* Parse externals: two passes because they can be ordered
1597            in any way, but gdb likes to have them segregated by their
1598            source file.  */
1599
1600         /* Pass 1 over external syms: Presize and partition the list */
1601         for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
1602                 esh = (EXTR *) (hdr->cbExtOffset) + s_idx;
1603                 fdr_to_pst[esh->ifd].n_globals++;
1604         }
1605
1606         if (global_psymbols.list) {
1607                 int origsize = global_psymbols.next - global_psymbols.list;
1608
1609                 global_psymbols.list = (struct partial_symbol *)
1610                         xrealloc (global_psymbols.list,
1611                            (h_max + origsize) * sizeof(struct partial_symbol));
1612                 global_psymbols.next = global_psymbols.list + origsize;
1613                 global_psymbols.size = h_max + origsize;
1614         } else {
1615                 global_psymbols.list = (struct partial_symbol *)
1616                                 xmalloc (h_max * sizeof(struct partial_symbol));
1617                 global_psymbols.next = global_psymbols.list;
1618                 global_psymbols.size = h_max;
1619         }
1620
1621         /* Pass 1.5 over files:  partition out global symbol space */
1622         s_idx    = global_psymbols.next - global_psymbols.list;
1623         for (f_idx = -1; f_idx < hdr->ifdMax; f_idx++) {
1624                 fdr_to_pst[f_idx].pst->globals_offset = s_idx;
1625                 s_idx += fdr_to_pst[f_idx].n_globals;
1626         }
1627
1628         /* Pass 1.6 over files:  partition out static symbol space.
1629            Note that this loop starts at 0, not at -1. */
1630         stat_idx = static_psymbols.next - static_psymbols.list;
1631         for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
1632                 fdr_to_pst[f_idx].pst->statics_offset = stat_idx;
1633                 fh = f_idx + (FDR *)(hdr->cbFdOffset);
1634                 stat_idx += fh->csym;
1635         }
1636
1637         /* Now that we know its max size, allocate static symbol list */
1638         if (static_psymbols.list) {
1639                 int origsize = static_psymbols.next - static_psymbols.list;
1640
1641                 static_psymbols.list = (struct partial_symbol *)
1642                         xrealloc (static_psymbols.list,
1643                            stat_idx * sizeof(struct partial_symbol));
1644                 static_psymbols.next = static_psymbols.list + origsize;
1645                 static_psymbols.size = stat_idx;
1646         } else {
1647                 static_psymbols.list = (struct partial_symbol *)
1648                         xmalloc (stat_idx * sizeof(struct partial_symbol));
1649                 static_psymbols.next = static_psymbols.list;
1650                 static_psymbols.size = stat_idx;
1651         }
1652
1653         /* Pass 2 over external syms: fill in external symbols */
1654         for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
1655                 register struct partial_symbol *p;
1656                 enum misc_function_type misc_type = mf_text;
1657                 esh = (EXTR *) (hdr->cbExtOffset) + s_idx;
1658
1659                 if (esh->asym.sc == scUndefined || esh->asym.sc == scNil)
1660                         continue;
1661
1662                 /* Locate the psymtab and the preallocated psymbol.  */
1663                 pst = fdr_to_pst[esh->ifd].pst;
1664                 p = global_psymbols.list + pst->globals_offset +
1665                          pst->n_global_syms++;
1666                 SYMBOL_NAME(p) = (char *)(esh->asym.iss);
1667                 SYMBOL_NAMESPACE(p) = VAR_NAMESPACE;
1668
1669                 switch (esh->asym.st) {
1670                 case stProc:
1671                         SYMBOL_CLASS(p) = LOC_BLOCK;
1672                         SYMBOL_VALUE(p) = esh->asym.value;
1673                         break;
1674                 case stGlobal:
1675                         SYMBOL_CLASS(p) = LOC_STATIC;
1676                         SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)esh->asym.value;
1677                         misc_type = mf_data;
1678                         break;
1679                 case stLabel:
1680                         SYMBOL_CLASS(p) = LOC_LABEL;
1681                         SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)esh->asym.value;
1682                         break;
1683                 default:
1684                         misc_type = mf_unknown;
1685                         complain (&unknown_ext_complaint, SYMBOL_NAME(p));
1686                 }
1687                 prim_record_misc_function (SYMBOL_NAME(p),
1688                                            SYMBOL_VALUE(p),
1689                                            misc_type);
1690         }
1691
1692         /* Pass 3 over files, over local syms: fill in static symbols */
1693         for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
1694                 fh = f_idx + (FDR *)(cur_hdr->cbFdOffset);
1695                 pst = fdr_to_pst[f_idx].pst;
1696                 pst->texthigh = pst->textlow;
1697                 
1698                 for (s_idx = 0; s_idx < fh->csym; ) {
1699                         register struct partial_symbol *p;
1700
1701                         sh = s_idx + (SYMR *) fh->isymBase;
1702
1703                         if (sh->sc == scUndefined || sh->sc == scNil ||
1704                             sh->index == 0xfffff) {
1705                                 /* FIXME, premature? */
1706                                 s_idx++;
1707                                 continue;
1708                         }
1709
1710                         /* Locate the preallocated psymbol.  */
1711                         p = static_psymbols.list + pst->statics_offset +
1712                                  pst->n_static_syms;
1713                         SYMBOL_NAME(p) = (char *)(sh->iss);
1714                         SYMBOL_VALUE(p) = sh->value;
1715                         SYMBOL_NAMESPACE(p) = VAR_NAMESPACE;
1716
1717                         switch (sh->st) {
1718                         case stProc:            /* Asm labels apparently */
1719                         case stStaticProc:              /* Function */
1720                                 SYMBOL_CLASS(p) = LOC_BLOCK;
1721                                 pst->n_static_syms++;   /* Use gdb symbol */
1722                                 /* Skip over procedure to next one. */
1723                                 s_idx = (sh->index + (AUXU *)fh->iauxBase)
1724                                           ->isym;
1725                                         {
1726                                         long high;
1727                                         long procaddr = sh->value;
1728
1729                                         sh = s_idx + (SYMR *) fh->isymBase - 1;
1730                                         if (sh->st != stEnd)
1731                                                 continue;
1732                                         high = procaddr + sh->value;
1733                                         if (high > pst->texthigh)
1734                                                 pst->texthigh = high;
1735                                         }
1736                                 continue;
1737                         case stStatic:                  /* Variable */
1738                                 SYMBOL_CLASS(p) = LOC_STATIC;
1739                                 SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)sh->value;
1740                                 break;
1741                         case stTypedef:                 /* Typedef */
1742                                 SYMBOL_CLASS(p) = LOC_TYPEDEF;
1743                                 break;
1744                         case stConstant:                /* Constant decl */
1745                                 SYMBOL_CLASS(p) = LOC_CONST;
1746                                 break;
1747                         case stBlock:                   /* { }, str, un, enum*/
1748                                 if (sh->sc == scInfo) {
1749                                       SYMBOL_NAMESPACE(p) = STRUCT_NAMESPACE;
1750                                       SYMBOL_CLASS(p) = LOC_TYPEDEF;
1751                                       pst->n_static_syms++;
1752                                 }
1753                                 /* Skip over the block */
1754                                 s_idx = sh->index;
1755                                 continue;
1756                         case stFile:                    /* File headers */
1757                         case stLabel:                   /* Labels */
1758                         case stEnd:                     /* Ends of files */
1759                                 goto skip;
1760                         default:
1761                                 complain (&unknown_sym_complaint, SYMBOL_NAME(p));
1762                                 complain (&unknown_st_complaint, sh->st);
1763                                 s_idx++;
1764                                 continue;
1765                         }
1766                         pst->n_static_syms++;   /* Use this gdb symbol */
1767                 skip:
1768                         s_idx++;                /* Go to next file symbol */
1769 #if 0
1770 /* We don't usually record static syms, but some we seem to.  chk dbxread. */
1771 /*FIXME*/               prim_record_misc_function (SYMBOL_NAME(p),
1772                                                    SYMBOL_VALUE(p),
1773                                                    misc_type);
1774 #endif
1775                 }
1776         }
1777
1778         /* The array (of lists) of globals must be sorted. */
1779         reorder_psymtabs();
1780
1781         /* Now sort the global psymbols.  */
1782         for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
1783                 struct partial_symtab *pst = fdr_to_pst[f_idx].pst;
1784                 if (pst->n_global_syms > 1)
1785                         qsort (global_psymbols.list + pst->globals_offset,
1786                                 pst->n_global_syms, sizeof (struct partial_symbol),
1787                                 compare_psymbols);
1788         }
1789
1790         /* Mark the last code address, and remember it for later */
1791         hdr->cbDnOffset = end_of_text_seg;
1792
1793         free(&fdr_to_pst[-1]);
1794         fdr_to_pst = 0;
1795 }
1796
1797
1798 /* Do the initial analisys of the F_IDX-th file descriptor.
1799    Allocates a partial symtab for it, and builds the list
1800    of dependent files by recursion. LEV says at which level
1801    of recursion we are called (to pretty up debug traces) */
1802
1803 static struct partial_symtab *
1804 parse_fdr(f_idx, lev, objfile)
1805         int f_idx;
1806         int lev;
1807         struct objfile *objfile;
1808 {
1809         register FDR *fh;
1810         register struct partial_symtab *pst;
1811         int s_idx, s_id0;
1812
1813         fh = (FDR *) (cur_hdr->cbFdOffset) + f_idx;
1814
1815         /* Use this to indicate into which symtab this file was parsed */
1816         if (fh->ioptBase)
1817                 return (struct partial_symtab *) fh->ioptBase;
1818
1819         /* Debuggability level */
1820         if (compare_glevel(max_glevel, fh->glevel) < 0)
1821                 max_glevel = fh->glevel;
1822
1823         /* Make a new partial_symtab */
1824         pst = new_psymtab(fh->rss, objfile);
1825         if (fh->cpd == 0){
1826                 pst->textlow = 0;
1827                 pst->texthigh = 0;
1828         } else {
1829                 pst->textlow = fh->adr;
1830                 pst->texthigh = fh->cpd;        /* To be fixed later */
1831         }
1832
1833         /* Make everything point to everything. */
1834         FDR_IDX(pst) = f_idx;
1835         fdr_to_pst[f_idx].pst = pst;
1836         fh->ioptBase = (int)pst;
1837
1838         /* Analyze its dependencies */
1839         if (fh->crfd <= 1)
1840                 return pst;
1841
1842         s_id0 = 0;
1843         if (fh->cpd == 0) {  /* If there are no functions defined here ... */
1844                 /* ...then presumably a .h file: drop reverse depends .h->.c */
1845                 for (; s_id0 < fh->crfd; s_id0++) {
1846                         RFDT *rh = (RFDT *) (fh->rfdBase) + s_id0;
1847                         if (*rh == f_idx) {
1848                                 s_id0++;        /* Skip self-dependency */
1849                                 break;
1850                         }
1851                 }
1852         }
1853         pst->number_of_dependencies = fh->crfd - s_id0;
1854         pst->dependencies = (struct partial_symtab **)
1855                 obstack_alloc (psymbol_obstack,
1856                                  pst->number_of_dependencies *
1857                                    sizeof (struct partial_symtab *));
1858         for (s_idx = s_id0; s_idx < fh->crfd; s_idx++) {
1859                 RFDT *rh = (RFDT *) (fh->rfdBase) + s_idx;
1860
1861                 pst->dependencies[s_idx-s_id0] = parse_fdr(*rh, lev+1, objfile);
1862         }
1863
1864         return pst;
1865 }
1866
1867
1868 /* Ancillary function to psymtab_to_symtab().  Does all the work
1869    for turning the partial symtab PST into a symtab, recurring
1870    first on all dependent psymtabs.  The argument FILENAME is
1871    only passed so we can see in debug stack traces what file
1872    is being read. */
1873
1874 static void
1875 psymtab_to_symtab_1(pst, filename)
1876         struct partial_symtab *pst;
1877         char *filename;
1878 {
1879         int             i, f_max;
1880         struct symtab  *st;
1881         FDR *fh;
1882
1883         if (pst->readin)
1884                 return;
1885         pst->readin = 1;
1886
1887         pending_list = (struct pending **) cur_hdr->cbOptOffset;
1888         if (pending_list == 0) {
1889                 pending_list = (struct pending **)
1890                         xzalloc(cur_hdr->ifdMax * sizeof(struct pending *));
1891                 cur_hdr->cbOptOffset = (int)pending_list;
1892         }
1893
1894         /* How many symbols will we need */
1895         /* FIXME, this does not count enum values. */
1896         f_max = pst->n_global_syms + pst->n_static_syms;
1897         if (FDR_IDX(pst) == -1) {
1898                 fh = 0;
1899                 st = new_symtab ("unknown", f_max, 0, pst->objfile);
1900         } else {
1901                 fh = (FDR *) (cur_hdr->cbFdOffset) + FDR_IDX(pst);
1902                 f_max += fh->csym + fh->cpd;
1903                 st = new_symtab (pst->filename, 2 * f_max, 2 * fh->cline,
1904                                  pst->objfile);
1905         }
1906
1907         /* Read in all partial symbtabs on which this one is dependent.
1908            NOTE that we do have circular dependencies, sigh.  We solved
1909            that by setting pst->readin before this point.  */
1910
1911         for (i = 0; i < pst->number_of_dependencies; i++)
1912                 if (!pst->dependencies[i]->readin) {
1913                         /* Inform about additional files to be read in.  */
1914                         if (info_verbose)
1915                           {
1916                             fputs_filtered (" ", stdout);
1917                             wrap_here ("");
1918                             fputs_filtered ("and ", stdout);
1919                             wrap_here ("");
1920                             printf_filtered ("%s...",
1921                                              pst->dependencies[i]->filename);
1922                             wrap_here ("");             /* Flush output */
1923                             fflush (stdout);
1924                           }
1925                         /* We only pass the filename for debug purposes */
1926                         psymtab_to_symtab_1(pst->dependencies[i], 
1927                                 pst->dependencies[i]->filename);
1928                 }
1929
1930         /* Now read the symbols for this symtab */
1931
1932         cur_fd = FDR_IDX(pst);
1933         cur_fdr = fh;
1934         cur_stab = st;
1935
1936         /* Get a new lexical context */
1937
1938         push_parse_stack();
1939         top_stack->cur_st = cur_stab;
1940         top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(cur_stab),
1941                                                  STATIC_BLOCK);
1942         BLOCK_START(top_stack->cur_block) = fh ? fh->adr : 0;
1943         BLOCK_END(top_stack->cur_block) = 0;
1944         top_stack->blocktype = stFile;
1945         top_stack->maxsyms = 2*f_max;
1946         top_stack->cur_type = 0;
1947         top_stack->procadr = 0;
1948         top_stack->numargs = 0;
1949
1950         /* Parse locals and procedures */
1951         if (fh)
1952                 parse_one_file(fh, cur_fd, (cur_fd == (cur_hdr->ifdMax - 1)) ?
1953                                 cur_hdr->cbDnOffset : fh[1].adr);
1954
1955         /* .. and our share of externals.
1956            XXX use the global list to speed up things here. how ? 
1957            FIXME, Maybe quit once we have found the right number of ext's? */
1958         /* parse_external clobbers top_stack->cur_block and ->cur_st here. */
1959         top_stack->blocktype = stFile;
1960         top_stack->maxsyms = cur_hdr->isymMax + cur_hdr->ipdMax + cur_hdr->iextMax;
1961         for (i = 0; i < cur_hdr->iextMax; i++) {
1962                 register EXTR *esh = (EXTR *) (cur_hdr->cbExtOffset) + i;
1963                 if (esh->ifd == cur_fd)
1964                         parse_external(esh, 1);
1965         }
1966
1967         /* If there are undefined, tell the user */
1968         if (n_undef_symbols) {
1969                 printf_filtered("File %s contains %d unresolved references:",
1970                                 st->filename, n_undef_symbols);
1971                 printf_filtered("\n\t%4d variables\n\t%4d procedures\n\t%4d labels\n",
1972                                 n_undef_vars, n_undef_procs, n_undef_labels);
1973                 n_undef_symbols = n_undef_labels = n_undef_vars = n_undef_procs = 0;
1974         }
1975
1976         pop_parse_stack();
1977
1978         /*
1979          * Sort the symbol table now, we are done adding symbols to it.
1980          */
1981         sort_symtab_syms(st);
1982
1983         /* Now link the psymtab and the symtab.  */
1984         pst->symtab = st;
1985 }
1986 \f
1987 /* Ancillary parsing procedures. */
1988
1989 /* Lookup the type at relative index RN.  Return it in TPP
1990    if found and in any event come up with its name PNAME.
1991    Return value says how many aux symbols we ate */
1992
1993 static
1994 cross_ref(rn, tpp, pname)
1995         RNDXR *rn;
1996         struct type **tpp;
1997         char **pname;
1998 {
1999         unsigned        rf;
2000
2001         /* Escape index means 'the next one' */
2002         if (rn->rfd == 0xfff)
2003                 rf = *(unsigned *) (rn + 1);
2004         else
2005                 rf = rn->rfd;
2006
2007         if (rf == -1) {
2008                 /* Ooops */
2009                 *pname = "<undefined>";
2010         } else {
2011                 /*
2012                  * Find the relative file descriptor and the symbol in it 
2013                  */
2014                 FDR            *fh = get_rfd(cur_fd, rf);
2015                 SYMR           *sh;
2016                 struct type    *t;
2017
2018                 /*
2019                  * If we have processed this symbol then we left a forwarding
2020                  * pointer to the corresponding GDB symbol.  If not, we`ll put
2021                  * it in a list of pending symbols, to be processed later when
2022                  * the file f will be.  In any event, we collect the name for
2023                  * the type here. Which is why we made a first pass at
2024                  * strings. 
2025                  */
2026                 sh = (SYMR *) (fh->isymBase) + rn->index;
2027
2028                 /* Careful, we might be looking at .o files */
2029                 *pname = (UNSAFE_DATA_ADDR(sh->iss)) ? "<undefined>" :
2030                         (char *) sh->iss;
2031
2032                 /* Have we parsed it ? */
2033                 if ((!UNSAFE_DATA_ADDR(sh->value)) && (sh->st == stParsed)) {
2034                         t = (struct type *) sh->value;
2035                         *tpp = t;
2036                 } else {
2037                         struct pending *p;
2038
2039                         /* Avoid duplicates */
2040                         p = is_pending_symbol(fh, sh);
2041
2042                         if (p)
2043                                 *tpp = p->t;
2044                         else
2045                                 add_pending(fh, sh, *tpp);
2046                 }
2047         }
2048
2049         /* We used one auxent normally, two if we got a "next one" rf. */
2050         return (rn->rfd == 0xfff? 2: 1);
2051 }
2052
2053
2054 /* Quick&dirty lookup procedure, to avoid the MI ones that require
2055    keeping the symtab sorted */
2056
2057 static struct symbol *
2058 mylookup_symbol (name, block, namespace, class)
2059      char *name;
2060      register struct block *block;
2061      enum namespace namespace;
2062      enum address_class class;
2063 {
2064         register int    bot, top, inc;
2065         register struct symbol *sym;
2066
2067         bot = 0;
2068         top = BLOCK_NSYMS(block);
2069         inc = name[0];
2070         while (bot < top) {
2071                 sym = BLOCK_SYM(block, bot);
2072                 if (SYMBOL_NAME(sym)[0] == inc
2073                     && SYMBOL_NAMESPACE(sym) == namespace
2074                     && SYMBOL_CLASS(sym) == class
2075                     && !strcmp(SYMBOL_NAME(sym), name))
2076                         return sym;
2077                 bot++;
2078         }
2079         if (block = BLOCK_SUPERBLOCK (block))
2080                 return mylookup_symbol (name, block, namespace, class);
2081         return 0;
2082 }
2083
2084
2085 /* Add a new symbol S to a block B.
2086    Infrequently, we will need to reallocate the block to make it bigger.
2087    We only detect this case when adding to top_stack->cur_block, since
2088    that's the only time we know how big the block is.  FIXME.  */
2089
2090 static void
2091 add_symbol(s,b)
2092         struct symbol *s;
2093         struct block *b;
2094 {
2095         int nsyms = BLOCK_NSYMS(b)++;
2096         struct block *origb;
2097         struct parse_stack *stackp;
2098
2099         if (b == top_stack->cur_block &&
2100             nsyms >= top_stack->maxsyms) {
2101                 complain (&block_overflow_complaint, s->name);
2102                 /* In this case shrink_block is actually grow_block, since
2103                    BLOCK_NSYMS(b) is larger than its current size.  */
2104                 origb = b;
2105                 b = shrink_block (top_stack->cur_block, top_stack->cur_st);
2106
2107                 /* Now run through the stack replacing pointers to the
2108                    original block.  shrink_block has already done this
2109                    for the blockvector and BLOCK_FUNCTION.  */
2110                 for (stackp = top_stack; stackp; stackp = stackp->next) {
2111                         if (stackp->cur_block == origb) {
2112                                 stackp->cur_block = b;
2113                                 stackp->maxsyms = BLOCK_NSYMS (b);
2114                         }
2115                 }
2116         }
2117         BLOCK_SYM(b,nsyms) = s;
2118 }
2119
2120 /* Add a new block B to a symtab S */
2121
2122 static void
2123 add_block(b,s)
2124         struct block *b;
2125         struct symtab *s;
2126 {
2127         struct blockvector *bv = BLOCKVECTOR(s);
2128
2129         bv = (struct blockvector *)xrealloc(bv, sizeof(struct blockvector) +
2130                                             BLOCKVECTOR_NBLOCKS(bv) * sizeof(bv->block));
2131         if (bv != BLOCKVECTOR(s))
2132                 BLOCKVECTOR(s) = bv;
2133
2134         BLOCKVECTOR_BLOCK(bv, BLOCKVECTOR_NBLOCKS(bv)++) = b;
2135 }
2136
2137 /* Add a new linenumber entry (LINENO,ADR) to a linevector LT.
2138    MIPS' linenumber encoding might need more than one byte
2139    to describe it, LAST is used to detect these continuation lines */
2140
2141 static int
2142 add_line(lt, lineno, adr, last)
2143         struct linetable *lt;
2144         int lineno;
2145         CORE_ADDR adr;
2146         int last;
2147 {
2148         if (last == 0)
2149                 last = -2;      /* make sure we record first line */
2150
2151         if (last == lineno)     /* skip continuation lines */
2152                 return lineno;
2153
2154         lt->item[lt->nitems].line = lineno;
2155         lt->item[lt->nitems++].pc = adr << 2;
2156         return lineno;
2157 }
2158
2159
2160 \f
2161 /* Comparison functions, used when sorting things */
2162
2163 /*  Symtabs must be ordered viz the code segments they cover */
2164
2165 static int
2166 compare_symtabs( s1, s2)
2167         struct symtab **s1, **s2;
2168 {
2169         /* "most specific" first */
2170
2171         register struct block *b1, *b2;
2172         b1 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s1),GLOBAL_BLOCK);
2173         b2 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s2),GLOBAL_BLOCK);
2174         if (BLOCK_END(b1) == BLOCK_END(b2))
2175                 return BLOCK_START(b1) - BLOCK_START(b2);
2176         return BLOCK_END(b1) - BLOCK_END(b2);
2177 }
2178
2179
2180 /*  Partial Symtabs, same */
2181
2182 static int
2183 compare_psymtabs( s1, s2)
2184         struct partial_symtab **s1, **s2;
2185 {
2186         /* Perf twist: put the ones with no code at the end */
2187
2188         register int a = (*s1)->textlow;
2189         register int b = (*s2)->textlow;
2190         if (a == 0)
2191                 return b;
2192         if (b == 0)
2193                 return -a;
2194         return a - b;
2195 }
2196
2197
2198 /* Partial symbols are compared lexicog by their print names */
2199
2200 static int
2201 compare_psymbols (s1, s2)
2202         register struct partial_symbol *s1, *s2;
2203 {
2204         register char
2205                        *st1 = SYMBOL_NAME(s1),
2206                        *st2 = SYMBOL_NAME(s2);
2207
2208         return (st1[0] - st2[0] ? st1[0] - st2[0] :
2209                 strcmp(st1 + 1, st2 + 1));
2210 }
2211
2212 /* Blocks with a smaller low bound should come first */
2213
2214 static int compare_blocks(b1,b2)
2215         struct block **b1, **b2;
2216 {
2217         register int addr_diff;
2218
2219         addr_diff = (BLOCK_START((*b1))) - (BLOCK_START((*b2)));
2220         if (addr_diff == 0)
2221                 return (BLOCK_END((*b1))) - (BLOCK_END((*b2)));
2222         return addr_diff;
2223 }
2224
2225 \f
2226 /* Sorting and reordering procedures */
2227
2228 /* Sort the blocks of a symtab S.
2229    Reorder the blocks in the blockvector by code-address,
2230    as required by some MI search routines */
2231
2232 static void
2233 sort_blocks(s)
2234         struct symtab *s;
2235 {
2236         struct blockvector *bv = BLOCKVECTOR(s);
2237
2238         if (BLOCKVECTOR_NBLOCKS(bv) <= 2) {
2239                 /* Cosmetic */
2240                 if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) == 0)
2241                         BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = 0;
2242                 if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) == 0)
2243                         BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = 0;
2244                 return;
2245         }
2246         /*
2247          * This is very unfortunate: normally all functions are compiled in
2248          * the order they are found, but if the file is compiled -O3 things
2249          * are very different.  It would be nice to find a reliable test
2250          * to detect -O3 images in advance.
2251          */
2252         if (BLOCKVECTOR_NBLOCKS(bv) > 3)
2253                 qsort(&BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK),
2254                       BLOCKVECTOR_NBLOCKS(bv) - FIRST_LOCAL_BLOCK,
2255                       sizeof(struct block *),
2256                       compare_blocks);
2257
2258         {
2259                 register CORE_ADDR high = 0;
2260                 register int    i, j = BLOCKVECTOR_NBLOCKS(bv);
2261
2262                 for (i = FIRST_LOCAL_BLOCK; i < j; i++)
2263                         if (high < BLOCK_END(BLOCKVECTOR_BLOCK(bv,i)))
2264                                 high = BLOCK_END(BLOCKVECTOR_BLOCK(bv,i));
2265                 BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = high;
2266         }
2267
2268         BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) =
2269                 BLOCK_START(BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK));
2270
2271         BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = 
2272                 BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK));
2273         BLOCK_END  (BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) =
2274                 BLOCK_END  (BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK));
2275 }
2276
2277 /* Sort the symtab list, as required by some search procedures.
2278    We want files ordered to make them look right to users, and for
2279    searching (see block_for_pc).  */
2280
2281 static void
2282 reorder_symtabs()
2283 {
2284         register int i;
2285         struct symtab *stab;
2286         register struct symtab **all_symtabs;
2287         register int symtab_count;
2288
2289         if (!symtab_list)
2290                 return;
2291
2292         /* Create an array of pointers to all the symtabs.  */
2293         for (symtab_count = 0, stab = symtab_list;
2294              stab;
2295              symtab_count++, stab = stab->next) {
2296                 obstack_grow (psymbol_obstack, &stab, sizeof (stab));
2297                 /* FIXME: Only sort blocks for new symtabs ??? */
2298                 sort_blocks(stab);
2299         }
2300
2301         all_symtabs = (struct symtab **)
2302                 obstack_base (psymbol_obstack);
2303         qsort((char *)all_symtabs, symtab_count,
2304                 sizeof(struct symtab *), compare_symtabs);
2305
2306         /* Re-construct the symtab list, but now it is sorted.  */
2307         for (i = 0; i < symtab_count-1; i++)
2308                 all_symtabs[i]->next = all_symtabs[i+1];
2309         all_symtabs[i]->next = 0;
2310         symtab_list = all_symtabs[0];
2311
2312         obstack_free (psymbol_obstack, all_symtabs);
2313 }
2314
2315 /* Sort the partial symtab list, as required by some search procedures.
2316    PC lookups stop at the first psymtab such that textlow <= PC < texthigh */
2317
2318 static void
2319 reorder_psymtabs()
2320 {
2321         register int i;
2322         register int all_psymtabs_count;
2323         struct partial_symtab *pstab;
2324         struct partial_symtab **all_psymtabs;
2325
2326         if (!partial_symtab_list)
2327                 return;
2328
2329         /* Create an array of pointers to all the partial_symtabs.  */
2330
2331         for (all_psymtabs_count = 0, pstab = partial_symtab_list;
2332              pstab;
2333              all_psymtabs_count++, pstab = pstab->next)
2334           obstack_grow (psymbol_obstack, &pstab, sizeof (pstab));
2335
2336         all_psymtabs = (struct partial_symtab **)
2337                  obstack_base (psymbol_obstack);
2338
2339         qsort((char *)all_psymtabs, all_psymtabs_count,
2340                 sizeof(struct partial_symtab *), compare_psymtabs);
2341
2342         /* Re-construct the partial_symtab_list, but now it is sorted.  */
2343
2344         for (i = 0; i < all_psymtabs_count-1; i++)
2345                 all_psymtabs[i]->next = all_psymtabs[i+1];
2346         all_psymtabs[i]->next = 0;
2347         partial_symtab_list = all_psymtabs[0];
2348
2349         obstack_free (psymbol_obstack, all_psymtabs);
2350 }
2351 \f
2352 /* Constructor/restructor/destructor procedures */
2353
2354 /* Allocate a new symtab for NAME.  Needs an estimate of how many symbols
2355    MAXSYMS and linenumbers MAXLINES we'll put in it */
2356
2357 static
2358 struct symtab *
2359 new_symtab(name, maxsyms, maxlines, objfile)
2360         char *name;
2361 {
2362         struct symtab *s = allocate_symtab (name, objfile);
2363
2364         LINETABLE(s) = new_linetable(maxlines);
2365
2366         /* All symtabs must have at least two blocks */
2367         BLOCKVECTOR(s) = new_bvect(2);
2368         BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK) = new_block(maxsyms);
2369         BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), STATIC_BLOCK) = new_block(maxsyms);
2370         BLOCK_SUPERBLOCK( BLOCKVECTOR_BLOCK(BLOCKVECTOR(s),STATIC_BLOCK)) =
2371                 BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK);
2372
2373         s->free_code = free_linetable;
2374
2375         /* Link the new symtab into the list of such.  */
2376         s->next = symtab_list;
2377         symtab_list = s;
2378
2379         return s;
2380 }
2381
2382 /* Allocate a new partial_symtab NAME */
2383
2384 static struct partial_symtab *
2385 new_psymtab(name, objfile)
2386         char *name;
2387         struct objfile *objfile;
2388 {
2389         struct partial_symtab *pst;
2390
2391         pst = (struct partial_symtab *)
2392               obstack_alloc (psymbol_obstack, sizeof (*pst));
2393         bzero (pst, sizeof (*pst));
2394
2395         if (name == (char*)-1)          /* FIXME -- why not null here? */
2396                 pst->filename = "<no name>";
2397         else
2398                 pst->filename = name;
2399
2400         /* Chain it to its object file */
2401         pst->objfile = objfile;
2402         pst->objfile_chain = objfile->psymtabs;
2403         objfile->psymtabs = pst;
2404         
2405         pst->next = partial_symtab_list;
2406         partial_symtab_list = pst;
2407
2408         /* Keep a backpointer to the file's symbols */
2409         pst->read_symtab_private = (char *) obstack_alloc (psymbol_obstack,
2410                                                   sizeof (struct symloc));
2411         CUR_HDR(pst) = cur_hdr;
2412
2413         /* The way to turn this into a symtab is to call... */
2414         pst->read_symtab = mipscoff_psymtab_to_symtab;
2415
2416         return pst;
2417 }
2418
2419
2420 /* Allocate a linetable array of the given SIZE */
2421
2422 static
2423 struct linetable *new_linetable(size)
2424 {
2425         struct linetable *l;
2426
2427         size = size * sizeof(l->item) + sizeof(struct linetable);
2428         l = (struct linetable *)xmalloc(size);
2429         l->nitems = 0;
2430         return l;
2431 }
2432
2433 /* Oops, too big. Shrink it.  This was important with the 2.4 linetables,
2434    I am not so sure about the 3.4 ones */
2435
2436 static void
2437 shrink_linetable(s)
2438         struct symtab *s;
2439 {
2440         struct linetable *l = new_linetable(LINETABLE(s)->nitems);
2441
2442         bcopy(LINETABLE(s), l,
2443               LINETABLE(s)->nitems * sizeof(l->item) + sizeof(struct linetable));
2444         free (LINETABLE(s));
2445         LINETABLE(s) = l;
2446 }
2447
2448 /* Allocate and zero a new blockvector of NBLOCKS blocks. */
2449
2450 static
2451 struct blockvector *
2452 new_bvect(nblocks)
2453 {
2454         struct blockvector *bv;
2455         int size;
2456
2457         size = sizeof(struct blockvector) + nblocks * sizeof(struct block*);
2458         bv = (struct blockvector *) xzalloc(size);
2459
2460         BLOCKVECTOR_NBLOCKS(bv) = nblocks;
2461
2462         return bv;
2463 }
2464
2465 /* Allocate and zero a new block of MAXSYMS symbols */
2466
2467 static
2468 struct block *
2469 new_block(maxsyms)
2470 {
2471         int size = sizeof(struct block) + (maxsyms-1) * sizeof(struct symbol *);
2472         struct block *b = (struct block *)xzalloc(size);
2473
2474         return b;
2475 }
2476
2477 /* Ooops, too big. Shrink block B in symtab S to its minimal size.
2478    Shrink_block can also be used by add_symbol to grow a block.  */
2479
2480 static struct block *
2481 shrink_block(b, s)
2482         struct block *b;
2483         struct symtab *s;
2484 {
2485         struct block *new;
2486         struct blockvector *bv = BLOCKVECTOR(s);
2487         int i;
2488
2489         /* Just reallocate it and fix references to the old one */
2490
2491         new = (struct block *) xrealloc ((char *)b, sizeof(struct block) +
2492                 (BLOCK_NSYMS(b)-1) * sizeof(struct symbol *));
2493
2494         /* Should chase pointers to old one.  Fortunately, that`s just
2495            the block`s function and inferior blocks */
2496         if (BLOCK_FUNCTION(new) && SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(new)) == b)
2497                 SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(new)) = new;
2498         for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++)
2499                 if (BLOCKVECTOR_BLOCK(bv,i) == b)
2500                         BLOCKVECTOR_BLOCK(bv,i) = new;
2501                 else if (BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) == b)
2502                         BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) = new;
2503         return new;
2504 }
2505
2506 /* Create a new symbol with printname NAME */
2507
2508 static
2509 struct symbol *
2510 new_symbol(name)
2511         char *name;
2512 {
2513         struct symbol *s = (struct symbol *) 
2514                 obstack_alloc (symbol_obstack, sizeof (struct symbol));
2515
2516         bzero (s, sizeof (*s));
2517         SYMBOL_NAME(s) = name;
2518         return s;
2519 }
2520
2521 /* Create a new type with printname NAME */
2522
2523 static
2524 struct type *
2525 new_type(name)
2526         char *name;
2527 {
2528         struct type *t = (struct type *)
2529                 obstack_alloc (symbol_obstack, sizeof (struct type));
2530
2531         bzero (t, sizeof (*t));
2532         TYPE_VPTR_FIELDNO (t) = -1;
2533         TYPE_NAME(t) = name;
2534         return t;
2535 }
2536
2537 /* Create and initialize a new type with printname NAME.
2538    CODE and LENGTH are the initial info we put in,
2539    UNS says whether the type is unsigned or not.  */
2540
2541 static
2542 struct type *
2543 make_type(code, length, uns, name)
2544      enum type_code code;
2545      int length, uns;
2546      char *name;
2547 {
2548         register struct type *type;
2549
2550         /* FIXME, I don't think this ever gets freed.  */
2551         type = (struct type *) xzalloc(sizeof(struct type));
2552         TYPE_CODE(type) = code;
2553         TYPE_LENGTH(type) = length;
2554         TYPE_FLAGS(type) = uns ? TYPE_FLAG_UNSIGNED : 0;
2555         TYPE_NAME(type) = name;
2556         TYPE_VPTR_FIELDNO (type) = -1;
2557
2558         return type;
2559 }
2560
2561 /* Create and initialize a new struct or union type, a la make_type.  */
2562
2563 static
2564 struct type *
2565 make_struct_type(code, length, uns, name)
2566      enum type_code code;
2567      int length, uns;
2568      char *name;
2569 {
2570         register struct type *type;
2571
2572         type = make_type (code, length, uns, name);
2573         
2574         /* FIXME, I don't think this ever gets freed.  */
2575         TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
2576           xzalloc (sizeof (struct cplus_struct_type));
2577         return type;
2578 }
2579
2580 /* Allocate a new field named NAME to the type TYPE */
2581
2582 static
2583 struct field *
2584 new_field(type,name)
2585         struct type *type;
2586         char *name;
2587 {
2588         struct field *f;
2589
2590         /* Fields are kept in an array */
2591         if (TYPE_NFIELDS(type))
2592                 TYPE_FIELDS(type) = (struct field*)xrealloc(TYPE_FIELDS(type),
2593                         (TYPE_NFIELDS(type)+1) * sizeof(struct field));
2594         else
2595                 TYPE_FIELDS(type) = (struct field*)xzalloc(sizeof(struct field));
2596         f = &(TYPE_FIELD(type,TYPE_NFIELDS(type)));
2597         TYPE_NFIELDS(type)++;
2598         bzero(f, sizeof(struct field));
2599         f->name = name;         /* Whether or not NAME is zero, this works. */
2600         return f;
2601 }
2602
2603 /* Make an enum constant for a member F of an enumerated type T */
2604
2605 static
2606 make_enum_constant(f,t)
2607         struct field *f;
2608         struct type *t;
2609 {
2610         struct symbol *s;
2611         /*
2612          * This is awful, but that`s the way it is supposed to be
2613          * (BTW, no need to free the real 'type', it's a builtin)
2614          */
2615         f->type = (struct type *) f->bitpos;
2616
2617         s = new_symbol(f->name);
2618         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
2619         SYMBOL_CLASS(s) = LOC_CONST;
2620         SYMBOL_TYPE(s) = t;
2621         SYMBOL_VALUE(s) = f->bitpos;
2622         add_symbol(s, top_stack->cur_block);
2623 }
2624
2625
2626 \f
2627 /* Things used for calling functions in the inferior.
2628    These functions are exported to our companion
2629    mips-dep.c file and are here because they play
2630    with the symbol-table explicitly. */
2631
2632 #if 0
2633 /* Need to make a new symbol on the fly for the dummy
2634    frame we put on the stack.  Which goes in the.. */
2635
2636 static struct symtab *dummy_symtab;
2637
2638 /* Make up a dummy symbol for the code we put at END_PC,
2639    of size SIZE, invoking a function with NARGS arguments
2640    and using a frame of FRAMESIZE bytes */
2641
2642 mips_create_dummy_symbol(end_pc, size, nargs, framesize)
2643 {
2644         struct block   *bl;
2645         struct symbol  *g;
2646         struct mips_extra_func_info *gdbinfo;
2647
2648         /* Allocate symtab if not done already */
2649         if (dummy_symtab == 0)
2650                 dummy_symtab = new_symtab(".dummy_symtab.", 100, 0);
2651
2652         /* Make a new block. Only needs one symbol */
2653         bl = new_block(1);
2654         BLOCK_START(bl) = end_pc - size;
2655         BLOCK_END(bl) = end_pc;
2656
2657         BLOCK_SUPERBLOCK(bl) =
2658                 BLOCKVECTOR_BLOCK(BLOCKVECTOR(dummy_symtab),GLOBAL_BLOCK);
2659         add_block(bl, dummy_symtab);
2660         sort_blocks(dummy_symtab);
2661
2662         BLOCK_FUNCTION(bl) = new_symbol("??");
2663         SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(bl)) = bl;
2664         g = new_symbol(".gdbinfo.");
2665         BLOCK_SYM(bl,BLOCK_NSYMS(bl)++) = g;
2666
2667         SYMBOL_NAMESPACE(g) = LABEL_NAMESPACE;
2668         SYMBOL_CLASS(g) = LOC_CONST;
2669         SYMBOL_TYPE(g) = builtin_type_void;
2670         gdbinfo = (struct mips_extra_func_info *)
2671                 xzalloc(sizeof(struct mips_extra_func_info));
2672
2673         SYMBOL_VALUE(g) = (long) gdbinfo;
2674
2675         gdbinfo->numargs = nargs;
2676         gdbinfo->framesize = framesize;
2677         gdbinfo->framereg = 29;
2678         gdbinfo->pcreg = 31;
2679         gdbinfo->regmask = -2;
2680         gdbinfo->regoffset = -4;
2681         gdbinfo->fregmask = 0;  /* XXX */
2682         gdbinfo->fregoffset = 0;        /* XXX */
2683 }
2684
2685 /* We just returned from the dummy code at END_PC, drop its symbol */
2686
2687 mips_destroy_dummy_symbol(end_pc)
2688 {
2689         struct block *bl;
2690         struct blockvector *bv = BLOCKVECTOR(dummy_symtab);
2691         int i;
2692
2693         bl = block_for_pc(end_pc);
2694         free(BLOCK_FUNCTION(bl));
2695         free(SYMBOL_VALUE(BLOCK_SYM(bl,0)));
2696         free(BLOCK_SYM(bl,0));
2697
2698         for (i = FIRST_LOCAL_BLOCK; i < BLOCKVECTOR_NBLOCKS(bv); i++)
2699                 if (BLOCKVECTOR_BLOCK(bv,i) == bl)
2700                         break;
2701         for (; i < BLOCKVECTOR_NBLOCKS(bv) - 1; i++)
2702                 BLOCKVECTOR_BLOCK(bv,i) = BLOCKVECTOR_BLOCK(bv,i+1);
2703         BLOCKVECTOR_NBLOCKS(bv)--;
2704         sort_blocks(dummy_symtab);
2705         free(bl);
2706 }
2707 #endif
2708
2709 /* Sigtramp: make sure we have all the necessary information
2710    about the signal trampoline code. Since the official code
2711    from MIPS does not do so, we make up that information ourselves.
2712    If they fix the library (unlikely) this code will neutralize itself. */
2713
2714 static
2715 fixup_sigtramp()
2716 {
2717         struct symbol  *s;
2718         struct symtab  *st;
2719         struct block   *b, *b0;
2720
2721         sigtramp_address = -1;
2722
2723         /* We know it is sold as sigvec */
2724         s = lookup_symbol("sigvec", 0, VAR_NAMESPACE, 0, NULL);
2725
2726         /* Most programs do not play with signals */
2727         if (s == 0)
2728                 return;
2729
2730         b0 = SYMBOL_BLOCK_VALUE(s);
2731
2732         /* A label of sigvec, to be more precise */
2733         s = lookup_symbol("sigtramp", b0, VAR_NAMESPACE, 0, NULL);
2734
2735         /* But maybe this program uses its own version of sigvec */
2736         if (s == 0)
2737                 return;
2738
2739         sigtramp_address = SYMBOL_VALUE(s);
2740         sigtramp_end = sigtramp_address + 0x88; /* black magic */
2741
2742         /* Did we or MIPSco fix the library ? */
2743         if (SYMBOL_CLASS(s) == LOC_BLOCK)
2744                 return;
2745
2746         /* But what symtab does it live in ? */
2747         st = find_pc_symtab(SYMBOL_VALUE(s));
2748
2749         /*
2750          * Ok, there goes the fix: turn it into a procedure, with all the
2751          * needed info.  Note we make it a nested procedure of sigvec,
2752          * which is the way the (assembly) code is actually written.
2753          */
2754         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
2755         SYMBOL_CLASS(s) = LOC_BLOCK;
2756         SYMBOL_TYPE(s) = make_type(TYPE_CODE_FUNC, 4, 0, 0);
2757         TYPE_TARGET_TYPE(SYMBOL_TYPE(s)) = builtin_type_void;
2758
2759         /* Need a block to allocate .gdbinfo. in */
2760         b = new_block(1);
2761         SYMBOL_BLOCK_VALUE(s) = b;
2762         BLOCK_START(b) = sigtramp_address;
2763         BLOCK_END(b) = sigtramp_end;
2764         BLOCK_FUNCTION(b) = s;
2765         BLOCK_SUPERBLOCK(b) = BLOCK_SUPERBLOCK(b0);
2766         add_block(b, st);
2767         sort_blocks(st);
2768
2769         /* Make a .gdbinfo. for it */
2770         {
2771                 struct mips_extra_func_info *e =
2772                         (struct mips_extra_func_info *)
2773                         xzalloc(sizeof(struct mips_extra_func_info));
2774
2775                 e->numargs = 0; /* the kernel thinks otherwise */
2776                 /* align_longword(sigcontext + SIGFRAME) */
2777                 e->framesize = 0x150;
2778                 e->framereg = SP_REGNUM;
2779                 e->pcreg = 31;
2780                 e->regmask = -2;
2781                 e->regoffset = -(41 * sizeof(int));
2782                 e->fregmask = -1;
2783                 e->fregoffset = -(37 * sizeof(int));
2784                 e->isym = (long)s;
2785
2786                 s = new_symbol(".gdbinfo.");
2787                 SYMBOL_VALUE(s) = (int) e;
2788                 SYMBOL_NAMESPACE(s) = LABEL_NAMESPACE;
2789                 SYMBOL_CLASS(s) = LOC_CONST;
2790                 SYMBOL_TYPE(s) = builtin_type_void;
2791         }
2792
2793         BLOCK_SYM(b,BLOCK_NSYMS(b)++) = s;
2794 }
2795 \f
2796 /* Initialization */
2797
2798 static struct sym_fns ecoff_sym_fns = {"ecoff", 5,
2799                 mipscoff_new_init, mipscoff_symfile_init,
2800                 mipscoff_symfile_read};
2801
2802 _initialize_mipsread ()
2803 {
2804         add_symtab_fns (&ecoff_sym_fns);
2805
2806         /* Missing basic types */
2807         builtin_type_string = make_type(TYPE_CODE_PASCAL_ARRAY,
2808                                         1, 0, "string");
2809         builtin_type_complex = make_type(TYPE_CODE_FLT,
2810                                          2 * sizeof(float), 0, "complex");
2811         builtin_type_double_complex = make_type(TYPE_CODE_FLT,
2812                                     2 * sizeof(double), 0, "double_complex");
2813         builtin_type_fixed_dec = make_type(TYPE_CODE_INT, sizeof(int),
2814                                            0, "fixed_decimal");
2815         builtin_type_float_dec = make_type(TYPE_CODE_FLT, sizeof(double),
2816                                            0, "floating_decimal");
2817 }
This page took 0.195526 seconds and 4 git commands to generate.