xref: /dflybsd-src/contrib/gdb-7/gdb/coffread.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Read coff symbol tables and convert to internal format, for GDB.
2*ef5ccd6cSJohn Marino    Copyright (C) 1987-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "gdbtypes.h"
235796c8dcSSimon Schubert #include "demangle.h"
245796c8dcSSimon Schubert #include "breakpoint.h"
255796c8dcSSimon Schubert 
265796c8dcSSimon Schubert #include "bfd.h"
275796c8dcSSimon Schubert #include "gdb_obstack.h"
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert #include "gdb_string.h"
305796c8dcSSimon Schubert #include <ctype.h>
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert #include "coff/internal.h"	/* Internal format of COFF symbols in BFD */
335796c8dcSSimon Schubert #include "libcoff.h"		/* FIXME secret internal data from BFD */
345796c8dcSSimon Schubert #include "objfiles.h"
355796c8dcSSimon Schubert #include "buildsym.h"
365796c8dcSSimon Schubert #include "gdb-stabs.h"
375796c8dcSSimon Schubert #include "stabsread.h"
385796c8dcSSimon Schubert #include "complaints.h"
395796c8dcSSimon Schubert #include "target.h"
405796c8dcSSimon Schubert #include "gdb_assert.h"
415796c8dcSSimon Schubert #include "block.h"
425796c8dcSSimon Schubert #include "dictionary.h"
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert #include "coff-pe-read.h"
455796c8dcSSimon Schubert 
46cf7f2e2dSJohn Marino #include "psymtab.h"
47cf7f2e2dSJohn Marino 
485796c8dcSSimon Schubert extern void _initialize_coffread (void);
495796c8dcSSimon Schubert 
50*ef5ccd6cSJohn Marino /* Key for COFF-associated data.  */
51*ef5ccd6cSJohn Marino 
52*ef5ccd6cSJohn Marino static const struct objfile_data *coff_objfile_data_key;
53*ef5ccd6cSJohn Marino 
54a45ae5f8SJohn Marino /* The objfile we are currently reading.  */
55a45ae5f8SJohn Marino 
56a45ae5f8SJohn Marino static struct objfile *coffread_objfile;
57a45ae5f8SJohn Marino 
585796c8dcSSimon Schubert struct coff_symfile_info
595796c8dcSSimon Schubert   {
60c50c785cSJohn Marino     file_ptr min_lineno_offset;	/* Where in file lowest line#s are.  */
61c50c785cSJohn Marino     file_ptr max_lineno_offset;	/* 1+last byte of line#s in file.  */
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert     CORE_ADDR textaddr;		/* Addr of .text section.  */
645796c8dcSSimon Schubert     unsigned int textsize;	/* Size of .text section.  */
655796c8dcSSimon Schubert     struct stab_section_list *stabsects;	/* .stab sections.  */
66c50c785cSJohn Marino     asection *stabstrsect;	/* Section pointer for .stab section.  */
675796c8dcSSimon Schubert     char *stabstrdata;
685796c8dcSSimon Schubert   };
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert /* Translate an external name string into a user-visible name.  */
715796c8dcSSimon Schubert #define	EXTERNAL_NAME(string, abfd) \
72c50c785cSJohn Marino 	(string[0] == bfd_get_symbol_leading_char (abfd) \
73c50c785cSJohn Marino 	? string + 1 : string)
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert /* To be an sdb debug type, type must have at least a basic or primary
765796c8dcSSimon Schubert    derived type.  Using this rather than checking against T_NULL is
775796c8dcSSimon Schubert    said to prevent core dumps if we try to operate on Michael Bloom
785796c8dcSSimon Schubert    dbx-in-coff file.  */
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert /* Core address of start and end of text of current source file.
835796c8dcSSimon Schubert    This comes from a ".text" symbol where x_nlinno > 0.  */
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert static CORE_ADDR current_source_start_addr;
865796c8dcSSimon Schubert static CORE_ADDR current_source_end_addr;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* The addresses of the symbol table stream and number of symbols
895796c8dcSSimon Schubert    of the object file we are reading (as copied into core).  */
905796c8dcSSimon Schubert 
915796c8dcSSimon Schubert static bfd *nlist_bfd_global;
925796c8dcSSimon Schubert static int nlist_nsyms_global;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert 
95c50c785cSJohn Marino /* Pointers to scratch storage, used for reading raw symbols and
96c50c785cSJohn Marino    auxents.  */
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert static char *temp_sym;
995796c8dcSSimon Schubert static char *temp_aux;
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert /* Local variables that hold the shift and mask values for the
1025796c8dcSSimon Schubert    COFF file that we are currently reading.  These come back to us
1035796c8dcSSimon Schubert    from BFD, and are referenced by their macro names, as well as
1045796c8dcSSimon Schubert    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
1055796c8dcSSimon Schubert    macros from include/coff/internal.h .  */
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert static unsigned local_n_btmask;
1085796c8dcSSimon Schubert static unsigned local_n_btshft;
1095796c8dcSSimon Schubert static unsigned local_n_tmask;
1105796c8dcSSimon Schubert static unsigned local_n_tshift;
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert #define	N_BTMASK	local_n_btmask
1135796c8dcSSimon Schubert #define	N_BTSHFT	local_n_btshft
1145796c8dcSSimon Schubert #define	N_TMASK		local_n_tmask
1155796c8dcSSimon Schubert #define	N_TSHIFT	local_n_tshift
1165796c8dcSSimon Schubert 
117c50c785cSJohn Marino /* Local variables that hold the sizes in the file of various COFF
118c50c785cSJohn Marino    structures.  (We only need to know this to read them from the file
119c50c785cSJohn Marino    -- BFD will then translate the data in them, into `internal_xxx'
120c50c785cSJohn Marino    structs in the right byte order, alignment, etc.)  */
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert static unsigned local_linesz;
1235796c8dcSSimon Schubert static unsigned local_symesz;
1245796c8dcSSimon Schubert static unsigned local_auxesz;
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* This is set if this is a PE format file.  */
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert static int pe_file;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert /* Chain of typedefs of pointers to empty struct/union types.
1315796c8dcSSimon Schubert    They are chained thru the SYMBOL_VALUE_CHAIN.  */
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert static struct symbol *opaque_type_chain[HASHSIZE];
1345796c8dcSSimon Schubert 
135c50c785cSJohn Marino /* Simplified internal version of coff symbol table information.  */
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert struct coff_symbol
1385796c8dcSSimon Schubert   {
1395796c8dcSSimon Schubert     char *c_name;
140c50c785cSJohn Marino     int c_symnum;		/* Symbol number of this entry.  */
141c50c785cSJohn Marino     int c_naux;			/* 0 if syment only, 1 if syment +
142c50c785cSJohn Marino 				   auxent, etc.  */
143c50c785cSJohn Marino     CORE_ADDR c_value;
1445796c8dcSSimon Schubert     int c_sclass;
1455796c8dcSSimon Schubert     int c_secnum;
1465796c8dcSSimon Schubert     unsigned int c_type;
1475796c8dcSSimon Schubert   };
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert extern void stabsread_clear_cache (void);
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert static struct type *coff_read_struct_type (int, int, int,
1525796c8dcSSimon Schubert 					   struct objfile *);
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert static struct type *decode_base_type (struct coff_symbol *,
155c50c785cSJohn Marino 				      unsigned int,
156c50c785cSJohn Marino 				      union internal_auxent *,
1575796c8dcSSimon Schubert 				      struct objfile *);
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert static struct type *decode_type (struct coff_symbol *, unsigned int,
1605796c8dcSSimon Schubert 				 union internal_auxent *,
1615796c8dcSSimon Schubert 				 struct objfile *);
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert static struct type *decode_function_type (struct coff_symbol *,
1645796c8dcSSimon Schubert 					  unsigned int,
1655796c8dcSSimon Schubert 					  union internal_auxent *,
1665796c8dcSSimon Schubert 					  struct objfile *);
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert static struct type *coff_read_enum_type (int, int, int,
1695796c8dcSSimon Schubert 					 struct objfile *);
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert static struct symbol *process_coff_symbol (struct coff_symbol *,
1725796c8dcSSimon Schubert 					   union internal_auxent *,
1735796c8dcSSimon Schubert 					   struct objfile *);
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert static void patch_opaque_types (struct symtab *);
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert static void enter_linenos (long, int, int, struct objfile *);
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert static void free_linetab (void);
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert static void free_linetab_cleanup (void *ignore);
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert static int init_lineno (bfd *, long, int);
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert static char *getsymname (struct internal_syment *);
1865796c8dcSSimon Schubert 
187c50c785cSJohn Marino static const char *coff_getfilename (union internal_auxent *);
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert static void free_stringtab (void);
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert static void free_stringtab_cleanup (void *ignore);
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert static int init_stringtab (bfd *, long);
1945796c8dcSSimon Schubert 
1955796c8dcSSimon Schubert static void read_one_sym (struct coff_symbol *,
196c50c785cSJohn Marino 			  struct internal_syment *,
197c50c785cSJohn Marino 			  union internal_auxent *);
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert static void coff_symtab_read (long, unsigned int, struct objfile *);
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert /* We are called once per section from coff_symfile_read.  We
2025796c8dcSSimon Schubert    need to examine each section we are passed, check to see
2035796c8dcSSimon Schubert    if it is something we are interested in processing, and
2045796c8dcSSimon Schubert    if so, stash away some access information for the section.
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert    FIXME: The section names should not be hardwired strings (what
2075796c8dcSSimon Schubert    should they be?  I don't think most object file formats have enough
2085796c8dcSSimon Schubert    section flags to specify what kind of debug section it is
2095796c8dcSSimon Schubert    -kingdon).  */
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert static void
coff_locate_sections(bfd * abfd,asection * sectp,void * csip)2125796c8dcSSimon Schubert coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
2135796c8dcSSimon Schubert {
2145796c8dcSSimon Schubert   struct coff_symfile_info *csi;
2155796c8dcSSimon Schubert   const char *name;
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert   csi = (struct coff_symfile_info *) csip;
2185796c8dcSSimon Schubert   name = bfd_get_section_name (abfd, sectp);
2195796c8dcSSimon Schubert   if (strcmp (name, ".text") == 0)
2205796c8dcSSimon Schubert     {
2215796c8dcSSimon Schubert       csi->textaddr = bfd_section_vma (abfd, sectp);
2225796c8dcSSimon Schubert       csi->textsize += bfd_section_size (abfd, sectp);
2235796c8dcSSimon Schubert     }
2245796c8dcSSimon Schubert   else if (strncmp (name, ".text", sizeof ".text" - 1) == 0)
2255796c8dcSSimon Schubert     {
2265796c8dcSSimon Schubert       csi->textsize += bfd_section_size (abfd, sectp);
2275796c8dcSSimon Schubert     }
2285796c8dcSSimon Schubert   else if (strcmp (name, ".stabstr") == 0)
2295796c8dcSSimon Schubert     {
2305796c8dcSSimon Schubert       csi->stabstrsect = sectp;
2315796c8dcSSimon Schubert     }
2325796c8dcSSimon Schubert   else if (strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
2335796c8dcSSimon Schubert     {
2345796c8dcSSimon Schubert       const char *s;
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert       /* We can have multiple .stab sections if linked with
2375796c8dcSSimon Schubert          --split-by-reloc.  */
2385796c8dcSSimon Schubert       for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
2395796c8dcSSimon Schubert 	if (!isdigit (*s))
2405796c8dcSSimon Schubert 	  break;
2415796c8dcSSimon Schubert       if (*s == '\0')
2425796c8dcSSimon Schubert 	{
2435796c8dcSSimon Schubert 	  struct stab_section_list *n, **pn;
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert 	  n = ((struct stab_section_list *)
2465796c8dcSSimon Schubert 	       xmalloc (sizeof (struct stab_section_list)));
2475796c8dcSSimon Schubert 	  n->section = sectp;
2485796c8dcSSimon Schubert 	  n->next = NULL;
2495796c8dcSSimon Schubert 	  for (pn = &csi->stabsects; *pn != NULL; pn = &(*pn)->next)
2505796c8dcSSimon Schubert 	    ;
2515796c8dcSSimon Schubert 	  *pn = n;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert 	  /* This will be run after coffstab_build_psymtabs is called
2545796c8dcSSimon Schubert 	     in coff_symfile_read, at which point we no longer need
2555796c8dcSSimon Schubert 	     the information.  */
2565796c8dcSSimon Schubert 	  make_cleanup (xfree, n);
2575796c8dcSSimon Schubert 	}
2585796c8dcSSimon Schubert     }
2595796c8dcSSimon Schubert }
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert /* Return the section_offsets* that CS points to.  */
2625796c8dcSSimon Schubert static int cs_to_section (struct coff_symbol *, struct objfile *);
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert struct find_targ_sec_arg
2655796c8dcSSimon Schubert   {
2665796c8dcSSimon Schubert     int targ_index;
2675796c8dcSSimon Schubert     asection **resultp;
2685796c8dcSSimon Schubert   };
2695796c8dcSSimon Schubert 
2705796c8dcSSimon Schubert static void
find_targ_sec(bfd * abfd,asection * sect,void * obj)2715796c8dcSSimon Schubert find_targ_sec (bfd *abfd, asection *sect, void *obj)
2725796c8dcSSimon Schubert {
2735796c8dcSSimon Schubert   struct find_targ_sec_arg *args = (struct find_targ_sec_arg *) obj;
274cf7f2e2dSJohn Marino 
2755796c8dcSSimon Schubert   if (sect->target_index == args->targ_index)
2765796c8dcSSimon Schubert     *args->resultp = sect;
2775796c8dcSSimon Schubert }
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert /* Return the bfd_section that CS points to.  */
2805796c8dcSSimon Schubert static struct bfd_section*
cs_to_bfd_section(struct coff_symbol * cs,struct objfile * objfile)2815796c8dcSSimon Schubert cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
2825796c8dcSSimon Schubert {
2835796c8dcSSimon Schubert   asection *sect = NULL;
2845796c8dcSSimon Schubert   struct find_targ_sec_arg args;
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert   args.targ_index = cs->c_secnum;
2875796c8dcSSimon Schubert   args.resultp = &sect;
2885796c8dcSSimon Schubert   bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
2895796c8dcSSimon Schubert   return sect;
2905796c8dcSSimon Schubert }
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert /* Return the section number (SECT_OFF_*) that CS points to.  */
2935796c8dcSSimon Schubert static int
cs_to_section(struct coff_symbol * cs,struct objfile * objfile)2945796c8dcSSimon Schubert cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
2955796c8dcSSimon Schubert {
2965796c8dcSSimon Schubert   asection *sect = cs_to_bfd_section (cs, objfile);
297cf7f2e2dSJohn Marino 
2985796c8dcSSimon Schubert   if (sect == NULL)
2995796c8dcSSimon Schubert     return SECT_OFF_TEXT (objfile);
3005796c8dcSSimon Schubert   return sect->index;
3015796c8dcSSimon Schubert }
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert /* Return the address of the section of a COFF symbol.  */
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert static CORE_ADDR
cs_section_address(struct coff_symbol * cs,bfd * abfd)3085796c8dcSSimon Schubert cs_section_address (struct coff_symbol *cs, bfd *abfd)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   asection *sect = NULL;
3115796c8dcSSimon Schubert   struct find_targ_sec_arg args;
3125796c8dcSSimon Schubert   CORE_ADDR addr = 0;
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   args.targ_index = cs->c_secnum;
3155796c8dcSSimon Schubert   args.resultp = &sect;
3165796c8dcSSimon Schubert   bfd_map_over_sections (abfd, find_targ_sec, &args);
3175796c8dcSSimon Schubert   if (sect != NULL)
318*ef5ccd6cSJohn Marino     addr = bfd_get_section_vma (abfd, sect);
3195796c8dcSSimon Schubert   return addr;
3205796c8dcSSimon Schubert }
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert /* Look up a coff type-number index.  Return the address of the slot
3235796c8dcSSimon Schubert    where the type for that index is stored.
3245796c8dcSSimon Schubert    The type-number is in INDEX.
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert    This can be used for finding the type associated with that index
3275796c8dcSSimon Schubert    or for associating a new type with the index.  */
3285796c8dcSSimon Schubert 
3295796c8dcSSimon Schubert static struct type **
coff_lookup_type(int index)3305796c8dcSSimon Schubert coff_lookup_type (int index)
3315796c8dcSSimon Schubert {
3325796c8dcSSimon Schubert   if (index >= type_vector_length)
3335796c8dcSSimon Schubert     {
3345796c8dcSSimon Schubert       int old_vector_length = type_vector_length;
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert       type_vector_length *= 2;
3375796c8dcSSimon Schubert       if (index /* is still */  >= type_vector_length)
3385796c8dcSSimon Schubert 	type_vector_length = index * 2;
3395796c8dcSSimon Schubert 
3405796c8dcSSimon Schubert       type_vector = (struct type **)
3415796c8dcSSimon Schubert 	xrealloc ((char *) type_vector,
3425796c8dcSSimon Schubert 		  type_vector_length * sizeof (struct type *));
3435796c8dcSSimon Schubert       memset (&type_vector[old_vector_length], 0,
3445796c8dcSSimon Schubert 	 (type_vector_length - old_vector_length) * sizeof (struct type *));
3455796c8dcSSimon Schubert     }
3465796c8dcSSimon Schubert   return &type_vector[index];
3475796c8dcSSimon Schubert }
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert /* Make sure there is a type allocated for type number index
3505796c8dcSSimon Schubert    and return the type object.
3515796c8dcSSimon Schubert    This can create an empty (zeroed) type object.  */
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert static struct type *
coff_alloc_type(int index)3545796c8dcSSimon Schubert coff_alloc_type (int index)
3555796c8dcSSimon Schubert {
3565796c8dcSSimon Schubert   struct type **type_addr = coff_lookup_type (index);
3575796c8dcSSimon Schubert   struct type *type = *type_addr;
3585796c8dcSSimon Schubert 
3595796c8dcSSimon Schubert   /* If we are referring to a type not known at all yet,
3605796c8dcSSimon Schubert      allocate an empty type for it.
3615796c8dcSSimon Schubert      We will fill it in later if we find out how.  */
3625796c8dcSSimon Schubert   if (type == NULL)
3635796c8dcSSimon Schubert     {
364a45ae5f8SJohn Marino       type = alloc_type (coffread_objfile);
3655796c8dcSSimon Schubert       *type_addr = type;
3665796c8dcSSimon Schubert     }
3675796c8dcSSimon Schubert   return type;
3685796c8dcSSimon Schubert }
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert /* Start a new symtab for a new source file.
3715796c8dcSSimon Schubert    This is called when a COFF ".file" symbol is seen;
3725796c8dcSSimon Schubert    it indicates the start of data for one original source file.  */
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert static void
coff_start_symtab(const char * name)375c50c785cSJohn Marino coff_start_symtab (const char *name)
3765796c8dcSSimon Schubert {
3775796c8dcSSimon Schubert   start_symtab (
378c50c785cSJohn Marino   /* We fill in the filename later.  start_symtab puts this pointer
379c50c785cSJohn Marino      into last_source_file and we put it in subfiles->name, which
380c50c785cSJohn Marino      end_symtab frees; that's why it must be malloc'd.  */
3815796c8dcSSimon Schubert 		 xstrdup (name),
3825796c8dcSSimon Schubert   /* We never know the directory name for COFF.  */
3835796c8dcSSimon Schubert 		 NULL,
3845796c8dcSSimon Schubert   /* The start address is irrelevant, since we set
3855796c8dcSSimon Schubert      last_source_start_addr in coff_end_symtab.  */
3865796c8dcSSimon Schubert 		 0);
3875796c8dcSSimon Schubert   record_debugformat ("COFF");
3885796c8dcSSimon Schubert }
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert /* Save the vital information from when starting to read a file,
3915796c8dcSSimon Schubert    for use when closing off the current file.
392c50c785cSJohn Marino    NAME is the file name the symbols came from, START_ADDR is the
393c50c785cSJohn Marino    first text address for the file, and SIZE is the number of bytes of
394c50c785cSJohn Marino    text.  */
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert static void
complete_symtab(const char * name,CORE_ADDR start_addr,unsigned int size)397c50c785cSJohn Marino complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
3985796c8dcSSimon Schubert {
399*ef5ccd6cSJohn Marino   set_last_source_file (name);
4005796c8dcSSimon Schubert   current_source_start_addr = start_addr;
4015796c8dcSSimon Schubert   current_source_end_addr = start_addr + size;
4025796c8dcSSimon Schubert }
4035796c8dcSSimon Schubert 
404c50c785cSJohn Marino /* Finish the symbol definitions for one main source file, close off
405c50c785cSJohn Marino    all the lexical contexts for that file (creating struct block's for
406c50c785cSJohn Marino    them), then make the struct symtab for that file and put it in the
407c50c785cSJohn Marino    list of all such.  */
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert static void
coff_end_symtab(struct objfile * objfile)4105796c8dcSSimon Schubert coff_end_symtab (struct objfile *objfile)
4115796c8dcSSimon Schubert {
4125796c8dcSSimon Schubert   last_source_start_addr = current_source_start_addr;
4135796c8dcSSimon Schubert 
414c50c785cSJohn Marino   end_symtab (current_source_end_addr, objfile,
415c50c785cSJohn Marino 	      SECT_OFF_TEXT (objfile));
4165796c8dcSSimon Schubert 
4175796c8dcSSimon Schubert   /* Reinitialize for beginning of new file.  */
418*ef5ccd6cSJohn Marino   set_last_source_file (NULL);
4195796c8dcSSimon Schubert }
4205796c8dcSSimon Schubert 
4215796c8dcSSimon Schubert static struct minimal_symbol *
record_minimal_symbol(struct coff_symbol * cs,CORE_ADDR address,enum minimal_symbol_type type,int section,struct objfile * objfile)4225796c8dcSSimon Schubert record_minimal_symbol (struct coff_symbol *cs, CORE_ADDR address,
4235796c8dcSSimon Schubert 		       enum minimal_symbol_type type, int section,
4245796c8dcSSimon Schubert 		       struct objfile *objfile)
4255796c8dcSSimon Schubert {
4265796c8dcSSimon Schubert   struct bfd_section *bfd_section;
427cf7f2e2dSJohn Marino 
428c50c785cSJohn Marino   /* We don't want TDESC entry points in the minimal symbol table.  */
4295796c8dcSSimon Schubert   if (cs->c_name[0] == '@')
4305796c8dcSSimon Schubert     return NULL;
4315796c8dcSSimon Schubert 
4325796c8dcSSimon Schubert   bfd_section = cs_to_bfd_section (cs, objfile);
433c50c785cSJohn Marino   return prim_record_minimal_symbol_and_info (cs->c_name, address,
434c50c785cSJohn Marino 					      type, section,
435c50c785cSJohn Marino 					      bfd_section, objfile);
4365796c8dcSSimon Schubert }
4375796c8dcSSimon Schubert 
4385796c8dcSSimon Schubert /* coff_symfile_init ()
4395796c8dcSSimon Schubert    is the coff-specific initialization routine for reading symbols.
4405796c8dcSSimon Schubert    It is passed a struct objfile which contains, among other things,
4415796c8dcSSimon Schubert    the BFD for the file whose symbols are being read, and a slot for
4425796c8dcSSimon Schubert    a pointer to "private data" which we fill with cookies and other
4435796c8dcSSimon Schubert    treats for coff_symfile_read ().
4445796c8dcSSimon Schubert 
445c50c785cSJohn Marino    We will only be called if this is a COFF or COFF-like file.  BFD
446c50c785cSJohn Marino    handles figuring out the format of the file, and code in symtab.c
4475796c8dcSSimon Schubert    uses BFD's determination to vector to us.
4485796c8dcSSimon Schubert 
449c50c785cSJohn Marino    The ultimate result is a new symtab (or, FIXME, eventually a
450c50c785cSJohn Marino    psymtab).  */
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert static void
coff_symfile_init(struct objfile * objfile)4535796c8dcSSimon Schubert coff_symfile_init (struct objfile *objfile)
4545796c8dcSSimon Schubert {
455*ef5ccd6cSJohn Marino   struct dbx_symfile_info *dbx;
456*ef5ccd6cSJohn Marino   struct coff_symfile_info *coff;
4575796c8dcSSimon Schubert 
458*ef5ccd6cSJohn Marino   /* Allocate struct to keep track of stab reading.  */
459*ef5ccd6cSJohn Marino   dbx = XCNEW (struct dbx_symfile_info);
460*ef5ccd6cSJohn Marino   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
4615796c8dcSSimon Schubert 
462c50c785cSJohn Marino   /* Allocate struct to keep track of the symfile.  */
463*ef5ccd6cSJohn Marino   coff = XCNEW (struct coff_symfile_info);
464*ef5ccd6cSJohn Marino   set_objfile_data (objfile, coff_objfile_data_key, coff);
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert   /* COFF objects may be reordered, so set OBJF_REORDERED.  If we
4675796c8dcSSimon Schubert      find this causes a significant slowdown in gdb then we could
4685796c8dcSSimon Schubert      set it in the debug symbol readers only when necessary.  */
4695796c8dcSSimon Schubert   objfile->flags |= OBJF_REORDERED;
4705796c8dcSSimon Schubert }
4715796c8dcSSimon Schubert 
472c50c785cSJohn Marino /* This function is called for every section; it finds the outer
473c50c785cSJohn Marino    limits of the line table (minimum and maximum file offset) so that
474c50c785cSJohn Marino    the mainline code can read the whole thing for efficiency.  */
4755796c8dcSSimon Schubert 
4765796c8dcSSimon Schubert static void
find_linenos(bfd * abfd,struct bfd_section * asect,void * vpinfo)4775796c8dcSSimon Schubert find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
4785796c8dcSSimon Schubert {
4795796c8dcSSimon Schubert   struct coff_symfile_info *info;
4805796c8dcSSimon Schubert   int size, count;
4815796c8dcSSimon Schubert   file_ptr offset, maxoff;
4825796c8dcSSimon Schubert 
4835796c8dcSSimon Schubert   /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
4845796c8dcSSimon Schubert   count = asect->lineno_count;
485c50c785cSJohn Marino   /* End of warning.  */
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert   if (count == 0)
4885796c8dcSSimon Schubert     return;
4895796c8dcSSimon Schubert   size = count * local_linesz;
4905796c8dcSSimon Schubert 
4915796c8dcSSimon Schubert   info = (struct coff_symfile_info *) vpinfo;
4925796c8dcSSimon Schubert   /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
4935796c8dcSSimon Schubert   offset = asect->line_filepos;
494c50c785cSJohn Marino   /* End of warning.  */
4955796c8dcSSimon Schubert 
4965796c8dcSSimon Schubert   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
4975796c8dcSSimon Schubert     info->min_lineno_offset = offset;
4985796c8dcSSimon Schubert 
4995796c8dcSSimon Schubert   maxoff = offset + size;
5005796c8dcSSimon Schubert   if (maxoff > info->max_lineno_offset)
5015796c8dcSSimon Schubert     info->max_lineno_offset = maxoff;
5025796c8dcSSimon Schubert }
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert /* The BFD for this file -- only good while we're actively reading
5065796c8dcSSimon Schubert    symbols into a psymtab or a symtab.  */
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert static bfd *symfile_bfd;
5095796c8dcSSimon Schubert 
5105796c8dcSSimon Schubert /* Read a symbol file, after initialization by coff_symfile_init.  */
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert static void
coff_symfile_read(struct objfile * objfile,int symfile_flags)513cf7f2e2dSJohn Marino coff_symfile_read (struct objfile *objfile, int symfile_flags)
5145796c8dcSSimon Schubert {
5155796c8dcSSimon Schubert   struct coff_symfile_info *info;
5165796c8dcSSimon Schubert   struct dbx_symfile_info *dbxinfo;
5175796c8dcSSimon Schubert   bfd *abfd = objfile->obfd;
5185796c8dcSSimon Schubert   coff_data_type *cdata = coff_data (abfd);
5195796c8dcSSimon Schubert   char *name = bfd_get_filename (abfd);
5205796c8dcSSimon Schubert   int val;
5215796c8dcSSimon Schubert   unsigned int num_symbols;
5225796c8dcSSimon Schubert   int symtab_offset;
5235796c8dcSSimon Schubert   int stringtab_offset;
5245796c8dcSSimon Schubert   struct cleanup *back_to, *cleanup_minimal_symbols;
5255796c8dcSSimon Schubert   int stabstrsize;
5265796c8dcSSimon Schubert 
527*ef5ccd6cSJohn Marino   info = objfile_data (objfile, coff_objfile_data_key);
528*ef5ccd6cSJohn Marino   dbxinfo = DBX_SYMFILE_INFO (objfile);
529c50c785cSJohn Marino   symfile_bfd = abfd;		/* Kludge for swap routines.  */
5305796c8dcSSimon Schubert 
5315796c8dcSSimon Schubert /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
5325796c8dcSSimon Schubert   num_symbols = bfd_get_symcount (abfd);	/* How many syms */
5335796c8dcSSimon Schubert   symtab_offset = cdata->sym_filepos;	/* Symbol table file offset */
5345796c8dcSSimon Schubert   stringtab_offset = symtab_offset +	/* String table file offset */
5355796c8dcSSimon Schubert     num_symbols * cdata->local_symesz;
5365796c8dcSSimon Schubert 
5375796c8dcSSimon Schubert   /* Set a few file-statics that give us specific information about
5385796c8dcSSimon Schubert      the particular COFF file format we're reading.  */
5395796c8dcSSimon Schubert   local_n_btmask = cdata->local_n_btmask;
5405796c8dcSSimon Schubert   local_n_btshft = cdata->local_n_btshft;
5415796c8dcSSimon Schubert   local_n_tmask = cdata->local_n_tmask;
5425796c8dcSSimon Schubert   local_n_tshift = cdata->local_n_tshift;
5435796c8dcSSimon Schubert   local_linesz = cdata->local_linesz;
5445796c8dcSSimon Schubert   local_symesz = cdata->local_symesz;
5455796c8dcSSimon Schubert   local_auxesz = cdata->local_auxesz;
5465796c8dcSSimon Schubert 
5475796c8dcSSimon Schubert   /* Allocate space for raw symbol and aux entries, based on their
5485796c8dcSSimon Schubert      space requirements as reported by BFD.  */
5495796c8dcSSimon Schubert   temp_sym = (char *) xmalloc
5505796c8dcSSimon Schubert     (cdata->local_symesz + cdata->local_auxesz);
5515796c8dcSSimon Schubert   temp_aux = temp_sym + cdata->local_symesz;
5525796c8dcSSimon Schubert   back_to = make_cleanup (free_current_contents, &temp_sym);
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert   /* We need to know whether this is a PE file, because in PE files,
5555796c8dcSSimon Schubert      unlike standard COFF files, symbol values are stored as offsets
5565796c8dcSSimon Schubert      from the section address, rather than as absolute addresses.
5575796c8dcSSimon Schubert      FIXME: We should use BFD to read the symbol table, and thus avoid
5585796c8dcSSimon Schubert      this problem.  */
5595796c8dcSSimon Schubert   pe_file =
5605796c8dcSSimon Schubert     strncmp (bfd_get_target (objfile->obfd), "pe", 2) == 0
5615796c8dcSSimon Schubert     || strncmp (bfd_get_target (objfile->obfd), "epoc-pe", 7) == 0;
5625796c8dcSSimon Schubert 
563c50c785cSJohn Marino   /* End of warning.  */
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert   info->min_lineno_offset = 0;
5665796c8dcSSimon Schubert   info->max_lineno_offset = 0;
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert   /* Only read line number information if we have symbols.
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert      On Windows NT, some of the system's DLL's have sections with
5715796c8dcSSimon Schubert      PointerToLinenumbers fields that are non-zero, but point at
5725796c8dcSSimon Schubert      random places within the image file.  (In the case I found,
5735796c8dcSSimon Schubert      KERNEL32.DLL's .text section has a line number info pointer that
5745796c8dcSSimon Schubert      points into the middle of the string `lib\\i386\kernel32.dll'.)
5755796c8dcSSimon Schubert 
5765796c8dcSSimon Schubert      However, these DLL's also have no symbols.  The line number
5775796c8dcSSimon Schubert      tables are meaningless without symbols.  And in fact, GDB never
5785796c8dcSSimon Schubert      uses the line number information unless there are symbols.  So we
5795796c8dcSSimon Schubert      can avoid spurious error messages (and maybe run a little
5805796c8dcSSimon Schubert      faster!) by not even reading the line number table unless we have
5815796c8dcSSimon Schubert      symbols.  */
5825796c8dcSSimon Schubert   if (num_symbols > 0)
5835796c8dcSSimon Schubert     {
5845796c8dcSSimon Schubert       /* Read the line number table, all at once.  */
5855796c8dcSSimon Schubert       bfd_map_over_sections (abfd, find_linenos, (void *) info);
5865796c8dcSSimon Schubert 
5875796c8dcSSimon Schubert       make_cleanup (free_linetab_cleanup, 0 /*ignore*/);
5885796c8dcSSimon Schubert       val = init_lineno (abfd, info->min_lineno_offset,
5895796c8dcSSimon Schubert                          info->max_lineno_offset - info->min_lineno_offset);
5905796c8dcSSimon Schubert       if (val < 0)
5915796c8dcSSimon Schubert         error (_("\"%s\": error reading line numbers."), name);
5925796c8dcSSimon Schubert     }
5935796c8dcSSimon Schubert 
5945796c8dcSSimon Schubert   /* Now read the string table, all at once.  */
5955796c8dcSSimon Schubert 
5965796c8dcSSimon Schubert   make_cleanup (free_stringtab_cleanup, 0 /*ignore*/);
5975796c8dcSSimon Schubert   val = init_stringtab (abfd, stringtab_offset);
5985796c8dcSSimon Schubert   if (val < 0)
5995796c8dcSSimon Schubert     error (_("\"%s\": can't get string table"), name);
6005796c8dcSSimon Schubert 
6015796c8dcSSimon Schubert   init_minimal_symbol_collection ();
6025796c8dcSSimon Schubert   cleanup_minimal_symbols = make_cleanup_discard_minimal_symbols ();
6035796c8dcSSimon Schubert 
6045796c8dcSSimon Schubert   /* Now that the executable file is positioned at symbol table,
6055796c8dcSSimon Schubert      process it and define symbols accordingly.  */
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert   coff_symtab_read ((long) symtab_offset, num_symbols, objfile);
6085796c8dcSSimon Schubert 
609c50c785cSJohn Marino   /* Install any minimal symbols that have been collected as the
610c50c785cSJohn Marino      current minimal symbols for this objfile.  */
6115796c8dcSSimon Schubert 
6125796c8dcSSimon Schubert   install_minimal_symbols (objfile);
6135796c8dcSSimon Schubert 
6145796c8dcSSimon Schubert   /* Free the installed minimal symbol data.  */
6155796c8dcSSimon Schubert   do_cleanups (cleanup_minimal_symbols);
6165796c8dcSSimon Schubert 
6175796c8dcSSimon Schubert   bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert   if (info->stabsects)
6205796c8dcSSimon Schubert     {
6215796c8dcSSimon Schubert       if (!info->stabstrsect)
6225796c8dcSSimon Schubert 	{
623c50c785cSJohn Marino 	  error (_("The debugging information in `%s' is corrupted.\nThe "
624c50c785cSJohn Marino 		   "file has a `.stabs' section, but no `.stabstr' section."),
6255796c8dcSSimon Schubert 		 name);
6265796c8dcSSimon Schubert 	}
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert       /* FIXME: dubious.  Why can't we use something normal like
6295796c8dcSSimon Schubert          bfd_get_section_contents?  */
6305796c8dcSSimon Schubert       bfd_seek (abfd, abfd->where, 0);
6315796c8dcSSimon Schubert 
6325796c8dcSSimon Schubert       stabstrsize = bfd_section_size (abfd, info->stabstrsect);
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert       coffstab_build_psymtabs (objfile,
6355796c8dcSSimon Schubert 			       info->textaddr, info->textsize,
6365796c8dcSSimon Schubert 			       info->stabsects,
6375796c8dcSSimon Schubert 			       info->stabstrsect->filepos, stabstrsize);
6385796c8dcSSimon Schubert     }
639a45ae5f8SJohn Marino   if (dwarf2_has_info (objfile, NULL))
6405796c8dcSSimon Schubert     {
6415796c8dcSSimon Schubert       /* DWARF2 sections.  */
642cf7f2e2dSJohn Marino       dwarf2_build_psymtabs (objfile);
6435796c8dcSSimon Schubert     }
6445796c8dcSSimon Schubert 
6455796c8dcSSimon Schubert   dwarf2_build_frame_info (objfile);
6465796c8dcSSimon Schubert 
647cf7f2e2dSJohn Marino   /* Try to add separate debug file if no symbols table found.   */
648cf7f2e2dSJohn Marino   if (!objfile_has_partial_symbols (objfile))
649cf7f2e2dSJohn Marino     {
650cf7f2e2dSJohn Marino       char *debugfile;
651cf7f2e2dSJohn Marino 
652cf7f2e2dSJohn Marino       debugfile = find_separate_debug_file_by_debuglink (objfile);
653*ef5ccd6cSJohn Marino       make_cleanup (xfree, debugfile);
654cf7f2e2dSJohn Marino 
655cf7f2e2dSJohn Marino       if (debugfile)
656cf7f2e2dSJohn Marino 	{
657cf7f2e2dSJohn Marino 	  bfd *abfd = symfile_bfd_open (debugfile);
658cf7f2e2dSJohn Marino 
659*ef5ccd6cSJohn Marino 	  make_cleanup_bfd_unref (abfd);
660cf7f2e2dSJohn Marino 	  symbol_file_add_separate (abfd, symfile_flags, objfile);
661cf7f2e2dSJohn Marino 	}
662cf7f2e2dSJohn Marino     }
663cf7f2e2dSJohn Marino 
6645796c8dcSSimon Schubert   do_cleanups (back_to);
6655796c8dcSSimon Schubert }
6665796c8dcSSimon Schubert 
6675796c8dcSSimon Schubert static void
coff_new_init(struct objfile * ignore)6685796c8dcSSimon Schubert coff_new_init (struct objfile *ignore)
6695796c8dcSSimon Schubert {
6705796c8dcSSimon Schubert }
6715796c8dcSSimon Schubert 
672c50c785cSJohn Marino /* Perform any local cleanups required when we are done with a
673c50c785cSJohn Marino    particular objfile.  I.E, we are in the process of discarding all
674c50c785cSJohn Marino    symbol information for an objfile, freeing up all memory held for
675c50c785cSJohn Marino    it, and unlinking the objfile struct from the global list of known
676c50c785cSJohn Marino    objfiles.  */
6775796c8dcSSimon Schubert 
6785796c8dcSSimon Schubert static void
coff_symfile_finish(struct objfile * objfile)6795796c8dcSSimon Schubert coff_symfile_finish (struct objfile *objfile)
6805796c8dcSSimon Schubert {
681c50c785cSJohn Marino   /* Let stabs reader clean up.  */
6825796c8dcSSimon Schubert   stabsread_clear_cache ();
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert   dwarf2_free_objfile (objfile);
6855796c8dcSSimon Schubert }
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert 
6885796c8dcSSimon Schubert /* Given pointers to a symbol table in coff style exec file,
6895796c8dcSSimon Schubert    analyze them and create struct symtab's describing the symbols.
6905796c8dcSSimon Schubert    NSYMS is the number of symbols in the symbol table.
6915796c8dcSSimon Schubert    We read them one at a time using read_one_sym ().  */
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert static void
coff_symtab_read(long symtab_offset,unsigned int nsyms,struct objfile * objfile)6945796c8dcSSimon Schubert coff_symtab_read (long symtab_offset, unsigned int nsyms,
6955796c8dcSSimon Schubert 		  struct objfile *objfile)
6965796c8dcSSimon Schubert {
6975796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6985796c8dcSSimon Schubert   struct context_stack *new;
6995796c8dcSSimon Schubert   struct coff_symbol coff_symbol;
7005796c8dcSSimon Schubert   struct coff_symbol *cs = &coff_symbol;
7015796c8dcSSimon Schubert   static struct internal_syment main_sym;
7025796c8dcSSimon Schubert   static union internal_auxent main_aux;
7035796c8dcSSimon Schubert   struct coff_symbol fcn_cs_saved;
7045796c8dcSSimon Schubert   static struct internal_syment fcn_sym_saved;
7055796c8dcSSimon Schubert   static union internal_auxent fcn_aux_saved;
7065796c8dcSSimon Schubert   struct symtab *s;
7075796c8dcSSimon Schubert   /* A .file is open.  */
7085796c8dcSSimon Schubert   int in_source_file = 0;
7095796c8dcSSimon Schubert   int next_file_symnum = -1;
7105796c8dcSSimon Schubert   /* Name of the current file.  */
711c50c785cSJohn Marino   const char *filestring = "";
7125796c8dcSSimon Schubert   int depth = 0;
7135796c8dcSSimon Schubert   int fcn_first_line = 0;
7145796c8dcSSimon Schubert   CORE_ADDR fcn_first_line_addr = 0;
7155796c8dcSSimon Schubert   int fcn_last_line = 0;
7165796c8dcSSimon Schubert   int fcn_start_addr = 0;
7175796c8dcSSimon Schubert   long fcn_line_ptr = 0;
7185796c8dcSSimon Schubert   int val;
7195796c8dcSSimon Schubert   CORE_ADDR tmpaddr;
7205796c8dcSSimon Schubert   struct minimal_symbol *msym;
7215796c8dcSSimon Schubert 
7225796c8dcSSimon Schubert   /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
723c50c785cSJohn Marino      it's hard to know I've really worked around it.  The fix should
724c50c785cSJohn Marino      be harmless, anyway).  The symptom of the bug is that the first
7255796c8dcSSimon Schubert      fread (in read_one_sym), will (in my example) actually get data
7265796c8dcSSimon Schubert      from file offset 268, when the fseek was to 264 (and ftell shows
7275796c8dcSSimon Schubert      264).  This causes all hell to break loose.  I was unable to
7285796c8dcSSimon Schubert      reproduce this on a short test program which operated on the same
7295796c8dcSSimon Schubert      file, performing (I think) the same sequence of operations.
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert      It stopped happening when I put in this (former) rewind().
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert      FIXME: Find out if this has been reported to Sun, whether it has
7345796c8dcSSimon Schubert      been fixed in a later release, etc.  */
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert   bfd_seek (objfile->obfd, 0, 0);
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert   /* Position to read the symbol table.  */
7395796c8dcSSimon Schubert   val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
7405796c8dcSSimon Schubert   if (val < 0)
7415796c8dcSSimon Schubert     perror_with_name (objfile->name);
7425796c8dcSSimon Schubert 
743a45ae5f8SJohn Marino   coffread_objfile = objfile;
7445796c8dcSSimon Schubert   nlist_bfd_global = objfile->obfd;
7455796c8dcSSimon Schubert   nlist_nsyms_global = nsyms;
746*ef5ccd6cSJohn Marino   set_last_source_file (NULL);
7475796c8dcSSimon Schubert   memset (opaque_type_chain, 0, sizeof opaque_type_chain);
7485796c8dcSSimon Schubert 
749c50c785cSJohn Marino   if (type_vector)		/* Get rid of previous one.  */
7505796c8dcSSimon Schubert     xfree (type_vector);
7515796c8dcSSimon Schubert   type_vector_length = 160;
7525796c8dcSSimon Schubert   type_vector = (struct type **)
7535796c8dcSSimon Schubert     xmalloc (type_vector_length * sizeof (struct type *));
7545796c8dcSSimon Schubert   memset (type_vector, 0, type_vector_length * sizeof (struct type *));
7555796c8dcSSimon Schubert 
7565796c8dcSSimon Schubert   coff_start_symtab ("");
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert   symnum = 0;
7595796c8dcSSimon Schubert   while (symnum < nsyms)
7605796c8dcSSimon Schubert     {
7615796c8dcSSimon Schubert       QUIT;			/* Make this command interruptable.  */
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert       read_one_sym (cs, &main_sym, &main_aux);
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
7665796c8dcSSimon Schubert 	{
767*ef5ccd6cSJohn Marino 	  if (get_last_source_file ())
7685796c8dcSSimon Schubert 	    coff_end_symtab (objfile);
7695796c8dcSSimon Schubert 
7705796c8dcSSimon Schubert 	  coff_start_symtab ("_globals_");
7715796c8dcSSimon Schubert 	  /* coff_start_symtab will set the language of this symtab to
7725796c8dcSSimon Schubert 	     language_unknown, since such a ``file name'' is not
7735796c8dcSSimon Schubert 	     recognized.  Override that with the minimal language to
7745796c8dcSSimon Schubert 	     allow printing values in this symtab.  */
7755796c8dcSSimon Schubert 	  current_subfile->language = language_minimal;
7765796c8dcSSimon Schubert 	  complete_symtab ("_globals_", 0, 0);
777c50c785cSJohn Marino 	  /* Done with all files, everything from here on out is
778c50c785cSJohn Marino 	     globals.  */
7795796c8dcSSimon Schubert 	}
7805796c8dcSSimon Schubert 
781c50c785cSJohn Marino       /* Special case for file with type declarations only, no
782c50c785cSJohn Marino 	 text.  */
783*ef5ccd6cSJohn Marino       if (!get_last_source_file () && SDB_TYPE (cs->c_type)
7845796c8dcSSimon Schubert 	  && cs->c_secnum == N_DEBUG)
7855796c8dcSSimon Schubert 	complete_symtab (filestring, 0, 0);
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert       /* Typedefs should not be treated as symbol definitions.  */
7885796c8dcSSimon Schubert       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
7895796c8dcSSimon Schubert 	{
790c50c785cSJohn Marino 	  /* Record all functions -- external and static -- in
791c50c785cSJohn Marino 	     minsyms.  */
7925796c8dcSSimon Schubert 	  int section = cs_to_section (cs, objfile);
793cf7f2e2dSJohn Marino 
794c50c785cSJohn Marino 	  tmpaddr = cs->c_value + ANOFFSET (objfile->section_offsets,
795c50c785cSJohn Marino 					    SECT_OFF_TEXT (objfile));
796c50c785cSJohn Marino 	  record_minimal_symbol (cs, tmpaddr, mst_text,
797c50c785cSJohn Marino 				 section, objfile);
7985796c8dcSSimon Schubert 
7995796c8dcSSimon Schubert 	  fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
8005796c8dcSSimon Schubert 	  fcn_start_addr = tmpaddr;
8015796c8dcSSimon Schubert 	  fcn_cs_saved = *cs;
8025796c8dcSSimon Schubert 	  fcn_sym_saved = main_sym;
8035796c8dcSSimon Schubert 	  fcn_aux_saved = main_aux;
8045796c8dcSSimon Schubert 	  continue;
8055796c8dcSSimon Schubert 	}
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert       switch (cs->c_sclass)
8085796c8dcSSimon Schubert 	{
8095796c8dcSSimon Schubert 	case C_EFCN:
8105796c8dcSSimon Schubert 	case C_EXTDEF:
8115796c8dcSSimon Schubert 	case C_ULABEL:
8125796c8dcSSimon Schubert 	case C_USTATIC:
8135796c8dcSSimon Schubert 	case C_LINE:
8145796c8dcSSimon Schubert 	case C_ALIAS:
8155796c8dcSSimon Schubert 	case C_HIDDEN:
816c50c785cSJohn Marino 	  complaint (&symfile_complaints,
817c50c785cSJohn Marino 		     _("Bad n_sclass for symbol %s"),
8185796c8dcSSimon Schubert 		     cs->c_name);
8195796c8dcSSimon Schubert 	  break;
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert 	case C_FILE:
822c50c785cSJohn Marino 	  /* c_value field contains symnum of next .file entry in
823c50c785cSJohn Marino 	     table or symnum of first global after last .file.  */
8245796c8dcSSimon Schubert 	  next_file_symnum = cs->c_value;
8255796c8dcSSimon Schubert 	  if (cs->c_naux > 0)
8265796c8dcSSimon Schubert 	    filestring = coff_getfilename (&main_aux);
8275796c8dcSSimon Schubert 	  else
8285796c8dcSSimon Schubert 	    filestring = "";
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert 	  /* Complete symbol table for last object file
8315796c8dcSSimon Schubert 	     containing debugging information.  */
832*ef5ccd6cSJohn Marino 	  if (get_last_source_file ())
8335796c8dcSSimon Schubert 	    {
8345796c8dcSSimon Schubert 	      coff_end_symtab (objfile);
8355796c8dcSSimon Schubert 	      coff_start_symtab (filestring);
8365796c8dcSSimon Schubert 	    }
8375796c8dcSSimon Schubert 	  in_source_file = 1;
8385796c8dcSSimon Schubert 	  break;
8395796c8dcSSimon Schubert 
840c50c785cSJohn Marino 	  /* C_LABEL is used for labels and static functions.
841c50c785cSJohn Marino 	     Including it here allows gdb to see static functions when
842c50c785cSJohn Marino 	     no debug info is available.  */
8435796c8dcSSimon Schubert 	case C_LABEL:
844c50c785cSJohn Marino 	  /* However, labels within a function can make weird
845c50c785cSJohn Marino 	     backtraces, so filter them out (from phdm@macqel.be).  */
8465796c8dcSSimon Schubert 	  if (within_function)
8475796c8dcSSimon Schubert 	    break;
8485796c8dcSSimon Schubert 	case C_STAT:
8495796c8dcSSimon Schubert 	case C_THUMBLABEL:
8505796c8dcSSimon Schubert 	case C_THUMBSTAT:
8515796c8dcSSimon Schubert 	case C_THUMBSTATFUNC:
8525796c8dcSSimon Schubert 	  if (cs->c_name[0] == '.')
8535796c8dcSSimon Schubert 	    {
8545796c8dcSSimon Schubert 	      if (strcmp (cs->c_name, ".text") == 0)
8555796c8dcSSimon Schubert 		{
856c50c785cSJohn Marino 		  /* FIXME: don't wire in ".text" as section name or
857c50c785cSJohn Marino 		     symbol name!  */
858c50c785cSJohn Marino 		  /* Check for in_source_file deals with case of a
859c50c785cSJohn Marino 		     file with debugging symbols followed by a later
860c50c785cSJohn Marino 		     file with no symbols.  */
8615796c8dcSSimon Schubert 		  if (in_source_file)
8625796c8dcSSimon Schubert 		    complete_symtab (filestring,
863c50c785cSJohn Marino 		    cs->c_value + ANOFFSET (objfile->section_offsets,
864c50c785cSJohn Marino 					    SECT_OFF_TEXT (objfile)),
8655796c8dcSSimon Schubert 				     main_aux.x_scn.x_scnlen);
8665796c8dcSSimon Schubert 		  in_source_file = 0;
8675796c8dcSSimon Schubert 		}
868c50c785cSJohn Marino 	      /* Flush rest of '.' symbols.  */
8695796c8dcSSimon Schubert 	      break;
8705796c8dcSSimon Schubert 	    }
8715796c8dcSSimon Schubert 	  else if (!SDB_TYPE (cs->c_type)
8725796c8dcSSimon Schubert 		   && cs->c_name[0] == 'L'
8735796c8dcSSimon Schubert 		   && (strncmp (cs->c_name, "LI%", 3) == 0
8745796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LF%", 3) == 0
8755796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LC%", 3) == 0
8765796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LP%", 3) == 0
8775796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LPB%", 4) == 0
8785796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LBB%", 4) == 0
8795796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LBE%", 4) == 0
8805796c8dcSSimon Schubert 		       || strncmp (cs->c_name, "LPBX%", 5) == 0))
8815796c8dcSSimon Schubert 	    /* At least on a 3b1, gcc generates swbeg and string labels
8825796c8dcSSimon Schubert 	       that look like this.  Ignore them.  */
8835796c8dcSSimon Schubert 	    break;
884c50c785cSJohn Marino 	  /* Fall in for static symbols that don't start with '.'  */
8855796c8dcSSimon Schubert 	case C_THUMBEXT:
8865796c8dcSSimon Schubert 	case C_THUMBEXTFUNC:
8875796c8dcSSimon Schubert 	case C_EXT:
8885796c8dcSSimon Schubert 	  {
8895796c8dcSSimon Schubert 	    /* Record it in the minimal symbols regardless of
8905796c8dcSSimon Schubert 	       SDB_TYPE.  This parallels what we do for other debug
8915796c8dcSSimon Schubert 	       formats, and probably is needed to make
8925796c8dcSSimon Schubert 	       print_address_symbolic work right without the (now
8935796c8dcSSimon Schubert 	       gone) "set fast-symbolic-addr off" kludge.  */
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert 	    enum minimal_symbol_type ms_type;
8965796c8dcSSimon Schubert 	    int sec;
8975796c8dcSSimon Schubert 
8985796c8dcSSimon Schubert 	    if (cs->c_secnum == N_UNDEF)
8995796c8dcSSimon Schubert 	      {
900c50c785cSJohn Marino 		/* This is a common symbol.  We used to rely on
901c50c785cSJohn Marino 		   the target to tell us whether it knows where
902c50c785cSJohn Marino 		   the symbol has been relocated to, but none of
903c50c785cSJohn Marino 		   the target implementations actually provided
904c50c785cSJohn Marino 		   that operation.  So we just ignore the symbol,
905c50c785cSJohn Marino 		   the same way we would do if we had a target-side
906c50c785cSJohn Marino 		   symbol lookup which returned no match.  */
9075796c8dcSSimon Schubert 		break;
9085796c8dcSSimon Schubert 	      }
9095796c8dcSSimon Schubert  	    else if (cs->c_secnum == N_ABS)
9105796c8dcSSimon Schubert  	      {
9115796c8dcSSimon Schubert  		/* Use the correct minimal symbol type (and don't
9125796c8dcSSimon Schubert  		   relocate) for absolute values.  */
9135796c8dcSSimon Schubert  		ms_type = mst_abs;
9145796c8dcSSimon Schubert  		sec = cs_to_section (cs, objfile);
9155796c8dcSSimon Schubert  		tmpaddr = cs->c_value;
9165796c8dcSSimon Schubert  	      }
9175796c8dcSSimon Schubert 	    else
9185796c8dcSSimon Schubert 	      {
9195796c8dcSSimon Schubert 		asection *bfd_section = cs_to_bfd_section (cs, objfile);
920cf7f2e2dSJohn Marino 
9215796c8dcSSimon Schubert 		sec = cs_to_section (cs, objfile);
9225796c8dcSSimon Schubert 		tmpaddr = cs->c_value;
923c50c785cSJohn Marino  		/* Statics in a PE file also get relocated.  */
9245796c8dcSSimon Schubert  		if (cs->c_sclass == C_EXT
9255796c8dcSSimon Schubert  		    || cs->c_sclass == C_THUMBEXTFUNC
9265796c8dcSSimon Schubert  		    || cs->c_sclass == C_THUMBEXT
9275796c8dcSSimon Schubert  		    || (pe_file && (cs->c_sclass == C_STAT)))
9285796c8dcSSimon Schubert 		  tmpaddr += ANOFFSET (objfile->section_offsets, sec);
9295796c8dcSSimon Schubert 
9305796c8dcSSimon Schubert 		if (bfd_section->flags & SEC_CODE)
9315796c8dcSSimon Schubert 		  {
9325796c8dcSSimon Schubert 		    ms_type =
9335796c8dcSSimon Schubert 		      cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
9345796c8dcSSimon Schubert 		      || cs->c_sclass == C_THUMBEXT ?
9355796c8dcSSimon Schubert 		      mst_text : mst_file_text;
936*ef5ccd6cSJohn Marino 		    tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
9375796c8dcSSimon Schubert 		  }
9385796c8dcSSimon Schubert 		else if (bfd_section->flags & SEC_ALLOC
9395796c8dcSSimon Schubert 			 && bfd_section->flags & SEC_LOAD)
9405796c8dcSSimon Schubert 		  {
9415796c8dcSSimon Schubert 		    ms_type =
942c50c785cSJohn Marino 		      cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
943c50c785cSJohn Marino 		      ? mst_data : mst_file_data;
9445796c8dcSSimon Schubert 		  }
9455796c8dcSSimon Schubert 		else if (bfd_section->flags & SEC_ALLOC)
9465796c8dcSSimon Schubert 		  {
9475796c8dcSSimon Schubert 		    ms_type =
948c50c785cSJohn Marino 		      cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
949c50c785cSJohn Marino 		      ? mst_bss : mst_file_bss;
9505796c8dcSSimon Schubert 		  }
9515796c8dcSSimon Schubert 		else
9525796c8dcSSimon Schubert 		  ms_type = mst_unknown;
9535796c8dcSSimon Schubert 	      }
9545796c8dcSSimon Schubert 
955c50c785cSJohn Marino 	    msym = record_minimal_symbol (cs, tmpaddr, ms_type,
956c50c785cSJohn Marino 					  sec, objfile);
9575796c8dcSSimon Schubert 	    if (msym)
958c50c785cSJohn Marino 	      gdbarch_coff_make_msymbol_special (gdbarch,
959c50c785cSJohn Marino 						 cs->c_sclass, msym);
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert 	    if (SDB_TYPE (cs->c_type))
9625796c8dcSSimon Schubert 	      {
9635796c8dcSSimon Schubert 		struct symbol *sym;
964cf7f2e2dSJohn Marino 
9655796c8dcSSimon Schubert 		sym = process_coff_symbol
9665796c8dcSSimon Schubert 		  (cs, &main_aux, objfile);
9675796c8dcSSimon Schubert 		SYMBOL_VALUE (sym) = tmpaddr;
9685796c8dcSSimon Schubert 		SYMBOL_SECTION (sym) = sec;
9695796c8dcSSimon Schubert 	      }
9705796c8dcSSimon Schubert 	  }
9715796c8dcSSimon Schubert 	  break;
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert 	case C_FCN:
9745796c8dcSSimon Schubert 	  if (strcmp (cs->c_name, ".bf") == 0)
9755796c8dcSSimon Schubert 	    {
9765796c8dcSSimon Schubert 	      within_function = 1;
9775796c8dcSSimon Schubert 
978c50c785cSJohn Marino 	      /* Value contains address of first non-init type
979c50c785cSJohn Marino 		 code.  */
9805796c8dcSSimon Schubert 	      /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
981c50c785cSJohn Marino 	         contains line number of '{' }.  */
9825796c8dcSSimon Schubert 	      if (cs->c_naux != 1)
9835796c8dcSSimon Schubert 		complaint (&symfile_complaints,
984c50c785cSJohn Marino 			   _("`.bf' symbol %d has no aux entry"),
985c50c785cSJohn Marino 			   cs->c_symnum);
9865796c8dcSSimon Schubert 	      fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
9875796c8dcSSimon Schubert 	      fcn_first_line_addr = cs->c_value;
9885796c8dcSSimon Schubert 
9895796c8dcSSimon Schubert 	      /* Might want to check that locals are 0 and
9905796c8dcSSimon Schubert 	         context_stack_depth is zero, and complain if not.  */
9915796c8dcSSimon Schubert 
9925796c8dcSSimon Schubert 	      depth = 0;
9935796c8dcSSimon Schubert 	      new = push_context (depth, fcn_start_addr);
9945796c8dcSSimon Schubert 	      fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
9955796c8dcSSimon Schubert 	      new->name =
996c50c785cSJohn Marino 		process_coff_symbol (&fcn_cs_saved,
997c50c785cSJohn Marino 				     &fcn_aux_saved, objfile);
9985796c8dcSSimon Schubert 	    }
9995796c8dcSSimon Schubert 	  else if (strcmp (cs->c_name, ".ef") == 0)
10005796c8dcSSimon Schubert 	    {
10015796c8dcSSimon Schubert 	      if (!within_function)
10025796c8dcSSimon Schubert 		error (_("Bad coff function information."));
1003c50c785cSJohn Marino 	      /* The value of .ef is the address of epilogue code;
10045796c8dcSSimon Schubert 	         not useful for gdb.  */
10055796c8dcSSimon Schubert 	      /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
10065796c8dcSSimon Schubert 	         contains number of lines to '}' */
10075796c8dcSSimon Schubert 
10085796c8dcSSimon Schubert 	      if (context_stack_depth <= 0)
1009c50c785cSJohn Marino 		{	/* We attempted to pop an empty context stack.  */
10105796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
1011c50c785cSJohn Marino 			     _("`.ef' symbol without matching `.bf' "
1012c50c785cSJohn Marino 			       "symbol ignored starting at symnum %d"),
10135796c8dcSSimon Schubert 			     cs->c_symnum);
10145796c8dcSSimon Schubert 		  within_function = 0;
10155796c8dcSSimon Schubert 		  break;
10165796c8dcSSimon Schubert 		}
10175796c8dcSSimon Schubert 
10185796c8dcSSimon Schubert 	      new = pop_context ();
10195796c8dcSSimon Schubert 	      /* Stack must be empty now.  */
10205796c8dcSSimon Schubert 	      if (context_stack_depth > 0 || new == NULL)
10215796c8dcSSimon Schubert 		{
10225796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
1023c50c785cSJohn Marino 			     _("Unmatched .ef symbol(s) ignored "
1024c50c785cSJohn Marino 			       "starting at symnum %d"),
10255796c8dcSSimon Schubert 			     cs->c_symnum);
10265796c8dcSSimon Schubert 		  within_function = 0;
10275796c8dcSSimon Schubert 		  break;
10285796c8dcSSimon Schubert 		}
10295796c8dcSSimon Schubert 	      if (cs->c_naux != 1)
10305796c8dcSSimon Schubert 		{
10315796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
1032c50c785cSJohn Marino 			     _("`.ef' symbol %d has no aux entry"),
1033c50c785cSJohn Marino 			     cs->c_symnum);
10345796c8dcSSimon Schubert 		  fcn_last_line = 0x7FFFFFFF;
10355796c8dcSSimon Schubert 		}
10365796c8dcSSimon Schubert 	      else
10375796c8dcSSimon Schubert 		{
10385796c8dcSSimon Schubert 		  fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
10395796c8dcSSimon Schubert 		}
10405796c8dcSSimon Schubert 	      /* fcn_first_line is the line number of the opening '{'.
10415796c8dcSSimon Schubert 	         Do not record it - because it would affect gdb's idea
1042c50c785cSJohn Marino 	         of the line number of the first statement of the
1043c50c785cSJohn Marino 	         function - except for one-line functions, for which
1044c50c785cSJohn Marino 	         it is also the line number of all the statements and
1045c50c785cSJohn Marino 	         of the closing '}', and for which we do not have any
1046c50c785cSJohn Marino 	         other statement-line-number.  */
10475796c8dcSSimon Schubert 	      if (fcn_last_line == 1)
10485796c8dcSSimon Schubert 		record_line (current_subfile, fcn_first_line,
10495796c8dcSSimon Schubert 			     gdbarch_addr_bits_remove (gdbarch,
10505796c8dcSSimon Schubert 						       fcn_first_line_addr));
10515796c8dcSSimon Schubert 	      else
1052c50c785cSJohn Marino 		enter_linenos (fcn_line_ptr, fcn_first_line,
1053c50c785cSJohn Marino 			       fcn_last_line, objfile);
10545796c8dcSSimon Schubert 
1055c50c785cSJohn Marino 	      finish_block (new->name, &local_symbols,
1056c50c785cSJohn Marino 			    new->old_blocks, new->start_addr,
10575796c8dcSSimon Schubert 			    fcn_cs_saved.c_value
10585796c8dcSSimon Schubert 			    + fcn_aux_saved.x_sym.x_misc.x_fsize
1059c50c785cSJohn Marino 			    + ANOFFSET (objfile->section_offsets,
1060c50c785cSJohn Marino 					SECT_OFF_TEXT (objfile)),
10615796c8dcSSimon Schubert 			    objfile
10625796c8dcSSimon Schubert 		);
10635796c8dcSSimon Schubert 	      within_function = 0;
10645796c8dcSSimon Schubert 	    }
10655796c8dcSSimon Schubert 	  break;
10665796c8dcSSimon Schubert 
10675796c8dcSSimon Schubert 	case C_BLOCK:
10685796c8dcSSimon Schubert 	  if (strcmp (cs->c_name, ".bb") == 0)
10695796c8dcSSimon Schubert 	    {
10705796c8dcSSimon Schubert 	      tmpaddr = cs->c_value;
1071c50c785cSJohn Marino 	      tmpaddr += ANOFFSET (objfile->section_offsets,
1072c50c785cSJohn Marino 				   SECT_OFF_TEXT (objfile));
10735796c8dcSSimon Schubert 	      push_context (++depth, tmpaddr);
10745796c8dcSSimon Schubert 	    }
10755796c8dcSSimon Schubert 	  else if (strcmp (cs->c_name, ".eb") == 0)
10765796c8dcSSimon Schubert 	    {
10775796c8dcSSimon Schubert 	      if (context_stack_depth <= 0)
1078c50c785cSJohn Marino 		{	/* We attempted to pop an empty context stack.  */
10795796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
1080c50c785cSJohn Marino 			     _("`.eb' symbol without matching `.bb' "
1081c50c785cSJohn Marino 			       "symbol ignored starting at symnum %d"),
10825796c8dcSSimon Schubert 			     cs->c_symnum);
10835796c8dcSSimon Schubert 		  break;
10845796c8dcSSimon Schubert 		}
10855796c8dcSSimon Schubert 
10865796c8dcSSimon Schubert 	      new = pop_context ();
10875796c8dcSSimon Schubert 	      if (depth-- != new->depth)
10885796c8dcSSimon Schubert 		{
10895796c8dcSSimon Schubert 		  complaint (&symfile_complaints,
1090c50c785cSJohn Marino 			     _("Mismatched .eb symbol ignored "
1091c50c785cSJohn Marino 			       "starting at symnum %d"),
10925796c8dcSSimon Schubert 			     symnum);
10935796c8dcSSimon Schubert 		  break;
10945796c8dcSSimon Schubert 		}
10955796c8dcSSimon Schubert 	      if (local_symbols && context_stack_depth > 0)
10965796c8dcSSimon Schubert 		{
10975796c8dcSSimon Schubert 		  tmpaddr =
1098c50c785cSJohn Marino 		    cs->c_value + ANOFFSET (objfile->section_offsets,
1099c50c785cSJohn Marino 					    SECT_OFF_TEXT (objfile));
11005796c8dcSSimon Schubert 		  /* Make a block for the local symbols within.  */
11015796c8dcSSimon Schubert 		  finish_block (0, &local_symbols, new->old_blocks,
11025796c8dcSSimon Schubert 				new->start_addr, tmpaddr, objfile);
11035796c8dcSSimon Schubert 		}
11045796c8dcSSimon Schubert 	      /* Now pop locals of block just finished.  */
11055796c8dcSSimon Schubert 	      local_symbols = new->locals;
11065796c8dcSSimon Schubert 	    }
11075796c8dcSSimon Schubert 	  break;
11085796c8dcSSimon Schubert 
11095796c8dcSSimon Schubert 	default:
11105796c8dcSSimon Schubert 	  process_coff_symbol (cs, &main_aux, objfile);
11115796c8dcSSimon Schubert 	  break;
11125796c8dcSSimon Schubert 	}
11135796c8dcSSimon Schubert     }
11145796c8dcSSimon Schubert 
11155796c8dcSSimon Schubert   if ((nsyms == 0) && (pe_file))
11165796c8dcSSimon Schubert     {
11175796c8dcSSimon Schubert       /* We've got no debugging symbols, but it's a portable
1118c50c785cSJohn Marino 	 executable, so try to read the export table.  */
11195796c8dcSSimon Schubert       read_pe_exported_syms (objfile);
11205796c8dcSSimon Schubert     }
11215796c8dcSSimon Schubert 
1122*ef5ccd6cSJohn Marino   if (get_last_source_file ())
11235796c8dcSSimon Schubert     coff_end_symtab (objfile);
11245796c8dcSSimon Schubert 
11255796c8dcSSimon Schubert   /* Patch up any opaque types (references to types that are not defined
11265796c8dcSSimon Schubert      in the file where they are referenced, e.g. "struct foo *bar").  */
11275796c8dcSSimon Schubert   ALL_OBJFILE_SYMTABS (objfile, s)
11285796c8dcSSimon Schubert     patch_opaque_types (s);
11295796c8dcSSimon Schubert 
1130a45ae5f8SJohn Marino   coffread_objfile = NULL;
11315796c8dcSSimon Schubert }
11325796c8dcSSimon Schubert 
11335796c8dcSSimon Schubert /* Routines for reading headers and symbols from executable.  */
11345796c8dcSSimon Schubert 
1135c50c785cSJohn Marino /* Read the next symbol, swap it, and return it in both
1136c50c785cSJohn Marino    internal_syment form, and coff_symbol form.  Also return its first
1137c50c785cSJohn Marino    auxent, if any, in internal_auxent form, and skip any other
1138c50c785cSJohn Marino    auxents.  */
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert static void
read_one_sym(struct coff_symbol * cs,struct internal_syment * sym,union internal_auxent * aux)11415796c8dcSSimon Schubert read_one_sym (struct coff_symbol *cs,
11425796c8dcSSimon Schubert 	      struct internal_syment *sym,
11435796c8dcSSimon Schubert 	      union internal_auxent *aux)
11445796c8dcSSimon Schubert {
11455796c8dcSSimon Schubert   int i;
11465796c8dcSSimon Schubert   bfd_size_type bytes;
11475796c8dcSSimon Schubert 
11485796c8dcSSimon Schubert   cs->c_symnum = symnum;
11495796c8dcSSimon Schubert   bytes = bfd_bread (temp_sym, local_symesz, nlist_bfd_global);
11505796c8dcSSimon Schubert   if (bytes != local_symesz)
1151a45ae5f8SJohn Marino     error (_("%s: error reading symbols"), coffread_objfile->name);
11525796c8dcSSimon Schubert   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
11535796c8dcSSimon Schubert   cs->c_naux = sym->n_numaux & 0xff;
11545796c8dcSSimon Schubert   if (cs->c_naux >= 1)
11555796c8dcSSimon Schubert     {
11565796c8dcSSimon Schubert       bytes  = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
11575796c8dcSSimon Schubert       if (bytes != local_auxesz)
1158a45ae5f8SJohn Marino 	error (_("%s: error reading symbols"), coffread_objfile->name);
1159c50c785cSJohn Marino       bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
1160c50c785cSJohn Marino 			    sym->n_type, sym->n_sclass,
11615796c8dcSSimon Schubert 			    0, cs->c_naux, (char *) aux);
11625796c8dcSSimon Schubert       /* If more than one aux entry, read past it (only the first aux
11635796c8dcSSimon Schubert          is important).  */
11645796c8dcSSimon Schubert       for (i = 1; i < cs->c_naux; i++)
11655796c8dcSSimon Schubert 	{
11665796c8dcSSimon Schubert 	  bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
11675796c8dcSSimon Schubert 	  if (bytes != local_auxesz)
1168a45ae5f8SJohn Marino 	    error (_("%s: error reading symbols"), coffread_objfile->name);
11695796c8dcSSimon Schubert 	}
11705796c8dcSSimon Schubert     }
11715796c8dcSSimon Schubert   cs->c_name = getsymname (sym);
11725796c8dcSSimon Schubert   cs->c_value = sym->n_value;
11735796c8dcSSimon Schubert   cs->c_sclass = (sym->n_sclass & 0xff);
11745796c8dcSSimon Schubert   cs->c_secnum = sym->n_scnum;
11755796c8dcSSimon Schubert   cs->c_type = (unsigned) sym->n_type;
11765796c8dcSSimon Schubert   if (!SDB_TYPE (cs->c_type))
11775796c8dcSSimon Schubert     cs->c_type = 0;
11785796c8dcSSimon Schubert 
11795796c8dcSSimon Schubert #if 0
11805796c8dcSSimon Schubert   if (cs->c_sclass & 128)
11815796c8dcSSimon Schubert     printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
11825796c8dcSSimon Schubert #endif
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert   symnum += 1 + cs->c_naux;
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert   /* The PE file format stores symbol values as offsets within the
11875796c8dcSSimon Schubert      section, rather than as absolute addresses.  We correct that
11885796c8dcSSimon Schubert      here, if the symbol has an appropriate storage class.  FIXME: We
11895796c8dcSSimon Schubert      should use BFD to read the symbols, rather than duplicating the
11905796c8dcSSimon Schubert      work here.  */
11915796c8dcSSimon Schubert   if (pe_file)
11925796c8dcSSimon Schubert     {
11935796c8dcSSimon Schubert       switch (cs->c_sclass)
11945796c8dcSSimon Schubert 	{
11955796c8dcSSimon Schubert 	case C_EXT:
11965796c8dcSSimon Schubert 	case C_THUMBEXT:
11975796c8dcSSimon Schubert 	case C_THUMBEXTFUNC:
11985796c8dcSSimon Schubert 	case C_SECTION:
11995796c8dcSSimon Schubert 	case C_NT_WEAK:
12005796c8dcSSimon Schubert 	case C_STAT:
12015796c8dcSSimon Schubert 	case C_THUMBSTAT:
12025796c8dcSSimon Schubert 	case C_THUMBSTATFUNC:
12035796c8dcSSimon Schubert 	case C_LABEL:
12045796c8dcSSimon Schubert 	case C_THUMBLABEL:
12055796c8dcSSimon Schubert 	case C_BLOCK:
12065796c8dcSSimon Schubert 	case C_FCN:
12075796c8dcSSimon Schubert 	case C_EFCN:
12085796c8dcSSimon Schubert 	  if (cs->c_secnum != 0)
12095796c8dcSSimon Schubert 	    cs->c_value += cs_section_address (cs, symfile_bfd);
12105796c8dcSSimon Schubert 	  break;
12115796c8dcSSimon Schubert 	}
12125796c8dcSSimon Schubert     }
12135796c8dcSSimon Schubert }
12145796c8dcSSimon Schubert 
1215c50c785cSJohn Marino /* Support for string table handling.  */
12165796c8dcSSimon Schubert 
12175796c8dcSSimon Schubert static char *stringtab = NULL;
12185796c8dcSSimon Schubert 
12195796c8dcSSimon Schubert static int
init_stringtab(bfd * abfd,long offset)12205796c8dcSSimon Schubert init_stringtab (bfd *abfd, long offset)
12215796c8dcSSimon Schubert {
12225796c8dcSSimon Schubert   long length;
12235796c8dcSSimon Schubert   int val;
12245796c8dcSSimon Schubert   unsigned char lengthbuf[4];
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert   free_stringtab ();
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert   /* If the file is stripped, the offset might be zero, indicating no
12295796c8dcSSimon Schubert      string table.  Just return with `stringtab' set to null.  */
12305796c8dcSSimon Schubert   if (offset == 0)
12315796c8dcSSimon Schubert     return 0;
12325796c8dcSSimon Schubert 
12335796c8dcSSimon Schubert   if (bfd_seek (abfd, offset, 0) < 0)
12345796c8dcSSimon Schubert     return -1;
12355796c8dcSSimon Schubert 
12365796c8dcSSimon Schubert   val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
12375796c8dcSSimon Schubert   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
12385796c8dcSSimon Schubert 
12395796c8dcSSimon Schubert   /* If no string table is needed, then the file may end immediately
12405796c8dcSSimon Schubert      after the symbols.  Just return with `stringtab' set to null.  */
12415796c8dcSSimon Schubert   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
12425796c8dcSSimon Schubert     return 0;
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert   stringtab = (char *) xmalloc (length);
1245c50c785cSJohn Marino   /* This is in target format (probably not very useful, and not
1246c50c785cSJohn Marino      currently used), not host format.  */
12475796c8dcSSimon Schubert   memcpy (stringtab, lengthbuf, sizeof lengthbuf);
1248c50c785cSJohn Marino   if (length == sizeof length)	/* Empty table -- just the count.  */
12495796c8dcSSimon Schubert     return 0;
12505796c8dcSSimon Schubert 
1251c50c785cSJohn Marino   val = bfd_bread (stringtab + sizeof lengthbuf,
1252c50c785cSJohn Marino 		   length - sizeof lengthbuf, abfd);
12535796c8dcSSimon Schubert   if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
12545796c8dcSSimon Schubert     return -1;
12555796c8dcSSimon Schubert 
12565796c8dcSSimon Schubert   return 0;
12575796c8dcSSimon Schubert }
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert static void
free_stringtab(void)12605796c8dcSSimon Schubert free_stringtab (void)
12615796c8dcSSimon Schubert {
12625796c8dcSSimon Schubert   if (stringtab)
12635796c8dcSSimon Schubert     xfree (stringtab);
12645796c8dcSSimon Schubert   stringtab = NULL;
12655796c8dcSSimon Schubert }
12665796c8dcSSimon Schubert 
12675796c8dcSSimon Schubert static void
free_stringtab_cleanup(void * ignore)12685796c8dcSSimon Schubert free_stringtab_cleanup (void *ignore)
12695796c8dcSSimon Schubert {
12705796c8dcSSimon Schubert   free_stringtab ();
12715796c8dcSSimon Schubert }
12725796c8dcSSimon Schubert 
12735796c8dcSSimon Schubert static char *
getsymname(struct internal_syment * symbol_entry)12745796c8dcSSimon Schubert getsymname (struct internal_syment *symbol_entry)
12755796c8dcSSimon Schubert {
12765796c8dcSSimon Schubert   static char buffer[SYMNMLEN + 1];
12775796c8dcSSimon Schubert   char *result;
12785796c8dcSSimon Schubert 
12795796c8dcSSimon Schubert   if (symbol_entry->_n._n_n._n_zeroes == 0)
12805796c8dcSSimon Schubert     {
12815796c8dcSSimon Schubert       /* FIXME: Probably should be detecting corrupt symbol files by
12825796c8dcSSimon Schubert          seeing whether offset points to within the stringtab.  */
12835796c8dcSSimon Schubert       result = stringtab + symbol_entry->_n._n_n._n_offset;
12845796c8dcSSimon Schubert     }
12855796c8dcSSimon Schubert   else
12865796c8dcSSimon Schubert     {
12875796c8dcSSimon Schubert       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
12885796c8dcSSimon Schubert       buffer[SYMNMLEN] = '\0';
12895796c8dcSSimon Schubert       result = buffer;
12905796c8dcSSimon Schubert     }
12915796c8dcSSimon Schubert   return result;
12925796c8dcSSimon Schubert }
12935796c8dcSSimon Schubert 
1294c50c785cSJohn Marino /* Extract the file name from the aux entry of a C_FILE symbol.
1295c50c785cSJohn Marino    Return only the last component of the name.  Result is in static
1296c50c785cSJohn Marino    storage and is only good for temporary use.  */
12975796c8dcSSimon Schubert 
1298c50c785cSJohn Marino static const char *
coff_getfilename(union internal_auxent * aux_entry)12995796c8dcSSimon Schubert coff_getfilename (union internal_auxent *aux_entry)
13005796c8dcSSimon Schubert {
13015796c8dcSSimon Schubert   static char buffer[BUFSIZ];
1302c50c785cSJohn Marino   const char *result;
13035796c8dcSSimon Schubert 
13045796c8dcSSimon Schubert   if (aux_entry->x_file.x_n.x_zeroes == 0)
1305c50c785cSJohn Marino     {
1306c50c785cSJohn Marino       if (strlen (stringtab + aux_entry->x_file.x_n.x_offset) >= BUFSIZ)
1307c50c785cSJohn Marino 	internal_error (__FILE__, __LINE__, _("coff file name too long"));
13085796c8dcSSimon Schubert       strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1309c50c785cSJohn Marino     }
13105796c8dcSSimon Schubert   else
13115796c8dcSSimon Schubert     {
13125796c8dcSSimon Schubert       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
13135796c8dcSSimon Schubert       buffer[FILNMLEN] = '\0';
13145796c8dcSSimon Schubert     }
13155796c8dcSSimon Schubert   result = buffer;
13165796c8dcSSimon Schubert 
13175796c8dcSSimon Schubert   /* FIXME: We should not be throwing away the information about what
13185796c8dcSSimon Schubert      directory.  It should go into dirname of the symtab, or some such
13195796c8dcSSimon Schubert      place.  */
1320c50c785cSJohn Marino   result = lbasename (result);
13215796c8dcSSimon Schubert   return (result);
13225796c8dcSSimon Schubert }
13235796c8dcSSimon Schubert 
13245796c8dcSSimon Schubert /* Support for line number handling.  */
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert static char *linetab = NULL;
13275796c8dcSSimon Schubert static long linetab_offset;
13285796c8dcSSimon Schubert static unsigned long linetab_size;
13295796c8dcSSimon Schubert 
13305796c8dcSSimon Schubert /* Read in all the line numbers for fast lookups later.  Leave them in
13315796c8dcSSimon Schubert    external (unswapped) format in memory; we'll swap them as we enter
13325796c8dcSSimon Schubert    them into GDB's data structures.  */
13335796c8dcSSimon Schubert 
13345796c8dcSSimon Schubert static int
init_lineno(bfd * abfd,long offset,int size)13355796c8dcSSimon Schubert init_lineno (bfd *abfd, long offset, int size)
13365796c8dcSSimon Schubert {
13375796c8dcSSimon Schubert   int val;
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert   linetab_offset = offset;
13405796c8dcSSimon Schubert   linetab_size = size;
13415796c8dcSSimon Schubert 
13425796c8dcSSimon Schubert   free_linetab ();
13435796c8dcSSimon Schubert 
13445796c8dcSSimon Schubert   if (size == 0)
13455796c8dcSSimon Schubert     return 0;
13465796c8dcSSimon Schubert 
13475796c8dcSSimon Schubert   if (bfd_seek (abfd, offset, 0) < 0)
13485796c8dcSSimon Schubert     return -1;
13495796c8dcSSimon Schubert 
1350c50c785cSJohn Marino   /* Allocate the desired table, plus a sentinel.  */
13515796c8dcSSimon Schubert   linetab = (char *) xmalloc (size + local_linesz);
13525796c8dcSSimon Schubert 
13535796c8dcSSimon Schubert   val = bfd_bread (linetab, size, abfd);
13545796c8dcSSimon Schubert   if (val != size)
13555796c8dcSSimon Schubert     return -1;
13565796c8dcSSimon Schubert 
1357c50c785cSJohn Marino   /* Terminate it with an all-zero sentinel record.  */
13585796c8dcSSimon Schubert   memset (linetab + size, 0, local_linesz);
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert   return 0;
13615796c8dcSSimon Schubert }
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert static void
free_linetab(void)13645796c8dcSSimon Schubert free_linetab (void)
13655796c8dcSSimon Schubert {
13665796c8dcSSimon Schubert   if (linetab)
13675796c8dcSSimon Schubert     xfree (linetab);
13685796c8dcSSimon Schubert   linetab = NULL;
13695796c8dcSSimon Schubert }
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert static void
free_linetab_cleanup(void * ignore)13725796c8dcSSimon Schubert free_linetab_cleanup (void *ignore)
13735796c8dcSSimon Schubert {
13745796c8dcSSimon Schubert   free_linetab ();
13755796c8dcSSimon Schubert }
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert #if !defined (L_LNNO32)
13785796c8dcSSimon Schubert #define L_LNNO32(lp) ((lp)->l_lnno)
13795796c8dcSSimon Schubert #endif
13805796c8dcSSimon Schubert 
13815796c8dcSSimon Schubert static void
enter_linenos(long file_offset,int first_line,int last_line,struct objfile * objfile)13825796c8dcSSimon Schubert enter_linenos (long file_offset, int first_line,
13835796c8dcSSimon Schubert 	       int last_line, struct objfile *objfile)
13845796c8dcSSimon Schubert {
13855796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
13865796c8dcSSimon Schubert   char *rawptr;
13875796c8dcSSimon Schubert   struct internal_lineno lptr;
13885796c8dcSSimon Schubert 
13895796c8dcSSimon Schubert   if (!linetab)
13905796c8dcSSimon Schubert     return;
13915796c8dcSSimon Schubert   if (file_offset < linetab_offset)
13925796c8dcSSimon Schubert     {
13935796c8dcSSimon Schubert       complaint (&symfile_complaints,
13945796c8dcSSimon Schubert 		 _("Line number pointer %ld lower than start of line numbers"),
13955796c8dcSSimon Schubert 		 file_offset);
13965796c8dcSSimon Schubert       if (file_offset > linetab_size)	/* Too big to be an offset?  */
13975796c8dcSSimon Schubert 	return;
1398c50c785cSJohn Marino       file_offset += linetab_offset;	/* Try reading at that linetab
1399c50c785cSJohn Marino 					   offset.  */
14005796c8dcSSimon Schubert     }
14015796c8dcSSimon Schubert 
14025796c8dcSSimon Schubert   rawptr = &linetab[file_offset - linetab_offset];
14035796c8dcSSimon Schubert 
1404c50c785cSJohn Marino   /* Skip first line entry for each function.  */
14055796c8dcSSimon Schubert   rawptr += local_linesz;
1406c50c785cSJohn Marino   /* Line numbers start at one for the first line of the function.  */
14075796c8dcSSimon Schubert   first_line--;
14085796c8dcSSimon Schubert 
14095796c8dcSSimon Schubert   /* If the line number table is full (e.g. 64K lines in COFF debug
14105796c8dcSSimon Schubert      info), the next function's L_LNNO32 might not be zero, so don't
14115796c8dcSSimon Schubert      overstep the table's end in any case.  */
14125796c8dcSSimon Schubert   while (rawptr <= &linetab[0] + linetab_size)
14135796c8dcSSimon Schubert     {
14145796c8dcSSimon Schubert       bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
14155796c8dcSSimon Schubert       rawptr += local_linesz;
14165796c8dcSSimon Schubert       /* The next function, or the sentinel, will have L_LNNO32 zero;
14175796c8dcSSimon Schubert 	 we exit.  */
14185796c8dcSSimon Schubert       if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
14195796c8dcSSimon Schubert 	{
14205796c8dcSSimon Schubert 	  CORE_ADDR addr = lptr.l_addr.l_paddr;
1421c50c785cSJohn Marino 	  addr += ANOFFSET (objfile->section_offsets,
1422c50c785cSJohn Marino 			    SECT_OFF_TEXT (objfile));
1423c50c785cSJohn Marino 	  record_line (current_subfile,
1424c50c785cSJohn Marino 		       first_line + L_LNNO32 (&lptr),
14255796c8dcSSimon Schubert 		       gdbarch_addr_bits_remove (gdbarch, addr));
14265796c8dcSSimon Schubert 	}
14275796c8dcSSimon Schubert       else
14285796c8dcSSimon Schubert 	break;
14295796c8dcSSimon Schubert     }
14305796c8dcSSimon Schubert }
14315796c8dcSSimon Schubert 
14325796c8dcSSimon Schubert static void
patch_type(struct type * type,struct type * real_type)14335796c8dcSSimon Schubert patch_type (struct type *type, struct type *real_type)
14345796c8dcSSimon Schubert {
14355796c8dcSSimon Schubert   struct type *target = TYPE_TARGET_TYPE (type);
14365796c8dcSSimon Schubert   struct type *real_target = TYPE_TARGET_TYPE (real_type);
14375796c8dcSSimon Schubert   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
14385796c8dcSSimon Schubert 
14395796c8dcSSimon Schubert   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
14405796c8dcSSimon Schubert   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1441c50c785cSJohn Marino   TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
1442c50c785cSJohn Marino 						      field_size);
14435796c8dcSSimon Schubert 
1444c50c785cSJohn Marino   memcpy (TYPE_FIELDS (target),
1445c50c785cSJohn Marino 	  TYPE_FIELDS (real_target),
1446c50c785cSJohn Marino 	  field_size);
14475796c8dcSSimon Schubert 
14485796c8dcSSimon Schubert   if (TYPE_NAME (real_target))
14495796c8dcSSimon Schubert     {
1450*ef5ccd6cSJohn Marino       /* The previous copy of TYPE_NAME is allocated by
1451*ef5ccd6cSJohn Marino 	 process_coff_symbol.  */
14525796c8dcSSimon Schubert       if (TYPE_NAME (target))
1453*ef5ccd6cSJohn Marino 	xfree ((char*) TYPE_NAME (target));
1454*ef5ccd6cSJohn Marino       TYPE_NAME (target) = xstrdup (TYPE_NAME (real_target));
14555796c8dcSSimon Schubert     }
14565796c8dcSSimon Schubert }
14575796c8dcSSimon Schubert 
14585796c8dcSSimon Schubert /* Patch up all appropriate typedef symbols in the opaque_type_chains
1459c50c785cSJohn Marino    so that they can be used to print out opaque data structures
1460c50c785cSJohn Marino    properly.  */
14615796c8dcSSimon Schubert 
14625796c8dcSSimon Schubert static void
patch_opaque_types(struct symtab * s)14635796c8dcSSimon Schubert patch_opaque_types (struct symtab *s)
14645796c8dcSSimon Schubert {
14655796c8dcSSimon Schubert   struct block *b;
1466*ef5ccd6cSJohn Marino   struct block_iterator iter;
14675796c8dcSSimon Schubert   struct symbol *real_sym;
14685796c8dcSSimon Schubert 
1469c50c785cSJohn Marino   /* Go through the per-file symbols only.  */
14705796c8dcSSimon Schubert   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
14715796c8dcSSimon Schubert   ALL_BLOCK_SYMBOLS (b, iter, real_sym)
14725796c8dcSSimon Schubert     {
14735796c8dcSSimon Schubert       /* Find completed typedefs to use to fix opaque ones.
14745796c8dcSSimon Schubert          Remove syms from the chain when their types are stored,
14755796c8dcSSimon Schubert          but search the whole chain, as there may be several syms
14765796c8dcSSimon Schubert          from different files with the same name.  */
1477cf7f2e2dSJohn Marino       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF
1478cf7f2e2dSJohn Marino 	  && SYMBOL_DOMAIN (real_sym) == VAR_DOMAIN
1479cf7f2e2dSJohn Marino 	  && TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR
1480cf7f2e2dSJohn Marino 	  && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
14815796c8dcSSimon Schubert 	{
1482*ef5ccd6cSJohn Marino 	  const char *name = SYMBOL_LINKAGE_NAME (real_sym);
14835796c8dcSSimon Schubert 	  int hash = hashname (name);
14845796c8dcSSimon Schubert 	  struct symbol *sym, *prev;
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert 	  prev = 0;
14875796c8dcSSimon Schubert 	  for (sym = opaque_type_chain[hash]; sym;)
14885796c8dcSSimon Schubert 	    {
1489cf7f2e2dSJohn Marino 	      if (name[0] == SYMBOL_LINKAGE_NAME (sym)[0]
1490cf7f2e2dSJohn Marino 		  && strcmp (name + 1, SYMBOL_LINKAGE_NAME (sym) + 1) == 0)
14915796c8dcSSimon Schubert 		{
14925796c8dcSSimon Schubert 		  if (prev)
14935796c8dcSSimon Schubert 		    {
14945796c8dcSSimon Schubert 		      SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
14955796c8dcSSimon Schubert 		    }
14965796c8dcSSimon Schubert 		  else
14975796c8dcSSimon Schubert 		    {
14985796c8dcSSimon Schubert 		      opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
14995796c8dcSSimon Schubert 		    }
15005796c8dcSSimon Schubert 
15015796c8dcSSimon Schubert 		  patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
15025796c8dcSSimon Schubert 
15035796c8dcSSimon Schubert 		  if (prev)
15045796c8dcSSimon Schubert 		    {
15055796c8dcSSimon Schubert 		      sym = SYMBOL_VALUE_CHAIN (prev);
15065796c8dcSSimon Schubert 		    }
15075796c8dcSSimon Schubert 		  else
15085796c8dcSSimon Schubert 		    {
15095796c8dcSSimon Schubert 		      sym = opaque_type_chain[hash];
15105796c8dcSSimon Schubert 		    }
15115796c8dcSSimon Schubert 		}
15125796c8dcSSimon Schubert 	      else
15135796c8dcSSimon Schubert 		{
15145796c8dcSSimon Schubert 		  prev = sym;
15155796c8dcSSimon Schubert 		  sym = SYMBOL_VALUE_CHAIN (sym);
15165796c8dcSSimon Schubert 		}
15175796c8dcSSimon Schubert 	    }
15185796c8dcSSimon Schubert 	}
15195796c8dcSSimon Schubert     }
15205796c8dcSSimon Schubert }
15215796c8dcSSimon Schubert 
15225796c8dcSSimon Schubert static int
coff_reg_to_regnum(struct symbol * sym,struct gdbarch * gdbarch)15235796c8dcSSimon Schubert coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
15245796c8dcSSimon Schubert {
15255796c8dcSSimon Schubert   return gdbarch_sdb_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
15265796c8dcSSimon Schubert }
15275796c8dcSSimon Schubert 
15285796c8dcSSimon Schubert static const struct symbol_register_ops coff_register_funcs = {
15295796c8dcSSimon Schubert   coff_reg_to_regnum
15305796c8dcSSimon Schubert };
15315796c8dcSSimon Schubert 
15325796c8dcSSimon Schubert static struct symbol *
process_coff_symbol(struct coff_symbol * cs,union internal_auxent * aux,struct objfile * objfile)15335796c8dcSSimon Schubert process_coff_symbol (struct coff_symbol *cs,
15345796c8dcSSimon Schubert 		     union internal_auxent *aux,
15355796c8dcSSimon Schubert 		     struct objfile *objfile)
15365796c8dcSSimon Schubert {
15375796c8dcSSimon Schubert   struct symbol *sym
15385796c8dcSSimon Schubert     = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
15395796c8dcSSimon Schubert 				       sizeof (struct symbol));
15405796c8dcSSimon Schubert   char *name;
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert   memset (sym, 0, sizeof (struct symbol));
15435796c8dcSSimon Schubert   name = cs->c_name;
15445796c8dcSSimon Schubert   name = EXTERNAL_NAME (name, objfile->obfd);
1545c50c785cSJohn Marino   SYMBOL_SET_LANGUAGE (sym, current_subfile->language);
1546cf7f2e2dSJohn Marino   SYMBOL_SET_NAMES (sym, name, strlen (name), 1, objfile);
15475796c8dcSSimon Schubert 
15485796c8dcSSimon Schubert   /* default assumptions */
15495796c8dcSSimon Schubert   SYMBOL_VALUE (sym) = cs->c_value;
15505796c8dcSSimon Schubert   SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15515796c8dcSSimon Schubert   SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
15525796c8dcSSimon Schubert 
15535796c8dcSSimon Schubert   if (ISFCN (cs->c_type))
15545796c8dcSSimon Schubert     {
1555c50c785cSJohn Marino       SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets,
1556c50c785cSJohn Marino 				      SECT_OFF_TEXT (objfile));
15575796c8dcSSimon Schubert       SYMBOL_TYPE (sym) =
1558c50c785cSJohn Marino 	lookup_function_type (decode_function_type (cs, cs->c_type,
1559c50c785cSJohn Marino 						    aux, objfile));
15605796c8dcSSimon Schubert 
15615796c8dcSSimon Schubert       SYMBOL_CLASS (sym) = LOC_BLOCK;
15625796c8dcSSimon Schubert       if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
15635796c8dcSSimon Schubert 	  || cs->c_sclass == C_THUMBSTATFUNC)
15645796c8dcSSimon Schubert 	add_symbol_to_list (sym, &file_symbols);
15655796c8dcSSimon Schubert       else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
15665796c8dcSSimon Schubert 	       || cs->c_sclass == C_THUMBEXTFUNC)
15675796c8dcSSimon Schubert 	add_symbol_to_list (sym, &global_symbols);
15685796c8dcSSimon Schubert     }
15695796c8dcSSimon Schubert   else
15705796c8dcSSimon Schubert     {
15715796c8dcSSimon Schubert       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux, objfile);
15725796c8dcSSimon Schubert       switch (cs->c_sclass)
15735796c8dcSSimon Schubert 	{
15745796c8dcSSimon Schubert 	case C_NULL:
15755796c8dcSSimon Schubert 	  break;
15765796c8dcSSimon Schubert 
15775796c8dcSSimon Schubert 	case C_AUTO:
15785796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_LOCAL;
15795796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &local_symbols);
15805796c8dcSSimon Schubert 	  break;
15815796c8dcSSimon Schubert 
15825796c8dcSSimon Schubert 	case C_THUMBEXT:
15835796c8dcSSimon Schubert 	case C_THUMBEXTFUNC:
15845796c8dcSSimon Schubert 	case C_EXT:
15855796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_STATIC;
15865796c8dcSSimon Schubert 	  SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1587c50c785cSJohn Marino 	  SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1588c50c785cSJohn Marino 						  SECT_OFF_TEXT (objfile));
15895796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &global_symbols);
15905796c8dcSSimon Schubert 	  break;
15915796c8dcSSimon Schubert 
15925796c8dcSSimon Schubert 	case C_THUMBSTAT:
15935796c8dcSSimon Schubert 	case C_THUMBSTATFUNC:
15945796c8dcSSimon Schubert 	case C_STAT:
15955796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_STATIC;
15965796c8dcSSimon Schubert 	  SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1597c50c785cSJohn Marino 	  SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1598c50c785cSJohn Marino 						  SECT_OFF_TEXT (objfile));
15995796c8dcSSimon Schubert 	  if (within_function)
16005796c8dcSSimon Schubert 	    {
1601c50c785cSJohn Marino 	      /* Static symbol of local scope.  */
16025796c8dcSSimon Schubert 	      add_symbol_to_list (sym, &local_symbols);
16035796c8dcSSimon Schubert 	    }
16045796c8dcSSimon Schubert 	  else
16055796c8dcSSimon Schubert 	    {
1606c50c785cSJohn Marino 	      /* Static symbol at top level of file.  */
16075796c8dcSSimon Schubert 	      add_symbol_to_list (sym, &file_symbols);
16085796c8dcSSimon Schubert 	    }
16095796c8dcSSimon Schubert 	  break;
16105796c8dcSSimon Schubert 
16115796c8dcSSimon Schubert #ifdef C_GLBLREG		/* AMD coff */
16125796c8dcSSimon Schubert 	case C_GLBLREG:
16135796c8dcSSimon Schubert #endif
16145796c8dcSSimon Schubert 	case C_REG:
16155796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_REGISTER;
16165796c8dcSSimon Schubert 	  SYMBOL_REGISTER_OPS (sym) = &coff_register_funcs;
16175796c8dcSSimon Schubert 	  SYMBOL_VALUE (sym) = cs->c_value;
16185796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &local_symbols);
16195796c8dcSSimon Schubert 	  break;
16205796c8dcSSimon Schubert 
16215796c8dcSSimon Schubert 	case C_THUMBLABEL:
16225796c8dcSSimon Schubert 	case C_LABEL:
16235796c8dcSSimon Schubert 	  break;
16245796c8dcSSimon Schubert 
16255796c8dcSSimon Schubert 	case C_ARG:
16265796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_ARG;
16275796c8dcSSimon Schubert 	  SYMBOL_IS_ARGUMENT (sym) = 1;
16285796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &local_symbols);
16295796c8dcSSimon Schubert 	  break;
16305796c8dcSSimon Schubert 
16315796c8dcSSimon Schubert 	case C_REGPARM:
16325796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_REGISTER;
16335796c8dcSSimon Schubert 	  SYMBOL_REGISTER_OPS (sym) = &coff_register_funcs;
16345796c8dcSSimon Schubert 	  SYMBOL_IS_ARGUMENT (sym) = 1;
16355796c8dcSSimon Schubert 	  SYMBOL_VALUE (sym) = cs->c_value;
16365796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &local_symbols);
16375796c8dcSSimon Schubert 	  break;
16385796c8dcSSimon Schubert 
16395796c8dcSSimon Schubert 	case C_TPDEF:
16405796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16415796c8dcSSimon Schubert 	  SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16425796c8dcSSimon Schubert 
1643c50c785cSJohn Marino 	  /* If type has no name, give it one.  */
16445796c8dcSSimon Schubert 	  if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16455796c8dcSSimon Schubert 	    {
16465796c8dcSSimon Schubert 	      if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
16475796c8dcSSimon Schubert 		  || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
16485796c8dcSSimon Schubert 		{
1649c50c785cSJohn Marino 		  /* If we are giving a name to a type such as
1650c50c785cSJohn Marino 		     "pointer to foo" or "function returning foo", we
1651c50c785cSJohn Marino 		     better not set the TYPE_NAME.  If the program
1652c50c785cSJohn Marino 		     contains "typedef char *caddr_t;", we don't want
1653c50c785cSJohn Marino 		     all variables of type char * to print as caddr_t.
1654c50c785cSJohn Marino 		     This is not just a consequence of GDB's type
1655c50c785cSJohn Marino 		     management; CC and GCC (at least through version
1656c50c785cSJohn Marino 		     2.4) both output variables of either type char *
1657c50c785cSJohn Marino 		     or caddr_t with the type refering to the C_TPDEF
1658c50c785cSJohn Marino 		     symbol for caddr_t.  If a future compiler cleans
1659c50c785cSJohn Marino 		     this up it GDB is not ready for it yet, but if it
1660c50c785cSJohn Marino 		     becomes ready we somehow need to disable this
1661c50c785cSJohn Marino 		     check (without breaking the PCC/GCC2.4 case).
16625796c8dcSSimon Schubert 
16635796c8dcSSimon Schubert 		     Sigh.
16645796c8dcSSimon Schubert 
16655796c8dcSSimon Schubert 		     Fortunately, this check seems not to be necessary
16665796c8dcSSimon Schubert 		     for anything except pointers or functions.  */
16675796c8dcSSimon Schubert 		  ;
16685796c8dcSSimon Schubert 		}
16695796c8dcSSimon Schubert 	      else
16705796c8dcSSimon Schubert 		TYPE_NAME (SYMBOL_TYPE (sym)) =
1671*ef5ccd6cSJohn Marino 		  xstrdup (SYMBOL_LINKAGE_NAME (sym));
16725796c8dcSSimon Schubert 	    }
16735796c8dcSSimon Schubert 
1674c50c785cSJohn Marino 	  /* Keep track of any type which points to empty structured
1675c50c785cSJohn Marino 	     type, so it can be filled from a definition from another
1676c50c785cSJohn Marino 	     file.  A simple forward reference (TYPE_CODE_UNDEF) is
1677c50c785cSJohn Marino 	     not an empty structured type, though; the forward
1678c50c785cSJohn Marino 	     references work themselves out via the magic of
1679c50c785cSJohn Marino 	     coff_lookup_type.  */
1680cf7f2e2dSJohn Marino 	  if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1681cf7f2e2dSJohn Marino 	      && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0
1682cf7f2e2dSJohn Marino 	      && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym)))
1683cf7f2e2dSJohn Marino 	         != TYPE_CODE_UNDEF)
16845796c8dcSSimon Schubert 	    {
16855796c8dcSSimon Schubert 	      int i = hashname (SYMBOL_LINKAGE_NAME (sym));
16865796c8dcSSimon Schubert 
16875796c8dcSSimon Schubert 	      SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
16885796c8dcSSimon Schubert 	      opaque_type_chain[i] = sym;
16895796c8dcSSimon Schubert 	    }
16905796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &file_symbols);
16915796c8dcSSimon Schubert 	  break;
16925796c8dcSSimon Schubert 
16935796c8dcSSimon Schubert 	case C_STRTAG:
16945796c8dcSSimon Schubert 	case C_UNTAG:
16955796c8dcSSimon Schubert 	case C_ENTAG:
16965796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16975796c8dcSSimon Schubert 	  SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16985796c8dcSSimon Schubert 
16995796c8dcSSimon Schubert 	  /* Some compilers try to be helpful by inventing "fake"
17005796c8dcSSimon Schubert 	     names for anonymous enums, structures, and unions, like
17015796c8dcSSimon Schubert 	     "~0fake" or ".0fake".  Thanks, but no thanks...  */
17025796c8dcSSimon Schubert 	  if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
17035796c8dcSSimon Schubert 	    if (SYMBOL_LINKAGE_NAME (sym) != NULL
17045796c8dcSSimon Schubert 		&& *SYMBOL_LINKAGE_NAME (sym) != '~'
17055796c8dcSSimon Schubert 		&& *SYMBOL_LINKAGE_NAME (sym) != '.')
17065796c8dcSSimon Schubert 	      TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
17075796c8dcSSimon Schubert 		concat (SYMBOL_LINKAGE_NAME (sym), (char *)NULL);
17085796c8dcSSimon Schubert 
17095796c8dcSSimon Schubert 	  add_symbol_to_list (sym, &file_symbols);
17105796c8dcSSimon Schubert 	  break;
17115796c8dcSSimon Schubert 
17125796c8dcSSimon Schubert 	default:
17135796c8dcSSimon Schubert 	  break;
17145796c8dcSSimon Schubert 	}
17155796c8dcSSimon Schubert     }
17165796c8dcSSimon Schubert   return sym;
17175796c8dcSSimon Schubert }
17185796c8dcSSimon Schubert 
17195796c8dcSSimon Schubert /* Decode a coff type specifier;  return the type that is meant.  */
17205796c8dcSSimon Schubert 
17215796c8dcSSimon Schubert static struct type *
decode_type(struct coff_symbol * cs,unsigned int c_type,union internal_auxent * aux,struct objfile * objfile)17225796c8dcSSimon Schubert decode_type (struct coff_symbol *cs, unsigned int c_type,
17235796c8dcSSimon Schubert 	     union internal_auxent *aux, struct objfile *objfile)
17245796c8dcSSimon Schubert {
17255796c8dcSSimon Schubert   struct type *type = 0;
17265796c8dcSSimon Schubert   unsigned int new_c_type;
17275796c8dcSSimon Schubert 
17285796c8dcSSimon Schubert   if (c_type & ~N_BTMASK)
17295796c8dcSSimon Schubert     {
17305796c8dcSSimon Schubert       new_c_type = DECREF (c_type);
17315796c8dcSSimon Schubert       if (ISPTR (c_type))
17325796c8dcSSimon Schubert 	{
17335796c8dcSSimon Schubert 	  type = decode_type (cs, new_c_type, aux, objfile);
17345796c8dcSSimon Schubert 	  type = lookup_pointer_type (type);
17355796c8dcSSimon Schubert 	}
17365796c8dcSSimon Schubert       else if (ISFCN (c_type))
17375796c8dcSSimon Schubert 	{
17385796c8dcSSimon Schubert 	  type = decode_type (cs, new_c_type, aux, objfile);
17395796c8dcSSimon Schubert 	  type = lookup_function_type (type);
17405796c8dcSSimon Schubert 	}
17415796c8dcSSimon Schubert       else if (ISARY (c_type))
17425796c8dcSSimon Schubert 	{
17435796c8dcSSimon Schubert 	  int i, n;
17445796c8dcSSimon Schubert 	  unsigned short *dim;
17455796c8dcSSimon Schubert 	  struct type *base_type, *index_type, *range_type;
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert 	  /* Define an array type.  */
1748c50c785cSJohn Marino 	  /* auxent refers to array, not base type.  */
17495796c8dcSSimon Schubert 	  if (aux->x_sym.x_tagndx.l == 0)
17505796c8dcSSimon Schubert 	    cs->c_naux = 0;
17515796c8dcSSimon Schubert 
1752c50c785cSJohn Marino 	  /* Shift the indices down.  */
17535796c8dcSSimon Schubert 	  dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
17545796c8dcSSimon Schubert 	  i = 1;
17555796c8dcSSimon Schubert 	  n = dim[0];
17565796c8dcSSimon Schubert 	  for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
17575796c8dcSSimon Schubert 	    *dim = *(dim + 1);
17585796c8dcSSimon Schubert 	  *dim = 0;
17595796c8dcSSimon Schubert 
17605796c8dcSSimon Schubert 	  base_type = decode_type (cs, new_c_type, aux, objfile);
17615796c8dcSSimon Schubert 	  index_type = objfile_type (objfile)->builtin_int;
17625796c8dcSSimon Schubert 	  range_type =
1763c50c785cSJohn Marino 	    create_range_type ((struct type *) NULL,
1764c50c785cSJohn Marino 			       index_type, 0, n - 1);
17655796c8dcSSimon Schubert 	  type =
1766c50c785cSJohn Marino 	    create_array_type ((struct type *) NULL,
1767c50c785cSJohn Marino 			       base_type, range_type);
17685796c8dcSSimon Schubert 	}
17695796c8dcSSimon Schubert       return type;
17705796c8dcSSimon Schubert     }
17715796c8dcSSimon Schubert 
1772c50c785cSJohn Marino   /* Reference to existing type.  This only occurs with the struct,
1773c50c785cSJohn Marino      union, and enum types.  EPI a29k coff fakes us out by producing
1774c50c785cSJohn Marino      aux entries with a nonzero x_tagndx for definitions of structs,
1775c50c785cSJohn Marino      unions, and enums, so we have to check the c_sclass field.  SCO
1776c50c785cSJohn Marino      3.2v4 cc gets confused with pointers to pointers to defined
1777c50c785cSJohn Marino      structs, and generates negative x_tagndx fields.  */
17785796c8dcSSimon Schubert   if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
17795796c8dcSSimon Schubert     {
17805796c8dcSSimon Schubert       if (cs->c_sclass != C_STRTAG
17815796c8dcSSimon Schubert 	  && cs->c_sclass != C_UNTAG
17825796c8dcSSimon Schubert 	  && cs->c_sclass != C_ENTAG
17835796c8dcSSimon Schubert 	  && aux->x_sym.x_tagndx.l >= 0)
17845796c8dcSSimon Schubert 	{
17855796c8dcSSimon Schubert 	  type = coff_alloc_type (aux->x_sym.x_tagndx.l);
17865796c8dcSSimon Schubert 	  return type;
17875796c8dcSSimon Schubert 	}
17885796c8dcSSimon Schubert       else
17895796c8dcSSimon Schubert 	{
17905796c8dcSSimon Schubert 	  complaint (&symfile_complaints,
17915796c8dcSSimon Schubert 		     _("Symbol table entry for %s has bad tagndx value"),
17925796c8dcSSimon Schubert 		     cs->c_name);
17935796c8dcSSimon Schubert 	  /* And fall through to decode_base_type...  */
17945796c8dcSSimon Schubert 	}
17955796c8dcSSimon Schubert     }
17965796c8dcSSimon Schubert 
17975796c8dcSSimon Schubert   return decode_base_type (cs, BTYPE (c_type), aux, objfile);
17985796c8dcSSimon Schubert }
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert /* Decode a coff type specifier for function definition;
18015796c8dcSSimon Schubert    return the type that the function returns.  */
18025796c8dcSSimon Schubert 
18035796c8dcSSimon Schubert static struct type *
decode_function_type(struct coff_symbol * cs,unsigned int c_type,union internal_auxent * aux,struct objfile * objfile)1804c50c785cSJohn Marino decode_function_type (struct coff_symbol *cs,
1805c50c785cSJohn Marino 		      unsigned int c_type,
1806c50c785cSJohn Marino 		      union internal_auxent *aux,
1807c50c785cSJohn Marino 		      struct objfile *objfile)
18085796c8dcSSimon Schubert {
18095796c8dcSSimon Schubert   if (aux->x_sym.x_tagndx.l == 0)
1810c50c785cSJohn Marino     cs->c_naux = 0;	/* auxent refers to function, not base
1811c50c785cSJohn Marino 			   type.  */
18125796c8dcSSimon Schubert 
18135796c8dcSSimon Schubert   return decode_type (cs, DECREF (c_type), aux, objfile);
18145796c8dcSSimon Schubert }
18155796c8dcSSimon Schubert 
1816c50c785cSJohn Marino /* Basic C types.  */
18175796c8dcSSimon Schubert 
18185796c8dcSSimon Schubert static struct type *
decode_base_type(struct coff_symbol * cs,unsigned int c_type,union internal_auxent * aux,struct objfile * objfile)1819c50c785cSJohn Marino decode_base_type (struct coff_symbol *cs,
1820c50c785cSJohn Marino 		  unsigned int c_type,
1821c50c785cSJohn Marino 		  union internal_auxent *aux,
1822c50c785cSJohn Marino 		  struct objfile *objfile)
18235796c8dcSSimon Schubert {
18245796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
18255796c8dcSSimon Schubert   struct type *type;
18265796c8dcSSimon Schubert 
18275796c8dcSSimon Schubert   switch (c_type)
18285796c8dcSSimon Schubert     {
18295796c8dcSSimon Schubert     case T_NULL:
1830c50c785cSJohn Marino       /* Shows up with "void (*foo)();" structure members.  */
18315796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_void;
18325796c8dcSSimon Schubert 
18335796c8dcSSimon Schubert #ifdef T_VOID
18345796c8dcSSimon Schubert     case T_VOID:
18355796c8dcSSimon Schubert       /* Intel 960 COFF has this symbol and meaning.  */
18365796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_void;
18375796c8dcSSimon Schubert #endif
18385796c8dcSSimon Schubert 
18395796c8dcSSimon Schubert     case T_CHAR:
18405796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_char;
18415796c8dcSSimon Schubert 
18425796c8dcSSimon Schubert     case T_SHORT:
18435796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_short;
18445796c8dcSSimon Schubert 
18455796c8dcSSimon Schubert     case T_INT:
18465796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_int;
18475796c8dcSSimon Schubert 
18485796c8dcSSimon Schubert     case T_LONG:
18495796c8dcSSimon Schubert       if (cs->c_sclass == C_FIELD
18505796c8dcSSimon Schubert 	  && aux->x_sym.x_misc.x_lnsz.x_size
18515796c8dcSSimon Schubert 	     > gdbarch_long_bit (gdbarch))
18525796c8dcSSimon Schubert 	return objfile_type (objfile)->builtin_long_long;
18535796c8dcSSimon Schubert       else
18545796c8dcSSimon Schubert 	return objfile_type (objfile)->builtin_long;
18555796c8dcSSimon Schubert 
18565796c8dcSSimon Schubert     case T_FLOAT:
18575796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_float;
18585796c8dcSSimon Schubert 
18595796c8dcSSimon Schubert     case T_DOUBLE:
18605796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_double;
18615796c8dcSSimon Schubert 
18625796c8dcSSimon Schubert     case T_LNGDBL:
18635796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_long_double;
18645796c8dcSSimon Schubert 
18655796c8dcSSimon Schubert     case T_STRUCT:
18665796c8dcSSimon Schubert       if (cs->c_naux != 1)
18675796c8dcSSimon Schubert 	{
1868c50c785cSJohn Marino 	  /* Anonymous structure type.  */
18695796c8dcSSimon Schubert 	  type = coff_alloc_type (cs->c_symnum);
18705796c8dcSSimon Schubert 	  TYPE_CODE (type) = TYPE_CODE_STRUCT;
18715796c8dcSSimon Schubert 	  TYPE_NAME (type) = NULL;
1872c50c785cSJohn Marino 	  /* This used to set the tag to "<opaque>".  But I think
1873c50c785cSJohn Marino 	     setting it to NULL is right, and the printing code can
1874c50c785cSJohn Marino 	     print it as "struct {...}".  */
18755796c8dcSSimon Schubert 	  TYPE_TAG_NAME (type) = NULL;
18765796c8dcSSimon Schubert 	  INIT_CPLUS_SPECIFIC (type);
18775796c8dcSSimon Schubert 	  TYPE_LENGTH (type) = 0;
18785796c8dcSSimon Schubert 	  TYPE_FIELDS (type) = 0;
18795796c8dcSSimon Schubert 	  TYPE_NFIELDS (type) = 0;
18805796c8dcSSimon Schubert 	}
18815796c8dcSSimon Schubert       else
18825796c8dcSSimon Schubert 	{
18835796c8dcSSimon Schubert 	  type = coff_read_struct_type (cs->c_symnum,
18845796c8dcSSimon Schubert 					aux->x_sym.x_misc.x_lnsz.x_size,
18855796c8dcSSimon Schubert 					aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
18865796c8dcSSimon Schubert 					objfile);
18875796c8dcSSimon Schubert 	}
18885796c8dcSSimon Schubert       return type;
18895796c8dcSSimon Schubert 
18905796c8dcSSimon Schubert     case T_UNION:
18915796c8dcSSimon Schubert       if (cs->c_naux != 1)
18925796c8dcSSimon Schubert 	{
1893c50c785cSJohn Marino 	  /* Anonymous union type.  */
18945796c8dcSSimon Schubert 	  type = coff_alloc_type (cs->c_symnum);
18955796c8dcSSimon Schubert 	  TYPE_NAME (type) = NULL;
1896c50c785cSJohn Marino 	  /* This used to set the tag to "<opaque>".  But I think
1897c50c785cSJohn Marino 	     setting it to NULL is right, and the printing code can
1898c50c785cSJohn Marino 	     print it as "union {...}".  */
18995796c8dcSSimon Schubert 	  TYPE_TAG_NAME (type) = NULL;
19005796c8dcSSimon Schubert 	  INIT_CPLUS_SPECIFIC (type);
19015796c8dcSSimon Schubert 	  TYPE_LENGTH (type) = 0;
19025796c8dcSSimon Schubert 	  TYPE_FIELDS (type) = 0;
19035796c8dcSSimon Schubert 	  TYPE_NFIELDS (type) = 0;
19045796c8dcSSimon Schubert 	}
19055796c8dcSSimon Schubert       else
19065796c8dcSSimon Schubert 	{
19075796c8dcSSimon Schubert 	  type = coff_read_struct_type (cs->c_symnum,
19085796c8dcSSimon Schubert 					aux->x_sym.x_misc.x_lnsz.x_size,
19095796c8dcSSimon Schubert 					aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
19105796c8dcSSimon Schubert 					objfile);
19115796c8dcSSimon Schubert 	}
19125796c8dcSSimon Schubert       TYPE_CODE (type) = TYPE_CODE_UNION;
19135796c8dcSSimon Schubert       return type;
19145796c8dcSSimon Schubert 
19155796c8dcSSimon Schubert     case T_ENUM:
19165796c8dcSSimon Schubert       if (cs->c_naux != 1)
19175796c8dcSSimon Schubert 	{
1918c50c785cSJohn Marino 	  /* Anonymous enum type.  */
19195796c8dcSSimon Schubert 	  type = coff_alloc_type (cs->c_symnum);
19205796c8dcSSimon Schubert 	  TYPE_CODE (type) = TYPE_CODE_ENUM;
19215796c8dcSSimon Schubert 	  TYPE_NAME (type) = NULL;
1922c50c785cSJohn Marino 	  /* This used to set the tag to "<opaque>".  But I think
1923c50c785cSJohn Marino 	     setting it to NULL is right, and the printing code can
1924c50c785cSJohn Marino 	     print it as "enum {...}".  */
19255796c8dcSSimon Schubert 	  TYPE_TAG_NAME (type) = NULL;
19265796c8dcSSimon Schubert 	  TYPE_LENGTH (type) = 0;
19275796c8dcSSimon Schubert 	  TYPE_FIELDS (type) = 0;
19285796c8dcSSimon Schubert 	  TYPE_NFIELDS (type) = 0;
19295796c8dcSSimon Schubert 	}
19305796c8dcSSimon Schubert       else
19315796c8dcSSimon Schubert 	{
19325796c8dcSSimon Schubert 	  type = coff_read_enum_type (cs->c_symnum,
19335796c8dcSSimon Schubert 				      aux->x_sym.x_misc.x_lnsz.x_size,
19345796c8dcSSimon Schubert 				      aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
19355796c8dcSSimon Schubert 				      objfile);
19365796c8dcSSimon Schubert 	}
19375796c8dcSSimon Schubert       return type;
19385796c8dcSSimon Schubert 
19395796c8dcSSimon Schubert     case T_MOE:
1940c50c785cSJohn Marino       /* Shouldn't show up here.  */
19415796c8dcSSimon Schubert       break;
19425796c8dcSSimon Schubert 
19435796c8dcSSimon Schubert     case T_UCHAR:
19445796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_unsigned_char;
19455796c8dcSSimon Schubert 
19465796c8dcSSimon Schubert     case T_USHORT:
19475796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_unsigned_short;
19485796c8dcSSimon Schubert 
19495796c8dcSSimon Schubert     case T_UINT:
19505796c8dcSSimon Schubert       return objfile_type (objfile)->builtin_unsigned_int;
19515796c8dcSSimon Schubert 
19525796c8dcSSimon Schubert     case T_ULONG:
19535796c8dcSSimon Schubert       if (cs->c_sclass == C_FIELD
19545796c8dcSSimon Schubert 	  && aux->x_sym.x_misc.x_lnsz.x_size
19555796c8dcSSimon Schubert 	     > gdbarch_long_bit (gdbarch))
19565796c8dcSSimon Schubert 	return objfile_type (objfile)->builtin_unsigned_long_long;
19575796c8dcSSimon Schubert       else
19585796c8dcSSimon Schubert 	return objfile_type (objfile)->builtin_unsigned_long;
19595796c8dcSSimon Schubert     }
1960c50c785cSJohn Marino   complaint (&symfile_complaints,
1961c50c785cSJohn Marino 	     _("Unexpected type for symbol %s"), cs->c_name);
19625796c8dcSSimon Schubert   return objfile_type (objfile)->builtin_void;
19635796c8dcSSimon Schubert }
19645796c8dcSSimon Schubert 
19655796c8dcSSimon Schubert /* This page contains subroutines of read_type.  */
19665796c8dcSSimon Schubert 
19675796c8dcSSimon Schubert /* Read the description of a structure (or union type) and return an
19685796c8dcSSimon Schubert    object describing the type.  */
19695796c8dcSSimon Schubert 
19705796c8dcSSimon Schubert static struct type *
coff_read_struct_type(int index,int length,int lastsym,struct objfile * objfile)19715796c8dcSSimon Schubert coff_read_struct_type (int index, int length, int lastsym,
19725796c8dcSSimon Schubert 		       struct objfile *objfile)
19735796c8dcSSimon Schubert {
19745796c8dcSSimon Schubert   struct nextfield
19755796c8dcSSimon Schubert     {
19765796c8dcSSimon Schubert       struct nextfield *next;
19775796c8dcSSimon Schubert       struct field field;
19785796c8dcSSimon Schubert     };
19795796c8dcSSimon Schubert 
19805796c8dcSSimon Schubert   struct type *type;
19815796c8dcSSimon Schubert   struct nextfield *list = 0;
19825796c8dcSSimon Schubert   struct nextfield *new;
19835796c8dcSSimon Schubert   int nfields = 0;
19845796c8dcSSimon Schubert   int n;
19855796c8dcSSimon Schubert   char *name;
19865796c8dcSSimon Schubert   struct coff_symbol member_sym;
19875796c8dcSSimon Schubert   struct coff_symbol *ms = &member_sym;
19885796c8dcSSimon Schubert   struct internal_syment sub_sym;
19895796c8dcSSimon Schubert   union internal_auxent sub_aux;
19905796c8dcSSimon Schubert   int done = 0;
19915796c8dcSSimon Schubert 
19925796c8dcSSimon Schubert   type = coff_alloc_type (index);
19935796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_STRUCT;
19945796c8dcSSimon Schubert   INIT_CPLUS_SPECIFIC (type);
19955796c8dcSSimon Schubert   TYPE_LENGTH (type) = length;
19965796c8dcSSimon Schubert 
19975796c8dcSSimon Schubert   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
19985796c8dcSSimon Schubert     {
19995796c8dcSSimon Schubert       read_one_sym (ms, &sub_sym, &sub_aux);
20005796c8dcSSimon Schubert       name = ms->c_name;
20015796c8dcSSimon Schubert       name = EXTERNAL_NAME (name, objfile->obfd);
20025796c8dcSSimon Schubert 
20035796c8dcSSimon Schubert       switch (ms->c_sclass)
20045796c8dcSSimon Schubert 	{
20055796c8dcSSimon Schubert 	case C_MOS:
20065796c8dcSSimon Schubert 	case C_MOU:
20075796c8dcSSimon Schubert 
20085796c8dcSSimon Schubert 	  /* Get space to record the next field's data.  */
20095796c8dcSSimon Schubert 	  new = (struct nextfield *) alloca (sizeof (struct nextfield));
20105796c8dcSSimon Schubert 	  new->next = list;
20115796c8dcSSimon Schubert 	  list = new;
20125796c8dcSSimon Schubert 
20135796c8dcSSimon Schubert 	  /* Save the data.  */
2014*ef5ccd6cSJohn Marino 	  list->field.name = obstack_copy0 (&objfile->objfile_obstack,
2015*ef5ccd6cSJohn Marino 					    name, strlen (name));
2016c50c785cSJohn Marino 	  FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2017c50c785cSJohn Marino 						  &sub_aux, objfile);
20185796c8dcSSimon Schubert 	  SET_FIELD_BITPOS (list->field, 8 * ms->c_value);
20195796c8dcSSimon Schubert 	  FIELD_BITSIZE (list->field) = 0;
20205796c8dcSSimon Schubert 	  nfields++;
20215796c8dcSSimon Schubert 	  break;
20225796c8dcSSimon Schubert 
20235796c8dcSSimon Schubert 	case C_FIELD:
20245796c8dcSSimon Schubert 
20255796c8dcSSimon Schubert 	  /* Get space to record the next field's data.  */
20265796c8dcSSimon Schubert 	  new = (struct nextfield *) alloca (sizeof (struct nextfield));
20275796c8dcSSimon Schubert 	  new->next = list;
20285796c8dcSSimon Schubert 	  list = new;
20295796c8dcSSimon Schubert 
20305796c8dcSSimon Schubert 	  /* Save the data.  */
2031*ef5ccd6cSJohn Marino 	  list->field.name = obstack_copy0 (&objfile->objfile_obstack,
2032*ef5ccd6cSJohn Marino 					    name, strlen (name));
2033c50c785cSJohn Marino 	  FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2034c50c785cSJohn Marino 						  &sub_aux, objfile);
20355796c8dcSSimon Schubert 	  SET_FIELD_BITPOS (list->field, ms->c_value);
20365796c8dcSSimon Schubert 	  FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
20375796c8dcSSimon Schubert 	  nfields++;
20385796c8dcSSimon Schubert 	  break;
20395796c8dcSSimon Schubert 
20405796c8dcSSimon Schubert 	case C_EOS:
20415796c8dcSSimon Schubert 	  done = 1;
20425796c8dcSSimon Schubert 	  break;
20435796c8dcSSimon Schubert 	}
20445796c8dcSSimon Schubert     }
20455796c8dcSSimon Schubert   /* Now create the vector of fields, and record how big it is.  */
20465796c8dcSSimon Schubert 
20475796c8dcSSimon Schubert   TYPE_NFIELDS (type) = nfields;
20485796c8dcSSimon Schubert   TYPE_FIELDS (type) = (struct field *)
20495796c8dcSSimon Schubert     TYPE_ALLOC (type, sizeof (struct field) * nfields);
20505796c8dcSSimon Schubert 
20515796c8dcSSimon Schubert   /* Copy the saved-up fields into the field vector.  */
20525796c8dcSSimon Schubert 
20535796c8dcSSimon Schubert   for (n = nfields; list; list = list->next)
20545796c8dcSSimon Schubert     TYPE_FIELD (type, --n) = list->field;
20555796c8dcSSimon Schubert 
20565796c8dcSSimon Schubert   return type;
20575796c8dcSSimon Schubert }
20585796c8dcSSimon Schubert 
20595796c8dcSSimon Schubert /* Read a definition of an enumeration type,
20605796c8dcSSimon Schubert    and create and return a suitable type object.
20615796c8dcSSimon Schubert    Also defines the symbols that represent the values of the type.  */
20625796c8dcSSimon Schubert 
20635796c8dcSSimon Schubert static struct type *
coff_read_enum_type(int index,int length,int lastsym,struct objfile * objfile)20645796c8dcSSimon Schubert coff_read_enum_type (int index, int length, int lastsym,
20655796c8dcSSimon Schubert 		     struct objfile *objfile)
20665796c8dcSSimon Schubert {
20675796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
20685796c8dcSSimon Schubert   struct symbol *sym;
20695796c8dcSSimon Schubert   struct type *type;
20705796c8dcSSimon Schubert   int nsyms = 0;
20715796c8dcSSimon Schubert   int done = 0;
20725796c8dcSSimon Schubert   struct pending **symlist;
20735796c8dcSSimon Schubert   struct coff_symbol member_sym;
20745796c8dcSSimon Schubert   struct coff_symbol *ms = &member_sym;
20755796c8dcSSimon Schubert   struct internal_syment sub_sym;
20765796c8dcSSimon Schubert   union internal_auxent sub_aux;
20775796c8dcSSimon Schubert   struct pending *osyms, *syms;
20785796c8dcSSimon Schubert   int o_nsyms;
20795796c8dcSSimon Schubert   int n;
20805796c8dcSSimon Schubert   char *name;
20815796c8dcSSimon Schubert   int unsigned_enum = 1;
20825796c8dcSSimon Schubert 
20835796c8dcSSimon Schubert   type = coff_alloc_type (index);
20845796c8dcSSimon Schubert   if (within_function)
20855796c8dcSSimon Schubert     symlist = &local_symbols;
20865796c8dcSSimon Schubert   else
20875796c8dcSSimon Schubert     symlist = &file_symbols;
20885796c8dcSSimon Schubert   osyms = *symlist;
20895796c8dcSSimon Schubert   o_nsyms = osyms ? osyms->nsyms : 0;
20905796c8dcSSimon Schubert 
20915796c8dcSSimon Schubert   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
20925796c8dcSSimon Schubert     {
20935796c8dcSSimon Schubert       read_one_sym (ms, &sub_sym, &sub_aux);
20945796c8dcSSimon Schubert       name = ms->c_name;
20955796c8dcSSimon Schubert       name = EXTERNAL_NAME (name, objfile->obfd);
20965796c8dcSSimon Schubert 
20975796c8dcSSimon Schubert       switch (ms->c_sclass)
20985796c8dcSSimon Schubert 	{
20995796c8dcSSimon Schubert 	case C_MOE:
21005796c8dcSSimon Schubert 	  sym = (struct symbol *) obstack_alloc
21015796c8dcSSimon Schubert 	    (&objfile->objfile_obstack, sizeof (struct symbol));
21025796c8dcSSimon Schubert 	  memset (sym, 0, sizeof (struct symbol));
21035796c8dcSSimon Schubert 
21045796c8dcSSimon Schubert 	  SYMBOL_SET_LINKAGE_NAME (sym,
2105*ef5ccd6cSJohn Marino 				   obstack_copy0 (&objfile->objfile_obstack,
2106*ef5ccd6cSJohn Marino 						  name, strlen (name)));
21075796c8dcSSimon Schubert 	  SYMBOL_CLASS (sym) = LOC_CONST;
21085796c8dcSSimon Schubert 	  SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21095796c8dcSSimon Schubert 	  SYMBOL_VALUE (sym) = ms->c_value;
21105796c8dcSSimon Schubert 	  add_symbol_to_list (sym, symlist);
21115796c8dcSSimon Schubert 	  nsyms++;
21125796c8dcSSimon Schubert 	  break;
21135796c8dcSSimon Schubert 
21145796c8dcSSimon Schubert 	case C_EOS:
21155796c8dcSSimon Schubert 	  /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
21165796c8dcSSimon Schubert 	     up the count of how many symbols to read.  So stop
21175796c8dcSSimon Schubert 	     on .eos.  */
21185796c8dcSSimon Schubert 	  done = 1;
21195796c8dcSSimon Schubert 	  break;
21205796c8dcSSimon Schubert 	}
21215796c8dcSSimon Schubert     }
21225796c8dcSSimon Schubert 
21235796c8dcSSimon Schubert   /* Now fill in the fields of the type-structure.  */
21245796c8dcSSimon Schubert 
21255796c8dcSSimon Schubert   if (length > 0)
21265796c8dcSSimon Schubert     TYPE_LENGTH (type) = length;
21275796c8dcSSimon Schubert   else /* Assume ints.  */
21285796c8dcSSimon Schubert     TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
21295796c8dcSSimon Schubert   TYPE_CODE (type) = TYPE_CODE_ENUM;
21305796c8dcSSimon Schubert   TYPE_NFIELDS (type) = nsyms;
21315796c8dcSSimon Schubert   TYPE_FIELDS (type) = (struct field *)
21325796c8dcSSimon Schubert     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
21335796c8dcSSimon Schubert 
21345796c8dcSSimon Schubert   /* Find the symbols for the values and put them into the type.
21355796c8dcSSimon Schubert      The symbols can be found in the symlist that we put them on
21365796c8dcSSimon Schubert      to cause them to be defined.  osyms contains the old value
21375796c8dcSSimon Schubert      of that symlist; everything up to there was defined by us.  */
21385796c8dcSSimon Schubert   /* Note that we preserve the order of the enum constants, so
21395796c8dcSSimon Schubert      that in something like "enum {FOO, LAST_THING=FOO}" we print
21405796c8dcSSimon Schubert      FOO, not LAST_THING.  */
21415796c8dcSSimon Schubert 
21425796c8dcSSimon Schubert   for (syms = *symlist, n = 0; syms; syms = syms->next)
21435796c8dcSSimon Schubert     {
21445796c8dcSSimon Schubert       int j = 0;
21455796c8dcSSimon Schubert 
21465796c8dcSSimon Schubert       if (syms == osyms)
21475796c8dcSSimon Schubert 	j = o_nsyms;
21485796c8dcSSimon Schubert       for (; j < syms->nsyms; j++, n++)
21495796c8dcSSimon Schubert 	{
21505796c8dcSSimon Schubert 	  struct symbol *xsym = syms->symbol[j];
2151cf7f2e2dSJohn Marino 
21525796c8dcSSimon Schubert 	  SYMBOL_TYPE (xsym) = type;
21535796c8dcSSimon Schubert 	  TYPE_FIELD_NAME (type, n) = SYMBOL_LINKAGE_NAME (xsym);
2154*ef5ccd6cSJohn Marino 	  SET_FIELD_ENUMVAL (TYPE_FIELD (type, n), SYMBOL_VALUE (xsym));
21555796c8dcSSimon Schubert 	  if (SYMBOL_VALUE (xsym) < 0)
21565796c8dcSSimon Schubert 	    unsigned_enum = 0;
21575796c8dcSSimon Schubert 	  TYPE_FIELD_BITSIZE (type, n) = 0;
21585796c8dcSSimon Schubert 	}
21595796c8dcSSimon Schubert       if (syms == osyms)
21605796c8dcSSimon Schubert 	break;
21615796c8dcSSimon Schubert     }
21625796c8dcSSimon Schubert 
21635796c8dcSSimon Schubert   if (unsigned_enum)
21645796c8dcSSimon Schubert     TYPE_UNSIGNED (type) = 1;
21655796c8dcSSimon Schubert 
21665796c8dcSSimon Schubert   return type;
21675796c8dcSSimon Schubert }
21685796c8dcSSimon Schubert 
21695796c8dcSSimon Schubert /* Register our ability to parse symbols for coff BFD files.  */
21705796c8dcSSimon Schubert 
2171c50c785cSJohn Marino static const struct sym_fns coff_sym_fns =
21725796c8dcSSimon Schubert {
21735796c8dcSSimon Schubert   bfd_target_coff_flavour,
2174c50c785cSJohn Marino   coff_new_init,		/* sym_new_init: init anything gbl to
2175c50c785cSJohn Marino 				   entire symtab */
2176c50c785cSJohn Marino   coff_symfile_init,		/* sym_init: read initial info, setup
2177c50c785cSJohn Marino 				   for sym_read() */
2178c50c785cSJohn Marino   coff_symfile_read,		/* sym_read: read a symbol file into
2179c50c785cSJohn Marino 				   symtab */
2180c50c785cSJohn Marino   NULL,				/* sym_read_psymbols */
2181c50c785cSJohn Marino   coff_symfile_finish,		/* sym_finish: finished with file,
2182c50c785cSJohn Marino 				   cleanup */
2183c50c785cSJohn Marino   default_symfile_offsets,	/* sym_offsets: xlate external to
2184c50c785cSJohn Marino 				   internal form */
2185c50c785cSJohn Marino   default_symfile_segments,	/* sym_segments: Get segment
2186c50c785cSJohn Marino 				   information from a file */
21875796c8dcSSimon Schubert   NULL,                         /* sym_read_linetable  */
2188c50c785cSJohn Marino 
2189c50c785cSJohn Marino   default_symfile_relocate,	/* sym_relocate: Relocate a debug
2190c50c785cSJohn Marino 				   section.  */
2191*ef5ccd6cSJohn Marino   NULL,				/* sym_probe_fns */
2192c50c785cSJohn Marino   &psym_functions
21935796c8dcSSimon Schubert };
21945796c8dcSSimon Schubert 
2195*ef5ccd6cSJohn Marino /* Free the per-objfile COFF data.  */
2196*ef5ccd6cSJohn Marino 
2197*ef5ccd6cSJohn Marino static void
coff_free_info(struct objfile * objfile,void * arg)2198*ef5ccd6cSJohn Marino coff_free_info (struct objfile *objfile, void *arg)
2199*ef5ccd6cSJohn Marino {
2200*ef5ccd6cSJohn Marino   xfree (arg);
2201*ef5ccd6cSJohn Marino }
2202*ef5ccd6cSJohn Marino 
22035796c8dcSSimon Schubert void
_initialize_coffread(void)22045796c8dcSSimon Schubert _initialize_coffread (void)
22055796c8dcSSimon Schubert {
22065796c8dcSSimon Schubert   add_symtab_fns (&coff_sym_fns);
2207*ef5ccd6cSJohn Marino 
2208*ef5ccd6cSJohn Marino   coff_objfile_data_key = register_objfile_data_with_cleanup (NULL,
2209*ef5ccd6cSJohn Marino 							      coff_free_info);
22105796c8dcSSimon Schubert }
2211