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