Update.
[glibc.git] / elf / dl-load.c
blob60e7d2e869e7c7d7234c8304e5e4f82cfc43f36e
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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 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.
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 Lesser General Public License for more details.
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. */
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 <abi-tag.h>
34 #include <dl-osinfo.h>
36 #include <dl-dst.h>
38 /* On some systems, no flag bits are given to specify file mapping. */
39 #ifndef MAP_FILE
40 # define MAP_FILE 0
41 #endif
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
51 # define MAP_COPY MAP_PRIVATE
52 #endif
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
60 # define MAP_BASE_ADDR(l) 0
61 #endif
64 #include <endian.h>
65 #if BYTE_ORDER == BIG_ENDIAN
66 # define byteorder ELFDATA2MSB
67 #elif BYTE_ORDER == LITTLE_ENDIAN
68 # define byteorder ELFDATA2LSB
69 #else
70 # error "Unknown BYTE_ORDER " BYTE_ORDER
71 # define byteorder ELFDATANONE
72 #endif
74 #define STRING(x) __STRING (x)
76 #ifdef MAP_ANON
77 /* The fd is not examined when using MAP_ANON. */
78 # define ANONFD -1
79 #else
80 int _dl_zerofd = -1;
81 # define ANONFD _dl_zerofd
82 #endif
84 /* Handle situations where we have a preferred location in memory for
85 the shared objects. */
86 #ifdef ELF_PREFERRED_ADDRESS_DATA
87 ELF_PREFERRED_ADDRESS_DATA;
88 #endif
89 #ifndef ELF_PREFERRED_ADDRESS
90 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
91 #endif
92 #ifndef ELF_FIXED_ADDRESS
93 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
94 #endif
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. */
108 struct filebuf
110 ssize_t len;
111 char buf[1024];
114 size_t _dl_pagesize;
116 unsigned int _dl_osversion;
118 int _dl_clktck;
120 extern const char *_dl_platform;
121 extern size_t _dl_platformlen;
123 /* The object to be initialized first. */
124 struct link_map *_dl_initfirst;
126 /* This is the decomposed LD_LIBRARY_PATH search path. */
127 static struct r_search_path_struct env_path_list;
129 /* List of the hardware capabilities we might end up using. */
130 static const struct r_strlenpair *capstr;
131 static size_t ncapstr;
132 static size_t max_capstrlen;
134 const unsigned char _dl_pf_to_prot[8] =
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
147 /* Get the generated information about the trusted directories. */
148 #include "trusted-dirs.h"
150 static const char system_dirs[] = SYSTEM_DIRS;
151 static const size_t system_dirs_len[] =
153 SYSTEM_DIRS_LEN
155 #define nsystem_dirs_len \
156 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
159 /* Local version of `strdup' function. */
160 static inline char *
161 local_strdup (const char *s)
163 size_t len = strlen (s) + 1;
164 void *new = malloc (len);
166 if (new == NULL)
167 return NULL;
169 return (char *) memcpy (new, s, len);
173 size_t
174 _dl_dst_count (const char *name, int is_path)
176 const char *const start = name;
177 size_t cnt = 0;
181 size_t len = 1;
183 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
184 is $ORIGIN alone) and it must always appear first in path.
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)))
195 if ((__builtin_expect (!__libc_enable_secure, 1)
196 || name[len] == '\0' || (is_path && name[len] == ':'))
197 && (name == start || (is_path && name[-1] == ':')))
198 ++cnt;
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))
206 ++cnt;
208 name = strchr (name + len, '$');
210 while (name != NULL);
212 return cnt;
216 char *
217 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
218 int is_path)
220 const char *const start = name;
221 char *last_elem, *wp;
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;
231 if (__builtin_expect (*name == '$', 0))
233 const char *repl = NULL;
234 size_t len = 1;
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)))
245 if ((__builtin_expect (!__libc_enable_secure, 1)
246 || name[len] == '\0' || (is_path && name[len] == ':'))
247 && (name == start || (is_path && name[-1] == ':')))
248 repl = l->l_origin;
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;
258 if (repl != NULL && repl != (const char *) -1)
260 wp = __stpcpy (wp, repl);
261 name += len;
263 else if (len > 1)
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;
272 else
273 /* No DST we recognize. */
274 *wp++ = *name++;
276 else
278 *wp++ = *name++;
279 if (is_path && *name == ':')
280 last_elem = wp;
283 while (*name != '\0');
285 *wp = '\0';
287 return result;
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. */
296 static char *
297 expand_dynamic_string_token (struct link_map *l, const char *s)
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;
307 /* Determine the number of DST elements. */
308 cnt = DL_DST_COUNT (s, 1);
310 /* If we do not have to replace anything simply copy the string. */
311 if (__builtin_expect (cnt, 0) == 0)
312 return local_strdup (s);
314 /* Determine the length of the substituted string. */
315 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
317 /* Allocate the necessary memory. */
318 result = (char *) malloc (total + 1);
319 if (result == NULL)
320 return NULL;
322 return DL_DST_SUBSTITUTE (l, s, result, 1);
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. */
330 static void
331 internal_function
332 add_name_to_object (struct link_map *l, const char *name)
334 struct libname_list *lnp, *lastp;
335 struct libname_list *newname;
336 size_t name_len;
338 lastp = NULL;
339 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
340 if (strcmp (name, lnp->name) == 0)
341 return;
343 name_len = strlen (name) + 1;
344 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
345 if (newname == NULL)
347 /* No more memory. */
348 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
349 return;
351 /* The object should have a libname set from _dl_new_object. */
352 assert (lastp != NULL);
354 newname->name = memcpy (newname + 1, name, name_len);
355 newname->next = NULL;
356 newname->dont_free = 0;
357 lastp->next = newname;
360 /* All known directories in sorted order. */
361 struct r_search_path_elem *_dl_all_dirs;
363 /* All directories after startup. */
364 struct r_search_path_elem *_dl_init_all_dirs;
366 /* Standard search directories. */
367 static struct r_search_path_struct rtld_search_dirs;
369 static size_t max_dirnamelen;
371 static inline struct r_search_path_elem **
372 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
373 int check_trusted, const char *what, const char *where)
375 char *cp;
376 size_t nelems = 0;
378 while ((cp = __strsep (&rpath, sep)) != NULL)
380 struct r_search_path_elem *dirp;
381 size_t len = strlen (cp);
383 /* `strsep' can pass an empty string. This has to be
384 interpreted as `use the current directory'. */
385 if (len == 0)
387 static const char curwd[] = "./";
388 cp = (char *) curwd;
391 /* Remove trailing slashes (except for "/"). */
392 while (len > 1 && cp[len - 1] == '/')
393 --len;
395 /* Now add one if there is none so far. */
396 if (len > 0 && cp[len - 1] != '/')
397 cp[len++] = '/';
399 /* Make sure we don't use untrusted directories if we run SUID. */
400 if (__builtin_expect (check_trusted, 0))
402 const char *trun = system_dirs;
403 size_t idx;
404 int unsecure = 1;
406 /* All trusted directories must be complete names. */
407 if (cp[0] == '/')
409 for (idx = 0; idx < nsystem_dirs_len; ++idx)
411 if (len == system_dirs_len[idx]
412 && memcmp (trun, cp, len) == 0)
414 /* Found it. */
415 unsecure = 0;
416 break;
419 trun += system_dirs_len[idx] + 1;
423 if (unsecure)
424 /* Simply drop this directory. */
425 continue;
428 /* See if this directory is already known. */
429 for (dirp = _dl_all_dirs; dirp != NULL; dirp = dirp->next)
430 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
431 break;
433 if (dirp != NULL)
435 /* It is available, see whether it's on our own list. */
436 size_t cnt;
437 for (cnt = 0; cnt < nelems; ++cnt)
438 if (result[cnt] == dirp)
439 break;
441 if (cnt == nelems)
442 result[nelems++] = dirp;
444 else
446 size_t cnt;
447 enum r_dir_status init_val;
448 size_t where_len = where ? strlen (where) + 1 : 0;
450 /* It's a new directory. Create an entry and add it. */
451 dirp = (struct r_search_path_elem *)
452 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
453 + where_len + len + 1);
454 if (dirp == NULL)
455 _dl_signal_error (ENOMEM, NULL, NULL,
456 N_("cannot create cache for search path"));
458 dirp->dirname = ((char *) dirp + sizeof (*dirp)
459 + ncapstr * sizeof (enum r_dir_status));
460 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
461 dirp->dirnamelen = len;
463 if (len > max_dirnamelen)
464 max_dirnamelen = len;
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;
470 for (cnt = 0; cnt < ncapstr; ++cnt)
471 dirp->status[cnt] = init_val;
473 dirp->what = what;
474 if (__builtin_expect (where != NULL, 1))
475 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
476 + ncapstr * sizeof (enum r_dir_status),
477 where, where_len);
478 else
479 dirp->where = NULL;
481 dirp->next = _dl_all_dirs;
482 _dl_all_dirs = dirp;
484 /* Put it in the result array. */
485 result[nelems++] = dirp;
489 /* Terminate the array. */
490 result[nelems] = NULL;
492 return result;
496 static void
497 internal_function
498 decompose_rpath (struct r_search_path_struct *sps,
499 const char *rpath, struct link_map *l, const char *what)
501 /* Make a copy we can work with. */
502 const char *where = l->l_name;
503 char *copy;
504 char *cp;
505 struct r_search_path_elem **result;
506 size_t nelems;
507 /* Initialize to please the compiler. */
508 const char *errstring = NULL;
510 /* First see whether we must forget the RUNPATH and RPATH from this
511 object. */
512 if (__builtin_expect (_dl_inhibit_rpath != NULL, 0) && !__libc_enable_secure)
514 const char *found = strstr (_dl_inhibit_rpath, where);
515 if (found != NULL)
517 size_t len = strlen (where);
518 if ((found == _dl_inhibit_rpath || found[-1] == ':')
519 && (found[len] == '\0' || found[len] == ':'))
521 /* This object is on the list of objects for which the
522 RUNPATH and RPATH must not be used. */
523 result = (struct r_search_path_elem **)
524 malloc (sizeof (*result));
525 if (result == NULL)
527 signal_error_cache:
528 errstring = N_("cannot create cache for search path");
529 signal_error:
530 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
533 result[0] = NULL;
535 sps->dirs = result;
536 sps->malloced = 1;
538 return;
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)
548 errstring = N_("cannot create RUNPATH/RPATH copy");
549 goto signal_error;
552 /* Count the number of necessary elements in the result array. */
553 nelems = 0;
554 for (cp = copy; *cp != '\0'; ++cp)
555 if (*cp == ':')
556 ++nelems;
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)
561 * sizeof (*result));
562 if (result == NULL)
563 goto signal_error_cache;
565 fillin_rpath (copy, result, ":", 0, what, where);
567 /* Free the copied RPATH string. `fillin_rpath' make own copies if
568 necessary. */
569 free (copy);
571 sps->dirs = result;
572 /* The caller will change this value if we haven't used a real malloc. */
573 sps->malloced = 1;
577 void
578 internal_function
579 _dl_init_paths (const char *llp)
581 size_t idx;
582 const char *strp;
583 struct r_search_path_elem *pelem, **aelem;
584 size_t round_size;
585 #ifdef SHARED
586 struct link_map *l;
587 #endif
588 /* Initialize to please the compiler. */
589 const char *errstring = NULL;
591 /* Fill in the information about the application's RPATH and the
592 directories addressed by the LD_LIBRARY_PATH environment variable. */
594 /* Get the capabilities. */
595 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
596 &ncapstr, &max_capstrlen);
598 /* First set up the rest of the default search directory entries. */
599 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
600 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
601 if (rtld_search_dirs.dirs == NULL)
603 errstring = N_("cannot create search path array");
604 signal_error:
605 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
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));
612 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
613 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
614 * round_size * sizeof (struct r_search_path_elem));
615 if (rtld_search_dirs.dirs[0] == NULL)
617 errstring = N_("cannot create cache for search path");
618 goto signal_error;
621 rtld_search_dirs.malloced = 0;
622 pelem = _dl_all_dirs = rtld_search_dirs.dirs[0];
623 strp = system_dirs;
624 idx = 0;
628 size_t cnt;
630 *aelem++ = pelem;
632 pelem->what = "system search path";
633 pelem->where = NULL;
635 pelem->dirname = strp;
636 pelem->dirnamelen = system_dirs_len[idx];
637 strp += system_dirs_len[idx] + 1;
639 /* System paths must be absolute. */
640 assert (pelem->dirname[0] == '/');
641 for (cnt = 0; cnt < ncapstr; ++cnt)
642 pelem->status[cnt] = unknown;
644 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
646 pelem += round_size;
648 while (idx < nsystem_dirs_len);
650 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
651 *aelem = NULL;
653 #ifdef SHARED
654 /* This points to the map of the main object. */
655 l = _dl_loaded;
656 if (l != NULL)
658 assert (l->l_type != lt_loaded);
660 if (l->l_info[DT_RUNPATH])
662 /* Allocate room for the search path and fill in information
663 from RUNPATH. */
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");
669 /* The RPATH is ignored. */
670 l->l_rpath_dirs.dirs = (void *) -1;
672 else
674 l->l_runpath_dirs.dirs = (void *) -1;
676 if (l->l_info[DT_RPATH])
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])
682 + l->l_info[DT_RPATH]->d_un.d_val),
683 l, "RPATH");
684 l->l_rpath_dirs.malloced = 0;
686 else
687 l->l_rpath_dirs.dirs = (void *) -1;
690 #endif /* SHARED */
692 if (llp != NULL && *llp != '\0')
694 size_t nllp;
695 const char *cp = llp;
696 char *llp_tmp = strdupa (llp);
698 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
699 elements it has. */
700 nllp = 1;
701 while (*cp)
703 if (*cp == ':' || *cp == ';')
704 ++nllp;
705 ++cp;
708 env_path_list.dirs = (struct r_search_path_elem **)
709 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
710 if (env_path_list.dirs == NULL)
712 errstring = N_("cannot create cache for search path");
713 goto signal_error;
716 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
717 __libc_enable_secure, "LD_LIBRARY_PATH", NULL);
719 if (env_path_list.dirs[0] == NULL)
721 free (env_path_list.dirs);
722 env_path_list.dirs = (void *) -1;
725 env_path_list.malloced = 0;
727 else
728 env_path_list.dirs = (void *) -1;
730 /* Remember the last search directory added at startup. */
731 _dl_init_all_dirs = _dl_all_dirs;
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. */
741 static void
742 __attribute__ ((noreturn))
743 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
744 const char *msg)
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 /* The file might already be closed. */
753 if (a[0] != -1)
754 (void) __close (a[0]);
755 if (l != NULL)
757 /* Remove the stillborn object from the list and free it. */
758 if (l->l_prev)
759 l->l_prev->l_next = l->l_next;
760 if (l->l_next)
761 l->l_next->l_prev = l->l_prev;
762 --_dl_nloaded;
763 free (l);
765 free (realname);
766 _dl_signal_error (code, name, NULL, msg);
770 /* Map in the shared object NAME, actually located in REALNAME, and already
771 opened on FD. */
773 #ifndef EXTERNAL_MAP_FROM_FD
774 static
775 #endif
776 struct link_map *
777 _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
778 char *realname, struct link_map *loader, int l_type,
779 int mode)
781 struct link_map *l = NULL;
782 const ElfW(Ehdr) *header;
783 const ElfW(Phdr) *phdr;
784 const ElfW(Phdr) *ph;
785 size_t maplength;
786 int type;
787 struct stat64 st;
788 /* Initialize to keep the compiler happy. */
789 const char *errstring = NULL;
790 int errval = 0;
792 /* Get file information. */
793 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st) < 0, 0))
795 errstring = N_("cannot stat shared object");
796 call_lose_errno:
797 errval = errno;
798 call_lose:
799 lose (errval, fd, name, realname, l, errstring);
802 /* Look again to see if the real name matched another already loaded. */
803 for (l = _dl_loaded; l; l = l->l_next)
804 if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
806 /* The object is already loaded.
807 Just bump its reference count and return it. */
808 __close (fd);
810 /* If the name is not in the list of names for this object add
811 it. */
812 free (realname);
813 add_name_to_object (l, name);
815 return l;
818 if (mode & RTLD_NOLOAD)
819 /* We are not supposed to load the object unless it is already
820 loaded. So return now. */
821 return NULL;
823 /* Print debugging message. */
824 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
825 _dl_debug_printf ("file=%s; generating link map\n", name);
827 /* This is the ELF header. We read it in `open_verify'. */
828 header = (void *) fbp->buf;
830 #ifndef MAP_ANON
831 # define MAP_ANON 0
832 if (_dl_zerofd == -1)
834 _dl_zerofd = _dl_sysdep_open_zero_fill ();
835 if (_dl_zerofd == -1)
837 __close (fd);
838 _dl_signal_error (errno, NULL, NULL,
839 N_("cannot open zero fill device"));
842 #endif
844 /* Enter the new object in the list of loaded objects. */
845 l = _dl_new_object (realname, name, l_type, loader);
846 if (__builtin_expect (! l, 0))
848 errstring = N_("cannot create shared object descriptor");
849 goto call_lose_errno;
852 /* Extract the remaining details we need from the ELF header
853 and then read in the program header table. */
854 l->l_entry = header->e_entry;
855 type = header->e_type;
856 l->l_phnum = header->e_phnum;
858 maplength = header->e_phnum * sizeof (ElfW(Phdr));
859 if (header->e_phoff + maplength <= fbp->len)
860 phdr = (void *) (fbp->buf + header->e_phoff);
861 else
863 phdr = alloca (maplength);
864 __lseek (fd, SEEK_SET, header->e_phoff);
865 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
867 errstring = N_("cannot read file data");
868 goto call_lose_errno;
873 /* Scan the program header table, collecting its load commands. */
874 struct loadcmd
876 ElfW(Addr) mapstart, mapend, dataend, allocend;
877 off_t mapoff;
878 int prot;
879 } loadcmds[l->l_phnum], *c;
880 size_t nloadcmds = 0;
882 /* The struct is initialized to zero so this is not necessary:
883 l->l_ld = 0;
884 l->l_phdr = 0;
885 l->l_addr = 0; */
886 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
887 switch (ph->p_type)
889 /* These entries tell us where to find things once the file's
890 segments are mapped in. We record the addresses it says
891 verbatim, and later correct for the run-time load address. */
892 case PT_DYNAMIC:
893 l->l_ld = (void *) ph->p_vaddr;
894 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
895 break;
897 case PT_PHDR:
898 l->l_phdr = (void *) ph->p_vaddr;
899 break;
901 case PT_LOAD:
902 /* A load command tells us to map in part of the file.
903 We record the load commands and process them all later. */
904 if ((ph->p_align & (_dl_pagesize - 1)) != 0)
906 errstring = N_("ELF load command alignment not page-aligned");
907 goto call_lose;
909 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
911 errstring
912 = N_("ELF load command address/offset not properly aligned");
913 goto call_lose;
917 struct loadcmd *c = &loadcmds[nloadcmds++];
918 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
919 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
920 & ~(_dl_pagesize - 1));
921 c->dataend = ph->p_vaddr + ph->p_filesz;
922 c->allocend = ph->p_vaddr + ph->p_memsz;
923 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
925 /* Optimize a common case. */
926 if ((PF_R | PF_W | PF_X) == 7)
927 c->prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_W | PF_X)];
928 else
930 c->prot = 0;
931 if (ph->p_flags & PF_R)
932 c->prot |= PROT_READ;
933 if (ph->p_flags & PF_W)
934 c->prot |= PROT_WRITE;
935 if (ph->p_flags & PF_X)
936 c->prot |= PROT_EXEC;
939 break;
942 /* Now process the load commands and map segments into memory. */
943 c = loadcmds;
945 /* Length of the sections to be loaded. */
946 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
948 if (__builtin_expect (type, ET_DYN) == ET_DYN)
950 /* This is a position-independent shared object. We can let the
951 kernel map it anywhere it likes, but we must have space for all
952 the segments in their specified positions relative to the first.
953 So we map the first segment without MAP_FIXED, but with its
954 extent increased to cover all the segments. Then we remove
955 access from excess portion, and there is known sufficient space
956 there to remap from the later segments.
958 As a refinement, sometimes we have an address that we would
959 prefer to map such objects at; but this is only a preference,
960 the OS can do whatever it likes. */
961 ElfW(Addr) mappref;
962 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
963 - MAP_BASE_ADDR (l));
965 /* Remember which part of the address space this object uses. */
966 l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
967 c->prot, MAP_COPY | MAP_FILE,
968 fd, c->mapoff);
969 if ((void *) l->l_map_start == MAP_FAILED)
971 map_error:
972 errstring = N_("failed to map segment from shared object");
973 goto call_lose_errno;
976 l->l_map_end = l->l_map_start + maplength;
977 l->l_addr = l->l_map_start - c->mapstart;
979 /* Change protection on the excess portion to disallow all access;
980 the portions we do not remap later will be inaccessible as if
981 unallocated. Then jump into the normal segment-mapping loop to
982 handle the portion of the segment past the end of the file
983 mapping. */
984 __mprotect ((caddr_t) (l->l_addr + c->mapend),
985 loadcmds[nloadcmds - 1].allocend - c->mapend,
986 PROT_NONE);
988 goto postmap;
990 else
992 /* This object is loaded at a fixed address. This must never
993 happen for objects loaded with dlopen(). */
994 if (__builtin_expect (mode & __RTLD_DLOPEN, 0))
996 errstring = N_("cannot dynamically load executable");
997 goto call_lose;
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 && (__mmap ((void *) (l->l_addr + c->mapstart),
1014 c->mapend - c->mapstart, c->prot,
1015 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff)
1016 == MAP_FAILED))
1017 goto map_error;
1019 postmap:
1020 if (l->l_phdr == 0
1021 && c->mapoff <= header->e_phoff
1022 && (c->mapend - c->mapstart + c->mapoff
1023 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1024 /* Found the program header in this segment. */
1025 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
1027 if (c->allocend > c->dataend)
1029 /* Extra zero pages should appear at the end of this segment,
1030 after the data mapped from the file. */
1031 ElfW(Addr) zero, zeroend, zeropage;
1033 zero = l->l_addr + c->dataend;
1034 zeroend = l->l_addr + c->allocend;
1035 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
1037 if (zeroend < zeropage)
1038 /* All the extra data is in the last page of the segment.
1039 We can just zero it. */
1040 zeropage = zeroend;
1042 if (zeropage > zero)
1044 /* Zero the final part of the last page of the segment. */
1045 if ((c->prot & PROT_WRITE) == 0)
1047 /* Dag nab it. */
1048 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1049 _dl_pagesize, c->prot|PROT_WRITE) < 0)
1051 errstring = N_("cannot change memory protections");
1052 goto call_lose_errno;
1055 memset ((void *) zero, '\0', zeropage - zero);
1056 if ((c->prot & PROT_WRITE) == 0)
1057 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1058 _dl_pagesize, c->prot);
1061 if (zeroend > zeropage)
1063 /* Map the remaining zero pages in from the zero fill FD. */
1064 caddr_t mapat;
1065 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1066 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1067 ANONFD, 0);
1068 if (mapat == MAP_FAILED)
1070 errstring = N_("cannot map zero-fill pages");
1071 goto call_lose_errno;
1076 ++c;
1079 if (l->l_phdr == NULL)
1081 /* The program header is not contained in any of the segments.
1082 We have to allocate memory ourself and copy it over from
1083 out temporary place. */
1084 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1085 * sizeof (ElfW(Phdr)));
1086 if (newp == NULL)
1088 errstring = N_("cannot allocate memory for program header");
1089 goto call_lose_errno;
1092 l->l_phdr = memcpy (newp, phdr,
1093 (header->e_phnum * sizeof (ElfW(Phdr))));
1094 l->l_phdr_allocated = 1;
1096 else
1097 /* Adjust the PT_PHDR value by the runtime load address. */
1098 (ElfW(Addr)) l->l_phdr += l->l_addr;
1101 /* We are done mapping in the file. We no longer need the descriptor. */
1102 __close (fd);
1103 /* Signal that we closed the file. */
1104 fd = -1;
1106 if (l->l_type == lt_library && type == ET_EXEC)
1107 l->l_type = lt_executable;
1109 if (l->l_ld == 0)
1111 if (type == ET_DYN)
1113 errstring = N_("object file has no dynamic section");
1114 goto call_lose;
1117 else
1118 (ElfW(Addr)) l->l_ld += l->l_addr;
1120 l->l_entry += l->l_addr;
1122 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
1123 _dl_debug_printf (" dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n"
1124 " entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1125 (int) sizeof (void *) * 2, (unsigned long int) l->l_ld,
1126 (int) sizeof (void *) * 2, (unsigned long int) l->l_addr,
1127 (int) sizeof (void *) * 2, maplength,
1128 (int) sizeof (void *) * 2, (unsigned long int) l->l_entry,
1129 (int) sizeof (void *) * 2, (unsigned long int) l->l_phdr,
1130 (int) sizeof (void *) * 2, l->l_phnum);
1132 elf_get_dynamic_info (l);
1134 /* Make sure we are dlopen()ing an object which has the DF_1_NOOPEN
1135 flag set. */
1136 if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1137 && (mode & __RTLD_DLOPEN))
1139 /* Remove from the module list. */
1140 assert (l->l_next == NULL);
1141 #ifndef SHARED
1142 if (l->l_prev == NULL)
1143 /* No other module loaded. */
1144 _dl_loaded = NULL;
1145 else
1146 #endif
1147 l->l_prev->l_next = NULL;
1148 --_dl_nloaded;
1150 /* We are not supposed to load this object. Free all resources. */
1151 __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1153 if (!l->l_libname->dont_free)
1154 free (l->l_libname);
1156 if (l->l_phdr_allocated)
1157 free ((void *) l->l_phdr);
1159 free (l);
1161 errstring = N_("shared object cannot be dlopen()ed");
1162 goto call_lose;
1165 if (l->l_info[DT_HASH])
1166 _dl_setup_hash (l);
1168 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1169 have to do this for the main map. */
1170 if (__builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0)
1171 && &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)
1184 errstring = N_("cannot create searchlist");
1185 goto call_lose_errno;
1188 l->l_symbolic_searchlist.r_list[0] = l;
1189 l->l_symbolic_searchlist.r_nlist = 1;
1191 /* Now move the existing entries one back. */
1192 memmove (&l->l_scope[1], &l->l_scope[0],
1193 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1195 /* Now add the new entry. */
1196 l->l_scope[0] = &l->l_symbolic_searchlist;
1199 /* Remember whether this object must be initialized first. */
1200 if (l->l_flags_1 & DF_1_INITFIRST)
1201 _dl_initfirst = l;
1203 /* Finally the file information. */
1204 l->l_dev = st.st_dev;
1205 l->l_ino = st.st_ino;
1207 return l;
1210 /* Print search path. */
1211 static void
1212 print_search_path (struct r_search_path_elem **list,
1213 const char *what, const char *name)
1215 char buf[max_dirnamelen + max_capstrlen];
1216 int first = 1;
1218 _dl_debug_printf (" search path=");
1220 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1222 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1223 size_t cnt;
1225 for (cnt = 0; cnt < ncapstr; ++cnt)
1226 if ((*list)->status[cnt] != nonexisting)
1228 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1229 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1230 cp[0] = '\0';
1231 else
1232 cp[-1] = '\0';
1234 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1235 first = 0;
1238 ++list;
1241 if (name != NULL)
1242 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1243 name[0] ? name : _dl_argv[0]);
1244 else
1245 _dl_debug_printf_c ("\t\t(%s)\n", what);
1248 /* Open a file and verify it is an ELF file for this architecture. We
1249 ignore only ELF files for other architectures. Non-ELF files and
1250 ELF files with different header information cause fatal errors since
1251 this could mean there is something wrong in the installation and the
1252 user might want to know about this. */
1253 static int
1254 open_verify (const char *name, struct filebuf *fbp)
1256 /* This is the expected ELF header. */
1257 #define ELF32_CLASS ELFCLASS32
1258 #define ELF64_CLASS ELFCLASS64
1259 #ifndef VALID_ELF_HEADER
1260 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1261 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1262 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1263 #endif
1264 static const unsigned char expected[EI_PAD] =
1266 [EI_MAG0] = ELFMAG0,
1267 [EI_MAG1] = ELFMAG1,
1268 [EI_MAG2] = ELFMAG2,
1269 [EI_MAG3] = ELFMAG3,
1270 [EI_CLASS] = ELFW(CLASS),
1271 [EI_DATA] = byteorder,
1272 [EI_VERSION] = EV_CURRENT,
1273 [EI_OSABI] = ELFOSABI_SYSV,
1274 [EI_ABIVERSION] = 0
1276 static const struct
1278 ElfW(Word) vendorlen;
1279 ElfW(Word) datalen;
1280 ElfW(Word) type;
1281 char vendor[4];
1282 } expected_note = { 4, 16, 1, "GNU" };
1283 int fd;
1284 /* Initialize it to make the compiler happy. */
1285 const char *errstring = NULL;
1286 int errval = 0;
1288 /* Open the file. We always open files read-only. */
1289 fd = __open (name, O_RDONLY);
1290 if (fd != -1)
1292 ElfW(Ehdr) *ehdr;
1293 ElfW(Phdr) *phdr, *ph;
1294 ElfW(Word) *abi_note, abi_note_buf[8];
1295 unsigned int osversion;
1296 size_t maplength;
1298 /* We successfully openened the file. Now verify it is a file
1299 we can use. */
1300 __set_errno (0);
1301 fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf));
1303 /* This is where the ELF header is loaded. */
1304 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1305 ehdr = (ElfW(Ehdr) *) fbp->buf;
1307 /* Now run the tests. */
1308 if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0))
1310 errval = errno;
1311 errstring = (errval == 0
1312 ? N_("file too short") : N_("cannot read file data"));
1313 call_lose:
1314 lose (errval, fd, name, NULL, NULL, errstring);
1317 /* See whether the ELF header is what we expect. */
1318 if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1319 EI_PAD), 0))
1321 /* Something is wrong. */
1322 if (*(Elf32_Word *) &ehdr->e_ident !=
1323 #if BYTE_ORDER == LITTLE_ENDIAN
1324 ((ELFMAG0 << (EI_MAG0 * 8)) |
1325 (ELFMAG1 << (EI_MAG1 * 8)) |
1326 (ELFMAG2 << (EI_MAG2 * 8)) |
1327 (ELFMAG3 << (EI_MAG3 * 8)))
1328 #else
1329 ((ELFMAG0 << (EI_MAG3 * 8)) |
1330 (ELFMAG1 << (EI_MAG2 * 8)) |
1331 (ELFMAG2 << (EI_MAG1 * 8)) |
1332 (ELFMAG3 << (EI_MAG0 * 8)))
1333 #endif
1335 errstring = N_("invalid ELF header");
1336 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1337 /* This is not a fatal error. On architectures where
1338 32-bit and 64-bit binaries can be run this might
1339 happen. */
1340 goto close_and_out;
1341 else if (ehdr->e_ident[EI_DATA] != byteorder)
1343 if (BYTE_ORDER == BIG_ENDIAN)
1344 errstring = N_("ELF file data encoding not big-endian");
1345 else
1346 errstring = N_("ELF file data encoding not little-endian");
1348 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1349 errstring
1350 = N_("ELF file version ident does not match current one");
1351 /* XXX We should be able so set system specific versions which are
1352 allowed here. */
1353 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1354 errstring = N_("ELF file OS ABI invalid");
1355 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION]))
1356 errstring = N_("ELF file ABI version invalid");
1357 else
1358 /* Otherwise we don't know what went wrong. */
1359 errstring = N_("internal error");
1361 goto call_lose;
1364 if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT)
1366 errstring = N_("ELF file version does not match current one");
1367 goto call_lose;
1369 if (! __builtin_expect (elf_machine_matches_host (ehdr), 1))
1370 goto close_and_out;
1371 else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
1372 != sizeof (ElfW(Phdr)))
1374 errstring = N_("ELF file's phentsize not the expected size");
1375 goto call_lose;
1377 else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN
1378 && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC)
1380 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1381 goto call_lose;
1384 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1385 if (ehdr->e_phoff + maplength <= fbp->len)
1386 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1387 else
1389 phdr = alloca (maplength);
1390 __lseek (fd, SEEK_SET, ehdr->e_phoff);
1391 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
1393 read_error:
1394 errval = errno;
1395 errstring = N_("cannot read file data");
1396 goto call_lose;
1400 /* Check .note.ABI-tag if present. */
1401 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1402 if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
1404 if (ph->p_offset + 32 <= fbp->len)
1405 abi_note = (void *) (fbp->buf + ph->p_offset);
1406 else
1408 __lseek (fd, SEEK_SET, ph->p_offset);
1409 if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
1410 goto read_error;
1412 abi_note = abi_note_buf;
1415 if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1416 continue;
1418 osversion = (abi_note[5] & 0xff) * 65536
1419 + (abi_note[6] & 0xff) * 256
1420 + (abi_note[7] & 0xff);
1421 if (abi_note[4] != __ABI_TAG_OS
1422 || (_dl_osversion && _dl_osversion < osversion))
1424 close_and_out:
1425 __close (fd);
1426 __set_errno (ENOENT);
1427 fd = -1;
1430 break;
1434 return fd;
1437 /* Try to open NAME in one of the directories in *DIRSP.
1438 Return the fd, or -1. If successful, fill in *REALNAME
1439 with the malloc'd full directory name. If it turns out
1440 that none of the directories in *DIRSP exists, *DIRSP is
1441 replaced with (void *) -1, and the old value is free()d
1442 if MAY_FREE_DIRS is true. */
1444 static int
1445 open_path (const char *name, size_t namelen, int preloaded,
1446 struct r_search_path_struct *sps, char **realname,
1447 struct filebuf *fbp)
1449 struct r_search_path_elem **dirs = sps->dirs;
1450 char *buf;
1451 int fd = -1;
1452 const char *current_what = NULL;
1453 int any = 0;
1455 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1458 struct r_search_path_elem *this_dir = *dirs;
1459 size_t buflen = 0;
1460 size_t cnt;
1461 char *edp;
1462 int here_any = 0;
1463 int err;
1465 /* If we are debugging the search for libraries print the path
1466 now if it hasn't happened now. */
1467 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0)
1468 && current_what != this_dir->what)
1470 current_what = this_dir->what;
1471 print_search_path (dirs, current_what, this_dir->where);
1474 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1475 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1477 /* Skip this directory if we know it does not exist. */
1478 if (this_dir->status[cnt] == nonexisting)
1479 continue;
1481 buflen =
1482 ((char *) __mempcpy (__mempcpy (edp,
1483 capstr[cnt].str, capstr[cnt].len),
1484 name, namelen)
1485 - buf);
1487 /* Print name we try if this is wanted. */
1488 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1489 _dl_debug_printf (" trying file=%s\n", buf);
1491 fd = open_verify (buf, fbp);
1492 if (this_dir->status[cnt] == unknown)
1494 if (fd != -1)
1495 this_dir->status[cnt] = existing;
1496 else
1498 /* We failed to open machine dependent library. Let's
1499 test whether there is any directory at all. */
1500 struct stat64 st;
1502 buf[buflen - namelen - 1] = '\0';
1504 if (__xstat64 (_STAT_VER, buf, &st) != 0
1505 || ! S_ISDIR (st.st_mode))
1506 /* The directory does not exist or it is no directory. */
1507 this_dir->status[cnt] = nonexisting;
1508 else
1509 this_dir->status[cnt] = existing;
1513 /* Remember whether we found any existing directory. */
1514 here_any |= this_dir->status[cnt] == existing;
1516 if (fd != -1 && __builtin_expect (preloaded, 0)
1517 && __libc_enable_secure)
1519 /* This is an extra security effort to make sure nobody can
1520 preload broken shared objects which are in the trusted
1521 directories and so exploit the bugs. */
1522 struct stat64 st;
1524 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1525 || (st.st_mode & S_ISUID) == 0)
1527 /* The shared object cannot be tested for being SUID
1528 or this bit is not set. In this case we must not
1529 use this object. */
1530 __close (fd);
1531 fd = -1;
1532 /* We simply ignore the file, signal this by setting
1533 the error value which would have been set by `open'. */
1534 errno = ENOENT;
1539 if (fd != -1)
1541 *realname = (char *) malloc (buflen);
1542 if (*realname != NULL)
1544 memcpy (*realname, buf, buflen);
1545 return fd;
1547 else
1549 /* No memory for the name, we certainly won't be able
1550 to load and link it. */
1551 __close (fd);
1552 return -1;
1555 if (here_any && (err = errno) != ENOENT && err != EACCES)
1556 /* The file exists and is readable, but something went wrong. */
1557 return -1;
1559 /* Remember whether we found anything. */
1560 any |= here_any;
1562 while (*++dirs != NULL);
1564 /* Remove the whole path if none of the directories exists. */
1565 if (__builtin_expect (! any, 0))
1567 /* Paths which were allocated using the minimal malloc() in ld.so
1568 must not be freed using the general free() in libc. */
1569 if (sps->malloced)
1570 free (sps->dirs);
1571 sps->dirs = (void *) -1;
1574 return -1;
1577 /* Map in the shared object file NAME. */
1579 struct link_map *
1580 internal_function
1581 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1582 int type, int trace_mode, int mode)
1584 int fd;
1585 char *realname;
1586 char *name_copy;
1587 struct link_map *l;
1588 struct filebuf fb;
1590 /* Look for this name among those already loaded. */
1591 for (l = _dl_loaded; l; l = l->l_next)
1593 /* If the requested name matches the soname of a loaded object,
1594 use that object. Elide this check for names that have not
1595 yet been opened. */
1596 if (__builtin_expect (l->l_faked, 0) != 0)
1597 continue;
1598 if (!_dl_name_match_p (name, l))
1600 const char *soname;
1602 if (__builtin_expect (l->l_soname_added, 1)
1603 || l->l_info[DT_SONAME] == NULL)
1604 continue;
1606 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1607 + l->l_info[DT_SONAME]->d_un.d_val);
1608 if (strcmp (name, soname) != 0)
1609 continue;
1611 /* We have a match on a new name -- cache it. */
1612 add_name_to_object (l, soname);
1613 l->l_soname_added = 1;
1616 /* We have a match. */
1617 return l;
1620 /* Display information if we are debugging. */
1621 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0) && loader != NULL)
1622 _dl_debug_printf ("\nfile=%s; needed by %s\n", name,
1623 loader->l_name[0] ? loader->l_name : _dl_argv[0]);
1625 if (strchr (name, '/') == NULL)
1627 /* Search for NAME in several places. */
1629 size_t namelen = strlen (name) + 1;
1631 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1632 _dl_debug_printf ("find library=%s; searching\n", name);
1634 fd = -1;
1636 /* When the object has the RUNPATH information we don't use any
1637 RPATHs. */
1638 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1640 /* First try the DT_RPATH of the dependent object that caused NAME
1641 to be loaded. Then that object's dependent, and on up. */
1642 for (l = loader; fd == -1 && l; l = l->l_loader)
1644 if (l->l_rpath_dirs.dirs == NULL)
1646 if (l->l_info[DT_RPATH] == NULL)
1648 /* There is no path. */
1649 l->l_rpath_dirs.dirs = (void *) -1;
1650 continue;
1652 else
1654 /* Make sure the cache information is available. */
1655 size_t ptrval = (D_PTR (l, l_info[DT_STRTAB])
1656 + l->l_info[DT_RPATH]->d_un.d_val);
1657 decompose_rpath (&l->l_rpath_dirs,
1658 (const char *) ptrval, l, "RPATH");
1662 if (l->l_rpath_dirs.dirs != (void *) -1)
1663 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1664 &realname, &fb);
1667 /* If dynamically linked, try the DT_RPATH of the executable
1668 itself. */
1669 l = _dl_loaded;
1670 if (fd == -1 && l && l->l_type != lt_loaded && l != loader
1671 && l->l_rpath_dirs.dirs != (void *) -1)
1672 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1673 &realname, &fb);
1676 /* Try the LD_LIBRARY_PATH environment variable. */
1677 if (fd == -1 && env_path_list.dirs != (void *) -1)
1678 fd = open_path (name, namelen, preloaded, &env_path_list,
1679 &realname, &fb);
1681 /* Look at the RUNPATH information for this binary.
1683 Note that this is no real loop. 'while' is used only to enable
1684 us to use 'break' instead of a 'goto' to jump to the end. The
1685 loop is always left after the first round. */
1686 while (fd == -1 && loader != NULL
1687 && loader->l_runpath_dirs.dirs != (void *) -1)
1689 if (loader->l_runpath_dirs.dirs == NULL)
1691 if (loader->l_info[DT_RUNPATH] == NULL)
1693 /* No RUNPATH. */
1694 loader->l_runpath_dirs.dirs = (void *) -1;
1695 break;
1697 else
1699 /* Make sure the cache information is available. */
1700 size_t ptrval = (D_PTR (loader, l_info[DT_STRTAB])
1701 + loader->l_info[DT_RUNPATH]->d_un.d_val);
1702 decompose_rpath (&loader->l_runpath_dirs,
1703 (const char *) ptrval, loader, "RUNPATH");
1707 if (loader->l_runpath_dirs.dirs != (void *) -1)
1708 fd = open_path (name, namelen, preloaded,
1709 &loader->l_runpath_dirs, &realname, &fb);
1710 break;
1713 if (fd == -1
1714 && (__builtin_expect (! preloaded, 1) || ! __libc_enable_secure))
1716 /* Check the list of libraries in the file /etc/ld.so.cache,
1717 for compatibility with Linux's ldconfig program. */
1718 const char *cached = _dl_load_cache_lookup (name);
1720 if (cached != NULL)
1722 #ifdef SHARED
1723 l = loader ?: _dl_loaded;
1724 #else
1725 l = loader;
1726 #endif
1728 /* If the loader has the DF_1_NODEFLIB flag set we must not
1729 use a cache entry from any of these directories. */
1730 if (
1731 #ifndef SHARED
1732 /* 'l' is always != NULL for dynamically linked objects. */
1733 l != NULL &&
1734 #endif
1735 __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
1737 const char *dirp = system_dirs;
1738 unsigned int cnt = 0;
1742 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
1744 /* The prefix matches. Don't use the entry. */
1745 cached = NULL;
1746 break;
1749 dirp += system_dirs_len[cnt] + 1;
1750 ++cnt;
1752 while (cnt < nsystem_dirs_len);
1755 if (cached != NULL)
1757 fd = open_verify (cached, &fb);
1758 if (__builtin_expect (fd != -1, 1))
1760 realname = local_strdup (cached);
1761 if (realname == NULL)
1763 __close (fd);
1764 fd = -1;
1771 /* Finally, try the default path. */
1772 if (fd == -1
1773 && ((l = loader ?: _dl_loaded)
1774 /* 'l' is always != NULL for dynamically linked objects. */
1775 #ifdef SHARED
1777 #else
1778 == NULL ||
1779 #endif
1780 __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
1781 && rtld_search_dirs.dirs != (void *) -1)
1782 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
1783 &realname, &fb);
1785 /* Add another newline when we a tracing the library loading. */
1786 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1787 _dl_debug_printf ("\n");
1789 else
1791 /* The path may contain dynamic string tokens. */
1792 realname = (loader
1793 ? expand_dynamic_string_token (loader, name)
1794 : local_strdup (name));
1795 if (realname == NULL)
1796 fd = -1;
1797 else
1799 fd = open_verify (realname, &fb);
1800 if (__builtin_expect (fd, 0) == -1)
1801 free (realname);
1805 if (__builtin_expect (fd, 0) == -1)
1807 if (trace_mode)
1809 /* We haven't found an appropriate library. But since we
1810 are only interested in the list of libraries this isn't
1811 so severe. Fake an entry with all the information we
1812 have. */
1813 static const Elf_Symndx dummy_bucket = STN_UNDEF;
1815 /* Enter the new object in the list of loaded objects. */
1816 if ((name_copy = local_strdup (name)) == NULL
1817 || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
1818 _dl_signal_error (ENOMEM, name, NULL,
1819 N_("cannot create shared object descriptor"));
1820 /* Signal that this is a faked entry. */
1821 l->l_faked = 1;
1822 /* Since the descriptor is initialized with zero we do not
1823 have do this here.
1824 l->l_reserved = 0; */
1825 l->l_buckets = &dummy_bucket;
1826 l->l_nbuckets = 1;
1827 l->l_relocated = 1;
1829 return l;
1831 else
1832 _dl_signal_error (errno, name, NULL,
1833 N_("cannot open shared object file"));
1836 return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode);