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