Update INSTALL with package versions that are known to work
[glibc.git] / elf / dl-load.c
blob9e2089cfaa7dd4b1abd23426dc2d5e78e7ce1f8e
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2021 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 <https://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 <gnu/lib-names.h>
35 /* Type for the buffer we put the ELF header and hopefully the program
36 header. This buffer does not really have to be too large. In most
37 cases the program header follows the ELF header directly. If this
38 is not the case all bets are off and we can make the header
39 arbitrarily large and still won't get it read. This means the only
40 question is how large are the ELF and program header combined. The
41 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
42 bytes long. Each program header entry is again 32 and 56 bytes
43 long respectively. I.e., even with a file which has 10 program
44 header entries we only have to read 372B/624B respectively. Add to
45 this a bit of margin for program notes and reading 512B and 832B
46 for 32-bit and 64-bit files respecitvely is enough. If this
47 heuristic should really fail for some file the code in
48 `_dl_map_object_from_fd' knows how to recover. */
49 struct filebuf
51 ssize_t len;
52 #if __WORDSIZE == 32
53 # define FILEBUF_SIZE 512
54 #else
55 # define FILEBUF_SIZE 832
56 #endif
57 char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr)))));
60 #include "dynamic-link.h"
61 #include <abi-tag.h>
62 #include <stackinfo.h>
63 #include <sysdep.h>
64 #include <stap-probe.h>
65 #include <libc-pointer-arith.h>
66 #include <array_length.h>
68 #include <dl-dst.h>
69 #include <dl-load.h>
70 #include <dl-map-segments.h>
71 #include <dl-unmap-segments.h>
72 #include <dl-machine-reject-phdr.h>
73 #include <dl-sysdep-open.h>
74 #include <dl-prop.h>
75 #include <not-cancel.h>
77 #include <endian.h>
78 #if BYTE_ORDER == BIG_ENDIAN
79 # define byteorder ELFDATA2MSB
80 #elif BYTE_ORDER == LITTLE_ENDIAN
81 # define byteorder ELFDATA2LSB
82 #else
83 # error "Unknown BYTE_ORDER " BYTE_ORDER
84 # define byteorder ELFDATANONE
85 #endif
87 #define STRING(x) __STRING (x)
90 int __stack_prot attribute_hidden attribute_relro
91 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
92 = PROT_GROWSDOWN;
93 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
94 = PROT_GROWSUP;
95 #else
96 = 0;
97 #endif
100 /* This is the decomposed LD_LIBRARY_PATH search path. */
101 struct r_search_path_struct __rtld_env_path_list attribute_relro;
103 /* List of the hardware capabilities we might end up using. */
104 #ifdef SHARED
105 static const struct r_strlenpair *capstr attribute_relro;
106 static size_t ncapstr attribute_relro;
107 static size_t max_capstrlen attribute_relro;
108 #else
109 enum { ncapstr = 1, max_capstrlen = 0 };
110 #endif
113 /* Get the generated information about the trusted directories. Use
114 an array of concatenated strings to avoid relocations. See
115 gen-trusted-dirs.awk. */
116 #include "trusted-dirs.h"
118 static const char system_dirs[] = SYSTEM_DIRS;
119 static const size_t system_dirs_len[] =
121 SYSTEM_DIRS_LEN
123 #define nsystem_dirs_len array_length (system_dirs_len)
125 static bool
126 is_trusted_path_normalize (const char *path, size_t len)
128 if (len == 0)
129 return false;
131 char *npath = (char *) alloca (len + 2);
132 char *wnp = npath;
133 while (*path != '\0')
135 if (path[0] == '/')
137 if (path[1] == '.')
139 if (path[2] == '.' && (path[3] == '/' || path[3] == '\0'))
141 while (wnp > npath && *--wnp != '/')
143 path += 3;
144 continue;
146 else if (path[2] == '/' || path[2] == '\0')
148 path += 2;
149 continue;
153 if (wnp > npath && wnp[-1] == '/')
155 ++path;
156 continue;
160 *wnp++ = *path++;
163 if (wnp == npath || wnp[-1] != '/')
164 *wnp++ = '/';
166 const char *trun = system_dirs;
168 for (size_t idx = 0; idx < nsystem_dirs_len; ++idx)
170 if (wnp - npath >= system_dirs_len[idx]
171 && memcmp (trun, npath, system_dirs_len[idx]) == 0)
172 /* Found it. */
173 return true;
175 trun += system_dirs_len[idx] + 1;
178 return false;
181 /* Given a substring starting at INPUT, just after the DST '$' start
182 token, determine if INPUT contains DST token REF, following the
183 ELF gABI rules for DSTs:
185 * Longest possible sequence using the rules (greedy).
187 * Must start with a $ (enforced by caller).
189 * Must follow $ with one underscore or ASCII [A-Za-z] (caller
190 follows these rules for REF) or '{' (start curly quoted name).
192 * Must follow first two characters with zero or more [A-Za-z0-9_]
193 (enforced by caller) or '}' (end curly quoted name).
195 If the sequence is a DST matching REF then the length of the DST
196 (excluding the $ sign but including curly braces, if any) is
197 returned, otherwise 0. */
198 static size_t
199 is_dst (const char *input, const char *ref)
201 bool is_curly = false;
203 /* Is a ${...} input sequence? */
204 if (input[0] == '{')
206 is_curly = true;
207 ++input;
210 /* Check for matching name, following closing curly brace (if
211 required), or trailing characters which are part of an
212 identifier. */
213 size_t rlen = strlen (ref);
214 if (strncmp (input, ref, rlen) != 0
215 || (is_curly && input[rlen] != '}')
216 || ((input[rlen] >= 'A' && input[rlen] <= 'Z')
217 || (input[rlen] >= 'a' && input[rlen] <= 'z')
218 || (input[rlen] >= '0' && input[rlen] <= '9')
219 || (input[rlen] == '_')))
220 return 0;
222 if (is_curly)
223 /* Count the two curly braces. */
224 return rlen + 2;
225 else
226 return rlen;
229 /* INPUT should be the start of a path e.g DT_RPATH or name e.g.
230 DT_NEEDED. The return value is the number of known DSTs found. We
231 count all known DSTs regardless of __libc_enable_secure; the caller
232 is responsible for enforcing the security of the substitution rules
233 (usually _dl_dst_substitute). */
234 size_t
235 _dl_dst_count (const char *input)
237 size_t cnt = 0;
239 input = strchr (input, '$');
241 /* Most likely there is no DST. */
242 if (__glibc_likely (input == NULL))
243 return 0;
247 size_t len;
249 ++input;
250 /* All DSTs must follow ELF gABI rules, see is_dst (). */
251 if ((len = is_dst (input, "ORIGIN")) != 0
252 || (len = is_dst (input, "PLATFORM")) != 0
253 || (len = is_dst (input, "LIB")) != 0)
254 ++cnt;
256 /* There may be more than one DST in the input. */
257 input = strchr (input + len, '$');
259 while (input != NULL);
261 return cnt;
264 /* Process INPUT for DSTs and store in RESULT using the information
265 from link map L to resolve the DSTs. This function only handles one
266 path at a time and does not handle colon-separated path lists (see
267 fillin_rpath ()). Lastly the size of result in bytes should be at
268 least equal to the value returned by DL_DST_REQUIRED. Note that it
269 is possible for a DT_NEEDED, DT_AUXILIARY, and DT_FILTER entries to
270 have colons, but we treat those as literal colons here, not as path
271 list delimeters. */
272 char *
273 _dl_dst_substitute (struct link_map *l, const char *input, char *result)
275 /* Copy character-by-character from input into the working pointer
276 looking for any DSTs. We track the start of input and if we are
277 going to check for trusted paths, all of which are part of $ORIGIN
278 handling in SUID/SGID cases (see below). In some cases, like when
279 a DST cannot be replaced, we may set result to an empty string and
280 return. */
281 char *wp = result;
282 const char *start = input;
283 bool check_for_trusted = false;
287 if (__glibc_unlikely (*input == '$'))
289 const char *repl = NULL;
290 size_t len;
292 ++input;
293 if ((len = is_dst (input, "ORIGIN")) != 0)
295 /* For SUID/GUID programs we normally ignore the path with
296 $ORIGIN in DT_RUNPATH, or DT_RPATH. However, there is
297 one exception to this rule, and it is:
299 * $ORIGIN appears as the first path element, and is
300 the only string in the path or is immediately
301 followed by a path separator and the rest of the
302 path,
304 and ...
306 * The path is rooted in a trusted directory.
308 This exception allows such programs to reference
309 shared libraries in subdirectories of trusted
310 directories. The use case is one of general
311 organization and deployment flexibility.
312 Trusted directories are usually such paths as "/lib64"
313 or "/usr/lib64", and the usual RPATHs take the form of
314 [$ORIGIN/../$LIB/somedir]. */
315 if (__glibc_unlikely (__libc_enable_secure)
316 && !(input == start + 1
317 && (input[len] == '\0' || input[len] == '/')))
318 repl = (const char *) -1;
319 else
320 repl = l->l_origin;
322 check_for_trusted = (__libc_enable_secure
323 && l->l_type == lt_executable);
325 else if ((len = is_dst (input, "PLATFORM")) != 0)
326 repl = GLRO(dl_platform);
327 else if ((len = is_dst (input, "LIB")) != 0)
328 repl = DL_DST_LIB;
330 if (repl != NULL && repl != (const char *) -1)
332 wp = __stpcpy (wp, repl);
333 input += len;
335 else if (len != 0)
337 /* We found a valid DST that we know about, but we could
338 not find a replacement value for it, therefore we
339 cannot use this path and discard it. */
340 *result = '\0';
341 return result;
343 else
344 /* No DST we recognize. */
345 *wp++ = '$';
347 else
349 *wp++ = *input++;
352 while (*input != '\0');
354 /* In SUID/SGID programs, after $ORIGIN expansion the normalized
355 path must be rooted in one of the trusted directories. The $LIB
356 and $PLATFORM DST cannot in any way be manipulated by the caller
357 because they are fixed values that are set by the dynamic loader
358 and therefore any paths using just $LIB or $PLATFORM need not be
359 checked for trust, the authors of the binaries themselves are
360 trusted to have designed this correctly. Only $ORIGIN is tested in
361 this way because it may be manipulated in some ways with hard
362 links. */
363 if (__glibc_unlikely (check_for_trusted)
364 && !is_trusted_path_normalize (result, wp - result))
366 *result = '\0';
367 return result;
370 *wp = '\0';
372 return result;
376 /* Return a malloc allocated copy of INPUT with all recognized DSTs
377 replaced. On some platforms it might not be possible to determine the
378 path from which the object belonging to the map is loaded. In this
379 case the path containing the DST is left out. On error NULL
380 is returned. */
381 static char *
382 expand_dynamic_string_token (struct link_map *l, const char *input)
384 /* We make two runs over the string. First we determine how large the
385 resulting string is and then we copy it over. Since this is no
386 frequently executed operation we are looking here not for performance
387 but rather for code size. */
388 size_t cnt;
389 size_t total;
390 char *result;
392 /* Determine the number of DSTs. */
393 cnt = _dl_dst_count (input);
395 /* If we do not have to replace anything simply copy the string. */
396 if (__glibc_likely (cnt == 0))
397 return __strdup (input);
399 /* Determine the length of the substituted string. */
400 total = DL_DST_REQUIRED (l, input, strlen (input), cnt);
402 /* Allocate the necessary memory. */
403 result = (char *) malloc (total + 1);
404 if (result == NULL)
405 return NULL;
407 return _dl_dst_substitute (l, input, result);
411 /* Add `name' to the list of names for a particular shared object.
412 `name' is expected to have been allocated with malloc and will
413 be freed if the shared object already has this name.
414 Returns false if the object already had this name. */
415 static void
416 add_name_to_object (struct link_map *l, const char *name)
418 struct libname_list *lnp, *lastp;
419 struct libname_list *newname;
420 size_t name_len;
422 lastp = NULL;
423 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
424 if (strcmp (name, lnp->name) == 0)
425 return;
427 name_len = strlen (name) + 1;
428 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
429 if (newname == NULL)
431 /* No more memory. */
432 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
433 return;
435 /* The object should have a libname set from _dl_new_object. */
436 assert (lastp != NULL);
438 newname->name = memcpy (newname + 1, name, name_len);
439 newname->next = NULL;
440 newname->dont_free = 0;
441 lastp->next = newname;
444 /* Standard search directories. */
445 struct r_search_path_struct __rtld_search_dirs attribute_relro;
447 static size_t max_dirnamelen;
449 static struct r_search_path_elem **
450 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
451 const char *what, const char *where, struct link_map *l)
453 char *cp;
454 size_t nelems = 0;
456 while ((cp = __strsep (&rpath, sep)) != NULL)
458 struct r_search_path_elem *dirp;
459 char *to_free = NULL;
460 size_t len = 0;
462 /* `strsep' can pass an empty string. */
463 if (*cp != '\0')
465 to_free = cp = expand_dynamic_string_token (l, cp);
467 /* expand_dynamic_string_token can return NULL in case of empty
468 path or memory allocation failure. */
469 if (cp == NULL)
470 continue;
472 /* Compute the length after dynamic string token expansion and
473 ignore empty paths. */
474 len = strlen (cp);
475 if (len == 0)
477 free (to_free);
478 continue;
481 /* Remove trailing slashes (except for "/"). */
482 while (len > 1 && cp[len - 1] == '/')
483 --len;
485 /* Now add one if there is none so far. */
486 if (len > 0 && cp[len - 1] != '/')
487 cp[len++] = '/';
490 /* See if this directory is already known. */
491 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
492 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
493 break;
495 if (dirp != NULL)
497 /* It is available, see whether it's on our own list. */
498 size_t cnt;
499 for (cnt = 0; cnt < nelems; ++cnt)
500 if (result[cnt] == dirp)
501 break;
503 if (cnt == nelems)
504 result[nelems++] = dirp;
506 else
508 size_t cnt;
509 enum r_dir_status init_val;
510 size_t where_len = where ? strlen (where) + 1 : 0;
512 /* It's a new directory. Create an entry and add it. */
513 dirp = (struct r_search_path_elem *)
514 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
515 + where_len + len + 1);
516 if (dirp == NULL)
517 _dl_signal_error (ENOMEM, NULL, NULL,
518 N_("cannot create cache for search path"));
520 dirp->dirname = ((char *) dirp + sizeof (*dirp)
521 + ncapstr * sizeof (enum r_dir_status));
522 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
523 dirp->dirnamelen = len;
525 if (len > max_dirnamelen)
526 max_dirnamelen = len;
528 /* We have to make sure all the relative directories are
529 never ignored. The current directory might change and
530 all our saved information would be void. */
531 init_val = cp[0] != '/' ? existing : unknown;
532 for (cnt = 0; cnt < ncapstr; ++cnt)
533 dirp->status[cnt] = init_val;
535 dirp->what = what;
536 if (__glibc_likely (where != NULL))
537 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
538 + (ncapstr * sizeof (enum r_dir_status)),
539 where, where_len);
540 else
541 dirp->where = NULL;
543 dirp->next = GL(dl_all_dirs);
544 GL(dl_all_dirs) = dirp;
546 /* Put it in the result array. */
547 result[nelems++] = dirp;
549 free (to_free);
552 /* Terminate the array. */
553 result[nelems] = NULL;
555 return result;
559 static bool
560 decompose_rpath (struct r_search_path_struct *sps,
561 const char *rpath, struct link_map *l, const char *what)
563 /* Make a copy we can work with. */
564 const char *where = l->l_name;
565 char *cp;
566 struct r_search_path_elem **result;
567 size_t nelems;
568 /* Initialize to please the compiler. */
569 const char *errstring = NULL;
571 /* First see whether we must forget the RUNPATH and RPATH from this
572 object. */
573 if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL)
574 && !__libc_enable_secure)
576 const char *inhp = GLRO(dl_inhibit_rpath);
580 const char *wp = where;
582 while (*inhp == *wp && *wp != '\0')
584 ++inhp;
585 ++wp;
588 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
590 /* This object is on the list of objects for which the
591 RUNPATH and RPATH must not be used. */
592 sps->dirs = (void *) -1;
593 return false;
596 while (*inhp != '\0')
597 if (*inhp++ == ':')
598 break;
600 while (*inhp != '\0');
603 /* Ignore empty rpaths. */
604 if (*rpath == '\0')
606 sps->dirs = (struct r_search_path_elem **) -1;
607 return false;
610 /* Make a writable copy. */
611 char *copy = __strdup (rpath);
612 if (copy == NULL)
614 errstring = N_("cannot create RUNPATH/RPATH copy");
615 goto signal_error;
618 /* Count the number of necessary elements in the result array. */
619 nelems = 0;
620 for (cp = copy; *cp != '\0'; ++cp)
621 if (*cp == ':')
622 ++nelems;
624 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
625 number of necessary entries. */
626 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
627 * sizeof (*result));
628 if (result == NULL)
630 free (copy);
631 errstring = N_("cannot create cache for search path");
632 signal_error:
633 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
636 fillin_rpath (copy, result, ":", what, where, l);
638 /* Free the copied RPATH string. `fillin_rpath' make own copies if
639 necessary. */
640 free (copy);
642 /* There is no path after expansion. */
643 if (result[0] == NULL)
645 free (result);
646 sps->dirs = (struct r_search_path_elem **) -1;
647 return false;
650 sps->dirs = result;
651 /* The caller will change this value if we haven't used a real malloc. */
652 sps->malloced = 1;
653 return true;
656 /* Make sure cached path information is stored in *SP
657 and return true if there are any paths to search there. */
658 static bool
659 cache_rpath (struct link_map *l,
660 struct r_search_path_struct *sp,
661 int tag,
662 const char *what)
664 if (sp->dirs == (void *) -1)
665 return false;
667 if (sp->dirs != NULL)
668 return true;
670 if (l->l_info[tag] == NULL)
672 /* There is no path. */
673 sp->dirs = (void *) -1;
674 return false;
677 /* Make sure the cache information is available. */
678 return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
679 + l->l_info[tag]->d_un.d_val),
680 l, what);
684 void
685 _dl_init_paths (const char *llp, const char *source,
686 const char *glibc_hwcaps_prepend,
687 const char *glibc_hwcaps_mask)
689 size_t idx;
690 const char *strp;
691 struct r_search_path_elem *pelem, **aelem;
692 size_t round_size;
693 struct link_map __attribute__ ((unused)) *l = NULL;
694 /* Initialize to please the compiler. */
695 const char *errstring = NULL;
697 /* Fill in the information about the application's RPATH and the
698 directories addressed by the LD_LIBRARY_PATH environment variable. */
700 #ifdef SHARED
701 /* Get the capabilities. */
702 capstr = _dl_important_hwcaps (glibc_hwcaps_prepend, glibc_hwcaps_mask,
703 &ncapstr, &max_capstrlen);
704 #endif
706 /* First set up the rest of the default search directory entries. */
707 aelem = __rtld_search_dirs.dirs = (struct r_search_path_elem **)
708 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
709 if (__rtld_search_dirs.dirs == NULL)
711 errstring = N_("cannot create search path array");
712 signal_error:
713 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
716 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
717 + ncapstr * sizeof (enum r_dir_status))
718 / sizeof (struct r_search_path_elem));
720 __rtld_search_dirs.dirs[0]
721 = malloc (nsystem_dirs_len * round_size
722 * sizeof (*__rtld_search_dirs.dirs[0]));
723 if (__rtld_search_dirs.dirs[0] == NULL)
725 errstring = N_("cannot create cache for search path");
726 goto signal_error;
729 __rtld_search_dirs.malloced = 0;
730 pelem = GL(dl_all_dirs) = __rtld_search_dirs.dirs[0];
731 strp = system_dirs;
732 idx = 0;
736 size_t cnt;
738 *aelem++ = pelem;
740 pelem->what = "system search path";
741 pelem->where = NULL;
743 pelem->dirname = strp;
744 pelem->dirnamelen = system_dirs_len[idx];
745 strp += system_dirs_len[idx] + 1;
747 /* System paths must be absolute. */
748 assert (pelem->dirname[0] == '/');
749 for (cnt = 0; cnt < ncapstr; ++cnt)
750 pelem->status[cnt] = unknown;
752 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
754 pelem += round_size;
756 while (idx < nsystem_dirs_len);
758 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
759 *aelem = NULL;
761 #ifdef SHARED
762 /* This points to the map of the main object. */
763 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
764 if (l != NULL)
766 assert (l->l_type != lt_loaded);
768 if (l->l_info[DT_RUNPATH])
770 /* Allocate room for the search path and fill in information
771 from RUNPATH. */
772 decompose_rpath (&l->l_runpath_dirs,
773 (const void *) (D_PTR (l, l_info[DT_STRTAB])
774 + l->l_info[DT_RUNPATH]->d_un.d_val),
775 l, "RUNPATH");
776 /* During rtld init the memory is allocated by the stub malloc,
777 prevent any attempt to free it by the normal malloc. */
778 l->l_runpath_dirs.malloced = 0;
780 /* The RPATH is ignored. */
781 l->l_rpath_dirs.dirs = (void *) -1;
783 else
785 l->l_runpath_dirs.dirs = (void *) -1;
787 if (l->l_info[DT_RPATH])
789 /* Allocate room for the search path and fill in information
790 from RPATH. */
791 decompose_rpath (&l->l_rpath_dirs,
792 (const void *) (D_PTR (l, l_info[DT_STRTAB])
793 + l->l_info[DT_RPATH]->d_un.d_val),
794 l, "RPATH");
795 /* During rtld init the memory is allocated by the stub
796 malloc, prevent any attempt to free it by the normal
797 malloc. */
798 l->l_rpath_dirs.malloced = 0;
800 else
801 l->l_rpath_dirs.dirs = (void *) -1;
804 #endif /* SHARED */
806 if (llp != NULL && *llp != '\0')
808 char *llp_tmp = strdupa (llp);
810 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
811 elements it has. */
812 size_t nllp = 1;
813 for (const char *cp = llp_tmp; *cp != '\0'; ++cp)
814 if (*cp == ':' || *cp == ';')
815 ++nllp;
817 __rtld_env_path_list.dirs = (struct r_search_path_elem **)
818 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
819 if (__rtld_env_path_list.dirs == NULL)
821 errstring = N_("cannot create cache for search path");
822 goto signal_error;
825 (void) fillin_rpath (llp_tmp, __rtld_env_path_list.dirs, ":;",
826 source, NULL, l);
828 if (__rtld_env_path_list.dirs[0] == NULL)
830 free (__rtld_env_path_list.dirs);
831 __rtld_env_path_list.dirs = (void *) -1;
834 __rtld_env_path_list.malloced = 0;
836 else
837 __rtld_env_path_list.dirs = (void *) -1;
841 /* Process PT_GNU_PROPERTY program header PH in module L after
842 PT_LOAD segments are mapped. Only one NT_GNU_PROPERTY_TYPE_0
843 note is handled which contains processor specific properties.
844 FD is -1 for the kernel mapped main executable otherwise it is
845 the fd used for loading module L. */
847 void
848 _dl_process_pt_gnu_property (struct link_map *l, int fd, const ElfW(Phdr) *ph)
850 const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
851 const ElfW(Addr) size = ph->p_memsz;
852 const ElfW(Addr) align = ph->p_align;
854 /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes in
855 32-bit objects and to 8 bytes in 64-bit objects. Skip notes
856 with incorrect alignment. */
857 if (align != (__ELF_NATIVE_CLASS / 8))
858 return;
860 const ElfW(Addr) start = (ElfW(Addr)) note;
861 unsigned int last_type = 0;
863 while ((ElfW(Addr)) (note + 1) - start < size)
865 /* Find the NT_GNU_PROPERTY_TYPE_0 note. */
866 if (note->n_namesz == 4
867 && note->n_type == NT_GNU_PROPERTY_TYPE_0
868 && memcmp (note + 1, "GNU", 4) == 0)
870 /* Check for invalid property. */
871 if (note->n_descsz < 8
872 || (note->n_descsz % sizeof (ElfW(Addr))) != 0)
873 return;
875 /* Start and end of property array. */
876 unsigned char *ptr = (unsigned char *) (note + 1) + 4;
877 unsigned char *ptr_end = ptr + note->n_descsz;
881 unsigned int type = *(unsigned int *) ptr;
882 unsigned int datasz = *(unsigned int *) (ptr + 4);
884 /* Property type must be in ascending order. */
885 if (type < last_type)
886 return;
888 ptr += 8;
889 if ((ptr + datasz) > ptr_end)
890 return;
892 last_type = type;
894 /* Target specific property processing. */
895 if (_dl_process_gnu_property (l, fd, type, datasz, ptr) == 0)
896 return;
898 /* Check the next property item. */
899 ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
901 while ((ptr_end - ptr) >= 8);
903 /* Only handle one NT_GNU_PROPERTY_TYPE_0. */
904 return;
907 note = ((const void *) note
908 + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz,
909 align));
914 /* Map in the shared object NAME, actually located in REALNAME, and already
915 opened on FD. */
917 #ifndef EXTERNAL_MAP_FROM_FD
918 static
919 #endif
920 struct link_map *
921 _dl_map_object_from_fd (const char *name, const char *origname, int fd,
922 struct filebuf *fbp, char *realname,
923 struct link_map *loader, int l_type, int mode,
924 void **stack_endp, Lmid_t nsid)
926 struct link_map *l = NULL;
927 const ElfW(Ehdr) *header;
928 const ElfW(Phdr) *phdr;
929 const ElfW(Phdr) *ph;
930 size_t maplength;
931 int type;
932 /* Initialize to keep the compiler happy. */
933 const char *errstring = NULL;
934 int errval = 0;
935 struct r_debug *r = _dl_debug_initialize (0, nsid);
936 bool make_consistent = false;
938 /* Get file information. To match the kernel behavior, do not fill
939 in this information for the executable in case of an explicit
940 loader invocation. */
941 struct r_file_id id;
942 if (mode & __RTLD_OPENEXEC)
944 assert (nsid == LM_ID_BASE);
945 memset (&id, 0, sizeof (id));
947 else
949 if (__glibc_unlikely (!_dl_get_file_id (fd, &id)))
951 errstring = N_("cannot stat shared object");
952 lose_errno:
953 errval = errno;
954 lose:
955 /* The file might already be closed. */
956 if (fd != -1)
957 __close_nocancel (fd);
958 if (l != NULL && l->l_map_start != 0)
959 _dl_unmap_segments (l);
960 if (l != NULL && l->l_origin != (char *) -1l)
961 free ((char *) l->l_origin);
962 if (l != NULL && !l->l_libname->dont_free)
963 free (l->l_libname);
964 if (l != NULL && l->l_phdr_allocated)
965 free ((void *) l->l_phdr);
966 free (l);
967 free (realname);
969 if (make_consistent && r != NULL)
971 r->r_state = RT_CONSISTENT;
972 _dl_debug_state ();
973 LIBC_PROBE (map_failed, 2, nsid, r);
976 _dl_signal_error (errval, name, NULL, errstring);
979 /* Look again to see if the real name matched another already loaded. */
980 for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
981 if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id))
983 /* The object is already loaded.
984 Just bump its reference count and return it. */
985 __close_nocancel (fd);
987 /* If the name is not in the list of names for this object add
988 it. */
989 free (realname);
990 add_name_to_object (l, name);
992 return l;
996 #ifdef SHARED
997 /* When loading into a namespace other than the base one we must
998 avoid loading ld.so since there can only be one copy. Ever. */
999 if (__glibc_unlikely (nsid != LM_ID_BASE)
1000 && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id)
1001 || _dl_name_match_p (name, &GL(dl_rtld_map))))
1003 /* This is indeed ld.so. Create a new link_map which refers to
1004 the real one for almost everything. */
1005 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
1006 if (l == NULL)
1007 goto fail_new;
1009 /* Refer to the real descriptor. */
1010 l->l_real = &GL(dl_rtld_map);
1012 /* No need to bump the refcount of the real object, ld.so will
1013 never be unloaded. */
1014 __close_nocancel (fd);
1016 /* Add the map for the mirrored object to the object list. */
1017 _dl_add_to_namespace_list (l, nsid);
1019 return l;
1021 #endif
1023 if (mode & RTLD_NOLOAD)
1025 /* We are not supposed to load the object unless it is already
1026 loaded. So return now. */
1027 free (realname);
1028 __close_nocancel (fd);
1029 return NULL;
1032 /* Print debugging message. */
1033 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1034 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
1036 /* This is the ELF header. We read it in `open_verify'. */
1037 header = (void *) fbp->buf;
1039 /* Signal that we are going to add new objects. */
1040 if (r->r_state == RT_CONSISTENT)
1042 #ifdef SHARED
1043 /* Auditing checkpoint: we are going to add new objects. */
1044 if ((mode & __RTLD_AUDIT) == 0
1045 && __glibc_unlikely (GLRO(dl_naudit) > 0))
1047 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
1048 /* Do not call the functions for any auditing object. */
1049 if (head->l_auditing == 0)
1051 struct audit_ifaces *afct = GLRO(dl_audit);
1052 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1054 if (afct->activity != NULL)
1055 afct->activity (&link_map_audit_state (head, cnt)->cookie,
1056 LA_ACT_ADD);
1058 afct = afct->next;
1062 #endif
1064 /* Notify the debugger we have added some objects. We need to
1065 call _dl_debug_initialize in a static program in case dynamic
1066 linking has not been used before. */
1067 r->r_state = RT_ADD;
1068 _dl_debug_state ();
1069 LIBC_PROBE (map_start, 2, nsid, r);
1070 make_consistent = true;
1072 else
1073 assert (r->r_state == RT_ADD);
1075 /* Enter the new object in the list of loaded objects. */
1076 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
1077 if (__glibc_unlikely (l == NULL))
1079 #ifdef SHARED
1080 fail_new:
1081 #endif
1082 errstring = N_("cannot create shared object descriptor");
1083 goto lose_errno;
1086 /* Extract the remaining details we need from the ELF header
1087 and then read in the program header table. */
1088 l->l_entry = header->e_entry;
1089 type = header->e_type;
1090 l->l_phnum = header->e_phnum;
1092 maplength = header->e_phnum * sizeof (ElfW(Phdr));
1093 if (header->e_phoff + maplength <= (size_t) fbp->len)
1094 phdr = (void *) (fbp->buf + header->e_phoff);
1095 else
1097 phdr = alloca (maplength);
1098 if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength,
1099 header->e_phoff) != maplength)
1101 errstring = N_("cannot read file data");
1102 goto lose_errno;
1106 /* On most platforms presume that PT_GNU_STACK is absent and the stack is
1107 * executable. Other platforms default to a nonexecutable stack and don't
1108 * need PT_GNU_STACK to do so. */
1109 uint_fast16_t stack_flags = DEFAULT_STACK_PERMS;
1112 /* Scan the program header table, collecting its load commands. */
1113 struct loadcmd loadcmds[l->l_phnum];
1114 size_t nloadcmds = 0;
1115 bool has_holes = false;
1117 /* The struct is initialized to zero so this is not necessary:
1118 l->l_ld = 0;
1119 l->l_phdr = 0;
1120 l->l_addr = 0; */
1121 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
1122 switch (ph->p_type)
1124 /* These entries tell us where to find things once the file's
1125 segments are mapped in. We record the addresses it says
1126 verbatim, and later correct for the run-time load address. */
1127 case PT_DYNAMIC:
1128 if (ph->p_filesz)
1130 /* Debuginfo only files from "objcopy --only-keep-debug"
1131 contain a PT_DYNAMIC segment with p_filesz == 0. Skip
1132 such a segment to avoid a crash later. */
1133 l->l_ld = (void *) ph->p_vaddr;
1134 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1136 break;
1138 case PT_PHDR:
1139 l->l_phdr = (void *) ph->p_vaddr;
1140 break;
1142 case PT_LOAD:
1143 /* A load command tells us to map in part of the file.
1144 We record the load commands and process them all later. */
1145 if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0))
1147 errstring = N_("ELF load command alignment not page-aligned");
1148 goto lose;
1150 if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
1151 & (ph->p_align - 1)) != 0))
1153 errstring
1154 = N_("ELF load command address/offset not properly aligned");
1155 goto lose;
1158 struct loadcmd *c = &loadcmds[nloadcmds++];
1159 c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize));
1160 c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
1161 c->dataend = ph->p_vaddr + ph->p_filesz;
1162 c->allocend = ph->p_vaddr + ph->p_memsz;
1163 c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
1165 /* Determine whether there is a gap between the last segment
1166 and this one. */
1167 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1168 has_holes = true;
1170 /* Optimize a common case. */
1171 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1172 c->prot = (PF_TO_PROT
1173 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1174 #else
1175 c->prot = 0;
1176 if (ph->p_flags & PF_R)
1177 c->prot |= PROT_READ;
1178 if (ph->p_flags & PF_W)
1179 c->prot |= PROT_WRITE;
1180 if (ph->p_flags & PF_X)
1181 c->prot |= PROT_EXEC;
1182 #endif
1183 break;
1185 case PT_TLS:
1186 if (ph->p_memsz == 0)
1187 /* Nothing to do for an empty segment. */
1188 break;
1190 l->l_tls_blocksize = ph->p_memsz;
1191 l->l_tls_align = ph->p_align;
1192 if (ph->p_align == 0)
1193 l->l_tls_firstbyte_offset = 0;
1194 else
1195 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1196 l->l_tls_initimage_size = ph->p_filesz;
1197 /* Since we don't know the load address yet only store the
1198 offset. We will adjust it later. */
1199 l->l_tls_initimage = (void *) ph->p_vaddr;
1201 /* l->l_tls_modid is assigned below, once there is no
1202 possibility for failure. */
1204 if (l->l_type != lt_library
1205 && GL(dl_tls_dtv_slotinfo_list) == NULL)
1207 #ifdef SHARED
1208 /* We are loading the executable itself when the dynamic
1209 linker was executed directly. The setup will happen
1210 later. */
1211 assert (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0);
1212 #else
1213 assert (false && "TLS not initialized in static application");
1214 #endif
1216 break;
1218 case PT_GNU_STACK:
1219 stack_flags = ph->p_flags;
1220 break;
1222 case PT_GNU_RELRO:
1223 l->l_relro_addr = ph->p_vaddr;
1224 l->l_relro_size = ph->p_memsz;
1225 break;
1228 if (__glibc_unlikely (nloadcmds == 0))
1230 /* This only happens for a bogus object that will be caught with
1231 another error below. But we don't want to go through the
1232 calculations below using NLOADCMDS - 1. */
1233 errstring = N_("object file has no loadable segments");
1234 goto lose;
1237 /* dlopen of an executable is not valid because it is not possible
1238 to perform proper relocations, handle static TLS, or run the
1239 ELF constructors. For PIE, the check needs the dynamic
1240 section, so there is another check below. */
1241 if (__glibc_unlikely (type != ET_DYN)
1242 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))
1244 /* This object is loaded at a fixed address. This must never
1245 happen for objects loaded with dlopen. */
1246 errstring = N_("cannot dynamically load executable");
1247 goto lose;
1250 /* Length of the sections to be loaded. */
1251 maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
1253 /* Now process the load commands and map segments into memory.
1254 This is responsible for filling in:
1255 l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr
1257 errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
1258 maplength, has_holes, loader);
1259 if (__glibc_unlikely (errstring != NULL))
1261 /* Mappings can be in an inconsistent state: avoid unmap. */
1262 l->l_map_start = l->l_map_end = 0;
1263 goto lose;
1267 if (l->l_ld == 0)
1269 if (__glibc_unlikely (type == ET_DYN))
1271 errstring = N_("object file has no dynamic section");
1272 goto lose;
1275 else
1276 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1278 elf_get_dynamic_info (l, NULL);
1280 /* Make sure we are not dlopen'ing an object that has the
1281 DF_1_NOOPEN flag set, or a PIE object. */
1282 if ((__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
1283 && (mode & __RTLD_DLOPEN))
1284 || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE)
1285 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)))
1287 if (l->l_flags_1 & DF_1_PIE)
1288 errstring
1289 = N_("cannot dynamically load position-independent executable");
1290 else
1291 errstring = N_("shared object cannot be dlopen()ed");
1292 goto lose;
1295 if (l->l_phdr == NULL)
1297 /* The program header is not contained in any of the segments.
1298 We have to allocate memory ourself and copy it over from out
1299 temporary place. */
1300 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1301 * sizeof (ElfW(Phdr)));
1302 if (newp == NULL)
1304 errstring = N_("cannot allocate memory for program header");
1305 goto lose_errno;
1308 l->l_phdr = memcpy (newp, phdr,
1309 (header->e_phnum * sizeof (ElfW(Phdr))));
1310 l->l_phdr_allocated = 1;
1312 else
1313 /* Adjust the PT_PHDR value by the runtime load address. */
1314 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1316 if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X))
1318 /* The stack is presently not executable, but this module
1319 requires that it be executable. We must change the
1320 protection of the variable which contains the flags used in
1321 the mprotect calls. */
1322 #ifdef SHARED
1323 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1325 const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
1326 const size_t s = (uintptr_t) (&__stack_prot + 1) - p;
1328 struct link_map *const m = &GL(dl_rtld_map);
1329 const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr
1330 + m->l_relro_size)
1331 & -GLRO(dl_pagesize));
1332 if (__glibc_likely (p + s <= relro_end))
1334 /* The variable lies in the region protected by RELRO. */
1335 if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0)
1337 errstring = N_("cannot change memory protections");
1338 goto lose_errno;
1340 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1341 __mprotect ((void *) p, s, PROT_READ);
1343 else
1344 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1346 else
1347 #endif
1348 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1350 #ifdef check_consistency
1351 check_consistency ();
1352 #endif
1354 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1355 if (errval)
1357 errstring = N_("\
1358 cannot enable executable stack as shared object requires");
1359 goto lose;
1363 /* Adjust the address of the TLS initialization image. */
1364 if (l->l_tls_initimage != NULL)
1365 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1367 /* Process program headers again after load segments are mapped in
1368 case processing requires accessing those segments. Scan program
1369 headers backward so that PT_NOTE can be skipped if PT_GNU_PROPERTY
1370 exits. */
1371 for (ph = &l->l_phdr[l->l_phnum]; ph != l->l_phdr; --ph)
1372 switch (ph[-1].p_type)
1374 case PT_NOTE:
1375 _dl_process_pt_note (l, fd, &ph[-1]);
1376 break;
1377 case PT_GNU_PROPERTY:
1378 _dl_process_pt_gnu_property (l, fd, &ph[-1]);
1379 break;
1382 /* We are done mapping in the file. We no longer need the descriptor. */
1383 if (__glibc_unlikely (__close_nocancel (fd) != 0))
1385 errstring = N_("cannot close file descriptor");
1386 goto lose_errno;
1388 /* Signal that we closed the file. */
1389 fd = -1;
1391 /* Failures before this point are handled locally via lose.
1392 There are no more failures in this function until return,
1393 to change that the cleanup handling needs to be updated. */
1395 /* If this is ET_EXEC, we should have loaded it as lt_executable. */
1396 assert (type != ET_EXEC || l->l_type == lt_executable);
1398 l->l_entry += l->l_addr;
1400 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
1401 _dl_debug_printf ("\
1402 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1403 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1404 (int) sizeof (void *) * 2,
1405 (unsigned long int) l->l_ld,
1406 (int) sizeof (void *) * 2,
1407 (unsigned long int) l->l_addr,
1408 (int) sizeof (void *) * 2, maplength,
1409 (int) sizeof (void *) * 2,
1410 (unsigned long int) l->l_entry,
1411 (int) sizeof (void *) * 2,
1412 (unsigned long int) l->l_phdr,
1413 (int) sizeof (void *) * 2, l->l_phnum);
1415 /* Set up the symbol hash table. */
1416 _dl_setup_hash (l);
1418 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1419 have to do this for the main map. */
1420 if ((mode & RTLD_DEEPBIND) == 0
1421 && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL)
1422 && &l->l_searchlist != l->l_scope[0])
1424 /* Create an appropriate searchlist. It contains only this map.
1425 This is the definition of DT_SYMBOLIC in SysVr4. */
1426 l->l_symbolic_searchlist.r_list[0] = l;
1427 l->l_symbolic_searchlist.r_nlist = 1;
1429 /* Now move the existing entries one back. */
1430 memmove (&l->l_scope[1], &l->l_scope[0],
1431 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1433 /* Now add the new entry. */
1434 l->l_scope[0] = &l->l_symbolic_searchlist;
1437 /* Remember whether this object must be initialized first. */
1438 if (l->l_flags_1 & DF_1_INITFIRST)
1439 GL(dl_initfirst) = l;
1441 /* Finally the file information. */
1442 l->l_file_id = id;
1444 #ifdef SHARED
1445 /* When auditing is used the recorded names might not include the
1446 name by which the DSO is actually known. Add that as well. */
1447 if (__glibc_unlikely (origname != NULL))
1448 add_name_to_object (l, origname);
1449 #else
1450 /* Audit modules only exist when linking is dynamic so ORIGNAME
1451 cannot be non-NULL. */
1452 assert (origname == NULL);
1453 #endif
1455 /* When we profile the SONAME might be needed for something else but
1456 loading. Add it right away. */
1457 if (__glibc_unlikely (GLRO(dl_profile) != NULL)
1458 && l->l_info[DT_SONAME] != NULL)
1459 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1460 + l->l_info[DT_SONAME]->d_un.d_val));
1462 /* If we have newly loaded libc.so, update the namespace
1463 description. */
1464 if (GL(dl_ns)[nsid].libc_map == NULL
1465 && l->l_info[DT_SONAME] != NULL
1466 && strcmp (((const char *) D_PTR (l, l_info[DT_STRTAB])
1467 + l->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) == 0)
1468 GL(dl_ns)[nsid].libc_map = l;
1470 /* _dl_close can only eventually undo the module ID assignment (via
1471 remove_slotinfo) if this function returns a pointer to a link
1472 map. Therefore, delay this step until all possibilities for
1473 failure have been excluded. */
1474 if (l->l_tls_blocksize > 0
1475 && (__glibc_likely (l->l_type == lt_library)
1476 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1477 not set up TLS data structures, so don't use them now. */
1478 || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL)))
1479 /* Assign the next available module ID. */
1480 l->l_tls_modid = _dl_next_tls_modid ();
1482 #ifdef DL_AFTER_LOAD
1483 DL_AFTER_LOAD (l);
1484 #endif
1486 /* Now that the object is fully initialized add it to the object list. */
1487 _dl_add_to_namespace_list (l, nsid);
1489 #ifdef SHARED
1490 /* Auditing checkpoint: we have a new object. */
1491 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
1492 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1494 struct audit_ifaces *afct = GLRO(dl_audit);
1495 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1497 if (afct->objopen != NULL)
1499 struct auditstate *state = link_map_audit_state (l, cnt);
1500 state->bindflags = afct->objopen (l, nsid, &state->cookie);
1501 l->l_audit_any_plt |= state->bindflags != 0;
1504 afct = afct->next;
1507 #endif
1509 return l;
1512 /* Print search path. */
1513 static void
1514 print_search_path (struct r_search_path_elem **list,
1515 const char *what, const char *name)
1517 char buf[max_dirnamelen + max_capstrlen];
1518 int first = 1;
1520 _dl_debug_printf (" search path=");
1522 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1524 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1525 size_t cnt;
1527 for (cnt = 0; cnt < ncapstr; ++cnt)
1528 if ((*list)->status[cnt] != nonexisting)
1530 #ifdef SHARED
1531 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1532 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1533 cp[0] = '\0';
1534 else
1535 cp[-1] = '\0';
1536 #else
1537 *endp = '\0';
1538 #endif
1540 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1541 first = 0;
1544 ++list;
1547 if (name != NULL)
1548 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1549 DSO_FILENAME (name));
1550 else
1551 _dl_debug_printf_c ("\t\t(%s)\n", what);
1554 /* Open a file and verify it is an ELF file for this architecture. We
1555 ignore only ELF files for other architectures. Non-ELF files and
1556 ELF files with different header information cause fatal errors since
1557 this could mean there is something wrong in the installation and the
1558 user might want to know about this.
1560 If FD is not -1, then the file is already open and FD refers to it.
1561 In that case, FD is consumed for both successful and error returns. */
1562 static int
1563 open_verify (const char *name, int fd,
1564 struct filebuf *fbp, struct link_map *loader,
1565 int whatcode, int mode, bool *found_other_class, bool free_name)
1567 /* This is the expected ELF header. */
1568 #define ELF32_CLASS ELFCLASS32
1569 #define ELF64_CLASS ELFCLASS64
1570 #ifndef VALID_ELF_HEADER
1571 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1572 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1573 # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0)
1574 #elif defined MORE_ELF_HEADER_DATA
1575 MORE_ELF_HEADER_DATA;
1576 #endif
1577 static const unsigned char expected[EI_NIDENT] =
1579 [EI_MAG0] = ELFMAG0,
1580 [EI_MAG1] = ELFMAG1,
1581 [EI_MAG2] = ELFMAG2,
1582 [EI_MAG3] = ELFMAG3,
1583 [EI_CLASS] = ELFW(CLASS),
1584 [EI_DATA] = byteorder,
1585 [EI_VERSION] = EV_CURRENT,
1586 [EI_OSABI] = ELFOSABI_SYSV,
1587 [EI_ABIVERSION] = 0
1589 static const struct
1591 ElfW(Word) vendorlen;
1592 ElfW(Word) datalen;
1593 ElfW(Word) type;
1594 char vendor[4];
1595 } expected_note = { 4, 16, 1, "GNU" };
1596 /* Initialize it to make the compiler happy. */
1597 const char *errstring = NULL;
1598 int errval = 0;
1600 #ifdef SHARED
1601 /* Give the auditing libraries a chance. */
1602 if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0
1603 && loader->l_auditing == 0)
1605 const char *original_name = name;
1606 struct audit_ifaces *afct = GLRO(dl_audit);
1607 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1609 if (afct->objsearch != NULL)
1611 struct auditstate *state = link_map_audit_state (loader, cnt);
1612 name = afct->objsearch (name, &state->cookie, whatcode);
1613 if (name == NULL)
1614 /* Ignore the path. */
1615 return -1;
1618 afct = afct->next;
1621 if (fd != -1 && name != original_name && strcmp (name, original_name))
1623 /* An audit library changed what we're supposed to open,
1624 so FD no longer matches it. */
1625 __close_nocancel (fd);
1626 fd = -1;
1629 #endif
1631 if (fd == -1)
1632 /* Open the file. We always open files read-only. */
1633 fd = __open64_nocancel (name, O_RDONLY | O_CLOEXEC);
1635 if (fd != -1)
1637 ElfW(Ehdr) *ehdr;
1638 ElfW(Phdr) *phdr, *ph;
1639 ElfW(Word) *abi_note;
1640 ElfW(Word) *abi_note_malloced = NULL;
1641 unsigned int osversion;
1642 size_t maplength;
1644 /* We successfully opened the file. Now verify it is a file
1645 we can use. */
1646 __set_errno (0);
1647 fbp->len = 0;
1648 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1649 /* Read in the header. */
1652 ssize_t retlen = __read_nocancel (fd, fbp->buf + fbp->len,
1653 sizeof (fbp->buf) - fbp->len);
1654 if (retlen <= 0)
1655 break;
1656 fbp->len += retlen;
1658 while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr))));
1660 /* This is where the ELF header is loaded. */
1661 ehdr = (ElfW(Ehdr) *) fbp->buf;
1663 /* Now run the tests. */
1664 if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr))))
1666 errval = errno;
1667 errstring = (errval == 0
1668 ? N_("file too short") : N_("cannot read file data"));
1669 lose:
1670 if (free_name)
1672 char *realname = (char *) name;
1673 name = strdupa (realname);
1674 free (realname);
1676 __close_nocancel (fd);
1677 _dl_signal_error (errval, name, NULL, errstring);
1680 /* See whether the ELF header is what we expect. */
1681 if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1682 EI_ABIVERSION)
1683 || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1684 ehdr->e_ident[EI_ABIVERSION])
1685 || memcmp (&ehdr->e_ident[EI_PAD],
1686 &expected[EI_PAD],
1687 EI_NIDENT - EI_PAD) != 0))
1689 /* Something is wrong. */
1690 const Elf32_Word *magp = (const void *) ehdr->e_ident;
1691 if (*magp !=
1692 #if BYTE_ORDER == LITTLE_ENDIAN
1693 ((ELFMAG0 << (EI_MAG0 * 8))
1694 | (ELFMAG1 << (EI_MAG1 * 8))
1695 | (ELFMAG2 << (EI_MAG2 * 8))
1696 | (ELFMAG3 << (EI_MAG3 * 8)))
1697 #else
1698 ((ELFMAG0 << (EI_MAG3 * 8))
1699 | (ELFMAG1 << (EI_MAG2 * 8))
1700 | (ELFMAG2 << (EI_MAG1 * 8))
1701 | (ELFMAG3 << (EI_MAG0 * 8)))
1702 #endif
1704 errstring = N_("invalid ELF header");
1705 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1707 /* This is not a fatal error. On architectures where
1708 32-bit and 64-bit binaries can be run this might
1709 happen. */
1710 *found_other_class = true;
1711 goto close_and_out;
1713 else if (ehdr->e_ident[EI_DATA] != byteorder)
1715 if (BYTE_ORDER == BIG_ENDIAN)
1716 errstring = N_("ELF file data encoding not big-endian");
1717 else
1718 errstring = N_("ELF file data encoding not little-endian");
1720 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1721 errstring
1722 = N_("ELF file version ident does not match current one");
1723 /* XXX We should be able so set system specific versions which are
1724 allowed here. */
1725 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1726 errstring = N_("ELF file OS ABI invalid");
1727 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1728 ehdr->e_ident[EI_ABIVERSION]))
1729 errstring = N_("ELF file ABI version invalid");
1730 else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD],
1731 EI_NIDENT - EI_PAD) != 0)
1732 errstring = N_("nonzero padding in e_ident");
1733 else
1734 /* Otherwise we don't know what went wrong. */
1735 errstring = N_("internal error");
1737 goto lose;
1740 if (__glibc_unlikely (ehdr->e_version != EV_CURRENT))
1742 errstring = N_("ELF file version does not match current one");
1743 goto lose;
1745 if (! __glibc_likely (elf_machine_matches_host (ehdr)))
1746 goto close_and_out;
1747 else if (__glibc_unlikely (ehdr->e_type != ET_DYN
1748 && ehdr->e_type != ET_EXEC))
1750 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1751 goto lose;
1753 else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr))))
1755 errstring = N_("ELF file's phentsize not the expected size");
1756 goto lose;
1759 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1760 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1761 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1762 else
1764 phdr = alloca (maplength);
1765 if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength,
1766 ehdr->e_phoff) != maplength)
1768 read_error:
1769 errval = errno;
1770 errstring = N_("cannot read file data");
1771 goto lose;
1775 if (__glibc_unlikely (elf_machine_reject_phdr_p
1776 (phdr, ehdr->e_phnum, fbp->buf, fbp->len,
1777 loader, fd)))
1778 goto close_and_out;
1780 /* Check .note.ABI-tag if present. */
1781 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1782 if (ph->p_type == PT_NOTE && ph->p_filesz >= 32
1783 && (ph->p_align == 4 || ph->p_align == 8))
1785 ElfW(Addr) size = ph->p_filesz;
1787 if (ph->p_offset + size <= (size_t) fbp->len)
1788 abi_note = (void *) (fbp->buf + ph->p_offset);
1789 else
1791 /* Note: __libc_use_alloca is not usable here, because
1792 thread info may not have been set up yet. */
1793 if (size < __MAX_ALLOCA_CUTOFF)
1794 abi_note = alloca (size);
1795 else
1797 /* There could be multiple PT_NOTEs. */
1798 abi_note_malloced = realloc (abi_note_malloced, size);
1799 if (abi_note_malloced == NULL)
1800 goto read_error;
1802 abi_note = abi_note_malloced;
1804 if (__pread64_nocancel (fd, (void *) abi_note, size,
1805 ph->p_offset) != size)
1807 free (abi_note_malloced);
1808 goto read_error;
1812 while (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1814 ElfW(Addr) note_size
1815 = ELF_NOTE_NEXT_OFFSET (abi_note[0], abi_note[1],
1816 ph->p_align);
1818 if (size - 32 < note_size)
1820 size = 0;
1821 break;
1823 size -= note_size;
1824 abi_note = (void *) abi_note + note_size;
1827 if (size == 0)
1828 continue;
1830 osversion = (abi_note[5] & 0xff) * 65536
1831 + (abi_note[6] & 0xff) * 256
1832 + (abi_note[7] & 0xff);
1833 if (abi_note[4] != __ABI_TAG_OS
1834 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1836 close_and_out:
1837 __close_nocancel (fd);
1838 __set_errno (ENOENT);
1839 fd = -1;
1842 break;
1844 free (abi_note_malloced);
1847 return fd;
1850 /* Try to open NAME in one of the directories in *DIRSP.
1851 Return the fd, or -1. If successful, fill in *REALNAME
1852 with the malloc'd full directory name. If it turns out
1853 that none of the directories in *DIRSP exists, *DIRSP is
1854 replaced with (void *) -1, and the old value is free()d
1855 if MAY_FREE_DIRS is true. */
1857 static int
1858 open_path (const char *name, size_t namelen, int mode,
1859 struct r_search_path_struct *sps, char **realname,
1860 struct filebuf *fbp, struct link_map *loader, int whatcode,
1861 bool *found_other_class)
1863 struct r_search_path_elem **dirs = sps->dirs;
1864 char *buf;
1865 int fd = -1;
1866 const char *current_what = NULL;
1867 int any = 0;
1869 if (__glibc_unlikely (dirs == NULL))
1870 /* We're called before _dl_init_paths when loading the main executable
1871 given on the command line when rtld is run directly. */
1872 return -1;
1874 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1877 struct r_search_path_elem *this_dir = *dirs;
1878 size_t buflen = 0;
1879 size_t cnt;
1880 char *edp;
1881 int here_any = 0;
1882 int err;
1884 /* If we are debugging the search for libraries print the path
1885 now if it hasn't happened now. */
1886 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)
1887 && current_what != this_dir->what)
1889 current_what = this_dir->what;
1890 print_search_path (dirs, current_what, this_dir->where);
1893 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1894 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1896 /* Skip this directory if we know it does not exist. */
1897 if (this_dir->status[cnt] == nonexisting)
1898 continue;
1900 #ifdef SHARED
1901 buflen =
1902 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1903 capstr[cnt].len),
1904 name, namelen)
1905 - buf);
1906 #else
1907 buflen = (char *) __mempcpy (edp, name, namelen) - buf;
1908 #endif
1910 /* Print name we try if this is wanted. */
1911 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1912 _dl_debug_printf (" trying file=%s\n", buf);
1914 fd = open_verify (buf, -1, fbp, loader, whatcode, mode,
1915 found_other_class, false);
1916 if (this_dir->status[cnt] == unknown)
1918 if (fd != -1)
1919 this_dir->status[cnt] = existing;
1920 /* Do not update the directory information when loading
1921 auditing code. We must try to disturb the program as
1922 little as possible. */
1923 else if (loader == NULL
1924 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0)
1926 /* We failed to open machine dependent library. Let's
1927 test whether there is any directory at all. */
1928 struct stat64 st;
1930 buf[buflen - namelen - 1] = '\0';
1932 if (__stat64 (buf, &st) != 0
1933 || ! S_ISDIR (st.st_mode))
1934 /* The directory does not exist or it is no directory. */
1935 this_dir->status[cnt] = nonexisting;
1936 else
1937 this_dir->status[cnt] = existing;
1941 /* Remember whether we found any existing directory. */
1942 here_any |= this_dir->status[cnt] != nonexisting;
1944 if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE)
1945 && __libc_enable_secure)
1947 /* This is an extra security effort to make sure nobody can
1948 preload broken shared objects which are in the trusted
1949 directories and so exploit the bugs. */
1950 struct stat64 st;
1952 if (__fstat64 (fd, &st) != 0
1953 || (st.st_mode & S_ISUID) == 0)
1955 /* The shared object cannot be tested for being SUID
1956 or this bit is not set. In this case we must not
1957 use this object. */
1958 __close_nocancel (fd);
1959 fd = -1;
1960 /* We simply ignore the file, signal this by setting
1961 the error value which would have been set by `open'. */
1962 errno = ENOENT;
1967 if (fd != -1)
1969 *realname = (char *) malloc (buflen);
1970 if (*realname != NULL)
1972 memcpy (*realname, buf, buflen);
1973 return fd;
1975 else
1977 /* No memory for the name, we certainly won't be able
1978 to load and link it. */
1979 __close_nocancel (fd);
1980 return -1;
1983 if (here_any && (err = errno) != ENOENT && err != EACCES)
1984 /* The file exists and is readable, but something went wrong. */
1985 return -1;
1987 /* Remember whether we found anything. */
1988 any |= here_any;
1990 while (*++dirs != NULL);
1992 /* Remove the whole path if none of the directories exists. */
1993 if (__glibc_unlikely (! any))
1995 /* Paths which were allocated using the minimal malloc() in ld.so
1996 must not be freed using the general free() in libc. */
1997 if (sps->malloced)
1998 free (sps->dirs);
2000 /* __rtld_search_dirs and __rtld_env_path_list are
2001 attribute_relro, therefore avoid writing to them. */
2002 if (sps != &__rtld_search_dirs && sps != &__rtld_env_path_list)
2003 sps->dirs = (void *) -1;
2006 return -1;
2009 /* Map in the shared object file NAME. */
2011 struct link_map *
2012 _dl_map_object (struct link_map *loader, const char *name,
2013 int type, int trace_mode, int mode, Lmid_t nsid)
2015 int fd;
2016 const char *origname = NULL;
2017 char *realname;
2018 char *name_copy;
2019 struct link_map *l;
2020 struct filebuf fb;
2022 assert (nsid >= 0);
2023 assert (nsid < GL(dl_nns));
2025 /* Look for this name among those already loaded. */
2026 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
2028 /* If the requested name matches the soname of a loaded object,
2029 use that object. Elide this check for names that have not
2030 yet been opened. */
2031 if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
2032 continue;
2033 if (!_dl_name_match_p (name, l))
2035 const char *soname;
2037 if (__glibc_likely (l->l_soname_added)
2038 || l->l_info[DT_SONAME] == NULL)
2039 continue;
2041 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
2042 + l->l_info[DT_SONAME]->d_un.d_val);
2043 if (strcmp (name, soname) != 0)
2044 continue;
2046 /* We have a match on a new name -- cache it. */
2047 add_name_to_object (l, soname);
2048 l->l_soname_added = 1;
2051 /* We have a match. */
2052 return l;
2055 /* Display information if we are debugging. */
2056 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
2057 && loader != NULL)
2058 _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0
2059 ? "\nfile=%s [%lu]; needed by %s [%lu]\n"
2060 : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n",
2061 name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns);
2063 #ifdef SHARED
2064 /* Give the auditing libraries a chance to change the name before we
2065 try anything. */
2066 if (__glibc_unlikely (GLRO(dl_naudit) > 0)
2067 && (loader == NULL || loader->l_auditing == 0))
2069 struct audit_ifaces *afct = GLRO(dl_audit);
2070 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2072 if (afct->objsearch != NULL)
2074 const char *before = name;
2075 struct auditstate *state = link_map_audit_state (loader, cnt);
2076 name = afct->objsearch (name, &state->cookie, LA_SER_ORIG);
2077 if (name == NULL)
2079 /* Do not try anything further. */
2080 fd = -1;
2081 goto no_file;
2083 if (before != name && strcmp (before, name) != 0)
2085 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
2086 _dl_debug_printf ("audit changed filename %s -> %s\n",
2087 before, name);
2089 if (origname == NULL)
2090 origname = before;
2094 afct = afct->next;
2097 #endif
2099 /* Will be true if we found a DSO which is of the other ELF class. */
2100 bool found_other_class = false;
2102 if (strchr (name, '/') == NULL)
2104 /* Search for NAME in several places. */
2106 size_t namelen = strlen (name) + 1;
2108 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2109 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
2111 fd = -1;
2113 /* When the object has the RUNPATH information we don't use any
2114 RPATHs. */
2115 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
2117 /* This is the executable's map (if there is one). Make sure that
2118 we do not look at it twice. */
2119 struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2120 bool did_main_map = false;
2122 /* First try the DT_RPATH of the dependent object that caused NAME
2123 to be loaded. Then that object's dependent, and on up. */
2124 for (l = loader; l; l = l->l_loader)
2125 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2127 fd = open_path (name, namelen, mode,
2128 &l->l_rpath_dirs,
2129 &realname, &fb, loader, LA_SER_RUNPATH,
2130 &found_other_class);
2131 if (fd != -1)
2132 break;
2134 did_main_map |= l == main_map;
2137 /* If dynamically linked, try the DT_RPATH of the executable
2138 itself. NB: we do this for lookups in any namespace. */
2139 if (fd == -1 && !did_main_map
2140 && main_map != NULL && main_map->l_type != lt_loaded
2141 && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH,
2142 "RPATH"))
2143 fd = open_path (name, namelen, mode,
2144 &main_map->l_rpath_dirs,
2145 &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
2146 &found_other_class);
2149 /* Try the LD_LIBRARY_PATH environment variable. */
2150 if (fd == -1 && __rtld_env_path_list.dirs != (void *) -1)
2151 fd = open_path (name, namelen, mode, &__rtld_env_path_list,
2152 &realname, &fb,
2153 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
2154 LA_SER_LIBPATH, &found_other_class);
2156 /* Look at the RUNPATH information for this binary. */
2157 if (fd == -1 && loader != NULL
2158 && cache_rpath (loader, &loader->l_runpath_dirs,
2159 DT_RUNPATH, "RUNPATH"))
2160 fd = open_path (name, namelen, mode,
2161 &loader->l_runpath_dirs, &realname, &fb, loader,
2162 LA_SER_RUNPATH, &found_other_class);
2164 if (fd == -1)
2166 realname = _dl_sysdep_open_object (name, namelen, &fd);
2167 if (realname != NULL)
2169 fd = open_verify (realname, fd,
2170 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2171 LA_SER_CONFIG, mode, &found_other_class,
2172 false);
2173 if (fd == -1)
2174 free (realname);
2178 #ifdef USE_LDCONFIG
2179 if (fd == -1
2180 && (__glibc_likely ((mode & __RTLD_SECURE) == 0)
2181 || ! __libc_enable_secure)
2182 && __glibc_likely (GLRO(dl_inhibit_cache) == 0))
2184 /* Check the list of libraries in the file /etc/ld.so.cache,
2185 for compatibility with Linux's ldconfig program. */
2186 char *cached = _dl_load_cache_lookup (name);
2188 if (cached != NULL)
2190 // XXX Correct to unconditionally default to namespace 0?
2191 l = (loader
2192 ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded
2193 # ifdef SHARED
2194 ?: &GL(dl_rtld_map)
2195 # endif
2198 /* If the loader has the DF_1_NODEFLIB flag set we must not
2199 use a cache entry from any of these directories. */
2200 if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB))
2202 const char *dirp = system_dirs;
2203 unsigned int cnt = 0;
2207 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2209 /* The prefix matches. Don't use the entry. */
2210 free (cached);
2211 cached = NULL;
2212 break;
2215 dirp += system_dirs_len[cnt] + 1;
2216 ++cnt;
2218 while (cnt < nsystem_dirs_len);
2221 if (cached != NULL)
2223 fd = open_verify (cached, -1,
2224 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2225 LA_SER_CONFIG, mode, &found_other_class,
2226 false);
2227 if (__glibc_likely (fd != -1))
2228 realname = cached;
2229 else
2230 free (cached);
2234 #endif
2236 /* Finally, try the default path. */
2237 if (fd == -1
2238 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2239 || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB)))
2240 && __rtld_search_dirs.dirs != (void *) -1)
2241 fd = open_path (name, namelen, mode, &__rtld_search_dirs,
2242 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
2244 /* Add another newline when we are tracing the library loading. */
2245 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2246 _dl_debug_printf ("\n");
2248 else
2250 /* The path may contain dynamic string tokens. */
2251 realname = (loader
2252 ? expand_dynamic_string_token (loader, name)
2253 : __strdup (name));
2254 if (realname == NULL)
2255 fd = -1;
2256 else
2258 fd = open_verify (realname, -1, &fb,
2259 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
2260 &found_other_class, true);
2261 if (__glibc_unlikely (fd == -1))
2262 free (realname);
2266 #ifdef SHARED
2267 no_file:
2268 #endif
2269 /* In case the LOADER information has only been provided to get to
2270 the appropriate RUNPATH/RPATH information we do not need it
2271 anymore. */
2272 if (mode & __RTLD_CALLMAP)
2273 loader = NULL;
2275 if (__glibc_unlikely (fd == -1))
2277 if (trace_mode
2278 && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0))
2280 /* We haven't found an appropriate library. But since we
2281 are only interested in the list of libraries this isn't
2282 so severe. Fake an entry with all the information we
2283 have. */
2284 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2286 /* Allocate a new object map. */
2287 if ((name_copy = __strdup (name)) == NULL
2288 || (l = _dl_new_object (name_copy, name, type, loader,
2289 mode, nsid)) == NULL)
2291 free (name_copy);
2292 _dl_signal_error (ENOMEM, name, NULL,
2293 N_("cannot create shared object descriptor"));
2295 /* Signal that this is a faked entry. */
2296 l->l_faked = 1;
2297 /* Since the descriptor is initialized with zero we do not
2298 have do this here.
2299 l->l_reserved = 0; */
2300 l->l_buckets = &dummy_bucket;
2301 l->l_nbuckets = 1;
2302 l->l_relocated = 1;
2304 /* Enter the object in the object list. */
2305 _dl_add_to_namespace_list (l, nsid);
2307 return l;
2309 else if (found_other_class)
2310 _dl_signal_error (0, name, NULL,
2311 ELFW(CLASS) == ELFCLASS32
2312 ? N_("wrong ELF class: ELFCLASS64")
2313 : N_("wrong ELF class: ELFCLASS32"));
2314 else
2315 _dl_signal_error (errno, name, NULL,
2316 N_("cannot open shared object file"));
2319 void *stack_end = __libc_stack_end;
2320 return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader,
2321 type, mode, &stack_end, nsid);
2324 struct add_path_state
2326 bool counting;
2327 unsigned int idx;
2328 Dl_serinfo *si;
2329 char *allocptr;
2332 static void
2333 add_path (struct add_path_state *p, const struct r_search_path_struct *sps,
2334 unsigned int flags)
2336 if (sps->dirs != (void *) -1)
2338 struct r_search_path_elem **dirs = sps->dirs;
2341 const struct r_search_path_elem *const r = *dirs++;
2342 if (p->counting)
2344 p->si->dls_cnt++;
2345 p->si->dls_size += MAX (2, r->dirnamelen);
2347 else
2349 Dl_serpath *const sp = &p->si->dls_serpath[p->idx++];
2350 sp->dls_name = p->allocptr;
2351 if (r->dirnamelen < 2)
2352 *p->allocptr++ = r->dirnamelen ? '/' : '.';
2353 else
2354 p->allocptr = __mempcpy (p->allocptr,
2355 r->dirname, r->dirnamelen - 1);
2356 *p->allocptr++ = '\0';
2357 sp->dls_flags = flags;
2360 while (*dirs != NULL);
2364 void
2365 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2367 if (counting)
2369 si->dls_cnt = 0;
2370 si->dls_size = 0;
2373 struct add_path_state p =
2375 .counting = counting,
2376 .idx = 0,
2377 .si = si,
2378 .allocptr = (char *) &si->dls_serpath[si->dls_cnt]
2381 # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */
2383 /* When the object has the RUNPATH information we don't use any RPATHs. */
2384 if (loader->l_info[DT_RUNPATH] == NULL)
2386 /* First try the DT_RPATH of the dependent object that caused NAME
2387 to be loaded. Then that object's dependent, and on up. */
2389 struct link_map *l = loader;
2392 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2393 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2394 l = l->l_loader;
2396 while (l != NULL);
2398 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2399 if (loader->l_ns == LM_ID_BASE)
2401 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2402 if (l != NULL && l->l_type != lt_loaded && l != loader)
2403 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2404 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
2408 /* Try the LD_LIBRARY_PATH environment variable. */
2409 add_path (&p, &__rtld_env_path_list, XXX_ENV);
2411 /* Look at the RUNPATH information for this binary. */
2412 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2413 add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH);
2415 /* XXX
2416 Here is where ld.so.cache gets checked, but we don't have
2417 a way to indicate that in the results for Dl_serinfo. */
2419 /* Finally, try the default path. */
2420 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2421 add_path (&p, &__rtld_search_dirs, XXX_default);
2423 if (counting)
2424 /* Count the struct size before the string area, which we didn't
2425 know before we completed dls_cnt. */
2426 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;