]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-load.c
Add clarifications.
[thirdparty/glibc.git] / elf / dl-load.c
CommitLineData
0a54e401 1/* Map in a shared object's segments from the file.
880f421f 2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37
UD
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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
13 Library General Public License for more details.
d66e34cd 14
afd4eb37
UD
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
d66e34cd 19
14e9dd67 20#include <elf.h>
0a54e401
UD
21#include <errno.h>
22#include <fcntl.h>
0a54e401 23#include <stdlib.h>
d66e34cd 24#include <string.h>
d66e34cd 25#include <unistd.h>
a853022c 26#include <elf/ldsodefs.h>
0a54e401 27#include <sys/mman.h>
f8f7e090 28#include <sys/param.h>
0a54e401
UD
29#include <sys/stat.h>
30#include <sys/types.h>
d66e34cd 31#include "dynamic-link.h"
8193034b 32#include <stdio-common/_itoa.h>
d66e34cd 33
f787edde
UD
34#include <dl-origin.h>
35
d66e34cd 36
9b8a44cd
RM
37/* On some systems, no flag bits are given to specify file mapping. */
38#ifndef MAP_FILE
39#define MAP_FILE 0
40#endif
41
42/* The right way to map in the shared library files is MAP_COPY, which
43 makes a virtual copy of the data at the time of the mmap call; this
44 guarantees the mapped pages will be consistent even if the file is
45 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
46 get is MAP_PRIVATE, which copies each page when it is modified; this
47 means if the file is overwritten, we may at some point get some pages
48 from the new version after starting with pages from the old version. */
49#ifndef MAP_COPY
50#define MAP_COPY MAP_PRIVATE
51#endif
52
f21acc89
UD
53/* Some systems link their relocatable objects for another base address
54 than 0. We want to know the base address for these such that we can
55 subtract this address from the segment addresses during mapping.
56 This results in a more efficient address space usage. Defaults to
57 zero for almost all systems. */
58#ifndef MAP_BASE_ADDR
59#define MAP_BASE_ADDR(l) 0
60#endif
61
9b8a44cd 62
d66e34cd
RM
63#include <endian.h>
64#if BYTE_ORDER == BIG_ENDIAN
65#define byteorder ELFDATA2MSB
66#define byteorder_name "big-endian"
67#elif BYTE_ORDER == LITTLE_ENDIAN
68#define byteorder ELFDATA2LSB
69#define byteorder_name "little-endian"
70#else
71#error "Unknown BYTE_ORDER " BYTE_ORDER
72#define byteorder ELFDATANONE
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. */
79#define ANONFD -1
80#else
d66e34cd 81int _dl_zerofd = -1;
2064087b
RM
82#define ANONFD _dl_zerofd
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
88ELF_PREFERRED_ADDRESS_DATA;
89#endif
90#ifndef ELF_PREFERRED_ADDRESS
91#define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
92#endif
93#ifndef ELF_FIXED_ADDRESS
94#define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
95#endif
96
266180eb 97size_t _dl_pagesize;
d66e34cd 98
0a54e401
UD
99extern const char *_dl_platform;
100extern size_t _dl_platformlen;
d66e34cd 101
40a55d20
UD
102/* This is a fake list to store the RPATH information for static
103 binaries. */
104static struct r_search_path_elem **fake_path_list;
105
12264bd7
UD
106/* List of the hardware capabilities we might end up using. */
107static const struct r_strlenpair *capstr;
108static size_t ncapstr;
109static size_t max_capstrlen;
110
40a55d20 111
32c85e43
UD
112/* This function has no public prototype. */
113extern ssize_t __libc_read (int, void *, size_t);
114
115
706074a5
UD
116/* Local version of `strdup' function. */
117static inline char *
118local_strdup (const char *s)
119{
120 size_t len = strlen (s) + 1;
121 void *new = malloc (len);
122
123 if (new == NULL)
124 return NULL;
125
126 return (char *) memcpy (new, s, len);
127}
128
f787edde
UD
129/* Return copy of argument with all recognized dynamic string tokens
130 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
131 might not be possible to determine the path from which the object
132 belonging to the map is loaded. In this case the path element
133 containing $ORIGIN is left out. */
134static char *
135expand_dynamic_string_token (struct link_map *l, const char *s)
136{
137 /* We make two runs over the string. First we determine how large the
138 resulting string is and then we copy it over. Since this is now
139 frequently executed operation we are looking here not for performance
140 but rather for code size. */
141 const char *st, *sf;
142 size_t cnt = 0;
143 size_t origin_len;
144 size_t total;
145 char *result, *last_elem, *wp;
146
147 st = s;
148 sf = strchr (s, '$');
149 while (sf != NULL)
150 {
151 size_t len = 1;
152
153 if (((strncmp (&sf[1], "ORIGIN", 6) == 0 && (len = 7) != 0)
154 || (strncmp (&sf[1], "PLATFORM", 8) == 0 && (len = 9) != 0))
155 && (s[len] == '\0' || s[len] == '/' || s[len] == ':'))
156 ++cnt;
157
158 st = sf + len;
159 sf = strchr (st, '$');
160 }
161
162 /* If we do not have to replace anything simply copy the string. */
163 if (cnt == 0)
164 return local_strdup (s);
165
166 /* Now we make a guess how many extra characters on top of the length
167 of S we need to represent the result. We know that we have CNT
168 replacements. Each at most can use
169 MAX (strlen (ORIGIN), strlen (_dl_platform))
170 minus 7 (which is the length of "$ORIGIN").
171
172 First get the origin string if it is not available yet. This can
173 only happen for the map of the executable. */
174 if (l->l_origin == NULL)
175 {
176 assert (l->l_name[0] == '\0');
177 l->l_origin = get_origin ();
178 origin_len = l->l_origin ? strlen (l->l_origin) : 0;
179 }
180 else
181 origin_len = l->l_origin == (char *) -1 ? 0 : strlen (l->l_origin);
182
183 total = strlen (s) + cnt * (MAX (origin_len, _dl_platformlen) - 7);
184 result = (char *) malloc (total + 1);
185 if (result == NULL)
186 return NULL;
187
188 /* Now fill the result path. While copying over the string we keep
189 track of the start of the last path element. When we come accross
190 a DST we copy over the value or (if the value is not available)
191 leave the entire path element out. */
192 last_elem = wp = result;
193 do
194 {
195 if (*s == '$')
196 {
197 const char *repl;
198 size_t len;
199
200 if (((strncmp (&s[1], "ORIGIN", 6) == 0 && (len = 7) != 0)
201 || (strncmp (&s[1], "PLATFORM", 8) == 0 && (len = 9) != 0))
202 && (s[len] == '\0' || s[len] == '/' || s[len] == ':'))
203 {
204 if ((repl = len == 7 ? l->l_origin : _dl_platform) != NULL
205 && repl != (const char *) -1)
206 {
207 wp = __stpcpy (wp, repl);
208 s += len;
209 }
210 else
211 {
212 /* We cannot use this path element, the value of the
213 replacement is unknown. */
214 wp = last_elem;
215 s += len;
216 while (*s != '\0' && *s != ':')
217 ++s;
218 }
219 }
220 else
221 /* No SDK we recognize. */
222 *wp++ = *s++;
223 }
224 else if (*s == ':')
225 {
226 *wp++ = *s++;
227 last_elem = wp;
228 }
229 else
230 *wp++ = *s++;
231 }
232 while (*s != '\0');
233
234 *wp = '\0';
235
236 return result;
237}
238
0413b54c
UD
239/* Add `name' to the list of names for a particular shared object.
240 `name' is expected to have been allocated with malloc and will
241 be freed if the shared object already has this name.
242 Returns false if the object already had this name. */
76156ea1 243static void
12264bd7 244internal_function
76156ea1 245add_name_to_object (struct link_map *l, const char *name)
0a54e401 246{
0413b54c
UD
247 struct libname_list *lnp, *lastp;
248 struct libname_list *newname;
76156ea1 249 size_t name_len;
0413b54c
UD
250
251 lastp = NULL;
252 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
253 if (strcmp (name, lnp->name) == 0)
76156ea1 254 return;
0413b54c 255
76156ea1
UD
256 name_len = strlen (name) + 1;
257 newname = malloc (sizeof *newname + name_len);
0413b54c 258 if (newname == NULL)
da832465
UD
259 {
260 /* No more memory. */
261 _dl_signal_error (ENOMEM, name, "cannot allocate name record");
76156ea1 262 return;
da832465 263 }
0413b54c
UD
264 /* The object should have a libname set from _dl_new_object. */
265 assert (lastp != NULL);
266
76156ea1 267 newname->name = memcpy (newname + 1, name, name_len);
0413b54c
UD
268 newname->next = NULL;
269 lastp->next = newname;
0413b54c
UD
270}
271
12264bd7
UD
272/* All known directories in sorted order. */
273static struct r_search_path_elem *all_dirs;
0413b54c 274
12264bd7
UD
275/* Standard search directories. */
276static struct r_search_path_elem **rtld_search_dirs;
0a54e401
UD
277
278static size_t max_dirnamelen;
279
280static inline struct r_search_path_elem **
281fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
b5efde2f 282 const char **trusted, const char *what, const char *where)
0a54e401
UD
283{
284 char *cp;
285 size_t nelems = 0;
286
287 while ((cp = __strsep (&rpath, sep)) != NULL)
288 {
289 struct r_search_path_elem *dirp;
290 size_t len = strlen (cp);
12264bd7 291
af3878df
UD
292 /* `strsep' can pass an empty string. This has to be
293 interpreted as `use the current directory'. */
12264bd7 294 if (len == 0)
af3878df
UD
295 {
296 static char curwd[2];
297 cp = strcpy (curwd, ".");
298 }
12264bd7 299
143e2b96 300 /* Remove trailing slashes (except for "/"). */
0a54e401
UD
301 while (len > 1 && cp[len - 1] == '/')
302 --len;
303
304 /* Make sure we don't use untrusted directories if we run SUID. */
305 if (trusted != NULL)
306 {
307 const char **trun = trusted;
308
b0b67c47 309 /* All trusted directories must be complete names. */
0a54e401
UD
310 if (cp[0] != '/')
311 continue;
312
313 while (*trun != NULL
12264bd7 314 && (memcmp (*trun, cp, len) != 0
b0b67c47
UD
315 || (*trun)[len] != '/'
316 || (*trun)[len + 1] != '\0'))
0a54e401
UD
317 ++trun;
318
319 if (*trun == NULL)
320 /* It's no trusted directory, skip it. */
321 continue;
322 }
323
143e2b96
UD
324 /* Now add one if there is none so far. */
325 if (len > 0 && cp[len - 1] != '/')
0a54e401
UD
326 cp[len++] = '/';
327
328 /* See if this directory is already known. */
329 for (dirp = all_dirs; dirp != NULL; dirp = dirp->next)
12264bd7 330 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
0a54e401
UD
331 break;
332
333 if (dirp != NULL)
334 {
12264bd7 335 /* It is available, see whether it's on our own list. */
0a54e401
UD
336 size_t cnt;
337 for (cnt = 0; cnt < nelems; ++cnt)
338 if (result[cnt] == dirp)
339 break;
340
341 if (cnt == nelems)
342 result[nelems++] = dirp;
343 }
344 else
345 {
12264bd7
UD
346 size_t cnt;
347
0a54e401 348 /* It's a new directory. Create an entry and add it. */
12264bd7
UD
349 dirp = (struct r_search_path_elem *)
350 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status));
0a54e401
UD
351 if (dirp == NULL)
352 _dl_signal_error (ENOMEM, NULL,
353 "cannot create cache for search path");
354
12264bd7 355 dirp->dirname = cp;
0a54e401 356 dirp->dirnamelen = len;
12264bd7
UD
357
358 if (len > max_dirnamelen)
359 max_dirnamelen = len;
360
3996f34b
UD
361 /* We have to make sure all the relative directories are never
362 ignored. The current directory might change and all our
363 saved information would be void. */
12264bd7
UD
364 if (cp[0] != '/')
365 for (cnt = 0; cnt < ncapstr; ++cnt)
366 dirp->status[cnt] = existing;
0a54e401 367 else
12264bd7
UD
368 for (cnt = 0; cnt < ncapstr; ++cnt)
369 dirp->status[cnt] = unknown;
0a54e401 370
b5efde2f
UD
371 dirp->what = what;
372 dirp->where = where;
373
0a54e401
UD
374 dirp->next = all_dirs;
375 all_dirs = dirp;
376
377 /* Put it in the result array. */
378 result[nelems++] = dirp;
379 }
380 }
381
382 /* Terminate the array. */
383 result[nelems] = NULL;
384
385 return result;
386}
387
388
389static struct r_search_path_elem **
12264bd7 390internal_function
f787edde 391decompose_rpath (const char *rpath, size_t additional_room, struct link_map *l)
0a54e401
UD
392{
393 /* Make a copy we can work with. */
f787edde 394 const char *where = l->l_name;
310930c1 395 char *copy;
0a54e401
UD
396 char *cp;
397 struct r_search_path_elem **result;
310930c1
UD
398 size_t nelems;
399
400 /* First see whether we must forget the RPATH from this object. */
b0a01055 401 if (_dl_inhibit_rpath != NULL && !__libc_enable_secure)
310930c1 402 {
b0a01055 403 const char *found = strstr (_dl_inhibit_rpath, where);
310930c1
UD
404 if (found != NULL)
405 {
406 size_t len = strlen (where);
b0a01055 407 if ((found == _dl_inhibit_rpath || found[-1] == ':')
310930c1
UD
408 && (found[len] == '\0' || found[len] == ':'))
409 {
410 /* This object is on the list of objects for which the RPATH
411 must not be used. */
412 result = (struct r_search_path_elem **)
413 malloc ((additional_room + 1) * sizeof (*result));
414 if (result == NULL)
415 _dl_signal_error (ENOMEM, NULL,
416 "cannot create cache for search path");
417 result[0] = NULL;
418
419 return result;
420 }
421 }
422 }
0a54e401 423
f787edde
UD
424 /* Make a writable copy. At the same time expand possible dynamic
425 string tokens. */
426 copy = expand_dynamic_string_token (l, rpath);
427 if (copy == NULL)
428 _dl_signal_error (ENOMEM, NULL, "cannot create RPATH copy");
429
310930c1 430 /* Count the number of necessary elements in the result array. */
310930c1 431 nelems = 0;
0a54e401
UD
432 for (cp = copy; *cp != '\0'; ++cp)
433 if (*cp == ':')
434 ++nelems;
435
436 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
437 limit for the number of necessary entries. */
438 result = (struct r_search_path_elem **) malloc ((nelems + 1
439 + additional_room + 1)
440 * sizeof (*result));
441 if (result == NULL)
442 _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
443
310930c1 444 return fillin_rpath (copy, result, ":", NULL, "RPATH", where);
0a54e401
UD
445}
446
447
448void
d0fc4041 449internal_function
880f421f 450_dl_init_paths (const char *llp)
0a54e401 451{
12264bd7 452 static const char *system_dirs[] =
40a55d20
UD
453 {
454#include "trusted-dirs.h"
455 NULL
456 };
12264bd7
UD
457 const char **strp;
458 struct r_search_path_elem *pelem, **aelem;
459 size_t round_size;
0a54e401
UD
460
461 /* We have in `search_path' the information about the RPATH of the
462 dynamic loader. Now fill in the information about the applications
463 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
464 variable. */
465 struct link_map *l;
466
880f421f 467 /* Number of elements in the library path. */
0a54e401
UD
468 size_t nllp;
469
880f421f 470 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
0a54e401
UD
471 if (llp != NULL && *llp != '\0')
472 {
473 /* Simply count the number of colons. */
474 const char *cp = llp;
475 nllp = 1;
476 while (*cp)
af3878df
UD
477 {
478 if (*cp == ':' || *cp == ';')
479 ++nllp;
480 ++cp;
481 }
0a54e401
UD
482 }
483 else
484 nllp = 0;
485
4317f9e1 486 /* Get the capabilities. */
12264bd7
UD
487 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
488 &ncapstr, &max_capstrlen);
489
490 /* First set up the rest of the default search directory entries. */
491 aelem = rtld_search_dirs = (struct r_search_path_elem **)
492 malloc ((ncapstr + 1) * sizeof (struct r_search_path_elem *));
493
494 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
495 + ncapstr * sizeof (enum r_dir_status))
496 / sizeof (struct r_search_path_elem));
497
498 rtld_search_dirs[0] = (struct r_search_path_elem *)
499 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]) - 1)
500 * round_size * sizeof (struct r_search_path_elem));
501 if (rtld_search_dirs[0] == NULL)
502 _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
503
b0b67c47 504 pelem = all_dirs = rtld_search_dirs[0];
12264bd7
UD
505 for (strp = system_dirs; *strp != NULL; ++strp, pelem += round_size)
506 {
507 size_t cnt;
508
509 *aelem++ = pelem;
510
511 pelem->next = *(strp + 1) == NULL ? NULL : (pelem + round_size);
512
513 pelem->what = "system search path";
514 pelem->where = NULL;
515
516 pelem->dirnamelen = strlen (pelem->dirname = *strp);
517 if (pelem->dirnamelen > max_dirnamelen)
518 max_dirnamelen = pelem->dirnamelen;
519
520 if (pelem->dirname[0] != '/')
521 for (cnt = 0; cnt < ncapstr; ++cnt)
522 pelem->status[cnt] = existing;
523 else
524 for (cnt = 0; cnt < ncapstr; ++cnt)
525 pelem->status[cnt] = unknown;
526 }
527 *aelem = NULL;
4317f9e1 528
0a54e401 529 l = _dl_loaded;
40a55d20 530 if (l != NULL)
0a54e401 531 {
be935610
UD
532 /* We should never get here when initializing in a static application.
533 If this is a dynamically linked application _dl_loaded always
534 points to the main map which is not dlopen()ed. */
535 assert (l->l_type != lt_loaded);
536
537 if (l->l_info[DT_RPATH])
40a55d20
UD
538 {
539 /* Allocate room for the search path and fill in information
540 from RPATH. */
541 l->l_rpath_dirs =
542 decompose_rpath ((const char *)
543 (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr
544 + l->l_info[DT_RPATH]->d_un.d_val),
f787edde 545 nllp, l);
40a55d20
UD
546 }
547 else
548 {
549 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
550 this somehow to prevent we look this up again and again. */
551 if (nllp == 0)
552 l->l_rpath_dirs = (struct r_search_path_elem **) -1l;
553 else
554 {
555 l->l_rpath_dirs = (struct r_search_path_elem **)
556 malloc ((nllp + 1) * sizeof (*l->l_rpath_dirs));
557 if (l->l_rpath_dirs == NULL)
558 _dl_signal_error (ENOMEM, NULL,
559 "cannot create cache for search path");
560 l->l_rpath_dirs[0] = NULL;
561 }
562 }
563
564 /* We don't need to search the list of fake entries which is searched
565 when no dynamic objects were loaded at this time. */
566 fake_path_list = NULL;
567
568 if (nllp > 0)
569 {
12264bd7 570 char *copy = local_strdup (llp);
40a55d20
UD
571
572 /* Decompose the LD_LIBRARY_PATH and fill in the result.
573 First search for the next place to enter elements. */
574 struct r_search_path_elem **result = l->l_rpath_dirs;
575 while (*result != NULL)
576 ++result;
577
578 /* We need to take care that the LD_LIBRARY_PATH environment
579 variable can contain a semicolon. */
580 (void) fillin_rpath (copy, result, ":;",
12264bd7 581 __libc_enable_secure ? system_dirs : NULL,
b5efde2f 582 "LD_LIBRARY_PATH", NULL);
40a55d20 583 }
0a54e401
UD
584 }
585 else
586 {
40a55d20
UD
587 /* This is a statically linked program but we still have to
588 take care for the LD_LIBRARY_PATH environment variable. We
589 use a fake link_map entry. This will only contain the
590 l_rpath_dirs information. */
591
0a54e401 592 if (nllp == 0)
40a55d20 593 fake_path_list = NULL;
0a54e401
UD
594 else
595 {
40a55d20
UD
596 fake_path_list = (struct r_search_path_elem **)
597 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
f41c8091
UD
598 if (fake_path_list == NULL)
599 _dl_signal_error (ENOMEM, NULL,
600 "cannot create cache for search path");
0a54e401 601
40a55d20 602 (void) fillin_rpath (local_strdup (llp), fake_path_list, ":;",
12264bd7 603 __libc_enable_secure ? system_dirs : NULL,
b5efde2f 604 "LD_LIBRARY_PATH", NULL);
40a55d20 605 }
0a54e401 606 }
0a54e401
UD
607}
608
609
ea03559a
RM
610/* Map in the shared object NAME, actually located in REALNAME, and already
611 opened on FD. */
612
f787edde
UD
613#ifndef EXTERNAL_MAP_FROM_FD
614static
615#endif
ea03559a 616struct link_map *
143e2b96 617_dl_map_object_from_fd (const char *name, int fd, char *realname,
ba79d61b 618 struct link_map *loader, int l_type)
ea03559a 619{
622586fb 620 struct link_map *l = NULL;
ea03559a 621
b122c703 622#define LOSE(s) lose (0, (s))
ea03559a
RM
623 void lose (int code, const char *msg)
624 {
266180eb 625 (void) __close (fd);
ba79d61b
RM
626 if (l)
627 {
628 /* Remove the stillborn object from the list and free it. */
629 if (l->l_prev)
630 l->l_prev->l_next = l->l_next;
631 if (l->l_next)
632 l->l_next->l_prev = l->l_prev;
633 free (l);
634 }
635 free (realname);
636 _dl_signal_error (code, name, msg);
ea03559a
RM
637 }
638
266180eb 639 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
b122c703
RM
640 int prot, int fixed, off_t offset)
641 {
266180eb
RM
642 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
643 fixed|MAP_COPY|MAP_FILE,
644 fd, offset);
0413b54c 645 if (mapat == MAP_FAILED)
b122c703
RM
646 lose (errno, "failed to map segment from shared object");
647 return mapat;
648 }
649
266180eb
RM
650 const ElfW(Ehdr) *header;
651 const ElfW(Phdr) *phdr;
652 const ElfW(Phdr) *ph;
8193034b 653 size_t maplength;
b122c703 654 int type;
32c85e43
UD
655 char *readbuf;
656 ssize_t readlength;
d66e34cd
RM
657
658 /* Look again to see if the real name matched another already loaded. */
659 for (l = _dl_loaded; l; l = l->l_next)
660 if (! strcmp (realname, l->l_name))
661 {
662 /* The object is already loaded.
663 Just bump its reference count and return it. */
266180eb 664 __close (fd);
c84142e8
UD
665
666 /* If the name is not in the list of names for this object add
667 it. */
ea03559a 668 free (realname);
0413b54c 669 add_name_to_object (l, name);
d66e34cd
RM
670 ++l->l_opencount;
671 return l;
672 }
673
8193034b
UD
674 /* Print debugging message. */
675 if (_dl_debug_files)
676 _dl_debug_message (1, "file=", name, "; generating link map\n", NULL);
677
32c85e43
UD
678 /* Read the header directly. */
679 readbuf = alloca (_dl_pagesize);
680 readlength = __libc_read (fd, readbuf, _dl_pagesize);
d47aac39 681 if (readlength < (ssize_t) sizeof(*header))
32c85e43
UD
682 lose (errno, "cannot read file data");
683 header = (void *) readbuf;
d66e34cd 684
d66e34cd 685 /* Check the header for basic validity. */
c4b72918
RM
686 if (*(Elf32_Word *) &header->e_ident !=
687#if BYTE_ORDER == LITTLE_ENDIAN
688 ((ELFMAG0 << (EI_MAG0 * 8)) |
689 (ELFMAG1 << (EI_MAG1 * 8)) |
690 (ELFMAG2 << (EI_MAG2 * 8)) |
691 (ELFMAG3 << (EI_MAG3 * 8)))
692#else
693 ((ELFMAG0 << (EI_MAG3 * 8)) |
694 (ELFMAG1 << (EI_MAG2 * 8)) |
695 (ELFMAG2 << (EI_MAG1 * 8)) |
696 (ELFMAG3 << (EI_MAG0 * 8)))
697#endif
698 )
d66e34cd 699 LOSE ("invalid ELF header");
266180eb
RM
700#define ELF32_CLASS ELFCLASS32
701#define ELF64_CLASS ELFCLASS64
702 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
14e9dd67 703 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS) "-bit");
d66e34cd
RM
704 if (header->e_ident[EI_DATA] != byteorder)
705 LOSE ("ELF file data encoding not " byteorder_name);
706 if (header->e_ident[EI_VERSION] != EV_CURRENT)
707 LOSE ("ELF file version ident not " STRING(EV_CURRENT));
f787edde
UD
708 /* XXX We should be able so set system specific versions which are
709 allowed here. */
710 if (header->e_ident[EI_OSABI] != ELFOSABI_SYSV)
711 LOSE ("ELF file OS ABI not " STRING(ELFOSABI_SYSV));
712 if (header->e_ident[EI_ABIVERSION] != 0)
713 LOSE ("ELF file ABI version not 0");
d66e34cd
RM
714 if (header->e_version != EV_CURRENT)
715 LOSE ("ELF file version not " STRING(EV_CURRENT));
716 if (! elf_machine_matches_host (header->e_machine))
717 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME);
266180eb 718 if (header->e_phentsize != sizeof (ElfW(Phdr)))
d66e34cd
RM
719 LOSE ("ELF file's phentsize not the expected size");
720
2064087b
RM
721#ifndef MAP_ANON
722#define MAP_ANON 0
d66e34cd
RM
723 if (_dl_zerofd == -1)
724 {
725 _dl_zerofd = _dl_sysdep_open_zero_fill ();
726 if (_dl_zerofd == -1)
ba79d61b
RM
727 {
728 __close (fd);
729 _dl_signal_error (errno, NULL, "cannot open zero fill device");
730 }
d66e34cd 731 }
2064087b 732#endif
d66e34cd 733
ba79d61b 734 /* Enter the new object in the list of loaded objects. */
be935610 735 l = _dl_new_object (realname, name, l_type, loader);
ba79d61b
RM
736 if (! l)
737 lose (ENOMEM, "cannot create shared object descriptor");
738 l->l_opencount = 1;
ba79d61b 739
b122c703 740 /* Extract the remaining details we need from the ELF header
32c85e43 741 and then read in the program header table. */
b122c703
RM
742 l->l_entry = header->e_entry;
743 type = header->e_type;
744 l->l_phnum = header->e_phnum;
32c85e43
UD
745
746 maplength = header->e_phnum * sizeof (ElfW(Phdr));
747 if (header->e_phoff + maplength <= readlength)
748 phdr = (void *) (readbuf + header->e_phoff);
749 else
750 {
751 phdr = alloca (maplength);
752 __lseek (fd, SEEK_SET, header->e_phoff);
753 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
754 lose (errno, "cannot read file data");
755 }
879bf2e6 756
b122c703
RM
757 {
758 /* Scan the program header table, collecting its load commands. */
759 struct loadcmd
760 {
266180eb 761 ElfW(Addr) mapstart, mapend, dataend, allocend;
b122c703
RM
762 off_t mapoff;
763 int prot;
764 } loadcmds[l->l_phnum], *c;
765 size_t nloadcmds = 0;
d66e34cd 766
d66e34cd 767 l->l_ld = 0;
b122c703
RM
768 l->l_phdr = 0;
769 l->l_addr = 0;
d66e34cd
RM
770 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
771 switch (ph->p_type)
772 {
773 /* These entries tell us where to find things once the file's
774 segments are mapped in. We record the addresses it says
775 verbatim, and later correct for the run-time load address. */
776 case PT_DYNAMIC:
777 l->l_ld = (void *) ph->p_vaddr;
778 break;
779 case PT_PHDR:
780 l->l_phdr = (void *) ph->p_vaddr;
781 break;
782
783 case PT_LOAD:
b122c703
RM
784 /* A load command tells us to map in part of the file.
785 We record the load commands and process them all later. */
266180eb 786 if (ph->p_align % _dl_pagesize != 0)
d66e34cd
RM
787 LOSE ("ELF load command alignment not page-aligned");
788 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
789 LOSE ("ELF load command address/offset not properly aligned");
790 {
b122c703
RM
791 struct loadcmd *c = &loadcmds[nloadcmds++];
792 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
266180eb
RM
793 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
794 & ~(_dl_pagesize - 1));
b122c703
RM
795 c->dataend = ph->p_vaddr + ph->p_filesz;
796 c->allocend = ph->p_vaddr + ph->p_memsz;
797 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
798 c->prot = 0;
d66e34cd 799 if (ph->p_flags & PF_R)
b122c703 800 c->prot |= PROT_READ;
d66e34cd 801 if (ph->p_flags & PF_W)
b122c703 802 c->prot |= PROT_WRITE;
d66e34cd 803 if (ph->p_flags & PF_X)
b122c703
RM
804 c->prot |= PROT_EXEC;
805 break;
806 }
807 }
d66e34cd 808
b122c703
RM
809 /* Now process the load commands and map segments into memory. */
810 c = loadcmds;
811
8193034b
UD
812 /* Length of the sections to be loaded. */
813 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
814
b122c703
RM
815 if (type == ET_DYN || type == ET_REL)
816 {
817 /* This is a position-independent shared object. We can let the
818 kernel map it anywhere it likes, but we must have space for all
819 the segments in their specified positions relative to the first.
820 So we map the first segment without MAP_FIXED, but with its
22930c9b
RM
821 extent increased to cover all the segments. Then we remove
822 access from excess portion, and there is known sufficient space
4cca6b86
UD
823 there to remap from the later segments.
824
825 As a refinement, sometimes we have an address that we would
826 prefer to map such objects at; but this is only a preference,
827 the OS can do whatever it likes. */
b122c703 828 caddr_t mapat;
4cca6b86 829 ElfW(Addr) mappref;
f21acc89
UD
830 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
831 - MAP_BASE_ADDR (l));
4cca6b86 832 mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
266180eb 833 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
b122c703 834
22930c9b
RM
835 /* Change protection on the excess portion to disallow all access;
836 the portions we do not remap later will be inaccessible as if
837 unallocated. Then jump into the normal segment-mapping loop to
838 handle the portion of the segment past the end of the file
839 mapping. */
0d3726c3 840 __mprotect ((caddr_t) (l->l_addr + c->mapend),
266180eb
RM
841 loadcmds[nloadcmds - 1].allocend - c->mapend,
842 0);
052b6a6c
UD
843
844 /* Remember which part of the address space this object uses. */
845 l->l_map_start = c->mapstart + l->l_addr;
846 l->l_map_end = l->l_map_start + maplength;
847
b122c703
RM
848 goto postmap;
849 }
4cca6b86
UD
850 else
851 {
852 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
853 fixed. */
854 ELF_FIXED_ADDRESS (loader, c->mapstart);
855 }
b122c703 856
052b6a6c
UD
857 /* Remember which part of the address space this object uses. */
858 l->l_map_start = c->mapstart + l->l_addr;
859 l->l_map_end = l->l_map_start + maplength;
860
b122c703
RM
861 while (c < &loadcmds[nloadcmds])
862 {
863 if (c->mapend > c->mapstart)
864 /* Map the segment contents from the file. */
865 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
866 c->prot, MAP_FIXED, c->mapoff);
867
868 postmap:
869 if (c->allocend > c->dataend)
870 {
871 /* Extra zero pages should appear at the end of this segment,
872 after the data mapped from the file. */
266180eb 873 ElfW(Addr) zero, zeroend, zeropage;
b122c703
RM
874
875 zero = l->l_addr + c->dataend;
876 zeroend = l->l_addr + c->allocend;
266180eb 877 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
d66e34cd 878
b122c703
RM
879 if (zeroend < zeropage)
880 /* All the extra data is in the last page of the segment.
881 We can just zero it. */
882 zeropage = zeroend;
883
884 if (zeropage > zero)
d66e34cd 885 {
b122c703
RM
886 /* Zero the final part of the last page of the segment. */
887 if ((c->prot & PROT_WRITE) == 0)
d66e34cd 888 {
b122c703 889 /* Dag nab it. */
266180eb
RM
890 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
891 _dl_pagesize, c->prot|PROT_WRITE) < 0)
b122c703 892 lose (errno, "cannot change memory protections");
d66e34cd 893 }
b122c703
RM
894 memset ((void *) zero, 0, zeropage - zero);
895 if ((c->prot & PROT_WRITE) == 0)
266180eb
RM
896 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
897 _dl_pagesize, c->prot);
b122c703 898 }
d66e34cd 899
b122c703
RM
900 if (zeroend > zeropage)
901 {
902 /* Map the remaining zero pages in from the zero fill FD. */
903 caddr_t mapat;
266180eb
RM
904 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
905 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
2064087b 906 ANONFD, 0);
0413b54c 907 if (mapat == MAP_FAILED)
9b8a44cd 908 lose (errno, "cannot map zero-fill pages");
d66e34cd
RM
909 }
910 }
d66e34cd 911
b122c703 912 ++c;
879bf2e6 913 }
0d3726c3
RM
914
915 if (l->l_phdr == 0)
916 {
917 /* There was no PT_PHDR specified. We need to find the phdr in the
918 load image ourselves. We assume it is in fact in the load image
919 somewhere, and that the first load command starts at the
920 beginning of the file and thus contains the ELF file header. */
921 ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart;
922 assert (loadcmds[0].mapoff == 0);
923 l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff);
924 }
925 else
926 /* Adjust the PT_PHDR value by the runtime load address. */
927 (ElfW(Addr)) l->l_phdr += l->l_addr;
b122c703 928 }
d66e34cd 929
6d9756c9
RM
930 /* We are done mapping in the file. We no longer need the descriptor. */
931 __close (fd);
932
ba79d61b
RM
933 if (l->l_type == lt_library && type == ET_EXEC)
934 l->l_type = lt_executable;
9b8a44cd 935
b122c703
RM
936 if (l->l_ld == 0)
937 {
938 if (type == ET_DYN)
939 LOSE ("object file has no dynamic section");
940 }
941 else
266180eb 942 (ElfW(Addr)) l->l_ld += l->l_addr;
879bf2e6 943
463e148b
RM
944 l->l_entry += l->l_addr;
945
8193034b
UD
946 if (_dl_debug_files)
947 {
948 const size_t nibbles = sizeof (void *) * 2;
949 char buf1[nibbles + 1];
950 char buf2[nibbles + 1];
951 char buf3[nibbles + 1];
952
953 buf1[nibbles] = '\0';
954 buf2[nibbles] = '\0';
955 buf3[nibbles] = '\0';
956
957 memset (buf1, '0', nibbles);
958 memset (buf2, '0', nibbles);
959 memset (buf3, '0', nibbles);
960 _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
961 _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
962 _itoa_word (maplength, &buf3[nibbles], 16, 0);
963
964 _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2,
965 " size: 0x", buf3, "\n", NULL);
966 memset (buf1, '0', nibbles);
967 memset (buf2, '0', nibbles);
968 memset (buf3, ' ', nibbles);
969 _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
970 _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
971 _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
972 _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2,
973 " phnum: ", buf3, "\n\n", NULL);
974 }
975
d66e34cd
RM
976 elf_get_dynamic_info (l->l_ld, l->l_info);
977 if (l->l_info[DT_HASH])
978 _dl_setup_hash (l);
979
be935610
UD
980 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
981 have to do this for the main map. */
982 if (l->l_info[DT_SYMBOLIC] && &l->l_searchlist != l->l_scope[0])
983 {
984 /* Create an appropriate searchlist. It contains only this map.
985
986 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
987 GNU ld.so implementation had a different interpretation which
988 is more reasonable. We are prepared to add this possibility
989 back as part of a GNU extension of the ELF format. */
990 l->l_symbolic_searchlist.r_list =
991 (struct link_map **) malloc (sizeof (struct link_map *));
992
993 if (l->l_symbolic_searchlist.r_list == NULL)
994 lose (ENOMEM, "cannot create searchlist");
995
996 l->l_symbolic_searchlist.r_list[0] = l;
997 l->l_symbolic_searchlist.r_nlist = 1;
998 l->l_symbolic_searchlist.r_duplist = l->l_symbolic_searchlist.r_list;
999 l->l_symbolic_searchlist.r_nduplist = 1;
1000
1001 /* Now move the existing entries one back. */
1002 memmove (&l->l_scope[1], &l->l_scope[0],
1003 3 * sizeof (struct r_scope_elem *));
1004
1005 /* Now add the new entry. */
1006 l->l_scope[0] = &l->l_symbolic_searchlist;
1007 }
1008
d66e34cd
RM
1009 return l;
1010}
ba79d61b 1011\f
b5efde2f
UD
1012/* Print search path. */
1013static void
1014print_search_path (struct r_search_path_elem **list,
1015 const char *what, const char *name)
1016{
12264bd7 1017 char buf[max_dirnamelen + max_capstrlen];
b5efde2f
UD
1018 int first = 1;
1019
8193034b 1020 _dl_debug_message (1, " search path=", NULL);
b5efde2f
UD
1021
1022 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1023 {
12264bd7
UD
1024 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1025 size_t cnt;
1026
1027 for (cnt = 0; cnt < ncapstr; ++cnt)
1028 if ((*list)->status[cnt] != nonexisting)
1029 {
1030 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
143e2b96
UD
1031 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1032 cp[0] = '\0';
1033 else
1034 cp[-1] = '\0';
12264bd7
UD
1035 _dl_debug_message (0, first ? "" : ":", buf, NULL);
1036 first = 0;
1037 }
b5efde2f 1038
b5efde2f
UD
1039 ++list;
1040 }
1041
1042 if (name != NULL)
8193034b 1043 _dl_debug_message (0, "\t\t(", what, " from file ",
b5efde2f
UD
1044 name[0] ? name : _dl_argv[0], ")\n", NULL);
1045 else
8193034b 1046 _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
b5efde2f
UD
1047}
1048\f
0a54e401 1049/* Try to open NAME in one of the directories in DIRS.
ba79d61b
RM
1050 Return the fd, or -1. If successful, fill in *REALNAME
1051 with the malloc'd full directory name. */
1052
1053static int
c6222ab9 1054open_path (const char *name, size_t namelen, int preloaded,
0a54e401
UD
1055 struct r_search_path_elem **dirs,
1056 char **realname)
ba79d61b
RM
1057{
1058 char *buf;
0a54e401 1059 int fd = -1;
b5efde2f 1060 const char *current_what = NULL;
ba79d61b 1061
0a54e401 1062 if (dirs == NULL || *dirs == NULL)
ba79d61b 1063 {
c4029823 1064 __set_errno (ENOENT);
ba79d61b
RM
1065 return -1;
1066 }
1067
b0b67c47 1068 buf = __alloca (max_dirnamelen + max_capstrlen + namelen);
ba79d61b
RM
1069 do
1070 {
0a54e401
UD
1071 struct r_search_path_elem *this_dir = *dirs;
1072 size_t buflen = 0;
12264bd7 1073 size_t cnt;
b0b67c47 1074 char *edp;
ba79d61b 1075
b5efde2f
UD
1076 /* If we are debugging the search for libraries print the path
1077 now if it hasn't happened now. */
1078 if (_dl_debug_libs && current_what != this_dir->what)
1079 {
1080 current_what = this_dir->what;
1081 print_search_path (dirs, current_what, this_dir->where);
1082 }
1083
b0b67c47 1084 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
12264bd7 1085 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
fd26970f 1086 {
12264bd7
UD
1087 /* Skip this directory if we know it does not exist. */
1088 if (this_dir->status[cnt] == nonexisting)
1089 continue;
0a54e401 1090
12264bd7 1091 buflen =
b0b67c47 1092 ((char *) __mempcpy (__mempcpy (edp,
12264bd7
UD
1093 capstr[cnt].str, capstr[cnt].len),
1094 name, namelen)
1095 - buf);
1096
1097 /* Print name we try if this is wanted. */
b5efde2f 1098 if (_dl_debug_libs)
8193034b 1099 _dl_debug_message (1, " trying file=", buf, "\n", NULL);
b5efde2f 1100
0a54e401 1101 fd = __open (buf, O_RDONLY);
12264bd7 1102 if (this_dir->status[cnt] == unknown)
0a54e401 1103 if (fd != -1)
12264bd7 1104 this_dir->status[cnt] = existing;
0a54e401
UD
1105 else
1106 {
1107 /* We failed to open machine dependent library. Let's
1108 test whether there is any directory at all. */
1109 struct stat st;
fd26970f 1110
b0b67c47 1111 buf[buflen - namelen - 1] = '\0';
fd26970f 1112
0a614877
UD
1113 if (__xstat (_STAT_VER, buf, &st) != 0
1114 || ! S_ISDIR (st.st_mode))
b0b67c47 1115 /* The directory does not exist or it is no directory. */
12264bd7 1116 this_dir->status[cnt] = nonexisting;
fd26970f 1117 else
12264bd7 1118 this_dir->status[cnt] = existing;
0a54e401 1119 }
fd26970f 1120
c6222ab9
UD
1121 if (fd != -1 && preloaded && __libc_enable_secure)
1122 {
1123 /* This is an extra security effort to make sure nobody can
1124 preload broken shared objects which are in the trusted
1125 directories and so exploit the bugs. */
1126 struct stat st;
1127
1128 if (__fxstat (_STAT_VER, fd, &st) != 0
1129 || (st.st_mode & S_ISUID) == 0)
1130 {
1131 /* The shared object cannot be tested for being SUID
1132 or this bit is not set. In this case we must not
1133 use this object. */
1134 __close (fd);
1135 fd = -1;
1136 /* We simply ignore the file, signal this by setting
1137 the error value which would have been set by `open'. */
1138 errno = ENOENT;
1139 }
1140 }
ba79d61b
RM
1141 }
1142
ba79d61b
RM
1143 if (fd != -1)
1144 {
1145 *realname = malloc (buflen);
c6222ab9 1146 if (*realname != NULL)
ba79d61b
RM
1147 {
1148 memcpy (*realname, buf, buflen);
1149 return fd;
1150 }
1151 else
1152 {
1153 /* No memory for the name, we certainly won't be able
1154 to load and link it. */
1155 __close (fd);
1156 return -1;
1157 }
1158 }
1159 if (errno != ENOENT && errno != EACCES)
1160 /* The file exists and is readable, but something went wrong. */
1161 return -1;
1162 }
0a54e401 1163 while (*++dirs != NULL);
ba79d61b
RM
1164
1165 return -1;
1166}
1167
1168/* Map in the shared object file NAME. */
1169
1170struct link_map *
d0fc4041 1171internal_function
c6222ab9
UD
1172_dl_map_object (struct link_map *loader, const char *name, int preloaded,
1173 int type, int trace_mode)
ba79d61b
RM
1174{
1175 int fd;
1176 char *realname;
14bab8de 1177 char *name_copy;
ba79d61b
RM
1178 struct link_map *l;
1179
1180 /* Look for this name among those already loaded. */
1181 for (l = _dl_loaded; l; l = l->l_next)
f41c8091
UD
1182 {
1183 /* If the requested name matches the soname of a loaded object,
1184 use that object. Elide this check for names that have not
1185 yet been opened. */
1186 if (l->l_opencount <= 0)
1187 continue;
1188 if (!_dl_name_match_p (name, l))
1189 {
1190 const char *soname;
1191
1192 if (l->l_info[DT_SONAME] == NULL)
1193 continue;
1194
1195 soname = (const char *) (l->l_addr
1196 + l->l_info[DT_STRTAB]->d_un.d_ptr
1197 + l->l_info[DT_SONAME]->d_un.d_val);
1198 if (strcmp (name, soname) != 0)
1199 continue;
1200
1201 /* We have a match on a new name -- cache it. */
76156ea1 1202 add_name_to_object (l, soname);
f41c8091
UD
1203 }
1204
1205 /* We have a match -- bump the reference count and return it. */
1206 ++l->l_opencount;
1207 return l;
1208 }
ba79d61b 1209
8193034b
UD
1210 /* Display information if we are debugging. */
1211 if (_dl_debug_files && loader != NULL)
1212 _dl_debug_message (1, "\nfile=", name, "; needed by ",
1213 loader->l_name[0] ? loader->l_name : _dl_argv[0],
1214 "\n", NULL);
1215
ba79d61b
RM
1216 if (strchr (name, '/') == NULL)
1217 {
1218 /* Search for NAME in several places. */
1219
1220 size_t namelen = strlen (name) + 1;
1221
b5efde2f 1222 if (_dl_debug_libs)
8193034b 1223 _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
b5efde2f 1224
ba79d61b 1225 fd = -1;
a23db8e4
RM
1226
1227 /* First try the DT_RPATH of the dependent object that caused NAME
1228 to be loaded. Then that object's dependent, and on up. */
1229 for (l = loader; fd == -1 && l; l = l->l_loader)
ba79d61b 1230 if (l && l->l_info[DT_RPATH])
fd26970f 1231 {
0a54e401
UD
1232 /* Make sure the cache information is available. */
1233 if (l->l_rpath_dirs == NULL)
1234 {
1235 size_t ptrval = (l->l_addr
1236 + l->l_info[DT_STRTAB]->d_un.d_ptr
1237 + l->l_info[DT_RPATH]->d_un.d_val);
1238 l->l_rpath_dirs =
f787edde 1239 decompose_rpath ((const char *) ptrval, 0, l);
0a54e401
UD
1240 }
1241
1242 if (l->l_rpath_dirs != (struct r_search_path_elem **) -1l)
c6222ab9
UD
1243 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs,
1244 &realname);
0a54e401
UD
1245 }
1246
1247 /* If dynamically linked, try the DT_RPATH of the executable itself
1248 and the LD_LIBRARY_PATH environment variable. */
1249 l = _dl_loaded;
143e2b96 1250 if (fd == -1 && l && l->l_type != lt_loaded && l != loader
0a54e401 1251 && l->l_rpath_dirs != (struct r_search_path_elem **) -1l)
c6222ab9 1252 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, &realname);
fd26970f 1253
40a55d20
UD
1254 /* This is used if a static binary uses dynamic loading and there
1255 is a LD_LIBRARY_PATH given. */
1256 if (fd == -1 && fake_path_list != NULL)
c6222ab9 1257 fd = open_path (name, namelen, preloaded, fake_path_list, &realname);
40a55d20 1258
f18edac3
RM
1259 if (fd == -1)
1260 {
1261 /* Check the list of libraries in the file /etc/ld.so.cache,
1262 for compatibility with Linux's ldconfig program. */
1263 extern const char *_dl_load_cache_lookup (const char *name);
1264 const char *cached = _dl_load_cache_lookup (name);
1265 if (cached)
1266 {
1267 fd = __open (cached, O_RDONLY);
1268 if (fd != -1)
1269 {
706074a5
UD
1270 realname = local_strdup (cached);
1271 if (realname == NULL)
f18edac3
RM
1272 {
1273 __close (fd);
1274 fd = -1;
1275 }
1276 }
1277 }
1278 }
0a54e401 1279
a23db8e4 1280 /* Finally, try the default path. */
ba79d61b 1281 if (fd == -1)
c6222ab9 1282 fd = open_path (name, namelen, preloaded, rtld_search_dirs, &realname);
b5efde2f
UD
1283
1284 /* Add another newline when we a tracing the library loading. */
1285 if (_dl_debug_libs)
8193034b 1286 _dl_debug_message (1, "\n", NULL);
ba79d61b
RM
1287 }
1288 else
1289 {
f787edde
UD
1290 /* The path may contain dynamic string tokens. */
1291 realname = (loader
1292 ? expand_dynamic_string_token (loader, name)
1293 : local_strdup (name));
1294 if (realname == NULL)
1295 fd = -1;
1296 else
ba79d61b 1297 {
f787edde
UD
1298 fd = __open (realname, O_RDONLY);
1299 if (fd == -1)
1300 free (realname);
ba79d61b
RM
1301 }
1302 }
1303
1304 if (fd == -1)
46ec036d
UD
1305 {
1306 if (trace_mode)
1307 {
1308 /* We haven't found an appropriate library. But since we
1309 are only interested in the list of libraries this isn't
1310 so severe. Fake an entry with all the information we
1ef32c3d 1311 have. */
fd26970f 1312 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
46ec036d
UD
1313
1314 /* Enter the new object in the list of loaded objects. */
1315 if ((name_copy = local_strdup (name)) == NULL
be935610 1316 || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
46ec036d
UD
1317 _dl_signal_error (ENOMEM, name,
1318 "cannot create shared object descriptor");
1319 /* We use an opencount of 0 as a sign for the faked entry. */
1320 l->l_opencount = 0;
1321 l->l_reserved = 0;
fd26970f
UD
1322 l->l_buckets = &dummy_bucket;
1323 l->l_nbuckets = 1;
1324 l->l_relocated = 1;
1325
1326 return l;
46ec036d
UD
1327 }
1328 else
1329 _dl_signal_error (errno, name, "cannot open shared object file");
1330 }
ba79d61b 1331
76156ea1 1332 return _dl_map_object_from_fd (name, fd, realname, loader, type);
ba79d61b 1333}