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