Remove hidden __libc_longjmp
[glibc.git] / elf / dl-load.c
bloba5e3a25462d4488deeb75b91fe6d35a7a054845d
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2018 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 <sysdep.h>
37 #include <stap-probe.h>
38 #include <libc-pointer-arith.h>
39 #include <array_length.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. Use
107 an array of concatenated strings to avoid relocations. See
108 gen-trusted-dirs.awk. */
109 #include "trusted-dirs.h"
111 static const char system_dirs[] = SYSTEM_DIRS;
112 static const size_t system_dirs_len[] =
114 SYSTEM_DIRS_LEN
116 #define nsystem_dirs_len array_length (system_dirs_len)
118 static bool
119 is_trusted_path_normalize (const char *path, size_t len)
121 if (len == 0)
122 return false;
124 if (*path == ':')
126 ++path;
127 --len;
130 char *npath = (char *) alloca (len + 2);
131 char *wnp = npath;
132 while (*path != '\0')
134 if (path[0] == '/')
136 if (path[1] == '.')
138 if (path[2] == '.' && (path[3] == '/' || path[3] == '\0'))
140 while (wnp > npath && *--wnp != '/')
142 path += 3;
143 continue;
145 else if (path[2] == '/' || path[2] == '\0')
147 path += 2;
148 continue;
152 if (wnp > npath && wnp[-1] == '/')
154 ++path;
155 continue;
159 *wnp++ = *path++;
162 if (wnp == npath || wnp[-1] != '/')
163 *wnp++ = '/';
165 const char *trun = system_dirs;
167 for (size_t idx = 0; idx < nsystem_dirs_len; ++idx)
169 if (wnp - npath >= system_dirs_len[idx]
170 && memcmp (trun, npath, system_dirs_len[idx]) == 0)
171 /* Found it. */
172 return true;
174 trun += system_dirs_len[idx] + 1;
177 return false;
181 static size_t
182 is_dst (const char *start, const char *name, const char *str, int secure)
184 size_t len;
185 bool is_curly = false;
187 if (name[0] == '{')
189 is_curly = true;
190 ++name;
193 len = 0;
194 while (name[len] == str[len] && name[len] != '\0')
195 ++len;
197 if (is_curly)
199 if (name[len] != '}')
200 return 0;
202 /* Point again at the beginning of the name. */
203 --name;
204 /* Skip over closing curly brace and adjust for the --name. */
205 len += 2;
207 else if (name[len] != '\0' && name[len] != '/')
208 return 0;
210 if (__glibc_unlikely (secure)
211 && ((name[len] != '\0' && name[len] != '/')
212 || (name != start + 1)))
213 return 0;
215 return len;
219 size_t
220 _dl_dst_count (const char *name)
222 const char *const start = name;
223 size_t cnt = 0;
227 size_t len;
229 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
230 is $ORIGIN alone) and it must always appear first in path. */
231 ++name;
232 if ((len = is_dst (start, name, "ORIGIN", __libc_enable_secure)) != 0
233 || (len = is_dst (start, name, "PLATFORM", 0)) != 0
234 || (len = is_dst (start, name, "LIB", 0)) != 0)
235 ++cnt;
237 name = strchr (name + len, '$');
239 while (name != NULL);
241 return cnt;
245 char *
246 _dl_dst_substitute (struct link_map *l, const char *name, char *result)
248 const char *const start = name;
250 /* Now fill the result path. While copying over the string we keep
251 track of the start of the last path element. When we come across
252 a DST we copy over the value or (if the value is not available)
253 leave the entire path element out. */
254 char *wp = result;
255 char *last_elem = result;
256 bool check_for_trusted = false;
260 if (__glibc_unlikely (*name == '$'))
262 const char *repl = NULL;
263 size_t len;
265 ++name;
266 if ((len = is_dst (start, name, "ORIGIN", __libc_enable_secure)) != 0)
268 repl = l->l_origin;
269 check_for_trusted = (__libc_enable_secure
270 && l->l_type == lt_executable);
272 else if ((len = is_dst (start, name, "PLATFORM", 0)) != 0)
273 repl = GLRO(dl_platform);
274 else if ((len = is_dst (start, name, "LIB", 0)) != 0)
275 repl = DL_DST_LIB;
277 if (repl != NULL && repl != (const char *) -1)
279 wp = __stpcpy (wp, repl);
280 name += len;
282 else if (len > 1)
284 /* We cannot use this path element, the value of the
285 replacement is unknown. */
286 wp = last_elem;
287 break;
289 else
290 /* No DST we recognize. */
291 *wp++ = '$';
293 else
295 *wp++ = *name++;
298 while (*name != '\0');
300 /* In SUID/SGID programs, after $ORIGIN expansion the normalized
301 path must be rooted in one of the trusted directories. */
302 if (__glibc_unlikely (check_for_trusted)
303 && !is_trusted_path_normalize (last_elem, wp - last_elem))
304 wp = last_elem;
306 *wp = '\0';
308 return result;
312 /* Return copy of argument with all recognized dynamic string tokens
313 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
314 might not be possible to determine the path from which the object
315 belonging to the map is loaded. In this case the path element
316 containing $ORIGIN is left out. */
317 static char *
318 expand_dynamic_string_token (struct link_map *l, const char *s)
320 /* We make two runs over the string. First we determine how large the
321 resulting string is and then we copy it over. Since this is no
322 frequently executed operation we are looking here not for performance
323 but rather for code size. */
324 size_t cnt;
325 size_t total;
326 char *result;
328 /* Determine the number of DST elements. */
329 cnt = DL_DST_COUNT (s);
331 /* If we do not have to replace anything simply copy the string. */
332 if (__glibc_likely (cnt == 0))
333 return __strdup (s);
335 /* Determine the length of the substituted string. */
336 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
338 /* Allocate the necessary memory. */
339 result = (char *) malloc (total + 1);
340 if (result == NULL)
341 return NULL;
343 return _dl_dst_substitute (l, s, result);
347 /* Add `name' to the list of names for a particular shared object.
348 `name' is expected to have been allocated with malloc and will
349 be freed if the shared object already has this name.
350 Returns false if the object already had this name. */
351 static void
352 add_name_to_object (struct link_map *l, const char *name)
354 struct libname_list *lnp, *lastp;
355 struct libname_list *newname;
356 size_t name_len;
358 lastp = NULL;
359 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
360 if (strcmp (name, lnp->name) == 0)
361 return;
363 name_len = strlen (name) + 1;
364 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
365 if (newname == NULL)
367 /* No more memory. */
368 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
369 return;
371 /* The object should have a libname set from _dl_new_object. */
372 assert (lastp != NULL);
374 newname->name = memcpy (newname + 1, name, name_len);
375 newname->next = NULL;
376 newname->dont_free = 0;
377 lastp->next = newname;
380 /* Standard search directories. */
381 static struct r_search_path_struct rtld_search_dirs attribute_relro;
383 static size_t max_dirnamelen;
385 static struct r_search_path_elem **
386 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
387 const char *what, const char *where, struct link_map *l)
389 char *cp;
390 size_t nelems = 0;
392 while ((cp = __strsep (&rpath, sep)) != NULL)
394 struct r_search_path_elem *dirp;
395 char *to_free = NULL;
396 size_t len = 0;
398 /* `strsep' can pass an empty string. */
399 if (*cp != '\0')
401 to_free = cp = expand_dynamic_string_token (l, cp);
403 /* expand_dynamic_string_token can return NULL in case of empty
404 path or memory allocation failure. */
405 if (cp == NULL)
406 continue;
408 /* Compute the length after dynamic string token expansion and
409 ignore empty paths. */
410 len = strlen (cp);
411 if (len == 0)
413 free (to_free);
414 continue;
417 /* Remove trailing slashes (except for "/"). */
418 while (len > 1 && cp[len - 1] == '/')
419 --len;
421 /* Now add one if there is none so far. */
422 if (len > 0 && cp[len - 1] != '/')
423 cp[len++] = '/';
426 /* See if this directory is already known. */
427 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
428 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
429 break;
431 if (dirp != NULL)
433 /* It is available, see whether it's on our own list. */
434 size_t cnt;
435 for (cnt = 0; cnt < nelems; ++cnt)
436 if (result[cnt] == dirp)
437 break;
439 if (cnt == nelems)
440 result[nelems++] = dirp;
442 else
444 size_t cnt;
445 enum r_dir_status init_val;
446 size_t where_len = where ? strlen (where) + 1 : 0;
448 /* It's a new directory. Create an entry and add it. */
449 dirp = (struct r_search_path_elem *)
450 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
451 + where_len + len + 1);
452 if (dirp == NULL)
453 _dl_signal_error (ENOMEM, NULL, NULL,
454 N_("cannot create cache for search path"));
456 dirp->dirname = ((char *) dirp + sizeof (*dirp)
457 + ncapstr * sizeof (enum r_dir_status));
458 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
459 dirp->dirnamelen = len;
461 if (len > max_dirnamelen)
462 max_dirnamelen = len;
464 /* We have to make sure all the relative directories are
465 never ignored. The current directory might change and
466 all our saved information would be void. */
467 init_val = cp[0] != '/' ? existing : unknown;
468 for (cnt = 0; cnt < ncapstr; ++cnt)
469 dirp->status[cnt] = init_val;
471 dirp->what = what;
472 if (__glibc_likely (where != NULL))
473 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
474 + (ncapstr * sizeof (enum r_dir_status)),
475 where, where_len);
476 else
477 dirp->where = NULL;
479 dirp->next = GL(dl_all_dirs);
480 GL(dl_all_dirs) = dirp;
482 /* Put it in the result array. */
483 result[nelems++] = dirp;
485 free (to_free);
488 /* Terminate the array. */
489 result[nelems] = NULL;
491 return result;
495 static bool
496 decompose_rpath (struct r_search_path_struct *sps,
497 const char *rpath, struct link_map *l, const char *what)
499 /* Make a copy we can work with. */
500 const char *where = l->l_name;
501 char *cp;
502 struct r_search_path_elem **result;
503 size_t nelems;
504 /* Initialize to please the compiler. */
505 const char *errstring = NULL;
507 /* First see whether we must forget the RUNPATH and RPATH from this
508 object. */
509 if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL)
510 && !__libc_enable_secure)
512 const char *inhp = GLRO(dl_inhibit_rpath);
516 const char *wp = where;
518 while (*inhp == *wp && *wp != '\0')
520 ++inhp;
521 ++wp;
524 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
526 /* This object is on the list of objects for which the
527 RUNPATH and RPATH must not be used. */
528 sps->dirs = (void *) -1;
529 return false;
532 while (*inhp != '\0')
533 if (*inhp++ == ':')
534 break;
536 while (*inhp != '\0');
539 /* Ignore empty rpaths. */
540 if (*rpath == '\0')
542 sps->dirs = (struct r_search_path_elem **) -1;
543 return false;
546 /* Make a writable copy. */
547 char *copy = __strdup (rpath);
548 if (copy == NULL)
550 errstring = N_("cannot create RUNPATH/RPATH copy");
551 goto signal_error;
554 /* Count the number of necessary elements in the result array. */
555 nelems = 0;
556 for (cp = copy; *cp != '\0'; ++cp)
557 if (*cp == ':')
558 ++nelems;
560 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
561 number of necessary entries. */
562 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
563 * sizeof (*result));
564 if (result == NULL)
566 free (copy);
567 errstring = N_("cannot create cache for search path");
568 signal_error:
569 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
572 fillin_rpath (copy, result, ":", what, where, l);
574 /* Free the copied RPATH string. `fillin_rpath' make own copies if
575 necessary. */
576 free (copy);
578 /* There is no path after expansion. */
579 if (result[0] == NULL)
581 free (result);
582 sps->dirs = (struct r_search_path_elem **) -1;
583 return false;
586 sps->dirs = result;
587 /* The caller will change this value if we haven't used a real malloc. */
588 sps->malloced = 1;
589 return true;
592 /* Make sure cached path information is stored in *SP
593 and return true if there are any paths to search there. */
594 static bool
595 cache_rpath (struct link_map *l,
596 struct r_search_path_struct *sp,
597 int tag,
598 const char *what)
600 if (sp->dirs == (void *) -1)
601 return false;
603 if (sp->dirs != NULL)
604 return true;
606 if (l->l_info[tag] == NULL)
608 /* There is no path. */
609 sp->dirs = (void *) -1;
610 return false;
613 /* Make sure the cache information is available. */
614 return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
615 + l->l_info[tag]->d_un.d_val),
616 l, what);
620 void
621 _dl_init_paths (const char *llp)
623 size_t idx;
624 const char *strp;
625 struct r_search_path_elem *pelem, **aelem;
626 size_t round_size;
627 struct link_map __attribute__ ((unused)) *l = NULL;
628 /* Initialize to please the compiler. */
629 const char *errstring = NULL;
631 /* Fill in the information about the application's RPATH and the
632 directories addressed by the LD_LIBRARY_PATH environment variable. */
634 /* Get the capabilities. */
635 capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen),
636 &ncapstr, &max_capstrlen);
638 /* First set up the rest of the default search directory entries. */
639 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
640 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
641 if (rtld_search_dirs.dirs == NULL)
643 errstring = N_("cannot create search path array");
644 signal_error:
645 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
648 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
649 + ncapstr * sizeof (enum r_dir_status))
650 / sizeof (struct r_search_path_elem));
652 rtld_search_dirs.dirs[0] = malloc (nsystem_dirs_len * round_size
653 * sizeof (*rtld_search_dirs.dirs[0]));
654 if (rtld_search_dirs.dirs[0] == NULL)
656 errstring = N_("cannot create cache for search path");
657 goto signal_error;
660 rtld_search_dirs.malloced = 0;
661 pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0];
662 strp = system_dirs;
663 idx = 0;
667 size_t cnt;
669 *aelem++ = pelem;
671 pelem->what = "system search path";
672 pelem->where = NULL;
674 pelem->dirname = strp;
675 pelem->dirnamelen = system_dirs_len[idx];
676 strp += system_dirs_len[idx] + 1;
678 /* System paths must be absolute. */
679 assert (pelem->dirname[0] == '/');
680 for (cnt = 0; cnt < ncapstr; ++cnt)
681 pelem->status[cnt] = unknown;
683 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
685 pelem += round_size;
687 while (idx < nsystem_dirs_len);
689 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
690 *aelem = NULL;
692 #ifdef SHARED
693 /* This points to the map of the main object. */
694 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
695 if (l != NULL)
697 assert (l->l_type != lt_loaded);
699 if (l->l_info[DT_RUNPATH])
701 /* Allocate room for the search path and fill in information
702 from RUNPATH. */
703 decompose_rpath (&l->l_runpath_dirs,
704 (const void *) (D_PTR (l, l_info[DT_STRTAB])
705 + l->l_info[DT_RUNPATH]->d_un.d_val),
706 l, "RUNPATH");
707 /* During rtld init the memory is allocated by the stub malloc,
708 prevent any attempt to free it by the normal malloc. */
709 l->l_runpath_dirs.malloced = 0;
711 /* The RPATH is ignored. */
712 l->l_rpath_dirs.dirs = (void *) -1;
714 else
716 l->l_runpath_dirs.dirs = (void *) -1;
718 if (l->l_info[DT_RPATH])
720 /* Allocate room for the search path and fill in information
721 from RPATH. */
722 decompose_rpath (&l->l_rpath_dirs,
723 (const void *) (D_PTR (l, l_info[DT_STRTAB])
724 + l->l_info[DT_RPATH]->d_un.d_val),
725 l, "RPATH");
726 /* During rtld init the memory is allocated by the stub
727 malloc, prevent any attempt to free it by the normal
728 malloc. */
729 l->l_rpath_dirs.malloced = 0;
731 else
732 l->l_rpath_dirs.dirs = (void *) -1;
735 #endif /* SHARED */
737 if (llp != NULL && *llp != '\0')
739 char *llp_tmp = strdupa (llp);
741 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
742 elements it has. */
743 size_t nllp = 1;
744 for (const char *cp = llp_tmp; *cp != '\0'; ++cp)
745 if (*cp == ':' || *cp == ';')
746 ++nllp;
748 env_path_list.dirs = (struct r_search_path_elem **)
749 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
750 if (env_path_list.dirs == NULL)
752 errstring = N_("cannot create cache for search path");
753 goto signal_error;
756 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
757 "LD_LIBRARY_PATH", NULL, l);
759 if (env_path_list.dirs[0] == NULL)
761 free (env_path_list.dirs);
762 env_path_list.dirs = (void *) -1;
765 env_path_list.malloced = 0;
767 else
768 env_path_list.dirs = (void *) -1;
772 static void
773 __attribute__ ((noreturn, noinline))
774 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
775 const char *msg, struct r_debug *r, Lmid_t nsid)
777 /* The file might already be closed. */
778 if (fd != -1)
779 (void) __close (fd);
780 if (l != NULL && l->l_origin != (char *) -1l)
781 free ((char *) l->l_origin);
782 free (l);
783 free (realname);
785 if (r != NULL)
787 r->r_state = RT_CONSISTENT;
788 _dl_debug_state ();
789 LIBC_PROBE (map_failed, 2, nsid, r);
792 _dl_signal_error (code, name, NULL, msg);
796 /* Map in the shared object NAME, actually located in REALNAME, and already
797 opened on FD. */
799 #ifndef EXTERNAL_MAP_FROM_FD
800 static
801 #endif
802 struct link_map *
803 _dl_map_object_from_fd (const char *name, const char *origname, int fd,
804 struct filebuf *fbp, char *realname,
805 struct link_map *loader, int l_type, int mode,
806 void **stack_endp, Lmid_t nsid)
808 struct link_map *l = NULL;
809 const ElfW(Ehdr) *header;
810 const ElfW(Phdr) *phdr;
811 const ElfW(Phdr) *ph;
812 size_t maplength;
813 int type;
814 /* Initialize to keep the compiler happy. */
815 const char *errstring = NULL;
816 int errval = 0;
817 struct r_debug *r = _dl_debug_initialize (0, nsid);
818 bool make_consistent = false;
820 /* Get file information. */
821 struct r_file_id id;
822 if (__glibc_unlikely (!_dl_get_file_id (fd, &id)))
824 errstring = N_("cannot stat shared object");
825 call_lose_errno:
826 errval = errno;
827 call_lose:
828 lose (errval, fd, name, realname, l, errstring,
829 make_consistent ? r : NULL, nsid);
832 /* Look again to see if the real name matched another already loaded. */
833 for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
834 if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id))
836 /* The object is already loaded.
837 Just bump its reference count and return it. */
838 __close (fd);
840 /* If the name is not in the list of names for this object add
841 it. */
842 free (realname);
843 add_name_to_object (l, name);
845 return l;
848 #ifdef SHARED
849 /* When loading into a namespace other than the base one we must
850 avoid loading ld.so since there can only be one copy. Ever. */
851 if (__glibc_unlikely (nsid != LM_ID_BASE)
852 && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id)
853 || _dl_name_match_p (name, &GL(dl_rtld_map))))
855 /* This is indeed ld.so. Create a new link_map which refers to
856 the real one for almost everything. */
857 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
858 if (l == NULL)
859 goto fail_new;
861 /* Refer to the real descriptor. */
862 l->l_real = &GL(dl_rtld_map);
864 /* No need to bump the refcount of the real object, ld.so will
865 never be unloaded. */
866 __close (fd);
868 /* Add the map for the mirrored object to the object list. */
869 _dl_add_to_namespace_list (l, nsid);
871 return l;
873 #endif
875 if (mode & RTLD_NOLOAD)
877 /* We are not supposed to load the object unless it is already
878 loaded. So return now. */
879 free (realname);
880 __close (fd);
881 return NULL;
884 /* Print debugging message. */
885 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
886 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
888 /* This is the ELF header. We read it in `open_verify'. */
889 header = (void *) fbp->buf;
891 #ifndef MAP_ANON
892 # define MAP_ANON 0
893 if (_dl_zerofd == -1)
895 _dl_zerofd = _dl_sysdep_open_zero_fill ();
896 if (_dl_zerofd == -1)
898 free (realname);
899 __close (fd);
900 _dl_signal_error (errno, NULL, NULL,
901 N_("cannot open zero fill device"));
904 #endif
906 /* Signal that we are going to add new objects. */
907 if (r->r_state == RT_CONSISTENT)
909 #ifdef SHARED
910 /* Auditing checkpoint: we are going to add new objects. */
911 if ((mode & __RTLD_AUDIT) == 0
912 && __glibc_unlikely (GLRO(dl_naudit) > 0))
914 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
915 /* Do not call the functions for any auditing object. */
916 if (head->l_auditing == 0)
918 struct audit_ifaces *afct = GLRO(dl_audit);
919 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
921 if (afct->activity != NULL)
922 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD);
924 afct = afct->next;
928 #endif
930 /* Notify the debugger we have added some objects. We need to
931 call _dl_debug_initialize in a static program in case dynamic
932 linking has not been used before. */
933 r->r_state = RT_ADD;
934 _dl_debug_state ();
935 LIBC_PROBE (map_start, 2, nsid, r);
936 make_consistent = true;
938 else
939 assert (r->r_state == RT_ADD);
941 /* Enter the new object in the list of loaded objects. */
942 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
943 if (__glibc_unlikely (l == NULL))
945 #ifdef SHARED
946 fail_new:
947 #endif
948 errstring = N_("cannot create shared object descriptor");
949 goto call_lose_errno;
952 /* Extract the remaining details we need from the ELF header
953 and then read in the program header table. */
954 l->l_entry = header->e_entry;
955 type = header->e_type;
956 l->l_phnum = header->e_phnum;
958 maplength = header->e_phnum * sizeof (ElfW(Phdr));
959 if (header->e_phoff + maplength <= (size_t) fbp->len)
960 phdr = (void *) (fbp->buf + header->e_phoff);
961 else
963 phdr = alloca (maplength);
964 __lseek (fd, header->e_phoff, SEEK_SET);
965 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
967 errstring = N_("cannot read file data");
968 goto call_lose_errno;
972 /* On most platforms presume that PT_GNU_STACK is absent and the stack is
973 * executable. Other platforms default to a nonexecutable stack and don't
974 * need PT_GNU_STACK to do so. */
975 uint_fast16_t stack_flags = DEFAULT_STACK_PERMS;
978 /* Scan the program header table, collecting its load commands. */
979 struct loadcmd loadcmds[l->l_phnum];
980 size_t nloadcmds = 0;
981 bool has_holes = false;
983 /* The struct is initialized to zero so this is not necessary:
984 l->l_ld = 0;
985 l->l_phdr = 0;
986 l->l_addr = 0; */
987 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
988 switch (ph->p_type)
990 /* These entries tell us where to find things once the file's
991 segments are mapped in. We record the addresses it says
992 verbatim, and later correct for the run-time load address. */
993 case PT_DYNAMIC:
994 if (ph->p_filesz)
996 /* Debuginfo only files from "objcopy --only-keep-debug"
997 contain a PT_DYNAMIC segment with p_filesz == 0. Skip
998 such a segment to avoid a crash later. */
999 l->l_ld = (void *) ph->p_vaddr;
1000 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1002 break;
1004 case PT_PHDR:
1005 l->l_phdr = (void *) ph->p_vaddr;
1006 break;
1008 case PT_LOAD:
1009 /* A load command tells us to map in part of the file.
1010 We record the load commands and process them all later. */
1011 if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0))
1013 errstring = N_("ELF load command alignment not page-aligned");
1014 goto call_lose;
1016 if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
1017 & (ph->p_align - 1)) != 0))
1019 errstring
1020 = N_("ELF load command address/offset not properly aligned");
1021 goto call_lose;
1024 struct loadcmd *c = &loadcmds[nloadcmds++];
1025 c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize));
1026 c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
1027 c->dataend = ph->p_vaddr + ph->p_filesz;
1028 c->allocend = ph->p_vaddr + ph->p_memsz;
1029 c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
1031 /* Determine whether there is a gap between the last segment
1032 and this one. */
1033 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1034 has_holes = true;
1036 /* Optimize a common case. */
1037 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1038 c->prot = (PF_TO_PROT
1039 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1040 #else
1041 c->prot = 0;
1042 if (ph->p_flags & PF_R)
1043 c->prot |= PROT_READ;
1044 if (ph->p_flags & PF_W)
1045 c->prot |= PROT_WRITE;
1046 if (ph->p_flags & PF_X)
1047 c->prot |= PROT_EXEC;
1048 #endif
1049 break;
1051 case PT_TLS:
1052 if (ph->p_memsz == 0)
1053 /* Nothing to do for an empty segment. */
1054 break;
1056 l->l_tls_blocksize = ph->p_memsz;
1057 l->l_tls_align = ph->p_align;
1058 if (ph->p_align == 0)
1059 l->l_tls_firstbyte_offset = 0;
1060 else
1061 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1062 l->l_tls_initimage_size = ph->p_filesz;
1063 /* Since we don't know the load address yet only store the
1064 offset. We will adjust it later. */
1065 l->l_tls_initimage = (void *) ph->p_vaddr;
1067 /* If not loading the initial set of shared libraries,
1068 check whether we should permit loading a TLS segment. */
1069 if (__glibc_likely (l->l_type == lt_library)
1070 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1071 not set up TLS data structures, so don't use them now. */
1072 || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL))
1074 /* Assign the next available module ID. */
1075 l->l_tls_modid = _dl_next_tls_modid ();
1076 break;
1079 #ifdef SHARED
1080 /* We are loading the executable itself when the dynamic
1081 linker was executed directly. The setup will happen
1082 later. Otherwise, the TLS data structures are already
1083 initialized, and we assigned a TLS modid above. */
1084 assert (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0);
1085 #else
1086 assert (false && "TLS not initialized in static application");
1087 #endif
1088 break;
1090 case PT_GNU_STACK:
1091 stack_flags = ph->p_flags;
1092 break;
1094 case PT_GNU_RELRO:
1095 l->l_relro_addr = ph->p_vaddr;
1096 l->l_relro_size = ph->p_memsz;
1097 break;
1100 if (__glibc_unlikely (nloadcmds == 0))
1102 /* This only happens for a bogus object that will be caught with
1103 another error below. But we don't want to go through the
1104 calculations below using NLOADCMDS - 1. */
1105 errstring = N_("object file has no loadable segments");
1106 goto call_lose;
1109 if (__glibc_unlikely (type != ET_DYN)
1110 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))
1112 /* This object is loaded at a fixed address. This must never
1113 happen for objects loaded with dlopen. */
1114 errstring = N_("cannot dynamically load executable");
1115 goto call_lose;
1118 /* Length of the sections to be loaded. */
1119 maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
1121 /* Now process the load commands and map segments into memory.
1122 This is responsible for filling in:
1123 l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr
1125 errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
1126 maplength, has_holes, loader);
1127 if (__glibc_unlikely (errstring != NULL))
1128 goto call_lose;
1131 if (l->l_ld == 0)
1133 if (__glibc_unlikely (type == ET_DYN))
1135 errstring = N_("object file has no dynamic section");
1136 goto call_lose;
1139 else
1140 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1142 elf_get_dynamic_info (l, NULL);
1144 /* Make sure we are not dlopen'ing an object that has the
1145 DF_1_NOOPEN flag set. */
1146 if (__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
1147 && (mode & __RTLD_DLOPEN))
1149 /* We are not supposed to load this object. Free all resources. */
1150 _dl_unmap_segments (l);
1152 if (!l->l_libname->dont_free)
1153 free (l->l_libname);
1155 if (l->l_phdr_allocated)
1156 free ((void *) l->l_phdr);
1158 errstring = N_("shared object cannot be dlopen()ed");
1159 goto call_lose;
1162 if (l->l_phdr == NULL)
1164 /* The program header is not contained in any of the segments.
1165 We have to allocate memory ourself and copy it over from out
1166 temporary place. */
1167 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1168 * sizeof (ElfW(Phdr)));
1169 if (newp == NULL)
1171 errstring = N_("cannot allocate memory for program header");
1172 goto call_lose_errno;
1175 l->l_phdr = memcpy (newp, phdr,
1176 (header->e_phnum * sizeof (ElfW(Phdr))));
1177 l->l_phdr_allocated = 1;
1179 else
1180 /* Adjust the PT_PHDR value by the runtime load address. */
1181 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1183 if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X))
1185 /* The stack is presently not executable, but this module
1186 requires that it be executable. We must change the
1187 protection of the variable which contains the flags used in
1188 the mprotect calls. */
1189 #ifdef SHARED
1190 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1192 const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
1193 const size_t s = (uintptr_t) (&__stack_prot + 1) - p;
1195 struct link_map *const m = &GL(dl_rtld_map);
1196 const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr
1197 + m->l_relro_size)
1198 & -GLRO(dl_pagesize));
1199 if (__glibc_likely (p + s <= relro_end))
1201 /* The variable lies in the region protected by RELRO. */
1202 if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0)
1204 errstring = N_("cannot change memory protections");
1205 goto call_lose_errno;
1207 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1208 __mprotect ((void *) p, s, PROT_READ);
1210 else
1211 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1213 else
1214 #endif
1215 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1217 #ifdef check_consistency
1218 check_consistency ();
1219 #endif
1221 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1222 if (errval)
1224 errstring = N_("\
1225 cannot enable executable stack as shared object requires");
1226 goto call_lose;
1230 /* Adjust the address of the TLS initialization image. */
1231 if (l->l_tls_initimage != NULL)
1232 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1234 /* We are done mapping in the file. We no longer need the descriptor. */
1235 if (__glibc_unlikely (__close (fd) != 0))
1237 errstring = N_("cannot close file descriptor");
1238 goto call_lose_errno;
1240 /* Signal that we closed the file. */
1241 fd = -1;
1243 /* If this is ET_EXEC, we should have loaded it as lt_executable. */
1244 assert (type != ET_EXEC || l->l_type == lt_executable);
1246 l->l_entry += l->l_addr;
1248 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1249 _dl_debug_printf ("\
1250 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1251 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1252 (int) sizeof (void *) * 2,
1253 (unsigned long int) l->l_ld,
1254 (int) sizeof (void *) * 2,
1255 (unsigned long int) l->l_addr,
1256 (int) sizeof (void *) * 2, maplength,
1257 (int) sizeof (void *) * 2,
1258 (unsigned long int) l->l_entry,
1259 (int) sizeof (void *) * 2,
1260 (unsigned long int) l->l_phdr,
1261 (int) sizeof (void *) * 2, l->l_phnum);
1263 /* Set up the symbol hash table. */
1264 _dl_setup_hash (l);
1266 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1267 have to do this for the main map. */
1268 if ((mode & RTLD_DEEPBIND) == 0
1269 && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL)
1270 && &l->l_searchlist != l->l_scope[0])
1272 /* Create an appropriate searchlist. It contains only this map.
1273 This is the definition of DT_SYMBOLIC in SysVr4. */
1274 l->l_symbolic_searchlist.r_list[0] = l;
1275 l->l_symbolic_searchlist.r_nlist = 1;
1277 /* Now move the existing entries one back. */
1278 memmove (&l->l_scope[1], &l->l_scope[0],
1279 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1281 /* Now add the new entry. */
1282 l->l_scope[0] = &l->l_symbolic_searchlist;
1285 /* Remember whether this object must be initialized first. */
1286 if (l->l_flags_1 & DF_1_INITFIRST)
1287 GL(dl_initfirst) = l;
1289 /* Finally the file information. */
1290 l->l_file_id = id;
1292 #ifdef SHARED
1293 /* When auditing is used the recorded names might not include the
1294 name by which the DSO is actually known. Add that as well. */
1295 if (__glibc_unlikely (origname != NULL))
1296 add_name_to_object (l, origname);
1297 #else
1298 /* Audit modules only exist when linking is dynamic so ORIGNAME
1299 cannot be non-NULL. */
1300 assert (origname == NULL);
1301 #endif
1303 /* When we profile the SONAME might be needed for something else but
1304 loading. Add it right away. */
1305 if (__glibc_unlikely (GLRO(dl_profile) != NULL)
1306 && l->l_info[DT_SONAME] != NULL)
1307 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1308 + l->l_info[DT_SONAME]->d_un.d_val));
1310 #ifdef DL_AFTER_LOAD
1311 DL_AFTER_LOAD (l);
1312 #endif
1314 /* Now that the object is fully initialized add it to the object list. */
1315 _dl_add_to_namespace_list (l, nsid);
1317 #ifdef SHARED
1318 /* Auditing checkpoint: we have a new object. */
1319 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1320 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1322 struct audit_ifaces *afct = GLRO(dl_audit);
1323 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1325 if (afct->objopen != NULL)
1327 l->l_audit[cnt].bindflags
1328 = afct->objopen (l, nsid, &l->l_audit[cnt].cookie);
1330 l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0;
1333 afct = afct->next;
1336 #endif
1338 return l;
1341 /* Print search path. */
1342 static void
1343 print_search_path (struct r_search_path_elem **list,
1344 const char *what, const char *name)
1346 char buf[max_dirnamelen + max_capstrlen];
1347 int first = 1;
1349 _dl_debug_printf (" search path=");
1351 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1353 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1354 size_t cnt;
1356 for (cnt = 0; cnt < ncapstr; ++cnt)
1357 if ((*list)->status[cnt] != nonexisting)
1359 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1360 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1361 cp[0] = '\0';
1362 else
1363 cp[-1] = '\0';
1365 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1366 first = 0;
1369 ++list;
1372 if (name != NULL)
1373 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1374 DSO_FILENAME (name));
1375 else
1376 _dl_debug_printf_c ("\t\t(%s)\n", what);
1379 /* Open a file and verify it is an ELF file for this architecture. We
1380 ignore only ELF files for other architectures. Non-ELF files and
1381 ELF files with different header information cause fatal errors since
1382 this could mean there is something wrong in the installation and the
1383 user might want to know about this.
1385 If FD is not -1, then the file is already open and FD refers to it.
1386 In that case, FD is consumed for both successful and error returns. */
1387 static int
1388 open_verify (const char *name, int fd,
1389 struct filebuf *fbp, struct link_map *loader,
1390 int whatcode, int mode, bool *found_other_class, bool free_name)
1392 /* This is the expected ELF header. */
1393 #define ELF32_CLASS ELFCLASS32
1394 #define ELF64_CLASS ELFCLASS64
1395 #ifndef VALID_ELF_HEADER
1396 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1397 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1398 # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0)
1399 #elif defined MORE_ELF_HEADER_DATA
1400 MORE_ELF_HEADER_DATA;
1401 #endif
1402 static const unsigned char expected[EI_NIDENT] =
1404 [EI_MAG0] = ELFMAG0,
1405 [EI_MAG1] = ELFMAG1,
1406 [EI_MAG2] = ELFMAG2,
1407 [EI_MAG3] = ELFMAG3,
1408 [EI_CLASS] = ELFW(CLASS),
1409 [EI_DATA] = byteorder,
1410 [EI_VERSION] = EV_CURRENT,
1411 [EI_OSABI] = ELFOSABI_SYSV,
1412 [EI_ABIVERSION] = 0
1414 static const struct
1416 ElfW(Word) vendorlen;
1417 ElfW(Word) datalen;
1418 ElfW(Word) type;
1419 char vendor[4];
1420 } expected_note = { 4, 16, 1, "GNU" };
1421 /* Initialize it to make the compiler happy. */
1422 const char *errstring = NULL;
1423 int errval = 0;
1425 #ifdef SHARED
1426 /* Give the auditing libraries a chance. */
1427 if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0
1428 && loader->l_auditing == 0)
1430 const char *original_name = name;
1431 struct audit_ifaces *afct = GLRO(dl_audit);
1432 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1434 if (afct->objsearch != NULL)
1436 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1437 whatcode);
1438 if (name == NULL)
1439 /* Ignore the path. */
1440 return -1;
1443 afct = afct->next;
1446 if (fd != -1 && name != original_name && strcmp (name, original_name))
1448 /* An audit library changed what we're supposed to open,
1449 so FD no longer matches it. */
1450 __close (fd);
1451 fd = -1;
1454 #endif
1456 if (fd == -1)
1457 /* Open the file. We always open files read-only. */
1458 fd = __open (name, O_RDONLY | O_CLOEXEC);
1460 if (fd != -1)
1462 ElfW(Ehdr) *ehdr;
1463 ElfW(Phdr) *phdr, *ph;
1464 ElfW(Word) *abi_note;
1465 unsigned int osversion;
1466 size_t maplength;
1468 /* We successfully opened the file. Now verify it is a file
1469 we can use. */
1470 __set_errno (0);
1471 fbp->len = 0;
1472 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1473 /* Read in the header. */
1476 ssize_t retlen = __libc_read (fd, fbp->buf + fbp->len,
1477 sizeof (fbp->buf) - fbp->len);
1478 if (retlen <= 0)
1479 break;
1480 fbp->len += retlen;
1482 while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr))));
1484 /* This is where the ELF header is loaded. */
1485 ehdr = (ElfW(Ehdr) *) fbp->buf;
1487 /* Now run the tests. */
1488 if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr))))
1490 errval = errno;
1491 errstring = (errval == 0
1492 ? N_("file too short") : N_("cannot read file data"));
1493 call_lose:
1494 if (free_name)
1496 char *realname = (char *) name;
1497 name = strdupa (realname);
1498 free (realname);
1500 lose (errval, fd, name, NULL, NULL, errstring, NULL, 0);
1503 /* See whether the ELF header is what we expect. */
1504 if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1505 EI_ABIVERSION)
1506 || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1507 ehdr->e_ident[EI_ABIVERSION])
1508 || memcmp (&ehdr->e_ident[EI_PAD],
1509 &expected[EI_PAD],
1510 EI_NIDENT - EI_PAD) != 0))
1512 /* Something is wrong. */
1513 const Elf32_Word *magp = (const void *) ehdr->e_ident;
1514 if (*magp !=
1515 #if BYTE_ORDER == LITTLE_ENDIAN
1516 ((ELFMAG0 << (EI_MAG0 * 8)) |
1517 (ELFMAG1 << (EI_MAG1 * 8)) |
1518 (ELFMAG2 << (EI_MAG2 * 8)) |
1519 (ELFMAG3 << (EI_MAG3 * 8)))
1520 #else
1521 ((ELFMAG0 << (EI_MAG3 * 8)) |
1522 (ELFMAG1 << (EI_MAG2 * 8)) |
1523 (ELFMAG2 << (EI_MAG1 * 8)) |
1524 (ELFMAG3 << (EI_MAG0 * 8)))
1525 #endif
1527 errstring = N_("invalid ELF header");
1528 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1530 /* This is not a fatal error. On architectures where
1531 32-bit and 64-bit binaries can be run this might
1532 happen. */
1533 *found_other_class = true;
1534 goto close_and_out;
1536 else if (ehdr->e_ident[EI_DATA] != byteorder)
1538 if (BYTE_ORDER == BIG_ENDIAN)
1539 errstring = N_("ELF file data encoding not big-endian");
1540 else
1541 errstring = N_("ELF file data encoding not little-endian");
1543 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1544 errstring
1545 = N_("ELF file version ident does not match current one");
1546 /* XXX We should be able so set system specific versions which are
1547 allowed here. */
1548 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1549 errstring = N_("ELF file OS ABI invalid");
1550 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1551 ehdr->e_ident[EI_ABIVERSION]))
1552 errstring = N_("ELF file ABI version invalid");
1553 else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD],
1554 EI_NIDENT - EI_PAD) != 0)
1555 errstring = N_("nonzero padding in e_ident");
1556 else
1557 /* Otherwise we don't know what went wrong. */
1558 errstring = N_("internal error");
1560 goto call_lose;
1563 if (__glibc_unlikely (ehdr->e_version != EV_CURRENT))
1565 errstring = N_("ELF file version does not match current one");
1566 goto call_lose;
1568 if (! __glibc_likely (elf_machine_matches_host (ehdr)))
1569 goto close_and_out;
1570 else if (__glibc_unlikely (ehdr->e_type != ET_DYN
1571 && ehdr->e_type != ET_EXEC))
1573 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1574 goto call_lose;
1576 else if (__glibc_unlikely (ehdr->e_type == ET_EXEC
1577 && (mode & __RTLD_OPENEXEC) == 0))
1579 /* BZ #16634. It is an error to dlopen ET_EXEC (unless
1580 __RTLD_OPENEXEC is explicitly set). We return error here
1581 so that code in _dl_map_object_from_fd does not try to set
1582 l_tls_modid for this module. */
1584 errstring = N_("cannot dynamically load executable");
1585 goto call_lose;
1587 else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr))))
1589 errstring = N_("ELF file's phentsize not the expected size");
1590 goto call_lose;
1593 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1594 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1595 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1596 else
1598 phdr = alloca (maplength);
1599 __lseek (fd, ehdr->e_phoff, SEEK_SET);
1600 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1602 read_error:
1603 errval = errno;
1604 errstring = N_("cannot read file data");
1605 goto call_lose;
1609 if (__glibc_unlikely (elf_machine_reject_phdr_p
1610 (phdr, ehdr->e_phnum, fbp->buf, fbp->len,
1611 loader, fd)))
1612 goto close_and_out;
1614 /* Check .note.ABI-tag if present. */
1615 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1616 if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 && ph->p_align >= 4)
1618 ElfW(Addr) size = ph->p_filesz;
1619 /* NB: Some PT_NOTE segment may have alignment value of 0
1620 or 1. gABI specifies that PT_NOTE segments should be
1621 aligned to 4 bytes in 32-bit objects and to 8 bytes in
1622 64-bit objects. As a Linux extension, we also support
1623 4 byte alignment in 64-bit objects. If p_align is less
1624 than 4, we treate alignment as 4 bytes since some note
1625 segments have 0 or 1 byte alignment. */
1626 ElfW(Addr) align = ph->p_align;
1627 if (align < 4)
1628 align = 4;
1629 else if (align != 4 && align != 8)
1630 continue;
1632 if (ph->p_offset + size <= (size_t) fbp->len)
1633 abi_note = (void *) (fbp->buf + ph->p_offset);
1634 else
1636 abi_note = alloca (size);
1637 __lseek (fd, ph->p_offset, SEEK_SET);
1638 if (__libc_read (fd, (void *) abi_note, size) != size)
1639 goto read_error;
1642 while (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1644 ElfW(Addr) note_size
1645 = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1],
1646 align);
1648 if (size - 32 < note_size)
1650 size = 0;
1651 break;
1653 size -= note_size;
1654 abi_note = (void *) abi_note + note_size;
1657 if (size == 0)
1658 continue;
1660 osversion = (abi_note[5] & 0xff) * 65536
1661 + (abi_note[6] & 0xff) * 256
1662 + (abi_note[7] & 0xff);
1663 if (abi_note[4] != __ABI_TAG_OS
1664 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1666 close_and_out:
1667 __close (fd);
1668 __set_errno (ENOENT);
1669 fd = -1;
1672 break;
1676 return fd;
1679 /* Try to open NAME in one of the directories in *DIRSP.
1680 Return the fd, or -1. If successful, fill in *REALNAME
1681 with the malloc'd full directory name. If it turns out
1682 that none of the directories in *DIRSP exists, *DIRSP is
1683 replaced with (void *) -1, and the old value is free()d
1684 if MAY_FREE_DIRS is true. */
1686 static int
1687 open_path (const char *name, size_t namelen, int mode,
1688 struct r_search_path_struct *sps, char **realname,
1689 struct filebuf *fbp, struct link_map *loader, int whatcode,
1690 bool *found_other_class)
1692 struct r_search_path_elem **dirs = sps->dirs;
1693 char *buf;
1694 int fd = -1;
1695 const char *current_what = NULL;
1696 int any = 0;
1698 if (__glibc_unlikely (dirs == NULL))
1699 /* We're called before _dl_init_paths when loading the main executable
1700 given on the command line when rtld is run directly. */
1701 return -1;
1703 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1706 struct r_search_path_elem *this_dir = *dirs;
1707 size_t buflen = 0;
1708 size_t cnt;
1709 char *edp;
1710 int here_any = 0;
1711 int err;
1713 /* If we are debugging the search for libraries print the path
1714 now if it hasn't happened now. */
1715 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)
1716 && current_what != this_dir->what)
1718 current_what = this_dir->what;
1719 print_search_path (dirs, current_what, this_dir->where);
1722 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1723 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1725 /* Skip this directory if we know it does not exist. */
1726 if (this_dir->status[cnt] == nonexisting)
1727 continue;
1729 buflen =
1730 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1731 capstr[cnt].len),
1732 name, namelen)
1733 - buf);
1735 /* Print name we try if this is wanted. */
1736 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1737 _dl_debug_printf (" trying file=%s\n", buf);
1739 fd = open_verify (buf, -1, fbp, loader, whatcode, mode,
1740 found_other_class, false);
1741 if (this_dir->status[cnt] == unknown)
1743 if (fd != -1)
1744 this_dir->status[cnt] = existing;
1745 /* Do not update the directory information when loading
1746 auditing code. We must try to disturb the program as
1747 little as possible. */
1748 else if (loader == NULL
1749 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0)
1751 /* We failed to open machine dependent library. Let's
1752 test whether there is any directory at all. */
1753 struct stat64 st;
1755 buf[buflen - namelen - 1] = '\0';
1757 if (__xstat64 (_STAT_VER, buf, &st) != 0
1758 || ! S_ISDIR (st.st_mode))
1759 /* The directory does not exist or it is no directory. */
1760 this_dir->status[cnt] = nonexisting;
1761 else
1762 this_dir->status[cnt] = existing;
1766 /* Remember whether we found any existing directory. */
1767 here_any |= this_dir->status[cnt] != nonexisting;
1769 if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE)
1770 && __libc_enable_secure)
1772 /* This is an extra security effort to make sure nobody can
1773 preload broken shared objects which are in the trusted
1774 directories and so exploit the bugs. */
1775 struct stat64 st;
1777 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1778 || (st.st_mode & S_ISUID) == 0)
1780 /* The shared object cannot be tested for being SUID
1781 or this bit is not set. In this case we must not
1782 use this object. */
1783 __close (fd);
1784 fd = -1;
1785 /* We simply ignore the file, signal this by setting
1786 the error value which would have been set by `open'. */
1787 errno = ENOENT;
1792 if (fd != -1)
1794 *realname = (char *) malloc (buflen);
1795 if (*realname != NULL)
1797 memcpy (*realname, buf, buflen);
1798 return fd;
1800 else
1802 /* No memory for the name, we certainly won't be able
1803 to load and link it. */
1804 __close (fd);
1805 return -1;
1808 if (here_any && (err = errno) != ENOENT && err != EACCES)
1809 /* The file exists and is readable, but something went wrong. */
1810 return -1;
1812 /* Remember whether we found anything. */
1813 any |= here_any;
1815 while (*++dirs != NULL);
1817 /* Remove the whole path if none of the directories exists. */
1818 if (__glibc_unlikely (! any))
1820 /* Paths which were allocated using the minimal malloc() in ld.so
1821 must not be freed using the general free() in libc. */
1822 if (sps->malloced)
1823 free (sps->dirs);
1825 /* rtld_search_dirs and env_path_list are attribute_relro, therefore
1826 avoid writing into it. */
1827 if (sps != &rtld_search_dirs && sps != &env_path_list)
1828 sps->dirs = (void *) -1;
1831 return -1;
1834 /* Map in the shared object file NAME. */
1836 struct link_map *
1837 _dl_map_object (struct link_map *loader, const char *name,
1838 int type, int trace_mode, int mode, Lmid_t nsid)
1840 int fd;
1841 const char *origname = NULL;
1842 char *realname;
1843 char *name_copy;
1844 struct link_map *l;
1845 struct filebuf fb;
1847 assert (nsid >= 0);
1848 assert (nsid < GL(dl_nns));
1850 /* Look for this name among those already loaded. */
1851 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
1853 /* If the requested name matches the soname of a loaded object,
1854 use that object. Elide this check for names that have not
1855 yet been opened. */
1856 if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
1857 continue;
1858 if (!_dl_name_match_p (name, l))
1860 const char *soname;
1862 if (__glibc_likely (l->l_soname_added)
1863 || l->l_info[DT_SONAME] == NULL)
1864 continue;
1866 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1867 + l->l_info[DT_SONAME]->d_un.d_val);
1868 if (strcmp (name, soname) != 0)
1869 continue;
1871 /* We have a match on a new name -- cache it. */
1872 add_name_to_object (l, soname);
1873 l->l_soname_added = 1;
1876 /* We have a match. */
1877 return l;
1880 /* Display information if we are debugging. */
1881 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
1882 && loader != NULL)
1883 _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0
1884 ? "\nfile=%s [%lu]; needed by %s [%lu]\n"
1885 : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n",
1886 name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns);
1888 #ifdef SHARED
1889 /* Give the auditing libraries a chance to change the name before we
1890 try anything. */
1891 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1892 && (loader == NULL || loader->l_auditing == 0))
1894 struct audit_ifaces *afct = GLRO(dl_audit);
1895 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1897 if (afct->objsearch != NULL)
1899 const char *before = name;
1900 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1901 LA_SER_ORIG);
1902 if (name == NULL)
1904 /* Do not try anything further. */
1905 fd = -1;
1906 goto no_file;
1908 if (before != name && strcmp (before, name) != 0)
1910 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1911 _dl_debug_printf ("audit changed filename %s -> %s\n",
1912 before, name);
1914 if (origname == NULL)
1915 origname = before;
1919 afct = afct->next;
1922 #endif
1924 /* Will be true if we found a DSO which is of the other ELF class. */
1925 bool found_other_class = false;
1927 if (strchr (name, '/') == NULL)
1929 /* Search for NAME in several places. */
1931 size_t namelen = strlen (name) + 1;
1933 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1934 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
1936 fd = -1;
1938 /* When the object has the RUNPATH information we don't use any
1939 RPATHs. */
1940 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1942 /* This is the executable's map (if there is one). Make sure that
1943 we do not look at it twice. */
1944 struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1945 bool did_main_map = false;
1947 /* First try the DT_RPATH of the dependent object that caused NAME
1948 to be loaded. Then that object's dependent, and on up. */
1949 for (l = loader; l; l = l->l_loader)
1950 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
1952 fd = open_path (name, namelen, mode,
1953 &l->l_rpath_dirs,
1954 &realname, &fb, loader, LA_SER_RUNPATH,
1955 &found_other_class);
1956 if (fd != -1)
1957 break;
1959 did_main_map |= l == main_map;
1962 /* If dynamically linked, try the DT_RPATH of the executable
1963 itself. NB: we do this for lookups in any namespace. */
1964 if (fd == -1 && !did_main_map
1965 && main_map != NULL && main_map->l_type != lt_loaded
1966 && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH,
1967 "RPATH"))
1968 fd = open_path (name, namelen, mode,
1969 &main_map->l_rpath_dirs,
1970 &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
1971 &found_other_class);
1974 /* Try the LD_LIBRARY_PATH environment variable. */
1975 if (fd == -1 && env_path_list.dirs != (void *) -1)
1976 fd = open_path (name, namelen, mode, &env_path_list,
1977 &realname, &fb,
1978 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
1979 LA_SER_LIBPATH, &found_other_class);
1981 /* Look at the RUNPATH information for this binary. */
1982 if (fd == -1 && loader != NULL
1983 && cache_rpath (loader, &loader->l_runpath_dirs,
1984 DT_RUNPATH, "RUNPATH"))
1985 fd = open_path (name, namelen, mode,
1986 &loader->l_runpath_dirs, &realname, &fb, loader,
1987 LA_SER_RUNPATH, &found_other_class);
1989 if (fd == -1)
1991 realname = _dl_sysdep_open_object (name, namelen, &fd);
1992 if (realname != NULL)
1994 fd = open_verify (realname, fd,
1995 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
1996 LA_SER_CONFIG, mode, &found_other_class,
1997 false);
1998 if (fd == -1)
1999 free (realname);
2003 #ifdef USE_LDCONFIG
2004 if (fd == -1
2005 && (__glibc_likely ((mode & __RTLD_SECURE) == 0)
2006 || ! __libc_enable_secure)
2007 && __glibc_likely (GLRO(dl_inhibit_cache) == 0))
2009 /* Check the list of libraries in the file /etc/ld.so.cache,
2010 for compatibility with Linux's ldconfig program. */
2011 char *cached = _dl_load_cache_lookup (name);
2013 if (cached != NULL)
2015 // XXX Correct to unconditionally default to namespace 0?
2016 l = (loader
2017 ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded
2018 # ifdef SHARED
2019 ?: &GL(dl_rtld_map)
2020 # endif
2023 /* If the loader has the DF_1_NODEFLIB flag set we must not
2024 use a cache entry from any of these directories. */
2025 if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB))
2027 const char *dirp = system_dirs;
2028 unsigned int cnt = 0;
2032 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2034 /* The prefix matches. Don't use the entry. */
2035 free (cached);
2036 cached = NULL;
2037 break;
2040 dirp += system_dirs_len[cnt] + 1;
2041 ++cnt;
2043 while (cnt < nsystem_dirs_len);
2046 if (cached != NULL)
2048 fd = open_verify (cached, -1,
2049 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2050 LA_SER_CONFIG, mode, &found_other_class,
2051 false);
2052 if (__glibc_likely (fd != -1))
2053 realname = cached;
2054 else
2055 free (cached);
2059 #endif
2061 /* Finally, try the default path. */
2062 if (fd == -1
2063 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2064 || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB)))
2065 && rtld_search_dirs.dirs != (void *) -1)
2066 fd = open_path (name, namelen, mode, &rtld_search_dirs,
2067 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
2069 /* Add another newline when we are tracing the library loading. */
2070 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2071 _dl_debug_printf ("\n");
2073 else
2075 /* The path may contain dynamic string tokens. */
2076 realname = (loader
2077 ? expand_dynamic_string_token (loader, name)
2078 : __strdup (name));
2079 if (realname == NULL)
2080 fd = -1;
2081 else
2083 fd = open_verify (realname, -1, &fb,
2084 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
2085 &found_other_class, true);
2086 if (__glibc_unlikely (fd == -1))
2087 free (realname);
2091 #ifdef SHARED
2092 no_file:
2093 #endif
2094 /* In case the LOADER information has only been provided to get to
2095 the appropriate RUNPATH/RPATH information we do not need it
2096 anymore. */
2097 if (mode & __RTLD_CALLMAP)
2098 loader = NULL;
2100 if (__glibc_unlikely (fd == -1))
2102 if (trace_mode
2103 && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0))
2105 /* We haven't found an appropriate library. But since we
2106 are only interested in the list of libraries this isn't
2107 so severe. Fake an entry with all the information we
2108 have. */
2109 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2111 /* Allocate a new object map. */
2112 if ((name_copy = __strdup (name)) == NULL
2113 || (l = _dl_new_object (name_copy, name, type, loader,
2114 mode, nsid)) == NULL)
2116 free (name_copy);
2117 _dl_signal_error (ENOMEM, name, NULL,
2118 N_("cannot create shared object descriptor"));
2120 /* Signal that this is a faked entry. */
2121 l->l_faked = 1;
2122 /* Since the descriptor is initialized with zero we do not
2123 have do this here.
2124 l->l_reserved = 0; */
2125 l->l_buckets = &dummy_bucket;
2126 l->l_nbuckets = 1;
2127 l->l_relocated = 1;
2129 /* Enter the object in the object list. */
2130 _dl_add_to_namespace_list (l, nsid);
2132 return l;
2134 else if (found_other_class)
2135 _dl_signal_error (0, name, NULL,
2136 ELFW(CLASS) == ELFCLASS32
2137 ? N_("wrong ELF class: ELFCLASS64")
2138 : N_("wrong ELF class: ELFCLASS32"));
2139 else
2140 _dl_signal_error (errno, name, NULL,
2141 N_("cannot open shared object file"));
2144 void *stack_end = __libc_stack_end;
2145 return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader,
2146 type, mode, &stack_end, nsid);
2149 struct add_path_state
2151 bool counting;
2152 unsigned int idx;
2153 Dl_serinfo *si;
2154 char *allocptr;
2157 static void
2158 add_path (struct add_path_state *p, const struct r_search_path_struct *sps,
2159 unsigned int flags)
2161 if (sps->dirs != (void *) -1)
2163 struct r_search_path_elem **dirs = sps->dirs;
2166 const struct r_search_path_elem *const r = *dirs++;
2167 if (p->counting)
2169 p->si->dls_cnt++;
2170 p->si->dls_size += MAX (2, r->dirnamelen);
2172 else
2174 Dl_serpath *const sp = &p->si->dls_serpath[p->idx++];
2175 sp->dls_name = p->allocptr;
2176 if (r->dirnamelen < 2)
2177 *p->allocptr++ = r->dirnamelen ? '/' : '.';
2178 else
2179 p->allocptr = __mempcpy (p->allocptr,
2180 r->dirname, r->dirnamelen - 1);
2181 *p->allocptr++ = '\0';
2182 sp->dls_flags = flags;
2185 while (*dirs != NULL);
2189 void
2190 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2192 if (counting)
2194 si->dls_cnt = 0;
2195 si->dls_size = 0;
2198 struct add_path_state p =
2200 .counting = counting,
2201 .idx = 0,
2202 .si = si,
2203 .allocptr = (char *) &si->dls_serpath[si->dls_cnt]
2206 # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */
2208 /* When the object has the RUNPATH information we don't use any RPATHs. */
2209 if (loader->l_info[DT_RUNPATH] == NULL)
2211 /* First try the DT_RPATH of the dependent object that caused NAME
2212 to be loaded. Then that object's dependent, and on up. */
2214 struct link_map *l = loader;
2217 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2218 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2219 l = l->l_loader;
2221 while (l != NULL);
2223 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2224 if (loader->l_ns == LM_ID_BASE)
2226 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2227 if (l != NULL && l->l_type != lt_loaded && l != loader)
2228 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2229 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2233 /* Try the LD_LIBRARY_PATH environment variable. */
2234 add_path (&p, &env_path_list, XXX_ENV);
2236 /* Look at the RUNPATH information for this binary. */
2237 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2238 add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH);
2240 /* XXX
2241 Here is where ld.so.cache gets checked, but we don't have
2242 a way to indicate that in the results for Dl_serinfo. */
2244 /* Finally, try the default path. */
2245 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2246 add_path (&p, &rtld_search_dirs, XXX_default);
2248 if (counting)
2249 /* Count the struct size before the string area, which we didn't
2250 know before we completed dls_cnt. */
2251 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;