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