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