Update.
[glibc.git] / elf / dl-load.c
blob9dfa482b62973eb1cc2a1b680e54e7d5e94e9a18
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <ldsodefs.h>
28 #include <sys/mman.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include "dynamic-link.h"
33 #include <stdio-common/_itoa.h>
35 #include <dl-dst.h>
37 /* On some systems, no flag bits are given to specify file mapping. */
38 #ifndef MAP_FILE
39 # define MAP_FILE 0
40 #endif
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
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
63 #include <endian.h>
64 #if BYTE_ORDER == BIG_ENDIAN
65 # define byteorder ELFDATA2MSB
66 #elif BYTE_ORDER == LITTLE_ENDIAN
67 # define byteorder ELFDATA2LSB
68 #else
69 # error "Unknown BYTE_ORDER " BYTE_ORDER
70 # define byteorder ELFDATANONE
71 #endif
73 #define STRING(x) __STRING (x)
75 #ifdef MAP_ANON
76 /* The fd is not examined when using MAP_ANON. */
77 # define ANONFD -1
78 #else
79 int _dl_zerofd = -1;
80 # define ANONFD _dl_zerofd
81 #endif
83 /* Handle situations where we have a preferred location in memory for
84 the shared objects. */
85 #ifdef ELF_PREFERRED_ADDRESS_DATA
86 ELF_PREFERRED_ADDRESS_DATA;
87 #endif
88 #ifndef ELF_PREFERRED_ADDRESS
89 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
90 #endif
91 #ifndef ELF_FIXED_ADDRESS
92 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
93 #endif
95 size_t _dl_pagesize;
97 extern const char *_dl_platform;
98 extern size_t _dl_platformlen;
100 /* This is the decomposed LD_LIBRARY_PATH search path. */
101 static struct r_search_path_struct env_path_list;
103 /* List of the hardware capabilities we might end up using. */
104 static const struct r_strlenpair *capstr;
105 static size_t ncapstr;
106 static size_t max_capstrlen;
108 const unsigned char _dl_pf_to_prot[8] =
110 [0] = PROT_NONE,
111 [PF_R] = PROT_READ,
112 [PF_W] = PROT_WRITE,
113 [PF_R | PF_W] = PROT_READ | PROT_WRITE,
114 [PF_X] = PROT_EXEC,
115 [PF_R | PF_X] = PROT_READ | PROT_EXEC,
116 [PF_W | PF_X] = PROT_WRITE | PROT_EXEC,
117 [PF_R | PF_W | PF_X] = PROT_READ | PROT_WRITE | PROT_EXEC
121 /* Get the generated information about the trusted directories. */
122 #include "trusted-dirs.h"
124 static const char system_dirs[] = SYSTEM_DIRS;
125 static const size_t system_dirs_len[] =
127 SYSTEM_DIRS_LEN
129 #define nsystem_dirs_len \
130 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
133 /* Local version of `strdup' function. */
134 static inline char *
135 local_strdup (const char *s)
137 size_t len = strlen (s) + 1;
138 void *new = malloc (len);
140 if (new == NULL)
141 return NULL;
143 return (char *) memcpy (new, s, len);
147 size_t
148 _dl_dst_count (const char *name, int is_path)
150 const char *const start = name;
151 size_t cnt = 0;
155 size_t len = 1;
157 /* $ORIGIN is not expanded for SUID/GUID programs.
159 Note that it is no bug that the strings in the first two `strncmp'
160 calls are longer than the sequence which is actually tested. */
161 if ((((strncmp (&name[1], "ORIGIN}", 6) == 0
162 && (!__libc_enable_secure
163 || ((name[7] == '\0' || (is_path && name[7] == ':'))
164 && (name == start || (is_path && name[-1] == ':'))))
165 && (len = 7) != 0)
166 || (strncmp (&name[1], "PLATFORM}", 8) == 0 && (len = 9) != 0))
167 && (name[len] == '\0' || name[len] == '/'
168 || (is_path && name[len] == ':')))
169 || (name[1] == '{'
170 && ((strncmp (&name[2], "ORIGIN}", 7) == 0
171 && (!__libc_enable_secure
172 || ((name[9] == '\0' || (is_path && name[9] == ':'))
173 && (name == start || (is_path && name[-1] == ':'))))
174 && (len = 9) != 0)
175 || (strncmp (&name[2], "PLATFORM}", 9) == 0
176 && (len = 11) != 0))))
177 ++cnt;
179 name = strchr (name + len, '$');
181 while (name != NULL);
183 return cnt;
187 char *
188 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
189 int is_path)
191 const char *const start = name;
192 char *last_elem, *wp;
194 /* Now fill the result path. While copying over the string we keep
195 track of the start of the last path element. When we come accross
196 a DST we copy over the value or (if the value is not available)
197 leave the entire path element out. */
198 last_elem = wp = result;
202 if (*name == '$')
204 const char *repl;
205 size_t len;
207 /* Note that it is no bug that the strings in the first two `strncmp'
208 calls are longer than the sequence which is actually tested. */
209 if ((((strncmp (&name[1], "ORIGIN}", 6) == 0 && (len = 7) != 0)
210 || (strncmp (&name[1], "PLATFORM}", 8) == 0 && (len = 9) != 0))
211 && (name[len] == '\0' || name[len] == '/'
212 || (is_path && name[len] == ':')))
213 || (name[1] == '{'
214 && ((strncmp (&name[2], "ORIGIN}", 7) == 0 && (len = 9) != 0)
215 || (strncmp (&name[2], "PLATFORM}", 9) == 0
216 && (len = 11) != 0))))
218 repl = ((len == 7 || name[2] == 'O')
219 ? (__libc_enable_secure
220 && ((name[len] != '\0'
221 && (!is_path || name[len] != ':'))
222 || (name != start
223 && (!is_path || name[-1] != ':')))
224 ? NULL : l->l_origin)
225 : _dl_platform);
227 if (repl != NULL && repl != (const char *) -1)
229 wp = __stpcpy (wp, repl);
230 name += len;
232 else
234 /* We cannot use this path element, the value of the
235 replacement is unknown. */
236 wp = last_elem;
237 name += len;
238 while (*name != '\0' && (!is_path || *name != ':'))
239 ++name;
242 else
243 /* No DST we recognize. */
244 *wp++ = *name++;
246 else if (is_path && *name == ':')
248 *wp++ = *name++;
249 last_elem = wp;
251 else
252 *wp++ = *name++;
254 while (*name != '\0');
256 *wp = '\0';
258 return result;
262 /* Return copy of argument with all recognized dynamic string tokens
263 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
264 might not be possible to determine the path from which the object
265 belonging to the map is loaded. In this case the path element
266 containing $ORIGIN is left out. */
267 static char *
268 expand_dynamic_string_token (struct link_map *l, const char *s)
270 /* We make two runs over the string. First we determine how large the
271 resulting string is and then we copy it over. Since this is now
272 frequently executed operation we are looking here not for performance
273 but rather for code size. */
274 size_t cnt;
275 size_t total;
276 char *result;
278 /* Determine the number of DST elements. */
279 cnt = DL_DST_COUNT (s, 1);
281 /* If we do not have to replace anything simply copy the string. */
282 if (cnt == 0)
283 return local_strdup (s);
285 /* Determine the length of the substituted string. */
286 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
288 /* Allocate the necessary memory. */
289 result = (char *) malloc (total + 1);
290 if (result == NULL)
291 return NULL;
293 return DL_DST_SUBSTITUTE (l, s, result, 1);
297 /* Add `name' to the list of names for a particular shared object.
298 `name' is expected to have been allocated with malloc and will
299 be freed if the shared object already has this name.
300 Returns false if the object already had this name. */
301 static void
302 internal_function
303 add_name_to_object (struct link_map *l, const char *name)
305 struct libname_list *lnp, *lastp;
306 struct libname_list *newname;
307 size_t name_len;
309 lastp = NULL;
310 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
311 if (strcmp (name, lnp->name) == 0)
312 return;
314 name_len = strlen (name) + 1;
315 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
316 if (newname == NULL)
318 /* No more memory. */
319 _dl_signal_error (ENOMEM, name, N_("cannot allocate name record"));
320 return;
322 /* The object should have a libname set from _dl_new_object. */
323 assert (lastp != NULL);
325 newname->name = memcpy (newname + 1, name, name_len);
326 newname->next = NULL;
327 newname->dont_free = 0;
328 lastp->next = newname;
331 /* All known directories in sorted order. */
332 struct r_search_path_elem *_dl_all_dirs;
334 /* All directories after startup. */
335 struct r_search_path_elem *_dl_init_all_dirs;
337 /* Standard search directories. */
338 static struct r_search_path_struct rtld_search_dirs;
340 static size_t max_dirnamelen;
342 static inline struct r_search_path_elem **
343 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
344 int check_trusted, const char *what, const char *where)
346 char *cp;
347 size_t nelems = 0;
349 while ((cp = __strsep (&rpath, sep)) != NULL)
351 struct r_search_path_elem *dirp;
352 size_t len = strlen (cp);
354 /* `strsep' can pass an empty string. This has to be
355 interpreted as `use the current directory'. */
356 if (len == 0)
358 static const char curwd[] = "./";
359 cp = (char *) curwd;
362 /* Remove trailing slashes (except for "/"). */
363 while (len > 1 && cp[len - 1] == '/')
364 --len;
366 /* Now add one if there is none so far. */
367 if (len > 0 && cp[len - 1] != '/')
368 cp[len++] = '/';
370 /* See if this directory is already known. */
371 for (dirp = _dl_all_dirs; dirp != NULL; dirp = dirp->next)
372 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
373 break;
375 if (dirp != NULL)
377 /* It is available, see whether it's on our own list. */
378 size_t cnt;
379 for (cnt = 0; cnt < nelems; ++cnt)
380 if (result[cnt] == dirp)
381 break;
383 if (cnt == nelems)
384 result[nelems++] = dirp;
386 else
388 size_t cnt;
389 enum r_dir_status init_val;
390 size_t where_len = where ? strlen (where) + 1 : 0;
392 /* It's a new directory. Create an entry and add it. */
393 dirp = (struct r_search_path_elem *)
394 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
395 + where_len + len + 1);
396 if (dirp == NULL)
397 _dl_signal_error (ENOMEM, NULL,
398 N_("cannot create cache for search path"));
400 dirp->dirname = ((char *) dirp + sizeof (*dirp)
401 + ncapstr * sizeof (enum r_dir_status));
402 memcpy ((char *) dirp->dirname, cp, len + 1);
403 dirp->dirnamelen = len;
405 if (len > max_dirnamelen)
406 max_dirnamelen = len;
408 /* Make sure we don't use untrusted directories if we run SUID. */
409 if (__builtin_expect (check_trusted, 0))
411 const char *trun = system_dirs;
412 size_t idx;
414 /* By default we don't trust anything. */
415 init_val = nonexisting;
417 /* All trusted directories must be complete names. */
418 if (cp[0] == '/')
420 for (idx = 0; idx < nsystem_dirs_len; ++idx)
422 if (len == system_dirs_len[idx]
423 && memcmp (trun, cp, len) == 0)
424 /* Found it. */
425 break;
427 trun += system_dirs_len[idx] + 1;
430 if (idx < nsystem_dirs_len)
431 /* It's a trusted directory so allow checking for it. */
432 init_val = unknown;
435 else
436 /* We don't have to check for trusted directories and can
437 accept everything. We have to make sure all the
438 relative directories are never ignored. The current
439 directory might change and all our saved information
440 would be void. */
441 init_val = cp[0] != '/' ? existing : unknown;
443 for (cnt = 0; cnt < ncapstr; ++cnt)
444 dirp->status[cnt] = init_val;
446 dirp->what = what;
447 if (__builtin_expect (where != NULL, 1))
448 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
449 + ncapstr * sizeof (enum r_dir_status),
450 where, where_len);
451 else
452 dirp->where = NULL;
454 dirp->next = _dl_all_dirs;
455 _dl_all_dirs = dirp;
457 /* Put it in the result array. */
458 result[nelems++] = dirp;
462 /* Terminate the array. */
463 result[nelems] = NULL;
465 return result;
469 static void
470 internal_function
471 decompose_rpath (struct r_search_path_struct *sps,
472 const char *rpath, struct link_map *l, const char *what)
474 /* Make a copy we can work with. */
475 const char *where = l->l_name;
476 char *copy;
477 char *cp;
478 struct r_search_path_elem **result;
479 size_t nelems;
481 /* First see whether we must forget the RUNPATH and RPATH from this
482 object. */
483 if (__builtin_expect (_dl_inhibit_rpath != NULL, 0) && !__libc_enable_secure)
485 const char *found = strstr (_dl_inhibit_rpath, where);
486 if (found != NULL)
488 size_t len = strlen (where);
489 if ((found == _dl_inhibit_rpath || found[-1] == ':')
490 && (found[len] == '\0' || found[len] == ':'))
492 /* This object is on the list of objects for which the
493 RUNPATH and RPATH must not be used. */
494 result = (struct r_search_path_elem **)
495 malloc (sizeof (*result));
496 if (result == NULL)
497 _dl_signal_error (ENOMEM, NULL,
498 N_("cannot create cache for search path"));
499 result[0] = NULL;
501 sps->dirs = result;
502 sps->malloced = 1;
504 return;
509 /* Make a writable copy. At the same time expand possible dynamic
510 string tokens. */
511 copy = expand_dynamic_string_token (l, rpath);
512 if (copy == NULL)
513 _dl_signal_error (ENOMEM, NULL, N_("cannot create RUNPATH/RPATH copy"));
515 /* Count the number of necessary elements in the result array. */
516 nelems = 0;
517 for (cp = copy; *cp != '\0'; ++cp)
518 if (*cp == ':')
519 ++nelems;
521 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
522 number of necessary entries. */
523 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
524 * sizeof (*result));
525 if (result == NULL)
526 _dl_signal_error (ENOMEM, NULL, N_("cannot create cache for search path"));
528 fillin_rpath (copy, result, ":", 0, what, where);
530 /* Free the copied RPATH string. `fillin_rpath' make own copies if
531 necessary. */
532 free (copy);
534 sps->dirs = result;
535 /* The caller will change this value if we haven't used a real malloc. */
536 sps->malloced = 1;
540 void
541 internal_function
542 _dl_init_paths (const char *llp)
544 size_t idx;
545 const char *strp;
546 struct r_search_path_elem *pelem, **aelem;
547 size_t round_size;
548 #ifdef SHARED
549 struct link_map *l;
550 #endif
552 /* Fill in the information about the application's RPATH and the
553 directories addressed by the LD_LIBRARY_PATH environment variable. */
555 /* Get the capabilities. */
556 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
557 &ncapstr, &max_capstrlen);
559 /* First set up the rest of the default search directory entries. */
560 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
561 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
562 if (rtld_search_dirs.dirs == NULL)
563 _dl_signal_error (ENOMEM, NULL, N_("cannot create search path array"));
565 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
566 + ncapstr * sizeof (enum r_dir_status))
567 / sizeof (struct r_search_path_elem));
569 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
570 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
571 * round_size * sizeof (struct r_search_path_elem));
572 if (rtld_search_dirs.dirs[0] == NULL)
573 _dl_signal_error (ENOMEM, NULL, N_("cannot create cache for search path"));
575 rtld_search_dirs.malloced = 0;
576 pelem = _dl_all_dirs = rtld_search_dirs.dirs[0];
577 strp = system_dirs;
578 idx = 0;
582 size_t cnt;
584 *aelem++ = pelem;
586 pelem->what = "system search path";
587 pelem->where = NULL;
589 pelem->dirname = strp;
590 pelem->dirnamelen = system_dirs_len[idx];
591 strp += system_dirs_len[idx] + 1;
593 /* System paths must be absolute. */
594 assert (pelem->dirname[0] == '/');
595 for (cnt = 0; cnt < ncapstr; ++cnt)
596 pelem->status[cnt] = unknown;
598 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
600 pelem += round_size;
602 while (idx < nsystem_dirs_len);
604 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
605 *aelem = NULL;
607 #ifdef SHARED
608 /* This points to the map of the main object. */
609 l = _dl_loaded;
610 if (l != NULL)
612 assert (l->l_type != lt_loaded);
614 if (l->l_info[DT_RUNPATH])
616 /* Allocate room for the search path and fill in information
617 from RUNPATH. */
618 decompose_rpath (&l->l_runpath_dirs,
619 (const void *) (D_PTR (l, l_info[DT_STRTAB])
620 + l->l_info[DT_RUNPATH]->d_un.d_val),
621 l, "RUNPATH");
623 /* The RPATH is ignored. */
624 l->l_rpath_dirs.dirs = (void *) -1;
626 else
628 l->l_runpath_dirs.dirs = (void *) -1;
630 if (l->l_info[DT_RPATH])
632 /* Allocate room for the search path and fill in information
633 from RPATH. */
634 decompose_rpath (&l->l_rpath_dirs,
635 (const void *) (D_PTR (l, l_info[DT_STRTAB])
636 + l->l_info[DT_RPATH]->d_un.d_val),
637 l, "RPATH");
638 l->l_rpath_dirs.malloced = 0;
640 else
641 l->l_rpath_dirs.dirs = (void *) -1;
644 #endif /* SHARED */
646 if (llp != NULL && *llp != '\0')
648 size_t nllp;
649 const char *cp = llp;
651 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
652 elements it has. */
653 nllp = 1;
654 while (*cp)
656 if (*cp == ':' || *cp == ';')
657 ++nllp;
658 ++cp;
661 env_path_list.dirs = (struct r_search_path_elem **)
662 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
663 if (env_path_list.dirs == NULL)
664 _dl_signal_error (ENOMEM, NULL,
665 N_("cannot create cache for search path"));
667 (void) fillin_rpath (strdupa (llp), env_path_list.dirs, ":;",
668 __libc_enable_secure, "LD_LIBRARY_PATH", NULL);
670 if (env_path_list.dirs[0] == NULL)
672 free (env_path_list.dirs);
673 env_path_list.dirs = (void *) -1;
676 env_path_list.malloced = 0;
678 else
679 env_path_list.dirs = (void *) -1;
681 /* Remember the last search directory added at startup. */
682 _dl_init_all_dirs = _dl_all_dirs;
686 /* Think twice before changing anything in this function. It is placed
687 here and prepared using the `alloca' magic to prevent it from being
688 inlined. The function is only called in case of an error. But then
689 performance does not count. The function used to be "inlinable" and
690 the compiled did so all the time. This increased the code size for
691 absolutely no good reason. */
692 #define LOSE(code, s) lose (code, fd, name, realname, l, s)
693 static void
694 __attribute__ ((noreturn))
695 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
696 const char *msg)
698 /* The use of `alloca' here looks ridiculous but it helps. The goal
699 is to avoid the function from being inlined. There is no official
700 way to do this so we use this trick. gcc never inlines functions
701 which use `alloca'. */
702 int *a = alloca (sizeof (int));
703 a[0] = fd;
704 (void) __close (a[0]);
705 if (l != NULL)
707 /* Remove the stillborn object from the list and free it. */
708 if (l->l_prev)
709 l->l_prev->l_next = l->l_next;
710 if (l->l_next)
711 l->l_next->l_prev = l->l_prev;
712 free (l);
714 free (realname);
715 _dl_signal_error (code, name, msg);
719 /* Map in the shared object NAME, actually located in REALNAME, and already
720 opened on FD. */
722 #ifndef EXTERNAL_MAP_FROM_FD
723 static
724 #endif
725 struct link_map *
726 _dl_map_object_from_fd (const char *name, int fd, char *realname,
727 struct link_map *loader, int l_type, int mode)
729 /* This is the expected ELF header. */
730 #define ELF32_CLASS ELFCLASS32
731 #define ELF64_CLASS ELFCLASS64
732 #ifndef VALID_ELF_HEADER
733 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
734 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
735 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
736 #endif
737 static const unsigned char expected[EI_PAD] =
739 [EI_MAG0] = ELFMAG0,
740 [EI_MAG1] = ELFMAG1,
741 [EI_MAG2] = ELFMAG2,
742 [EI_MAG3] = ELFMAG3,
743 [EI_CLASS] = ELFW(CLASS),
744 [EI_DATA] = byteorder,
745 [EI_VERSION] = EV_CURRENT,
746 [EI_OSABI] = ELFOSABI_SYSV,
747 [EI_ABIVERSION] = 0
749 struct link_map *l = NULL;
751 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
752 int prot, int fixed, off_t offset)
754 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
755 fixed|MAP_COPY|MAP_FILE,
756 fd, offset);
757 if (mapat == MAP_FAILED)
758 LOSE (errno, N_("failed to map segment from shared object"));
759 return mapat;
762 const ElfW(Ehdr) *header;
763 const ElfW(Phdr) *phdr;
764 const ElfW(Phdr) *ph;
765 size_t maplength;
766 int type;
767 char *readbuf;
768 ssize_t readlength;
769 struct stat64 st;
771 /* Get file information. */
772 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
773 LOSE (errno, N_("cannot stat shared object"));
775 /* Look again to see if the real name matched another already loaded. */
776 for (l = _dl_loaded; l; l = l->l_next)
777 if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
779 /* The object is already loaded.
780 Just bump its reference count and return it. */
781 __close (fd);
783 /* If the name is not in the list of names for this object add
784 it. */
785 free (realname);
786 add_name_to_object (l, name);
787 ++l->l_opencount;
788 return l;
791 if (mode & RTLD_NOLOAD)
792 /* We are not supposed to load the object unless it is already
793 loaded. So return now. */
794 return NULL;
796 /* Print debugging message. */
797 if (__builtin_expect (_dl_debug_files, 0))
798 _dl_debug_message (1, "file=", name, "; generating link map\n", NULL);
800 /* Read the header directly. */
801 readbuf = alloca (_dl_pagesize);
802 readlength = __libc_read (fd, readbuf, _dl_pagesize);
803 if (readlength < (ssize_t) sizeof (*header))
804 LOSE (errno, N_("cannot read file data"));
805 header = (void *) readbuf;
807 /* Check the header for basic validity. */
808 if (__builtin_expect (!VALID_ELF_HEADER (header->e_ident, expected, EI_PAD),
811 /* Something is wrong. */
812 if (*(Elf32_Word *) &header->e_ident !=
813 #if BYTE_ORDER == LITTLE_ENDIAN
814 ((ELFMAG0 << (EI_MAG0 * 8)) |
815 (ELFMAG1 << (EI_MAG1 * 8)) |
816 (ELFMAG2 << (EI_MAG2 * 8)) |
817 (ELFMAG3 << (EI_MAG3 * 8)))
818 #else
819 ((ELFMAG0 << (EI_MAG3 * 8)) |
820 (ELFMAG1 << (EI_MAG2 * 8)) |
821 (ELFMAG2 << (EI_MAG1 * 8)) |
822 (ELFMAG3 << (EI_MAG0 * 8)))
823 #endif
825 LOSE (0, N_("invalid ELF header"));
826 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
828 if (__ELF_NATIVE_CLASS == 32)
829 LOSE (0, N_("ELF file class not 32-bit"));
830 else
831 LOSE (0, N_("ELF file class not 64-bit"));
833 if (header->e_ident[EI_DATA] != byteorder)
835 if (BYTE_ORDER == BIG_ENDIAN)
836 LOSE (0, "ELF file data encoding not big-endian");
837 else
838 LOSE (0, "ELF file data encoding not little-endian");
840 if (header->e_ident[EI_VERSION] != EV_CURRENT)
841 LOSE (0, N_("ELF file version ident does not match current one"));
842 /* XXX We should be able so set system specific versions which are
843 allowed here. */
844 if (!VALID_ELF_OSABI (header->e_ident[EI_OSABI]))
845 LOSE (0, N_("ELF file OS ABI invalid."));
846 if (!VALID_ELF_ABIVERSION (header->e_ident[EI_ABIVERSION]))
847 LOSE (0, N_("ELF file ABI version invalid."));
848 LOSE (0, N_("internal error"));
851 if (__builtin_expect (header->e_version, EV_CURRENT) != EV_CURRENT)
852 LOSE (0, N_("ELF file version does not not match current one"));
853 if (! __builtin_expect (elf_machine_matches_host (header->e_machine), 1))
854 LOSE (0, N_("ELF file machine architecture does not match"));
855 if (__builtin_expect (header->e_phentsize, sizeof (ElfW(Phdr)))
856 != sizeof (ElfW(Phdr)))
857 LOSE (0, N_("ELF file's phentsize not the expected size"));
859 #ifndef MAP_ANON
860 # define MAP_ANON 0
861 if (_dl_zerofd == -1)
863 _dl_zerofd = _dl_sysdep_open_zero_fill ();
864 if (_dl_zerofd == -1)
866 __close (fd);
867 _dl_signal_error (errno, NULL, N_("cannot open zero fill device"));
870 #endif
872 /* Enter the new object in the list of loaded objects. */
873 l = _dl_new_object (realname, name, l_type, loader);
874 if (__builtin_expect (! l, 0))
875 LOSE (ENOMEM, N_("cannot create shared object descriptor"));
876 l->l_opencount = 1;
878 /* Extract the remaining details we need from the ELF header
879 and then read in the program header table. */
880 l->l_entry = header->e_entry;
881 type = header->e_type;
882 l->l_phnum = header->e_phnum;
884 maplength = header->e_phnum * sizeof (ElfW(Phdr));
885 if (header->e_phoff + maplength <= readlength)
886 phdr = (void *) (readbuf + header->e_phoff);
887 else
889 phdr = alloca (maplength);
890 __lseek (fd, SEEK_SET, header->e_phoff);
891 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
892 LOSE (errno, N_("cannot read file data"));
896 /* Scan the program header table, collecting its load commands. */
897 struct loadcmd
899 ElfW(Addr) mapstart, mapend, dataend, allocend;
900 off_t mapoff;
901 int prot;
902 } loadcmds[l->l_phnum], *c;
903 size_t nloadcmds = 0;
905 /* The struct is initialized to zero so this is not necessary:
906 l->l_ld = 0;
907 l->l_phdr = 0;
908 l->l_addr = 0; */
909 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
910 switch (ph->p_type)
912 /* These entries tell us where to find things once the file's
913 segments are mapped in. We record the addresses it says
914 verbatim, and later correct for the run-time load address. */
915 case PT_DYNAMIC:
916 l->l_ld = (void *) ph->p_vaddr;
917 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
918 break;
919 case PT_PHDR:
920 l->l_phdr = (void *) ph->p_vaddr;
921 break;
923 case PT_LOAD:
924 /* A load command tells us to map in part of the file.
925 We record the load commands and process them all later. */
926 if (ph->p_align % _dl_pagesize != 0)
927 LOSE (0, N_("ELF load command alignment not page-aligned"));
928 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
929 LOSE (0,
930 N_("ELF load command address/offset not properly aligned"));
932 struct loadcmd *c = &loadcmds[nloadcmds++];
933 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
934 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
935 & ~(_dl_pagesize - 1));
936 c->dataend = ph->p_vaddr + ph->p_filesz;
937 c->allocend = ph->p_vaddr + ph->p_memsz;
938 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
940 /* Optimize a common case. */
941 if ((PF_R | PF_W | PF_X) == 7
942 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
943 c->prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_W | PF_X)];
944 else
946 c->prot = 0;
947 if (ph->p_flags & PF_R)
948 c->prot |= PROT_READ;
949 if (ph->p_flags & PF_W)
950 c->prot |= PROT_WRITE;
951 if (ph->p_flags & PF_X)
952 c->prot |= PROT_EXEC;
954 break;
958 /* Now process the load commands and map segments into memory. */
959 c = loadcmds;
961 /* Length of the sections to be loaded. */
962 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
964 if (type == ET_DYN || type == ET_REL)
966 /* This is a position-independent shared object. We can let the
967 kernel map it anywhere it likes, but we must have space for all
968 the segments in their specified positions relative to the first.
969 So we map the first segment without MAP_FIXED, but with its
970 extent increased to cover all the segments. Then we remove
971 access from excess portion, and there is known sufficient space
972 there to remap from the later segments.
974 As a refinement, sometimes we have an address that we would
975 prefer to map such objects at; but this is only a preference,
976 the OS can do whatever it likes. */
977 ElfW(Addr) mappref;
978 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
979 - MAP_BASE_ADDR (l));
981 /* Remember which part of the address space this object uses. */
982 l->l_map_start = (ElfW(Addr)) map_segment (mappref, maplength, c->prot,
983 0, c->mapoff);
984 l->l_map_end = l->l_map_start + maplength;
985 l->l_addr = l->l_map_start - c->mapstart;
987 /* Change protection on the excess portion to disallow all access;
988 the portions we do not remap later will be inaccessible as if
989 unallocated. Then jump into the normal segment-mapping loop to
990 handle the portion of the segment past the end of the file
991 mapping. */
992 __mprotect ((caddr_t) (l->l_addr + c->mapend),
993 loadcmds[nloadcmds - 1].allocend - c->mapend,
996 goto postmap;
998 else
1000 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
1001 fixed. */
1002 ELF_FIXED_ADDRESS (loader, c->mapstart);
1005 /* Remember which part of the address space this object uses. */
1006 l->l_map_start = c->mapstart + l->l_addr;
1007 l->l_map_end = l->l_map_start + maplength;
1009 while (c < &loadcmds[nloadcmds])
1011 if (c->mapend > c->mapstart)
1012 /* Map the segment contents from the file. */
1013 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
1014 c->prot, MAP_FIXED, c->mapoff);
1016 postmap:
1017 if (l->l_phdr == 0
1018 && c->mapoff <= header->e_phoff
1019 && (c->mapend - c->mapstart + c->mapoff
1020 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1021 /* Found the program header in this segment. */
1022 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
1024 if (c->allocend > c->dataend)
1026 /* Extra zero pages should appear at the end of this segment,
1027 after the data mapped from the file. */
1028 ElfW(Addr) zero, zeroend, zeropage;
1030 zero = l->l_addr + c->dataend;
1031 zeroend = l->l_addr + c->allocend;
1032 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
1034 if (zeroend < zeropage)
1035 /* All the extra data is in the last page of the segment.
1036 We can just zero it. */
1037 zeropage = zeroend;
1039 if (zeropage > zero)
1041 /* Zero the final part of the last page of the segment. */
1042 if ((c->prot & PROT_WRITE) == 0)
1044 /* Dag nab it. */
1045 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1046 _dl_pagesize, c->prot|PROT_WRITE) < 0)
1047 LOSE (errno, N_("cannot change memory protections"));
1049 memset ((void *) zero, 0, zeropage - zero);
1050 if ((c->prot & PROT_WRITE) == 0)
1051 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1052 _dl_pagesize, c->prot);
1055 if (zeroend > zeropage)
1057 /* Map the remaining zero pages in from the zero fill FD. */
1058 caddr_t mapat;
1059 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1060 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1061 ANONFD, 0);
1062 if (mapat == MAP_FAILED)
1063 LOSE (errno, N_("cannot map zero-fill pages"));
1067 ++c;
1070 if (l->l_phdr == NULL)
1072 /* The program header is not contained in any of the segmenst.
1073 We have to allocate memory ourself and copy it over from
1074 out temporary place. */
1075 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1076 * sizeof (ElfW(Phdr)));
1077 if (newp == NULL)
1078 LOSE (ENOMEM, N_("cannot allocate memory for program header"));
1080 l->l_phdr = memcpy (newp, phdr,
1081 (header->e_phnum * sizeof (ElfW(Phdr))));
1082 l->l_phdr_allocated = 1;
1084 else
1085 /* Adjust the PT_PHDR value by the runtime load address. */
1086 (ElfW(Addr)) l->l_phdr += l->l_addr;
1089 /* We are done mapping in the file. We no longer need the descriptor. */
1090 __close (fd);
1092 if (l->l_type == lt_library && type == ET_EXEC)
1093 l->l_type = lt_executable;
1095 if (l->l_ld == 0)
1097 if (type == ET_DYN)
1098 LOSE (0, N_("object file has no dynamic section"));
1100 else
1101 (ElfW(Addr)) l->l_ld += l->l_addr;
1103 l->l_entry += l->l_addr;
1105 if (__builtin_expect (_dl_debug_files, 0))
1107 const size_t nibbles = sizeof (void *) * 2;
1108 char buf1[nibbles + 1];
1109 char buf2[nibbles + 1];
1110 char buf3[nibbles + 1];
1112 buf1[nibbles] = '\0';
1113 buf2[nibbles] = '\0';
1114 buf3[nibbles] = '\0';
1116 memset (buf1, '0', nibbles);
1117 memset (buf2, '0', nibbles);
1118 memset (buf3, '0', nibbles);
1119 _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
1120 _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
1121 _itoa_word (maplength, &buf3[nibbles], 16, 0);
1123 _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2,
1124 " size: 0x", buf3, "\n", NULL);
1125 memset (buf1, '0', nibbles);
1126 memset (buf2, '0', nibbles);
1127 memset (buf3, ' ', nibbles);
1128 _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
1129 _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
1130 _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
1131 _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2,
1132 " phnum: ", buf3, "\n\n", NULL);
1135 elf_get_dynamic_info (l);
1137 /* Make sure we are dlopen()ing an object which has the DF_1_NOOPEN
1138 flag set. */
1139 if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1140 && (mode & __RTLD_DLOPEN))
1142 /* Remove from the module list. */
1143 assert (l->l_next == NULL);
1144 #ifndef SHARED
1145 if (l->l_prev == NULL)
1146 /* No other module loaded. */
1147 _dl_loaded = NULL;
1148 else
1149 #endif
1150 l->l_prev->l_next = NULL;
1151 --_dl_nloaded;
1153 /* We are not supposed to load this object. Free all resources. */
1154 __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1156 free (l->l_libname);
1158 if (l->l_phdr_allocated)
1159 free ((void *) l->l_phdr);
1161 free (l);
1163 _dl_signal_error (0, name, N_("shared object cannot be dlopen()ed"));
1166 if (l->l_info[DT_HASH])
1167 _dl_setup_hash (l);
1169 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1170 have to do this for the main map. */
1171 if (l->l_info[DT_SYMBOLIC] && &l->l_searchlist != l->l_scope[0])
1173 /* Create an appropriate searchlist. It contains only this map.
1175 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1176 GNU ld.so implementation had a different interpretation which
1177 is more reasonable. We are prepared to add this possibility
1178 back as part of a GNU extension of the ELF format. */
1179 l->l_symbolic_searchlist.r_list =
1180 (struct link_map **) malloc (sizeof (struct link_map *));
1182 if (l->l_symbolic_searchlist.r_list == NULL)
1183 LOSE (ENOMEM, N_("cannot create searchlist"));
1185 l->l_symbolic_searchlist.r_list[0] = l;
1186 l->l_symbolic_searchlist.r_nlist = 1;
1187 l->l_symbolic_searchlist.r_duplist = l->l_symbolic_searchlist.r_list;
1188 l->l_symbolic_searchlist.r_nduplist = 1;
1190 /* Now move the existing entries one back. */
1191 memmove (&l->l_scope[1], &l->l_scope[0],
1192 sizeof (l->l_scope) - sizeof (l->l_scope[0]));
1194 /* Now add the new entry. */
1195 l->l_scope[0] = &l->l_symbolic_searchlist;
1198 /* Finally the file information. */
1199 l->l_dev = st.st_dev;
1200 l->l_ino = st.st_ino;
1202 return l;
1205 /* Print search path. */
1206 static void
1207 print_search_path (struct r_search_path_elem **list,
1208 const char *what, const char *name)
1210 char buf[max_dirnamelen + max_capstrlen];
1211 int first = 1;
1213 _dl_debug_message (1, " search path=", NULL);
1215 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1217 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1218 size_t cnt;
1220 for (cnt = 0; cnt < ncapstr; ++cnt)
1221 if ((*list)->status[cnt] != nonexisting)
1223 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1224 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1225 cp[0] = '\0';
1226 else
1227 cp[-1] = '\0';
1228 _dl_debug_message (0, first ? "" : ":", buf, NULL);
1229 first = 0;
1232 ++list;
1235 if (name != NULL)
1236 _dl_debug_message (0, "\t\t(", what, " from file ",
1237 name[0] ? name : _dl_argv[0], ")\n", NULL);
1238 else
1239 _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
1242 /* Try to open NAME in one of the directories in *DIRSP.
1243 Return the fd, or -1. If successful, fill in *REALNAME
1244 with the malloc'd full directory name. If it turns out
1245 that none of the directories in *DIRSP exists, *DIRSP is
1246 replaced with (void *) -1, and the old value is free()d
1247 if MAY_FREE_DIRS is true. */
1249 static int
1250 open_path (const char *name, size_t namelen, int preloaded,
1251 struct r_search_path_struct *sps, char **realname)
1253 struct r_search_path_elem **dirs = sps->dirs;
1254 char *buf;
1255 int fd = -1;
1256 const char *current_what = NULL;
1257 int any = 0;
1259 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1262 struct r_search_path_elem *this_dir = *dirs;
1263 size_t buflen = 0;
1264 size_t cnt;
1265 char *edp;
1267 /* If we are debugging the search for libraries print the path
1268 now if it hasn't happened now. */
1269 if (__builtin_expect (_dl_debug_libs, 0)
1270 && current_what != this_dir->what)
1272 current_what = this_dir->what;
1273 print_search_path (dirs, current_what, this_dir->where);
1276 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1277 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1279 /* Skip this directory if we know it does not exist. */
1280 if (this_dir->status[cnt] == nonexisting)
1281 continue;
1283 buflen =
1284 ((char *) __mempcpy (__mempcpy (edp,
1285 capstr[cnt].str, capstr[cnt].len),
1286 name, namelen)
1287 - buf);
1289 /* Print name we try if this is wanted. */
1290 if (__builtin_expect (_dl_debug_libs, 0))
1291 _dl_debug_message (1, " trying file=", buf, "\n", NULL);
1293 fd = __open (buf, O_RDONLY);
1294 if (this_dir->status[cnt] == unknown)
1296 if (fd != -1)
1297 this_dir->status[cnt] = existing;
1298 else
1300 /* We failed to open machine dependent library. Let's
1301 test whether there is any directory at all. */
1302 struct stat64 st;
1304 buf[buflen - namelen - 1] = '\0';
1306 if (__xstat64 (_STAT_VER, buf, &st) != 0
1307 || ! S_ISDIR (st.st_mode))
1308 /* The directory does not exist or it is no directory. */
1309 this_dir->status[cnt] = nonexisting;
1310 else
1311 this_dir->status[cnt] = existing;
1315 /* Remember whether we found any existing directory. */
1316 any |= this_dir->status[cnt] == existing;
1318 if (fd != -1 && preloaded && __libc_enable_secure)
1320 /* This is an extra security effort to make sure nobody can
1321 preload broken shared objects which are in the trusted
1322 directories and so exploit the bugs. */
1323 struct stat64 st;
1325 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1326 || (st.st_mode & S_ISUID) == 0)
1328 /* The shared object cannot be tested for being SUID
1329 or this bit is not set. In this case we must not
1330 use this object. */
1331 __close (fd);
1332 fd = -1;
1333 /* We simply ignore the file, signal this by setting
1334 the error value which would have been set by `open'. */
1335 errno = ENOENT;
1340 if (fd != -1)
1342 *realname = malloc (buflen);
1343 if (*realname != NULL)
1345 memcpy (*realname, buf, buflen);
1346 return fd;
1348 else
1350 /* No memory for the name, we certainly won't be able
1351 to load and link it. */
1352 __close (fd);
1353 return -1;
1356 if (errno != ENOENT && errno != EACCES)
1357 /* The file exists and is readable, but something went wrong. */
1358 return -1;
1360 while (*++dirs != NULL);
1362 /* Remove the whole path if none of the directories exists. */
1363 if (! any)
1365 /* Paths which were allocated using the minimal malloc() in ld.so
1366 must not be freed using the general free() in libc. */
1367 if (sps->malloced)
1368 free (sps->dirs);
1369 sps->dirs = (void *) -1;
1372 return -1;
1375 /* Map in the shared object file NAME. */
1377 struct link_map *
1378 internal_function
1379 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1380 int type, int trace_mode, int mode)
1382 int fd;
1383 char *realname;
1384 char *name_copy;
1385 struct link_map *l;
1387 /* Look for this name among those already loaded. */
1388 for (l = _dl_loaded; l; l = l->l_next)
1390 /* If the requested name matches the soname of a loaded object,
1391 use that object. Elide this check for names that have not
1392 yet been opened. */
1393 if (l->l_opencount <= 0)
1394 continue;
1395 if (!_dl_name_match_p (name, l))
1397 const char *soname;
1399 if (l->l_info[DT_SONAME] == NULL)
1400 continue;
1402 soname = (const void *) (D_PTR (l, l_info[DT_STRTAB])
1403 + l->l_info[DT_SONAME]->d_un.d_val);
1404 if (strcmp (name, soname) != 0)
1405 continue;
1407 /* We have a match on a new name -- cache it. */
1408 add_name_to_object (l, soname);
1411 /* We have a match -- bump the reference count and return it. */
1412 ++l->l_opencount;
1413 return l;
1416 /* Display information if we are debugging. */
1417 if (__builtin_expect (_dl_debug_files, 0) && loader != NULL)
1418 _dl_debug_message (1, "\nfile=", name, "; needed by ",
1419 loader->l_name[0] ? loader->l_name : _dl_argv[0],
1420 "\n", NULL);
1422 if (strchr (name, '/') == NULL)
1424 /* Search for NAME in several places. */
1426 size_t namelen = strlen (name) + 1;
1428 if (__builtin_expect (_dl_debug_libs, 0))
1429 _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
1431 fd = -1;
1433 /* When the object has the RUNPATH information we don't use any
1434 RPATHs. */
1435 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1437 /* First try the DT_RPATH of the dependent object that caused NAME
1438 to be loaded. Then that object's dependent, and on up. */
1439 for (l = loader; fd == -1 && l; l = l->l_loader)
1441 if (l->l_rpath_dirs.dirs == NULL)
1443 if (l->l_info[DT_RPATH] == NULL)
1444 /* There is no path. */
1445 l->l_rpath_dirs.dirs = (void *) -1;
1446 else
1448 /* Make sure the cache information is available. */
1449 size_t ptrval = (D_PTR (l, l_info[DT_STRTAB])
1450 + l->l_info[DT_RPATH]->d_un.d_val);
1451 decompose_rpath (&l->l_rpath_dirs,
1452 (const char *) ptrval, l, "RPATH");
1454 if (l->l_rpath_dirs.dirs != (void *) -1)
1455 fd = open_path (name, namelen, preloaded,
1456 &l->l_rpath_dirs, &realname);
1459 else if (l->l_rpath_dirs.dirs != (void *) -1)
1460 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1461 &realname);
1464 /* If dynamically linked, try the DT_RPATH of the executable
1465 itself. */
1466 l = _dl_loaded;
1467 if (fd == -1 && l && l->l_type != lt_loaded && l != loader
1468 && l->l_rpath_dirs.dirs != (void *) -1)
1469 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1470 &realname);
1473 /* Try the LD_LIBRARY_PATH environment variable. */
1474 if (fd == -1 && env_path_list.dirs != (void *) -1)
1475 fd = open_path (name, namelen, preloaded, &env_path_list,
1476 &realname);
1478 /* Look at the RUNPATH informaiton for this binary. */
1479 if (loader != NULL && loader->l_runpath_dirs.dirs != (void *) -1)
1481 if (loader->l_runpath_dirs.dirs == NULL)
1483 if (loader->l_info[DT_RUNPATH] == NULL)
1484 /* No RUNPATH. */
1485 loader->l_runpath_dirs.dirs = (void *) -1;
1486 else
1488 /* Make sure the cache information is available. */
1489 size_t ptrval = (D_PTR (loader, l_info[DT_STRTAB])
1490 + loader->l_info[DT_RUNPATH]->d_un.d_val);
1491 decompose_rpath (&loader->l_runpath_dirs,
1492 (const char *) ptrval, loader, "RUNPATH");
1494 if (loader->l_runpath_dirs.dirs != (void *) -1)
1495 fd = open_path (name, namelen, preloaded,
1496 &loader->l_runpath_dirs, &realname);
1499 else if (loader->l_runpath_dirs.dirs != (void *) -1)
1500 fd = open_path (name, namelen, preloaded,
1501 &loader->l_runpath_dirs, &realname);
1504 if (fd == -1)
1506 /* Check the list of libraries in the file /etc/ld.so.cache,
1507 for compatibility with Linux's ldconfig program. */
1508 extern const char *_dl_load_cache_lookup (const char *name);
1509 const char *cached = _dl_load_cache_lookup (name);
1511 #ifdef SHARED
1512 l = loader ?: _dl_loaded;
1513 #else
1514 l = loader;
1515 #endif
1517 if (cached)
1519 /* If the loader has the DF_1_NODEFLIB flag set we must not
1520 use a cache entry from any of these directories. */
1521 if (l && __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
1523 const char *dirp = system_dirs;
1524 int cnt = 0;
1528 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
1530 /* The prefix matches. Don't use the entry. */
1531 cached = NULL;
1532 break;
1535 dirp += system_dirs_len[cnt] + 1;
1536 ++cnt;
1538 while (cnt < nsystem_dirs_len);
1541 if (cached)
1543 fd = __open (cached, O_RDONLY);
1544 if (fd != -1)
1546 realname = local_strdup (cached);
1547 if (realname == NULL)
1549 __close (fd);
1550 fd = -1;
1557 /* Finally, try the default path. */
1558 if (fd == -1
1559 && (l == NULL ||
1560 __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
1561 && rtld_search_dirs.dirs != (void *) -1)
1562 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
1563 &realname);
1565 /* Add another newline when we a tracing the library loading. */
1566 if (__builtin_expect (_dl_debug_libs, 0))
1567 _dl_debug_message (1, "\n", NULL);
1569 else
1571 /* The path may contain dynamic string tokens. */
1572 realname = (loader
1573 ? expand_dynamic_string_token (loader, name)
1574 : local_strdup (name));
1575 if (realname == NULL)
1576 fd = -1;
1577 else
1579 fd = __open (realname, O_RDONLY);
1580 if (fd == -1)
1581 free (realname);
1585 if (fd == -1)
1587 if (trace_mode)
1589 /* We haven't found an appropriate library. But since we
1590 are only interested in the list of libraries this isn't
1591 so severe. Fake an entry with all the information we
1592 have. */
1593 static const Elf_Symndx dummy_bucket = STN_UNDEF;
1595 /* Enter the new object in the list of loaded objects. */
1596 if ((name_copy = local_strdup (name)) == NULL
1597 || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
1598 _dl_signal_error (ENOMEM, name,
1599 N_("cannot create shared object descriptor"));
1600 /* We use an opencount of 0 as a sign for the faked entry.
1601 Since the descriptor is initialized with zero we do not
1602 have do this here.
1603 l->l_opencount = 0;
1604 l->l_reserved = 0; */
1605 l->l_buckets = &dummy_bucket;
1606 l->l_nbuckets = 1;
1607 l->l_relocated = 1;
1609 return l;
1611 else
1612 _dl_signal_error (errno, name, N_("cannot open shared object file"));
1615 return _dl_map_object_from_fd (name, fd, realname, loader, type, mode);