Wed Mar 4 16:50:18 1998 Jason Molenda (crash@bugshack.cygnus.com)
[deliverable/binutils-gdb.git] / gdb / solib.c
index cc6019680d75a249d72655ae1674c3b729b3ce23..b09b140af8b0086b265c40b4bf756ee48b8a2f88 100644 (file)
@@ -1,5 +1,5 @@
 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
-   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
+   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998
    Free Software Foundation, Inc.
    
 This file is part of GDB.
@@ -21,6 +21,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "defs.h"
 
+/* This file is only compilable if link.h is available. */
+
+#ifdef HAVE_LINK_H
+
 #include <sys/types.h>
 #include <signal.h>
 #include "gdb_string.h"
@@ -64,7 +68,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #ifdef SVR4_SHARED_LIBS
 static char *solib_break_names[] = {
   "r_debug_state",
+  "_r_debug_state",
   "_dl_debug_state",
+  "rtld_db_dlactivity",
   NULL
 };
 #endif
@@ -143,11 +149,19 @@ static struct so_list *so_list_head;      /* List of known shared objects */
 static CORE_ADDR debug_base;           /* Base of dynamic linker structures */
 static CORE_ADDR breakpoint_addr;      /* Address where end bkpt is set */
 
+static int solib_cleanup_queued = 0;    /* make_run_cleanup called */
+
 extern int
 fdmatch PARAMS ((int, int));           /* In libiberty */
 
 /* Local function prototypes */
 
+static void
+do_clear_solib PARAMS ((PTR));
+
+static int
+match_main PARAMS ((char *));
+
 static void
 special_symbol_handling PARAMS ((struct so_list *));
 
@@ -157,9 +171,6 @@ sharedlibrary_command PARAMS ((char *, int));
 static int
 enable_break PARAMS ((void));
 
-static int
-disable_break PARAMS ((void));
-
 static void
 info_sharedlibrary_command PARAMS ((char *, int));
 
@@ -185,6 +196,9 @@ elf_locate_base PARAMS ((void));
 
 #else
 
+static int
+disable_break PARAMS ((void));
+
 static void
 allocate_rt_common_objfile PARAMS ((void));
 
@@ -193,6 +207,15 @@ solib_add_common_symbols PARAMS ((struct rtc_symb *));
 
 #endif
 
+/* 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;
+
 /*
 
 LOCAL FUNCTION
@@ -232,10 +255,38 @@ solib_map_sections (so)
   bfd *abfd;
   
   filename = tilde_expand (so -> so_name);
-  old_chain = make_cleanup (free, filename);
   
-  scratch_chan = openp (get_in_environ (inferior_environ, "PATH"), 
-                       1, filename, O_RDONLY, 0, &scratch_pathname);
+  if (solib_absolute_prefix && ROOTED_P (filename))
+    /* Prefix shared libraries with absolute filenames with
+       SOLIB_ABSOLUTE_PREFIX.  */
+    {
+      char *pfxed_fn;
+      int pfx_len;
+
+      pfx_len = strlen (solib_absolute_prefix);
+
+      /* Remove trailing slashes.  */
+      while (pfx_len > 0 && SLASH_P (solib_absolute_prefix[pfx_len - 1]))
+       pfx_len--;
+
+      pfxed_fn = xmalloc (pfx_len + strlen (filename) + 1);
+      strcpy (pfxed_fn, solib_absolute_prefix);
+      strcat (pfxed_fn, filename);
+      free (filename);
+
+      filename = pfxed_fn;
+    }
+
+  old_chain = make_cleanup (free, filename);
+
+  scratch_chan = -1;
+
+  if (solib_search_path)
+    scratch_chan = openp (solib_search_path,
+                         1, filename, O_RDONLY, 0, &scratch_pathname);
+  if (scratch_chan < 0)
+    scratch_chan = openp (get_in_environ (inferior_environ, "PATH"), 
+                         1, filename, O_RDONLY, 0, &scratch_pathname);
   if (scratch_chan < 0)
     {
       scratch_chan = openp (get_in_environ 
@@ -345,7 +396,6 @@ solib_add_common_symbols (rtc_symp)
   struct nlist inferior_rtc_nlist;
   int len;
   char *name;
-  char *origname;
 
   /* Remove any runtime common symbols from previous runs.  */
 
@@ -376,18 +426,16 @@ solib_add_common_symbols (rtc_symp)
             behind the name of the symbol. */
          len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
 
-         origname = name = xmalloc (len);
+         name = xmalloc (len);
          read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
 
          /* Allocate the runtime common objfile if necessary. */
          if (rt_common_objfile == NULL)
            allocate_rt_common_objfile ();
 
-         name = obsavestring (name, strlen (name),
-                              &rt_common_objfile -> symbol_obstack);
          prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
                                      mst_bss, rt_common_objfile);
-         free (origname);
+         free (name);
        }
       rtc_symp = inferior_rtc_symb.rtc_next;
     }
@@ -403,34 +451,6 @@ solib_add_common_symbols (rtc_symp)
 
 #ifdef SVR4_SHARED_LIBS
 
-#ifdef HANDLE_SVR4_EXEC_EMULATORS
-
-/*
-       Solaris BCP (the part of Solaris which allows it to run SunOS4
-       a.out files) throws in another wrinkle. Solaris does not fill
-       in the usual a.out link map structures when running BCP programs,
-       the only way to get at them is via groping around in the dynamic
-       linker.
-       The dynamic linker and it's structures are located in the shared
-       C library, which gets run as the executable's "interpreter" by
-       the kernel.
-
-       Note that we can assume nothing about the process state at the time
-       we need to find these structures.  We may be stopped on the first
-       instruction of the interpreter (C shared library), the first
-       instruction of the executable itself, or somewhere else entirely
-       (if we attached to the process for example).
-*/
-
-static char *debug_base_symbols[] = {
-  "r_debug",   /* Solaris 2.3 */
-  "_r_debug",  /* Solaris 2.1, 2.2 */
-  NULL
-};
-
-static int
-look_for_base PARAMS ((int, CORE_ADDR));
-
 static CORE_ADDR
 bfd_lookup_symbol PARAMS ((bfd *, char *));
 
@@ -491,6 +511,34 @@ bfd_lookup_symbol (abfd, symname)
   return (symaddr);
 }
 
+#ifdef HANDLE_SVR4_EXEC_EMULATORS
+
+/*
+       Solaris BCP (the part of Solaris which allows it to run SunOS4
+       a.out files) throws in another wrinkle. Solaris does not fill
+       in the usual a.out link map structures when running BCP programs,
+       the only way to get at them is via groping around in the dynamic
+       linker.
+       The dynamic linker and it's structures are located in the shared
+       C library, which gets run as the executable's "interpreter" by
+       the kernel.
+
+       Note that we can assume nothing about the process state at the time
+       we need to find these structures.  We may be stopped on the first
+       instruction of the interpreter (C shared library), the first
+       instruction of the executable itself, or somewhere else entirely
+       (if we attached to the process for example).
+*/
+
+static char *debug_base_symbols[] = {
+  "r_debug",   /* Solaris 2.3 */
+  "_r_debug",  /* Solaris 2.1, 2.2 */
+  NULL
+};
+
+static int
+look_for_base PARAMS ((int, CORE_ADDR));
+
 /*
 
 LOCAL FUNCTION
@@ -755,7 +803,7 @@ locate_base ()
        debug_base = elf_locate_base ();
 #ifdef HANDLE_SVR4_EXEC_EMULATORS
       /* Try it the hard way for emulated executables.  */
-      else if (inferior_pid != 0)
+      else if (inferior_pid != 0 && target_has_execution)
        proc_iterate_over_mappings (look_for_base);
 #endif
     }
@@ -897,6 +945,13 @@ find_solib (so_list_ptr)
       else
        {
          so_list_head = new;
+
+         if (! solib_cleanup_queued)
+           {
+             make_run_cleanup (do_clear_solib);
+             solib_cleanup_queued = 1;
+           }
+         
        }      
       so_list_next = new;
       read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
@@ -931,12 +986,34 @@ symbol_add_stub (arg)
      char *arg;
 {
   register struct so_list *so = (struct so_list *) arg;        /* catch_errs bogon */
+  CORE_ADDR text_addr = 0;
+
+  if (so -> textsection)
+    text_addr = so -> textsection -> addr;
+  else
+    {
+      asection *lowest_sect;
+
+      /* If we didn't find a mapped non zero sized .text section, set up
+        text_addr so that the relocation in symbol_file_add does no harm.  */
+
+      lowest_sect = bfd_get_section_by_name (so -> abfd, ".text");
+      if (lowest_sect == NULL)
+       bfd_map_over_sections (so -> abfd, find_lowest_section,
+                              (PTR) &lowest_sect);
+      if (lowest_sect)
+       text_addr = bfd_section_vma (so -> abfd, lowest_sect)
+                   + (CORE_ADDR) LM_ADDR (so);
+    }
   
+  ALL_OBJFILES (so -> objfile)
+    {
+      if (strcmp (so -> objfile -> name, so -> so_name) == 0)
+       return 1;
+    }
   so -> objfile =
     symbol_file_add (so -> so_name, so -> from_tty,
-                    (so->textsection == NULL
-                     ? 0
-                     : (unsigned int) so -> textsection -> addr),
+                    text_addr,
                     0, 0, 0);
   return (1);
 }
@@ -1223,6 +1300,34 @@ clear_solib()
   debug_base = 0;
 }
 
+static void
+do_clear_solib (dummy)
+     PTR dummy;
+{
+  solib_cleanup_queued = 0;
+  clear_solib ();
+}
+
+#ifdef SVR4_SHARED_LIBS
+
+/* Return 1 if PC lies in the dynamic symbol resolution code of the
+   SVR4 run time loader.  */
+
+static CORE_ADDR interp_text_sect_low;
+static CORE_ADDR interp_text_sect_high;
+static CORE_ADDR interp_plt_sect_low;
+static CORE_ADDR interp_plt_sect_high;
+
+int
+in_svr4_dynsym_resolve_code (pc)
+     CORE_ADDR pc;
+{
+  return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
+         || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
+         || in_plt_section (pc, NULL));
+}
+#endif
+
 /*
 
 LOCAL FUNCTION
@@ -1240,6 +1345,8 @@ DESCRIPTION
 
 */
 
+#ifndef SVR4_SHARED_LIBS
+
 static int
 disable_break ()
 {
@@ -1288,6 +1395,8 @@ disable_break ()
   return (status);
 }
 
+#endif /* #ifdef SVR4_SHARED_LIBS */
+
 /*
 
 LOCAL FUNCTION
@@ -1371,9 +1480,7 @@ enable_break ()
 #ifdef BKPT_AT_SYMBOL
 
   struct minimal_symbol *msymbol;
-  struct objfile *objfile;
   char **bkpt_namep;
-  CORE_ADDR bkpt_addr;
   asection *interp_sect;
 
   /* First, remove all the solib event breakpoints.  Their addresses
@@ -1381,6 +1488,9 @@ enable_break ()
   remove_solib_event_breakpoints ();
 
 #ifdef SVR4_SHARED_LIBS
+  interp_text_sect_low = interp_text_sect_high = 0;
+  interp_plt_sect_low = interp_plt_sect_high = 0;
+
   /* Find the .interp section; if not found, warn the user and drop
      into the old breakpoint at symbol code.  */
   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
@@ -1390,7 +1500,7 @@ enable_break ()
       char *buf;
       CORE_ADDR load_addr;
       bfd *tmp_bfd;
-      asection *lowest_sect;
+      CORE_ADDR sym_addr = 0;
 
       /* Read the contents of the .interp section into a local buffer;
         the contents specify the dynamic linker this program uses.  */
@@ -1424,42 +1534,40 @@ enable_break ()
         linker) and subtracting the offset of the entry point.  */
       load_addr = read_pc () - tmp_bfd->start_address;
 
-      /* load_addr now has the base address of the dynamic linker;
-        however, due to severe braindamage in syms_from_objfile
-        we need to add the address of the .text section, or the
-        lowest section of .text doesn't exist to work around the
-        braindamage.  Gross.  */
-      lowest_sect = bfd_get_section_by_name (tmp_bfd, ".text");
-      if (lowest_sect == NULL)
-        bfd_map_over_sections (tmp_bfd, find_lowest_section,
-                               (PTR) &lowest_sect);
-
-      if (lowest_sect == NULL)
+      /* Record the relocated start and end address of the dynamic linker
+        text and plt section for in_svr4_dynsym_resolve_code.  */
+      interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
+      if (interp_sect)
        {
-         warning ("Unable to find base address for dynamic linker %s\n", buf);
-         bfd_close (tmp_bfd);
-         goto bkpt_at_symbol;
+         interp_text_sect_low =
+           bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
+         interp_text_sect_high =
+           interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
+       }
+      interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
+      if (interp_sect)
+       {
+         interp_plt_sect_low =
+           bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
+         interp_plt_sect_high =
+           interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
        }
 
-      load_addr += bfd_section_vma (tmp_bfd, lowest_sect);
+      /* Now try to set a breakpoint in the dynamic linker.  */
+      for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
+       {
+         sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
+         if (sym_addr != 0)
+           break;
+       }
 
       /* We're done with the temporary bfd.  */
       bfd_close (tmp_bfd);
 
-      /* Now make GDB aware of the symbols in the dynamic linker.  Some
-        might complain about namespace pollution, but as a developer I've
-        often wanted these symbols available from within the debugger.  */
-      objfile = symbol_file_add (buf, 0, load_addr, 0, 0, 1);
-
-      /* Now try to set a breakpoint in the dynamic linker.  */
-      for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
+      if (sym_addr != 0)
        {
-         msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objfile);
-         if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
-           {
-             create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
-             return 1;
-           }
+         create_solib_event_breakpoint (load_addr + sym_addr);
+         return 1;
        }
 
       /* For whatever reason we couldn't set a breakpoint in the dynamic
@@ -1486,7 +1594,7 @@ bkpt_at_symbol:
     }
 
   /* Nothing good happened.  */
-  return 0;
+  success = 0;
 
 #endif /* BKPT_AT_SYMBOL */
 
@@ -1701,10 +1809,13 @@ int from_tty;
   solib_add (args, from_tty, (struct target_ops *) 0);
 }
 
+#endif /* HAVE_LINK_H */
+
 void
 _initialize_solib()
 {
-  
+#ifdef HAVE_LINK_H
+
   add_com ("sharedlibrary", class_files, sharedlibrary_command,
           "Load shared object library symbols for files matching REGEXP.");
   add_info ("sharedlibrary", info_sharedlibrary_command, 
@@ -1720,4 +1831,21 @@ informs gdb that a new library has been loaded.  Otherwise, symbols\n\
 must be loaded manually, using `sharedlibrary'.",
                  &setlist),
      &showlist);
+
+  add_show_from_set
+    (add_set_cmd ("solib-absolute-prefix", class_support, var_filename,
+                 (char *) &solib_absolute_prefix,
+                 "Set prefix for loading absolute shared library symbol files.\n\
+For other (relative) files, you can add values using `set solib-search-path'.",
+                 &setlist),
+     &showlist);
+  add_show_from_set
+    (add_set_cmd ("solib-search-path", class_support, var_string,
+                 (char *) &solib_search_path,
+                 "Set the search path for loading non-absolute shared library symbol files.\n\
+This takes precedence over the environment variables PATH and LD_LIBRARY_PATH.",
+                 &setlist),
+     &showlist);
+
+#endif /* HAVE_LINK_H */
 }
This page took 0.034076 seconds and 4 git commands to generate.