include/elf/
[deliverable/binutils-gdb.git] / gdb / elfread.c
index 646b314a1ca281415687945021ce3a8520c6bc12..5a201c9197a06174f9cb919e8a9f464752fc98d0 100644 (file)
@@ -1,7 +1,8 @@
 /* Read ELF (Executable and Linking Format) object files for GDB.
 
    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+   Free Software Foundation, Inc.
 
    Written by Fred Fish at Cygnus Support.
 
@@ -9,7 +10,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
 #include "bfd.h"
 #include "gdb_string.h"
 #include "elf-bfd.h"
+#include "elf/common.h"
+#include "elf/internal.h"
 #include "elf/mips.h"
 #include "symtab.h"
 #include "symfile.h"
@@ -44,10 +45,6 @@ extern void _initialize_elfread (void);
 
 struct elfinfo
   {
-    file_ptr dboffset;         /* Offset to dwarf debug section */
-    unsigned int dbsize;       /* Size of dwarf debug section */
-    file_ptr lnoffset;         /* Offset to dwarf line number section */
-    unsigned int lnsize;       /* Size of dwarf line number section */
     asection *stabsect;                /* Section pointer for .stab section */
     asection *stabindexsect;   /* Section pointer for .stab.index section */
     asection *mdebugsect;      /* Section pointer for .mdebug section */
@@ -55,6 +52,76 @@ struct elfinfo
 
 static void free_elfinfo (void *);
 
+/* Locate the segments in ABFD.  */
+
+static struct symfile_segment_data *
+elf_symfile_segments (bfd *abfd)
+{
+  Elf_Internal_Phdr *phdrs, **segments;
+  long phdrs_size;
+  int num_phdrs, num_segments, num_sections, i;
+  asection *sect;
+  struct symfile_segment_data *data;
+
+  phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
+  if (phdrs_size == -1)
+    return NULL;
+
+  phdrs = alloca (phdrs_size);
+  num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
+  if (num_phdrs == -1)
+    return NULL;
+
+  num_segments = 0;
+  segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs);
+  for (i = 0; i < num_phdrs; i++)
+    if (phdrs[i].p_type == PT_LOAD)
+      segments[num_segments++] = &phdrs[i];
+
+  if (num_segments == 0)
+    return NULL;
+
+  data = XZALLOC (struct symfile_segment_data);
+  data->num_segments = num_segments;
+  data->segment_bases = XCALLOC (num_segments, CORE_ADDR);
+  data->segment_sizes = XCALLOC (num_segments, CORE_ADDR);
+
+  for (i = 0; i < num_segments; i++)
+    {
+      data->segment_bases[i] = segments[i]->p_vaddr;
+      data->segment_sizes[i] = segments[i]->p_memsz;
+    }
+
+  num_sections = bfd_count_sections (abfd);
+  data->segment_info = XCALLOC (num_sections, int);
+
+  for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
+    {
+      int j;
+      CORE_ADDR vma;
+
+      if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
+       continue;
+
+      vma = bfd_get_section_vma (abfd, sect);
+
+      for (j = 0; j < num_segments; j++)
+       if (segments[j]->p_memsz > 0
+           && vma >= segments[j]->p_vaddr
+           && vma < segments[j]->p_vaddr + segments[j]->p_memsz)
+         {
+           data->segment_info[i] = j + 1;
+           break;
+         }
+
+      if (bfd_get_section_size (sect) > 0 && j == num_segments)
+       warning (_("Loadable segment \"%s\" outside of ELF segments"),
+                bfd_section_name (abfd, sect));
+    }
+
+  return data;
+}
+
 /* We are called once per section from elf_symfile_read.  We
    need to examine each section we are passed, check to see
    if it is something we are interested in processing, and
@@ -80,17 +147,7 @@ elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
   struct elfinfo *ei;
 
   ei = (struct elfinfo *) eip;
-  if (strcmp (sectp->name, ".debug") == 0)
-    {
-      ei->dboffset = sectp->filepos;
-      ei->dbsize = bfd_get_section_size (sectp);
-    }
-  else if (strcmp (sectp->name, ".line") == 0)
-    {
-      ei->lnoffset = sectp->filepos;
-      ei->lnsize = bfd_get_section_size (sectp);
-    }
-  else if (strcmp (sectp->name, ".stab") == 0)
+  if (strcmp (sectp->name, ".stab") == 0)
     {
       ei->stabsect = sectp;
     }
@@ -110,7 +167,7 @@ record_minimal_symbol (char *name, CORE_ADDR address,
                       asection *bfd_section, struct objfile *objfile)
 {
   if (ms_type == mst_text || ms_type == mst_file_text)
-    address = SMASH_TEXT_ADDRESS (address);
+    address = gdbarch_smash_text_address (current_gdbarch, address);
 
   return prim_record_minimal_symbol_and_info
     (name, address, ms_type, NULL, bfd_section->index, bfd_section, objfile);
@@ -124,14 +181,14 @@ record_minimal_symbol (char *name, CORE_ADDR address,
 
    SYNOPSIS
 
-   void elf_symtab_read (struct objfile *objfile, int dynamic,
+   void elf_symtab_read (struct objfile *objfile, int type,
                         long number_of_symbols, asymbol **symbol_table)
 
    DESCRIPTION
 
    Given an objfile, a symbol table, and a flag indicating whether the
-   symbol table contains dynamic symbols, add all the global function
-   and data symbols to the minimal symbol table.
+   symbol table contains regular, dynamic, or synthetic symbols, add all
+   the global function and data symbols to the minimal symbol table.
 
    In stabs-in-ELF, as implemented by Sun, there are some local symbols
    defined in the ELF symbol table, which can be used to locate
@@ -141,8 +198,12 @@ record_minimal_symbol (char *name, CORE_ADDR address,
 
  */
 
+#define ST_REGULAR 0
+#define ST_DYNAMIC 1
+#define ST_SYNTHETIC 2
+
 static void
-elf_symtab_read (struct objfile *objfile, int dynamic,
+elf_symtab_read (struct objfile *objfile, int type,
                 long number_of_symbols, asymbol **symbol_table)
 {
   long storage_needed;
@@ -157,10 +218,8 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
   /* If filesym is nonzero, it points to a file symbol, but we haven't
      seen any section info for it yet.  */
   asymbol *filesym = 0;
-#ifdef SOFUN_ADDRESS_MAYBE_MISSING
   /* Name of filesym, as saved on the objfile_obstack.  */
   char *filesymname = obsavestring ("", 0, &objfile->objfile_obstack);
-#endif
   struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info;
   int stripped = (bfd_get_symcount (objfile->obfd) == 0);
 
@@ -181,11 +240,13 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
        continue;
 
       offset = ANOFFSET (objfile->section_offsets, sym->section->index);
-      if (dynamic
+      if (type == ST_DYNAMIC
          && sym->section == &bfd_und_section
          && (sym->flags & BSF_FUNCTION))
        {
          struct minimal_symbol *msym;
+         bfd *abfd = objfile->obfd;
+         asection *sect; 
 
          /* Symbol is a reference to a function defined in
             a shared library.
@@ -198,21 +259,37 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
          symaddr = sym->value;
          if (symaddr == 0)
            continue;
-         symaddr += offset;
+
+         /* sym->section is the undefined section.  However, we want to
+            record the section where the PLT stub resides with the
+            minimal symbol.  Search the section table for the one that
+            covers the stub's address.  */
+         for (sect = abfd->sections; sect != NULL; sect = sect->next)
+           {
+             if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
+               continue;
+
+             if (symaddr >= bfd_get_section_vma (abfd, sect)
+                 && symaddr < bfd_get_section_vma (abfd, sect)
+                              + bfd_get_section_size (sect))
+               break;
+           }
+         if (!sect)
+           continue;
+
+         symaddr += ANOFFSET (objfile->section_offsets, sect->index);
+
          msym = record_minimal_symbol
-           ((char *) sym->name, symaddr,
-            mst_solib_trampoline, sym->section, objfile);
-#ifdef SOFUN_ADDRESS_MAYBE_MISSING
+           ((char *) sym->name, symaddr, mst_solib_trampoline, sect, objfile);
          if (msym != NULL)
            msym->filename = filesymname;
-#endif
          continue;
        }
 
       /* If it is a nonstripped executable, do not enter dynamic
         symbols, as the dynamic symbol table is usually a subset
         of the main symbol table.  */
-      if (dynamic && !stripped)
+      if (type == ST_DYNAMIC && !stripped)
        continue;
       if (sym->flags & BSF_FILE)
        {
@@ -225,11 +302,9 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
              sectinfo = NULL;
            }
          filesym = sym;
-#ifdef SOFUN_ADDRESS_MAYBE_MISSING
          filesymname =
            obsavestring ((char *) filesym->name, strlen (filesym->name),
                          &objfile->objfile_obstack);
-#endif
        }
       else if (sym->flags & BSF_SECTION_SYM)
        continue;
@@ -254,8 +329,11 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
            {
              /* This is a hack to get the minimal symbol type
                 right for Irix 5, which has absolute addresses
-                with special section indices for dynamic symbols. */
-             unsigned short shndx =
+                with special section indices for dynamic symbols.
+
+                NOTE: uweigand-20071112: Synthetic symbols do not
+                have an ELF-private part, so do not touch those.  */
+             unsigned int shndx = type == ST_SYNTHETIC ? 0 : 
                ((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;
 
              switch (shndx)
@@ -414,17 +492,28 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
          msym = record_minimal_symbol
            ((char *) sym->name, symaddr,
             ms_type, sym->section, objfile);
+
          if (msym)
            {
              /* Pass symbol size field in via BFD.  FIXME!!!  */
-             unsigned long size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
-             MSYMBOL_SIZE(msym) = size;
+             elf_symbol_type *elf_sym;
+
+             /* NOTE: uweigand-20071112: A synthetic symbol does not have an
+                ELF-private part.  However, in some cases (e.g. synthetic
+                'dot' symbols on ppc64) the udata.p entry is set to point back
+                to the original ELF symbol it was derived from.  Get the size
+                from that symbol.  */ 
+             if (type != ST_SYNTHETIC)
+               elf_sym = (elf_symbol_type *) sym;
+             else
+               elf_sym = (elf_symbol_type *) sym->udata.p;
+
+             if (elf_sym)
+               MSYMBOL_SIZE(msym) = elf_sym->internal_elf_sym.st_size;
            }
-#ifdef SOFUN_ADDRESS_MAYBE_MISSING
          if (msym != NULL)
            msym->filename = filesymname;
-#endif
-         ELF_MAKE_MSYMBOL_SPECIAL (sym, msym);
+         gdbarch_elf_make_msymbol_special (current_gdbarch, sym, msym);
        }
     }
 }
@@ -451,7 +540,6 @@ elf_symtab_read (struct objfile *objfile, int dynamic,
    We look for sections with specific names, to tell us what debug
    format to look for:  FIXME!!!
 
-   dwarf_build_psymtabs() builds psymtabs for DWARF symbols;
    elfstab_build_psymtabs() handles STABS symbols;
    mdebug_build_psymtabs() handles ECOFF debugging information.
 
@@ -502,7 +590,7 @@ elf_symfile_read (struct objfile *objfile, int mainline)
        error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
               bfd_errmsg (bfd_get_error ()));
 
-      elf_symtab_read (objfile, 0, symcount, symbol_table);
+      elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table);
     }
 
   /* Add the dynamic symbols.  */
@@ -520,7 +608,7 @@ elf_symfile_read (struct objfile *objfile, int mainline)
        error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
               bfd_errmsg (bfd_get_error ()));
 
-      elf_symtab_read (objfile, 1, dynsymcount, dyn_symbol_table);
+      elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table);
     }
 
   /* Add synthetic symbols - for instance, names for any PLT entries.  */
@@ -538,7 +626,7 @@ elf_symfile_read (struct objfile *objfile, int mainline)
       for (i = 0; i < synthcount; i++)
        synth_symbol_table[i] = synthsyms + i;
       make_cleanup (xfree, synth_symbol_table);
-      elf_symtab_read (objfile, 0, synthcount, synth_symbol_table);
+      elf_symtab_read (objfile, ST_SYNTHETIC, synthcount, synth_symbol_table);
     }
 
   /* Install any minimal symbols that have been collected as the current
@@ -608,14 +696,6 @@ elf_symfile_read (struct objfile *objfile, int mainline)
       /* DWARF 2 sections */
       dwarf2_build_psymtabs (objfile, mainline);
     }
-  else if (ei.dboffset && ei.lnoffset)
-    {
-      /* DWARF sections */
-      dwarf_build_psymtabs (objfile,
-                           mainline,
-                           ei.dboffset, ei.dbsize,
-                           ei.lnoffset, ei.lnsize);
-    }
 
   /* FIXME: kettenis/20030504: This still needs to be integrated with
      dwarf2read.c in a better way.  */
@@ -670,6 +750,8 @@ elf_symfile_finish (struct objfile *objfile)
     {
       xfree (objfile->deprecated_sym_stab_info);
     }
+
+  dwarf2_free_objfile (objfile);
 }
 
 /* ELF specific initialization routine for reading symbols.
@@ -764,6 +846,9 @@ static struct sym_fns elf_sym_fns =
   elf_symfile_read,            /* sym_read: read a symbol file into symtab */
   elf_symfile_finish,          /* sym_finish: finished with file, cleanup */
   default_symfile_offsets,     /* sym_offsets:  Translate ext. to int. relocation */
+  elf_symfile_segments,                /* sym_segments: Get segment information from
+                                  a file.  */
+  NULL,                         /* sym_read_linetable */
   NULL                         /* next: pointer to next struct sym_fns */
 };
 
This page took 0.029399 seconds and 4 git commands to generate.