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