OSDN Git Service

*** empty log message ***
[pf3gnuchains/sourceware.git] / gdb / machoread.c
index ebd90c6..dbf9ae4 100644 (file)
@@ -1,5 +1,5 @@
 /* Darwin support for GDB, the GNU debugger.
-   Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
    Contributed by AdaCore.
 
 #include "gdb_assert.h"
 #include "aout/stab_gnu.h"
 #include "vec.h"
+#include "psympriv.h"
 
 #include <string.h>
 
 /* If non-zero displays debugging message.  */
 static int mach_o_debug_level = 0;
 
-static void
-macho_new_init (struct objfile *objfile)
-{
-}
-
-static void
-macho_symfile_init (struct objfile *objfile)
-{
-  objfile->flags |= OBJF_REORDERED;
-  init_entry_point_info (objfile);
-}
-
 /* Dwarf debugging information are never in the final executable.  They stay
    in object files and the executable contains the list of object files read
    during the link.
    Each time an oso (other source) is found in the executable, the reader
    creates such a structure.  They are read after the processing of the
-   executable.
-*/
+   executable.  */
+
 typedef struct oso_el
 {
   /* Object file name.  */
@@ -69,21 +58,53 @@ typedef struct oso_el
   int num_sections;
 
   /* Each seaction of the object file is represented by a symbol and its
-     offset.  */
+     offset.  If the offset is 0, we assume that the symbol is at offset 0
+     in the OSO object file and a symbol lookup in the main file is
+     required to get the offset.  */
   asymbol **symbols;
   bfd_vma *offsets;
 }
 oso_el;
 
-/* Vector of object files to be read after the executable.  */
+/* Vector of object files to be read after the executable.  This is one
+   global variable but it's life-time is the one of macho_symfile_read.  */
 DEF_VEC_O (oso_el);
 static VEC (oso_el) *oso_vector;
 
-/*  Add a new OSO to the vector.  */
+struct macho_oso_data
+{
+  /* Per objfile symbol table.  This is used to apply relocation to sections
+     It is loaded only once, then relocated, and free after sections are
+     relocated.  */
+  asymbol **symbol_table;
+
+  /* The offsets for this objfile.  Used to relocate the symbol_table.  */
+  struct oso_el *oso;
+
+  struct objfile *main_objfile;
+};
+
+/* Data for OSO being processed.  */
+
+static struct macho_oso_data current_oso;
+
+static void
+macho_new_init (struct objfile *objfile)
+{
+}
+
+static void
+macho_symfile_init (struct objfile *objfile)
+{
+  objfile->flags |= OBJF_REORDERED;
+  init_entry_point_info (objfile);
+}
+
+/*  Add a new OSO to the vector of OSO to load.  */
 
 static void
-macho_add_oso (const asymbol *oso_sym, int nbr_sections,
-              asymbol **symbols, bfd_vma *offsets)
+macho_register_oso (const asymbol *oso_sym, int nbr_sections,
+                    asymbol **symbols, bfd_vma *offsets)
 {
   oso_el el;
 
@@ -124,14 +145,17 @@ macho_symtab_read (struct objfile *objfile,
        {
          bfd_vma addr;
 
+          /* Debugging symbols are used to collect OSO file names as well
+             as section offsets.  */
+
          switch (mach_o_sym->n_type)
            {
            case N_SO:
-             if ((sym->name == NULL || sym->name[0] == 0)
-                 && oso_file != NULL)
+              /* An empty SO entry terminates a chunk for an OSO file.  */
+             if ((sym->name == NULL || sym->name[0] == 0) && oso_file != NULL)
                {
-                 macho_add_oso (oso_file, nbr_sections,
-                                first_symbol, first_offset);
+                 macho_register_oso (oso_file, nbr_sections,
+                                      first_symbol, first_offset);
                  first_symbol = NULL;
                  first_offset = NULL;
                  oso_file = NULL;
@@ -149,6 +173,7 @@ macho_symtab_read (struct objfile *objfile,
              if (addr != 0
                  && first_symbol[sym->section->index] == NULL)
                {
+                  /* These STAB entries can directly relocate a section.  */
                  first_symbol[sym->section->index] = sym;
                  first_offset[sym->section->index] = addr + offset;
                }
@@ -156,9 +181,15 @@ macho_symtab_read (struct objfile *objfile,
            case N_GSYM:
              gdb_assert (oso_file != NULL);
              if (first_symbol[sym->section->index] == NULL)
-               first_symbol[sym->section->index] = sym;
+                {
+                  /* This STAB entry needs a symbol look-up to relocate
+                     the section.  */
+                  first_symbol[sym->section->index] = sym;
+                  first_offset[sym->section->index] = 0;
+                }
              break;
            case N_OSO:
+              /* New OSO file.  */
              gdb_assert (oso_file == NULL);
              first_symbol = (asymbol **)xmalloc (nbr_sections
                                                  * sizeof (asymbol *));
@@ -175,7 +206,7 @@ macho_symtab_read (struct objfile *objfile,
       if (sym->name == NULL || *sym->name == '\0')
        {
          /* Skip names that don't exist (shouldn't happen), or names
-            that are null strings (may happen). */
+            that are null strings (may happen).  */
          continue;
        }
 
@@ -184,12 +215,12 @@ macho_symtab_read (struct objfile *objfile,
          struct minimal_symbol *msym;
          CORE_ADDR symaddr;
 
-         /* Bfd symbols are section relative. */
+         /* Bfd symbols are section relative.  */
          symaddr = sym->value + sym->section->vma;
 
          /* Select global/local/weak symbols.  Note that bfd puts abs
             symbols in their own section, so all symbols we are
-            interested in will have a section. */
+            interested in will have a section.  */
          /* Relocate all non-absolute and non-TLS symbols by the
             section offset.  */
          if (sym->section != &bfd_abs_section
@@ -227,12 +258,13 @@ macho_symtab_read (struct objfile *objfile,
                ms_type = mst_unknown;
            }
          else
-           continue;   /* Skip this symbol. */
+           continue;   /* Skip this symbol.  */
 
          gdb_assert (sym->section->index < nbr_sections);
          if (oso_file != NULL
              && first_symbol[sym->section->index] == NULL)
            {
+              /* Standard symbols can directly relocate sections.  */
              first_symbol[sym->section->index] = sym;
              first_offset[sym->section->index] = symaddr;
            }
@@ -243,8 +275,9 @@ macho_symtab_read (struct objfile *objfile,
        }
     }
 
+  /* Just in case there is no trailing SO entry.  */
   if (oso_file != NULL)
-    macho_add_oso (oso_file, nbr_sections, first_symbol, first_offset);
+    macho_register_oso (oso_file, nbr_sections, first_symbol, first_offset);
 }
 
 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
@@ -278,16 +311,16 @@ oso_el_compare_name (const void *vl, const void *vr)
 /* Add an oso file as a symbol file.  */
 
 static void
-macho_add_oso_symfile (oso_el *oso, bfd *abfd, struct objfile *main_objfile)
+macho_add_oso_symfile (oso_el *oso, bfd *abfd,
+                       struct objfile *main_objfile, int symfile_flags)
 {
-  struct section_addr_info *addrs;
-  int len;
+  struct objfile *objfile;
   int i;
   char leading_char;
 
   if (mach_o_debug_level > 0)
     printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
-      
+
   if (!bfd_check_format (abfd, bfd_object))
     {
       warning (_("`%s': can't read symbols: %s."), oso->name,
@@ -297,65 +330,88 @@ macho_add_oso_symfile (oso_el *oso, bfd *abfd, struct objfile *main_objfile)
     }
 
   bfd_set_cacheable (abfd, 1);
-  
-  /* Compute addr length.  */
-  len = 0;
-  for (i = 0; i < oso->num_sections; i++)
-    if (oso->symbols[i] != NULL)
-      len++;
-  
-  addrs = alloc_section_addr_info (len);
+
+  /* Relocate sections.  */
 
   leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
 
-  len = 0;
   for (i = 0; i < oso->num_sections; i++)
-    if (oso->symbols[i] != NULL)
-      {
-        if (oso->offsets[i])
-          addrs->other[len].addr = oso->offsets[i];
-        else
-          {
-            struct minimal_symbol *msym;
-            const char *name = oso->symbols[i]->name;
-            
-            if (name[0] == leading_char)
-              ++name;
-
-            if (mach_o_debug_level > 3)
-              printf_unfiltered (_("resolve sect %s with %s\n"),
-                                 oso->symbols[i]->section->name,
-                                 oso->symbols[i]->name);
-            msym = lookup_minimal_symbol (name, NULL, main_objfile);
-            if (msym == NULL)
-              {
-                warning (_("can't find symbol '%s' in minsymtab"),
-                         oso->symbols[i]->name);
-                addrs->other[len].addr = 0;
-              }
-            else
-              addrs->other[len].addr = SYMBOL_VALUE_ADDRESS (msym);
-          }
-        addrs->other[len].name = (char *)oso->symbols[i]->section->name;
-        len++;
-      }
-      
-  if (mach_o_debug_level > 1)
     {
-      int j;
-      for (j = 0; j < addrs->num_sections; j++)
+      asection *sect;
+      const char *sectname;
+      bfd_vma vma;
+
+      /* Empty slot.  */
+      if (oso->symbols[i] == NULL)
+        continue;
+
+      if (oso->offsets[i])
+        vma = oso->offsets[i];
+      else
+        {
+          struct minimal_symbol *msym;
+          const char *name = oso->symbols[i]->name;
+
+          if (name[0] == leading_char)
+            ++name;
+
+          if (mach_o_debug_level > 3)
+            printf_unfiltered (_("resolve sect %s with %s\n"),
+                               oso->symbols[i]->section->name,
+                               oso->symbols[i]->name);
+          msym = lookup_minimal_symbol (name, NULL, main_objfile);
+          if (msym == NULL)
+            {
+              warning (_("can't find symbol '%s' in minsymtab"), name);
+              continue;
+            }
+          else
+            vma = SYMBOL_VALUE_ADDRESS (msym);
+        }
+      sectname = (char *)oso->symbols[i]->section->name;
+
+      sect = bfd_get_section_by_name (abfd, sectname);
+      if (sect == NULL)
+        {
+          warning (_("can't find section '%s' in OSO file %s"),
+                   sectname, oso->name);
+          continue;
+        }
+      bfd_set_section_vma (abfd, sect, vma);
+
+      if (mach_o_debug_level > 1)
         printf_unfiltered (_("  %s: %s\n"),
-                           core_addr_to_string (addrs->other[j].addr),
-                           addrs->other[j].name);
+                           core_addr_to_string (vma), sectname);
     }
 
-  symbol_file_add_from_bfd (abfd, 0, addrs, 0);
+  /* Make sure that the filename was malloc'ed.  The current filename comes
+     either from an OSO symbol name or from an archive name.  Memory for both
+     is not managed by gdb.  */
+  abfd->filename = xstrdup (abfd->filename);
+
+  gdb_assert (current_oso.symbol_table == NULL);
+  current_oso.main_objfile = main_objfile;
+
+  /* We need to clear SYMFILE_MAINLINE to avoid interractive question
+     from symfile.c:symbol_file_add_with_addrs_or_offsets.  */
+  objfile = symbol_file_add_from_bfd
+    (abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
+     main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
+                            | OBJF_READNOW | OBJF_USERLOADED));
+  add_separate_debug_objfile (objfile, main_objfile);
+
+  current_oso.main_objfile = NULL;
+  if (current_oso.symbol_table)
+    {
+      xfree (current_oso.symbol_table);
+      current_oso.symbol_table = NULL;
+    }
 }
 
 /* Read symbols from the vector of oso files.  */
 
 static void
-macho_oso_symfile (struct objfile *main_objfile)
+macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
 {
   int ix;
   VEC (oso_el) *vec;
@@ -439,7 +495,8 @@ macho_oso_symfile (struct objfile *main_objfile)
                       && !memcmp (member_name, oso2->name + pfx_len + 1,
                                   member_len))
                     {
-                      macho_add_oso_symfile (oso2, member_bfd, main_objfile);
+                      macho_add_oso_symfile (oso2, member_bfd,
+                                             main_objfile, symfile_flags);
                       oso2->name = NULL;
                       break;
                     }
@@ -472,7 +529,7 @@ macho_oso_symfile (struct objfile *main_objfile)
             warning (_("`%s': can't open to read symbols: %s."), oso->name,
                      bfd_errmsg (bfd_get_error ()));
           else
-            macho_add_oso_symfile (oso, abfd, main_objfile);
+            macho_add_oso_symfile (oso, abfd, main_objfile, symfile_flags);
 
           ix++;
         }
@@ -505,9 +562,8 @@ macho_check_dsym (struct objfile *objfile)
   size_t base_len = strlen (base_name);
   char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
   bfd *dsym_bfd;
-  asection *sect;
-  bfd_byte main_uuid[16];
-  bfd_byte dsym_uuid[16];
+  bfd_mach_o_load_command *main_uuid;
+  bfd_mach_o_load_command *dsym_uuid;
 
   strcpy (dsym_filename, objfile->name);
   strcpy (dsym_filename + name_len, DSYM_SUFFIX);
@@ -516,19 +572,12 @@ macho_check_dsym (struct objfile *objfile)
   if (access (dsym_filename, R_OK) != 0)
     return NULL;
 
-  sect = bfd_get_section_by_name (objfile->obfd, "LC_UUID");
-  if (sect == NULL)
+  if (bfd_mach_o_lookup_command (objfile->obfd,
+                                 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
     {
       warning (_("can't find UUID in %s"), objfile->name);
       return NULL;
     }
-  if (!bfd_get_section_contents (objfile->obfd, sect, main_uuid,
-                                0, sizeof (main_uuid)))
-    {
-      warning (_("can't read UUID in %s"), objfile->name);
-      return NULL;
-    }
-
   dsym_filename = xstrdup (dsym_filename);
   dsym_bfd = bfd_openr (dsym_filename, gnutarget);
   if (dsym_bfd == NULL)
@@ -546,23 +595,16 @@ macho_check_dsym (struct objfile *objfile)
       return NULL;
     }
 
-  sect = bfd_get_section_by_name (dsym_bfd, "LC_UUID");
-  if (sect == NULL)
+  if (bfd_mach_o_lookup_command (dsym_bfd,
+                                 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
     {
       warning (_("can't find UUID in %s"), dsym_filename);
       bfd_close (dsym_bfd);
       xfree (dsym_filename);
       return NULL;
     }
-  if (!bfd_get_section_contents (dsym_bfd, sect, dsym_uuid,
-                                0, sizeof (dsym_uuid)))
-    {
-      warning (_("can't read UUID in %s"), dsym_filename);
-      bfd_close (dsym_bfd);
-      xfree (dsym_filename);
-      return NULL;
-    }
-  if (memcmp (dsym_uuid, main_uuid, sizeof (main_uuid)))
+  if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
+              sizeof (main_uuid->command.uuid.uuid)))
     {
       warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
       bfd_close (dsym_bfd);
@@ -570,11 +612,10 @@ macho_check_dsym (struct objfile *objfile)
       return NULL;
     }
   return dsym_bfd;
-
 }
 
 static void
-macho_symfile_read (struct objfile *objfile, int mainline)
+macho_symfile_read (struct objfile *objfile, int symfile_flags)
 {
   bfd *abfd = objfile->obfd;
   struct cleanup *back_to;
@@ -628,6 +669,7 @@ macho_symfile_read (struct objfile *objfile, int mainline)
        {
          int ix;
          oso_el *oso;
+          struct bfd_section *asect, *dsect;
 
          if (mach_o_debug_level > 0)
            printf_unfiltered (_("dsym file found\n"));
@@ -641,18 +683,29 @@ macho_symfile_read (struct objfile *objfile, int mainline)
          VEC_free (oso_el, oso_vector);
          oso_vector = NULL;
 
-         /* Now recurse: read dwarf from dsym.  */
-         symbol_file_add_from_bfd (dsym_bfd, 0, NULL, 0);
+          /* Set dsym section size.  */
+          for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
+               asect && dsect;
+               asect = asect->next, dsect = dsect->next)
+            {
+              if (strcmp (asect->name, dsect->name) != 0)
+                break;
+              bfd_set_section_size (dsym_bfd, dsect,
+                                    bfd_get_section_size (asect));
+            }
+
+         /* Add the dsym file as a separate file.  */
+          symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
       
          /* Don't try to read dwarf2 from main file or shared libraries.  */
-         return;
+          return;
        }
     }
 
   if (dwarf2_has_info (objfile))
     {
       /* DWARF 2 sections */
-      dwarf2_build_psymtabs (objfile, mainline);
+      dwarf2_build_psymtabs (objfile);
     }
 
   /* Do not try to read .eh_frame/.debug_frame as they are not relocated
@@ -660,7 +713,66 @@ macho_symfile_read (struct objfile *objfile, int mainline)
 
   /* Then the oso.  */
   if (oso_vector != NULL)
-    macho_oso_symfile (objfile);
+    macho_symfile_read_all_oso (objfile, symfile_flags);
+}
+
+static bfd_byte *
+macho_symfile_relocate (struct objfile *objfile, asection *sectp,
+                        bfd_byte *buf)
+{
+  bfd *abfd = objfile->obfd;
+
+  /* We're only interested in sections with relocation
+     information.  */
+  if ((sectp->flags & SEC_RELOC) == 0)
+    return NULL;
+
+  if (mach_o_debug_level > 0)
+    printf_unfiltered (_("Relocate section '%s' of %s\n"),
+                       sectp->name, objfile->name);
+
+  if (current_oso.symbol_table == NULL)
+    {
+      int storage;
+      int i;
+      char leading_char;
+
+      storage = bfd_get_symtab_upper_bound (abfd);
+      current_oso.symbol_table = (asymbol **) xmalloc (storage);
+      bfd_canonicalize_symtab (abfd, current_oso.symbol_table);
+
+      leading_char = bfd_get_symbol_leading_char (abfd);
+
+      for (i = 0; current_oso.symbol_table[i]; i++)
+        {
+          asymbol *sym = current_oso.symbol_table[i];
+
+          if (bfd_is_com_section (sym->section))
+            {
+              /* This one must be solved.  */
+              struct minimal_symbol *msym;
+              const char *name = sym->name;
+
+              if (name[0] == leading_char)
+                name++;
+
+              msym = lookup_minimal_symbol
+                (name, NULL, current_oso.main_objfile);
+              if (msym == NULL)
+                {
+                  warning (_("can't find symbol '%s' in minsymtab"), name);
+                  continue;
+                }
+              else
+                {
+                  sym->section = &bfd_abs_section;
+                  sym->value = SYMBOL_VALUE_ADDRESS (msym);
+                }
+            }
+        }
+    }
+
+  return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
 }
 
 static void
@@ -725,15 +837,19 @@ macho_symfile_offsets (struct objfile *objfile,
     }
 }
 
-static struct sym_fns macho_sym_fns = {
+static const struct sym_fns macho_sym_fns = {
   bfd_target_mach_o_flavour,
 
-  macho_new_init,               /* sym_new_init: init anything gbl to entire symtab */
-  macho_symfile_init,           /* sym_init: read initial info, setup for sym_read() */
-  macho_symfile_read,           /* sym_read: read a symbol file into symtab */
-  macho_symfile_finish,         /* sym_finish: finished with file, cleanup */
-  macho_symfile_offsets,        /* sym_offsets:  xlate external to internal form */
-  NULL                          /* next: pointer to next struct sym_fns */
+  macho_new_init,               /* init anything gbl to entire symtab */
+  macho_symfile_init,           /* read initial info, setup for sym_read() */
+  macho_symfile_read,           /* read a symbol file into symtab */
+  NULL,                                /* sym_read_psymbols */
+  macho_symfile_finish,         /* finished with file, cleanup */
+  macho_symfile_offsets,        /* xlate external to internal form */
+  default_symfile_segments,    /* Get segment information from a file.  */
+  NULL,
+  macho_symfile_relocate,      /* Relocate a debug section.  */
+  &psym_functions
 };
 
 void
@@ -742,9 +858,9 @@ _initialize_machoread ()
   add_symtab_fns (&macho_sym_fns);
 
   add_setshow_zinteger_cmd ("mach-o", class_obscure,
-                           &mach_o_debug_level, _("\
-Set if printing Mach-O symbols processing."), _("\
-Show if printing Mach-O symbols processing."), NULL,
-                           NULL, NULL,
+                           &mach_o_debug_level,
+                           _("Set if printing Mach-O symbols processing."),
+                           _("Show if printing Mach-O symbols processing."),
+                           NULL, NULL, NULL,
                            &setdebuglist, &showdebuglist);
 }