hurd: fix _hurd_self_sigstate reference from ____longjmp_chk
[glibc.git] / elf / dl-load.c
blobc0d6249373fb4cb1b5971fe953cbcbf6ae28c1f4
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2016 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <elf.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <libintl.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <ldsodefs.h>
28 #include <bits/wordsize.h>
29 #include <sys/mman.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include "dynamic-link.h"
34 #include <abi-tag.h>
35 #include <stackinfo.h>
36 #include <caller.h>
37 #include <sysdep.h>
38 #include <stap-probe.h>
39 #include <libc-internal.h>
41 #include <dl-dst.h>
42 #include <dl-load.h>
43 #include <dl-map-segments.h>
44 #include <dl-unmap-segments.h>
45 #include <dl-machine-reject-phdr.h>
46 #include <dl-sysdep-open.h>
49 #include <endian.h>
50 #if BYTE_ORDER == BIG_ENDIAN
51 # define byteorder ELFDATA2MSB
52 #elif BYTE_ORDER == LITTLE_ENDIAN
53 # define byteorder ELFDATA2LSB
54 #else
55 # error "Unknown BYTE_ORDER " BYTE_ORDER
56 # define byteorder ELFDATANONE
57 #endif
59 #define STRING(x) __STRING (x)
62 int __stack_prot attribute_hidden attribute_relro
63 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
64 = PROT_GROWSDOWN;
65 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
66 = PROT_GROWSUP;
67 #else
68 = 0;
69 #endif
72 /* Type for the buffer we put the ELF header and hopefully the program
73 header. This buffer does not really have to be too large. In most
74 cases the program header follows the ELF header directly. If this
75 is not the case all bets are off and we can make the header
76 arbitrarily large and still won't get it read. This means the only
77 question is how large are the ELF and program header combined. The
78 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
79 bytes long. Each program header entry is again 32 and 56 bytes
80 long respectively. I.e., even with a file which has 10 program
81 header entries we only have to read 372B/624B respectively. Add to
82 this a bit of margin for program notes and reading 512B and 832B
83 for 32-bit and 64-bit files respecitvely is enough. If this
84 heuristic should really fail for some file the code in
85 `_dl_map_object_from_fd' knows how to recover. */
86 struct filebuf
88 ssize_t len;
89 #if __WORDSIZE == 32
90 # define FILEBUF_SIZE 512
91 #else
92 # define FILEBUF_SIZE 832
93 #endif
94 char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr)))));
97 /* This is the decomposed LD_LIBRARY_PATH search path. */
98 static struct r_search_path_struct env_path_list attribute_relro;
100 /* List of the hardware capabilities we might end up using. */
101 static const struct r_strlenpair *capstr attribute_relro;
102 static size_t ncapstr attribute_relro;
103 static size_t max_capstrlen attribute_relro;
106 /* Get the generated information about the trusted directories. */
107 #include "trusted-dirs.h"
109 static const char system_dirs[] = SYSTEM_DIRS;
110 static const size_t system_dirs_len[] =
112 SYSTEM_DIRS_LEN
114 #define nsystem_dirs_len \
115 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
118 static bool
119 is_trusted_path (const char *path, size_t len)
121 const char *trun = system_dirs;
123 for (size_t idx = 0; idx < nsystem_dirs_len; ++idx)
125 if (len == system_dirs_len[idx] && memcmp (trun, path, len) == 0)
126 /* Found it. */
127 return true;
129 trun += system_dirs_len[idx] + 1;
132 return false;
136 static bool
137 is_trusted_path_normalize (const char *path, size_t len)
139 if (len == 0)
140 return false;
142 if (*path == ':')
144 ++path;
145 --len;
148 char *npath = (char *) alloca (len + 2);
149 char *wnp = npath;
150 while (*path != '\0')
152 if (path[0] == '/')
154 if (path[1] == '.')
156 if (path[2] == '.' && (path[3] == '/' || path[3] == '\0'))
158 while (wnp > npath && *--wnp != '/')
160 path += 3;
161 continue;
163 else if (path[2] == '/' || path[2] == '\0')
165 path += 2;
166 continue;
170 if (wnp > npath && wnp[-1] == '/')
172 ++path;
173 continue;
177 *wnp++ = *path++;
180 if (wnp == npath || wnp[-1] != '/')
181 *wnp++ = '/';
183 const char *trun = system_dirs;
185 for (size_t idx = 0; idx < nsystem_dirs_len; ++idx)
187 if (wnp - npath >= system_dirs_len[idx]
188 && memcmp (trun, npath, system_dirs_len[idx]) == 0)
189 /* Found it. */
190 return true;
192 trun += system_dirs_len[idx] + 1;
195 return false;
199 static size_t
200 is_dst (const char *start, const char *name, const char *str,
201 int is_path, int secure)
203 size_t len;
204 bool is_curly = false;
206 if (name[0] == '{')
208 is_curly = true;
209 ++name;
212 len = 0;
213 while (name[len] == str[len] && name[len] != '\0')
214 ++len;
216 if (is_curly)
218 if (name[len] != '}')
219 return 0;
221 /* Point again at the beginning of the name. */
222 --name;
223 /* Skip over closing curly brace and adjust for the --name. */
224 len += 2;
226 else if (name[len] != '\0' && name[len] != '/'
227 && (!is_path || name[len] != ':'))
228 return 0;
230 if (__glibc_unlikely (secure)
231 && ((name[len] != '\0' && name[len] != '/'
232 && (!is_path || name[len] != ':'))
233 || (name != start + 1 && (!is_path || name[-2] != ':'))))
234 return 0;
236 return len;
240 size_t
241 _dl_dst_count (const char *name, int is_path)
243 const char *const start = name;
244 size_t cnt = 0;
248 size_t len;
250 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
251 is $ORIGIN alone) and it must always appear first in path. */
252 ++name;
253 if ((len = is_dst (start, name, "ORIGIN", is_path,
254 __libc_enable_secure)) != 0
255 || (len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0
256 || (len = is_dst (start, name, "LIB", is_path, 0)) != 0)
257 ++cnt;
259 name = strchr (name + len, '$');
261 while (name != NULL);
263 return cnt;
267 char *
268 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
269 int is_path)
271 const char *const start = name;
273 /* Now fill the result path. While copying over the string we keep
274 track of the start of the last path element. When we come across
275 a DST we copy over the value or (if the value is not available)
276 leave the entire path element out. */
277 char *wp = result;
278 char *last_elem = result;
279 bool check_for_trusted = false;
283 if (__glibc_unlikely (*name == '$'))
285 const char *repl = NULL;
286 size_t len;
288 ++name;
289 if ((len = is_dst (start, name, "ORIGIN", is_path,
290 __libc_enable_secure)) != 0)
292 repl = l->l_origin;
293 check_for_trusted = (__libc_enable_secure
294 && l->l_type == lt_executable);
296 else if ((len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0)
297 repl = GLRO(dl_platform);
298 else if ((len = is_dst (start, name, "LIB", is_path, 0)) != 0)
299 repl = DL_DST_LIB;
301 if (repl != NULL && repl != (const char *) -1)
303 wp = __stpcpy (wp, repl);
304 name += len;
306 else if (len > 1)
308 /* We cannot use this path element, the value of the
309 replacement is unknown. */
310 wp = last_elem;
311 name += len;
312 while (*name != '\0' && (!is_path || *name != ':'))
313 ++name;
314 /* Also skip following colon if this is the first rpath
315 element, but keep an empty element at the end. */
316 if (wp == result && is_path && *name == ':' && name[1] != '\0')
317 ++name;
319 else
320 /* No DST we recognize. */
321 *wp++ = '$';
323 else
325 *wp++ = *name++;
326 if (is_path && *name == ':')
328 /* In SUID/SGID programs, after $ORIGIN expansion the
329 normalized path must be rooted in one of the trusted
330 directories. */
331 if (__glibc_unlikely (check_for_trusted)
332 && !is_trusted_path_normalize (last_elem, wp - last_elem))
333 wp = last_elem;
334 else
335 last_elem = wp;
337 check_for_trusted = false;
341 while (*name != '\0');
343 /* In SUID/SGID programs, after $ORIGIN expansion the normalized
344 path must be rooted in one of the trusted directories. */
345 if (__glibc_unlikely (check_for_trusted)
346 && !is_trusted_path_normalize (last_elem, wp - last_elem))
347 wp = last_elem;
349 *wp = '\0';
351 return result;
355 /* Return copy of argument with all recognized dynamic string tokens
356 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
357 might not be possible to determine the path from which the object
358 belonging to the map is loaded. In this case the path element
359 containing $ORIGIN is left out. */
360 static char *
361 expand_dynamic_string_token (struct link_map *l, const char *s, int is_path)
363 /* We make two runs over the string. First we determine how large the
364 resulting string is and then we copy it over. Since this is no
365 frequently executed operation we are looking here not for performance
366 but rather for code size. */
367 size_t cnt;
368 size_t total;
369 char *result;
371 /* Determine the number of DST elements. */
372 cnt = DL_DST_COUNT (s, is_path);
374 /* If we do not have to replace anything simply copy the string. */
375 if (__glibc_likely (cnt == 0))
376 return __strdup (s);
378 /* Determine the length of the substituted string. */
379 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
381 /* Allocate the necessary memory. */
382 result = (char *) malloc (total + 1);
383 if (result == NULL)
384 return NULL;
386 return _dl_dst_substitute (l, s, result, is_path);
390 /* Add `name' to the list of names for a particular shared object.
391 `name' is expected to have been allocated with malloc and will
392 be freed if the shared object already has this name.
393 Returns false if the object already had this name. */
394 static void
395 internal_function
396 add_name_to_object (struct link_map *l, const char *name)
398 struct libname_list *lnp, *lastp;
399 struct libname_list *newname;
400 size_t name_len;
402 lastp = NULL;
403 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
404 if (strcmp (name, lnp->name) == 0)
405 return;
407 name_len = strlen (name) + 1;
408 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
409 if (newname == NULL)
411 /* No more memory. */
412 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
413 return;
415 /* The object should have a libname set from _dl_new_object. */
416 assert (lastp != NULL);
418 newname->name = memcpy (newname + 1, name, name_len);
419 newname->next = NULL;
420 newname->dont_free = 0;
421 lastp->next = newname;
424 /* Standard search directories. */
425 static struct r_search_path_struct rtld_search_dirs attribute_relro;
427 static size_t max_dirnamelen;
429 static struct r_search_path_elem **
430 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
431 int check_trusted, const char *what, const char *where,
432 struct link_map *l)
434 char *cp;
435 size_t nelems = 0;
436 char *to_free;
438 while ((cp = __strsep (&rpath, sep)) != NULL)
440 struct r_search_path_elem *dirp;
442 to_free = cp = expand_dynamic_string_token (l, cp, 1);
444 size_t len = strlen (cp);
446 /* `strsep' can pass an empty string. This has to be
447 interpreted as `use the current directory'. */
448 if (len == 0)
450 static const char curwd[] = "./";
451 cp = (char *) curwd;
454 /* Remove trailing slashes (except for "/"). */
455 while (len > 1 && cp[len - 1] == '/')
456 --len;
458 /* Now add one if there is none so far. */
459 if (len > 0 && cp[len - 1] != '/')
460 cp[len++] = '/';
462 /* Make sure we don't use untrusted directories if we run SUID. */
463 if (__glibc_unlikely (check_trusted) && !is_trusted_path (cp, len))
465 free (to_free);
466 continue;
469 /* See if this directory is already known. */
470 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
471 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
472 break;
474 if (dirp != NULL)
476 /* It is available, see whether it's on our own list. */
477 size_t cnt;
478 for (cnt = 0; cnt < nelems; ++cnt)
479 if (result[cnt] == dirp)
480 break;
482 if (cnt == nelems)
483 result[nelems++] = dirp;
485 else
487 size_t cnt;
488 enum r_dir_status init_val;
489 size_t where_len = where ? strlen (where) + 1 : 0;
491 /* It's a new directory. Create an entry and add it. */
492 dirp = (struct r_search_path_elem *)
493 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
494 + where_len + len + 1);
495 if (dirp == NULL)
496 _dl_signal_error (ENOMEM, NULL, NULL,
497 N_("cannot create cache for search path"));
499 dirp->dirname = ((char *) dirp + sizeof (*dirp)
500 + ncapstr * sizeof (enum r_dir_status));
501 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
502 dirp->dirnamelen = len;
504 if (len > max_dirnamelen)
505 max_dirnamelen = len;
507 /* We have to make sure all the relative directories are
508 never ignored. The current directory might change and
509 all our saved information would be void. */
510 init_val = cp[0] != '/' ? existing : unknown;
511 for (cnt = 0; cnt < ncapstr; ++cnt)
512 dirp->status[cnt] = init_val;
514 dirp->what = what;
515 if (__glibc_likely (where != NULL))
516 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
517 + (ncapstr * sizeof (enum r_dir_status)),
518 where, where_len);
519 else
520 dirp->where = NULL;
522 dirp->next = GL(dl_all_dirs);
523 GL(dl_all_dirs) = dirp;
525 /* Put it in the result array. */
526 result[nelems++] = dirp;
528 free (to_free);
531 /* Terminate the array. */
532 result[nelems] = NULL;
534 return result;
538 static bool
539 internal_function
540 decompose_rpath (struct r_search_path_struct *sps,
541 const char *rpath, struct link_map *l, const char *what)
543 /* Make a copy we can work with. */
544 const char *where = l->l_name;
545 char *copy;
546 char *cp;
547 struct r_search_path_elem **result;
548 size_t nelems;
549 /* Initialize to please the compiler. */
550 const char *errstring = NULL;
552 /* First see whether we must forget the RUNPATH and RPATH from this
553 object. */
554 if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL)
555 && !__libc_enable_secure)
557 const char *inhp = GLRO(dl_inhibit_rpath);
561 const char *wp = where;
563 while (*inhp == *wp && *wp != '\0')
565 ++inhp;
566 ++wp;
569 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
571 /* This object is on the list of objects for which the
572 RUNPATH and RPATH must not be used. */
573 sps->dirs = (void *) -1;
574 return false;
577 while (*inhp != '\0')
578 if (*inhp++ == ':')
579 break;
581 while (*inhp != '\0');
584 /* Make a writable copy. */
585 copy = __strdup (rpath);
586 if (copy == NULL)
588 errstring = N_("cannot create RUNPATH/RPATH copy");
589 goto signal_error;
592 /* Ignore empty rpaths. */
593 if (*copy == 0)
595 free (copy);
596 sps->dirs = (struct r_search_path_elem **) -1;
597 return false;
600 /* Count the number of necessary elements in the result array. */
601 nelems = 0;
602 for (cp = copy; *cp != '\0'; ++cp)
603 if (*cp == ':')
604 ++nelems;
606 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
607 number of necessary entries. */
608 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
609 * sizeof (*result));
610 if (result == NULL)
612 free (copy);
613 errstring = N_("cannot create cache for search path");
614 signal_error:
615 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
618 fillin_rpath (copy, result, ":", 0, what, where, l);
620 /* Free the copied RPATH string. `fillin_rpath' make own copies if
621 necessary. */
622 free (copy);
624 sps->dirs = result;
625 /* The caller will change this value if we haven't used a real malloc. */
626 sps->malloced = 1;
627 return true;
630 /* Make sure cached path information is stored in *SP
631 and return true if there are any paths to search there. */
632 static bool
633 cache_rpath (struct link_map *l,
634 struct r_search_path_struct *sp,
635 int tag,
636 const char *what)
638 if (sp->dirs == (void *) -1)
639 return false;
641 if (sp->dirs != NULL)
642 return true;
644 if (l->l_info[tag] == NULL)
646 /* There is no path. */
647 sp->dirs = (void *) -1;
648 return false;
651 /* Make sure the cache information is available. */
652 return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
653 + l->l_info[tag]->d_un.d_val),
654 l, what);
658 void
659 internal_function
660 _dl_init_paths (const char *llp)
662 size_t idx;
663 const char *strp;
664 struct r_search_path_elem *pelem, **aelem;
665 size_t round_size;
666 struct link_map __attribute__ ((unused)) *l = NULL;
667 /* Initialize to please the compiler. */
668 const char *errstring = NULL;
670 /* Fill in the information about the application's RPATH and the
671 directories addressed by the LD_LIBRARY_PATH environment variable. */
673 /* Get the capabilities. */
674 capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen),
675 &ncapstr, &max_capstrlen);
677 /* First set up the rest of the default search directory entries. */
678 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
679 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
680 if (rtld_search_dirs.dirs == NULL)
682 errstring = N_("cannot create search path array");
683 signal_error:
684 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
687 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
688 + ncapstr * sizeof (enum r_dir_status))
689 / sizeof (struct r_search_path_elem));
691 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
692 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
693 * round_size * sizeof (struct r_search_path_elem));
694 if (rtld_search_dirs.dirs[0] == NULL)
696 errstring = N_("cannot create cache for search path");
697 goto signal_error;
700 rtld_search_dirs.malloced = 0;
701 pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0];
702 strp = system_dirs;
703 idx = 0;
707 size_t cnt;
709 *aelem++ = pelem;
711 pelem->what = "system search path";
712 pelem->where = NULL;
714 pelem->dirname = strp;
715 pelem->dirnamelen = system_dirs_len[idx];
716 strp += system_dirs_len[idx] + 1;
718 /* System paths must be absolute. */
719 assert (pelem->dirname[0] == '/');
720 for (cnt = 0; cnt < ncapstr; ++cnt)
721 pelem->status[cnt] = unknown;
723 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
725 pelem += round_size;
727 while (idx < nsystem_dirs_len);
729 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
730 *aelem = NULL;
732 #ifdef SHARED
733 /* This points to the map of the main object. */
734 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
735 if (l != NULL)
737 assert (l->l_type != lt_loaded);
739 if (l->l_info[DT_RUNPATH])
741 /* Allocate room for the search path and fill in information
742 from RUNPATH. */
743 decompose_rpath (&l->l_runpath_dirs,
744 (const void *) (D_PTR (l, l_info[DT_STRTAB])
745 + l->l_info[DT_RUNPATH]->d_un.d_val),
746 l, "RUNPATH");
747 /* During rtld init the memory is allocated by the stub malloc,
748 prevent any attempt to free it by the normal malloc. */
749 l->l_runpath_dirs.malloced = 0;
751 /* The RPATH is ignored. */
752 l->l_rpath_dirs.dirs = (void *) -1;
754 else
756 l->l_runpath_dirs.dirs = (void *) -1;
758 if (l->l_info[DT_RPATH])
760 /* Allocate room for the search path and fill in information
761 from RPATH. */
762 decompose_rpath (&l->l_rpath_dirs,
763 (const void *) (D_PTR (l, l_info[DT_STRTAB])
764 + l->l_info[DT_RPATH]->d_un.d_val),
765 l, "RPATH");
766 /* During rtld init the memory is allocated by the stub
767 malloc, prevent any attempt to free it by the normal
768 malloc. */
769 l->l_rpath_dirs.malloced = 0;
771 else
772 l->l_rpath_dirs.dirs = (void *) -1;
775 #endif /* SHARED */
777 if (llp != NULL && *llp != '\0')
779 size_t nllp;
780 const char *cp = llp;
781 char *llp_tmp;
783 #ifdef SHARED
784 /* Expand DSTs. */
785 size_t cnt = DL_DST_COUNT (llp, 1);
786 if (__glibc_likely (cnt == 0))
787 llp_tmp = strdupa (llp);
788 else
790 /* Determine the length of the substituted string. */
791 size_t total = DL_DST_REQUIRED (l, llp, strlen (llp), cnt);
793 /* Allocate the necessary memory. */
794 llp_tmp = (char *) alloca (total + 1);
795 llp_tmp = _dl_dst_substitute (l, llp, llp_tmp, 1);
797 #else
798 llp_tmp = strdupa (llp);
799 #endif
801 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
802 elements it has. */
803 nllp = 1;
804 while (*cp)
806 if (*cp == ':' || *cp == ';')
807 ++nllp;
808 ++cp;
811 env_path_list.dirs = (struct r_search_path_elem **)
812 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
813 if (env_path_list.dirs == NULL)
815 errstring = N_("cannot create cache for search path");
816 goto signal_error;
819 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
820 __libc_enable_secure, "LD_LIBRARY_PATH",
821 NULL, l);
823 if (env_path_list.dirs[0] == NULL)
825 free (env_path_list.dirs);
826 env_path_list.dirs = (void *) -1;
829 env_path_list.malloced = 0;
831 else
832 env_path_list.dirs = (void *) -1;
836 static void
837 __attribute__ ((noreturn, noinline))
838 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
839 const char *msg, struct r_debug *r, Lmid_t nsid)
841 /* The file might already be closed. */
842 if (fd != -1)
843 (void) __close (fd);
844 if (l != NULL && l->l_origin != (char *) -1l)
845 free ((char *) l->l_origin);
846 free (l);
847 free (realname);
849 if (r != NULL)
851 r->r_state = RT_CONSISTENT;
852 _dl_debug_state ();
853 LIBC_PROBE (map_failed, 2, nsid, r);
856 _dl_signal_error (code, name, NULL, msg);
860 /* Map in the shared object NAME, actually located in REALNAME, and already
861 opened on FD. */
863 #ifndef EXTERNAL_MAP_FROM_FD
864 static
865 #endif
866 struct link_map *
867 _dl_map_object_from_fd (const char *name, const char *origname, int fd,
868 struct filebuf *fbp, char *realname,
869 struct link_map *loader, int l_type, int mode,
870 void **stack_endp, Lmid_t nsid)
872 struct link_map *l = NULL;
873 const ElfW(Ehdr) *header;
874 const ElfW(Phdr) *phdr;
875 const ElfW(Phdr) *ph;
876 size_t maplength;
877 int type;
878 /* Initialize to keep the compiler happy. */
879 const char *errstring = NULL;
880 int errval = 0;
881 struct r_debug *r = _dl_debug_initialize (0, nsid);
882 bool make_consistent = false;
884 /* Get file information. */
885 struct r_file_id id;
886 if (__glibc_unlikely (!_dl_get_file_id (fd, &id)))
888 errstring = N_("cannot stat shared object");
889 call_lose_errno:
890 errval = errno;
891 call_lose:
892 lose (errval, fd, name, realname, l, errstring,
893 make_consistent ? r : NULL, nsid);
896 /* Look again to see if the real name matched another already loaded. */
897 for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
898 if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id))
900 /* The object is already loaded.
901 Just bump its reference count and return it. */
902 __close (fd);
904 /* If the name is not in the list of names for this object add
905 it. */
906 free (realname);
907 add_name_to_object (l, name);
909 return l;
912 #ifdef SHARED
913 /* When loading into a namespace other than the base one we must
914 avoid loading ld.so since there can only be one copy. Ever. */
915 if (__glibc_unlikely (nsid != LM_ID_BASE)
916 && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id)
917 || _dl_name_match_p (name, &GL(dl_rtld_map))))
919 /* This is indeed ld.so. Create a new link_map which refers to
920 the real one for almost everything. */
921 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
922 if (l == NULL)
923 goto fail_new;
925 /* Refer to the real descriptor. */
926 l->l_real = &GL(dl_rtld_map);
928 /* No need to bump the refcount of the real object, ld.so will
929 never be unloaded. */
930 __close (fd);
932 /* Add the map for the mirrored object to the object list. */
933 _dl_add_to_namespace_list (l, nsid);
935 return l;
937 #endif
939 if (mode & RTLD_NOLOAD)
941 /* We are not supposed to load the object unless it is already
942 loaded. So return now. */
943 free (realname);
944 __close (fd);
945 return NULL;
948 /* Print debugging message. */
949 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
950 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
952 /* This is the ELF header. We read it in `open_verify'. */
953 header = (void *) fbp->buf;
955 #ifndef MAP_ANON
956 # define MAP_ANON 0
957 if (_dl_zerofd == -1)
959 _dl_zerofd = _dl_sysdep_open_zero_fill ();
960 if (_dl_zerofd == -1)
962 free (realname);
963 __close (fd);
964 _dl_signal_error (errno, NULL, NULL,
965 N_("cannot open zero fill device"));
968 #endif
970 /* Signal that we are going to add new objects. */
971 if (r->r_state == RT_CONSISTENT)
973 #ifdef SHARED
974 /* Auditing checkpoint: we are going to add new objects. */
975 if ((mode & __RTLD_AUDIT) == 0
976 && __glibc_unlikely (GLRO(dl_naudit) > 0))
978 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
979 /* Do not call the functions for any auditing object. */
980 if (head->l_auditing == 0)
982 struct audit_ifaces *afct = GLRO(dl_audit);
983 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
985 if (afct->activity != NULL)
986 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD);
988 afct = afct->next;
992 #endif
994 /* Notify the debugger we have added some objects. We need to
995 call _dl_debug_initialize in a static program in case dynamic
996 linking has not been used before. */
997 r->r_state = RT_ADD;
998 _dl_debug_state ();
999 LIBC_PROBE (map_start, 2, nsid, r);
1000 make_consistent = true;
1002 else
1003 assert (r->r_state == RT_ADD);
1005 /* Enter the new object in the list of loaded objects. */
1006 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
1007 if (__glibc_unlikely (l == NULL))
1009 #ifdef SHARED
1010 fail_new:
1011 #endif
1012 errstring = N_("cannot create shared object descriptor");
1013 goto call_lose_errno;
1016 /* Extract the remaining details we need from the ELF header
1017 and then read in the program header table. */
1018 l->l_entry = header->e_entry;
1019 type = header->e_type;
1020 l->l_phnum = header->e_phnum;
1022 maplength = header->e_phnum * sizeof (ElfW(Phdr));
1023 if (header->e_phoff + maplength <= (size_t) fbp->len)
1024 phdr = (void *) (fbp->buf + header->e_phoff);
1025 else
1027 phdr = alloca (maplength);
1028 __lseek (fd, header->e_phoff, SEEK_SET);
1029 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1031 errstring = N_("cannot read file data");
1032 goto call_lose_errno;
1036 /* On most platforms presume that PT_GNU_STACK is absent and the stack is
1037 * executable. Other platforms default to a nonexecutable stack and don't
1038 * need PT_GNU_STACK to do so. */
1039 uint_fast16_t stack_flags = DEFAULT_STACK_PERMS;
1042 /* Scan the program header table, collecting its load commands. */
1043 struct loadcmd loadcmds[l->l_phnum];
1044 size_t nloadcmds = 0;
1045 bool has_holes = false;
1047 /* The struct is initialized to zero so this is not necessary:
1048 l->l_ld = 0;
1049 l->l_phdr = 0;
1050 l->l_addr = 0; */
1051 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
1052 switch (ph->p_type)
1054 /* These entries tell us where to find things once the file's
1055 segments are mapped in. We record the addresses it says
1056 verbatim, and later correct for the run-time load address. */
1057 case PT_DYNAMIC:
1058 l->l_ld = (void *) ph->p_vaddr;
1059 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1060 break;
1062 case PT_PHDR:
1063 l->l_phdr = (void *) ph->p_vaddr;
1064 break;
1066 case PT_LOAD:
1067 /* A load command tells us to map in part of the file.
1068 We record the load commands and process them all later. */
1069 if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0))
1071 errstring = N_("ELF load command alignment not page-aligned");
1072 goto call_lose;
1074 if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
1075 & (ph->p_align - 1)) != 0))
1077 errstring
1078 = N_("ELF load command address/offset not properly aligned");
1079 goto call_lose;
1082 struct loadcmd *c = &loadcmds[nloadcmds++];
1083 c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize));
1084 c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
1085 c->dataend = ph->p_vaddr + ph->p_filesz;
1086 c->allocend = ph->p_vaddr + ph->p_memsz;
1087 c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
1089 /* Determine whether there is a gap between the last segment
1090 and this one. */
1091 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1092 has_holes = true;
1094 /* Optimize a common case. */
1095 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1096 c->prot = (PF_TO_PROT
1097 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1098 #else
1099 c->prot = 0;
1100 if (ph->p_flags & PF_R)
1101 c->prot |= PROT_READ;
1102 if (ph->p_flags & PF_W)
1103 c->prot |= PROT_WRITE;
1104 if (ph->p_flags & PF_X)
1105 c->prot |= PROT_EXEC;
1106 #endif
1107 break;
1109 case PT_TLS:
1110 if (ph->p_memsz == 0)
1111 /* Nothing to do for an empty segment. */
1112 break;
1114 l->l_tls_blocksize = ph->p_memsz;
1115 l->l_tls_align = ph->p_align;
1116 if (ph->p_align == 0)
1117 l->l_tls_firstbyte_offset = 0;
1118 else
1119 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1120 l->l_tls_initimage_size = ph->p_filesz;
1121 /* Since we don't know the load address yet only store the
1122 offset. We will adjust it later. */
1123 l->l_tls_initimage = (void *) ph->p_vaddr;
1125 /* If not loading the initial set of shared libraries,
1126 check whether we should permit loading a TLS segment. */
1127 if (__glibc_likely (l->l_type == lt_library)
1128 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1129 not set up TLS data structures, so don't use them now. */
1130 || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL))
1132 /* Assign the next available module ID. */
1133 l->l_tls_modid = _dl_next_tls_modid ();
1134 break;
1137 #ifdef SHARED
1138 if (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0)
1139 /* We are loading the executable itself when the dynamic linker
1140 was executed directly. The setup will happen later. */
1141 break;
1143 # ifdef _LIBC_REENTRANT
1144 /* In a static binary there is no way to tell if we dynamically
1145 loaded libpthread. */
1146 if (GL(dl_error_catch_tsd) == &_dl_initial_error_catch_tsd)
1147 # endif
1148 #endif
1150 /* We have not yet loaded libpthread.
1151 We can do the TLS setup right now! */
1153 void *tcb;
1155 /* The first call allocates TLS bookkeeping data structures.
1156 Then we allocate the TCB for the initial thread. */
1157 if (__glibc_unlikely (_dl_tls_setup ())
1158 || __glibc_unlikely ((tcb = _dl_allocate_tls (NULL)) == NULL))
1160 errval = ENOMEM;
1161 errstring = N_("\
1162 cannot allocate TLS data structures for initial thread");
1163 goto call_lose;
1166 /* Now we install the TCB in the thread register. */
1167 errstring = TLS_INIT_TP (tcb);
1168 if (__glibc_likely (errstring == NULL))
1170 /* Now we are all good. */
1171 l->l_tls_modid = ++GL(dl_tls_max_dtv_idx);
1172 break;
1175 /* The kernel is too old or somesuch. */
1176 errval = 0;
1177 _dl_deallocate_tls (tcb, 1);
1178 goto call_lose;
1181 /* Uh-oh, the binary expects TLS support but we cannot
1182 provide it. */
1183 errval = 0;
1184 errstring = N_("cannot handle TLS data");
1185 goto call_lose;
1186 break;
1188 case PT_GNU_STACK:
1189 stack_flags = ph->p_flags;
1190 break;
1192 case PT_GNU_RELRO:
1193 l->l_relro_addr = ph->p_vaddr;
1194 l->l_relro_size = ph->p_memsz;
1195 break;
1198 if (__glibc_unlikely (nloadcmds == 0))
1200 /* This only happens for a bogus object that will be caught with
1201 another error below. But we don't want to go through the
1202 calculations below using NLOADCMDS - 1. */
1203 errstring = N_("object file has no loadable segments");
1204 goto call_lose;
1207 if (__glibc_unlikely (type != ET_DYN)
1208 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))
1210 /* This object is loaded at a fixed address. This must never
1211 happen for objects loaded with dlopen. */
1212 errstring = N_("cannot dynamically load executable");
1213 goto call_lose;
1216 /* Length of the sections to be loaded. */
1217 maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
1219 /* Now process the load commands and map segments into memory.
1220 This is responsible for filling in:
1221 l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr
1223 errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
1224 maplength, has_holes, loader);
1225 if (__glibc_unlikely (errstring != NULL))
1226 goto call_lose;
1229 if (l->l_ld == 0)
1231 if (__glibc_unlikely (type == ET_DYN))
1233 errstring = N_("object file has no dynamic section");
1234 goto call_lose;
1237 else
1238 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1240 elf_get_dynamic_info (l, NULL);
1242 /* Make sure we are not dlopen'ing an object that has the
1243 DF_1_NOOPEN flag set. */
1244 if (__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
1245 && (mode & __RTLD_DLOPEN))
1247 /* We are not supposed to load this object. Free all resources. */
1248 _dl_unmap_segments (l);
1250 if (!l->l_libname->dont_free)
1251 free (l->l_libname);
1253 if (l->l_phdr_allocated)
1254 free ((void *) l->l_phdr);
1256 errstring = N_("shared object cannot be dlopen()ed");
1257 goto call_lose;
1260 if (l->l_phdr == NULL)
1262 /* The program header is not contained in any of the segments.
1263 We have to allocate memory ourself and copy it over from out
1264 temporary place. */
1265 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1266 * sizeof (ElfW(Phdr)));
1267 if (newp == NULL)
1269 errstring = N_("cannot allocate memory for program header");
1270 goto call_lose_errno;
1273 l->l_phdr = memcpy (newp, phdr,
1274 (header->e_phnum * sizeof (ElfW(Phdr))));
1275 l->l_phdr_allocated = 1;
1277 else
1278 /* Adjust the PT_PHDR value by the runtime load address. */
1279 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1281 if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X))
1283 if (__glibc_unlikely (__check_caller (RETURN_ADDRESS (0), allow_ldso) != 0))
1285 errstring = N_("invalid caller");
1286 goto call_lose;
1289 /* The stack is presently not executable, but this module
1290 requires that it be executable. We must change the
1291 protection of the variable which contains the flags used in
1292 the mprotect calls. */
1293 #ifdef SHARED
1294 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1296 const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
1297 const size_t s = (uintptr_t) (&__stack_prot + 1) - p;
1299 struct link_map *const m = &GL(dl_rtld_map);
1300 const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr
1301 + m->l_relro_size)
1302 & -GLRO(dl_pagesize));
1303 if (__glibc_likely (p + s <= relro_end))
1305 /* The variable lies in the region protected by RELRO. */
1306 if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0)
1308 errstring = N_("cannot change memory protections");
1309 goto call_lose_errno;
1311 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1312 __mprotect ((void *) p, s, PROT_READ);
1314 else
1315 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1317 else
1318 #endif
1319 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1321 #ifdef check_consistency
1322 check_consistency ();
1323 #endif
1325 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1326 if (errval)
1328 errstring = N_("\
1329 cannot enable executable stack as shared object requires");
1330 goto call_lose;
1334 /* Adjust the address of the TLS initialization image. */
1335 if (l->l_tls_initimage != NULL)
1336 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1338 /* We are done mapping in the file. We no longer need the descriptor. */
1339 if (__glibc_unlikely (__close (fd) != 0))
1341 errstring = N_("cannot close file descriptor");
1342 goto call_lose_errno;
1344 /* Signal that we closed the file. */
1345 fd = -1;
1347 /* If this is ET_EXEC, we should have loaded it as lt_executable. */
1348 assert (type != ET_EXEC || l->l_type == lt_executable);
1350 l->l_entry += l->l_addr;
1352 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1353 _dl_debug_printf ("\
1354 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1355 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1356 (int) sizeof (void *) * 2,
1357 (unsigned long int) l->l_ld,
1358 (int) sizeof (void *) * 2,
1359 (unsigned long int) l->l_addr,
1360 (int) sizeof (void *) * 2, maplength,
1361 (int) sizeof (void *) * 2,
1362 (unsigned long int) l->l_entry,
1363 (int) sizeof (void *) * 2,
1364 (unsigned long int) l->l_phdr,
1365 (int) sizeof (void *) * 2, l->l_phnum);
1367 /* Set up the symbol hash table. */
1368 _dl_setup_hash (l);
1370 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1371 have to do this for the main map. */
1372 if ((mode & RTLD_DEEPBIND) == 0
1373 && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL)
1374 && &l->l_searchlist != l->l_scope[0])
1376 /* Create an appropriate searchlist. It contains only this map.
1377 This is the definition of DT_SYMBOLIC in SysVr4. */
1378 l->l_symbolic_searchlist.r_list[0] = l;
1379 l->l_symbolic_searchlist.r_nlist = 1;
1381 /* Now move the existing entries one back. */
1382 memmove (&l->l_scope[1], &l->l_scope[0],
1383 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1385 /* Now add the new entry. */
1386 l->l_scope[0] = &l->l_symbolic_searchlist;
1389 /* Remember whether this object must be initialized first. */
1390 if (l->l_flags_1 & DF_1_INITFIRST)
1391 GL(dl_initfirst) = l;
1393 /* Finally the file information. */
1394 l->l_file_id = id;
1396 #ifdef SHARED
1397 /* When auditing is used the recorded names might not include the
1398 name by which the DSO is actually known. Add that as well. */
1399 if (__glibc_unlikely (origname != NULL))
1400 add_name_to_object (l, origname);
1401 #else
1402 /* Audit modules only exist when linking is dynamic so ORIGNAME
1403 cannot be non-NULL. */
1404 assert (origname == NULL);
1405 #endif
1407 /* When we profile the SONAME might be needed for something else but
1408 loading. Add it right away. */
1409 if (__glibc_unlikely (GLRO(dl_profile) != NULL)
1410 && l->l_info[DT_SONAME] != NULL)
1411 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1412 + l->l_info[DT_SONAME]->d_un.d_val));
1414 #ifdef DL_AFTER_LOAD
1415 DL_AFTER_LOAD (l);
1416 #endif
1418 /* Now that the object is fully initialized add it to the object list. */
1419 _dl_add_to_namespace_list (l, nsid);
1421 #ifdef SHARED
1422 /* Auditing checkpoint: we have a new object. */
1423 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1424 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1426 struct audit_ifaces *afct = GLRO(dl_audit);
1427 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1429 if (afct->objopen != NULL)
1431 l->l_audit[cnt].bindflags
1432 = afct->objopen (l, nsid, &l->l_audit[cnt].cookie);
1434 l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0;
1437 afct = afct->next;
1440 #endif
1442 return l;
1445 /* Print search path. */
1446 static void
1447 print_search_path (struct r_search_path_elem **list,
1448 const char *what, const char *name)
1450 char buf[max_dirnamelen + max_capstrlen];
1451 int first = 1;
1453 _dl_debug_printf (" search path=");
1455 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1457 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1458 size_t cnt;
1460 for (cnt = 0; cnt < ncapstr; ++cnt)
1461 if ((*list)->status[cnt] != nonexisting)
1463 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1464 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1465 cp[0] = '\0';
1466 else
1467 cp[-1] = '\0';
1469 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1470 first = 0;
1473 ++list;
1476 if (name != NULL)
1477 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1478 DSO_FILENAME (name));
1479 else
1480 _dl_debug_printf_c ("\t\t(%s)\n", what);
1483 /* Open a file and verify it is an ELF file for this architecture. We
1484 ignore only ELF files for other architectures. Non-ELF files and
1485 ELF files with different header information cause fatal errors since
1486 this could mean there is something wrong in the installation and the
1487 user might want to know about this.
1489 If FD is not -1, then the file is already open and FD refers to it.
1490 In that case, FD is consumed for both successful and error returns. */
1491 static int
1492 open_verify (const char *name, int fd,
1493 struct filebuf *fbp, struct link_map *loader,
1494 int whatcode, int mode, bool *found_other_class, bool free_name)
1496 /* This is the expected ELF header. */
1497 #define ELF32_CLASS ELFCLASS32
1498 #define ELF64_CLASS ELFCLASS64
1499 #ifndef VALID_ELF_HEADER
1500 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1501 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1502 # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0)
1503 #elif defined MORE_ELF_HEADER_DATA
1504 MORE_ELF_HEADER_DATA;
1505 #endif
1506 static const unsigned char expected[EI_NIDENT] =
1508 [EI_MAG0] = ELFMAG0,
1509 [EI_MAG1] = ELFMAG1,
1510 [EI_MAG2] = ELFMAG2,
1511 [EI_MAG3] = ELFMAG3,
1512 [EI_CLASS] = ELFW(CLASS),
1513 [EI_DATA] = byteorder,
1514 [EI_VERSION] = EV_CURRENT,
1515 [EI_OSABI] = ELFOSABI_SYSV,
1516 [EI_ABIVERSION] = 0
1518 static const struct
1520 ElfW(Word) vendorlen;
1521 ElfW(Word) datalen;
1522 ElfW(Word) type;
1523 char vendor[4];
1524 } expected_note = { 4, 16, 1, "GNU" };
1525 /* Initialize it to make the compiler happy. */
1526 const char *errstring = NULL;
1527 int errval = 0;
1529 #ifdef SHARED
1530 /* Give the auditing libraries a chance. */
1531 if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0
1532 && loader->l_auditing == 0)
1534 const char *original_name = name;
1535 struct audit_ifaces *afct = GLRO(dl_audit);
1536 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1538 if (afct->objsearch != NULL)
1540 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1541 whatcode);
1542 if (name == NULL)
1543 /* Ignore the path. */
1544 return -1;
1547 afct = afct->next;
1550 if (fd != -1 && name != original_name && strcmp (name, original_name))
1552 /* An audit library changed what we're supposed to open,
1553 so FD no longer matches it. */
1554 __close (fd);
1555 fd = -1;
1558 #endif
1560 if (fd == -1)
1561 /* Open the file. We always open files read-only. */
1562 fd = __open (name, O_RDONLY | O_CLOEXEC);
1564 if (fd != -1)
1566 ElfW(Ehdr) *ehdr;
1567 ElfW(Phdr) *phdr, *ph;
1568 ElfW(Word) *abi_note;
1569 unsigned int osversion;
1570 size_t maplength;
1572 /* We successfully opened the file. Now verify it is a file
1573 we can use. */
1574 __set_errno (0);
1575 fbp->len = 0;
1576 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1577 /* Read in the header. */
1580 ssize_t retlen = __libc_read (fd, fbp->buf + fbp->len,
1581 sizeof (fbp->buf) - fbp->len);
1582 if (retlen <= 0)
1583 break;
1584 fbp->len += retlen;
1586 while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr))));
1588 /* This is where the ELF header is loaded. */
1589 ehdr = (ElfW(Ehdr) *) fbp->buf;
1591 /* Now run the tests. */
1592 if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr))))
1594 errval = errno;
1595 errstring = (errval == 0
1596 ? N_("file too short") : N_("cannot read file data"));
1597 call_lose:
1598 if (free_name)
1600 char *realname = (char *) name;
1601 name = strdupa (realname);
1602 free (realname);
1604 lose (errval, fd, name, NULL, NULL, errstring, NULL, 0);
1607 /* See whether the ELF header is what we expect. */
1608 if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1609 EI_ABIVERSION)
1610 || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1611 ehdr->e_ident[EI_ABIVERSION])
1612 || memcmp (&ehdr->e_ident[EI_PAD],
1613 &expected[EI_PAD],
1614 EI_NIDENT - EI_PAD) != 0))
1616 /* Something is wrong. */
1617 const Elf32_Word *magp = (const void *) ehdr->e_ident;
1618 if (*magp !=
1619 #if BYTE_ORDER == LITTLE_ENDIAN
1620 ((ELFMAG0 << (EI_MAG0 * 8)) |
1621 (ELFMAG1 << (EI_MAG1 * 8)) |
1622 (ELFMAG2 << (EI_MAG2 * 8)) |
1623 (ELFMAG3 << (EI_MAG3 * 8)))
1624 #else
1625 ((ELFMAG0 << (EI_MAG3 * 8)) |
1626 (ELFMAG1 << (EI_MAG2 * 8)) |
1627 (ELFMAG2 << (EI_MAG1 * 8)) |
1628 (ELFMAG3 << (EI_MAG0 * 8)))
1629 #endif
1631 errstring = N_("invalid ELF header");
1632 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1634 /* This is not a fatal error. On architectures where
1635 32-bit and 64-bit binaries can be run this might
1636 happen. */
1637 *found_other_class = true;
1638 goto close_and_out;
1640 else if (ehdr->e_ident[EI_DATA] != byteorder)
1642 if (BYTE_ORDER == BIG_ENDIAN)
1643 errstring = N_("ELF file data encoding not big-endian");
1644 else
1645 errstring = N_("ELF file data encoding not little-endian");
1647 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1648 errstring
1649 = N_("ELF file version ident does not match current one");
1650 /* XXX We should be able so set system specific versions which are
1651 allowed here. */
1652 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1653 errstring = N_("ELF file OS ABI invalid");
1654 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1655 ehdr->e_ident[EI_ABIVERSION]))
1656 errstring = N_("ELF file ABI version invalid");
1657 else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD],
1658 EI_NIDENT - EI_PAD) != 0)
1659 errstring = N_("nonzero padding in e_ident");
1660 else
1661 /* Otherwise we don't know what went wrong. */
1662 errstring = N_("internal error");
1664 goto call_lose;
1667 if (__glibc_unlikely (ehdr->e_version != EV_CURRENT))
1669 errstring = N_("ELF file version does not match current one");
1670 goto call_lose;
1672 if (! __glibc_likely (elf_machine_matches_host (ehdr)))
1673 goto close_and_out;
1674 else if (__glibc_unlikely (ehdr->e_type != ET_DYN
1675 && ehdr->e_type != ET_EXEC))
1677 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1678 goto call_lose;
1680 else if (__glibc_unlikely (ehdr->e_type == ET_EXEC
1681 && (mode & __RTLD_OPENEXEC) == 0))
1683 /* BZ #16634. It is an error to dlopen ET_EXEC (unless
1684 __RTLD_OPENEXEC is explicitly set). We return error here
1685 so that code in _dl_map_object_from_fd does not try to set
1686 l_tls_modid for this module. */
1688 errstring = N_("cannot dynamically load executable");
1689 goto call_lose;
1691 else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr))))
1693 errstring = N_("ELF file's phentsize not the expected size");
1694 goto call_lose;
1697 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1698 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1699 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1700 else
1702 phdr = alloca (maplength);
1703 __lseek (fd, ehdr->e_phoff, SEEK_SET);
1704 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1706 read_error:
1707 errval = errno;
1708 errstring = N_("cannot read file data");
1709 goto call_lose;
1713 if (__glibc_unlikely (elf_machine_reject_phdr_p
1714 (phdr, ehdr->e_phnum, fbp->buf, fbp->len,
1715 loader, fd)))
1716 goto close_and_out;
1718 /* Check .note.ABI-tag if present. */
1719 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1720 if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 && ph->p_align >= 4)
1722 ElfW(Addr) size = ph->p_filesz;
1724 if (ph->p_offset + size <= (size_t) fbp->len)
1725 abi_note = (void *) (fbp->buf + ph->p_offset);
1726 else
1728 abi_note = alloca (size);
1729 __lseek (fd, ph->p_offset, SEEK_SET);
1730 if (__libc_read (fd, (void *) abi_note, size) != size)
1731 goto read_error;
1734 while (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1736 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
1737 ElfW(Addr) note_size = 3 * sizeof (ElfW(Word))
1738 + ROUND (abi_note[0])
1739 + ROUND (abi_note[1]);
1741 if (size - 32 < note_size)
1743 size = 0;
1744 break;
1746 size -= note_size;
1747 abi_note = (void *) abi_note + note_size;
1750 if (size == 0)
1751 continue;
1753 osversion = (abi_note[5] & 0xff) * 65536
1754 + (abi_note[6] & 0xff) * 256
1755 + (abi_note[7] & 0xff);
1756 if (abi_note[4] != __ABI_TAG_OS
1757 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1759 close_and_out:
1760 __close (fd);
1761 __set_errno (ENOENT);
1762 fd = -1;
1765 break;
1769 return fd;
1772 /* Try to open NAME in one of the directories in *DIRSP.
1773 Return the fd, or -1. If successful, fill in *REALNAME
1774 with the malloc'd full directory name. If it turns out
1775 that none of the directories in *DIRSP exists, *DIRSP is
1776 replaced with (void *) -1, and the old value is free()d
1777 if MAY_FREE_DIRS is true. */
1779 static int
1780 open_path (const char *name, size_t namelen, int mode,
1781 struct r_search_path_struct *sps, char **realname,
1782 struct filebuf *fbp, struct link_map *loader, int whatcode,
1783 bool *found_other_class)
1785 struct r_search_path_elem **dirs = sps->dirs;
1786 char *buf;
1787 int fd = -1;
1788 const char *current_what = NULL;
1789 int any = 0;
1791 if (__glibc_unlikely (dirs == NULL))
1792 /* We're called before _dl_init_paths when loading the main executable
1793 given on the command line when rtld is run directly. */
1794 return -1;
1796 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1799 struct r_search_path_elem *this_dir = *dirs;
1800 size_t buflen = 0;
1801 size_t cnt;
1802 char *edp;
1803 int here_any = 0;
1804 int err;
1806 /* If we are debugging the search for libraries print the path
1807 now if it hasn't happened now. */
1808 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)
1809 && current_what != this_dir->what)
1811 current_what = this_dir->what;
1812 print_search_path (dirs, current_what, this_dir->where);
1815 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1816 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1818 /* Skip this directory if we know it does not exist. */
1819 if (this_dir->status[cnt] == nonexisting)
1820 continue;
1822 buflen =
1823 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1824 capstr[cnt].len),
1825 name, namelen)
1826 - buf);
1828 /* Print name we try if this is wanted. */
1829 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1830 _dl_debug_printf (" trying file=%s\n", buf);
1832 fd = open_verify (buf, -1, fbp, loader, whatcode, mode,
1833 found_other_class, false);
1834 if (this_dir->status[cnt] == unknown)
1836 if (fd != -1)
1837 this_dir->status[cnt] = existing;
1838 /* Do not update the directory information when loading
1839 auditing code. We must try to disturb the program as
1840 little as possible. */
1841 else if (loader == NULL
1842 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0)
1844 /* We failed to open machine dependent library. Let's
1845 test whether there is any directory at all. */
1846 struct stat64 st;
1848 buf[buflen - namelen - 1] = '\0';
1850 if (__xstat64 (_STAT_VER, buf, &st) != 0
1851 || ! S_ISDIR (st.st_mode))
1852 /* The directory does not exist or it is no directory. */
1853 this_dir->status[cnt] = nonexisting;
1854 else
1855 this_dir->status[cnt] = existing;
1859 /* Remember whether we found any existing directory. */
1860 here_any |= this_dir->status[cnt] != nonexisting;
1862 if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE)
1863 && __libc_enable_secure)
1865 /* This is an extra security effort to make sure nobody can
1866 preload broken shared objects which are in the trusted
1867 directories and so exploit the bugs. */
1868 struct stat64 st;
1870 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1871 || (st.st_mode & S_ISUID) == 0)
1873 /* The shared object cannot be tested for being SUID
1874 or this bit is not set. In this case we must not
1875 use this object. */
1876 __close (fd);
1877 fd = -1;
1878 /* We simply ignore the file, signal this by setting
1879 the error value which would have been set by `open'. */
1880 errno = ENOENT;
1885 if (fd != -1)
1887 *realname = (char *) malloc (buflen);
1888 if (*realname != NULL)
1890 memcpy (*realname, buf, buflen);
1891 return fd;
1893 else
1895 /* No memory for the name, we certainly won't be able
1896 to load and link it. */
1897 __close (fd);
1898 return -1;
1901 if (here_any && (err = errno) != ENOENT && err != EACCES)
1902 /* The file exists and is readable, but something went wrong. */
1903 return -1;
1905 /* Remember whether we found anything. */
1906 any |= here_any;
1908 while (*++dirs != NULL);
1910 /* Remove the whole path if none of the directories exists. */
1911 if (__glibc_unlikely (! any))
1913 /* Paths which were allocated using the minimal malloc() in ld.so
1914 must not be freed using the general free() in libc. */
1915 if (sps->malloced)
1916 free (sps->dirs);
1918 /* rtld_search_dirs and env_path_list are attribute_relro, therefore
1919 avoid writing into it. */
1920 if (sps != &rtld_search_dirs && sps != &env_path_list)
1921 sps->dirs = (void *) -1;
1924 return -1;
1927 /* Map in the shared object file NAME. */
1929 struct link_map *
1930 internal_function
1931 _dl_map_object (struct link_map *loader, const char *name,
1932 int type, int trace_mode, int mode, Lmid_t nsid)
1934 int fd;
1935 const char *origname = NULL;
1936 char *realname;
1937 char *name_copy;
1938 struct link_map *l;
1939 struct filebuf fb;
1941 assert (nsid >= 0);
1942 assert (nsid < GL(dl_nns));
1944 /* Look for this name among those already loaded. */
1945 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
1947 /* If the requested name matches the soname of a loaded object,
1948 use that object. Elide this check for names that have not
1949 yet been opened. */
1950 if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
1951 continue;
1952 if (!_dl_name_match_p (name, l))
1954 const char *soname;
1956 if (__glibc_likely (l->l_soname_added)
1957 || l->l_info[DT_SONAME] == NULL)
1958 continue;
1960 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1961 + l->l_info[DT_SONAME]->d_un.d_val);
1962 if (strcmp (name, soname) != 0)
1963 continue;
1965 /* We have a match on a new name -- cache it. */
1966 add_name_to_object (l, soname);
1967 l->l_soname_added = 1;
1970 /* We have a match. */
1971 return l;
1974 /* Display information if we are debugging. */
1975 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
1976 && loader != NULL)
1977 _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0
1978 ? "\nfile=%s [%lu]; needed by %s [%lu]\n"
1979 : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n",
1980 name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns);
1982 #ifdef SHARED
1983 /* Give the auditing libraries a chance to change the name before we
1984 try anything. */
1985 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1986 && (loader == NULL || loader->l_auditing == 0))
1988 struct audit_ifaces *afct = GLRO(dl_audit);
1989 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1991 if (afct->objsearch != NULL)
1993 const char *before = name;
1994 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1995 LA_SER_ORIG);
1996 if (name == NULL)
1998 /* Do not try anything further. */
1999 fd = -1;
2000 goto no_file;
2002 if (before != name && strcmp (before, name) != 0)
2004 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
2005 _dl_debug_printf ("audit changed filename %s -> %s\n",
2006 before, name);
2008 if (origname == NULL)
2009 origname = before;
2013 afct = afct->next;
2016 #endif
2018 /* Will be true if we found a DSO which is of the other ELF class. */
2019 bool found_other_class = false;
2021 if (strchr (name, '/') == NULL)
2023 /* Search for NAME in several places. */
2025 size_t namelen = strlen (name) + 1;
2027 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2028 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
2030 fd = -1;
2032 /* When the object has the RUNPATH information we don't use any
2033 RPATHs. */
2034 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
2036 /* This is the executable's map (if there is one). Make sure that
2037 we do not look at it twice. */
2038 struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2039 bool did_main_map = false;
2041 /* First try the DT_RPATH of the dependent object that caused NAME
2042 to be loaded. Then that object's dependent, and on up. */
2043 for (l = loader; l; l = l->l_loader)
2044 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2046 fd = open_path (name, namelen, mode,
2047 &l->l_rpath_dirs,
2048 &realname, &fb, loader, LA_SER_RUNPATH,
2049 &found_other_class);
2050 if (fd != -1)
2051 break;
2053 did_main_map |= l == main_map;
2056 /* If dynamically linked, try the DT_RPATH of the executable
2057 itself. NB: we do this for lookups in any namespace. */
2058 if (fd == -1 && !did_main_map
2059 && main_map != NULL && main_map->l_type != lt_loaded
2060 && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH,
2061 "RPATH"))
2062 fd = open_path (name, namelen, mode,
2063 &main_map->l_rpath_dirs,
2064 &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
2065 &found_other_class);
2068 /* Try the LD_LIBRARY_PATH environment variable. */
2069 if (fd == -1 && env_path_list.dirs != (void *) -1)
2070 fd = open_path (name, namelen, mode, &env_path_list,
2071 &realname, &fb,
2072 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
2073 LA_SER_LIBPATH, &found_other_class);
2075 /* Look at the RUNPATH information for this binary. */
2076 if (fd == -1 && loader != NULL
2077 && cache_rpath (loader, &loader->l_runpath_dirs,
2078 DT_RUNPATH, "RUNPATH"))
2079 fd = open_path (name, namelen, mode,
2080 &loader->l_runpath_dirs, &realname, &fb, loader,
2081 LA_SER_RUNPATH, &found_other_class);
2083 if (fd == -1)
2085 realname = _dl_sysdep_open_object (name, namelen, &fd);
2086 if (realname != NULL)
2088 fd = open_verify (realname, fd,
2089 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2090 LA_SER_CONFIG, mode, &found_other_class,
2091 false);
2092 if (fd == -1)
2093 free (realname);
2097 #ifdef USE_LDCONFIG
2098 if (fd == -1
2099 && (__glibc_likely ((mode & __RTLD_SECURE) == 0)
2100 || ! __libc_enable_secure)
2101 && __glibc_likely (GLRO(dl_inhibit_cache) == 0))
2103 /* Check the list of libraries in the file /etc/ld.so.cache,
2104 for compatibility with Linux's ldconfig program. */
2105 char *cached = _dl_load_cache_lookup (name);
2107 if (cached != NULL)
2109 // XXX Correct to unconditionally default to namespace 0?
2110 l = (loader
2111 ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded
2112 # ifdef SHARED
2113 ?: &GL(dl_rtld_map)
2114 # endif
2117 /* If the loader has the DF_1_NODEFLIB flag set we must not
2118 use a cache entry from any of these directories. */
2119 if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB))
2121 const char *dirp = system_dirs;
2122 unsigned int cnt = 0;
2126 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2128 /* The prefix matches. Don't use the entry. */
2129 free (cached);
2130 cached = NULL;
2131 break;
2134 dirp += system_dirs_len[cnt] + 1;
2135 ++cnt;
2137 while (cnt < nsystem_dirs_len);
2140 if (cached != NULL)
2142 fd = open_verify (cached, -1,
2143 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2144 LA_SER_CONFIG, mode, &found_other_class,
2145 false);
2146 if (__glibc_likely (fd != -1))
2147 realname = cached;
2148 else
2149 free (cached);
2153 #endif
2155 /* Finally, try the default path. */
2156 if (fd == -1
2157 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2158 || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB)))
2159 && rtld_search_dirs.dirs != (void *) -1)
2160 fd = open_path (name, namelen, mode, &rtld_search_dirs,
2161 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
2163 /* Add another newline when we are tracing the library loading. */
2164 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2165 _dl_debug_printf ("\n");
2167 else
2169 /* The path may contain dynamic string tokens. */
2170 realname = (loader
2171 ? expand_dynamic_string_token (loader, name, 0)
2172 : __strdup (name));
2173 if (realname == NULL)
2174 fd = -1;
2175 else
2177 fd = open_verify (realname, -1, &fb,
2178 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
2179 &found_other_class, true);
2180 if (__glibc_unlikely (fd == -1))
2181 free (realname);
2185 #ifdef SHARED
2186 no_file:
2187 #endif
2188 /* In case the LOADER information has only been provided to get to
2189 the appropriate RUNPATH/RPATH information we do not need it
2190 anymore. */
2191 if (mode & __RTLD_CALLMAP)
2192 loader = NULL;
2194 if (__glibc_unlikely (fd == -1))
2196 if (trace_mode
2197 && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0))
2199 /* We haven't found an appropriate library. But since we
2200 are only interested in the list of libraries this isn't
2201 so severe. Fake an entry with all the information we
2202 have. */
2203 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2205 /* Allocate a new object map. */
2206 if ((name_copy = __strdup (name)) == NULL
2207 || (l = _dl_new_object (name_copy, name, type, loader,
2208 mode, nsid)) == NULL)
2210 free (name_copy);
2211 _dl_signal_error (ENOMEM, name, NULL,
2212 N_("cannot create shared object descriptor"));
2214 /* Signal that this is a faked entry. */
2215 l->l_faked = 1;
2216 /* Since the descriptor is initialized with zero we do not
2217 have do this here.
2218 l->l_reserved = 0; */
2219 l->l_buckets = &dummy_bucket;
2220 l->l_nbuckets = 1;
2221 l->l_relocated = 1;
2223 /* Enter the object in the object list. */
2224 _dl_add_to_namespace_list (l, nsid);
2226 return l;
2228 else if (found_other_class)
2229 _dl_signal_error (0, name, NULL,
2230 ELFW(CLASS) == ELFCLASS32
2231 ? N_("wrong ELF class: ELFCLASS64")
2232 : N_("wrong ELF class: ELFCLASS32"));
2233 else
2234 _dl_signal_error (errno, name, NULL,
2235 N_("cannot open shared object file"));
2238 void *stack_end = __libc_stack_end;
2239 return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader,
2240 type, mode, &stack_end, nsid);
2243 struct add_path_state
2245 bool counting;
2246 unsigned int idx;
2247 Dl_serinfo *si;
2248 char *allocptr;
2251 static void
2252 add_path (struct add_path_state *p, const struct r_search_path_struct *sps,
2253 unsigned int flags)
2255 if (sps->dirs != (void *) -1)
2257 struct r_search_path_elem **dirs = sps->dirs;
2260 const struct r_search_path_elem *const r = *dirs++;
2261 if (p->counting)
2263 p->si->dls_cnt++;
2264 p->si->dls_size += MAX (2, r->dirnamelen);
2266 else
2268 Dl_serpath *const sp = &p->si->dls_serpath[p->idx++];
2269 sp->dls_name = p->allocptr;
2270 if (r->dirnamelen < 2)
2271 *p->allocptr++ = r->dirnamelen ? '/' : '.';
2272 else
2273 p->allocptr = __mempcpy (p->allocptr,
2274 r->dirname, r->dirnamelen - 1);
2275 *p->allocptr++ = '\0';
2276 sp->dls_flags = flags;
2279 while (*dirs != NULL);
2283 void
2284 internal_function
2285 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2287 if (counting)
2289 si->dls_cnt = 0;
2290 si->dls_size = 0;
2293 struct add_path_state p =
2295 .counting = counting,
2296 .idx = 0,
2297 .si = si,
2298 .allocptr = (char *) &si->dls_serpath[si->dls_cnt]
2301 # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */
2303 /* When the object has the RUNPATH information we don't use any RPATHs. */
2304 if (loader->l_info[DT_RUNPATH] == NULL)
2306 /* First try the DT_RPATH of the dependent object that caused NAME
2307 to be loaded. Then that object's dependent, and on up. */
2309 struct link_map *l = loader;
2312 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2313 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2314 l = l->l_loader;
2316 while (l != NULL);
2318 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2319 if (loader->l_ns == LM_ID_BASE)
2321 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2322 if (l != NULL && l->l_type != lt_loaded && l != loader)
2323 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2324 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2328 /* Try the LD_LIBRARY_PATH environment variable. */
2329 add_path (&p, &env_path_list, XXX_ENV);
2331 /* Look at the RUNPATH information for this binary. */
2332 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2333 add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH);
2335 /* XXX
2336 Here is where ld.so.cache gets checked, but we don't have
2337 a way to indicate that in the results for Dl_serinfo. */
2339 /* Finally, try the default path. */
2340 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2341 add_path (&p, &rtld_search_dirs, XXX_default);
2343 if (counting)
2344 /* Count the struct size before the string area, which we didn't
2345 know before we completed dls_cnt. */
2346 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;