glibc-2.27-ldso-rpath-prefix-option.diff 6.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
ld.so: Add --rpath-prefix option

Scratchbox2 uses the --rpath-prefix option to make the linker search for
required libraries under the target rootfs.

Packages that run their own binaries as a build step may use RPATH to
resolve dependencies located under the build tree - such RPATHs must not
be redirected under the target rootfs. It is a common assumption in
Scratchbox2 that builds happen under user's home directory. Therefore,
RPATHs under /home are excluded from this manipulation.

12
diff --git a/elf/dl-load.c b/elf/dl-load.c
13
index c51e4b37..57a357ea 100644
14 15
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
16
@@ -443,7 +443,8 @@ static size_t max_dirnamelen;
17
 
18 19
 static struct r_search_path_elem **
 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
20 21 22
-	      const char *what, const char *where, struct link_map *l)
+	      const char *what, const char *where, struct link_map *l, 
+	      const char *rpath_prefix)
23 24 25
 {
   char *cp;
   size_t nelems = 0;
26
@@ -483,9 +484,24 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
27
 	}
28
 
29 30 31 32
       /* See if this directory is already known.  */
-      for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
-	if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
-	  break;
33 34
+      if (__builtin_expect (rpath_prefix != NULL, 0)
+	  && (memcmp (cp, "/home/", 6) != 0))
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
+	{
+	  /* has rpath_prefix */
+	  size_t rpath_prefix_len = strlen (rpath_prefix);
+
+	  for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
+	    if (dirp->dirnamelen == (rpath_prefix_len+len) &&
+		(memcmp (cp, rpath_prefix, rpath_prefix_len) == 0) &&
+		(memcmp (cp+rpath_prefix_len, dirp->dirname, len) == 0))
+	      break;
+	}
+      else
+	{
+	  for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
+	    if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
+	      break;
+	}
51
 
52 53
       if (dirp != NULL)
 	{
54
@@ -503,22 +519,44 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
55 56 57 58 59 60
 	  size_t cnt;
 	  enum r_dir_status init_val;
 	  size_t where_len = where ? strlen (where) + 1 : 0;
+	  size_t rpath_prefix_len = 0;
+
+	  if (__builtin_expect (rpath_prefix != NULL, 0)
61
+	      && (memcmp (cp, "/home/", 6) != 0)
62
+	      && !__libc_enable_secure)
63 64 65 66
+	    {
+		rpath_prefix_len = strlen (rpath_prefix);
+		if (*cp != '/') rpath_prefix_len++; /* need to add a '/' */
+	    }
67
 
68 69 70 71 72 73 74 75
 	  /* It's a new directory.  Create an entry and add it.  */
 	  dirp = (struct r_search_path_elem *)
 	    malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
-		    + where_len + len + 1);
+		    + where_len + rpath_prefix_len + len + 1);
 	  if (dirp == NULL)
 	    _dl_signal_error (ENOMEM, NULL, NULL,
 			      N_("cannot create cache for search path"));
76
 
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 	  dirp->dirname = ((char *) dirp + sizeof (*dirp)
 			   + ncapstr * sizeof (enum r_dir_status));
-	  *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
-	  dirp->dirnamelen = len;
+	  if (rpath_prefix_len == 0)
+	    {
+		  *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
+	    }
+	  else
+	    {
+		char *prefixend;
+
+		prefixend = (char *) __mempcpy ((char *) dirp->dirname,
+				rpath_prefix, rpath_prefix_len);
+		if (*cp != '/')
+		  prefixend[-1] = '/'; /* replace \0 */
+		*((char *) __mempcpy (prefixend, cp, len)) = '\0';
+	    }
+	  dirp->dirnamelen = len + rpath_prefix_len;
96
 
97 98 99 100
-	  if (len > max_dirnamelen)
-	    max_dirnamelen = len;
+	  if ((len + rpath_prefix_len) > max_dirnamelen)
+	    max_dirnamelen = len + rpath_prefix_len;
101
 
102 103
 	  /* We have to make sure all the relative directories are
 	     never ignored.  The current directory might change and
104
@@ -529,7 +567,8 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
105
 
106
 	  dirp->what = what;
107
 	  if (__glibc_likely (where != NULL))
108 109 110 111 112 113
-	    dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
+	    dirp->where = memcpy ((char *) dirp + sizeof (*dirp)
+				  + rpath_prefix_len + len + 1
 				  + (ncapstr * sizeof (enum r_dir_status)),
 				  where, where_len);
 	  else
114
@@ -628,7 +667,7 @@ decompose_rpath (struct r_search_path_struct *sps,
115 116
       _dl_signal_error (ENOMEM, NULL, NULL, errstring);
     }
117 118 119 120
 
-  fillin_rpath (copy, result, ":", what, where, l);
+  fillin_rpath (copy, result, ":", what, where, l, GLRO(dl_rpath_prefix));
 
121 122
   /* Free the copied RPATH string.  `fillin_rpath' make own copies if
      necessary.  */
123
@@ -813,7 +852,7 @@ _dl_init_paths (const char *llp)
124 125
 	}
 
126
       (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
127 128 129
-			   "LD_LIBRARY_PATH", NULL, l);
+			   "LD_LIBRARY_PATH", NULL, l, NULL/*no prefix*/);
 
130 131
       if (env_path_list.dirs[0] == NULL)
 	{
132
diff --git a/elf/dl-support.c b/elf/dl-support.c
133
index b5f10d5a..cdf189b7 100644
134 135 136
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -58,6 +58,9 @@ const char *_dl_profile_output;
137 138
    ignored.  */
 const char *_dl_inhibit_rpath;
139
 
140 141 142 143 144
+/* prefix to be added to all RUNPATHs and RPATHs */
+const char *_dl_rpath_prefix = NULL;
+
 /* The map for the object we will profile.  */
 struct link_map *_dl_profile_map;
145 146
 
diff --git a/elf/rtld.c b/elf/rtld.c
147
index 385b5f88..9bc6d162 100644
148 149
--- a/elf/rtld.c
+++ b/elf/rtld.c
150
@@ -967,6 +967,15 @@ dl_main (const ElfW(Phdr) *phdr,
151 152 153 154 155 156 157
 	  {
 	    GLRO(dl_inhibit_rpath) = _dl_argv[2];
 
+	    _dl_skip_args += 2;
+	    _dl_argc -= 2;
+	    _dl_argv += 2;
+	  }
158
+	else if (! strcmp (_dl_argv[1], "--rpath-prefix")
159 160
+		 && _dl_argc > 2)
+	  {
161
+	    GLRO(dl_rpath_prefix) = _dl_argv[2];
162
+
163 164 165
 	    _dl_skip_args += 2;
 	    _dl_argc -= 2;
 	    _dl_argv += 2;
166
@@ -1013,6 +1022,8 @@ of this helper program; chances are you did not intend to run this program.\n\
167
   --inhibit-cache       Do not use " LD_SO_CACHE "\n\
168 169 170
   --library-path PATH   use given PATH instead of content of the environment\n\
 			variable LD_LIBRARY_PATH\n\
+  --rpath-prefix PREFIX add PREFIX to every RUNPATH and RPATH component\n\
171
+			unless it is a path under the /home tree\n\
172 173
   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
 			in LIST\n\
174
   --argv0 STRING        use STRING as argv[0]\n\
175
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
176
index 95dc8751..83b6ae2f 100644
177 178
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
179
@@ -617,6 +617,12 @@ struct rtld_global_ro
180 181 182
   /* List of auditing interfaces.  */
   struct audit_ifaces *_dl_audit;
   unsigned int _dl_naudit;
183 184 185 186 187 188 189 190
+#endif
+
+  /* prefix for RPATH + RUNPATH components.  */
+  EXTERN const char *_dl_rpath_prefix;
+
+#ifdef SHARED
 };
 # define __rtld_global_attribute__
191
 # if IS_IN (rtld)