Using 'solib-search-path' on 64/32-bit targets

Dmitry Antipov <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
For the remote debugging on a 64-bit target with 32-bit compatibility, is it reasonable
to have gdb.setup with 'solib-search-path' pointed to both 64- and 32-bit libraries
(say, 'set solib-search-path /path/to/target/fs/lib64:/path/to/target/fs/lib32')? Currently GDB
just gives up the search if an inappropriate library is found, and will not try to examine
the rest of 'solib-search-path' entries. IOW if we're debugging 32-bit process with libXXX.so
dependency and 'solib-search-path' is set as above, /path/to/target/fs/lib32/libXXX.so won't
be found if /path/to/target/fs/lib64/libXXX.so is present. Attached is a simple attempt to
"fix" that behavior (toggled by new 'solib-search-giveup' flag), but I'm not sure whether
it needs to be "fixed" in that way at all :-).

Dmitry
gdb_solib_lookup_2.patch (text/x-patch, 3.6 KB)
diff --git a/gdb/solib.c b/gdb/solib.c
index 3dba5eaa8a..500f76c823 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -91,6 +91,16 @@ struct target_so_ops *current_target_so_ops;
 
 /* Local function prototypes */
 
+static int solib_search_giveup = 1;
+
+static void
+show_solib_search_giveup (struct ui_file *file, int from_tty,
+			  struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Giving up search after finding inappropriate "
+			    "library is %s.\n"), value);
+}
+
 /* 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.  */
@@ -493,7 +503,7 @@ solib_bfd_fopen (char *pathname, int fd)
 /* Find shared library PATHNAME and open a BFD for it.  */
 
 gdb_bfd_ref_ptr
-solib_bfd_open (char *pathname)
+solib_bfd_open_1 (char *pathname)
 {
   char *found_pathname;
   int found_file;
@@ -516,20 +526,59 @@ solib_bfd_open (char *pathname)
 
   /* Check bfd format.  */
   if (!bfd_check_format (abfd.get (), bfd_object))
-    error (_("`%s': not in executable format: %s"),
-	   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
+    {
+      if (solib_search_giveup)
+	error (_("`%s': not in executable format: %s"),
+	       bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
+      else
+	{
+	  warning (_("`%s': not in executable format: %s"),
+		   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
+	  return NULL;
+	}
+    }
 
   /* Check bfd arch.  */
   b = gdbarch_bfd_arch_info (target_gdbarch ());
   if (!b->compatible (b, bfd_get_arch_info (abfd.get ())))
-    warning (_("`%s': Shared library architecture %s is not compatible "
-               "with target architecture %s."), bfd_get_filename (abfd),
-             bfd_get_arch_info (abfd.get ())->printable_name,
-	     b->printable_name);
+    {
+      warning (_("`%s': Shared library architecture %s is not compatible "
+		 "with target architecture %s."), bfd_get_filename (abfd),
+	       bfd_get_arch_info (abfd.get ())->printable_name,
+	       b->printable_name);
+      if (!solib_search_giveup)
+	return NULL;
+    }
 
   return abfd;
 }
 
+gdb_bfd_ref_ptr
+solib_bfd_open (char *pathname)
+{
+  if (solib_search_path && !solib_search_giveup)
+    {
+      /* Lookup PATHNAME in all solib_search_path entries until
+	 suitable solib is found.  */
+      gdb_bfd_ref_ptr abfd;
+      char *savedpath = solib_search_path;
+      char *p, *orig, *sopath = xstrdup (solib_search_path);
+      const char sep[] { DIRNAME_SEPARATOR, '\0' };
+
+      orig = sopath;
+      while ((p = strsep (&sopath, sep)))
+	{
+	  solib_search_path = p;
+	  if ((abfd = solib_bfd_open_1 (pathname)) != NULL)
+	    break;
+	}
+      solib_search_path = savedpath;
+      xfree (orig);
+      return abfd;
+    }
+  return solib_bfd_open_1 (pathname);
+}
+
 /* Given a pointer to one of the shared objects in our list of mapped
    objects, use the recorded name to open a bfd descriptor for the
    object, build a section table, relocate all the section addresses
@@ -1654,6 +1703,11 @@ For other (relative) files, you can add directories using\n\
   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
 		 &showlist);
 
+  add_setshow_boolean_cmd ("solib-search-giveup", class_support,
+			   &solib_search_giveup, "DOCME", "DOCME", "DOCME",
+			   NULL, show_solib_search_giveup,
+			   &setlist, &showlist);
+
   add_setshow_optional_filename_cmd ("solib-search-path", class_support,
 				     &solib_search_path, _("\
 Set the search path for loading non-absolute shared library symbol files."),
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.