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