]>
Commit | Line | Data |
---|---|---|
0a54e401 | 1 | /* Map in a shared object's segments from the file. |
db2ebcef | 2 | Copyright (C) 1995,96,97,98,99,2000,2001,2002 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
d66e34cd | 9 | |
afd4eb37 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
d66e34cd | 19 | |
14e9dd67 | 20 | #include <elf.h> |
0a54e401 UD |
21 | #include <errno.h> |
22 | #include <fcntl.h> | |
8e17ea58 | 23 | #include <libintl.h> |
379d4ec4 | 24 | #include <stdbool.h> |
0a54e401 | 25 | #include <stdlib.h> |
d66e34cd | 26 | #include <string.h> |
d66e34cd | 27 | #include <unistd.h> |
a42195db | 28 | #include <ldsodefs.h> |
0a54e401 | 29 | #include <sys/mman.h> |
f8f7e090 | 30 | #include <sys/param.h> |
0a54e401 UD |
31 | #include <sys/stat.h> |
32 | #include <sys/types.h> | |
d66e34cd | 33 | #include "dynamic-link.h" |
a986484f UD |
34 | #include <abi-tag.h> |
35 | #include <dl-osinfo.h> | |
d66e34cd | 36 | |
dc5efe83 | 37 | #include <dl-dst.h> |
d66e34cd | 38 | |
9b8a44cd RM |
39 | /* On some systems, no flag bits are given to specify file mapping. */ |
40 | #ifndef MAP_FILE | |
fcf70d41 | 41 | # define MAP_FILE 0 |
9b8a44cd RM |
42 | #endif |
43 | ||
44 | /* The right way to map in the shared library files is MAP_COPY, which | |
45 | makes a virtual copy of the data at the time of the mmap call; this | |
46 | guarantees the mapped pages will be consistent even if the file is | |
47 | overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we | |
48 | get is MAP_PRIVATE, which copies each page when it is modified; this | |
49 | means if the file is overwritten, we may at some point get some pages | |
50 | from the new version after starting with pages from the old version. */ | |
51 | #ifndef MAP_COPY | |
fcf70d41 | 52 | # define MAP_COPY MAP_PRIVATE |
9b8a44cd RM |
53 | #endif |
54 | ||
f21acc89 UD |
55 | /* Some systems link their relocatable objects for another base address |
56 | than 0. We want to know the base address for these such that we can | |
57 | subtract this address from the segment addresses during mapping. | |
58 | This results in a more efficient address space usage. Defaults to | |
59 | zero for almost all systems. */ | |
60 | #ifndef MAP_BASE_ADDR | |
fcf70d41 | 61 | # define MAP_BASE_ADDR(l) 0 |
f21acc89 UD |
62 | #endif |
63 | ||
9b8a44cd | 64 | |
d66e34cd RM |
65 | #include <endian.h> |
66 | #if BYTE_ORDER == BIG_ENDIAN | |
fcf70d41 | 67 | # define byteorder ELFDATA2MSB |
d66e34cd | 68 | #elif BYTE_ORDER == LITTLE_ENDIAN |
fcf70d41 | 69 | # define byteorder ELFDATA2LSB |
d66e34cd | 70 | #else |
fcf70d41 UD |
71 | # error "Unknown BYTE_ORDER " BYTE_ORDER |
72 | # define byteorder ELFDATANONE | |
d66e34cd RM |
73 | #endif |
74 | ||
14e9dd67 | 75 | #define STRING(x) __STRING (x) |
d66e34cd | 76 | |
2064087b RM |
77 | #ifdef MAP_ANON |
78 | /* The fd is not examined when using MAP_ANON. */ | |
fcf70d41 | 79 | # define ANONFD -1 |
2064087b | 80 | #else |
d66e34cd | 81 | int _dl_zerofd = -1; |
fcf70d41 | 82 | # define ANONFD _dl_zerofd |
2064087b RM |
83 | #endif |
84 | ||
4cca6b86 UD |
85 | /* Handle situations where we have a preferred location in memory for |
86 | the shared objects. */ | |
87 | #ifdef ELF_PREFERRED_ADDRESS_DATA | |
88 | ELF_PREFERRED_ADDRESS_DATA; | |
89 | #endif | |
90 | #ifndef ELF_PREFERRED_ADDRESS | |
fcf70d41 | 91 | # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref) |
4cca6b86 UD |
92 | #endif |
93 | #ifndef ELF_FIXED_ADDRESS | |
fcf70d41 | 94 | # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0) |
4cca6b86 UD |
95 | #endif |
96 | ||
a35e137a UD |
97 | /* Type for the buffer we put the ELF header and hopefully the program |
98 | header. This buffer does not really have to be too large. In most | |
99 | cases the program header follows the ELF header directly. If this | |
100 | is not the case all bets are off and we can make the header arbitrarily | |
101 | large and still won't get it read. This means the only question is | |
102 | how large are the ELF and program header combined. The ELF header | |
103 | in 64-bit files is 56 bytes long. Each program header entry is again | |
104 | 56 bytes long. I.e., even with a file which has 17 program header | |
105 | entries we only have to read 1kB. And 17 program header entries is | |
106 | plenty, normal files have < 10. If this heuristic should really fail | |
107 | for some file the code in `_dl_map_object_from_fd' knows how to | |
108 | recover. */ | |
109 | struct filebuf | |
110 | { | |
111 | ssize_t len; | |
112 | char buf[1024]; | |
113 | }; | |
114 | ||
7ef90c15 | 115 | /* This is the decomposed LD_LIBRARY_PATH search path. */ |
f55727ca | 116 | static struct r_search_path_struct env_path_list; |
40a55d20 | 117 | |
12264bd7 UD |
118 | /* List of the hardware capabilities we might end up using. */ |
119 | static const struct r_strlenpair *capstr; | |
120 | static size_t ncapstr; | |
121 | static size_t max_capstrlen; | |
122 | ||
40a55d20 | 123 | |
ab7eb292 UD |
124 | /* Get the generated information about the trusted directories. */ |
125 | #include "trusted-dirs.h" | |
126 | ||
127 | static const char system_dirs[] = SYSTEM_DIRS; | |
128 | static const size_t system_dirs_len[] = | |
129 | { | |
130 | SYSTEM_DIRS_LEN | |
131 | }; | |
4a6d1198 UD |
132 | #define nsystem_dirs_len \ |
133 | (sizeof (system_dirs_len) / sizeof (system_dirs_len[0])) | |
ab7eb292 | 134 | |
32c85e43 | 135 | |
706074a5 UD |
136 | /* Local version of `strdup' function. */ |
137 | static inline char * | |
138 | local_strdup (const char *s) | |
139 | { | |
140 | size_t len = strlen (s) + 1; | |
141 | void *new = malloc (len); | |
142 | ||
143 | if (new == NULL) | |
144 | return NULL; | |
145 | ||
146 | return (char *) memcpy (new, s, len); | |
147 | } | |
148 | ||
dc5efe83 | 149 | |
6d5d3ae3 | 150 | static size_t |
379d4ec4 | 151 | is_dst (const char *start, const char *name, const char *str, |
d43c8c5f | 152 | int is_path, int secure) |
6d5d3ae3 UD |
153 | { |
154 | size_t len; | |
379d4ec4 | 155 | bool is_curly = false; |
6d5d3ae3 | 156 | |
379d4ec4 UD |
157 | if (name[0] == '{') |
158 | { | |
159 | is_curly = true; | |
160 | ++name; | |
161 | } | |
162 | ||
163 | len = 0; | |
164 | while (name[len] == str[len] && name[len] != '\0') | |
165 | ++len; | |
166 | ||
167 | if (is_curly) | |
168 | { | |
169 | if (name[len] != '}') | |
170 | return 0; | |
171 | ||
172 | /* Point again at the beginning of the name. */ | |
173 | --name; | |
174 | /* Skip over closing curly brace and adjust for the --name. */ | |
175 | len += 2; | |
176 | } | |
177 | else if (name[len] != '\0' && name[len] != '/' | |
178 | && (!is_path || name[len] != ':')) | |
6d5d3ae3 UD |
179 | return 0; |
180 | ||
181 | if (__builtin_expect (secure, 0) | |
379d4ec4 | 182 | && ((name[len] != '\0' && (!is_path || name[len] != ':')) |
6d5d3ae3 UD |
183 | || (name != start + 1 && (!is_path || name[-2] != ':')))) |
184 | return 0; | |
185 | ||
186 | return len; | |
187 | } | |
188 | ||
189 | ||
dc5efe83 UD |
190 | size_t |
191 | _dl_dst_count (const char *name, int is_path) | |
f787edde | 192 | { |
2864e767 | 193 | const char *const start = name; |
f787edde | 194 | size_t cnt = 0; |
f787edde | 195 | |
dc5efe83 | 196 | do |
f787edde | 197 | { |
379d4ec4 | 198 | size_t len; |
f787edde | 199 | |
32463b1a | 200 | /* $ORIGIN is not expanded for SUID/GUID programs (except if it |
379d4ec4 UD |
201 | is $ORIGIN alone) and it must always appear first in path. */ |
202 | ++name; | |
203 | if ((len = is_dst (start, name, "ORIGIN", is_path, | |
6d5d3ae3 | 204 | __libc_enable_secure)) != 0 |
379d4ec4 | 205 | || ((len = is_dst (start, name, "PLATFORM", is_path, 0)) |
6d5d3ae3 | 206 | != 0)) |
f787edde UD |
207 | ++cnt; |
208 | ||
dc5efe83 | 209 | name = strchr (name + len, '$'); |
f787edde | 210 | } |
dc5efe83 | 211 | while (name != NULL); |
f787edde | 212 | |
dc5efe83 UD |
213 | return cnt; |
214 | } | |
7969407a | 215 | INTDEF (_dl_dst_count) |
f787edde | 216 | |
f787edde | 217 | |
dc5efe83 UD |
218 | char * |
219 | _dl_dst_substitute (struct link_map *l, const char *name, char *result, | |
220 | int is_path) | |
221 | { | |
2864e767 | 222 | const char *const start = name; |
dc5efe83 | 223 | char *last_elem, *wp; |
f787edde UD |
224 | |
225 | /* Now fill the result path. While copying over the string we keep | |
226 | track of the start of the last path element. When we come accross | |
227 | a DST we copy over the value or (if the value is not available) | |
228 | leave the entire path element out. */ | |
229 | last_elem = wp = result; | |
dc5efe83 | 230 | |
f787edde UD |
231 | do |
232 | { | |
39b3385d | 233 | if (__builtin_expect (*name == '$', 0)) |
f787edde | 234 | { |
2541eda0 | 235 | const char *repl = NULL; |
379d4ec4 | 236 | size_t len; |
2541eda0 | 237 | |
379d4ec4 UD |
238 | ++name; |
239 | if ((len = is_dst (start, name, "ORIGIN", is_path, | |
6d5d3ae3 UD |
240 | __libc_enable_secure)) != 0) |
241 | repl = l->l_origin; | |
379d4ec4 | 242 | else if ((len = is_dst (start, name, "PLATFORM", is_path, |
6d5d3ae3 | 243 | 0)) != 0) |
d6b5d570 | 244 | repl = GL(dl_platform); |
2541eda0 | 245 | |
2541eda0 UD |
246 | if (repl != NULL && repl != (const char *) -1) |
247 | { | |
248 | wp = __stpcpy (wp, repl); | |
249 | name += len; | |
250 | } | |
251 | else if (len > 1) | |
f787edde | 252 | { |
2541eda0 UD |
253 | /* We cannot use this path element, the value of the |
254 | replacement is unknown. */ | |
255 | wp = last_elem; | |
256 | name += len; | |
257 | while (*name != '\0' && (!is_path || *name != ':')) | |
258 | ++name; | |
f787edde UD |
259 | } |
260 | else | |
27aa0631 | 261 | /* No DST we recognize. */ |
379d4ec4 | 262 | *wp++ = '$'; |
f787edde | 263 | } |
2541eda0 | 264 | else |
f787edde | 265 | { |
dc5efe83 | 266 | *wp++ = *name++; |
2541eda0 UD |
267 | if (is_path && *name == ':') |
268 | last_elem = wp; | |
f787edde | 269 | } |
f787edde | 270 | } |
dc5efe83 | 271 | while (*name != '\0'); |
f787edde UD |
272 | |
273 | *wp = '\0'; | |
274 | ||
275 | return result; | |
276 | } | |
7969407a | 277 | INTDEF (_dl_dst_substitute) |
f787edde | 278 | |
dc5efe83 UD |
279 | |
280 | /* Return copy of argument with all recognized dynamic string tokens | |
281 | ($ORIGIN and $PLATFORM for now) replaced. On some platforms it | |
282 | might not be possible to determine the path from which the object | |
283 | belonging to the map is loaded. In this case the path element | |
284 | containing $ORIGIN is left out. */ | |
285 | static char * | |
286 | expand_dynamic_string_token (struct link_map *l, const char *s) | |
287 | { | |
288 | /* We make two runs over the string. First we determine how large the | |
289 | resulting string is and then we copy it over. Since this is now | |
290 | frequently executed operation we are looking here not for performance | |
291 | but rather for code size. */ | |
292 | size_t cnt; | |
293 | size_t total; | |
294 | char *result; | |
295 | ||
2864e767 | 296 | /* Determine the number of DST elements. */ |
dc5efe83 UD |
297 | cnt = DL_DST_COUNT (s, 1); |
298 | ||
299 | /* If we do not have to replace anything simply copy the string. */ | |
55c91021 | 300 | if (__builtin_expect (cnt, 0) == 0) |
dc5efe83 UD |
301 | return local_strdup (s); |
302 | ||
303 | /* Determine the length of the substituted string. */ | |
304 | total = DL_DST_REQUIRED (l, s, strlen (s), cnt); | |
305 | ||
306 | /* Allocate the necessary memory. */ | |
307 | result = (char *) malloc (total + 1); | |
308 | if (result == NULL) | |
309 | return NULL; | |
310 | ||
cff26a3e | 311 | return INTUSE(_dl_dst_substitute) (l, s, result, 1); |
dc5efe83 UD |
312 | } |
313 | ||
314 | ||
0413b54c UD |
315 | /* Add `name' to the list of names for a particular shared object. |
316 | `name' is expected to have been allocated with malloc and will | |
317 | be freed if the shared object already has this name. | |
318 | Returns false if the object already had this name. */ | |
76156ea1 | 319 | static void |
12264bd7 | 320 | internal_function |
76156ea1 | 321 | add_name_to_object (struct link_map *l, const char *name) |
0a54e401 | 322 | { |
0413b54c UD |
323 | struct libname_list *lnp, *lastp; |
324 | struct libname_list *newname; | |
76156ea1 | 325 | size_t name_len; |
0413b54c UD |
326 | |
327 | lastp = NULL; | |
328 | for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next) | |
329 | if (strcmp (name, lnp->name) == 0) | |
76156ea1 | 330 | return; |
0413b54c | 331 | |
76156ea1 | 332 | name_len = strlen (name) + 1; |
839be784 | 333 | newname = (struct libname_list *) malloc (sizeof *newname + name_len); |
0413b54c | 334 | if (newname == NULL) |
da832465 UD |
335 | { |
336 | /* No more memory. */ | |
cff26a3e | 337 | INTUSE(_dl_signal_error) (ENOMEM, name, NULL, |
7969407a | 338 | N_("cannot allocate name record")); |
76156ea1 | 339 | return; |
da832465 | 340 | } |
0413b54c UD |
341 | /* The object should have a libname set from _dl_new_object. */ |
342 | assert (lastp != NULL); | |
343 | ||
76156ea1 | 344 | newname->name = memcpy (newname + 1, name, name_len); |
0413b54c | 345 | newname->next = NULL; |
752a2a50 | 346 | newname->dont_free = 0; |
0413b54c | 347 | lastp->next = newname; |
0413b54c UD |
348 | } |
349 | ||
12264bd7 | 350 | /* Standard search directories. */ |
f55727ca | 351 | static struct r_search_path_struct rtld_search_dirs; |
0a54e401 UD |
352 | |
353 | static size_t max_dirnamelen; | |
354 | ||
355 | static inline struct r_search_path_elem ** | |
356 | fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, | |
ab7eb292 | 357 | int check_trusted, const char *what, const char *where) |
0a54e401 UD |
358 | { |
359 | char *cp; | |
360 | size_t nelems = 0; | |
361 | ||
362 | while ((cp = __strsep (&rpath, sep)) != NULL) | |
363 | { | |
364 | struct r_search_path_elem *dirp; | |
365 | size_t len = strlen (cp); | |
12264bd7 | 366 | |
af3878df UD |
367 | /* `strsep' can pass an empty string. This has to be |
368 | interpreted as `use the current directory'. */ | |
12264bd7 | 369 | if (len == 0) |
af3878df | 370 | { |
ab7eb292 UD |
371 | static const char curwd[] = "./"; |
372 | cp = (char *) curwd; | |
af3878df | 373 | } |
12264bd7 | 374 | |
143e2b96 | 375 | /* Remove trailing slashes (except for "/"). */ |
0a54e401 UD |
376 | while (len > 1 && cp[len - 1] == '/') |
377 | --len; | |
378 | ||
ab7eb292 UD |
379 | /* Now add one if there is none so far. */ |
380 | if (len > 0 && cp[len - 1] != '/') | |
381 | cp[len++] = '/'; | |
382 | ||
07ba7349 UD |
383 | /* Make sure we don't use untrusted directories if we run SUID. */ |
384 | if (__builtin_expect (check_trusted, 0)) | |
385 | { | |
386 | const char *trun = system_dirs; | |
387 | size_t idx; | |
388 | int unsecure = 1; | |
389 | ||
390 | /* All trusted directories must be complete names. */ | |
391 | if (cp[0] == '/') | |
392 | { | |
393 | for (idx = 0; idx < nsystem_dirs_len; ++idx) | |
394 | { | |
395 | if (len == system_dirs_len[idx] | |
396 | && memcmp (trun, cp, len) == 0) | |
397 | { | |
398 | /* Found it. */ | |
399 | unsecure = 0; | |
400 | break; | |
401 | } | |
402 | ||
403 | trun += system_dirs_len[idx] + 1; | |
404 | } | |
405 | } | |
406 | ||
407 | if (unsecure) | |
408 | /* Simply drop this directory. */ | |
409 | continue; | |
410 | } | |
411 | ||
0a54e401 | 412 | /* See if this directory is already known. */ |
d6b5d570 | 413 | for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next) |
12264bd7 | 414 | if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0) |
0a54e401 UD |
415 | break; |
416 | ||
417 | if (dirp != NULL) | |
418 | { | |
12264bd7 | 419 | /* It is available, see whether it's on our own list. */ |
0a54e401 UD |
420 | size_t cnt; |
421 | for (cnt = 0; cnt < nelems; ++cnt) | |
422 | if (result[cnt] == dirp) | |
423 | break; | |
424 | ||
425 | if (cnt == nelems) | |
426 | result[nelems++] = dirp; | |
427 | } | |
428 | else | |
429 | { | |
12264bd7 | 430 | size_t cnt; |
839be784 | 431 | enum r_dir_status init_val; |
4a6d1198 | 432 | size_t where_len = where ? strlen (where) + 1 : 0; |
12264bd7 | 433 | |
0a54e401 | 434 | /* It's a new directory. Create an entry and add it. */ |
12264bd7 | 435 | dirp = (struct r_search_path_elem *) |
32ee8d95 | 436 | malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status) |
f55727ca | 437 | + where_len + len + 1); |
0a54e401 | 438 | if (dirp == NULL) |
cff26a3e AJ |
439 | INTUSE(_dl_signal_error) (ENOMEM, NULL, NULL, |
440 | N_("cannot create cache for search path")); | |
0a54e401 | 441 | |
f55727ca UD |
442 | dirp->dirname = ((char *) dirp + sizeof (*dirp) |
443 | + ncapstr * sizeof (enum r_dir_status)); | |
104d0bd3 | 444 | *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0'; |
0a54e401 | 445 | dirp->dirnamelen = len; |
12264bd7 UD |
446 | |
447 | if (len > max_dirnamelen) | |
448 | max_dirnamelen = len; | |
449 | ||
07ba7349 UD |
450 | /* We have to make sure all the relative directories are |
451 | never ignored. The current directory might change and | |
452 | all our saved information would be void. */ | |
453 | init_val = cp[0] != '/' ? existing : unknown; | |
839be784 UD |
454 | for (cnt = 0; cnt < ncapstr; ++cnt) |
455 | dirp->status[cnt] = init_val; | |
0a54e401 | 456 | |
b5efde2f | 457 | dirp->what = what; |
4a6d1198 | 458 | if (__builtin_expect (where != NULL, 1)) |
f55727ca | 459 | dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1 |
d6b5d570 | 460 | + (ncapstr * sizeof (enum r_dir_status)), |
4a6d1198 UD |
461 | where, where_len); |
462 | else | |
463 | dirp->where = NULL; | |
b5efde2f | 464 | |
d6b5d570 UD |
465 | dirp->next = GL(dl_all_dirs); |
466 | GL(dl_all_dirs) = dirp; | |
0a54e401 UD |
467 | |
468 | /* Put it in the result array. */ | |
469 | result[nelems++] = dirp; | |
470 | } | |
471 | } | |
472 | ||
473 | /* Terminate the array. */ | |
474 | result[nelems] = NULL; | |
475 | ||
476 | return result; | |
477 | } | |
478 | ||
479 | ||
f55727ca | 480 | static void |
12264bd7 | 481 | internal_function |
f55727ca UD |
482 | decompose_rpath (struct r_search_path_struct *sps, |
483 | const char *rpath, struct link_map *l, const char *what) | |
0a54e401 UD |
484 | { |
485 | /* Make a copy we can work with. */ | |
f787edde | 486 | const char *where = l->l_name; |
310930c1 | 487 | char *copy; |
0a54e401 UD |
488 | char *cp; |
489 | struct r_search_path_elem **result; | |
310930c1 | 490 | size_t nelems; |
39b3385d UD |
491 | /* Initialize to please the compiler. */ |
492 | const char *errstring = NULL; | |
310930c1 | 493 | |
fcf70d41 UD |
494 | /* First see whether we must forget the RUNPATH and RPATH from this |
495 | object. */ | |
d6b5d570 UD |
496 | if (__builtin_expect (GL(dl_inhibit_rpath) != NULL, 0) |
497 | && !__libc_enable_secure) | |
310930c1 | 498 | { |
9710f75d UD |
499 | const char *inhp = GL(dl_inhibit_rpath); |
500 | ||
501 | do | |
310930c1 | 502 | { |
9710f75d UD |
503 | const char *wp = where; |
504 | ||
505 | while (*inhp == *wp && *wp != '\0') | |
506 | { | |
507 | ++inhp; | |
508 | ++wp; | |
509 | } | |
510 | ||
511 | if (*wp == '\0' && (*inhp == '\0' || *inhp == ':')) | |
310930c1 | 512 | { |
fcf70d41 UD |
513 | /* This object is on the list of objects for which the |
514 | RUNPATH and RPATH must not be used. */ | |
310930c1 | 515 | result = (struct r_search_path_elem **) |
7ef90c15 | 516 | malloc (sizeof (*result)); |
310930c1 | 517 | if (result == NULL) |
39b3385d UD |
518 | { |
519 | signal_error_cache: | |
520 | errstring = N_("cannot create cache for search path"); | |
521 | signal_error: | |
cff26a3e | 522 | INTUSE(_dl_signal_error) (ENOMEM, NULL, NULL, errstring); |
39b3385d UD |
523 | } |
524 | ||
310930c1 UD |
525 | result[0] = NULL; |
526 | ||
f55727ca UD |
527 | sps->dirs = result; |
528 | sps->malloced = 1; | |
529 | ||
530 | return; | |
310930c1 | 531 | } |
9710f75d UD |
532 | |
533 | while (*inhp != '\0') | |
534 | if (*inhp++ == ':') | |
535 | break; | |
310930c1 | 536 | } |
9710f75d | 537 | while (*inhp != '\0'); |
310930c1 | 538 | } |
0a54e401 | 539 | |
f787edde UD |
540 | /* Make a writable copy. At the same time expand possible dynamic |
541 | string tokens. */ | |
542 | copy = expand_dynamic_string_token (l, rpath); | |
543 | if (copy == NULL) | |
39b3385d UD |
544 | { |
545 | errstring = N_("cannot create RUNPATH/RPATH copy"); | |
546 | goto signal_error; | |
547 | } | |
f787edde | 548 | |
310930c1 | 549 | /* Count the number of necessary elements in the result array. */ |
310930c1 | 550 | nelems = 0; |
0a54e401 UD |
551 | for (cp = copy; *cp != '\0'; ++cp) |
552 | if (*cp == ':') | |
553 | ++nelems; | |
554 | ||
7ef90c15 UD |
555 | /* Allocate room for the result. NELEMS + 1 is an upper limit for the |
556 | number of necessary entries. */ | |
557 | result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1) | |
0a54e401 UD |
558 | * sizeof (*result)); |
559 | if (result == NULL) | |
39b3385d | 560 | goto signal_error_cache; |
0a54e401 | 561 | |
f55727ca UD |
562 | fillin_rpath (copy, result, ":", 0, what, where); |
563 | ||
564 | /* Free the copied RPATH string. `fillin_rpath' make own copies if | |
565 | necessary. */ | |
566 | free (copy); | |
567 | ||
568 | sps->dirs = result; | |
569 | /* The caller will change this value if we haven't used a real malloc. */ | |
570 | sps->malloced = 1; | |
0a54e401 UD |
571 | } |
572 | ||
573 | ||
574 | void | |
d0fc4041 | 575 | internal_function |
880f421f | 576 | _dl_init_paths (const char *llp) |
0a54e401 | 577 | { |
ab7eb292 UD |
578 | size_t idx; |
579 | const char *strp; | |
12264bd7 UD |
580 | struct r_search_path_elem *pelem, **aelem; |
581 | size_t round_size; | |
b5567b2a | 582 | #ifdef SHARED |
c94a8080 UD |
583 | struct link_map *l; |
584 | #endif | |
39b3385d UD |
585 | /* Initialize to please the compiler. */ |
586 | const char *errstring = NULL; | |
0a54e401 | 587 | |
7ef90c15 UD |
588 | /* Fill in the information about the application's RPATH and the |
589 | directories addressed by the LD_LIBRARY_PATH environment variable. */ | |
0a54e401 | 590 | |
4317f9e1 | 591 | /* Get the capabilities. */ |
d6b5d570 | 592 | capstr = _dl_important_hwcaps (GL(dl_platform), GL(dl_platformlen), |
12264bd7 UD |
593 | &ncapstr, &max_capstrlen); |
594 | ||
595 | /* First set up the rest of the default search directory entries. */ | |
f55727ca | 596 | aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **) |
4a6d1198 | 597 | malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *)); |
f55727ca | 598 | if (rtld_search_dirs.dirs == NULL) |
39b3385d UD |
599 | { |
600 | errstring = N_("cannot create search path array"); | |
601 | signal_error: | |
cff26a3e | 602 | INTUSE(_dl_signal_error) (ENOMEM, NULL, NULL, errstring); |
39b3385d | 603 | } |
12264bd7 UD |
604 | |
605 | round_size = ((2 * sizeof (struct r_search_path_elem) - 1 | |
606 | + ncapstr * sizeof (enum r_dir_status)) | |
607 | / sizeof (struct r_search_path_elem)); | |
608 | ||
f55727ca | 609 | rtld_search_dirs.dirs[0] = (struct r_search_path_elem *) |
228589d2 | 610 | malloc ((sizeof (system_dirs) / sizeof (system_dirs[0])) |
12264bd7 | 611 | * round_size * sizeof (struct r_search_path_elem)); |
f55727ca | 612 | if (rtld_search_dirs.dirs[0] == NULL) |
39b3385d UD |
613 | { |
614 | errstring = N_("cannot create cache for search path"); | |
615 | goto signal_error; | |
616 | } | |
12264bd7 | 617 | |
f55727ca | 618 | rtld_search_dirs.malloced = 0; |
d6b5d570 | 619 | pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0]; |
ab7eb292 UD |
620 | strp = system_dirs; |
621 | idx = 0; | |
622 | ||
623 | do | |
12264bd7 UD |
624 | { |
625 | size_t cnt; | |
626 | ||
627 | *aelem++ = pelem; | |
628 | ||
12264bd7 UD |
629 | pelem->what = "system search path"; |
630 | pelem->where = NULL; | |
631 | ||
ab7eb292 UD |
632 | pelem->dirname = strp; |
633 | pelem->dirnamelen = system_dirs_len[idx]; | |
634 | strp += system_dirs_len[idx] + 1; | |
12264bd7 | 635 | |
f55727ca UD |
636 | /* System paths must be absolute. */ |
637 | assert (pelem->dirname[0] == '/'); | |
638 | for (cnt = 0; cnt < ncapstr; ++cnt) | |
639 | pelem->status[cnt] = unknown; | |
ab7eb292 | 640 | |
4a6d1198 | 641 | pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size)); |
ab7eb292 UD |
642 | |
643 | pelem += round_size; | |
12264bd7 | 644 | } |
4a6d1198 | 645 | while (idx < nsystem_dirs_len); |
ab7eb292 UD |
646 | |
647 | max_dirnamelen = SYSTEM_DIRS_MAX_LEN; | |
12264bd7 | 648 | *aelem = NULL; |
4317f9e1 | 649 | |
b5567b2a | 650 | #ifdef SHARED |
81e0cb2d | 651 | /* This points to the map of the main object. */ |
d6b5d570 | 652 | l = GL(dl_loaded); |
011ce8ed UD |
653 | if (l != NULL) |
654 | { | |
011ce8ed | 655 | assert (l->l_type != lt_loaded); |
40a55d20 | 656 | |
fcf70d41 UD |
657 | if (l->l_info[DT_RUNPATH]) |
658 | { | |
659 | /* Allocate room for the search path and fill in information | |
660 | from RUNPATH. */ | |
f55727ca UD |
661 | decompose_rpath (&l->l_runpath_dirs, |
662 | (const void *) (D_PTR (l, l_info[DT_STRTAB]) | |
663 | + l->l_info[DT_RUNPATH]->d_un.d_val), | |
664 | l, "RUNPATH"); | |
fcf70d41 UD |
665 | |
666 | /* The RPATH is ignored. */ | |
f55727ca | 667 | l->l_rpath_dirs.dirs = (void *) -1; |
fcf70d41 | 668 | } |
011ce8ed | 669 | else |
fcf70d41 | 670 | { |
f55727ca | 671 | l->l_runpath_dirs.dirs = (void *) -1; |
fcf70d41 UD |
672 | |
673 | if (l->l_info[DT_RPATH]) | |
f55727ca UD |
674 | { |
675 | /* Allocate room for the search path and fill in information | |
676 | from RPATH. */ | |
677 | decompose_rpath (&l->l_rpath_dirs, | |
678 | (const void *) (D_PTR (l, l_info[DT_STRTAB]) | |
fcf70d41 UD |
679 | + l->l_info[DT_RPATH]->d_un.d_val), |
680 | l, "RPATH"); | |
f55727ca UD |
681 | l->l_rpath_dirs.malloced = 0; |
682 | } | |
fcf70d41 | 683 | else |
f55727ca | 684 | l->l_rpath_dirs.dirs = (void *) -1; |
fcf70d41 | 685 | } |
0a54e401 | 686 | } |
b5567b2a | 687 | #endif /* SHARED */ |
7ef90c15 UD |
688 | |
689 | if (llp != NULL && *llp != '\0') | |
0a54e401 | 690 | { |
7ef90c15 UD |
691 | size_t nllp; |
692 | const char *cp = llp; | |
b0ed91ae | 693 | char *llp_tmp = strdupa (llp); |
011ce8ed | 694 | |
7ef90c15 UD |
695 | /* Decompose the LD_LIBRARY_PATH contents. First determine how many |
696 | elements it has. */ | |
697 | nllp = 1; | |
698 | while (*cp) | |
0a54e401 | 699 | { |
7ef90c15 UD |
700 | if (*cp == ':' || *cp == ';') |
701 | ++nllp; | |
702 | ++cp; | |
011ce8ed | 703 | } |
7ef90c15 | 704 | |
f55727ca | 705 | env_path_list.dirs = (struct r_search_path_elem **) |
7ef90c15 | 706 | malloc ((nllp + 1) * sizeof (struct r_search_path_elem *)); |
f55727ca | 707 | if (env_path_list.dirs == NULL) |
39b3385d UD |
708 | { |
709 | errstring = N_("cannot create cache for search path"); | |
710 | goto signal_error; | |
711 | } | |
7ef90c15 | 712 | |
b0ed91ae | 713 | (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;", |
ab7eb292 | 714 | __libc_enable_secure, "LD_LIBRARY_PATH", NULL); |
6a7c9bb4 | 715 | |
f55727ca | 716 | if (env_path_list.dirs[0] == NULL) |
6a7c9bb4 | 717 | { |
f55727ca UD |
718 | free (env_path_list.dirs); |
719 | env_path_list.dirs = (void *) -1; | |
6a7c9bb4 | 720 | } |
f55727ca UD |
721 | |
722 | env_path_list.malloced = 0; | |
81e0cb2d | 723 | } |
152e7964 | 724 | else |
f55727ca UD |
725 | env_path_list.dirs = (void *) -1; |
726 | ||
727 | /* Remember the last search directory added at startup. */ | |
d6b5d570 | 728 | GL(dl_init_all_dirs) = GL(dl_all_dirs); |
0a54e401 UD |
729 | } |
730 | ||
731 | ||
4f46e038 UD |
732 | /* Think twice before changing anything in this function. It is placed |
733 | here and prepared using the `alloca' magic to prevent it from being | |
734 | inlined. The function is only called in case of an error. But then | |
735 | performance does not count. The function used to be "inlinable" and | |
736 | the compiled did so all the time. This increased the code size for | |
737 | absolutely no good reason. */ | |
a6291c3d | 738 | static void |
126b06f9 UD |
739 | __attribute__ ((noreturn)) |
740 | lose (int code, int fd, const char *name, char *realname, struct link_map *l, | |
741 | const char *msg) | |
742 | { | |
743 | /* The use of `alloca' here looks ridiculous but it helps. The goal | |
744 | is to avoid the function from being inlined. There is no official | |
745 | way to do this so we use this trick. gcc never inlines functions | |
746 | which use `alloca'. */ | |
6d5d3ae3 | 747 | int *a = (int *) alloca (sizeof (int)); |
126b06f9 | 748 | a[0] = fd; |
fb0356b9 UD |
749 | /* The file might already be closed. */ |
750 | if (a[0] != -1) | |
751 | (void) __close (a[0]); | |
126b06f9 UD |
752 | if (l != NULL) |
753 | { | |
754 | /* Remove the stillborn object from the list and free it. */ | |
8bbd5f84 UD |
755 | assert (l->l_next == NULL); |
756 | #ifndef SHARED | |
757 | if (l->l_prev == NULL) | |
758 | /* No other module loaded. */ | |
d6b5d570 | 759 | GL(dl_loaded) = NULL; |
8bbd5f84 UD |
760 | else |
761 | #endif | |
762 | l->l_prev->l_next = NULL; | |
d6b5d570 | 763 | --GL(dl_nloaded); |
126b06f9 UD |
764 | free (l); |
765 | } | |
766 | free (realname); | |
cff26a3e | 767 | INTUSE(_dl_signal_error) (code, name, NULL, msg); |
126b06f9 UD |
768 | } |
769 | ||
770 | ||
ea03559a RM |
771 | /* Map in the shared object NAME, actually located in REALNAME, and already |
772 | opened on FD. */ | |
773 | ||
f787edde UD |
774 | #ifndef EXTERNAL_MAP_FROM_FD |
775 | static | |
776 | #endif | |
ea03559a | 777 | struct link_map * |
a35e137a UD |
778 | _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, |
779 | char *realname, struct link_map *loader, int l_type, | |
780 | int mode) | |
ea03559a | 781 | { |
622586fb | 782 | struct link_map *l = NULL; |
266180eb RM |
783 | const ElfW(Ehdr) *header; |
784 | const ElfW(Phdr) *phdr; | |
785 | const ElfW(Phdr) *ph; | |
8193034b | 786 | size_t maplength; |
b122c703 | 787 | int type; |
5763742f | 788 | struct stat64 st; |
39b3385d UD |
789 | /* Initialize to keep the compiler happy. */ |
790 | const char *errstring = NULL; | |
791 | int errval = 0; | |
61e0617a UD |
792 | |
793 | /* Get file information. */ | |
39b3385d UD |
794 | if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st) < 0, 0)) |
795 | { | |
796 | errstring = N_("cannot stat shared object"); | |
797 | call_lose_errno: | |
798 | errval = errno; | |
799 | call_lose: | |
800 | lose (errval, fd, name, realname, l, errstring); | |
801 | } | |
d66e34cd RM |
802 | |
803 | /* Look again to see if the real name matched another already loaded. */ | |
d6b5d570 | 804 | for (l = GL(dl_loaded); l; l = l->l_next) |
61e0617a | 805 | if (l->l_ino == st.st_ino && l->l_dev == st.st_dev) |
d66e34cd RM |
806 | { |
807 | /* The object is already loaded. | |
808 | Just bump its reference count and return it. */ | |
266180eb | 809 | __close (fd); |
c84142e8 UD |
810 | |
811 | /* If the name is not in the list of names for this object add | |
812 | it. */ | |
ea03559a | 813 | free (realname); |
0413b54c | 814 | add_name_to_object (l, name); |
8699e7b1 | 815 | |
d66e34cd RM |
816 | return l; |
817 | } | |
818 | ||
2f54c82d | 819 | if (mode & RTLD_NOLOAD) |
bf8b3e74 UD |
820 | /* We are not supposed to load the object unless it is already |
821 | loaded. So return now. */ | |
822 | return NULL; | |
823 | ||
8193034b | 824 | /* Print debugging message. */ |
d6b5d570 | 825 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0)) |
cff26a3e | 826 | INTUSE(_dl_debug_printf) ("file=%s; generating link map\n", name); |
8193034b | 827 | |
a35e137a UD |
828 | /* This is the ELF header. We read it in `open_verify'. */ |
829 | header = (void *) fbp->buf; | |
d66e34cd | 830 | |
2064087b | 831 | #ifndef MAP_ANON |
126b06f9 | 832 | # define MAP_ANON 0 |
d66e34cd RM |
833 | if (_dl_zerofd == -1) |
834 | { | |
835 | _dl_zerofd = _dl_sysdep_open_zero_fill (); | |
836 | if (_dl_zerofd == -1) | |
ba79d61b RM |
837 | { |
838 | __close (fd); | |
cff26a3e | 839 | INTUSE(_dl_signal_error) (errno, NULL, NULL, |
7969407a | 840 | N_("cannot open zero fill device")); |
ba79d61b | 841 | } |
d66e34cd | 842 | } |
2064087b | 843 | #endif |
d66e34cd | 844 | |
ba79d61b | 845 | /* Enter the new object in the list of loaded objects. */ |
be935610 | 846 | l = _dl_new_object (realname, name, l_type, loader); |
6c790888 | 847 | if (__builtin_expect (! l, 0)) |
39b3385d UD |
848 | { |
849 | errstring = N_("cannot create shared object descriptor"); | |
850 | goto call_lose_errno; | |
851 | } | |
ba79d61b | 852 | |
b122c703 | 853 | /* Extract the remaining details we need from the ELF header |
32c85e43 | 854 | and then read in the program header table. */ |
b122c703 RM |
855 | l->l_entry = header->e_entry; |
856 | type = header->e_type; | |
857 | l->l_phnum = header->e_phnum; | |
32c85e43 UD |
858 | |
859 | maplength = header->e_phnum * sizeof (ElfW(Phdr)); | |
a35e137a UD |
860 | if (header->e_phoff + maplength <= fbp->len) |
861 | phdr = (void *) (fbp->buf + header->e_phoff); | |
32c85e43 UD |
862 | else |
863 | { | |
864 | phdr = alloca (maplength); | |
865 | __lseek (fd, SEEK_SET, header->e_phoff); | |
866 | if (__libc_read (fd, (void *) phdr, maplength) != maplength) | |
39b3385d UD |
867 | { |
868 | errstring = N_("cannot read file data"); | |
869 | goto call_lose_errno; | |
870 | } | |
32c85e43 | 871 | } |
879bf2e6 | 872 | |
b122c703 RM |
873 | { |
874 | /* Scan the program header table, collecting its load commands. */ | |
875 | struct loadcmd | |
876 | { | |
266180eb | 877 | ElfW(Addr) mapstart, mapend, dataend, allocend; |
b122c703 RM |
878 | off_t mapoff; |
879 | int prot; | |
880 | } loadcmds[l->l_phnum], *c; | |
881 | size_t nloadcmds = 0; | |
d66e34cd | 882 | |
126b06f9 | 883 | /* The struct is initialized to zero so this is not necessary: |
d66e34cd | 884 | l->l_ld = 0; |
b122c703 | 885 | l->l_phdr = 0; |
126b06f9 | 886 | l->l_addr = 0; */ |
d66e34cd RM |
887 | for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph) |
888 | switch (ph->p_type) | |
889 | { | |
890 | /* These entries tell us where to find things once the file's | |
891 | segments are mapped in. We record the addresses it says | |
892 | verbatim, and later correct for the run-time load address. */ | |
893 | case PT_DYNAMIC: | |
894 | l->l_ld = (void *) ph->p_vaddr; | |
dacc8ffa | 895 | l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn)); |
d66e34cd | 896 | break; |
39b3385d | 897 | |
d66e34cd RM |
898 | case PT_PHDR: |
899 | l->l_phdr = (void *) ph->p_vaddr; | |
900 | break; | |
901 | ||
902 | case PT_LOAD: | |
b122c703 RM |
903 | /* A load command tells us to map in part of the file. |
904 | We record the load commands and process them all later. */ | |
d6b5d570 | 905 | if ((ph->p_align & (GL(dl_pagesize) - 1)) != 0) |
39b3385d UD |
906 | { |
907 | errstring = N_("ELF load command alignment not page-aligned"); | |
908 | goto call_lose; | |
909 | } | |
910 | if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0) | |
911 | { | |
912 | errstring | |
913 | = N_("ELF load command address/offset not properly aligned"); | |
914 | goto call_lose; | |
915 | } | |
916 | ||
d66e34cd | 917 | { |
b122c703 RM |
918 | struct loadcmd *c = &loadcmds[nloadcmds++]; |
919 | c->mapstart = ph->p_vaddr & ~(ph->p_align - 1); | |
d6b5d570 UD |
920 | c->mapend = ((ph->p_vaddr + ph->p_filesz + GL(dl_pagesize) - 1) |
921 | & ~(GL(dl_pagesize) - 1)); | |
b122c703 RM |
922 | c->dataend = ph->p_vaddr + ph->p_filesz; |
923 | c->allocend = ph->p_vaddr + ph->p_memsz; | |
924 | c->mapoff = ph->p_offset & ~(ph->p_align - 1); | |
126b06f9 UD |
925 | |
926 | /* Optimize a common case. */ | |
94a758fe UD |
927 | #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7 |
928 | c->prot = (PF_TO_PROT | |
929 | >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf; | |
930 | #else | |
931 | c->prot = 0; | |
932 | if (ph->p_flags & PF_R) | |
933 | c->prot |= PROT_READ; | |
934 | if (ph->p_flags & PF_W) | |
935 | c->prot |= PROT_WRITE; | |
936 | if (ph->p_flags & PF_X) | |
937 | c->prot |= PROT_EXEC; | |
938 | #endif | |
b122c703 | 939 | } |
55c91021 | 940 | break; |
96f208a4 UD |
941 | |
942 | #ifdef USE_TLS | |
943 | case PT_TLS: | |
aed283dd | 944 | if (ph->p_memsz > 0) |
96f208a4 | 945 | { |
aed283dd UD |
946 | l->l_tls_blocksize = ph->p_memsz; |
947 | l->l_tls_align = ph->p_align; | |
948 | l->l_tls_initimage_size = ph->p_filesz; | |
949 | /* Since we don't know the load address yet only store the | |
950 | offset. We will adjust it later. */ | |
951 | l->l_tls_initimage = (void *) ph->p_offset; | |
952 | ||
953 | /* Assign the next available module ID. */ | |
954 | l->l_tls_modid = _dl_next_tls_modid (); | |
96f208a4 | 955 | } |
96f208a4 UD |
956 | break; |
957 | #endif | |
b122c703 | 958 | } |
d66e34cd | 959 | |
b122c703 RM |
960 | /* Now process the load commands and map segments into memory. */ |
961 | c = loadcmds; | |
962 | ||
8193034b UD |
963 | /* Length of the sections to be loaded. */ |
964 | maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart; | |
965 | ||
9987236e | 966 | if (__builtin_expect (type, ET_DYN) == ET_DYN) |
b122c703 RM |
967 | { |
968 | /* This is a position-independent shared object. We can let the | |
969 | kernel map it anywhere it likes, but we must have space for all | |
970 | the segments in their specified positions relative to the first. | |
971 | So we map the first segment without MAP_FIXED, but with its | |
22930c9b RM |
972 | extent increased to cover all the segments. Then we remove |
973 | access from excess portion, and there is known sufficient space | |
4cca6b86 UD |
974 | there to remap from the later segments. |
975 | ||
976 | As a refinement, sometimes we have an address that we would | |
977 | prefer to map such objects at; but this is only a preference, | |
978 | the OS can do whatever it likes. */ | |
4cca6b86 | 979 | ElfW(Addr) mappref; |
f21acc89 UD |
980 | mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart) |
981 | - MAP_BASE_ADDR (l)); | |
c0282c06 UD |
982 | |
983 | /* Remember which part of the address space this object uses. */ | |
39b3385d UD |
984 | l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength, |
985 | c->prot, MAP_COPY | MAP_FILE, | |
986 | fd, c->mapoff); | |
987 | if ((void *) l->l_map_start == MAP_FAILED) | |
988 | { | |
989 | map_error: | |
990 | errstring = N_("failed to map segment from shared object"); | |
991 | goto call_lose_errno; | |
992 | } | |
993 | ||
c0282c06 | 994 | l->l_map_end = l->l_map_start + maplength; |
c77ec56d | 995 | l->l_addr = l->l_map_start - c->mapstart; |
b122c703 | 996 | |
22930c9b RM |
997 | /* Change protection on the excess portion to disallow all access; |
998 | the portions we do not remap later will be inaccessible as if | |
999 | unallocated. Then jump into the normal segment-mapping loop to | |
1000 | handle the portion of the segment past the end of the file | |
1001 | mapping. */ | |
c77ec56d | 1002 | __mprotect ((caddr_t) (l->l_addr + c->mapend), |
266180eb | 1003 | loadcmds[nloadcmds - 1].allocend - c->mapend, |
44ad8377 | 1004 | PROT_NONE); |
052b6a6c | 1005 | |
b122c703 RM |
1006 | goto postmap; |
1007 | } | |
4cca6b86 UD |
1008 | else |
1009 | { | |
48da1092 UD |
1010 | /* This object is loaded at a fixed address. This must never |
1011 | happen for objects loaded with dlopen(). */ | |
55c91021 | 1012 | if (__builtin_expect (mode & __RTLD_DLOPEN, 0)) |
48da1092 | 1013 | { |
39b3385d UD |
1014 | errstring = N_("cannot dynamically load executable"); |
1015 | goto call_lose; | |
48da1092 UD |
1016 | } |
1017 | ||
4cca6b86 UD |
1018 | /* Notify ELF_PREFERRED_ADDRESS that we have to load this one |
1019 | fixed. */ | |
1020 | ELF_FIXED_ADDRESS (loader, c->mapstart); | |
1021 | } | |
b122c703 | 1022 | |
052b6a6c UD |
1023 | /* Remember which part of the address space this object uses. */ |
1024 | l->l_map_start = c->mapstart + l->l_addr; | |
1025 | l->l_map_end = l->l_map_start + maplength; | |
1026 | ||
b122c703 RM |
1027 | while (c < &loadcmds[nloadcmds]) |
1028 | { | |
39b3385d UD |
1029 | if (c->mapend > c->mapstart |
1030 | /* Map the segment contents from the file. */ | |
1031 | && (__mmap ((void *) (l->l_addr + c->mapstart), | |
1032 | c->mapend - c->mapstart, c->prot, | |
1033 | MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff) | |
1034 | == MAP_FAILED)) | |
1035 | goto map_error; | |
b122c703 RM |
1036 | |
1037 | postmap: | |
7bcaca43 UD |
1038 | if (l->l_phdr == 0 |
1039 | && c->mapoff <= header->e_phoff | |
1040 | && (c->mapend - c->mapstart + c->mapoff | |
1041 | >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr)))) | |
1042 | /* Found the program header in this segment. */ | |
1043 | l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff); | |
1044 | ||
b122c703 RM |
1045 | if (c->allocend > c->dataend) |
1046 | { | |
1047 | /* Extra zero pages should appear at the end of this segment, | |
1048 | after the data mapped from the file. */ | |
266180eb | 1049 | ElfW(Addr) zero, zeroend, zeropage; |
b122c703 RM |
1050 | |
1051 | zero = l->l_addr + c->dataend; | |
1052 | zeroend = l->l_addr + c->allocend; | |
d6b5d570 UD |
1053 | zeropage = ((zero + GL(dl_pagesize) - 1) |
1054 | & ~(GL(dl_pagesize) - 1)); | |
d66e34cd | 1055 | |
b122c703 RM |
1056 | if (zeroend < zeropage) |
1057 | /* All the extra data is in the last page of the segment. | |
1058 | We can just zero it. */ | |
1059 | zeropage = zeroend; | |
1060 | ||
1061 | if (zeropage > zero) | |
d66e34cd | 1062 | { |
b122c703 RM |
1063 | /* Zero the final part of the last page of the segment. */ |
1064 | if ((c->prot & PROT_WRITE) == 0) | |
d66e34cd | 1065 | { |
b122c703 | 1066 | /* Dag nab it. */ |
d6b5d570 UD |
1067 | if (__mprotect ((caddr_t) (zero & ~(GL(dl_pagesize) - 1)), |
1068 | GL(dl_pagesize), c->prot|PROT_WRITE) < 0) | |
39b3385d UD |
1069 | { |
1070 | errstring = N_("cannot change memory protections"); | |
1071 | goto call_lose_errno; | |
1072 | } | |
d66e34cd | 1073 | } |
55c91021 | 1074 | memset ((void *) zero, '\0', zeropage - zero); |
b122c703 | 1075 | if ((c->prot & PROT_WRITE) == 0) |
d6b5d570 UD |
1076 | __mprotect ((caddr_t) (zero & ~(GL(dl_pagesize) - 1)), |
1077 | GL(dl_pagesize), c->prot); | |
b122c703 | 1078 | } |
d66e34cd | 1079 | |
b122c703 RM |
1080 | if (zeroend > zeropage) |
1081 | { | |
1082 | /* Map the remaining zero pages in from the zero fill FD. */ | |
1083 | caddr_t mapat; | |
266180eb RM |
1084 | mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage, |
1085 | c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED, | |
2064087b | 1086 | ANONFD, 0); |
0413b54c | 1087 | if (mapat == MAP_FAILED) |
39b3385d UD |
1088 | { |
1089 | errstring = N_("cannot map zero-fill pages"); | |
1090 | goto call_lose_errno; | |
1091 | } | |
d66e34cd RM |
1092 | } |
1093 | } | |
d66e34cd | 1094 | |
b122c703 | 1095 | ++c; |
879bf2e6 | 1096 | } |
0d3726c3 | 1097 | |
7bcaca43 | 1098 | if (l->l_phdr == NULL) |
0d3726c3 | 1099 | { |
55c91021 | 1100 | /* The program header is not contained in any of the segments. |
7bcaca43 UD |
1101 | We have to allocate memory ourself and copy it over from |
1102 | out temporary place. */ | |
1103 | ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum | |
1104 | * sizeof (ElfW(Phdr))); | |
1105 | if (newp == NULL) | |
39b3385d UD |
1106 | { |
1107 | errstring = N_("cannot allocate memory for program header"); | |
1108 | goto call_lose_errno; | |
1109 | } | |
7bcaca43 UD |
1110 | |
1111 | l->l_phdr = memcpy (newp, phdr, | |
1112 | (header->e_phnum * sizeof (ElfW(Phdr)))); | |
1113 | l->l_phdr_allocated = 1; | |
0d3726c3 RM |
1114 | } |
1115 | else | |
1116 | /* Adjust the PT_PHDR value by the runtime load address. */ | |
1117 | (ElfW(Addr)) l->l_phdr += l->l_addr; | |
b122c703 | 1118 | } |
d66e34cd | 1119 | |
a330abe2 UD |
1120 | #ifdef USE_TLS |
1121 | /* Adjust the address of the TLS initialization image. */ | |
1122 | if (l->l_tls_initimage != NULL) | |
1123 | l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_map_start; | |
1124 | #endif | |
1125 | ||
6d9756c9 RM |
1126 | /* We are done mapping in the file. We no longer need the descriptor. */ |
1127 | __close (fd); | |
fb0356b9 UD |
1128 | /* Signal that we closed the file. */ |
1129 | fd = -1; | |
6d9756c9 | 1130 | |
ba79d61b RM |
1131 | if (l->l_type == lt_library && type == ET_EXEC) |
1132 | l->l_type = lt_executable; | |
9b8a44cd | 1133 | |
b122c703 RM |
1134 | if (l->l_ld == 0) |
1135 | { | |
1136 | if (type == ET_DYN) | |
39b3385d UD |
1137 | { |
1138 | errstring = N_("object file has no dynamic section"); | |
1139 | goto call_lose; | |
1140 | } | |
b122c703 RM |
1141 | } |
1142 | else | |
266180eb | 1143 | (ElfW(Addr)) l->l_ld += l->l_addr; |
879bf2e6 | 1144 | |
463e148b RM |
1145 | l->l_entry += l->l_addr; |
1146 | ||
d6b5d570 | 1147 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0)) |
cff26a3e | 1148 | INTUSE(_dl_debug_printf) ("\ |
7969407a UD |
1149 | dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\ |
1150 | entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n", | |
1151 | (int) sizeof (void *) * 2, | |
1152 | (unsigned long int) l->l_ld, | |
1153 | (int) sizeof (void *) * 2, | |
1154 | (unsigned long int) l->l_addr, | |
1155 | (int) sizeof (void *) * 2, maplength, | |
1156 | (int) sizeof (void *) * 2, | |
1157 | (unsigned long int) l->l_entry, | |
1158 | (int) sizeof (void *) * 2, | |
1159 | (unsigned long int) l->l_phdr, | |
1160 | (int) sizeof (void *) * 2, l->l_phnum); | |
8193034b | 1161 | |
fcf70d41 | 1162 | elf_get_dynamic_info (l); |
2f54c82d UD |
1163 | |
1164 | /* Make sure we are dlopen()ing an object which has the DF_1_NOOPEN | |
1165 | flag set. */ | |
ec70c011 UD |
1166 | if ((__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0) |
1167 | #ifdef USE_TLS | |
1168 | || __builtin_expect (l->l_flags & DF_STATIC_TLS, 0) | |
1169 | #endif | |
1170 | ) | |
2f54c82d UD |
1171 | && (mode & __RTLD_DLOPEN)) |
1172 | { | |
2f54c82d UD |
1173 | /* We are not supposed to load this object. Free all resources. */ |
1174 | __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start); | |
1175 | ||
11810621 UD |
1176 | if (!l->l_libname->dont_free) |
1177 | free (l->l_libname); | |
2f54c82d UD |
1178 | |
1179 | if (l->l_phdr_allocated) | |
1180 | free ((void *) l->l_phdr); | |
1181 | ||
fb0356b9 UD |
1182 | errstring = N_("shared object cannot be dlopen()ed"); |
1183 | goto call_lose; | |
2f54c82d UD |
1184 | } |
1185 | ||
d66e34cd RM |
1186 | if (l->l_info[DT_HASH]) |
1187 | _dl_setup_hash (l); | |
1188 | ||
be935610 UD |
1189 | /* If this object has DT_SYMBOLIC set modify now its scope. We don't |
1190 | have to do this for the main map. */ | |
7ad9abc0 | 1191 | if (__builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0) |
c1d32f33 | 1192 | && &l->l_searchlist != l->l_scope[0]) |
be935610 UD |
1193 | { |
1194 | /* Create an appropriate searchlist. It contains only this map. | |
1195 | ||
1196 | XXX This is the definition of DT_SYMBOLIC in SysVr4. The old | |
1197 | GNU ld.so implementation had a different interpretation which | |
1198 | is more reasonable. We are prepared to add this possibility | |
1199 | back as part of a GNU extension of the ELF format. */ | |
1200 | l->l_symbolic_searchlist.r_list = | |
1201 | (struct link_map **) malloc (sizeof (struct link_map *)); | |
1202 | ||
1203 | if (l->l_symbolic_searchlist.r_list == NULL) | |
39b3385d UD |
1204 | { |
1205 | errstring = N_("cannot create searchlist"); | |
1206 | goto call_lose_errno; | |
1207 | } | |
be935610 UD |
1208 | |
1209 | l->l_symbolic_searchlist.r_list[0] = l; | |
1210 | l->l_symbolic_searchlist.r_nlist = 1; | |
be935610 UD |
1211 | |
1212 | /* Now move the existing entries one back. */ | |
1213 | memmove (&l->l_scope[1], &l->l_scope[0], | |
9596d0dd | 1214 | (l->l_scope_max - 1) * sizeof (l->l_scope[0])); |
be935610 UD |
1215 | |
1216 | /* Now add the new entry. */ | |
1217 | l->l_scope[0] = &l->l_symbolic_searchlist; | |
1218 | } | |
1219 | ||
5d916713 | 1220 | /* Remember whether this object must be initialized first. */ |
39b3385d | 1221 | if (l->l_flags_1 & DF_1_INITFIRST) |
d6b5d570 | 1222 | GL(dl_initfirst) = l; |
5d916713 | 1223 | |
61e0617a UD |
1224 | /* Finally the file information. */ |
1225 | l->l_dev = st.st_dev; | |
1226 | l->l_ino = st.st_ino; | |
1227 | ||
d66e34cd RM |
1228 | return l; |
1229 | } | |
ba79d61b | 1230 | \f |
b5efde2f UD |
1231 | /* Print search path. */ |
1232 | static void | |
1233 | print_search_path (struct r_search_path_elem **list, | |
1234 | const char *what, const char *name) | |
1235 | { | |
12264bd7 | 1236 | char buf[max_dirnamelen + max_capstrlen]; |
b5efde2f UD |
1237 | int first = 1; |
1238 | ||
cff26a3e | 1239 | INTUSE(_dl_debug_printf) (" search path="); |
b5efde2f UD |
1240 | |
1241 | while (*list != NULL && (*list)->what == what) /* Yes, ==. */ | |
1242 | { | |
12264bd7 UD |
1243 | char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen); |
1244 | size_t cnt; | |
1245 | ||
1246 | for (cnt = 0; cnt < ncapstr; ++cnt) | |
1247 | if ((*list)->status[cnt] != nonexisting) | |
1248 | { | |
1249 | char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len); | |
143e2b96 UD |
1250 | if (cp == buf || (cp == buf + 1 && buf[0] == '/')) |
1251 | cp[0] = '\0'; | |
1252 | else | |
1253 | cp[-1] = '\0'; | |
fb0356b9 UD |
1254 | |
1255 | _dl_debug_printf_c (first ? "%s" : ":%s", buf); | |
1256 | first = 0; | |
12264bd7 | 1257 | } |
b5efde2f | 1258 | |
b5efde2f UD |
1259 | ++list; |
1260 | } | |
1261 | ||
1262 | if (name != NULL) | |
35fc382a UD |
1263 | _dl_debug_printf_c ("\t\t(%s from file %s)\n", what, |
1264 | name[0] ? name : _dl_argv[0]); | |
b5efde2f | 1265 | else |
35fc382a | 1266 | _dl_debug_printf_c ("\t\t(%s)\n", what); |
b5efde2f UD |
1267 | } |
1268 | \f | |
a35e137a UD |
1269 | /* Open a file and verify it is an ELF file for this architecture. We |
1270 | ignore only ELF files for other architectures. Non-ELF files and | |
1271 | ELF files with different header information cause fatal errors since | |
1272 | this could mean there is something wrong in the installation and the | |
1273 | user might want to know about this. */ | |
1274 | static int | |
1275 | open_verify (const char *name, struct filebuf *fbp) | |
1276 | { | |
1277 | /* This is the expected ELF header. */ | |
1278 | #define ELF32_CLASS ELFCLASS32 | |
1279 | #define ELF64_CLASS ELFCLASS64 | |
1280 | #ifndef VALID_ELF_HEADER | |
1281 | # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0) | |
1282 | # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV) | |
1283 | # define VALID_ELF_ABIVERSION(ver) (ver == 0) | |
1284 | #endif | |
1285 | static const unsigned char expected[EI_PAD] = | |
1286 | { | |
1287 | [EI_MAG0] = ELFMAG0, | |
1288 | [EI_MAG1] = ELFMAG1, | |
1289 | [EI_MAG2] = ELFMAG2, | |
1290 | [EI_MAG3] = ELFMAG3, | |
1291 | [EI_CLASS] = ELFW(CLASS), | |
1292 | [EI_DATA] = byteorder, | |
1293 | [EI_VERSION] = EV_CURRENT, | |
1294 | [EI_OSABI] = ELFOSABI_SYSV, | |
1295 | [EI_ABIVERSION] = 0 | |
1296 | }; | |
39b3385d UD |
1297 | static const struct |
1298 | { | |
1299 | ElfW(Word) vendorlen; | |
1300 | ElfW(Word) datalen; | |
1301 | ElfW(Word) type; | |
55c91021 | 1302 | char vendor[4]; |
a986484f | 1303 | } expected_note = { 4, 16, 1, "GNU" }; |
a35e137a | 1304 | int fd; |
39b3385d UD |
1305 | /* Initialize it to make the compiler happy. */ |
1306 | const char *errstring = NULL; | |
1307 | int errval = 0; | |
a35e137a UD |
1308 | |
1309 | /* Open the file. We always open files read-only. */ | |
1310 | fd = __open (name, O_RDONLY); | |
1311 | if (fd != -1) | |
1312 | { | |
1313 | ElfW(Ehdr) *ehdr; | |
a986484f UD |
1314 | ElfW(Phdr) *phdr, *ph; |
1315 | ElfW(Word) *abi_note, abi_note_buf[8]; | |
1316 | unsigned int osversion; | |
1317 | size_t maplength; | |
a35e137a UD |
1318 | |
1319 | /* We successfully openened the file. Now verify it is a file | |
1320 | we can use. */ | |
1321 | __set_errno (0); | |
1322 | fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf)); | |
1323 | ||
1324 | /* This is where the ELF header is loaded. */ | |
1325 | assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr))); | |
1326 | ehdr = (ElfW(Ehdr) *) fbp->buf; | |
1327 | ||
1328 | /* Now run the tests. */ | |
1329 | if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0)) | |
39b3385d UD |
1330 | { |
1331 | errval = errno; | |
1332 | errstring = (errval == 0 | |
1333 | ? N_("file too short") : N_("cannot read file data")); | |
1334 | call_lose: | |
1335 | lose (errval, fd, name, NULL, NULL, errstring); | |
1336 | } | |
a35e137a UD |
1337 | |
1338 | /* See whether the ELF header is what we expect. */ | |
1339 | if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected, | |
1340 | EI_PAD), 0)) | |
1341 | { | |
1342 | /* Something is wrong. */ | |
1343 | if (*(Elf32_Word *) &ehdr->e_ident != | |
1344 | #if BYTE_ORDER == LITTLE_ENDIAN | |
1345 | ((ELFMAG0 << (EI_MAG0 * 8)) | | |
1346 | (ELFMAG1 << (EI_MAG1 * 8)) | | |
1347 | (ELFMAG2 << (EI_MAG2 * 8)) | | |
1348 | (ELFMAG3 << (EI_MAG3 * 8))) | |
1349 | #else | |
1350 | ((ELFMAG0 << (EI_MAG3 * 8)) | | |
1351 | (ELFMAG1 << (EI_MAG2 * 8)) | | |
1352 | (ELFMAG2 << (EI_MAG1 * 8)) | | |
1353 | (ELFMAG3 << (EI_MAG0 * 8))) | |
1354 | #endif | |
1355 | ) | |
39b3385d UD |
1356 | errstring = N_("invalid ELF header"); |
1357 | else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS)) | |
a35e137a UD |
1358 | /* This is not a fatal error. On architectures where |
1359 | 32-bit and 64-bit binaries can be run this might | |
1360 | happen. */ | |
1361 | goto close_and_out; | |
39b3385d | 1362 | else if (ehdr->e_ident[EI_DATA] != byteorder) |
a35e137a UD |
1363 | { |
1364 | if (BYTE_ORDER == BIG_ENDIAN) | |
39b3385d | 1365 | errstring = N_("ELF file data encoding not big-endian"); |
a35e137a | 1366 | else |
39b3385d | 1367 | errstring = N_("ELF file data encoding not little-endian"); |
a35e137a | 1368 | } |
39b3385d UD |
1369 | else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) |
1370 | errstring | |
1371 | = N_("ELF file version ident does not match current one"); | |
a35e137a UD |
1372 | /* XXX We should be able so set system specific versions which are |
1373 | allowed here. */ | |
39b3385d UD |
1374 | else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI])) |
1375 | errstring = N_("ELF file OS ABI invalid"); | |
1376 | else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION])) | |
1377 | errstring = N_("ELF file ABI version invalid"); | |
1378 | else | |
1379 | /* Otherwise we don't know what went wrong. */ | |
1380 | errstring = N_("internal error"); | |
1381 | ||
1382 | goto call_lose; | |
a35e137a UD |
1383 | } |
1384 | ||
1385 | if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT) | |
39b3385d UD |
1386 | { |
1387 | errstring = N_("ELF file version does not match current one"); | |
1388 | goto call_lose; | |
1389 | } | |
a35e137a | 1390 | if (! __builtin_expect (elf_machine_matches_host (ehdr), 1)) |
a986484f | 1391 | goto close_and_out; |
a35e137a UD |
1392 | else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr))) |
1393 | != sizeof (ElfW(Phdr))) | |
39b3385d UD |
1394 | { |
1395 | errstring = N_("ELF file's phentsize not the expected size"); | |
1396 | goto call_lose; | |
1397 | } | |
9987236e UD |
1398 | else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN |
1399 | && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC) | |
39b3385d UD |
1400 | { |
1401 | errstring = N_("only ET_DYN and ET_EXEC can be loaded"); | |
1402 | goto call_lose; | |
1403 | } | |
a986484f UD |
1404 | |
1405 | maplength = ehdr->e_phnum * sizeof (ElfW(Phdr)); | |
1406 | if (ehdr->e_phoff + maplength <= fbp->len) | |
1407 | phdr = (void *) (fbp->buf + ehdr->e_phoff); | |
1408 | else | |
1409 | { | |
1410 | phdr = alloca (maplength); | |
1411 | __lseek (fd, SEEK_SET, ehdr->e_phoff); | |
1412 | if (__libc_read (fd, (void *) phdr, maplength) != maplength) | |
39b3385d UD |
1413 | { |
1414 | read_error: | |
1415 | errval = errno; | |
1416 | errstring = N_("cannot read file data"); | |
1417 | goto call_lose; | |
1418 | } | |
a986484f UD |
1419 | } |
1420 | ||
1421 | /* Check .note.ABI-tag if present. */ | |
1422 | for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph) | |
1423 | if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4) | |
1424 | { | |
1425 | if (ph->p_offset + 32 <= fbp->len) | |
1426 | abi_note = (void *) (fbp->buf + ph->p_offset); | |
1427 | else | |
1428 | { | |
1429 | __lseek (fd, SEEK_SET, ph->p_offset); | |
1430 | if (__libc_read (fd, (void *) abi_note_buf, 32) != 32) | |
39b3385d UD |
1431 | goto read_error; |
1432 | ||
a986484f UD |
1433 | abi_note = abi_note_buf; |
1434 | } | |
1435 | ||
1436 | if (memcmp (abi_note, &expected_note, sizeof (expected_note))) | |
1437 | continue; | |
1438 | ||
55c91021 UD |
1439 | osversion = (abi_note[5] & 0xff) * 65536 |
1440 | + (abi_note[6] & 0xff) * 256 | |
1441 | + (abi_note[7] & 0xff); | |
1442 | if (abi_note[4] != __ABI_TAG_OS | |
d6b5d570 | 1443 | || (GL(dl_osversion) && GL(dl_osversion) < osversion)) |
a986484f UD |
1444 | { |
1445 | close_and_out: | |
1446 | __close (fd); | |
1447 | __set_errno (ENOENT); | |
1448 | fd = -1; | |
1449 | } | |
1450 | ||
1451 | break; | |
1452 | } | |
a35e137a UD |
1453 | } |
1454 | ||
1455 | return fd; | |
1456 | } | |
1457 | \f | |
04ea3b0f | 1458 | /* Try to open NAME in one of the directories in *DIRSP. |
ba79d61b | 1459 | Return the fd, or -1. If successful, fill in *REALNAME |
04ea3b0f UD |
1460 | with the malloc'd full directory name. If it turns out |
1461 | that none of the directories in *DIRSP exists, *DIRSP is | |
1462 | replaced with (void *) -1, and the old value is free()d | |
1463 | if MAY_FREE_DIRS is true. */ | |
ba79d61b RM |
1464 | |
1465 | static int | |
c6222ab9 | 1466 | open_path (const char *name, size_t namelen, int preloaded, |
a35e137a UD |
1467 | struct r_search_path_struct *sps, char **realname, |
1468 | struct filebuf *fbp) | |
ba79d61b | 1469 | { |
f55727ca | 1470 | struct r_search_path_elem **dirs = sps->dirs; |
ba79d61b | 1471 | char *buf; |
0a54e401 | 1472 | int fd = -1; |
b5efde2f | 1473 | const char *current_what = NULL; |
152e7964 | 1474 | int any = 0; |
ba79d61b | 1475 | |
5431ece5 | 1476 | buf = alloca (max_dirnamelen + max_capstrlen + namelen); |
ba79d61b RM |
1477 | do |
1478 | { | |
0a54e401 UD |
1479 | struct r_search_path_elem *this_dir = *dirs; |
1480 | size_t buflen = 0; | |
12264bd7 | 1481 | size_t cnt; |
b0b67c47 | 1482 | char *edp; |
f5858039 | 1483 | int here_any = 0; |
55c91021 | 1484 | int err; |
ba79d61b | 1485 | |
b5efde2f UD |
1486 | /* If we are debugging the search for libraries print the path |
1487 | now if it hasn't happened now. */ | |
d6b5d570 | 1488 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0) |
cf197e41 | 1489 | && current_what != this_dir->what) |
b5efde2f UD |
1490 | { |
1491 | current_what = this_dir->what; | |
1492 | print_search_path (dirs, current_what, this_dir->where); | |
1493 | } | |
1494 | ||
b0b67c47 | 1495 | edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen); |
12264bd7 | 1496 | for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt) |
fd26970f | 1497 | { |
12264bd7 UD |
1498 | /* Skip this directory if we know it does not exist. */ |
1499 | if (this_dir->status[cnt] == nonexisting) | |
1500 | continue; | |
0a54e401 | 1501 | |
12264bd7 | 1502 | buflen = |
d6b5d570 UD |
1503 | ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str, |
1504 | capstr[cnt].len), | |
12264bd7 UD |
1505 | name, namelen) |
1506 | - buf); | |
1507 | ||
1508 | /* Print name we try if this is wanted. */ | |
d6b5d570 | 1509 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0)) |
cff26a3e | 1510 | INTUSE(_dl_debug_printf) (" trying file=%s\n", buf); |
b5efde2f | 1511 | |
a35e137a | 1512 | fd = open_verify (buf, fbp); |
12264bd7 | 1513 | if (this_dir->status[cnt] == unknown) |
6e4c40ba UD |
1514 | { |
1515 | if (fd != -1) | |
1516 | this_dir->status[cnt] = existing; | |
1517 | else | |
1518 | { | |
1519 | /* We failed to open machine dependent library. Let's | |
1520 | test whether there is any directory at all. */ | |
5763742f | 1521 | struct stat64 st; |
6e4c40ba UD |
1522 | |
1523 | buf[buflen - namelen - 1] = '\0'; | |
1524 | ||
5763742f | 1525 | if (__xstat64 (_STAT_VER, buf, &st) != 0 |
6e4c40ba UD |
1526 | || ! S_ISDIR (st.st_mode)) |
1527 | /* The directory does not exist or it is no directory. */ | |
1528 | this_dir->status[cnt] = nonexisting; | |
1529 | else | |
1530 | this_dir->status[cnt] = existing; | |
1531 | } | |
1532 | } | |
fd26970f | 1533 | |
152e7964 | 1534 | /* Remember whether we found any existing directory. */ |
f5858039 | 1535 | here_any |= this_dir->status[cnt] == existing; |
152e7964 | 1536 | |
55c91021 UD |
1537 | if (fd != -1 && __builtin_expect (preloaded, 0) |
1538 | && __libc_enable_secure) | |
c6222ab9 UD |
1539 | { |
1540 | /* This is an extra security effort to make sure nobody can | |
1541 | preload broken shared objects which are in the trusted | |
1542 | directories and so exploit the bugs. */ | |
5763742f | 1543 | struct stat64 st; |
c6222ab9 | 1544 | |
5763742f | 1545 | if (__fxstat64 (_STAT_VER, fd, &st) != 0 |
c6222ab9 UD |
1546 | || (st.st_mode & S_ISUID) == 0) |
1547 | { | |
1548 | /* The shared object cannot be tested for being SUID | |
1549 | or this bit is not set. In this case we must not | |
1550 | use this object. */ | |
1551 | __close (fd); | |
1552 | fd = -1; | |
1553 | /* We simply ignore the file, signal this by setting | |
1554 | the error value which would have been set by `open'. */ | |
1555 | errno = ENOENT; | |
1556 | } | |
1557 | } | |
ba79d61b RM |
1558 | } |
1559 | ||
ba79d61b RM |
1560 | if (fd != -1) |
1561 | { | |
2f653c01 | 1562 | *realname = (char *) malloc (buflen); |
c6222ab9 | 1563 | if (*realname != NULL) |
ba79d61b RM |
1564 | { |
1565 | memcpy (*realname, buf, buflen); | |
1566 | return fd; | |
1567 | } | |
1568 | else | |
1569 | { | |
1570 | /* No memory for the name, we certainly won't be able | |
1571 | to load and link it. */ | |
1572 | __close (fd); | |
1573 | return -1; | |
1574 | } | |
1575 | } | |
55c91021 | 1576 | if (here_any && (err = errno) != ENOENT && err != EACCES) |
ba79d61b RM |
1577 | /* The file exists and is readable, but something went wrong. */ |
1578 | return -1; | |
f5858039 UD |
1579 | |
1580 | /* Remember whether we found anything. */ | |
1581 | any |= here_any; | |
ba79d61b | 1582 | } |
0a54e401 | 1583 | while (*++dirs != NULL); |
ba79d61b | 1584 | |
152e7964 | 1585 | /* Remove the whole path if none of the directories exists. */ |
55c91021 | 1586 | if (__builtin_expect (! any, 0)) |
152e7964 | 1587 | { |
04ea3b0f UD |
1588 | /* Paths which were allocated using the minimal malloc() in ld.so |
1589 | must not be freed using the general free() in libc. */ | |
f55727ca UD |
1590 | if (sps->malloced) |
1591 | free (sps->dirs); | |
1592 | sps->dirs = (void *) -1; | |
152e7964 UD |
1593 | } |
1594 | ||
ba79d61b RM |
1595 | return -1; |
1596 | } | |
1597 | ||
1598 | /* Map in the shared object file NAME. */ | |
1599 | ||
1600 | struct link_map * | |
d0fc4041 | 1601 | internal_function |
c6222ab9 | 1602 | _dl_map_object (struct link_map *loader, const char *name, int preloaded, |
2f54c82d | 1603 | int type, int trace_mode, int mode) |
ba79d61b RM |
1604 | { |
1605 | int fd; | |
1606 | char *realname; | |
14bab8de | 1607 | char *name_copy; |
ba79d61b | 1608 | struct link_map *l; |
a35e137a | 1609 | struct filebuf fb; |
ba79d61b RM |
1610 | |
1611 | /* Look for this name among those already loaded. */ | |
d6b5d570 | 1612 | for (l = GL(dl_loaded); l; l = l->l_next) |
f41c8091 UD |
1613 | { |
1614 | /* If the requested name matches the soname of a loaded object, | |
1615 | use that object. Elide this check for names that have not | |
1616 | yet been opened. */ | |
55c91021 | 1617 | if (__builtin_expect (l->l_faked, 0) != 0) |
f41c8091 UD |
1618 | continue; |
1619 | if (!_dl_name_match_p (name, l)) | |
1620 | { | |
1621 | const char *soname; | |
1622 | ||
c91bc73e UD |
1623 | if (__builtin_expect (l->l_soname_added, 1) |
1624 | || l->l_info[DT_SONAME] == NULL) | |
f41c8091 UD |
1625 | continue; |
1626 | ||
8699e7b1 UD |
1627 | soname = ((const char *) D_PTR (l, l_info[DT_STRTAB]) |
1628 | + l->l_info[DT_SONAME]->d_un.d_val); | |
f41c8091 UD |
1629 | if (strcmp (name, soname) != 0) |
1630 | continue; | |
1631 | ||
1632 | /* We have a match on a new name -- cache it. */ | |
76156ea1 | 1633 | add_name_to_object (l, soname); |
c91bc73e | 1634 | l->l_soname_added = 1; |
f41c8091 UD |
1635 | } |
1636 | ||
42c4f32a | 1637 | /* We have a match. */ |
f41c8091 UD |
1638 | return l; |
1639 | } | |
ba79d61b | 1640 | |
8193034b | 1641 | /* Display information if we are debugging. */ |
7969407a UD |
1642 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0) |
1643 | && loader != NULL) | |
cff26a3e AJ |
1644 | INTUSE(_dl_debug_printf) ("\nfile=%s; needed by %s\n", name, |
1645 | loader->l_name[0] ? loader->l_name : _dl_argv[0]); | |
8193034b | 1646 | |
ba79d61b RM |
1647 | if (strchr (name, '/') == NULL) |
1648 | { | |
1649 | /* Search for NAME in several places. */ | |
1650 | ||
1651 | size_t namelen = strlen (name) + 1; | |
1652 | ||
d6b5d570 | 1653 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0)) |
cff26a3e | 1654 | INTUSE(_dl_debug_printf) ("find library=%s; searching\n", name); |
b5efde2f | 1655 | |
ba79d61b | 1656 | fd = -1; |
a23db8e4 | 1657 | |
fcf70d41 UD |
1658 | /* When the object has the RUNPATH information we don't use any |
1659 | RPATHs. */ | |
4c540916 | 1660 | if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL) |
fcf70d41 UD |
1661 | { |
1662 | /* First try the DT_RPATH of the dependent object that caused NAME | |
1663 | to be loaded. Then that object's dependent, and on up. */ | |
1664 | for (l = loader; fd == -1 && l; l = l->l_loader) | |
152e7964 | 1665 | { |
f55727ca | 1666 | if (l->l_rpath_dirs.dirs == NULL) |
152e7964 UD |
1667 | { |
1668 | if (l->l_info[DT_RPATH] == NULL) | |
2f653c01 UD |
1669 | { |
1670 | /* There is no path. */ | |
1671 | l->l_rpath_dirs.dirs = (void *) -1; | |
1672 | continue; | |
1673 | } | |
152e7964 UD |
1674 | else |
1675 | { | |
1676 | /* Make sure the cache information is available. */ | |
1677 | size_t ptrval = (D_PTR (l, l_info[DT_STRTAB]) | |
1678 | + l->l_info[DT_RPATH]->d_un.d_val); | |
f55727ca UD |
1679 | decompose_rpath (&l->l_rpath_dirs, |
1680 | (const char *) ptrval, l, "RPATH"); | |
152e7964 UD |
1681 | } |
1682 | } | |
2f653c01 UD |
1683 | |
1684 | if (l->l_rpath_dirs.dirs != (void *) -1) | |
152e7964 | 1685 | fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs, |
a35e137a | 1686 | &realname, &fb); |
152e7964 | 1687 | } |
0a54e401 | 1688 | |
fcf70d41 UD |
1689 | /* If dynamically linked, try the DT_RPATH of the executable |
1690 | itself. */ | |
d6b5d570 | 1691 | l = GL(dl_loaded); |
fcf70d41 | 1692 | if (fd == -1 && l && l->l_type != lt_loaded && l != loader |
f55727ca UD |
1693 | && l->l_rpath_dirs.dirs != (void *) -1) |
1694 | fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs, | |
a35e137a | 1695 | &realname, &fb); |
fcf70d41 | 1696 | } |
fd26970f | 1697 | |
7ef90c15 | 1698 | /* Try the LD_LIBRARY_PATH environment variable. */ |
f55727ca UD |
1699 | if (fd == -1 && env_path_list.dirs != (void *) -1) |
1700 | fd = open_path (name, namelen, preloaded, &env_path_list, | |
a35e137a | 1701 | &realname, &fb); |
40a55d20 | 1702 | |
2f653c01 UD |
1703 | /* Look at the RUNPATH information for this binary. |
1704 | ||
1705 | Note that this is no real loop. 'while' is used only to enable | |
1706 | us to use 'break' instead of a 'goto' to jump to the end. The | |
1707 | loop is always left after the first round. */ | |
1708 | while (fd == -1 && loader != NULL | |
1709 | && loader->l_runpath_dirs.dirs != (void *) -1) | |
fcf70d41 | 1710 | { |
f55727ca | 1711 | if (loader->l_runpath_dirs.dirs == NULL) |
152e7964 UD |
1712 | { |
1713 | if (loader->l_info[DT_RUNPATH] == NULL) | |
2f653c01 UD |
1714 | { |
1715 | /* No RUNPATH. */ | |
1716 | loader->l_runpath_dirs.dirs = (void *) -1; | |
1717 | break; | |
1718 | } | |
152e7964 UD |
1719 | else |
1720 | { | |
1721 | /* Make sure the cache information is available. */ | |
1722 | size_t ptrval = (D_PTR (loader, l_info[DT_STRTAB]) | |
1723 | + loader->l_info[DT_RUNPATH]->d_un.d_val); | |
f55727ca UD |
1724 | decompose_rpath (&loader->l_runpath_dirs, |
1725 | (const char *) ptrval, loader, "RUNPATH"); | |
152e7964 UD |
1726 | } |
1727 | } | |
95589177 UD |
1728 | |
1729 | if (loader->l_runpath_dirs.dirs != (void *) -1) | |
152e7964 | 1730 | fd = open_path (name, namelen, preloaded, |
a35e137a | 1731 | &loader->l_runpath_dirs, &realname, &fb); |
2f653c01 | 1732 | break; |
fcf70d41 UD |
1733 | } |
1734 | ||
55c91021 UD |
1735 | if (fd == -1 |
1736 | && (__builtin_expect (! preloaded, 1) || ! __libc_enable_secure)) | |
f18edac3 RM |
1737 | { |
1738 | /* Check the list of libraries in the file /etc/ld.so.cache, | |
1739 | for compatibility with Linux's ldconfig program. */ | |
f18edac3 | 1740 | const char *cached = _dl_load_cache_lookup (name); |
0f6b172f | 1741 | |
2f653c01 UD |
1742 | if (cached != NULL) |
1743 | { | |
0f6b172f | 1744 | #ifdef SHARED |
d6b5d570 | 1745 | l = loader ?: GL(dl_loaded); |
0f6b172f | 1746 | #else |
2f653c01 | 1747 | l = loader; |
0f6b172f UD |
1748 | #endif |
1749 | ||
266bb989 UD |
1750 | /* If the loader has the DF_1_NODEFLIB flag set we must not |
1751 | use a cache entry from any of these directories. */ | |
2f653c01 UD |
1752 | if ( |
1753 | #ifndef SHARED | |
1754 | /* 'l' is always != NULL for dynamically linked objects. */ | |
1755 | l != NULL && | |
1756 | #endif | |
1757 | __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0)) | |
266bb989 UD |
1758 | { |
1759 | const char *dirp = system_dirs; | |
2e47aff5 | 1760 | unsigned int cnt = 0; |
266bb989 UD |
1761 | |
1762 | do | |
1763 | { | |
1764 | if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0) | |
1765 | { | |
1766 | /* The prefix matches. Don't use the entry. */ | |
1767 | cached = NULL; | |
1768 | break; | |
1769 | } | |
1770 | ||
1771 | dirp += system_dirs_len[cnt] + 1; | |
1772 | ++cnt; | |
1773 | } | |
4a6d1198 | 1774 | while (cnt < nsystem_dirs_len); |
266bb989 UD |
1775 | } |
1776 | ||
2f653c01 | 1777 | if (cached != NULL) |
f18edac3 | 1778 | { |
a35e137a | 1779 | fd = open_verify (cached, &fb); |
2f653c01 | 1780 | if (__builtin_expect (fd != -1, 1)) |
f18edac3 | 1781 | { |
266bb989 UD |
1782 | realname = local_strdup (cached); |
1783 | if (realname == NULL) | |
1784 | { | |
1785 | __close (fd); | |
1786 | fd = -1; | |
1787 | } | |
f18edac3 RM |
1788 | } |
1789 | } | |
1790 | } | |
1791 | } | |
0a54e401 | 1792 | |
a23db8e4 | 1793 | /* Finally, try the default path. */ |
266bb989 | 1794 | if (fd == -1 |
d6b5d570 | 1795 | && ((l = loader ?: GL(dl_loaded)) == NULL |
db2ebcef | 1796 | || __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1)) |
f55727ca UD |
1797 | && rtld_search_dirs.dirs != (void *) -1) |
1798 | fd = open_path (name, namelen, preloaded, &rtld_search_dirs, | |
a35e137a | 1799 | &realname, &fb); |
b5efde2f UD |
1800 | |
1801 | /* Add another newline when we a tracing the library loading. */ | |
d6b5d570 | 1802 | if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0)) |
cff26a3e | 1803 | INTUSE(_dl_debug_printf) ("\n"); |
ba79d61b RM |
1804 | } |
1805 | else | |
1806 | { | |
f787edde UD |
1807 | /* The path may contain dynamic string tokens. */ |
1808 | realname = (loader | |
1809 | ? expand_dynamic_string_token (loader, name) | |
1810 | : local_strdup (name)); | |
1811 | if (realname == NULL) | |
1812 | fd = -1; | |
1813 | else | |
ba79d61b | 1814 | { |
a35e137a | 1815 | fd = open_verify (realname, &fb); |
55c91021 | 1816 | if (__builtin_expect (fd, 0) == -1) |
f787edde | 1817 | free (realname); |
ba79d61b RM |
1818 | } |
1819 | } | |
1820 | ||
55c91021 | 1821 | if (__builtin_expect (fd, 0) == -1) |
46ec036d | 1822 | { |
32e6df36 | 1823 | if (trace_mode |
d6b5d570 | 1824 | && __builtin_expect (GL(dl_debug_mask) & DL_DEBUG_PRELINK, 0) == 0) |
46ec036d UD |
1825 | { |
1826 | /* We haven't found an appropriate library. But since we | |
1827 | are only interested in the list of libraries this isn't | |
1828 | so severe. Fake an entry with all the information we | |
1ef32c3d | 1829 | have. */ |
a1eca9f3 | 1830 | static const Elf_Symndx dummy_bucket = STN_UNDEF; |
46ec036d UD |
1831 | |
1832 | /* Enter the new object in the list of loaded objects. */ | |
1833 | if ((name_copy = local_strdup (name)) == NULL | |
be935610 | 1834 | || (l = _dl_new_object (name_copy, name, type, loader)) == NULL) |
cff26a3e | 1835 | INTUSE(_dl_signal_error) (ENOMEM, name, NULL, N_("\ |
7969407a | 1836 | cannot create shared object descriptor")); |
a881e0a0 UD |
1837 | /* Signal that this is a faked entry. */ |
1838 | l->l_faked = 1; | |
1839 | /* Since the descriptor is initialized with zero we do not | |
126b06f9 | 1840 | have do this here. |
126b06f9 | 1841 | l->l_reserved = 0; */ |
fd26970f UD |
1842 | l->l_buckets = &dummy_bucket; |
1843 | l->l_nbuckets = 1; | |
1844 | l->l_relocated = 1; | |
1845 | ||
1846 | return l; | |
46ec036d UD |
1847 | } |
1848 | else | |
cff26a3e | 1849 | INTUSE(_dl_signal_error) (errno, name, NULL, |
7969407a | 1850 | N_("cannot open shared object file")); |
46ec036d | 1851 | } |
ba79d61b | 1852 | |
a35e137a | 1853 | return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode); |
ba79d61b | 1854 | } |
7969407a | 1855 | INTDEF (_dl_map_object) |