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