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