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