119370Spst/* Read ELF (Executable and Linking Format) object files for GDB.
2130803Smarcel
3130803Smarcel   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4130803Smarcel   2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5130803Smarcel
619370Spst   Written by Fred Fish at Cygnus Support.
719370Spst
898944Sobrien   This file is part of GDB.
919370Spst
1098944Sobrien   This program is free software; you can redistribute it and/or modify
1198944Sobrien   it under the terms of the GNU General Public License as published by
1298944Sobrien   the Free Software Foundation; either version 2 of the License, or
1398944Sobrien   (at your option) any later version.
1419370Spst
1598944Sobrien   This program is distributed in the hope that it will be useful,
1698944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1798944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1898944Sobrien   GNU General Public License for more details.
1919370Spst
2098944Sobrien   You should have received a copy of the GNU General Public License
2198944Sobrien   along with this program; if not, write to the Free Software
2298944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2398944Sobrien   Boston, MA 02111-1307, USA.  */
2419370Spst
2519370Spst#include "defs.h"
2619370Spst#include "bfd.h"
2719370Spst#include "gdb_string.h"
2819370Spst#include "elf-bfd.h"
2919370Spst#include "elf/mips.h"
3019370Spst#include "symtab.h"
3119370Spst#include "symfile.h"
3219370Spst#include "objfiles.h"
3319370Spst#include "buildsym.h"
3419370Spst#include "stabsread.h"
3519370Spst#include "gdb-stabs.h"
3619370Spst#include "complaints.h"
3719370Spst#include "demangle.h"
3819370Spst
3998944Sobrienextern void _initialize_elfread (void);
4098944Sobrien
4119370Spst/* The struct elfinfo is available only during ELF symbol table and
4298944Sobrien   psymtab reading.  It is destroyed at the completion of psymtab-reading.
4319370Spst   It's local to elf_symfile_read.  */
4419370Spst
4598944Sobrienstruct elfinfo
4698944Sobrien  {
4798944Sobrien    file_ptr dboffset;		/* Offset to dwarf debug section */
4898944Sobrien    unsigned int dbsize;	/* Size of dwarf debug section */
4998944Sobrien    file_ptr lnoffset;		/* Offset to dwarf line number section */
5098944Sobrien    unsigned int lnsize;	/* Size of dwarf line number section */
5198944Sobrien    asection *stabsect;		/* Section pointer for .stab section */
5298944Sobrien    asection *stabindexsect;	/* Section pointer for .stab.index section */
5398944Sobrien    asection *mdebugsect;	/* Section pointer for .mdebug section */
5498944Sobrien  };
5519370Spst
5698944Sobrienstatic void free_elfinfo (void *);
5719370Spst
5819370Spst/* We are called once per section from elf_symfile_read.  We
5919370Spst   need to examine each section we are passed, check to see
6019370Spst   if it is something we are interested in processing, and
6119370Spst   if so, stash away some access information for the section.
6219370Spst
6319370Spst   For now we recognize the dwarf debug information sections and
6419370Spst   line number sections from matching their section names.  The
6519370Spst   ELF definition is no real help here since it has no direct
6619370Spst   knowledge of DWARF (by design, so any debugging format can be
6719370Spst   used).
6819370Spst
6919370Spst   We also recognize the ".stab" sections used by the Sun compilers
7019370Spst   released with Solaris 2.
7119370Spst
7219370Spst   FIXME: The section names should not be hardwired strings (what
7319370Spst   should they be?  I don't think most object file formats have enough
7419370Spst   section flags to specify what kind of debug section it is
7519370Spst   -kingdon).  */
7619370Spst
7719370Spststatic void
7898944Sobrienelf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
7919370Spst{
80130803Smarcel  struct elfinfo *ei;
8119370Spst
8219370Spst  ei = (struct elfinfo *) eip;
83130803Smarcel  if (strcmp (sectp->name, ".debug") == 0)
8419370Spst    {
8598944Sobrien      ei->dboffset = sectp->filepos;
86218822Sdim      ei->dbsize = bfd_get_section_size (sectp);
8719370Spst    }
88130803Smarcel  else if (strcmp (sectp->name, ".line") == 0)
8919370Spst    {
9098944Sobrien      ei->lnoffset = sectp->filepos;
91218822Sdim      ei->lnsize = bfd_get_section_size (sectp);
9219370Spst    }
93130803Smarcel  else if (strcmp (sectp->name, ".stab") == 0)
9419370Spst    {
9598944Sobrien      ei->stabsect = sectp;
9619370Spst    }
97130803Smarcel  else if (strcmp (sectp->name, ".stab.index") == 0)
9819370Spst    {
9998944Sobrien      ei->stabindexsect = sectp;
10019370Spst    }
101130803Smarcel  else if (strcmp (sectp->name, ".mdebug") == 0)
10219370Spst    {
10398944Sobrien      ei->mdebugsect = sectp;
10419370Spst    }
10519370Spst}
10619370Spst
10719370Spststatic struct minimal_symbol *
108130803Smarcelrecord_minimal_symbol (char *name, CORE_ADDR address,
109130803Smarcel		       enum minimal_symbol_type ms_type,
110130803Smarcel		       asection *bfd_section, struct objfile *objfile)
11119370Spst{
11298944Sobrien  if (ms_type == mst_text || ms_type == mst_file_text)
11398944Sobrien    address = SMASH_TEXT_ADDRESS (address);
11419370Spst
11519370Spst  return prim_record_minimal_symbol_and_info
116130803Smarcel    (name, address, ms_type, NULL, bfd_section->index, bfd_section, objfile);
11719370Spst}
11819370Spst
11919370Spst/*
12019370Spst
12198944Sobrien   LOCAL FUNCTION
12219370Spst
12398944Sobrien   elf_symtab_read -- read the symbol table of an ELF file
12419370Spst
12598944Sobrien   SYNOPSIS
12619370Spst
127218822Sdim   void elf_symtab_read (struct objfile *objfile, int dynamic,
128218822Sdim			 long number_of_symbols, asymbol **symbol_table)
12919370Spst
13098944Sobrien   DESCRIPTION
13119370Spst
132218822Sdim   Given an objfile, a symbol table, and a flag indicating whether the
133218822Sdim   symbol table contains dynamic symbols, add all the global function
134218822Sdim   and data symbols to the minimal symbol table.
13519370Spst
13698944Sobrien   In stabs-in-ELF, as implemented by Sun, there are some local symbols
13798944Sobrien   defined in the ELF symbol table, which can be used to locate
13898944Sobrien   the beginnings of sections from each ".o" file that was linked to
13998944Sobrien   form the executable objfile.  We gather any such info and record it
14098944Sobrien   in data structures hung off the objfile's private data.
14119370Spst
14298944Sobrien */
14319370Spst
14419370Spststatic void
145218822Sdimelf_symtab_read (struct objfile *objfile, int dynamic,
146218822Sdim		 long number_of_symbols, asymbol **symbol_table)
14719370Spst{
14819370Spst  long storage_needed;
14919370Spst  asymbol *sym;
15019370Spst  long i;
15119370Spst  CORE_ADDR symaddr;
15298944Sobrien  CORE_ADDR offset;
15319370Spst  enum minimal_symbol_type ms_type;
15419370Spst  /* If sectinfo is nonNULL, it contains section info that should end up
15519370Spst     filed in the objfile.  */
15619370Spst  struct stab_section_info *sectinfo = NULL;
15719370Spst  /* If filesym is nonzero, it points to a file symbol, but we haven't
15819370Spst     seen any section info for it yet.  */
15919370Spst  asymbol *filesym = 0;
16019370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING
161130803Smarcel  /* Name of filesym, as saved on the objfile_obstack.  */
162130803Smarcel  char *filesymname = obsavestring ("", 0, &objfile->objfile_obstack);
16319370Spst#endif
16446283Sdfr  struct dbx_symfile_info *dbx = objfile->sym_stab_info;
16598944Sobrien  int stripped = (bfd_get_symcount (objfile->obfd) == 0);
16698944Sobrien
167218822Sdim  if (1)
16819370Spst    {
16919370Spst      for (i = 0; i < number_of_symbols; i++)
17019370Spst	{
17119370Spst	  sym = symbol_table[i];
17298944Sobrien	  if (sym->name == NULL || *sym->name == '\0')
17319370Spst	    {
17419370Spst	      /* Skip names that don't exist (shouldn't happen), or names
17598944Sobrien	         that are null strings (may happen). */
17619370Spst	      continue;
17719370Spst	    }
17819370Spst
17998944Sobrien          offset = ANOFFSET (objfile->section_offsets, sym->section->index);
18019370Spst	  if (dynamic
18198944Sobrien	      && sym->section == &bfd_und_section
18298944Sobrien	      && (sym->flags & BSF_FUNCTION))
18319370Spst	    {
18419370Spst	      struct minimal_symbol *msym;
18519370Spst
18619370Spst	      /* Symbol is a reference to a function defined in
18798944Sobrien	         a shared library.
18898944Sobrien	         If its value is non zero then it is usually the address
18998944Sobrien	         of the corresponding entry in the procedure linkage table,
19098944Sobrien	         plus the desired section offset.
19198944Sobrien	         If its value is zero then the dynamic linker has to resolve
19298944Sobrien	         the symbol. We are unable to find any meaningful address
19398944Sobrien	         for this symbol in the executable file, so we skip it.  */
19498944Sobrien	      symaddr = sym->value;
19519370Spst	      if (symaddr == 0)
19619370Spst		continue;
19798944Sobrien	      symaddr += offset;
198130803Smarcel	      msym = record_minimal_symbol
19998944Sobrien		((char *) sym->name, symaddr,
200130803Smarcel		 mst_solib_trampoline, sym->section, objfile);
20119370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING
20219370Spst	      if (msym != NULL)
20319370Spst		msym->filename = filesymname;
20419370Spst#endif
20519370Spst	      continue;
20619370Spst	    }
20719370Spst
20819370Spst	  /* If it is a nonstripped executable, do not enter dynamic
20919370Spst	     symbols, as the dynamic symbol table is usually a subset
21019370Spst	     of the main symbol table.  */
21119370Spst	  if (dynamic && !stripped)
21219370Spst	    continue;
21398944Sobrien	  if (sym->flags & BSF_FILE)
21419370Spst	    {
21519370Spst	      /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
21698944Sobrien	         Chain any old one onto the objfile; remember new sym.  */
21719370Spst	      if (sectinfo != NULL)
21819370Spst		{
21998944Sobrien		  sectinfo->next = dbx->stab_section_info;
22098944Sobrien		  dbx->stab_section_info = sectinfo;
22119370Spst		  sectinfo = NULL;
22219370Spst		}
22319370Spst	      filesym = sym;
22419370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING
22519370Spst	      filesymname =
22698944Sobrien		obsavestring ((char *) filesym->name, strlen (filesym->name),
227130803Smarcel			      &objfile->objfile_obstack);
22819370Spst#endif
22919370Spst	    }
23098944Sobrien	  else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
23119370Spst	    {
23219370Spst	      struct minimal_symbol *msym;
23319370Spst
23419370Spst	      /* Select global/local/weak symbols.  Note that bfd puts abs
23598944Sobrien	         symbols in their own section, so all symbols we are
23698944Sobrien	         interested in will have a section. */
23719370Spst	      /* Bfd symbols are section relative. */
23898944Sobrien	      symaddr = sym->value + sym->section->vma;
23998944Sobrien	      /* Relocate all non-absolute symbols by the section offset.  */
24098944Sobrien	      if (sym->section != &bfd_abs_section)
24119370Spst		{
24298944Sobrien		  symaddr += offset;
24319370Spst		}
24419370Spst	      /* For non-absolute symbols, use the type of the section
24598944Sobrien	         they are relative to, to intuit text/data.  Bfd provides
24698944Sobrien	         no way of figuring this out for absolute symbols. */
24798944Sobrien	      if (sym->section == &bfd_abs_section)
24819370Spst		{
24919370Spst		  /* This is a hack to get the minimal symbol type
25098944Sobrien		     right for Irix 5, which has absolute addresses
25119370Spst		     with special section indices for dynamic symbols. */
25219370Spst		  unsigned short shndx =
25398944Sobrien		  ((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;
25419370Spst
25519370Spst		  switch (shndx)
25619370Spst		    {
25719370Spst		    case SHN_MIPS_TEXT:
25819370Spst		      ms_type = mst_text;
25919370Spst		      break;
26019370Spst		    case SHN_MIPS_DATA:
26119370Spst		      ms_type = mst_data;
26219370Spst		      break;
26319370Spst		    case SHN_MIPS_ACOMMON:
26419370Spst		      ms_type = mst_bss;
26519370Spst		      break;
26619370Spst		    default:
26719370Spst		      ms_type = mst_abs;
26819370Spst		    }
26946283Sdfr
27046283Sdfr		  /* If it is an Irix dynamic symbol, skip section name
27198944Sobrien		     symbols, relocate all others by section offset. */
27246283Sdfr		  if (ms_type != mst_abs)
27346283Sdfr		    {
27446283Sdfr		      if (sym->name[0] == '.')
27546283Sdfr			continue;
27698944Sobrien		      symaddr += offset;
27746283Sdfr		    }
27819370Spst		}
27998944Sobrien	      else if (sym->section->flags & SEC_CODE)
28019370Spst		{
28198944Sobrien		  if (sym->flags & BSF_GLOBAL)
28219370Spst		    {
28319370Spst		      ms_type = mst_text;
28419370Spst		    }
28519370Spst		  else if ((sym->name[0] == '.' && sym->name[1] == 'L')
28698944Sobrien			   || ((sym->flags & BSF_LOCAL)
28719370Spst			       && sym->name[0] == '$'
28819370Spst			       && sym->name[1] == 'L'))
289130803Smarcel		    /* Looks like a compiler-generated label.  Skip
290130803Smarcel		       it.  The assembler should be skipping these (to
291130803Smarcel		       keep executables small), but apparently with
292130803Smarcel		       gcc on the (deleted) delta m88k SVR4, it loses.
293130803Smarcel		       So to have us check too should be harmless (but
294130803Smarcel		       I encourage people to fix this in the assembler
295130803Smarcel		       instead of adding checks here).  */
29619370Spst		    continue;
29719370Spst		  else
29819370Spst		    {
29919370Spst		      ms_type = mst_file_text;
30019370Spst		    }
30119370Spst		}
30298944Sobrien	      else if (sym->section->flags & SEC_ALLOC)
30319370Spst		{
30498944Sobrien		  if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
30519370Spst		    {
30698944Sobrien		      if (sym->section->flags & SEC_LOAD)
30719370Spst			{
30819370Spst			  ms_type = mst_data;
30919370Spst			}
31019370Spst		      else
31119370Spst			{
31219370Spst			  ms_type = mst_bss;
31319370Spst			}
31419370Spst		    }
31598944Sobrien		  else if (sym->flags & BSF_LOCAL)
31619370Spst		    {
317130803Smarcel		      /* Named Local variable in a Data section.
318130803Smarcel		         Check its name for stabs-in-elf.  */
319130803Smarcel		      int special_local_sect;
320130803Smarcel		      if (strcmp ("Bbss.bss", sym->name) == 0)
321130803Smarcel			special_local_sect = SECT_OFF_BSS (objfile);
322130803Smarcel		      else if (strcmp ("Ddata.data", sym->name) == 0)
323130803Smarcel			special_local_sect = SECT_OFF_DATA (objfile);
324130803Smarcel		      else if (strcmp ("Drodata.rodata", sym->name) == 0)
325130803Smarcel			special_local_sect = SECT_OFF_RODATA (objfile);
326130803Smarcel		      else
327130803Smarcel			special_local_sect = -1;
328130803Smarcel		      if (special_local_sect >= 0)
32919370Spst			{
33019370Spst			  /* Found a special local symbol.  Allocate a
33119370Spst			     sectinfo, if needed, and fill it in.  */
33219370Spst			  if (sectinfo == NULL)
33319370Spst			    {
334130803Smarcel			      int max_index;
335130803Smarcel			      size_t size;
336130803Smarcel
337130803Smarcel			      max_index
338130803Smarcel				= max (SECT_OFF_BSS (objfile),
339130803Smarcel				       max (SECT_OFF_DATA (objfile),
340130803Smarcel					    SECT_OFF_RODATA (objfile)));
341130803Smarcel
342130803Smarcel                              /* max_index is the largest index we'll
343130803Smarcel                                 use into this array, so we must
344130803Smarcel                                 allocate max_index+1 elements for it.
345130803Smarcel                                 However, 'struct stab_section_info'
346130803Smarcel                                 already includes one element, so we
347130803Smarcel                                 need to allocate max_index aadditional
348130803Smarcel                                 elements.  */
349130803Smarcel			      size = (sizeof (struct stab_section_info)
350130803Smarcel				      + (sizeof (CORE_ADDR)
351130803Smarcel					 * max_index));
35219370Spst			      sectinfo = (struct stab_section_info *)
353130803Smarcel				xmmalloc (objfile->md, size);
354130803Smarcel			      memset (sectinfo, 0, size);
355130803Smarcel			      sectinfo->num_sections = max_index;
35619370Spst			      if (filesym == NULL)
35719370Spst				{
358130803Smarcel				  complaint (&symfile_complaints,
359130803Smarcel					     "elf/stab section information %s without a preceding file symbol",
360130803Smarcel					     sym->name);
36119370Spst				}
36219370Spst			      else
36319370Spst				{
36498944Sobrien				  sectinfo->filename =
36598944Sobrien				    (char *) filesym->name;
36619370Spst				}
36719370Spst			    }
368130803Smarcel			  if (sectinfo->sections[special_local_sect] != 0)
369130803Smarcel			    complaint (&symfile_complaints,
370130803Smarcel				       "duplicated elf/stab section information for %s",
371130803Smarcel				       sectinfo->filename);
372130803Smarcel			  /* BFD symbols are section relative.  */
37398944Sobrien			  symaddr = sym->value + sym->section->vma;
374130803Smarcel			  /* Relocate non-absolute symbols by the
375130803Smarcel                             section offset.  */
37698944Sobrien			  if (sym->section != &bfd_abs_section)
377130803Smarcel			    symaddr += offset;
378130803Smarcel			  sectinfo->sections[special_local_sect] = symaddr;
37919370Spst			  /* The special local symbols don't go in the
380130803Smarcel			     minimal symbol table, so ignore this one.  */
38119370Spst			  continue;
38219370Spst			}
38319370Spst		      /* Not a special stabs-in-elf symbol, do regular
384130803Smarcel		         symbol processing.  */
38598944Sobrien		      if (sym->section->flags & SEC_LOAD)
38619370Spst			{
38719370Spst			  ms_type = mst_file_data;
38819370Spst			}
38919370Spst		      else
39019370Spst			{
39119370Spst			  ms_type = mst_file_bss;
39219370Spst			}
39319370Spst		    }
39419370Spst		  else
39519370Spst		    {
39619370Spst		      ms_type = mst_unknown;
39719370Spst		    }
39819370Spst		}
39919370Spst	      else
40019370Spst		{
40119370Spst		  /* FIXME:  Solaris2 shared libraries include lots of
40219370Spst		     odd "absolute" and "undefined" symbols, that play
40319370Spst		     hob with actions like finding what function the PC
40419370Spst		     is in.  Ignore them if they aren't text, data, or bss.  */
40519370Spst		  /* ms_type = mst_unknown; */
40698944Sobrien		  continue;	/* Skip this symbol. */
40719370Spst		}
408130803Smarcel	      msym = record_minimal_symbol
40998944Sobrien		((char *) sym->name, symaddr,
410130803Smarcel		 ms_type, sym->section, objfile);
411130803Smarcel	      if (msym)
412130803Smarcel	      {
413130803Smarcel		/* Pass symbol size field in via BFD.  FIXME!!!  */
414130803Smarcel		unsigned long size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
415130803Smarcel		MSYMBOL_SIZE(msym) = size;
416130803Smarcel	      }
41719370Spst#ifdef SOFUN_ADDRESS_MAYBE_MISSING
41819370Spst	      if (msym != NULL)
41919370Spst		msym->filename = filesymname;
42019370Spst#endif
42198944Sobrien	      ELF_MAKE_MSYMBOL_SPECIAL (sym, msym);
42219370Spst	    }
42319370Spst	}
42419370Spst    }
42519370Spst}
42619370Spst
42719370Spst/* Scan and build partial symbols for a symbol file.
42819370Spst   We have been initialized by a call to elf_symfile_init, which
42919370Spst   currently does nothing.
43019370Spst
43119370Spst   SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
43219370Spst   in each section.  We simplify it down to a single offset for all
43319370Spst   symbols.  FIXME.
43419370Spst
43519370Spst   MAINLINE is true if we are reading the main symbol
43619370Spst   table (as opposed to a shared lib or dynamically loaded file).
43719370Spst
43819370Spst   This function only does the minimum work necessary for letting the
43919370Spst   user "name" things symbolically; it does not read the entire symtab.
44019370Spst   Instead, it reads the external and static symbols and puts them in partial
44119370Spst   symbol tables.  When more extensive information is requested of a
44219370Spst   file, the corresponding partial symbol table is mutated into a full
44319370Spst   fledged symbol table by going back and reading the symbols
44419370Spst   for real.
44519370Spst
44619370Spst   We look for sections with specific names, to tell us what debug
44719370Spst   format to look for:  FIXME!!!
44819370Spst
44919370Spst   dwarf_build_psymtabs() builds psymtabs for DWARF symbols;
45019370Spst   elfstab_build_psymtabs() handles STABS symbols;
45119370Spst   mdebug_build_psymtabs() handles ECOFF debugging information.
45219370Spst
45319370Spst   Note that ELF files have a "minimal" symbol table, which looks a lot
45419370Spst   like a COFF symbol table, but has only the minimal information necessary
45519370Spst   for linking.  We process this also, and use the information to
45619370Spst   build gdb's minimal symbol table.  This gives us some minimal debugging
45719370Spst   capability even for files compiled without -g.  */
45819370Spst
45919370Spststatic void
46098944Sobrienelf_symfile_read (struct objfile *objfile, int mainline)
46119370Spst{
46219370Spst  bfd *abfd = objfile->obfd;
46319370Spst  struct elfinfo ei;
46419370Spst  struct cleanup *back_to;
46519370Spst  CORE_ADDR offset;
466218822Sdim  long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
467218822Sdim  asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
468218822Sdim  asymbol *synthsyms;
46919370Spst
47019370Spst  init_minimal_symbol_collection ();
47198944Sobrien  back_to = make_cleanup_discard_minimal_symbols ();
47219370Spst
47319370Spst  memset ((char *) &ei, 0, sizeof (ei));
47419370Spst
47519370Spst  /* Allocate struct to keep track of the symfile */
47646283Sdfr  objfile->sym_stab_info = (struct dbx_symfile_info *)
47798944Sobrien    xmmalloc (objfile->md, sizeof (struct dbx_symfile_info));
47819370Spst  memset ((char *) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
47998944Sobrien  make_cleanup (free_elfinfo, (void *) objfile);
48019370Spst
48119370Spst  /* Process the normal ELF symbol table first.  This may write some
48219370Spst     chain of info into the dbx_symfile_info in objfile->sym_stab_info,
48319370Spst     which can later be used by elfstab_offset_sections.  */
48419370Spst
485218822Sdim  storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
486218822Sdim  if (storage_needed < 0)
487218822Sdim    error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
488218822Sdim	   bfd_errmsg (bfd_get_error ()));
48919370Spst
490218822Sdim  if (storage_needed > 0)
491218822Sdim    {
492218822Sdim      symbol_table = (asymbol **) xmalloc (storage_needed);
493218822Sdim      make_cleanup (xfree, symbol_table);
494218822Sdim      symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
495218822Sdim
496218822Sdim      if (symcount < 0)
497218822Sdim	error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
498218822Sdim	       bfd_errmsg (bfd_get_error ()));
499218822Sdim
500218822Sdim      elf_symtab_read (objfile, 0, symcount, symbol_table);
501218822Sdim    }
502218822Sdim
50319370Spst  /* Add the dynamic symbols.  */
50419370Spst
505218822Sdim  storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);
50619370Spst
507218822Sdim  if (storage_needed > 0)
508218822Sdim    {
509218822Sdim      dyn_symbol_table = (asymbol **) xmalloc (storage_needed);
510218822Sdim      make_cleanup (xfree, dyn_symbol_table);
511218822Sdim      dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
512218822Sdim						     dyn_symbol_table);
513218822Sdim
514218822Sdim      if (dynsymcount < 0)
515218822Sdim	error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
516218822Sdim	       bfd_errmsg (bfd_get_error ()));
517218822Sdim
518218822Sdim      elf_symtab_read (objfile, 1, dynsymcount, dyn_symbol_table);
519218822Sdim    }
520218822Sdim
521218822Sdim  /* Add synthetic symbols - for instance, names for any PLT entries.  */
522218822Sdim
523218822Sdim  synthcount = bfd_get_synthetic_symtab (abfd, symcount, symbol_table,
524218822Sdim					 dynsymcount, dyn_symbol_table,
525218822Sdim					 &synthsyms);
526218822Sdim  if (synthcount > 0)
527218822Sdim    {
528218822Sdim      asymbol **synth_symbol_table;
529218822Sdim      long i;
530218822Sdim
531218822Sdim      make_cleanup (xfree, synthsyms);
532218822Sdim      synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount);
533218822Sdim      for (i = 0; i < synthcount; i++)
534218822Sdim	{
535218822Sdim	  synth_symbol_table[i] = synthsyms + i;
536218822Sdim	  /* Synthetic symbols are not, strictly speaking, either local
537218822Sdim	     or global.  But we can treat them as global symbols, since
538218822Sdim	     they are effectively dynamic symbols.  */
539218822Sdim	  synth_symbol_table[i]->flags |= BSF_GLOBAL;
540218822Sdim	}
541218822Sdim      make_cleanup (xfree, synth_symbol_table);
542218822Sdim      elf_symtab_read (objfile, 0, synthcount, synth_symbol_table);
543218822Sdim    }
544218822Sdim
545130803Smarcel  /* Install any minimal symbols that have been collected as the current
546130803Smarcel     minimal symbols for this objfile.  The debug readers below this point
547130803Smarcel     should not generate new minimal symbols; if they do it's their
548130803Smarcel     responsibility to install them.  "mdebug" appears to be the only one
549130803Smarcel     which will do this.  */
550130803Smarcel
551130803Smarcel  install_minimal_symbols (objfile);
552130803Smarcel  do_cleanups (back_to);
553130803Smarcel
55419370Spst  /* Now process debugging information, which is contained in
55546283Sdfr     special ELF sections. */
55619370Spst
55746283Sdfr  /* If we are reinitializing, or if we have never loaded syms yet,
55846283Sdfr     set table to empty.  MAINLINE is cleared so that *_read_psymtab
55946283Sdfr     functions do not all also re-initialize the psymbol table. */
56046283Sdfr  if (mainline)
56146283Sdfr    {
56246283Sdfr      init_psymbol_list (objfile, 0);
56346283Sdfr      mainline = 0;
56446283Sdfr    }
56546283Sdfr
56646283Sdfr  /* We first have to find them... */
56798944Sobrien  bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
56846283Sdfr
56946283Sdfr  /* ELF debugging information is inserted into the psymtab in the
57046283Sdfr     order of least informative first - most informative last.  Since
57146283Sdfr     the psymtab table is searched `most recent insertion first' this
57246283Sdfr     increases the probability that more detailed debug information
57346283Sdfr     for a section is found.
57446283Sdfr
57546283Sdfr     For instance, an object file might contain both .mdebug (XCOFF)
57646283Sdfr     and .debug_info (DWARF2) sections then .mdebug is inserted first
57746283Sdfr     (searched last) and DWARF2 is inserted last (searched first).  If
57846283Sdfr     we don't do this then the XCOFF info is found first - for code in
57946283Sdfr     an included file XCOFF info is useless. */
58046283Sdfr
58146283Sdfr  if (ei.mdebugsect)
58219370Spst    {
58346283Sdfr      const struct ecoff_debug_swap *swap;
58446283Sdfr
58546283Sdfr      /* .mdebug section, presumably holding ECOFF debugging
58698944Sobrien         information.  */
58746283Sdfr      swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
58846283Sdfr      if (swap)
58998944Sobrien	elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
59019370Spst    }
59119370Spst  if (ei.stabsect)
59219370Spst    {
59319370Spst      asection *str_sect;
59419370Spst
59519370Spst      /* Stab sections have an associated string table that looks like
59698944Sobrien         a separate section.  */
59719370Spst      str_sect = bfd_get_section_by_name (abfd, ".stabstr");
59819370Spst
59919370Spst      /* FIXME should probably warn about a stab section without a stabstr.  */
60019370Spst      if (str_sect)
60119370Spst	elfstab_build_psymtabs (objfile,
60219370Spst				mainline,
603130803Smarcel				ei.stabsect,
60419370Spst				str_sect->filepos,
60519370Spst				bfd_section_size (abfd, str_sect));
60619370Spst    }
60746283Sdfr  if (dwarf2_has_info (abfd))
60819370Spst    {
60946283Sdfr      /* DWARF 2 sections */
61098944Sobrien      dwarf2_build_psymtabs (objfile, mainline);
61119370Spst    }
61246283Sdfr  else if (ei.dboffset && ei.lnoffset)
61346283Sdfr    {
61446283Sdfr      /* DWARF sections */
61546283Sdfr      dwarf_build_psymtabs (objfile,
61698944Sobrien			    mainline,
61746283Sdfr			    ei.dboffset, ei.dbsize,
61846283Sdfr			    ei.lnoffset, ei.lnsize);
61946283Sdfr    }
62019370Spst
621130803Smarcel  /* FIXME: kettenis/20030504: This still needs to be integrated with
622130803Smarcel     dwarf2read.c in a better way.  */
623130803Smarcel  dwarf2_build_frame_info (objfile);
62419370Spst}
62519370Spst
62619370Spst/* This cleans up the objfile's sym_stab_info pointer, and the chain of
62719370Spst   stab_section_info's, that might be dangling from it.  */
62819370Spst
62919370Spststatic void
63098944Sobrienfree_elfinfo (void *objp)
63119370Spst{
63298944Sobrien  struct objfile *objfile = (struct objfile *) objp;
63346283Sdfr  struct dbx_symfile_info *dbxinfo = objfile->sym_stab_info;
63419370Spst  struct stab_section_info *ssi, *nssi;
63519370Spst
63619370Spst  ssi = dbxinfo->stab_section_info;
63719370Spst  while (ssi)
63819370Spst    {
63919370Spst      nssi = ssi->next;
64098944Sobrien      xmfree (objfile->md, ssi);
64119370Spst      ssi = nssi;
64219370Spst    }
64319370Spst
64419370Spst  dbxinfo->stab_section_info = 0;	/* Just say No mo info about this.  */
64519370Spst}
64619370Spst
64719370Spst
64819370Spst/* Initialize anything that needs initializing when a completely new symbol
64919370Spst   file is specified (not just adding some symbols from another file, e.g. a
65019370Spst   shared library).
65119370Spst
65219370Spst   We reinitialize buildsym, since we may be reading stabs from an ELF file.  */
65319370Spst
65419370Spststatic void
65598944Sobrienelf_new_init (struct objfile *ignore)
65619370Spst{
65719370Spst  stabsread_new_init ();
65819370Spst  buildsym_new_init ();
65919370Spst}
66019370Spst
66119370Spst/* Perform any local cleanups required when we are done with a particular
66219370Spst   objfile.  I.E, we are in the process of discarding all symbol information
66319370Spst   for an objfile, freeing up all memory held for it, and unlinking the
66419370Spst   objfile struct from the global list of known objfiles. */
66519370Spst
66619370Spststatic void
66798944Sobrienelf_symfile_finish (struct objfile *objfile)
66819370Spst{
66998944Sobrien  if (objfile->sym_stab_info != NULL)
67019370Spst    {
67198944Sobrien      xmfree (objfile->md, objfile->sym_stab_info);
67219370Spst    }
67319370Spst}
67419370Spst
67519370Spst/* ELF specific initialization routine for reading symbols.
67619370Spst
67719370Spst   It is passed a pointer to a struct sym_fns which contains, among other
67819370Spst   things, the BFD for the file whose symbols are being read, and a slot for
67919370Spst   a pointer to "private data" which we can fill with goodies.
68019370Spst
68119370Spst   For now at least, we have nothing in particular to do, so this function is
68219370Spst   just a stub. */
68319370Spst
68419370Spststatic void
68598944Sobrienelf_symfile_init (struct objfile *objfile)
68619370Spst{
68719370Spst  /* ELF objects may be reordered, so set OBJF_REORDERED.  If we
68819370Spst     find this causes a significant slowdown in gdb then we could
68919370Spst     set it in the debug symbol readers only when necessary.  */
69019370Spst  objfile->flags |= OBJF_REORDERED;
69119370Spst}
69219370Spst
69319370Spst/* When handling an ELF file that contains Sun STABS debug info,
69419370Spst   some of the debug info is relative to the particular chunk of the
69519370Spst   section that was generated in its individual .o file.  E.g.
69619370Spst   offsets to static variables are relative to the start of the data
69719370Spst   segment *for that module before linking*.  This information is
69819370Spst   painfully squirreled away in the ELF symbol table as local symbols
69919370Spst   with wierd names.  Go get 'em when needed.  */
70019370Spst
70119370Spstvoid
70298944Sobrienelfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst)
70319370Spst{
70419370Spst  char *filename = pst->filename;
70546283Sdfr  struct dbx_symfile_info *dbx = objfile->sym_stab_info;
70619370Spst  struct stab_section_info *maybe = dbx->stab_section_info;
70719370Spst  struct stab_section_info *questionable = 0;
70819370Spst  int i;
70919370Spst  char *p;
71019370Spst
71119370Spst  /* The ELF symbol info doesn't include path names, so strip the path
71219370Spst     (if any) from the psymtab filename.  */
71319370Spst  while (0 != (p = strchr (filename, '/')))
71498944Sobrien    filename = p + 1;
71519370Spst
71619370Spst  /* FIXME:  This linear search could speed up significantly
71719370Spst     if it was chained in the right order to match how we search it,
71819370Spst     and if we unchained when we found a match. */
71919370Spst  for (; maybe; maybe = maybe->next)
72019370Spst    {
72119370Spst      if (filename[0] == maybe->filename[0]
722130803Smarcel	  && strcmp (filename, maybe->filename) == 0)
72319370Spst	{
72419370Spst	  /* We found a match.  But there might be several source files
72519370Spst	     (from different directories) with the same name.  */
72619370Spst	  if (0 == maybe->found)
72719370Spst	    break;
72898944Sobrien	  questionable = maybe;	/* Might use it later.  */
72919370Spst	}
73019370Spst    }
73119370Spst
73219370Spst  if (maybe == 0 && questionable != 0)
73319370Spst    {
734130803Smarcel      complaint (&symfile_complaints,
735130803Smarcel		 "elf/stab section information questionable for %s", filename);
73619370Spst      maybe = questionable;
73719370Spst    }
73819370Spst
73919370Spst  if (maybe)
74019370Spst    {
74119370Spst      /* Found it!  Allocate a new psymtab struct, and fill it in.  */
74219370Spst      maybe->found++;
74319370Spst      pst->section_offsets = (struct section_offsets *)
744130803Smarcel	obstack_alloc (&objfile->objfile_obstack,
745130803Smarcel		       SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
746130803Smarcel      for (i = 0; i < maybe->num_sections; i++)
74798944Sobrien	(pst->section_offsets)->offsets[i] = maybe->sections[i];
74819370Spst      return;
74919370Spst    }
75019370Spst
75119370Spst  /* We were unable to find any offsets for this file.  Complain.  */
75298944Sobrien  if (dbx->stab_section_info)	/* If there *is* any info, */
753130803Smarcel    complaint (&symfile_complaints,
754130803Smarcel	       "elf/stab section information missing for %s", filename);
75519370Spst}
75619370Spst
75719370Spst/* Register that we are able to handle ELF object file formats.  */
75819370Spst
75919370Spststatic struct sym_fns elf_sym_fns =
76019370Spst{
76119370Spst  bfd_target_elf_flavour,
76298944Sobrien  elf_new_init,			/* sym_new_init: init anything gbl to entire symtab */
76398944Sobrien  elf_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
76498944Sobrien  elf_symfile_read,		/* sym_read: read a symbol file into symtab */
76598944Sobrien  elf_symfile_finish,		/* sym_finish: finished with file, cleanup */
76698944Sobrien  default_symfile_offsets,	/* sym_offsets:  Translate ext. to int. relocation */
76798944Sobrien  NULL				/* next: pointer to next struct sym_fns */
76819370Spst};
76919370Spst
77019370Spstvoid
77198944Sobrien_initialize_elfread (void)
77219370Spst{
77319370Spst  add_symtab_fns (&elf_sym_fns);
77419370Spst}
775