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