* stabsread.c (get_substring): Declare second arg as int.
[deliverable/binutils-gdb.git] / gdb / solib.c
index 89d7e7d2701727ec57f392aeb4327c7213c34975..b8fffd75d43c6cb0df037090974a566a52bac2fd 100644 (file)
@@ -1,5 +1,5 @@
 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
-   Copyright 1990, 1991, 1992, 1993, 1994, 1995
+   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
    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"
@@ -45,22 +49,30 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "command.h"
 #include "target.h"
 #include "frame.h"
-#include "regex.h"
+#include "gnu-regex.h"
 #include "inferior.h"
+#include "environ.h"
 #include "language.h"
 #include "gdbcmd.h"
 
 #define MAX_PATH_SIZE 512              /* FIXME: Should be dynamic */
 
-/* On SVR4 systems, for the initial implementation, use some runtime startup
-   symbol as the "startup mapping complete" breakpoint address.  The models
-   for SunOS and SVR4 dynamic linking debugger support are different in that
-   SunOS hits one breakpoint when all mapping is complete while using the SVR4
-   debugger support takes two breakpoint hits for each file mapped, and
-   there is no way to know when the "last" one is hit.  Both these
-   mechanisms should be tied to a "breakpoint service routine" that
-   gets automatically executed whenever one of the breakpoints indicating
-   a change in mapping is hit.  This is a future enhancement.  (FIXME) */
+/* On SVR4 systems, a list of symbols in the dynamic linker where
+   GDB can try to place a breakpoint to monitor shared library
+   events.
+
+   If none of these symbols are found, or other errors occur, then
+   SVR4 systems will fall back to using a symbol as the "startup
+   mapping complete" breakpoint address.  */
+
+#ifdef SVR4_SHARED_LIBS
+static char *solib_break_names[] = {
+  "r_debug_state",
+  "_r_debug_state",
+  "_dl_debug_state",
+  NULL
+};
+#endif
 
 #define BKPT_AT_SYMBOL 1
 
@@ -92,17 +104,6 @@ static char *main_name_list[] = {
 
 /* local data declarations */
 
-/* If true, then shared library symbols will be added automatically
-   when the inferior is created.  This is almost always what users
-   will want to have happen; but for very large programs, the startup
-   time will be excessive, and so if this is a problem, the user can
-   clear this flag and then add the shared library symbols as needed.
-   Note that there is a potential for confusion, since if the shared
-   library symbols are not loaded, commands like "info fun" will *not*
-   report all the functions that are actually present.  */
-   
-int auto_solib_add_at_startup = 1;
-
 #ifndef SVR4_SHARED_LIBS
 
 #define LM_ADDR(so) ((so) -> lm.lm_addr)
@@ -161,9 +162,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));
 
@@ -189,6 +187,9 @@ elf_locate_base PARAMS ((void));
 
 #else
 
+static int
+disable_break PARAMS ((void));
+
 static void
 allocate_rt_common_objfile PARAMS ((void));
 
@@ -197,6 +198,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
@@ -236,14 +246,43 @@ solib_map_sections (so)
   bfd *abfd;
   
   filename = tilde_expand (so -> so_name);
-  old_chain = make_cleanup (free, filename);
   
-  scratch_chan = openp (getenv ("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 (getenv ("LD_LIBRARY_PATH"), 1, filename,
-                           O_RDONLY, 0, &scratch_pathname);
+      scratch_chan = openp (get_in_environ 
+                           (inferior_environ, "LD_LIBRARY_PATH"), 
+                           1, filename, O_RDONLY, 0, &scratch_pathname);
     }
   if (scratch_chan < 0)
     {
@@ -310,6 +349,8 @@ allocate_rt_common_objfile ()
   objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
   memset (objfile, 0, sizeof (struct objfile));
   objfile -> md = NULL;
+  obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
+                             xmalloc, free);
   obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
                              free);
   obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
@@ -346,7 +387,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.  */
 
@@ -377,18 +417,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;
     }
@@ -404,34 +442,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 *));
 
@@ -492,6 +502,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
@@ -756,7 +794,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
     }
@@ -1011,6 +1049,13 @@ solib_add (arg_string, from_tty, target)
       
       if (count)
        {
+         int update_coreops;
+
+         /* We must update the to_sections field in the core_ops structure
+            here, otherwise we dereference a potential dangling pointer
+            for each call to target_read/write_memory within this routine.  */
+         update_coreops = core_ops.to_sections == target->to_sections;
+            
          /* Reallocate the target's section table including the new size.  */
          if (target -> to_sections)
            {
@@ -1027,6 +1072,14 @@ solib_add (arg_string, from_tty, target)
            }
          target -> to_sections_end = target -> to_sections + (count + old);
          
+         /* Update the to_sections field in the core_ops structure
+            if needed.  */
+         if (update_coreops)
+           {
+             core_ops.to_sections = target->to_sections;
+             core_ops.to_sections_end = target->to_sections_end;
+           }
+
          /* Add these section table entries to the target's table.  */
          while ((so = find_solib (so)) != NULL)
            {
@@ -1141,7 +1194,7 @@ GLOBAL FUNCTION
 
 SYNOPSIS
 
-       int solib_address (CORE_ADDR address)
+       char * solib_address (CORE_ADDR address)
 
 DESCRIPTION
 
@@ -1157,7 +1210,7 @@ DESCRIPTION
        mapped in.
  */
 
-int
+char *
 solib_address (address)
      CORE_ADDR address;
 {
@@ -1169,9 +1222,7 @@ solib_address (address)
        {
          if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
              (address < (CORE_ADDR) so -> lmend))
-           {
-             return (1);
-           }
+           return (so->so_name);
        }
     }
   return (0);
@@ -1228,6 +1279,8 @@ DESCRIPTION
 
 */
 
+#ifndef SVR4_SHARED_LIBS
+
 static int
 disable_break ()
 {
@@ -1276,6 +1329,8 @@ disable_break ()
   return (status);
 }
 
+#endif /* #ifdef SVR4_SHARED_LIBS */
+
 /*
 
 LOCAL FUNCTION
@@ -1360,7 +1415,81 @@ enable_break ()
 
   struct minimal_symbol *msymbol;
   char **bkpt_namep;
-  CORE_ADDR bkpt_addr;
+  asection *interp_sect;
+
+  /* First, remove all the solib event breakpoints.  Their addresses
+     may have changed since the last time we ran the program.  */
+  remove_solib_event_breakpoints ();
+
+#ifdef SVR4_SHARED_LIBS
+  /* 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");
+  if (interp_sect)
+    {
+      unsigned int interp_sect_size;
+      char *buf;
+      CORE_ADDR load_addr;
+      bfd *tmp_bfd;
+      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.  */
+      interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
+      buf = alloca (interp_sect_size);
+      bfd_get_section_contents (exec_bfd, interp_sect,
+                               buf, 0, interp_sect_size);
+
+      /* Now we need to figure out where the dynamic linker was
+        loaded so that we can load its symbols and place a breakpoint
+        in the dynamic linker itself.
+
+        This address is stored on the stack.  However, I've been unable
+        to find any magic formula to find it for Solaris (appears to
+        be trivial on Linux).  Therefore, we have to try an alternate
+        mechanism to find the dynamic linker's base address.  */
+      tmp_bfd = bfd_openr (buf, gnutarget);
+      if (tmp_bfd == NULL)
+       goto bkpt_at_symbol;
+
+      /* Make sure the dynamic linker's really a useful object.  */
+      if (!bfd_check_format (tmp_bfd, bfd_object))
+       {
+         warning ("Unable to grok dynamic linker %s as an object file", buf);
+         bfd_close (tmp_bfd);
+         goto bkpt_at_symbol;
+       }
+
+      /* We find the dynamic linker's base address by examining the
+        current pc (which point at the entry point for the dynamic
+        linker) and subtracting the offset of the entry point.  */
+      load_addr = read_pc () - tmp_bfd->start_address;
+
+      /* 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);
+
+      if (sym_addr != 0)
+       {
+         create_solib_event_breakpoint (load_addr + sym_addr);
+         return 1;
+       }
+
+      /* 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.");
+      warning ("GDB will be unable to debug shared library initializers");
+      warning ("and track explicitly loaded dynamic code.");
+    }
+#endif
 
   /* Scan through the list of symbols, trying to look up the symbol and
      set a breakpoint there.  Terminate loop when we/if we succeed. */
@@ -1371,31 +1500,13 @@ enable_break ()
       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
        {
-         bkpt_addr = SYMBOL_VALUE_ADDRESS (msymbol);
-         if (target_insert_breakpoint (bkpt_addr, shadow_contents) == 0)
-           {
-             breakpoint_addr = bkpt_addr;
-             success = 1;
-             break;
-           }
+         create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
+         return 1;
        }
     }
 
-#else  /* !BKPT_AT_SYMBOL */
-
-  struct symtab_and_line sal;
-
-  /* Read the debugger interface structure directly. */
-
-  read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
-
-  /* Set breakpoint at the debugger interface stub routine that will
-     be called just prior to each mapping change and again after the
-     mapping change is complete.  Set up the (nonexistent) handler to
-     deal with hitting these breakpoints.  (FIXME). */
-
-  warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
-  success = 1;
+  /* Nothing good happened.  */
+  return 0;
 
 #endif /* BKPT_AT_SYMBOL */
 
@@ -1477,7 +1588,12 @@ solib_create_inferior_hook()
       return;
     }
 
-  /* Now run the target.  It will eventually hit the breakpoint, at
+#ifndef SVR4_SHARED_LIBS
+  /* Only SunOS needs the loop below, other systems should be using the
+     special shared library breakpoints and the shared library breakpoint
+     service routine.
+
+     Now run the target.  It will eventually hit the breakpoint, at
      which point all of the libraries will have been mapped in and we
      can go groveling around in the dynamic linker structures to find
      out what we need to know about them. */
@@ -1509,8 +1625,9 @@ solib_create_inferior_hook()
       warning ("shared library handler failed to disable breakpoint");
     }
 
-  if (auto_solib_add_at_startup)
+  if (auto_solib_add)
     solib_add ((char *) 0, 0, (struct target_ops *) 0);
+#endif
 }
 
 /*
@@ -1604,10 +1721,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, 
@@ -1615,11 +1735,29 @@ _initialize_solib()
 
   add_show_from_set
     (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
-                 (char *) &auto_solib_add_at_startup,
-                 "Set autoloading of shared library symbols at startup.\n\
+                 (char *) &auto_solib_add,
+                 "Set autoloading of shared library symbols.\n\
 If nonzero, symbols from all shared object libraries will be loaded\n\
-automatically when the inferior begins execution.  Otherwise, symbols\n\
+automatically when the inferior begins execution or when the dynamic linker\n\
+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.029477 seconds and 4 git commands to generate.