* complaints.c: New file, code moved from utils.c.
[deliverable/binutils-gdb.git] / gdb / coffread.c
CommitLineData
bd5635a1 1/* Read coff symbol tables and convert to internal format, for GDB.
d8ce1326 2 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
7e258d18 3 Copyright 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
99a7de40 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
99a7de40
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
99a7de40 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
99a7de40
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 20\f
bd5635a1 21#include "defs.h"
bd5635a1 22#include "symtab.h"
318bf84f 23#include "gdbtypes.h"
bd5635a1
RP
24#include "breakpoint.h"
25#include "bfd.h"
bd5635a1 26#include "symfile.h"
3624c875 27#include "objfiles.h"
318bf84f 28#include "buildsym.h"
51b80b00 29#include "complaints.h"
bd5635a1 30#include <obstack.h>
318bf84f 31
bd5635a1
RP
32#include <string.h>
33
f5f0679a 34#include "coff/internal.h" /* Internal format of COFF symbols in BFD */
63989338
JG
35#include "libcoff.h" /* FIXME secret internal data from BFD */
36
8714ff35
JK
37/* To be an sdb debug type, type must have at least a basic or primary
38 derived type. Using this rather than checking against T_NULL is
ab8f22a9
JK
39 said to prevent core dumps if we try to operate on Michael Bloom
40 dbx-in-coff file. */
8714ff35
JK
41
42#define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
43
d8ce1326
JG
44/*
45 * Convert from an sdb register number to an internal gdb register number.
46 * This should be defined in tm.h, if REGISTER_NAMES is not set up
47 * to map one to one onto the sdb register numbers.
48 */
49#ifndef SDB_REG_TO_REGNUM
50# define SDB_REG_TO_REGNUM(value) (value)
51#endif
52
bd5635a1
RP
53/* Core address of start and end of text of current source file.
54 This comes from a ".text" symbol where x_nlinno > 0. */
55
56static CORE_ADDR cur_src_start_addr;
57static CORE_ADDR cur_src_end_addr;
58
59/* Core address of the end of the first object file. */
60static CORE_ADDR first_object_file_end;
61
bd5635a1
RP
62/* The addresses of the symbol table stream and number of symbols
63 of the object file we are reading (as copied into core). */
64
65static FILE *nlist_stream_global;
66static int nlist_nsyms_global;
67
bd5635a1
RP
68/* Vector of line number information. */
69
70static struct linetable *line_vector;
71
72/* Index of next entry to go in line_vector_index. */
73
74static int line_vector_index;
75
76/* Last line number recorded in the line vector. */
77
78static int prev_line_number;
79
80/* Number of elements allocated for line_vector currently. */
81
82static int line_vector_length;
83
d8ce1326
JG
84/* Pointers to scratch storage, used for reading raw symbols and auxents. */
85
86static char *temp_sym;
87static char *temp_aux;
88
89/* Local variables that hold the shift and mask values for the
90 COFF file that we are currently reading. These come back to us
91 from BFD, and are referenced by their macro names, as well as
92 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
93 macros from ../internalcoff.h . */
94
95static unsigned local_n_btmask;
96static unsigned local_n_btshft;
97static unsigned local_n_tmask;
98static unsigned local_n_tshift;
99
100#define N_BTMASK local_n_btmask
101#define N_BTSHFT local_n_btshft
102#define N_TMASK local_n_tmask
103#define N_TSHIFT local_n_tshift
104
105/* Local variables that hold the sizes in the file of various COFF structures.
106 (We only need to know this to read them from the file -- BFD will then
107 translate the data in them, into `internal_xxx' structs in the right
108 byte order, alignment, etc.) */
109
110static unsigned local_linesz;
111static unsigned local_symesz;
112static unsigned local_auxesz;
113
114
bd5635a1
RP
115/* Chain of typedefs of pointers to empty struct/union types.
116 They are chained thru the SYMBOL_VALUE_CHAIN. */
117
bd5635a1
RP
118static struct symbol *opaque_type_chain[HASHSIZE];
119
120/* Record the symbols defined for each context in a list.
121 We don't create a struct block for the context until we
122 know how long to make it. */
123
318bf84f 124struct coff_pending
bd5635a1 125{
318bf84f 126 struct coff_pending *next;
bd5635a1
RP
127 struct symbol *symbol;
128};
129
130/* Here are the three lists that symbols are put on. */
131
318bf84f 132struct coff_pending *coff_file_symbols; /* static at top level, and types */
bd5635a1 133
85f0a848 134struct coff_pending *coff_global_symbols; /* global functions and variables */
bd5635a1 135
85f0a848 136struct coff_pending *coff_local_symbols; /* everything local to lexical context */
bd5635a1
RP
137
138/* List of unclosed lexical contexts
139 (that will become blocks, eventually). */
140
318bf84f 141struct coff_context_stack
bd5635a1 142{
318bf84f
FF
143 struct coff_context_stack *next;
144 struct coff_pending *locals;
bd5635a1
RP
145 struct pending_block *old_blocks;
146 struct symbol *name;
147 CORE_ADDR start_addr;
148 int depth;
149};
150
318bf84f 151struct coff_context_stack *coff_context_stack;
bd5635a1
RP
152
153/* Nonzero if within a function (so symbols should be local,
154 if nothing says specifically). */
155
156int within_function;
157
d8ce1326
JG
158#if 0
159/* The type of the function we are currently reading in. This is
160 used by define_symbol to record the type of arguments to a function. */
161
162struct type *in_function_type;
163#endif
164
bd5635a1
RP
165struct pending_block *pending_blocks;
166
bd5635a1
RP
167/* Complaints about various problems in the file being read */
168
169struct complaint ef_complaint =
170 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
171
7e258d18
PB
172struct complaint bf_no_aux_complaint =
173 {"`.bf' symbol %d has no aux entry", 0, 0};
174
175struct complaint ef_no_aux_complaint =
176 {"`.ef' symbol %d has no aux entry", 0, 0};
e64fbb3a 177
63989338
JG
178struct complaint lineno_complaint =
179 {"Line number pointer %d lower than start of line numbers", 0, 0};
180
7e258d18
PB
181struct complaint unexpected_type_complaint =
182 {"Unexpected type for symbol %s", 0, 0};
183
184struct complaint bad_sclass_complaint =
185 {"Bad n_sclass for symbol %s", 0, 0};
318bf84f 186
f70be3e4
JG
187struct complaint misordered_blocks_complaint =
188 {"Blocks out of order at address %x", 0, 0};
189
190struct complaint tagndx_bad_complaint =
191 {"Symbol table entry for %s has bad tagndx value", 0, 0};
192
318bf84f
FF
193/* Simplified internal version of coff symbol table information */
194
195struct coff_symbol {
196 char *c_name;
197 int c_symnum; /* symbol number of this entry */
198 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
199 long c_value;
200 int c_sclass;
201 int c_secnum;
202 unsigned int c_type;
203};
204
205static struct type *
206coff_read_struct_type PARAMS ((int, int, int));
207
208static struct type *
209decode_base_type PARAMS ((struct coff_symbol *, unsigned int,
210 union internal_auxent *));
211
212static struct type *
213decode_type PARAMS ((struct coff_symbol *, unsigned int,
214 union internal_auxent *));
215
216static struct type *
217decode_function_type PARAMS ((struct coff_symbol *, unsigned int,
218 union internal_auxent *));
219
220static struct type *
221coff_read_enum_type PARAMS ((int, int, int));
222
223static struct blockvector *
224make_blockvector PARAMS ((struct objfile *));
225
226static struct symbol *
227process_coff_symbol PARAMS ((struct coff_symbol *, union internal_auxent *,
228 struct objfile *));
229
f70be3e4
JG
230static void
231patch_opaque_types PARAMS ((struct symtab *));
318bf84f
FF
232
233static void
234patch_type PARAMS ((struct type *, struct type *));
235
236static void
237enter_linenos PARAMS ((long, int, int));
238
239static int
240init_lineno PARAMS ((int, long, int));
241
242static char *
243getfilename PARAMS ((union internal_auxent *));
244
245static char *
246getsymname PARAMS ((struct internal_syment *));
247
248static void
249free_stringtab PARAMS ((void));
250
251static int
252init_stringtab PARAMS ((int, long));
253
254static void
255read_one_sym PARAMS ((struct coff_symbol *, struct internal_syment *,
256 union internal_auxent *));
257
258static void
f70be3e4 259read_coff_symtab PARAMS ((long, int, struct objfile *));
318bf84f
FF
260
261static void
80d68b1d 262find_linenos PARAMS ((bfd *, sec_ptr, PTR));
318bf84f
FF
263
264static void
80d68b1d 265coff_symfile_init PARAMS ((struct objfile *));
318bf84f
FF
266
267static void
80d68b1d 268coff_new_init PARAMS ((struct objfile *));
318bf84f
FF
269
270static void
85f0a848 271coff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
80d68b1d
FF
272
273static void
274coff_symfile_finish PARAMS ((struct objfile *));
318bf84f
FF
275
276static void
f70be3e4 277record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type));
318bf84f
FF
278
279static void
280coff_end_symtab PARAMS ((struct objfile *));
281
282static void
283complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
284
285static void
286coff_start_symtab PARAMS ((void));
287
288static void
289coff_record_line PARAMS ((int, CORE_ADDR));
290
291static void
292coff_finish_block PARAMS ((struct symbol *, struct coff_pending **,
293 struct pending_block *, CORE_ADDR, CORE_ADDR,
294 struct objfile *));
295
296static void
297coff_add_symbol_to_list PARAMS ((struct symbol *, struct coff_pending **));
298
299static struct type *
300coff_alloc_type PARAMS ((int));
301
302static struct type **
303coff_lookup_type PARAMS ((int));
304
bd5635a1
RP
305\f
306/* Look up a coff type-number index. Return the address of the slot
307 where the type for that index is stored.
308 The type-number is in INDEX.
309
310 This can be used for finding the type associated with that index
311 or for associating a new type with the index. */
312
313static struct type **
314coff_lookup_type (index)
315 register int index;
316{
317 if (index >= type_vector_length)
318 {
319 int old_vector_length = type_vector_length;
320
321 type_vector_length *= 2;
322 if (type_vector_length < index) {
323 type_vector_length = index * 2;
324 }
159a075e 325 type_vector = (struct type **)
318bf84f
FF
326 xrealloc ((char *) type_vector,
327 type_vector_length * sizeof (struct type *));
4ed3a9ea 328 memset (&type_vector[old_vector_length], 0,
bd5635a1
RP
329 (type_vector_length - old_vector_length) * sizeof(struct type *));
330 }
159a075e 331 return &type_vector[index];
bd5635a1
RP
332}
333
334/* Make sure there is a type allocated for type number index
335 and return the type object.
336 This can create an empty (zeroed) type object. */
337
338static struct type *
339coff_alloc_type (index)
340 int index;
341{
342 register struct type **type_addr = coff_lookup_type (index);
343 register struct type *type = *type_addr;
344
345 /* If we are referring to a type not known at all yet,
346 allocate an empty type for it.
347 We will fill it in later if we find out how. */
318bf84f 348 if (type == NULL)
bd5635a1 349 {
318bf84f 350 type = alloc_type (current_objfile);
bd5635a1
RP
351 *type_addr = type;
352 }
353 return type;
354}
355\f
356/* maintain the lists of symbols and blocks */
357
358/* Add a symbol to one of the lists of symbols. */
359static void
318bf84f 360coff_add_symbol_to_list (symbol, listhead)
bd5635a1 361 struct symbol *symbol;
318bf84f 362 struct coff_pending **listhead;
bd5635a1 363{
318bf84f
FF
364 register struct coff_pending *link
365 = (struct coff_pending *) xmalloc (sizeof (struct coff_pending));
bd5635a1
RP
366
367 link->next = *listhead;
368 link->symbol = symbol;
369 *listhead = link;
370}
371
372/* Take one of the lists of symbols and make a block from it.
373 Put the block on the list of pending blocks. */
374
375static void
318bf84f 376coff_finish_block (symbol, listhead, old_blocks, start, end, objfile)
bd5635a1 377 struct symbol *symbol;
318bf84f 378 struct coff_pending **listhead;
bd5635a1
RP
379 struct pending_block *old_blocks;
380 CORE_ADDR start, end;
318bf84f 381 struct objfile *objfile;
bd5635a1 382{
318bf84f 383 register struct coff_pending *next, *next1;
bd5635a1
RP
384 register struct block *block;
385 register struct pending_block *pblock;
386 struct pending_block *opblock;
387 register int i;
388
389 /* Count the length of the list of symbols. */
390
391 for (next = *listhead, i = 0; next; next = next->next, i++);
392
393 block = (struct block *)
318bf84f 394 obstack_alloc (&objfile->symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
bd5635a1
RP
395
396 /* Copy the symbols into the block. */
397
398 BLOCK_NSYMS (block) = i;
399 for (next = *listhead; next; next = next->next)
400 BLOCK_SYM (block, --i) = next->symbol;
401
402 BLOCK_START (block) = start;
403 BLOCK_END (block) = end;
404 BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
405
406 /* Put the block in as the value of the symbol that names it. */
407
408 if (symbol)
409 {
410 SYMBOL_BLOCK_VALUE (symbol) = block;
411 BLOCK_FUNCTION (block) = symbol;
412 }
413 else
414 BLOCK_FUNCTION (block) = 0;
415
416 /* Now free the links of the list, and empty the list. */
417
418 for (next = *listhead; next; next = next1)
419 {
420 next1 = next->next;
f70be3e4 421 free ((PTR)next);
bd5635a1
RP
422 }
423 *listhead = 0;
424
425 /* Install this block as the superblock
426 of all blocks made since the start of this scope
427 that don't have superblocks yet. */
428
429 opblock = 0;
430 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
431 {
432 if (BLOCK_SUPERBLOCK (pblock->block) == 0)
433 BLOCK_SUPERBLOCK (pblock->block) = block;
434 opblock = pblock;
435 }
436
437 /* Record this block on the list of all blocks in the file.
438 Put it after opblock, or at the beginning if opblock is 0.
439 This puts the block in the list after all its subblocks. */
440
441 pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
442 pblock->block = block;
443 if (opblock)
444 {
445 pblock->next = opblock->next;
446 opblock->next = pblock;
447 }
448 else
449 {
450 pblock->next = pending_blocks;
451 pending_blocks = pblock;
452 }
453}
454
455static struct blockvector *
318bf84f
FF
456make_blockvector (objfile)
457 struct objfile *objfile;
bd5635a1
RP
458{
459 register struct pending_block *next, *next1;
460 register struct blockvector *blockvector;
461 register int i;
462
463 /* Count the length of the list of blocks. */
464
465 for (next = pending_blocks, i = 0; next; next = next->next, i++);
466
467 blockvector = (struct blockvector *)
318bf84f 468 obstack_alloc (&objfile->symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
bd5635a1
RP
469
470 /* Copy the blocks into the blockvector.
471 This is done in reverse order, which happens to put
472 the blocks into the proper order (ascending starting address).
318bf84f 473 coff_finish_block has hair to insert each block into the list
bd5635a1
RP
474 after its subblocks in order to make sure this is true. */
475
476 BLOCKVECTOR_NBLOCKS (blockvector) = i;
477 for (next = pending_blocks; next; next = next->next)
478 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
479
480 /* Now free the links of the list, and empty the list. */
481
482 for (next = pending_blocks; next; next = next1)
483 {
484 next1 = next->next;
f70be3e4 485 free ((PTR)next);
bd5635a1
RP
486 }
487 pending_blocks = 0;
488
489 return blockvector;
490}
491
492/* Manage the vector of line numbers. */
493
e1ce8aa5 494static void
318bf84f 495coff_record_line (line, pc)
bd5635a1
RP
496 int line;
497 CORE_ADDR pc;
498{
499 struct linetable_entry *e;
500 /* Make sure line vector is big enough. */
501
502 if (line_vector_index + 2 >= line_vector_length)
503 {
504 line_vector_length *= 2;
505 line_vector = (struct linetable *)
318bf84f 506 xrealloc ((char *) line_vector, sizeof (struct linetable)
bd5635a1
RP
507 + (line_vector_length
508 * sizeof (struct linetable_entry)));
509 }
510
511 e = line_vector->item + line_vector_index++;
512 e->line = line; e->pc = pc;
513}
514\f
515/* Start a new symtab for a new source file.
516 This is called when a COFF ".file" symbol is seen;
517 it indicates the start of data for one original source file. */
518
519static void
318bf84f 520coff_start_symtab ()
bd5635a1 521{
318bf84f
FF
522 coff_file_symbols = 0;
523 coff_global_symbols = 0;
524 coff_context_stack = 0;
bd5635a1 525 within_function = 0;
85f0a848 526 last_source_file = NULL;
bd5635a1 527
cadbb07a 528 /* Initialize the source file line number information for this file. */
bd5635a1 529
cadbb07a 530 if (line_vector) /* Unlikely, but maybe possible? */
f70be3e4 531 free ((PTR)line_vector);
bd5635a1
RP
532 line_vector_index = 0;
533 line_vector_length = 1000;
534 prev_line_number = -2; /* Force first line number to be explicit */
535 line_vector = (struct linetable *)
536 xmalloc (sizeof (struct linetable)
537 + line_vector_length * sizeof (struct linetable_entry));
538}
539
540/* Save the vital information from when starting to read a file,
541 for use when closing off the current file.
542 NAME is the file name the symbols came from, START_ADDR is the first
543 text address for the file, and SIZE is the number of bytes of text. */
544
545static void
546complete_symtab (name, start_addr, size)
547 char *name;
548 CORE_ADDR start_addr;
549 unsigned int size;
550{
551 last_source_file = savestring (name, strlen (name));
552 cur_src_start_addr = start_addr;
553 cur_src_end_addr = start_addr + size;
554
3624c875
FF
555 if (current_objfile -> ei.entry_point >= cur_src_start_addr &&
556 current_objfile -> ei.entry_point < cur_src_end_addr)
bd5635a1 557 {
3624c875
FF
558 current_objfile -> ei.entry_file_lowpc = cur_src_start_addr;
559 current_objfile -> ei.entry_file_highpc = cur_src_end_addr;
bd5635a1
RP
560 }
561}
562
563/* Finish the symbol definitions for one main source file,
564 close off all the lexical contexts for that file
565 (creating struct block's for them), then make the
566 struct symtab for that file and put it in the list of all such. */
567
568static void
318bf84f 569coff_end_symtab (objfile)
a048c8f5 570 struct objfile *objfile;
bd5635a1
RP
571{
572 register struct symtab *symtab;
318bf84f 573 register struct coff_context_stack *cstk;
bd5635a1
RP
574 register struct blockvector *blockvector;
575 register struct linetable *lv;
576
577 /* Finish the lexical context of the last function in the file. */
578
318bf84f 579 if (coff_context_stack)
bd5635a1 580 {
318bf84f
FF
581 cstk = coff_context_stack;
582 coff_context_stack = 0;
bd5635a1 583 /* Make a block for the local symbols within. */
318bf84f
FF
584 coff_finish_block (cstk->name, &coff_local_symbols, cstk->old_blocks,
585 cstk->start_addr, cur_src_end_addr, objfile);
f70be3e4 586 free ((PTR)cstk);
bd5635a1
RP
587 }
588
589 /* Ignore a file that has no functions with real debugging info. */
318bf84f 590 if (pending_blocks == 0 && coff_file_symbols == 0 && coff_global_symbols == 0)
bd5635a1 591 {
f70be3e4 592 free ((PTR)line_vector);
bd5635a1
RP
593 line_vector = 0;
594 line_vector_length = -1;
85f0a848 595 last_source_file = NULL;
bd5635a1
RP
596 return;
597 }
598
f70be3e4
JG
599 /* It is unfortunate that in amdcoff, pending blocks might not be ordered
600 in this stage. Especially, blocks for static functions will show up at
601 the end. We need to sort them, so tools like `find_pc_function' and
602 `find_pc_block' can work reliably. */
603 if (pending_blocks) {
604 /* FIXME! Remove this horrid bubble sort and use qsort!!! */
605 int swapped;
606 do {
607 struct pending_block *pb, *pbnext;
608
609 pb = pending_blocks, pbnext = pb->next;
610 swapped = 0;
611
612 while ( pbnext ) {
613
614 /* swap blocks if unordered! */
615
616 if (BLOCK_START(pb->block) < BLOCK_START(pbnext->block)) {
617 struct block *tmp = pb->block;
51b80b00 618 complain (&misordered_blocks_complaint, BLOCK_START (pb->block));
f70be3e4
JG
619 pb->block = pbnext->block;
620 pbnext->block = tmp;
621 swapped = 1;
622 }
623 pb = pbnext;
624 pbnext = pbnext->next;
625 }
626 } while (swapped);
627 }
628
a6e2b424
JG
629 /* Create the two top-level blocks for this file (STATIC_BLOCK and
630 GLOBAL_BLOCK). */
318bf84f
FF
631 coff_finish_block (0, &coff_file_symbols, 0, cur_src_start_addr, cur_src_end_addr, objfile);
632 coff_finish_block (0, &coff_global_symbols, 0, cur_src_start_addr, cur_src_end_addr, objfile);
bd5635a1
RP
633
634 /* Create the blockvector that points to all the file's blocks. */
318bf84f 635 blockvector = make_blockvector (objfile);
bd5635a1
RP
636
637 /* Now create the symtab object for this source file. */
a048c8f5 638 symtab = allocate_symtab (last_source_file, objfile);
bd5635a1
RP
639
640 /* Fill in its components. */
641 symtab->blockvector = blockvector;
642 symtab->free_code = free_linetable;
d8ce1326 643 symtab->free_ptr = 0;
bd5635a1 644 symtab->filename = last_source_file;
b203fc18 645 symtab->dirname = NULL;
bd5635a1
RP
646 lv = line_vector;
647 lv->nitems = line_vector_index;
648 symtab->linetable = (struct linetable *)
318bf84f 649 xrealloc ((char *) lv, (sizeof (struct linetable)
bd5635a1 650 + lv->nitems * sizeof (struct linetable_entry)));
b203fc18 651
a6e2b424
JG
652 free_named_symtabs (symtab->filename);
653
bd5635a1
RP
654 /* Reinitialize for beginning of new file. */
655 line_vector = 0;
656 line_vector_length = -1;
85f0a848 657 last_source_file = NULL;
bd5635a1
RP
658}
659\f
660static void
f70be3e4 661record_minimal_symbol (name, address, type)
bd5635a1
RP
662 char *name;
663 CORE_ADDR address;
f70be3e4 664 enum minimal_symbol_type type;
bd5635a1 665{
318bf84f 666 /* We don't want TDESC entry points in the minimal symbol table */
bd5635a1 667 if (name[0] == '@') return;
e140f1da 668
318bf84f
FF
669 /* mst_text isn't true, but apparently COFF doesn't tell us what it really
670 is, so this guess is more useful than mst_unknown. */
671 prim_record_minimal_symbol (savestring (name, strlen (name)),
bd5635a1 672 address,
f70be3e4 673 type);
bd5635a1
RP
674}
675\f
676/* coff_symfile_init ()
677 is the coff-specific initialization routine for reading symbols.
3624c875 678 It is passed a struct objfile which contains, among other things,
bd5635a1
RP
679 the BFD for the file whose symbols are being read, and a slot for
680 a pointer to "private data" which we fill with cookies and other
681 treats for coff_symfile_read ().
682
683 We will only be called if this is a COFF or COFF-like file.
684 BFD handles figuring out the format of the file, and code in symtab.c
685 uses BFD's determination to vector to us.
686
687 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
688
689struct coff_symfile_info {
690 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
691 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
692};
693
d8ce1326
JG
694static int text_bfd_scnum;
695
9bba3334 696static void
80d68b1d
FF
697coff_symfile_init (objfile)
698 struct objfile *objfile;
bd5635a1 699{
d8ce1326 700 asection *section;
80d68b1d 701 bfd *abfd = objfile->obfd;
bd5635a1
RP
702
703 /* Allocate struct to keep track of the symfile */
80d68b1d
FF
704 objfile -> sym_private = xmmalloc (objfile -> md,
705 sizeof (struct coff_symfile_info));
bd5635a1 706
3624c875
FF
707 init_entry_point_info (objfile);
708
709 /* Save the section number for the text section */
f70be3e4
JG
710 section = bfd_get_section_by_name(abfd,".text");
711 if (section)
3624c875 712 text_bfd_scnum = section->index;
bd5635a1 713 else
3624c875 714 text_bfd_scnum = -1;
bd5635a1
RP
715}
716
717/* This function is called for every section; it finds the outer limits
718 of the line table (minimum and maximum file offset) so that the
719 mainline code can read the whole thing for efficiency. */
720
e1ce8aa5 721/* ARGSUSED */
bd5635a1
RP
722static void
723find_linenos (abfd, asect, vpinfo)
724 bfd *abfd;
725 sec_ptr asect;
318bf84f 726 PTR vpinfo;
bd5635a1
RP
727{
728 struct coff_symfile_info *info;
729 int size, count;
730 file_ptr offset, maxoff;
731
732/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
733 count = asect->lineno_count;
734/* End of warning */
735
736 if (count == 0)
737 return;
d8ce1326 738 size = count * local_linesz;
bd5635a1
RP
739
740 info = (struct coff_symfile_info *)vpinfo;
741/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
742 offset = asect->line_filepos;
743/* End of warning */
744
745 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
746 info->min_lineno_offset = offset;
747
748 maxoff = offset + size;
749 if (maxoff > info->max_lineno_offset)
750 info->max_lineno_offset = maxoff;
751}
752
753
e1ce8aa5
JK
754/* The BFD for this file -- only good while we're actively reading
755 symbols into a psymtab or a symtab. */
756
757static bfd *symfile_bfd;
758
bd5635a1
RP
759/* Read a symbol file, after initialization by coff_symfile_init. */
760/* FIXME! Addr and Mainline are not used yet -- this will not work for
761 shared libraries or add_file! */
762
61a7292f 763/* ARGSUSED */
9bba3334 764static void
85f0a848 765coff_symfile_read (objfile, section_offsets, mainline)
80d68b1d 766 struct objfile *objfile;
85f0a848 767 struct section_offsets *section_offsets;
bd5635a1
RP
768 int mainline;
769{
80d68b1d
FF
770 struct coff_symfile_info *info;
771 bfd *abfd = objfile->obfd;
d8ce1326 772 coff_data_type *cdata = coff_data (abfd);
bd5635a1
RP
773 char *name = bfd_get_filename (abfd);
774 int desc;
775 register int val;
776 int num_symbols;
777 int symtab_offset;
778 int stringtab_offset;
779
80d68b1d 780 info = (struct coff_symfile_info *) objfile -> sym_private;
bd5635a1
RP
781 symfile_bfd = abfd; /* Kludge for swap routines */
782
783/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
d8ce1326 784 desc = fileno ((FILE *)(abfd->iostream)); /* File descriptor */
bd5635a1 785 num_symbols = bfd_get_symcount (abfd); /* How many syms */
d8ce1326
JG
786 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
787 stringtab_offset = symtab_offset + /* String table file offset */
788 num_symbols * cdata->local_symesz;
789
790 /* Set a few file-statics that give us specific information about
791 the particular COFF file format we're reading. */
792 local_linesz = cdata->local_linesz;
793 local_n_btmask = cdata->local_n_btmask;
794 local_n_btshft = cdata->local_n_btshft;
795 local_n_tmask = cdata->local_n_tmask;
796 local_n_tshift = cdata->local_n_tshift;
797 local_linesz = cdata->local_linesz;
798 local_symesz = cdata->local_symesz;
799 local_auxesz = cdata->local_auxesz;
800
801 /* Allocate space for raw symbol and aux entries, based on their
802 space requirements as reported by BFD. */
803 temp_sym = (char *) xmalloc
804 (cdata->local_symesz + cdata->local_auxesz);
805 temp_aux = temp_sym + cdata->local_symesz;
806 make_cleanup (free_current_contents, &temp_sym);
bd5635a1
RP
807/* End of warning */
808
bd5635a1
RP
809 /* Read the line number table, all at once. */
810 info->min_lineno_offset = 0;
811 info->max_lineno_offset = 0;
f70be3e4 812 bfd_map_over_sections (abfd, find_linenos, (PTR)info);
bd5635a1
RP
813
814 val = init_lineno (desc, info->min_lineno_offset,
815 info->max_lineno_offset - info->min_lineno_offset);
816 if (val < 0)
817 error ("\"%s\": error reading line numbers\n", name);
818
819 /* Now read the string table, all at once. */
820
821 val = init_stringtab (desc, stringtab_offset);
822 if (val < 0)
a048c8f5 823 error ("\"%s\": can't get string table", name);
bd5635a1
RP
824 make_cleanup (free_stringtab, 0);
825
318bf84f
FF
826 init_minimal_symbol_collection ();
827 make_cleanup (discard_minimal_symbols, 0);
bd5635a1
RP
828
829 /* Now that the executable file is positioned at symbol table,
830 process it and define symbols accordingly. */
831
f70be3e4 832 read_coff_symtab ((long)symtab_offset, num_symbols, objfile);
bd5635a1
RP
833
834 /* Sort symbols alphabetically within each block. */
835
836 sort_all_symtab_syms ();
837
318bf84f
FF
838 /* Install any minimal symbols that have been collected as the current
839 minimal symbols for this objfile. */
bd5635a1 840
80d68b1d 841 install_minimal_symbols (objfile);
bd5635a1
RP
842}
843
9bba3334 844static void
f70be3e4
JG
845coff_new_init (ignore)
846 struct objfile *ignore;
bd5635a1 847{
a048c8f5 848 /* Nothin' to do */
bd5635a1 849}
80d68b1d
FF
850
851/* Perform any local cleanups required when we are done with a particular
852 objfile. I.E, we are in the process of discarding all symbol information
853 for an objfile, freeing up all memory held for it, and unlinking the
854 objfile struct from the global list of known objfiles. */
855
856static void
857coff_symfile_finish (objfile)
858 struct objfile *objfile;
859{
860 if (objfile -> sym_private != NULL)
861 {
862 mfree (objfile -> md, objfile -> sym_private);
863 }
864}
865
bd5635a1 866\f
bd5635a1
RP
867/* Given pointers to a symbol table in coff style exec file,
868 analyze them and create struct symtab's describing the symbols.
869 NSYMS is the number of symbols in the symbol table.
870 We read them one at a time using read_one_sym (). */
871
872static void
f70be3e4
JG
873read_coff_symtab (symtab_offset, nsyms, objfile)
874 long symtab_offset;
bd5635a1 875 int nsyms;
a048c8f5 876 struct objfile *objfile;
bd5635a1 877{
bd5635a1 878 FILE *stream;
318bf84f 879 register struct coff_context_stack *new;
bd5635a1
RP
880 struct coff_symbol coff_symbol;
881 register struct coff_symbol *cs = &coff_symbol;
dcc35536
JG
882 static struct internal_syment main_sym;
883 static union internal_auxent main_aux;
bd5635a1 884 struct coff_symbol fcn_cs_saved;
dcc35536
JG
885 static struct internal_syment fcn_sym_saved;
886 static union internal_auxent fcn_aux_saved;
f70be3e4
JG
887 struct symtab *s;
888
bd5635a1
RP
889 /* A .file is open. */
890 int in_source_file = 0;
891 int num_object_files = 0;
892 int next_file_symnum = -1;
893
894 /* Name of the current file. */
895 char *filestring = "";
f70be3e4
JG
896 int depth = 0;
897 int fcn_first_line = 0;
898 int fcn_last_line = 0;
899 int fcn_start_addr = 0;
900 long fcn_line_ptr = 0;
bd5635a1 901 struct cleanup *old_chain;
f70be3e4 902 int val;
bd5635a1 903
f70be3e4
JG
904 stream = fopen (objfile->name, FOPEN_RB);
905 if (!stream)
906 perror_with_name(objfile->name);
bd5635a1 907
f70be3e4
JG
908 /* Position to read the symbol table. */
909 val = fseek (stream, (long)symtab_offset, 0);
910 if (val < 0)
911 perror_with_name (objfile->name);
bd5635a1 912
a048c8f5
JG
913 /* These cleanups will be discarded below if we succeed. */
914 old_chain = make_cleanup (free_objfile, objfile);
bd5635a1 915 make_cleanup (fclose, stream);
a048c8f5 916
318bf84f 917 current_objfile = objfile;
bd5635a1
RP
918 nlist_stream_global = stream;
919 nlist_nsyms_global = nsyms;
85f0a848 920 last_source_file = NULL;
4ed3a9ea 921 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
bd5635a1 922
cadbb07a 923 if (type_vector) /* Get rid of previous one */
f70be3e4 924 free ((PTR)type_vector);
bd5635a1 925 type_vector_length = 160;
159a075e
SG
926 type_vector = (struct type **)
927 xmalloc (type_vector_length * sizeof (struct type *));
4ed3a9ea 928 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
bd5635a1 929
318bf84f 930 coff_start_symtab ();
bd5635a1
RP
931
932 symnum = 0;
933 while (symnum < nsyms)
934 {
935 QUIT; /* Make this command interruptable. */
936 read_one_sym (cs, &main_sym, &main_aux);
937
938#ifdef SEM
939 temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
940 cs->c_name[2] << 8 | cs->c_name[3];
941 if (int_sem_val == temp_sem_val)
942 last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
943#endif
944
945 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
946 {
947 if (last_source_file)
318bf84f 948 coff_end_symtab (objfile);
bd5635a1 949
318bf84f 950 coff_start_symtab ();
bd5635a1
RP
951 complete_symtab ("_globals_", 0, first_object_file_end);
952 /* done with all files, everything from here on out is globals */
953 }
954
955 /* Special case for file with type declarations only, no text. */
8714ff35
JK
956 if (!last_source_file && SDB_TYPE (cs->c_type)
957 && cs->c_secnum == N_DEBUG)
bd5635a1
RP
958 complete_symtab (filestring, 0, 0);
959
960 /* Typedefs should not be treated as symbol definitions. */
961 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
962 {
318bf84f 963 /* record as a minimal symbol. if we get '.bf' next,
bd5635a1
RP
964 * then we undo this step
965 */
f70be3e4 966 record_minimal_symbol (cs->c_name, cs->c_value, mst_text);
bd5635a1
RP
967
968 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
969 fcn_start_addr = cs->c_value;
970 fcn_cs_saved = *cs;
971 fcn_sym_saved = main_sym;
972 fcn_aux_saved = main_aux;
973 continue;
974 }
975
976 switch (cs->c_sclass)
977 {
978 case C_EFCN:
979 case C_EXTDEF:
980 case C_ULABEL:
981 case C_USTATIC:
982 case C_LINE:
983 case C_ALIAS:
984 case C_HIDDEN:
7e258d18 985 complain (&bad_sclass_complaint, cs->c_name);
bd5635a1
RP
986 break;
987
988 case C_FILE:
989 /*
990 * c_value field contains symnum of next .file entry in table
991 * or symnum of first global after last .file.
992 */
993 next_file_symnum = cs->c_value;
994 filestring = getfilename (&main_aux);
995 /*
996 * Complete symbol table for last object file
997 * containing debugging information.
998 */
999 if (last_source_file)
1000 {
318bf84f
FF
1001 coff_end_symtab (objfile);
1002 coff_start_symtab ();
bd5635a1
RP
1003 }
1004 in_source_file = 1;
1005 break;
1006
1007 case C_STAT:
1008 if (cs->c_name[0] == '.') {
d8ce1326
JG
1009 if (strcmp (cs->c_name, ".text") == 0) {
1010 /* FIXME: don't wire in ".text" as section name
1011 or symbol name! */
bd5635a1
RP
1012 if (++num_object_files == 1) {
1013 /* last address of startup file */
1014 first_object_file_end = cs->c_value +
1015 main_aux.x_scn.x_scnlen;
1016 }
1017 /* Check for in_source_file deals with case of
1018 a file with debugging symbols
1019 followed by a later file with no symbols. */
1020 if (in_source_file)
1021 complete_symtab (filestring, cs->c_value,
1022 main_aux.x_scn.x_scnlen);
1023 in_source_file = 0;
1024 }
1025 /* flush rest of '.' symbols */
1026 break;
1027 }
8714ff35 1028 else if (!SDB_TYPE (cs->c_type)
bd5635a1
RP
1029 && cs->c_name[0] == 'L'
1030 && (strncmp (cs->c_name, "LI%", 3) == 0
8714ff35 1031 || strncmp (cs->c_name, "LF%", 3) == 0
bd5635a1
RP
1032 || strncmp (cs->c_name,"LC%",3) == 0
1033 || strncmp (cs->c_name,"LP%",3) == 0
1034 || strncmp (cs->c_name,"LPB%",4) == 0
1035 || strncmp (cs->c_name,"LBB%",4) == 0
1036 || strncmp (cs->c_name,"LBE%",4) == 0
1037 || strncmp (cs->c_name,"LPBX%",5) == 0))
1038 /* At least on a 3b1, gcc generates swbeg and string labels
1039 that look like this. Ignore them. */
1040 break;
1041 /* fall in for static symbols that don't start with '.' */
1042 case C_EXT:
8714ff35 1043 if (!SDB_TYPE (cs->c_type)) {
d8ce1326
JG
1044 /* FIXME: This is BOGUS Will Robinson!
1045 Coff should provide the SEC_CODE flag for executable sections,
1046 then if we could look up sections by section number we
1047 could see if the flags indicate SEC_CODE. If so, then
318bf84f
FF
1048 record this symbol as a function in the minimal symbol table.
1049 But why are absolute syms recorded as functions, anyway? */
d8ce1326 1050 if (cs->c_secnum <= text_bfd_scnum+1) {/* text or abs */
f70be3e4
JG
1051 record_minimal_symbol (cs->c_name, cs->c_value,
1052 mst_text);
bd5635a1
RP
1053 break;
1054 } else {
f70be3e4
JG
1055 record_minimal_symbol (cs->c_name, cs->c_value,
1056 mst_data);
1057 break;
bd5635a1
RP
1058 }
1059 }
4ed3a9ea 1060 process_coff_symbol (cs, &main_aux, objfile);
bd5635a1
RP
1061 break;
1062
1063 case C_FCN:
1064 if (strcmp (cs->c_name, ".bf") == 0)
1065 {
1066 within_function = 1;
1067
1068 /* value contains address of first non-init type code */
1069 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
1070 contains line number of '{' } */
e64fbb3a 1071 if (cs->c_naux != 1)
51b80b00 1072 complain (&bf_no_aux_complaint, cs->c_symnum);
bd5635a1
RP
1073 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1074
318bf84f
FF
1075 new = (struct coff_context_stack *)
1076 xmalloc (sizeof (struct coff_context_stack));
bd5635a1
RP
1077 new->depth = depth = 0;
1078 new->next = 0;
318bf84f 1079 coff_context_stack = new;
bd5635a1
RP
1080 new->locals = 0;
1081 new->old_blocks = pending_blocks;
1082 new->start_addr = fcn_start_addr;
1083 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
1084 new->name = process_coff_symbol (&fcn_cs_saved,
318bf84f 1085 &fcn_aux_saved, objfile);
bd5635a1
RP
1086 }
1087 else if (strcmp (cs->c_name, ".ef") == 0)
1088 {
1089 /* the value of .ef is the address of epilogue code;
1090 * not useful for gdb
1091 */
1092 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1093 contains number of lines to '}' */
318bf84f 1094 new = coff_context_stack;
bd5635a1
RP
1095 if (new == 0)
1096 {
51b80b00 1097 complain (&ef_complaint, cs->c_symnum);
bd5635a1
RP
1098 within_function = 0;
1099 break;
1100 }
e64fbb3a 1101 if (cs->c_naux != 1) {
51b80b00 1102 complain (&ef_no_aux_complaint, cs->c_symnum);
e64fbb3a
JG
1103 fcn_last_line = 0x7FFFFFFF;
1104 } else {
1105 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1106 }
bd5635a1
RP
1107 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
1108
318bf84f 1109 coff_finish_block (new->name, &coff_local_symbols, new->old_blocks,
bd5635a1
RP
1110 new->start_addr,
1111#if defined (FUNCTION_EPILOGUE_SIZE)
1112 /* This macro should be defined only on
1113 machines where the
1114 fcn_aux_saved.x_sym.x_misc.x_fsize
1115 field is always zero.
1116 So use the .bf record information that
1117 points to the epilogue and add the size
1118 of the epilogue. */
318bf84f 1119 cs->c_value + FUNCTION_EPILOGUE_SIZE,
bd5635a1
RP
1120#else
1121 fcn_cs_saved.c_value +
318bf84f 1122 fcn_aux_saved.x_sym.x_misc.x_fsize,
bd5635a1 1123#endif
318bf84f 1124 objfile
bd5635a1 1125 );
318bf84f 1126 coff_context_stack = 0;
bd5635a1 1127 within_function = 0;
f70be3e4 1128 free ((PTR)new);
bd5635a1
RP
1129 }
1130 break;
1131
1132 case C_BLOCK:
1133 if (strcmp (cs->c_name, ".bb") == 0)
1134 {
318bf84f
FF
1135 new = (struct coff_context_stack *)
1136 xmalloc (sizeof (struct coff_context_stack));
bd5635a1
RP
1137 depth++;
1138 new->depth = depth;
318bf84f
FF
1139 new->next = coff_context_stack;
1140 coff_context_stack = new;
1141 new->locals = coff_local_symbols;
bd5635a1
RP
1142 new->old_blocks = pending_blocks;
1143 new->start_addr = cs->c_value;
1144 new->name = 0;
318bf84f 1145 coff_local_symbols = 0;
bd5635a1
RP
1146 }
1147 else if (strcmp (cs->c_name, ".eb") == 0)
1148 {
318bf84f 1149 new = coff_context_stack;
bd5635a1
RP
1150 if (new == 0 || depth != new->depth)
1151 error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1152 symnum);
318bf84f 1153 if (coff_local_symbols && coff_context_stack->next)
bd5635a1
RP
1154 {
1155 /* Make a block for the local symbols within. */
318bf84f
FF
1156 coff_finish_block (0, &coff_local_symbols, new->old_blocks,
1157 new->start_addr, cs->c_value, objfile);
bd5635a1
RP
1158 }
1159 depth--;
318bf84f
FF
1160 coff_local_symbols = new->locals;
1161 coff_context_stack = new->next;
f70be3e4 1162 free ((PTR)new);
bd5635a1
RP
1163 }
1164 break;
bd5635a1
RP
1165
1166 default:
4ed3a9ea 1167 process_coff_symbol (cs, &main_aux, objfile);
bd5635a1
RP
1168 break;
1169 }
1170 }
1171
1172 if (last_source_file)
318bf84f 1173 coff_end_symtab (objfile);
bd5635a1 1174 fclose (stream);
f70be3e4
JG
1175
1176 /* Patch up any opaque types (references to types that are not defined
1177 in the file where they are referenced, e.g. "struct foo *bar"). */
1178 ALL_OBJFILE_SYMTABS (objfile, s)
1179 patch_opaque_types (s);
1180
bd5635a1 1181 discard_cleanups (old_chain);
318bf84f 1182 current_objfile = NULL;
bd5635a1
RP
1183}
1184\f
1185/* Routines for reading headers and symbols from executable. */
1186
1187#ifdef FIXME
1188/* Move these XXXMAGIC symbol defns into BFD! */
1189
1190/* Read COFF file header, check magic number,
1191 and return number of symbols. */
1192read_file_hdr (chan, file_hdr)
1193 int chan;
1194 FILHDR *file_hdr;
1195{
1196 lseek (chan, 0L, 0);
1197 if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1198 return -1;
1199
1200 switch (file_hdr->f_magic)
1201 {
1202#ifdef MC68MAGIC
1203 case MC68MAGIC:
1204#endif
1205#ifdef NS32GMAGIC
1206 case NS32GMAGIC:
1207 case NS32SMAGIC:
1208#endif
1209#ifdef I386MAGIC
1210 case I386MAGIC:
1211#endif
1212#ifdef CLIPPERMAGIC
1213 case CLIPPERMAGIC:
1214#endif
1215#if defined (MC68KWRMAGIC) \
1216 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1217 case MC68KWRMAGIC:
1218#endif
1219#ifdef MC68KROMAGIC
1220 case MC68KROMAGIC:
1221 case MC68KPGMAGIC:
1222#endif
1223#ifdef MC88DGMAGIC
1224 case MC88DGMAGIC:
1225#endif
1226#ifdef MC88MAGIC
1227 case MC88MAGIC:
1228#endif
1229#ifdef I960ROMAGIC
1230 case I960ROMAGIC: /* Intel 960 */
1231#endif
1232#ifdef I960RWMAGIC
1233 case I960RWMAGIC: /* Intel 960 */
1234#endif
1235 return file_hdr->f_nsyms;
1236
1237 default:
1238#ifdef BADMAG
1239 if (BADMAG(file_hdr))
1240 return -1;
1241 else
1242 return file_hdr->f_nsyms;
1243#else
1244 return -1;
1245#endif
1246 }
1247}
1248#endif
1249
dcc35536
JG
1250/* Read the next symbol, swap it, and return it in both internal_syment
1251 form, and coff_symbol form. Also return its first auxent, if any,
1252 in internal_auxent form, and skip any other auxents. */
bd5635a1
RP
1253
1254static void
1255read_one_sym (cs, sym, aux)
1256 register struct coff_symbol *cs;
dcc35536
JG
1257 register struct internal_syment *sym;
1258 register union internal_auxent *aux;
bd5635a1 1259{
bd5635a1
RP
1260 int i;
1261
1262 cs->c_symnum = symnum;
d8ce1326
JG
1263 fread (temp_sym, local_symesz, 1, nlist_stream_global);
1264 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
e64fbb3a
JG
1265 cs->c_naux = sym->n_numaux & 0xff;
1266 if (cs->c_naux >= 1)
bd5635a1 1267 {
d8ce1326
JG
1268 fread (temp_aux, local_auxesz, 1, nlist_stream_global);
1269 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
1270 (char *)aux);
bd5635a1
RP
1271 /* If more than one aux entry, read past it (only the first aux
1272 is important). */
e64fbb3a 1273 for (i = 1; i < cs->c_naux; i++)
d8ce1326 1274 fread (temp_aux, local_auxesz, 1, nlist_stream_global);
bd5635a1
RP
1275 }
1276 cs->c_name = getsymname (sym);
1277 cs->c_value = sym->n_value;
1278 cs->c_sclass = (sym->n_sclass & 0xff);
1279 cs->c_secnum = sym->n_scnum;
1280 cs->c_type = (unsigned) sym->n_type;
8714ff35
JK
1281 if (!SDB_TYPE (cs->c_type))
1282 cs->c_type = 0;
bd5635a1 1283
e64fbb3a 1284 symnum += 1 + cs->c_naux;
bd5635a1
RP
1285}
1286\f
1287/* Support for string table handling */
1288
1289static char *stringtab = NULL;
1290
1291static int
1292init_stringtab (chan, offset)
1293 int chan;
1294 long offset;
1295{
1296 long length;
1297 int val;
1298 unsigned char lengthbuf[4];
1299
1300 if (stringtab)
1301 {
1302 free (stringtab);
1303 stringtab = NULL;
1304 }
1305
1306 if (lseek (chan, offset, 0) < 0)
1307 return -1;
1308
1309 val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
dcc35536 1310 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
bd5635a1
RP
1311
1312 /* If no string table is needed, then the file may end immediately
1313 after the symbols. Just return with `stringtab' set to null. */
1314 if (val != sizeof length || length < sizeof length)
1315 return 0;
1316
1317 stringtab = (char *) xmalloc (length);
1318 if (stringtab == NULL)
1319 return -1;
1320
4ed3a9ea 1321 memcpy (stringtab, &length, sizeof length);
bd5635a1
RP
1322 if (length == sizeof length) /* Empty table -- just the count */
1323 return 0;
1324
1325 val = myread (chan, stringtab + sizeof length, length - sizeof length);
1326 if (val != length - sizeof length || stringtab[length - 1] != '\0')
1327 return -1;
1328
1329 return 0;
1330}
1331
1332static void
1333free_stringtab ()
1334{
1335 if (stringtab)
1336 free (stringtab);
1337 stringtab = NULL;
1338}
1339
1340static char *
1341getsymname (symbol_entry)
dcc35536 1342 struct internal_syment *symbol_entry;
bd5635a1
RP
1343{
1344 static char buffer[SYMNMLEN+1];
1345 char *result;
1346
dcc35536 1347 if (symbol_entry->_n._n_n._n_zeroes == 0)
bd5635a1 1348 {
dcc35536 1349 result = stringtab + symbol_entry->_n._n_n._n_offset;
bd5635a1
RP
1350 }
1351 else
1352 {
dcc35536 1353 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
bd5635a1
RP
1354 buffer[SYMNMLEN] = '\0';
1355 result = buffer;
1356 }
1357 return result;
1358}
1359
1360static char *
1361getfilename (aux_entry)
dcc35536 1362 union internal_auxent *aux_entry;
bd5635a1
RP
1363{
1364 static char buffer[BUFSIZ];
1365 register char *temp;
1366 char *result;
bd5635a1
RP
1367
1368#ifndef COFF_NO_LONG_FILE_NAMES
8aa13b87
JK
1369#if defined (x_zeroes)
1370 /* Data General. */
1371 if (aux_entry->x_zeroes == 0)
1372 strcpy (buffer, stringtab + aux_entry->x_offset);
1373#else /* no x_zeroes */
bd5635a1
RP
1374 if (aux_entry->x_file.x_n.x_zeroes == 0)
1375 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
8aa13b87 1376#endif /* no x_zeroes */
bd5635a1
RP
1377 else
1378#endif /* COFF_NO_LONG_FILE_NAMES */
1379 {
1380#if defined (x_name)
1381 /* Data General. */
1382 strncpy (buffer, aux_entry->x_name, FILNMLEN);
1383#else
1384 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1385#endif
1386 buffer[FILNMLEN] = '\0';
1387 }
1388 result = buffer;
318bf84f 1389 if ((temp = strrchr (result, '/')) != NULL)
bd5635a1
RP
1390 result = temp + 1;
1391 return (result);
1392}
1393\f
1394/* Support for line number handling */
1395static char *linetab = NULL;
1396static long linetab_offset;
1397static unsigned long linetab_size;
1398
dcc35536
JG
1399/* Read in all the line numbers for fast lookups later. Leave them in
1400 external (unswapped) format in memory; we'll swap them as we enter
1401 them into GDB's data structures. */
bd5635a1
RP
1402
1403static int
1404init_lineno (chan, offset, size)
1405 int chan;
1406 long offset;
1407 int size;
1408{
1409 int val;
bd5635a1 1410
61a7292f
SG
1411 linetab_offset = offset;
1412 linetab_size = size;
1413
1414 if (size == 0)
1415 return 0;
1416
bd5635a1
RP
1417 if (lseek (chan, offset, 0) < 0)
1418 return -1;
1419
e64fbb3a
JG
1420 /* Allocate the desired table, plus a sentinel */
1421 linetab = (char *) xmalloc (size + local_linesz);
bd5635a1
RP
1422
1423 val = myread (chan, linetab, size);
1424 if (val != size)
1425 return -1;
1426
e64fbb3a 1427 /* Terminate it with an all-zero sentinel record */
4ed3a9ea 1428 memset (linetab + size, 0, local_linesz);
e64fbb3a 1429
bd5635a1
RP
1430 make_cleanup (free, linetab); /* Be sure it gets de-allocated. */
1431 return 0;
1432}
1433
1434#if !defined (L_LNNO32)
1435#define L_LNNO32(lp) ((lp)->l_lnno)
1436#endif
1437
1438static void
1439enter_linenos (file_offset, first_line, last_line)
1440 long file_offset;
1441 register int first_line;
1442 register int last_line;
1443{
1444 register char *rawptr;
dcc35536 1445 struct internal_lineno lptr;
bd5635a1
RP
1446
1447 if (file_offset < linetab_offset)
1448 {
51b80b00 1449 complain (&lineno_complaint, file_offset);
63989338
JG
1450 if (file_offset > linetab_size) /* Too big to be an offset? */
1451 return;
1452 file_offset += linetab_offset; /* Try reading at that linetab offset */
bd5635a1
RP
1453 }
1454
1455 rawptr = &linetab[file_offset - linetab_offset];
1456
1457 /* skip first line entry for each function */
d8ce1326 1458 rawptr += local_linesz;
bd5635a1
RP
1459 /* line numbers start at one for the first line of the function */
1460 first_line--;
1461
dcc35536 1462 for (;;) {
d8ce1326
JG
1463 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1464 rawptr += local_linesz;
e64fbb3a 1465 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
dcc35536 1466 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
318bf84f 1467 coff_record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
dcc35536
JG
1468 else
1469 break;
1470 }
bd5635a1
RP
1471}
1472\f
bd5635a1
RP
1473static void
1474patch_type (type, real_type)
1475 struct type *type;
1476 struct type *real_type;
1477{
1478 register struct type *target = TYPE_TARGET_TYPE (type);
1479 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1480 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1481
1482 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1483 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
85f0a848 1484 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
bd5635a1 1485
4ed3a9ea 1486 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
bd5635a1
RP
1487
1488 if (TYPE_NAME (real_target))
1489 {
1490 if (TYPE_NAME (target))
1491 free (TYPE_NAME (target));
58ae87f6 1492 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
bd5635a1
RP
1493 }
1494}
1495
7e258d18 1496/* Patch up all appropriate typedef symbols in the opaque_type_chains
f70be3e4 1497 so that they can be used to print out opaque data structures properly. */
bd5635a1 1498
f70be3e4
JG
1499static void
1500patch_opaque_types (s)
318bf84f 1501 struct symtab *s;
318bf84f
FF
1502{
1503 register struct block *b;
1504 register int i;
1505 register struct symbol *real_sym;
1506
1507 /* Go through the per-file symbols only */
1508 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1509 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
bd5635a1 1510 {
318bf84f
FF
1511 /* Find completed typedefs to use to fix opaque ones.
1512 Remove syms from the chain when their types are stored,
1513 but search the whole chain, as there may be several syms
1514 from different files with the same name. */
1515 real_sym = BLOCK_SYM (b, i);
1516 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1517 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1518 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1519 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
bd5635a1 1520 {
318bf84f
FF
1521 register char *name = SYMBOL_NAME (real_sym);
1522 register int hash = hashname (name);
1523 register struct symbol *sym, *prev;
1524
1525 prev = 0;
1526 for (sym = opaque_type_chain[hash]; sym;)
bd5635a1 1527 {
318bf84f
FF
1528 if (name[0] == SYMBOL_NAME (sym)[0] &&
1529 !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
bd5635a1 1530 {
318bf84f 1531 if (prev)
bd5635a1 1532 {
318bf84f 1533 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
bd5635a1
RP
1534 }
1535 else
1536 {
318bf84f 1537 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
bd5635a1 1538 }
318bf84f
FF
1539
1540 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1541
1542 if (prev)
1543 {
1544 sym = SYMBOL_VALUE_CHAIN (prev);
1545 }
1546 else
1547 {
1548 sym = opaque_type_chain[hash];
1549 }
1550 }
1551 else
1552 {
1553 prev = sym;
1554 sym = SYMBOL_VALUE_CHAIN (sym);
bd5635a1
RP
1555 }
1556 }
1557 }
1558 }
1559}
1560\f
bd5635a1 1561static struct symbol *
318bf84f 1562process_coff_symbol (cs, aux, objfile)
bd5635a1 1563 register struct coff_symbol *cs;
dcc35536 1564 register union internal_auxent *aux;
318bf84f 1565 struct objfile *objfile;
bd5635a1
RP
1566{
1567 register struct symbol *sym
318bf84f 1568 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
bd5635a1
RP
1569 char *name;
1570#ifdef NAMES_HAVE_UNDERSCORE
1571 int offset = 1;
1572#else
1573 int offset = 0;
1574#endif
318bf84f 1575 struct type *temptype;
bd5635a1 1576
4ed3a9ea 1577 memset (sym, 0, sizeof (struct symbol));
bd5635a1
RP
1578 name = cs->c_name;
1579 name = (name[0] == '_' ? name + offset : name);
318bf84f 1580 SYMBOL_NAME (sym) = obstack_copy0 (&objfile->symbol_obstack, name, strlen (name));
bd5635a1
RP
1581
1582 /* default assumptions */
1583 SYMBOL_VALUE (sym) = cs->c_value;
1584 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1585
1586 if (ISFCN (cs->c_type))
1587 {
d8ce1326
JG
1588#if 0
1589 /* FIXME: This has NOT been tested. The DBX version has.. */
1590 /* Generate a template for the type of this function. The
1591 types of the arguments will be added as we read the symbol
1592 table. */
1593 struct type *new = (struct type *)
318bf84f 1594 obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
d8ce1326 1595
4ed3a9ea 1596 memcpy (new, lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
51b57ded 1597 sizeof(struct type));
d8ce1326
JG
1598 SYMBOL_TYPE (sym) = new;
1599 in_function_type = SYMBOL_TYPE(sym);
1600#else
1601 SYMBOL_TYPE(sym) =
1602 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1603#endif
1604
bd5635a1
RP
1605 SYMBOL_CLASS (sym) = LOC_BLOCK;
1606 if (cs->c_sclass == C_STAT)
318bf84f 1607 coff_add_symbol_to_list (sym, &coff_file_symbols);
bd5635a1 1608 else if (cs->c_sclass == C_EXT)
318bf84f 1609 coff_add_symbol_to_list (sym, &coff_global_symbols);
bd5635a1
RP
1610 }
1611 else
1612 {
1613 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1614 switch (cs->c_sclass)
1615 {
1616 case C_NULL:
1617 break;
1618
1619 case C_AUTO:
1620 SYMBOL_CLASS (sym) = LOC_LOCAL;
318bf84f 1621 coff_add_symbol_to_list (sym, &coff_local_symbols);
bd5635a1
RP
1622 break;
1623
1624 case C_EXT:
1625 SYMBOL_CLASS (sym) = LOC_STATIC;
1626 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
318bf84f 1627 coff_add_symbol_to_list (sym, &coff_global_symbols);
bd5635a1
RP
1628 break;
1629
1630 case C_STAT:
1631 SYMBOL_CLASS (sym) = LOC_STATIC;
1632 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1633 if (within_function) {
1634 /* Static symbol of local scope */
318bf84f 1635 coff_add_symbol_to_list (sym, &coff_local_symbols);
bd5635a1
RP
1636 }
1637 else {
1638 /* Static symbol at top level of file */
318bf84f 1639 coff_add_symbol_to_list (sym, &coff_file_symbols);
bd5635a1
RP
1640 }
1641 break;
1642
d8ce1326
JG
1643#ifdef C_GLBLREG /* AMD coff */
1644 case C_GLBLREG:
1645#endif
bd5635a1
RP
1646 case C_REG:
1647 SYMBOL_CLASS (sym) = LOC_REGISTER;
d8ce1326 1648 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
318bf84f 1649 coff_add_symbol_to_list (sym, &coff_local_symbols);
bd5635a1
RP
1650 break;
1651
1652 case C_LABEL:
1653 break;
1654
1655 case C_ARG:
1656 SYMBOL_CLASS (sym) = LOC_ARG;
d8ce1326 1657#if 0
7e258d18 1658 /* FIXME: This has not been tested. */
d8ce1326
JG
1659 /* Add parameter to function. */
1660 add_param_to_type(&in_function_type,sym);
1661#endif
318bf84f 1662 coff_add_symbol_to_list (sym, &coff_local_symbols);
bd5635a1
RP
1663#if !defined (BELIEVE_PCC_PROMOTION)
1664 /* If PCC says a parameter is a short or a char,
1665 it is really an int. */
318bf84f
FF
1666 temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
1667 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1668 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1669 {
1670 SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1671 ? lookup_fundamental_type (current_objfile,
1672 FT_UNSIGNED_INTEGER)
1673 : temptype;
1674 }
bd5635a1
RP
1675#endif
1676 break;
1677
1678 case C_REGPARM:
1679 SYMBOL_CLASS (sym) = LOC_REGPARM;
d8ce1326 1680 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
318bf84f 1681 coff_add_symbol_to_list (sym, &coff_local_symbols);
bd5635a1
RP
1682#if !defined (BELIEVE_PCC_PROMOTION)
1683 /* If PCC says a parameter is a short or a char,
1684 it is really an int. */
318bf84f
FF
1685 temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
1686 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1687 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1688 {
1689 SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1690 ? lookup_fundamental_type (current_objfile,
1691 FT_UNSIGNED_INTEGER)
1692 : temptype;
1693 }
bd5635a1
RP
1694#endif
1695 break;
1696
1697 case C_TPDEF:
1698 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1699 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1700
1701 /* If type has no name, give it one */
318bf84f
FF
1702 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1703 TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), NULL);
bd5635a1
RP
1704
1705 /* Keep track of any type which points to empty structured type,
f70be3e4
JG
1706 so it can be filled from a definition from another file. A
1707 simple forward reference (TYPE_CODE_UNDEF) is not an
1708 empty structured type, though; the forward references
1709 work themselves out via the magic of coff_lookup_type. */
bd5635a1 1710 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
f70be3e4
JG
1711 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1712 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1713 TYPE_CODE_UNDEF)
bd5635a1
RP
1714 {
1715 register int i = hashname (SYMBOL_NAME (sym));
1716
1717 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1718 opaque_type_chain[i] = sym;
1719 }
318bf84f 1720 coff_add_symbol_to_list (sym, &coff_file_symbols);
bd5635a1
RP
1721 break;
1722
1723 case C_STRTAG:
1724 case C_UNTAG:
1725 case C_ENTAG:
1726 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1727 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
318bf84f 1728 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
bd5635a1
RP
1729 TYPE_NAME (SYMBOL_TYPE (sym))
1730 = concat ("",
1731 (cs->c_sclass == C_ENTAG
1732 ? "enum "
1733 : (cs->c_sclass == C_STRTAG
1734 ? "struct " : "union ")),
58ae87f6 1735 SYMBOL_NAME (sym), NULL);
318bf84f 1736 coff_add_symbol_to_list (sym, &coff_file_symbols);
bd5635a1
RP
1737 break;
1738
1739 default:
1740 break;
1741 }
1742 }
1743 return sym;
1744}
1745\f
1746/* Decode a coff type specifier;
1747 return the type that is meant. */
1748
1749static
1750struct type *
1751decode_type (cs, c_type, aux)
1752 register struct coff_symbol *cs;
1753 unsigned int c_type;
dcc35536 1754 register union internal_auxent *aux;
bd5635a1
RP
1755{
1756 register struct type *type = 0;
1757 unsigned int new_c_type;
1758
1759 if (c_type & ~N_BTMASK)
1760 {
1761 new_c_type = DECREF (c_type);
1762 if (ISPTR (c_type))
1763 {
1764 type = decode_type (cs, new_c_type, aux);
1765 type = lookup_pointer_type (type);
1766 }
1767 else if (ISFCN (c_type))
1768 {
1769 type = decode_type (cs, new_c_type, aux);
1770 type = lookup_function_type (type);
1771 }
1772 else if (ISARY (c_type))
1773 {
1774 int i, n;
1775 register unsigned short *dim;
85f0a848 1776 struct type *base_type, *index_type;
bd5635a1
RP
1777
1778 /* Define an array type. */
1779 /* auxent refers to array, not base type */
6988f5c0 1780 if (aux->x_sym.x_tagndx.l == 0)
e64fbb3a 1781 cs->c_naux = 0;
bd5635a1
RP
1782
1783 /* shift the indices down */
1784 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1785 i = 1;
1786 n = dim[0];
1787 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1788 *dim = *(dim + 1);
1789 *dim = 0;
1790
bd5635a1 1791 base_type = decode_type (cs, new_c_type, aux);
85f0a848
FF
1792 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
1793 type = create_array_type ((struct type *) NULL, base_type,
1794 index_type, 0, n - 1);
bd5635a1
RP
1795 }
1796 return type;
1797 }
1798
f70be3e4
JG
1799 /* Reference to existing type. This only occurs with the
1800 struct, union, and enum types. EPI a29k coff
1801 fakes us out by producing aux entries with a nonzero
1802 x_tagndx for definitions of structs, unions, and enums, so we
1803 have to check the c_sclass field. */
e64fbb3a 1804 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
bd5635a1 1805 {
f70be3e4
JG
1806 if (cs->c_sclass != C_STRTAG
1807 && cs->c_sclass != C_UNTAG
1808 && cs->c_sclass != C_ENTAG)
1809 {
1810 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1811 return type;
1812 } else {
1813 complain (&tagndx_bad_complaint, cs->c_name);
1814 /* And fall through to decode_base_type... */
1815 }
bd5635a1
RP
1816 }
1817
1818 return decode_base_type (cs, BTYPE (c_type), aux);
1819}
1820
1821/* Decode a coff type specifier for function definition;
1822 return the type that the function returns. */
1823
1824static
1825struct type *
1826decode_function_type (cs, c_type, aux)
1827 register struct coff_symbol *cs;
1828 unsigned int c_type;
dcc35536 1829 register union internal_auxent *aux;
bd5635a1 1830{
6988f5c0 1831 if (aux->x_sym.x_tagndx.l == 0)
e64fbb3a 1832 cs->c_naux = 0; /* auxent refers to function, not base type */
bd5635a1
RP
1833
1834 return decode_type (cs, DECREF (c_type), aux);
1835}
1836\f
1837/* basic C types */
1838
1839static
1840struct type *
1841decode_base_type (cs, c_type, aux)
1842 register struct coff_symbol *cs;
1843 unsigned int c_type;
dcc35536 1844 register union internal_auxent *aux;
bd5635a1
RP
1845{
1846 struct type *type;
1847
1848 switch (c_type)
1849 {
1850 case T_NULL:
1851 /* shows up with "void (*foo)();" structure members */
318bf84f 1852 return lookup_fundamental_type (current_objfile, FT_VOID);
bd5635a1 1853
8aa13b87
JK
1854#if 0
1855/* DGUX actually defines both T_ARG and T_VOID to the same value. */
bd5635a1
RP
1856#ifdef T_ARG
1857 case T_ARG:
1858 /* Shows up in DGUX, I think. Not sure where. */
318bf84f 1859 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
bd5635a1 1860#endif
8aa13b87 1861#endif /* 0 */
bd5635a1
RP
1862
1863#ifdef T_VOID
1864 case T_VOID:
1865 /* Intel 960 COFF has this symbol and meaning. */
318bf84f 1866 return lookup_fundamental_type (current_objfile, FT_VOID);
bd5635a1
RP
1867#endif
1868
1869 case T_CHAR:
318bf84f 1870 return lookup_fundamental_type (current_objfile, FT_CHAR);
bd5635a1
RP
1871
1872 case T_SHORT:
318bf84f 1873 return lookup_fundamental_type (current_objfile, FT_SHORT);
bd5635a1
RP
1874
1875 case T_INT:
318bf84f 1876 return lookup_fundamental_type (current_objfile, FT_INTEGER);
bd5635a1
RP
1877
1878 case T_LONG:
318bf84f 1879 return lookup_fundamental_type (current_objfile, FT_LONG);
bd5635a1
RP
1880
1881 case T_FLOAT:
318bf84f 1882 return lookup_fundamental_type (current_objfile, FT_FLOAT);
bd5635a1
RP
1883
1884 case T_DOUBLE:
318bf84f 1885 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
bd5635a1
RP
1886
1887 case T_STRUCT:
e64fbb3a 1888 if (cs->c_naux != 1)
bd5635a1
RP
1889 {
1890 /* anonymous structure type */
1891 type = coff_alloc_type (cs->c_symnum);
1892 TYPE_CODE (type) = TYPE_CODE_STRUCT;
58ae87f6 1893 TYPE_NAME (type) = concat ("struct ", "<opaque>", NULL);
be8303da 1894 INIT_CPLUS_SPECIFIC(type);
bd5635a1
RP
1895 TYPE_LENGTH (type) = 0;
1896 TYPE_FIELDS (type) = 0;
1897 TYPE_NFIELDS (type) = 0;
1898 }
1899 else
1900 {
318bf84f 1901 type = coff_read_struct_type (cs->c_symnum,
bd5635a1 1902 aux->x_sym.x_misc.x_lnsz.x_size,
be8303da 1903 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
bd5635a1
RP
1904 }
1905 return type;
1906
1907 case T_UNION:
e64fbb3a 1908 if (cs->c_naux != 1)
bd5635a1
RP
1909 {
1910 /* anonymous union type */
1911 type = coff_alloc_type (cs->c_symnum);
58ae87f6 1912 TYPE_NAME (type) = concat ("union ", "<opaque>", NULL);
be8303da 1913 INIT_CPLUS_SPECIFIC(type);
f1d77e90 1914 TYPE_LENGTH (type) = 0;
bd5635a1
RP
1915 TYPE_LENGTH (type) = 0;
1916 TYPE_FIELDS (type) = 0;
1917 TYPE_NFIELDS (type) = 0;
1918 }
1919 else
1920 {
318bf84f 1921 type = coff_read_struct_type (cs->c_symnum,
bd5635a1 1922 aux->x_sym.x_misc.x_lnsz.x_size,
be8303da 1923 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
bd5635a1
RP
1924 }
1925 TYPE_CODE (type) = TYPE_CODE_UNION;
1926 return type;
1927
1928 case T_ENUM:
318bf84f 1929 return coff_read_enum_type (cs->c_symnum,
bd5635a1 1930 aux->x_sym.x_misc.x_lnsz.x_size,
be8303da 1931 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
bd5635a1
RP
1932
1933 case T_MOE:
1934 /* shouldn't show up here */
1935 break;
1936
1937 case T_UCHAR:
318bf84f 1938 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
bd5635a1
RP
1939
1940 case T_USHORT:
318bf84f 1941 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
bd5635a1
RP
1942
1943 case T_UINT:
318bf84f 1944 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
bd5635a1
RP
1945
1946 case T_ULONG:
318bf84f 1947 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
bd5635a1 1948 }
7e258d18 1949 complain (&unexpected_type_complaint, cs->c_name);
318bf84f 1950 return lookup_fundamental_type (current_objfile, FT_VOID);
bd5635a1
RP
1951}
1952\f
1953/* This page contains subroutines of read_type. */
1954
1955/* Read the description of a structure (or union type)
1956 and return an object describing the type. */
1957
1958static struct type *
318bf84f 1959coff_read_struct_type (index, length, lastsym)
bd5635a1
RP
1960 int index;
1961 int length;
1962 int lastsym;
1963{
1964 struct nextfield
1965 {
1966 struct nextfield *next;
1967 struct field field;
1968 };
1969
1970 register struct type *type;
1971 register struct nextfield *list = 0;
1972 struct nextfield *new;
1973 int nfields = 0;
1974 register int n;
1975 char *name;
1976#ifdef NAMES_HAVE_UNDERSCORE
1977 int offset = 1;
1978#else
1979 int offset = 0;
1980#endif
1981 struct coff_symbol member_sym;
1982 register struct coff_symbol *ms = &member_sym;
dcc35536
JG
1983 struct internal_syment sub_sym;
1984 union internal_auxent sub_aux;
bd5635a1
RP
1985 int done = 0;
1986
1987 type = coff_alloc_type (index);
1988 TYPE_CODE (type) = TYPE_CODE_STRUCT;
be8303da 1989 INIT_CPLUS_SPECIFIC(type);
bd5635a1
RP
1990 TYPE_LENGTH (type) = length;
1991
1992 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1993 {
1994 read_one_sym (ms, &sub_sym, &sub_aux);
1995 name = ms->c_name;
1996 name = (name[0] == '_' ? name + offset : name);
1997
1998 switch (ms->c_sclass)
1999 {
2000 case C_MOS:
2001 case C_MOU:
2002
2003 /* Get space to record the next field's data. */
2004 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2005 new->next = list;
2006 list = new;
2007
2008 /* Save the data. */
2009 list->field.name = savestring (name, strlen (name));
2010 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
2011 list->field.bitpos = 8 * ms->c_value;
2012 list->field.bitsize = 0;
2013 nfields++;
2014 break;
2015
2016 case C_FIELD:
2017
2018 /* Get space to record the next field's data. */
2019 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2020 new->next = list;
2021 list = new;
2022
2023 /* Save the data. */
2024 list->field.name = savestring (name, strlen (name));
2025 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
2026 list->field.bitpos = ms->c_value;
2027 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2028 nfields++;
2029 break;
2030
2031 case C_EOS:
2032 done = 1;
2033 break;
2034 }
2035 }
2036 /* Now create the vector of fields, and record how big it is. */
2037
2038 TYPE_NFIELDS (type) = nfields;
2039 TYPE_FIELDS (type) = (struct field *)
85f0a848 2040 TYPE_ALLOC (type, sizeof (struct field) * nfields);
bd5635a1
RP
2041
2042 /* Copy the saved-up fields into the field vector. */
2043
2044 for (n = nfields; list; list = list->next)
2045 TYPE_FIELD (type, --n) = list->field;
2046
2047 return type;
2048}
2049\f
2050/* Read a definition of an enumeration type,
2051 and create and return a suitable type object.
2052 Also defines the symbols that represent the values of the type. */
e1ce8aa5 2053/* Currently assumes it's sizeof (int) and doesn't use length. */
bd5635a1 2054
61a7292f 2055/* ARGSUSED */
bd5635a1 2056static struct type *
318bf84f 2057coff_read_enum_type (index, length, lastsym)
bd5635a1
RP
2058 int index;
2059 int length;
2060 int lastsym;
2061{
2062 register struct symbol *sym;
2063 register struct type *type;
2064 int nsyms = 0;
2065 int done = 0;
318bf84f 2066 struct coff_pending **symlist;
bd5635a1
RP
2067 struct coff_symbol member_sym;
2068 register struct coff_symbol *ms = &member_sym;
dcc35536
JG
2069 struct internal_syment sub_sym;
2070 union internal_auxent sub_aux;
318bf84f 2071 struct coff_pending *osyms, *syms;
bd5635a1
RP
2072 register int n;
2073 char *name;
2074#ifdef NAMES_HAVE_UNDERSCORE
2075 int offset = 1;
2076#else
2077 int offset = 0;
2078#endif
2079
2080 type = coff_alloc_type (index);
2081 if (within_function)
318bf84f 2082 symlist = &coff_local_symbols;
bd5635a1 2083 else
318bf84f 2084 symlist = &coff_file_symbols;
bd5635a1
RP
2085 osyms = *symlist;
2086
2087 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2088 {
2089 read_one_sym (ms, &sub_sym, &sub_aux);
2090 name = ms->c_name;
2091 name = (name[0] == '_' ? name + offset : name);
2092
2093 switch (ms->c_sclass)
2094 {
2095 case C_MOE:
2096 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
4ed3a9ea 2097 memset (sym, 0, sizeof (struct symbol));
bd5635a1
RP
2098
2099 SYMBOL_NAME (sym) = savestring (name, strlen (name));
2100 SYMBOL_CLASS (sym) = LOC_CONST;
2101 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2102 SYMBOL_VALUE (sym) = ms->c_value;
318bf84f 2103 coff_add_symbol_to_list (sym, symlist);
bd5635a1
RP
2104 nsyms++;
2105 break;
2106
2107 case C_EOS:
2108 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2109 up the count of how many symbols to read. So stop
2110 on .eos. */
2111 done = 1;
2112 break;
2113 }
2114 }
2115
2116 /* Now fill in the fields of the type-structure. */
2117
be8303da 2118 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT;
bd5635a1
RP
2119 TYPE_CODE (type) = TYPE_CODE_ENUM;
2120 TYPE_NFIELDS (type) = nsyms;
2121 TYPE_FIELDS (type) = (struct field *)
85f0a848 2122 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
bd5635a1
RP
2123
2124 /* Find the symbols for the values and put them into the type.
2125 The symbols can be found in the symlist that we put them on
2126 to cause them to be defined. osyms contains the old value
2127 of that symlist; everything up to there was defined by us. */
2128
2129 for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
2130 {
2131 SYMBOL_TYPE (syms->symbol) = type;
2132 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
2133 TYPE_FIELD_VALUE (type, n) = 0;
2134 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
2135 TYPE_FIELD_BITSIZE (type, n) = 0;
2136 }
d8ce1326
JG
2137 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2138 if(TYPE_NFIELDS(type) == 2 &&
2139 ((!strcmp(TYPE_FIELD_NAME(type,0),"TRUE") &&
2140 !strcmp(TYPE_FIELD_NAME(type,1),"FALSE")) ||
2141 (!strcmp(TYPE_FIELD_NAME(type,1),"TRUE") &&
2142 !strcmp(TYPE_FIELD_NAME(type,0),"FALSE"))))
2143 TYPE_CODE(type) = TYPE_CODE_BOOL;
bd5635a1
RP
2144 return type;
2145}
2146
85f0a848
FF
2147/* Fake up support for relocating symbol addresses. FIXME. */
2148
2149struct section_offsets coff_symfile_faker = {0};
2150
2151struct section_offsets *
2152coff_symfile_offsets (objfile, addr)
2153 struct objfile *objfile;
2154 CORE_ADDR addr;
2155{
2156 return &coff_symfile_faker;
2157}
2158
bd5635a1
RP
2159/* Register our ability to parse symbols for coff BFD files */
2160
2161static struct sym_fns coff_sym_fns =
2162{
80d68b1d
FF
2163 "coff", /* sym_name: name or name prefix of BFD target type */
2164 4, /* sym_namelen: number of significant sym_name chars */
2165 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2166 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2167 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2168 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
85f0a848 2169 coff_symfile_offsets, /* sym_offsets: xlate external to internal form */
80d68b1d 2170 NULL /* next: pointer to next struct sym_fns */
bd5635a1
RP
2171};
2172
2173void
2174_initialize_coffread ()
2175{
2176 add_symtab_fns(&coff_sym_fns);
2177}
This page took 0.254285 seconds and 4 git commands to generate.