public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* A patch for bug in rpath/rpath-link
@ 2001-04-30 22:04 H . J . Lu
  2001-05-01  9:30 ` H . J . Lu
  0 siblings, 1 reply; 2+ messages in thread
From: H . J . Lu @ 2001-04-30 22:04 UTC (permalink / raw)
  To: binutils; +Cc: GNU C Library

[-- Attachment #1: Type: text/plain, Size: 3927 bytes --]

The problem with -rpath is when you pass -rpath,./ to ld, the linker
records set

Version References:
  required from .//lib-a.so:
	        ^^^^^^^
    0x0d696910 0x00 03 GLIBC_2.0

in the binary. It won't match the runtime linker which loads
./lib-a.so. As the result, we get a core dump. Here is a patch. I am
also enclosing a testcase here.


H.J.
----
2001-04-30  H.J. Lu  <hjl@gnu.org>

	* lexsup.c (strip_slash): New. A function to strip slash from
	rpath.
	(parse_args): Call strip_slash () for OPTION_RPATH and
	OPTION_RPATH_LINK.

Index: lexsup.c
===================================================================
RCS file: /work/cvs/gnu/binutils/ld/lexsup.c,v
retrieving revision 1.29
diff -u -p -r1.29 lexsup.c
--- lexsup.c	2001/04/13 18:47:36	1.29
+++ lexsup.c	2001/05/01 04:55:47
@@ -61,6 +61,7 @@ static int is_num PARAMS ((const char *,
 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
 static void set_section_start PARAMS ((char *sect, char *valstr));
 static void help PARAMS ((void));
+static char *strip_slash PARAMS ((char *));
 
 /* Non-zero if we are processing a --defsym from the command line.  */
 int parsing_defsym = 0;
@@ -435,6 +436,64 @@ is_num (string, min, max, err)
   return result;
 }
 
+static char *
+strip_slash (path)
+     char *path;
+{
+  char *s, *t;
+  boolean found;
+  size_t len = strlen (slash);
+
+  t = path;
+  s = t;
+  found = (strncmp (s, slash, len) == 0);
+  if (found)
+    {
+      t += len;
+      s += len;
+    }
+  else
+    {
+      t++;
+      s++;
+    }
+  while (*s)
+    {
+      if (strncmp (s, slash, len) == 0)
+	{
+	  if (found)
+	    {
+	      s += len;
+	      break;
+	    }
+	  found = true;
+	}
+      else
+	found = false;
+      if (found)
+	{
+	  memmove (t, s, len);
+	  t += len;
+	  s += len;
+	}
+      else
+	{
+	  *t = *s;
+	  t++;
+	  s++;
+	}
+    }
+
+  while ((t - len) > path && strncmp (t - len, slash, len) == 0)
+    {
+      t -= len;
+      *t = '\0';
+    }
+  *t = '\0';
+
+  return path;
+}
+
 void
 parse_args (argc, argv)
      unsigned argc;
@@ -818,11 +877,12 @@ parse_args (argc, argv)
 	  /* Fall through.  */
 	case OPTION_RPATH:
 	  if (command_line.rpath == NULL)
-	    command_line.rpath = buystring (optarg);
+	    command_line.rpath = strip_slash (buystring (optarg));
 	  else
 	    {
+	      char *rpath = strip_slash (buystring (optarg));
 	      size_t rpath_len = strlen (command_line.rpath);
-	      size_t optarg_len = strlen (optarg);
+	      size_t optarg_len = strlen (rpath);
 	      char *buf;
 	      char *cp = command_line.rpath;
 
@@ -830,9 +890,9 @@ parse_args (argc, argv)
 	      do
 		{
 		  size_t idx = 0;
-		  while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
+		  while (rpath[idx] != '\0' && rpath[idx] == cp[idx])
 		    ++idx;
-		  if (optarg[idx] == '\0'
+		  if (rpath[idx] == '\0'
 		      && (cp[idx] == '\0' || cp[idx] == ':'))
 		    /* We found it.  */
 		    break;
@@ -847,24 +907,27 @@ parse_args (argc, argv)
 	      if (cp == NULL)
 		{
 		  buf = xmalloc (rpath_len + optarg_len + 2);
-		  sprintf (buf, "%s:%s", command_line.rpath, optarg);
+		  sprintf (buf, "%s:%s", command_line.rpath, rpath);
 		  free (command_line.rpath);
+		  free (rpath);
 		  command_line.rpath = buf;
 		}
 	    }
 	  break;
 	case OPTION_RPATH_LINK:
 	  if (command_line.rpath_link == NULL)
-	    command_line.rpath_link = buystring (optarg);
+	    command_line.rpath_link = strip_slash (buystring (optarg));
 	  else
 	    {
+	      char *rpath = strip_slash (buystring (optarg));
 	      char *buf;
 
 	      buf = xmalloc (strlen (command_line.rpath_link)
-			     + strlen (optarg)
+			     + strlen (rpath)
 			     + 2);
-	      sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
+	      sprintf (buf, "%s:%s", command_line.rpath_link, rpath);
 	      free (command_line.rpath_link);
+	      free (rpath);
 	      command_line.rpath_link = buf;
 	    }
 	  break;

[-- Attachment #2: needed.3.tar.gz --]
[-- Type: application/x-gzip, Size: 605 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: A patch for bug in rpath/rpath-link
  2001-04-30 22:04 A patch for bug in rpath/rpath-link H . J . Lu
@ 2001-05-01  9:30 ` H . J . Lu
  0 siblings, 0 replies; 2+ messages in thread
From: H . J . Lu @ 2001-05-01  9:30 UTC (permalink / raw)
  To: binutils; +Cc: GNU C Library

On Mon, Apr 30, 2001 at 10:04:32PM -0700, H . J . Lu wrote:
> The problem with -rpath is when you pass -rpath,./ to ld, the linker
> records set
> 
> Version References:
>   required from .//lib-a.so:
> 	        ^^^^^^^
>     0x0d696910 0x00 03 GLIBC_2.0
> 
> in the binary. It won't match the runtime linker which loads
> ./lib-a.so. As the result, we get a core dump. Here is a patch. I am
> also enclosing a testcase here.
> 
> 

Well, my patch is incorrect. Here is the new one. The problem is we
should only put the soname in the required file for version references.
This patch should the right thing. I cannot believe we haven't been
bitten by this bug :-(. I also changed elf32.em to use basename. Any
comments?


Thanks.


H.J.
----
Index: bfd/elfcode.h
===================================================================
RCS file: /work/cvs/gnu/binutils/bfd/elfcode.h,v
retrieving revision 1.1.1.14
diff -u -p -r1.1.1.14 elfcode.h
--- bfd/elfcode.h	2001/03/09 19:15:58	1.1.1.14
+++ bfd/elfcode.h	2001/05/01 16:21:51
@@ -66,6 +66,7 @@ Foundation, Inc., 59 Temple Place - Suit
 
 #include "bfd.h"
 #include "sysdep.h"
+#include "libiberty.h"
 #include "bfdlink.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
Index: bfd/elflink.h
===================================================================
RCS file: /work/cvs/gnu/binutils/bfd/elflink.h,v
retrieving revision 1.57
diff -u -p -r1.57 elflink.h
--- bfd/elflink.h	2001/04/27 21:05:00	1.57
+++ bfd/elflink.h	2001/05/01 16:21:52
@@ -1334,7 +1334,7 @@ elf_link_add_object_symbols (abfd, info)
       /* Save the SONAME, if there is one, because sometimes the
          linker emulation code will need to know it.  */
       if (*name == '\0')
-	name = bfd_get_filename (abfd);
+	name = basename (bfd_get_filename (abfd));
       elf_dt_name (abfd) = name;
     }
 
@@ -3329,7 +3329,8 @@ NAME(bfd_elf,size_dynamic_sections) (out
 					     true, false);
 		else
 		  indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-					     t->vn_bfd->filename, true, false);
+					     basename (t->vn_bfd->filename),
+					     true, false);
 		if (indx == (bfd_size_type) -1)
 		  return false;
 		t->vn_file = indx;
Index: ld/emultempl/elf32.em
===================================================================
RCS file: /work/cvs/gnu/binutils/ld/emultempl/elf32.em,v
retrieving revision 1.38
diff -u -p -r1.38 elf32.em
--- ld/emultempl/elf32.em	2001/04/24 23:03:29	1.38
+++ ld/emultempl/elf32.em	2001/05/01 16:21:54
@@ -32,6 +32,7 @@ Foundation, Inc., 59 Temple Place - Suit
 
 #include "bfd.h"
 #include "sysdep.h"
+#include "libiberty.h"
 
 #include <ctype.h>
 
@@ -130,7 +131,7 @@ static void
 gld${EMULATION_NAME}_vercheck (s)
      lang_input_statement_type *s;
 {
-  const char *soname, *f;
+  const char *soname;
   struct bfd_link_needed_list *l;
 
   if (global_vercheck_failed)
@@ -141,19 +142,13 @@ gld${EMULATION_NAME}_vercheck (s)
 
   soname = bfd_elf_get_dt_soname (s->the_bfd);
   if (soname == NULL)
-    soname = bfd_get_filename (s->the_bfd);
-
-  f = strrchr (soname, '/');
-  if (f != NULL)
-    ++f;
-  else
-    f = soname;
+    soname = basename (bfd_get_filename (s->the_bfd));
 
   for (l = global_vercheck_needed; l != NULL; l = l->next)
     {
       const char *suffix;
 
-      if (strcmp (f, l->name) == 0)
+      if (strcmp (soname, l->name) == 0)
 	{
 	  /* Probably can't happen, but it's an easy check.  */
 	  continue;
@@ -168,7 +163,7 @@ gld${EMULATION_NAME}_vercheck (s)
 
       suffix += sizeof ".so." - 1;
 
-      if (strncmp (f, l->name, suffix - l->name) == 0)
+      if (strncmp (soname, l->name, suffix - l->name) == 0)
 	{
 	  /* Here we know that S is a dynamic object FOO.SO.VER1, and
              the object we are considering needs a dynamic object
@@ -192,7 +187,6 @@ gld${EMULATION_NAME}_stat_needed (s)
   struct stat st;
   const char *suffix;
   const char *soname;
-  const char *f;
 
   if (global_found)
     return;
@@ -230,17 +224,12 @@ gld${EMULATION_NAME}_stat_needed (s)
 
   soname = bfd_elf_get_dt_soname (s->the_bfd);
   if (soname == NULL)
-    soname = s->filename;
+    soname = basename (s->filename);
 
-  f = strrchr (soname, '/');
-  if (f != NULL)
-    ++f;
-  else
-    f = soname;
-
-  if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
+  if (strncmp (soname, global_needed->name,
+	       suffix - global_needed->name) == 0)
     einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
-	   global_needed->name, global_needed->by, f);
+	   global_needed->name, global_needed->by, soname);
 }
 
 
@@ -340,11 +329,7 @@ cat >>e${EMULATION_NAME}.c <<EOF
     einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
 
   /* First strip off everything before the last '/'.  */
-  soname = strrchr (abfd->filename, '/');
-  if (soname)
-    soname++;
-  else
-    soname = abfd->filename;
+  soname = basename (abfd->filename);
 
   if (trace_file_tries)
     info_msg (_("found %s at %s\n"), soname, name);
@@ -964,20 +949,13 @@ gld${EMULATION_NAME}_open_dynamic_archiv
   if (bfd_check_format (entry->the_bfd, bfd_object)
       && (entry->the_bfd->flags & DYNAMIC) != 0)
     {
-      char *needed_name;
-
       ASSERT (entry->is_archive && entry->search_dirs_flag);
 
       /* Rather than duplicating the logic above.  Just use the
-	 filename we recorded earlier.
+	 filename we recorded earlier.  */
 
-	 First strip off everything before the last '/'.  */
-      filename = strrchr (entry->filename, '/');
-      filename++;
-
-      needed_name = (char *) xmalloc (strlen (filename) + 1);
-      strcpy (needed_name, filename);
-      bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
+      filename = xstrdup (basename (entry->filename));
+      bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
     }
 
   return true;
Index: ld/testsuite/ld-elfvers/vers19.ver
===================================================================
RCS file: /work/cvs/gnu/binutils/ld/testsuite/ld-elfvers/vers19.ver,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 vers19.ver
--- ld/testsuite/ld-elfvers/vers19.ver	1999/07/09 15:21:41	1.1.1.1
+++ ld/testsuite/ld-elfvers/vers19.ver	2001/05/01 16:21:54
@@ -1,3 +1,3 @@
 Version References:
-  required from *tmpdir/vers17.so:
+  required from vers17.so:
     0x0a7922b0 0x00 02 VERS_2.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-05-01  9:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-30 22:04 A patch for bug in rpath/rpath-link H . J . Lu
2001-05-01  9:30 ` H . J . Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).