2001-01-02 Michael Snyder <msnyder@cleaver.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
index 13fea47e52d0adb68c46a89543bbdf5e5f4ee030..603c69c490865c9f8f35a6700bb124992ad9895b 100644 (file)
@@ -82,6 +82,7 @@ static char *solib_break_names[] =
   "_r_debug_state",
   "_dl_debug_state",
   "rtld_db_dlactivity",
+  "_rtld_debug_state",
   NULL
 };
 #endif
@@ -287,7 +288,6 @@ IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
 
 #endif /* !SVR4_SHARED_LIBS */
 
-
 static CORE_ADDR debug_base;   /* Base of dynamic linker structures */
 static CORE_ADDR breakpoint_addr;      /* Address where end bkpt is set */
 
@@ -295,16 +295,6 @@ static CORE_ADDR breakpoint_addr;  /* Address where end bkpt is set */
 
 static int match_main (char *);
 
-/* If non-zero, this is a prefix that will be added to the front of the name
-   shared libraries with an absolute filename for loading.  */
-static char *solib_absolute_prefix = NULL;
-
-/* If non-empty, this is a search path for loading non-absolute shared library
-   symbol files.  This takes precedence over the environment variables PATH
-   and LD_LIBRARY_PATH.  */
-static char *solib_search_path = NULL;
-
-
 #ifndef SVR4_SHARED_LIBS
 
 /* Allocate the runtime common object file.  */
@@ -319,13 +309,13 @@ allocate_rt_common_objfile (void)
   memset (objfile, 0, sizeof (struct objfile));
   objfile->md = NULL;
   obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0,
-                             xmalloc, free);
+                             xmalloc, xfree);
   obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc,
-                             free);
+                             xfree);
   obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc,
-                             free);
+                             xfree);
   obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc,
-                             free);
+                             xfree);
   objfile->name = mstrsave (objfile->md, "rt_common");
 
   /* Add this file onto the tail of the linked list of other such files. */
@@ -362,7 +352,7 @@ solib_add_common_symbols (CORE_ADDR rtc_symp)
     {
       obstack_free (&rt_common_objfile->symbol_obstack, 0);
       obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0,
-                                 xmalloc, free);
+                                 xmalloc, xfree);
       rt_common_objfile->minimal_symbol_count = 0;
       rt_common_objfile->msymbols = NULL;
     }
@@ -395,7 +385,7 @@ solib_add_common_symbols (CORE_ADDR rtc_symp)
 
          prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
                                      mst_bss, rt_common_objfile);
-         free (name);
+         xfree (name);
        }
       rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next);
     }
@@ -450,7 +440,7 @@ bfd_lookup_symbol (bfd *abfd, char *symname)
   if (storage_needed > 0)
     {
       symbol_table = (asymbol **) xmalloc (storage_needed);
-      back_to = make_cleanup (free, (PTR) symbol_table);
+      back_to = make_cleanup (xfree, (PTR) symbol_table);
       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
 
       for (i = 0; i < number_of_symbols; i++)
@@ -477,7 +467,7 @@ bfd_lookup_symbol (bfd *abfd, char *symname)
   if (storage_needed > 0)
     {
       symbol_table = (asymbol **) xmalloc (storage_needed);
-      back_to = make_cleanup (free, (PTR) symbol_table);
+      back_to = make_cleanup (xfree, (PTR) symbol_table);
       number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
 
       for (i = 0; i < number_of_symbols; i++)
@@ -864,7 +854,7 @@ first_link_map_member (void)
 #else /* SVR4_SHARED_LIBS */
   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
   char *r_map_buf = xmalloc (lmo->r_map_size);
-  struct cleanup *cleanups = make_cleanup (free, r_map_buf);
+  struct cleanup *cleanups = make_cleanup (xfree, r_map_buf);
 
   read_memory (debug_base + lmo->r_map_offset, r_map_buf, lmo->r_map_size);
 
@@ -913,7 +903,7 @@ open_symbol_file_object (void *from_ttyp)
   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);
-  struct cleanup *cleanups = make_cleanup (free, l_name_buf);
+  struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
 
   if (symfile_objfile)
     if (!query ("Attempt to reload symbols from process? "))
@@ -948,7 +938,7 @@ open_symbol_file_object (void *from_ttyp)
       return 0;
     }
 
-  make_cleanup (free, filename);
+  make_cleanup (xfree, filename);
   /* Have a pathname: read the symbol file.  */
   symbol_file_command (filename, from_tty);
 
@@ -1011,15 +1001,15 @@ svr4_current_sos (void)
       struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
       struct so_list *new
        = (struct so_list *) xmalloc (sizeof (struct so_list));
-      struct cleanup *old_chain = make_cleanup (free, new);
+      struct cleanup *old_chain = make_cleanup (xfree, new);
 
       memset (new, 0, sizeof (*new));
 
       new->lm_info = xmalloc (sizeof (struct lm_info));
-      make_cleanup (free, new->lm_info);
+      make_cleanup (xfree, new->lm_info);
 
       new->lm_info->lm = xmalloc (lmo->link_map_size);
-      make_cleanup (free, new->lm_info->lm);
+      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);
@@ -1050,7 +1040,7 @@ svr4_current_sos (void)
            {
              strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
              new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
-             free (buffer);
+             xfree (buffer);
              strcpy (new->so_original_name, new->so_name);
            }
 
@@ -1269,7 +1259,9 @@ enable_break (void)
       unsigned int interp_sect_size;
       char *buf;
       CORE_ADDR load_addr;
-      bfd *tmp_bfd;
+      bfd *tmp_bfd = NULL;
+      int tmp_fd = -1;
+      char *tmp_pathname = NULL;
       CORE_ADDR sym_addr = 0;
 
       /* Read the contents of the .interp section into a local buffer;
@@ -1287,7 +1279,11 @@ enable_break (void)
          to find any magic formula to find it for Solaris (appears to
          be trivial on GNU/Linux).  Therefore, we have to try an alternate
          mechanism to find the dynamic linker's base address.  */
-      tmp_bfd = bfd_openr (buf, gnutarget);
+
+      tmp_fd  = solib_open (buf, &tmp_pathname);
+      if (tmp_fd >= 0)
+       tmp_bfd = bfd_fdopenr (tmp_pathname, gnutarget, tmp_fd);
+
       if (tmp_bfd == NULL)
        goto bkpt_at_symbol;
 
@@ -1437,6 +1433,112 @@ svr4_special_symbol_handling (void)
 #endif /* !SVR4_SHARED_LIBS */
 }
 
+/* Relocate the main executable.  This function should be called upon
+   stopping the inferior process at the entry point to the program. 
+   The entry point from BFD is compared to the PC and if they are
+   different, the main executable is relocated by the proper amount. 
+   
+   As written it will only attempt to relocate executables which
+   lack interpreter sections.  It seems likely that only dynamic
+   linker executables will get relocated, though it should work
+   properly for a position-independent static executable as well.  */
+
+static void
+svr4_relocate_main_executable (void)
+{
+  asection *interp_sect;
+  CORE_ADDR pc = read_pc ();
+
+  /* Decide if the objfile needs to be relocated.  As indicated above,
+     we will only be here when execution is stopped at the beginning
+     of the program.  Relocation is necessary if the address at which
+     we are presently stopped differs from the start address stored in
+     the executable AND there's no interpreter section.  The condition
+     regarding the interpreter section is very important because if
+     there *is* an interpreter section, execution will begin there
+     instead.  When there is an interpreter section, the start address
+     is (presumably) used by the interpreter at some point to start
+     execution of the program.
+
+     If there is an interpreter, it is normal for it to be set to an
+     arbitrary address at the outset.  The job of finding it is
+     handled in enable_break().
+
+     So, to summarize, relocations are necessary when there is no
+     interpreter section and the start address obtained from the
+     executable is different from the address at which GDB is
+     currently stopped.
+     
+     [ The astute reader will note that we also test to make sure that
+       the executable in question has the DYNAMIC flag set.  It is my
+       opinion that this test is unnecessary (undesirable even).  It
+       was added to avoid inadvertent relocation of an executable
+       whose e_type member in the ELF header is not ET_DYN.  There may
+       be a time in the future when it is desirable to do relocations
+       on other types of files as well in which case this condition
+       should either be removed or modified to accomodate the new file
+       type.  (E.g, an ET_EXEC executable which has been built to be
+       position-independent could safely be relocated by the OS if
+       desired.  It is true that this violates the ABI, but the ABI
+       has been known to be bent from time to time.)  - Kevin, Nov 2000. ]
+     */
+
+  interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
+  if (interp_sect == NULL 
+      && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0
+      && bfd_get_start_address (exec_bfd) != pc)
+    {
+      struct cleanup *old_chain;
+      struct section_offsets *new_offsets;
+      int i, changed;
+      CORE_ADDR displacement;
+      
+      /* It is necessary to relocate the objfile.  The amount to
+        relocate by is simply the address at which we are stopped
+        minus the starting address from the executable.
+
+        We relocate all of the sections by the same amount.  This
+        behavior is mandated by recent editions of the System V ABI. 
+        According to the System V Application Binary Interface,
+        Edition 4.1, page 5-5:
+
+          ...  Though the system chooses virtual addresses for
+          individual processes, it maintains the segments' relative
+          positions.  Because position-independent code uses relative
+          addressesing between segments, the difference between
+          virtual addresses in memory must match the difference
+          between virtual addresses in the file.  The difference
+          between the virtual address of any segment in memory and
+          the corresponding virtual address in the file is thus a
+          single constant value for any one executable or shared
+          object in a given process.  This difference is the base
+          address.  One use of the base address is to relocate the
+          memory image of the program during dynamic linking.
+
+        The same language also appears in Edition 4.0 of the System V
+        ABI and is left unspecified in some of the earlier editions.  */
+
+      displacement = pc - bfd_get_start_address (exec_bfd);
+      changed = 0;
+
+      new_offsets = xcalloc (sizeof (struct section_offsets),
+                            symfile_objfile->num_sections);
+      old_chain = make_cleanup (xfree, new_offsets);
+
+      for (i = 0; i < symfile_objfile->num_sections; i++)
+       {
+         if (displacement != ANOFFSET (symfile_objfile->section_offsets, i))
+           changed = 1;
+         new_offsets->offsets[i] = displacement;
+       }
+
+      if (changed)
+       objfile_relocate (symfile_objfile, new_offsets);
+
+      do_cleanups (old_chain);
+    }
+}
+
 /*
 
    GLOBAL FUNCTION
@@ -1489,9 +1591,12 @@ svr4_special_symbol_handling (void)
    Also, what if child has exit()ed?  Must exit loop somehow.
  */
 
-void
+static void
 svr4_solib_create_inferior_hook (void)
 {
+  /* Relocate the main executable if necessary.  */
+  svr4_relocate_main_executable ();
+
   /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
      yet.  In fact, in the case of a SunOS4 executable being run on
      Solaris, we can't get it yet.  current_sos will get it when it needs
@@ -1563,8 +1668,16 @@ svr4_clear_solib (void)
 static void
 svr4_free_so (struct so_list *so)
 {
-  free (so->lm_info->lm);
-  free (so->lm_info);
+  xfree (so->lm_info->lm);
+  xfree (so->lm_info);
+}
+
+static void
+svr4_relocate_section_addresses (struct so_list *so,
+                                 struct section_table *sec)
+{
+  sec->addr += LM_ADDR (so);
+  sec->endaddr += LM_ADDR (so);
 }
 
 static struct target_so_ops svr4_so_ops;
@@ -1572,7 +1685,7 @@ static struct target_so_ops svr4_so_ops;
 void
 _initialize_svr4_solib (void)
 {
-  svr4_so_ops.lm_addr = LM_ADDR;
+  svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
   svr4_so_ops.free_so = svr4_free_so;
   svr4_so_ops.clear_solib = svr4_clear_solib;
   svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
This page took 0.028702 seconds and 4 git commands to generate.