hurd: Fix getting ssize_t for <sys/socket.h>
[glibc.git] / elf / dl-load.c
bloba067760cc64ec79af2dd6040b9e8508004e1a63f
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2017 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-pointer-arith.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 add_name_to_object (struct link_map *l, const char *name)
397 struct libname_list *lnp, *lastp;
398 struct libname_list *newname;
399 size_t name_len;
401 lastp = NULL;
402 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
403 if (strcmp (name, lnp->name) == 0)
404 return;
406 name_len = strlen (name) + 1;
407 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
408 if (newname == NULL)
410 /* No more memory. */
411 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
412 return;
414 /* The object should have a libname set from _dl_new_object. */
415 assert (lastp != NULL);
417 newname->name = memcpy (newname + 1, name, name_len);
418 newname->next = NULL;
419 newname->dont_free = 0;
420 lastp->next = newname;
423 /* Standard search directories. */
424 static struct r_search_path_struct rtld_search_dirs attribute_relro;
426 static size_t max_dirnamelen;
428 static struct r_search_path_elem **
429 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
430 int check_trusted, const char *what, const char *where,
431 struct link_map *l)
433 char *cp;
434 size_t nelems = 0;
435 char *to_free;
437 while ((cp = __strsep (&rpath, sep)) != NULL)
439 struct r_search_path_elem *dirp;
441 to_free = cp = expand_dynamic_string_token (l, cp, 1);
443 size_t len = strlen (cp);
445 /* `strsep' can pass an empty string. This has to be
446 interpreted as `use the current directory'. */
447 if (len == 0)
449 static const char curwd[] = "./";
450 cp = (char *) curwd;
453 /* Remove trailing slashes (except for "/"). */
454 while (len > 1 && cp[len - 1] == '/')
455 --len;
457 /* Now add one if there is none so far. */
458 if (len > 0 && cp[len - 1] != '/')
459 cp[len++] = '/';
461 /* Make sure we don't use untrusted directories if we run SUID. */
462 if (__glibc_unlikely (check_trusted) && !is_trusted_path (cp, len))
464 free (to_free);
465 continue;
468 /* See if this directory is already known. */
469 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
470 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
471 break;
473 if (dirp != NULL)
475 /* It is available, see whether it's on our own list. */
476 size_t cnt;
477 for (cnt = 0; cnt < nelems; ++cnt)
478 if (result[cnt] == dirp)
479 break;
481 if (cnt == nelems)
482 result[nelems++] = dirp;
484 else
486 size_t cnt;
487 enum r_dir_status init_val;
488 size_t where_len = where ? strlen (where) + 1 : 0;
490 /* It's a new directory. Create an entry and add it. */
491 dirp = (struct r_search_path_elem *)
492 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
493 + where_len + len + 1);
494 if (dirp == NULL)
495 _dl_signal_error (ENOMEM, NULL, NULL,
496 N_("cannot create cache for search path"));
498 dirp->dirname = ((char *) dirp + sizeof (*dirp)
499 + ncapstr * sizeof (enum r_dir_status));
500 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
501 dirp->dirnamelen = len;
503 if (len > max_dirnamelen)
504 max_dirnamelen = len;
506 /* We have to make sure all the relative directories are
507 never ignored. The current directory might change and
508 all our saved information would be void. */
509 init_val = cp[0] != '/' ? existing : unknown;
510 for (cnt = 0; cnt < ncapstr; ++cnt)
511 dirp->status[cnt] = init_val;
513 dirp->what = what;
514 if (__glibc_likely (where != NULL))
515 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
516 + (ncapstr * sizeof (enum r_dir_status)),
517 where, where_len);
518 else
519 dirp->where = NULL;
521 dirp->next = GL(dl_all_dirs);
522 GL(dl_all_dirs) = dirp;
524 /* Put it in the result array. */
525 result[nelems++] = dirp;
527 free (to_free);
530 /* Terminate the array. */
531 result[nelems] = NULL;
533 return result;
537 static bool
538 decompose_rpath (struct r_search_path_struct *sps,
539 const char *rpath, struct link_map *l, const char *what)
541 /* Make a copy we can work with. */
542 const char *where = l->l_name;
543 char *copy;
544 char *cp;
545 struct r_search_path_elem **result;
546 size_t nelems;
547 /* Initialize to please the compiler. */
548 const char *errstring = NULL;
550 /* First see whether we must forget the RUNPATH and RPATH from this
551 object. */
552 if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL)
553 && !__libc_enable_secure)
555 const char *inhp = GLRO(dl_inhibit_rpath);
559 const char *wp = where;
561 while (*inhp == *wp && *wp != '\0')
563 ++inhp;
564 ++wp;
567 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
569 /* This object is on the list of objects for which the
570 RUNPATH and RPATH must not be used. */
571 sps->dirs = (void *) -1;
572 return false;
575 while (*inhp != '\0')
576 if (*inhp++ == ':')
577 break;
579 while (*inhp != '\0');
582 /* Make a writable copy. */
583 copy = __strdup (rpath);
584 if (copy == NULL)
586 errstring = N_("cannot create RUNPATH/RPATH copy");
587 goto signal_error;
590 /* Ignore empty rpaths. */
591 if (*copy == 0)
593 free (copy);
594 sps->dirs = (struct r_search_path_elem **) -1;
595 return false;
598 /* Count the number of necessary elements in the result array. */
599 nelems = 0;
600 for (cp = copy; *cp != '\0'; ++cp)
601 if (*cp == ':')
602 ++nelems;
604 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
605 number of necessary entries. */
606 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
607 * sizeof (*result));
608 if (result == NULL)
610 free (copy);
611 errstring = N_("cannot create cache for search path");
612 signal_error:
613 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
616 fillin_rpath (copy, result, ":", 0, what, where, l);
618 /* Free the copied RPATH string. `fillin_rpath' make own copies if
619 necessary. */
620 free (copy);
622 sps->dirs = result;
623 /* The caller will change this value if we haven't used a real malloc. */
624 sps->malloced = 1;
625 return true;
628 /* Make sure cached path information is stored in *SP
629 and return true if there are any paths to search there. */
630 static bool
631 cache_rpath (struct link_map *l,
632 struct r_search_path_struct *sp,
633 int tag,
634 const char *what)
636 if (sp->dirs == (void *) -1)
637 return false;
639 if (sp->dirs != NULL)
640 return true;
642 if (l->l_info[tag] == NULL)
644 /* There is no path. */
645 sp->dirs = (void *) -1;
646 return false;
649 /* Make sure the cache information is available. */
650 return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
651 + l->l_info[tag]->d_un.d_val),
652 l, what);
656 void
657 _dl_init_paths (const char *llp)
659 size_t idx;
660 const char *strp;
661 struct r_search_path_elem *pelem, **aelem;
662 size_t round_size;
663 struct link_map __attribute__ ((unused)) *l = NULL;
664 /* Initialize to please the compiler. */
665 const char *errstring = NULL;
667 /* Fill in the information about the application's RPATH and the
668 directories addressed by the LD_LIBRARY_PATH environment variable. */
670 /* Get the capabilities. */
671 capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen),
672 &ncapstr, &max_capstrlen);
674 /* First set up the rest of the default search directory entries. */
675 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
676 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
677 if (rtld_search_dirs.dirs == NULL)
679 errstring = N_("cannot create search path array");
680 signal_error:
681 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
684 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
685 + ncapstr * sizeof (enum r_dir_status))
686 / sizeof (struct r_search_path_elem));
688 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
689 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
690 * round_size * sizeof (struct r_search_path_elem));
691 if (rtld_search_dirs.dirs[0] == NULL)
693 errstring = N_("cannot create cache for search path");
694 goto signal_error;
697 rtld_search_dirs.malloced = 0;
698 pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0];
699 strp = system_dirs;
700 idx = 0;
704 size_t cnt;
706 *aelem++ = pelem;
708 pelem->what = "system search path";
709 pelem->where = NULL;
711 pelem->dirname = strp;
712 pelem->dirnamelen = system_dirs_len[idx];
713 strp += system_dirs_len[idx] + 1;
715 /* System paths must be absolute. */
716 assert (pelem->dirname[0] == '/');
717 for (cnt = 0; cnt < ncapstr; ++cnt)
718 pelem->status[cnt] = unknown;
720 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
722 pelem += round_size;
724 while (idx < nsystem_dirs_len);
726 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
727 *aelem = NULL;
729 #ifdef SHARED
730 /* This points to the map of the main object. */
731 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
732 if (l != NULL)
734 assert (l->l_type != lt_loaded);
736 if (l->l_info[DT_RUNPATH])
738 /* Allocate room for the search path and fill in information
739 from RUNPATH. */
740 decompose_rpath (&l->l_runpath_dirs,
741 (const void *) (D_PTR (l, l_info[DT_STRTAB])
742 + l->l_info[DT_RUNPATH]->d_un.d_val),
743 l, "RUNPATH");
744 /* During rtld init the memory is allocated by the stub malloc,
745 prevent any attempt to free it by the normal malloc. */
746 l->l_runpath_dirs.malloced = 0;
748 /* The RPATH is ignored. */
749 l->l_rpath_dirs.dirs = (void *) -1;
751 else
753 l->l_runpath_dirs.dirs = (void *) -1;
755 if (l->l_info[DT_RPATH])
757 /* Allocate room for the search path and fill in information
758 from RPATH. */
759 decompose_rpath (&l->l_rpath_dirs,
760 (const void *) (D_PTR (l, l_info[DT_STRTAB])
761 + l->l_info[DT_RPATH]->d_un.d_val),
762 l, "RPATH");
763 /* During rtld init the memory is allocated by the stub
764 malloc, prevent any attempt to free it by the normal
765 malloc. */
766 l->l_rpath_dirs.malloced = 0;
768 else
769 l->l_rpath_dirs.dirs = (void *) -1;
772 #endif /* SHARED */
774 if (llp != NULL && *llp != '\0')
776 size_t nllp;
777 const char *cp = llp;
778 char *llp_tmp;
780 #ifdef SHARED
781 /* Expand DSTs. */
782 size_t cnt = DL_DST_COUNT (llp, 1);
783 if (__glibc_likely (cnt == 0))
784 llp_tmp = strdupa (llp);
785 else
787 /* Determine the length of the substituted string. */
788 size_t total = DL_DST_REQUIRED (l, llp, strlen (llp), cnt);
790 /* Allocate the necessary memory. */
791 llp_tmp = (char *) alloca (total + 1);
792 llp_tmp = _dl_dst_substitute (l, llp, llp_tmp, 1);
794 #else
795 llp_tmp = strdupa (llp);
796 #endif
798 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
799 elements it has. */
800 nllp = 1;
801 while (*cp)
803 if (*cp == ':' || *cp == ';')
804 ++nllp;
805 ++cp;
808 env_path_list.dirs = (struct r_search_path_elem **)
809 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
810 if (env_path_list.dirs == NULL)
812 errstring = N_("cannot create cache for search path");
813 goto signal_error;
816 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
817 __libc_enable_secure, "LD_LIBRARY_PATH",
818 NULL, l);
820 if (env_path_list.dirs[0] == NULL)
822 free (env_path_list.dirs);
823 env_path_list.dirs = (void *) -1;
826 env_path_list.malloced = 0;
828 else
829 env_path_list.dirs = (void *) -1;
833 static void
834 __attribute__ ((noreturn, noinline))
835 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
836 const char *msg, struct r_debug *r, Lmid_t nsid)
838 /* The file might already be closed. */
839 if (fd != -1)
840 (void) __close (fd);
841 if (l != NULL && l->l_origin != (char *) -1l)
842 free ((char *) l->l_origin);
843 free (l);
844 free (realname);
846 if (r != NULL)
848 r->r_state = RT_CONSISTENT;
849 _dl_debug_state ();
850 LIBC_PROBE (map_failed, 2, nsid, r);
853 _dl_signal_error (code, name, NULL, msg);
857 /* Map in the shared object NAME, actually located in REALNAME, and already
858 opened on FD. */
860 #ifndef EXTERNAL_MAP_FROM_FD
861 static
862 #endif
863 struct link_map *
864 _dl_map_object_from_fd (const char *name, const char *origname, int fd,
865 struct filebuf *fbp, char *realname,
866 struct link_map *loader, int l_type, int mode,
867 void **stack_endp, Lmid_t nsid)
869 struct link_map *l = NULL;
870 const ElfW(Ehdr) *header;
871 const ElfW(Phdr) *phdr;
872 const ElfW(Phdr) *ph;
873 size_t maplength;
874 int type;
875 /* Initialize to keep the compiler happy. */
876 const char *errstring = NULL;
877 int errval = 0;
878 struct r_debug *r = _dl_debug_initialize (0, nsid);
879 bool make_consistent = false;
881 /* Get file information. */
882 struct r_file_id id;
883 if (__glibc_unlikely (!_dl_get_file_id (fd, &id)))
885 errstring = N_("cannot stat shared object");
886 call_lose_errno:
887 errval = errno;
888 call_lose:
889 lose (errval, fd, name, realname, l, errstring,
890 make_consistent ? r : NULL, nsid);
893 /* Look again to see if the real name matched another already loaded. */
894 for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
895 if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id))
897 /* The object is already loaded.
898 Just bump its reference count and return it. */
899 __close (fd);
901 /* If the name is not in the list of names for this object add
902 it. */
903 free (realname);
904 add_name_to_object (l, name);
906 return l;
909 #ifdef SHARED
910 /* When loading into a namespace other than the base one we must
911 avoid loading ld.so since there can only be one copy. Ever. */
912 if (__glibc_unlikely (nsid != LM_ID_BASE)
913 && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id)
914 || _dl_name_match_p (name, &GL(dl_rtld_map))))
916 /* This is indeed ld.so. Create a new link_map which refers to
917 the real one for almost everything. */
918 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
919 if (l == NULL)
920 goto fail_new;
922 /* Refer to the real descriptor. */
923 l->l_real = &GL(dl_rtld_map);
925 /* No need to bump the refcount of the real object, ld.so will
926 never be unloaded. */
927 __close (fd);
929 /* Add the map for the mirrored object to the object list. */
930 _dl_add_to_namespace_list (l, nsid);
932 return l;
934 #endif
936 if (mode & RTLD_NOLOAD)
938 /* We are not supposed to load the object unless it is already
939 loaded. So return now. */
940 free (realname);
941 __close (fd);
942 return NULL;
945 /* Print debugging message. */
946 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
947 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
949 /* This is the ELF header. We read it in `open_verify'. */
950 header = (void *) fbp->buf;
952 #ifndef MAP_ANON
953 # define MAP_ANON 0
954 if (_dl_zerofd == -1)
956 _dl_zerofd = _dl_sysdep_open_zero_fill ();
957 if (_dl_zerofd == -1)
959 free (realname);
960 __close (fd);
961 _dl_signal_error (errno, NULL, NULL,
962 N_("cannot open zero fill device"));
965 #endif
967 /* Signal that we are going to add new objects. */
968 if (r->r_state == RT_CONSISTENT)
970 #ifdef SHARED
971 /* Auditing checkpoint: we are going to add new objects. */
972 if ((mode & __RTLD_AUDIT) == 0
973 && __glibc_unlikely (GLRO(dl_naudit) > 0))
975 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
976 /* Do not call the functions for any auditing object. */
977 if (head->l_auditing == 0)
979 struct audit_ifaces *afct = GLRO(dl_audit);
980 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
982 if (afct->activity != NULL)
983 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD);
985 afct = afct->next;
989 #endif
991 /* Notify the debugger we have added some objects. We need to
992 call _dl_debug_initialize in a static program in case dynamic
993 linking has not been used before. */
994 r->r_state = RT_ADD;
995 _dl_debug_state ();
996 LIBC_PROBE (map_start, 2, nsid, r);
997 make_consistent = true;
999 else
1000 assert (r->r_state == RT_ADD);
1002 /* Enter the new object in the list of loaded objects. */
1003 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
1004 if (__glibc_unlikely (l == NULL))
1006 #ifdef SHARED
1007 fail_new:
1008 #endif
1009 errstring = N_("cannot create shared object descriptor");
1010 goto call_lose_errno;
1013 /* Extract the remaining details we need from the ELF header
1014 and then read in the program header table. */
1015 l->l_entry = header->e_entry;
1016 type = header->e_type;
1017 l->l_phnum = header->e_phnum;
1019 maplength = header->e_phnum * sizeof (ElfW(Phdr));
1020 if (header->e_phoff + maplength <= (size_t) fbp->len)
1021 phdr = (void *) (fbp->buf + header->e_phoff);
1022 else
1024 phdr = alloca (maplength);
1025 __lseek (fd, header->e_phoff, SEEK_SET);
1026 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1028 errstring = N_("cannot read file data");
1029 goto call_lose_errno;
1033 /* On most platforms presume that PT_GNU_STACK is absent and the stack is
1034 * executable. Other platforms default to a nonexecutable stack and don't
1035 * need PT_GNU_STACK to do so. */
1036 uint_fast16_t stack_flags = DEFAULT_STACK_PERMS;
1039 /* Scan the program header table, collecting its load commands. */
1040 struct loadcmd loadcmds[l->l_phnum];
1041 size_t nloadcmds = 0;
1042 bool has_holes = false;
1044 /* The struct is initialized to zero so this is not necessary:
1045 l->l_ld = 0;
1046 l->l_phdr = 0;
1047 l->l_addr = 0; */
1048 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
1049 switch (ph->p_type)
1051 /* These entries tell us where to find things once the file's
1052 segments are mapped in. We record the addresses it says
1053 verbatim, and later correct for the run-time load address. */
1054 case PT_DYNAMIC:
1055 l->l_ld = (void *) ph->p_vaddr;
1056 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1057 break;
1059 case PT_PHDR:
1060 l->l_phdr = (void *) ph->p_vaddr;
1061 break;
1063 case PT_LOAD:
1064 /* A load command tells us to map in part of the file.
1065 We record the load commands and process them all later. */
1066 if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0))
1068 errstring = N_("ELF load command alignment not page-aligned");
1069 goto call_lose;
1071 if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
1072 & (ph->p_align - 1)) != 0))
1074 errstring
1075 = N_("ELF load command address/offset not properly aligned");
1076 goto call_lose;
1079 struct loadcmd *c = &loadcmds[nloadcmds++];
1080 c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize));
1081 c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
1082 c->dataend = ph->p_vaddr + ph->p_filesz;
1083 c->allocend = ph->p_vaddr + ph->p_memsz;
1084 c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
1086 /* Determine whether there is a gap between the last segment
1087 and this one. */
1088 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1089 has_holes = true;
1091 /* Optimize a common case. */
1092 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1093 c->prot = (PF_TO_PROT
1094 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1095 #else
1096 c->prot = 0;
1097 if (ph->p_flags & PF_R)
1098 c->prot |= PROT_READ;
1099 if (ph->p_flags & PF_W)
1100 c->prot |= PROT_WRITE;
1101 if (ph->p_flags & PF_X)
1102 c->prot |= PROT_EXEC;
1103 #endif
1104 break;
1106 case PT_TLS:
1107 if (ph->p_memsz == 0)
1108 /* Nothing to do for an empty segment. */
1109 break;
1111 l->l_tls_blocksize = ph->p_memsz;
1112 l->l_tls_align = ph->p_align;
1113 if (ph->p_align == 0)
1114 l->l_tls_firstbyte_offset = 0;
1115 else
1116 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1117 l->l_tls_initimage_size = ph->p_filesz;
1118 /* Since we don't know the load address yet only store the
1119 offset. We will adjust it later. */
1120 l->l_tls_initimage = (void *) ph->p_vaddr;
1122 /* If not loading the initial set of shared libraries,
1123 check whether we should permit loading a TLS segment. */
1124 if (__glibc_likely (l->l_type == lt_library)
1125 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1126 not set up TLS data structures, so don't use them now. */
1127 || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL))
1129 /* Assign the next available module ID. */
1130 l->l_tls_modid = _dl_next_tls_modid ();
1131 break;
1134 #ifdef SHARED
1135 /* We are loading the executable itself when the dynamic
1136 linker was executed directly. The setup will happen
1137 later. Otherwise, the TLS data structures are already
1138 initialized, and we assigned a TLS modid above. */
1139 assert (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0);
1140 #else
1141 assert (false && "TLS not initialized in static application");
1142 #endif
1143 break;
1145 case PT_GNU_STACK:
1146 stack_flags = ph->p_flags;
1147 break;
1149 case PT_GNU_RELRO:
1150 l->l_relro_addr = ph->p_vaddr;
1151 l->l_relro_size = ph->p_memsz;
1152 break;
1155 if (__glibc_unlikely (nloadcmds == 0))
1157 /* This only happens for a bogus object that will be caught with
1158 another error below. But we don't want to go through the
1159 calculations below using NLOADCMDS - 1. */
1160 errstring = N_("object file has no loadable segments");
1161 goto call_lose;
1164 if (__glibc_unlikely (type != ET_DYN)
1165 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))
1167 /* This object is loaded at a fixed address. This must never
1168 happen for objects loaded with dlopen. */
1169 errstring = N_("cannot dynamically load executable");
1170 goto call_lose;
1173 /* Length of the sections to be loaded. */
1174 maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
1176 /* Now process the load commands and map segments into memory.
1177 This is responsible for filling in:
1178 l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr
1180 errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
1181 maplength, has_holes, loader);
1182 if (__glibc_unlikely (errstring != NULL))
1183 goto call_lose;
1186 if (l->l_ld == 0)
1188 if (__glibc_unlikely (type == ET_DYN))
1190 errstring = N_("object file has no dynamic section");
1191 goto call_lose;
1194 else
1195 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1197 elf_get_dynamic_info (l, NULL);
1199 /* Make sure we are not dlopen'ing an object that has the
1200 DF_1_NOOPEN flag set. */
1201 if (__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
1202 && (mode & __RTLD_DLOPEN))
1204 /* We are not supposed to load this object. Free all resources. */
1205 _dl_unmap_segments (l);
1207 if (!l->l_libname->dont_free)
1208 free (l->l_libname);
1210 if (l->l_phdr_allocated)
1211 free ((void *) l->l_phdr);
1213 errstring = N_("shared object cannot be dlopen()ed");
1214 goto call_lose;
1217 if (l->l_phdr == NULL)
1219 /* The program header is not contained in any of the segments.
1220 We have to allocate memory ourself and copy it over from out
1221 temporary place. */
1222 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1223 * sizeof (ElfW(Phdr)));
1224 if (newp == NULL)
1226 errstring = N_("cannot allocate memory for program header");
1227 goto call_lose_errno;
1230 l->l_phdr = memcpy (newp, phdr,
1231 (header->e_phnum * sizeof (ElfW(Phdr))));
1232 l->l_phdr_allocated = 1;
1234 else
1235 /* Adjust the PT_PHDR value by the runtime load address. */
1236 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1238 if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X))
1240 if (__glibc_unlikely (__check_caller (RETURN_ADDRESS (0), allow_ldso) != 0))
1242 errstring = N_("invalid caller");
1243 goto call_lose;
1246 /* The stack is presently not executable, but this module
1247 requires that it be executable. We must change the
1248 protection of the variable which contains the flags used in
1249 the mprotect calls. */
1250 #ifdef SHARED
1251 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1253 const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
1254 const size_t s = (uintptr_t) (&__stack_prot + 1) - p;
1256 struct link_map *const m = &GL(dl_rtld_map);
1257 const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr
1258 + m->l_relro_size)
1259 & -GLRO(dl_pagesize));
1260 if (__glibc_likely (p + s <= relro_end))
1262 /* The variable lies in the region protected by RELRO. */
1263 if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0)
1265 errstring = N_("cannot change memory protections");
1266 goto call_lose_errno;
1268 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1269 __mprotect ((void *) p, s, PROT_READ);
1271 else
1272 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1274 else
1275 #endif
1276 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1278 #ifdef check_consistency
1279 check_consistency ();
1280 #endif
1282 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1283 if (errval)
1285 errstring = N_("\
1286 cannot enable executable stack as shared object requires");
1287 goto call_lose;
1291 /* Adjust the address of the TLS initialization image. */
1292 if (l->l_tls_initimage != NULL)
1293 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1295 /* We are done mapping in the file. We no longer need the descriptor. */
1296 if (__glibc_unlikely (__close (fd) != 0))
1298 errstring = N_("cannot close file descriptor");
1299 goto call_lose_errno;
1301 /* Signal that we closed the file. */
1302 fd = -1;
1304 /* If this is ET_EXEC, we should have loaded it as lt_executable. */
1305 assert (type != ET_EXEC || l->l_type == lt_executable);
1307 l->l_entry += l->l_addr;
1309 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1310 _dl_debug_printf ("\
1311 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1312 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1313 (int) sizeof (void *) * 2,
1314 (unsigned long int) l->l_ld,
1315 (int) sizeof (void *) * 2,
1316 (unsigned long int) l->l_addr,
1317 (int) sizeof (void *) * 2, maplength,
1318 (int) sizeof (void *) * 2,
1319 (unsigned long int) l->l_entry,
1320 (int) sizeof (void *) * 2,
1321 (unsigned long int) l->l_phdr,
1322 (int) sizeof (void *) * 2, l->l_phnum);
1324 /* Set up the symbol hash table. */
1325 _dl_setup_hash (l);
1327 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1328 have to do this for the main map. */
1329 if ((mode & RTLD_DEEPBIND) == 0
1330 && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL)
1331 && &l->l_searchlist != l->l_scope[0])
1333 /* Create an appropriate searchlist. It contains only this map.
1334 This is the definition of DT_SYMBOLIC in SysVr4. */
1335 l->l_symbolic_searchlist.r_list[0] = l;
1336 l->l_symbolic_searchlist.r_nlist = 1;
1338 /* Now move the existing entries one back. */
1339 memmove (&l->l_scope[1], &l->l_scope[0],
1340 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1342 /* Now add the new entry. */
1343 l->l_scope[0] = &l->l_symbolic_searchlist;
1346 /* Remember whether this object must be initialized first. */
1347 if (l->l_flags_1 & DF_1_INITFIRST)
1348 GL(dl_initfirst) = l;
1350 /* Finally the file information. */
1351 l->l_file_id = id;
1353 #ifdef SHARED
1354 /* When auditing is used the recorded names might not include the
1355 name by which the DSO is actually known. Add that as well. */
1356 if (__glibc_unlikely (origname != NULL))
1357 add_name_to_object (l, origname);
1358 #else
1359 /* Audit modules only exist when linking is dynamic so ORIGNAME
1360 cannot be non-NULL. */
1361 assert (origname == NULL);
1362 #endif
1364 /* When we profile the SONAME might be needed for something else but
1365 loading. Add it right away. */
1366 if (__glibc_unlikely (GLRO(dl_profile) != NULL)
1367 && l->l_info[DT_SONAME] != NULL)
1368 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1369 + l->l_info[DT_SONAME]->d_un.d_val));
1371 #ifdef DL_AFTER_LOAD
1372 DL_AFTER_LOAD (l);
1373 #endif
1375 /* Now that the object is fully initialized add it to the object list. */
1376 _dl_add_to_namespace_list (l, nsid);
1378 #ifdef SHARED
1379 /* Auditing checkpoint: we have a new object. */
1380 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1381 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1383 struct audit_ifaces *afct = GLRO(dl_audit);
1384 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1386 if (afct->objopen != NULL)
1388 l->l_audit[cnt].bindflags
1389 = afct->objopen (l, nsid, &l->l_audit[cnt].cookie);
1391 l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0;
1394 afct = afct->next;
1397 #endif
1399 return l;
1402 /* Print search path. */
1403 static void
1404 print_search_path (struct r_search_path_elem **list,
1405 const char *what, const char *name)
1407 char buf[max_dirnamelen + max_capstrlen];
1408 int first = 1;
1410 _dl_debug_printf (" search path=");
1412 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1414 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1415 size_t cnt;
1417 for (cnt = 0; cnt < ncapstr; ++cnt)
1418 if ((*list)->status[cnt] != nonexisting)
1420 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1421 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1422 cp[0] = '\0';
1423 else
1424 cp[-1] = '\0';
1426 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1427 first = 0;
1430 ++list;
1433 if (name != NULL)
1434 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1435 DSO_FILENAME (name));
1436 else
1437 _dl_debug_printf_c ("\t\t(%s)\n", what);
1440 /* Open a file and verify it is an ELF file for this architecture. We
1441 ignore only ELF files for other architectures. Non-ELF files and
1442 ELF files with different header information cause fatal errors since
1443 this could mean there is something wrong in the installation and the
1444 user might want to know about this.
1446 If FD is not -1, then the file is already open and FD refers to it.
1447 In that case, FD is consumed for both successful and error returns. */
1448 static int
1449 open_verify (const char *name, int fd,
1450 struct filebuf *fbp, struct link_map *loader,
1451 int whatcode, int mode, bool *found_other_class, bool free_name)
1453 /* This is the expected ELF header. */
1454 #define ELF32_CLASS ELFCLASS32
1455 #define ELF64_CLASS ELFCLASS64
1456 #ifndef VALID_ELF_HEADER
1457 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1458 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1459 # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0)
1460 #elif defined MORE_ELF_HEADER_DATA
1461 MORE_ELF_HEADER_DATA;
1462 #endif
1463 static const unsigned char expected[EI_NIDENT] =
1465 [EI_MAG0] = ELFMAG0,
1466 [EI_MAG1] = ELFMAG1,
1467 [EI_MAG2] = ELFMAG2,
1468 [EI_MAG3] = ELFMAG3,
1469 [EI_CLASS] = ELFW(CLASS),
1470 [EI_DATA] = byteorder,
1471 [EI_VERSION] = EV_CURRENT,
1472 [EI_OSABI] = ELFOSABI_SYSV,
1473 [EI_ABIVERSION] = 0
1475 static const struct
1477 ElfW(Word) vendorlen;
1478 ElfW(Word) datalen;
1479 ElfW(Word) type;
1480 char vendor[4];
1481 } expected_note = { 4, 16, 1, "GNU" };
1482 /* Initialize it to make the compiler happy. */
1483 const char *errstring = NULL;
1484 int errval = 0;
1486 #ifdef SHARED
1487 /* Give the auditing libraries a chance. */
1488 if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0
1489 && loader->l_auditing == 0)
1491 const char *original_name = name;
1492 struct audit_ifaces *afct = GLRO(dl_audit);
1493 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1495 if (afct->objsearch != NULL)
1497 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1498 whatcode);
1499 if (name == NULL)
1500 /* Ignore the path. */
1501 return -1;
1504 afct = afct->next;
1507 if (fd != -1 && name != original_name && strcmp (name, original_name))
1509 /* An audit library changed what we're supposed to open,
1510 so FD no longer matches it. */
1511 __close (fd);
1512 fd = -1;
1515 #endif
1517 if (fd == -1)
1518 /* Open the file. We always open files read-only. */
1519 fd = __open (name, O_RDONLY | O_CLOEXEC);
1521 if (fd != -1)
1523 ElfW(Ehdr) *ehdr;
1524 ElfW(Phdr) *phdr, *ph;
1525 ElfW(Word) *abi_note;
1526 unsigned int osversion;
1527 size_t maplength;
1529 /* We successfully opened the file. Now verify it is a file
1530 we can use. */
1531 __set_errno (0);
1532 fbp->len = 0;
1533 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1534 /* Read in the header. */
1537 ssize_t retlen = __libc_read (fd, fbp->buf + fbp->len,
1538 sizeof (fbp->buf) - fbp->len);
1539 if (retlen <= 0)
1540 break;
1541 fbp->len += retlen;
1543 while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr))));
1545 /* This is where the ELF header is loaded. */
1546 ehdr = (ElfW(Ehdr) *) fbp->buf;
1548 /* Now run the tests. */
1549 if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr))))
1551 errval = errno;
1552 errstring = (errval == 0
1553 ? N_("file too short") : N_("cannot read file data"));
1554 call_lose:
1555 if (free_name)
1557 char *realname = (char *) name;
1558 name = strdupa (realname);
1559 free (realname);
1561 lose (errval, fd, name, NULL, NULL, errstring, NULL, 0);
1564 /* See whether the ELF header is what we expect. */
1565 if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1566 EI_ABIVERSION)
1567 || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1568 ehdr->e_ident[EI_ABIVERSION])
1569 || memcmp (&ehdr->e_ident[EI_PAD],
1570 &expected[EI_PAD],
1571 EI_NIDENT - EI_PAD) != 0))
1573 /* Something is wrong. */
1574 const Elf32_Word *magp = (const void *) ehdr->e_ident;
1575 if (*magp !=
1576 #if BYTE_ORDER == LITTLE_ENDIAN
1577 ((ELFMAG0 << (EI_MAG0 * 8)) |
1578 (ELFMAG1 << (EI_MAG1 * 8)) |
1579 (ELFMAG2 << (EI_MAG2 * 8)) |
1580 (ELFMAG3 << (EI_MAG3 * 8)))
1581 #else
1582 ((ELFMAG0 << (EI_MAG3 * 8)) |
1583 (ELFMAG1 << (EI_MAG2 * 8)) |
1584 (ELFMAG2 << (EI_MAG1 * 8)) |
1585 (ELFMAG3 << (EI_MAG0 * 8)))
1586 #endif
1588 errstring = N_("invalid ELF header");
1589 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1591 /* This is not a fatal error. On architectures where
1592 32-bit and 64-bit binaries can be run this might
1593 happen. */
1594 *found_other_class = true;
1595 goto close_and_out;
1597 else if (ehdr->e_ident[EI_DATA] != byteorder)
1599 if (BYTE_ORDER == BIG_ENDIAN)
1600 errstring = N_("ELF file data encoding not big-endian");
1601 else
1602 errstring = N_("ELF file data encoding not little-endian");
1604 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1605 errstring
1606 = N_("ELF file version ident does not match current one");
1607 /* XXX We should be able so set system specific versions which are
1608 allowed here. */
1609 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1610 errstring = N_("ELF file OS ABI invalid");
1611 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1612 ehdr->e_ident[EI_ABIVERSION]))
1613 errstring = N_("ELF file ABI version invalid");
1614 else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD],
1615 EI_NIDENT - EI_PAD) != 0)
1616 errstring = N_("nonzero padding in e_ident");
1617 else
1618 /* Otherwise we don't know what went wrong. */
1619 errstring = N_("internal error");
1621 goto call_lose;
1624 if (__glibc_unlikely (ehdr->e_version != EV_CURRENT))
1626 errstring = N_("ELF file version does not match current one");
1627 goto call_lose;
1629 if (! __glibc_likely (elf_machine_matches_host (ehdr)))
1630 goto close_and_out;
1631 else if (__glibc_unlikely (ehdr->e_type != ET_DYN
1632 && ehdr->e_type != ET_EXEC))
1634 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1635 goto call_lose;
1637 else if (__glibc_unlikely (ehdr->e_type == ET_EXEC
1638 && (mode & __RTLD_OPENEXEC) == 0))
1640 /* BZ #16634. It is an error to dlopen ET_EXEC (unless
1641 __RTLD_OPENEXEC is explicitly set). We return error here
1642 so that code in _dl_map_object_from_fd does not try to set
1643 l_tls_modid for this module. */
1645 errstring = N_("cannot dynamically load executable");
1646 goto call_lose;
1648 else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr))))
1650 errstring = N_("ELF file's phentsize not the expected size");
1651 goto call_lose;
1654 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1655 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1656 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1657 else
1659 phdr = alloca (maplength);
1660 __lseek (fd, ehdr->e_phoff, SEEK_SET);
1661 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1663 read_error:
1664 errval = errno;
1665 errstring = N_("cannot read file data");
1666 goto call_lose;
1670 if (__glibc_unlikely (elf_machine_reject_phdr_p
1671 (phdr, ehdr->e_phnum, fbp->buf, fbp->len,
1672 loader, fd)))
1673 goto close_and_out;
1675 /* Check .note.ABI-tag if present. */
1676 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1677 if (ph->p_type == PT_NOTE && ph->p_filesz >= 32 && ph->p_align >= 4)
1679 ElfW(Addr) size = ph->p_filesz;
1681 if (ph->p_offset + size <= (size_t) fbp->len)
1682 abi_note = (void *) (fbp->buf + ph->p_offset);
1683 else
1685 abi_note = alloca (size);
1686 __lseek (fd, ph->p_offset, SEEK_SET);
1687 if (__libc_read (fd, (void *) abi_note, size) != size)
1688 goto read_error;
1691 while (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1693 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
1694 ElfW(Addr) note_size = 3 * sizeof (ElfW(Word))
1695 + ROUND (abi_note[0])
1696 + ROUND (abi_note[1]);
1698 if (size - 32 < note_size)
1700 size = 0;
1701 break;
1703 size -= note_size;
1704 abi_note = (void *) abi_note + note_size;
1707 if (size == 0)
1708 continue;
1710 osversion = (abi_note[5] & 0xff) * 65536
1711 + (abi_note[6] & 0xff) * 256
1712 + (abi_note[7] & 0xff);
1713 if (abi_note[4] != __ABI_TAG_OS
1714 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1716 close_and_out:
1717 __close (fd);
1718 __set_errno (ENOENT);
1719 fd = -1;
1722 break;
1726 return fd;
1729 /* Try to open NAME in one of the directories in *DIRSP.
1730 Return the fd, or -1. If successful, fill in *REALNAME
1731 with the malloc'd full directory name. If it turns out
1732 that none of the directories in *DIRSP exists, *DIRSP is
1733 replaced with (void *) -1, and the old value is free()d
1734 if MAY_FREE_DIRS is true. */
1736 static int
1737 open_path (const char *name, size_t namelen, int mode,
1738 struct r_search_path_struct *sps, char **realname,
1739 struct filebuf *fbp, struct link_map *loader, int whatcode,
1740 bool *found_other_class)
1742 struct r_search_path_elem **dirs = sps->dirs;
1743 char *buf;
1744 int fd = -1;
1745 const char *current_what = NULL;
1746 int any = 0;
1748 if (__glibc_unlikely (dirs == NULL))
1749 /* We're called before _dl_init_paths when loading the main executable
1750 given on the command line when rtld is run directly. */
1751 return -1;
1753 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1756 struct r_search_path_elem *this_dir = *dirs;
1757 size_t buflen = 0;
1758 size_t cnt;
1759 char *edp;
1760 int here_any = 0;
1761 int err;
1763 /* If we are debugging the search for libraries print the path
1764 now if it hasn't happened now. */
1765 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)
1766 && current_what != this_dir->what)
1768 current_what = this_dir->what;
1769 print_search_path (dirs, current_what, this_dir->where);
1772 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1773 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1775 /* Skip this directory if we know it does not exist. */
1776 if (this_dir->status[cnt] == nonexisting)
1777 continue;
1779 buflen =
1780 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1781 capstr[cnt].len),
1782 name, namelen)
1783 - buf);
1785 /* Print name we try if this is wanted. */
1786 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1787 _dl_debug_printf (" trying file=%s\n", buf);
1789 fd = open_verify (buf, -1, fbp, loader, whatcode, mode,
1790 found_other_class, false);
1791 if (this_dir->status[cnt] == unknown)
1793 if (fd != -1)
1794 this_dir->status[cnt] = existing;
1795 /* Do not update the directory information when loading
1796 auditing code. We must try to disturb the program as
1797 little as possible. */
1798 else if (loader == NULL
1799 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0)
1801 /* We failed to open machine dependent library. Let's
1802 test whether there is any directory at all. */
1803 struct stat64 st;
1805 buf[buflen - namelen - 1] = '\0';
1807 if (__xstat64 (_STAT_VER, buf, &st) != 0
1808 || ! S_ISDIR (st.st_mode))
1809 /* The directory does not exist or it is no directory. */
1810 this_dir->status[cnt] = nonexisting;
1811 else
1812 this_dir->status[cnt] = existing;
1816 /* Remember whether we found any existing directory. */
1817 here_any |= this_dir->status[cnt] != nonexisting;
1819 if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE)
1820 && __libc_enable_secure)
1822 /* This is an extra security effort to make sure nobody can
1823 preload broken shared objects which are in the trusted
1824 directories and so exploit the bugs. */
1825 struct stat64 st;
1827 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1828 || (st.st_mode & S_ISUID) == 0)
1830 /* The shared object cannot be tested for being SUID
1831 or this bit is not set. In this case we must not
1832 use this object. */
1833 __close (fd);
1834 fd = -1;
1835 /* We simply ignore the file, signal this by setting
1836 the error value which would have been set by `open'. */
1837 errno = ENOENT;
1842 if (fd != -1)
1844 *realname = (char *) malloc (buflen);
1845 if (*realname != NULL)
1847 memcpy (*realname, buf, buflen);
1848 return fd;
1850 else
1852 /* No memory for the name, we certainly won't be able
1853 to load and link it. */
1854 __close (fd);
1855 return -1;
1858 if (here_any && (err = errno) != ENOENT && err != EACCES)
1859 /* The file exists and is readable, but something went wrong. */
1860 return -1;
1862 /* Remember whether we found anything. */
1863 any |= here_any;
1865 while (*++dirs != NULL);
1867 /* Remove the whole path if none of the directories exists. */
1868 if (__glibc_unlikely (! any))
1870 /* Paths which were allocated using the minimal malloc() in ld.so
1871 must not be freed using the general free() in libc. */
1872 if (sps->malloced)
1873 free (sps->dirs);
1875 /* rtld_search_dirs and env_path_list are attribute_relro, therefore
1876 avoid writing into it. */
1877 if (sps != &rtld_search_dirs && sps != &env_path_list)
1878 sps->dirs = (void *) -1;
1881 return -1;
1884 /* Map in the shared object file NAME. */
1886 struct link_map *
1887 _dl_map_object (struct link_map *loader, const char *name,
1888 int type, int trace_mode, int mode, Lmid_t nsid)
1890 int fd;
1891 const char *origname = NULL;
1892 char *realname;
1893 char *name_copy;
1894 struct link_map *l;
1895 struct filebuf fb;
1897 assert (nsid >= 0);
1898 assert (nsid < GL(dl_nns));
1900 /* Look for this name among those already loaded. */
1901 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
1903 /* If the requested name matches the soname of a loaded object,
1904 use that object. Elide this check for names that have not
1905 yet been opened. */
1906 if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
1907 continue;
1908 if (!_dl_name_match_p (name, l))
1910 const char *soname;
1912 if (__glibc_likely (l->l_soname_added)
1913 || l->l_info[DT_SONAME] == NULL)
1914 continue;
1916 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1917 + l->l_info[DT_SONAME]->d_un.d_val);
1918 if (strcmp (name, soname) != 0)
1919 continue;
1921 /* We have a match on a new name -- cache it. */
1922 add_name_to_object (l, soname);
1923 l->l_soname_added = 1;
1926 /* We have a match. */
1927 return l;
1930 /* Display information if we are debugging. */
1931 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
1932 && loader != NULL)
1933 _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0
1934 ? "\nfile=%s [%lu]; needed by %s [%lu]\n"
1935 : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n",
1936 name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns);
1938 #ifdef SHARED
1939 /* Give the auditing libraries a chance to change the name before we
1940 try anything. */
1941 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1942 && (loader == NULL || loader->l_auditing == 0))
1944 struct audit_ifaces *afct = GLRO(dl_audit);
1945 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1947 if (afct->objsearch != NULL)
1949 const char *before = name;
1950 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1951 LA_SER_ORIG);
1952 if (name == NULL)
1954 /* Do not try anything further. */
1955 fd = -1;
1956 goto no_file;
1958 if (before != name && strcmp (before, name) != 0)
1960 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1961 _dl_debug_printf ("audit changed filename %s -> %s\n",
1962 before, name);
1964 if (origname == NULL)
1965 origname = before;
1969 afct = afct->next;
1972 #endif
1974 /* Will be true if we found a DSO which is of the other ELF class. */
1975 bool found_other_class = false;
1977 if (strchr (name, '/') == NULL)
1979 /* Search for NAME in several places. */
1981 size_t namelen = strlen (name) + 1;
1983 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1984 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
1986 fd = -1;
1988 /* When the object has the RUNPATH information we don't use any
1989 RPATHs. */
1990 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1992 /* This is the executable's map (if there is one). Make sure that
1993 we do not look at it twice. */
1994 struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1995 bool did_main_map = false;
1997 /* First try the DT_RPATH of the dependent object that caused NAME
1998 to be loaded. Then that object's dependent, and on up. */
1999 for (l = loader; l; l = l->l_loader)
2000 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2002 fd = open_path (name, namelen, mode,
2003 &l->l_rpath_dirs,
2004 &realname, &fb, loader, LA_SER_RUNPATH,
2005 &found_other_class);
2006 if (fd != -1)
2007 break;
2009 did_main_map |= l == main_map;
2012 /* If dynamically linked, try the DT_RPATH of the executable
2013 itself. NB: we do this for lookups in any namespace. */
2014 if (fd == -1 && !did_main_map
2015 && main_map != NULL && main_map->l_type != lt_loaded
2016 && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH,
2017 "RPATH"))
2018 fd = open_path (name, namelen, mode,
2019 &main_map->l_rpath_dirs,
2020 &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
2021 &found_other_class);
2024 /* Try the LD_LIBRARY_PATH environment variable. */
2025 if (fd == -1 && env_path_list.dirs != (void *) -1)
2026 fd = open_path (name, namelen, mode, &env_path_list,
2027 &realname, &fb,
2028 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
2029 LA_SER_LIBPATH, &found_other_class);
2031 /* Look at the RUNPATH information for this binary. */
2032 if (fd == -1 && loader != NULL
2033 && cache_rpath (loader, &loader->l_runpath_dirs,
2034 DT_RUNPATH, "RUNPATH"))
2035 fd = open_path (name, namelen, mode,
2036 &loader->l_runpath_dirs, &realname, &fb, loader,
2037 LA_SER_RUNPATH, &found_other_class);
2039 if (fd == -1)
2041 realname = _dl_sysdep_open_object (name, namelen, &fd);
2042 if (realname != NULL)
2044 fd = open_verify (realname, fd,
2045 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2046 LA_SER_CONFIG, mode, &found_other_class,
2047 false);
2048 if (fd == -1)
2049 free (realname);
2053 #ifdef USE_LDCONFIG
2054 if (fd == -1
2055 && (__glibc_likely ((mode & __RTLD_SECURE) == 0)
2056 || ! __libc_enable_secure)
2057 && __glibc_likely (GLRO(dl_inhibit_cache) == 0))
2059 /* Check the list of libraries in the file /etc/ld.so.cache,
2060 for compatibility with Linux's ldconfig program. */
2061 char *cached = _dl_load_cache_lookup (name);
2063 if (cached != NULL)
2065 // XXX Correct to unconditionally default to namespace 0?
2066 l = (loader
2067 ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded
2068 # ifdef SHARED
2069 ?: &GL(dl_rtld_map)
2070 # endif
2073 /* If the loader has the DF_1_NODEFLIB flag set we must not
2074 use a cache entry from any of these directories. */
2075 if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB))
2077 const char *dirp = system_dirs;
2078 unsigned int cnt = 0;
2082 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2084 /* The prefix matches. Don't use the entry. */
2085 free (cached);
2086 cached = NULL;
2087 break;
2090 dirp += system_dirs_len[cnt] + 1;
2091 ++cnt;
2093 while (cnt < nsystem_dirs_len);
2096 if (cached != NULL)
2098 fd = open_verify (cached, -1,
2099 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2100 LA_SER_CONFIG, mode, &found_other_class,
2101 false);
2102 if (__glibc_likely (fd != -1))
2103 realname = cached;
2104 else
2105 free (cached);
2109 #endif
2111 /* Finally, try the default path. */
2112 if (fd == -1
2113 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2114 || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB)))
2115 && rtld_search_dirs.dirs != (void *) -1)
2116 fd = open_path (name, namelen, mode, &rtld_search_dirs,
2117 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
2119 /* Add another newline when we are tracing the library loading. */
2120 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2121 _dl_debug_printf ("\n");
2123 else
2125 /* The path may contain dynamic string tokens. */
2126 realname = (loader
2127 ? expand_dynamic_string_token (loader, name, 0)
2128 : __strdup (name));
2129 if (realname == NULL)
2130 fd = -1;
2131 else
2133 fd = open_verify (realname, -1, &fb,
2134 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
2135 &found_other_class, true);
2136 if (__glibc_unlikely (fd == -1))
2137 free (realname);
2141 #ifdef SHARED
2142 no_file:
2143 #endif
2144 /* In case the LOADER information has only been provided to get to
2145 the appropriate RUNPATH/RPATH information we do not need it
2146 anymore. */
2147 if (mode & __RTLD_CALLMAP)
2148 loader = NULL;
2150 if (__glibc_unlikely (fd == -1))
2152 if (trace_mode
2153 && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0))
2155 /* We haven't found an appropriate library. But since we
2156 are only interested in the list of libraries this isn't
2157 so severe. Fake an entry with all the information we
2158 have. */
2159 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2161 /* Allocate a new object map. */
2162 if ((name_copy = __strdup (name)) == NULL
2163 || (l = _dl_new_object (name_copy, name, type, loader,
2164 mode, nsid)) == NULL)
2166 free (name_copy);
2167 _dl_signal_error (ENOMEM, name, NULL,
2168 N_("cannot create shared object descriptor"));
2170 /* Signal that this is a faked entry. */
2171 l->l_faked = 1;
2172 /* Since the descriptor is initialized with zero we do not
2173 have do this here.
2174 l->l_reserved = 0; */
2175 l->l_buckets = &dummy_bucket;
2176 l->l_nbuckets = 1;
2177 l->l_relocated = 1;
2179 /* Enter the object in the object list. */
2180 _dl_add_to_namespace_list (l, nsid);
2182 return l;
2184 else if (found_other_class)
2185 _dl_signal_error (0, name, NULL,
2186 ELFW(CLASS) == ELFCLASS32
2187 ? N_("wrong ELF class: ELFCLASS64")
2188 : N_("wrong ELF class: ELFCLASS32"));
2189 else
2190 _dl_signal_error (errno, name, NULL,
2191 N_("cannot open shared object file"));
2194 void *stack_end = __libc_stack_end;
2195 return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader,
2196 type, mode, &stack_end, nsid);
2199 struct add_path_state
2201 bool counting;
2202 unsigned int idx;
2203 Dl_serinfo *si;
2204 char *allocptr;
2207 static void
2208 add_path (struct add_path_state *p, const struct r_search_path_struct *sps,
2209 unsigned int flags)
2211 if (sps->dirs != (void *) -1)
2213 struct r_search_path_elem **dirs = sps->dirs;
2216 const struct r_search_path_elem *const r = *dirs++;
2217 if (p->counting)
2219 p->si->dls_cnt++;
2220 p->si->dls_size += MAX (2, r->dirnamelen);
2222 else
2224 Dl_serpath *const sp = &p->si->dls_serpath[p->idx++];
2225 sp->dls_name = p->allocptr;
2226 if (r->dirnamelen < 2)
2227 *p->allocptr++ = r->dirnamelen ? '/' : '.';
2228 else
2229 p->allocptr = __mempcpy (p->allocptr,
2230 r->dirname, r->dirnamelen - 1);
2231 *p->allocptr++ = '\0';
2232 sp->dls_flags = flags;
2235 while (*dirs != NULL);
2239 void
2240 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2242 if (counting)
2244 si->dls_cnt = 0;
2245 si->dls_size = 0;
2248 struct add_path_state p =
2250 .counting = counting,
2251 .idx = 0,
2252 .si = si,
2253 .allocptr = (char *) &si->dls_serpath[si->dls_cnt]
2256 # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */
2258 /* When the object has the RUNPATH information we don't use any RPATHs. */
2259 if (loader->l_info[DT_RUNPATH] == NULL)
2261 /* First try the DT_RPATH of the dependent object that caused NAME
2262 to be loaded. Then that object's dependent, and on up. */
2264 struct link_map *l = loader;
2267 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2268 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2269 l = l->l_loader;
2271 while (l != NULL);
2273 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2274 if (loader->l_ns == LM_ID_BASE)
2276 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2277 if (l != NULL && l->l_type != lt_loaded && l != loader)
2278 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2279 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2283 /* Try the LD_LIBRARY_PATH environment variable. */
2284 add_path (&p, &env_path_list, XXX_ENV);
2286 /* Look at the RUNPATH information for this binary. */
2287 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2288 add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH);
2290 /* XXX
2291 Here is where ld.so.cache gets checked, but we don't have
2292 a way to indicate that in the results for Dl_serinfo. */
2294 /* Finally, try the default path. */
2295 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2296 add_path (&p, &rtld_search_dirs, XXX_default);
2298 if (counting)
2299 /* Count the struct size before the string area, which we didn't
2300 know before we completed dls_cnt. */
2301 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;