119370Spst/* Read dbx symbol tables and convert to internal format, for GDB.
298948Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3130809Smarcel   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004.
419370Spst   Free Software Foundation, Inc.
519370Spst
698948Sobrien   This file is part of GDB.
719370Spst
898948Sobrien   This program is free software; you can redistribute it and/or modify
998948Sobrien   it under the terms of the GNU General Public License as published by
1098948Sobrien   the Free Software Foundation; either version 2 of the License, or
1198948Sobrien   (at your option) any later version.
1219370Spst
1398948Sobrien   This program is distributed in the hope that it will be useful,
1498948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698948Sobrien   GNU General Public License for more details.
1719370Spst
1898948Sobrien   You should have received a copy of the GNU General Public License
1998948Sobrien   along with this program; if not, write to the Free Software
2098948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198948Sobrien   Boston, MA 02111-1307, USA.  */
2219370Spst
2319370Spst/* This module provides three functions: dbx_symfile_init,
2419370Spst   which initializes to read a symbol file; dbx_new_init, which
2519370Spst   discards existing cached information when all symbols are being
2619370Spst   discarded; and dbx_symfile_read, which reads a symbol table
2719370Spst   from a file.
2819370Spst
2919370Spst   dbx_symfile_read only does the minimum work necessary for letting the
3019370Spst   user "name" things symbolically; it does not read the entire symtab.
3119370Spst   Instead, it reads the external and static symbols and puts them in partial
3219370Spst   symbol tables.  When more extensive information is requested of a
3319370Spst   file, the corresponding partial symbol table is mutated into a full
3419370Spst   fledged symbol table by going back and reading the symbols
3519370Spst   for real.  dbx_psymtab_to_symtab() is the function that does this */
3619370Spst
3719370Spst#include "defs.h"
3819370Spst#include "gdb_string.h"
3919370Spst
4019370Spst#if defined(USG) || defined(__CYGNUSCLIB__)
4119370Spst#include <sys/types.h>
4219370Spst#include <fcntl.h>
4319370Spst#endif
4419370Spst
45130809Smarcel#include "gdb_obstack.h"
4619370Spst#include "gdb_stat.h"
4719370Spst#include "symtab.h"
4819370Spst#include "breakpoint.h"
4919370Spst#include "target.h"
5019370Spst#include "gdbcore.h"		/* for bfd stuff */
5198948Sobrien#include "libaout.h"		/* FIXME Secret internal BFD stuff for a.out */
5219370Spst#include "objfiles.h"
5319370Spst#include "buildsym.h"
5419370Spst#include "stabsread.h"
5519370Spst#include "gdb-stabs.h"
5619370Spst#include "demangle.h"
5798948Sobrien#include "language.h"		/* Needed for local_hex_string */
5819370Spst#include "complaints.h"
5998948Sobrien#include "cp-abi.h"
60130809Smarcel#include "gdb_assert.h"
6119370Spst
6219370Spst#include "aout/aout64.h"
6319370Spst#include "aout/stab_gnu.h"	/* We always use GNU stabs, not native, now */
6498948Sobrien
6519370Spst
6619370Spst/* We put a pointer to this structure in the read_symtab_private field
6719370Spst   of the psymtab.  */
6819370Spst
6998948Sobrienstruct symloc
7098948Sobrien  {
7198948Sobrien    /* Offset within the file symbol table of first local symbol for this
7298948Sobrien       file.  */
7319370Spst
7498948Sobrien    int ldsymoff;
7519370Spst
7698948Sobrien    /* Length (in bytes) of the section of the symbol table devoted to
7798948Sobrien       this file's symbols (actually, the section bracketed may contain
7898948Sobrien       more than just this file's symbols).  If ldsymlen is 0, the only
7998948Sobrien       reason for this thing's existence is the dependency list.  Nothing
8098948Sobrien       else will happen when it is read in.  */
8119370Spst
8298948Sobrien    int ldsymlen;
8319370Spst
8498948Sobrien    /* The size of each symbol in the symbol file (in external form).  */
8519370Spst
8698948Sobrien    int symbol_size;
8719370Spst
8898948Sobrien    /* Further information needed to locate the symbols if they are in
8998948Sobrien       an ELF file.  */
9019370Spst
9198948Sobrien    int symbol_offset;
9298948Sobrien    int string_offset;
9398948Sobrien    int file_string_offset;
9498948Sobrien  };
9519370Spst
9619370Spst#define LDSYMOFF(p) (((struct symloc *)((p)->read_symtab_private))->ldsymoff)
9719370Spst#define LDSYMLEN(p) (((struct symloc *)((p)->read_symtab_private))->ldsymlen)
9819370Spst#define SYMLOC(p) ((struct symloc *)((p)->read_symtab_private))
9919370Spst#define SYMBOL_SIZE(p) (SYMLOC(p)->symbol_size)
10019370Spst#define SYMBOL_OFFSET(p) (SYMLOC(p)->symbol_offset)
10119370Spst#define STRING_OFFSET(p) (SYMLOC(p)->string_offset)
10219370Spst#define FILE_STRING_OFFSET(p) (SYMLOC(p)->file_string_offset)
10398948Sobrien
10419370Spst
10519370Spst/* Remember what we deduced to be the source language of this psymtab. */
10619370Spst
10719370Spststatic enum language psymtab_language = language_unknown;
10819370Spst
10919370Spst/* The BFD for this file -- implicit parameter to next_symbol_text.  */
11019370Spst
11119370Spststatic bfd *symfile_bfd;
11219370Spst
11319370Spst/* The size of each symbol in the symbol file (in external form).
11419370Spst   This is set by dbx_symfile_read when building psymtabs, and by
11519370Spst   dbx_psymtab_to_symtab when building symtabs.  */
11619370Spst
11719370Spststatic unsigned symbol_size;
11819370Spst
11946283Sdfr/* This is the offset of the symbol table in the executable file. */
12046283Sdfr
12119370Spststatic unsigned symbol_table_offset;
12219370Spst
12346283Sdfr/* This is the offset of the string table in the executable file. */
12446283Sdfr
12519370Spststatic unsigned string_table_offset;
12619370Spst
12719370Spst/* For elf+stab executables, the n_strx field is not a simple index
12846283Sdfr   into the string table.  Instead, each .o file has a base offset in
12946283Sdfr   the string table, and the associated symbols contain offsets from
13046283Sdfr   this base.  The following two variables contain the base offset for
13146283Sdfr   the current and next .o files. */
13246283Sdfr
13319370Spststatic unsigned int file_string_table_offset;
13419370Spststatic unsigned int next_file_string_table_offset;
13519370Spst
13646283Sdfr/* .o and NLM files contain unrelocated addresses which are based at
13746283Sdfr   0.  When non-zero, this flag disables some of the special cases for
13846283Sdfr   Solaris elf+stab text addresses at location 0. */
13919370Spst
14019370Spststatic int symfile_relocatable = 0;
14119370Spst
14246283Sdfr/* If this is nonzero, N_LBRAC, N_RBRAC, and N_SLINE entries are
14346283Sdfr   relative to the function start address.  */
14419370Spst
14519370Spststatic int block_address_function_relative = 0;
14619370Spst
14719370Spst/* The lowest text address we have yet encountered.  This is needed
14819370Spst   because in an a.out file, there is no header field which tells us
14919370Spst   what address the program is actually going to be loaded at, so we
15019370Spst   need to make guesses based on the symbols (which *are* relocated to
15119370Spst   reflect the address it will be loaded at).  */
15246283Sdfr
15319370Spststatic CORE_ADDR lowest_text_address;
15419370Spst
15546283Sdfr/* Non-zero if there is any line number info in the objfile.  Prevents
15646283Sdfr   end_psymtab from discarding an otherwise empty psymtab.  */
15746283Sdfr
15846283Sdfrstatic int has_line_numbers;
15946283Sdfr
16019370Spst/* Complaints about the symbols we have encountered.  */
16119370Spst
162130809Smarcelstatic void
163130809Smarcelunknown_symtype_complaint (const char *arg1)
164130809Smarcel{
165130809Smarcel  complaint (&symfile_complaints, "unknown symbol type %s", arg1);
166130809Smarcel}
16719370Spst
168130809Smarcelstatic void
169130809Smarcellbrac_mismatch_complaint (int arg1)
170130809Smarcel{
171130809Smarcel  complaint (&symfile_complaints,
172130809Smarcel	     "N_LBRAC/N_RBRAC symbol mismatch at symtab pos %d", arg1);
173130809Smarcel}
17419370Spst
175130809Smarcelstatic void
176130809Smarcelrepeated_header_complaint (const char *arg1, int arg2)
177130809Smarcel{
178130809Smarcel  complaint (&symfile_complaints,
179130809Smarcel	     "\"repeated\" header file %s not previously seen, at symtab pos %d",
180130809Smarcel	     arg1, arg2);
181130809Smarcel}
18219370Spst
18398948Sobrien/* find_text_range --- find start and end of loadable code sections
18498948Sobrien
18598948Sobrien   The find_text_range function finds the shortest address range that
18698948Sobrien   encloses all sections containing executable code, and stores it in
18798948Sobrien   objfile's text_addr and text_size members.
18898948Sobrien
18998948Sobrien   dbx_symfile_read will use this to finish off the partial symbol
19098948Sobrien   table, in some cases.  */
19198948Sobrien
19298948Sobrienstatic void
19398948Sobrienfind_text_range (bfd * sym_bfd, struct objfile *objfile)
19498948Sobrien{
19598948Sobrien  asection *sec;
19698948Sobrien  int found_any = 0;
19798948Sobrien  CORE_ADDR start = 0;
19898948Sobrien  CORE_ADDR end = 0;
19998948Sobrien
20098948Sobrien  for (sec = sym_bfd->sections; sec; sec = sec->next)
20198948Sobrien    if (bfd_get_section_flags (sym_bfd, sec) & SEC_CODE)
20298948Sobrien      {
20398948Sobrien	CORE_ADDR sec_start = bfd_section_vma (sym_bfd, sec);
20498948Sobrien	CORE_ADDR sec_end = sec_start + bfd_section_size (sym_bfd, sec);
20598948Sobrien
20698948Sobrien	if (found_any)
20798948Sobrien	  {
20898948Sobrien	    if (sec_start < start)
20998948Sobrien	      start = sec_start;
21098948Sobrien	    if (sec_end > end)
21198948Sobrien	      end = sec_end;
21298948Sobrien	  }
21398948Sobrien	else
21498948Sobrien	  {
21598948Sobrien	    start = sec_start;
21698948Sobrien	    end = sec_end;
21798948Sobrien	  }
21898948Sobrien
21998948Sobrien	found_any = 1;
22098948Sobrien      }
22198948Sobrien
22298948Sobrien  if (!found_any)
22398948Sobrien    error ("Can't find any code sections in symbol file");
22498948Sobrien
22598948Sobrien  DBX_TEXT_ADDR (objfile) = start;
22698948Sobrien  DBX_TEXT_SIZE (objfile) = end - start;
22798948Sobrien}
22898948Sobrien
22998948Sobrien
23098948Sobrien
23119370Spst/* During initial symbol readin, we need to have a structure to keep
23219370Spst   track of which psymtabs have which bincls in them.  This structure
23319370Spst   is used during readin to setup the list of dependencies within each
23419370Spst   partial symbol table. */
23519370Spst
23619370Spststruct header_file_location
23719370Spst{
23819370Spst  char *name;			/* Name of header file */
23919370Spst  int instance;			/* See above */
24019370Spst  struct partial_symtab *pst;	/* Partial symtab that has the
24119370Spst				   BINCL/EINCL defs for this file */
24219370Spst};
24319370Spst
24419370Spst/* The actual list and controling variables */
24519370Spststatic struct header_file_location *bincl_list, *next_bincl;
24619370Spststatic int bincls_allocated;
24719370Spst
24819370Spst/* Local function prototypes */
24919370Spst
25098948Sobrienextern void _initialize_dbxread (void);
25146283Sdfr
25298948Sobrienstatic void read_ofile_symtab (struct partial_symtab *);
25319370Spst
25498948Sobrienstatic void dbx_psymtab_to_symtab (struct partial_symtab *);
25519370Spst
25698948Sobrienstatic void dbx_psymtab_to_symtab_1 (struct partial_symtab *);
25719370Spst
25898948Sobrienstatic void read_dbx_dynamic_symtab (struct objfile *objfile);
25919370Spst
26098948Sobrienstatic void read_dbx_symtab (struct objfile *);
26119370Spst
26298948Sobrienstatic void free_bincl_list (struct objfile *);
26319370Spst
26498948Sobrienstatic struct partial_symtab *find_corresponding_bincl_psymtab (char *, int);
26519370Spst
26698948Sobrienstatic void add_bincl_to_list (struct partial_symtab *, char *, int);
26719370Spst
26898948Sobrienstatic void init_bincl_list (int, struct objfile *);
26919370Spst
27098948Sobrienstatic char *dbx_next_symbol_text (struct objfile *);
27119370Spst
27298948Sobrienstatic void fill_symbuf (bfd *);
27319370Spst
27498948Sobrienstatic void dbx_symfile_init (struct objfile *);
27519370Spst
27698948Sobrienstatic void dbx_new_init (struct objfile *);
27719370Spst
27898948Sobrienstatic void dbx_symfile_read (struct objfile *, int);
27919370Spst
28098948Sobrienstatic void dbx_symfile_finish (struct objfile *);
28119370Spst
28298948Sobrienstatic void record_minimal_symbol (char *, CORE_ADDR, int, struct objfile *);
28319370Spst
28498948Sobrienstatic void add_new_header_file (char *, int);
28519370Spst
28698948Sobrienstatic void add_old_header_file (char *, int);
28719370Spst
28898948Sobrienstatic void add_this_object_header_file (int);
28919370Spst
29098948Sobrienstatic struct partial_symtab *start_psymtab (struct objfile *, char *,
29198948Sobrien					     CORE_ADDR, int,
29298948Sobrien					     struct partial_symbol **,
29398948Sobrien					     struct partial_symbol **);
29419370Spst
29519370Spst/* Free up old header file tables */
29619370Spst
29798948Sobrienvoid
29898948Sobrienfree_header_files (void)
29919370Spst{
30019370Spst  if (this_object_header_files)
30119370Spst    {
30298948Sobrien      xfree (this_object_header_files);
30319370Spst      this_object_header_files = NULL;
30419370Spst    }
30519370Spst  n_allocated_this_object_header_files = 0;
30619370Spst}
30719370Spst
30819370Spst/* Allocate new header file tables */
30919370Spst
31098948Sobrienvoid
31198948Sobrieninit_header_files (void)
31219370Spst{
31319370Spst  n_allocated_this_object_header_files = 10;
31419370Spst  this_object_header_files = (int *) xmalloc (10 * sizeof (int));
31519370Spst}
31619370Spst
31719370Spst/* Add header file number I for this object file
31819370Spst   at the next successive FILENUM.  */
31919370Spst
32019370Spststatic void
32198948Sobrienadd_this_object_header_file (int i)
32219370Spst{
32319370Spst  if (n_this_object_header_files == n_allocated_this_object_header_files)
32419370Spst    {
32519370Spst      n_allocated_this_object_header_files *= 2;
32619370Spst      this_object_header_files
32719370Spst	= (int *) xrealloc ((char *) this_object_header_files,
32898948Sobrien		       n_allocated_this_object_header_files * sizeof (int));
32919370Spst    }
33019370Spst
33119370Spst  this_object_header_files[n_this_object_header_files++] = i;
33219370Spst}
33319370Spst
33419370Spst/* Add to this file an "old" header file, one already seen in
33519370Spst   a previous object file.  NAME is the header file's name.
33619370Spst   INSTANCE is its instance code, to select among multiple
33719370Spst   symbol tables for the same header file.  */
33819370Spst
33919370Spststatic void
34098948Sobrienadd_old_header_file (char *name, int instance)
34119370Spst{
342130809Smarcel  struct header_file *p = HEADER_FILES (current_objfile);
343130809Smarcel  int i;
34419370Spst
34546283Sdfr  for (i = 0; i < N_HEADER_FILES (current_objfile); i++)
346130809Smarcel    if (strcmp (p[i].name, name) == 0 && instance == p[i].instance)
34719370Spst      {
34819370Spst	add_this_object_header_file (i);
34919370Spst	return;
35019370Spst      }
351130809Smarcel  repeated_header_complaint (name, symnum);
35219370Spst}
35319370Spst
35419370Spst/* Add to this file a "new" header file: definitions for its types follow.
35519370Spst   NAME is the header file's name.
35619370Spst   Most often this happens only once for each distinct header file,
35719370Spst   but not necessarily.  If it happens more than once, INSTANCE has
35819370Spst   a different value each time, and references to the header file
35919370Spst   use INSTANCE values to select among them.
36019370Spst
36119370Spst   dbx output contains "begin" and "end" markers for each new header file,
36219370Spst   but at this level we just need to know which files there have been;
36319370Spst   so we record the file when its "begin" is seen and ignore the "end".  */
36419370Spst
36519370Spststatic void
36698948Sobrienadd_new_header_file (char *name, int instance)
36719370Spst{
368130809Smarcel  int i;
369130809Smarcel  struct header_file *hfile;
37019370Spst
37119370Spst  /* Make sure there is room for one more header file.  */
37219370Spst
37346283Sdfr  i = N_ALLOCATED_HEADER_FILES (current_objfile);
37446283Sdfr
37546283Sdfr  if (N_HEADER_FILES (current_objfile) == i)
37619370Spst    {
37746283Sdfr      if (i == 0)
37846283Sdfr	{
37946283Sdfr	  N_ALLOCATED_HEADER_FILES (current_objfile) = 10;
38046283Sdfr	  HEADER_FILES (current_objfile) = (struct header_file *)
38146283Sdfr	    xmalloc (10 * sizeof (struct header_file));
38246283Sdfr	}
38346283Sdfr      else
38446283Sdfr	{
38546283Sdfr	  i *= 2;
38646283Sdfr	  N_ALLOCATED_HEADER_FILES (current_objfile) = i;
38746283Sdfr	  HEADER_FILES (current_objfile) = (struct header_file *)
38846283Sdfr	    xrealloc ((char *) HEADER_FILES (current_objfile),
38946283Sdfr		      (i * sizeof (struct header_file)));
39046283Sdfr	}
39119370Spst    }
39219370Spst
39319370Spst  /* Create an entry for this header file.  */
39419370Spst
39546283Sdfr  i = N_HEADER_FILES (current_objfile)++;
39646283Sdfr  hfile = HEADER_FILES (current_objfile) + i;
39798948Sobrien  hfile->name = savestring (name, strlen (name));
39846283Sdfr  hfile->instance = instance;
39946283Sdfr  hfile->length = 10;
40046283Sdfr  hfile->vector
40119370Spst    = (struct type **) xmalloc (10 * sizeof (struct type *));
40246283Sdfr  memset (hfile->vector, 0, 10 * sizeof (struct type *));
40319370Spst
40419370Spst  add_this_object_header_file (i);
40519370Spst}
40619370Spst
40719370Spst#if 0
40819370Spststatic struct type **
40998948Sobrienexplicit_lookup_type (int real_filenum, int index)
41019370Spst{
411130809Smarcel  struct header_file *f = &HEADER_FILES (current_objfile)[real_filenum];
41219370Spst
41319370Spst  if (index >= f->length)
41419370Spst    {
41519370Spst      f->length *= 2;
41619370Spst      f->vector = (struct type **)
41719370Spst	xrealloc (f->vector, f->length * sizeof (struct type *));
41819370Spst      memset (&f->vector[f->length / 2],
41998948Sobrien	      '\0', f->length * sizeof (struct type *) / 2);
42019370Spst    }
42119370Spst  return &f->vector[index];
42219370Spst}
42319370Spst#endif
42419370Spst
42519370Spststatic void
42698948Sobrienrecord_minimal_symbol (char *name, CORE_ADDR address, int type,
42798948Sobrien		       struct objfile *objfile)
42819370Spst{
42919370Spst  enum minimal_symbol_type ms_type;
43019370Spst  int section;
43146283Sdfr  asection *bfd_section;
43219370Spst
43319370Spst  switch (type)
43419370Spst    {
43519370Spst    case N_TEXT | N_EXT:
43619370Spst      ms_type = mst_text;
43798948Sobrien      section = SECT_OFF_TEXT (objfile);
43846283Sdfr      bfd_section = DBX_TEXT_SECTION (objfile);
43919370Spst      break;
44019370Spst    case N_DATA | N_EXT:
44119370Spst      ms_type = mst_data;
44298948Sobrien      section = SECT_OFF_DATA (objfile);
44346283Sdfr      bfd_section = DBX_DATA_SECTION (objfile);
44419370Spst      break;
44519370Spst    case N_BSS | N_EXT:
44619370Spst      ms_type = mst_bss;
44798948Sobrien      section = SECT_OFF_BSS (objfile);
44846283Sdfr      bfd_section = DBX_BSS_SECTION (objfile);
44919370Spst      break;
45019370Spst    case N_ABS | N_EXT:
45119370Spst      ms_type = mst_abs;
45219370Spst      section = -1;
45346283Sdfr      bfd_section = NULL;
45419370Spst      break;
45519370Spst#ifdef N_SETV
45619370Spst    case N_SETV | N_EXT:
45719370Spst      ms_type = mst_data;
45898948Sobrien      section = SECT_OFF_DATA (objfile);
45946283Sdfr      bfd_section = DBX_DATA_SECTION (objfile);
46019370Spst      break;
46119370Spst    case N_SETV:
46219370Spst      /* I don't think this type actually exists; since a N_SETV is the result
46398948Sobrien         of going over many .o files, it doesn't make sense to have one
46498948Sobrien         file local.  */
46519370Spst      ms_type = mst_file_data;
46698948Sobrien      section = SECT_OFF_DATA (objfile);
46746283Sdfr      bfd_section = DBX_DATA_SECTION (objfile);
46819370Spst      break;
46919370Spst#endif
47019370Spst    case N_TEXT:
47119370Spst    case N_NBTEXT:
47219370Spst    case N_FN:
47319370Spst    case N_FN_SEQ:
47419370Spst      ms_type = mst_file_text;
47598948Sobrien      section = SECT_OFF_TEXT (objfile);
47646283Sdfr      bfd_section = DBX_TEXT_SECTION (objfile);
47719370Spst      break;
47819370Spst    case N_DATA:
47919370Spst      ms_type = mst_file_data;
48019370Spst
48119370Spst      /* Check for __DYNAMIC, which is used by Sun shared libraries.
48298948Sobrien         Record it as global even if it's local, not global, so
48398948Sobrien         lookup_minimal_symbol can find it.  We don't check symbol_leading_char
48498948Sobrien         because for SunOS4 it always is '_'.  */
485130809Smarcel      if (name[8] == 'C' && DEPRECATED_STREQ ("__DYNAMIC", name))
48619370Spst	ms_type = mst_data;
48719370Spst
48819370Spst      /* Same with virtual function tables, both global and static.  */
48919370Spst      {
49019370Spst	char *tempstring = name;
49119370Spst	if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
49219370Spst	  ++tempstring;
49398948Sobrien	if (is_vtable_name (tempstring))
49419370Spst	  ms_type = mst_data;
49519370Spst      }
49698948Sobrien      section = SECT_OFF_DATA (objfile);
49746283Sdfr      bfd_section = DBX_DATA_SECTION (objfile);
49819370Spst      break;
49919370Spst    case N_BSS:
50019370Spst      ms_type = mst_file_bss;
50198948Sobrien      section = SECT_OFF_BSS (objfile);
50246283Sdfr      bfd_section = DBX_BSS_SECTION (objfile);
50319370Spst      break;
50419370Spst    default:
50519370Spst      ms_type = mst_unknown;
50619370Spst      section = -1;
50746283Sdfr      bfd_section = NULL;
50819370Spst      break;
50998948Sobrien    }
51019370Spst
51119370Spst  if ((ms_type == mst_file_text || ms_type == mst_text)
51219370Spst      && address < lowest_text_address)
51319370Spst    lowest_text_address = address;
51419370Spst
51519370Spst  prim_record_minimal_symbol_and_info
51646283Sdfr    (name, address, ms_type, NULL, section, bfd_section, objfile);
51719370Spst}
51819370Spst
51919370Spst/* Scan and build partial symbols for a symbol file.
52019370Spst   We have been initialized by a call to dbx_symfile_init, which
52119370Spst   put all the relevant info into a "struct dbx_symfile_info",
52219370Spst   hung off the objfile structure.
52319370Spst
52419370Spst   MAINLINE is true if we are reading the main symbol
52519370Spst   table (as opposed to a shared lib or dynamically loaded file).  */
52619370Spst
52719370Spststatic void
52898948Sobriendbx_symfile_read (struct objfile *objfile, int mainline)
52919370Spst{
53019370Spst  bfd *sym_bfd;
53119370Spst  int val;
53219370Spst  struct cleanup *back_to;
53319370Spst
53446283Sdfr  sym_bfd = objfile->obfd;
53546283Sdfr
53619370Spst  /* .o and .nlm files are relocatables with text, data and bss segs based at
53719370Spst     0.  This flag disables special (Solaris stabs-in-elf only) fixups for
53846283Sdfr     symbols with a value of 0.  */
53919370Spst
54046283Sdfr  symfile_relocatable = bfd_get_file_flags (sym_bfd) & HAS_RELOC;
54119370Spst
54219370Spst  /* This is true for Solaris (and all other systems which put stabs
54319370Spst     in sections, hopefully, since it would be silly to do things
54419370Spst     differently from Solaris), and false for SunOS4 and other a.out
54519370Spst     file formats.  */
54619370Spst  block_address_function_relative =
54746283Sdfr    ((0 == strncmp (bfd_get_target (sym_bfd), "elf", 3))
54846283Sdfr     || (0 == strncmp (bfd_get_target (sym_bfd), "som", 3))
54946283Sdfr     || (0 == strncmp (bfd_get_target (sym_bfd), "coff", 4))
55046283Sdfr     || (0 == strncmp (bfd_get_target (sym_bfd), "pe", 2))
55198948Sobrien     || (0 == strncmp (bfd_get_target (sym_bfd), "epoc-pe", 7))
55246283Sdfr     || (0 == strncmp (bfd_get_target (sym_bfd), "nlm", 3)));
55319370Spst
55446283Sdfr  val = bfd_seek (sym_bfd, DBX_SYMTAB_OFFSET (objfile), SEEK_SET);
55519370Spst  if (val < 0)
55619370Spst    perror_with_name (objfile->name);
55719370Spst
55819370Spst  /* If we are reinitializing, or if we have never loaded syms yet, init */
55919370Spst  if (mainline
56098948Sobrien      || (objfile->global_psymbols.size == 0
56198948Sobrien	  &&  objfile->static_psymbols.size == 0))
56219370Spst    init_psymbol_list (objfile, DBX_SYMCOUNT (objfile));
56319370Spst
56419370Spst  symbol_size = DBX_SYMBOL_SIZE (objfile);
56519370Spst  symbol_table_offset = DBX_SYMTAB_OFFSET (objfile);
56619370Spst
56746283Sdfr  free_pending_blocks ();
56898948Sobrien  back_to = make_cleanup (really_free_pendings, 0);
56919370Spst
57019370Spst  init_minimal_symbol_collection ();
57198948Sobrien  make_cleanup_discard_minimal_symbols ();
57219370Spst
57398948Sobrien  /* Read stabs data from executable file and define symbols. */
57419370Spst
57598948Sobrien  read_dbx_symtab (objfile);
57619370Spst
57719370Spst  /* Add the dynamic symbols.  */
57819370Spst
57998948Sobrien  read_dbx_dynamic_symtab (objfile);
58019370Spst
58119370Spst  /* Install any minimal symbols that have been collected as the current
58219370Spst     minimal symbols for this objfile. */
58319370Spst
58419370Spst  install_minimal_symbols (objfile);
58519370Spst
58619370Spst  do_cleanups (back_to);
58719370Spst}
58819370Spst
58919370Spst/* Initialize anything that needs initializing when a completely new
59019370Spst   symbol file is specified (not just adding some symbols from another
59119370Spst   file, e.g. a shared library).  */
59219370Spst
59319370Spststatic void
59498948Sobriendbx_new_init (struct objfile *ignore)
59519370Spst{
59619370Spst  stabsread_new_init ();
59719370Spst  buildsym_new_init ();
59819370Spst  init_header_files ();
59919370Spst}
60019370Spst
60119370Spst
60219370Spst/* dbx_symfile_init ()
60319370Spst   is the dbx-specific initialization routine for reading symbols.
60419370Spst   It is passed a struct objfile which contains, among other things,
60519370Spst   the BFD for the file whose symbols are being read, and a slot for a pointer
60619370Spst   to "private data" which we fill with goodies.
60719370Spst
60819370Spst   We read the string table into malloc'd space and stash a pointer to it.
60919370Spst
61019370Spst   Since BFD doesn't know how to read debug symbols in a format-independent
61119370Spst   way (and may never do so...), we have to do it ourselves.  We will never
61219370Spst   be called unless this is an a.out (or very similar) file.
61319370Spst   FIXME, there should be a cleaner peephole into the BFD environment here.  */
61419370Spst
61598948Sobrien#define DBX_STRINGTAB_SIZE_SIZE sizeof(long)	/* FIXME */
61619370Spst
61719370Spststatic void
61898948Sobriendbx_symfile_init (struct objfile *objfile)
61919370Spst{
62019370Spst  int val;
62119370Spst  bfd *sym_bfd = objfile->obfd;
62219370Spst  char *name = bfd_get_filename (sym_bfd);
62319370Spst  asection *text_sect;
62419370Spst  unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
62519370Spst
62619370Spst  /* Allocate struct to keep track of the symfile */
62746283Sdfr  objfile->sym_stab_info = (struct dbx_symfile_info *)
62898948Sobrien    xmmalloc (objfile->md, sizeof (struct dbx_symfile_info));
629130809Smarcel  memset (objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
63019370Spst
63146283Sdfr  DBX_TEXT_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
63246283Sdfr  DBX_DATA_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".data");
63346283Sdfr  DBX_BSS_SECTION (objfile) = bfd_get_section_by_name (sym_bfd, ".bss");
63446283Sdfr
63519370Spst  /* FIXME POKING INSIDE BFD DATA STRUCTURES */
63619370Spst#define	STRING_TABLE_OFFSET	(sym_bfd->origin + obj_str_filepos (sym_bfd))
63719370Spst#define	SYMBOL_TABLE_OFFSET	(sym_bfd->origin + obj_sym_filepos (sym_bfd))
63819370Spst
63919370Spst  /* FIXME POKING INSIDE BFD DATA STRUCTURES */
64019370Spst
64119370Spst  DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
64298948Sobrien
64319370Spst  text_sect = bfd_get_section_by_name (sym_bfd, ".text");
64419370Spst  if (!text_sect)
64519370Spst    error ("Can't find .text section in symbol file");
64619370Spst  DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
64719370Spst  DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
64819370Spst
64919370Spst  DBX_SYMBOL_SIZE (objfile) = obj_symbol_entry_size (sym_bfd);
65019370Spst  DBX_SYMCOUNT (objfile) = bfd_get_symcount (sym_bfd);
65119370Spst  DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
65219370Spst
653130809Smarcel  /* Read the string table and stash it away in the objfile_obstack.
654130809Smarcel     When we blow away the objfile the string table goes away as well.
65519370Spst     Note that gdb used to use the results of attempting to malloc the
65619370Spst     string table, based on the size it read, as a form of sanity check
65719370Spst     for botched byte swapping, on the theory that a byte swapped string
65819370Spst     table size would be so totally bogus that the malloc would fail.  Now
659130809Smarcel     that we put in on the objfile_obstack, we can't do this since gdb gets
66019370Spst     a fatal error (out of virtual memory) if the size is bogus.  We can
66119370Spst     however at least check to see if the size is less than the size of
66219370Spst     the size field itself, or larger than the size of the entire file.
66319370Spst     Note that all valid string tables have a size greater than zero, since
66419370Spst     the bytes used to hold the size are included in the count. */
66519370Spst
66619370Spst  if (STRING_TABLE_OFFSET == 0)
66719370Spst    {
66819370Spst      /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
66998948Sobrien         will never be zero, even when there is no string table.  This
67098948Sobrien         would appear to be a bug in bfd. */
67119370Spst      DBX_STRINGTAB_SIZE (objfile) = 0;
67219370Spst      DBX_STRINGTAB (objfile) = NULL;
67319370Spst    }
67419370Spst  else
67519370Spst    {
67619370Spst      val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
67719370Spst      if (val < 0)
67819370Spst	perror_with_name (name);
67998948Sobrien
680130809Smarcel      memset (size_temp, 0, sizeof (size_temp));
681130809Smarcel      val = bfd_bread (size_temp, sizeof (size_temp), sym_bfd);
68219370Spst      if (val < 0)
68319370Spst	{
68419370Spst	  perror_with_name (name);
68519370Spst	}
68619370Spst      else if (val == 0)
68719370Spst	{
68819370Spst	  /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
68919370Spst	     EOF if there is no string table, and attempting to read the size
69019370Spst	     from EOF will read zero bytes. */
69119370Spst	  DBX_STRINGTAB_SIZE (objfile) = 0;
69219370Spst	  DBX_STRINGTAB (objfile) = NULL;
69319370Spst	}
69419370Spst      else
69519370Spst	{
69619370Spst	  /* Read some data that would appear to be the string table size.
69719370Spst	     If there really is a string table, then it is probably the right
69819370Spst	     size.  Byteswap if necessary and validate the size.  Note that
69919370Spst	     the minimum is DBX_STRINGTAB_SIZE_SIZE.  If we just read some
70019370Spst	     random data that happened to be at STRING_TABLE_OFFSET, because
70119370Spst	     bfd can't tell us there is no string table, the sanity checks may
70219370Spst	     or may not catch this. */
70319370Spst	  DBX_STRINGTAB_SIZE (objfile) = bfd_h_get_32 (sym_bfd, size_temp);
70498948Sobrien
70519370Spst	  if (DBX_STRINGTAB_SIZE (objfile) < sizeof (size_temp)
70619370Spst	      || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
70719370Spst	    error ("ridiculous string table size (%d bytes).",
70819370Spst		   DBX_STRINGTAB_SIZE (objfile));
70998948Sobrien
71019370Spst	  DBX_STRINGTAB (objfile) =
711130809Smarcel	    (char *) obstack_alloc (&objfile->objfile_obstack,
71219370Spst				    DBX_STRINGTAB_SIZE (objfile));
71319370Spst	  OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile));
71498948Sobrien
71519370Spst	  /* Now read in the string table in one big gulp.  */
71698948Sobrien
71719370Spst	  val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, SEEK_SET);
71819370Spst	  if (val < 0)
71919370Spst	    perror_with_name (name);
72098948Sobrien	  val = bfd_bread (DBX_STRINGTAB (objfile),
72198948Sobrien			   DBX_STRINGTAB_SIZE (objfile),
72298948Sobrien			   sym_bfd);
72319370Spst	  if (val != DBX_STRINGTAB_SIZE (objfile))
72419370Spst	    perror_with_name (name);
72519370Spst	}
72619370Spst    }
72719370Spst}
72819370Spst
72919370Spst/* Perform any local cleanups required when we are done with a particular
73019370Spst   objfile.  I.E, we are in the process of discarding all symbol information
73119370Spst   for an objfile, freeing up all memory held for it, and unlinking the
73219370Spst   objfile struct from the global list of known objfiles. */
73319370Spst
73419370Spststatic void
73598948Sobriendbx_symfile_finish (struct objfile *objfile)
73619370Spst{
73719370Spst  if (objfile->sym_stab_info != NULL)
73819370Spst    {
73946283Sdfr      if (HEADER_FILES (objfile) != NULL)
74046283Sdfr	{
741130809Smarcel	  int i = N_HEADER_FILES (objfile);
742130809Smarcel	  struct header_file *hfiles = HEADER_FILES (objfile);
74346283Sdfr
74446283Sdfr	  while (--i >= 0)
74546283Sdfr	    {
74698948Sobrien	      xfree (hfiles[i].name);
74798948Sobrien	      xfree (hfiles[i].vector);
74846283Sdfr	    }
74998948Sobrien	  xfree (hfiles);
75046283Sdfr	}
75198948Sobrien      xmfree (objfile->md, objfile->sym_stab_info);
75219370Spst    }
75319370Spst  free_header_files ();
75419370Spst}
75598948Sobrien
75619370Spst
75719370Spst/* Buffer for reading the symbol table entries.  */
75846283Sdfrstatic struct external_nlist symbuf[4096];
75919370Spststatic int symbuf_idx;
76019370Spststatic int symbuf_end;
76119370Spst
76219370Spst/* Name of last function encountered.  Used in Solaris to approximate
76319370Spst   object file boundaries.  */
76419370Spststatic char *last_function_name;
76519370Spst
76619370Spst/* The address in memory of the string table of the object file we are
76719370Spst   reading (which might not be the "main" object file, but might be a
76819370Spst   shared library or some other dynamically loaded thing).  This is
76919370Spst   set by read_dbx_symtab when building psymtabs, and by
77019370Spst   read_ofile_symtab when building symtabs, and is used only by
77119370Spst   next_symbol_text.  FIXME: If that is true, we don't need it when
77219370Spst   building psymtabs, right?  */
77319370Spststatic char *stringtab_global;
77419370Spst
77519370Spst/* These variables are used to control fill_symbuf when the stabs
77619370Spst   symbols are not contiguous (as may be the case when a COFF file is
77719370Spst   linked using --split-by-reloc).  */
77819370Spststatic struct stab_section_list *symbuf_sections;
77919370Spststatic unsigned int symbuf_left;
78019370Spststatic unsigned int symbuf_read;
78119370Spst
782130809Smarcel/* This variable stores a global stabs buffer, if we read stabs into
783130809Smarcel   memory in one chunk in order to process relocations.  */
784130809Smarcelstatic bfd_byte *stabs_data;
785130809Smarcel
78619370Spst/* Refill the symbol table input buffer
78719370Spst   and set the variables that control fetching entries from it.
78819370Spst   Reports an error if no data available.
78919370Spst   This function can read past the end of the symbol table
79019370Spst   (into the string table) but this does no harm.  */
79119370Spst
79219370Spststatic void
79398948Sobrienfill_symbuf (bfd *sym_bfd)
79419370Spst{
79519370Spst  unsigned int count;
79619370Spst  int nbytes;
79719370Spst
798130809Smarcel  if (stabs_data)
799130809Smarcel    {
800130809Smarcel      nbytes = sizeof (symbuf);
801130809Smarcel      if (nbytes > symbuf_left)
802130809Smarcel        nbytes = symbuf_left;
803130809Smarcel      memcpy (symbuf, stabs_data + symbuf_read, nbytes);
804130809Smarcel    }
805130809Smarcel  else if (symbuf_sections == NULL)
806130809Smarcel    {
807130809Smarcel      count = sizeof (symbuf);
808130809Smarcel      nbytes = bfd_bread (symbuf, count, sym_bfd);
809130809Smarcel    }
81019370Spst  else
81119370Spst    {
81219370Spst      if (symbuf_left <= 0)
81319370Spst	{
81419370Spst	  file_ptr filepos = symbuf_sections->section->filepos;
81519370Spst	  if (bfd_seek (sym_bfd, filepos, SEEK_SET) != 0)
81619370Spst	    perror_with_name (bfd_get_filename (sym_bfd));
81719370Spst	  symbuf_left = bfd_section_size (sym_bfd, symbuf_sections->section);
81819370Spst	  symbol_table_offset = filepos - symbuf_read;
81919370Spst	  symbuf_sections = symbuf_sections->next;
82019370Spst	}
82119370Spst
82219370Spst      count = symbuf_left;
82319370Spst      if (count > sizeof (symbuf))
82419370Spst	count = sizeof (symbuf);
825130809Smarcel      nbytes = bfd_bread (symbuf, count, sym_bfd);
82619370Spst    }
82719370Spst
82819370Spst  if (nbytes < 0)
82919370Spst    perror_with_name (bfd_get_filename (sym_bfd));
83019370Spst  else if (nbytes == 0)
83119370Spst    error ("Premature end of file reading symbol table");
83219370Spst  symbuf_end = nbytes / symbol_size;
83319370Spst  symbuf_idx = 0;
83419370Spst  symbuf_left -= nbytes;
83519370Spst  symbuf_read += nbytes;
83619370Spst}
83719370Spst
838130809Smarcelstatic void
839130809Smarcelstabs_seek (int sym_offset)
840130809Smarcel{
841130809Smarcel  if (stabs_data)
842130809Smarcel    {
843130809Smarcel      symbuf_read += sym_offset;
844130809Smarcel      symbuf_left -= sym_offset;
845130809Smarcel    }
846130809Smarcel  else
847130809Smarcel    bfd_seek (symfile_bfd, sym_offset, SEEK_CUR);
848130809Smarcel}
849130809Smarcel
85046283Sdfr#define INTERNALIZE_SYMBOL(intern, extern, abfd)			\
85146283Sdfr  {									\
85246283Sdfr    (intern).n_type = bfd_h_get_8 (abfd, (extern)->e_type);		\
85346283Sdfr    (intern).n_strx = bfd_h_get_32 (abfd, (extern)->e_strx);		\
85446283Sdfr    (intern).n_desc = bfd_h_get_16 (abfd, (extern)->e_desc);  		\
85598948Sobrien    if (bfd_get_sign_extend_vma (abfd))					\
85698948Sobrien      (intern).n_value = bfd_h_get_signed_32 (abfd, (extern)->e_value);	\
85798948Sobrien    else								\
85898948Sobrien      (intern).n_value = bfd_h_get_32 (abfd, (extern)->e_value);	\
85946283Sdfr  }
86046283Sdfr
86119370Spst/* Invariant: The symbol pointed to by symbuf_idx is the first one
86219370Spst   that hasn't been swapped.  Swap the symbol at the same time
86319370Spst   that symbuf_idx is incremented.  */
86419370Spst
86519370Spst/* dbx allows the text of a symbol name to be continued into the
86619370Spst   next symbol name!  When such a continuation is encountered
86719370Spst   (a \ at the end of the text of a name)
86819370Spst   call this function to get the continuation.  */
86919370Spst
87019370Spststatic char *
87198948Sobriendbx_next_symbol_text (struct objfile *objfile)
87219370Spst{
87346283Sdfr  struct internal_nlist nlist;
87446283Sdfr
87519370Spst  if (symbuf_idx == symbuf_end)
87619370Spst    fill_symbuf (symfile_bfd);
87746283Sdfr
87819370Spst  symnum++;
87998948Sobrien  INTERNALIZE_SYMBOL (nlist, &symbuf[symbuf_idx], symfile_bfd);
88019370Spst  OBJSTAT (objfile, n_stabs++);
88146283Sdfr
88246283Sdfr  symbuf_idx++;
88346283Sdfr
88446283Sdfr  return nlist.n_strx + stringtab_global + file_string_table_offset;
88519370Spst}
88619370Spst
88719370Spst/* Initialize the list of bincls to contain none and have some
88819370Spst   allocated.  */
88919370Spst
89019370Spststatic void
89198948Sobrieninit_bincl_list (int number, struct objfile *objfile)
89219370Spst{
89319370Spst  bincls_allocated = number;
89419370Spst  next_bincl = bincl_list = (struct header_file_location *)
89598948Sobrien    xmmalloc (objfile->md, bincls_allocated * sizeof (struct header_file_location));
89619370Spst}
89719370Spst
89819370Spst/* Add a bincl to the list.  */
89919370Spst
90019370Spststatic void
90198948Sobrienadd_bincl_to_list (struct partial_symtab *pst, char *name, int instance)
90219370Spst{
90319370Spst  if (next_bincl >= bincl_list + bincls_allocated)
90419370Spst    {
90519370Spst      int offset = next_bincl - bincl_list;
90619370Spst      bincls_allocated *= 2;
90719370Spst      bincl_list = (struct header_file_location *)
90898948Sobrien	xmrealloc (pst->objfile->md, (char *) bincl_list,
90998948Sobrien		   bincls_allocated * sizeof (struct header_file_location));
91019370Spst      next_bincl = bincl_list + offset;
91119370Spst    }
91219370Spst  next_bincl->pst = pst;
91319370Spst  next_bincl->instance = instance;
91419370Spst  next_bincl++->name = name;
91519370Spst}
91619370Spst
91719370Spst/* Given a name, value pair, find the corresponding
91819370Spst   bincl in the list.  Return the partial symtab associated
91919370Spst   with that header_file_location.  */
92019370Spst
92119370Spststatic struct partial_symtab *
92298948Sobrienfind_corresponding_bincl_psymtab (char *name, int instance)
92319370Spst{
92419370Spst  struct header_file_location *bincl;
92519370Spst
92619370Spst  for (bincl = bincl_list; bincl < next_bincl; bincl++)
92719370Spst    if (bincl->instance == instance
928130809Smarcel	&& strcmp (name, bincl->name) == 0)
92919370Spst      return bincl->pst;
93019370Spst
931130809Smarcel  repeated_header_complaint (name, symnum);
93219370Spst  return (struct partial_symtab *) 0;
93319370Spst}
93419370Spst
93519370Spst/* Free the storage allocated for the bincl list.  */
93619370Spst
93719370Spststatic void
93898948Sobrienfree_bincl_list (struct objfile *objfile)
93919370Spst{
940130809Smarcel  xmfree (objfile->md, bincl_list);
94119370Spst  bincls_allocated = 0;
94219370Spst}
94319370Spst
94498948Sobrienstatic void
94598948Sobriendo_free_bincl_list_cleanup (void *objfile)
94698948Sobrien{
94798948Sobrien  free_bincl_list (objfile);
94898948Sobrien}
94998948Sobrien
95098948Sobrienstatic struct cleanup *
95198948Sobrienmake_cleanup_free_bincl_list (struct objfile *objfile)
95298948Sobrien{
95398948Sobrien  return make_cleanup (do_free_bincl_list_cleanup, objfile);
95498948Sobrien}
95598948Sobrien
95698948Sobrien/* Set namestring based on nlist.  If the string table index is invalid,
95798948Sobrien   give a fake name, and print a single error message per symbol file read,
95898948Sobrien   rather than abort the symbol reading or flood the user with messages.  */
95998948Sobrien
96098948Sobrienstatic char *
96198948Sobrienset_namestring (struct objfile *objfile, struct internal_nlist nlist)
96298948Sobrien{
96398948Sobrien  char *namestring;
96498948Sobrien
96598948Sobrien  if (((unsigned) nlist.n_strx + file_string_table_offset) >=
96698948Sobrien      DBX_STRINGTAB_SIZE (objfile))
96798948Sobrien    {
968130809Smarcel      complaint (&symfile_complaints, "bad string table offset in symbol %d",
969130809Smarcel		 symnum);
97098948Sobrien      namestring = "<bad string table offset>";
97198948Sobrien    }
97298948Sobrien  else
97398948Sobrien    namestring = nlist.n_strx + file_string_table_offset +
97498948Sobrien      DBX_STRINGTAB (objfile);
97598948Sobrien  return namestring;
97698948Sobrien}
97798948Sobrien
97819370Spst/* Scan a SunOs dynamic symbol table for symbols of interest and
97919370Spst   add them to the minimal symbol table.  */
98019370Spst
98119370Spststatic void
98298948Sobrienread_dbx_dynamic_symtab (struct objfile *objfile)
98319370Spst{
98419370Spst  bfd *abfd = objfile->obfd;
98519370Spst  struct cleanup *back_to;
98619370Spst  int counter;
98719370Spst  long dynsym_size;
98819370Spst  long dynsym_count;
98919370Spst  asymbol **dynsyms;
99019370Spst  asymbol **symptr;
99119370Spst  arelent **relptr;
99219370Spst  long dynrel_size;
99319370Spst  long dynrel_count;
99419370Spst  arelent **dynrels;
99519370Spst  CORE_ADDR sym_value;
99619370Spst  char *name;
99719370Spst
99819370Spst  /* Check that the symbol file has dynamic symbols that we know about.
99919370Spst     bfd_arch_unknown can happen if we are reading a sun3 symbol file
100019370Spst     on a sun4 host (and vice versa) and bfd is not configured
100119370Spst     --with-target=all.  This would trigger an assertion in bfd/sunos.c,
100219370Spst     so we ignore the dynamic symbols in this case.  */
100319370Spst  if (bfd_get_flavour (abfd) != bfd_target_aout_flavour
100419370Spst      || (bfd_get_file_flags (abfd) & DYNAMIC) == 0
100519370Spst      || bfd_get_arch (abfd) == bfd_arch_unknown)
100619370Spst    return;
100719370Spst
100819370Spst  dynsym_size = bfd_get_dynamic_symtab_upper_bound (abfd);
100919370Spst  if (dynsym_size < 0)
101019370Spst    return;
101119370Spst
101219370Spst  dynsyms = (asymbol **) xmalloc (dynsym_size);
101398948Sobrien  back_to = make_cleanup (xfree, dynsyms);
101419370Spst
101519370Spst  dynsym_count = bfd_canonicalize_dynamic_symtab (abfd, dynsyms);
101619370Spst  if (dynsym_count < 0)
101719370Spst    {
101819370Spst      do_cleanups (back_to);
101919370Spst      return;
102019370Spst    }
102119370Spst
102219370Spst  /* Enter dynamic symbols into the minimal symbol table
102319370Spst     if this is a stripped executable.  */
102419370Spst  if (bfd_get_symcount (abfd) <= 0)
102519370Spst    {
102619370Spst      symptr = dynsyms;
102719370Spst      for (counter = 0; counter < dynsym_count; counter++, symptr++)
102819370Spst	{
102919370Spst	  asymbol *sym = *symptr;
103019370Spst	  asection *sec;
103119370Spst	  int type;
103219370Spst
103319370Spst	  sec = bfd_get_section (sym);
103419370Spst
103519370Spst	  /* BFD symbols are section relative.  */
103619370Spst	  sym_value = sym->value + sec->vma;
103719370Spst
103819370Spst	  if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
103919370Spst	    {
104098948Sobrien	      sym_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
104119370Spst	      type = N_TEXT;
104219370Spst	    }
104319370Spst	  else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
104419370Spst	    {
104598948Sobrien	      sym_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
104619370Spst	      type = N_DATA;
104719370Spst	    }
104819370Spst	  else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
104919370Spst	    {
105098948Sobrien	      sym_value += ANOFFSET (objfile->section_offsets, SECT_OFF_BSS (objfile));
105119370Spst	      type = N_BSS;
105219370Spst	    }
105319370Spst	  else
105419370Spst	    continue;
105519370Spst
105619370Spst	  if (sym->flags & BSF_GLOBAL)
105719370Spst	    type |= N_EXT;
105819370Spst
105919370Spst	  record_minimal_symbol ((char *) bfd_asymbol_name (sym), sym_value,
106019370Spst				 type, objfile);
106119370Spst	}
106219370Spst    }
106319370Spst
106419370Spst  /* Symbols from shared libraries have a dynamic relocation entry
106519370Spst     that points to the associated slot in the procedure linkage table.
106619370Spst     We make a mininal symbol table entry with type mst_solib_trampoline
106719370Spst     at the address in the procedure linkage table.  */
106819370Spst  dynrel_size = bfd_get_dynamic_reloc_upper_bound (abfd);
106919370Spst  if (dynrel_size < 0)
107019370Spst    {
107119370Spst      do_cleanups (back_to);
107219370Spst      return;
107319370Spst    }
107498948Sobrien
107519370Spst  dynrels = (arelent **) xmalloc (dynrel_size);
107698948Sobrien  make_cleanup (xfree, dynrels);
107719370Spst
107819370Spst  dynrel_count = bfd_canonicalize_dynamic_reloc (abfd, dynrels, dynsyms);
107919370Spst  if (dynrel_count < 0)
108019370Spst    {
108119370Spst      do_cleanups (back_to);
108219370Spst      return;
108319370Spst    }
108419370Spst
108519370Spst  for (counter = 0, relptr = dynrels;
108619370Spst       counter < dynrel_count;
108719370Spst       counter++, relptr++)
108819370Spst    {
108919370Spst      arelent *rel = *relptr;
109019370Spst      CORE_ADDR address =
109198948Sobrien      rel->address + ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
109219370Spst
109319370Spst      switch (bfd_get_arch (abfd))
109419370Spst	{
109519370Spst	case bfd_arch_sparc:
109619370Spst	  if (rel->howto->type != RELOC_JMP_SLOT)
109719370Spst	    continue;
109819370Spst	  break;
109919370Spst	case bfd_arch_m68k:
110019370Spst	  /* `16' is the type BFD produces for a jump table relocation.  */
110119370Spst	  if (rel->howto->type != 16)
110219370Spst	    continue;
110319370Spst
110419370Spst	  /* Adjust address in the jump table to point to
110519370Spst	     the start of the bsr instruction.  */
110619370Spst	  address -= 2;
110719370Spst	  break;
110819370Spst	default:
110919370Spst	  continue;
111019370Spst	}
111119370Spst
111219370Spst      name = (char *) bfd_asymbol_name (*rel->sym_ptr_ptr);
111346283Sdfr      prim_record_minimal_symbol (name, address, mst_solib_trampoline,
111446283Sdfr				  objfile);
111519370Spst    }
111619370Spst
111719370Spst  do_cleanups (back_to);
111819370Spst}
111919370Spst
1120130809Smarcel#ifdef SOFUN_ADDRESS_MAYBE_MISSING
1121130809Smarcelstatic CORE_ADDR
1122130809Smarcelfind_stab_function_addr (char *namestring, char *filename,
1123130809Smarcel			 struct objfile *objfile)
1124130809Smarcel{
1125130809Smarcel  struct minimal_symbol *msym;
1126130809Smarcel  char *p;
1127130809Smarcel  int n;
1128130809Smarcel
1129130809Smarcel  p = strchr (namestring, ':');
1130130809Smarcel  if (p == NULL)
1131130809Smarcel    p = namestring;
1132130809Smarcel  n = p - namestring;
1133130809Smarcel  p = alloca (n + 2);
1134130809Smarcel  strncpy (p, namestring, n);
1135130809Smarcel  p[n] = 0;
1136130809Smarcel
1137130809Smarcel  msym = lookup_minimal_symbol (p, filename, objfile);
1138130809Smarcel  if (msym == NULL)
1139130809Smarcel    {
1140130809Smarcel      /* Sun Fortran appends an underscore to the minimal symbol name,
1141130809Smarcel         try again with an appended underscore if the minimal symbol
1142130809Smarcel         was not found.  */
1143130809Smarcel      p[n] = '_';
1144130809Smarcel      p[n + 1] = 0;
1145130809Smarcel      msym = lookup_minimal_symbol (p, filename, objfile);
1146130809Smarcel    }
1147130809Smarcel
1148130809Smarcel  if (msym == NULL && filename != NULL)
1149130809Smarcel    {
1150130809Smarcel      /* Try again without the filename. */
1151130809Smarcel      p[n] = 0;
1152130809Smarcel      msym = lookup_minimal_symbol (p, NULL, objfile);
1153130809Smarcel    }
1154130809Smarcel  if (msym == NULL && filename != NULL)
1155130809Smarcel    {
1156130809Smarcel      /* And try again for Sun Fortran, but without the filename. */
1157130809Smarcel      p[n] = '_';
1158130809Smarcel      p[n + 1] = 0;
1159130809Smarcel      msym = lookup_minimal_symbol (p, NULL, objfile);
1160130809Smarcel    }
1161130809Smarcel
1162130809Smarcel  return msym == NULL ? 0 : SYMBOL_VALUE_ADDRESS (msym);
1163130809Smarcel}
1164130809Smarcel#endif /* SOFUN_ADDRESS_MAYBE_MISSING */
1165130809Smarcel
1166130809Smarcelstatic void
1167130809Smarcelfunction_outside_compilation_unit_complaint (const char *arg1)
1168130809Smarcel{
1169130809Smarcel  complaint (&symfile_complaints,
1170130809Smarcel	     "function `%s' appears to be defined outside of all compilation units",
1171130809Smarcel	     arg1);
1172130809Smarcel}
1173130809Smarcel
117498948Sobrien/* Setup partial_symtab's describing each source file for which
117598948Sobrien   debugging information is available. */
117619370Spst
117719370Spststatic void
117898948Sobrienread_dbx_symtab (struct objfile *objfile)
117919370Spst{
1180130809Smarcel  struct external_nlist *bufp = 0;	/* =0 avoids gcc -Wall glitch */
118146283Sdfr  struct internal_nlist nlist;
118298948Sobrien  CORE_ADDR text_addr;
118398948Sobrien  int text_size;
118446283Sdfr
1185130809Smarcel  char *namestring;
118619370Spst  int nsl;
118719370Spst  int past_first_source_file = 0;
118819370Spst  CORE_ADDR last_o_file_start = 0;
118919370Spst  CORE_ADDR last_function_start = 0;
119019370Spst  struct cleanup *back_to;
119119370Spst  bfd *abfd;
119246283Sdfr  int textlow_not_set;
1193130809Smarcel  int data_sect_index;
119419370Spst
119519370Spst  /* Current partial symtab */
119619370Spst  struct partial_symtab *pst;
119719370Spst
119819370Spst  /* List of current psymtab's include files */
119919370Spst  char **psymtab_include_list;
120019370Spst  int includes_allocated;
120119370Spst  int includes_used;
120219370Spst
120319370Spst  /* Index within current psymtab dependency list */
120419370Spst  struct partial_symtab **dependency_list;
120519370Spst  int dependencies_used, dependencies_allocated;
120619370Spst
120798948Sobrien  text_addr = DBX_TEXT_ADDR (objfile);
120898948Sobrien  text_size = DBX_TEXT_SIZE (objfile);
120998948Sobrien
121019370Spst  /* FIXME.  We probably want to change stringtab_global rather than add this
121119370Spst     while processing every symbol entry.  FIXME.  */
121219370Spst  file_string_table_offset = 0;
121319370Spst  next_file_string_table_offset = 0;
121419370Spst
121519370Spst  stringtab_global = DBX_STRINGTAB (objfile);
121698948Sobrien
121719370Spst  pst = (struct partial_symtab *) 0;
121819370Spst
121919370Spst  includes_allocated = 30;
122019370Spst  includes_used = 0;
122119370Spst  psymtab_include_list = (char **) alloca (includes_allocated *
122219370Spst					   sizeof (char *));
122319370Spst
122419370Spst  dependencies_allocated = 30;
122519370Spst  dependencies_used = 0;
122619370Spst  dependency_list =
122719370Spst    (struct partial_symtab **) alloca (dependencies_allocated *
122819370Spst				       sizeof (struct partial_symtab *));
122919370Spst
123019370Spst  /* Init bincl list */
123119370Spst  init_bincl_list (20, objfile);
123298948Sobrien  back_to = make_cleanup_free_bincl_list (objfile);
123319370Spst
123419370Spst  last_source_file = NULL;
123519370Spst
123698948Sobrien  lowest_text_address = (CORE_ADDR) -1;
123719370Spst
123819370Spst  symfile_bfd = objfile->obfd;	/* For next_text_symbol */
123919370Spst  abfd = objfile->obfd;
124019370Spst  symbuf_end = symbuf_idx = 0;
124119370Spst  next_symbol_text_func = dbx_next_symbol_text;
124246283Sdfr  textlow_not_set = 1;
124346283Sdfr  has_line_numbers = 0;
124419370Spst
1245130809Smarcel  /* FIXME: jimb/2003-09-12: We don't apply the right section's offset
1246130809Smarcel     to global and static variables.  The stab for a global or static
1247130809Smarcel     variable doesn't give us any indication of which section it's in,
1248130809Smarcel     so we can't tell immediately which offset in
1249130809Smarcel     objfile->section_offsets we should apply to the variable's
1250130809Smarcel     address.
1251130809Smarcel
1252130809Smarcel     We could certainly find out which section contains the variable
1253130809Smarcel     by looking up the variable's unrelocated address with
1254130809Smarcel     find_pc_section, but that would be expensive; this is the
1255130809Smarcel     function that constructs the partial symbol tables by examining
1256130809Smarcel     every symbol in the entire executable, and it's
1257130809Smarcel     performance-critical.  So that expense would not be welcome.  I'm
1258130809Smarcel     not sure what to do about this at the moment.
1259130809Smarcel
1260130809Smarcel     What we have done for years is to simply assume that the .data
1261130809Smarcel     section's offset is appropriate for all global and static
1262130809Smarcel     variables.  Recently, this was expanded to fall back to the .bss
1263130809Smarcel     section's offset if there is no .data section, and then to the
1264130809Smarcel     .rodata section's offset.  */
1265130809Smarcel  data_sect_index = objfile->sect_index_data;
1266130809Smarcel  if (data_sect_index == -1)
1267130809Smarcel    data_sect_index = SECT_OFF_BSS (objfile);
1268130809Smarcel  if (data_sect_index == -1)
1269130809Smarcel    data_sect_index = SECT_OFF_RODATA (objfile);
1270130809Smarcel
1271130809Smarcel  /* If data_sect_index is still -1, that's okay.  It's perfectly fine
1272130809Smarcel     for the file to have no .data, no .bss, and no .text at all, if
1273130809Smarcel     it also has no global or static variables.  If it does, we will
1274130809Smarcel     get an internal error from an ANOFFSET macro below when we try to
1275130809Smarcel     use data_sect_index.  */
1276130809Smarcel
127719370Spst  for (symnum = 0; symnum < DBX_SYMCOUNT (objfile); symnum++)
127819370Spst    {
127919370Spst      /* Get the symbol for this run and pull out some info */
128098948Sobrien      QUIT;			/* allow this to be interruptable */
128119370Spst      if (symbuf_idx == symbuf_end)
128219370Spst	fill_symbuf (abfd);
128319370Spst      bufp = &symbuf[symbuf_idx++];
128419370Spst
128519370Spst      /*
128619370Spst       * Special case to speed up readin.
128719370Spst       */
128846283Sdfr      if (bfd_h_get_8 (abfd, bufp->e_type) == N_SLINE)
128946283Sdfr	{
129046283Sdfr	  has_line_numbers = 1;
129146283Sdfr	  continue;
129246283Sdfr	}
129319370Spst
129446283Sdfr      INTERNALIZE_SYMBOL (nlist, bufp, abfd);
129519370Spst      OBJSTAT (objfile, n_stabs++);
129619370Spst
129719370Spst      /* Ok.  There is a lot of code duplicated in the rest of this
129819370Spst         switch statement (for efficiency reasons).  Since I don't
129919370Spst         like duplicating code, I will do my penance here, and
130019370Spst         describe the code which is duplicated:
130119370Spst
130298948Sobrien         *) The assignment to namestring.
130398948Sobrien         *) The call to strchr.
130498948Sobrien         *) The addition of a partial symbol the the two partial
130598948Sobrien         symbol lists.  This last is a large section of code, so
130698948Sobrien         I've imbedded it in the following macro.
130798948Sobrien      */
130819370Spst
130998948Sobrien      switch (nlist.n_type)
131098948Sobrien	{
131198948Sobrien	  char *p;
131298948Sobrien	  /*
131398948Sobrien	   * Standard, external, non-debugger, symbols
131498948Sobrien	   */
131519370Spst
131698948Sobrien	  case N_TEXT | N_EXT:
131798948Sobrien	  case N_NBTEXT | N_EXT:
131898948Sobrien	  nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
131998948Sobrien	  goto record_it;
132019370Spst
132198948Sobrien	  case N_DATA | N_EXT:
132298948Sobrien	  case N_NBDATA | N_EXT:
132398948Sobrien	  nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
132498948Sobrien	  goto record_it;
132598948Sobrien
132698948Sobrien	  case N_BSS:
132798948Sobrien	  case N_BSS | N_EXT:
132898948Sobrien	  case N_NBBSS | N_EXT:
132998948Sobrien	  case N_SETV | N_EXT:		/* FIXME, is this in BSS? */
133098948Sobrien	  nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_BSS (objfile));
133198948Sobrien	  goto record_it;
133298948Sobrien
133398948Sobrien	  case N_ABS | N_EXT:
133498948Sobrien	  record_it:
133598948Sobrien	  namestring = set_namestring (objfile, nlist);
133698948Sobrien
133798948Sobrien	  bss_ext_symbol:
133898948Sobrien	  record_minimal_symbol (namestring, nlist.n_value,
133998948Sobrien				 nlist.n_type, objfile);	/* Always */
134098948Sobrien	  continue;
134198948Sobrien
134298948Sobrien	  /* Standard, local, non-debugger, symbols */
134398948Sobrien
134498948Sobrien	  case N_NBTEXT:
134598948Sobrien
134698948Sobrien	  /* We need to be able to deal with both N_FN or N_TEXT,
134798948Sobrien	     because we have no way of knowing whether the sys-supplied ld
134898948Sobrien	     or GNU ld was used to make the executable.  Sequents throw
134998948Sobrien	     in another wrinkle -- they renumbered N_FN.  */
135098948Sobrien
135198948Sobrien	  case N_FN:
135298948Sobrien	  case N_FN_SEQ:
135398948Sobrien	  case N_TEXT:
135498948Sobrien	  nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
135598948Sobrien	  namestring = set_namestring (objfile, nlist);
135698948Sobrien
135798948Sobrien	  if ((namestring[0] == '-' && namestring[1] == 'l')
135898948Sobrien	      || (namestring[(nsl = strlen (namestring)) - 1] == 'o'
135998948Sobrien		  && namestring[nsl - 2] == '.'))
136098948Sobrien	  {
136198948Sobrien	    if (objfile->ei.entry_point < nlist.n_value &&
136298948Sobrien		objfile->ei.entry_point >= last_o_file_start)
136398948Sobrien	      {
1364130809Smarcel		objfile->ei.deprecated_entry_file_lowpc = last_o_file_start;
1365130809Smarcel		objfile->ei.deprecated_entry_file_highpc = nlist.n_value;
136698948Sobrien	      }
136798948Sobrien	    if (past_first_source_file && pst
136898948Sobrien		/* The gould NP1 uses low values for .o and -l symbols
136998948Sobrien		   which are not the address.  */
1370130809Smarcel		&& nlist.n_value >= pst->textlow)
137198948Sobrien	      {
137298948Sobrien		end_psymtab (pst, psymtab_include_list, includes_used,
137398948Sobrien			     symnum * symbol_size,
1374130809Smarcel			     nlist.n_value > pst->texthigh
1375130809Smarcel			     ? nlist.n_value : pst->texthigh,
137698948Sobrien			     dependency_list, dependencies_used, textlow_not_set);
137798948Sobrien		pst = (struct partial_symtab *) 0;
137898948Sobrien		includes_used = 0;
137998948Sobrien		dependencies_used = 0;
138098948Sobrien	      }
138198948Sobrien	    else
138298948Sobrien	      past_first_source_file = 1;
138398948Sobrien	    last_o_file_start = nlist.n_value;
138498948Sobrien	  }
138598948Sobrien	  else
138698948Sobrien	  goto record_it;
138798948Sobrien	  continue;
138898948Sobrien
138998948Sobrien	  case N_DATA:
139098948Sobrien	  nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_DATA (objfile));
139198948Sobrien	  goto record_it;
139298948Sobrien
139398948Sobrien	  case N_UNDF | N_EXT:
139498948Sobrien	  if (nlist.n_value != 0)
139598948Sobrien	  {
139698948Sobrien	    /* This is a "Fortran COMMON" symbol.  See if the target
139798948Sobrien	       environment knows where it has been relocated to.  */
139898948Sobrien
139998948Sobrien	    CORE_ADDR reladdr;
140098948Sobrien
140198948Sobrien	    namestring = set_namestring (objfile, nlist);
140298948Sobrien	    if (target_lookup_symbol (namestring, &reladdr))
140398948Sobrien	      {
140498948Sobrien		continue;		/* Error in lookup; ignore symbol for now.  */
140598948Sobrien	      }
140698948Sobrien	    nlist.n_type ^= (N_BSS ^ N_UNDF);	/* Define it as a bss-symbol */
140798948Sobrien	    nlist.n_value = reladdr;
140898948Sobrien	    goto bss_ext_symbol;
140998948Sobrien	  }
141098948Sobrien	  continue;			/* Just undefined, not COMMON */
141198948Sobrien
141298948Sobrien	  case N_UNDF:
141398948Sobrien	  if (processing_acc_compilation && nlist.n_strx == 1)
141498948Sobrien	  {
141598948Sobrien	    /* Deal with relative offsets in the string table
141698948Sobrien	       used in ELF+STAB under Solaris.  If we want to use the
141798948Sobrien	       n_strx field, which contains the name of the file,
141898948Sobrien	       we must adjust file_string_table_offset *before* calling
141998948Sobrien	       set_namestring().  */
142098948Sobrien	    past_first_source_file = 1;
142198948Sobrien	    file_string_table_offset = next_file_string_table_offset;
142298948Sobrien	    next_file_string_table_offset =
142398948Sobrien	      file_string_table_offset + nlist.n_value;
142498948Sobrien	    if (next_file_string_table_offset < file_string_table_offset)
142598948Sobrien	      error ("string table offset backs up at %d", symnum);
142698948Sobrien	    /* FIXME -- replace error() with complaint.  */
142798948Sobrien	    continue;
142898948Sobrien	  }
142998948Sobrien	  continue;
143098948Sobrien
143198948Sobrien	  /* Lots of symbol types we can just ignore.  */
143298948Sobrien
143398948Sobrien	  case N_ABS:
143498948Sobrien	  case N_NBDATA:
143598948Sobrien	  case N_NBBSS:
143698948Sobrien	  continue;
143798948Sobrien
143898948Sobrien	  /* Keep going . . . */
143998948Sobrien
144098948Sobrien	  /*
144198948Sobrien	   * Special symbol types for GNU
144298948Sobrien	   */
144398948Sobrien	  case N_INDR:
144498948Sobrien	  case N_INDR | N_EXT:
144598948Sobrien	  case N_SETA:
144698948Sobrien	  case N_SETA | N_EXT:
144798948Sobrien	  case N_SETT:
144898948Sobrien	  case N_SETT | N_EXT:
144998948Sobrien	  case N_SETD:
145098948Sobrien	  case N_SETD | N_EXT:
145198948Sobrien	  case N_SETB:
145298948Sobrien	  case N_SETB | N_EXT:
145398948Sobrien	  case N_SETV:
145498948Sobrien	  continue;
145598948Sobrien
145698948Sobrien	  /*
145798948Sobrien	   * Debugger symbols
145898948Sobrien	   */
145998948Sobrien
146098948Sobrien	  case N_SO:
146198948Sobrien	  {
146298948Sobrien	    CORE_ADDR valu;
146398948Sobrien	    static int prev_so_symnum = -10;
146498948Sobrien	    static int first_so_symnum;
146598948Sobrien	    char *p;
146698948Sobrien	    int prev_textlow_not_set;
146798948Sobrien
146898948Sobrien	    valu = nlist.n_value + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
146998948Sobrien
147098948Sobrien	    prev_textlow_not_set = textlow_not_set;
147198948Sobrien
147298948Sobrien#ifdef SOFUN_ADDRESS_MAYBE_MISSING
147398948Sobrien	    /* A zero value is probably an indication for the SunPRO 3.0
147498948Sobrien	       compiler. end_psymtab explicitly tests for zero, so
147598948Sobrien	       don't relocate it.  */
147698948Sobrien
147798948Sobrien	    if (nlist.n_value == 0)
147898948Sobrien	      {
147998948Sobrien		textlow_not_set = 1;
148098948Sobrien		valu = 0;
148198948Sobrien	      }
148298948Sobrien	    else
148398948Sobrien	      textlow_not_set = 0;
148498948Sobrien#else
148598948Sobrien	    textlow_not_set = 0;
148698948Sobrien#endif
148798948Sobrien	    past_first_source_file = 1;
148898948Sobrien
148998948Sobrien	    if (prev_so_symnum != symnum - 1)
149098948Sobrien	      {			/* Here if prev stab wasn't N_SO */
149198948Sobrien		first_so_symnum = symnum;
149298948Sobrien
149398948Sobrien		if (pst)
149498948Sobrien		  {
149598948Sobrien		    end_psymtab (pst, psymtab_include_list, includes_used,
149698948Sobrien				 symnum * symbol_size,
1497130809Smarcel				 valu > pst->texthigh ? valu : pst->texthigh,
149898948Sobrien				 dependency_list, dependencies_used,
149998948Sobrien				 prev_textlow_not_set);
150098948Sobrien		    pst = (struct partial_symtab *) 0;
150198948Sobrien		    includes_used = 0;
150298948Sobrien		    dependencies_used = 0;
150398948Sobrien		  }
150498948Sobrien	      }
150598948Sobrien
150698948Sobrien	    prev_so_symnum = symnum;
150798948Sobrien
150898948Sobrien	    /* End the current partial symtab and start a new one */
150998948Sobrien
151098948Sobrien	    namestring = set_namestring (objfile, nlist);
151198948Sobrien
151298948Sobrien	    /* Null name means end of .o file.  Don't start a new one. */
151398948Sobrien	    if (*namestring == '\000')
151498948Sobrien	      continue;
151598948Sobrien
151698948Sobrien	    /* Some compilers (including gcc) emit a pair of initial N_SOs.
151798948Sobrien	       The first one is a directory name; the second the file name.
151898948Sobrien	       If pst exists, is empty, and has a filename ending in '/',
151998948Sobrien	       we assume the previous N_SO was a directory name. */
152098948Sobrien
152198948Sobrien	    p = strrchr (namestring, '/');
152298948Sobrien	    if (p && *(p + 1) == '\000')
152398948Sobrien	      continue;		/* Simply ignore directory name SOs */
152498948Sobrien
152598948Sobrien	    /* Some other compilers (C++ ones in particular) emit useless
152698948Sobrien	       SOs for non-existant .c files.  We ignore all subsequent SOs that
152798948Sobrien	       immediately follow the first.  */
152898948Sobrien
152998948Sobrien	    if (!pst)
153098948Sobrien	      pst = start_psymtab (objfile,
153198948Sobrien				   namestring, valu,
153298948Sobrien				   first_so_symnum * symbol_size,
153398948Sobrien				   objfile->global_psymbols.next,
153498948Sobrien				   objfile->static_psymbols.next);
153598948Sobrien	    continue;
153698948Sobrien	  }
153798948Sobrien
153898948Sobrien	  case N_BINCL:
153998948Sobrien	  {
154098948Sobrien	    enum language tmp_language;
154198948Sobrien	    /* Add this bincl to the bincl_list for future EXCLs.  No
154298948Sobrien	       need to save the string; it'll be around until
154398948Sobrien	       read_dbx_symtab function returns */
154498948Sobrien
154598948Sobrien	    namestring = set_namestring (objfile, nlist);
154698948Sobrien	    tmp_language = deduce_language_from_filename (namestring);
154798948Sobrien
154898948Sobrien	    /* Only change the psymtab's language if we've learned
154998948Sobrien	       something useful (eg. tmp_language is not language_unknown).
155098948Sobrien	       In addition, to match what start_subfile does, never change
155198948Sobrien	       from C++ to C.  */
155298948Sobrien	    if (tmp_language != language_unknown
155398948Sobrien		&& (tmp_language != language_c
155498948Sobrien		    || psymtab_language != language_cplus))
155598948Sobrien	    psymtab_language = tmp_language;
155698948Sobrien
155798948Sobrien	    if (pst == NULL)
155898948Sobrien	    {
155998948Sobrien	      /* FIXME: we should not get here without a PST to work on.
156098948Sobrien		 Attempt to recover.  */
1561130809Smarcel	      complaint (&symfile_complaints,
1562130809Smarcel			 "N_BINCL %s not in entries for any file, at symtab pos %d",
1563130809Smarcel			 namestring, symnum);
156498948Sobrien	      continue;
156598948Sobrien	    }
156698948Sobrien	    add_bincl_to_list (pst, namestring, nlist.n_value);
156798948Sobrien
156898948Sobrien	    /* Mark down an include file in the current psymtab */
156998948Sobrien
157098948Sobrien	    goto record_include_file;
157198948Sobrien	  }
157298948Sobrien
157398948Sobrien	  case N_SOL:
157498948Sobrien	  {
157598948Sobrien	    enum language tmp_language;
157698948Sobrien	    /* Mark down an include file in the current psymtab */
157798948Sobrien
157898948Sobrien	    namestring = set_namestring (objfile, nlist);
157998948Sobrien	    tmp_language = deduce_language_from_filename (namestring);
158098948Sobrien
158198948Sobrien	    /* Only change the psymtab's language if we've learned
158298948Sobrien	       something useful (eg. tmp_language is not language_unknown).
158398948Sobrien	       In addition, to match what start_subfile does, never change
158498948Sobrien	       from C++ to C.  */
158598948Sobrien	    if (tmp_language != language_unknown
158698948Sobrien		&& (tmp_language != language_c
158798948Sobrien		    || psymtab_language != language_cplus))
158898948Sobrien	    psymtab_language = tmp_language;
158998948Sobrien
159098948Sobrien	    /* In C++, one may expect the same filename to come round many
159198948Sobrien	       times, when code is coming alternately from the main file
159298948Sobrien	       and from inline functions in other files. So I check to see
159398948Sobrien	       if this is a file we've seen before -- either the main
159498948Sobrien	       source file, or a previously included file.
159598948Sobrien
159698948Sobrien	       This seems to be a lot of time to be spending on N_SOL, but
159798948Sobrien	       things like "break c-exp.y:435" need to work (I
159898948Sobrien	       suppose the psymtab_include_list could be hashed or put
159998948Sobrien	       in a binary tree, if profiling shows this is a major hog).  */
1600130809Smarcel	    if (pst && strcmp (namestring, pst->filename) == 0)
160198948Sobrien	    continue;
160298948Sobrien	    {
1603130809Smarcel	      int i;
160498948Sobrien	      for (i = 0; i < includes_used; i++)
1605130809Smarcel		if (strcmp (namestring, psymtab_include_list[i]) == 0)
160698948Sobrien		  {
160798948Sobrien		    i = -1;
160898948Sobrien		    break;
160998948Sobrien		  }
161098948Sobrien	      if (i == -1)
161198948Sobrien		continue;
161298948Sobrien	    }
161398948Sobrien
161498948Sobrien	    record_include_file:
161598948Sobrien
161698948Sobrien	    psymtab_include_list[includes_used++] = namestring;
161798948Sobrien	    if (includes_used >= includes_allocated)
161898948Sobrien	    {
161998948Sobrien	      char **orig = psymtab_include_list;
162098948Sobrien
162198948Sobrien	      psymtab_include_list = (char **)
162298948Sobrien		alloca ((includes_allocated *= 2) *
162398948Sobrien			sizeof (char *));
1624130809Smarcel	      memcpy (psymtab_include_list, orig,
162598948Sobrien		      includes_used * sizeof (char *));
162698948Sobrien	    }
162798948Sobrien	    continue;
162898948Sobrien	  }
162998948Sobrien	  case N_LSYM:			/* Typedef or automatic variable. */
163098948Sobrien	  case N_STSYM:		/* Data seg var -- static  */
163198948Sobrien	  case N_LCSYM:		/* BSS      "  */
163298948Sobrien	  case N_ROSYM:		/* Read-only data seg var -- static.  */
163398948Sobrien	  case N_NBSTS:		/* Gould nobase.  */
163498948Sobrien	  case N_NBLCS:		/* symbols.  */
163598948Sobrien	  case N_FUN:
163698948Sobrien	  case N_GSYM:			/* Global (extern) variable; can be
163798948Sobrien					   data or bss (sigh FIXME).  */
163898948Sobrien
163998948Sobrien	  /* Following may probably be ignored; I'll leave them here
164098948Sobrien	     for now (until I do Pascal and Modula 2 extensions).  */
164198948Sobrien
164298948Sobrien	  case N_PC:			/* I may or may not need this; I
164398948Sobrien					   suspect not.  */
164498948Sobrien	  case N_M2C:			/* I suspect that I can ignore this here. */
164598948Sobrien	  case N_SCOPE:		/* Same.   */
164698948Sobrien
164798948Sobrien	  namestring = set_namestring (objfile, nlist);
164898948Sobrien
164998948Sobrien	  /* See if this is an end of function stab.  */
165098948Sobrien	  if (pst && nlist.n_type == N_FUN && *namestring == '\000')
165198948Sobrien	  {
165298948Sobrien	    CORE_ADDR valu;
165398948Sobrien
165498948Sobrien	    /* It's value is the size (in bytes) of the function for
165598948Sobrien	       function relative stabs, or the address of the function's
165698948Sobrien	       end for old style stabs.  */
165798948Sobrien	    valu = nlist.n_value + last_function_start;
1658130809Smarcel	    if (pst->texthigh == 0 || valu > pst->texthigh)
1659130809Smarcel	      pst->texthigh = valu;
166098948Sobrien	    break;
166198948Sobrien	  }
166298948Sobrien
166398948Sobrien	  p = (char *) strchr (namestring, ':');
166498948Sobrien	  if (!p)
166598948Sobrien	  continue;			/* Not a debugging symbol.   */
166698948Sobrien
166798948Sobrien
166898948Sobrien
166998948Sobrien	  /* Main processing section for debugging symbols which
167098948Sobrien	     the initial read through the symbol tables needs to worry
167198948Sobrien	     about.  If we reach this point, the symbol which we are
167298948Sobrien	     considering is definitely one we are interested in.
167398948Sobrien	     p must also contain the (valid) index into the namestring
167498948Sobrien	     which indicates the debugging type symbol.  */
167598948Sobrien
167698948Sobrien	  switch (p[1])
167798948Sobrien	  {
167898948Sobrien	  case 'S':
1679130809Smarcel	    nlist.n_value += ANOFFSET (objfile->section_offsets, data_sect_index);
168098948Sobrien#ifdef STATIC_TRANSFORM_NAME
168198948Sobrien	    namestring = STATIC_TRANSFORM_NAME (namestring);
168298948Sobrien#endif
168398948Sobrien	    add_psymbol_to_list (namestring, p - namestring,
1684130809Smarcel				 VAR_DOMAIN, LOC_STATIC,
168598948Sobrien				 &objfile->static_psymbols,
168698948Sobrien				 0, nlist.n_value,
168798948Sobrien				 psymtab_language, objfile);
168898948Sobrien	    continue;
168998948Sobrien	  case 'G':
1690130809Smarcel	    nlist.n_value += ANOFFSET (objfile->section_offsets, data_sect_index);
169198948Sobrien	    /* The addresses in these entries are reported to be
169298948Sobrien	       wrong.  See the code that reads 'G's for symtabs. */
169398948Sobrien	    add_psymbol_to_list (namestring, p - namestring,
1694130809Smarcel				 VAR_DOMAIN, LOC_STATIC,
169598948Sobrien				 &objfile->global_psymbols,
169698948Sobrien				 0, nlist.n_value,
169798948Sobrien				 psymtab_language, objfile);
169898948Sobrien	    continue;
169998948Sobrien
170098948Sobrien	  case 'T':
170198948Sobrien	    /* When a 'T' entry is defining an anonymous enum, it
170298948Sobrien	       may have a name which is the empty string, or a
170398948Sobrien	       single space.  Since they're not really defining a
170498948Sobrien	       symbol, those shouldn't go in the partial symbol
170598948Sobrien	       table.  We do pick up the elements of such enums at
170698948Sobrien	       'check_enum:', below.  */
170798948Sobrien	    if (p >= namestring + 2
170898948Sobrien		|| (p == namestring + 1
170998948Sobrien		    && namestring[0] != ' '))
171098948Sobrien	      {
171198948Sobrien		add_psymbol_to_list (namestring, p - namestring,
1712130809Smarcel				     STRUCT_DOMAIN, LOC_TYPEDEF,
171398948Sobrien				     &objfile->static_psymbols,
171498948Sobrien				     nlist.n_value, 0,
171598948Sobrien				     psymtab_language, objfile);
171698948Sobrien		if (p[2] == 't')
171798948Sobrien		  {
171898948Sobrien		    /* Also a typedef with the same name.  */
171998948Sobrien		    add_psymbol_to_list (namestring, p - namestring,
1720130809Smarcel					 VAR_DOMAIN, LOC_TYPEDEF,
172198948Sobrien					 &objfile->static_psymbols,
172298948Sobrien					 nlist.n_value, 0,
172398948Sobrien					 psymtab_language, objfile);
172498948Sobrien		    p += 1;
172598948Sobrien		  }
172698948Sobrien	      }
172798948Sobrien	    goto check_enum;
172898948Sobrien	  case 't':
172998948Sobrien	    if (p != namestring)	/* a name is there, not just :T... */
173098948Sobrien	      {
173198948Sobrien		add_psymbol_to_list (namestring, p - namestring,
1732130809Smarcel				     VAR_DOMAIN, LOC_TYPEDEF,
173398948Sobrien				     &objfile->static_psymbols,
173498948Sobrien				     nlist.n_value, 0,
173598948Sobrien				     psymtab_language, objfile);
173698948Sobrien	      }
173798948Sobrien	  check_enum:
173898948Sobrien	    /* If this is an enumerated type, we need to
173998948Sobrien	       add all the enum constants to the partial symbol
174098948Sobrien	       table.  This does not cover enums without names, e.g.
174198948Sobrien	       "enum {a, b} c;" in C, but fortunately those are
174298948Sobrien	       rare.  There is no way for GDB to find those from the
174398948Sobrien	       enum type without spending too much time on it.  Thus
174498948Sobrien	       to solve this problem, the compiler needs to put out the
174598948Sobrien	       enum in a nameless type.  GCC2 does this.  */
174698948Sobrien
174798948Sobrien	    /* We are looking for something of the form
174898948Sobrien	       <name> ":" ("t" | "T") [<number> "="] "e"
174998948Sobrien	       {<constant> ":" <value> ","} ";".  */
175098948Sobrien
175198948Sobrien	    /* Skip over the colon and the 't' or 'T'.  */
175298948Sobrien	    p += 2;
175398948Sobrien	    /* This type may be given a number.  Also, numbers can come
175498948Sobrien	       in pairs like (0,26).  Skip over it.  */
175598948Sobrien	    while ((*p >= '0' && *p <= '9')
175698948Sobrien		   || *p == '(' || *p == ',' || *p == ')'
175798948Sobrien		   || *p == '=')
175898948Sobrien	      p++;
175998948Sobrien
176098948Sobrien	    if (*p++ == 'e')
176198948Sobrien	      {
176298948Sobrien		/* The aix4 compiler emits extra crud before the members.  */
176398948Sobrien		if (*p == '-')
176498948Sobrien		  {
176598948Sobrien		    /* Skip over the type (?).  */
176698948Sobrien		    while (*p != ':')
176798948Sobrien		      p++;
176898948Sobrien
176998948Sobrien		    /* Skip over the colon.  */
177098948Sobrien		    p++;
177198948Sobrien		  }
177298948Sobrien
177398948Sobrien		/* We have found an enumerated type.  */
177498948Sobrien		/* According to comments in read_enum_type
177598948Sobrien		   a comma could end it instead of a semicolon.
177698948Sobrien		   I don't know where that happens.
177798948Sobrien		   Accept either.  */
177898948Sobrien		while (*p && *p != ';' && *p != ',')
177998948Sobrien		  {
178098948Sobrien		    char *q;
178198948Sobrien
178298948Sobrien		    /* Check for and handle cretinous dbx symbol name
178398948Sobrien		       continuation!  */
178498948Sobrien		    if (*p == '\\' || (*p == '?' && p[1] == '\0'))
178598948Sobrien		      p = next_symbol_text (objfile);
178698948Sobrien
178798948Sobrien		    /* Point to the character after the name
178898948Sobrien		       of the enum constant.  */
178998948Sobrien		    for (q = p; *q && *q != ':'; q++)
179098948Sobrien		      ;
179198948Sobrien		    /* Note that the value doesn't matter for
179298948Sobrien		       enum constants in psymtabs, just in symtabs.  */
179398948Sobrien		    add_psymbol_to_list (p, q - p,
1794130809Smarcel					 VAR_DOMAIN, LOC_CONST,
179598948Sobrien					 &objfile->static_psymbols, 0,
179698948Sobrien					 0, psymtab_language, objfile);
179798948Sobrien		    /* Point past the name.  */
179898948Sobrien		    p = q;
179998948Sobrien		    /* Skip over the value.  */
180098948Sobrien		    while (*p && *p != ',')
180198948Sobrien		      p++;
180298948Sobrien		    /* Advance past the comma.  */
180398948Sobrien		    if (*p)
180498948Sobrien		      p++;
180598948Sobrien		  }
180698948Sobrien	      }
180798948Sobrien	    continue;
180898948Sobrien	  case 'c':
180998948Sobrien	    /* Constant, e.g. from "const" in Pascal.  */
181098948Sobrien	    add_psymbol_to_list (namestring, p - namestring,
1811130809Smarcel				 VAR_DOMAIN, LOC_CONST,
181298948Sobrien				 &objfile->static_psymbols, nlist.n_value,
181398948Sobrien				 0, psymtab_language, objfile);
181498948Sobrien	    continue;
181598948Sobrien
181698948Sobrien	  case 'f':
181798948Sobrien	    if (! pst)
181898948Sobrien	      {
181998948Sobrien		int name_len = p - namestring;
182098948Sobrien		char *name = xmalloc (name_len + 1);
182198948Sobrien		memcpy (name, namestring, name_len);
182298948Sobrien		name[name_len] = '\0';
1823130809Smarcel		function_outside_compilation_unit_complaint (name);
182498948Sobrien		xfree (name);
182598948Sobrien	      }
182698948Sobrien	    nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
182798948Sobrien	    /* Kludges for ELF/STABS with Sun ACC */
182898948Sobrien	    last_function_name = namestring;
182998948Sobrien#ifdef SOFUN_ADDRESS_MAYBE_MISSING
183098948Sobrien	    /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
183198948Sobrien	       value for the bottom of the text seg in those cases. */
183298948Sobrien	    if (nlist.n_value == ANOFFSET (objfile->section_offsets,
183398948Sobrien					   SECT_OFF_TEXT (objfile)))
183498948Sobrien	      {
183598948Sobrien		CORE_ADDR minsym_valu =
183698948Sobrien		  find_stab_function_addr (namestring, pst->filename, objfile);
183798948Sobrien		/* find_stab_function_addr will return 0 if the minimal
183898948Sobrien		   symbol wasn't found.  (Unfortunately, this might also
183998948Sobrien		   be a valid address.)  Anyway, if it *does* return 0,
184098948Sobrien		   it is likely that the value was set correctly to begin
184198948Sobrien		   with... */
184298948Sobrien		if (minsym_valu != 0)
184398948Sobrien		  nlist.n_value = minsym_valu;
184498948Sobrien	      }
184598948Sobrien	    if (pst && textlow_not_set)
184698948Sobrien	      {
1847130809Smarcel		pst->textlow = nlist.n_value;
184898948Sobrien		textlow_not_set = 0;
184998948Sobrien	      }
185098948Sobrien#endif
185198948Sobrien	    /* End kludge.  */
185298948Sobrien
185398948Sobrien	    /* Keep track of the start of the last function so we
185498948Sobrien	       can handle end of function symbols.  */
185598948Sobrien	    last_function_start = nlist.n_value;
185698948Sobrien
185798948Sobrien	    /* In reordered executables this function may lie outside
185898948Sobrien	       the bounds created by N_SO symbols.  If that's the case
185998948Sobrien	       use the address of this function as the low bound for
186098948Sobrien	       the partial symbol table.  */
186198948Sobrien	    if (pst
186298948Sobrien		&& (textlow_not_set
1863130809Smarcel		    || (nlist.n_value < pst->textlow
186498948Sobrien			&& (nlist.n_value
186598948Sobrien			    != ANOFFSET (objfile->section_offsets,
186698948Sobrien					 SECT_OFF_TEXT (objfile))))))
186798948Sobrien	      {
1868130809Smarcel		pst->textlow = nlist.n_value;
186998948Sobrien		textlow_not_set = 0;
187098948Sobrien	      }
187198948Sobrien	    add_psymbol_to_list (namestring, p - namestring,
1872130809Smarcel				 VAR_DOMAIN, LOC_BLOCK,
187398948Sobrien				 &objfile->static_psymbols,
187498948Sobrien				 0, nlist.n_value,
187598948Sobrien				 psymtab_language, objfile);
187698948Sobrien	    continue;
187798948Sobrien
187898948Sobrien	    /* Global functions were ignored here, but now they
187998948Sobrien	       are put into the global psymtab like one would expect.
188098948Sobrien	       They're also in the minimal symbol table.  */
188198948Sobrien	  case 'F':
188298948Sobrien	    if (! pst)
188398948Sobrien	      {
188498948Sobrien		int name_len = p - namestring;
188598948Sobrien		char *name = xmalloc (name_len + 1);
188698948Sobrien		memcpy (name, namestring, name_len);
188798948Sobrien		name[name_len] = '\0';
1888130809Smarcel		function_outside_compilation_unit_complaint (name);
188998948Sobrien		xfree (name);
189098948Sobrien	      }
189198948Sobrien	    nlist.n_value += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
189298948Sobrien	    /* Kludges for ELF/STABS with Sun ACC */
189398948Sobrien	    last_function_name = namestring;
189498948Sobrien#ifdef SOFUN_ADDRESS_MAYBE_MISSING
189598948Sobrien	    /* Do not fix textlow==0 for .o or NLM files, as 0 is a legit
189698948Sobrien	       value for the bottom of the text seg in those cases. */
189798948Sobrien	    if (nlist.n_value == ANOFFSET (objfile->section_offsets,
189898948Sobrien					   SECT_OFF_TEXT (objfile)))
189998948Sobrien	      {
190098948Sobrien		CORE_ADDR minsym_valu =
190198948Sobrien		  find_stab_function_addr (namestring, pst->filename, objfile);
190298948Sobrien		/* find_stab_function_addr will return 0 if the minimal
190398948Sobrien		   symbol wasn't found.  (Unfortunately, this might also
190498948Sobrien		   be a valid address.)  Anyway, if it *does* return 0,
190598948Sobrien		   it is likely that the value was set correctly to begin
190698948Sobrien		   with... */
190798948Sobrien		if (minsym_valu != 0)
190898948Sobrien		  nlist.n_value = minsym_valu;
190998948Sobrien	      }
191098948Sobrien	    if (pst && textlow_not_set)
191198948Sobrien	      {
1912130809Smarcel		pst->textlow = nlist.n_value;
191398948Sobrien		textlow_not_set = 0;
191498948Sobrien	      }
191598948Sobrien#endif
191698948Sobrien	    /* End kludge.  */
191798948Sobrien
191898948Sobrien	    /* Keep track of the start of the last function so we
191998948Sobrien	       can handle end of function symbols.  */
192098948Sobrien	    last_function_start = nlist.n_value;
192198948Sobrien
192298948Sobrien	    /* In reordered executables this function may lie outside
192398948Sobrien	       the bounds created by N_SO symbols.  If that's the case
192498948Sobrien	       use the address of this function as the low bound for
192598948Sobrien	       the partial symbol table.  */
192698948Sobrien	    if (pst
192798948Sobrien		&& (textlow_not_set
1928130809Smarcel		    || (nlist.n_value < pst->textlow
192998948Sobrien			&& (nlist.n_value
193098948Sobrien			    != ANOFFSET (objfile->section_offsets,
193198948Sobrien					 SECT_OFF_TEXT (objfile))))))
193298948Sobrien	      {
1933130809Smarcel		pst->textlow = nlist.n_value;
193498948Sobrien		textlow_not_set = 0;
193598948Sobrien	      }
193698948Sobrien	    add_psymbol_to_list (namestring, p - namestring,
1937130809Smarcel				 VAR_DOMAIN, LOC_BLOCK,
193898948Sobrien				 &objfile->global_psymbols,
193998948Sobrien				 0, nlist.n_value,
194098948Sobrien				 psymtab_language, objfile);
194198948Sobrien	    continue;
194298948Sobrien
194398948Sobrien	    /* Two things show up here (hopefully); static symbols of
194498948Sobrien	       local scope (static used inside braces) or extensions
194598948Sobrien	       of structure symbols.  We can ignore both.  */
194698948Sobrien	  case 'V':
194798948Sobrien	  case '(':
194898948Sobrien	  case '0':
194998948Sobrien	  case '1':
195098948Sobrien	  case '2':
195198948Sobrien	  case '3':
195298948Sobrien	  case '4':
195398948Sobrien	  case '5':
195498948Sobrien	  case '6':
195598948Sobrien	  case '7':
195698948Sobrien	  case '8':
195798948Sobrien	  case '9':
195898948Sobrien	  case '-':
195998948Sobrien	  case '#':		/* for symbol identification (used in live ranges) */
196098948Sobrien	    continue;
196198948Sobrien
196298948Sobrien	  case ':':
196398948Sobrien	    /* It is a C++ nested symbol.  We don't need to record it
196498948Sobrien	       (I don't think); if we try to look up foo::bar::baz,
196598948Sobrien	       then symbols for the symtab containing foo should get
196698948Sobrien	       read in, I think.  */
196798948Sobrien	    /* Someone says sun cc puts out symbols like
196898948Sobrien	       /foo/baz/maclib::/usr/local/bin/maclib,
196998948Sobrien	       which would get here with a symbol type of ':'.  */
197098948Sobrien	    continue;
197198948Sobrien
197298948Sobrien	  default:
197398948Sobrien	    /* Unexpected symbol descriptor.  The second and subsequent stabs
197498948Sobrien	       of a continued stab can show up here.  The question is
197598948Sobrien	       whether they ever can mimic a normal stab--it would be
197698948Sobrien	       nice if not, since we certainly don't want to spend the
197798948Sobrien	       time searching to the end of every string looking for
197898948Sobrien	       a backslash.  */
197998948Sobrien
1980130809Smarcel	    complaint (&symfile_complaints, "unknown symbol descriptor `%c'",
1981130809Smarcel		       p[1]);
198298948Sobrien
198398948Sobrien	    /* Ignore it; perhaps it is an extension that we don't
198498948Sobrien	       know about.  */
198598948Sobrien	    continue;
198698948Sobrien	  }
198798948Sobrien
198898948Sobrien	  case N_EXCL:
198998948Sobrien
199098948Sobrien	  namestring = set_namestring (objfile, nlist);
199198948Sobrien
199298948Sobrien	  /* Find the corresponding bincl and mark that psymtab on the
199398948Sobrien	     psymtab dependency list */
199498948Sobrien	  {
199598948Sobrien	    struct partial_symtab *needed_pst =
199698948Sobrien	      find_corresponding_bincl_psymtab (namestring, nlist.n_value);
199798948Sobrien
199898948Sobrien	    /* If this include file was defined earlier in this file,
199998948Sobrien	       leave it alone.  */
200098948Sobrien	    if (needed_pst == pst)
200198948Sobrien	      continue;
200298948Sobrien
200398948Sobrien	    if (needed_pst)
200498948Sobrien	      {
200598948Sobrien		int i;
200698948Sobrien		int found = 0;
200798948Sobrien
200898948Sobrien		for (i = 0; i < dependencies_used; i++)
200998948Sobrien		  if (dependency_list[i] == needed_pst)
201098948Sobrien		    {
201198948Sobrien		      found = 1;
201298948Sobrien		      break;
201398948Sobrien		    }
201498948Sobrien
201598948Sobrien		/* If it's already in the list, skip the rest.  */
201698948Sobrien		if (found)
201798948Sobrien		  continue;
201898948Sobrien
201998948Sobrien		dependency_list[dependencies_used++] = needed_pst;
202098948Sobrien		if (dependencies_used >= dependencies_allocated)
202198948Sobrien		  {
202298948Sobrien		    struct partial_symtab **orig = dependency_list;
202398948Sobrien		    dependency_list =
202498948Sobrien		      (struct partial_symtab **)
202598948Sobrien		      alloca ((dependencies_allocated *= 2)
202698948Sobrien			      * sizeof (struct partial_symtab *));
2027130809Smarcel		    memcpy (dependency_list, orig,
202898948Sobrien			    (dependencies_used
202998948Sobrien			     * sizeof (struct partial_symtab *)));
203098948Sobrien#ifdef DEBUG_INFO
203198948Sobrien		    fprintf_unfiltered (gdb_stderr, "Had to reallocate dependency list.\n");
203298948Sobrien		    fprintf_unfiltered (gdb_stderr, "New dependencies allocated: %d\n",
203398948Sobrien					dependencies_allocated);
203498948Sobrien#endif
203598948Sobrien		  }
203698948Sobrien	      }
203798948Sobrien	  }
203898948Sobrien	  continue;
203998948Sobrien
204098948Sobrien	  case N_ENDM:
204198948Sobrien#ifdef SOFUN_ADDRESS_MAYBE_MISSING
204298948Sobrien	  /* Solaris 2 end of module, finish current partial symbol table.
2043130809Smarcel	     end_psymtab will set pst->texthigh to the proper value, which
204498948Sobrien	     is necessary if a module compiled without debugging info
204598948Sobrien	     follows this module.  */
204698948Sobrien	  if (pst)
204798948Sobrien	  {
204898948Sobrien	    end_psymtab (pst, psymtab_include_list, includes_used,
204998948Sobrien			 symnum * symbol_size,
205098948Sobrien			 (CORE_ADDR) 0,
205198948Sobrien			 dependency_list, dependencies_used, textlow_not_set);
205298948Sobrien	    pst = (struct partial_symtab *) 0;
205398948Sobrien	    includes_used = 0;
205498948Sobrien	    dependencies_used = 0;
205598948Sobrien	  }
205698948Sobrien#endif
205798948Sobrien	  continue;
205898948Sobrien
205998948Sobrien	  case N_RBRAC:
206098948Sobrien#ifdef HANDLE_RBRAC
206198948Sobrien	  HANDLE_RBRAC (nlist.n_value);
206298948Sobrien	  continue;
206398948Sobrien#endif
206498948Sobrien	  case N_EINCL:
206598948Sobrien	  case N_DSLINE:
206698948Sobrien	  case N_BSLINE:
206798948Sobrien	  case N_SSYM:			/* Claim: Structure or union element.
206898948Sobrien					   Hopefully, I can ignore this.  */
206998948Sobrien	  case N_ENTRY:		/* Alternate entry point; can ignore. */
207098948Sobrien	  case N_MAIN:			/* Can definitely ignore this.   */
207198948Sobrien	  case N_CATCH:		/* These are GNU C++ extensions */
207298948Sobrien	  case N_EHDECL:		/* that can safely be ignored here. */
207398948Sobrien	  case N_LENG:
207498948Sobrien	  case N_BCOMM:
207598948Sobrien	  case N_ECOMM:
207698948Sobrien	  case N_ECOML:
207798948Sobrien	  case N_FNAME:
207898948Sobrien	  case N_SLINE:
207998948Sobrien	  case N_RSYM:
208098948Sobrien	  case N_PSYM:
208198948Sobrien	  case N_LBRAC:
208298948Sobrien	  case N_NSYMS:		/* Ultrix 4.0: symbol count */
208398948Sobrien	  case N_DEFD:			/* GNU Modula-2 */
208498948Sobrien	  case N_ALIAS:		/* SunPro F77: alias name, ignore for now.  */
208598948Sobrien
208698948Sobrien	  case N_OBJ:			/* useless types from Solaris */
208798948Sobrien	  case N_OPT:
2088130809Smarcel	  case N_PATCH:
208998948Sobrien	  /* These symbols aren't interesting; don't worry about them */
209098948Sobrien
209198948Sobrien	  continue;
209298948Sobrien
209398948Sobrien	  default:
209498948Sobrien	  /* If we haven't found it yet, ignore it.  It's probably some
209598948Sobrien	     new type we don't know about yet.  */
2096130809Smarcel	  unknown_symtype_complaint (local_hex_string (nlist.n_type));
209798948Sobrien	  continue;
209898948Sobrien	}
209919370Spst    }
210019370Spst
210119370Spst  /* If there's stuff to be cleaned up, clean it up.  */
210298948Sobrien  if (DBX_SYMCOUNT (objfile) > 0	/* We have some syms */
210398948Sobrien      /*FIXME, does this have a bug at start address 0? */
210419370Spst      && last_o_file_start
210598948Sobrien      && objfile->ei.entry_point < nlist.n_value
210698948Sobrien      && objfile->ei.entry_point >= last_o_file_start)
210719370Spst    {
2108130809Smarcel      objfile->ei.deprecated_entry_file_lowpc = last_o_file_start;
2109130809Smarcel      objfile->ei.deprecated_entry_file_highpc = nlist.n_value;
211019370Spst    }
211119370Spst
211219370Spst  if (pst)
211319370Spst    {
211446283Sdfr      /* Don't set pst->texthigh lower than it already is.  */
211546283Sdfr      CORE_ADDR text_end =
211698948Sobrien	(lowest_text_address == (CORE_ADDR) -1
211798948Sobrien	 ? (text_addr + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)))
211846283Sdfr	 : lowest_text_address)
211946283Sdfr	+ text_size;
212046283Sdfr
212119370Spst      end_psymtab (pst, psymtab_include_list, includes_used,
212219370Spst		   symnum * symbol_size,
2123130809Smarcel		   text_end > pst->texthigh ? text_end : pst->texthigh,
212446283Sdfr		   dependency_list, dependencies_used, textlow_not_set);
212519370Spst    }
212619370Spst
212719370Spst  do_cleanups (back_to);
212819370Spst}
212919370Spst
213019370Spst/* Allocate and partially fill a partial symtab.  It will be
213119370Spst   completely filled at the end of the symbol list.
213219370Spst
213319370Spst   SYMFILE_NAME is the name of the symbol-file we are reading from, and ADDR
213419370Spst   is the address relative to which its symbols are (incremental) or 0
213519370Spst   (normal). */
213619370Spst
213719370Spst
213898948Sobrienstatic struct partial_symtab *
213998948Sobrienstart_psymtab (struct objfile *objfile, char *filename, CORE_ADDR textlow,
214098948Sobrien	       int ldsymoff, struct partial_symbol **global_syms,
214198948Sobrien	       struct partial_symbol **static_syms)
214219370Spst{
214319370Spst  struct partial_symtab *result =
214498948Sobrien  start_psymtab_common (objfile, objfile->section_offsets,
214598948Sobrien			filename, textlow, global_syms, static_syms);
214619370Spst
214719370Spst  result->read_symtab_private = (char *)
2148130809Smarcel    obstack_alloc (&objfile->objfile_obstack, sizeof (struct symloc));
214998948Sobrien  LDSYMOFF (result) = ldsymoff;
215019370Spst  result->read_symtab = dbx_psymtab_to_symtab;
215198948Sobrien  SYMBOL_SIZE (result) = symbol_size;
215298948Sobrien  SYMBOL_OFFSET (result) = symbol_table_offset;
215398948Sobrien  STRING_OFFSET (result) = string_table_offset;
215498948Sobrien  FILE_STRING_OFFSET (result) = file_string_table_offset;
215519370Spst
215619370Spst  /* If we're handling an ELF file, drag some section-relocation info
215719370Spst     for this source file out of the ELF symbol table, to compensate for
215819370Spst     Sun brain death.  This replaces the section_offsets in this psymtab,
215919370Spst     if successful.  */
216019370Spst  elfstab_offset_sections (objfile, result);
216119370Spst
216219370Spst  /* Deduce the source language from the filename for this psymtab. */
216319370Spst  psymtab_language = deduce_language_from_filename (filename);
216419370Spst
216519370Spst  return result;
216619370Spst}
216719370Spst
216819370Spst/* Close off the current usage of PST.
216919370Spst   Returns PST or NULL if the partial symtab was empty and thrown away.
217019370Spst
217119370Spst   FIXME:  List variables and peculiarities of same.  */
217219370Spst
217319370Spststruct partial_symtab *
217498948Sobrienend_psymtab (struct partial_symtab *pst, char **include_list, int num_includes,
217598948Sobrien	     int capping_symbol_offset, CORE_ADDR capping_text,
217698948Sobrien	     struct partial_symtab **dependency_list, int number_dependencies,
217798948Sobrien	     int textlow_not_set)
217819370Spst{
217919370Spst  int i;
218098948Sobrien  struct objfile *objfile = pst->objfile;
218119370Spst
218219370Spst  if (capping_symbol_offset != -1)
218398948Sobrien    LDSYMLEN (pst) = capping_symbol_offset - LDSYMOFF (pst);
2184130809Smarcel  pst->texthigh = capping_text;
218519370Spst
218619370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING
218719370Spst  /* Under Solaris, the N_SO symbols always have a value of 0,
218819370Spst     instead of the usual address of the .o file.  Therefore,
218919370Spst     we have to do some tricks to fill in texthigh and textlow.
219098948Sobrien     The first trick is: if we see a static
219119370Spst     or global function, and the textlow for the current pst
219246283Sdfr     is not set (ie: textlow_not_set), then we use that function's
219346283Sdfr     address for the textlow of the pst.  */
219419370Spst
219519370Spst  /* Now, to fill in texthigh, we remember the last function seen
219698948Sobrien     in the .o file.  Also, there's a hack in
219719370Spst     bfd/elf.c and gdb/elfread.c to pass the ELF st_size field
219819370Spst     to here via the misc_info field.  Therefore, we can fill in
219919370Spst     a reliable texthigh by taking the address plus size of the
220019370Spst     last function in the file.  */
220119370Spst
2202130809Smarcel  if (pst->texthigh == 0 && last_function_name)
220346283Sdfr    {
220446283Sdfr      char *p;
220546283Sdfr      int n;
220646283Sdfr      struct minimal_symbol *minsym;
220719370Spst
220846283Sdfr      p = strchr (last_function_name, ':');
220946283Sdfr      if (p == NULL)
221046283Sdfr	p = last_function_name;
221146283Sdfr      n = p - last_function_name;
221246283Sdfr      p = alloca (n + 2);
221346283Sdfr      strncpy (p, last_function_name, n);
221446283Sdfr      p[n] = 0;
221598948Sobrien
221646283Sdfr      minsym = lookup_minimal_symbol (p, pst->filename, objfile);
221746283Sdfr      if (minsym == NULL)
221846283Sdfr	{
221946283Sdfr	  /* Sun Fortran appends an underscore to the minimal symbol name,
222046283Sdfr	     try again with an appended underscore if the minimal symbol
222146283Sdfr	     was not found.  */
222246283Sdfr	  p[n] = '_';
222346283Sdfr	  p[n + 1] = 0;
222446283Sdfr	  minsym = lookup_minimal_symbol (p, pst->filename, objfile);
222546283Sdfr	}
222619370Spst
222746283Sdfr      if (minsym)
2228130809Smarcel	pst->texthigh = SYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym);
222919370Spst
223046283Sdfr      last_function_name = NULL;
223146283Sdfr    }
223219370Spst
223319370Spst  /* this test will be true if the last .o file is only data */
223446283Sdfr  if (textlow_not_set)
2235130809Smarcel    pst->textlow = pst->texthigh;
223646283Sdfr  else
223746283Sdfr    {
223846283Sdfr      struct partial_symtab *p1;
223919370Spst
224046283Sdfr      /* If we know our own starting text address, then walk through all other
224198948Sobrien         psymtabs for this objfile, and if any didn't know their ending text
224298948Sobrien         address, set it to our starting address.  Take care to not set our
224398948Sobrien         own ending address to our starting address, nor to set addresses on
224498948Sobrien         `dependency' files that have both textlow and texthigh zero.  */
224519370Spst
224646283Sdfr      ALL_OBJFILE_PSYMTABS (objfile, p1)
224798948Sobrien      {
2248130809Smarcel	if (p1->texthigh == 0 && p1->textlow != 0 && p1 != pst)
224998948Sobrien	  {
2250130809Smarcel	    p1->texthigh = pst->textlow;
225198948Sobrien	    /* if this file has only data, then make textlow match texthigh */
2252130809Smarcel	    if (p1->textlow == 0)
2253130809Smarcel	      p1->textlow = p1->texthigh;
225498948Sobrien	  }
225598948Sobrien      }
225619370Spst    }
225719370Spst
225819370Spst  /* End of kludge for patching Solaris textlow and texthigh.  */
225919370Spst#endif /* SOFUN_ADDRESS_MAYBE_MISSING.  */
226019370Spst
226119370Spst  pst->n_global_syms =
226219370Spst    objfile->global_psymbols.next - (objfile->global_psymbols.list + pst->globals_offset);
226319370Spst  pst->n_static_syms =
226419370Spst    objfile->static_psymbols.next - (objfile->static_psymbols.list + pst->statics_offset);
226519370Spst
226619370Spst  pst->number_of_dependencies = number_dependencies;
226719370Spst  if (number_dependencies)
226819370Spst    {
226919370Spst      pst->dependencies = (struct partial_symtab **)
2270130809Smarcel	obstack_alloc (&objfile->objfile_obstack,
227198948Sobrien		    number_dependencies * sizeof (struct partial_symtab *));
227219370Spst      memcpy (pst->dependencies, dependency_list,
227398948Sobrien	      number_dependencies * sizeof (struct partial_symtab *));
227419370Spst    }
227519370Spst  else
227619370Spst    pst->dependencies = 0;
227719370Spst
227819370Spst  for (i = 0; i < num_includes; i++)
227919370Spst    {
228019370Spst      struct partial_symtab *subpst =
228198948Sobrien      allocate_psymtab (include_list[i], objfile);
228219370Spst
228398948Sobrien      /* Copy the sesction_offsets array from the main psymtab. */
228419370Spst      subpst->section_offsets = pst->section_offsets;
228519370Spst      subpst->read_symtab_private =
2286130809Smarcel	(char *) obstack_alloc (&objfile->objfile_obstack,
228798948Sobrien				sizeof (struct symloc));
228898948Sobrien      LDSYMOFF (subpst) =
228998948Sobrien	LDSYMLEN (subpst) =
2290130809Smarcel	subpst->textlow =
2291130809Smarcel	subpst->texthigh = 0;
229219370Spst
229319370Spst      /* We could save slight bits of space by only making one of these,
229498948Sobrien         shared by the entire set of include files.  FIXME-someday.  */
229519370Spst      subpst->dependencies = (struct partial_symtab **)
2296130809Smarcel	obstack_alloc (&objfile->objfile_obstack,
229719370Spst		       sizeof (struct partial_symtab *));
229819370Spst      subpst->dependencies[0] = pst;
229919370Spst      subpst->number_of_dependencies = 1;
230019370Spst
230119370Spst      subpst->globals_offset =
230219370Spst	subpst->n_global_syms =
230398948Sobrien	subpst->statics_offset =
230498948Sobrien	subpst->n_static_syms = 0;
230519370Spst
230619370Spst      subpst->readin = 0;
230719370Spst      subpst->symtab = 0;
230819370Spst      subpst->read_symtab = pst->read_symtab;
230919370Spst    }
231019370Spst
231119370Spst  sort_pst_symbols (pst);
231219370Spst
231319370Spst  /* If there is already a psymtab or symtab for a file of this name, remove it.
231419370Spst     (If there is a symtab, more drastic things also happen.)
231519370Spst     This happens in VxWorks.  */
231619370Spst  free_named_symtabs (pst->filename);
231719370Spst
231819370Spst  if (num_includes == 0
231919370Spst      && number_dependencies == 0
232019370Spst      && pst->n_global_syms == 0
232146283Sdfr      && pst->n_static_syms == 0
232246283Sdfr      && has_line_numbers == 0)
232319370Spst    {
232419370Spst      /* Throw away this psymtab, it's empty.  We can't deallocate it, since
232598948Sobrien         it is on the obstack, but we can forget to chain it on the list.  */
232619370Spst      /* Empty psymtabs happen as a result of header files which don't have
232798948Sobrien         any symbols in them.  There can be a lot of them.  But this check
232898948Sobrien         is wrong, in that a psymtab with N_SLINE entries but nothing else
232998948Sobrien         is not empty, but we don't realize that.  Fixing that without slowing
233098948Sobrien         things down might be tricky.  */
233119370Spst
233246283Sdfr      discard_psymtab (pst);
233319370Spst
233419370Spst      /* Indicate that psymtab was thrown away.  */
233598948Sobrien      pst = (struct partial_symtab *) NULL;
233619370Spst    }
233719370Spst  return pst;
233819370Spst}
233919370Spst
234019370Spststatic void
234198948Sobriendbx_psymtab_to_symtab_1 (struct partial_symtab *pst)
234219370Spst{
234319370Spst  struct cleanup *old_chain;
234419370Spst  int i;
234598948Sobrien
234619370Spst  if (!pst)
234719370Spst    return;
234819370Spst
234919370Spst  if (pst->readin)
235019370Spst    {
235119370Spst      fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
235298948Sobrien			  pst->filename);
235319370Spst      return;
235419370Spst    }
235519370Spst
235619370Spst  /* Read in all partial symtabs on which this one is dependent */
235719370Spst  for (i = 0; i < pst->number_of_dependencies; i++)
235819370Spst    if (!pst->dependencies[i]->readin)
235919370Spst      {
236019370Spst	/* Inform about additional files that need to be read in.  */
236119370Spst	if (info_verbose)
236219370Spst	  {
236319370Spst	    fputs_filtered (" ", gdb_stdout);
236419370Spst	    wrap_here ("");
236519370Spst	    fputs_filtered ("and ", gdb_stdout);
236619370Spst	    wrap_here ("");
236719370Spst	    printf_filtered ("%s...", pst->dependencies[i]->filename);
236898948Sobrien	    wrap_here ("");	/* Flush output */
236919370Spst	    gdb_flush (gdb_stdout);
237019370Spst	  }
237119370Spst	dbx_psymtab_to_symtab_1 (pst->dependencies[i]);
237219370Spst      }
237319370Spst
237498948Sobrien  if (LDSYMLEN (pst))		/* Otherwise it's a dummy */
237519370Spst    {
237619370Spst      /* Init stuff necessary for reading in symbols */
237719370Spst      stabsread_init ();
237819370Spst      buildsym_init ();
237998948Sobrien      old_chain = make_cleanup (really_free_pendings, 0);
238019370Spst      file_string_table_offset = FILE_STRING_OFFSET (pst);
238119370Spst      symbol_size = SYMBOL_SIZE (pst);
238219370Spst
238319370Spst      /* Read in this file's symbols */
238419370Spst      bfd_seek (pst->objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
238519370Spst      read_ofile_symtab (pst);
238619370Spst
238719370Spst      do_cleanups (old_chain);
238819370Spst    }
238919370Spst
239019370Spst  pst->readin = 1;
239119370Spst}
239219370Spst
239319370Spst/* Read in all of the symbols for a given psymtab for real.
239419370Spst   Be verbose about it if the user wants that.  */
239519370Spst
239619370Spststatic void
239798948Sobriendbx_psymtab_to_symtab (struct partial_symtab *pst)
239819370Spst{
239919370Spst  bfd *sym_bfd;
2400130809Smarcel  struct cleanup *back_to = NULL;
240119370Spst
240219370Spst  if (!pst)
240319370Spst    return;
240419370Spst
240519370Spst  if (pst->readin)
240619370Spst    {
240719370Spst      fprintf_unfiltered (gdb_stderr, "Psymtab for %s already read in.  Shouldn't happen.\n",
240898948Sobrien			  pst->filename);
240919370Spst      return;
241019370Spst    }
241119370Spst
241298948Sobrien  if (LDSYMLEN (pst) || pst->number_of_dependencies)
241319370Spst    {
241419370Spst      /* Print the message now, before reading the string table,
241598948Sobrien         to avoid disconcerting pauses.  */
241619370Spst      if (info_verbose)
241719370Spst	{
241819370Spst	  printf_filtered ("Reading in symbols for %s...", pst->filename);
241919370Spst	  gdb_flush (gdb_stdout);
242019370Spst	}
242119370Spst
242219370Spst      sym_bfd = pst->objfile->obfd;
242319370Spst
242419370Spst      next_symbol_text_func = dbx_next_symbol_text;
242519370Spst
2426130809Smarcel      if (DBX_STAB_SECTION (pst->objfile))
2427130809Smarcel	{
2428130809Smarcel	  stabs_data
2429130809Smarcel	    = symfile_relocate_debug_section (pst->objfile->obfd,
2430130809Smarcel					      DBX_STAB_SECTION (pst->objfile),
2431130809Smarcel					      NULL);
2432130809Smarcel	  if (stabs_data)
2433130809Smarcel	    back_to = make_cleanup (free_current_contents, (void *) &stabs_data);
2434130809Smarcel	}
2435130809Smarcel
243619370Spst      dbx_psymtab_to_symtab_1 (pst);
243719370Spst
2438130809Smarcel      if (back_to)
2439130809Smarcel	do_cleanups (back_to);
2440130809Smarcel
244119370Spst      /* Match with global symbols.  This only needs to be done once,
244219370Spst         after all of the symtabs and dependencies have been read in.   */
244319370Spst      scan_file_globals (pst->objfile);
244419370Spst
244519370Spst      /* Finish up the debug error message.  */
244619370Spst      if (info_verbose)
244719370Spst	printf_filtered ("done.\n");
244819370Spst    }
244919370Spst}
245019370Spst
245119370Spst/* Read in a defined section of a specific object file's symbols. */
245298948Sobrien
245319370Spststatic void
245498948Sobrienread_ofile_symtab (struct partial_symtab *pst)
245519370Spst{
2456130809Smarcel  char *namestring;
2457130809Smarcel  struct external_nlist *bufp;
245846283Sdfr  struct internal_nlist nlist;
245919370Spst  unsigned char type;
246019370Spst  unsigned max_symnum;
2461130809Smarcel  bfd *abfd;
246219370Spst  struct objfile *objfile;
246319370Spst  int sym_offset;		/* Offset to start of symbols to read */
246419370Spst  int sym_size;			/* Size of symbols to read */
246519370Spst  CORE_ADDR text_offset;	/* Start of text segment for symbols */
246619370Spst  int text_size;		/* Size of text segment for symbols */
246719370Spst  struct section_offsets *section_offsets;
246819370Spst
246919370Spst  objfile = pst->objfile;
247098948Sobrien  sym_offset = LDSYMOFF (pst);
247198948Sobrien  sym_size = LDSYMLEN (pst);
2472130809Smarcel  text_offset = pst->textlow;
2473130809Smarcel  text_size = pst->texthigh - pst->textlow;
247498948Sobrien  /* This cannot be simply objfile->section_offsets because of
247598948Sobrien     elfstab_offset_sections() which initializes the psymtab section
247698948Sobrien     offsets information in a special way, and that is different from
247798948Sobrien     objfile->section_offsets. */
247819370Spst  section_offsets = pst->section_offsets;
247919370Spst
248019370Spst  current_objfile = objfile;
248119370Spst  subfile_stack = NULL;
248219370Spst
248319370Spst  stringtab_global = DBX_STRINGTAB (objfile);
248419370Spst  last_source_file = NULL;
248519370Spst
248619370Spst  abfd = objfile->obfd;
248719370Spst  symfile_bfd = objfile->obfd;	/* Implicit param to next_text_symbol */
248819370Spst  symbuf_end = symbuf_idx = 0;
2489130809Smarcel  symbuf_read = 0;
2490130809Smarcel  symbuf_left = sym_offset + sym_size;
249119370Spst
249219370Spst  /* It is necessary to actually read one symbol *before* the start
249319370Spst     of this symtab's symbols, because the GCC_COMPILED_FLAG_SYMBOL
249419370Spst     occurs before the N_SO symbol.
249519370Spst
249619370Spst     Detecting this in read_dbx_symtab
249719370Spst     would slow down initial readin, so we look for it here instead.  */
249898948Sobrien  if (!processing_acc_compilation && sym_offset >= (int) symbol_size)
249919370Spst    {
2500130809Smarcel      stabs_seek (sym_offset - symbol_size);
250119370Spst      fill_symbuf (abfd);
250219370Spst      bufp = &symbuf[symbuf_idx++];
250346283Sdfr      INTERNALIZE_SYMBOL (nlist, bufp, abfd);
250419370Spst      OBJSTAT (objfile, n_stabs++);
250519370Spst
250698948Sobrien      namestring = set_namestring (objfile, nlist);
250719370Spst
250819370Spst      processing_gcc_compilation = 0;
250946283Sdfr      if (nlist.n_type == N_TEXT)
251019370Spst	{
251119370Spst	  const char *tempstring = namestring;
251219370Spst
2513130809Smarcel	  if (DEPRECATED_STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
251419370Spst	    processing_gcc_compilation = 1;
2515130809Smarcel	  else if (DEPRECATED_STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
251619370Spst	    processing_gcc_compilation = 2;
251719370Spst	  if (tempstring[0] == bfd_get_symbol_leading_char (symfile_bfd))
251819370Spst	    ++tempstring;
2519130809Smarcel	  if (DEPRECATED_STREQN (tempstring, "__gnu_compiled", 14))
252019370Spst	    processing_gcc_compilation = 2;
252119370Spst	}
252219370Spst
252319370Spst      /* Try to select a C++ demangling based on the compilation unit
252498948Sobrien         producer. */
252519370Spst
252698948Sobrien#if 0
252798948Sobrien      /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
252898948Sobrien	 know whether it will use the old style or v3 mangling.  */
252919370Spst      if (processing_gcc_compilation)
253019370Spst	{
253119370Spst	  if (AUTO_DEMANGLING)
253219370Spst	    {
253319370Spst	      set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
253419370Spst	    }
253519370Spst	}
253698948Sobrien#endif
253719370Spst    }
253819370Spst  else
253919370Spst    {
254019370Spst      /* The N_SO starting this symtab is the first symbol, so we
254198948Sobrien         better not check the symbol before it.  I'm not this can
254298948Sobrien         happen, but it doesn't hurt to check for it.  */
2543130809Smarcel      stabs_seek (sym_offset);
254419370Spst      processing_gcc_compilation = 0;
254519370Spst    }
254619370Spst
254719370Spst  if (symbuf_idx == symbuf_end)
254819370Spst    fill_symbuf (abfd);
254919370Spst  bufp = &symbuf[symbuf_idx];
255046283Sdfr  if (bfd_h_get_8 (abfd, bufp->e_type) != N_SO)
255198948Sobrien    error ("First symbol in segment of executable not a source symbol");
255219370Spst
255319370Spst  max_symnum = sym_size / symbol_size;
255419370Spst
255519370Spst  for (symnum = 0;
255619370Spst       symnum < max_symnum;
255719370Spst       symnum++)
255819370Spst    {
255919370Spst      QUIT;			/* Allow this to be interruptable */
256019370Spst      if (symbuf_idx == symbuf_end)
256198948Sobrien	fill_symbuf (abfd);
256219370Spst      bufp = &symbuf[symbuf_idx++];
256346283Sdfr      INTERNALIZE_SYMBOL (nlist, bufp, abfd);
256419370Spst      OBJSTAT (objfile, n_stabs++);
256519370Spst
256646283Sdfr      type = bfd_h_get_8 (abfd, bufp->e_type);
256719370Spst
256898948Sobrien      namestring = set_namestring (objfile, nlist);
256919370Spst
257098948Sobrien      if (type & N_STAB)
257198948Sobrien	{
257246283Sdfr	  process_one_symbol (type, nlist.n_desc, nlist.n_value,
257319370Spst			      namestring, section_offsets, objfile);
257498948Sobrien	}
257519370Spst      /* We skip checking for a new .o or -l file; that should never
257619370Spst         happen in this routine. */
257719370Spst      else if (type == N_TEXT)
257819370Spst	{
257919370Spst	  /* I don't think this code will ever be executed, because
258019370Spst	     the GCC_COMPILED_FLAG_SYMBOL usually is right before
258119370Spst	     the N_SO symbol which starts this source file.
258219370Spst	     However, there is no reason not to accept
258319370Spst	     the GCC_COMPILED_FLAG_SYMBOL anywhere.  */
258419370Spst
2585130809Smarcel	  if (DEPRECATED_STREQ (namestring, GCC_COMPILED_FLAG_SYMBOL))
258619370Spst	    processing_gcc_compilation = 1;
2587130809Smarcel	  else if (DEPRECATED_STREQ (namestring, GCC2_COMPILED_FLAG_SYMBOL))
258819370Spst	    processing_gcc_compilation = 2;
258919370Spst
259098948Sobrien#if 0
259198948Sobrien	  /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
259298948Sobrien	     know whether it will use the old style or v3 mangling.  */
259319370Spst	  if (AUTO_DEMANGLING)
259419370Spst	    {
259519370Spst	      set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
259619370Spst	    }
259798948Sobrien#endif
259819370Spst	}
259998948Sobrien      else if (type & N_EXT || type == (unsigned char) N_TEXT
260098948Sobrien	       || type == (unsigned char) N_NBTEXT
260198948Sobrien	)
260298948Sobrien	{
260319370Spst	  /* Global symbol: see if we came across a dbx defintion for
260419370Spst	     a corresponding symbol.  If so, store the value.  Remove
260519370Spst	     syms from the chain when their values are stored, but
260619370Spst	     search the whole chain, as there may be several syms from
260719370Spst	     different files with the same name. */
260819370Spst	  /* This is probably not true.  Since the files will be read
260919370Spst	     in one at a time, each reference to a global symbol will
261019370Spst	     be satisfied in each file as it appears. So we skip this
261119370Spst	     section. */
261219370Spst	  ;
261398948Sobrien	}
261419370Spst    }
261519370Spst
261619370Spst  current_objfile = NULL;
261719370Spst
261819370Spst  /* In a Solaris elf file, this variable, which comes from the
261919370Spst     value of the N_SO symbol, will still be 0.  Luckily, text_offset,
2620130809Smarcel     which comes from pst->textlow is correct. */
262119370Spst  if (last_source_start_addr == 0)
262219370Spst    last_source_start_addr = text_offset;
262319370Spst
262419370Spst  /* In reordered executables last_source_start_addr may not be the
262519370Spst     lower bound for this symtab, instead use text_offset which comes
2626130809Smarcel     from pst->textlow which is correct.  */
262719370Spst  if (last_source_start_addr > text_offset)
262819370Spst    last_source_start_addr = text_offset;
262919370Spst
263098948Sobrien  pst->symtab = end_symtab (text_offset + text_size, objfile, SECT_OFF_TEXT (objfile));
263146283Sdfr
263219370Spst  end_stabs ();
263319370Spst}
263498948Sobrien
263519370Spst
263619370Spst/* This handles a single symbol from the symbol-file, building symbols
263719370Spst   into a GDB symtab.  It takes these arguments and an implicit argument.
263819370Spst
263919370Spst   TYPE is the type field of the ".stab" symbol entry.
264019370Spst   DESC is the desc field of the ".stab" entry.
264119370Spst   VALU is the value field of the ".stab" entry.
264219370Spst   NAME is the symbol name, in our address space.
264319370Spst   SECTION_OFFSETS is a set of amounts by which the sections of this object
264498948Sobrien   file were relocated when it was loaded into memory.
264598948Sobrien   Note that these section_offsets are not the
264698948Sobrien   objfile->section_offsets but the pst->section_offsets.
264798948Sobrien   All symbols that refer
264898948Sobrien   to memory locations need to be offset by these amounts.
264919370Spst   OBJFILE is the object file from which we are reading symbols.
265098948Sobrien   It is used in end_symtab.  */
265119370Spst
265219370Spstvoid
265398948Sobrienprocess_one_symbol (int type, int desc, CORE_ADDR valu, char *name,
265498948Sobrien		    struct section_offsets *section_offsets,
265598948Sobrien		    struct objfile *objfile)
265619370Spst{
265719370Spst#ifdef SUN_FIXED_LBRAC_BUG
265819370Spst  /* If SUN_FIXED_LBRAC_BUG is defined, then it tells us whether we need
265919370Spst     to correct the address of N_LBRAC's.  If it is not defined, then
266019370Spst     we never need to correct the addresses.  */
266119370Spst
266219370Spst  /* This records the last pc address we've seen.  We depend on there being
266319370Spst     an SLINE or FUN or SO before the first LBRAC, since the variable does
266419370Spst     not get reset in between reads of different symbol files.  */
266519370Spst  static CORE_ADDR last_pc_address;
266619370Spst#endif
266719370Spst
2668130809Smarcel  struct context_stack *new;
266919370Spst  /* This remembers the address of the start of a function.  It is used
267019370Spst     because in Solaris 2, N_LBRAC, N_RBRAC, and N_SLINE entries are
267119370Spst     relative to the current function's start address.  On systems
267219370Spst     other than Solaris 2, this just holds the SECT_OFF_TEXT value, and is
267319370Spst     used to relocate these symbol types rather than SECTION_OFFSETS.  */
267419370Spst  static CORE_ADDR function_start_offset;
267519370Spst
267698948Sobrien  /* This holds the address of the start of a function, without the system
267798948Sobrien     peculiarities of function_start_offset.  */
267898948Sobrien  static CORE_ADDR last_function_start;
267998948Sobrien
2680130809Smarcel  /* If this is nonzero, we've seen an N_SLINE since the start of the
2681130809Smarcel     current function.  We use this to tell us to move the first sline
2682130809Smarcel     to the beginning of the function regardless of what its given
2683130809Smarcel     value is. */
268498948Sobrien  static int sline_found_in_function = 1;
268598948Sobrien
268619370Spst  /* If this is nonzero, we've seen a non-gcc N_OPT symbol for this source
268719370Spst     file.  Used to detect the SunPRO solaris compiler.  */
268819370Spst  static int n_opt_found;
268919370Spst
269019370Spst  /* The stab type used for the definition of the last function.
269119370Spst     N_STSYM or N_GSYM for SunOS4 acc; N_FUN for other compilers.  */
269219370Spst  static int function_stab_type = 0;
269319370Spst
269419370Spst  if (!block_address_function_relative)
269519370Spst    /* N_LBRAC, N_RBRAC and N_SLINE entries are not relative to the
269619370Spst       function start address, so just use the text offset.  */
269798948Sobrien    function_start_offset = ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
269819370Spst
269919370Spst  /* Something is wrong if we see real data before
270019370Spst     seeing a source file name.  */
270119370Spst
270298948Sobrien  if (last_source_file == NULL && type != (unsigned char) N_SO)
270319370Spst    {
270446283Sdfr      /* Ignore any symbols which appear before an N_SO symbol.
270598948Sobrien         Currently no one puts symbols there, but we should deal
270698948Sobrien         gracefully with the case.  A complain()t might be in order,
270798948Sobrien         but this should not be an error ().  */
270819370Spst      return;
270919370Spst    }
271019370Spst
271119370Spst  switch (type)
271219370Spst    {
271319370Spst    case N_FUN:
271419370Spst    case N_FNAME:
271519370Spst
271646283Sdfr      if (*name == '\000')
271719370Spst	{
271819370Spst	  /* This N_FUN marks the end of a function.  This closes off the
271919370Spst	     current block.  */
2720130809Smarcel
2721130809Smarcel 	  if (context_stack_depth <= 0)
2722130809Smarcel 	    {
2723130809Smarcel	      lbrac_mismatch_complaint (symnum);
2724130809Smarcel 	      break;
2725130809Smarcel 	    }
2726130809Smarcel
2727130809Smarcel	  /* The following check is added before recording line 0 at
2728130809Smarcel	     end of function so as to handle hand-generated stabs
2729130809Smarcel	     which may have an N_FUN stabs at the end of the function, but
2730130809Smarcel	     no N_SLINE stabs.  */
2731130809Smarcel	  if (sline_found_in_function)
2732130809Smarcel	    record_line (current_subfile, 0, last_function_start + valu);
2733130809Smarcel
273419370Spst	  within_function = 0;
273519370Spst	  new = pop_context ();
273619370Spst
273719370Spst	  /* Make a block for the local symbols within.  */
273819370Spst	  finish_block (new->name, &local_symbols, new->old_blocks,
273946283Sdfr			new->start_addr, new->start_addr + valu,
274019370Spst			objfile);
274146283Sdfr
274246283Sdfr	  /* May be switching to an assembler file which may not be using
274346283Sdfr	     block relative stabs, so reset the offset.  */
274446283Sdfr	  if (block_address_function_relative)
274546283Sdfr	    function_start_offset = 0;
274646283Sdfr
274719370Spst	  break;
274819370Spst	}
274919370Spst
275098948Sobrien      sline_found_in_function = 0;
275198948Sobrien
275219370Spst      /* Relocate for dynamic loading */
275398948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
275498948Sobrien      valu = SMASH_TEXT_ADDRESS (valu);
275598948Sobrien      last_function_start = valu;
275698948Sobrien
275719370Spst      goto define_a_symbol;
275819370Spst
275919370Spst    case N_LBRAC:
276019370Spst      /* This "symbol" just indicates the start of an inner lexical
276198948Sobrien         context within a function.  */
276219370Spst
276319370Spst      /* Ignore extra outermost context from SunPRO cc and acc.  */
276419370Spst      if (n_opt_found && desc == 1)
276519370Spst	break;
276619370Spst
276719370Spst      if (block_address_function_relative)
276819370Spst	/* Relocate for Sun ELF acc fn-relative syms.  */
276919370Spst	valu += function_start_offset;
277019370Spst      else
277119370Spst	/* On most machines, the block addresses are relative to the
277219370Spst	   N_SO, the linker did not relocate them (sigh).  */
277319370Spst	valu += last_source_start_addr;
277419370Spst
277519370Spst#ifdef SUN_FIXED_LBRAC_BUG
277698948Sobrien      if (!SUN_FIXED_LBRAC_BUG && valu < last_pc_address)
277798948Sobrien	{
277898948Sobrien	  /* Patch current LBRAC pc value to match last handy pc value */
2779130809Smarcel	  complaint (&symfile_complaints, "bad block start address patched");
278098948Sobrien	  valu = last_pc_address;
278198948Sobrien	}
278219370Spst#endif
278319370Spst      new = push_context (desc, valu);
278419370Spst      break;
278519370Spst
278619370Spst    case N_RBRAC:
278719370Spst      /* This "symbol" just indicates the end of an inner lexical
278898948Sobrien         context that was started with N_LBRAC.  */
278919370Spst
279019370Spst      /* Ignore extra outermost context from SunPRO cc and acc.  */
279119370Spst      if (n_opt_found && desc == 1)
279219370Spst	break;
279319370Spst
279419370Spst      if (block_address_function_relative)
279519370Spst	/* Relocate for Sun ELF acc fn-relative syms.  */
279619370Spst	valu += function_start_offset;
279719370Spst      else
279819370Spst	/* On most machines, the block addresses are relative to the
279919370Spst	   N_SO, the linker did not relocate them (sigh).  */
280019370Spst	valu += last_source_start_addr;
280119370Spst
2802130809Smarcel      if (context_stack_depth <= 0)
2803130809Smarcel	{
2804130809Smarcel	  lbrac_mismatch_complaint (symnum);
2805130809Smarcel	  break;
2806130809Smarcel	}
2807130809Smarcel
280898948Sobrien      new = pop_context ();
280919370Spst      if (desc != new->depth)
2810130809Smarcel	lbrac_mismatch_complaint (symnum);
281119370Spst
281219370Spst      /* Some compilers put the variable decls inside of an
281319370Spst         LBRAC/RBRAC block.  This macro should be nonzero if this
281498948Sobrien         is true.  DESC is N_DESC from the N_RBRAC symbol.
281598948Sobrien         GCC_P is true if we've detected the GCC_COMPILED_SYMBOL
281698948Sobrien         or the GCC2_COMPILED_SYMBOL.  */
281719370Spst#if !defined (VARIABLES_INSIDE_BLOCK)
281819370Spst#define VARIABLES_INSIDE_BLOCK(desc, gcc_p) 0
281919370Spst#endif
282019370Spst
282119370Spst      /* Can only use new->locals as local symbols here if we're in
282219370Spst         gcc or on a machine that puts them before the lbrack.  */
282398948Sobrien      if (!VARIABLES_INSIDE_BLOCK (desc, processing_gcc_compilation))
2824130809Smarcel	{
2825130809Smarcel	  if (local_symbols != NULL)
2826130809Smarcel	    {
2827130809Smarcel	      /* GCC development snapshots from March to December of
2828130809Smarcel		 2000 would output N_LSYM entries after N_LBRAC
2829130809Smarcel		 entries.  As a consequence, these symbols are simply
2830130809Smarcel		 discarded.  Complain if this is the case.  Note that
2831130809Smarcel		 there are some compilers which legitimately put local
2832130809Smarcel		 symbols within an LBRAC/RBRAC block; this complaint
2833130809Smarcel		 might also help sort out problems in which
2834130809Smarcel		 VARIABLES_INSIDE_BLOCK is incorrectly defined.  */
2835130809Smarcel	      complaint (&symfile_complaints,
2836130809Smarcel			 "misplaced N_LBRAC entry; discarding local symbols which have no enclosing block");
2837130809Smarcel	    }
2838130809Smarcel	  local_symbols = new->locals;
2839130809Smarcel	}
284019370Spst
284119370Spst      if (context_stack_depth
284298948Sobrien	  > !VARIABLES_INSIDE_BLOCK (desc, processing_gcc_compilation))
284319370Spst	{
284419370Spst	  /* This is not the outermost LBRAC...RBRAC pair in the function,
284519370Spst	     its local symbols preceded it, and are the ones just recovered
284619370Spst	     from the context stack.  Define the block for them (but don't
284719370Spst	     bother if the block contains no symbols.  Should we complain
284819370Spst	     on blocks without symbols?  I can't think of any useful purpose
284919370Spst	     for them).  */
285019370Spst	  if (local_symbols != NULL)
285119370Spst	    {
285219370Spst	      /* Muzzle a compiler bug that makes end < start.  (which
285398948Sobrien	         compilers?  Is this ever harmful?).  */
285419370Spst	      if (new->start_addr > valu)
285519370Spst		{
2856130809Smarcel		  complaint (&symfile_complaints,
2857130809Smarcel			     "block start larger than block end");
285819370Spst		  new->start_addr = valu;
285919370Spst		}
286019370Spst	      /* Make a block for the local symbols within.  */
286119370Spst	      finish_block (0, &local_symbols, new->old_blocks,
286219370Spst			    new->start_addr, valu, objfile);
286319370Spst	    }
286419370Spst	}
286519370Spst      else
286619370Spst	{
286719370Spst	  /* This is the outermost LBRAC...RBRAC pair.  There is no
286819370Spst	     need to do anything; leave the symbols that preceded it
286919370Spst	     to be attached to the function's own block.  We need to
287019370Spst	     indicate that we just moved outside of the function.  */
287119370Spst	  within_function = 0;
287219370Spst	}
287319370Spst
287498948Sobrien      if (VARIABLES_INSIDE_BLOCK (desc, processing_gcc_compilation))
287519370Spst	/* Now pop locals of block just finished.  */
287619370Spst	local_symbols = new->locals;
287719370Spst      break;
287819370Spst
287919370Spst    case N_FN:
288019370Spst    case N_FN_SEQ:
288119370Spst      /* This kind of symbol indicates the start of an object file.  */
288219370Spst      /* Relocate for dynamic loading */
288398948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
288419370Spst      break;
288519370Spst
288619370Spst    case N_SO:
288719370Spst      /* This type of symbol indicates the start of data
288898948Sobrien         for one source file.
288998948Sobrien         Finish the symbol table of the previous source file
289098948Sobrien         (if any) and start accumulating a new symbol table.  */
289119370Spst      /* Relocate for dynamic loading */
289298948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
289319370Spst
289419370Spst      n_opt_found = 0;
289519370Spst
289619370Spst#ifdef SUN_FIXED_LBRAC_BUG
289719370Spst      last_pc_address = valu;	/* Save for SunOS bug circumcision */
289819370Spst#endif
289919370Spst
290019370Spst#ifdef PCC_SOL_BROKEN
290119370Spst      /* pcc bug, occasionally puts out SO for SOL.  */
290219370Spst      if (context_stack_depth > 0)
290319370Spst	{
290419370Spst	  start_subfile (name, NULL);
290519370Spst	  break;
290619370Spst	}
290719370Spst#endif
290819370Spst      if (last_source_file)
290919370Spst	{
291019370Spst	  /* Check if previous symbol was also an N_SO (with some
291119370Spst	     sanity checks).  If so, that one was actually the directory
291219370Spst	     name, and the current one is the real file name.
291398948Sobrien	     Patch things up. */
291419370Spst	  if (previous_stab_code == (unsigned char) N_SO)
291519370Spst	    {
291619370Spst	      patch_subfile_names (current_subfile, name);
291719370Spst	      break;		/* Ignore repeated SOs */
291819370Spst	    }
291998948Sobrien	  end_symtab (valu, objfile, SECT_OFF_TEXT (objfile));
292019370Spst	  end_stabs ();
292119370Spst	}
292219370Spst
292319370Spst      /* Null name means this just marks the end of text for this .o file.
292498948Sobrien         Don't start a new symtab in this case.  */
292519370Spst      if (*name == '\000')
292619370Spst	break;
292719370Spst
292846283Sdfr      if (block_address_function_relative)
292998948Sobrien	function_start_offset = 0;
293046283Sdfr
293119370Spst      start_stabs ();
293219370Spst      start_symtab (name, NULL, valu);
293346283Sdfr      record_debugformat ("stabs");
293419370Spst      break;
293519370Spst
293619370Spst    case N_SOL:
293719370Spst      /* This type of symbol indicates the start of data for
293898948Sobrien         a sub-source-file, one whose contents were copied or
293998948Sobrien         included in the compilation of the main source file
294098948Sobrien         (whose name was given in the N_SO symbol.)  */
294119370Spst      /* Relocate for dynamic loading */
294298948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
294319370Spst      start_subfile (name, current_subfile->dirname);
294419370Spst      break;
294519370Spst
294619370Spst    case N_BINCL:
294719370Spst      push_subfile ();
294819370Spst      add_new_header_file (name, valu);
294919370Spst      start_subfile (name, current_subfile->dirname);
295019370Spst      break;
295119370Spst
295219370Spst    case N_EINCL:
295319370Spst      start_subfile (pop_subfile (), current_subfile->dirname);
295419370Spst      break;
295519370Spst
295619370Spst    case N_EXCL:
295719370Spst      add_old_header_file (name, valu);
295819370Spst      break;
295919370Spst
296019370Spst    case N_SLINE:
296119370Spst      /* This type of "symbol" really just records
296298948Sobrien         one line-number -- core-address correspondence.
296398948Sobrien         Enter it in the line list for this symbol table.  */
296446283Sdfr
296519370Spst      /* Relocate for dynamic loading and for ELF acc fn-relative syms.  */
296619370Spst      valu += function_start_offset;
296746283Sdfr
296819370Spst#ifdef SUN_FIXED_LBRAC_BUG
296919370Spst      last_pc_address = valu;	/* Save for SunOS bug circumcision */
297019370Spst#endif
297198948Sobrien      /* If this is the first SLINE note in the function, record it at
297298948Sobrien	 the start of the function instead of at the listed location.  */
297398948Sobrien      if (within_function && sline_found_in_function == 0)
297498948Sobrien	{
297598948Sobrien	  record_line (current_subfile, desc, last_function_start);
297698948Sobrien	  sline_found_in_function = 1;
297798948Sobrien	}
297898948Sobrien      else
297998948Sobrien	record_line (current_subfile, desc, valu);
298019370Spst      break;
298119370Spst
298219370Spst    case N_BCOMM:
298319370Spst      common_block_start (name, objfile);
298419370Spst      break;
298519370Spst
298619370Spst    case N_ECOMM:
298719370Spst      common_block_end (objfile);
298819370Spst      break;
298919370Spst
299098948Sobrien      /* The following symbol types need to have the appropriate offset added
299198948Sobrien         to their value; then we process symbol definitions in the name.  */
299219370Spst
299319370Spst    case N_STSYM:		/* Static symbol in data seg */
299419370Spst    case N_LCSYM:		/* Static symbol in BSS seg */
299519370Spst    case N_ROSYM:		/* Static symbol in Read-only data seg */
299698948Sobrien      /* HORRID HACK DEPT.  However, it's Sun's furgin' fault.
299798948Sobrien         Solaris2's stabs-in-elf makes *most* symbols relative
299898948Sobrien         but leaves a few absolute (at least for Solaris 2.1 and version
299998948Sobrien         2.0.1 of the SunPRO compiler).  N_STSYM and friends sit on the fence.
300098948Sobrien         .stab "foo:S...",N_STSYM        is absolute (ld relocates it)
300198948Sobrien         .stab "foo:V...",N_STSYM        is relative (section base subtracted).
300298948Sobrien         This leaves us no choice but to search for the 'S' or 'V'...
300398948Sobrien         (or pass the whole section_offsets stuff down ONE MORE function
300498948Sobrien         call level, which we really don't want to do).  */
300519370Spst      {
300619370Spst	char *p;
300719370Spst
300819370Spst	/* .o files and NLMs have non-zero text seg offsets, but don't need
300919370Spst	   their static syms offset in this fashion.  XXX - This is really a
301019370Spst	   crock that should be fixed in the solib handling code so that I
301119370Spst	   don't have to work around it here. */
301219370Spst
301319370Spst	if (!symfile_relocatable)
301419370Spst	  {
301519370Spst	    p = strchr (name, ':');
301619370Spst	    if (p != 0 && p[1] == 'S')
301719370Spst	      {
301819370Spst		/* The linker relocated it.  We don't want to add an
301919370Spst		   elfstab_offset_sections-type offset, but we *do* want
302019370Spst		   to add whatever solib.c passed to symbol_file_add as
302119370Spst		   addr (this is known to affect SunOS4, and I suspect ELF
302219370Spst		   too).  Since elfstab_offset_sections currently does not
302319370Spst		   muck with the text offset (there is no Ttext.text
302419370Spst		   symbol), we can get addr from the text offset.  If
302519370Spst		   elfstab_offset_sections ever starts dealing with the
302619370Spst		   text offset, and we still need to do this, we need to
302719370Spst		   invent a SECT_OFF_ADDR_KLUDGE or something.  */
302898948Sobrien		valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
302919370Spst		goto define_a_symbol;
303019370Spst	      }
303119370Spst	  }
303219370Spst	/* Since it's not the kludge case, re-dispatch to the right handler. */
303398948Sobrien	switch (type)
303498948Sobrien	  {
303598948Sobrien	  case N_STSYM:
303698948Sobrien	    goto case_N_STSYM;
303798948Sobrien	  case N_LCSYM:
303898948Sobrien	    goto case_N_LCSYM;
303998948Sobrien	  case N_ROSYM:
304098948Sobrien	    goto case_N_ROSYM;
304198948Sobrien	  default:
304298948Sobrien	    internal_error (__FILE__, __LINE__, "failed internal consistency check");
304398948Sobrien	  }
304419370Spst      }
304519370Spst
304619370Spst    case_N_STSYM:		/* Static symbol in data seg */
304719370Spst    case N_DSLINE:		/* Source line number, data seg */
304898948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_DATA (objfile));
304919370Spst      goto define_a_symbol;
305019370Spst
305119370Spst    case_N_LCSYM:		/* Static symbol in BSS seg */
305219370Spst    case N_BSLINE:		/* Source line number, bss seg */
305398948Sobrien      /*   N_BROWS:       overlaps with N_BSLINE */
305498948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_BSS (objfile));
305519370Spst      goto define_a_symbol;
305619370Spst
305719370Spst    case_N_ROSYM:		/* Static symbol in Read-only data seg */
305898948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_RODATA (objfile));
305919370Spst      goto define_a_symbol;
306019370Spst
306119370Spst    case N_ENTRY:		/* Alternate entry point */
306219370Spst      /* Relocate for dynamic loading */
306398948Sobrien      valu += ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile));
306419370Spst      goto define_a_symbol;
306519370Spst
306698948Sobrien      /* The following symbol types we don't know how to process.  Handle
306798948Sobrien         them in a "default" way, but complain to people who care.  */
306819370Spst    default:
306919370Spst    case N_CATCH:		/* Exception handler catcher */
307019370Spst    case N_EHDECL:		/* Exception handler name */
307119370Spst    case N_PC:			/* Global symbol in Pascal */
307298948Sobrien    case N_M2C:		/* Modula-2 compilation unit */
307398948Sobrien      /*   N_MOD2:        overlaps with N_EHDECL */
307419370Spst    case N_SCOPE:		/* Modula-2 scope information */
307519370Spst    case N_ECOML:		/* End common (local name) */
307619370Spst    case N_NBTEXT:		/* Gould Non-Base-Register symbols??? */
307719370Spst    case N_NBDATA:
307819370Spst    case N_NBBSS:
307919370Spst    case N_NBSTS:
308019370Spst    case N_NBLCS:
3081130809Smarcel      unknown_symtype_complaint (local_hex_string (type));
308219370Spst      /* FALLTHROUGH */
308319370Spst
308498948Sobrien      /* The following symbol types don't need the address field relocated,
308598948Sobrien         since it is either unused, or is absolute.  */
308619370Spst    define_a_symbol:
308719370Spst    case N_GSYM:		/* Global variable */
308819370Spst    case N_NSYMS:		/* Number of symbols (ultrix) */
308919370Spst    case N_NOMAP:		/* No map?  (ultrix) */
309019370Spst    case N_RSYM:		/* Register variable */
309119370Spst    case N_DEFD:		/* Modula-2 GNU module dependency */
309219370Spst    case N_SSYM:		/* Struct or union element */
309319370Spst    case N_LSYM:		/* Local symbol in stack */
309419370Spst    case N_PSYM:		/* Parameter variable */
309519370Spst    case N_LENG:		/* Length of preceding symbol type */
309619370Spst      if (name)
309719370Spst	{
309819370Spst	  int deftype;
309919370Spst	  char *colon_pos = strchr (name, ':');
310019370Spst	  if (colon_pos == NULL)
310119370Spst	    deftype = '\0';
310219370Spst	  else
310319370Spst	    deftype = colon_pos[1];
310419370Spst
310519370Spst	  switch (deftype)
310619370Spst	    {
310719370Spst	    case 'f':
310819370Spst	    case 'F':
310919370Spst	      function_stab_type = type;
311019370Spst
311119370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING
311219370Spst	      /* Deal with the SunPRO 3.0 compiler which omits the address
311398948Sobrien	         from N_FUN symbols.  */
311419370Spst	      if (type == N_FUN
311598948Sobrien		  && valu == ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)))
311619370Spst		{
311798948Sobrien		  CORE_ADDR minsym_valu =
311898948Sobrien		    find_stab_function_addr (name, last_source_file, objfile);
311919370Spst
312098948Sobrien		  /* find_stab_function_addr will return 0 if the minimal
312198948Sobrien		     symbol wasn't found.  (Unfortunately, this might also
312298948Sobrien		     be a valid address.)  Anyway, if it *does* return 0,
312398948Sobrien		     it is likely that the value was set correctly to begin
312498948Sobrien		     with... */
312598948Sobrien		  if (minsym_valu != 0)
312698948Sobrien		    valu = minsym_valu;
312719370Spst		}
312819370Spst#endif
312919370Spst
313019370Spst#ifdef SUN_FIXED_LBRAC_BUG
313119370Spst	      /* The Sun acc compiler, under SunOS4, puts out
313298948Sobrien	         functions with N_GSYM or N_STSYM.  The problem is
313398948Sobrien	         that the address of the symbol is no good (for N_GSYM
313498948Sobrien	         it doesn't even attept an address; for N_STSYM it
313598948Sobrien	         puts out an address but then it gets relocated
313698948Sobrien	         relative to the data segment, not the text segment).
313798948Sobrien	         Currently we can't fix this up later as we do for
313898948Sobrien	         some types of symbol in scan_file_globals.
313998948Sobrien	         Fortunately we do have a way of finding the address -
314098948Sobrien	         we know that the value in last_pc_address is either
314198948Sobrien	         the one we want (if we're dealing with the first
314298948Sobrien	         function in an object file), or somewhere in the
314398948Sobrien	         previous function. This means that we can use the
314498948Sobrien	         minimal symbol table to get the address.  */
314519370Spst
314619370Spst	      /* Starting with release 3.0, the Sun acc compiler,
314798948Sobrien	         under SunOS4, puts out functions with N_FUN and a value
314898948Sobrien	         of zero. This gets relocated to the start of the text
314998948Sobrien	         segment of the module, which is no good either.
315098948Sobrien	         Under SunOS4 we can deal with this as N_SLINE and N_SO
315198948Sobrien	         entries contain valid absolute addresses.
315298948Sobrien	         Release 3.0 acc also puts out N_OPT entries, which makes
315398948Sobrien	         it possible to discern acc from cc or gcc.  */
315419370Spst
315519370Spst	      if (type == N_GSYM || type == N_STSYM
315619370Spst		  || (type == N_FUN
315719370Spst		      && n_opt_found && !block_address_function_relative))
315819370Spst		{
315919370Spst		  struct minimal_symbol *m;
316019370Spst		  int l = colon_pos - name;
316119370Spst
316219370Spst		  m = lookup_minimal_symbol_by_pc (last_pc_address);
3163130809Smarcel		  if (m && strncmp (DEPRECATED_SYMBOL_NAME (m), name, l) == 0
3164130809Smarcel		      && DEPRECATED_SYMBOL_NAME (m)[l] == '\0')
316519370Spst		    /* last_pc_address was in this function */
316619370Spst		    valu = SYMBOL_VALUE (m);
3167130809Smarcel		  else if (m && DEPRECATED_SYMBOL_NAME (m + 1)
3168130809Smarcel			   && strncmp (DEPRECATED_SYMBOL_NAME (m + 1), name, l) == 0
3169130809Smarcel			   && DEPRECATED_SYMBOL_NAME (m + 1)[l] == '\0')
317019370Spst		    /* last_pc_address was in last function */
317198948Sobrien		    valu = SYMBOL_VALUE (m + 1);
317219370Spst		  else
317319370Spst		    /* Not found - use last_pc_address (for finish_block) */
317419370Spst		    valu = last_pc_address;
317519370Spst		}
317619370Spst
317719370Spst	      last_pc_address = valu;	/* Save for SunOS bug circumcision */
317819370Spst#endif
317919370Spst
318019370Spst	      if (block_address_function_relative)
318119370Spst		/* For Solaris 2.0 compilers, the block addresses and
318219370Spst		   N_SLINE's are relative to the start of the
318319370Spst		   function.  On normal systems, and when using gcc on
318419370Spst		   Solaris 2.0, these addresses are just absolute, or
318519370Spst		   relative to the N_SO, depending on
318619370Spst		   BLOCK_ADDRESS_ABSOLUTE.  */
318798948Sobrien		function_start_offset = valu;
318819370Spst
318919370Spst	      within_function = 1;
319098948Sobrien
319198948Sobrien	      if (context_stack_depth > 1)
319298948Sobrien		{
3193130809Smarcel		  complaint (&symfile_complaints,
3194130809Smarcel			     "unmatched N_LBRAC before symtab pos %d", symnum);
319598948Sobrien		  break;
319698948Sobrien		}
319798948Sobrien
319819370Spst	      if (context_stack_depth > 0)
319919370Spst		{
320019370Spst		  new = pop_context ();
320119370Spst		  /* Make a block for the local symbols within.  */
320219370Spst		  finish_block (new->name, &local_symbols, new->old_blocks,
320319370Spst				new->start_addr, valu, objfile);
320419370Spst		}
320519370Spst
320619370Spst	      new = push_context (0, valu);
320719370Spst	      new->name = define_symbol (valu, name, desc, type, objfile);
320819370Spst	      break;
320919370Spst
321019370Spst	    default:
321119370Spst	      define_symbol (valu, name, desc, type, objfile);
321219370Spst	      break;
321319370Spst	    }
321419370Spst	}
321519370Spst      break;
321619370Spst
321798948Sobrien      /* We use N_OPT to carry the gcc2_compiled flag.  Sun uses it
321898948Sobrien         for a bunch of other flags, too.  Someday we may parse their
321998948Sobrien         flags; for now we ignore theirs and hope they'll ignore ours.  */
322098948Sobrien    case N_OPT:		/* Solaris 2:  Compiler options */
322119370Spst      if (name)
322219370Spst	{
3223130809Smarcel	  if (strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0)
322419370Spst	    {
322519370Spst	      processing_gcc_compilation = 2;
322698948Sobrien#if 0				/* Works, but is experimental.  -fnf */
322798948Sobrien	      /* For now, stay with AUTO_DEMANGLING for g++ output, as we don't
322898948Sobrien		 know whether it will use the old style or v3 mangling.  */
322919370Spst	      if (AUTO_DEMANGLING)
323019370Spst		{
323119370Spst		  set_demangling_style (GNU_DEMANGLING_STYLE_STRING);
323219370Spst		}
323319370Spst#endif
323419370Spst	    }
323519370Spst	  else
323619370Spst	    n_opt_found = 1;
323719370Spst	}
323819370Spst      break;
323919370Spst
324098948Sobrien    case N_MAIN:		/* Name of main routine.  */
324198948Sobrien      /* FIXME: If one has a symbol file with N_MAIN and then replaces
324298948Sobrien	 it with a symbol file with "main" and without N_MAIN.  I'm
324398948Sobrien	 not sure exactly what rule to follow but probably something
324498948Sobrien	 like: N_MAIN takes precedence over "main" no matter what
324598948Sobrien	 objfile it is in; If there is more than one N_MAIN, choose
324698948Sobrien	 the one in the symfile_objfile; If there is more than one
324798948Sobrien	 N_MAIN within a given objfile, complain() and choose
324898948Sobrien	 arbitrarily. (kingdon) */
324998948Sobrien      if (name != NULL)
325098948Sobrien	set_main_name (name);
325198948Sobrien      break;
325298948Sobrien
325398948Sobrien      /* The following symbol types can be ignored.  */
325498948Sobrien    case N_OBJ:		/* Solaris 2:  Object file dir and name */
3255130809Smarcel    case N_PATCH:	/* Solaris2: Patch Run Time Checker.  */
325698948Sobrien      /*   N_UNDF:                   Solaris 2:  file separator mark */
325798948Sobrien      /*   N_UNDF: -- we will never encounter it, since we only process one
325898948Sobrien         file's symbols at once.  */
325919370Spst    case N_ENDM:		/* Solaris 2:  End of module */
326046283Sdfr    case N_ALIAS:		/* SunPro F77: alias name, ignore for now.  */
326119370Spst      break;
326219370Spst    }
326319370Spst
326446283Sdfr  /* '#' is a GNU C extension to allow one symbol to refer to another
326546283Sdfr     related symbol.
326646283Sdfr
326746283Sdfr     Generally this is used so that an alias can refer to its main
326898948Sobrien     symbol.  */
326946283Sdfr  if (name[0] == '#')
327046283Sdfr    {
327146283Sdfr      /* Initialize symbol reference names and determine if this is
327246283Sdfr         a definition.  If symbol reference is being defined, go
327346283Sdfr         ahead and add it.  Otherwise, just return sym. */
327446283Sdfr
327546283Sdfr      char *s = name;
327646283Sdfr      int refnum;
327746283Sdfr
327846283Sdfr      /* If this stab defines a new reference ID that is not on the
327998948Sobrien         reference list, then put it on the reference list.
328046283Sdfr
328198948Sobrien         We go ahead and advance NAME past the reference, even though
328298948Sobrien         it is not strictly necessary at this time.  */
328346283Sdfr      refnum = symbol_reference_defined (&s);
328446283Sdfr      if (refnum >= 0)
328546283Sdfr	if (!ref_search (refnum))
328646283Sdfr	  ref_add (refnum, 0, name, valu);
328746283Sdfr      name = s;
328846283Sdfr    }
328946283Sdfr
329046283Sdfr
329119370Spst  previous_stab_code = type;
329219370Spst}
329319370Spst
329419370Spst/* FIXME: The only difference between this and elfstab_build_psymtabs
329519370Spst   is the call to install_minimal_symbols for elf, and the support for
329619370Spst   split sections.  If the differences are really that small, the code
329719370Spst   should be shared.  */
329819370Spst
329919370Spst/* Scan and build partial symbols for an coff symbol file.
330019370Spst   The coff file has already been processed to get its minimal symbols.
330119370Spst
330219370Spst   This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
330319370Spst   rolled into one.
330419370Spst
330519370Spst   OBJFILE is the object file we are reading symbols from.
330619370Spst   ADDR is the address relative to which the symbols are (e.g.
330719370Spst   the base address of the text segment).
330819370Spst   MAINLINE is true if we are reading the main symbol
330919370Spst   table (as opposed to a shared lib or dynamically loaded file).
331019370Spst   TEXTADDR is the address of the text section.
331119370Spst   TEXTSIZE is the size of the text section.
331219370Spst   STABSECTS is the list of .stab sections in OBJFILE.
331319370Spst   STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
331419370Spst   .stabstr section exists.
331519370Spst
331619370Spst   This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
331719370Spst   adjusted for coff details. */
331819370Spst
331919370Spstvoid
332098948Sobriencoffstab_build_psymtabs (struct objfile *objfile, int mainline,
332198948Sobrien			 CORE_ADDR textaddr, unsigned int textsize,
332298948Sobrien			 struct stab_section_list *stabsects,
332398948Sobrien			 file_ptr stabstroffset, unsigned int stabstrsize)
332419370Spst{
332519370Spst  int val;
332619370Spst  bfd *sym_bfd = objfile->obfd;
332719370Spst  char *name = bfd_get_filename (sym_bfd);
332819370Spst  struct dbx_symfile_info *info;
332919370Spst  unsigned int stabsize;
333019370Spst
333119370Spst  /* There is already a dbx_symfile_info allocated by our caller.
333219370Spst     It might even contain some info from the coff symtab to help us.  */
333346283Sdfr  info = objfile->sym_stab_info;
333419370Spst
333519370Spst  DBX_TEXT_ADDR (objfile) = textaddr;
333619370Spst  DBX_TEXT_SIZE (objfile) = textsize;
333719370Spst
333819370Spst#define	COFF_STABS_SYMBOL_SIZE	12	/* XXX FIXME XXX */
333998948Sobrien  DBX_SYMBOL_SIZE (objfile) = COFF_STABS_SYMBOL_SIZE;
334019370Spst  DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
334198948Sobrien
334219370Spst  if (stabstrsize > bfd_get_size (sym_bfd))
334319370Spst    error ("ridiculous string table size: %d bytes", stabstrsize);
334419370Spst  DBX_STRINGTAB (objfile) = (char *)
3345130809Smarcel    obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
334698948Sobrien  OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
334719370Spst
334819370Spst  /* Now read in the string table in one big gulp.  */
334919370Spst
335019370Spst  val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
335119370Spst  if (val < 0)
335219370Spst    perror_with_name (name);
335398948Sobrien  val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
335419370Spst  if (val != stabstrsize)
335519370Spst    perror_with_name (name);
335619370Spst
335719370Spst  stabsread_new_init ();
335819370Spst  buildsym_new_init ();
335919370Spst  free_header_files ();
336019370Spst  init_header_files ();
336119370Spst
336219370Spst  processing_acc_compilation = 1;
336319370Spst
336419370Spst  /* In a coff file, we've already installed the minimal symbols that came
336519370Spst     from the coff (non-stab) symbol table, so always act like an
336619370Spst     incremental load here. */
336719370Spst  if (stabsects->next == NULL)
336819370Spst    {
336919370Spst      stabsize = bfd_section_size (sym_bfd, stabsects->section);
337019370Spst      DBX_SYMCOUNT (objfile) = stabsize / DBX_SYMBOL_SIZE (objfile);
337119370Spst      DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
337219370Spst    }
337319370Spst  else
337419370Spst    {
337519370Spst      struct stab_section_list *stabsect;
337619370Spst
337719370Spst      DBX_SYMCOUNT (objfile) = 0;
337819370Spst      for (stabsect = stabsects; stabsect != NULL; stabsect = stabsect->next)
337919370Spst	{
338019370Spst	  stabsize = bfd_section_size (sym_bfd, stabsect->section);
338119370Spst	  DBX_SYMCOUNT (objfile) += stabsize / DBX_SYMBOL_SIZE (objfile);
338219370Spst	}
338319370Spst
338419370Spst      DBX_SYMTAB_OFFSET (objfile) = stabsects->section->filepos;
338519370Spst
338619370Spst      symbuf_sections = stabsects->next;
338719370Spst      symbuf_left = bfd_section_size (sym_bfd, stabsects->section);
338819370Spst      symbuf_read = 0;
338919370Spst    }
339019370Spst
339198948Sobrien  dbx_symfile_read (objfile, 0);
339219370Spst}
339319370Spst
339419370Spst/* Scan and build partial symbols for an ELF symbol file.
339519370Spst   This ELF file has already been processed to get its minimal symbols,
339619370Spst   and any DWARF symbols that were in it.
339719370Spst
339819370Spst   This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
339919370Spst   rolled into one.
340019370Spst
340119370Spst   OBJFILE is the object file we are reading symbols from.
340219370Spst   ADDR is the address relative to which the symbols are (e.g.
340319370Spst   the base address of the text segment).
340419370Spst   MAINLINE is true if we are reading the main symbol
340519370Spst   table (as opposed to a shared lib or dynamically loaded file).
3406130809Smarcel   STABSECT is the BFD section information for the .stab section.
340719370Spst   STABSTROFFSET and STABSTRSIZE define the location in OBJFILE where the
340819370Spst   .stabstr section exists.
340919370Spst
341019370Spst   This routine is mostly copied from dbx_symfile_init and dbx_symfile_read,
341119370Spst   adjusted for elf details. */
341219370Spst
341319370Spstvoid
341498948Sobrienelfstab_build_psymtabs (struct objfile *objfile, int mainline,
3415130809Smarcel			asection *stabsect,
341698948Sobrien			file_ptr stabstroffset, unsigned int stabstrsize)
341719370Spst{
341819370Spst  int val;
341919370Spst  bfd *sym_bfd = objfile->obfd;
342019370Spst  char *name = bfd_get_filename (sym_bfd);
342119370Spst  struct dbx_symfile_info *info;
3422130809Smarcel  struct cleanup *back_to = NULL;
342319370Spst
342419370Spst  /* There is already a dbx_symfile_info allocated by our caller.
342519370Spst     It might even contain some info from the ELF symtab to help us.  */
342646283Sdfr  info = objfile->sym_stab_info;
342719370Spst
342898948Sobrien  /* Find the first and last text address.  dbx_symfile_read seems to
342998948Sobrien     want this.  */
343098948Sobrien  find_text_range (sym_bfd, objfile);
343119370Spst
343219370Spst#define	ELF_STABS_SYMBOL_SIZE	12	/* XXX FIXME XXX */
343398948Sobrien  DBX_SYMBOL_SIZE (objfile) = ELF_STABS_SYMBOL_SIZE;
3434130809Smarcel  DBX_SYMCOUNT (objfile)
3435130809Smarcel    = bfd_section_size (objfile->obfd, stabsect) / DBX_SYMBOL_SIZE (objfile);
343619370Spst  DBX_STRINGTAB_SIZE (objfile) = stabstrsize;
3437130809Smarcel  DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;
3438130809Smarcel  DBX_STAB_SECTION (objfile) = stabsect;
343998948Sobrien
344019370Spst  if (stabstrsize > bfd_get_size (sym_bfd))
344119370Spst    error ("ridiculous string table size: %d bytes", stabstrsize);
344219370Spst  DBX_STRINGTAB (objfile) = (char *)
3443130809Smarcel    obstack_alloc (&objfile->objfile_obstack, stabstrsize + 1);
344498948Sobrien  OBJSTAT (objfile, sz_strtab += stabstrsize + 1);
344519370Spst
344619370Spst  /* Now read in the string table in one big gulp.  */
344719370Spst
344819370Spst  val = bfd_seek (sym_bfd, stabstroffset, SEEK_SET);
344919370Spst  if (val < 0)
345019370Spst    perror_with_name (name);
345198948Sobrien  val = bfd_bread (DBX_STRINGTAB (objfile), stabstrsize, sym_bfd);
345219370Spst  if (val != stabstrsize)
345319370Spst    perror_with_name (name);
345419370Spst
345519370Spst  stabsread_new_init ();
345619370Spst  buildsym_new_init ();
345719370Spst  free_header_files ();
345819370Spst  init_header_files ();
345919370Spst
346019370Spst  processing_acc_compilation = 1;
346119370Spst
3462130809Smarcel  symbuf_read = 0;
3463130809Smarcel  symbuf_left = bfd_section_size (objfile->obfd, stabsect);
3464130809Smarcel  stabs_data = symfile_relocate_debug_section (objfile->obfd, stabsect, NULL);
3465130809Smarcel  if (stabs_data)
3466130809Smarcel    back_to = make_cleanup (free_current_contents, (void *) &stabs_data);
3467130809Smarcel
346819370Spst  /* In an elf file, we've already installed the minimal symbols that came
346919370Spst     from the elf (non-stab) symbol table, so always act like an
3470130809Smarcel     incremental load here.  dbx_symfile_read should not generate any new
3471130809Smarcel     minimal symbols, since we will have already read the ELF dynamic symbol
3472130809Smarcel     table and normal symbol entries won't be in the ".stab" section; but in
3473130809Smarcel     case it does, it will install them itself.  */
347498948Sobrien  dbx_symfile_read (objfile, 0);
3475130809Smarcel
3476130809Smarcel  if (back_to)
3477130809Smarcel    do_cleanups (back_to);
347819370Spst}
347919370Spst
348019370Spst/* Scan and build partial symbols for a file with special sections for stabs
348119370Spst   and stabstrings.  The file has already been processed to get its minimal
348219370Spst   symbols, and any other symbols that might be necessary to resolve GSYMs.
348319370Spst
348419370Spst   This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
348519370Spst   rolled into one.
348619370Spst
348719370Spst   OBJFILE is the object file we are reading symbols from.
348819370Spst   ADDR is the address relative to which the symbols are (e.g. the base address
348998948Sobrien   of the text segment).
349019370Spst   MAINLINE is true if we are reading the main symbol table (as opposed to a
349198948Sobrien   shared lib or dynamically loaded file).
349219370Spst   STAB_NAME is the name of the section that contains the stabs.
349319370Spst   STABSTR_NAME is the name of the section that contains the stab strings.
349419370Spst
349519370Spst   This routine is mostly copied from dbx_symfile_init and dbx_symfile_read. */
349619370Spst
349719370Spstvoid
349898948Sobrienstabsect_build_psymtabs (struct objfile *objfile, int mainline, char *stab_name,
349998948Sobrien			 char *stabstr_name, char *text_name)
350019370Spst{
350119370Spst  int val;
350219370Spst  bfd *sym_bfd = objfile->obfd;
350319370Spst  char *name = bfd_get_filename (sym_bfd);
350419370Spst  asection *stabsect;
350519370Spst  asection *stabstrsect;
350619370Spst  asection *text_sect;
350719370Spst
350819370Spst  stabsect = bfd_get_section_by_name (sym_bfd, stab_name);
350919370Spst  stabstrsect = bfd_get_section_by_name (sym_bfd, stabstr_name);
351019370Spst
351119370Spst  if (!stabsect)
351219370Spst    return;
351319370Spst
351419370Spst  if (!stabstrsect)
351519370Spst    error ("stabsect_build_psymtabs:  Found stabs (%s), but not string section (%s)",
351619370Spst	   stab_name, stabstr_name);
351719370Spst
351846283Sdfr  objfile->sym_stab_info = (struct dbx_symfile_info *)
351946283Sdfr    xmalloc (sizeof (struct dbx_symfile_info));
352046283Sdfr  memset (objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
352119370Spst
352219370Spst  text_sect = bfd_get_section_by_name (sym_bfd, text_name);
352319370Spst  if (!text_sect)
352419370Spst    error ("Can't find %s section in symbol file", text_name);
352519370Spst  DBX_TEXT_ADDR (objfile) = bfd_section_vma (sym_bfd, text_sect);
352619370Spst  DBX_TEXT_SIZE (objfile) = bfd_section_size (sym_bfd, text_sect);
352719370Spst
352898948Sobrien  DBX_SYMBOL_SIZE (objfile) = sizeof (struct external_nlist);
352998948Sobrien  DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
353019370Spst    / DBX_SYMBOL_SIZE (objfile);
353119370Spst  DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stabstrsect);
353298948Sobrien  DBX_SYMTAB_OFFSET (objfile) = stabsect->filepos;	/* XXX - FIXME: POKING INSIDE BFD DATA STRUCTURES */
353398948Sobrien
353419370Spst  if (DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
353519370Spst    error ("ridiculous string table size: %d bytes", DBX_STRINGTAB_SIZE (objfile));
353619370Spst  DBX_STRINGTAB (objfile) = (char *)
3537130809Smarcel    obstack_alloc (&objfile->objfile_obstack, DBX_STRINGTAB_SIZE (objfile) + 1);
353819370Spst  OBJSTAT (objfile, sz_strtab += DBX_STRINGTAB_SIZE (objfile) + 1);
353919370Spst
354019370Spst  /* Now read in the string table in one big gulp.  */
354119370Spst
354298948Sobrien  val = bfd_get_section_contents (sym_bfd,	/* bfd */
354398948Sobrien				  stabstrsect,	/* bfd section */
354498948Sobrien				  DBX_STRINGTAB (objfile),	/* input buffer */
354598948Sobrien				  0,	/* offset into section */
354698948Sobrien				  DBX_STRINGTAB_SIZE (objfile));	/* amount to read */
354719370Spst
354819370Spst  if (!val)
354919370Spst    perror_with_name (name);
355019370Spst
355119370Spst  stabsread_new_init ();
355219370Spst  buildsym_new_init ();
355319370Spst  free_header_files ();
355419370Spst  init_header_files ();
355519370Spst
355619370Spst  /* Now, do an incremental load */
355719370Spst
355819370Spst  processing_acc_compilation = 1;
355998948Sobrien  dbx_symfile_read (objfile, 0);
356019370Spst}
356119370Spst
356219370Spststatic struct sym_fns aout_sym_fns =
356319370Spst{
356419370Spst  bfd_target_aout_flavour,
356598948Sobrien  dbx_new_init,			/* sym_new_init: init anything gbl to entire symtab */
356698948Sobrien  dbx_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
356798948Sobrien  dbx_symfile_read,		/* sym_read: read a symbol file into symtab */
356898948Sobrien  dbx_symfile_finish,		/* sym_finish: finished with file, cleanup */
356998948Sobrien  default_symfile_offsets,	/* sym_offsets: parse user's offsets to internal form */
357098948Sobrien  NULL				/* next: pointer to next struct sym_fns */
357119370Spst};
357219370Spst
357319370Spstvoid
357498948Sobrien_initialize_dbxread (void)
357519370Spst{
357698948Sobrien  add_symtab_fns (&aout_sym_fns);
357719370Spst}
3578