Home | History | Annotate | Line # | Download | only in gdb
dbxread.c revision 1.1.1.3
      1 /* Read dbx symbol tables and convert to internal format, for GDB.
      2    Copyright (C) 1986-2015 Free Software Foundation, Inc.
      3 
      4    This file is part of GDB.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18 
     19 /* This module provides three functions: dbx_symfile_init,
     20    which initializes to read a symbol file; dbx_new_init, which
     21    discards existing cached information when all symbols are being
     22    discarded; and dbx_symfile_read, which reads a symbol table
     23    from a file.
     24 
     25    dbx_symfile_read only does the minimum work necessary for letting the
     26    user "name" things symbolically; it does not read the entire symtab.
     27    Instead, it reads the external and static symbols and puts them in partial
     28    symbol tables.  When more extensive information is requested of a
     29    file, the corresponding partial symbol table is mutated into a full
     30    fledged symbol table by going back and reading the symbols
     31    for real.  dbx_psymtab_to_symtab() is the function that does this */
     32 
     33 #include "defs.h"
     34 #if defined(__CYGNUSCLIB__)
     35 #include <sys/types.h>
     36 #include <fcntl.h>
     37 #endif
     38 
     39 #include "gdb_obstack.h"
     40 #include <sys/stat.h>
     41 #include "symtab.h"
     42 #include "breakpoint.h"
     43 #include "target.h"
     44 #include "gdbcore.h"		/* for bfd stuff */
     45 #include "libaout.h"		/* FIXME Secret internal BFD stuff for a.out */
     46 #include "filenames.h"
     47 #include "objfiles.h"
     48 #include "buildsym.h"
     49 #include "stabsread.h"
     50 #include "gdb-stabs.h"
     51 #include "demangle.h"
     52 #include "complaints.h"
     53 #include "cp-abi.h"
     54 #include "cp-support.h"
     55 #include "psympriv.h"
     56 #include "block.h"
     57 
     58 #include "aout/aout64.h"
     59 #include "aout/stab_gnu.h"	/* We always use GNU stabs, not
     60 				   native, now.  */
     61 
     62 
     64 /* Key for dbx-associated data.  */
     65 
     66 const struct objfile_data *dbx_objfile_data_key;
     67 
     68 /* We put a pointer to this structure in the read_symtab_private field
     69    of the psymtab.  */
     70 
     71 struct symloc
     72   {
     73     /* Offset within the file symbol table of first local symbol for this
     74        file.  */
     75 
     76     int ldsymoff;
     77 
     78     /* Length (in bytes) of the section of the symbol table devoted to
     79        this file's symbols (actually, the section bracketed may contain
     80        more than just this file's symbols).  If ldsymlen is 0, the only
     81        reason for this thing's existence is the dependency list.  Nothing
     82        else will happen when it is read in.  */
     83 
     84     int ldsymlen;
     85 
     86     /* The size of each symbol in the symbol file (in external form).  */
     87 
     88     int symbol_size;
     89 
     90     /* Further information needed to locate the symbols if they are in
     91        an ELF file.  */
     92 
     93     int symbol_offset;
     94     int string_offset;
     95     int file_string_offset;
     96   };
     97 
     98 #define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
     99 #define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
    100 #define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
    101 #define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
    102 #define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
    103 #define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
    104 #define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
    105 
    106 
    108 /* The objfile we are currently reading.  */
    109 
    110 static struct objfile *dbxread_objfile;
    111 
    112 /* Remember what we deduced to be the source language of this psymtab.  */
    113 
    114 static enum language psymtab_language = language_unknown;
    115 
    116 /* The BFD for this file -- implicit parameter to next_symbol_text.  */
    117 
    118 static bfd *symfile_bfd;
    119 
    120 /* The size of each symbol in the symbol file (in external form).
    121    This is set by dbx_symfile_read when building psymtabs, and by
    122    dbx_psymtab_to_symtab when building symtabs.  */
    123 
    124 static unsigned symbol_size;
    125 
    126 /* This is the offset of the symbol table in the executable file.  */
    127 
    128 static unsigned symbol_table_offset;
    129 
    130 /* This is the offset of the string table in the executable file.  */
    131 
    132 static unsigned string_table_offset;
    133 
    134 /* For elf+stab executables, the n_strx field is not a simple index
    135    into the string table.  Instead, each .o file has a base offset in
    136    the string table, and the associated symbols contain offsets from
    137    this base.  The following two variables contain the base offset for
    138    the current and next .o files.  */
    139 
    140 static unsigned int file_string_table_offset;
    141 static unsigned int next_file_string_table_offset;
    142 
    143 /* .o and NLM files contain unrelocated addresses which are based at
    144    0.  When non-zero, this flag disables some of the special cases for
    145    Solaris elf+stab text addresses at location 0.  */
    146 
    147 static int symfile_relocatable = 0;
    148 
    149 /* If this is nonzero, N_LBRAC, N_RBRAC, and N_SLINE entries are
    150    relative to the function start address.  */
    151 
    152 static int block_address_function_relative = 0;
    153 
    154 /* The lowest text address we have yet encountered.  This is needed
    156    because in an a.out file, there is no header field which tells us
    157    what address the program is actually going to be loaded at, so we
    158    need to make guesses based on the symbols (which *are* relocated to
    159    reflect the address it will be loaded at).  */
    160 
    161 static CORE_ADDR lowest_text_address;
    162 
    163 /* Non-zero if there is any line number info in the objfile.  Prevents
    164    end_psymtab from discarding an otherwise empty psymtab.  */
    165 
    166 static int has_line_numbers;
    167 
    168 /* Complaints about the symbols we have encountered.  */
    169 
    170 static void
    171 unknown_symtype_complaint (const char *arg1)
    172 {
    173   complaint (&symfile_complaints, _("unknown symbol type %s"), arg1);
    174 }
    175 
    176 static void
    177 lbrac_mismatch_complaint (int arg1)
    178 {
    179   complaint (&symfile_complaints,
    180 	     _("N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d"), arg1);
    181 }
    182 
    183 static void
    184 repeated_header_complaint (const char *arg1, int arg2)
    185 {
    186   complaint (&symfile_complaints,
    187 	     _("\"repeated\" header file %s not "
    188 	       "previously seen, at symtab pos %d"),
    189 	     arg1, arg2);
    190 }
    191 
    192 /* find_text_range --- find start and end of loadable code sections
    193 
    194    The find_text_range function finds the shortest address range that
    195    encloses all sections containing executable code, and stores it in
    196    objfile's text_addr and text_size members.
    197 
    198    dbx_symfile_read will use this to finish off the partial symbol
    199    table, in some cases.  */
    200 
    201 static void
    202 find_text_range (bfd * sym_bfd, struct objfile *objfile)
    203 {
    204   asection *sec;
    205   int found_any = 0;
    206   CORE_ADDR start = 0;
    207   CORE_ADDR end = 0;
    208 
    209   for (sec = sym_bfd->sections; sec; sec = sec->next)
    210     if (bfd_get_section_flags (sym_bfd, sec) & SEC_CODE)
    211       {
    212 	CORE_ADDR sec_start = bfd_section_vma (sym_bfd, sec);
    213 	CORE_ADDR sec_end = sec_start + bfd_section_size (sym_bfd, sec);
    214 
    215 	if (found_any)
    216 	  {
    217 	    if (sec_start < start)
    218 	      start = sec_start;
    219 	    if (sec_end > end)
    220 	      end = sec_end;
    221 	  }
    222 	else
    223 	  {
    224 	    start = sec_start;
    225 	    end = sec_end;
    226 	  }
    227 
    228 	found_any = 1;
    229       }
    230 
    231   if (!found_any)
    232     error (_("Can't find any code sections in symbol file"));
    233 
    234   DBX_TEXT_ADDR (objfile) = start;
    235   DBX_TEXT_SIZE (objfile) = end - start;
    236 }
    237 
    238 
    240 
    241 /* During initial symbol readin, we need to have a structure to keep
    242    track of which psymtabs have which bincls in them.  This structure
    243    is used during readin to setup the list of dependencies within each
    244    partial symbol table.  */
    245 
    246 struct header_file_location
    247 {
    248   char *name;			/* Name of header file */
    249   int instance;			/* See above */
    250   struct partial_symtab *pst;	/* Partial symtab that has the
    251 				   BINCL/EINCL defs for this file.  */
    252 };
    253 
    254 /* The actual list and controling variables.  */
    255 static struct header_file_location *bincl_list, *next_bincl;
    256 static int bincls_allocated;
    257 
    258 /* Local function prototypes.  */
    259 
    260 extern void _initialize_dbxread (void);
    261 
    262 static void read_ofile_symtab (struct objfile *, struct partial_symtab *);
    263 
    264 static void dbx_read_symtab (struct partial_symtab *self,
    265 			     struct objfile *objfile);
    266 
    267 static void dbx_psymtab_to_symtab_1 (struct objfile *, struct partial_symtab *);
    268 
    269 static void read_dbx_dynamic_symtab (struct objfile *objfile);
    270 
    271 static void read_dbx_symtab (struct objfile *);
    272 
    273 static void free_bincl_list (struct objfile *);
    274 
    275 static struct partial_symtab *find_corresponding_bincl_psymtab (char *, int);
    276 
    277 static void add_bincl_to_list (struct partial_symtab *, char *, int);
    278 
    279 static void init_bincl_list (int, struct objfile *);
    280 
    281 static char *dbx_next_symbol_text (struct objfile *);
    282 
    283 static void fill_symbuf (bfd *);
    284 
    285 static void dbx_symfile_init (struct objfile *);
    286 
    287 static void dbx_new_init (struct objfile *);
    288 
    289 static void dbx_symfile_read (struct objfile *, int);
    290 
    291 static void dbx_symfile_finish (struct objfile *);
    292 
    293 static void record_minimal_symbol (const char *, CORE_ADDR, int,
    294 				   struct objfile *);
    295 
    296 static void add_new_header_file (char *, int);
    297 
    298 static void add_old_header_file (char *, int);
    299 
    300 static void add_this_object_header_file (int);
    301 
    302 static struct partial_symtab *start_psymtab (struct objfile *, char *,
    303 					     CORE_ADDR, int,
    304 					     struct partial_symbol **,
    305 					     struct partial_symbol **);
    306 
    307 /* Free up old header file tables.  */
    308 
    309 void
    310 free_header_files (void)
    311 {
    312   if (this_object_header_files)
    313     {
    314       xfree (this_object_header_files);
    315       this_object_header_files = NULL;
    316     }
    317   n_allocated_this_object_header_files = 0;
    318 }
    319 
    320 /* Allocate new header file tables.  */
    321 
    322 void
    323 init_header_files (void)
    324 {
    325   n_allocated_this_object_header_files = 10;
    326   this_object_header_files = (int *) xmalloc (10 * sizeof (int));
    327 }
    328 
    329 /* Add header file number I for this object file
    330    at the next successive FILENUM.  */
    331 
    332 static void
    333 add_this_object_header_file (int i)
    334 {
    335   if (n_this_object_header_files == n_allocated_this_object_header_files)
    336     {
    337       n_allocated_this_object_header_files *= 2;
    338       this_object_header_files
    339 	= (int *) xrealloc ((char *) this_object_header_files,
    340 		       n_allocated_this_object_header_files * sizeof (int));
    341     }
    342 
    343   this_object_header_files[n_this_object_header_files++] = i;
    344 }
    345 
    346 /* Add to this file an "old" header file, one already seen in
    347    a previous object file.  NAME is the header file's name.
    348    INSTANCE is its instance code, to select among multiple
    349    symbol tables for the same header file.  */
    350 
    351 static void
    352 add_old_header_file (char *name, int instance)
    353 {
    354   struct header_file *p = HEADER_FILES (dbxread_objfile);
    355   int i;
    356 
    357   for (i = 0; i < N_HEADER_FILES (dbxread_objfile); i++)
    358     if (filename_cmp (p[i].name, name) == 0 && instance == p[i].instance)
    359       {
    360 	add_this_object_header_file (i);
    361 	return;
    362       }
    363   repeated_header_complaint (name, symnum);
    364 }
    365 
    366 /* Add to this file a "new" header file: definitions for its types follow.
    367    NAME is the header file's name.
    368    Most often this happens only once for each distinct header file,
    369    but not necessarily.  If it happens more than once, INSTANCE has
    370    a different value each time, and references to the header file
    371    use INSTANCE values to select among them.
    372 
    373    dbx output contains "begin" and "end" markers for each new header file,
    374    but at this level we just need to know which files there have been;
    375    so we record the file when its "begin" is seen and ignore the "end".  */
    376 
    377 static void
    378 add_new_header_file (char *name, int instance)
    379 {
    380   int i;
    381   struct header_file *hfile;
    382 
    383   /* Make sure there is room for one more header file.  */
    384 
    385   i = N_ALLOCATED_HEADER_FILES (dbxread_objfile);
    386 
    387   if (N_HEADER_FILES (dbxread_objfile) == i)
    388     {
    389       if (i == 0)
    390 	{
    391 	  N_ALLOCATED_HEADER_FILES (dbxread_objfile) = 10;
    392 	  HEADER_FILES (dbxread_objfile) = (struct header_file *)
    393 	    xmalloc (10 * sizeof (struct header_file));
    394 	}
    395       else
    396 	{
    397 	  i *= 2;
    398 	  N_ALLOCATED_HEADER_FILES (dbxread_objfile) = i;
    399 	  HEADER_FILES (dbxread_objfile) = (struct header_file *)
    400 	    xrealloc ((char *) HEADER_FILES (dbxread_objfile),
    401 		      (i * sizeof (struct header_file)));
    402 	}
    403     }
    404 
    405   /* Create an entry for this header file.  */
    406 
    407   i = N_HEADER_FILES (dbxread_objfile)++;
    408   hfile = HEADER_FILES (dbxread_objfile) + i;
    409   hfile->name = xstrdup (name);
    410   hfile->instance = instance;
    411   hfile->length = 10;
    412   hfile->vector
    413     = (struct type **) xmalloc (10 * sizeof (struct type *));
    414   memset (hfile->vector, 0, 10 * sizeof (struct type *));
    415 
    416   add_this_object_header_file (i);
    417 }
    418 
    419 #if 0
    420 static struct type **
    421 explicit_lookup_type (int real_filenum, int index)
    422 {
    423   struct header_file *f = &HEADER_FILES (dbxread_objfile)[real_filenum];
    424 
    425   if (index >= f->length)
    426     {
    427       f->length *= 2;
    428       f->vector = (struct type **)
    429 	xrealloc (f->vector, f->length * sizeof (struct type *));
    430       memset (&f->vector[f->length / 2],
    431 	      '\0', f->length * sizeof (struct type *) / 2);
    432     }
    433   return &f->vector[index];
    434 }
    435 #endif
    436 
    437 static void
    439 record_minimal_symbol (const char *name, CORE_ADDR address, int type,
    440 		       struct objfile *objfile)
    441 {
    442   enum minimal_symbol_type ms_type;
    443   int section;
    444 
    445   switch (type)
    446     {
    447     case N_TEXT | N_EXT:
    448       ms_type = mst_text;
    449       section = SECT_OFF_TEXT (objfile);
    450       break;
    451     case N_DATA | N_EXT:
    452       ms_type = mst_data;
    453       section = SECT_OFF_DATA (objfile);
    454       break;
    455     case N_BSS | N_EXT:
    456       ms_type = mst_bss;
    457       section = SECT_OFF_BSS (objfile);
    458       break;
    459     case N_ABS | N_EXT:
    460       ms_type = mst_abs;
    461       section = -1;
    462       break;
    463 #ifdef N_SETV
    464     case N_SETV | N_EXT:
    465       ms_type = mst_data;
    466       section = SECT_OFF_DATA (objfile);
    467       break;
    468     case N_SETV:
    469       /* I don't think this type actually exists; since a N_SETV is the result
    470          of going over many .o files, it doesn't make sense to have one
    471          file local.  */
    472       ms_type = mst_file_data;
    473       section = SECT_OFF_DATA (objfile);
    474       break;
    475 #endif
    476     case N_TEXT:
    477     case N_NBTEXT:
    478     case N_FN:
    479     case N_FN_SEQ:
    480       ms_type = mst_file_text;
    481       section = SECT_OFF_TEXT (objfile);
    482       break;
    483     case N_DATA:
    484       ms_type = mst_file_data;
    485 
    486       /* Check for __DYNAMIC, which is used by Sun shared libraries.
    487          Record it as global even if it's local, not global, so
    488          lookup_minimal_symbol can find it.  We don't check symbol_leading_char
    489          because for SunOS4 it always is '_'.  */
    490       if (name[8] == 'C' && strcmp ("__DYNAMIC", name) == 0)
    491 	ms_type = mst_data;
    492 
    493       /* Same with virtual function tables, both global and static.  */
    494       {
    495 	const char *tempstring = name;
    496 
    497 	if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
    498 	  ++tempstring;
    499 	if (is_vtable_name (tempstring))
    500 	  ms_type = mst_data;
    501       }
    502       section = SECT_OFF_DATA (objfile);
    503       break;
    504     case N_BSS:
    505       ms_type = mst_file_bss;
    506       section = SECT_OFF_BSS (objfile);
    507       break;
    508     default:
    509       ms_type = mst_unknown;
    510       section = -1;
    511       break;
    512     }
    513 
    514   if ((ms_type == mst_file_text || ms_type == mst_text)
    515       && address < lowest_text_address)
    516     lowest_text_address = address;
    517 
    518   prim_record_minimal_symbol_and_info
    519     (name, address, ms_type, section, objfile);
    520 }
    521 
    522 /* Scan and build partial symbols for a symbol file.
    524    We have been initialized by a call to dbx_symfile_init, which
    525    put all the relevant info into a "struct dbx_symfile_info",
    526    hung off the objfile structure.  */
    527 
    528 static void
    529 dbx_symfile_read (struct objfile *objfile, int symfile_flags)
    530 {
    531   bfd *sym_bfd;
    532   int val;
    533   struct cleanup *back_to;
    534 
    535   sym_bfd = objfile->obfd;
    536 
    537   /* .o and .nlm files are relocatables with text, data and bss segs based at
    538      0.  This flag disables special (Solaris stabs-in-elf only) fixups for
    539      symbols with a value of 0.  */
    540 
    541   symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
    542 
    543   /* This is true for Solaris (and all other systems which put stabs
    544      in sections, hopefully, since it would be silly to do things
    545      differently from Solaris), and false for SunOS4 and other a.out
    546      file formats.  */
    547   block_address_function_relative =
    548     ((startswith (bfd_get_target (sym_bfd), "elf"))
    549      || (startswith (bfd_get_target (sym_bfd), "som"))
    550      || (startswith (bfd_get_target (sym_bfd), "coff"))
    551      || (startswith (bfd_get_target (sym_bfd), "pe"))
    552      || (startswith (bfd_get_target (sym_bfd), "epoc-pe"))
    553      || (startswith (bfd_get_target (sym_bfd), "nlm")));
    554 
    555   val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
    556   if (val < 0)
    557     perror_with_name (objfile_name (objfile));
    558 
    559   /* Size the symbol table.  */
    560   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
    561     init_psymbol_list (objfile, DBX_SYMCOUNT (objfile));
    562 
    563   symbol_size = DBX_SYMBOL_SIZE (objfile);
    564   symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
    565 
    566   free_pending_blocks ();
    567   back_to = make_cleanup (really_free_pendings, 0);
    568 
    569   init_minimal_symbol_collection ();
    570   make_cleanup_discard_minimal_symbols ();
    571 
    572   /* Read stabs data from executable file and define symbols.  */
    573 
    574   read_dbx_symtab (objfile);
    575 
    576   /* Add the dynamic symbols.  */
    577 
    578   read_dbx_dynamic_symtab (objfile);
    579 
    580   /* Install any minimal symbols that have been collected as the current
    581      minimal symbols for this objfile.  */
    582 
    583   install_minimal_symbols (objfile);
    584 
    585   do_cleanups (back_to);
    586 }
    587 
    588 /* Initialize anything that needs initializing when a completely new
    589    symbol file is specified (not just adding some symbols from another
    590    file, e.g. a shared library).  */
    591 
    592 static void
    593 dbx_new_init (struct objfile *ignore)
    594 {
    595   stabsread_new_init ();
    596   buildsym_new_init ();
    597   init_header_files ();
    598 }
    599 
    600 
    601 /* dbx_symfile_init ()
    602    is the dbx-specific initialization routine for reading symbols.
    603    It is passed a struct objfile which contains, among other things,
    604    the BFD for the file whose symbols are being read, and a slot for a pointer
    605    to "private data" which we fill with goodies.
    606 
    607    We read the string table into malloc'd space and stash a pointer to it.
    608 
    609    Since BFD doesn't know how to read debug symbols in a format-independent
    610    way (and may never do so...), we have to do it ourselves.  We will never
    611    be called unless this is an a.out (or very similar) file.
    612    FIXME, there should be a cleaner peephole into the BFD environment here.  */
    613 
    614 #define DBX_STRINGTAB_SIZE_SIZE sizeof(long)	/* FIXME */
    615 
    616 static void
    617 dbx_symfile_init (struct objfile *objfile)
    618 {
    619   int val;
    620   bfd *sym_bfd = objfile->obfd;
    621   char *name = bfd_get_filename (sym_bfd);
    622   asection *text_sect;
    623   unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
    624   struct dbx_symfile_info *dbx;
    625 
    626   /* Allocate struct to keep track of the symfile.  */
    627   dbx = XCNEW (struct dbx_symfile_info);
    628   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
    629 
    630   DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
    631   DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
    632   DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
    633 
    634   /* FIXME POKING INSIDE BFD DATA STRUCTURES.  */
    635 #define	STRING_TABLE_OFFSET	(sym_bfd->origin + obj_str_filepos (sym_bfd))
    636 #define	SYMBOL_TABLE_OFFSET	(sym_bfd->origin + obj_sym_filepos (sym_bfd))
    637 
    638   /* FIXME POKING INSIDE BFD DATA STRUCTURES.  */
    639 
    640   text_sect = bfd_get_section_by_name (sym_bfd, ".text");
    641   if (!text_sect)
    642     error (_("Can't find .text section in symbol file"));
    643   DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
    644   DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
    645 
    646   DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
    647   DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
    648   DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
    649 
    650   /* Read the string table and stash it away in the objfile_obstack.
    651      When we blow away the objfile the string table goes away as well.
    652      Note that gdb used to use the results of attempting to malloc the
    653      string table, based on the size it read, as a form of sanity check
    654      for botched byte swapping, on the theory that a byte swapped string
    655      table size would be so totally bogus that the malloc would fail.  Now
    656      that we put in on the objfile_obstack, we can't do this since gdb gets
    657      a fatal error (out of virtual memory) if the size is bogus.  We can
    658      however at least check to see if the size is less than the size of
    659      the size field itself, or larger than the size of the entire file.
    660      Note that all valid string tables have a size greater than zero, since
    661      the bytes used to hold the size are included in the count.  */
    662 
    663   if (STRING_TABLE_OFFSET == 0)
    664     {
    665       /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
    666          will never be zero, even when there is no string table.  This
    667          would appear to be a bug in bfd.  */
    668       DBX_STRINGTAB_SIZE (objfile) = 0;
    669       DBX_STRINGTAB (objfile) = NULL;
    670     }
    671   else
    672     {
    673       val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
    674       if (val < 0)
    675 	perror_with_name (name);
    676 
    677       memset (size_temp, 0, sizeof (size_temp));
    678       val = bfd_bread (size_temp, sizeof (size_temp), sym_bfd);
    679       if (val < 0)
    680 	{
    681 	  perror_with_name (name);
    682 	}
    683       else if (val == 0)
    684 	{
    685 	  /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
    686 	     EOF if there is no string table, and attempting to read the size
    687 	     from EOF will read zero bytes.  */
    688 	  DBX_STRINGTAB_SIZE (objfile) = 0;
    689 	  DBX_STRINGTAB (objfile) = NULL;
    690 	}
    691       else
    692 	{
    693 	  /* Read some data that would appear to be the string table size.
    694 	     If there really is a string table, then it is probably the right
    695 	     size.  Byteswap if necessary and validate the size.  Note that
    696 	     the minimum is DBX_STRINGTAB_SIZE_SIZE.  If we just read some
    697 	     random data that happened to be at STRING_TABLE_OFFSET, because
    698 	     bfd can't tell us there is no string table, the sanity checks may
    699 	     or may not catch this.  */
    700 	  DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
    701 
    702 	  if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
    703 	      || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
    704 	    error (_("ridiculous string table size (%d bytes)."),
    705 		   DBX_STRINGTAB_SIZE (objfile));
    706 
    707 	  DBX_STRINGTAB (objfile) =
    708 	    (char *) obstack_alloc (&objfile->objfile_obstack,
    709 				    DBX_STRINGTAB_SIZE (objfile));
    710 	  OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
    711 
    712 	  /* Now read in the string table in one big gulp.  */
    713 
    714 	  val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
    715 	  if (val < 0)
    716 	    perror_with_name (name);
    717 	  val = bfd_bread (DBX_STRINGTAB (objfile),
    718 			   DBX_STRINGTAB_SIZE (objfile),
    719 			   sym_bfd);
    720 	  if (val != DBX_STRINGTAB_SIZE (objfile))
    721 	    perror_with_name (name);
    722 	}
    723     }
    724 }
    725 
    726 /* Perform any local cleanups required when we are done with a particular
    727    objfile.  I.E, we are in the process of discarding all symbol information
    728    for an objfile, freeing up all memory held for it, and unlinking the
    729    objfile struct from the global list of known objfiles.  */
    730 
    731 static void
    732 dbx_symfile_finish (struct objfile *objfile)
    733 {
    734   free_header_files ();
    735 }
    736 
    737 static void
    738 dbx_free_symfile_info (struct objfile *objfile, void *arg)
    739 {
    740   struct dbx_symfile_info *dbx = arg;
    741 
    742   if (dbx->header_files != NULL)
    743     {
    744       int i = dbx->n_header_files;
    745       struct header_file *hfiles = dbx->header_files;
    746 
    747       while (--i >= 0)
    748 	{
    749 	  xfree (hfiles[i].name);
    750 	  xfree (hfiles[i].vector);
    751 	}
    752       xfree (hfiles);
    753     }
    754 
    755   xfree (dbx);
    756 }
    757 
    758 
    759 
    761 /* Buffer for reading the symbol table entries.  */
    762 static struct external_nlist symbuf[4096];
    763 static int symbuf_idx;
    764 static int symbuf_end;
    765 
    766 /* Name of last function encountered.  Used in Solaris to approximate
    767    object file boundaries.  */
    768 static char *last_function_name;
    769 
    770 /* The address in memory of the string table of the object file we are
    771    reading (which might not be the "main" object file, but might be a
    772    shared library or some other dynamically loaded thing).  This is
    773    set by read_dbx_symtab when building psymtabs, and by
    774    read_ofile_symtab when building symtabs, and is used only by
    775    next_symbol_text.  FIXME: If that is true, we don't need it when
    776    building psymtabs, right?  */
    777 static char *stringtab_global;
    778 
    779 /* These variables are used to control fill_symbuf when the stabs
    780    symbols are not contiguous (as may be the case when a COFF file is
    781    linked using --split-by-reloc).  */
    782 static struct stab_section_list *symbuf_sections;
    783 static unsigned int symbuf_left;
    784 static unsigned int symbuf_read;
    785 
    786 /* This variable stores a global stabs buffer, if we read stabs into
    787    memory in one chunk in order to process relocations.  */
    788 static bfd_byte *stabs_data;
    789 
    790 /* Refill the symbol table input buffer
    791    and set the variables that control fetching entries from it.
    792    Reports an error if no data available.
    793    This function can read past the end of the symbol table
    794    (into the string table) but this does no harm.  */
    795 
    796 static void
    797 fill_symbuf (bfd *sym_bfd)
    798 {
    799   unsigned int count;
    800   int nbytes;
    801 
    802   if (stabs_data)
    803     {
    804       nbytes = sizeof (symbuf);
    805       if (nbytes > symbuf_left)
    806         nbytes = symbuf_left;
    807       memcpy (symbuf, stabs_data + symbuf_read, nbytes);
    808     }
    809   else if (symbuf_sections == NULL)
    810     {
    811       count = sizeof (symbuf);
    812       nbytes = bfd_bread (symbuf, count, sym_bfd);
    813     }
    814   else
    815     {
    816       if (symbuf_left <= 0)
    817 	{
    818 	  file_ptr filepos = symbuf_sections->section->filepos;
    819 
    820 	  if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
    821 	    perror_with_name (bfd_get_filename (sym_bfd));
    822 	  symbuf_left = bfd_section_size (sym_bfd, symbuf_sections->section);
    823 	  symbol_table_offset = filepos - symbuf_read;
    824 	  symbuf_sections = symbuf_sections->next;
    825 	}
    826 
    827       count = symbuf_left;
    828       if (count > sizeof (symbuf))
    829 	count = sizeof (symbuf);
    830       nbytes = bfd_bread (symbuf, count, sym_bfd);
    831     }
    832 
    833   if (nbytes < 0)
    834     perror_with_name (bfd_get_filename (sym_bfd));
    835   else if (nbytes == 0)
    836     error (_("Premature end of file reading symbol table"));
    837   symbuf_end = nbytes / symbol_size;
    838   symbuf_idx = 0;
    839   symbuf_left -= nbytes;
    840   symbuf_read += nbytes;
    841 }
    842 
    843 static void
    844 stabs_seek (int sym_offset)
    845 {
    846   if (stabs_data)
    847     {
    848       symbuf_read += sym_offset;
    849       symbuf_left -= sym_offset;
    850     }
    851   else
    852     bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
    853 }
    854 
    855 #define INTERNALIZE_SYMBOL(intern, extern, abfd)			\
    856   {									\
    857     (intern).n_strx = bfd_h_get_32 (abfd, (extern)->e_strx);		\
    858     (intern).n_type = bfd_h_get_8 (abfd, (extern)->e_type);		\
    859     (intern).n_other = 0;						\
    860     (intern).n_desc = bfd_h_get_16 (abfd, (extern)->e_desc);  		\
    861     if (bfd_get_sign_extend_vma (abfd))					\
    862       (intern).n_value = bfd_h_get_signed_32 (abfd, (extern)->e_value);	\
    863     else								\
    864       (intern).n_value = bfd_h_get_32 (abfd, (extern)->e_value);	\
    865   }
    866 
    867 /* Invariant: The symbol pointed to by symbuf_idx is the first one
    868    that hasn't been swapped.  Swap the symbol at the same time
    869    that symbuf_idx is incremented.  */
    870 
    871 /* dbx allows the text of a symbol name to be continued into the
    872    next symbol name!  When such a continuation is encountered
    873    (a \ at the end of the text of a name)
    874    call this function to get the continuation.  */
    875 
    876 static char *
    877 dbx_next_symbol_text (struct objfile *objfile)
    878 {
    879   struct internal_nlist nlist;
    880 
    881   if (symbuf_idx == symbuf_end)
    882     fill_symbuf (symfile_bfd);
    883 
    884   symnum++;
    885   INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], symfile_bfd);
    886   OBJSTAT (objfile, n_stabs++);
    887 
    888   symbuf_idx++;
    889 
    890   return nlist.n_strx + stringtab_global + file_string_table_offset;
    891 }
    892 
    893 /* Initialize the list of bincls to contain none and have some
    895    allocated.  */
    896 
    897 static void
    898 init_bincl_list (int number, struct objfile *objfile)
    899 {
    900   bincls_allocated = number;
    901   next_bincl = bincl_list = (struct header_file_location *)
    902     xmalloc (bincls_allocated * sizeof (struct header_file_location));
    903 }
    904 
    905 /* Add a bincl to the list.  */
    906 
    907 static void
    908 add_bincl_to_list (struct partial_symtab *pst, char *name, int instance)
    909 {
    910   if (next_bincl >= bincl_list + bincls_allocated)
    911     {
    912       int offset = next_bincl - bincl_list;
    913 
    914       bincls_allocated *= 2;
    915       bincl_list = (struct header_file_location *)
    916 	xrealloc ((char *) bincl_list,
    917 		  bincls_allocated * sizeof (struct header_file_location));
    918       next_bincl = bincl_list + offset;
    919     }
    920   next_bincl->pst = pst;
    921   next_bincl->instance = instance;
    922   next_bincl++->name = name;
    923 }
    924 
    925 /* Given a name, value pair, find the corresponding
    926    bincl in the list.  Return the partial symtab associated
    927    with that header_file_location.  */
    928 
    929 static struct partial_symtab *
    930 find_corresponding_bincl_psymtab (char *name, int instance)
    931 {
    932   struct header_file_location *bincl;
    933 
    934   for (bincl = bincl_list; bincl < next_bincl; bincl++)
    935     if (bincl->instance == instance
    936 	&& strcmp (name, bincl->name) == 0)
    937       return bincl->pst;
    938 
    939   repeated_header_complaint (name, symnum);
    940   return (struct partial_symtab *) 0;
    941 }
    942 
    943 /* Free the storage allocated for the bincl list.  */
    944 
    945 static void
    946 free_bincl_list (struct objfile *objfile)
    947 {
    948   xfree (bincl_list);
    949   bincls_allocated = 0;
    950 }
    951 
    952 static void
    953 do_free_bincl_list_cleanup (void *objfile)
    954 {
    955   free_bincl_list (objfile);
    956 }
    957 
    958 static struct cleanup *
    959 make_cleanup_free_bincl_list (struct objfile *objfile)
    960 {
    961   return make_cleanup (do_free_bincl_list_cleanup, objfile);
    962 }
    963 
    964 /* Set namestring based on nlist.  If the string table index is invalid,
    965    give a fake name, and print a single error message per symbol file read,
    966    rather than abort the symbol reading or flood the user with messages.  */
    967 
    968 static char *
    969 set_namestring (struct objfile *objfile, const struct internal_nlist *nlist)
    970 {
    971   char *namestring;
    972 
    973   if (nlist->n_strx + file_string_table_offset
    974       >= DBX_STRINGTAB_SIZE (objfile)
    975       || nlist->n_strx + file_string_table_offset < nlist->n_strx)
    976     {
    977       complaint (&symfile_complaints,
    978 		 _("bad string table offset in symbol %d"),
    979 		 symnum);
    980       namestring = "<bad string table offset>";
    981     }
    982   else
    983     namestring = (nlist->n_strx + file_string_table_offset
    984 		  + DBX_STRINGTAB (objfile));
    985   return namestring;
    986 }
    987 
    988 /* Scan a SunOs dynamic symbol table for symbols of interest and
    989    add them to the minimal symbol table.  */
    990 
    991 static void
    992 read_dbx_dynamic_symtab (struct objfile *objfile)
    993 {
    994   bfd *abfd = objfile->obfd;
    995   struct cleanup *back_to;
    996   int counter;
    997   long dynsym_size;
    998   long dynsym_count;
    999   asymbol **dynsyms;
   1000   asymbol **symptr;
   1001   arelent **relptr;
   1002   long dynrel_size;
   1003   long dynrel_count;
   1004   arelent **dynrels;
   1005   CORE_ADDR sym_value;
   1006   const char *name;
   1007 
   1008   /* Check that the symbol file has dynamic symbols that we know about.
   1009      bfd_arch_unknown can happen if we are reading a sun3 symbol file
   1010      on a sun4 host (and vice versa) and bfd is not configured
   1011      --with-target=all.  This would trigger an assertion in bfd/sunos.c,
   1012      so we ignore the dynamic symbols in this case.  */
   1013   if (bfd_get_flavour (abfd) != bfd_target_aout_flavour
   1014       || (bfd_get_file_flags (abfd) & DYNAMIC) == 0
   1015       || bfd_get_arch (abfd) == bfd_arch_unknown)
   1016     return;
   1017 
   1018   dynsym_size = bfd_get_dynamic_symtab_upper_bound (abfd);
   1019   if (dynsym_size < 0)
   1020     return;
   1021 
   1022   dynsyms = (asymbol **) xmalloc (dynsym_size);
   1023   back_to = make_cleanup (xfree, dynsyms);
   1024 
   1025   dynsym_count = bfd_canonicalize_dynamic_symtab (abfd, dynsyms);
   1026   if (dynsym_count < 0)
   1027     {
   1028       do_cleanups (back_to);
   1029       return;
   1030     }
   1031 
   1032   /* Enter dynamic symbols into the minimal symbol table
   1033      if this is a stripped executable.  */
   1034   if (bfd_get_symcount (abfd) <= 0)
   1035     {
   1036       symptr = dynsyms;
   1037       for (counter = 0; counter < dynsym_count; counter++, symptr++)
   1038 	{
   1039 	  asymbol *sym = *symptr;
   1040 	  asection *sec;
   1041 	  int type;
   1042 
   1043 	  sec = bfd_get_section (sym);
   1044 
   1045 	  /* BFD symbols are section relative.  */
   1046 	  sym_value = sym->value + sec->vma;
   1047 
   1048 	  if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
   1049 	    {
   1050 	      type = N_TEXT;
   1051 	    }
   1052 	  else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
   1053 	    {
   1054 	      type = N_DATA;
   1055 	    }
   1056 	  else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
   1057 	    {
   1058 	      type = N_BSS;
   1059 	    }
   1060 	  else
   1061 	    continue;
   1062 
   1063 	  if (sym->flags & BSF_GLOBAL)
   1064 	    type |= N_EXT;
   1065 
   1066 	  record_minimal_symbol (bfd_asymbol_name (sym), sym_value,
   1067 				 type, objfile);
   1068 	}
   1069     }
   1070 
   1071   /* Symbols from shared libraries have a dynamic relocation entry
   1072      that points to the associated slot in the procedure linkage table.
   1073      We make a mininal symbol table entry with type mst_solib_trampoline
   1074      at the address in the procedure linkage table.  */
   1075   dynrel_size = bfd_get_dynamic_reloc_upper_bound (abfd);
   1076   if (dynrel_size < 0)
   1077     {
   1078       do_cleanups (back_to);
   1079       return;
   1080     }
   1081 
   1082   dynrels = (arelent **) xmalloc (dynrel_size);
   1083   make_cleanup (xfree, dynrels);
   1084 
   1085   dynrel_count = bfd_canonicalize_dynamic_reloc (abfd, dynrels, dynsyms);
   1086   if (dynrel_count < 0)
   1087     {
   1088       do_cleanups (back_to);
   1089       return;
   1090     }
   1091 
   1092   for (counter = 0, relptr = dynrels;
   1093        counter < dynrel_count;
   1094        counter++, relptr++)
   1095     {
   1096       arelent *rel = *relptr;
   1097       CORE_ADDR address = rel->address;
   1098 
   1099       switch (bfd_get_arch (abfd))
   1100 	{
   1101 	case bfd_arch_sparc:
   1102 	  if (rel->howto->type != RELOC_JMP_SLOT)
   1103 	    continue;
   1104 	  break;
   1105 	case bfd_arch_m68k:
   1106 	  /* `16' is the type BFD produces for a jump table relocation.  */
   1107 	  if (rel->howto->type != 16)
   1108 	    continue;
   1109 
   1110 	  /* Adjust address in the jump table to point to
   1111 	     the start of the bsr instruction.  */
   1112 	  address -= 2;
   1113 	  break;
   1114 	default:
   1115 	  continue;
   1116 	}
   1117 
   1118       name = bfd_asymbol_name (*rel->sym_ptr_ptr);
   1119       prim_record_minimal_symbol (name, address, mst_solib_trampoline,
   1120 				  objfile);
   1121     }
   1122 
   1123   do_cleanups (back_to);
   1124 }
   1125 
   1126 static CORE_ADDR
   1127 find_stab_function_addr (char *namestring, const char *filename,
   1128 			 struct objfile *objfile)
   1129 {
   1130   struct bound_minimal_symbol msym;
   1131   char *p;
   1132   int n;
   1133 
   1134   p = strchr (namestring, ':');
   1135   if (p == NULL)
   1136     p = namestring;
   1137   n = p - namestring;
   1138   p = alloca (n + 2);
   1139   strncpy (p, namestring, n);
   1140   p[n] = 0;
   1141 
   1142   msym = lookup_minimal_symbol (p, filename, objfile);
   1143   if (msym.minsym == NULL)
   1144     {
   1145       /* Sun Fortran appends an underscore to the minimal symbol name,
   1146          try again with an appended underscore if the minimal symbol
   1147          was not found.  */
   1148       p[n] = '_';
   1149       p[n + 1] = 0;
   1150       msym = lookup_minimal_symbol (p, filename, objfile);
   1151     }
   1152 
   1153   if (msym.minsym == NULL && filename != NULL)
   1154     {
   1155       /* Try again without the filename.  */
   1156       p[n] = 0;
   1157       msym = lookup_minimal_symbol (p, NULL, objfile);
   1158     }
   1159   if (msym.minsym == NULL && filename != NULL)
   1160     {
   1161       /* And try again for Sun Fortran, but without the filename.  */
   1162       p[n] = '_';
   1163       p[n + 1] = 0;
   1164       msym = lookup_minimal_symbol (p, NULL, objfile);
   1165     }
   1166 
   1167   return msym.minsym == NULL ? 0 : BMSYMBOL_VALUE_ADDRESS (msym);
   1168 }
   1169 
   1170 static void
   1171 function_outside_compilation_unit_complaint (const char *arg1)
   1172 {
   1173   complaint (&symfile_complaints,
   1174 	     _("function `%s' appears to be defined "
   1175 	       "outside of all compilation units"),
   1176 	     arg1);
   1177 }
   1178 
   1179 /* Setup partial_symtab's describing each source file for which
   1180    debugging information is available.  */
   1181 
   1182 static void
   1183 read_dbx_symtab (struct objfile *objfile)
   1184 {
   1185   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   1186   struct external_nlist *bufp = 0;	/* =0 avoids gcc -Wall glitch.  */
   1187   struct internal_nlist nlist;
   1188   CORE_ADDR text_addr;
   1189   int text_size;
   1190   char *sym_name;
   1191   int sym_len;
   1192 
   1193   char *namestring;
   1194   int nsl;
   1195   int past_first_source_file = 0;
   1196   CORE_ADDR last_function_start = 0;
   1197   struct cleanup *back_to;
   1198   bfd *abfd;
   1199   int textlow_not_set;
   1200   int data_sect_index;
   1201 
   1202   /* Current partial symtab.  */
   1203   struct partial_symtab *pst;
   1204 
   1205   /* List of current psymtab's include files.  */
   1206   const char **psymtab_include_list;
   1207   int includes_allocated;
   1208   int includes_used;
   1209 
   1210   /* Index within current psymtab dependency list.  */
   1211   struct partial_symtab **dependency_list;
   1212   int dependencies_used, dependencies_allocated;
   1213 
   1214   text_addr = DBX_TEXT_ADDR (objfile);
   1215   text_size = DBX_TEXT_SIZE (objfile);
   1216 
   1217   /* FIXME.  We probably want to change stringtab_global rather than add this
   1218      while processing every symbol entry.  FIXME.  */
   1219   file_string_table_offset = 0;
   1220   next_file_string_table_offset = 0;
   1221 
   1222   stringtab_global = DBX_STRINGTAB (objfile);
   1223 
   1224   pst = (struct partial_symtab *) 0;
   1225 
   1226   includes_allocated = 30;
   1227   includes_used = 0;
   1228   psymtab_include_list = (const char **) alloca (includes_allocated *
   1229 						 sizeof (const char *));
   1230 
   1231   dependencies_allocated = 30;
   1232   dependencies_used = 0;
   1233   dependency_list =
   1234     (struct partial_symtab **) alloca (dependencies_allocated *
   1235 				       sizeof (struct partial_symtab *));
   1236 
   1237   /* Init bincl list */
   1238   init_bincl_list (20, objfile);
   1239   back_to = make_cleanup_free_bincl_list (objfile);
   1240 
   1241   set_last_source_file (NULL);
   1242 
   1243   lowest_text_address = (CORE_ADDR) -1;
   1244 
   1245   symfile_bfd = objfile->obfd;	/* For next_text_symbol.  */
   1246   abfd = objfile->obfd;
   1247   symbuf_end = symbuf_idx = 0;
   1248   next_symbol_text_func = dbx_next_symbol_text;
   1249   textlow_not_set = 1;
   1250   has_line_numbers = 0;
   1251 
   1252   /* FIXME: jimb/2003-09-12: We don't apply the right section's offset
   1253      to global and static variables.  The stab for a global or static
   1254      variable doesn't give us any indication of which section it's in,
   1255      so we can't tell immediately which offset in
   1256      objfile->section_offsets we should apply to the variable's
   1257      address.
   1258 
   1259      We could certainly find out which section contains the variable
   1260      by looking up the variable's unrelocated address with
   1261      find_pc_section, but that would be expensive; this is the
   1262      function that constructs the partial symbol tables by examining
   1263      every symbol in the entire executable, and it's
   1264      performance-critical.  So that expense would not be welcome.  I'm
   1265      not sure what to do about this at the moment.
   1266 
   1267      What we have done for years is to simply assume that the .data
   1268      section's offset is appropriate for all global and static
   1269      variables.  Recently, this was expanded to fall back to the .bss
   1270      section's offset if there is no .data section, and then to the
   1271      .rodata section's offset.  */
   1272   data_sect_index = objfile->sect_index_data;
   1273   if (data_sect_index == -1)
   1274     data_sect_index = SECT_OFF_BSS (objfile);
   1275   if (data_sect_index == -1)
   1276     data_sect_index = SECT_OFF_RODATA (objfile);
   1277 
   1278   /* If data_sect_index is still -1, that's okay.  It's perfectly fine
   1279      for the file to have no .data, no .bss, and no .text at all, if
   1280      it also has no global or static variables.  If it does, we will
   1281      get an internal error from an ANOFFSET macro below when we try to
   1282      use data_sect_index.  */
   1283 
   1284   for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
   1285     {
   1286       /* Get the symbol for this run and pull out some info.  */
   1287       QUIT;			/* Allow this to be interruptable.  */
   1288       if (symbuf_idx == symbuf_end)
   1289 	fill_symbuf (abfd);
   1290       bufp = &symbuf[symbuf_idx++];
   1291 
   1292       /*
   1293        * Special case to speed up readin.
   1294        */
   1295       if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
   1296 	{
   1297 	  has_line_numbers = 1;
   1298 	  continue;
   1299 	}
   1300 
   1301       INTERNALIZE_SYMBOL (nlist, bufp, abfd);
   1302       OBJSTAT (objfile, n_stabs++);
   1303 
   1304       /* Ok.  There is a lot of code duplicated in the rest of this
   1305          switch statement (for efficiency reasons).  Since I don't
   1306          like duplicating code, I will do my penance here, and
   1307          describe the code which is duplicated:
   1308 
   1309          *) The assignment to namestring.
   1310          *) The call to strchr.
   1311          *) The addition of a partial symbol the two partial
   1312          symbol lists.  This last is a large section of code, so
   1313          I've imbedded it in the following macro.  */
   1314 
   1315       switch (nlist.n_type)
   1316 	{
   1317 	  /*
   1318 	   * Standard, external, non-debugger, symbols
   1319 	   */
   1320 
   1321 	case N_TEXT | N_EXT:
   1322 	case N_NBTEXT | N_EXT:
   1323 	  goto record_it;
   1324 
   1325 	case N_DATA | N_EXT:
   1326 	case N_NBDATA | N_EXT:
   1327 	  goto record_it;
   1328 
   1329 	case N_BSS:
   1330 	case N_BSS | N_EXT:
   1331 	case N_NBBSS | N_EXT:
   1332 	case N_SETV | N_EXT:		/* FIXME, is this in BSS? */
   1333 	  goto record_it;
   1334 
   1335 	case N_ABS | N_EXT:
   1336 	  record_it:
   1337 	  namestring = set_namestring (objfile, &nlist);
   1338 
   1339 	  record_minimal_symbol (namestring, nlist.n_value,
   1340 				 nlist.n_type, objfile);	/* Always */
   1341 	  continue;
   1342 
   1343 	  /* Standard, local, non-debugger, symbols.  */
   1344 
   1345 	case N_NBTEXT:
   1346 
   1347 	  /* We need to be able to deal with both N_FN or N_TEXT,
   1348 	     because we have no way of knowing whether the sys-supplied ld
   1349 	     or GNU ld was used to make the executable.  Sequents throw
   1350 	     in another wrinkle -- they renumbered N_FN.  */
   1351 
   1352 	case N_FN:
   1353 	case N_FN_SEQ:
   1354 	case N_TEXT:
   1355 	  namestring = set_namestring (objfile, &nlist);
   1356 
   1357 	  if ((namestring[0] == '-' && namestring[1] == 'l')
   1358 	      || (namestring[(nsl = strlen (namestring)) - 1] == 'o'
   1359 		  && namestring[nsl - 2] == '.'))
   1360 	    {
   1361 	      nlist.n_value += ANOFFSET (objfile->section_offsets,
   1362 					 SECT_OFF_TEXT (objfile));
   1363 
   1364 	      if (past_first_source_file && pst
   1365 		  /* The gould NP1 uses low values for .o and -l symbols
   1366 		     which are not the address.  */
   1367 		  && nlist.n_value >= pst->textlow)
   1368 		{
   1369 		  end_psymtab (objfile, pst, psymtab_include_list,
   1370 			       includes_used, symnum * symbol_size,
   1371 			       nlist.n_value > pst->texthigh
   1372 			       ? nlist.n_value : pst->texthigh,
   1373 			       dependency_list, dependencies_used,
   1374 			       textlow_not_set);
   1375 		  pst = (struct partial_symtab *) 0;
   1376 		  includes_used = 0;
   1377 		  dependencies_used = 0;
   1378 		  has_line_numbers = 0;
   1379 		}
   1380 	      else
   1381 		past_first_source_file = 1;
   1382 	    }
   1383 	  else
   1384 	    goto record_it;
   1385 	  continue;
   1386 
   1387 	case N_DATA:
   1388 	  goto record_it;
   1389 
   1390 	case N_UNDF | N_EXT:
   1391 	  /* The case (nlist.n_value != 0) is a "Fortran COMMON" symbol.
   1392 	     We used to rely on the target to tell us whether it knows
   1393 	     where the symbol has been relocated to, but none of the
   1394 	     target implementations actually provided that operation.
   1395 	     So we just ignore the symbol, the same way we would do if
   1396 	     we had a target-side symbol lookup which returned no match.
   1397 
   1398 	     All other symbols (with nlist.n_value == 0), are really
   1399 	     undefined, and so we ignore them too.  */
   1400 	  continue;
   1401 
   1402 	case N_UNDF:
   1403 	  if (processing_acc_compilation && nlist.n_strx == 1)
   1404 	    {
   1405 	      /* Deal with relative offsets in the string table
   1406 		 used in ELF+STAB under Solaris.  If we want to use the
   1407 		 n_strx field, which contains the name of the file,
   1408 		 we must adjust file_string_table_offset *before* calling
   1409 		 set_namestring().  */
   1410 	      past_first_source_file = 1;
   1411 	      file_string_table_offset = next_file_string_table_offset;
   1412 	      next_file_string_table_offset =
   1413 		file_string_table_offset + nlist.n_value;
   1414 	      if (next_file_string_table_offset < file_string_table_offset)
   1415 		error (_("string table offset backs up at %d"), symnum);
   1416 	      /* FIXME -- replace error() with complaint.  */
   1417 	      continue;
   1418 	    }
   1419 	  continue;
   1420 
   1421 	  /* Lots of symbol types we can just ignore.  */
   1422 
   1423 	case N_ABS:
   1424 	case N_NBDATA:
   1425 	case N_NBBSS:
   1426 	  continue;
   1427 
   1428 	  /* Keep going . . .  */
   1429 
   1430 	  /*
   1431 	   * Special symbol types for GNU
   1432 	   */
   1433 	case N_INDR:
   1434 	case N_INDR | N_EXT:
   1435 	case N_SETA:
   1436 	case N_SETA | N_EXT:
   1437 	case N_SETT:
   1438 	case N_SETT | N_EXT:
   1439 	case N_SETD:
   1440 	case N_SETD | N_EXT:
   1441 	case N_SETB:
   1442 	case N_SETB | N_EXT:
   1443 	case N_SETV:
   1444 	  continue;
   1445 
   1446 	  /*
   1447 	   * Debugger symbols
   1448 	   */
   1449 
   1450 	case N_SO:
   1451 	  {
   1452 	    CORE_ADDR valu;
   1453 	    static int prev_so_symnum = -10;
   1454 	    static int first_so_symnum;
   1455 	    const char *p;
   1456 	    static char *dirname_nso;
   1457 	    int prev_textlow_not_set;
   1458 
   1459 	    valu = nlist.n_value + ANOFFSET (objfile->section_offsets,
   1460 					     SECT_OFF_TEXT (objfile));
   1461 
   1462 	    prev_textlow_not_set = textlow_not_set;
   1463 
   1464 	    /* A zero value is probably an indication for the SunPRO 3.0
   1465 	       compiler.  end_psymtab explicitly tests for zero, so
   1466 	       don't relocate it.  */
   1467 
   1468 	    if (nlist.n_value == 0
   1469 		&& gdbarch_sofun_address_maybe_missing (gdbarch))
   1470 	      {
   1471 		textlow_not_set = 1;
   1472 		valu = 0;
   1473 	      }
   1474 	    else
   1475 	      textlow_not_set = 0;
   1476 
   1477 	    past_first_source_file = 1;
   1478 
   1479 	    if (prev_so_symnum != symnum - 1)
   1480 	      {			/* Here if prev stab wasn't N_SO.  */
   1481 		first_so_symnum = symnum;
   1482 
   1483 		if (pst)
   1484 		  {
   1485 		    end_psymtab (objfile, pst, psymtab_include_list,
   1486 				 includes_used, symnum * symbol_size,
   1487 				 valu > pst->texthigh ? valu : pst->texthigh,
   1488 				 dependency_list, dependencies_used,
   1489 				 prev_textlow_not_set);
   1490 		    pst = (struct partial_symtab *) 0;
   1491 		    includes_used = 0;
   1492 		    dependencies_used = 0;
   1493 		    has_line_numbers = 0;
   1494 		  }
   1495 	      }
   1496 
   1497 	    prev_so_symnum = symnum;
   1498 
   1499 	    /* End the current partial symtab and start a new one.  */
   1500 
   1501 	    namestring = set_namestring (objfile, &nlist);
   1502 
   1503 	    /* Null name means end of .o file.  Don't start a new one.  */
   1504 	    if (*namestring == '\000')
   1505 	      continue;
   1506 
   1507 	    /* Some compilers (including gcc) emit a pair of initial N_SOs.
   1508 	       The first one is a directory name; the second the file name.
   1509 	       If pst exists, is empty, and has a filename ending in '/',
   1510 	       we assume the previous N_SO was a directory name.  */
   1511 
   1512 	    p = lbasename (namestring);
   1513 	    if (p != namestring && *p == '\000')
   1514 	      {
   1515 		/* Save the directory name SOs locally, then save it into
   1516 		   the psymtab when it's created below.  */
   1517 	        dirname_nso = namestring;
   1518 	        continue;
   1519 	      }
   1520 
   1521 	    /* Some other compilers (C++ ones in particular) emit useless
   1522 	       SOs for non-existant .c files.  We ignore all subsequent SOs
   1523 	       that immediately follow the first.  */
   1524 
   1525 	    if (!pst)
   1526 	      {
   1527 		pst = start_psymtab (objfile,
   1528 				     namestring, valu,
   1529 				     first_so_symnum * symbol_size,
   1530 				     objfile->global_psymbols.next,
   1531 				     objfile->static_psymbols.next);
   1532 		pst->dirname = dirname_nso;
   1533 		dirname_nso = NULL;
   1534 	      }
   1535 	    continue;
   1536 	  }
   1537 
   1538 	case N_BINCL:
   1539 	  {
   1540 	    enum language tmp_language;
   1541 
   1542 	    /* Add this bincl to the bincl_list for future EXCLs.  No
   1543 	       need to save the string; it'll be around until
   1544 	       read_dbx_symtab function returns.  */
   1545 
   1546 	    namestring = set_namestring (objfile, &nlist);
   1547 	    tmp_language = deduce_language_from_filename (namestring);
   1548 
   1549 	    /* Only change the psymtab's language if we've learned
   1550 	       something useful (eg. tmp_language is not language_unknown).
   1551 	       In addition, to match what start_subfile does, never change
   1552 	       from C++ to C.  */
   1553 	    if (tmp_language != language_unknown
   1554 		&& (tmp_language != language_c
   1555 		    || psymtab_language != language_cplus))
   1556 	      psymtab_language = tmp_language;
   1557 
   1558 	    if (pst == NULL)
   1559 	      {
   1560 		/* FIXME: we should not get here without a PST to work on.
   1561 		   Attempt to recover.  */
   1562 		complaint (&symfile_complaints,
   1563 			   _("N_BINCL %s not in entries for "
   1564 			     "any file, at symtab pos %d"),
   1565 			   namestring, symnum);
   1566 		continue;
   1567 	      }
   1568 	    add_bincl_to_list (pst, namestring, nlist.n_value);
   1569 
   1570 	    /* Mark down an include file in the current psymtab.  */
   1571 
   1572 	    goto record_include_file;
   1573 	  }
   1574 
   1575 	case N_SOL:
   1576 	  {
   1577 	    enum language tmp_language;
   1578 
   1579 	    /* Mark down an include file in the current psymtab.  */
   1580 	    namestring = set_namestring (objfile, &nlist);
   1581 	    tmp_language = deduce_language_from_filename (namestring);
   1582 
   1583 	    /* Only change the psymtab's language if we've learned
   1584 	       something useful (eg. tmp_language is not language_unknown).
   1585 	       In addition, to match what start_subfile does, never change
   1586 	       from C++ to C.  */
   1587 	    if (tmp_language != language_unknown
   1588 		&& (tmp_language != language_c
   1589 		    || psymtab_language != language_cplus))
   1590 	      psymtab_language = tmp_language;
   1591 
   1592 	    /* In C++, one may expect the same filename to come round many
   1593 	       times, when code is coming alternately from the main file
   1594 	       and from inline functions in other files.  So I check to see
   1595 	       if this is a file we've seen before -- either the main
   1596 	       source file, or a previously included file.
   1597 
   1598 	       This seems to be a lot of time to be spending on N_SOL, but
   1599 	       things like "break c-exp.y:435" need to work (I
   1600 	       suppose the psymtab_include_list could be hashed or put
   1601 	       in a binary tree, if profiling shows this is a major hog).  */
   1602 	    if (pst && filename_cmp (namestring, pst->filename) == 0)
   1603 	      continue;
   1604 	    {
   1605 	      int i;
   1606 
   1607 	      for (i = 0; i < includes_used; i++)
   1608 		if (filename_cmp (namestring, psymtab_include_list[i]) == 0)
   1609 		  {
   1610 		    i = -1;
   1611 		    break;
   1612 		  }
   1613 	      if (i == -1)
   1614 		continue;
   1615 	    }
   1616 
   1617 	  record_include_file:
   1618 
   1619 	    psymtab_include_list[includes_used++] = namestring;
   1620 	    if (includes_used >= includes_allocated)
   1621 	      {
   1622 		const char **orig = psymtab_include_list;
   1623 
   1624 		psymtab_include_list = (const char **)
   1625 		  alloca ((includes_allocated *= 2) * sizeof (const char *));
   1626 		memcpy (psymtab_include_list, orig,
   1627 			includes_used * sizeof (const char *));
   1628 	      }
   1629 	    continue;
   1630 	  }
   1631 	case N_LSYM:		/* Typedef or automatic variable.  */
   1632 	case N_STSYM:		/* Data seg var -- static.  */
   1633 	case N_LCSYM:		/* BSS      "  */
   1634 	case N_ROSYM:		/* Read-only data seg var -- static.  */
   1635 	case N_NBSTS:		/* Gould nobase.  */
   1636 	case N_NBLCS:		/* symbols.  */
   1637 	case N_FUN:
   1638 	case N_GSYM:		/* Global (extern) variable; can be
   1639 				   data or bss (sigh FIXME).  */
   1640 
   1641 	  /* Following may probably be ignored; I'll leave them here
   1642 	     for now (until I do Pascal and Modula 2 extensions).  */
   1643 
   1644 	case N_PC:		/* I may or may not need this; I
   1645 				   suspect not.  */
   1646 	case N_M2C:		/* I suspect that I can ignore this here.  */
   1647 	case N_SCOPE:		/* Same.   */
   1648 	{
   1649 	  char *p;
   1650 
   1651 	  namestring = set_namestring (objfile, &nlist);
   1652 
   1653 	  /* See if this is an end of function stab.  */
   1654 	  if (pst && nlist.n_type == N_FUN && *namestring == '\000')
   1655 	    {
   1656 	      CORE_ADDR valu;
   1657 
   1658 	      /* It's value is the size (in bytes) of the function for
   1659 		 function relative stabs, or the address of the function's
   1660 		 end for old style stabs.  */
   1661 	      valu = nlist.n_value + last_function_start;
   1662 	      if (pst->texthigh == 0 || valu > pst->texthigh)
   1663 		pst->texthigh = valu;
   1664 	      break;
   1665 	    }
   1666 
   1667 	  p = (char *) strchr (namestring, ':');
   1668 	  if (!p)
   1669 	    continue;		/* Not a debugging symbol.   */
   1670 
   1671  	  sym_len = 0;
   1672 	  sym_name = NULL;	/* pacify "gcc -Werror" */
   1673  	  if (psymtab_language == language_cplus)
   1674  	    {
   1675  	      char *new_name, *name = xmalloc (p - namestring + 1);
   1676  	      memcpy (name, namestring, p - namestring);
   1677 
   1678  	      name[p - namestring] = '\0';
   1679  	      new_name = cp_canonicalize_string (name);
   1680  	      if (new_name != NULL)
   1681  		{
   1682  		  sym_len = strlen (new_name);
   1683  		  sym_name = obstack_copy0 (&objfile->objfile_obstack,
   1684 					    new_name, sym_len);
   1685  		  xfree (new_name);
   1686  		}
   1687               xfree (name);
   1688  	    }
   1689 
   1690  	  if (sym_len == 0)
   1691  	    {
   1692  	      sym_name = namestring;
   1693  	      sym_len = p - namestring;
   1694  	    }
   1695 
   1696 	  /* Main processing section for debugging symbols which
   1697 	     the initial read through the symbol tables needs to worry
   1698 	     about.  If we reach this point, the symbol which we are
   1699 	     considering is definitely one we are interested in.
   1700 	     p must also contain the (valid) index into the namestring
   1701 	     which indicates the debugging type symbol.  */
   1702 
   1703 	  switch (p[1])
   1704 	    {
   1705 	    case 'S':
   1706 	      nlist.n_value += ANOFFSET (objfile->section_offsets,
   1707 					 data_sect_index);
   1708 
   1709 	      if (gdbarch_static_transform_name_p (gdbarch))
   1710 		gdbarch_static_transform_name (gdbarch, namestring);
   1711 
   1712 	      add_psymbol_to_list (sym_name, sym_len, 1,
   1713 				   VAR_DOMAIN, LOC_STATIC,
   1714 				   &objfile->static_psymbols,
   1715 				   0, nlist.n_value,
   1716 				   psymtab_language, objfile);
   1717 	      continue;
   1718 
   1719 	    case 'G':
   1720 	      nlist.n_value += ANOFFSET (objfile->section_offsets,
   1721 					 data_sect_index);
   1722 	      /* The addresses in these entries are reported to be
   1723 		 wrong.  See the code that reads 'G's for symtabs.  */
   1724 	      add_psymbol_to_list (sym_name, sym_len, 1,
   1725 				   VAR_DOMAIN, LOC_STATIC,
   1726 				   &objfile->global_psymbols,
   1727 				   0, nlist.n_value,
   1728 				   psymtab_language, objfile);
   1729 	      continue;
   1730 
   1731 	    case 'T':
   1732 	      /* When a 'T' entry is defining an anonymous enum, it
   1733 		 may have a name which is the empty string, or a
   1734 		 single space.  Since they're not really defining a
   1735 		 symbol, those shouldn't go in the partial symbol
   1736 		 table.  We do pick up the elements of such enums at
   1737 		 'check_enum:', below.  */
   1738 	      if (p >= namestring + 2
   1739 		  || (p == namestring + 1
   1740 		      && namestring[0] != ' '))
   1741 		{
   1742 		  add_psymbol_to_list (sym_name, sym_len, 1,
   1743 				       STRUCT_DOMAIN, LOC_TYPEDEF,
   1744 				       &objfile->static_psymbols,
   1745 				       nlist.n_value, 0,
   1746 				       psymtab_language, objfile);
   1747 		  if (p[2] == 't')
   1748 		    {
   1749 		      /* Also a typedef with the same name.  */
   1750 		      add_psymbol_to_list (sym_name, sym_len, 1,
   1751 					   VAR_DOMAIN, LOC_TYPEDEF,
   1752 					   &objfile->static_psymbols,
   1753 					   nlist.n_value, 0,
   1754 					   psymtab_language, objfile);
   1755 		      p += 1;
   1756 		    }
   1757 		}
   1758 	      goto check_enum;
   1759 
   1760 	    case 't':
   1761 	      if (p != namestring)	/* a name is there, not just :T...  */
   1762 		{
   1763 		  add_psymbol_to_list (sym_name, sym_len, 1,
   1764 				       VAR_DOMAIN, LOC_TYPEDEF,
   1765 				       &objfile->static_psymbols,
   1766 				       nlist.n_value, 0,
   1767 				       psymtab_language, objfile);
   1768 		}
   1769 	    check_enum:
   1770 	      /* If this is an enumerated type, we need to
   1771 		 add all the enum constants to the partial symbol
   1772 		 table.  This does not cover enums without names, e.g.
   1773 		 "enum {a, b} c;" in C, but fortunately those are
   1774 		 rare.  There is no way for GDB to find those from the
   1775 		 enum type without spending too much time on it.  Thus
   1776 		 to solve this problem, the compiler needs to put out the
   1777 		 enum in a nameless type.  GCC2 does this.  */
   1778 
   1779 	      /* We are looking for something of the form
   1780 		 <name> ":" ("t" | "T") [<number> "="] "e"
   1781 		 {<constant> ":" <value> ","} ";".  */
   1782 
   1783 	      /* Skip over the colon and the 't' or 'T'.  */
   1784 	      p += 2;
   1785 	      /* This type may be given a number.  Also, numbers can come
   1786 		 in pairs like (0,26).  Skip over it.  */
   1787 	      while ((*p >= '0' && *p <= '9')
   1788 		     || *p == '(' || *p == ',' || *p == ')'
   1789 		     || *p == '=')
   1790 		p++;
   1791 
   1792 	      if (*p++ == 'e')
   1793 		{
   1794 		  /* The aix4 compiler emits extra crud before the members.  */
   1795 		  if (*p == '-')
   1796 		    {
   1797 		      /* Skip over the type (?).  */
   1798 		      while (*p != ':')
   1799 			p++;
   1800 
   1801 		      /* Skip over the colon.  */
   1802 		      p++;
   1803 		    }
   1804 
   1805 		  /* We have found an enumerated type.  */
   1806 		  /* According to comments in read_enum_type
   1807 		     a comma could end it instead of a semicolon.
   1808 		     I don't know where that happens.
   1809 		     Accept either.  */
   1810 		  while (*p && *p != ';' && *p != ',')
   1811 		    {
   1812 		      char *q;
   1813 
   1814 		      /* Check for and handle cretinous dbx symbol name
   1815 			 continuation!  */
   1816 		      if (*p == '\\' || (*p == '?' && p[1] == '\0'))
   1817 			p = next_symbol_text (objfile);
   1818 
   1819 		      /* Point to the character after the name
   1820 			 of the enum constant.  */
   1821 		      for (q = p; *q && *q != ':'; q++)
   1822 			;
   1823 		      /* Note that the value doesn't matter for
   1824 			 enum constants in psymtabs, just in symtabs.  */
   1825 		      add_psymbol_to_list (p, q - p, 1,
   1826 					   VAR_DOMAIN, LOC_CONST,
   1827 					   &objfile->static_psymbols, 0,
   1828 					   0, psymtab_language, objfile);
   1829 		      /* Point past the name.  */
   1830 		      p = q;
   1831 		      /* Skip over the value.  */
   1832 		      while (*p && *p != ',')
   1833 			p++;
   1834 		      /* Advance past the comma.  */
   1835 		      if (*p)
   1836 			p++;
   1837 		    }
   1838 		}
   1839 	      continue;
   1840 
   1841 	    case 'c':
   1842 	      /* Constant, e.g. from "const" in Pascal.  */
   1843 	      add_psymbol_to_list (sym_name, sym_len, 1,
   1844 				   VAR_DOMAIN, LOC_CONST,
   1845 				   &objfile->static_psymbols, nlist.n_value,
   1846 				   0, psymtab_language, objfile);
   1847 	      continue;
   1848 
   1849 	    case 'f':
   1850 	      if (! pst)
   1851 		{
   1852 		  int name_len = p - namestring;
   1853 		  char *name = xmalloc (name_len + 1);
   1854 
   1855 		  memcpy (name, namestring, name_len);
   1856 		  name[name_len] = '\0';
   1857 		  function_outside_compilation_unit_complaint (name);
   1858 		  xfree (name);
   1859 		}
   1860 	      nlist.n_value += ANOFFSET (objfile->section_offsets,
   1861 					 SECT_OFF_TEXT (objfile));
   1862 	      /* Kludges for ELF/STABS with Sun ACC.  */
   1863 	      last_function_name = namestring;
   1864 	      /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
   1865 		 value for the bottom of the text seg in those cases.  */
   1866 	      if (nlist.n_value == ANOFFSET (objfile->section_offsets,
   1867 					     SECT_OFF_TEXT (objfile))
   1868 		  && gdbarch_sofun_address_maybe_missing (gdbarch))
   1869 		{
   1870 		  CORE_ADDR minsym_valu =
   1871 		    find_stab_function_addr (namestring,
   1872 					     pst ? pst->filename : NULL,
   1873 					     objfile);
   1874 
   1875 		  /* find_stab_function_addr will return 0 if the minimal
   1876 		     symbol wasn't found.  (Unfortunately, this might also
   1877 		     be a valid address.)  Anyway, if it *does* return 0,
   1878 		     it is likely that the value was set correctly to begin
   1879 		     with...  */
   1880 		  if (minsym_valu != 0)
   1881 		    nlist.n_value = minsym_valu;
   1882 		}
   1883 	      if (pst && textlow_not_set
   1884 		  && gdbarch_sofun_address_maybe_missing (gdbarch))
   1885 		{
   1886 		  pst->textlow = nlist.n_value;
   1887 		  textlow_not_set = 0;
   1888 		}
   1889 	      /* End kludge.  */
   1890 
   1891 	      /* Keep track of the start of the last function so we
   1892 		 can handle end of function symbols.  */
   1893 	      last_function_start = nlist.n_value;
   1894 
   1895 	      /* In reordered executables this function may lie outside
   1896 		 the bounds created by N_SO symbols.  If that's the case
   1897 		 use the address of this function as the low bound for
   1898 		 the partial symbol table.  */
   1899 	      if (pst
   1900 		  && (textlow_not_set
   1901 		      || (nlist.n_value < pst->textlow
   1902 			  && (nlist.n_value
   1903 			      != ANOFFSET (objfile->section_offsets,
   1904 					   SECT_OFF_TEXT (objfile))))))
   1905 		{
   1906 		  pst->textlow = nlist.n_value;
   1907 		  textlow_not_set = 0;
   1908 		}
   1909 	      add_psymbol_to_list (sym_name, sym_len, 1,
   1910 				   VAR_DOMAIN, LOC_BLOCK,
   1911 				   &objfile->static_psymbols,
   1912 				   0, nlist.n_value,
   1913 				   psymtab_language, objfile);
   1914 	      continue;
   1915 
   1916 	      /* Global functions were ignored here, but now they
   1917 		 are put into the global psymtab like one would expect.
   1918 		 They're also in the minimal symbol table.  */
   1919 	    case 'F':
   1920 	      if (! pst)
   1921 		{
   1922 		  int name_len = p - namestring;
   1923 		  char *name = xmalloc (name_len + 1);
   1924 
   1925 		  memcpy (name, namestring, name_len);
   1926 		  name[name_len] = '\0';
   1927 		  function_outside_compilation_unit_complaint (name);
   1928 		  xfree (name);
   1929 		}
   1930 	      nlist.n_value += ANOFFSET (objfile->section_offsets,
   1931 					 SECT_OFF_TEXT (objfile));
   1932 	      /* Kludges for ELF/STABS with Sun ACC.  */
   1933 	      last_function_name = namestring;
   1934 	      /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
   1935 		 value for the bottom of the text seg in those cases.  */
   1936 	      if (nlist.n_value == ANOFFSET (objfile->section_offsets,
   1937 					     SECT_OFF_TEXT (objfile))
   1938 		  && gdbarch_sofun_address_maybe_missing (gdbarch))
   1939 		{
   1940 		  CORE_ADDR minsym_valu =
   1941 		    find_stab_function_addr (namestring,
   1942 					     pst ? pst->filename : NULL,
   1943 					     objfile);
   1944 
   1945 		  /* find_stab_function_addr will return 0 if the minimal
   1946 		     symbol wasn't found.  (Unfortunately, this might also
   1947 		     be a valid address.)  Anyway, if it *does* return 0,
   1948 		     it is likely that the value was set correctly to begin
   1949 		     with...  */
   1950 		  if (minsym_valu != 0)
   1951 		    nlist.n_value = minsym_valu;
   1952 		}
   1953 	      if (pst && textlow_not_set
   1954 		  && gdbarch_sofun_address_maybe_missing (gdbarch))
   1955 		{
   1956 		  pst->textlow = nlist.n_value;
   1957 		  textlow_not_set = 0;
   1958 		}
   1959 	      /* End kludge.  */
   1960 
   1961 	      /* Keep track of the start of the last function so we
   1962 		 can handle end of function symbols.  */
   1963 	      last_function_start = nlist.n_value;
   1964 
   1965 	      /* In reordered executables this function may lie outside
   1966 		 the bounds created by N_SO symbols.  If that's the case
   1967 		 use the address of this function as the low bound for
   1968 		 the partial symbol table.  */
   1969 	      if (pst
   1970 		  && (textlow_not_set
   1971 		      || (nlist.n_value < pst->textlow
   1972 			  && (nlist.n_value
   1973 			      != ANOFFSET (objfile->section_offsets,
   1974 					   SECT_OFF_TEXT (objfile))))))
   1975 		{
   1976 		  pst->textlow = nlist.n_value;
   1977 		  textlow_not_set = 0;
   1978 		}
   1979 	      add_psymbol_to_list (sym_name, sym_len, 1,
   1980 				   VAR_DOMAIN, LOC_BLOCK,
   1981 				   &objfile->global_psymbols,
   1982 				   0, nlist.n_value,
   1983 				   psymtab_language, objfile);
   1984 	      continue;
   1985 
   1986 	      /* Two things show up here (hopefully); static symbols of
   1987 		 local scope (static used inside braces) or extensions
   1988 		 of structure symbols.  We can ignore both.  */
   1989 	    case 'V':
   1990 	    case '(':
   1991 	    case '0':
   1992 	    case '1':
   1993 	    case '2':
   1994 	    case '3':
   1995 	    case '4':
   1996 	    case '5':
   1997 	    case '6':
   1998 	    case '7':
   1999 	    case '8':
   2000 	    case '9':
   2001 	    case '-':
   2002 	    case '#':	/* For symbol identification (used in live ranges).  */
   2003 	      continue;
   2004 
   2005 	    case ':':
   2006 	      /* It is a C++ nested symbol.  We don't need to record it
   2007 		 (I don't think); if we try to look up foo::bar::baz,
   2008 		 then symbols for the symtab containing foo should get
   2009 		 read in, I think.  */
   2010 	      /* Someone says sun cc puts out symbols like
   2011 		 /foo/baz/maclib::/usr/local/bin/maclib,
   2012 		 which would get here with a symbol type of ':'.  */
   2013 	      continue;
   2014 
   2015 	    default:
   2016 	      /* Unexpected symbol descriptor.  The second and subsequent stabs
   2017 		 of a continued stab can show up here.  The question is
   2018 		 whether they ever can mimic a normal stab--it would be
   2019 		 nice if not, since we certainly don't want to spend the
   2020 		 time searching to the end of every string looking for
   2021 		 a backslash.  */
   2022 
   2023 	      complaint (&symfile_complaints,
   2024 			 _("unknown symbol descriptor `%c'"),
   2025 			 p[1]);
   2026 
   2027 	      /* Ignore it; perhaps it is an extension that we don't
   2028 		 know about.  */
   2029 	      continue;
   2030 	    }
   2031 	}
   2032 
   2033 	case N_EXCL:
   2034 
   2035 	  namestring = set_namestring (objfile, &nlist);
   2036 
   2037 	  /* Find the corresponding bincl and mark that psymtab on the
   2038 	     psymtab dependency list.  */
   2039 	  {
   2040 	    struct partial_symtab *needed_pst =
   2041 	      find_corresponding_bincl_psymtab (namestring, nlist.n_value);
   2042 
   2043 	    /* If this include file was defined earlier in this file,
   2044 	       leave it alone.  */
   2045 	    if (needed_pst == pst)
   2046 	      continue;
   2047 
   2048 	    if (needed_pst)
   2049 	      {
   2050 		int i;
   2051 		int found = 0;
   2052 
   2053 		for (i = 0; i < dependencies_used; i++)
   2054 		  if (dependency_list[i] == needed_pst)
   2055 		    {
   2056 		      found = 1;
   2057 		      break;
   2058 		    }
   2059 
   2060 		/* If it's already in the list, skip the rest.  */
   2061 		if (found)
   2062 		  continue;
   2063 
   2064 		dependency_list[dependencies_used++] = needed_pst;
   2065 		if (dependencies_used >= dependencies_allocated)
   2066 		  {
   2067 		    struct partial_symtab **orig = dependency_list;
   2068 
   2069 		    dependency_list =
   2070 		      (struct partial_symtab **)
   2071 		      alloca ((dependencies_allocated *= 2)
   2072 			      * sizeof (struct partial_symtab *));
   2073 		    memcpy (dependency_list, orig,
   2074 			    (dependencies_used
   2075 			     * sizeof (struct partial_symtab *)));
   2076 #ifdef DEBUG_INFO
   2077 		    fprintf_unfiltered (gdb_stderr,
   2078 					"Had to reallocate "
   2079 					"dependency list.\n");
   2080 		    fprintf_unfiltered (gdb_stderr,
   2081 					"New dependencies allocated: %d\n",
   2082 					dependencies_allocated);
   2083 #endif
   2084 		  }
   2085 	      }
   2086 	  }
   2087 	  continue;
   2088 
   2089 	case N_ENDM:
   2090 	  /* Solaris 2 end of module, finish current partial symbol table.
   2091 	     end_psymtab will set pst->texthigh to the proper value, which
   2092 	     is necessary if a module compiled without debugging info
   2093 	     follows this module.  */
   2094 	  if (pst && gdbarch_sofun_address_maybe_missing (gdbarch))
   2095 	    {
   2096 	      end_psymtab (objfile, pst, psymtab_include_list, includes_used,
   2097 			   symnum * symbol_size,
   2098 			   (CORE_ADDR) 0, dependency_list,
   2099 			   dependencies_used, textlow_not_set);
   2100 	      pst = (struct partial_symtab *) 0;
   2101 	      includes_used = 0;
   2102 	      dependencies_used = 0;
   2103 	      has_line_numbers = 0;
   2104 	    }
   2105 	  continue;
   2106 
   2107 	case N_RBRAC:
   2108 #ifdef HANDLE_RBRAC
   2109 	  HANDLE_RBRAC (nlist.n_value);
   2110 	  continue;
   2111 #endif
   2112 	case N_EINCL:
   2113 	case N_DSLINE:
   2114 	case N_BSLINE:
   2115 	case N_SSYM:		/* Claim: Structure or union element.
   2116 				   Hopefully, I can ignore this.  */
   2117 	case N_ENTRY:		/* Alternate entry point; can ignore.  */
   2118 	case N_MAIN:		/* Can definitely ignore this.   */
   2119 	case N_CATCH:		/* These are GNU C++ extensions */
   2120 	case N_EHDECL:		/* that can safely be ignored here.  */
   2121 	case N_LENG:
   2122 	case N_BCOMM:
   2123 	case N_ECOMM:
   2124 	case N_ECOML:
   2125 	case N_FNAME:
   2126 	case N_SLINE:
   2127 	case N_RSYM:
   2128 	case N_PSYM:
   2129 	case N_BNSYM:
   2130 	case N_ENSYM:
   2131 	case N_LBRAC:
   2132 	case N_NSYMS:		/* Ultrix 4.0: symbol count */
   2133 	case N_DEFD:		/* GNU Modula-2 */
   2134 	case N_ALIAS:		/* SunPro F77: alias name, ignore for now.  */
   2135 
   2136 	case N_OBJ:		/* Useless types from Solaris.  */
   2137 	case N_OPT:
   2138 	case N_PATCH:
   2139 	  /* These symbols aren't interesting; don't worry about them.  */
   2140 	  continue;
   2141 
   2142 	default:
   2143 	  /* If we haven't found it yet, ignore it.  It's probably some
   2144 	     new type we don't know about yet.  */
   2145 	  unknown_symtype_complaint (hex_string (nlist.n_type));
   2146 	  continue;
   2147 	}
   2148     }
   2149 
   2150   /* If there's stuff to be cleaned up, clean it up.  */
   2151   if (pst)
   2152     {
   2153       /* Don't set pst->texthigh lower than it already is.  */
   2154       CORE_ADDR text_end =
   2155 	(lowest_text_address == (CORE_ADDR) -1
   2156 	 ? (text_addr + ANOFFSET (objfile->section_offsets,
   2157 				  SECT_OFF_TEXT (objfile)))
   2158 	 : lowest_text_address)
   2159 	+ text_size;
   2160 
   2161       end_psymtab (objfile, pst, psymtab_include_list, includes_used,
   2162 		   symnum * symbol_size,
   2163 		   text_end > pst->texthigh ? text_end : pst->texthigh,
   2164 		   dependency_list, dependencies_used, textlow_not_set);
   2165     }
   2166 
   2167   do_cleanups (back_to);
   2168 }
   2169 
   2170 /* Allocate and partially fill a partial symtab.  It will be
   2171    completely filled at the end of the symbol list.
   2172 
   2173    SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
   2174    is the address relative to which its symbols are (incremental) or 0
   2175    (normal).  */
   2176 
   2177 static struct partial_symtab *
   2178 start_psymtab (struct objfile *objfile, char *filename, CORE_ADDR textlow,
   2179 	       int ldsymoff, struct partial_symbol **global_syms,
   2180 	       struct partial_symbol **static_syms)
   2181 {
   2182   struct partial_symtab *result =
   2183     start_psymtab_common (objfile, filename, textlow,
   2184 			  global_syms, static_syms);
   2185 
   2186   result->read_symtab_private = obstack_alloc (&objfile->objfile_obstack,
   2187 					       sizeof (struct symloc));
   2188   LDSYMOFF (result) = ldsymoff;
   2189   result->read_symtab = dbx_read_symtab;
   2190   SYMBOL_SIZE (result) = symbol_size;
   2191   SYMBOL_OFFSET (result) = symbol_table_offset;
   2192   STRING_OFFSET (result) = string_table_offset;
   2193   FILE_STRING_OFFSET (result) = file_string_table_offset;
   2194 
   2195   /* Deduce the source language from the filename for this psymtab.  */
   2196   psymtab_language = deduce_language_from_filename (filename);
   2197 
   2198   return result;
   2199 }
   2200 
   2201 /* Close off the current usage of PST.
   2202    Returns PST or NULL if the partial symtab was empty and thrown away.
   2203 
   2204    FIXME:  List variables and peculiarities of same.  */
   2205 
   2206 struct partial_symtab *
   2207 end_psymtab (struct objfile *objfile, struct partial_symtab *pst,
   2208 	     const char **include_list, int num_includes,
   2209 	     int capping_symbol_offset, CORE_ADDR capping_text,
   2210 	     struct partial_symtab **dependency_list, int number_dependencies,
   2211 	     int textlow_not_set)
   2212 {
   2213   int i;
   2214   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   2215 
   2216   if (capping_symbol_offset != -1)
   2217     LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
   2218   pst->texthigh = capping_text;
   2219 
   2220   /* Under Solaris, the N_SO symbols always have a value of 0,
   2221      instead of the usual address of the .o file.  Therefore,
   2222      we have to do some tricks to fill in texthigh and textlow.
   2223      The first trick is: if we see a static
   2224      or global function, and the textlow for the current pst
   2225      is not set (ie: textlow_not_set), then we use that function's
   2226      address for the textlow of the pst.  */
   2227 
   2228   /* Now, to fill in texthigh, we remember the last function seen
   2229      in the .o file.  Also, there's a hack in
   2230      bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
   2231      to here via the misc_info field.  Therefore, we can fill in
   2232      a reliable texthigh by taking the address plus size of the
   2233      last function in the file.  */
   2234 
   2235   if (pst->texthigh == 0 && last_function_name
   2236       && gdbarch_sofun_address_maybe_missing (gdbarch))
   2237     {
   2238       char *p;
   2239       int n;
   2240       struct bound_minimal_symbol minsym;
   2241 
   2242       p = strchr (last_function_name, ':');
   2243       if (p == NULL)
   2244 	p = last_function_name;
   2245       n = p - last_function_name;
   2246       p = alloca (n + 2);
   2247       strncpy (p, last_function_name, n);
   2248       p[n] = 0;
   2249 
   2250       minsym = lookup_minimal_symbol (p, pst->filename, objfile);
   2251       if (minsym.minsym == NULL)
   2252 	{
   2253 	  /* Sun Fortran appends an underscore to the minimal symbol name,
   2254 	     try again with an appended underscore if the minimal symbol
   2255 	     was not found.  */
   2256 	  p[n] = '_';
   2257 	  p[n + 1] = 0;
   2258 	  minsym = lookup_minimal_symbol (p, pst->filename, objfile);
   2259 	}
   2260 
   2261       if (minsym.minsym)
   2262 	pst->texthigh = (BMSYMBOL_VALUE_ADDRESS (minsym)
   2263 			 + MSYMBOL_SIZE (minsym.minsym));
   2264 
   2265       last_function_name = NULL;
   2266     }
   2267 
   2268   if (!gdbarch_sofun_address_maybe_missing (gdbarch))
   2269     ;
   2270   /* This test will be true if the last .o file is only data.  */
   2271   else if (textlow_not_set)
   2272     pst->textlow = pst->texthigh;
   2273   else
   2274     {
   2275       struct partial_symtab *p1;
   2276 
   2277       /* If we know our own starting text address, then walk through all other
   2278          psymtabs for this objfile, and if any didn't know their ending text
   2279          address, set it to our starting address.  Take care to not set our
   2280          own ending address to our starting address, nor to set addresses on
   2281          `dependency' files that have both textlow and texthigh zero.  */
   2282 
   2283       ALL_OBJFILE_PSYMTABS (objfile, p1)
   2284       {
   2285 	if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst)
   2286 	  {
   2287 	    p1->texthigh = pst->textlow;
   2288 	    /* If this file has only data, then make textlow match
   2289 	       texthigh.  */
   2290 	    if (p1->textlow == 0)
   2291 	      p1->textlow = p1->texthigh;
   2292 	  }
   2293       }
   2294     }
   2295 
   2296   /* End of kludge for patching Solaris textlow and texthigh.  */
   2297 
   2298   pst->n_global_syms =
   2299     objfile->global_psymbols.next - (objfile->global_psymbols.list
   2300 				     + pst->globals_offset);
   2301   pst->n_static_syms =
   2302     objfile->static_psymbols.next - (objfile->static_psymbols.list
   2303 				     + pst->statics_offset);
   2304 
   2305   pst->number_of_dependencies = number_dependencies;
   2306   if (number_dependencies)
   2307     {
   2308       pst->dependencies = (struct partial_symtab **)
   2309 	obstack_alloc (&objfile->objfile_obstack,
   2310 		       number_dependencies * sizeof (struct partial_symtab *));
   2311       memcpy (pst->dependencies, dependency_list,
   2312 	      number_dependencies * sizeof (struct partial_symtab *));
   2313     }
   2314   else
   2315     pst->dependencies = 0;
   2316 
   2317   for (i = 0; i < num_includes; i++)
   2318     {
   2319       struct partial_symtab *subpst =
   2320 	allocate_psymtab (include_list[i], objfile);
   2321 
   2322       subpst->read_symtab_private =
   2323 	obstack_alloc (&objfile->objfile_obstack, sizeof (struct symloc));
   2324       LDSYMOFF (subpst) =
   2325 	LDSYMLEN (subpst) =
   2326 	subpst->textlow =
   2327 	subpst->texthigh = 0;
   2328 
   2329       /* We could save slight bits of space by only making one of these,
   2330          shared by the entire set of include files.  FIXME-someday.  */
   2331       subpst->dependencies = (struct partial_symtab **)
   2332 	obstack_alloc (&objfile->objfile_obstack,
   2333 		       sizeof (struct partial_symtab *));
   2334       subpst->dependencies[0] = pst;
   2335       subpst->number_of_dependencies = 1;
   2336 
   2337       subpst->globals_offset =
   2338 	subpst->n_global_syms =
   2339 	subpst->statics_offset =
   2340 	subpst->n_static_syms = 0;
   2341 
   2342       subpst->readin = 0;
   2343       subpst->compunit_symtab = 0;
   2344       subpst->read_symtab = pst->read_symtab;
   2345     }
   2346 
   2347   sort_pst_symbols (objfile, pst);
   2348 
   2349   if (num_includes == 0
   2350       && number_dependencies == 0
   2351       && pst->n_global_syms == 0
   2352       && pst->n_static_syms == 0
   2353       && has_line_numbers == 0)
   2354     {
   2355       /* Throw away this psymtab, it's empty.  We can't deallocate it, since
   2356          it is on the obstack, but we can forget to chain it on the list.  */
   2357       /* Empty psymtabs happen as a result of header files which don't have
   2358          any symbols in them.  There can be a lot of them.  But this check
   2359          is wrong, in that a psymtab with N_SLINE entries but nothing else
   2360          is not empty, but we don't realize that.  Fixing that without slowing
   2361          things down might be tricky.  */
   2362 
   2363       discard_psymtab (objfile, pst);
   2364 
   2365       /* Indicate that psymtab was thrown away.  */
   2366       pst = (struct partial_symtab *) NULL;
   2367     }
   2368   return pst;
   2369 }
   2370 
   2371 static void
   2373 dbx_psymtab_to_symtab_1 (struct objfile *objfile, struct partial_symtab *pst)
   2374 {
   2375   struct cleanup *old_chain;
   2376   int i;
   2377 
   2378   if (pst->readin)
   2379     {
   2380       fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  "
   2381 			  "Shouldn't happen.\n",
   2382 			  pst->filename);
   2383       return;
   2384     }
   2385 
   2386   /* Read in all partial symtabs on which this one is dependent.  */
   2387   for (i = 0; i < pst->number_of_dependencies; i++)
   2388     if (!pst->dependencies[i]->readin)
   2389       {
   2390 	/* Inform about additional files that need to be read in.  */
   2391 	if (info_verbose)
   2392 	  {
   2393 	    fputs_filtered (" ", gdb_stdout);
   2394 	    wrap_here ("");
   2395 	    fputs_filtered ("and ", gdb_stdout);
   2396 	    wrap_here ("");
   2397 	    printf_filtered ("%s...", pst->dependencies[i]->filename);
   2398 	    wrap_here ("");	/* Flush output.  */
   2399 	    gdb_flush (gdb_stdout);
   2400 	  }
   2401 	dbx_psymtab_to_symtab_1 (objfile, pst->dependencies[i]);
   2402       }
   2403 
   2404   if (LDSYMLEN (pst))		/* Otherwise it's a dummy.  */
   2405     {
   2406       /* Init stuff necessary for reading in symbols */
   2407       stabsread_init ();
   2408       buildsym_init ();
   2409       old_chain = make_cleanup (really_free_pendings, 0);
   2410       file_string_table_offset = FILE_STRING_OFFSET (pst);
   2411       symbol_size = SYMBOL_SIZE (pst);
   2412 
   2413       /* Read in this file's symbols.  */
   2414       bfd_seek (objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
   2415       read_ofile_symtab (objfile, pst);
   2416 
   2417       do_cleanups (old_chain);
   2418     }
   2419 
   2420   pst->readin = 1;
   2421 }
   2422 
   2423 /* Read in all of the symbols for a given psymtab for real.
   2424    Be verbose about it if the user wants that.  SELF is not NULL.  */
   2425 
   2426 static void
   2427 dbx_read_symtab (struct partial_symtab *self, struct objfile *objfile)
   2428 {
   2429   bfd *sym_bfd;
   2430 
   2431   if (self->readin)
   2432     {
   2433       fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  "
   2434 			  "Shouldn't happen.\n",
   2435 			  self->filename);
   2436       return;
   2437     }
   2438 
   2439   if (LDSYMLEN (self) || self->number_of_dependencies)
   2440     {
   2441       struct cleanup *back_to;
   2442 
   2443       /* Print the message now, before reading the string table,
   2444          to avoid disconcerting pauses.  */
   2445       if (info_verbose)
   2446 	{
   2447 	  printf_filtered ("Reading in symbols for %s...", self->filename);
   2448 	  gdb_flush (gdb_stdout);
   2449 	}
   2450 
   2451       sym_bfd = objfile->obfd;
   2452 
   2453       next_symbol_text_func = dbx_next_symbol_text;
   2454 
   2455       back_to = make_cleanup (null_cleanup, NULL);
   2456 
   2457       if (DBX_STAB_SECTION (objfile))
   2458 	{
   2459 	  stabs_data
   2460 	    = symfile_relocate_debug_section (objfile,
   2461 					      DBX_STAB_SECTION (objfile),
   2462 					      NULL);
   2463 
   2464 	  if (stabs_data)
   2465 	    make_cleanup (free_current_contents, (void *) &stabs_data);
   2466 	}
   2467 
   2468       dbx_psymtab_to_symtab_1 (objfile, self);
   2469 
   2470       do_cleanups (back_to);
   2471 
   2472       /* Match with global symbols.  This only needs to be done once,
   2473          after all of the symtabs and dependencies have been read in.   */
   2474       scan_file_globals (objfile);
   2475 
   2476       /* Finish up the debug error message.  */
   2477       if (info_verbose)
   2478 	printf_filtered ("done.\n");
   2479     }
   2480 }
   2481 
   2482 /* Read in a defined section of a specific object file's symbols.  */
   2483 
   2484 static void
   2485 read_ofile_symtab (struct objfile *objfile, struct partial_symtab *pst)
   2486 {
   2487   char *namestring;
   2488   struct external_nlist *bufp;
   2489   struct internal_nlist nlist;
   2490   unsigned char type;
   2491   unsigned max_symnum;
   2492   bfd *abfd;
   2493   int sym_offset;		/* Offset to start of symbols to read */
   2494   int sym_size;			/* Size of symbols to read */
   2495   CORE_ADDR text_offset;	/* Start of text segment for symbols */
   2496   int text_size;		/* Size of text segment for symbols */
   2497   struct section_offsets *section_offsets;
   2498 
   2499   sym_offset = LDSYMOFF (pst);
   2500   sym_size = LDSYMLEN (pst);
   2501   text_offset = pst->textlow;
   2502   text_size = pst->texthigh - pst->textlow;
   2503   section_offsets = objfile->section_offsets;
   2504 
   2505   dbxread_objfile = objfile;
   2506 
   2507   stringtab_global = DBX_STRINGTAB (objfile);
   2508   set_last_source_file (NULL);
   2509 
   2510   abfd = objfile->obfd;
   2511   symfile_bfd = objfile->obfd;	/* Implicit param to next_text_symbol.  */
   2512   symbuf_end = symbuf_idx = 0;
   2513   symbuf_read = 0;
   2514   symbuf_left = sym_offset + sym_size;
   2515 
   2516   /* It is necessary to actually read one symbol *before* the start
   2517      of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
   2518      occurs before the N_SO symbol.
   2519 
   2520      Detecting this in read_dbx_symtab
   2521      would slow down initial readin, so we look for it here instead.  */
   2522   if (!processing_acc_compilation && sym_offset >= (int) symbol_size)
   2523     {
   2524       stabs_seek (sym_offset - symbol_size);
   2525       fill_symbuf (abfd);
   2526       bufp = &symbuf[symbuf_idx++];
   2527       INTERNALIZE_SYMBOL (nlist, bufp, abfd);
   2528       OBJSTAT (objfile, n_stabs++);
   2529 
   2530       namestring = set_namestring (objfile, &nlist);
   2531 
   2532       processing_gcc_compilation = 0;
   2533       if (nlist.n_type == N_TEXT)
   2534 	{
   2535 	  const char *tempstring = namestring;
   2536 
   2537 	  if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
   2538 	    processing_gcc_compilation = 1;
   2539 	  else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
   2540 	    processing_gcc_compilation = 2;
   2541 	  if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
   2542 	    ++tempstring;
   2543 	  if (startswith (tempstring, "__gnu_compiled"))
   2544 	    processing_gcc_compilation = 2;
   2545 	}
   2546     }
   2547   else
   2548     {
   2549       /* The N_SO starting this symtab is the first symbol, so we
   2550          better not check the symbol before it.  I'm not this can
   2551          happen, but it doesn't hurt to check for it.  */
   2552       stabs_seek (sym_offset);
   2553       processing_gcc_compilation = 0;
   2554     }
   2555 
   2556   if (symbuf_idx == symbuf_end)
   2557     fill_symbuf (abfd);
   2558   bufp = &symbuf[symbuf_idx];
   2559   if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
   2560     error (_("First symbol in segment of executable not a source symbol"));
   2561 
   2562   max_symnum = sym_size / symbol_size;
   2563 
   2564   for (symnum = 0;
   2565        symnum < max_symnum;
   2566        symnum++)
   2567     {
   2568       QUIT;			/* Allow this to be interruptable.  */
   2569       if (symbuf_idx == symbuf_end)
   2570 	fill_symbuf (abfd);
   2571       bufp = &symbuf[symbuf_idx++];
   2572       INTERNALIZE_SYMBOL (nlist, bufp, abfd);
   2573       OBJSTAT (objfile, n_stabs++);
   2574 
   2575       type = bfd_h_get_8 (abfd, bufp->e_type);
   2576 
   2577       namestring = set_namestring (objfile, &nlist);
   2578 
   2579       if (type & N_STAB)
   2580 	{
   2581 	  if (sizeof (nlist.n_value) > 4
   2582 	      /* We are a 64-bit debugger debugging a 32-bit program.  */
   2583 	      && (type == N_LSYM || type == N_PSYM))
   2584 	      /* We have to be careful with the n_value in the case of N_LSYM
   2585 		 and N_PSYM entries, because they are signed offsets from frame
   2586 		 pointer, but we actually read them as unsigned 32-bit values.
   2587 		 This is not a problem for 32-bit debuggers, for which negative
   2588 		 values end up being interpreted correctly (as negative
   2589 		 offsets) due to integer overflow.
   2590 		 But we need to sign-extend the value for 64-bit debuggers,
   2591 		 or we'll end up interpreting negative values as very large
   2592 		 positive offsets.  */
   2593 	    nlist.n_value = (nlist.n_value ^ 0x80000000) - 0x80000000;
   2594 	  process_one_symbol (type, nlist.n_desc, nlist.n_value,
   2595 			      namestring, section_offsets, objfile);
   2596 	}
   2597       /* We skip checking for a new .o or -l file; that should never
   2598          happen in this routine.  */
   2599       else if (type == N_TEXT)
   2600 	{
   2601 	  /* I don't think this code will ever be executed, because
   2602 	     the GCC_COMPILED_FLAG_SYMBOL usually is right before
   2603 	     the N_SO symbol which starts this source file.
   2604 	     However, there is no reason not to accept
   2605 	     the GCC_COMPILED_FLAG_SYMBOL anywhere.  */
   2606 
   2607 	  if (strcmp (namestring, GCC_COMPILED_FLAG_SYMBOL) == 0)
   2608 	    processing_gcc_compilation = 1;
   2609 	  else if (strcmp (namestring, GCC2_COMPILED_FLAG_SYMBOL) == 0)
   2610 	    processing_gcc_compilation = 2;
   2611 	}
   2612       else if (type & N_EXT || type == (unsigned char) N_TEXT
   2613 	       || type == (unsigned char) N_NBTEXT)
   2614 	{
   2615 	  /* Global symbol: see if we came across a dbx defintion for
   2616 	     a corresponding symbol.  If so, store the value.  Remove
   2617 	     syms from the chain when their values are stored, but
   2618 	     search the whole chain, as there may be several syms from
   2619 	     different files with the same name.  */
   2620 	  /* This is probably not true.  Since the files will be read
   2621 	     in one at a time, each reference to a global symbol will
   2622 	     be satisfied in each file as it appears.  So we skip this
   2623 	     section.  */
   2624 	  ;
   2625 	}
   2626     }
   2627 
   2628   /* In a Solaris elf file, this variable, which comes from the
   2629      value of the N_SO symbol, will still be 0.  Luckily, text_offset,
   2630      which comes from pst->textlow is correct.  */
   2631   if (last_source_start_addr == 0)
   2632     last_source_start_addr = text_offset;
   2633 
   2634   /* In reordered executables last_source_start_addr may not be the
   2635      lower bound for this symtab, instead use text_offset which comes
   2636      from pst->textlow which is correct.  */
   2637   if (last_source_start_addr > text_offset)
   2638     last_source_start_addr = text_offset;
   2639 
   2640   pst->compunit_symtab = end_symtab (text_offset + text_size,
   2641 				     SECT_OFF_TEXT (objfile));
   2642 
   2643   end_stabs ();
   2644 
   2645   dbxread_objfile = NULL;
   2646 }
   2647 
   2648 
   2650 /* Record the namespace that the function defined by SYMBOL was
   2651    defined in, if necessary.  BLOCK is the associated block; use
   2652    OBSTACK for allocation.  */
   2653 
   2654 static void
   2655 cp_set_block_scope (const struct symbol *symbol,
   2656 		    struct block *block,
   2657 		    struct obstack *obstack)
   2658 {
   2659   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
   2660     {
   2661       /* Try to figure out the appropriate namespace from the
   2662 	 demangled name.  */
   2663 
   2664       /* FIXME: carlton/2003-04-15: If the function in question is
   2665 	 a method of a class, the name will actually include the
   2666 	 name of the class as well.  This should be harmless, but
   2667 	 is a little unfortunate.  */
   2668 
   2669       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
   2670       unsigned int prefix_len = cp_entire_prefix_len (name);
   2671 
   2672       block_set_scope (block,
   2673 		       obstack_copy0 (obstack, name, prefix_len),
   2674 		       obstack);
   2675     }
   2676 }
   2677 
   2678 /* This handles a single symbol from the symbol-file, building symbols
   2679    into a GDB symtab.  It takes these arguments and an implicit argument.
   2680 
   2681    TYPE is the type field of the ".stab" symbol entry.
   2682    DESC is the desc field of the ".stab" entry.
   2683    VALU is the value field of the ".stab" entry.
   2684    NAME is the symbol name, in our address space.
   2685    SECTION_OFFSETS is a set of amounts by which the sections of this
   2686    object file were relocated when it was loaded into memory.  Note
   2687    that these section_offsets are not the objfile->section_offsets but
   2688    the pst->section_offsets.  All symbols that refer to memory
   2689    locations need to be offset by these amounts.
   2690    OBJFILE is the object file from which we are reading symbols.  It
   2691    is used in end_symtab.  */
   2692 
   2693 void
   2694 process_one_symbol (int type, int desc, CORE_ADDR valu, char *name,
   2695 		    const struct section_offsets *section_offsets,
   2696 		    struct objfile *objfile)
   2697 {
   2698   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   2699   struct context_stack *newobj;
   2700   /* This remembers the address of the start of a function.  It is
   2701      used because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries
   2702      are relative to the current function's start address.  On systems
   2703      other than Solaris 2, this just holds the SECT_OFF_TEXT value,
   2704      and is used to relocate these symbol types rather than
   2705      SECTION_OFFSETS.  */
   2706   static CORE_ADDR function_start_offset;
   2707 
   2708   /* This holds the address of the start of a function, without the
   2709      system peculiarities of function_start_offset.  */
   2710   static CORE_ADDR last_function_start;
   2711 
   2712   /* If this is nonzero, we've seen an N_SLINE since the start of the
   2713      current function.  We use this to tell us to move the first sline
   2714      to the beginning of the function regardless of what its given
   2715      value is.  */
   2716   static int sline_found_in_function = 1;
   2717 
   2718   /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this
   2719      source file.  Used to detect the SunPRO solaris compiler.  */
   2720   static int n_opt_found;
   2721 
   2722   /* The stab type used for the definition of the last function.
   2723      N_STSYM or N_GSYM for SunOS4 acc; N_FUN for other compilers.  */
   2724   static int function_stab_type = 0;
   2725 
   2726   if (!block_address_function_relative)
   2727     {
   2728       /* N_LBRAC, N_RBRAC and N_SLINE entries are not relative to the
   2729 	 function start address, so just use the text offset.  */
   2730       function_start_offset =
   2731 	ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   2732     }
   2733 
   2734   /* Something is wrong if we see real data before seeing a source
   2735      file name.  */
   2736 
   2737   if (get_last_source_file () == NULL && type != (unsigned char) N_SO)
   2738     {
   2739       /* Ignore any symbols which appear before an N_SO symbol.
   2740          Currently no one puts symbols there, but we should deal
   2741          gracefully with the case.  A complain()t might be in order,
   2742          but this should not be an error ().  */
   2743       return;
   2744     }
   2745 
   2746   switch (type)
   2747     {
   2748     case N_FUN:
   2749     case N_FNAME:
   2750 
   2751       if (*name == '\000')
   2752 	{
   2753 	  /* This N_FUN marks the end of a function.  This closes off
   2754 	     the current block.  */
   2755 	  struct block *block;
   2756 
   2757  	  if (context_stack_depth <= 0)
   2758  	    {
   2759 	      lbrac_mismatch_complaint (symnum);
   2760  	      break;
   2761  	    }
   2762 
   2763 	  /* The following check is added before recording line 0 at
   2764 	     end of function so as to handle hand-generated stabs
   2765 	     which may have an N_FUN stabs at the end of the function,
   2766 	     but no N_SLINE stabs.  */
   2767 	  if (sline_found_in_function)
   2768 	    {
   2769 	      CORE_ADDR addr = last_function_start + valu;
   2770 
   2771 	      record_line (current_subfile, 0,
   2772 			   gdbarch_addr_bits_remove (gdbarch, addr));
   2773 	    }
   2774 
   2775 	  within_function = 0;
   2776 	  newobj = pop_context ();
   2777 
   2778 	  /* Make a block for the local symbols within.  */
   2779 	  block = finish_block (newobj->name, &local_symbols,
   2780 				newobj->old_blocks,
   2781 				newobj->start_addr, newobj->start_addr + valu);
   2782 
   2783 	  /* For C++, set the block's scope.  */
   2784 	  if (SYMBOL_LANGUAGE (newobj->name) == language_cplus)
   2785 	    cp_set_block_scope (newobj->name, block, &objfile->objfile_obstack);
   2786 
   2787 	  /* May be switching to an assembler file which may not be using
   2788 	     block relative stabs, so reset the offset.  */
   2789 	  if (block_address_function_relative)
   2790 	    function_start_offset = 0;
   2791 
   2792 	  break;
   2793 	}
   2794 
   2795       sline_found_in_function = 0;
   2796 
   2797       /* Relocate for dynamic loading.  */
   2798       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   2799       valu = gdbarch_addr_bits_remove (gdbarch, valu);
   2800       last_function_start = valu;
   2801 
   2802       goto define_a_symbol;
   2803 
   2804     case N_LBRAC:
   2805       /* This "symbol" just indicates the start of an inner lexical
   2806          context within a function.  */
   2807 
   2808       /* Ignore extra outermost context from SunPRO cc and acc.  */
   2809       if (n_opt_found && desc == 1)
   2810 	break;
   2811 
   2812       if (block_address_function_relative)
   2813 	/* Relocate for Sun ELF acc fn-relative syms.  */
   2814 	valu += function_start_offset;
   2815       else
   2816 	/* On most machines, the block addresses are relative to the
   2817 	   N_SO, the linker did not relocate them (sigh).  */
   2818 	valu += last_source_start_addr;
   2819 
   2820       push_context (desc, valu);
   2821       break;
   2822 
   2823     case N_RBRAC:
   2824       /* This "symbol" just indicates the end of an inner lexical
   2825          context that was started with N_LBRAC.  */
   2826 
   2827       /* Ignore extra outermost context from SunPRO cc and acc.  */
   2828       if (n_opt_found && desc == 1)
   2829 	break;
   2830 
   2831       if (block_address_function_relative)
   2832 	/* Relocate for Sun ELF acc fn-relative syms.  */
   2833 	valu += function_start_offset;
   2834       else
   2835 	/* On most machines, the block addresses are relative to the
   2836 	   N_SO, the linker did not relocate them (sigh).  */
   2837 	valu += last_source_start_addr;
   2838 
   2839       if (context_stack_depth <= 0)
   2840 	{
   2841 	  lbrac_mismatch_complaint (symnum);
   2842 	  break;
   2843 	}
   2844 
   2845       newobj = pop_context ();
   2846       if (desc != newobj->depth)
   2847 	lbrac_mismatch_complaint (symnum);
   2848 
   2849       if (local_symbols != NULL)
   2850 	{
   2851 	  /* GCC development snapshots from March to December of
   2852 	     2000 would output N_LSYM entries after N_LBRAC
   2853 	     entries.  As a consequence, these symbols are simply
   2854 	     discarded.  Complain if this is the case.  */
   2855 	  complaint (&symfile_complaints,
   2856 		     _("misplaced N_LBRAC entry; discarding local "
   2857 		       "symbols which have no enclosing block"));
   2858 	}
   2859       local_symbols = newobj->locals;
   2860 
   2861       if (context_stack_depth > 1)
   2862 	{
   2863 	  /* This is not the outermost LBRAC...RBRAC pair in the
   2864 	     function, its local symbols preceded it, and are the ones
   2865 	     just recovered from the context stack.  Define the block
   2866 	     for them (but don't bother if the block contains no
   2867 	     symbols.  Should we complain on blocks without symbols?
   2868 	     I can't think of any useful purpose for them).  */
   2869 	  if (local_symbols != NULL)
   2870 	    {
   2871 	      /* Muzzle a compiler bug that makes end < start.
   2872 
   2873 		 ??? Which compilers?  Is this ever harmful?.  */
   2874 	      if (newobj->start_addr > valu)
   2875 		{
   2876 		  complaint (&symfile_complaints,
   2877 			     _("block start larger than block end"));
   2878 		  newobj->start_addr = valu;
   2879 		}
   2880 	      /* Make a block for the local symbols within.  */
   2881 	      finish_block (0, &local_symbols, newobj->old_blocks,
   2882 			    newobj->start_addr, valu);
   2883 	    }
   2884 	}
   2885       else
   2886 	{
   2887 	  /* This is the outermost LBRAC...RBRAC pair.  There is no
   2888 	     need to do anything; leave the symbols that preceded it
   2889 	     to be attached to the function's own block.  We need to
   2890 	     indicate that we just moved outside of the function.  */
   2891 	  within_function = 0;
   2892 	}
   2893 
   2894       break;
   2895 
   2896     case N_FN:
   2897     case N_FN_SEQ:
   2898       /* This kind of symbol indicates the start of an object file.
   2899          Relocate for dynamic loading.  */
   2900       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   2901       break;
   2902 
   2903     case N_SO:
   2904       /* This type of symbol indicates the start of data for one
   2905          source file.  Finish the symbol table of the previous source
   2906          file (if any) and start accumulating a new symbol table.
   2907          Relocate for dynamic loading.  */
   2908       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   2909 
   2910       n_opt_found = 0;
   2911 
   2912       if (get_last_source_file ())
   2913 	{
   2914 	  /* Check if previous symbol was also an N_SO (with some
   2915 	     sanity checks).  If so, that one was actually the
   2916 	     directory name, and the current one is the real file
   2917 	     name.  Patch things up.  */
   2918 	  if (previous_stab_code == (unsigned char) N_SO)
   2919 	    {
   2920 	      patch_subfile_names (current_subfile, name);
   2921 	      break;		/* Ignore repeated SOs.  */
   2922 	    }
   2923 	  end_symtab (valu, SECT_OFF_TEXT (objfile));
   2924 	  end_stabs ();
   2925 	}
   2926 
   2927       /* Null name means this just marks the end of text for this .o
   2928          file.  Don't start a new symtab in this case.  */
   2929       if (*name == '\000')
   2930 	break;
   2931 
   2932       if (block_address_function_relative)
   2933 	function_start_offset = 0;
   2934 
   2935       start_stabs ();
   2936       start_symtab (objfile, name, NULL, valu);
   2937       record_debugformat ("stabs");
   2938       break;
   2939 
   2940     case N_SOL:
   2941       /* This type of symbol indicates the start of data for a
   2942          sub-source-file, one whose contents were copied or included
   2943          in the compilation of the main source file (whose name was
   2944          given in the N_SO symbol).  Relocate for dynamic loading.  */
   2945       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   2946       start_subfile (name);
   2947       break;
   2948 
   2949     case N_BINCL:
   2950       push_subfile ();
   2951       add_new_header_file (name, valu);
   2952       start_subfile (name);
   2953       break;
   2954 
   2955     case N_EINCL:
   2956       start_subfile (pop_subfile ());
   2957       break;
   2958 
   2959     case N_EXCL:
   2960       add_old_header_file (name, valu);
   2961       break;
   2962 
   2963     case N_SLINE:
   2964       /* This type of "symbol" really just records one line-number --
   2965          core-address correspondence.  Enter it in the line list for
   2966          this symbol table.  */
   2967 
   2968       /* Relocate for dynamic loading and for ELF acc
   2969          function-relative symbols.  */
   2970       valu += function_start_offset;
   2971 
   2972       /* GCC 2.95.3 emits the first N_SLINE stab somwehere in the
   2973 	 middle of the prologue instead of right at the start of the
   2974 	 function.  To deal with this we record the address for the
   2975 	 first N_SLINE stab to be the start of the function instead of
   2976 	 the listed location.  We really shouldn't to this.  When
   2977 	 compiling with optimization, this first N_SLINE stab might be
   2978 	 optimized away.  Other (non-GCC) compilers don't emit this
   2979 	 stab at all.  There is no real harm in having an extra
   2980 	 numbered line, although it can be a bit annoying for the
   2981 	 user.  However, it totally screws up our testsuite.
   2982 
   2983 	 So for now, keep adjusting the address of the first N_SLINE
   2984 	 stab, but only for code compiled with GCC.  */
   2985 
   2986       if (within_function && sline_found_in_function == 0)
   2987 	{
   2988 	  CORE_ADDR addr = processing_gcc_compilation == 2 ?
   2989 			   last_function_start : valu;
   2990 
   2991 	  record_line (current_subfile, desc,
   2992 		       gdbarch_addr_bits_remove (gdbarch, addr));
   2993 	  sline_found_in_function = 1;
   2994 	}
   2995       else
   2996 	record_line (current_subfile, desc,
   2997 		     gdbarch_addr_bits_remove (gdbarch, valu));
   2998       break;
   2999 
   3000     case N_BCOMM:
   3001       common_block_start (name, objfile);
   3002       break;
   3003 
   3004     case N_ECOMM:
   3005       common_block_end (objfile);
   3006       break;
   3007 
   3008       /* The following symbol types need to have the appropriate
   3009          offset added to their value; then we process symbol
   3010          definitions in the name.  */
   3011 
   3012     case N_STSYM:		/* Static symbol in data segment.  */
   3013     case N_LCSYM:		/* Static symbol in BSS segment.  */
   3014     case N_ROSYM:		/* Static symbol in read-only data segment.  */
   3015       /* HORRID HACK DEPT.  However, it's Sun's furgin' fault.
   3016          Solaris 2's stabs-in-elf makes *most* symbols relative but
   3017          leaves a few absolute (at least for Solaris 2.1 and version
   3018          2.0.1 of the SunPRO compiler).  N_STSYM and friends sit on
   3019          the fence.  .stab "foo:S...",N_STSYM is absolute (ld
   3020          relocates it) .stab "foo:V...",N_STSYM is relative (section
   3021          base subtracted).  This leaves us no choice but to search for
   3022          the 'S' or 'V'...  (or pass the whole section_offsets stuff
   3023          down ONE MORE function call level, which we really don't want
   3024          to do).  */
   3025       {
   3026 	char *p;
   3027 
   3028 	/* Normal object file and NLMs have non-zero text seg offsets,
   3029 	   but don't need their static syms offset in this fashion.
   3030 	   XXX - This is really a crock that should be fixed in the
   3031 	   solib handling code so that I don't have to work around it
   3032 	   here.  */
   3033 
   3034 	if (!symfile_relocatable)
   3035 	  {
   3036 	    p = strchr (name, ':');
   3037 	    if (p != 0 && p[1] == 'S')
   3038 	      {
   3039 		/* The linker relocated it.  We don't want to add a
   3040 		   Sun-stabs Tfoo.foo-like offset, but we *do*
   3041 		   want to add whatever solib.c passed to
   3042 		   symbol_file_add as addr (this is known to affect
   3043 		   SunOS 4, and I suspect ELF too).  Since there is no
   3044 		   Ttext.text symbol, we can get addr from the text offset.  */
   3045 		valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   3046 		goto define_a_symbol;
   3047 	      }
   3048 	  }
   3049 	/* Since it's not the kludge case, re-dispatch to the right
   3050            handler.  */
   3051 	switch (type)
   3052 	  {
   3053 	  case N_STSYM:
   3054 	    goto case_N_STSYM;
   3055 	  case N_LCSYM:
   3056 	    goto case_N_LCSYM;
   3057 	  case N_ROSYM:
   3058 	    goto case_N_ROSYM;
   3059 	  default:
   3060 	    internal_error (__FILE__, __LINE__,
   3061 			    _("failed internal consistency check"));
   3062 	  }
   3063       }
   3064 
   3065     case_N_STSYM:		/* Static symbol in data segment.  */
   3066     case N_DSLINE:		/* Source line number, data segment.  */
   3067       valu += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
   3068       goto define_a_symbol;
   3069 
   3070     case_N_LCSYM:		/* Static symbol in BSS segment.  */
   3071     case N_BSLINE:		/* Source line number, BSS segment.  */
   3072       /* N_BROWS: overlaps with N_BSLINE.  */
   3073       valu += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
   3074       goto define_a_symbol;
   3075 
   3076     case_N_ROSYM:		/* Static symbol in read-only data segment.  */
   3077       valu += ANOFFSET (section_offsets, SECT_OFF_RODATA (objfile));
   3078       goto define_a_symbol;
   3079 
   3080     case N_ENTRY:		/* Alternate entry point.  */
   3081       /* Relocate for dynamic loading.  */
   3082       valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
   3083       goto define_a_symbol;
   3084 
   3085       /* The following symbol types we don't know how to process.
   3086          Handle them in a "default" way, but complain to people who
   3087          care.  */
   3088     default:
   3089     case N_CATCH:		/* Exception handler catcher.  */
   3090     case N_EHDECL:		/* Exception handler name.  */
   3091     case N_PC:			/* Global symbol in Pascal.  */
   3092     case N_M2C:			/* Modula-2 compilation unit.  */
   3093       /* N_MOD2: overlaps with N_EHDECL.  */
   3094     case N_SCOPE:		/* Modula-2 scope information.  */
   3095     case N_ECOML:		/* End common (local name).  */
   3096     case N_NBTEXT:		/* Gould Non-Base-Register symbols???  */
   3097     case N_NBDATA:
   3098     case N_NBBSS:
   3099     case N_NBSTS:
   3100     case N_NBLCS:
   3101       unknown_symtype_complaint (hex_string (type));
   3102       /* FALLTHROUGH */
   3103 
   3104       /* The following symbol types don't need the address field
   3105          relocated, since it is either unused, or is absolute.  */
   3106     define_a_symbol:
   3107     case N_GSYM:		/* Global variable.  */
   3108     case N_NSYMS:		/* Number of symbols (Ultrix).  */
   3109     case N_NOMAP:		/* No map?  (Ultrix).  */
   3110     case N_RSYM:		/* Register variable.  */
   3111     case N_DEFD:		/* Modula-2 GNU module dependency.  */
   3112     case N_SSYM:		/* Struct or union element.  */
   3113     case N_LSYM:		/* Local symbol in stack.  */
   3114     case N_PSYM:		/* Parameter variable.  */
   3115     case N_LENG:		/* Length of preceding symbol type.  */
   3116       if (name)
   3117 	{
   3118 	  int deftype;
   3119 	  char *colon_pos = strchr (name, ':');
   3120 
   3121 	  if (colon_pos == NULL)
   3122 	    deftype = '\0';
   3123 	  else
   3124 	    deftype = colon_pos[1];
   3125 
   3126 	  switch (deftype)
   3127 	    {
   3128 	    case 'f':
   3129 	    case 'F':
   3130 	      function_stab_type = type;
   3131 
   3132 	      /* Deal with the SunPRO 3.0 compiler which omits the
   3133 	         address from N_FUN symbols.  */
   3134 	      if (type == N_FUN
   3135 		  && valu == ANOFFSET (section_offsets,
   3136 				       SECT_OFF_TEXT (objfile))
   3137 		  && gdbarch_sofun_address_maybe_missing (gdbarch))
   3138 		{
   3139 		  CORE_ADDR minsym_valu =
   3140 		    find_stab_function_addr (name, get_last_source_file (),
   3141 					     objfile);
   3142 
   3143 		  /* The function find_stab_function_addr will return
   3144 		     0 if the minimal symbol wasn't found.
   3145 		     (Unfortunately, this might also be a valid
   3146 		     address.)  Anyway, if it *does* return 0, it is
   3147 		     likely that the value was set correctly to begin
   3148 		     with...  */
   3149 		  if (minsym_valu != 0)
   3150 		    valu = minsym_valu;
   3151 		}
   3152 
   3153 	      if (block_address_function_relative)
   3154 		/* For Solaris 2 compilers, the block addresses and
   3155 		   N_SLINE's are relative to the start of the
   3156 		   function.  On normal systems, and when using GCC on
   3157 		   Solaris 2, these addresses are just absolute, or
   3158 		   relative to the N_SO, depending on
   3159 		   BLOCK_ADDRESS_ABSOLUTE.  */
   3160 		function_start_offset = valu;
   3161 
   3162 	      within_function = 1;
   3163 
   3164 	      if (context_stack_depth > 1)
   3165 		{
   3166 		  complaint (&symfile_complaints,
   3167 			     _("unmatched N_LBRAC before symtab pos %d"),
   3168 			     symnum);
   3169 		  break;
   3170 		}
   3171 
   3172 	      if (context_stack_depth > 0)
   3173 		{
   3174 		  struct block *block;
   3175 
   3176 		  newobj = pop_context ();
   3177 		  /* Make a block for the local symbols within.  */
   3178 		  block = finish_block (newobj->name, &local_symbols,
   3179 					newobj->old_blocks, newobj->start_addr,
   3180 					valu);
   3181 
   3182 		  /* For C++, set the block's scope.  */
   3183 		  if (SYMBOL_LANGUAGE (newobj->name) == language_cplus)
   3184 		    cp_set_block_scope (newobj->name, block,
   3185 					&objfile->objfile_obstack);
   3186 		}
   3187 
   3188 	      newobj = push_context (0, valu);
   3189 	      newobj->name = define_symbol (valu, name, desc, type, objfile);
   3190 	      break;
   3191 
   3192 	    default:
   3193 	      define_symbol (valu, name, desc, type, objfile);
   3194 	      break;
   3195 	    }
   3196 	}
   3197       break;
   3198 
   3199       /* We use N_OPT to carry the gcc2_compiled flag.  Sun uses it
   3200          for a bunch of other flags, too.  Someday we may parse their
   3201          flags; for now we ignore theirs and hope they'll ignore ours.  */
   3202     case N_OPT:			/* Solaris 2: Compiler options.  */
   3203       if (name)
   3204 	{
   3205 	  if (strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)
   3206 	    {
   3207 	      processing_gcc_compilation = 2;
   3208 	    }
   3209 	  else
   3210 	    n_opt_found = 1;
   3211 	}
   3212       break;
   3213 
   3214     case N_MAIN:		/* Name of main routine.  */
   3215       /* FIXME: If one has a symbol file with N_MAIN and then replaces
   3216 	 it with a symbol file with "main" and without N_MAIN.  I'm
   3217 	 not sure exactly what rule to follow but probably something
   3218 	 like: N_MAIN takes precedence over "main" no matter what
   3219 	 objfile it is in; If there is more than one N_MAIN, choose
   3220 	 the one in the symfile_objfile; If there is more than one
   3221 	 N_MAIN within a given objfile, complain() and choose
   3222 	 arbitrarily.  (kingdon) */
   3223       if (name != NULL)
   3224 	set_objfile_main_name (objfile, name, language_unknown);
   3225       break;
   3226 
   3227       /* The following symbol types can be ignored.  */
   3228     case N_OBJ:			/* Solaris 2: Object file dir and name.  */
   3229     case N_PATCH:		/* Solaris 2: Patch Run Time Checker.  */
   3230       /* N_UNDF:                   Solaris 2: File separator mark.  */
   3231       /* N_UNDF: -- we will never encounter it, since we only process
   3232          one file's symbols at once.  */
   3233     case N_ENDM:		/* Solaris 2: End of module.  */
   3234     case N_ALIAS:		/* SunPro F77: alias name, ignore for now.  */
   3235       break;
   3236     }
   3237 
   3238   /* '#' is a GNU C extension to allow one symbol to refer to another
   3239      related symbol.
   3240 
   3241      Generally this is used so that an alias can refer to its main
   3242      symbol.  */
   3243   gdb_assert (name);
   3244   if (name[0] == '#')
   3245     {
   3246       /* Initialize symbol reference names and determine if this is a
   3247          definition.  If a symbol reference is being defined, go ahead
   3248          and add it.  Otherwise, just return.  */
   3249 
   3250       char *s = name;
   3251       int refnum;
   3252 
   3253       /* If this stab defines a new reference ID that is not on the
   3254          reference list, then put it on the reference list.
   3255 
   3256          We go ahead and advance NAME past the reference, even though
   3257          it is not strictly necessary at this time.  */
   3258       refnum = symbol_reference_defined (&s);
   3259       if (refnum >= 0)
   3260 	if (!ref_search (refnum))
   3261 	  ref_add (refnum, 0, name, valu);
   3262       name = s;
   3263     }
   3264 
   3265   previous_stab_code = type;
   3266 }
   3267 
   3268 /* FIXME: The only difference between this and elfstab_build_psymtabs
   3270    is the call to install_minimal_symbols for elf, and the support for
   3271    split sections.  If the differences are really that small, the code
   3272    should be shared.  */
   3273 
   3274 /* Scan and build partial symbols for an coff symbol file.
   3275    The coff file has already been processed to get its minimal symbols.
   3276 
   3277    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
   3278    rolled into one.
   3279 
   3280    OBJFILE is the object file we are reading symbols from.
   3281    ADDR is the address relative to which the symbols are (e.g.
   3282    the base address of the text segment).
   3283    TEXTADDR is the address of the text section.
   3284    TEXTSIZE is the size of the text section.
   3285    STABSECTS is the list of .stab sections in OBJFILE.
   3286    STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
   3287    .stabstr section exists.
   3288 
   3289    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
   3290    adjusted for coff details.  */
   3291 
   3292 void
   3293 coffstab_build_psymtabs (struct objfile *objfile,
   3294 			 CORE_ADDR textaddr, unsigned int textsize,
   3295 			 struct stab_section_list *stabsects,
   3296 			 file_ptr stabstroffset, unsigned int stabstrsize)
   3297 {
   3298   int val;
   3299   bfd *sym_bfd = objfile->obfd;
   3300   char *name = bfd_get_filename (sym_bfd);
   3301   struct dbx_symfile_info *info;
   3302   unsigned int stabsize;
   3303 
   3304   /* There is already a dbx_symfile_info allocated by our caller.
   3305      It might even contain some info from the coff symtab to help us.  */
   3306   info = DBX_SYMFILE_INFO (objfile);
   3307 
   3308   DBX_TEXT_ADDR (objfile) = textaddr;
   3309   DBX_TEXT_SIZE (objfile) = textsize;
   3310 
   3311 #define	COFF_STABS_SYMBOL_SIZE	12	/* XXX FIXME XXX */
   3312   DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
   3313   DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
   3314 
   3315   if (stabstrsize > bfd_get_size (sym_bfd))
   3316     error (_("ridiculous string table size: %d bytes"), stabstrsize);
   3317   DBX_STRINGTAB (objfile) = (char *)
   3318     obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
   3319   OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
   3320 
   3321   /* Now read in the string table in one big gulp.  */
   3322 
   3323   val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
   3324   if (val < 0)
   3325     perror_with_name (name);
   3326   val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
   3327   if (val != stabstrsize)
   3328     perror_with_name (name);
   3329 
   3330   stabsread_new_init ();
   3331   buildsym_new_init ();
   3332   free_header_files ();
   3333   init_header_files ();
   3334 
   3335   processing_acc_compilation = 1;
   3336 
   3337   /* In a coff file, we've already installed the minimal symbols that came
   3338      from the coff (non-stab) symbol table, so always act like an
   3339      incremental load here.  */
   3340   if (stabsects->next == NULL)
   3341     {
   3342       stabsize = bfd_section_size (sym_bfd, stabsects->section);
   3343       DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
   3344       DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
   3345     }
   3346   else
   3347     {
   3348       struct stab_section_list *stabsect;
   3349 
   3350       DBX_SYMCOUNT (objfile) = 0;
   3351       for (stabsect = stabsects; stabsect != NULL; stabsect = stabsect->next)
   3352 	{
   3353 	  stabsize = bfd_section_size (sym_bfd, stabsect->section);
   3354 	  DBX_SYMCOUNT (objfile) += stabsize / DBX_SYMBOL_SIZE (objfile);
   3355 	}
   3356 
   3357       DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
   3358 
   3359       symbuf_sections = stabsects->next;
   3360       symbuf_left = bfd_section_size (sym_bfd, stabsects->section);
   3361       symbuf_read = 0;
   3362     }
   3363 
   3364   dbx_symfile_read (objfile, 0);
   3365 }
   3366 
   3367 /* Scan and build partial symbols for an ELF symbol file.
   3369    This ELF file has already been processed to get its minimal symbols.
   3370 
   3371    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
   3372    rolled into one.
   3373 
   3374    OBJFILE is the object file we are reading symbols from.
   3375    ADDR is the address relative to which the symbols are (e.g.
   3376    the base address of the text segment).
   3377    STABSECT is the BFD section information for the .stab section.
   3378    STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
   3379    .stabstr section exists.
   3380 
   3381    This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
   3382    adjusted for elf details.  */
   3383 
   3384 void
   3385 elfstab_build_psymtabs (struct objfile *objfile, asection *stabsect,
   3386 			file_ptr stabstroffset, unsigned int stabstrsize)
   3387 {
   3388   int val;
   3389   bfd *sym_bfd = objfile->obfd;
   3390   char *name = bfd_get_filename (sym_bfd);
   3391   struct dbx_symfile_info *info;
   3392   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
   3393 
   3394   /* There is already a dbx_symfile_info allocated by our caller.
   3395      It might even contain some info from the ELF symtab to help us.  */
   3396   info = DBX_SYMFILE_INFO (objfile);
   3397 
   3398   /* Find the first and last text address.  dbx_symfile_read seems to
   3399      want this.  */
   3400   find_text_range (sym_bfd, objfile);
   3401 
   3402 #define	ELF_STABS_SYMBOL_SIZE	12	/* XXX FIXME XXX */
   3403   DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
   3404   DBX_SYMCOUNT (objfile)
   3405     = bfd_section_size (objfile->obfd, stabsect) / DBX_SYMBOL_SIZE (objfile);
   3406   DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
   3407   DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;
   3408   DBX_STAB_SECTION (objfile) = stabsect;
   3409 
   3410   if (stabstrsize > bfd_get_size (sym_bfd))
   3411     error (_("ridiculous string table size: %d bytes"), stabstrsize);
   3412   DBX_STRINGTAB (objfile) = (char *)
   3413     obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
   3414   OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
   3415 
   3416   /* Now read in the string table in one big gulp.  */
   3417 
   3418   val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
   3419   if (val < 0)
   3420     perror_with_name (name);
   3421   val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
   3422   if (val != stabstrsize)
   3423     perror_with_name (name);
   3424 
   3425   stabsread_new_init ();
   3426   buildsym_new_init ();
   3427   free_header_files ();
   3428   init_header_files ();
   3429 
   3430   processing_acc_compilation = 1;
   3431 
   3432   symbuf_read = 0;
   3433   symbuf_left = bfd_section_size (objfile->obfd, stabsect);
   3434   stabs_data = symfile_relocate_debug_section (objfile, stabsect, NULL);
   3435   if (stabs_data)
   3436     make_cleanup (free_current_contents, (void *) &stabs_data);
   3437 
   3438   /* In an elf file, we've already installed the minimal symbols that came
   3439      from the elf (non-stab) symbol table, so always act like an
   3440      incremental load here.  dbx_symfile_read should not generate any new
   3441      minimal symbols, since we will have already read the ELF dynamic symbol
   3442      table and normal symbol entries won't be in the ".stab" section; but in
   3443      case it does, it will install them itself.  */
   3444   dbx_symfile_read (objfile, 0);
   3445 
   3446   do_cleanups (back_to);
   3447 }
   3448 
   3449 /* Scan and build partial symbols for a file with special sections for stabs
   3451    and stabstrings.  The file has already been processed to get its minimal
   3452    symbols, and any other symbols that might be necessary to resolve GSYMs.
   3453 
   3454    This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
   3455    rolled into one.
   3456 
   3457    OBJFILE is the object file we are reading symbols from.
   3458    ADDR is the address relative to which the symbols are (e.g. the base address
   3459    of the text segment).
   3460    STAB_NAME is the name of the section that contains the stabs.
   3461    STABSTR_NAME is the name of the section that contains the stab strings.
   3462 
   3463    This routine is mostly copied from dbx_symfile_init and
   3464    dbx_symfile_read.  */
   3465 
   3466 void
   3467 stabsect_build_psymtabs (struct objfile *objfile, char *stab_name,
   3468 			 char *stabstr_name, char *text_name)
   3469 {
   3470   int val;
   3471   bfd *sym_bfd = objfile->obfd;
   3472   char *name = bfd_get_filename (sym_bfd);
   3473   asection *stabsect;
   3474   asection *stabstrsect;
   3475   asection *text_sect;
   3476   struct dbx_symfile_info *dbx;
   3477 
   3478   stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
   3479   stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
   3480 
   3481   if (!stabsect)
   3482     return;
   3483 
   3484   if (!stabstrsect)
   3485     error (_("stabsect_build_psymtabs:  Found stabs (%s), "
   3486 	     "but not string section (%s)"),
   3487 	   stab_name, stabstr_name);
   3488 
   3489   dbx = XCNEW (struct dbx_symfile_info);
   3490   set_objfile_data (objfile, dbx_objfile_data_key, dbx);
   3491 
   3492   text_sect = bfd_get_section_by_name (sym_bfd, text_name);
   3493   if (!text_sect)
   3494     error (_("Can't find %s section in symbol file"), text_name);
   3495   DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
   3496   DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
   3497 
   3498   DBX_SYMBOL_SIZE (objfile) = sizeof (struct external_nlist);
   3499   DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
   3500     / DBX_SYMBOL_SIZE (objfile);
   3501   DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
   3502   DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;	/* XXX - FIXME: POKING
   3503 							   INSIDE BFD DATA
   3504 							   STRUCTURES */
   3505 
   3506   if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
   3507     error (_("ridiculous string table size: %d bytes"),
   3508 	   DBX_STRINGTAB_SIZE (objfile));
   3509   DBX_STRINGTAB (objfile) = (char *)
   3510     obstack_alloc (&objfile->objfile_obstack,
   3511 		   DBX_STRINGTAB_SIZE (objfile) + 1);
   3512   OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile) + 1);
   3513 
   3514   /* Now read in the string table in one big gulp.  */
   3515 
   3516   val = bfd_get_section_contents (sym_bfd,	/* bfd */
   3517 				  stabstrsect,	/* bfd section */
   3518 				  DBX_STRINGTAB (objfile), /* input buffer */
   3519 				  0,		/* offset into section */
   3520 				  DBX_STRINGTAB_SIZE (objfile)); /* amount to
   3521 								    read */
   3522 
   3523   if (!val)
   3524     perror_with_name (name);
   3525 
   3526   stabsread_new_init ();
   3527   buildsym_new_init ();
   3528   free_header_files ();
   3529   init_header_files ();
   3530 
   3531   /* Now, do an incremental load.  */
   3532 
   3533   processing_acc_compilation = 1;
   3534   dbx_symfile_read (objfile, 0);
   3535 }
   3536 
   3537 static const struct sym_fns aout_sym_fns =
   3539 {
   3540   dbx_new_init,			/* init anything gbl to entire symtab */
   3541   dbx_symfile_init,		/* read initial info, setup for sym_read() */
   3542   dbx_symfile_read,		/* read a symbol file into symtab */
   3543   NULL,				/* sym_read_psymbols */
   3544   dbx_symfile_finish,		/* finished with file, cleanup */
   3545   default_symfile_offsets, 	/* parse user's offsets to internal form */
   3546   default_symfile_segments,	/* Get segment information from a file.  */
   3547   NULL,
   3548   default_symfile_relocate,	/* Relocate a debug section.  */
   3549   NULL,				/* sym_probe_fns */
   3550   &psym_functions
   3551 };
   3552 
   3553 void
   3554 _initialize_dbxread (void)
   3555 {
   3556   add_symtab_fns (bfd_target_aout_flavour, &aout_sym_fns);
   3557 
   3558   dbx_objfile_data_key
   3559     = register_objfile_data_with_cleanup (NULL, dbx_free_symfile_info);
   3560 }
   3561