gdb/
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
index a1e21b8494995f4d20fa0502067aae765cbc51af..89fd81a4d055e15897f77ad7536003399c425e8c 100644 (file)
@@ -1,7 +1,7 @@
 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
 
-   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
-   2000, 2001, 2003, 2004, 2005
+   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
+   2000, 2001, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -18,8 +18,8 @@
 
    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., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
 
 #include "defs.h"
 
 #include "gdb_assert.h"
 
 #include "solist.h"
+#include "solib.h"
 #include "solib-svr4.h"
 
 #include "bfd-target.h"
+#include "elf-bfd.h"
 #include "exec.h"
 
 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
@@ -57,7 +59,14 @@ struct lm_info
     /* Pointer to copy of link map from inferior.  The type is char *
        rather than void *, so that we may use byte offsets to find the
        various fields without the need for a cast.  */
-    char *lm;
+    gdb_byte *lm;
+
+    /* Amount by which addresses in the binary should be relocated to
+       match the inferior.  This could most often be taken directly
+       from lm, but when prelinking is involved and the prelink base
+       address changes, we may need a different offset, we want to
+       warn about the difference and compute it only once.  */
+    CORE_ADDR l_addr;
   };
 
 /* On SVR4 systems, a list of symbols in the dynamic linker where
@@ -126,14 +135,101 @@ static char *main_name_list[] =
 /* link map access functions */
 
 static CORE_ADDR
-LM_ADDR (struct so_list *so)
+LM_ADDR_FROM_LINK_MAP (struct so_list *so)
 {
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
 
-  return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lmo->l_addr_offset, 
+  return (CORE_ADDR) extract_signed_integer (so->lm_info->lm
+                                            + lmo->l_addr_offset,
                                             lmo->l_addr_size);
 }
 
+static int
+HAS_LM_DYNAMIC_FROM_LINK_MAP ()
+{
+  struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
+
+  return (lmo->l_ld_size != 0);
+}
+
+static CORE_ADDR
+LM_DYNAMIC_FROM_LINK_MAP (struct so_list *so)
+{
+  struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
+
+  gdb_assert (lmo->l_ld_size != 0);
+
+  return (CORE_ADDR) extract_signed_integer (so->lm_info->lm
+                                            + lmo->l_ld_offset,
+                                            lmo->l_ld_size);
+}
+
+static CORE_ADDR
+LM_ADDR_CHECK (struct so_list *so, bfd *abfd)
+{
+  if (so->lm_info->l_addr == (CORE_ADDR)-1)
+    {
+      struct bfd_section *dyninfo_sect;
+      CORE_ADDR l_addr, l_dynaddr, dynaddr, align = 0x1000;
+
+      l_addr = LM_ADDR_FROM_LINK_MAP (so);
+
+      if (! abfd || ! HAS_LM_DYNAMIC_FROM_LINK_MAP ())
+       goto set_addr;
+
+      l_dynaddr = LM_DYNAMIC_FROM_LINK_MAP (so);
+
+      dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
+      if (dyninfo_sect == NULL)
+       goto set_addr;
+
+      dynaddr = bfd_section_vma (abfd, dyninfo_sect);
+
+      if (dynaddr + l_addr != l_dynaddr)
+       {
+         warning (_(".dynamic section for \"%s\" "
+                    "is not at the expected address"), so->so_name);
+
+         if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+           {
+             Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
+             Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
+             int i;
+
+             align = 1;
+
+             for (i = 0; i < ehdr->e_phnum; i++)
+               if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
+                 align = phdr[i].p_align;
+           }
+
+         /* Turn it into a mask.  */
+         align--;
+
+         /* If the changes match the alignment requirements, we
+            assume we're using a core file that was generated by the
+            same binary, just prelinked with a different base offset.
+            If it doesn't match, we may have a different binary, the
+            same binary with the dynamic table loaded at an unrelated
+            location, or anything, really.  To avoid regressions,
+            don't adjust the base offset in the latter case, although
+            odds are that, if things really changed, debugging won't
+            quite work.  */
+         if ((l_addr & align) == 0 && ((dynaddr - l_dynaddr) & align) == 0)
+           {
+             l_addr = l_dynaddr - dynaddr;
+             warning (_("difference appears to be caused by prelink, "
+                        "adjusting expectations"));
+           }
+       }
+
+    set_addr:
+      so->lm_info->l_addr = l_addr;
+    }
+
+  return so->lm_info->l_addr;
+}
+
 static CORE_ADDR
 LM_NEXT (struct so_list *so)
 {
@@ -292,8 +388,8 @@ elf_locate_base (void)
   struct bfd_section *dyninfo_sect;
   int dyninfo_sect_size;
   CORE_ADDR dyninfo_addr;
-  char *buf;
-  char *bufend;
+  gdb_byte *buf;
+  gdb_byte *bufend;
   int arch_size;
 
   /* Find the start address of the .dynamic section.  */
@@ -337,7 +433,7 @@ elf_locate_base (void)
            }
          else if (dyn_tag == DT_MIPS_RLD_MAP)
            {
-             char *pbuf;
+             gdb_byte *pbuf;
              int pbuf_size = TARGET_PTR_BIT / HOST_CHAR_BIT;
 
              pbuf = alloca (pbuf_size);
@@ -372,7 +468,7 @@ elf_locate_base (void)
            }
          else if (dyn_tag == DT_MIPS_RLD_MAP)
            {
-             char *pbuf;
+             gdb_byte *pbuf;
              int pbuf_size = TARGET_PTR_BIT / HOST_CHAR_BIT;
 
              pbuf = alloca (pbuf_size);
@@ -446,43 +542,40 @@ locate_base (void)
   return (debug_base);
 }
 
-/*
+/* Find the first element in the inferior's dynamic link map, and
+   return its address in the inferior.
 
-   LOCAL FUNCTION
-
-   first_link_map_member -- locate first member in dynamic linker's map
-
-   SYNOPSIS
-
-   static CORE_ADDR first_link_map_member (void)
-
-   DESCRIPTION
-
-   Find the first element in the inferior's dynamic link map, and
-   return its address in the inferior.  This function doesn't copy the
-   link map entry itself into our address space; current_sos actually
-   does the reading.  */
+   FIXME: Perhaps we should validate the info somehow, perhaps by
+   checking r_version for a known version number, or r_state for
+   RT_CONSISTENT.  */
 
 static CORE_ADDR
-first_link_map_member (void)
+solib_svr4_r_map (void)
 {
-  CORE_ADDR lm = 0;
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-  char *r_map_buf = xmalloc (lmo->r_map_size);
-  struct cleanup *cleanups = make_cleanup (xfree, r_map_buf);
 
-  read_memory (debug_base + lmo->r_map_offset, r_map_buf, lmo->r_map_size);
+  return read_memory_typed_address (debug_base + lmo->r_map_offset,
+                                   builtin_type_void_data_ptr);
+}
 
-  /* Assume that the address is unsigned.  */
-  lm = extract_unsigned_integer (r_map_buf, lmo->r_map_size);
+/* Find the link map for the dynamic linker (if it is not in the
+   normal list of loaded shared objects).  */
 
-  /* FIXME:  Perhaps we should validate the info somehow, perhaps by
-     checking r_version for a known version number, or r_state for
-     RT_CONSISTENT. */
+static CORE_ADDR
+solib_svr4_r_ldsomap (void)
+{
+  struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
+  ULONGEST version;
 
-  do_cleanups (cleanups);
+  /* Check version, and return zero if `struct r_debug' doesn't have
+     the r_ldsomap member.  */
+  version = read_memory_unsigned_integer (debug_base + lmo->r_version_offset,
+                                         lmo->r_version_size);
+  if (version < 2 || lmo->r_ldsomap_offset == -1)
+    return 0;
 
-  return (lm);
+  return read_memory_typed_address (debug_base + lmo->r_ldsomap_offset,
+                                   builtin_type_void_data_ptr);
 }
 
 /*
@@ -515,7 +608,7 @@ open_symbol_file_object (void *from_ttyp)
   int errcode;
   int from_tty = *(int *)from_ttyp;
   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-  char *l_name_buf = xmalloc (lmo->l_name_size);
+  gdb_byte *l_name_buf = xmalloc (lmo->l_name_size);
   struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
 
   if (symfile_objfile)
@@ -526,7 +619,8 @@ open_symbol_file_object (void *from_ttyp)
     return 0;  /* failed somehow... */
 
   /* First link map member should be the executable.  */
-  if ((lm = first_link_map_member ()) == 0)
+  lm = solib_svr4_r_map ();
+  if (lm == 0)
     return 0;  /* failed somehow... */
 
   /* Read address of name from target memory to GDB.  */
@@ -584,6 +678,7 @@ svr4_current_sos (void)
   CORE_ADDR lm;
   struct so_list *head = 0;
   struct so_list **link_ptr = &head;
+  CORE_ADDR ldsomap = 0;
 
   /* Make sure we've looked up the inferior's dynamic linker's base
      structure.  */
@@ -599,22 +694,19 @@ svr4_current_sos (void)
 
   /* Walk the inferior's link map list, and build our list of
      `struct so_list' nodes.  */
-  lm = first_link_map_member ();  
+  lm = solib_svr4_r_map ();
   while (lm)
     {
       struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
-      struct so_list *new
-       = (struct so_list *) xmalloc (sizeof (struct so_list));
+      struct so_list *new = XZALLOC (struct so_list);
       struct cleanup *old_chain = make_cleanup (xfree, new);
 
-      memset (new, 0, sizeof (*new));
-
       new->lm_info = xmalloc (sizeof (struct lm_info));
       make_cleanup (xfree, new->lm_info);
 
-      new->lm_info->lm = xmalloc (lmo->link_map_size);
+      new->lm_info->l_addr = (CORE_ADDR)-1;
+      new->lm_info->lm = xzalloc (lmo->link_map_size);
       make_cleanup (xfree, new->lm_info->lm);
-      memset (new->lm_info->lm, 0, lmo->link_map_size);
 
       read_memory (lm, new->lm_info->lm, lmo->link_map_size);
 
@@ -625,7 +717,7 @@ svr4_current_sos (void)
          SVR4, it has no name.  For others (Solaris 2.3 for example), it
          does have a name, so we can no longer use a missing name to
          decide when to ignore it. */
-      if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
+      if (IGNORE_FIRST_LINK_MAP_ENTRY (new) && ldsomap == 0)
        free_so (new);
       else
        {
@@ -659,6 +751,13 @@ svr4_current_sos (void)
            }
        }
 
+      /* On Solaris, the dynamic linker is not in the normal list of
+        shared objects, so make sure we pick it up too.  Having
+        symbol information for the dynamic linker is quite crucial
+        for skipping dynamic linker resolver code.  */
+      if (lm == 0 && ldsomap == 0)
+       lm = ldsomap = solib_svr4_r_ldsomap ();
+
       discard_cleanups (old_chain);
     }
 
@@ -680,7 +779,7 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
     return 0;   /* failed somehow... */
 
   /* Position ourselves on the first link map.  */
-  lm = first_link_map_member ();  
+  lm = solib_svr4_r_map ();  
   while (lm)
     {
       /* Get info on the layout of the r_debug and link_map structures. */
@@ -690,15 +789,14 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
       struct lm_info objfile_lm_info;
       struct cleanup *old_chain;
       CORE_ADDR name_address;
-      char *l_name_buf = xmalloc (lmo->l_name_size);
+      gdb_byte *l_name_buf = xmalloc (lmo->l_name_size);
       old_chain = make_cleanup (xfree, l_name_buf);
 
       /* Set up the buffer to contain the portion of the link_map
          structure that gdb cares about.  Note that this is not the
          whole link_map structure.  */
-      objfile_lm_info.lm = xmalloc (lmo->link_map_size);
+      objfile_lm_info.lm = xzalloc (lmo->link_map_size);
       make_cleanup (xfree, objfile_lm_info.lm);
-      memset (objfile_lm_info.lm, 0, lmo->link_map_size);
 
       /* Read the link map into our internal structure.  */
       read_memory (lm, objfile_lm_info.lm, lmo->link_map_size);
@@ -883,7 +981,7 @@ enable_break (void)
 
       tmp_fd  = solib_open (buf, &tmp_pathname);
       if (tmp_fd >= 0)
-       tmp_bfd = bfd_fdopenr (tmp_pathname, gnutarget, tmp_fd);
+       tmp_bfd = bfd_fopen (tmp_pathname, gnutarget, FOPEN_RB, tmp_fd);
 
       if (tmp_bfd == NULL)
        goto bkpt_at_symbol;
@@ -910,7 +1008,7 @@ enable_break (void)
          if (strcmp (buf, so->so_original_name) == 0)
            {
              load_addr_found = 1;
-             load_addr = LM_ADDR (so);
+             load_addr = LM_ADDR_CHECK (so, tmp_bfd);
              break;
            }
          so = so->next;
@@ -972,7 +1070,9 @@ enable_break (void)
       /* For whatever reason we couldn't set a breakpoint in the dynamic
          linker.  Warn and drop into the old code.  */
     bkpt_at_symbol:
-      warning (_("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code."));
+      warning (_("Unable to find dynamic linker breakpoint function.\n"
+               "GDB will be unable to debug shared library initializers\n"
+               "and track explicitly loaded dynamic code."));
     }
 
   /* Scan through the list of symbols, trying to look up the symbol and
@@ -1270,8 +1370,10 @@ static void
 svr4_relocate_section_addresses (struct so_list *so,
                                  struct section_table *sec)
 {
-  sec->addr    = svr4_truncate_ptr (sec->addr    + LM_ADDR (so));
-  sec->endaddr = svr4_truncate_ptr (sec->endaddr + LM_ADDR (so));
+  sec->addr    = svr4_truncate_ptr (sec->addr    + LM_ADDR_CHECK (so,
+                                                                 sec->bfd));
+  sec->endaddr = svr4_truncate_ptr (sec->endaddr + LM_ADDR_CHECK (so,
+                                                                 sec->bfd));
 }
 \f
 
@@ -1349,21 +1451,23 @@ svr4_ilp32_fetch_link_map_offsets (void)
     {
       lmp = &lmo;
 
-      /* Everything we need is in the first 8 bytes.  */
-      lmo.r_debug_size = 8;
+      lmo.r_version_offset = 0;
+      lmo.r_version_size = 4;
       lmo.r_map_offset = 4;
-      lmo.r_map_size   = 4;
+      lmo.r_ldsomap_offset = 20;
 
       /* Everything we need is in the first 20 bytes.  */
       lmo.link_map_size = 20;
       lmo.l_addr_offset = 0;
-      lmo.l_addr_size   = 4;
+      lmo.l_addr_size = 4;
       lmo.l_name_offset = 4;
-      lmo.l_name_size   = 4;
+      lmo.l_name_size = 4;
+      lmo.l_ld_offset = 8;
+      lmo.l_ld_size = 4;
       lmo.l_next_offset = 12;
-      lmo.l_next_size   = 4;
+      lmo.l_next_size = 4;
       lmo.l_prev_offset = 16;
-      lmo.l_prev_size   = 4;
+      lmo.l_prev_size = 4;
     }
 
   return lmp;
@@ -1382,21 +1486,23 @@ svr4_lp64_fetch_link_map_offsets (void)
     {
       lmp = &lmo;
 
-      /* Everything we need is in the first 16 bytes.  */
-      lmo.r_debug_size = 16;
+      lmo.r_version_offset = 0;
+      lmo.r_version_size = 4;
       lmo.r_map_offset = 8;
-      lmo.r_map_size   = 8;
+      lmo.r_ldsomap_offset = 40;
 
       /* Everything we need is in the first 40 bytes.  */
       lmo.link_map_size = 40;
       lmo.l_addr_offset = 0;
-      lmo.l_addr_size   = 8;
+      lmo.l_addr_size = 8;
       lmo.l_name_offset = 8;
-      lmo.l_name_size   = 8;
+      lmo.l_name_size = 8;
+      lmo.l_ld_offset = 16;
+      lmo.l_ld_size = 8;
       lmo.l_next_offset = 24;
-      lmo.l_next_size   = 8;
+      lmo.l_next_size = 8;
       lmo.l_prev_offset = 32;
-      lmo.l_prev_size   = 8;
+      lmo.l_prev_size = 8;
     }
 
   return lmp;
This page took 0.03382 seconds and 4 git commands to generate.