* sentinel-frame.c (sentinel_frame_prev_register): Do not call
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
index 63918277b90693bf18f79c3ecd33bf1e1e9466f1..8ef400d75a5bf3c0a1264789dedc9c5a6923ae94 100644 (file)
@@ -1,13 +1,13 @@
 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
 
    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
-   2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   2001, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    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,
@@ -16,9 +16,7 @@
    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-target.h"
 #include "elf-bfd.h"
 #include "exec.h"
+#include "auxv.h"
 
 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
 static int svr4_have_link_map_offsets (void);
 
-/* This hook is set to a function that provides native link map
-   offsets if the code in solib-legacy.c is linked in.  */
-struct link_map_offsets *(*legacy_svr4_fetch_link_map_offsets_hook) (void);
-
 /* Link map info to include in an allocated so_list entry */
 
 struct lm_info
@@ -354,7 +349,7 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
   int arch_size, step, sect_size;
   long dyn_tag;
   CORE_ADDR dyn_ptr, dyn_addr;
-  gdb_byte *bufend, *buf;
+  gdb_byte *bufend, *bufstart, *buf;
   Elf32_External_Dyn *x_dynp_32;
   Elf64_External_Dyn *x_dynp_64;
   struct bfd_section *sect;
@@ -371,16 +366,13 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
     return 0;
   dyn_addr = bfd_section_vma (abfd, sect);
 
-  /* Read in .dynamic section, silently ignore errors.  */
+  /* Read in .dynamic from the BFD.  We will get the actual value
+     from memory later.  */
   sect_size = bfd_section_size (abfd, sect);
-  buf = alloca (sect_size);
-  if (target_read_memory (dyn_addr, buf, sect_size))
-    {
-      /* If target_read_memory fails, try reading the BFD file.  */
-      if (!bfd_get_section_contents (abfd, sect,
-                                    buf, 0, sect_size))
-       return 0;
-    }
+  buf = bufstart = alloca (sect_size);
+  if (!bfd_get_section_contents (abfd, sect,
+                                buf, 0, sect_size))
+    return 0;
 
   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
@@ -395,7 +387,7 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
        dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
        dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
       }
-      else
+    else
       {
        x_dynp_64 = (Elf64_External_Dyn *) buf;
        dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
@@ -405,9 +397,20 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
        return 0;
      if (dyn_tag == dyntag)
        {
+        /* If requested, try to read the runtime value of this .dynamic
+           entry.  */
         if (ptr)
-          *ptr = dyn_ptr;
-         return 1;
+          {
+            gdb_byte ptr_buf[8];
+            CORE_ADDR ptr_addr;
+
+            ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
+            if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
+              dyn_ptr = extract_typed_address (ptr_buf,
+                                               builtin_type_void_data_ptr);
+            *ptr = dyn_ptr;
+          }
+        return 1;
        }
   }
 
@@ -445,11 +448,9 @@ elf_locate_base (void)
   struct minimal_symbol *msymbol;
   CORE_ADDR dyn_ptr;
 
-  /* Find DT_DEBUG.  */
-  if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr))
-    return dyn_ptr;
-
-  /* Find DT_MIPS_RLD_MAP.  */
+  /* Look for DT_MIPS_RLD_MAP first.  MIPS executables use this
+     instead of DT_DEBUG, although they sometimes contain an unused
+     DT_DEBUG.  */
   if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr))
     {
       gdb_byte *pbuf;
@@ -462,6 +463,10 @@ elf_locate_base (void)
       return extract_typed_address (pbuf, builtin_type_void_data_ptr);
     }
 
+  /* Find DT_DEBUG.  */
+  if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr))
+    return dyn_ptr;
+
   /* This may be a static executable.  Look for the symbol
      conventionally named _r_debug, as a last resort.  */
   msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
@@ -977,6 +982,7 @@ enable_break (void)
       char *buf;
       CORE_ADDR load_addr = 0;
       int load_addr_found = 0;
+      int loader_found_in_list = 0;
       struct so_list *so;
       bfd *tmp_bfd = NULL;
       struct target_ops *tmp_bfd_target;
@@ -1000,11 +1006,6 @@ enable_break (void)
          be trivial on GNU/Linux).  Therefore, we have to try an alternate
          mechanism to find the dynamic linker's base address.  */
 
-      /* TODO drow/2006-09-12: This is somewhat fragile, because it
-        relies on read_pc.  On both Solaris and GNU/Linux we can use
-        the AT_BASE auxilliary entry, which GDB now knows how to
-        access, to find the base address.  */
-
       tmp_fd = solib_open (buf, &tmp_pathname);
       if (tmp_fd >= 0)
        tmp_bfd = bfd_fopen (tmp_pathname, gnutarget, FOPEN_RB, tmp_fd);
@@ -1034,19 +1035,32 @@ enable_break (void)
          if (strcmp (buf, so->so_original_name) == 0)
            {
              load_addr_found = 1;
+             loader_found_in_list = 1;
              load_addr = LM_ADDR_CHECK (so, tmp_bfd);
              break;
            }
          so = so->next;
        }
 
+      /* If we were not able to find the base address of the loader
+         from our so_list, then try using the AT_BASE auxilliary entry.  */
+      if (!load_addr_found)
+        if (target_auxv_search (&current_target, AT_BASE, &load_addr) > 0)
+          load_addr_found = 1;
+
       /* Otherwise we find the dynamic linker's base address by examining
         the current pc (which should point at the entry point for the
-        dynamic linker) and subtracting the offset of the entry point.  */
+        dynamic linker) and subtracting the offset of the entry point.
+
+         This is more fragile than the previous approaches, but is a good
+         fallback method because it has actually been working well in
+         most cases.  */
       if (!load_addr_found)
+       load_addr = (read_pc ()
+                    - exec_entry_point (tmp_bfd, tmp_bfd_target));
+
+      if (!loader_found_in_list)
        {
-         load_addr = (read_pc ()
-                      - exec_entry_point (tmp_bfd, tmp_bfd_target));
          debug_loader_name = xstrdup (buf);
          debug_loader_offset_p = 1;
          debug_loader_offset = load_addr;
@@ -1350,7 +1364,7 @@ svr4_solib_create_inferior_hook (void)
   do
     {
       target_resume (pid_to_ptid (-1), 0, stop_signal);
-      wait_for_inferior ();
+      wait_for_inferior (0);
     }
   while (stop_signal != TARGET_SIGNAL_TRAP);
   stop_soon = NO_STOP_QUIETLY;
@@ -1430,12 +1444,12 @@ solib_svr4_init (struct obstack *obstack)
   struct solib_svr4_ops *ops;
 
   ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops);
-  ops->fetch_link_map_offsets = legacy_svr4_fetch_link_map_offsets_hook;
+  ops->fetch_link_map_offsets = NULL;
   return ops;
 }
 
 /* Set the architecture-specific `struct link_map_offsets' fetcher for
-   GDBARCH to FLMO.  */
+   GDBARCH to FLMO.  Also, install SVR4 solib_ops into GDBARCH.  */
 
 void
 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
@@ -1444,6 +1458,8 @@ set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
   struct solib_svr4_ops *ops = gdbarch_data (gdbarch, solib_svr4_data);
 
   ops->fetch_link_map_offsets = flmo;
+
+  set_solib_ops (gdbarch, &svr4_so_ops);
 }
 
 /* Fetch a link_map_offsets structure using the architecture-specific
@@ -1549,10 +1565,29 @@ elf_lookup_lib_symbol (const struct objfile *objfile,
      || scan_dyntag (DT_SYMBOLIC, objfile->obfd, NULL) != 1)
     return NULL;
 
-  return  lookup_global_symbol_from_objfile
+  return lookup_global_symbol_from_objfile
                (objfile, name, linkage_name, domain, symtab);
 }
 
+static int
+svr4_same (struct so_list *gdb, struct so_list *inferior)
+{
+  if (! strcmp (gdb->so_original_name, inferior->so_original_name))
+    return 1;
+
+  /* On Solaris, when starting inferior we think that dynamic linker is
+     /usr/lib/ld.so.1, but later on, the table of loaded shared libraries 
+     contains /lib/ld.so.1.  Sometimes one file is a link to another, but 
+     sometimes they have identical content, but are not linked to each
+     other.  We don't restrict this check for Solaris, but the chances
+     of running into this situation elsewhere are very low.  */
+  if (strcmp (gdb->so_original_name, "/usr/lib/ld.so.1") == 0
+      && strcmp (inferior->so_original_name, "/lib/ld.so.1") == 0)
+    return 1;
+
+  return 0;
+}
+
 extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */
 
 void
@@ -1569,7 +1604,5 @@ _initialize_svr4_solib (void)
   svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
   svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
   svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol;
-
-  /* FIXME: Don't do this here.  *_gdbarch_init() should set so_ops. */
-  current_target_so_ops = &svr4_so_ops;
+  svr4_so_ops.same = svr4_same;
 }
This page took 0.030656 seconds and 4 git commands to generate.