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