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