* sysdeps/unix/sysv/linux/alpha/bits/mman.h: Fix MADV_REMOVE value.
[glibc.git] / elf / dl-load.c
blob29fdfd8f1948a5efa73c0b94a2dc494a51245430
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2005, 2006 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 <stackinfo.h>
37 #include <caller.h>
38 #include <sysdep.h>
40 #include <dl-dst.h>
42 /* On some systems, no flag bits are given to specify file mapping. */
43 #ifndef MAP_FILE
44 # define MAP_FILE 0
45 #endif
47 /* The right way to map in the shared library files is MAP_COPY, which
48 makes a virtual copy of the data at the time of the mmap call; this
49 guarantees the mapped pages will be consistent even if the file is
50 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
51 get is MAP_PRIVATE, which copies each page when it is modified; this
52 means if the file is overwritten, we may at some point get some pages
53 from the new version after starting with pages from the old version.
55 To make up for the lack and avoid the overwriting problem,
56 what Linux does have is MAP_DENYWRITE. This prevents anyone
57 from modifying the file while we have it mapped. */
58 #ifndef MAP_COPY
59 # ifdef MAP_DENYWRITE
60 # define MAP_COPY (MAP_PRIVATE | MAP_DENYWRITE)
61 # else
62 # define MAP_COPY MAP_PRIVATE
63 # endif
64 #endif
66 /* Some systems link their relocatable objects for another base address
67 than 0. We want to know the base address for these such that we can
68 subtract this address from the segment addresses during mapping.
69 This results in a more efficient address space usage. Defaults to
70 zero for almost all systems. */
71 #ifndef MAP_BASE_ADDR
72 # define MAP_BASE_ADDR(l) 0
73 #endif
76 #include <endian.h>
77 #if BYTE_ORDER == BIG_ENDIAN
78 # define byteorder ELFDATA2MSB
79 #elif BYTE_ORDER == LITTLE_ENDIAN
80 # define byteorder ELFDATA2LSB
81 #else
82 # error "Unknown BYTE_ORDER " BYTE_ORDER
83 # define byteorder ELFDATANONE
84 #endif
86 #define STRING(x) __STRING (x)
88 #ifdef MAP_ANON
89 /* The fd is not examined when using MAP_ANON. */
90 # define ANONFD -1
91 #else
92 int _dl_zerofd = -1;
93 # define ANONFD _dl_zerofd
94 #endif
96 /* Handle situations where we have a preferred location in memory for
97 the shared objects. */
98 #ifdef ELF_PREFERRED_ADDRESS_DATA
99 ELF_PREFERRED_ADDRESS_DATA;
100 #endif
101 #ifndef ELF_PREFERRED_ADDRESS
102 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
103 #endif
104 #ifndef ELF_FIXED_ADDRESS
105 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
106 #endif
109 int __stack_prot attribute_hidden attribute_relro
110 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
111 = PROT_GROWSDOWN;
112 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
113 = PROT_GROWSUP;
114 #else
115 = 0;
116 #endif
119 /* Type for the buffer we put the ELF header and hopefully the program
120 header. This buffer does not really have to be too large. In most
121 cases the program header follows the ELF header directly. If this
122 is not the case all bets are off and we can make the header
123 arbitrarily large and still won't get it read. This means the only
124 question is how large are the ELF and program header combined. The
125 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
126 bytes long. Each program header entry is again 32 and 56 bytes
127 long respectively. I.e., even with a file which has 10 program
128 header entries we only have to read 372B/624B respectively. Add to
129 this a bit of margin for program notes and reading 512B and 832B
130 for 32-bit and 64-bit files respecitvely is enough. If this
131 heuristic should really fail for some file the code in
132 `_dl_map_object_from_fd' knows how to recover. */
133 struct filebuf
135 ssize_t len;
136 #if __WORDSIZE == 32
137 # define FILEBUF_SIZE 512
138 #else
139 # define FILEBUF_SIZE 832
140 #endif
141 char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr)))));
144 /* This is the decomposed LD_LIBRARY_PATH search path. */
145 static struct r_search_path_struct env_path_list attribute_relro;
147 /* List of the hardware capabilities we might end up using. */
148 static const struct r_strlenpair *capstr attribute_relro;
149 static size_t ncapstr attribute_relro;
150 static size_t max_capstrlen attribute_relro;
153 /* Get the generated information about the trusted directories. */
154 #include "trusted-dirs.h"
156 static const char system_dirs[] = SYSTEM_DIRS;
157 static const size_t system_dirs_len[] =
159 SYSTEM_DIRS_LEN
161 #define nsystem_dirs_len \
162 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
165 /* Local version of `strdup' function. */
166 static inline char *
167 local_strdup (const char *s)
169 size_t len = strlen (s) + 1;
170 void *new = malloc (len);
172 if (new == NULL)
173 return NULL;
175 return (char *) memcpy (new, s, len);
179 static size_t
180 is_dst (const char *start, const char *name, const char *str,
181 int is_path, int secure)
183 size_t len;
184 bool is_curly = false;
186 if (name[0] == '{')
188 is_curly = true;
189 ++name;
192 len = 0;
193 while (name[len] == str[len] && name[len] != '\0')
194 ++len;
196 if (is_curly)
198 if (name[len] != '}')
199 return 0;
201 /* Point again at the beginning of the name. */
202 --name;
203 /* Skip over closing curly brace and adjust for the --name. */
204 len += 2;
206 else if (name[len] != '\0' && name[len] != '/'
207 && (!is_path || name[len] != ':'))
208 return 0;
210 if (__builtin_expect (secure, 0)
211 && ((name[len] != '\0' && (!is_path || name[len] != ':'))
212 || (name != start + 1 && (!is_path || name[-2] != ':'))))
213 return 0;
215 return len;
219 size_t
220 _dl_dst_count (const char *name, int is_path)
222 const char *const start = name;
223 size_t cnt = 0;
227 size_t len;
229 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
230 is $ORIGIN alone) and it must always appear first in path. */
231 ++name;
232 if ((len = is_dst (start, name, "ORIGIN", is_path,
233 INTUSE(__libc_enable_secure))) != 0
234 || (len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0
235 || (len = is_dst (start, name, "LIB", is_path, 0)) != 0)
236 ++cnt;
238 name = strchr (name + len, '$');
240 while (name != NULL);
242 return cnt;
246 char *
247 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
248 int is_path)
250 const char *const start = name;
251 char *last_elem, *wp;
253 /* Now fill the result path. While copying over the string we keep
254 track of the start of the last path element. When we come accross
255 a DST we copy over the value or (if the value is not available)
256 leave the entire path element out. */
257 last_elem = wp = result;
261 if (__builtin_expect (*name == '$', 0))
263 const char *repl = NULL;
264 size_t len;
266 ++name;
267 if ((len = is_dst (start, name, "ORIGIN", is_path,
268 INTUSE(__libc_enable_secure))) != 0)
269 repl = l->l_origin;
270 else if ((len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0)
271 repl = GLRO(dl_platform);
272 else if ((len = is_dst (start, name, "LIB", is_path, 0)) != 0)
273 repl = DL_DST_LIB;
275 if (repl != NULL && repl != (const char *) -1)
277 wp = __stpcpy (wp, repl);
278 name += len;
280 else if (len > 1)
282 /* We cannot use this path element, the value of the
283 replacement is unknown. */
284 wp = last_elem;
285 name += len;
286 while (*name != '\0' && (!is_path || *name != ':'))
287 ++name;
289 else
290 /* No DST we recognize. */
291 *wp++ = '$';
293 else
295 *wp++ = *name++;
296 if (is_path && *name == ':')
297 last_elem = wp;
300 while (*name != '\0');
302 *wp = '\0';
304 return result;
308 /* Return copy of argument with all recognized dynamic string tokens
309 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
310 might not be possible to determine the path from which the object
311 belonging to the map is loaded. In this case the path element
312 containing $ORIGIN is left out. */
313 static char *
314 expand_dynamic_string_token (struct link_map *l, const char *s)
316 /* We make two runs over the string. First we determine how large the
317 resulting string is and then we copy it over. Since this is now
318 frequently executed operation we are looking here not for performance
319 but rather for code size. */
320 size_t cnt;
321 size_t total;
322 char *result;
324 /* Determine the number of DST elements. */
325 cnt = DL_DST_COUNT (s, 1);
327 /* If we do not have to replace anything simply copy the string. */
328 if (__builtin_expect (cnt, 0) == 0)
329 return local_strdup (s);
331 /* Determine the length of the substituted string. */
332 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
334 /* Allocate the necessary memory. */
335 result = (char *) malloc (total + 1);
336 if (result == NULL)
337 return NULL;
339 return _dl_dst_substitute (l, s, result, 1);
343 /* Add `name' to the list of names for a particular shared object.
344 `name' is expected to have been allocated with malloc and will
345 be freed if the shared object already has this name.
346 Returns false if the object already had this name. */
347 static void
348 internal_function
349 add_name_to_object (struct link_map *l, const char *name)
351 struct libname_list *lnp, *lastp;
352 struct libname_list *newname;
353 size_t name_len;
355 lastp = NULL;
356 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
357 if (strcmp (name, lnp->name) == 0)
358 return;
360 name_len = strlen (name) + 1;
361 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
362 if (newname == NULL)
364 /* No more memory. */
365 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
366 return;
368 /* The object should have a libname set from _dl_new_object. */
369 assert (lastp != NULL);
371 newname->name = memcpy (newname + 1, name, name_len);
372 newname->next = NULL;
373 newname->dont_free = 0;
374 lastp->next = newname;
377 /* Standard search directories. */
378 static struct r_search_path_struct rtld_search_dirs attribute_relro;
380 static size_t max_dirnamelen;
382 static struct r_search_path_elem **
383 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
384 int check_trusted, const char *what, const char *where)
386 char *cp;
387 size_t nelems = 0;
389 while ((cp = __strsep (&rpath, sep)) != NULL)
391 struct r_search_path_elem *dirp;
392 size_t len = strlen (cp);
394 /* `strsep' can pass an empty string. This has to be
395 interpreted as `use the current directory'. */
396 if (len == 0)
398 static const char curwd[] = "./";
399 cp = (char *) curwd;
402 /* Remove trailing slashes (except for "/"). */
403 while (len > 1 && cp[len - 1] == '/')
404 --len;
406 /* Now add one if there is none so far. */
407 if (len > 0 && cp[len - 1] != '/')
408 cp[len++] = '/';
410 /* Make sure we don't use untrusted directories if we run SUID. */
411 if (__builtin_expect (check_trusted, 0))
413 const char *trun = system_dirs;
414 size_t idx;
415 int unsecure = 1;
417 /* All trusted directories must be complete names. */
418 if (cp[0] == '/')
420 for (idx = 0; idx < nsystem_dirs_len; ++idx)
422 if (len == system_dirs_len[idx]
423 && memcmp (trun, cp, len) == 0)
425 /* Found it. */
426 unsecure = 0;
427 break;
430 trun += system_dirs_len[idx] + 1;
434 if (unsecure)
435 /* Simply drop this directory. */
436 continue;
439 /* See if this directory is already known. */
440 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
441 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
442 break;
444 if (dirp != NULL)
446 /* It is available, see whether it's on our own list. */
447 size_t cnt;
448 for (cnt = 0; cnt < nelems; ++cnt)
449 if (result[cnt] == dirp)
450 break;
452 if (cnt == nelems)
453 result[nelems++] = dirp;
455 else
457 size_t cnt;
458 enum r_dir_status init_val;
459 size_t where_len = where ? strlen (where) + 1 : 0;
461 /* It's a new directory. Create an entry and add it. */
462 dirp = (struct r_search_path_elem *)
463 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
464 + where_len + len + 1);
465 if (dirp == NULL)
466 _dl_signal_error (ENOMEM, NULL, NULL,
467 N_("cannot create cache for search path"));
469 dirp->dirname = ((char *) dirp + sizeof (*dirp)
470 + ncapstr * sizeof (enum r_dir_status));
471 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
472 dirp->dirnamelen = len;
474 if (len > max_dirnamelen)
475 max_dirnamelen = len;
477 /* We have to make sure all the relative directories are
478 never ignored. The current directory might change and
479 all our saved information would be void. */
480 init_val = cp[0] != '/' ? existing : unknown;
481 for (cnt = 0; cnt < ncapstr; ++cnt)
482 dirp->status[cnt] = init_val;
484 dirp->what = what;
485 if (__builtin_expect (where != NULL, 1))
486 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
487 + (ncapstr * sizeof (enum r_dir_status)),
488 where, where_len);
489 else
490 dirp->where = NULL;
492 dirp->next = GL(dl_all_dirs);
493 GL(dl_all_dirs) = dirp;
495 /* Put it in the result array. */
496 result[nelems++] = dirp;
500 /* Terminate the array. */
501 result[nelems] = NULL;
503 return result;
507 static void
508 internal_function
509 decompose_rpath (struct r_search_path_struct *sps,
510 const char *rpath, struct link_map *l, const char *what)
512 /* Make a copy we can work with. */
513 const char *where = l->l_name;
514 char *copy;
515 char *cp;
516 struct r_search_path_elem **result;
517 size_t nelems;
518 /* Initialize to please the compiler. */
519 const char *errstring = NULL;
521 /* First see whether we must forget the RUNPATH and RPATH from this
522 object. */
523 if (__builtin_expect (GLRO(dl_inhibit_rpath) != NULL, 0)
524 && !INTUSE(__libc_enable_secure))
526 const char *inhp = GLRO(dl_inhibit_rpath);
530 const char *wp = where;
532 while (*inhp == *wp && *wp != '\0')
534 ++inhp;
535 ++wp;
538 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
540 /* This object is on the list of objects for which the
541 RUNPATH and RPATH must not be used. */
542 result = calloc (1, sizeof *result);
543 if (result == NULL)
545 signal_error_cache:
546 errstring = N_("cannot create cache for search path");
547 signal_error:
548 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
551 sps->dirs = result;
552 sps->malloced = 1;
554 return;
557 while (*inhp != '\0')
558 if (*inhp++ == ':')
559 break;
561 while (*inhp != '\0');
564 /* Make a writable copy. At the same time expand possible dynamic
565 string tokens. */
566 copy = expand_dynamic_string_token (l, rpath);
567 if (copy == NULL)
569 errstring = N_("cannot create RUNPATH/RPATH copy");
570 goto signal_error;
573 /* Count the number of necessary elements in the result array. */
574 nelems = 0;
575 for (cp = copy; *cp != '\0'; ++cp)
576 if (*cp == ':')
577 ++nelems;
579 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
580 number of necessary entries. */
581 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
582 * sizeof (*result));
583 if (result == NULL)
584 goto signal_error_cache;
586 fillin_rpath (copy, result, ":", 0, what, where);
588 /* Free the copied RPATH string. `fillin_rpath' make own copies if
589 necessary. */
590 free (copy);
592 sps->dirs = result;
593 /* The caller will change this value if we haven't used a real malloc. */
594 sps->malloced = 1;
597 /* Make sure cached path information is stored in *SP
598 and return true if there are any paths to search there. */
599 static bool
600 cache_rpath (struct link_map *l,
601 struct r_search_path_struct *sp,
602 int tag,
603 const char *what)
605 if (sp->dirs == (void *) -1)
606 return false;
608 if (sp->dirs != NULL)
609 return true;
611 if (l->l_info[tag] == NULL)
613 /* There is no path. */
614 sp->dirs = (void *) -1;
615 return false;
618 /* Make sure the cache information is available. */
619 decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
620 + l->l_info[tag]->d_un.d_val),
621 l, what);
622 return true;
626 void
627 internal_function
628 _dl_init_paths (const char *llp)
630 size_t idx;
631 const char *strp;
632 struct r_search_path_elem *pelem, **aelem;
633 size_t round_size;
634 #ifdef SHARED
635 struct link_map *l;
636 #endif
637 /* Initialize to please the compiler. */
638 const char *errstring = NULL;
640 /* Fill in the information about the application's RPATH and the
641 directories addressed by the LD_LIBRARY_PATH environment variable. */
643 /* Get the capabilities. */
644 capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen),
645 &ncapstr, &max_capstrlen);
647 /* First set up the rest of the default search directory entries. */
648 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
649 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
650 if (rtld_search_dirs.dirs == NULL)
652 errstring = N_("cannot create search path array");
653 signal_error:
654 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
657 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
658 + ncapstr * sizeof (enum r_dir_status))
659 / sizeof (struct r_search_path_elem));
661 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
662 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
663 * round_size * sizeof (struct r_search_path_elem));
664 if (rtld_search_dirs.dirs[0] == NULL)
666 errstring = N_("cannot create cache for search path");
667 goto signal_error;
670 rtld_search_dirs.malloced = 0;
671 pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0];
672 strp = system_dirs;
673 idx = 0;
677 size_t cnt;
679 *aelem++ = pelem;
681 pelem->what = "system search path";
682 pelem->where = NULL;
684 pelem->dirname = strp;
685 pelem->dirnamelen = system_dirs_len[idx];
686 strp += system_dirs_len[idx] + 1;
688 /* System paths must be absolute. */
689 assert (pelem->dirname[0] == '/');
690 for (cnt = 0; cnt < ncapstr; ++cnt)
691 pelem->status[cnt] = unknown;
693 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
695 pelem += round_size;
697 while (idx < nsystem_dirs_len);
699 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
700 *aelem = NULL;
702 #ifdef SHARED
703 /* This points to the map of the main object. */
704 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
705 if (l != NULL)
707 assert (l->l_type != lt_loaded);
709 if (l->l_info[DT_RUNPATH])
711 /* Allocate room for the search path and fill in information
712 from RUNPATH. */
713 decompose_rpath (&l->l_runpath_dirs,
714 (const void *) (D_PTR (l, l_info[DT_STRTAB])
715 + l->l_info[DT_RUNPATH]->d_un.d_val),
716 l, "RUNPATH");
718 /* The RPATH is ignored. */
719 l->l_rpath_dirs.dirs = (void *) -1;
721 else
723 l->l_runpath_dirs.dirs = (void *) -1;
725 if (l->l_info[DT_RPATH])
727 /* Allocate room for the search path and fill in information
728 from RPATH. */
729 decompose_rpath (&l->l_rpath_dirs,
730 (const void *) (D_PTR (l, l_info[DT_STRTAB])
731 + l->l_info[DT_RPATH]->d_un.d_val),
732 l, "RPATH");
733 l->l_rpath_dirs.malloced = 0;
735 else
736 l->l_rpath_dirs.dirs = (void *) -1;
739 #endif /* SHARED */
741 if (llp != NULL && *llp != '\0')
743 size_t nllp;
744 const char *cp = llp;
745 char *llp_tmp = strdupa (llp);
747 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
748 elements it has. */
749 nllp = 1;
750 while (*cp)
752 if (*cp == ':' || *cp == ';')
753 ++nllp;
754 ++cp;
757 env_path_list.dirs = (struct r_search_path_elem **)
758 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
759 if (env_path_list.dirs == NULL)
761 errstring = N_("cannot create cache for search path");
762 goto signal_error;
765 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
766 INTUSE(__libc_enable_secure), "LD_LIBRARY_PATH",
767 NULL);
769 if (env_path_list.dirs[0] == NULL)
771 free (env_path_list.dirs);
772 env_path_list.dirs = (void *) -1;
775 env_path_list.malloced = 0;
777 else
778 env_path_list.dirs = (void *) -1;
780 /* Remember the last search directory added at startup. */
781 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
785 static void
786 __attribute__ ((noreturn, noinline))
787 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
788 const char *msg, struct r_debug *r)
790 /* The file might already be closed. */
791 if (fd != -1)
792 (void) __close (fd);
793 if (l != NULL)
795 /* Remove the stillborn object from the list and free it. */
796 assert (l->l_next == NULL);
797 if (l->l_prev == NULL)
798 /* No other module loaded. This happens only in the static library,
799 or in rtld under --verify. */
800 GL(dl_ns)[l->l_ns]._ns_loaded = NULL;
801 else
802 l->l_prev->l_next = NULL;
803 --GL(dl_ns)[l->l_ns]._ns_nloaded;
804 free (l);
806 free (realname);
808 if (r != NULL)
810 r->r_state = RT_CONSISTENT;
811 _dl_debug_state ();
814 _dl_signal_error (code, name, NULL, msg);
818 /* Map in the shared object NAME, actually located in REALNAME, and already
819 opened on FD. */
821 #ifndef EXTERNAL_MAP_FROM_FD
822 static
823 #endif
824 struct link_map *
825 _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
826 char *realname, struct link_map *loader, int l_type,
827 int mode, void **stack_endp, Lmid_t nsid)
829 struct link_map *l = NULL;
830 const ElfW(Ehdr) *header;
831 const ElfW(Phdr) *phdr;
832 const ElfW(Phdr) *ph;
833 size_t maplength;
834 int type;
835 struct stat64 st;
836 /* Initialize to keep the compiler happy. */
837 const char *errstring = NULL;
838 int errval = 0;
839 struct r_debug *r = _dl_debug_initialize (0, nsid);
840 bool make_consistent = false;
842 /* Get file information. */
843 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st) < 0, 0))
845 errstring = N_("cannot stat shared object");
846 call_lose_errno:
847 errval = errno;
848 call_lose:
849 lose (errval, fd, name, realname, l, errstring,
850 make_consistent ? r : NULL);
853 /* Look again to see if the real name matched another already loaded. */
854 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
855 if (l->l_removed == 0 && l->l_ino == st.st_ino && l->l_dev == st.st_dev)
857 /* The object is already loaded.
858 Just bump its reference count and return it. */
859 __close (fd);
861 /* If the name is not in the list of names for this object add
862 it. */
863 free (realname);
864 add_name_to_object (l, name);
866 return l;
869 #ifdef SHARED
870 /* When loading into a namespace other than the base one we must
871 avoid loading ld.so since there can only be one copy. Ever. */
872 if (__builtin_expect (nsid != LM_ID_BASE, 0)
873 && ((st.st_ino == GL(dl_rtld_map).l_ino
874 && st.st_dev == GL(dl_rtld_map).l_dev)
875 || _dl_name_match_p (name, &GL(dl_rtld_map))))
877 /* This is indeed ld.so. Create a new link_map which refers to
878 the real one for almost everything. */
879 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
880 if (l == NULL)
881 goto fail_new;
883 /* Refer to the real descriptor. */
884 l->l_real = &GL(dl_rtld_map);
886 /* No need to bump the refcount of the real object, ld.so will
887 never be unloaded. */
888 __close (fd);
890 return l;
892 #endif
894 if (mode & RTLD_NOLOAD)
895 /* We are not supposed to load the object unless it is already
896 loaded. So return now. */
897 return NULL;
899 /* Print debugging message. */
900 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
901 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
903 /* This is the ELF header. We read it in `open_verify'. */
904 header = (void *) fbp->buf;
906 #ifndef MAP_ANON
907 # define MAP_ANON 0
908 if (_dl_zerofd == -1)
910 _dl_zerofd = _dl_sysdep_open_zero_fill ();
911 if (_dl_zerofd == -1)
913 __close (fd);
914 _dl_signal_error (errno, NULL, NULL,
915 N_("cannot open zero fill device"));
918 #endif
920 /* Signal that we are going to add new objects. */
921 if (r->r_state == RT_CONSISTENT)
923 #ifdef SHARED
924 /* Auditing checkpoint: we are going to add new objects. */
925 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
927 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
928 /* Do not call the functions for any auditing object. */
929 if (head->l_auditing == 0)
931 struct audit_ifaces *afct = GLRO(dl_audit);
932 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
934 if (afct->activity != NULL)
935 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD);
937 afct = afct->next;
941 #endif
943 /* Notify the debugger we have added some objects. We need to
944 call _dl_debug_initialize in a static program in case dynamic
945 linking has not been used before. */
946 r->r_state = RT_ADD;
947 _dl_debug_state ();
948 make_consistent = true;
950 else
951 assert (r->r_state == RT_ADD);
953 /* Enter the new object in the list of loaded objects. */
954 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
955 if (__builtin_expect (l == NULL, 0))
957 #ifdef SHARED
958 fail_new:
959 #endif
960 errstring = N_("cannot create shared object descriptor");
961 goto call_lose_errno;
964 /* Extract the remaining details we need from the ELF header
965 and then read in the program header table. */
966 l->l_entry = header->e_entry;
967 type = header->e_type;
968 l->l_phnum = header->e_phnum;
970 maplength = header->e_phnum * sizeof (ElfW(Phdr));
971 if (header->e_phoff + maplength <= (size_t) fbp->len)
972 phdr = (void *) (fbp->buf + header->e_phoff);
973 else
975 phdr = alloca (maplength);
976 __lseek (fd, header->e_phoff, SEEK_SET);
977 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
979 errstring = N_("cannot read file data");
980 goto call_lose_errno;
984 /* Presumed absent PT_GNU_STACK. */
985 uint_fast16_t stack_flags = PF_R|PF_W|PF_X;
988 /* Scan the program header table, collecting its load commands. */
989 struct loadcmd
991 ElfW(Addr) mapstart, mapend, dataend, allocend;
992 off_t mapoff;
993 int prot;
994 } loadcmds[l->l_phnum], *c;
995 size_t nloadcmds = 0;
996 bool has_holes = false;
998 /* The struct is initialized to zero so this is not necessary:
999 l->l_ld = 0;
1000 l->l_phdr = 0;
1001 l->l_addr = 0; */
1002 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
1003 switch (ph->p_type)
1005 /* These entries tell us where to find things once the file's
1006 segments are mapped in. We record the addresses it says
1007 verbatim, and later correct for the run-time load address. */
1008 case PT_DYNAMIC:
1009 l->l_ld = (void *) ph->p_vaddr;
1010 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1011 break;
1013 case PT_PHDR:
1014 l->l_phdr = (void *) ph->p_vaddr;
1015 break;
1017 case PT_LOAD:
1018 /* A load command tells us to map in part of the file.
1019 We record the load commands and process them all later. */
1020 if (__builtin_expect ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0,
1023 errstring = N_("ELF load command alignment not page-aligned");
1024 goto call_lose;
1026 if (__builtin_expect (((ph->p_vaddr - ph->p_offset)
1027 & (ph->p_align - 1)) != 0, 0))
1029 errstring
1030 = N_("ELF load command address/offset not properly aligned");
1031 goto call_lose;
1034 c = &loadcmds[nloadcmds++];
1035 c->mapstart = ph->p_vaddr & ~(GLRO(dl_pagesize) - 1);
1036 c->mapend = ((ph->p_vaddr + ph->p_filesz + GLRO(dl_pagesize) - 1)
1037 & ~(GLRO(dl_pagesize) - 1));
1038 c->dataend = ph->p_vaddr + ph->p_filesz;
1039 c->allocend = ph->p_vaddr + ph->p_memsz;
1040 c->mapoff = ph->p_offset & ~(GLRO(dl_pagesize) - 1);
1042 /* Determine whether there is a gap between the last segment
1043 and this one. */
1044 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1045 has_holes = true;
1047 /* Optimize a common case. */
1048 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1049 c->prot = (PF_TO_PROT
1050 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1051 #else
1052 c->prot = 0;
1053 if (ph->p_flags & PF_R)
1054 c->prot |= PROT_READ;
1055 if (ph->p_flags & PF_W)
1056 c->prot |= PROT_WRITE;
1057 if (ph->p_flags & PF_X)
1058 c->prot |= PROT_EXEC;
1059 #endif
1060 break;
1062 case PT_TLS:
1063 #ifdef USE_TLS
1064 if (ph->p_memsz == 0)
1065 /* Nothing to do for an empty segment. */
1066 break;
1068 l->l_tls_blocksize = ph->p_memsz;
1069 l->l_tls_align = ph->p_align;
1070 if (ph->p_align == 0)
1071 l->l_tls_firstbyte_offset = 0;
1072 else
1073 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1074 l->l_tls_initimage_size = ph->p_filesz;
1075 /* Since we don't know the load address yet only store the
1076 offset. We will adjust it later. */
1077 l->l_tls_initimage = (void *) ph->p_vaddr;
1079 /* If not loading the initial set of shared libraries,
1080 check whether we should permit loading a TLS segment. */
1081 if (__builtin_expect (l->l_type == lt_library, 1)
1082 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1083 not set up TLS data structures, so don't use them now. */
1084 || __builtin_expect (GL(dl_tls_dtv_slotinfo_list) != NULL, 1))
1086 /* Assign the next available module ID. */
1087 l->l_tls_modid = _dl_next_tls_modid ();
1088 break;
1091 # ifdef SHARED
1092 if (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0)
1093 /* We are loading the executable itself when the dynamic linker
1094 was executed directly. The setup will happen later. */
1095 break;
1097 /* In a static binary there is no way to tell if we dynamically
1098 loaded libpthread. */
1099 if (GL(dl_error_catch_tsd) == &_dl_initial_error_catch_tsd)
1100 # endif
1102 /* We have not yet loaded libpthread.
1103 We can do the TLS setup right now! */
1105 void *tcb;
1107 /* The first call allocates TLS bookkeeping data structures.
1108 Then we allocate the TCB for the initial thread. */
1109 if (__builtin_expect (_dl_tls_setup (), 0)
1110 || __builtin_expect ((tcb = _dl_allocate_tls (NULL)) == NULL,
1113 errval = ENOMEM;
1114 errstring = N_("\
1115 cannot allocate TLS data structures for initial thread");
1116 goto call_lose;
1119 /* Now we install the TCB in the thread register. */
1120 errstring = TLS_INIT_TP (tcb, 0);
1121 if (__builtin_expect (errstring == NULL, 1))
1123 /* Now we are all good. */
1124 l->l_tls_modid = ++GL(dl_tls_max_dtv_idx);
1125 break;
1128 /* The kernel is too old or somesuch. */
1129 errval = 0;
1130 _dl_deallocate_tls (tcb, 1);
1131 goto call_lose;
1133 #endif
1135 /* Uh-oh, the binary expects TLS support but we cannot
1136 provide it. */
1137 errval = 0;
1138 errstring = N_("cannot handle TLS data");
1139 goto call_lose;
1140 break;
1142 case PT_GNU_STACK:
1143 stack_flags = ph->p_flags;
1144 break;
1146 case PT_GNU_RELRO:
1147 l->l_relro_addr = ph->p_vaddr;
1148 l->l_relro_size = ph->p_memsz;
1149 break;
1152 if (__builtin_expect (nloadcmds == 0, 0))
1154 /* This only happens for a bogus object that will be caught with
1155 another error below. But we don't want to go through the
1156 calculations below using NLOADCMDS - 1. */
1157 errstring = N_("object file has no loadable segments");
1158 goto call_lose;
1161 /* Now process the load commands and map segments into memory. */
1162 c = loadcmds;
1164 /* Length of the sections to be loaded. */
1165 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
1167 if (__builtin_expect (type, ET_DYN) == ET_DYN)
1169 /* This is a position-independent shared object. We can let the
1170 kernel map it anywhere it likes, but we must have space for all
1171 the segments in their specified positions relative to the first.
1172 So we map the first segment without MAP_FIXED, but with its
1173 extent increased to cover all the segments. Then we remove
1174 access from excess portion, and there is known sufficient space
1175 there to remap from the later segments.
1177 As a refinement, sometimes we have an address that we would
1178 prefer to map such objects at; but this is only a preference,
1179 the OS can do whatever it likes. */
1180 ElfW(Addr) mappref;
1181 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength,
1182 c->mapstart & GLRO(dl_use_load_bias))
1183 - MAP_BASE_ADDR (l));
1185 /* Remember which part of the address space this object uses. */
1186 l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
1187 c->prot,
1188 MAP_COPY|MAP_FILE,
1189 fd, c->mapoff);
1190 if (__builtin_expect ((void *) l->l_map_start == MAP_FAILED, 0))
1192 map_error:
1193 errstring = N_("failed to map segment from shared object");
1194 goto call_lose_errno;
1197 l->l_map_end = l->l_map_start + maplength;
1198 l->l_addr = l->l_map_start - c->mapstart;
1200 if (has_holes)
1201 /* Change protection on the excess portion to disallow all access;
1202 the portions we do not remap later will be inaccessible as if
1203 unallocated. Then jump into the normal segment-mapping loop to
1204 handle the portion of the segment past the end of the file
1205 mapping. */
1206 __mprotect ((caddr_t) (l->l_addr + c->mapend),
1207 loadcmds[nloadcmds - 1].mapstart - c->mapend,
1208 PROT_NONE);
1210 goto postmap;
1213 /* This object is loaded at a fixed address. This must never
1214 happen for objects loaded with dlopen(). */
1215 if (__builtin_expect ((mode & __RTLD_OPENEXEC) == 0, 0))
1217 errstring = N_("cannot dynamically load executable");
1218 goto call_lose;
1221 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
1222 fixed. */
1223 ELF_FIXED_ADDRESS (loader, c->mapstart);
1226 /* Remember which part of the address space this object uses. */
1227 l->l_map_start = c->mapstart + l->l_addr;
1228 l->l_map_end = l->l_map_start + maplength;
1230 while (c < &loadcmds[nloadcmds])
1232 if (c->mapend > c->mapstart
1233 /* Map the segment contents from the file. */
1234 && (__mmap ((void *) (l->l_addr + c->mapstart),
1235 c->mapend - c->mapstart, c->prot,
1236 MAP_FIXED|MAP_COPY|MAP_FILE,
1237 fd, c->mapoff)
1238 == MAP_FAILED))
1239 goto map_error;
1241 postmap:
1242 if (c->prot & PROT_EXEC)
1243 l->l_text_end = l->l_addr + c->mapend;
1245 if (l->l_phdr == 0
1246 && (ElfW(Off)) c->mapoff <= header->e_phoff
1247 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
1248 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1249 /* Found the program header in this segment. */
1250 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
1252 if (c->allocend > c->dataend)
1254 /* Extra zero pages should appear at the end of this segment,
1255 after the data mapped from the file. */
1256 ElfW(Addr) zero, zeroend, zeropage;
1258 zero = l->l_addr + c->dataend;
1259 zeroend = l->l_addr + c->allocend;
1260 zeropage = ((zero + GLRO(dl_pagesize) - 1)
1261 & ~(GLRO(dl_pagesize) - 1));
1263 if (zeroend < zeropage)
1264 /* All the extra data is in the last page of the segment.
1265 We can just zero it. */
1266 zeropage = zeroend;
1268 if (zeropage > zero)
1270 /* Zero the final part of the last page of the segment. */
1271 if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
1273 /* Dag nab it. */
1274 if (__mprotect ((caddr_t) (zero
1275 & ~(GLRO(dl_pagesize) - 1)),
1276 GLRO(dl_pagesize), c->prot|PROT_WRITE) < 0)
1278 errstring = N_("cannot change memory protections");
1279 goto call_lose_errno;
1282 memset ((void *) zero, '\0', zeropage - zero);
1283 if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
1284 __mprotect ((caddr_t) (zero & ~(GLRO(dl_pagesize) - 1)),
1285 GLRO(dl_pagesize), c->prot);
1288 if (zeroend > zeropage)
1290 /* Map the remaining zero pages in from the zero fill FD. */
1291 caddr_t mapat;
1292 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1293 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1294 ANONFD, 0);
1295 if (__builtin_expect (mapat == MAP_FAILED, 0))
1297 errstring = N_("cannot map zero-fill pages");
1298 goto call_lose_errno;
1303 ++c;
1307 if (l->l_ld == 0)
1309 if (__builtin_expect (type == ET_DYN, 0))
1311 errstring = N_("object file has no dynamic section");
1312 goto call_lose;
1315 else
1316 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1318 elf_get_dynamic_info (l, NULL);
1320 /* Make sure we are not dlopen'ing an object that has the
1321 DF_1_NOOPEN flag set. */
1322 if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1323 && (mode & __RTLD_DLOPEN))
1325 /* We are not supposed to load this object. Free all resources. */
1326 __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1328 if (!l->l_libname->dont_free)
1329 free (l->l_libname);
1331 if (l->l_phdr_allocated)
1332 free ((void *) l->l_phdr);
1334 errstring = N_("shared object cannot be dlopen()ed");
1335 goto call_lose;
1338 if (l->l_phdr == NULL)
1340 /* The program header is not contained in any of the segments.
1341 We have to allocate memory ourself and copy it over from out
1342 temporary place. */
1343 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1344 * sizeof (ElfW(Phdr)));
1345 if (newp == NULL)
1347 errstring = N_("cannot allocate memory for program header");
1348 goto call_lose_errno;
1351 l->l_phdr = memcpy (newp, phdr,
1352 (header->e_phnum * sizeof (ElfW(Phdr))));
1353 l->l_phdr_allocated = 1;
1355 else
1356 /* Adjust the PT_PHDR value by the runtime load address. */
1357 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1359 if (__builtin_expect ((stack_flags &~ GL(dl_stack_flags)) & PF_X, 0))
1361 if (__builtin_expect (__check_caller (RETURN_ADDRESS (0), allow_ldso),
1362 0) != 0)
1364 errstring = N_("invalid caller");
1365 goto call_lose;
1368 /* The stack is presently not executable, but this module
1369 requires that it be executable. We must change the
1370 protection of the variable which contains the flags used in
1371 the mprotect calls. */
1372 #if defined HAVE_Z_RELRO && defined SHARED
1373 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1375 const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
1376 const size_t s = (uintptr_t) (&__stack_prot + 1) - p;
1378 struct link_map *const m = &GL(dl_rtld_map);
1379 const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr
1380 + m->l_relro_size)
1381 & -GLRO(dl_pagesize));
1382 if (__builtin_expect (p + s <= relro_end, 1))
1384 /* The variable lies in the region protected by RELRO. */
1385 __mprotect ((void *) p, s, PROT_READ|PROT_WRITE);
1386 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1387 __mprotect ((void *) p, s, PROT_READ);
1389 else
1390 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1392 else
1393 #endif
1394 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1396 #ifdef check_consistency
1397 check_consistency ();
1398 #endif
1400 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1401 if (errval)
1403 errstring = N_("\
1404 cannot enable executable stack as shared object requires");
1405 goto call_lose;
1409 #ifdef USE_TLS
1410 /* Adjust the address of the TLS initialization image. */
1411 if (l->l_tls_initimage != NULL)
1412 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1413 #endif
1415 /* We are done mapping in the file. We no longer need the descriptor. */
1416 if (__builtin_expect (__close (fd) != 0, 0))
1418 errstring = N_("cannot close file descriptor");
1419 goto call_lose_errno;
1421 /* Signal that we closed the file. */
1422 fd = -1;
1424 if (l->l_type == lt_library && type == ET_EXEC)
1425 l->l_type = lt_executable;
1427 l->l_entry += l->l_addr;
1429 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
1430 _dl_debug_printf ("\
1431 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1432 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1433 (int) sizeof (void *) * 2,
1434 (unsigned long int) l->l_ld,
1435 (int) sizeof (void *) * 2,
1436 (unsigned long int) l->l_addr,
1437 (int) sizeof (void *) * 2, maplength,
1438 (int) sizeof (void *) * 2,
1439 (unsigned long int) l->l_entry,
1440 (int) sizeof (void *) * 2,
1441 (unsigned long int) l->l_phdr,
1442 (int) sizeof (void *) * 2, l->l_phnum);
1444 /* Set up the symbol hash table. */
1445 _dl_setup_hash (l);
1447 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1448 have to do this for the main map. */
1449 if ((mode & RTLD_DEEPBIND) == 0
1450 && __builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0)
1451 && &l->l_searchlist != l->l_scope[0])
1453 /* Create an appropriate searchlist. It contains only this map.
1454 This is the definition of DT_SYMBOLIC in SysVr4. */
1455 l->l_symbolic_searchlist.r_list =
1456 (struct link_map **) malloc (sizeof (struct link_map *));
1458 if (l->l_symbolic_searchlist.r_list == NULL)
1460 errstring = N_("cannot create searchlist");
1461 goto call_lose_errno;
1464 l->l_symbolic_searchlist.r_list[0] = l;
1465 l->l_symbolic_searchlist.r_nlist = 1;
1467 /* Now move the existing entries one back. */
1468 memmove (&l->l_scope[1], &l->l_scope[0],
1469 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1471 /* Now add the new entry. */
1472 l->l_scope[0] = &l->l_symbolic_searchlist;
1475 /* Remember whether this object must be initialized first. */
1476 if (l->l_flags_1 & DF_1_INITFIRST)
1477 GL(dl_initfirst) = l;
1479 /* Finally the file information. */
1480 l->l_dev = st.st_dev;
1481 l->l_ino = st.st_ino;
1483 /* When we profile the SONAME might be needed for something else but
1484 loading. Add it right away. */
1485 if (__builtin_expect (GLRO(dl_profile) != NULL, 0)
1486 && l->l_info[DT_SONAME] != NULL)
1487 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1488 + l->l_info[DT_SONAME]->d_un.d_val));
1490 #ifdef SHARED
1491 /* Auditing checkpoint: we have a new object. */
1492 if (__builtin_expect (GLRO(dl_naudit) > 0, 0)
1493 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1495 struct audit_ifaces *afct = GLRO(dl_audit);
1496 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1498 if (afct->objopen != NULL)
1500 l->l_audit[cnt].bindflags
1501 = afct->objopen (l, nsid, &l->l_audit[cnt].cookie);
1503 l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0;
1506 afct = afct->next;
1509 #endif
1511 return l;
1514 /* Print search path. */
1515 static void
1516 print_search_path (struct r_search_path_elem **list,
1517 const char *what, const char *name)
1519 char buf[max_dirnamelen + max_capstrlen];
1520 int first = 1;
1522 _dl_debug_printf (" search path=");
1524 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1526 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1527 size_t cnt;
1529 for (cnt = 0; cnt < ncapstr; ++cnt)
1530 if ((*list)->status[cnt] != nonexisting)
1532 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1533 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1534 cp[0] = '\0';
1535 else
1536 cp[-1] = '\0';
1538 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1539 first = 0;
1542 ++list;
1545 if (name != NULL)
1546 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1547 name[0] ? name : rtld_progname);
1548 else
1549 _dl_debug_printf_c ("\t\t(%s)\n", what);
1552 /* Open a file and verify it is an ELF file for this architecture. We
1553 ignore only ELF files for other architectures. Non-ELF files and
1554 ELF files with different header information cause fatal errors since
1555 this could mean there is something wrong in the installation and the
1556 user might want to know about this. */
1557 static int
1558 open_verify (const char *name, struct filebuf *fbp, struct link_map *loader,
1559 int whatcode, bool *found_other_class, bool free_name)
1561 /* This is the expected ELF header. */
1562 #define ELF32_CLASS ELFCLASS32
1563 #define ELF64_CLASS ELFCLASS64
1564 #ifndef VALID_ELF_HEADER
1565 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1566 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1567 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1568 #endif
1569 static const unsigned char expected[EI_PAD] =
1571 [EI_MAG0] = ELFMAG0,
1572 [EI_MAG1] = ELFMAG1,
1573 [EI_MAG2] = ELFMAG2,
1574 [EI_MAG3] = ELFMAG3,
1575 [EI_CLASS] = ELFW(CLASS),
1576 [EI_DATA] = byteorder,
1577 [EI_VERSION] = EV_CURRENT,
1578 [EI_OSABI] = ELFOSABI_SYSV,
1579 [EI_ABIVERSION] = 0
1581 static const struct
1583 ElfW(Word) vendorlen;
1584 ElfW(Word) datalen;
1585 ElfW(Word) type;
1586 char vendor[4];
1587 } expected_note = { 4, 16, 1, "GNU" };
1588 /* Initialize it to make the compiler happy. */
1589 const char *errstring = NULL;
1590 int errval = 0;
1592 #ifdef SHARED
1593 /* Give the auditing libraries a chance. */
1594 if (__builtin_expect (GLRO(dl_naudit) > 0, 0) && whatcode != 0
1595 && loader->l_auditing == 0)
1597 struct audit_ifaces *afct = GLRO(dl_audit);
1598 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1600 if (afct->objsearch != NULL)
1602 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1603 whatcode);
1604 if (name == NULL)
1605 /* Ignore the path. */
1606 return -1;
1609 afct = afct->next;
1612 #endif
1614 /* Open the file. We always open files read-only. */
1615 int fd = __open (name, O_RDONLY);
1616 if (fd != -1)
1618 ElfW(Ehdr) *ehdr;
1619 ElfW(Phdr) *phdr, *ph;
1620 ElfW(Word) *abi_note, abi_note_buf[8];
1621 unsigned int osversion;
1622 size_t maplength;
1624 /* We successfully openened the file. Now verify it is a file
1625 we can use. */
1626 __set_errno (0);
1627 fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf));
1629 /* This is where the ELF header is loaded. */
1630 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1631 ehdr = (ElfW(Ehdr) *) fbp->buf;
1633 /* Now run the tests. */
1634 if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0))
1636 errval = errno;
1637 errstring = (errval == 0
1638 ? N_("file too short") : N_("cannot read file data"));
1639 call_lose:
1640 if (free_name)
1642 char *realname = (char *) name;
1643 name = strdupa (realname);
1644 free (realname);
1646 lose (errval, fd, name, NULL, NULL, errstring, NULL);
1649 /* See whether the ELF header is what we expect. */
1650 if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1651 EI_PAD), 0))
1653 /* Something is wrong. */
1654 if (*(Elf32_Word *) &ehdr->e_ident !=
1655 #if BYTE_ORDER == LITTLE_ENDIAN
1656 ((ELFMAG0 << (EI_MAG0 * 8)) |
1657 (ELFMAG1 << (EI_MAG1 * 8)) |
1658 (ELFMAG2 << (EI_MAG2 * 8)) |
1659 (ELFMAG3 << (EI_MAG3 * 8)))
1660 #else
1661 ((ELFMAG0 << (EI_MAG3 * 8)) |
1662 (ELFMAG1 << (EI_MAG2 * 8)) |
1663 (ELFMAG2 << (EI_MAG1 * 8)) |
1664 (ELFMAG3 << (EI_MAG0 * 8)))
1665 #endif
1667 errstring = N_("invalid ELF header");
1668 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1670 /* This is not a fatal error. On architectures where
1671 32-bit and 64-bit binaries can be run this might
1672 happen. */
1673 *found_other_class = true;
1674 goto close_and_out;
1676 else if (ehdr->e_ident[EI_DATA] != byteorder)
1678 if (BYTE_ORDER == BIG_ENDIAN)
1679 errstring = N_("ELF file data encoding not big-endian");
1680 else
1681 errstring = N_("ELF file data encoding not little-endian");
1683 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1684 errstring
1685 = N_("ELF file version ident does not match current one");
1686 /* XXX We should be able so set system specific versions which are
1687 allowed here. */
1688 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1689 errstring = N_("ELF file OS ABI invalid");
1690 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION]))
1691 errstring = N_("ELF file ABI version invalid");
1692 else
1693 /* Otherwise we don't know what went wrong. */
1694 errstring = N_("internal error");
1696 goto call_lose;
1699 if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT)
1701 errstring = N_("ELF file version does not match current one");
1702 goto call_lose;
1704 if (! __builtin_expect (elf_machine_matches_host (ehdr), 1))
1705 goto close_and_out;
1706 else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN
1707 && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC)
1709 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1710 goto call_lose;
1712 else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
1713 != sizeof (ElfW(Phdr)))
1715 errstring = N_("ELF file's phentsize not the expected size");
1716 goto call_lose;
1719 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1720 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1721 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1722 else
1724 phdr = alloca (maplength);
1725 __lseek (fd, ehdr->e_phoff, SEEK_SET);
1726 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1728 read_error:
1729 errval = errno;
1730 errstring = N_("cannot read file data");
1731 goto call_lose;
1735 /* Check .note.ABI-tag if present. */
1736 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1737 if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
1739 if (ph->p_offset + 32 <= (size_t) fbp->len)
1740 abi_note = (void *) (fbp->buf + ph->p_offset);
1741 else
1743 __lseek (fd, ph->p_offset, SEEK_SET);
1744 if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
1745 goto read_error;
1747 abi_note = abi_note_buf;
1750 if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1751 continue;
1753 osversion = (abi_note[5] & 0xff) * 65536
1754 + (abi_note[6] & 0xff) * 256
1755 + (abi_note[7] & 0xff);
1756 if (abi_note[4] != __ABI_TAG_OS
1757 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1759 close_and_out:
1760 __close (fd);
1761 __set_errno (ENOENT);
1762 fd = -1;
1765 break;
1769 return fd;
1772 /* Try to open NAME in one of the directories in *DIRSP.
1773 Return the fd, or -1. If successful, fill in *REALNAME
1774 with the malloc'd full directory name. If it turns out
1775 that none of the directories in *DIRSP exists, *DIRSP is
1776 replaced with (void *) -1, and the old value is free()d
1777 if MAY_FREE_DIRS is true. */
1779 static int
1780 open_path (const char *name, size_t namelen, int preloaded,
1781 struct r_search_path_struct *sps, char **realname,
1782 struct filebuf *fbp, struct link_map *loader, int whatcode,
1783 bool *found_other_class)
1785 struct r_search_path_elem **dirs = sps->dirs;
1786 char *buf;
1787 int fd = -1;
1788 const char *current_what = NULL;
1789 int any = 0;
1791 if (__builtin_expect (dirs == NULL, 0))
1792 /* We're called before _dl_init_paths when loading the main executable
1793 given on the command line when rtld is run directly. */
1794 return -1;
1796 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1799 struct r_search_path_elem *this_dir = *dirs;
1800 size_t buflen = 0;
1801 size_t cnt;
1802 char *edp;
1803 int here_any = 0;
1804 int err;
1806 /* If we are debugging the search for libraries print the path
1807 now if it hasn't happened now. */
1808 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)
1809 && current_what != this_dir->what)
1811 current_what = this_dir->what;
1812 print_search_path (dirs, current_what, this_dir->where);
1815 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1816 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1818 /* Skip this directory if we know it does not exist. */
1819 if (this_dir->status[cnt] == nonexisting)
1820 continue;
1822 buflen =
1823 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1824 capstr[cnt].len),
1825 name, namelen)
1826 - buf);
1828 /* Print name we try if this is wanted. */
1829 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1830 _dl_debug_printf (" trying file=%s\n", buf);
1832 fd = open_verify (buf, fbp, loader, whatcode, found_other_class,
1833 false);
1834 if (this_dir->status[cnt] == unknown)
1836 if (fd != -1)
1837 this_dir->status[cnt] = existing;
1838 /* Do not update the directory information when loading
1839 auditing code. We must try to disturb the program as
1840 little as possible. */
1841 else if (loader == NULL
1842 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_audit == 0)
1844 /* We failed to open machine dependent library. Let's
1845 test whether there is any directory at all. */
1846 struct stat64 st;
1848 buf[buflen - namelen - 1] = '\0';
1850 if (__xstat64 (_STAT_VER, buf, &st) != 0
1851 || ! S_ISDIR (st.st_mode))
1852 /* The directory does not exist or it is no directory. */
1853 this_dir->status[cnt] = nonexisting;
1854 else
1855 this_dir->status[cnt] = existing;
1859 /* Remember whether we found any existing directory. */
1860 here_any |= this_dir->status[cnt] != nonexisting;
1862 if (fd != -1 && __builtin_expect (preloaded, 0)
1863 && INTUSE(__libc_enable_secure))
1865 /* This is an extra security effort to make sure nobody can
1866 preload broken shared objects which are in the trusted
1867 directories and so exploit the bugs. */
1868 struct stat64 st;
1870 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1871 || (st.st_mode & S_ISUID) == 0)
1873 /* The shared object cannot be tested for being SUID
1874 or this bit is not set. In this case we must not
1875 use this object. */
1876 __close (fd);
1877 fd = -1;
1878 /* We simply ignore the file, signal this by setting
1879 the error value which would have been set by `open'. */
1880 errno = ENOENT;
1885 if (fd != -1)
1887 *realname = (char *) malloc (buflen);
1888 if (*realname != NULL)
1890 memcpy (*realname, buf, buflen);
1891 return fd;
1893 else
1895 /* No memory for the name, we certainly won't be able
1896 to load and link it. */
1897 __close (fd);
1898 return -1;
1901 if (here_any && (err = errno) != ENOENT && err != EACCES)
1902 /* The file exists and is readable, but something went wrong. */
1903 return -1;
1905 /* Remember whether we found anything. */
1906 any |= here_any;
1908 while (*++dirs != NULL);
1910 /* Remove the whole path if none of the directories exists. */
1911 if (__builtin_expect (! any, 0))
1913 /* Paths which were allocated using the minimal malloc() in ld.so
1914 must not be freed using the general free() in libc. */
1915 if (sps->malloced)
1916 free (sps->dirs);
1917 #ifdef HAVE_Z_RELRO
1918 /* rtld_search_dirs is attribute_relro, therefore avoid writing
1919 into it. */
1920 if (sps != &rtld_search_dirs)
1921 #endif
1922 sps->dirs = (void *) -1;
1925 return -1;
1928 /* Map in the shared object file NAME. */
1930 struct link_map *
1931 internal_function
1932 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1933 int type, int trace_mode, int mode, Lmid_t nsid)
1935 int fd;
1936 char *realname;
1937 char *name_copy;
1938 struct link_map *l;
1939 struct filebuf fb;
1941 assert (nsid >= 0);
1942 assert (nsid < DL_NNS);
1944 /* Look for this name among those already loaded. */
1945 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
1947 /* If the requested name matches the soname of a loaded object,
1948 use that object. Elide this check for names that have not
1949 yet been opened. */
1950 if (__builtin_expect (l->l_faked, 0) != 0
1951 || __builtin_expect (l->l_removed, 0) != 0)
1952 continue;
1953 if (!_dl_name_match_p (name, l))
1955 const char *soname;
1957 if (__builtin_expect (l->l_soname_added, 1)
1958 || l->l_info[DT_SONAME] == NULL)
1959 continue;
1961 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1962 + l->l_info[DT_SONAME]->d_un.d_val);
1963 if (strcmp (name, soname) != 0)
1964 continue;
1966 /* We have a match on a new name -- cache it. */
1967 add_name_to_object (l, soname);
1968 l->l_soname_added = 1;
1971 /* We have a match. */
1972 return l;
1975 /* Display information if we are debugging. */
1976 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0)
1977 && loader != NULL)
1978 _dl_debug_printf ("\nfile=%s [%lu]; needed by %s [%lu]\n", name, nsid,
1979 loader->l_name[0]
1980 ? loader->l_name : rtld_progname, loader->l_ns);
1982 #ifdef SHARED
1983 /* Give the auditing libraries a chance to change the name before we
1984 try anything. */
1985 if (__builtin_expect (GLRO(dl_naudit) > 0, 0)
1986 && (loader == NULL || loader->l_auditing == 0))
1988 struct audit_ifaces *afct = GLRO(dl_audit);
1989 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1991 if (afct->objsearch != NULL)
1993 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1994 LA_SER_ORIG);
1995 if (name == NULL)
1997 /* Do not try anything further. */
1998 fd = -1;
1999 goto no_file;
2003 afct = afct->next;
2006 #endif
2008 /* Will be true if we found a DSO which is of the other ELF class. */
2009 bool found_other_class = false;
2011 if (strchr (name, '/') == NULL)
2013 /* Search for NAME in several places. */
2015 size_t namelen = strlen (name) + 1;
2017 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2018 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
2020 fd = -1;
2022 /* When the object has the RUNPATH information we don't use any
2023 RPATHs. */
2024 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
2026 /* First try the DT_RPATH of the dependent object that caused NAME
2027 to be loaded. Then that object's dependent, and on up. */
2028 for (l = loader; fd == -1 && l; l = l->l_loader)
2029 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2030 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
2031 &realname, &fb, loader, LA_SER_RUNPATH,
2032 &found_other_class);
2034 /* If dynamically linked, try the DT_RPATH of the executable
2035 itself. NB: we do this for lookups in any namespace. */
2036 if (fd == -1)
2038 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2039 if (l && l->l_type != lt_loaded && l != loader
2040 && cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2041 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
2042 &realname, &fb, loader ?: l, LA_SER_RUNPATH,
2043 &found_other_class);
2047 /* Try the LD_LIBRARY_PATH environment variable. */
2048 if (fd == -1 && env_path_list.dirs != (void *) -1)
2049 fd = open_path (name, namelen, preloaded, &env_path_list,
2050 &realname, &fb,
2051 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
2052 LA_SER_LIBPATH, &found_other_class);
2054 /* Look at the RUNPATH information for this binary. */
2055 if (fd == -1 && loader != NULL
2056 && cache_rpath (loader, &loader->l_runpath_dirs,
2057 DT_RUNPATH, "RUNPATH"))
2058 fd = open_path (name, namelen, preloaded,
2059 &loader->l_runpath_dirs, &realname, &fb, loader,
2060 LA_SER_RUNPATH, &found_other_class);
2062 if (fd == -1
2063 && (__builtin_expect (! preloaded, 1)
2064 || ! INTUSE(__libc_enable_secure)))
2066 /* Check the list of libraries in the file /etc/ld.so.cache,
2067 for compatibility with Linux's ldconfig program. */
2068 const char *cached = _dl_load_cache_lookup (name);
2070 if (cached != NULL)
2072 #ifdef SHARED
2073 // XXX Correct to unconditionally default to namespace 0?
2074 l = loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2075 #else
2076 l = loader;
2077 #endif
2079 /* If the loader has the DF_1_NODEFLIB flag set we must not
2080 use a cache entry from any of these directories. */
2081 if (
2082 #ifndef SHARED
2083 /* 'l' is always != NULL for dynamically linked objects. */
2084 l != NULL &&
2085 #endif
2086 __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
2088 const char *dirp = system_dirs;
2089 unsigned int cnt = 0;
2093 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2095 /* The prefix matches. Don't use the entry. */
2096 cached = NULL;
2097 break;
2100 dirp += system_dirs_len[cnt] + 1;
2101 ++cnt;
2103 while (cnt < nsystem_dirs_len);
2106 if (cached != NULL)
2108 fd = open_verify (cached,
2109 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2110 LA_SER_CONFIG, &found_other_class, false);
2111 if (__builtin_expect (fd != -1, 1))
2113 realname = local_strdup (cached);
2114 if (realname == NULL)
2116 __close (fd);
2117 fd = -1;
2124 /* Finally, try the default path. */
2125 if (fd == -1
2126 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2127 || __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
2128 && rtld_search_dirs.dirs != (void *) -1)
2129 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
2130 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
2132 /* Add another newline when we are tracing the library loading. */
2133 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2134 _dl_debug_printf ("\n");
2136 else
2138 /* The path may contain dynamic string tokens. */
2139 realname = (loader
2140 ? expand_dynamic_string_token (loader, name)
2141 : local_strdup (name));
2142 if (realname == NULL)
2143 fd = -1;
2144 else
2146 fd = open_verify (realname, &fb,
2147 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0,
2148 &found_other_class, true);
2149 if (__builtin_expect (fd, 0) == -1)
2150 free (realname);
2154 #ifdef SHARED
2155 no_file:
2156 #endif
2157 /* In case the LOADER information has only been provided to get to
2158 the appropriate RUNPATH/RPATH information we do not need it
2159 anymore. */
2160 if (mode & __RTLD_CALLMAP)
2161 loader = NULL;
2163 if (__builtin_expect (fd, 0) == -1)
2165 if (trace_mode
2166 && __builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK, 0) == 0)
2168 /* We haven't found an appropriate library. But since we
2169 are only interested in the list of libraries this isn't
2170 so severe. Fake an entry with all the information we
2171 have. */
2172 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2174 /* Enter the new object in the list of loaded objects. */
2175 if ((name_copy = local_strdup (name)) == NULL
2176 || (l = _dl_new_object (name_copy, name, type, loader,
2177 mode, nsid)) == NULL)
2179 free (name_copy);
2180 _dl_signal_error (ENOMEM, name, NULL,
2181 N_("cannot create shared object descriptor"));
2183 /* Signal that this is a faked entry. */
2184 l->l_faked = 1;
2185 /* Since the descriptor is initialized with zero we do not
2186 have do this here.
2187 l->l_reserved = 0; */
2188 l->l_buckets = &dummy_bucket;
2189 l->l_nbuckets = 1;
2190 l->l_relocated = 1;
2192 return l;
2194 else if (found_other_class)
2195 _dl_signal_error (0, name, NULL,
2196 ELFW(CLASS) == ELFCLASS32
2197 ? N_("wrong ELF class: ELFCLASS64")
2198 : N_("wrong ELF class: ELFCLASS32"));
2199 else
2200 _dl_signal_error (errno, name, NULL,
2201 N_("cannot open shared object file"));
2204 void *stack_end = __libc_stack_end;
2205 return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode,
2206 &stack_end, nsid);
2210 void
2211 internal_function
2212 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2214 if (counting)
2216 si->dls_cnt = 0;
2217 si->dls_size = 0;
2220 unsigned int idx = 0;
2221 char *allocptr = (char *) &si->dls_serpath[si->dls_cnt];
2222 void add_path (const struct r_search_path_struct *sps, unsigned int flags)
2223 # define add_path(sps, flags) add_path(sps, 0) /* XXX */
2225 if (sps->dirs != (void *) -1)
2227 struct r_search_path_elem **dirs = sps->dirs;
2230 const struct r_search_path_elem *const r = *dirs++;
2231 if (counting)
2233 si->dls_cnt++;
2234 si->dls_size += r->dirnamelen;
2236 else
2238 Dl_serpath *const sp = &si->dls_serpath[idx++];
2239 sp->dls_name = allocptr;
2240 allocptr = __mempcpy (allocptr,
2241 r->dirname, r->dirnamelen - 1);
2242 *allocptr++ = '\0';
2243 sp->dls_flags = flags;
2246 while (*dirs != NULL);
2250 /* When the object has the RUNPATH information we don't use any RPATHs. */
2251 if (loader->l_info[DT_RUNPATH] == NULL)
2253 /* First try the DT_RPATH of the dependent object that caused NAME
2254 to be loaded. Then that object's dependent, and on up. */
2256 struct link_map *l = loader;
2259 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2260 add_path (&l->l_rpath_dirs, XXX_RPATH);
2261 l = l->l_loader;
2263 while (l != NULL);
2265 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2266 if (loader->l_ns == LM_ID_BASE)
2268 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2269 if (l != NULL && l->l_type != lt_loaded && l != loader)
2270 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2271 add_path (&l->l_rpath_dirs, XXX_RPATH);
2275 /* Try the LD_LIBRARY_PATH environment variable. */
2276 add_path (&env_path_list, XXX_ENV);
2278 /* Look at the RUNPATH information for this binary. */
2279 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2280 add_path (&loader->l_runpath_dirs, XXX_RUNPATH);
2282 /* XXX
2283 Here is where ld.so.cache gets checked, but we don't have
2284 a way to indicate that in the results for Dl_serinfo. */
2286 /* Finally, try the default path. */
2287 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2288 add_path (&rtld_search_dirs, XXX_default);
2290 if (counting)
2291 /* Count the struct size before the string area, which we didn't
2292 know before we completed dls_cnt. */
2293 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;