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