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