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