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