* sysdeps/unix/sysv/linux/sparc/bits/poll.h: Add POLLMSG,
[glibc.git] / elf / dl-load.c
blob088954a04f15812796bfe7bf0ceffbc6ca389a8b
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <elf.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <libintl.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <ldsodefs.h>
30 #include <bits/wordsize.h>
31 #include <sys/mman.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include "dynamic-link.h"
36 #include <abi-tag.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.
56 To make up for the lack and avoid the overwriting problem,
57 what Linux does have is MAP_DENYWRITE. This prevents anyone
58 from modifying the file while we have it mapped. */
59 #ifndef MAP_COPY
60 # ifdef MAP_DENYWRITE
61 # define MAP_COPY (MAP_PRIVATE | MAP_DENYWRITE)
62 # else
63 # define MAP_COPY MAP_PRIVATE
64 # endif
65 #endif
67 /* Some systems link their relocatable objects for another base address
68 than 0. We want to know the base address for these such that we can
69 subtract this address from the segment addresses during mapping.
70 This results in a more efficient address space usage. Defaults to
71 zero for almost all systems. */
72 #ifndef MAP_BASE_ADDR
73 # define MAP_BASE_ADDR(l) 0
74 #endif
77 #include <endian.h>
78 #if BYTE_ORDER == BIG_ENDIAN
79 # define byteorder ELFDATA2MSB
80 #elif BYTE_ORDER == LITTLE_ENDIAN
81 # define byteorder ELFDATA2LSB
82 #else
83 # error "Unknown BYTE_ORDER " BYTE_ORDER
84 # define byteorder ELFDATANONE
85 #endif
87 #define STRING(x) __STRING (x)
89 #ifdef MAP_ANON
90 /* The fd is not examined when using MAP_ANON. */
91 # define ANONFD -1
92 #else
93 int _dl_zerofd = -1;
94 # define ANONFD _dl_zerofd
95 #endif
97 /* Handle situations where we have a preferred location in memory for
98 the shared objects. */
99 #ifdef ELF_PREFERRED_ADDRESS_DATA
100 ELF_PREFERRED_ADDRESS_DATA;
101 #endif
102 #ifndef ELF_PREFERRED_ADDRESS
103 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
104 #endif
105 #ifndef ELF_FIXED_ADDRESS
106 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
107 #endif
110 int __stack_prot attribute_hidden attribute_relro
111 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
112 = PROT_GROWSDOWN;
113 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
114 = PROT_GROWSUP;
115 #else
116 = 0;
117 #endif
120 /* Type for the buffer we put the ELF header and hopefully the program
121 header. This buffer does not really have to be too large. In most
122 cases the program header follows the ELF header directly. If this
123 is not the case all bets are off and we can make the header
124 arbitrarily large and still won't get it read. This means the only
125 question is how large are the ELF and program header combined. The
126 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
127 bytes long. Each program header entry is again 32 and 56 bytes
128 long respectively. I.e., even with a file which has 10 program
129 header entries we only have to read 372B/624B respectively. Add to
130 this a bit of margin for program notes and reading 512B and 832B
131 for 32-bit and 64-bit files respecitvely is enough. If this
132 heuristic should really fail for some file the code in
133 `_dl_map_object_from_fd' knows how to recover. */
134 struct filebuf
136 ssize_t len;
137 #if __WORDSIZE == 32
138 # define FILEBUF_SIZE 512
139 #else
140 # define FILEBUF_SIZE 832
141 #endif
142 char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr)))));
145 /* This is the decomposed LD_LIBRARY_PATH search path. */
146 static struct r_search_path_struct env_path_list attribute_relro;
148 /* List of the hardware capabilities we might end up using. */
149 static const struct r_strlenpair *capstr attribute_relro;
150 static size_t ncapstr attribute_relro;
151 static size_t max_capstrlen attribute_relro;
154 /* Get the generated information about the trusted directories. */
155 #include "trusted-dirs.h"
157 static const char system_dirs[] = SYSTEM_DIRS;
158 static const size_t system_dirs_len[] =
160 SYSTEM_DIRS_LEN
162 #define nsystem_dirs_len \
163 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
166 /* Local version of `strdup' function. */
167 static inline char *
168 local_strdup (const char *s)
170 size_t len = strlen (s) + 1;
171 void *new = malloc (len);
173 if (new == NULL)
174 return NULL;
176 return (char *) memcpy (new, s, len);
180 static size_t
181 is_dst (const char *start, const char *name, const char *str,
182 int is_path, int secure)
184 size_t len;
185 bool is_curly = false;
187 if (name[0] == '{')
189 is_curly = true;
190 ++name;
193 len = 0;
194 while (name[len] == str[len] && name[len] != '\0')
195 ++len;
197 if (is_curly)
199 if (name[len] != '}')
200 return 0;
202 /* Point again at the beginning of the name. */
203 --name;
204 /* Skip over closing curly brace and adjust for the --name. */
205 len += 2;
207 else if (name[len] != '\0' && name[len] != '/'
208 && (!is_path || name[len] != ':'))
209 return 0;
211 if (__builtin_expect (secure, 0)
212 && ((name[len] != '\0' && (!is_path || name[len] != ':'))
213 || (name != start + 1 && (!is_path || name[-2] != ':'))))
214 return 0;
216 return len;
220 size_t
221 _dl_dst_count (const char *name, int is_path)
223 const char *const start = name;
224 size_t cnt = 0;
228 size_t len;
230 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
231 is $ORIGIN alone) and it must always appear first in path. */
232 ++name;
233 if ((len = is_dst (start, name, "ORIGIN", is_path,
234 INTUSE(__libc_enable_secure))) != 0
235 || (len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0
236 || (len = is_dst (start, name, "LIB", is_path, 0)) != 0)
237 ++cnt;
239 name = strchr (name + len, '$');
241 while (name != NULL);
243 return cnt;
247 char *
248 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
249 int is_path)
251 const char *const start = name;
252 char *last_elem, *wp;
254 /* Now fill the result path. While copying over the string we keep
255 track of the start of the last path element. When we come accross
256 a DST we copy over the value or (if the value is not available)
257 leave the entire path element out. */
258 last_elem = wp = result;
262 if (__builtin_expect (*name == '$', 0))
264 const char *repl = NULL;
265 size_t len;
267 ++name;
268 if ((len = is_dst (start, name, "ORIGIN", is_path,
269 INTUSE(__libc_enable_secure))) != 0)
270 repl = l->l_origin;
271 else if ((len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0)
272 repl = GLRO(dl_platform);
273 else if ((len = is_dst (start, name, "LIB", is_path, 0)) != 0)
274 repl = DL_DST_LIB;
276 if (repl != NULL && repl != (const char *) -1)
278 wp = __stpcpy (wp, repl);
279 name += len;
281 else if (len > 1)
283 /* We cannot use this path element, the value of the
284 replacement is unknown. */
285 wp = last_elem;
286 name += len;
287 while (*name != '\0' && (!is_path || *name != ':'))
288 ++name;
290 else
291 /* No DST we recognize. */
292 *wp++ = '$';
294 else
296 *wp++ = *name++;
297 if (is_path && *name == ':')
298 last_elem = wp;
301 while (*name != '\0');
303 *wp = '\0';
305 return result;
309 /* Return copy of argument with all recognized dynamic string tokens
310 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
311 might not be possible to determine the path from which the object
312 belonging to the map is loaded. In this case the path element
313 containing $ORIGIN is left out. */
314 static char *
315 expand_dynamic_string_token (struct link_map *l, const char *s)
317 /* We make two runs over the string. First we determine how large the
318 resulting string is and then we copy it over. Since this is now
319 frequently executed operation we are looking here not for performance
320 but rather for code size. */
321 size_t cnt;
322 size_t total;
323 char *result;
325 /* Determine the number of DST elements. */
326 cnt = DL_DST_COUNT (s, 1);
328 /* If we do not have to replace anything simply copy the string. */
329 if (__builtin_expect (cnt, 0) == 0)
330 return local_strdup (s);
332 /* Determine the length of the substituted string. */
333 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
335 /* Allocate the necessary memory. */
336 result = (char *) malloc (total + 1);
337 if (result == NULL)
338 return NULL;
340 return _dl_dst_substitute (l, s, result, 1);
344 /* Add `name' to the list of names for a particular shared object.
345 `name' is expected to have been allocated with malloc and will
346 be freed if the shared object already has this name.
347 Returns false if the object already had this name. */
348 static void
349 internal_function
350 add_name_to_object (struct link_map *l, const char *name)
352 struct libname_list *lnp, *lastp;
353 struct libname_list *newname;
354 size_t name_len;
356 lastp = NULL;
357 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
358 if (strcmp (name, lnp->name) == 0)
359 return;
361 name_len = strlen (name) + 1;
362 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
363 if (newname == NULL)
365 /* No more memory. */
366 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
367 return;
369 /* The object should have a libname set from _dl_new_object. */
370 assert (lastp != NULL);
372 newname->name = memcpy (newname + 1, name, name_len);
373 newname->next = NULL;
374 newname->dont_free = 0;
375 lastp->next = newname;
378 /* Standard search directories. */
379 static struct r_search_path_struct rtld_search_dirs attribute_relro;
381 static size_t max_dirnamelen;
383 static struct r_search_path_elem **
384 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
385 int check_trusted, const char *what, const char *where)
387 char *cp;
388 size_t nelems = 0;
390 while ((cp = __strsep (&rpath, sep)) != NULL)
392 struct r_search_path_elem *dirp;
393 size_t len = strlen (cp);
395 /* `strsep' can pass an empty string. This has to be
396 interpreted as `use the current directory'. */
397 if (len == 0)
399 static const char curwd[] = "./";
400 cp = (char *) curwd;
403 /* Remove trailing slashes (except for "/"). */
404 while (len > 1 && cp[len - 1] == '/')
405 --len;
407 /* Now add one if there is none so far. */
408 if (len > 0 && cp[len - 1] != '/')
409 cp[len++] = '/';
411 /* Make sure we don't use untrusted directories if we run SUID. */
412 if (__builtin_expect (check_trusted, 0))
414 const char *trun = system_dirs;
415 size_t idx;
416 int unsecure = 1;
418 /* All trusted directories must be complete names. */
419 if (cp[0] == '/')
421 for (idx = 0; idx < nsystem_dirs_len; ++idx)
423 if (len == system_dirs_len[idx]
424 && memcmp (trun, cp, len) == 0)
426 /* Found it. */
427 unsecure = 0;
428 break;
431 trun += system_dirs_len[idx] + 1;
435 if (unsecure)
436 /* Simply drop this directory. */
437 continue;
440 /* See if this directory is already known. */
441 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
442 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
443 break;
445 if (dirp != NULL)
447 /* It is available, see whether it's on our own list. */
448 size_t cnt;
449 for (cnt = 0; cnt < nelems; ++cnt)
450 if (result[cnt] == dirp)
451 break;
453 if (cnt == nelems)
454 result[nelems++] = dirp;
456 else
458 size_t cnt;
459 enum r_dir_status init_val;
460 size_t where_len = where ? strlen (where) + 1 : 0;
462 /* It's a new directory. Create an entry and add it. */
463 dirp = (struct r_search_path_elem *)
464 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
465 + where_len + len + 1);
466 if (dirp == NULL)
467 _dl_signal_error (ENOMEM, NULL, NULL,
468 N_("cannot create cache for search path"));
470 dirp->dirname = ((char *) dirp + sizeof (*dirp)
471 + ncapstr * sizeof (enum r_dir_status));
472 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
473 dirp->dirnamelen = len;
475 if (len > max_dirnamelen)
476 max_dirnamelen = len;
478 /* We have to make sure all the relative directories are
479 never ignored. The current directory might change and
480 all our saved information would be void. */
481 init_val = cp[0] != '/' ? existing : unknown;
482 for (cnt = 0; cnt < ncapstr; ++cnt)
483 dirp->status[cnt] = init_val;
485 dirp->what = what;
486 if (__builtin_expect (where != NULL, 1))
487 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
488 + (ncapstr * sizeof (enum r_dir_status)),
489 where, where_len);
490 else
491 dirp->where = NULL;
493 dirp->next = GL(dl_all_dirs);
494 GL(dl_all_dirs) = dirp;
496 /* Put it in the result array. */
497 result[nelems++] = dirp;
501 /* Terminate the array. */
502 result[nelems] = NULL;
504 return result;
508 static void
509 internal_function
510 decompose_rpath (struct r_search_path_struct *sps,
511 const char *rpath, struct link_map *l, const char *what)
513 /* Make a copy we can work with. */
514 const char *where = l->l_name;
515 char *copy;
516 char *cp;
517 struct r_search_path_elem **result;
518 size_t nelems;
519 /* Initialize to please the compiler. */
520 const char *errstring = NULL;
522 /* First see whether we must forget the RUNPATH and RPATH from this
523 object. */
524 if (__builtin_expect (GLRO(dl_inhibit_rpath) != NULL, 0)
525 && !INTUSE(__libc_enable_secure))
527 const char *inhp = GLRO(dl_inhibit_rpath);
531 const char *wp = where;
533 while (*inhp == *wp && *wp != '\0')
535 ++inhp;
536 ++wp;
539 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
541 /* This object is on the list of objects for which the
542 RUNPATH and RPATH must not be used. */
543 result = calloc (1, sizeof *result);
544 if (result == NULL)
546 signal_error_cache:
547 errstring = N_("cannot create cache for search path");
548 signal_error:
549 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
552 sps->dirs = result;
553 sps->malloced = 1;
555 return;
558 while (*inhp != '\0')
559 if (*inhp++ == ':')
560 break;
562 while (*inhp != '\0');
565 /* Make a writable copy. At the same time expand possible dynamic
566 string tokens. */
567 copy = expand_dynamic_string_token (l, rpath);
568 if (copy == NULL)
570 errstring = N_("cannot create RUNPATH/RPATH copy");
571 goto signal_error;
574 /* Count the number of necessary elements in the result array. */
575 nelems = 0;
576 for (cp = copy; *cp != '\0'; ++cp)
577 if (*cp == ':')
578 ++nelems;
580 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
581 number of necessary entries. */
582 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
583 * sizeof (*result));
584 if (result == NULL)
585 goto signal_error_cache;
587 fillin_rpath (copy, result, ":", 0, what, where);
589 /* Free the copied RPATH string. `fillin_rpath' make own copies if
590 necessary. */
591 free (copy);
593 sps->dirs = result;
594 /* The caller will change this value if we haven't used a real malloc. */
595 sps->malloced = 1;
598 /* Make sure cached path information is stored in *SP
599 and return true if there are any paths to search there. */
600 static bool
601 cache_rpath (struct link_map *l,
602 struct r_search_path_struct *sp,
603 int tag,
604 const char *what)
606 if (sp->dirs == (void *) -1)
607 return false;
609 if (sp->dirs != NULL)
610 return true;
612 if (l->l_info[tag] == NULL)
614 /* There is no path. */
615 sp->dirs = (void *) -1;
616 return false;
619 /* Make sure the cache information is available. */
620 decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
621 + l->l_info[tag]->d_un.d_val),
622 l, what);
623 return true;
627 void
628 internal_function
629 _dl_init_paths (const char *llp)
631 size_t idx;
632 const char *strp;
633 struct r_search_path_elem *pelem, **aelem;
634 size_t round_size;
635 #ifdef SHARED
636 struct link_map *l;
637 #endif
638 /* Initialize to please the compiler. */
639 const char *errstring = NULL;
641 /* Fill in the information about the application's RPATH and the
642 directories addressed by the LD_LIBRARY_PATH environment variable. */
644 /* Get the capabilities. */
645 capstr = _dl_important_hwcaps (GLRO(dl_platform), GLRO(dl_platformlen),
646 &ncapstr, &max_capstrlen);
648 /* First set up the rest of the default search directory entries. */
649 aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
650 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
651 if (rtld_search_dirs.dirs == NULL)
653 errstring = N_("cannot create search path array");
654 signal_error:
655 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
658 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
659 + ncapstr * sizeof (enum r_dir_status))
660 / sizeof (struct r_search_path_elem));
662 rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
663 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
664 * round_size * sizeof (struct r_search_path_elem));
665 if (rtld_search_dirs.dirs[0] == NULL)
667 errstring = N_("cannot create cache for search path");
668 goto signal_error;
671 rtld_search_dirs.malloced = 0;
672 pelem = GL(dl_all_dirs) = rtld_search_dirs.dirs[0];
673 strp = system_dirs;
674 idx = 0;
678 size_t cnt;
680 *aelem++ = pelem;
682 pelem->what = "system search path";
683 pelem->where = NULL;
685 pelem->dirname = strp;
686 pelem->dirnamelen = system_dirs_len[idx];
687 strp += system_dirs_len[idx] + 1;
689 /* System paths must be absolute. */
690 assert (pelem->dirname[0] == '/');
691 for (cnt = 0; cnt < ncapstr; ++cnt)
692 pelem->status[cnt] = unknown;
694 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
696 pelem += round_size;
698 while (idx < nsystem_dirs_len);
700 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
701 *aelem = NULL;
703 #ifdef SHARED
704 /* This points to the map of the main object. */
705 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
706 if (l != NULL)
708 assert (l->l_type != lt_loaded);
710 if (l->l_info[DT_RUNPATH])
712 /* Allocate room for the search path and fill in information
713 from RUNPATH. */
714 decompose_rpath (&l->l_runpath_dirs,
715 (const void *) (D_PTR (l, l_info[DT_STRTAB])
716 + l->l_info[DT_RUNPATH]->d_un.d_val),
717 l, "RUNPATH");
719 /* The RPATH is ignored. */
720 l->l_rpath_dirs.dirs = (void *) -1;
722 else
724 l->l_runpath_dirs.dirs = (void *) -1;
726 if (l->l_info[DT_RPATH])
728 /* Allocate room for the search path and fill in information
729 from RPATH. */
730 decompose_rpath (&l->l_rpath_dirs,
731 (const void *) (D_PTR (l, l_info[DT_STRTAB])
732 + l->l_info[DT_RPATH]->d_un.d_val),
733 l, "RPATH");
734 l->l_rpath_dirs.malloced = 0;
736 else
737 l->l_rpath_dirs.dirs = (void *) -1;
740 #endif /* SHARED */
742 if (llp != NULL && *llp != '\0')
744 size_t nllp;
745 const char *cp = llp;
746 char *llp_tmp = strdupa (llp);
748 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
749 elements it has. */
750 nllp = 1;
751 while (*cp)
753 if (*cp == ':' || *cp == ';')
754 ++nllp;
755 ++cp;
758 env_path_list.dirs = (struct r_search_path_elem **)
759 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
760 if (env_path_list.dirs == NULL)
762 errstring = N_("cannot create cache for search path");
763 goto signal_error;
766 (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
767 INTUSE(__libc_enable_secure), "LD_LIBRARY_PATH",
768 NULL);
770 if (env_path_list.dirs[0] == NULL)
772 free (env_path_list.dirs);
773 env_path_list.dirs = (void *) -1;
776 env_path_list.malloced = 0;
778 else
779 env_path_list.dirs = (void *) -1;
781 /* Remember the last search directory added at startup. */
782 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
786 static void
787 __attribute__ ((noreturn, noinline))
788 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
789 const char *msg)
791 /* The file might already be closed. */
792 if (fd != -1)
793 (void) __close (fd);
794 if (l != NULL)
796 /* Remove the stillborn object from the list and free it. */
797 assert (l->l_next == NULL);
798 if (l->l_prev == NULL)
799 /* No other module loaded. This happens only in the static library,
800 or in rtld under --verify. */
801 GL(dl_ns)[l->l_ns]._ns_loaded = NULL;
802 else
803 l->l_prev->l_next = NULL;
804 --GL(dl_ns)[l->l_ns]._ns_nloaded;
805 free (l);
807 free (realname);
808 _dl_signal_error (code, name, NULL, msg);
812 /* Map in the shared object NAME, actually located in REALNAME, and already
813 opened on FD. */
815 #ifndef EXTERNAL_MAP_FROM_FD
816 static
817 #endif
818 struct link_map *
819 _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
820 char *realname, struct link_map *loader, int l_type,
821 int mode, void **stack_endp, Lmid_t nsid)
823 struct link_map *l = NULL;
824 const ElfW(Ehdr) *header;
825 const ElfW(Phdr) *phdr;
826 const ElfW(Phdr) *ph;
827 size_t maplength;
828 int type;
829 struct stat64 st;
830 /* Initialize to keep the compiler happy. */
831 const char *errstring = NULL;
832 int errval = 0;
833 struct r_debug *r = _dl_debug_initialize (0, nsid);
834 bool make_consistent = false;
836 /* Get file information. */
837 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st) < 0, 0))
839 errstring = N_("cannot stat shared object");
840 call_lose_errno:
841 errval = errno;
842 call_lose:
843 if (make_consistent)
845 r->r_state = RT_CONSISTENT;
846 _dl_debug_state ();
849 lose (errval, fd, name, realname, l, errstring);
852 /* Look again to see if the real name matched another already loaded. */
853 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
854 if (l->l_removed == 0 && l->l_ino == st.st_ino && l->l_dev == st.st_dev)
856 /* The object is already loaded.
857 Just bump its reference count and return it. */
858 __close (fd);
860 /* If the name is not in the list of names for this object add
861 it. */
862 free (realname);
863 add_name_to_object (l, name);
865 return l;
868 #ifdef SHARED
869 /* When loading into a namespace other than the base one we must
870 avoid loading ld.so since there can only be one copy. Ever. */
871 if (__builtin_expect (nsid != LM_ID_BASE, 0)
872 && ((st.st_ino == GL(dl_rtld_map).l_ino
873 && st.st_dev == GL(dl_rtld_map).l_dev)
874 || _dl_name_match_p (name, &GL(dl_rtld_map))))
876 /* This is indeed ld.so. Create a new link_map which refers to
877 the real one for almost everything. */
878 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
879 if (l == NULL)
880 goto fail_new;
882 /* Refer to the real descriptor. */
883 l->l_real = &GL(dl_rtld_map);
885 /* No need to bump the refcount of the real object, ld.so will
886 never be unloaded. */
887 __close (fd);
889 return l;
891 #endif
893 if (mode & RTLD_NOLOAD)
894 /* We are not supposed to load the object unless it is already
895 loaded. So return now. */
896 return NULL;
898 /* Print debugging message. */
899 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
900 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
902 /* This is the ELF header. We read it in `open_verify'. */
903 header = (void *) fbp->buf;
905 #ifndef MAP_ANON
906 # define MAP_ANON 0
907 if (_dl_zerofd == -1)
909 _dl_zerofd = _dl_sysdep_open_zero_fill ();
910 if (_dl_zerofd == -1)
912 __close (fd);
913 _dl_signal_error (errno, NULL, NULL,
914 N_("cannot open zero fill device"));
917 #endif
919 /* Signal that we are going to add new objects. */
920 if (r->r_state == RT_CONSISTENT)
922 #ifdef SHARED
923 /* Auditing checkpoint: we are going to add new objects. */
924 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
926 struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
927 /* Do not call the functions for any auditing object. */
928 if (head->l_auditing == 0)
930 struct audit_ifaces *afct = GLRO(dl_audit);
931 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
933 if (afct->activity != NULL)
934 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_ADD);
936 afct = afct->next;
940 #endif
942 /* Notify the debugger we have added some objects. We need to
943 call _dl_debug_initialize in a static program in case dynamic
944 linking has not been used before. */
945 r->r_state = RT_ADD;
946 _dl_debug_state ();
947 make_consistent = true;
949 else
950 assert (r->r_state == RT_ADD);
952 /* Enter the new object in the list of loaded objects. */
953 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
954 if (__builtin_expect (l == NULL, 0))
956 #ifdef SHARED
957 fail_new:
958 #endif
959 errstring = N_("cannot create shared object descriptor");
960 goto call_lose_errno;
963 /* Extract the remaining details we need from the ELF header
964 and then read in the program header table. */
965 l->l_entry = header->e_entry;
966 type = header->e_type;
967 l->l_phnum = header->e_phnum;
969 maplength = header->e_phnum * sizeof (ElfW(Phdr));
970 if (header->e_phoff + maplength <= (size_t) fbp->len)
971 phdr = (void *) (fbp->buf + header->e_phoff);
972 else
974 phdr = alloca (maplength);
975 __lseek (fd, header->e_phoff, SEEK_SET);
976 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
978 errstring = N_("cannot read file data");
979 goto call_lose_errno;
983 /* Presumed absent PT_GNU_STACK. */
984 uint_fast16_t stack_flags = PF_R|PF_W|PF_X;
987 /* Scan the program header table, collecting its load commands. */
988 struct loadcmd
990 ElfW(Addr) mapstart, mapend, dataend, allocend;
991 off_t mapoff;
992 int prot;
993 } loadcmds[l->l_phnum], *c;
994 size_t nloadcmds = 0;
995 bool has_holes = false;
997 /* The struct is initialized to zero so this is not necessary:
998 l->l_ld = 0;
999 l->l_phdr = 0;
1000 l->l_addr = 0; */
1001 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
1002 switch (ph->p_type)
1004 /* These entries tell us where to find things once the file's
1005 segments are mapped in. We record the addresses it says
1006 verbatim, and later correct for the run-time load address. */
1007 case PT_DYNAMIC:
1008 l->l_ld = (void *) ph->p_vaddr;
1009 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1010 break;
1012 case PT_PHDR:
1013 l->l_phdr = (void *) ph->p_vaddr;
1014 break;
1016 case PT_LOAD:
1017 /* A load command tells us to map in part of the file.
1018 We record the load commands and process them all later. */
1019 if (__builtin_expect ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0,
1022 errstring = N_("ELF load command alignment not page-aligned");
1023 goto call_lose;
1025 if (__builtin_expect (((ph->p_vaddr - ph->p_offset)
1026 & (ph->p_align - 1)) != 0, 0))
1028 errstring
1029 = N_("ELF load command address/offset not properly aligned");
1030 goto call_lose;
1033 c = &loadcmds[nloadcmds++];
1034 c->mapstart = ph->p_vaddr & ~(GLRO(dl_pagesize) - 1);
1035 c->mapend = ((ph->p_vaddr + ph->p_filesz + GLRO(dl_pagesize) - 1)
1036 & ~(GLRO(dl_pagesize) - 1));
1037 c->dataend = ph->p_vaddr + ph->p_filesz;
1038 c->allocend = ph->p_vaddr + ph->p_memsz;
1039 c->mapoff = ph->p_offset & ~(GLRO(dl_pagesize) - 1);
1041 /* Determine whether there is a gap between the last segment
1042 and this one. */
1043 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1044 has_holes = true;
1046 /* Optimize a common case. */
1047 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1048 c->prot = (PF_TO_PROT
1049 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
1050 #else
1051 c->prot = 0;
1052 if (ph->p_flags & PF_R)
1053 c->prot |= PROT_READ;
1054 if (ph->p_flags & PF_W)
1055 c->prot |= PROT_WRITE;
1056 if (ph->p_flags & PF_X)
1057 c->prot |= PROT_EXEC;
1058 #endif
1059 break;
1061 case PT_TLS:
1062 #ifdef USE_TLS
1063 if (ph->p_memsz == 0)
1064 /* Nothing to do for an empty segment. */
1065 break;
1067 l->l_tls_blocksize = ph->p_memsz;
1068 l->l_tls_align = ph->p_align;
1069 if (ph->p_align == 0)
1070 l->l_tls_firstbyte_offset = 0;
1071 else
1072 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
1073 l->l_tls_initimage_size = ph->p_filesz;
1074 /* Since we don't know the load address yet only store the
1075 offset. We will adjust it later. */
1076 l->l_tls_initimage = (void *) ph->p_vaddr;
1078 /* If not loading the initial set of shared libraries,
1079 check whether we should permit loading a TLS segment. */
1080 if (__builtin_expect (l->l_type == lt_library, 1)
1081 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1082 not set up TLS data structures, so don't use them now. */
1083 || __builtin_expect (GL(dl_tls_dtv_slotinfo_list) != NULL, 1))
1085 /* Assign the next available module ID. */
1086 l->l_tls_modid = _dl_next_tls_modid ();
1087 break;
1090 # ifdef SHARED
1091 if (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0)
1092 /* We are loading the executable itself when the dynamic linker
1093 was executed directly. The setup will happen later. */
1094 break;
1096 /* In a static binary there is no way to tell if we dynamically
1097 loaded libpthread. */
1098 if (GL(dl_error_catch_tsd) == &_dl_initial_error_catch_tsd)
1099 # endif
1101 /* We have not yet loaded libpthread.
1102 We can do the TLS setup right now! */
1104 void *tcb;
1106 /* The first call allocates TLS bookkeeping data structures.
1107 Then we allocate the TCB for the initial thread. */
1108 if (__builtin_expect (_dl_tls_setup (), 0)
1109 || __builtin_expect ((tcb = _dl_allocate_tls (NULL)) == NULL,
1112 errval = ENOMEM;
1113 errstring = N_("\
1114 cannot allocate TLS data structures for initial thread");
1115 goto call_lose;
1118 /* Now we install the TCB in the thread register. */
1119 errstring = TLS_INIT_TP (tcb, 0);
1120 if (__builtin_expect (errstring == NULL, 1))
1122 /* Now we are all good. */
1123 l->l_tls_modid = ++GL(dl_tls_max_dtv_idx);
1124 break;
1127 /* The kernel is too old or somesuch. */
1128 errval = 0;
1129 _dl_deallocate_tls (tcb, 1);
1130 goto call_lose;
1132 #endif
1134 /* Uh-oh, the binary expects TLS support but we cannot
1135 provide it. */
1136 errval = 0;
1137 errstring = N_("cannot handle TLS data");
1138 goto call_lose;
1139 break;
1141 case PT_GNU_STACK:
1142 stack_flags = ph->p_flags;
1143 break;
1145 case PT_GNU_RELRO:
1146 l->l_relro_addr = ph->p_vaddr;
1147 l->l_relro_size = ph->p_memsz;
1148 break;
1151 if (__builtin_expect (nloadcmds == 0, 0))
1153 /* This only happens for a bogus object that will be caught with
1154 another error below. But we don't want to go through the
1155 calculations below using NLOADCMDS - 1. */
1156 errstring = N_("object file has no loadable segments");
1157 goto call_lose;
1160 /* Now process the load commands and map segments into memory. */
1161 c = loadcmds;
1163 /* Length of the sections to be loaded. */
1164 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
1166 if (__builtin_expect (type, ET_DYN) == ET_DYN)
1168 /* This is a position-independent shared object. We can let the
1169 kernel map it anywhere it likes, but we must have space for all
1170 the segments in their specified positions relative to the first.
1171 So we map the first segment without MAP_FIXED, but with its
1172 extent increased to cover all the segments. Then we remove
1173 access from excess portion, and there is known sufficient space
1174 there to remap from the later segments.
1176 As a refinement, sometimes we have an address that we would
1177 prefer to map such objects at; but this is only a preference,
1178 the OS can do whatever it likes. */
1179 ElfW(Addr) mappref;
1180 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength,
1181 c->mapstart & GLRO(dl_use_load_bias))
1182 - MAP_BASE_ADDR (l));
1184 /* Remember which part of the address space this object uses. */
1185 l->l_map_start = (ElfW(Addr)) __mmap ((void *) mappref, maplength,
1186 c->prot,
1187 MAP_COPY|MAP_FILE,
1188 fd, c->mapoff);
1189 if (__builtin_expect ((void *) l->l_map_start == MAP_FAILED, 0))
1191 map_error:
1192 errstring = N_("failed to map segment from shared object");
1193 goto call_lose_errno;
1196 l->l_map_end = l->l_map_start + maplength;
1197 l->l_addr = l->l_map_start - c->mapstart;
1199 if (has_holes)
1200 /* Change protection on the excess portion to disallow all access;
1201 the portions we do not remap later will be inaccessible as if
1202 unallocated. Then jump into the normal segment-mapping loop to
1203 handle the portion of the segment past the end of the file
1204 mapping. */
1205 __mprotect ((caddr_t) (l->l_addr + c->mapend),
1206 loadcmds[nloadcmds - 1].mapstart - c->mapend,
1207 PROT_NONE);
1209 goto postmap;
1212 /* This object is loaded at a fixed address. This must never
1213 happen for objects loaded with dlopen(). */
1214 if (__builtin_expect ((mode & __RTLD_OPENEXEC) == 0, 0))
1216 errstring = N_("cannot dynamically load executable");
1217 goto call_lose;
1220 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
1221 fixed. */
1222 ELF_FIXED_ADDRESS (loader, c->mapstart);
1225 /* Remember which part of the address space this object uses. */
1226 l->l_map_start = c->mapstart + l->l_addr;
1227 l->l_map_end = l->l_map_start + maplength;
1229 while (c < &loadcmds[nloadcmds])
1231 if (c->mapend > c->mapstart
1232 /* Map the segment contents from the file. */
1233 && (__mmap ((void *) (l->l_addr + c->mapstart),
1234 c->mapend - c->mapstart, c->prot,
1235 MAP_FIXED|MAP_COPY|MAP_FILE,
1236 fd, c->mapoff)
1237 == MAP_FAILED))
1238 goto map_error;
1240 postmap:
1241 if (c->prot & PROT_EXEC)
1242 l->l_text_end = l->l_addr + c->mapend;
1244 if (l->l_phdr == 0
1245 && (ElfW(Off)) c->mapoff <= header->e_phoff
1246 && ((size_t) (c->mapend - c->mapstart + c->mapoff)
1247 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
1248 /* Found the program header in this segment. */
1249 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
1251 if (c->allocend > c->dataend)
1253 /* Extra zero pages should appear at the end of this segment,
1254 after the data mapped from the file. */
1255 ElfW(Addr) zero, zeroend, zeropage;
1257 zero = l->l_addr + c->dataend;
1258 zeroend = l->l_addr + c->allocend;
1259 zeropage = ((zero + GLRO(dl_pagesize) - 1)
1260 & ~(GLRO(dl_pagesize) - 1));
1262 if (zeroend < zeropage)
1263 /* All the extra data is in the last page of the segment.
1264 We can just zero it. */
1265 zeropage = zeroend;
1267 if (zeropage > zero)
1269 /* Zero the final part of the last page of the segment. */
1270 if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
1272 /* Dag nab it. */
1273 if (__mprotect ((caddr_t) (zero
1274 & ~(GLRO(dl_pagesize) - 1)),
1275 GLRO(dl_pagesize), c->prot|PROT_WRITE) < 0)
1277 errstring = N_("cannot change memory protections");
1278 goto call_lose_errno;
1281 memset ((void *) zero, '\0', zeropage - zero);
1282 if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
1283 __mprotect ((caddr_t) (zero & ~(GLRO(dl_pagesize) - 1)),
1284 GLRO(dl_pagesize), c->prot);
1287 if (zeroend > zeropage)
1289 /* Map the remaining zero pages in from the zero fill FD. */
1290 caddr_t mapat;
1291 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1292 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1293 ANONFD, 0);
1294 if (__builtin_expect (mapat == MAP_FAILED, 0))
1296 errstring = N_("cannot map zero-fill pages");
1297 goto call_lose_errno;
1302 ++c;
1306 if (l->l_ld == 0)
1308 if (__builtin_expect (type == ET_DYN, 0))
1310 errstring = N_("object file has no dynamic section");
1311 goto call_lose;
1314 else
1315 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1317 elf_get_dynamic_info (l, NULL);
1319 /* Make sure we are not dlopen'ing an object that has the
1320 DF_1_NOOPEN flag set. */
1321 if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1322 && (mode & __RTLD_DLOPEN))
1324 /* We are not supposed to load this object. Free all resources. */
1325 __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1327 if (!l->l_libname->dont_free)
1328 free (l->l_libname);
1330 if (l->l_phdr_allocated)
1331 free ((void *) l->l_phdr);
1333 errstring = N_("shared object cannot be dlopen()ed");
1334 goto call_lose;
1337 if (l->l_phdr == NULL)
1339 /* The program header is not contained in any of the segments.
1340 We have to allocate memory ourself and copy it over from out
1341 temporary place. */
1342 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1343 * sizeof (ElfW(Phdr)));
1344 if (newp == NULL)
1346 errstring = N_("cannot allocate memory for program header");
1347 goto call_lose_errno;
1350 l->l_phdr = memcpy (newp, phdr,
1351 (header->e_phnum * sizeof (ElfW(Phdr))));
1352 l->l_phdr_allocated = 1;
1354 else
1355 /* Adjust the PT_PHDR value by the runtime load address. */
1356 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1358 if (__builtin_expect ((stack_flags &~ GL(dl_stack_flags)) & PF_X, 0))
1360 if (__builtin_expect (__check_caller (RETURN_ADDRESS (0), allow_ldso),
1361 0) != 0)
1363 errstring = N_("invalid caller");
1364 goto call_lose;
1367 /* The stack is presently not executable, but this module
1368 requires that it be executable. We must change the
1369 protection of the variable which contains the flags used in
1370 the mprotect calls. */
1371 #if defined HAVE_Z_RELRO && defined SHARED
1372 if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
1374 const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
1375 const size_t s = (uintptr_t) (&__stack_prot + 1) - p;
1377 struct link_map *const m = &GL(dl_rtld_map);
1378 const uintptr_t relro_end = ((m->l_addr + m->l_relro_addr
1379 + m->l_relro_size)
1380 & -GLRO(dl_pagesize));
1381 if (__builtin_expect (p + s <= relro_end, 1))
1383 /* The variable lies in the region protected by RELRO. */
1384 __mprotect ((void *) p, s, PROT_READ|PROT_WRITE);
1385 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1386 __mprotect ((void *) p, s, PROT_READ);
1388 else
1389 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1391 else
1392 #endif
1393 __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
1395 #ifdef check_consistency
1396 check_consistency ();
1397 #endif
1399 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
1400 if (errval)
1402 errstring = N_("\
1403 cannot enable executable stack as shared object requires");
1404 goto call_lose;
1408 #ifdef USE_TLS
1409 /* Adjust the address of the TLS initialization image. */
1410 if (l->l_tls_initimage != NULL)
1411 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
1412 #endif
1414 /* We are done mapping in the file. We no longer need the descriptor. */
1415 if (__builtin_expect (__close (fd) != 0, 0))
1417 errstring = N_("cannot close file descriptor");
1418 goto call_lose_errno;
1420 /* Signal that we closed the file. */
1421 fd = -1;
1423 if (l->l_type == lt_library && type == ET_EXEC)
1424 l->l_type = lt_executable;
1426 l->l_entry += l->l_addr;
1428 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
1429 _dl_debug_printf ("\
1430 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1431 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1432 (int) sizeof (void *) * 2,
1433 (unsigned long int) l->l_ld,
1434 (int) sizeof (void *) * 2,
1435 (unsigned long int) l->l_addr,
1436 (int) sizeof (void *) * 2, maplength,
1437 (int) sizeof (void *) * 2,
1438 (unsigned long int) l->l_entry,
1439 (int) sizeof (void *) * 2,
1440 (unsigned long int) l->l_phdr,
1441 (int) sizeof (void *) * 2, l->l_phnum);
1443 /* Set up the symbol hash table. */
1444 _dl_setup_hash (l);
1446 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1447 have to do this for the main map. */
1448 if ((mode & RTLD_DEEPBIND) == 0
1449 && __builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0)
1450 && &l->l_searchlist != l->l_scope[0])
1452 /* Create an appropriate searchlist. It contains only this map.
1453 This is the definition of DT_SYMBOLIC in SysVr4. */
1454 l->l_symbolic_searchlist.r_list =
1455 (struct link_map **) malloc (sizeof (struct link_map *));
1457 if (l->l_symbolic_searchlist.r_list == NULL)
1459 errstring = N_("cannot create searchlist");
1460 goto call_lose_errno;
1463 l->l_symbolic_searchlist.r_list[0] = l;
1464 l->l_symbolic_searchlist.r_nlist = 1;
1466 /* Now move the existing entries one back. */
1467 memmove (&l->l_scope[1], &l->l_scope[0],
1468 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1470 /* Now add the new entry. */
1471 l->l_scope[0] = &l->l_symbolic_searchlist;
1474 /* Remember whether this object must be initialized first. */
1475 if (l->l_flags_1 & DF_1_INITFIRST)
1476 GL(dl_initfirst) = l;
1478 /* Finally the file information. */
1479 l->l_dev = st.st_dev;
1480 l->l_ino = st.st_ino;
1482 /* When we profile the SONAME might be needed for something else but
1483 loading. Add it right away. */
1484 if (__builtin_expect (GLRO(dl_profile) != NULL, 0)
1485 && l->l_info[DT_SONAME] != NULL)
1486 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1487 + l->l_info[DT_SONAME]->d_un.d_val));
1489 #ifdef SHARED
1490 /* Auditing checkpoint: we have a new object. */
1491 if (__builtin_expect (GLRO(dl_naudit) > 0, 0)
1492 && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1494 struct audit_ifaces *afct = GLRO(dl_audit);
1495 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1497 if (afct->objopen != NULL)
1499 l->l_audit[cnt].bindflags
1500 = afct->objopen (l, nsid, &l->l_audit[cnt].cookie);
1502 l->l_audit_any_plt |= l->l_audit[cnt].bindflags != 0;
1505 afct = afct->next;
1508 #endif
1510 return l;
1513 /* Print search path. */
1514 static void
1515 print_search_path (struct r_search_path_elem **list,
1516 const char *what, const char *name)
1518 char buf[max_dirnamelen + max_capstrlen];
1519 int first = 1;
1521 _dl_debug_printf (" search path=");
1523 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1525 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1526 size_t cnt;
1528 for (cnt = 0; cnt < ncapstr; ++cnt)
1529 if ((*list)->status[cnt] != nonexisting)
1531 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1532 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1533 cp[0] = '\0';
1534 else
1535 cp[-1] = '\0';
1537 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1538 first = 0;
1541 ++list;
1544 if (name != NULL)
1545 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1546 name[0] ? name : rtld_progname);
1547 else
1548 _dl_debug_printf_c ("\t\t(%s)\n", what);
1551 /* Open a file and verify it is an ELF file for this architecture. We
1552 ignore only ELF files for other architectures. Non-ELF files and
1553 ELF files with different header information cause fatal errors since
1554 this could mean there is something wrong in the installation and the
1555 user might want to know about this. */
1556 static int
1557 open_verify (const char *name, struct filebuf *fbp, struct link_map *loader,
1558 int whatcode, bool *found_other_class, bool free_name)
1560 /* This is the expected ELF header. */
1561 #define ELF32_CLASS ELFCLASS32
1562 #define ELF64_CLASS ELFCLASS64
1563 #ifndef VALID_ELF_HEADER
1564 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1565 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1566 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1567 #endif
1568 static const unsigned char expected[EI_PAD] =
1570 [EI_MAG0] = ELFMAG0,
1571 [EI_MAG1] = ELFMAG1,
1572 [EI_MAG2] = ELFMAG2,
1573 [EI_MAG3] = ELFMAG3,
1574 [EI_CLASS] = ELFW(CLASS),
1575 [EI_DATA] = byteorder,
1576 [EI_VERSION] = EV_CURRENT,
1577 [EI_OSABI] = ELFOSABI_SYSV,
1578 [EI_ABIVERSION] = 0
1580 static const struct
1582 ElfW(Word) vendorlen;
1583 ElfW(Word) datalen;
1584 ElfW(Word) type;
1585 char vendor[4];
1586 } expected_note = { 4, 16, 1, "GNU" };
1587 /* Initialize it to make the compiler happy. */
1588 const char *errstring = NULL;
1589 int errval = 0;
1591 #ifdef SHARED
1592 /* Give the auditing libraries a chance. */
1593 if (__builtin_expect (GLRO(dl_naudit) > 0, 0) && whatcode != 0
1594 && loader->l_auditing == 0)
1596 struct audit_ifaces *afct = GLRO(dl_audit);
1597 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1599 if (afct->objsearch != NULL)
1601 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1602 whatcode);
1603 if (name == NULL)
1604 /* Ignore the path. */
1605 return -1;
1608 afct = afct->next;
1611 #endif
1613 /* Open the file. We always open files read-only. */
1614 int fd = __open (name, O_RDONLY);
1615 if (fd != -1)
1617 ElfW(Ehdr) *ehdr;
1618 ElfW(Phdr) *phdr, *ph;
1619 ElfW(Word) *abi_note, abi_note_buf[8];
1620 unsigned int osversion;
1621 size_t maplength;
1623 /* We successfully openened the file. Now verify it is a file
1624 we can use. */
1625 __set_errno (0);
1626 fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf));
1628 /* This is where the ELF header is loaded. */
1629 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1630 ehdr = (ElfW(Ehdr) *) fbp->buf;
1632 /* Now run the tests. */
1633 if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0))
1635 errval = errno;
1636 errstring = (errval == 0
1637 ? N_("file too short") : N_("cannot read file data"));
1638 call_lose:
1639 if (free_name)
1641 char *realname = (char *) name;
1642 name = strdupa (realname);
1643 free (realname);
1645 lose (errval, fd, name, NULL, NULL, errstring);
1648 /* See whether the ELF header is what we expect. */
1649 if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1650 EI_PAD), 0))
1652 /* Something is wrong. */
1653 if (*(Elf32_Word *) &ehdr->e_ident !=
1654 #if BYTE_ORDER == LITTLE_ENDIAN
1655 ((ELFMAG0 << (EI_MAG0 * 8)) |
1656 (ELFMAG1 << (EI_MAG1 * 8)) |
1657 (ELFMAG2 << (EI_MAG2 * 8)) |
1658 (ELFMAG3 << (EI_MAG3 * 8)))
1659 #else
1660 ((ELFMAG0 << (EI_MAG3 * 8)) |
1661 (ELFMAG1 << (EI_MAG2 * 8)) |
1662 (ELFMAG2 << (EI_MAG1 * 8)) |
1663 (ELFMAG3 << (EI_MAG0 * 8)))
1664 #endif
1666 errstring = N_("invalid ELF header");
1667 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1669 /* This is not a fatal error. On architectures where
1670 32-bit and 64-bit binaries can be run this might
1671 happen. */
1672 *found_other_class = true;
1673 goto close_and_out;
1675 else if (ehdr->e_ident[EI_DATA] != byteorder)
1677 if (BYTE_ORDER == BIG_ENDIAN)
1678 errstring = N_("ELF file data encoding not big-endian");
1679 else
1680 errstring = N_("ELF file data encoding not little-endian");
1682 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1683 errstring
1684 = N_("ELF file version ident does not match current one");
1685 /* XXX We should be able so set system specific versions which are
1686 allowed here. */
1687 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1688 errstring = N_("ELF file OS ABI invalid");
1689 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION]))
1690 errstring = N_("ELF file ABI version invalid");
1691 else
1692 /* Otherwise we don't know what went wrong. */
1693 errstring = N_("internal error");
1695 goto call_lose;
1698 if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT)
1700 errstring = N_("ELF file version does not match current one");
1701 goto call_lose;
1703 if (! __builtin_expect (elf_machine_matches_host (ehdr), 1))
1704 goto close_and_out;
1705 else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN
1706 && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC)
1708 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
1709 goto call_lose;
1711 else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
1712 != sizeof (ElfW(Phdr)))
1714 errstring = N_("ELF file's phentsize not the expected size");
1715 goto call_lose;
1718 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1719 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
1720 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1721 else
1723 phdr = alloca (maplength);
1724 __lseek (fd, ehdr->e_phoff, SEEK_SET);
1725 if ((size_t) __libc_read (fd, (void *) phdr, maplength) != maplength)
1727 read_error:
1728 errval = errno;
1729 errstring = N_("cannot read file data");
1730 goto call_lose;
1734 /* Check .note.ABI-tag if present. */
1735 for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1736 if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
1738 if (ph->p_offset + 32 <= (size_t) fbp->len)
1739 abi_note = (void *) (fbp->buf + ph->p_offset);
1740 else
1742 __lseek (fd, ph->p_offset, SEEK_SET);
1743 if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
1744 goto read_error;
1746 abi_note = abi_note_buf;
1749 if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1750 continue;
1752 osversion = (abi_note[5] & 0xff) * 65536
1753 + (abi_note[6] & 0xff) * 256
1754 + (abi_note[7] & 0xff);
1755 if (abi_note[4] != __ABI_TAG_OS
1756 || (GLRO(dl_osversion) && GLRO(dl_osversion) < osversion))
1758 close_and_out:
1759 __close (fd);
1760 __set_errno (ENOENT);
1761 fd = -1;
1764 break;
1768 return fd;
1771 /* Try to open NAME in one of the directories in *DIRSP.
1772 Return the fd, or -1. If successful, fill in *REALNAME
1773 with the malloc'd full directory name. If it turns out
1774 that none of the directories in *DIRSP exists, *DIRSP is
1775 replaced with (void *) -1, and the old value is free()d
1776 if MAY_FREE_DIRS is true. */
1778 static int
1779 open_path (const char *name, size_t namelen, int preloaded,
1780 struct r_search_path_struct *sps, char **realname,
1781 struct filebuf *fbp, struct link_map *loader, int whatcode,
1782 bool *found_other_class)
1784 struct r_search_path_elem **dirs = sps->dirs;
1785 char *buf;
1786 int fd = -1;
1787 const char *current_what = NULL;
1788 int any = 0;
1790 if (__builtin_expect (dirs == NULL, 0))
1791 /* We're called before _dl_init_paths when loading the main executable
1792 given on the command line when rtld is run directly. */
1793 return -1;
1795 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1798 struct r_search_path_elem *this_dir = *dirs;
1799 size_t buflen = 0;
1800 size_t cnt;
1801 char *edp;
1802 int here_any = 0;
1803 int err;
1805 /* If we are debugging the search for libraries print the path
1806 now if it hasn't happened now. */
1807 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)
1808 && current_what != this_dir->what)
1810 current_what = this_dir->what;
1811 print_search_path (dirs, current_what, this_dir->where);
1814 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1815 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1817 /* Skip this directory if we know it does not exist. */
1818 if (this_dir->status[cnt] == nonexisting)
1819 continue;
1821 buflen =
1822 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1823 capstr[cnt].len),
1824 name, namelen)
1825 - buf);
1827 /* Print name we try if this is wanted. */
1828 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1829 _dl_debug_printf (" trying file=%s\n", buf);
1831 fd = open_verify (buf, fbp, loader, whatcode, found_other_class,
1832 false);
1833 if (this_dir->status[cnt] == unknown)
1835 if (fd != -1)
1836 this_dir->status[cnt] = existing;
1837 /* Do not update the directory information when loading
1838 auditing code. We must try to disturb the program as
1839 little as possible. */
1840 else if (loader == NULL
1841 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_audit == 0)
1843 /* We failed to open machine dependent library. Let's
1844 test whether there is any directory at all. */
1845 struct stat64 st;
1847 buf[buflen - namelen - 1] = '\0';
1849 if (__xstat64 (_STAT_VER, buf, &st) != 0
1850 || ! S_ISDIR (st.st_mode))
1851 /* The directory does not exist or it is no directory. */
1852 this_dir->status[cnt] = nonexisting;
1853 else
1854 this_dir->status[cnt] = existing;
1858 /* Remember whether we found any existing directory. */
1859 here_any |= this_dir->status[cnt] != nonexisting;
1861 if (fd != -1 && __builtin_expect (preloaded, 0)
1862 && INTUSE(__libc_enable_secure))
1864 /* This is an extra security effort to make sure nobody can
1865 preload broken shared objects which are in the trusted
1866 directories and so exploit the bugs. */
1867 struct stat64 st;
1869 if (__fxstat64 (_STAT_VER, fd, &st) != 0
1870 || (st.st_mode & S_ISUID) == 0)
1872 /* The shared object cannot be tested for being SUID
1873 or this bit is not set. In this case we must not
1874 use this object. */
1875 __close (fd);
1876 fd = -1;
1877 /* We simply ignore the file, signal this by setting
1878 the error value which would have been set by `open'. */
1879 errno = ENOENT;
1884 if (fd != -1)
1886 *realname = (char *) malloc (buflen);
1887 if (*realname != NULL)
1889 memcpy (*realname, buf, buflen);
1890 return fd;
1892 else
1894 /* No memory for the name, we certainly won't be able
1895 to load and link it. */
1896 __close (fd);
1897 return -1;
1900 if (here_any && (err = errno) != ENOENT && err != EACCES)
1901 /* The file exists and is readable, but something went wrong. */
1902 return -1;
1904 /* Remember whether we found anything. */
1905 any |= here_any;
1907 while (*++dirs != NULL);
1909 /* Remove the whole path if none of the directories exists. */
1910 if (__builtin_expect (! any, 0))
1912 /* Paths which were allocated using the minimal malloc() in ld.so
1913 must not be freed using the general free() in libc. */
1914 if (sps->malloced)
1915 free (sps->dirs);
1916 #ifdef HAVE_Z_RELRO
1917 /* rtld_search_dirs is attribute_relro, therefore avoid writing
1918 into it. */
1919 if (sps != &rtld_search_dirs)
1920 #endif
1921 sps->dirs = (void *) -1;
1924 return -1;
1927 /* Map in the shared object file NAME. */
1929 struct link_map *
1930 internal_function
1931 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1932 int type, int trace_mode, int mode, Lmid_t nsid)
1934 int fd;
1935 char *realname;
1936 char *name_copy;
1937 struct link_map *l;
1938 struct filebuf fb;
1940 assert (nsid >= 0);
1941 assert (nsid < DL_NNS);
1943 /* Look for this name among those already loaded. */
1944 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
1946 /* If the requested name matches the soname of a loaded object,
1947 use that object. Elide this check for names that have not
1948 yet been opened. */
1949 if (__builtin_expect (l->l_faked, 0) != 0
1950 || __builtin_expect (l->l_removed, 0) != 0)
1951 continue;
1952 if (!_dl_name_match_p (name, l))
1954 const char *soname;
1956 if (__builtin_expect (l->l_soname_added, 1)
1957 || l->l_info[DT_SONAME] == NULL)
1958 continue;
1960 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1961 + l->l_info[DT_SONAME]->d_un.d_val);
1962 if (strcmp (name, soname) != 0)
1963 continue;
1965 /* We have a match on a new name -- cache it. */
1966 add_name_to_object (l, soname);
1967 l->l_soname_added = 1;
1970 /* We have a match. */
1971 return l;
1974 /* Display information if we are debugging. */
1975 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0)
1976 && loader != NULL)
1977 _dl_debug_printf ("\nfile=%s [%lu]; needed by %s [%lu]\n", name, nsid,
1978 loader->l_name[0]
1979 ? loader->l_name : rtld_progname, loader->l_ns);
1981 #ifdef SHARED
1982 /* Give the auditing libraries a chance to change the name before we
1983 try anything. */
1984 if (__builtin_expect (GLRO(dl_naudit) > 0, 0)
1985 && (loader == NULL || loader->l_auditing == 0))
1987 struct audit_ifaces *afct = GLRO(dl_audit);
1988 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1990 if (afct->objsearch != NULL)
1992 name = afct->objsearch (name, &loader->l_audit[cnt].cookie,
1993 LA_SER_ORIG);
1994 if (name == NULL)
1996 /* Do not try anything further. */
1997 fd = -1;
1998 goto no_file;
2002 afct = afct->next;
2005 #endif
2007 /* Will be true if we found a DSO which is of the other ELF class. */
2008 bool found_other_class = false;
2010 if (strchr (name, '/') == NULL)
2012 /* Search for NAME in several places. */
2014 size_t namelen = strlen (name) + 1;
2016 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2017 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
2019 fd = -1;
2021 /* When the object has the RUNPATH information we don't use any
2022 RPATHs. */
2023 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
2025 /* First try the DT_RPATH of the dependent object that caused NAME
2026 to be loaded. Then that object's dependent, and on up. */
2027 for (l = loader; fd == -1 && l; l = l->l_loader)
2028 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2029 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
2030 &realname, &fb, loader, LA_SER_RUNPATH,
2031 &found_other_class);
2033 /* If dynamically linked, try the DT_RPATH of the executable
2034 itself. NB: we do this for lookups in any namespace. */
2035 if (fd == -1)
2037 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2038 if (l && l->l_type != lt_loaded && l != loader
2039 && cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2040 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
2041 &realname, &fb, loader ?: l, LA_SER_RUNPATH,
2042 &found_other_class);
2046 /* Try the LD_LIBRARY_PATH environment variable. */
2047 if (fd == -1 && env_path_list.dirs != (void *) -1)
2048 fd = open_path (name, namelen, preloaded, &env_path_list,
2049 &realname, &fb,
2050 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
2051 LA_SER_LIBPATH, &found_other_class);
2053 /* Look at the RUNPATH information for this binary. */
2054 if (fd == -1 && loader != NULL
2055 && cache_rpath (loader, &loader->l_runpath_dirs,
2056 DT_RUNPATH, "RUNPATH"))
2057 fd = open_path (name, namelen, preloaded,
2058 &loader->l_runpath_dirs, &realname, &fb, loader,
2059 LA_SER_RUNPATH, &found_other_class);
2061 if (fd == -1
2062 && (__builtin_expect (! preloaded, 1)
2063 || ! INTUSE(__libc_enable_secure)))
2065 /* Check the list of libraries in the file /etc/ld.so.cache,
2066 for compatibility with Linux's ldconfig program. */
2067 const char *cached = _dl_load_cache_lookup (name);
2069 if (cached != NULL)
2071 #ifdef SHARED
2072 // XXX Correct to unconditionally default to namespace 0?
2073 l = loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2074 #else
2075 l = loader;
2076 #endif
2078 /* If the loader has the DF_1_NODEFLIB flag set we must not
2079 use a cache entry from any of these directories. */
2080 if (
2081 #ifndef SHARED
2082 /* 'l' is always != NULL for dynamically linked objects. */
2083 l != NULL &&
2084 #endif
2085 __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
2087 const char *dirp = system_dirs;
2088 unsigned int cnt = 0;
2092 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2094 /* The prefix matches. Don't use the entry. */
2095 cached = NULL;
2096 break;
2099 dirp += system_dirs_len[cnt] + 1;
2100 ++cnt;
2102 while (cnt < nsystem_dirs_len);
2105 if (cached != NULL)
2107 fd = open_verify (cached,
2108 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
2109 LA_SER_CONFIG, &found_other_class, false);
2110 if (__builtin_expect (fd != -1, 1))
2112 realname = local_strdup (cached);
2113 if (realname == NULL)
2115 __close (fd);
2116 fd = -1;
2123 /* Finally, try the default path. */
2124 if (fd == -1
2125 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
2126 || __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
2127 && rtld_search_dirs.dirs != (void *) -1)
2128 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
2129 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
2131 /* Add another newline when we are tracing the library loading. */
2132 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2133 _dl_debug_printf ("\n");
2135 else
2137 /* The path may contain dynamic string tokens. */
2138 realname = (loader
2139 ? expand_dynamic_string_token (loader, name)
2140 : local_strdup (name));
2141 if (realname == NULL)
2142 fd = -1;
2143 else
2145 fd = open_verify (realname, &fb,
2146 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0,
2147 &found_other_class, true);
2148 if (__builtin_expect (fd, 0) == -1)
2149 free (realname);
2153 #ifdef SHARED
2154 no_file:
2155 #endif
2156 /* In case the LOADER information has only been provided to get to
2157 the appropriate RUNPATH/RPATH information we do not need it
2158 anymore. */
2159 if (mode & __RTLD_CALLMAP)
2160 loader = NULL;
2162 if (__builtin_expect (fd, 0) == -1)
2164 if (trace_mode
2165 && __builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK, 0) == 0)
2167 /* We haven't found an appropriate library. But since we
2168 are only interested in the list of libraries this isn't
2169 so severe. Fake an entry with all the information we
2170 have. */
2171 static const Elf_Symndx dummy_bucket = STN_UNDEF;
2173 /* Enter the new object in the list of loaded objects. */
2174 if ((name_copy = local_strdup (name)) == NULL
2175 || (l = _dl_new_object (name_copy, name, type, loader,
2176 mode, nsid)) == NULL)
2178 free (name_copy);
2179 _dl_signal_error (ENOMEM, name, NULL,
2180 N_("cannot create shared object descriptor"));
2182 /* Signal that this is a faked entry. */
2183 l->l_faked = 1;
2184 /* Since the descriptor is initialized with zero we do not
2185 have do this here.
2186 l->l_reserved = 0; */
2187 l->l_buckets = &dummy_bucket;
2188 l->l_nbuckets = 1;
2189 l->l_relocated = 1;
2191 return l;
2193 else if (found_other_class)
2194 _dl_signal_error (0, name, NULL,
2195 ELFW(CLASS) == ELFCLASS32
2196 ? N_("wrong ELF class: ELFCLASS64")
2197 : N_("wrong ELF class: ELFCLASS32"));
2198 else
2199 _dl_signal_error (errno, name, NULL,
2200 N_("cannot open shared object file"));
2203 void *stack_end = __libc_stack_end;
2204 return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode,
2205 &stack_end, nsid);
2209 void
2210 internal_function
2211 _dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2213 if (counting)
2215 si->dls_cnt = 0;
2216 si->dls_size = 0;
2219 unsigned int idx = 0;
2220 char *allocptr = (char *) &si->dls_serpath[si->dls_cnt];
2221 void add_path (const struct r_search_path_struct *sps, unsigned int flags)
2222 # define add_path(sps, flags) add_path(sps, 0) /* XXX */
2224 if (sps->dirs != (void *) -1)
2226 struct r_search_path_elem **dirs = sps->dirs;
2229 const struct r_search_path_elem *const r = *dirs++;
2230 if (counting)
2232 si->dls_cnt++;
2233 si->dls_size += r->dirnamelen;
2235 else
2237 Dl_serpath *const sp = &si->dls_serpath[idx++];
2238 sp->dls_name = allocptr;
2239 allocptr = __mempcpy (allocptr,
2240 r->dirname, r->dirnamelen - 1);
2241 *allocptr++ = '\0';
2242 sp->dls_flags = flags;
2245 while (*dirs != NULL);
2249 /* When the object has the RUNPATH information we don't use any RPATHs. */
2250 if (loader->l_info[DT_RUNPATH] == NULL)
2252 /* First try the DT_RPATH of the dependent object that caused NAME
2253 to be loaded. Then that object's dependent, and on up. */
2255 struct link_map *l = loader;
2258 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2259 add_path (&l->l_rpath_dirs, XXX_RPATH);
2260 l = l->l_loader;
2262 while (l != NULL);
2264 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2265 if (loader->l_ns == LM_ID_BASE)
2267 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2268 if (l != NULL && l->l_type != lt_loaded && l != loader)
2269 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
2270 add_path (&l->l_rpath_dirs, XXX_RPATH);
2274 /* Try the LD_LIBRARY_PATH environment variable. */
2275 add_path (&env_path_list, XXX_ENV);
2277 /* Look at the RUNPATH information for this binary. */
2278 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
2279 add_path (&loader->l_runpath_dirs, XXX_RUNPATH);
2281 /* XXX
2282 Here is where ld.so.cache gets checked, but we don't have
2283 a way to indicate that in the results for Dl_serinfo. */
2285 /* Finally, try the default path. */
2286 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
2287 add_path (&rtld_search_dirs, XXX_default);
2289 if (counting)
2290 /* Count the struct size before the string area, which we didn't
2291 know before we completed dls_cnt. */
2292 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;