-
Notifications
You must be signed in to change notification settings - Fork 4
/
0010-scratchbox2-Add-new-option-rpath-prefix-to-ld.so.patch
202 lines (184 loc) · 7.26 KB
/
0010-scratchbox2-Add-new-option-rpath-prefix-to-ld.so.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Bidar?= <[email protected]>
Date: Tue, 31 Aug 2021 01:58:49 +0300
Subject: [PATCH] (scratchbox2) Add new option "--rpath-prefix" to ld.so
This patch adds a new option "--rpath-prefix" to ld.so.
This makes it possible to add a prefix to all RPATH and
RUNPATH options from binaries and libraries; that way
Scratchbox 2 can run also host-compatible binaries that
contain such options (without this, the RPATHs would
refer to the libraries on the host system and not to
the correct libraries under 'target_root').
Note that this patch causes no harm in the default case, i.e.
when the dynamic linker is started "automatically" by the kernel.
---
elf/dl-load.c | 63 ++++++++++++++++++++++++++++++--------
elf/dl-support.c | 3 ++
elf/rtld.c | 11 +++++++
sysdeps/generic/ldsodefs.h | 6 ++++
4 files changed, 71 insertions(+), 12 deletions(-)
diff --git a/elf/dl-load.c b/elf/dl-load.c
index c092ed3e674650f3b93dfca992bbdc775f6d1b8e..3b00898028f8b65af56b1034a50c01f9012e1cde 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -443,7 +443,8 @@ static size_t max_dirnamelen;
static struct r_search_path_elem **
fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
- const char *what, const char *where, struct link_map *l)
+ const char *what, const char *where, struct link_map *l,
+ const char *rpath_prefix)
{
char *cp;
size_t nelems = 0;
@@ -483,9 +484,24 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
}
/* 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;
+ if (__builtin_expect (rpath_prefix != NULL, 0)
+ && (memcmp (cp, "/home/", 6) != 0))
+ {
+ /* 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;
+ }
if (dirp != NULL)
{
@@ -503,22 +519,44 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
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)
+ && (memcmp (cp, "/home/", 6) != 0)
+ && !__libc_enable_secure)
+ {
+ rpath_prefix_len = strlen (rpath_prefix);
+ if (*cp != '/') rpath_prefix_len++; /* need to add a '/' */
+ }
/* 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"));
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;
- if (len > max_dirnamelen)
- max_dirnamelen = len;
+ if ((len + rpath_prefix_len) > max_dirnamelen)
+ max_dirnamelen = len + rpath_prefix_len;
/* We have to make sure all the relative directories are
never ignored. The current directory might change and
@@ -529,7 +567,8 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
dirp->what = what;
if (__glibc_likely (where != NULL))
- 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
@@ -628,7 +667,7 @@ decompose_rpath (struct r_search_path_struct *sps,
_dl_signal_error (ENOMEM, NULL, NULL, errstring);
}
- fillin_rpath (copy, result, ":", what, where, l);
+ fillin_rpath (copy, result, ":", what, where, l, GLRO(dl_rpath_prefix));
/* Free the copied RPATH string. `fillin_rpath' make own copies if
necessary. */
@@ -813,7 +852,7 @@ _dl_init_paths (const char *llp)
}
(void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
- "LD_LIBRARY_PATH", NULL, l);
+ "LD_LIBRARY_PATH", NULL, l, NULL/*no prefix*/);
if (env_path_list.dirs[0] == NULL)
{
diff --git a/elf/dl-support.c b/elf/dl-support.c
index a9cf3366633f2aa8ea4c16b8eb2ecd2015174e57..02894dbf2aaea4bdb31223968f1248c37fe54de2 100644
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -61,6 +61,9 @@ const char *_dl_inhibit_rpath;
/* flag: don't search default directories if set. */
int _dl_no_default_dirs = 0;
+/* 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;
diff --git a/elf/rtld.c b/elf/rtld.c
index c2022d5bf7cafba0fe3fa3cfaf7cf6470ab23320..415188c233792eea6484ce709f4bfc128775323b 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -1188,6 +1188,15 @@ dl_main (const ElfW(Phdr) *phdr,
{
GLRO(dl_inhibit_rpath) = _dl_argv[2];
+ _dl_skip_args += 2;
+ _dl_argc -= 2;
+ _dl_argv += 2;
+ }
+ else if (! strcmp (_dl_argv[1], "--rpath-prefix")
+ && _dl_argc > 2)
+ {
+ GLRO(dl_rpath_prefix) = _dl_argv[2];
+
_dl_skip_args += 2;
_dl_argc -= 2;
_dl_argv += 2;
@@ -1242,6 +1251,8 @@ of this helper program; chances are you did not intend to run this program.\n\
--inhibit-cache Do not use " LD_SO_CACHE "\n\
--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\
+ unless it is a path under the /home tree\n\
--inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
in LIST\n\
--argv0 STRING use STRING as argv[0]\n\
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index ddf23e32ad1952aab5b1ca955d98f90f42e346ec..f946848d25930056a8135d96381e80ca0eb02bf7 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -614,6 +614,12 @@ struct rtld_global_ro
/* List of auditing interfaces. */
struct audit_ifaces *_dl_audit;
unsigned int _dl_naudit;
+#endif
+
+ /* prefix for RPATH + RUNPATH components. */
+ EXTERN const char *_dl_rpath_prefix;
+
+#ifdef SHARED
};
# define __rtld_global_attribute__
# if IS_IN (rtld)