(pthread_handle_create): Start the child thread with the cancel signal blocked, so...
[glibc.git] / elf / dl-load.c
blob0bc01eca206a63cca316ae87a95ef22713d263f9
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;
135 /* Get the generated information about the trusted directories. */
136 #include "trusted-dirs.h"
138 static const char system_dirs[] = SYSTEM_DIRS;
139 static const size_t system_dirs_len[] =
141 SYSTEM_DIRS_LEN
143 #define nsystem_dirs_len \
144 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
147 /* Local version of `strdup' function. */
148 static inline char *
149 local_strdup (const char *s)
151 size_t len = strlen (s) + 1;
152 void *new = malloc (len);
154 if (new == NULL)
155 return NULL;
157 return (char *) memcpy (new, s, len);
161 static size_t
162 is_dst (const char *start, const char *name, const char *str, size_t cmplen,
163 int is_path, int secure)
165 size_t len;
167 if (strncmp (name, str, cmplen) == 0)
168 len = cmplen + 1;
169 else if (strncmp (name, str + 1, cmplen - 2) == 0
170 && (name[cmplen - 2] == '\0' || name[cmplen - 2] == '/'
171 || (is_path && name[cmplen - 2] == ':')))
172 len = cmplen - 1;
173 else
174 return 0;
176 if (__builtin_expect (secure, 0)
177 && ((name[len - 1] != '\0' && (!is_path || name[len - 1] != ':'))
178 || (name != start + 1 && (!is_path || name[-2] != ':'))))
179 return 0;
181 return len;
185 size_t
186 _dl_dst_count (const char *name, int is_path)
188 const char *const start = name;
189 size_t cnt = 0;
193 size_t len = 1;
195 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
196 is $ORIGIN alone) and it must always appear first in path.
198 Note that it is no bug that the string in the second and
199 fourth `strncmp' call is longer than the sequence which is
200 actually tested. */
201 if ((len = is_dst (start, name + 1, "{ORIGIN}", 8, is_path,
202 __libc_enable_secure)) != 0
203 || ((len = is_dst (start, name + 1, "{PLATFORM}", 10, is_path, 0))
204 != 0))
205 ++cnt;
207 name = strchr (name + len, '$');
209 while (name != NULL);
211 return cnt;
215 char *
216 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
217 int is_path)
219 const char *const start = name;
220 char *last_elem, *wp;
222 /* Now fill the result path. While copying over the string we keep
223 track of the start of the last path element. When we come accross
224 a DST we copy over the value or (if the value is not available)
225 leave the entire path element out. */
226 last_elem = wp = result;
230 if (__builtin_expect (*name == '$', 0))
232 const char *repl = NULL;
233 size_t len = 1;
235 /* Note that it is no bug that the string in the second and
236 fourth `strncmp' call is longer than the sequence which
237 is actually tested. */
238 if ((len = is_dst (start, name + 1, "{ORIGIN}", 8, is_path,
239 __libc_enable_secure)) != 0)
240 repl = l->l_origin;
241 else if ((len = is_dst (start, name + 1, "{PLATFORM}", 10, is_path,
242 0)) != 0)
243 repl = _dl_platform;
245 if (repl != NULL && repl != (const char *) -1)
247 wp = __stpcpy (wp, repl);
248 name += len;
250 else if (len > 1)
252 /* We cannot use this path element, the value of the
253 replacement is unknown. */
254 wp = last_elem;
255 name += len;
256 while (*name != '\0' && (!is_path || *name != ':'))
257 ++name;
259 else
260 /* No DST we recognize. */
261 *wp++ = *name++;
263 else
265 *wp++ = *name++;
266 if (is_path && *name == ':')
267 last_elem = wp;
270 while (*name != '\0');
272 *wp = '\0';
274 return result;
278 /* Return copy of argument with all recognized dynamic string tokens
279 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
280 might not be possible to determine the path from which the object
281 belonging to the map is loaded. In this case the path element
282 containing $ORIGIN is left out. */
283 static char *
284 expand_dynamic_string_token (struct link_map *l, const char *s)
286 /* We make two runs over the string. First we determine how large the
287 resulting string is and then we copy it over. Since this is now
288 frequently executed operation we are looking here not for performance
289 but rather for code size. */
290 size_t cnt;
291 size_t total;
292 char *result;
294 /* Determine the number of DST elements. */
295 cnt = DL_DST_COUNT (s, 1);
297 /* If we do not have to replace anything simply copy the string. */
298 if (__builtin_expect (cnt, 0) == 0)
299 return local_strdup (s);
301 /* Determine the length of the substituted string. */
302 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
304 /* Allocate the necessary memory. */
305 result = (char *) malloc (total + 1);
306 if (result == NULL)
307 return NULL;
309 return DL_DST_SUBSTITUTE (l, s, result, 1);
313 /* Add `name' to the list of names for a particular shared object.
314 `name' is expected to have been allocated with malloc and will
315 be freed if the shared object already has this name.
316 Returns false if the object already had this name. */
317 static void
318 internal_function
319 add_name_to_object (struct link_map *l, const char *name)
321 struct libname_list *lnp, *lastp;
322 struct libname_list *newname;
323 size_t name_len;
325 lastp = NULL;
326 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
327 if (strcmp (name, lnp->name) == 0)
328 return;
330 name_len = strlen (name) + 1;
331 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
332 if (newname == NULL)
334 /* No more memory. */
335 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
336 return;
338 /* The object should have a libname set from _dl_new_object. */
339 assert (lastp != NULL);
341 newname->name = memcpy (newname + 1, name, name_len);
342 newname->next = NULL;
343 newname->dont_free = 0;
344 lastp->next = newname;
347 /* All known directories in sorted order. */
348 struct r_search_path_elem *_dl_all_dirs;
350 /* All directories after startup. */
351 struct r_search_path_elem *_dl_init_all_dirs;
353 /* Standard search directories. */
354 static struct r_search_path_struct rtld_search_dirs;
356 static size_t max_dirnamelen;
358 static inline struct r_search_path_elem **
359 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
360 int check_trusted, const char *what, const char *where)
362 char *cp;
363 size_t nelems = 0;
365 while ((cp = __strsep (&rpath, sep)) != NULL)
367 struct r_search_path_elem *dirp;
368 size_t len = strlen (cp);
370 /* `strsep' can pass an empty string. This has to be
371 interpreted as `use the current directory'. */
372 if (len == 0)
374 static const char curwd[] = "./";
375 cp = (char *) curwd;
378 /* Remove trailing slashes (except for "/"). */
379 while (len > 1 && cp[len - 1] == '/')
380 --len;
382 /* Now add one if there is none so far. */
383 if (len > 0 && cp[len - 1] != '/')
384 cp[len++] = '/';
386 /* Make sure we don't use untrusted directories if we run SUID. */
387 if (__builtin_expect (check_trusted, 0))
389 const char *trun = system_dirs;
390 size_t idx;
391 int unsecure = 1;
393 /* All trusted directories must be complete names. */
394 if (cp[0] == '/')
396 for (idx = 0; idx < nsystem_dirs_len; ++idx)
398 if (len == system_dirs_len[idx]
399 && memcmp (trun, cp, len) == 0)
401 /* Found it. */
402 unsecure = 0;
403 break;
406 trun += system_dirs_len[idx] + 1;
410 if (unsecure)
411 /* Simply drop this directory. */
412 continue;
415 /* See if this directory is already known. */
416 for (dirp = _dl_all_dirs; dirp != NULL; dirp = dirp->next)
417 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
418 break;
420 if (dirp != NULL)
422 /* It is available, see whether it's on our own list. */
423 size_t cnt;
424 for (cnt = 0; cnt < nelems; ++cnt)
425 if (result[cnt] == dirp)
426 break;
428 if (cnt == nelems)
429 result[nelems++] = dirp;
431 else
433 size_t cnt;
434 enum r_dir_status init_val;
435 size_t where_len = where ? strlen (where) + 1 : 0;
437 /* It's a new directory. Create an entry and add it. */
438 dirp = (struct r_search_path_elem *)
439 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
440 + where_len + len + 1);
441 if (dirp == NULL)
442 _dl_signal_error (ENOMEM, NULL, NULL,
443 N_("cannot create cache for search path"));
445 dirp->dirname = ((char *) dirp + sizeof (*dirp)
446 + ncapstr * sizeof (enum r_dir_status));
447 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
448 dirp->dirnamelen = len;
450 if (len > max_dirnamelen)
451 max_dirnamelen = len;
453 /* We have to make sure all the relative directories are
454 never ignored. The current directory might change and
455 all our saved information would be void. */
456 init_val = cp[0] != '/' ? existing : unknown;
457 for (cnt = 0; cnt < ncapstr; ++cnt)
458 dirp->status[cnt] = init_val;
460 dirp->what = what;
461 if (__builtin_expect (where != NULL, 1))
462 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
463 + ncapstr * sizeof (enum r_dir_status),
464 where, where_len);
465 else
466 dirp->where = NULL;
468 dirp->next = _dl_all_dirs;
469 _dl_all_dirs = dirp;
471 /* Put it in the result array. */
472 result[nelems++] = dirp;
476 /* Terminate the array. */
477 result[nelems] = NULL;
479 return result;
483 static void
484 internal_function
485 decompose_rpath (struct r_search_path_struct *sps,
486 const char *rpath, struct link_map *l, const char *what)
488 /* Make a copy we can work with. */
489 const char *where = l->l_name;
490 char *copy;
491 char *cp;
492 struct r_search_path_elem **result;
493 size_t nelems;
494 /* Initialize to please the compiler. */
495 const char *errstring = NULL;
497 /* First see whether we must forget the RUNPATH and RPATH from this
498 object. */
499 if (__builtin_expect (_dl_inhibit_rpath != NULL, 0) && !__libc_enable_secure)
501 const char *found = strstr (_dl_inhibit_rpath, where);
502 if (found != NULL)
504 size_t len = strlen (where);
505 if ((found == _dl_inhibit_rpath || found[-1] == ':')
506 && (found[len] == '\0' || found[len] == ':'))
508 /* This object is on the list of objects for which the
509 RUNPATH and RPATH must not be used. */
510 result = (struct r_search_path_elem **)
511 malloc (sizeof (*result));
512 if (result == NULL)
514 signal_error_cache:
515 errstring = N_("cannot create cache for search path");
516 signal_error:
517 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
520 result[0] = NULL;
522 sps->dirs = result;
523 sps->malloced = 1;
525 return;
530 /* Make a writable copy. At the same time expand possible dynamic
531 string tokens. */
532 copy = expand_dynamic_string_token (l, rpath);
533 if (copy == NULL)
535 errstring = N_("cannot create RUNPATH/RPATH copy");
536 goto signal_error;
539 /* Count the number of necessary elements in the result array. */
540 nelems = 0;
541 for (cp = copy; *cp != '\0'; ++cp)
542 if (*cp == ':')
543 ++nelems;
545 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
546 number of necessary entries. */
547 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
548 * sizeof (*result));
549 if (result == NULL)
550 goto signal_error_cache;
552 fillin_rpath (copy, result, ":", 0, what, where);
554 /* Free the copied RPATH string. `fillin_rpath' make own copies if
555 necessary. */
556 free (copy);
558 sps->dirs = result;
559 /* The caller will change this value if we haven't used a real malloc. */
560 sps->malloced = 1;
564 void
565 internal_function
566 _dl_init_paths (const char *llp)
568 size_t idx;
569 const char *strp;
570 struct r_search_path_elem *pelem, **aelem;
571 size_t round_size;
572 #ifdef SHARED
573 struct link_map *l;
574 #endif
575 /* Initialize to please the compiler. */
576 const char *errstring = NULL;
578 /* Fill in the information about the application's RPATH and the
579 directories addressed by the LD_LIBRARY_PATH environment variable. */
581 /* Get the capabilities. */
582 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
583 &ncapstr, &max_capstrlen);
585 /* First set up the rest of the default search directory entries. */
586 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
587 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
588 if (rtld_search_dirs.dirs == NULL)
590 errstring = N_("cannot create search path array");
591 signal_error:
592 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
595 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
596 + ncapstr * sizeof (enum r_dir_status))
597 / sizeof (struct r_search_path_elem));
599 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
600 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
601 * round_size * sizeof (struct r_search_path_elem));
602 if (rtld_search_dirs.dirs[0] == NULL)
604 errstring = N_("cannot create cache for search path");
605 goto signal_error;
608 rtld_search_dirs.malloced = 0;
609 pelem = _dl_all_dirs = rtld_search_dirs.dirs[0];
610 strp = system_dirs;
611 idx = 0;
615 size_t cnt;
617 *aelem++ = pelem;
619 pelem->what = "system search path";
620 pelem->where = NULL;
622 pelem->dirname = strp;
623 pelem->dirnamelen = system_dirs_len[idx];
624 strp += system_dirs_len[idx] + 1;
626 /* System paths must be absolute. */
627 assert (pelem->dirname[0] == '/');
628 for (cnt = 0; cnt < ncapstr; ++cnt)
629 pelem->status[cnt] = unknown;
631 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
633 pelem += round_size;
635 while (idx < nsystem_dirs_len);
637 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
638 *aelem = NULL;
640 #ifdef SHARED
641 /* This points to the map of the main object. */
642 l = _dl_loaded;
643 if (l != NULL)
645 assert (l->l_type != lt_loaded);
647 if (l->l_info[DT_RUNPATH])
649 /* Allocate room for the search path and fill in information
650 from RUNPATH. */
651 decompose_rpath (&l->l_runpath_dirs,
652 (const void *) (D_PTR (l, l_info[DT_STRTAB])
653 + l->l_info[DT_RUNPATH]->d_un.d_val),
654 l, "RUNPATH");
656 /* The RPATH is ignored. */
657 l->l_rpath_dirs.dirs = (void *) -1;
659 else
661 l->l_runpath_dirs.dirs = (void *) -1;
663 if (l->l_info[DT_RPATH])
665 /* Allocate room for the search path and fill in information
666 from RPATH. */
667 decompose_rpath (&l->l_rpath_dirs,
668 (const void *) (D_PTR (l, l_info[DT_STRTAB])
669 + l->l_info[DT_RPATH]->d_un.d_val),
670 l, "RPATH");
671 l->l_rpath_dirs.malloced = 0;
673 else
674 l->l_rpath_dirs.dirs = (void *) -1;
677 #endif /* SHARED */
679 if (llp != NULL && *llp != '\0')
681 size_t nllp;
682 const char *cp = llp;
683 char *llp_tmp = strdupa (llp);
685 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
686 elements it has. */
687 nllp = 1;
688 while (*cp)
690 if (*cp == ':' || *cp == ';')
691 ++nllp;
692 ++cp;
695 env_path_list.dirs = (struct r_search_path_elem **)
696 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
697 if (env_path_list.dirs == NULL)
699 errstring = N_("cannot create cache for search path");
700 goto signal_error;
703 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
704 __libc_enable_secure, "LD_LIBRARY_PATH", NULL);
706 if (env_path_list.dirs[0] == NULL)
708 free (env_path_list.dirs);
709 env_path_list.dirs = (void *) -1;
712 env_path_list.malloced = 0;
714 else
715 env_path_list.dirs = (void *) -1;
717 /* Remember the last search directory added at startup. */
718 _dl_init_all_dirs = _dl_all_dirs;
722 /* Think twice before changing anything in this function. It is placed
723 here and prepared using the `alloca' magic to prevent it from being
724 inlined. The function is only called in case of an error. But then
725 performance does not count. The function used to be "inlinable" and
726 the compiled did so all the time. This increased the code size for
727 absolutely no good reason. */
728 static void
729 __attribute__ ((noreturn))
730 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
731 const char *msg)
733 /* The use of `alloca' here looks ridiculous but it helps. The goal
734 is to avoid the function from being inlined. There is no official
735 way to do this so we use this trick. gcc never inlines functions
736 which use `alloca'. */
737 int *a = (int *) alloca (sizeof (int));
738 a[0] = fd;
739 /* The file might already be closed. */
740 if (a[0] != -1)
741 (void) __close (a[0]);
742 if (l != NULL)
744 /* Remove the stillborn object from the list and free it. */
745 assert (l->l_next == NULL);
746 #ifndef SHARED
747 if (l->l_prev == NULL)
748 /* No other module loaded. */
749 _dl_loaded = NULL;
750 else
751 #endif
752 l->l_prev->l_next = NULL;
753 --_dl_nloaded;
754 free (l);
756 free (realname);
757 _dl_signal_error (code, name, NULL, msg);
761 /* Map in the shared object NAME, actually located in REALNAME, and already
762 opened on FD. */
764 #ifndef EXTERNAL_MAP_FROM_FD
765 static
766 #endif
767 struct link_map *
768 _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
769 char *realname, struct link_map *loader, int l_type,
770 int mode)
772 struct link_map *l = NULL;
773 const ElfW(Ehdr) *header;
774 const ElfW(Phdr) *phdr;
775 const ElfW(Phdr) *ph;
776 size_t maplength;
777 int type;
778 struct stat64 st;
779 /* Initialize to keep the compiler happy. */
780 const char *errstring = NULL;
781 int errval = 0;
783 /* Get file information. */
784 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st) < 0, 0))
786 errstring = N_("cannot stat shared object");
787 call_lose_errno:
788 errval = errno;
789 call_lose:
790 lose (errval, fd, name, realname, l, errstring);
793 /* Look again to see if the real name matched another already loaded. */
794 for (l = _dl_loaded; l; l = l->l_next)
795 if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
797 /* The object is already loaded.
798 Just bump its reference count and return it. */
799 __close (fd);
801 /* If the name is not in the list of names for this object add
802 it. */
803 free (realname);
804 add_name_to_object (l, name);
806 return l;
809 if (mode & RTLD_NOLOAD)
810 /* We are not supposed to load the object unless it is already
811 loaded. So return now. */
812 return NULL;
814 /* Print debugging message. */
815 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
816 _dl_debug_printf ("file=%s; generating link map\n", name);
818 /* This is the ELF header. We read it in `open_verify'. */
819 header = (void *) fbp->buf;
821 #ifndef MAP_ANON
822 # define MAP_ANON 0
823 if (_dl_zerofd == -1)
825 _dl_zerofd = _dl_sysdep_open_zero_fill ();
826 if (_dl_zerofd == -1)
828 __close (fd);
829 _dl_signal_error (errno, NULL, NULL,
830 N_("cannot open zero fill device"));
833 #endif
835 /* Enter the new object in the list of loaded objects. */
836 l = _dl_new_object (realname, name, l_type, loader);
837 if (__builtin_expect (! l, 0))
839 errstring = N_("cannot create shared object descriptor");
840 goto call_lose_errno;
843 /* Extract the remaining details we need from the ELF header
844 and then read in the program header table. */
845 l->l_entry = header->e_entry;
846 type = header->e_type;
847 l->l_phnum = header->e_phnum;
849 maplength = header->e_phnum * sizeof (ElfW(Phdr));
850 if (header->e_phoff + maplength <= fbp->len)
851 phdr = (void *) (fbp->buf + header->e_phoff);
852 else
854 phdr = alloca (maplength);
855 __lseek (fd, SEEK_SET, header->e_phoff);
856 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
858 errstring = N_("cannot read file data");
859 goto call_lose_errno;
864 /* Scan the program header table, collecting its load commands. */
865 struct loadcmd
867 ElfW(Addr) mapstart, mapend, dataend, allocend;
868 off_t mapoff;
869 int prot;
870 } loadcmds[l->l_phnum], *c;
871 size_t nloadcmds = 0;
873 /* The struct is initialized to zero so this is not necessary:
874 l->l_ld = 0;
875 l->l_phdr = 0;
876 l->l_addr = 0; */
877 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
878 switch (ph->p_type)
880 /* These entries tell us where to find things once the file's
881 segments are mapped in. We record the addresses it says
882 verbatim, and later correct for the run-time load address. */
883 case PT_DYNAMIC:
884 l->l_ld = (void *) ph->p_vaddr;
885 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
886 break;
888 case PT_PHDR:
889 l->l_phdr = (void *) ph->p_vaddr;
890 break;
892 case PT_LOAD:
893 /* A load command tells us to map in part of the file.
894 We record the load commands and process them all later. */
895 if ((ph->p_align & (_dl_pagesize - 1)) != 0)
897 errstring = N_("ELF load command alignment not page-aligned");
898 goto call_lose;
900 if (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)
902 errstring
903 = N_("ELF load command address/offset not properly aligned");
904 goto call_lose;
908 struct loadcmd *c = &loadcmds[nloadcmds++];
909 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
910 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
911 & ~(_dl_pagesize - 1));
912 c->dataend = ph->p_vaddr + ph->p_filesz;
913 c->allocend = ph->p_vaddr + ph->p_memsz;
914 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
916 /* Optimize a common case. */
917 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
918 c->prot = (PF_TO_PROT
919 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
920 #else
921 c->prot = 0;
922 if (ph->p_flags & PF_R)
923 c->prot |= PROT_READ;
924 if (ph->p_flags & PF_W)
925 c->prot |= PROT_WRITE;
926 if (ph->p_flags & PF_X)
927 c->prot |= PROT_EXEC;
928 #endif
930 break;
933 /* Now process the load commands and map segments into memory. */
934 c = loadcmds;
936 /* Length of the sections to be loaded. */
937 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
939 if (__builtin_expect (type, ET_DYN) == ET_DYN)
941 /* This is a position-independent shared object. We can let the
942 kernel map it anywhere it likes, but we must have space for all
943 the segments in their specified positions relative to the first.
944 So we map the first segment without MAP_FIXED, but with its
945 extent increased to cover all the segments. Then we remove
946 access from excess portion, and there is known sufficient space
947 there to remap from the later segments.
949 As a refinement, sometimes we have an address that we would
950 prefer to map such objects at; but this is only a preference,
951 the OS can do whatever it likes. */
952 ElfW(Addr) mappref;
953 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
954 - MAP_BASE_ADDR (l));
956 /* Remember which part of the address space this object uses. */
957 l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
958 c->prot, MAP_COPY | MAP_FILE,
959 fd, c->mapoff);
960 if ((void *) l->l_map_start == MAP_FAILED)
962 map_error:
963 errstring = N_("failed to map segment from shared object");
964 goto call_lose_errno;
967 l->l_map_end = l->l_map_start + maplength;
968 l->l_addr = l->l_map_start - c->mapstart;
970 /* Change protection on the excess portion to disallow all access;
971 the portions we do not remap later will be inaccessible as if
972 unallocated. Then jump into the normal segment-mapping loop to
973 handle the portion of the segment past the end of the file
974 mapping. */
975 __mprotect ((caddr_t) (l->l_addr + c->mapend),
976 loadcmds[nloadcmds - 1].allocend - c->mapend,
977 PROT_NONE);
979 goto postmap;
981 else
983 /* This object is loaded at a fixed address. This must never
984 happen for objects loaded with dlopen(). */
985 if (__builtin_expect (mode & __RTLD_DLOPEN, 0))
987 errstring = N_("cannot dynamically load executable");
988 goto call_lose;
991 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
992 fixed. */
993 ELF_FIXED_ADDRESS (loader, c->mapstart);
996 /* Remember which part of the address space this object uses. */
997 l->l_map_start = c->mapstart + l->l_addr;
998 l->l_map_end = l->l_map_start + maplength;
1000 while (c < &loadcmds[nloadcmds])
1002 if (c->mapend > c->mapstart
1003 /* Map the segment contents from the file. */
1004 && (__mmap ((void *) (l->l_addr + c->mapstart),
1005 c->mapend - c->mapstart, c->prot,
1006 MAP_FIXED | MAP_COPY | MAP_FILE, fd, c->mapoff)
1007 == MAP_FAILED))
1008 goto map_error;
1010 postmap:
1011 if (l->l_phdr == 0
1012 && c->mapoff <= header->e_phoff
1013 && (c->mapend - c->mapstart + c->mapoff
1014 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1015 /* Found the program header in this segment. */
1016 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
1018 if (c->allocend > c->dataend)
1020 /* Extra zero pages should appear at the end of this segment,
1021 after the data mapped from the file. */
1022 ElfW(Addr) zero, zeroend, zeropage;
1024 zero = l->l_addr + c->dataend;
1025 zeroend = l->l_addr + c->allocend;
1026 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
1028 if (zeroend < zeropage)
1029 /* All the extra data is in the last page of the segment.
1030 We can just zero it. */
1031 zeropage = zeroend;
1033 if (zeropage > zero)
1035 /* Zero the final part of the last page of the segment. */
1036 if ((c->prot & PROT_WRITE) == 0)
1038 /* Dag nab it. */
1039 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1040 _dl_pagesize, c->prot|PROT_WRITE) < 0)
1042 errstring = N_("cannot change memory protections");
1043 goto call_lose_errno;
1046 memset ((void *) zero, '\0', zeropage - zero);
1047 if ((c->prot & PROT_WRITE) == 0)
1048 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1049 _dl_pagesize, c->prot);
1052 if (zeroend > zeropage)
1054 /* Map the remaining zero pages in from the zero fill FD. */
1055 caddr_t mapat;
1056 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1057 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1058 ANONFD, 0);
1059 if (mapat == MAP_FAILED)
1061 errstring = N_("cannot map zero-fill pages");
1062 goto call_lose_errno;
1067 ++c;
1070 if (l->l_phdr == NULL)
1072 /* The program header is not contained in any of the segments.
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)
1079 errstring = N_("cannot allocate memory for program header");
1080 goto call_lose_errno;
1083 l->l_phdr = memcpy (newp, phdr,
1084 (header->e_phnum * sizeof (ElfW(Phdr))));
1085 l->l_phdr_allocated = 1;
1087 else
1088 /* Adjust the PT_PHDR value by the runtime load address. */
1089 (ElfW(Addr)) l->l_phdr += l->l_addr;
1092 /* We are done mapping in the file. We no longer need the descriptor. */
1093 __close (fd);
1094 /* Signal that we closed the file. */
1095 fd = -1;
1097 if (l->l_type == lt_library && type == ET_EXEC)
1098 l->l_type = lt_executable;
1100 if (l->l_ld == 0)
1102 if (type == ET_DYN)
1104 errstring = N_("object file has no dynamic section");
1105 goto call_lose;
1108 else
1109 (ElfW(Addr)) l->l_ld += l->l_addr;
1111 l->l_entry += l->l_addr;
1113 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
1114 _dl_debug_printf (" dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n"
1115 " entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1116 (int) sizeof (void *) * 2, (unsigned long int) l->l_ld,
1117 (int) sizeof (void *) * 2, (unsigned long int) l->l_addr,
1118 (int) sizeof (void *) * 2, maplength,
1119 (int) sizeof (void *) * 2, (unsigned long int) l->l_entry,
1120 (int) sizeof (void *) * 2, (unsigned long int) l->l_phdr,
1121 (int) sizeof (void *) * 2, l->l_phnum);
1123 elf_get_dynamic_info (l);
1125 /* Make sure we are dlopen()ing an object which has the DF_1_NOOPEN
1126 flag set. */
1127 if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1128 && (mode & __RTLD_DLOPEN))
1130 /* We are not supposed to load this object. Free all resources. */
1131 __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1133 if (!l->l_libname->dont_free)
1134 free (l->l_libname);
1136 if (l->l_phdr_allocated)
1137 free ((void *) l->l_phdr);
1139 errstring = N_("shared object cannot be dlopen()ed");
1140 goto call_lose;
1143 if (l->l_info[DT_HASH])
1144 _dl_setup_hash (l);
1146 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1147 have to do this for the main map. */
1148 if (__builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0)
1149 && &l->l_searchlist != l->l_scope[0])
1151 /* Create an appropriate searchlist. It contains only this map.
1153 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1154 GNU ld.so implementation had a different interpretation which
1155 is more reasonable. We are prepared to add this possibility
1156 back as part of a GNU extension of the ELF format. */
1157 l->l_symbolic_searchlist.r_list =
1158 (struct link_map **) malloc (sizeof (struct link_map *));
1160 if (l->l_symbolic_searchlist.r_list == NULL)
1162 errstring = N_("cannot create searchlist");
1163 goto call_lose_errno;
1166 l->l_symbolic_searchlist.r_list[0] = l;
1167 l->l_symbolic_searchlist.r_nlist = 1;
1169 /* Now move the existing entries one back. */
1170 memmove (&l->l_scope[1], &l->l_scope[0],
1171 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1173 /* Now add the new entry. */
1174 l->l_scope[0] = &l->l_symbolic_searchlist;
1177 /* Remember whether this object must be initialized first. */
1178 if (l->l_flags_1 & DF_1_INITFIRST)
1179 _dl_initfirst = l;
1181 /* Finally the file information. */
1182 l->l_dev = st.st_dev;
1183 l->l_ino = st.st_ino;
1185 return l;
1188 /* Print search path. */
1189 static void
1190 print_search_path (struct r_search_path_elem **list,
1191 const char *what, const char *name)
1193 char buf[max_dirnamelen + max_capstrlen];
1194 int first = 1;
1196 _dl_debug_printf (" search path=");
1198 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1200 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1201 size_t cnt;
1203 for (cnt = 0; cnt < ncapstr; ++cnt)
1204 if ((*list)->status[cnt] != nonexisting)
1206 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1207 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1208 cp[0] = '\0';
1209 else
1210 cp[-1] = '\0';
1212 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1213 first = 0;
1216 ++list;
1219 if (name != NULL)
1220 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1221 name[0] ? name : _dl_argv[0]);
1222 else
1223 _dl_debug_printf_c ("\t\t(%s)\n", what);
1226 /* Open a file and verify it is an ELF file for this architecture. We
1227 ignore only ELF files for other architectures. Non-ELF files and
1228 ELF files with different header information cause fatal errors since
1229 this could mean there is something wrong in the installation and the
1230 user might want to know about this. */
1231 static int
1232 open_verify (const char *name, struct filebuf *fbp)
1234 /* This is the expected ELF header. */
1235 #define ELF32_CLASS ELFCLASS32
1236 #define ELF64_CLASS ELFCLASS64
1237 #ifndef VALID_ELF_HEADER
1238 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1239 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1240 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1241 #endif
1242 static const unsigned char expected[EI_PAD] =
1244 [EI_MAG0] = ELFMAG0,
1245 [EI_MAG1] = ELFMAG1,
1246 [EI_MAG2] = ELFMAG2,
1247 [EI_MAG3] = ELFMAG3,
1248 [EI_CLASS] = ELFW(CLASS),
1249 [EI_DATA] = byteorder,
1250 [EI_VERSION] = EV_CURRENT,
1251 [EI_OSABI] = ELFOSABI_SYSV,
1252 [EI_ABIVERSION] = 0
1254 static const struct
1256 ElfW(Word) vendorlen;
1257 ElfW(Word) datalen;
1258 ElfW(Word) type;
1259 char vendor[4];
1260 } expected_note = { 4, 16, 1, "GNU" };
1261 int fd;
1262 /* Initialize it to make the compiler happy. */
1263 const char *errstring = NULL;
1264 int errval = 0;
1266 /* Open the file. We always open files read-only. */
1267 fd = __open (name, O_RDONLY);
1268 if (fd != -1)
1270 ElfW(Ehdr) *ehdr;
1271 ElfW(Phdr) *phdr, *ph;
1272 ElfW(Word) *abi_note, abi_note_buf[8];
1273 unsigned int osversion;
1274 size_t maplength;
1276 /* We successfully openened the file. Now verify it is a file
1277 we can use. */
1278 __set_errno (0);
1279 fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf));
1281 /* This is where the ELF header is loaded. */
1282 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1283 ehdr = (ElfW(Ehdr) *) fbp->buf;
1285 /* Now run the tests. */
1286 if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0))
1288 errval = errno;
1289 errstring = (errval == 0
1290 ? N_("file too short") : N_("cannot read file data"));
1291 call_lose:
1292 lose (errval, fd, name, NULL, NULL, errstring);
1295 /* See whether the ELF header is what we expect. */
1296 if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1297 EI_PAD), 0))
1299 /* Something is wrong. */
1300 if (*(Elf32_Word *) &ehdr->e_ident !=
1301 #if BYTE_ORDER == LITTLE_ENDIAN
1302 ((ELFMAG0 << (EI_MAG0 * 8)) |
1303 (ELFMAG1 << (EI_MAG1 * 8)) |
1304 (ELFMAG2 << (EI_MAG2 * 8)) |
1305 (ELFMAG3 << (EI_MAG3 * 8)))
1306 #else
1307 ((ELFMAG0 << (EI_MAG3 * 8)) |
1308 (ELFMAG1 << (EI_MAG2 * 8)) |
1309 (ELFMAG2 << (EI_MAG1 * 8)) |
1310 (ELFMAG3 << (EI_MAG0 * 8)))
1311 #endif
1313 errstring = N_("invalid ELF header");
1314 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1315 /* This is not a fatal error. On architectures where
1316 32-bit and 64-bit binaries can be run this might
1317 happen. */
1318 goto close_and_out;
1319 else if (ehdr->e_ident[EI_DATA] != byteorder)
1321 if (BYTE_ORDER == BIG_ENDIAN)
1322 errstring = N_("ELF file data encoding not big-endian");
1323 else
1324 errstring = N_("ELF file data encoding not little-endian");
1326 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1327 errstring
1328 = N_("ELF file version ident does not match current one");
1329 /* XXX We should be able so set system specific versions which are
1330 allowed here. */
1331 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1332 errstring = N_("ELF file OS ABI invalid");
1333 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION]))
1334 errstring = N_("ELF file ABI version invalid");
1335 else
1336 /* Otherwise we don't know what went wrong. */
1337 errstring = N_("internal error");
1339 goto call_lose;
1342 if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT)
1344 errstring = N_("ELF file version does not match current one");
1345 goto call_lose;
1347 if (! __builtin_expect (elf_machine_matches_host (ehdr), 1))
1348 goto close_and_out;
1349 else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
1350 != sizeof (ElfW(Phdr)))
1352 errstring = N_("ELF file's phentsize not the expected size");
1353 goto call_lose;
1355 else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN
1356 && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC)
1358 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1359 goto call_lose;
1362 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1363 if (ehdr->e_phoff + maplength <= fbp->len)
1364 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1365 else
1367 phdr = alloca (maplength);
1368 __lseek (fd, SEEK_SET, ehdr->e_phoff);
1369 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
1371 read_error:
1372 errval = errno;
1373 errstring = N_("cannot read file data");
1374 goto call_lose;
1378 /* Check .note.ABI-tag if present. */
1379 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1380 if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
1382 if (ph->p_offset + 32 <= fbp->len)
1383 abi_note = (void *) (fbp->buf + ph->p_offset);
1384 else
1386 __lseek (fd, SEEK_SET, ph->p_offset);
1387 if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
1388 goto read_error;
1390 abi_note = abi_note_buf;
1393 if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1394 continue;
1396 osversion = (abi_note[5] & 0xff) * 65536
1397 + (abi_note[6] & 0xff) * 256
1398 + (abi_note[7] & 0xff);
1399 if (abi_note[4] != __ABI_TAG_OS
1400 || (_dl_osversion && _dl_osversion < osversion))
1402 close_and_out:
1403 __close (fd);
1404 __set_errno (ENOENT);
1405 fd = -1;
1408 break;
1412 return fd;
1415 /* Try to open NAME in one of the directories in *DIRSP.
1416 Return the fd, or -1. If successful, fill in *REALNAME
1417 with the malloc'd full directory name. If it turns out
1418 that none of the directories in *DIRSP exists, *DIRSP is
1419 replaced with (void *) -1, and the old value is free()d
1420 if MAY_FREE_DIRS is true. */
1422 static int
1423 open_path (const char *name, size_t namelen, int preloaded,
1424 struct r_search_path_struct *sps, char **realname,
1425 struct filebuf *fbp)
1427 struct r_search_path_elem **dirs = sps->dirs;
1428 char *buf;
1429 int fd = -1;
1430 const char *current_what = NULL;
1431 int any = 0;
1433 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1436 struct r_search_path_elem *this_dir = *dirs;
1437 size_t buflen = 0;
1438 size_t cnt;
1439 char *edp;
1440 int here_any = 0;
1441 int err;
1443 /* If we are debugging the search for libraries print the path
1444 now if it hasn't happened now. */
1445 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0)
1446 && current_what != this_dir->what)
1448 current_what = this_dir->what;
1449 print_search_path (dirs, current_what, this_dir->where);
1452 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1453 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1455 /* Skip this directory if we know it does not exist. */
1456 if (this_dir->status[cnt] == nonexisting)
1457 continue;
1459 buflen =
1460 ((char *) __mempcpy (__mempcpy (edp,
1461 capstr[cnt].str, capstr[cnt].len),
1462 name, namelen)
1463 - buf);
1465 /* Print name we try if this is wanted. */
1466 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1467 _dl_debug_printf (" trying file=%s\n", buf);
1469 fd = open_verify (buf, fbp);
1470 if (this_dir->status[cnt] == unknown)
1472 if (fd != -1)
1473 this_dir->status[cnt] = existing;
1474 else
1476 /* We failed to open machine dependent library. Let's
1477 test whether there is any directory at all. */
1478 struct stat64 st;
1480 buf[buflen - namelen - 1] = '\0';
1482 if (__xstat64 (_STAT_VER, buf, &st) != 0
1483 || ! S_ISDIR (st.st_mode))
1484 /* The directory does not exist or it is no directory. */
1485 this_dir->status[cnt] = nonexisting;
1486 else
1487 this_dir->status[cnt] = existing;
1491 /* Remember whether we found any existing directory. */
1492 here_any |= this_dir->status[cnt] == existing;
1494 if (fd != -1 && __builtin_expect (preloaded, 0)
1495 && __libc_enable_secure)
1497 /* This is an extra security effort to make sure nobody can
1498 preload broken shared objects which are in the trusted
1499 directories and so exploit the bugs. */
1500 struct stat64 st;
1502 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1503 || (st.st_mode & S_ISUID) == 0)
1505 /* The shared object cannot be tested for being SUID
1506 or this bit is not set. In this case we must not
1507 use this object. */
1508 __close (fd);
1509 fd = -1;
1510 /* We simply ignore the file, signal this by setting
1511 the error value which would have been set by `open'. */
1512 errno = ENOENT;
1517 if (fd != -1)
1519 *realname = (char *) malloc (buflen);
1520 if (*realname != NULL)
1522 memcpy (*realname, buf, buflen);
1523 return fd;
1525 else
1527 /* No memory for the name, we certainly won't be able
1528 to load and link it. */
1529 __close (fd);
1530 return -1;
1533 if (here_any && (err = errno) != ENOENT && err != EACCES)
1534 /* The file exists and is readable, but something went wrong. */
1535 return -1;
1537 /* Remember whether we found anything. */
1538 any |= here_any;
1540 while (*++dirs != NULL);
1542 /* Remove the whole path if none of the directories exists. */
1543 if (__builtin_expect (! any, 0))
1545 /* Paths which were allocated using the minimal malloc() in ld.so
1546 must not be freed using the general free() in libc. */
1547 if (sps->malloced)
1548 free (sps->dirs);
1549 sps->dirs = (void *) -1;
1552 return -1;
1555 /* Map in the shared object file NAME. */
1557 struct link_map *
1558 internal_function
1559 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1560 int type, int trace_mode, int mode)
1562 int fd;
1563 char *realname;
1564 char *name_copy;
1565 struct link_map *l;
1566 struct filebuf fb;
1568 /* Look for this name among those already loaded. */
1569 for (l = _dl_loaded; l; l = l->l_next)
1571 /* If the requested name matches the soname of a loaded object,
1572 use that object. Elide this check for names that have not
1573 yet been opened. */
1574 if (__builtin_expect (l->l_faked, 0) != 0)
1575 continue;
1576 if (!_dl_name_match_p (name, l))
1578 const char *soname;
1580 if (__builtin_expect (l->l_soname_added, 1)
1581 || l->l_info[DT_SONAME] == NULL)
1582 continue;
1584 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1585 + l->l_info[DT_SONAME]->d_un.d_val);
1586 if (strcmp (name, soname) != 0)
1587 continue;
1589 /* We have a match on a new name -- cache it. */
1590 add_name_to_object (l, soname);
1591 l->l_soname_added = 1;
1594 /* We have a match. */
1595 return l;
1598 /* Display information if we are debugging. */
1599 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0) && loader != NULL)
1600 _dl_debug_printf ("\nfile=%s; needed by %s\n", name,
1601 loader->l_name[0] ? loader->l_name : _dl_argv[0]);
1603 if (strchr (name, '/') == NULL)
1605 /* Search for NAME in several places. */
1607 size_t namelen = strlen (name) + 1;
1609 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1610 _dl_debug_printf ("find library=%s; searching\n", name);
1612 fd = -1;
1614 /* When the object has the RUNPATH information we don't use any
1615 RPATHs. */
1616 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1618 /* First try the DT_RPATH of the dependent object that caused NAME
1619 to be loaded. Then that object's dependent, and on up. */
1620 for (l = loader; fd == -1 && l; l = l->l_loader)
1622 if (l->l_rpath_dirs.dirs == NULL)
1624 if (l->l_info[DT_RPATH] == NULL)
1626 /* There is no path. */
1627 l->l_rpath_dirs.dirs = (void *) -1;
1628 continue;
1630 else
1632 /* Make sure the cache information is available. */
1633 size_t ptrval = (D_PTR (l, l_info[DT_STRTAB])
1634 + l->l_info[DT_RPATH]->d_un.d_val);
1635 decompose_rpath (&l->l_rpath_dirs,
1636 (const char *) ptrval, l, "RPATH");
1640 if (l->l_rpath_dirs.dirs != (void *) -1)
1641 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1642 &realname, &fb);
1645 /* If dynamically linked, try the DT_RPATH of the executable
1646 itself. */
1647 l = _dl_loaded;
1648 if (fd == -1 && l && l->l_type != lt_loaded && l != loader
1649 && l->l_rpath_dirs.dirs != (void *) -1)
1650 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1651 &realname, &fb);
1654 /* Try the LD_LIBRARY_PATH environment variable. */
1655 if (fd == -1 && env_path_list.dirs != (void *) -1)
1656 fd = open_path (name, namelen, preloaded, &env_path_list,
1657 &realname, &fb);
1659 /* Look at the RUNPATH information for this binary.
1661 Note that this is no real loop. 'while' is used only to enable
1662 us to use 'break' instead of a 'goto' to jump to the end. The
1663 loop is always left after the first round. */
1664 while (fd == -1 && loader != NULL
1665 && loader->l_runpath_dirs.dirs != (void *) -1)
1667 if (loader->l_runpath_dirs.dirs == NULL)
1669 if (loader->l_info[DT_RUNPATH] == NULL)
1671 /* No RUNPATH. */
1672 loader->l_runpath_dirs.dirs = (void *) -1;
1673 break;
1675 else
1677 /* Make sure the cache information is available. */
1678 size_t ptrval = (D_PTR (loader, l_info[DT_STRTAB])
1679 + loader->l_info[DT_RUNPATH]->d_un.d_val);
1680 decompose_rpath (&loader->l_runpath_dirs,
1681 (const char *) ptrval, loader, "RUNPATH");
1685 if (loader->l_runpath_dirs.dirs != (void *) -1)
1686 fd = open_path (name, namelen, preloaded,
1687 &loader->l_runpath_dirs, &realname, &fb);
1688 break;
1691 if (fd == -1
1692 && (__builtin_expect (! preloaded, 1) || ! __libc_enable_secure))
1694 /* Check the list of libraries in the file /etc/ld.so.cache,
1695 for compatibility with Linux's ldconfig program. */
1696 const char *cached = _dl_load_cache_lookup (name);
1698 if (cached != NULL)
1700 #ifdef SHARED
1701 l = loader ?: _dl_loaded;
1702 #else
1703 l = loader;
1704 #endif
1706 /* If the loader has the DF_1_NODEFLIB flag set we must not
1707 use a cache entry from any of these directories. */
1708 if (
1709 #ifndef SHARED
1710 /* 'l' is always != NULL for dynamically linked objects. */
1711 l != NULL &&
1712 #endif
1713 __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
1715 const char *dirp = system_dirs;
1716 unsigned int cnt = 0;
1720 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
1722 /* The prefix matches. Don't use the entry. */
1723 cached = NULL;
1724 break;
1727 dirp += system_dirs_len[cnt] + 1;
1728 ++cnt;
1730 while (cnt < nsystem_dirs_len);
1733 if (cached != NULL)
1735 fd = open_verify (cached, &fb);
1736 if (__builtin_expect (fd != -1, 1))
1738 realname = local_strdup (cached);
1739 if (realname == NULL)
1741 __close (fd);
1742 fd = -1;
1749 /* Finally, try the default path. */
1750 if (fd == -1
1751 && ((l = loader ?: _dl_loaded)
1752 /* 'l' is always != NULL for dynamically linked objects. */
1753 #ifdef SHARED
1755 #else
1756 == NULL ||
1757 #endif
1758 __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
1759 && rtld_search_dirs.dirs != (void *) -1)
1760 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
1761 &realname, &fb);
1763 /* Add another newline when we a tracing the library loading. */
1764 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1765 _dl_debug_printf ("\n");
1767 else
1769 /* The path may contain dynamic string tokens. */
1770 realname = (loader
1771 ? expand_dynamic_string_token (loader, name)
1772 : local_strdup (name));
1773 if (realname == NULL)
1774 fd = -1;
1775 else
1777 fd = open_verify (realname, &fb);
1778 if (__builtin_expect (fd, 0) == -1)
1779 free (realname);
1783 if (__builtin_expect (fd, 0) == -1)
1785 if (trace_mode)
1787 /* We haven't found an appropriate library. But since we
1788 are only interested in the list of libraries this isn't
1789 so severe. Fake an entry with all the information we
1790 have. */
1791 static const Elf_Symndx dummy_bucket = STN_UNDEF;
1793 /* Enter the new object in the list of loaded objects. */
1794 if ((name_copy = local_strdup (name)) == NULL
1795 || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
1796 _dl_signal_error (ENOMEM, name, NULL,
1797 N_("cannot create shared object descriptor"));
1798 /* Signal that this is a faked entry. */
1799 l->l_faked = 1;
1800 /* Since the descriptor is initialized with zero we do not
1801 have do this here.
1802 l->l_reserved = 0; */
1803 l->l_buckets = &dummy_bucket;
1804 l->l_nbuckets = 1;
1805 l->l_relocated = 1;
1807 return l;
1809 else
1810 _dl_signal_error (errno, name, NULL,
1811 N_("cannot open shared object file"));
1814 return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode);