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