gdb/
[binutils-gdb.git] / gdb / solib-svr4.c
index 3ad067ab7e14c51970ad86ec2bbfb0cfafe2851e..f8e8e84631823ce9826557c8aea39f5f59557e4f 100644 (file)
@@ -50,6 +50,7 @@
 
 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
 static int svr4_have_link_map_offsets (void);
+static void svr4_relocate_main_executable (void);
 
 /* Link map info to include in an allocated so_list entry */
 
@@ -85,6 +86,7 @@ static char *solib_break_names[] =
   "_r_debug_state",
   "_dl_debug_state",
   "rtld_db_dlactivity",
+  "__dl_rtld_db_dlactivity",
   "_rtld_debug_state",
 
   NULL
@@ -174,7 +176,7 @@ 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;
+      CORE_ADDR l_addr, l_dynaddr, dynaddr;
 
       l_addr = LM_ADDR_FROM_LINK_MAP (so);
 
@@ -191,6 +193,9 @@ LM_ADDR_CHECK (struct so_list *so, bfd *abfd)
 
       if (dynaddr + l_addr != l_dynaddr)
        {
+         CORE_ADDR align = 0x1000;
+         CORE_ADDR minpagesize = align;
+
          if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
            {
              Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
@@ -202,6 +207,8 @@ LM_ADDR_CHECK (struct so_list *so, bfd *abfd)
              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;
+
+             minpagesize = get_elf_backend_data (abfd)->minpagesize;
            }
 
          /* Turn it into a mask.  */
@@ -215,15 +222,33 @@ LM_ADDR_CHECK (struct so_list *so, bfd *abfd)
             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) == ((l_dynaddr - dynaddr) & align))
+            quite work.
+
+            One could expect more the condition
+              ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
+            but the one below is relaxed for PPC.  The PPC kernel supports
+            either 4k or 64k page sizes.  To be prepared for 64k pages,
+            PPC ELF files are built using an alignment requirement of 64k.
+            However, when running on a kernel supporting 4k pages, the memory
+            mapping of the library may not actually happen on a 64k boundary!
+
+            (In the usual case where (l_addr & align) == 0, this check is
+            equivalent to the possibly expected check above.)
+
+            Even on PPC it must be zero-aligned at least for MINPAGESIZE.  */
+
+         if ((l_addr & (minpagesize - 1)) == 0
+             && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
            {
              l_addr = l_dynaddr - dynaddr;
 
-             warning (_(".dynamic section for \"%s\" "
-                    "is not at the expected address"), so->so_name);
-             warning (_("difference appears to be caused by prelink, "
-                        "adjusting expectations"));
+             if (info_verbose)
+               {
+                 warning (_(".dynamic section for \"%s\" "
+                            "is not at the expected address"), so->so_name);
+                 warning (_("difference appears to be caused by prelink, "
+                            "adjusting expectations"));
+               }
            }
          else
            warning (_(".dynamic section for \"%s\" "
@@ -573,6 +598,7 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
   Elf32_External_Dyn *x_dynp_32;
   Elf64_External_Dyn *x_dynp_64;
   struct bfd_section *sect;
+  struct target_section *target_section;
 
   if (abfd == NULL)
     return 0;
@@ -588,7 +614,23 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
   sect = bfd_get_section_by_name (abfd, ".dynamic");
   if (sect == NULL)
     return 0;
-  dyn_addr = bfd_section_vma (abfd, sect);
+
+  for (target_section = current_target_sections->sections;
+       target_section < current_target_sections->sections_end;
+       target_section++)
+    if (sect == target_section->the_bfd_section)
+      break;
+  if (target_section < current_target_sections->sections_end)
+    dyn_addr = target_section->addr;
+  else
+    {
+      /* ABFD may come from OBJFILE acting only as a symbol file without being
+        loaded into the target (see add_symbol_file_command).  This case is
+        such fallback to the file VMA address without the possibility of
+        having the section relocated to its actual in-memory address.  */
+
+      dyn_addr = bfd_section_vma (abfd, sect);
+    }
 
   /* Read in .dynamic from the BFD.  We will get the actual value
      from memory later.  */
@@ -1299,6 +1341,25 @@ enable_break (struct svr4_info *info, int from_tty)
                                                              sym_addr,
                                                              &current_target));
 
+      /* On at least some versions of Solaris there's a dynamic relocation
+        on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
+        we get control before the dynamic linker has self-relocated.
+        Check if SYM_ADDR is in a known section, if it is assume we can
+        trust its value.  This is just a heuristic though, it could go away
+        or be replaced if it's getting in the way.
+
+        On ARM we need to know whether the ISA of rtld_db_dlactivity (or
+        however it's spelled in your particular system) is ARM or Thumb.
+        That knowledge is encoded in the address, if it's Thumb the low bit
+        is 1.  However, we've stripped that info above and it's not clear
+        what all the consequences are of passing a non-addr_bits_remove'd
+        address to create_solib_event_breakpoint.  The call to
+        find_pc_section verifies we know about the address and have some
+        hope of computing the right kind of breakpoint to use (via
+        symbol info).  It does mean that GDB needs to be pointed at a
+        non-stripped version of the dynamic linker in order to obtain
+        information it already knows about.  Sigh.  */
+
       os = find_pc_section (sym_addr);
       if (os != NULL)
        {
@@ -1533,6 +1594,7 @@ enable_break (struct svr4_info *info, int from_tty)
 static void
 svr4_special_symbol_handling (void)
 {
+  svr4_relocate_main_executable ();
 }
 
 /* Decide if the objfile needs to be relocated.  As indicated above,
@@ -1611,13 +1673,15 @@ static CORE_ADDR
 svr4_exec_displacement (void)
 {
   int found;
+  /* ENTRY_POINT is a possible function descriptor - before
+     a call to gdbarch_convert_from_func_ptr_addr.  */
   CORE_ADDR entry_point;
 
   if (exec_bfd == NULL)
     return 0;
 
   if (target_auxv_search (&current_target, AT_ENTRY, &entry_point) == 1)
-    return entry_point - exec_entry_point (exec_bfd, &current_target);
+    return entry_point - bfd_get_start_address (exec_bfd);
 
   return svr4_static_exec_displacement ();
 }
@@ -1649,6 +1713,15 @@ svr4_relocate_main_executable (void)
 
       objfile_relocate (symfile_objfile, new_offsets);
     }
+  else if (exec_bfd)
+    {
+      asection *asect;
+
+      for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
+       exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
+                                 (bfd_section_vma (exec_bfd, asect)
+                                  + displacement));
+    }
 }
 
 /*
@@ -1713,7 +1786,8 @@ svr4_solib_create_inferior_hook (int from_tty)
   info = get_svr4_info ();
 
   /* Relocate the main executable if necessary.  */
-  svr4_relocate_main_executable ();
+  if (current_inferior ()->attach_flag == 0)
+    svr4_relocate_main_executable ();
 
   if (!svr4_have_link_map_offsets ())
     return;
@@ -1942,8 +2016,19 @@ elf_lookup_lib_symbol (const struct objfile *objfile,
                       const char *linkage_name,
                       const domain_enum domain)
 {
-  if (objfile->obfd == NULL
-     || scan_dyntag (DT_SYMBOLIC, objfile->obfd, NULL) != 1)
+  bfd *abfd;
+
+  if (objfile == symfile_objfile)
+    abfd = exec_bfd;
+  else
+    {
+      /* OBJFILE should have been passed as the non-debug one.  */
+      gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
+
+      abfd = objfile->obfd;
+    }
+
+  if (abfd == NULL || scan_dyntag (DT_SYMBOLIC, abfd, NULL) != 1)
     return NULL;
 
   return lookup_global_symbol_from_objfile