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