2.3.4-9
[glibc.git] / elf / dl-load.c
blob088b2224e2631b8f881484a9f2c674e68fc75bfe
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <libintl.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <ldsodefs.h>
29 #include <bits/wordsize.h>
30 #include <sys/mman.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include "dynamic-link.h"
35 #include <abi-tag.h>
36 #include <dl-osinfo.h>
37 #include <stackinfo.h>
38 #include <caller.h>
39 #include <sysdep.h>
41 #include <dl-dst.h>
43 /* On some systems, no flag bits are given to specify file mapping. */
44 #ifndef MAP_FILE
45 # define MAP_FILE 0
46 #endif
48 /* The right way to map in the shared library files is MAP_COPY, which
49 makes a virtual copy of the data at the time of the mmap call; this
50 guarantees the mapped pages will be consistent even if the file is
51 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
52 get is MAP_PRIVATE, which copies each page when it is modified; this
53 means if the file is overwritten, we may at some point get some pages
54 from the new version after starting with pages from the old version. */
55 #ifndef MAP_COPY
56 # define MAP_COPY MAP_PRIVATE
57 #endif
59 /* We want to prevent people from modifying DSOs which are currently in
60 use. This is what MAP_DENYWRITE is for. */
61 #ifndef MAP_DENYWRITE
62 # define MAP_DENYWRITE 0
63 #endif
65 /* Some systems link their relocatable objects for another base address
66 than 0. We want to know the base address for these such that we can
67 subtract this address from the segment addresses during mapping.
68 This results in a more efficient address space usage. Defaults to
69 zero for almost all systems. */
70 #ifndef MAP_BASE_ADDR
71 # define MAP_BASE_ADDR(l) 0
72 #endif
75 #include <endian.h>
76 #if BYTE_ORDER == BIG_ENDIAN
77 # define byteorder ELFDATA2MSB
78 #elif BYTE_ORDER == LITTLE_ENDIAN
79 # define byteorder ELFDATA2LSB
80 #else
81 # error "Unknown BYTE_ORDER " BYTE_ORDER
82 # define byteorder ELFDATANONE
83 #endif
85 #define STRING(x) __STRING (x)
87 #ifdef MAP_ANON
88 /* The fd is not examined when using MAP_ANON. */
89 # define ANONFD -1
90 #else
91 int _dl_zerofd = -1;
92 # define ANONFD _dl_zerofd
93 #endif
95 /* Handle situations where we have a preferred location in memory for
96 the shared objects. */
97 #ifdef ELF_PREFERRED_ADDRESS_DATA
98 ELF_PREFERRED_ADDRESS_DATA;
99 #endif
100 #ifndef ELF_PREFERRED_ADDRESS
101 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
102 #endif
103 #ifndef ELF_FIXED_ADDRESS
104 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
105 #endif
108 int __stack_prot attribute_hidden attribute_relro
109 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
110 = PROT_GROWSDOWN;
111 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
112 = PROT_GROWSUP;
113 #endif
116 /* Type for the buffer we put the ELF header and hopefully the program
117 header. This buffer does not really have to be too large. In most
118 cases the program header follows the ELF header directly. If this
119 is not the case all bets are off and we can make the header
120 arbitrarily large and still won't get it read. This means the only
121 question is how large are the ELF and program header combined. The
122 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
123 bytes long. Each program header entry is again 32 and 56 bytes
124 long respectively. I.e., even with a file which has 7 program
125 header entries we only have to read 512B. Add to this a bit of
126 margin for program notes and reading 512B and 640B for 32-bit and
127 64-bit files respecitvely is enough. If this heuristic should
128 really fail for some file the code in `_dl_map_object_from_fd'
129 knows how to recover. */
130 struct filebuf
132 ssize_t len;
133 #if __WORDSIZE == 32
134 # define FILEBUF_SIZE 512
135 #else
136 # define FILEBUF_SIZE 640
137 #endif
138 char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr)))));
141 /* This is the decomposed LD_LIBRARY_PATH search path. */
142 static struct r_search_path_struct env_path_list attribute_relro;
144 /* List of the hardware capabilities we might end up using. */
145 static const struct r_strlenpair *capstr attribute_relro;
146 static size_t ncapstr attribute_relro;
147 static size_t max_capstrlen attribute_relro;
150 /* Get the generated information about the trusted directories. */
151 #include "trusted-dirs.h"
153 static const char system_dirs[] = SYSTEM_DIRS;
154 static const size_t system_dirs_len[] =
156 SYSTEM_DIRS_LEN
158 #define nsystem_dirs_len \
159 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
162 /* Local version of `strdup' function. */
163 static inline char *
164 local_strdup (const char *s)
166 size_t len = strlen (s) + 1;
167 void *new = malloc (len);
169 if (new == NULL)
170 return NULL;
172 return (char *) memcpy (new, s, len);
176 static size_t
177 is_dst (const char *start, const char *name, const char *str,
178 int is_path, int secure)
180 size_t len;
181 bool is_curly = false;
183 if (name[0] == '{')
185 is_curly = true;
186 ++name;
189 len = 0;
190 while (name[len] == str[len] && name[len] != '\0')
191 ++len;
193 if (is_curly)
195 if (name[len] != '}')
196 return 0;
198 /* Point again at the beginning of the name. */
199 --name;
200 /* Skip over closing curly brace and adjust for the --name. */
201 len += 2;
203 else if (name[len] != '\0' && name[len] != '/'
204 && (!is_path || name[len] != ':'))
205 return 0;
207 if (__builtin_expect (secure, 0)
208 && ((name[len] != '\0' && (!is_path || name[len] != ':'))
209 || (name != start + 1 && (!is_path || name[-2] != ':'))))
210 return 0;
212 return len;
216 size_t
217 _dl_dst_count (const char *name, int is_path)
219 const char *const start = name;
220 size_t cnt = 0;
224 size_t len;
226 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
227 is $ORIGIN alone) and it must always appear first in path. */
228 ++name;
229 if ((len = is_dst (start, name, "ORIGIN", is_path,
230 INTUSE(__libc_enable_secure))) != 0
231 || (len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0
232 || (len = is_dst (start, name, "LIB", is_path, 0)) != 0)
233 ++cnt;
235 name = strchr (name + len, '$');
237 while (name != NULL);
239 return cnt;
243 char *
244 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
245 int is_path)
247 const char *const start = name;
248 char *last_elem, *wp;
250 /* Now fill the result path. While copying over the string we keep
251 track of the start of the last path element. When we come accross
252 a DST we copy over the value or (if the value is not available)
253 leave the entire path element out. */
254 last_elem = wp = result;
258 if (__builtin_expect (*name == '$', 0))
260 const char *repl = NULL;
261 size_t len;
263 ++name;
264 if ((len = is_dst (start, name, "ORIGIN", is_path,
265 INTUSE(__libc_enable_secure))) != 0)
266 repl = l->l_origin;
267 else if ((len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0)
268 repl = GLRO(dl_platform);
269 else if ((len = is_dst (start, name, "LIB", is_path, 0)) != 0)
270 repl = DL_DST_LIB;
272 if (repl != NULL && repl != (const char *) -1)
274 wp = __stpcpy (wp, repl);
275 name += len;
277 else if (len > 1)
279 /* We cannot use this path element, the value of the
280 replacement is unknown. */
281 wp = last_elem;
282 name += len;
283 while (*name != '\0' && (!is_path || *name != ':'))
284 ++name;
286 else
287 /* No DST we recognize. */
288 *wp++ = '$';
290 else
292 *wp++ = *name++;
293 if (is_path && *name == ':')
294 last_elem = wp;
297 while (*name != '\0');
299 *wp = '\0';
301 return result;
305 /* Return copy of argument with all recognized dynamic string tokens
306 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
307 might not be possible to determine the path from which the object
308 belonging to the map is loaded. In this case the path element
309 containing $ORIGIN is left out. */
310 static char *
311 expand_dynamic_string_token (struct link_map *l, const char *s)
313 /* We make two runs over the string. First we determine how large the
314 resulting string is and then we copy it over. Since this is now
315 frequently executed operation we are looking here not for performance
316 but rather for code size. */
317 size_t cnt;
318 size_t total;
319 char *result;
321 /* Determine the number of DST elements. */
322 cnt = DL_DST_COUNT (s, 1);
324 /* If we do not have to replace anything simply copy the string. */
325 if (__builtin_expect (cnt, 0) == 0)
326 return local_strdup (s);
328 /* Determine the length of the substituted string. */
329 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
331 /* Allocate the necessary memory. */
332 result = (char *) malloc (total + 1);
333 if (result == NULL)
334 return NULL;
336 return _dl_dst_substitute (l, s, result, 1);
340 /* Add `name' to the list of names for a particular shared object.
341 `name' is expected to have been allocated with malloc and will
342 be freed if the shared object already has this name.
343 Returns false if the object already had this name. */
344 static void
345 internal_function
346 add_name_to_object (struct link_map *l, const char *name)
348 struct libname_list *lnp, *lastp;
349 struct libname_list *newname;
350 size_t name_len;
352 lastp = NULL;
353 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
354 if (strcmp (name, lnp->name) == 0)
355 return;
357 name_len = strlen (name) + 1;
358 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
359 if (newname == NULL)
361 /* No more memory. */
362 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
363 return;
365 /* The object should have a libname set from _dl_new_object. */
366 assert (lastp != NULL);
368 newname->name = memcpy (newname + 1, name, name_len);
369 newname->next = NULL;
370 newname->dont_free = 0;
371 lastp->next = newname;
374 /* Standard search directories. */
375 static struct r_search_path_struct rtld_search_dirs attribute_relro;
377 static size_t max_dirnamelen;
379 static struct r_search_path_elem **
380 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
381 int check_trusted, const char *what, const char *where)
383 char *cp;
384 size_t nelems = 0;
386 while ((cp = __strsep (&rpath, sep)) != NULL)
388 struct r_search_path_elem *dirp;
389 size_t len = strlen (cp);
391 /* `strsep' can pass an empty string. This has to be
392 interpreted as `use the current directory'. */
393 if (len == 0)
395 static const char curwd[] = "./";
396 cp = (char *) curwd;
399 /* Remove trailing slashes (except for "/"). */
400 while (len > 1 && cp[len - 1] == '/')
401 --len;
403 /* Now add one if there is none so far. */
404 if (len > 0 && cp[len - 1] != '/')
405 cp[len++] = '/';
407 /* Make sure we don't use untrusted directories if we run SUID. */
408 if (__builtin_expect (check_trusted, 0))
410 const char *trun = system_dirs;
411 size_t idx;
412 int unsecure = 1;
414 /* All trusted directories must be complete names. */
415 if (cp[0] == '/')
417 for (idx = 0; idx < nsystem_dirs_len; ++idx)
419 if (len == system_dirs_len[idx]
420 && memcmp (trun, cp, len) == 0)
422 /* Found it. */
423 unsecure = 0;
424 break;
427 trun += system_dirs_len[idx] + 1;
431 if (unsecure)
432 /* Simply drop this directory. */
433 continue;
436 /* See if this directory is already known. */
437 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
438 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
439 break;
441 if (dirp != NULL)
443 /* It is available, see whether it's on our own list. */
444 size_t cnt;
445 for (cnt = 0; cnt < nelems; ++cnt)
446 if (result[cnt] == dirp)
447 break;
449 if (cnt == nelems)
450 result[nelems++] = dirp;
452 else
454 size_t cnt;
455 enum r_dir_status init_val;
456 size_t where_len = where ? strlen (where) + 1 : 0;
458 /* It's a new directory. Create an entry and add it. */
459 dirp = (struct r_search_path_elem *)
460 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
461 + where_len + len + 1);
462 if (dirp == NULL)
463 _dl_signal_error (ENOMEM, NULL, NULL,
464 N_("cannot create cache for search path"));
466 dirp->dirname = ((char *) dirp + sizeof (*dirp)
467 + ncapstr * sizeof (enum r_dir_status));
468 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
469 dirp->dirnamelen = len;
471 if (len > max_dirnamelen)
472 max_dirnamelen = len;
474 /* We have to make sure all the relative directories are
475 never ignored. The current directory might change and
476 all our saved information would be void. */
477 init_val = cp[0] != '/' ? existing : unknown;
478 for (cnt = 0; cnt < ncapstr; ++cnt)
479 dirp->status[cnt] = init_val;
481 dirp->what = what;
482 if (__builtin_expect (where != NULL, 1))
483 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
484 + (ncapstr * sizeof (enum r_dir_status)),
485 where, where_len);
486 else
487 dirp->where = NULL;
489 dirp->next = GL(dl_all_dirs);
490 GL(dl_all_dirs) = dirp;
492 /* Put it in the result array. */
493 result[nelems++] = dirp;
497 /* Terminate the array. */
498 result[nelems] = NULL;
500 return result;
504 static void
505 internal_function
506 decompose_rpath (struct r_search_path_struct *sps,
507 const char *rpath, struct link_map *l, const char *what)
509 /* Make a copy we can work with. */
510 const char *where = l->l_name;
511 char *copy;
512 char *cp;
513 struct r_search_path_elem **result;
514 size_t nelems;
515 /* Initialize to please the compiler. */
516 const char *errstring = NULL;
518 /* First see whether we must forget the RUNPATH and RPATH from this
519 object. */
520 if (__builtin_expect (GLRO(dl_inhibit_rpath) != NULL, 0)
521 && !INTUSE(__libc_enable_secure))
523 const char *inhp = GLRO(dl_inhibit_rpath);
527 const char *wp = where;
529 while (*inhp == *wp && *wp != '\0')
531 ++inhp;
532 ++wp;
535 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
537 /* This object is on the list of objects for which the
538 RUNPATH and RPATH must not be used. */
539 result = calloc (1, sizeof *result);
540 if (result == NULL)
542 signal_error_cache:
543 errstring = N_("cannot create cache for search path");
544 signal_error:
545 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
548 sps->dirs = result;
549 sps->malloced = 1;
551 return;
554 while (*inhp != '\0')
555 if (*inhp++ == ':')
556 break;
558 while (*inhp != '\0');
561 /* Make a writable copy. At the same time expand possible dynamic
562 string tokens. */
563 copy = expand_dynamic_string_token (l, rpath);
564 if (copy == NULL)
566 errstring = N_("cannot create RUNPATH/RPATH copy");
567 goto signal_error;
570 /* Count the number of necessary elements in the result array. */
571 nelems = 0;
572 for (cp = copy; *cp != '\0'; ++cp)
573 if (*cp == ':')
574 ++nelems;
576 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
577 number of necessary entries. */
578 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
579 * sizeof (*result));
580 if (result == NULL)
581 goto signal_error_cache;
583 fillin_rpath (copy, result, ":", 0, what, where);
585 /* Free the copied RPATH string. `fillin_rpath' make own copies if
586 necessary. */
587 free (copy);
589 sps->dirs = result;
590 /* The caller will change this value if we haven't used a real malloc. */
591 sps->malloced = 1;
594 /* Make sure cached path information is stored in *SP
595 and return true if there are any paths to search there. */
596 static bool
597 cache_rpath (struct link_map *l,
598 struct r_search_path_struct *sp,
599 int tag,
600 const char *what)
602 if (sp->dirs == (void *) -1)
603 return false;
605 if (sp->dirs != NULL)
606 return true;
608 if (l->l_info[tag] == NULL)
610 /* There is no path. */
611 sp->dirs = (void *) -1;
612 return false;
615 /* Make sure the cache information is available. */
616 decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
617 + l->l_info[tag]->d_un.d_val),
618 l, what);
619 return true;
623 void
624 internal_function
625 _dl_init_paths (const char *llp)
627 size_t idx;
628 const char *strp;
629 struct r_search_path_elem *pelem, **aelem;
630 size_t round_size;
631 #ifdef SHARED
632 struct link_map *l;
633 #endif
634 /* Initialize to please the compiler. */
635 const char *errstring = NULL;
637 /* Fill in the information about the application's RPATH and the
638 directories addressed by the LD_LIBRARY_PATH environment variable. */
640 /* Get the capabilities. */
641 capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen),
642 &ncapstr, &max_capstrlen);
644 /* First set up the rest of the default search directory entries. */
645 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
646 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
647 if (rtld_search_dirs.dirs == NULL)
649 errstring = N_("cannot create search path array");
650 signal_error:
651 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
654 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
655 + ncapstr * sizeof (enum r_dir_status))
656 / sizeof (struct r_search_path_elem));
658 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
659 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
660 * round_size * sizeof (struct r_search_path_elem));
661 if (rtld_search_dirs.dirs[0] == NULL)
663 errstring = N_("cannot create cache for search path");
664 goto signal_error;
667 rtld_search_dirs.malloced = 0;
668 pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0];
669 strp = system_dirs;
670 idx = 0;
674 size_t cnt;
676 *aelem++ = pelem;
678 pelem->what = "system search path";
679 pelem->where = NULL;
681 pelem->dirname = strp;
682 pelem->dirnamelen = system_dirs_len[idx];
683 strp += system_dirs_len[idx] + 1;
685 /* System paths must be absolute. */
686 assert (pelem->dirname[0] == '/');
687 for (cnt = 0; cnt < ncapstr; ++cnt)
688 pelem->status[cnt] = unknown;
690 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
692 pelem += round_size;
694 while (idx < nsystem_dirs_len);
696 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
697 *aelem = NULL;
699 #ifdef SHARED
700 /* This points to the map of the main object. */
701 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
702 if (l != NULL)
704 assert (l->l_type != lt_loaded);
706 if (l->l_info[DT_RUNPATH])
708 /* Allocate room for the search path and fill in information
709 from RUNPATH. */
710 decompose_rpath (&l->l_runpath_dirs,
711 (const void *) (D_PTR (l, l_info[DT_STRTAB])
712 + l->l_info[DT_RUNPATH]->d_un.d_val),
713 l, "RUNPATH");
715 /* The RPATH is ignored. */
716 l->l_rpath_dirs.dirs = (void *) -1;
718 else
720 l->l_runpath_dirs.dirs = (void *) -1;
722 if (l->l_info[DT_RPATH])
724 /* Allocate room for the search path and fill in information
725 from RPATH. */
726 decompose_rpath (&l->l_rpath_dirs,
727 (const void *) (D_PTR (l, l_info[DT_STRTAB])
728 + l->l_info[DT_RPATH]->d_un.d_val),
729 l, "RPATH");
730 l->l_rpath_dirs.malloced = 0;
732 else
733 l->l_rpath_dirs.dirs = (void *) -1;
736 #endif /* SHARED */
738 if (llp != NULL && *llp != '\0')
740 size_t nllp;
741 const char *cp = llp;
742 char *llp_tmp = strdupa (llp);
744 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
745 elements it has. */
746 nllp = 1;
747 while (*cp)
749 if (*cp == ':' || *cp == ';')
750 ++nllp;
751 ++cp;
754 env_path_list.dirs = (struct r_search_path_elem **)
755 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
756 if (env_path_list.dirs == NULL)
758 errstring = N_("cannot create cache for search path");
759 goto signal_error;
762 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
763 INTUSE(__libc_enable_secure), "LD_LIBRARY_PATH",
764 NULL);
766 if (env_path_list.dirs[0] == NULL)
768 free (env_path_list.dirs);
769 env_path_list.dirs = (void *) -1;
772 env_path_list.malloced = 0;
774 else
775 env_path_list.dirs = (void *) -1;
777 /* Remember the last search directory added at startup. */
778 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
782 static void
783 __attribute__ ((noreturn, noinline))
784 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
785 const char *msg)
787 /* The file might already be closed. */
788 if (fd != -1)
789 (void) __close (fd);
790 if (l != NULL)
792 /* Remove the stillborn object from the list and free it. */
793 assert (l->l_next == NULL);
794 if (l->l_prev == NULL)
795 /* No other module loaded. This happens only in the static library,
796 or in rtld under --verify. */
797 GL(dl_ns)[l->l_ns]._ns_loaded = NULL;
798 else
799 l->l_prev->l_next = NULL;
800 --GL(dl_ns)[l->l_ns]._ns_nloaded;
801 free (l);
803 free (realname);
804 _dl_signal_error (code, name, NULL, msg);
808 /* Map in the shared object NAME, actually located in REALNAME, and already
809 opened on FD. */
811 #ifndef EXTERNAL_MAP_FROM_FD
812 static
813 #endif
814 struct link_map *
815 _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
816 char *realname, struct link_map *loader, int l_type,
817 int mode, void **stack_endp, Lmid_t nsid)
819 struct link_map *l = NULL;
820 const ElfW(Ehdr) *header;
821 const ElfW(Phdr) *phdr;
822 const ElfW(Phdr) *ph;
823 size_t maplength;
824 int type;
825 struct stat64 st;
826 /* Initialize to keep the compiler happy. */
827 const char *errstring = NULL;
828 int errval = 0;
829 struct r_debug *r = _dl_debug_initialize (0, nsid);
830 bool make_consistent = false;
832 /* Get file information. */
833 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st) < 0, 0))
835 errstring = N_("cannot stat shared object");
836 call_lose_errno:
837 errval = errno;
838 call_lose:
839 if (make_consistent)
841 r->r_state = RT_CONSISTENT;
842 _dl_debug_state ();
845 lose (errval, fd, name, realname, l, errstring);
848 /* Look again to see if the real name matched another already loaded. */
849 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
850 if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
852 /* The object is already loaded.
853 Just bump its reference count and return it. */
854 __close (fd);
856 /* If the name is not in the list of names for this object add
857 it. */
858 free (realname);
859 add_name_to_object (l, name);
861 return l;
864 #ifdef SHARED
865 /* When loading into a namespace other than the base one we must
866 avoid loading ld.so since there can only be one copy. Ever. */
867 if (__builtin_expect (nsid != LM_ID_BASE, 0)
868 && ((st.st_ino == GL(dl_rtld_map).l_ino
869 && st.st_dev == GL(dl_rtld_map).l_dev)
870 || _dl_name_match_p (name, &GL(dl_rtld_map))))
872 /* This is indeed ld.so. Create a new link_map which refers to
873 the real one for almost everything. */
874 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
875 if (l == NULL)
876 goto fail_new;
878 /* Refer to the real descriptor. */
879 l->l_real = &GL(dl_rtld_map);
881 /* No need to bump the refcount of the real object, ld.so will
882 never be unloaded. */
883 __close (fd);
885 return l;
887 #endif
889 if (mode & RTLD_NOLOAD)
890 /* We are not supposed to load the object unless it is already
891 loaded. So return now. */
892 return NULL;
894 /* Print debugging message. */
895 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
896 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
898 /* This is the ELF header. We read it in `open_verify'. */
899 header = (void *) fbp->buf;
901 #ifndef MAP_ANON
902 # define MAP_ANON 0
903 if (_dl_zerofd == -1)
905 _dl_zerofd = _dl_sysdep_open_zero_fill ();
906 if (_dl_zerofd == -1)
908 __close (fd);
909 _dl_signal_error (errno, NULL, NULL,
910 N_("cannot open zero fill device"));
913 #endif
915 /* Signal that we are going to add new objects. */
916 if (r->r_state == RT_CONSISTENT)
918 #ifdef SHARED
919 /* Auditing checkpoint: we are going to add new objects. */
920 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
922 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
923 /* Do not call the functions for any auditing object. */
924 if (head->l_auditing == 0)
926 struct audit_ifaces *afct = GLRO(dl_audit);
927 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
929 if (afct->activity != NULL)
930 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD);
932 afct = afct->next;
936 #endif
938 /* Notify the debugger we have added some objects. We need to
939 call _dl_debug_initialize in a static program in case dynamic
940 linking has not been used before. */
941 r->r_state = RT_ADD;
942 _dl_debug_state ();
943 make_consistent = true;
945 else
946 assert (r->r_state == RT_ADD);
948 /* Enter the new object in the list of loaded objects. */
949 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
950 if (__builtin_expect (l == NULL, 0))
952 #ifdef SHARED
953 fail_new:
954 #endif
955 errstring = N_("cannot create shared object descriptor");
956 goto call_lose_errno;
959 /* Extract the remaining details we need from the ELF header
960 and then read in the program header table. */
961 l->l_entry = header->e_entry;
962 type = header->e_type;
963 l->l_phnum = header->e_phnum;
965 maplength = header->e_phnum * sizeof (ElfW(Phdr));
966 if (header->e_phoff + maplength <= (size_t) fbp->len)
967 phdr = (void *) (fbp->buf + header->e_phoff);
968 else
970 phdr = alloca (maplength);
971 __lseek (fd, header->e_phoff, SEEK_SET);
972 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
974 errstring = N_("cannot read file data");
975 goto call_lose_errno;
979 /* Presumed absent PT_GNU_STACK. */
980 uint_fast16_t stack_flags = PF_R|PF_W|PF_X;
983 /* Scan the program header table, collecting its load commands. */
984 struct loadcmd
986 ElfW(Addr) mapstart, mapend, dataend, allocend;
987 off_t mapoff;
988 int prot;
989 } loadcmds[l->l_phnum], *c;
990 size_t nloadcmds = 0;
991 bool has_holes = false;
993 /* The struct is initialized to zero so this is not necessary:
994 l->l_ld = 0;
995 l->l_phdr = 0;
996 l->l_addr = 0; */
997 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
998 switch (ph->p_type)
1000 /* These entries tell us where to find things once the file's
1001 segments are mapped in. We record the addresses it says
1002 verbatim, and later correct for the run-time load address. */
1003 case PT_DYNAMIC:
1004 l->l_ld = (void *) ph->p_vaddr;
1005 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1006 break;
1008 case PT_PHDR:
1009 l->l_phdr = (void *) ph->p_vaddr;
1010 break;
1012 case PT_LOAD:
1013 /* A load command tells us to map in part of the file.
1014 We record the load commands and process them all later. */
1015 if (__builtin_expect ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0,
1018 errstring = N_("ELF load command alignment not page-aligned");
1019 goto call_lose;
1021 if (__builtin_expect (((ph->p_vaddr - ph->p_offset)
1022 & (ph->p_align - 1)) != 0, 0))
1024 errstring
1025 = N_("ELF load command address/offset not properly aligned");
1026 goto call_lose;
1029 c = &loadcmds[nloadcmds++];
1030 c->mapstart = ph->p_vaddr & ~(GLRO(dl_pagesize) - 1);
1031 c->mapend = ((ph->p_vaddr + ph->p_filesz + GLRO(dl_pagesize) - 1)
1032 & ~(GLRO(dl_pagesize) - 1));
1033 c->dataend = ph->p_vaddr + ph->p_filesz;
1034 c->allocend = ph->p_vaddr + ph->p_memsz;
1035 c->mapoff = ph->p_offset & ~(GLRO(dl_pagesize) - 1);
1037 /* Determine whether there is a gap between the last segment
1038 and this one. */
1039 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1040 has_holes = true;
1042 /* Optimize a common case. */
1043 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1044 c->prot = (PF_TO_PROT
1045 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1046 #else
1047 c->prot = 0;
1048 if (ph->p_flags & PF_R)
1049 c->prot |= PROT_READ;
1050 if (ph->p_flags & PF_W)
1051 c->prot |= PROT_WRITE;
1052 if (ph->p_flags & PF_X)
1053 c->prot |= PROT_EXEC;
1054 #endif
1055 break;
1057 case PT_TLS:
1058 #ifdef USE_TLS
1059 if (ph->p_memsz == 0)
1060 /* Nothing to do for an empty segment. */
1061 break;
1063 l->l_tls_blocksize = ph->p_memsz;
1064 l->l_tls_align = ph->p_align;
1065 if (ph->p_align == 0)
1066 l->l_tls_firstbyte_offset = 0;
1067 else
1068 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1069 l->l_tls_initimage_size = ph->p_filesz;
1070 /* Since we don't know the load address yet only store the
1071 offset. We will adjust it later. */
1072 l->l_tls_initimage = (void *) ph->p_vaddr;
1074 /* If not loading the initial set of shared libraries,
1075 check whether we should permit loading a TLS segment. */
1076 if (__builtin_expect (l->l_type == lt_library, 1)
1077 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1078 not set up TLS data structures, so don't use them now. */
1079 || __builtin_expect (GL(dl_tls_dtv_slotinfo_list) != NULL, 1))
1081 /* Assign the next available module ID. */
1082 l->l_tls_modid = _dl_next_tls_modid ();
1083 break;
1086 # ifdef SHARED
1087 if (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0)
1088 /* We are loading the executable itself when the dynamic linker
1089 was executed directly. The setup will happen later. */
1090 break;
1092 /* In a static binary there is no way to tell if we dynamically
1093 loaded libpthread. */
1094 if (GL(dl_error_catch_tsd) == &_dl_initial_error_catch_tsd)
1095 # endif
1097 /* We have not yet loaded libpthread.
1098 We can do the TLS setup right now! */
1100 void *tcb;
1102 /* The first call allocates TLS bookkeeping data structures.
1103 Then we allocate the TCB for the initial thread. */
1104 if (__builtin_expect (_dl_tls_setup (), 0)
1105 || __builtin_expect ((tcb = _dl_allocate_tls (NULL)) == NULL,
1108 errval = ENOMEM;
1109 errstring = N_("\
1110 cannot allocate TLS data structures for initial thread");
1111 goto call_lose;
1114 /* Now we install the TCB in the thread register. */
1115 errstring = TLS_INIT_TP (tcb, 0);
1116 if (__builtin_expect (errstring == NULL, 1))
1118 /* Now we are all good. */
1119 l->l_tls_modid = ++GL(dl_tls_max_dtv_idx);
1120 break;
1123 /* The kernel is too old or somesuch. */
1124 errval = 0;
1125 _dl_deallocate_tls (tcb, 1);
1126 goto call_lose;
1128 #endif
1130 /* Uh-oh, the binary expects TLS support but we cannot
1131 provide it. */
1132 errval = 0;
1133 errstring = N_("cannot handle TLS data");
1134 goto call_lose;
1135 break;
1137 case PT_GNU_STACK:
1138 stack_flags = ph->p_flags;
1139 break;
1141 case PT_GNU_RELRO:
1142 l->l_relro_addr = ph->p_vaddr;
1143 l->l_relro_size = ph->p_memsz;
1144 break;
1147 if (__builtin_expect (nloadcmds == 0, 0))
1149 /* This only happens for a bogus object that will be caught with
1150 another error below. But we don't want to go through the
1151 calculations below using NLOADCMDS - 1. */
1152 errstring = N_("object file has no loadable segments");
1153 goto call_lose;
1156 /* Now process the load commands and map segments into memory. */
1157 c = loadcmds;
1159 /* Length of the sections to be loaded. */
1160 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
1162 if (__builtin_expect (type, ET_DYN) == ET_DYN)
1164 /* This is a position-independent shared object. We can let the
1165 kernel map it anywhere it likes, but we must have space for all
1166 the segments in their specified positions relative to the first.
1167 So we map the first segment without MAP_FIXED, but with its
1168 extent increased to cover all the segments. Then we remove
1169 access from excess portion, and there is known sufficient space
1170 there to remap from the later segments.
1172 As a refinement, sometimes we have an address that we would
1173 prefer to map such objects at; but this is only a preference,
1174 the OS can do whatever it likes. */
1175 ElfW(Addr) mappref;
1176 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength,
1177 c->mapstart & GLRO(dl_use_load_bias))
1178 - MAP_BASE_ADDR (l));
1180 /* Remember which part of the address space this object uses. */
1181 l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
1182 c->prot,
1183 MAP_COPY|MAP_FILE|MAP_DENYWRITE,
1184 fd, c->mapoff);
1185 if (__builtin_expect ((void *) l->l_map_start == MAP_FAILED, 0))
1187 map_error:
1188 errstring = N_("failed to map segment from shared object");
1189 goto call_lose_errno;
1192 l->l_map_end = l->l_map_start + maplength;
1193 l->l_addr = l->l_map_start - c->mapstart;
1195 if (has_holes)
1196 /* Change protection on the excess portion to disallow all access;
1197 the portions we do not remap later will be inaccessible as if
1198 unallocated. Then jump into the normal segment-mapping loop to
1199 handle the portion of the segment past the end of the file
1200 mapping. */
1201 __mprotect ((caddr_t) (l->l_addr + c->mapend),
1202 loadcmds[nloadcmds - 1].allocend - c->mapend,
1203 PROT_NONE);
1205 goto postmap;
1208 /* This object is loaded at a fixed address. This must never
1209 happen for objects loaded with dlopen(). */
1210 if (__builtin_expect ((mode & __RTLD_OPENEXEC) == 0, 0))
1212 errstring = N_("cannot dynamically load executable");
1213 goto call_lose;
1216 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
1217 fixed. */
1218 ELF_FIXED_ADDRESS (loader, c->mapstart);
1221 /* Remember which part of the address space this object uses. */
1222 l->l_map_start = c->mapstart + l->l_addr;
1223 l->l_map_end = l->l_map_start + maplength;
1225 while (c < &loadcmds[nloadcmds])
1227 if (c->mapend > c->mapstart
1228 /* Map the segment contents from the file. */
1229 && (__mmap ((void *) (l->l_addr + c->mapstart),
1230 c->mapend - c->mapstart, c->prot,
1231 MAP_FIXED|MAP_COPY|MAP_FILE|MAP_DENYWRITE,
1232 fd, c->mapoff)
1233 == MAP_FAILED))
1234 goto map_error;
1236 postmap:
1237 if (c->prot & PROT_EXEC)
1238 l->l_text_end = l->l_addr + c->mapend;
1240 if (l->l_phdr == 0
1241 && (ElfW(Off)) c->mapoff <= header->e_phoff
1242 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
1243 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1244 /* Found the program header in this segment. */
1245 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
1247 if (c->allocend > c->dataend)
1249 /* Extra zero pages should appear at the end of this segment,
1250 after the data mapped from the file. */
1251 ElfW(Addr) zero, zeroend, zeropage;
1253 zero = l->l_addr + c->dataend;
1254 zeroend = l->l_addr + c->allocend;
1255 zeropage = ((zero + GLRO(dl_pagesize) - 1)
1256 & ~(GLRO(dl_pagesize) - 1));
1258 if (zeroend < zeropage)
1259 /* All the extra data is in the last page of the segment.
1260 We can just zero it. */
1261 zeropage = zeroend;
1263 if (zeropage > zero)
1265 /* Zero the final part of the last page of the segment. */
1266 if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
1268 /* Dag nab it. */
1269 if (__mprotect ((caddr_t) (zero
1270 & ~(GLRO(dl_pagesize) - 1)),
1271 GLRO(dl_pagesize), c->prot|PROT_WRITE) < 0)
1273 errstring = N_("cannot change memory protections");
1274 goto call_lose_errno;
1277 memset ((void *) zero, '\0', zeropage - zero);
1278 if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
1279 __mprotect ((caddr_t) (zero & ~(GLRO(dl_pagesize) - 1)),
1280 GLRO(dl_pagesize), c->prot);
1283 if (zeroend > zeropage)
1285 /* Map the remaining zero pages in from the zero fill FD. */
1286 caddr_t mapat;
1287 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1288 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1289 ANONFD, 0);
1290 if (__builtin_expect (mapat == MAP_FAILED, 0))
1292 errstring = N_("cannot map zero-fill pages");
1293 goto call_lose_errno;
1298 ++c;
1302 if (l->l_ld == 0)
1304 if (__builtin_expect (type == ET_DYN, 0))
1306 errstring = N_("object file has no dynamic section");
1307 goto call_lose;
1310 else
1311 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1313 elf_get_dynamic_info (l, NULL);
1315 /* Make sure we are not dlopen'ing an object that has the
1316 DF_1_NOOPEN flag set. */
1317 if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1318 && (mode & __RTLD_DLOPEN))
1320 /* We are not supposed to load this object. Free all resources. */
1321 __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1323 if (!l->l_libname->dont_free)
1324 free (l->l_libname);
1326 if (l->l_phdr_allocated)
1327 free ((void *) l->l_phdr);
1329 errstring = N_("shared object cannot be dlopen()ed");
1330 goto call_lose;
1333 if (l->l_phdr == NULL)
1335 /* The program header is not contained in any of the segments.
1336 We have to allocate memory ourself and copy it over from out
1337 temporary place. */
1338 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1339 * sizeof (ElfW(Phdr)));
1340 if (newp == NULL)
1342 errstring = N_("cannot allocate memory for program header");
1343 goto call_lose_errno;
1346 l->l_phdr = memcpy (newp, phdr,
1347 (header->e_phnum * sizeof (ElfW(Phdr))));
1348 l->l_phdr_allocated = 1;
1350 else
1351 /* Adjust the PT_PHDR value by the runtime load address. */
1352 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1354 if (__builtin_expect ((stack_flags &~ GL(dl_stack_flags)) & PF_X, 0))
1356 /* The stack is presently not executable, but this module
1357 requires that it be executable. We must change the
1358 protection of the variable which contains the flags used in
1359 the mprotect calls. */
1360 #ifdef HAVE_Z_RELRO
1361 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1363 uintptr_t p = ((uintptr_t) &__stack_prot) & ~(GLRO(dl_pagesize) - 1);
1364 size_t s = (uintptr_t) &__stack_prot - p + sizeof (int);
1366 __mprotect ((void *) p, s, PROT_READ|PROT_WRITE);
1367 if (__builtin_expect (__check_caller (RETURN_ADDRESS (0),
1368 allow_ldso) == 0,
1370 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1371 __mprotect ((void *) p, s, PROT_READ);
1373 else
1374 #endif
1375 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1377 #ifdef check_consistency
1378 check_consistency ();
1379 #endif
1381 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1382 if (errval)
1384 errstring = N_("\
1385 cannot enable executable stack as shared object requires");
1386 goto call_lose;
1390 #ifdef USE_TLS
1391 /* Adjust the address of the TLS initialization image. */
1392 if (l->l_tls_initimage != NULL)
1393 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1394 #endif
1396 /* We are done mapping in the file. We no longer need the descriptor. */
1397 if (__builtin_expect (__close (fd) != 0, 0))
1399 errstring = N_("cannot close file descriptor");
1400 goto call_lose_errno;
1402 /* Signal that we closed the file. */
1403 fd = -1;
1405 if (l->l_type == lt_library && type == ET_EXEC)
1406 l->l_type = lt_executable;
1408 l->l_entry += l->l_addr;
1410 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
1411 _dl_debug_printf ("\
1412 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1413 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1414 (int) sizeof (void *) * 2,
1415 (unsigned long int) l->l_ld,
1416 (int) sizeof (void *) * 2,
1417 (unsigned long int) l->l_addr,
1418 (int) sizeof (void *) * 2, maplength,
1419 (int) sizeof (void *) * 2,
1420 (unsigned long int) l->l_entry,
1421 (int) sizeof (void *) * 2,
1422 (unsigned long int) l->l_phdr,
1423 (int) sizeof (void *) * 2, l->l_phnum);
1425 /* Set up the symbol hash table. */
1426 _dl_setup_hash (l);
1428 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1429 have to do this for the main map. */
1430 if ((mode & RTLD_DEEPBIND) == 0
1431 && __builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0)
1432 && &l->l_searchlist != l->l_scope[0])
1434 /* Create an appropriate searchlist. It contains only this map.
1435 This is the definition of DT_SYMBOLIC in SysVr4. */
1436 l->l_symbolic_searchlist.r_list =
1437 (struct link_map **) malloc (sizeof (struct link_map *));
1439 if (l->l_symbolic_searchlist.r_list == NULL)
1441 errstring = N_("cannot create searchlist");
1442 goto call_lose_errno;
1445 l->l_symbolic_searchlist.r_list[0] = l;
1446 l->l_symbolic_searchlist.r_nlist = 1;
1448 /* Now move the existing entries one back. */
1449 memmove (&l->l_scope[1], &l->l_scope[0],
1450 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1452 /* Now add the new entry. */
1453 l->l_scope[0] = &l->l_symbolic_searchlist;
1456 /* Remember whether this object must be initialized first. */
1457 if (l->l_flags_1 & DF_1_INITFIRST)
1458 GL(dl_initfirst) = l;
1460 /* Finally the file information. */
1461 l->l_dev = st.st_dev;
1462 l->l_ino = st.st_ino;
1464 /* When we profile the SONAME might be needed for something else but
1465 loading. Add it right away. */
1466 if (__builtin_expect (GLRO(dl_profile) != NULL, 0)
1467 && l->l_info[DT_SONAME] != NULL)
1468 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1469 + l->l_info[DT_SONAME]->d_un.d_val));
1471 #ifdef SHARED
1472 /* Auditing checkpoint: we have a new object. */
1473 if (__builtin_expect (GLRO(dl_naudit) > 0, 0)
1474 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1476 struct audit_ifaces *afct = GLRO(dl_audit);
1477 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1479 if (afct->objopen != NULL)
1481 l->l_audit[cnt].bindflags
1482 = afct->objopen (l, nsid, &l->l_audit[cnt].cookie);
1484 l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0;
1487 afct = afct->next;
1490 #endif
1492 return l;
1495 /* Print search path. */
1496 static void
1497 print_search_path (struct r_search_path_elem **list,
1498 const char *what, const char *name)
1500 char buf[max_dirnamelen + max_capstrlen];
1501 int first = 1;
1503 _dl_debug_printf (" search path=");
1505 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1507 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1508 size_t cnt;
1510 for (cnt = 0; cnt < ncapstr; ++cnt)
1511 if ((*list)->status[cnt] != nonexisting)
1513 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1514 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1515 cp[0] = '\0';
1516 else
1517 cp[-1] = '\0';
1519 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1520 first = 0;
1523 ++list;
1526 if (name != NULL)
1527 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1528 name[0] ? name : rtld_progname);
1529 else
1530 _dl_debug_printf_c ("\t\t(%s)\n", what);
1533 /* Open a file and verify it is an ELF file for this architecture. We
1534 ignore only ELF files for other architectures. Non-ELF files and
1535 ELF files with different header information cause fatal errors since
1536 this could mean there is something wrong in the installation and the
1537 user might want to know about this. */
1538 static int
1539 open_verify (const char *name, struct filebuf *fbp, struct link_map *loader,
1540 int whatcode)
1542 /* This is the expected ELF header. */
1543 #define ELF32_CLASS ELFCLASS32
1544 #define ELF64_CLASS ELFCLASS64
1545 #ifndef VALID_ELF_HEADER
1546 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1547 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1548 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1549 #endif
1550 static const unsigned char expected[EI_PAD] =
1552 [EI_MAG0] = ELFMAG0,
1553 [EI_MAG1] = ELFMAG1,
1554 [EI_MAG2] = ELFMAG2,
1555 [EI_MAG3] = ELFMAG3,
1556 [EI_CLASS] = ELFW(CLASS),
1557 [EI_DATA] = byteorder,
1558 [EI_VERSION] = EV_CURRENT,
1559 [EI_OSABI] = ELFOSABI_SYSV,
1560 [EI_ABIVERSION] = 0
1562 static const struct
1564 ElfW(Word) vendorlen;
1565 ElfW(Word) datalen;
1566 ElfW(Word) type;
1567 char vendor[4];
1568 } expected_note = { 4, 16, 1, "GNU" };
1569 /* Initialize it to make the compiler happy. */
1570 const char *errstring = NULL;
1571 int errval = 0;
1573 #ifdef SHARED
1574 /* Give the auditing libraries a chance. */
1575 if (__builtin_expect (GLRO(dl_naudit) > 0, 0) && whatcode != 0
1576 && loader->l_auditing == 0)
1578 struct audit_ifaces *afct = GLRO(dl_audit);
1579 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1581 if (afct->objsearch != NULL)
1583 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1584 whatcode);
1585 if (name == NULL)
1586 /* Ignore the path. */
1587 return -1;
1590 afct = afct->next;
1593 #endif
1595 /* Open the file. We always open files read-only. */
1596 int fd = __open (name, O_RDONLY);
1597 if (fd != -1)
1599 ElfW(Ehdr) *ehdr;
1600 ElfW(Phdr) *phdr, *ph;
1601 ElfW(Word) *abi_note, abi_note_buf[8];
1602 unsigned int osversion;
1603 size_t maplength;
1605 /* We successfully openened the file. Now verify it is a file
1606 we can use. */
1607 __set_errno (0);
1608 fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf));
1610 /* This is where the ELF header is loaded. */
1611 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1612 ehdr = (ElfW(Ehdr) *) fbp->buf;
1614 /* Now run the tests. */
1615 if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0))
1617 errval = errno;
1618 errstring = (errval == 0
1619 ? N_("file too short") : N_("cannot read file data"));
1620 call_lose:
1621 lose (errval, fd, name, NULL, NULL, errstring);
1624 /* See whether the ELF header is what we expect. */
1625 if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1626 EI_PAD), 0))
1628 /* Something is wrong. */
1629 if (*(Elf32_Word *) &ehdr->e_ident !=
1630 #if BYTE_ORDER == LITTLE_ENDIAN
1631 ((ELFMAG0 << (EI_MAG0 * 8)) |
1632 (ELFMAG1 << (EI_MAG1 * 8)) |
1633 (ELFMAG2 << (EI_MAG2 * 8)) |
1634 (ELFMAG3 << (EI_MAG3 * 8)))
1635 #else
1636 ((ELFMAG0 << (EI_MAG3 * 8)) |
1637 (ELFMAG1 << (EI_MAG2 * 8)) |
1638 (ELFMAG2 << (EI_MAG1 * 8)) |
1639 (ELFMAG3 << (EI_MAG0 * 8)))
1640 #endif
1642 errstring = N_("invalid ELF header");
1643 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1644 /* This is not a fatal error. On architectures where
1645 32-bit and 64-bit binaries can be run this might
1646 happen. */
1647 goto close_and_out;
1648 else if (ehdr->e_ident[EI_DATA] != byteorder)
1650 if (BYTE_ORDER == BIG_ENDIAN)
1651 errstring = N_("ELF file data encoding not big-endian");
1652 else
1653 errstring = N_("ELF file data encoding not little-endian");
1655 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1656 errstring
1657 = N_("ELF file version ident does not match current one");
1658 /* XXX We should be able so set system specific versions which are
1659 allowed here. */
1660 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1661 errstring = N_("ELF file OS ABI invalid");
1662 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION]))
1663 errstring = N_("ELF file ABI version invalid");
1664 else
1665 /* Otherwise we don't know what went wrong. */
1666 errstring = N_("internal error");
1668 goto call_lose;
1671 if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT)
1673 errstring = N_("ELF file version does not match current one");
1674 goto call_lose;
1676 if (! __builtin_expect (elf_machine_matches_host (ehdr), 1))
1677 goto close_and_out;
1678 else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN
1679 && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC)
1681 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1682 goto call_lose;
1684 else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
1685 != sizeof (ElfW(Phdr)))
1687 errstring = N_("ELF file's phentsize not the expected size");
1688 goto call_lose;
1691 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1692 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1693 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1694 else
1696 phdr = alloca (maplength);
1697 __lseek (fd, ehdr->e_phoff, SEEK_SET);
1698 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1700 read_error:
1701 errval = errno;
1702 errstring = N_("cannot read file data");
1703 goto call_lose;
1707 /* Check .note.ABI-tag if present. */
1708 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1709 if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
1711 if (ph->p_offset + 32 <= (size_t) fbp->len)
1712 abi_note = (void *) (fbp->buf + ph->p_offset);
1713 else
1715 __lseek (fd, ph->p_offset, SEEK_SET);
1716 if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
1717 goto read_error;
1719 abi_note = abi_note_buf;
1722 if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1723 continue;
1725 osversion = (abi_note[5] & 0xff) * 65536
1726 + (abi_note[6] & 0xff) * 256
1727 + (abi_note[7] & 0xff);
1728 if (abi_note[4] != __ABI_TAG_OS
1729 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1731 close_and_out:
1732 __close (fd);
1733 __set_errno (ENOENT);
1734 fd = -1;
1737 break;
1741 return fd;
1744 /* Try to open NAME in one of the directories in *DIRSP.
1745 Return the fd, or -1. If successful, fill in *REALNAME
1746 with the malloc'd full directory name. If it turns out
1747 that none of the directories in *DIRSP exists, *DIRSP is
1748 replaced with (void *) -1, and the old value is free()d
1749 if MAY_FREE_DIRS is true. */
1751 static int
1752 open_path (const char *name, size_t namelen, int preloaded,
1753 struct r_search_path_struct *sps, char **realname,
1754 struct filebuf *fbp, struct link_map *loader, int whatcode)
1756 struct r_search_path_elem **dirs = sps->dirs;
1757 char *buf;
1758 int fd = -1;
1759 const char *current_what = NULL;
1760 int any = 0;
1762 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1765 struct r_search_path_elem *this_dir = *dirs;
1766 size_t buflen = 0;
1767 size_t cnt;
1768 char *edp;
1769 int here_any = 0;
1770 int err;
1772 /* If we are debugging the search for libraries print the path
1773 now if it hasn't happened now. */
1774 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)
1775 && current_what != this_dir->what)
1777 current_what = this_dir->what;
1778 print_search_path (dirs, current_what, this_dir->where);
1781 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1782 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1784 /* Skip this directory if we know it does not exist. */
1785 if (this_dir->status[cnt] == nonexisting)
1786 continue;
1788 buflen =
1789 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1790 capstr[cnt].len),
1791 name, namelen)
1792 - buf);
1794 /* Print name we try if this is wanted. */
1795 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1796 _dl_debug_printf (" trying file=%s\n", buf);
1798 fd = open_verify (buf, fbp, loader, whatcode);
1799 if (this_dir->status[cnt] == unknown)
1801 if (fd != -1)
1802 this_dir->status[cnt] = existing;
1803 /* Do not update the directory information when loading
1804 auditing code. We must try to disturb the program as
1805 little as possible. */
1806 else if (loader == NULL
1807 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_audit == 0)
1809 /* We failed to open machine dependent library. Let's
1810 test whether there is any directory at all. */
1811 struct stat64 st;
1813 buf[buflen - namelen - 1] = '\0';
1815 if (__xstat64 (_STAT_VER, buf, &st) != 0
1816 || ! S_ISDIR (st.st_mode))
1817 /* The directory does not exist or it is no directory. */
1818 this_dir->status[cnt] = nonexisting;
1819 else
1820 this_dir->status[cnt] = existing;
1824 /* Remember whether we found any existing directory. */
1825 here_any |= this_dir->status[cnt] != nonexisting;
1827 if (fd != -1 && __builtin_expect (preloaded, 0)
1828 && INTUSE(__libc_enable_secure))
1830 /* This is an extra security effort to make sure nobody can
1831 preload broken shared objects which are in the trusted
1832 directories and so exploit the bugs. */
1833 struct stat64 st;
1835 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1836 || (st.st_mode & S_ISUID) == 0)
1838 /* The shared object cannot be tested for being SUID
1839 or this bit is not set. In this case we must not
1840 use this object. */
1841 __close (fd);
1842 fd = -1;
1843 /* We simply ignore the file, signal this by setting
1844 the error value which would have been set by `open'. */
1845 errno = ENOENT;
1850 if (fd != -1)
1852 *realname = (char *) malloc (buflen);
1853 if (*realname != NULL)
1855 memcpy (*realname, buf, buflen);
1856 return fd;
1858 else
1860 /* No memory for the name, we certainly won't be able
1861 to load and link it. */
1862 __close (fd);
1863 return -1;
1866 if (here_any && (err = errno) != ENOENT && err != EACCES)
1867 /* The file exists and is readable, but something went wrong. */
1868 return -1;
1870 /* Remember whether we found anything. */
1871 any |= here_any;
1873 while (*++dirs != NULL);
1875 /* Remove the whole path if none of the directories exists. */
1876 if (__builtin_expect (! any, 0))
1878 /* Paths which were allocated using the minimal malloc() in ld.so
1879 must not be freed using the general free() in libc. */
1880 if (sps->malloced)
1881 free (sps->dirs);
1882 #ifdef HAVE_Z_RELRO
1883 /* rtld_search_dirs is attribute_relro, therefore avoid writing
1884 into it. */
1885 if (sps != &rtld_search_dirs)
1886 #endif
1887 sps->dirs = (void *) -1;
1890 return -1;
1893 /* Map in the shared object file NAME. */
1895 struct link_map *
1896 internal_function
1897 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1898 int type, int trace_mode, int mode, Lmid_t nsid)
1900 int fd;
1901 char *realname;
1902 char *name_copy;
1903 struct link_map *l;
1904 struct filebuf fb;
1906 assert (nsid >= 0);
1907 assert (nsid < DL_NNS);
1909 /* Look for this name among those already loaded. */
1910 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
1912 /* If the requested name matches the soname of a loaded object,
1913 use that object. Elide this check for names that have not
1914 yet been opened. */
1915 if (__builtin_expect (l->l_faked, 0) != 0)
1916 continue;
1917 if (!_dl_name_match_p (name, l))
1919 const char *soname;
1921 if (__builtin_expect (l->l_soname_added, 1)
1922 || l->l_info[DT_SONAME] == NULL)
1923 continue;
1925 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1926 + l->l_info[DT_SONAME]->d_un.d_val);
1927 if (strcmp (name, soname) != 0)
1928 continue;
1930 /* We have a match on a new name -- cache it. */
1931 add_name_to_object (l, soname);
1932 l->l_soname_added = 1;
1935 /* We have a match. */
1936 return l;
1939 /* Display information if we are debugging. */
1940 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0)
1941 && loader != NULL)
1942 _dl_debug_printf ("\nfile=%s [%lu]; needed by %s [%lu]\n", name, nsid,
1943 loader->l_name[0]
1944 ? loader->l_name : rtld_progname, loader->l_ns);
1946 #ifdef SHARED
1947 /* Give the auditing libraries a chance to change the name before we
1948 try anything. */
1949 if (__builtin_expect (GLRO(dl_naudit) > 0, 0)
1950 && (loader == NULL || loader->l_auditing == 0))
1952 struct audit_ifaces *afct = GLRO(dl_audit);
1953 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1955 if (afct->objsearch != NULL)
1957 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1958 LA_SER_ORIG);
1959 if (name == NULL)
1961 /* Do not try anything further. */
1962 fd = -1;
1963 goto no_file;
1967 afct = afct->next;
1970 #endif
1972 if (strchr (name, '/') == NULL)
1974 /* Search for NAME in several places. */
1976 size_t namelen = strlen (name) + 1;
1978 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1979 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
1981 fd = -1;
1983 /* When the object has the RUNPATH information we don't use any
1984 RPATHs. */
1985 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1987 /* First try the DT_RPATH of the dependent object that caused NAME
1988 to be loaded. Then that object's dependent, and on up. */
1989 for (l = loader; fd == -1 && l; l = l->l_loader)
1990 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
1991 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1992 &realname, &fb, loader, LA_SER_RUNPATH);
1994 /* If dynamically linked, try the DT_RPATH of the executable
1995 itself. NB: we do this for lookups in any namespace. */
1996 if (fd == -1)
1998 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1999 if (l && l->l_type != lt_loaded && l != loader
2000 && cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2001 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
2002 &realname, &fb, loader ?: l, LA_SER_RUNPATH);
2006 /* Try the LD_LIBRARY_PATH environment variable. */
2007 if (fd == -1 && env_path_list.dirs != (void *) -1)
2008 fd = open_path (name, namelen, preloaded, &env_path_list,
2009 &realname, &fb,
2010 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
2011 LA_SER_LIBPATH);
2013 /* Look at the RUNPATH information for this binary. */
2014 if (fd == -1 && loader != NULL
2015 && cache_rpath (loader, &loader->l_runpath_dirs,
2016 DT_RUNPATH, "RUNPATH"))
2017 fd = open_path (name, namelen, preloaded,
2018 &loader->l_runpath_dirs, &realname, &fb, loader,
2019 LA_SER_RUNPATH);
2021 if (fd == -1
2022 && (__builtin_expect (! preloaded, 1)
2023 || ! INTUSE(__libc_enable_secure)))
2025 /* Check the list of libraries in the file /etc/ld.so.cache,
2026 for compatibility with Linux's ldconfig program. */
2027 const char *cached = _dl_load_cache_lookup (name);
2029 if (cached != NULL)
2031 #ifdef SHARED
2032 // XXX Correct to unconditionally default to namespace 0?
2033 l = loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2034 #else
2035 l = loader;
2036 #endif
2038 /* If the loader has the DF_1_NODEFLIB flag set we must not
2039 use a cache entry from any of these directories. */
2040 if (
2041 #ifndef SHARED
2042 /* 'l' is always != NULL for dynamically linked objects. */
2043 l != NULL &&
2044 #endif
2045 __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
2047 const char *dirp = system_dirs;
2048 unsigned int cnt = 0;
2052 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2054 /* The prefix matches. Don't use the entry. */
2055 cached = NULL;
2056 break;
2059 dirp += system_dirs_len[cnt] + 1;
2060 ++cnt;
2062 while (cnt < nsystem_dirs_len);
2065 if (cached != NULL)
2067 fd = open_verify (cached,
2068 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2069 LA_SER_CONFIG);
2070 if (__builtin_expect (fd != -1, 1))
2072 realname = local_strdup (cached);
2073 if (realname == NULL)
2075 __close (fd);
2076 fd = -1;
2083 /* Finally, try the default path. */
2084 if (fd == -1
2085 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2086 || __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
2087 && rtld_search_dirs.dirs != (void *) -1)
2088 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
2089 &realname, &fb, l, LA_SER_DEFAULT);
2091 /* Add another newline when we are tracing the library loading. */
2092 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2093 _dl_debug_printf ("\n");
2095 else
2097 /* The path may contain dynamic string tokens. */
2098 realname = (loader
2099 ? expand_dynamic_string_token (loader, name)
2100 : local_strdup (name));
2101 if (realname == NULL)
2102 fd = -1;
2103 else
2105 fd = open_verify (realname, &fb,
2106 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0);
2107 if (__builtin_expect (fd, 0) == -1)
2108 free (realname);
2112 #ifdef SHARED
2113 no_file:
2114 #endif
2115 /* In case the LOADER information has only been provided to get to
2116 the appropriate RUNPATH/RPATH information we do not need it
2117 anymore. */
2118 if (mode & __RTLD_CALLMAP)
2119 loader = NULL;
2121 if (__builtin_expect (fd, 0) == -1)
2123 if (trace_mode
2124 && __builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK, 0) == 0)
2126 /* We haven't found an appropriate library. But since we
2127 are only interested in the list of libraries this isn't
2128 so severe. Fake an entry with all the information we
2129 have. */
2130 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2132 /* Enter the new object in the list of loaded objects. */
2133 if ((name_copy = local_strdup (name)) == NULL
2134 || (l = _dl_new_object (name_copy, name, type, loader,
2135 mode, nsid)) == NULL)
2136 _dl_signal_error (ENOMEM, name, NULL,
2137 N_("cannot create shared object descriptor"));
2138 /* Signal that this is a faked entry. */
2139 l->l_faked = 1;
2140 /* Since the descriptor is initialized with zero we do not
2141 have do this here.
2142 l->l_reserved = 0; */
2143 l->l_buckets = &dummy_bucket;
2144 l->l_nbuckets = 1;
2145 l->l_relocated = 1;
2147 return l;
2149 else
2150 _dl_signal_error (errno, name, NULL,
2151 N_("cannot open shared object file"));
2154 void *stack_end = __libc_stack_end;
2155 return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode,
2156 &stack_end, nsid);
2160 void
2161 internal_function
2162 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2164 if (counting)
2166 si->dls_cnt = 0;
2167 si->dls_size = 0;
2170 unsigned int idx = 0;
2171 char *allocptr = (char *) &si->dls_serpath[si->dls_cnt];
2172 void add_path (const struct r_search_path_struct *sps, unsigned int flags)
2173 # define add_path(sps, flags) add_path(sps, 0) /* XXX */
2175 if (sps->dirs != (void *) -1)
2177 struct r_search_path_elem **dirs = sps->dirs;
2180 const struct r_search_path_elem *const r = *dirs++;
2181 if (counting)
2183 si->dls_cnt++;
2184 si->dls_size += r->dirnamelen;
2186 else
2188 Dl_serpath *const sp = &si->dls_serpath[idx++];
2189 sp->dls_name = allocptr;
2190 allocptr = __mempcpy (allocptr,
2191 r->dirname, r->dirnamelen - 1);
2192 *allocptr++ = '\0';
2193 sp->dls_flags = flags;
2196 while (*dirs != NULL);
2200 /* When the object has the RUNPATH information we don't use any RPATHs. */
2201 if (loader->l_info[DT_RUNPATH] == NULL)
2203 /* First try the DT_RPATH of the dependent object that caused NAME
2204 to be loaded. Then that object's dependent, and on up. */
2206 struct link_map *l = loader;
2209 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2210 add_path (&l->l_rpath_dirs, XXX_RPATH);
2211 l = l->l_loader;
2213 while (l != NULL);
2215 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2216 if (loader->l_ns == LM_ID_BASE)
2218 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2219 if (l != NULL && l->l_type != lt_loaded && l != loader)
2220 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2221 add_path (&l->l_rpath_dirs, XXX_RPATH);
2225 /* Try the LD_LIBRARY_PATH environment variable. */
2226 add_path (&env_path_list, XXX_ENV);
2228 /* Look at the RUNPATH information for this binary. */
2229 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2230 add_path (&loader->l_runpath_dirs, XXX_RUNPATH);
2232 /* XXX
2233 Here is where ld.so.cache gets checked, but we don't have
2234 a way to indicate that in the results for Dl_serinfo. */
2236 /* Finally, try the default path. */
2237 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2238 add_path (&rtld_search_dirs, XXX_default);
2240 if (counting)
2241 /* Count the struct size before the string area, which we didn't
2242 know before we completed dls_cnt. */
2243 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;