This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[RFA] Encapsulate method of searching for a solib.



The enable_break function from solib-svr4.c needs to use the same
search method for shared libs as is used in solib.c (utilizing
SOLIB_ABSOLUTE_PREFIX and SOLIB_SEARCH_PATH), so I've abstracted
this method into a function and cleaned it up a bit.

2000-10-31  Michael Snyder  <msnyder@cleaver.cygnus.com>

        * solib.c (solib_open): New function.  Abstracts the code from
        solib_map_sections that searches for a solib file.
        (solib_map_sections): Call solib_open instead of working inline.
        * solib-svr4.c (enable_break): Call solib_open to find the
        dynamic linker's base address.
        * solib-svr4.h (solib_open): Export the declaration.

Index: solib.c
===================================================================
RCS file: /cvs/cvsfiles/devo/gdb/solib.c,v
retrieving revision 1.154
diff -c -3 -p -r1.154 solib.c
*** solib.c	2000/10/31 20:19:49	1.154
--- solib.c	2000/10/31 21:59:52
*************** static char *solib_search_path = NULL;
*** 66,71 ****
--- 66,150 ----
  
  /*
  
+    GLOBAL FUNCTION
+ 
+    solib_open -- Find a shared library file and open it.
+ 
+    SYNOPSIS
+ 
+    int solib_open (char *in_patname, char **found_pathname);
+ 
+    DESCRIPTION
+ 
+    Global variable SOLIB_ABSOLUTE_PREFIX is used as a prefix directory
+    to search for shared libraries if they have an absolute path.
+ 
+    Global variable SOLIB_SEARCH_PATH is used as a prefix directory
+    (or set of directories, as in LD_LIBRARY_PATH) to search for all
+    shared libraries if not found in SOLIB_ABSOLUTE_PREFIX.
+ 
+    Search order:
+    * If path is absolute, look in SOLIB_ABSOLUTE_PREFIX.
+    * If path is absolute, look for it literally (unmodified).
+    * Look in SOLIB_SEARCH_PATH.
+    * Look in inferior's $PATH.
+    * Look in inferior's $LD_LIBRARY_PATH.
+ 
+    RETURNS
+    
+    file handle for opened solib, or -1 for failure.  */
+ 
+ int
+ solib_open (char *in_pathname, char **found_pathname)
+ {
+   int found_file = -1;
+   char *temp_pathname = NULL;
+ 
+   if (solib_absolute_prefix != NULL &&
+       ROOTED_P (in_pathname))
+     {
+       int  prefix_len = strlen (solib_absolute_prefix);
+ 
+       /* Remove trailing slashes from absolute prefix.  */
+       while (prefix_len > 0 && SLASH_P (solib_absolute_prefix[prefix_len - 1]))
+ 	prefix_len--;
+ 
+       /* Cat the prefixed pathname together.  */
+       temp_pathname = alloca (prefix_len + strlen (in_pathname) + 1);
+       strncpy (temp_pathname, solib_absolute_prefix, prefix_len);
+       temp_pathname[prefix_len] = '\0';
+       strcat (temp_pathname, in_pathname);
+ 
+       /* Now see if we can open it.  */
+       found_file = open (temp_pathname, O_RDONLY, 0);
+     }
+ 
+   /* If not found, next search the solib_search_path (if any).  */
+   if (found_file < 0 && solib_search_path != NULL)
+     found_file = openp (solib_search_path,
+ 			1, in_pathname, O_RDONLY, 0, &temp_pathname);
+ 
+   /* If not found, next search the inferior's $PATH environment variable. */
+   if (found_file < 0 && solib_search_path != NULL)
+     found_file = openp (get_in_environ (inferior_environ, "PATH"),
+ 			1, in_pathname, O_RDONLY, 0, &temp_pathname);
+ 
+   /* If not found, next search the inferior's $LD_LIBRARY_PATH 
+      environment variable. */
+   if (found_file < 0 && solib_search_path != NULL)
+     found_file = openp (get_in_environ (inferior_environ, "LD_LIBRARY_PATH"),
+ 			1, in_pathname, O_RDONLY, 0, &temp_pathname);
+ 
+   /* Done.  If not found, tough luck.  Return found_file and 
+      (optionally) found_pathname.  */
+   if (found_pathname != NULL)
+     *found_pathname = strsave (temp_pathname);
+   return found_file;
+ }
+ 
+ 
+ /*
+ 
     LOCAL FUNCTION
  
     solib_map_sections -- open bfd and build sections for shared lib
*************** solib_map_sections (PTR arg)
*** 104,152 ****
  
    filename = tilde_expand (so->so_name);
  
-   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
- 			    (inferior_environ, "LD_LIBRARY_PATH"),
- 			    1, filename, O_RDONLY, 0, &scratch_pathname);
-     }
    if (scratch_chan < 0)
      {
        perror_with_name (filename);
      }
-   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
  
    abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
    if (!abfd)
      {
--- 183,197 ----
  
    filename = tilde_expand (so->so_name);
  
    old_chain = make_cleanup (free, filename);
+   scratch_chan = solib_open (filename, &scratch_pathname);
  
    if (scratch_chan < 0)
      {
        perror_with_name (filename);
      }
  
+   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
    abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
    if (!abfd)
      {
*************** solib_map_sections (PTR arg)
*** 154,165 ****
        error ("Could not open `%s' as an executable file: %s",
  	     scratch_pathname, bfd_errmsg (bfd_get_error ()));
      }
    /* Leave bfd open, core_xfer_memory and "info files" need it.  */
    so->abfd = abfd;
    abfd->cacheable = true;
  
!   /* copy full path name into so_name, so that later symbol_file_add can find
!      it */
    if (strlen (scratch_pathname) >= SO_NAME_MAX_PATH_SIZE)
      error ("Full path name length of shared library exceeds SO_NAME_MAX_PATH_SIZE in so_list structure.");
    strcpy (so->so_name, scratch_pathname);
--- 199,211 ----
        error ("Could not open `%s' as an executable file: %s",
  	     scratch_pathname, bfd_errmsg (bfd_get_error ()));
      }
+ 
    /* Leave bfd open, core_xfer_memory and "info files" need it.  */
    so->abfd = abfd;
    abfd->cacheable = true;
  
!   /* copy full path name into so_name, so that later symbol_file_add
!      can find it */
    if (strlen (scratch_pathname) >= SO_NAME_MAX_PATH_SIZE)
      error ("Full path name length of shared library exceeds SO_NAME_MAX_PATH_SIZE in so_list structure.");
    strcpy (so->so_name, scratch_pathname);
Index: solib-svr4.c
===================================================================
RCS file: /cvs/cvsfiles/devo/gdb/solib-svr4.c,v
retrieving revision 1.3
diff -c -3 -p -r1.3 solib-svr4.c
*** solib-svr4.c	2000/10/31 20:19:49	1.3
--- solib-svr4.c	2000/10/31 21:59:52
*************** enable_break (void)
*** 1269,1275 ****
        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;
--- 1269,1277 ----
        unsigned int interp_sect_size;
        char *buf;
        CORE_ADDR load_addr;
!       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;
*************** enable_break (void)
*** 1287,1293 ****
           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);
        if (tmp_bfd == NULL)
  	goto bkpt_at_symbol;
  
--- 1289,1299 ----
           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_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;
  
Index: solib-svr4.h
===================================================================
RCS file: /cvs/cvsfiles/devo/gdb/solib-svr4.h,v
retrieving revision 1.2
diff -c -3 -p -r1.2 solib-svr4.h
*** solib-svr4.h	2000/10/30 22:03:33	1.2
--- solib-svr4.h	2000/10/31 21:59:52
*************** struct link_map_offsets
*** 64,69 ****
--- 64,72 ----
      int l_name_size;
    };
  
+ /* Find solib binary file and open it.  */
+ extern int solib_open (char *in_pathname, char **found_pathname);
+ 
  #ifndef SVR4_FETCH_LINK_MAP_OFFSETS
  extern struct link_map_offsets *default_svr4_fetch_link_map_offsets (void);
  #define SVR4_FETCH_LINK_MAP_OFFSETS() default_svr4_fetch_link_map_offsets ()

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]