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