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