1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
28 #include <bits/wordsize.h>
30 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <gnu/lib-names.h>
35 /* Type for the buffer we put the ELF header and hopefully the program
36 header. This buffer does not really have to be too large. In most
37 cases the program header follows the ELF header directly. If this
38 is not the case all bets are off and we can make the header
39 arbitrarily large and still won't get it read. This means the only
40 question is how large are the ELF and program header combined. The
41 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
42 bytes long. Each program header entry is again 32 and 56 bytes
43 long respectively. I.e., even with a file which has 10 program
44 header entries we only have to read 372B/624B respectively. Add to
45 this a bit of margin for program notes and reading 512B and 832B
46 for 32-bit and 64-bit files respecitvely is enough. If this
47 heuristic should really fail for some file the code in
48 `_dl_map_object_from_fd' knows how to recover. */
53 # define FILEBUF_SIZE 512
55 # define FILEBUF_SIZE 832
57 char buf
[FILEBUF_SIZE
] __attribute__ ((aligned (__alignof (ElfW(Ehdr
)))));
60 #include "dynamic-link.h"
62 #include <stackinfo.h>
64 #include <stap-probe.h>
65 #include <libc-pointer-arith.h>
66 #include <array_length.h>
70 #include <dl-map-segments.h>
71 #include <dl-unmap-segments.h>
72 #include <dl-machine-reject-phdr.h>
73 #include <dl-sysdep-open.h>
75 #include <not-cancel.h>
78 #if BYTE_ORDER == BIG_ENDIAN
79 # define byteorder ELFDATA2MSB
80 #elif BYTE_ORDER == LITTLE_ENDIAN
81 # define byteorder ELFDATA2LSB
83 # error "Unknown BYTE_ORDER " BYTE_ORDER
84 # define byteorder ELFDATANONE
87 #define STRING(x) __STRING (x)
90 int __stack_prot attribute_hidden attribute_relro
91 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
93 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
100 /* This is the decomposed LD_LIBRARY_PATH search path. */
101 struct r_search_path_struct __rtld_env_path_list attribute_relro
;
103 /* List of the hardware capabilities we might end up using. */
105 static const struct r_strlenpair
*capstr attribute_relro
;
106 static size_t ncapstr attribute_relro
;
107 static size_t max_capstrlen attribute_relro
;
109 enum { ncapstr
= 1, max_capstrlen
= 0 };
113 /* Get the generated information about the trusted directories. Use
114 an array of concatenated strings to avoid relocations. See
115 gen-trusted-dirs.awk. */
116 #include "trusted-dirs.h"
118 static const char system_dirs
[] = SYSTEM_DIRS
;
119 static const size_t system_dirs_len
[] =
123 #define nsystem_dirs_len array_length (system_dirs_len)
126 is_trusted_path_normalize (const char *path
, size_t len
)
131 char *npath
= (char *) alloca (len
+ 2);
133 while (*path
!= '\0')
139 if (path
[2] == '.' && (path
[3] == '/' || path
[3] == '\0'))
141 while (wnp
> npath
&& *--wnp
!= '/')
146 else if (path
[2] == '/' || path
[2] == '\0')
153 if (wnp
> npath
&& wnp
[-1] == '/')
163 if (wnp
== npath
|| wnp
[-1] != '/')
166 const char *trun
= system_dirs
;
168 for (size_t idx
= 0; idx
< nsystem_dirs_len
; ++idx
)
170 if (wnp
- npath
>= system_dirs_len
[idx
]
171 && memcmp (trun
, npath
, system_dirs_len
[idx
]) == 0)
175 trun
+= system_dirs_len
[idx
] + 1;
181 /* Given a substring starting at INPUT, just after the DST '$' start
182 token, determine if INPUT contains DST token REF, following the
183 ELF gABI rules for DSTs:
185 * Longest possible sequence using the rules (greedy).
187 * Must start with a $ (enforced by caller).
189 * Must follow $ with one underscore or ASCII [A-Za-z] (caller
190 follows these rules for REF) or '{' (start curly quoted name).
192 * Must follow first two characters with zero or more [A-Za-z0-9_]
193 (enforced by caller) or '}' (end curly quoted name).
195 If the sequence is a DST matching REF then the length of the DST
196 (excluding the $ sign but including curly braces, if any) is
197 returned, otherwise 0. */
199 is_dst (const char *input
, const char *ref
)
201 bool is_curly
= false;
203 /* Is a ${...} input sequence? */
210 /* Check for matching name, following closing curly brace (if
211 required), or trailing characters which are part of an
213 size_t rlen
= strlen (ref
);
214 if (strncmp (input
, ref
, rlen
) != 0
215 || (is_curly
&& input
[rlen
] != '}')
216 || ((input
[rlen
] >= 'A' && input
[rlen
] <= 'Z')
217 || (input
[rlen
] >= 'a' && input
[rlen
] <= 'z')
218 || (input
[rlen
] >= '0' && input
[rlen
] <= '9')
219 || (input
[rlen
] == '_')))
223 /* Count the two curly braces. */
229 /* INPUT should be the start of a path e.g DT_RPATH or name e.g.
230 DT_NEEDED. The return value is the number of known DSTs found. We
231 count all known DSTs regardless of __libc_enable_secure; the caller
232 is responsible for enforcing the security of the substitution rules
233 (usually _dl_dst_substitute). */
235 _dl_dst_count (const char *input
)
239 input
= strchr (input
, '$');
241 /* Most likely there is no DST. */
242 if (__glibc_likely (input
== NULL
))
250 /* All DSTs must follow ELF gABI rules, see is_dst (). */
251 if ((len
= is_dst (input
, "ORIGIN")) != 0
252 || (len
= is_dst (input
, "PLATFORM")) != 0
253 || (len
= is_dst (input
, "LIB")) != 0)
256 /* There may be more than one DST in the input. */
257 input
= strchr (input
+ len
, '$');
259 while (input
!= NULL
);
264 /* Process INPUT for DSTs and store in RESULT using the information
265 from link map L to resolve the DSTs. This function only handles one
266 path at a time and does not handle colon-separated path lists (see
267 fillin_rpath ()). Lastly the size of result in bytes should be at
268 least equal to the value returned by DL_DST_REQUIRED. Note that it
269 is possible for a DT_NEEDED, DT_AUXILIARY, and DT_FILTER entries to
270 have colons, but we treat those as literal colons here, not as path
273 _dl_dst_substitute (struct link_map
*l
, const char *input
, char *result
)
275 /* Copy character-by-character from input into the working pointer
276 looking for any DSTs. We track the start of input and if we are
277 going to check for trusted paths, all of which are part of $ORIGIN
278 handling in SUID/SGID cases (see below). In some cases, like when
279 a DST cannot be replaced, we may set result to an empty string and
282 const char *start
= input
;
283 bool check_for_trusted
= false;
287 if (__glibc_unlikely (*input
== '$'))
289 const char *repl
= NULL
;
293 if ((len
= is_dst (input
, "ORIGIN")) != 0)
295 /* For SUID/GUID programs we normally ignore the path with
296 $ORIGIN in DT_RUNPATH, or DT_RPATH. However, there is
297 one exception to this rule, and it is:
299 * $ORIGIN appears as the first path element, and is
300 the only string in the path or is immediately
301 followed by a path separator and the rest of the
306 * The path is rooted in a trusted directory.
308 This exception allows such programs to reference
309 shared libraries in subdirectories of trusted
310 directories. The use case is one of general
311 organization and deployment flexibility.
312 Trusted directories are usually such paths as "/lib64"
313 or "/usr/lib64", and the usual RPATHs take the form of
314 [$ORIGIN/../$LIB/somedir]. */
315 if (__glibc_unlikely (__libc_enable_secure
)
316 && !(input
== start
+ 1
317 && (input
[len
] == '\0' || input
[len
] == '/')))
318 repl
= (const char *) -1;
322 check_for_trusted
= (__libc_enable_secure
323 && l
->l_type
== lt_executable
);
325 else if ((len
= is_dst (input
, "PLATFORM")) != 0)
326 repl
= GLRO(dl_platform
);
327 else if ((len
= is_dst (input
, "LIB")) != 0)
330 if (repl
!= NULL
&& repl
!= (const char *) -1)
332 wp
= __stpcpy (wp
, repl
);
337 /* We found a valid DST that we know about, but we could
338 not find a replacement value for it, therefore we
339 cannot use this path and discard it. */
344 /* No DST we recognize. */
352 while (*input
!= '\0');
354 /* In SUID/SGID programs, after $ORIGIN expansion the normalized
355 path must be rooted in one of the trusted directories. The $LIB
356 and $PLATFORM DST cannot in any way be manipulated by the caller
357 because they are fixed values that are set by the dynamic loader
358 and therefore any paths using just $LIB or $PLATFORM need not be
359 checked for trust, the authors of the binaries themselves are
360 trusted to have designed this correctly. Only $ORIGIN is tested in
361 this way because it may be manipulated in some ways with hard
363 if (__glibc_unlikely (check_for_trusted
)
364 && !is_trusted_path_normalize (result
, wp
- result
))
376 /* Return a malloc allocated copy of INPUT with all recognized DSTs
377 replaced. On some platforms it might not be possible to determine the
378 path from which the object belonging to the map is loaded. In this
379 case the path containing the DST is left out. On error NULL
382 expand_dynamic_string_token (struct link_map
*l
, const char *input
)
384 /* We make two runs over the string. First we determine how large the
385 resulting string is and then we copy it over. Since this is no
386 frequently executed operation we are looking here not for performance
387 but rather for code size. */
392 /* Determine the number of DSTs. */
393 cnt
= _dl_dst_count (input
);
395 /* If we do not have to replace anything simply copy the string. */
396 if (__glibc_likely (cnt
== 0))
397 return __strdup (input
);
399 /* Determine the length of the substituted string. */
400 total
= DL_DST_REQUIRED (l
, input
, strlen (input
), cnt
);
402 /* Allocate the necessary memory. */
403 result
= (char *) malloc (total
+ 1);
407 return _dl_dst_substitute (l
, input
, result
);
411 /* Add `name' to the list of names for a particular shared object.
412 `name' is expected to have been allocated with malloc and will
413 be freed if the shared object already has this name.
414 Returns false if the object already had this name. */
416 add_name_to_object (struct link_map
*l
, const char *name
)
418 struct libname_list
*lnp
, *lastp
;
419 struct libname_list
*newname
;
423 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
424 if (strcmp (name
, lnp
->name
) == 0)
427 name_len
= strlen (name
) + 1;
428 newname
= (struct libname_list
*) malloc (sizeof *newname
+ name_len
);
431 /* No more memory. */
432 _dl_signal_error (ENOMEM
, name
, NULL
, N_("cannot allocate name record"));
435 /* The object should have a libname set from _dl_new_object. */
436 assert (lastp
!= NULL
);
438 newname
->name
= memcpy (newname
+ 1, name
, name_len
);
439 newname
->next
= NULL
;
440 newname
->dont_free
= 0;
441 /* CONCURRENCY NOTES:
443 Make sure the initialization of newname happens before its address is
444 read from the lastp->next store below.
446 GL(dl_load_lock) is held here (and by other writers, e.g. dlclose), so
447 readers of libname_list->next (e.g. _dl_check_caller or the reads above)
448 can use that for synchronization, however the read in _dl_name_match_p
449 may be executed without holding the lock during _dl_runtime_resolve
450 (i.e. lazy symbol resolution when a function of library l is called).
452 The release MO store below synchronizes with the acquire MO load in
453 _dl_name_match_p. Other writes need to synchronize with that load too,
454 however those happen either early when the process is single threaded
455 (dl_main) or when the library is unloaded (dlclose) and the user has to
456 synchronize library calls with unloading. */
457 atomic_store_release (&lastp
->next
, newname
);
460 /* Standard search directories. */
461 struct r_search_path_struct __rtld_search_dirs attribute_relro
;
463 static size_t max_dirnamelen
;
465 static struct r_search_path_elem
**
466 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
467 const char *what
, const char *where
, struct link_map
*l
)
472 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
474 struct r_search_path_elem
*dirp
;
475 char *to_free
= NULL
;
478 /* `strsep' can pass an empty string. */
481 to_free
= cp
= expand_dynamic_string_token (l
, cp
);
483 /* expand_dynamic_string_token can return NULL in case of empty
484 path or memory allocation failure. */
488 /* Compute the length after dynamic string token expansion and
489 ignore empty paths. */
497 /* Remove trailing slashes (except for "/"). */
498 while (len
> 1 && cp
[len
- 1] == '/')
501 /* Now add one if there is none so far. */
502 if (len
> 0 && cp
[len
- 1] != '/')
506 /* See if this directory is already known. */
507 for (dirp
= GL(dl_all_dirs
); dirp
!= NULL
; dirp
= dirp
->next
)
508 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
513 /* It is available, see whether it's on our own list. */
515 for (cnt
= 0; cnt
< nelems
; ++cnt
)
516 if (result
[cnt
] == dirp
)
520 result
[nelems
++] = dirp
;
525 enum r_dir_status init_val
;
526 size_t where_len
= where
? strlen (where
) + 1 : 0;
528 /* It's a new directory. Create an entry and add it. */
529 dirp
= (struct r_search_path_elem
*)
530 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
)
531 + where_len
+ len
+ 1);
533 _dl_signal_error (ENOMEM
, NULL
, NULL
,
534 N_("cannot create cache for search path"));
536 dirp
->dirname
= ((char *) dirp
+ sizeof (*dirp
)
537 + ncapstr
* sizeof (enum r_dir_status
));
538 *((char *) __mempcpy ((char *) dirp
->dirname
, cp
, len
)) = '\0';
539 dirp
->dirnamelen
= len
;
541 if (len
> max_dirnamelen
)
542 max_dirnamelen
= len
;
544 /* We have to make sure all the relative directories are
545 never ignored. The current directory might change and
546 all our saved information would be void. */
547 init_val
= cp
[0] != '/' ? existing
: unknown
;
548 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
549 dirp
->status
[cnt
] = init_val
;
552 if (__glibc_likely (where
!= NULL
))
553 dirp
->where
= memcpy ((char *) dirp
+ sizeof (*dirp
) + len
+ 1
554 + (ncapstr
* sizeof (enum r_dir_status
)),
559 dirp
->next
= GL(dl_all_dirs
);
560 GL(dl_all_dirs
) = dirp
;
562 /* Put it in the result array. */
563 result
[nelems
++] = dirp
;
568 /* Terminate the array. */
569 result
[nelems
] = NULL
;
576 decompose_rpath (struct r_search_path_struct
*sps
,
577 const char *rpath
, struct link_map
*l
, const char *what
)
579 /* Make a copy we can work with. */
580 const char *where
= l
->l_name
;
582 struct r_search_path_elem
**result
;
584 /* Initialize to please the compiler. */
585 const char *errstring
= NULL
;
587 /* First see whether we must forget the RUNPATH and RPATH from this
589 if (__glibc_unlikely (GLRO(dl_inhibit_rpath
) != NULL
)
590 && !__libc_enable_secure
)
592 const char *inhp
= GLRO(dl_inhibit_rpath
);
596 const char *wp
= where
;
598 while (*inhp
== *wp
&& *wp
!= '\0')
604 if (*wp
== '\0' && (*inhp
== '\0' || *inhp
== ':'))
606 /* This object is on the list of objects for which the
607 RUNPATH and RPATH must not be used. */
608 sps
->dirs
= (void *) -1;
612 while (*inhp
!= '\0')
616 while (*inhp
!= '\0');
619 /* Ignore empty rpaths. */
622 sps
->dirs
= (struct r_search_path_elem
**) -1;
626 /* Make a writable copy. */
627 char *copy
= __strdup (rpath
);
630 errstring
= N_("cannot create RUNPATH/RPATH copy");
634 /* Count the number of necessary elements in the result array. */
636 for (cp
= copy
; *cp
!= '\0'; ++cp
)
640 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
641 number of necessary entries. */
642 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1 + 1)
647 errstring
= N_("cannot create cache for search path");
649 _dl_signal_error (ENOMEM
, NULL
, NULL
, errstring
);
652 fillin_rpath (copy
, result
, ":", what
, where
, l
);
654 /* Free the copied RPATH string. `fillin_rpath' make own copies if
658 /* There is no path after expansion. */
659 if (result
[0] == NULL
)
662 sps
->dirs
= (struct r_search_path_elem
**) -1;
667 /* The caller will change this value if we haven't used a real malloc. */
672 /* Make sure cached path information is stored in *SP
673 and return true if there are any paths to search there. */
675 cache_rpath (struct link_map
*l
,
676 struct r_search_path_struct
*sp
,
680 if (sp
->dirs
== (void *) -1)
683 if (sp
->dirs
!= NULL
)
686 if (l
->l_info
[tag
] == NULL
)
688 /* There is no path. */
689 sp
->dirs
= (void *) -1;
693 /* Make sure the cache information is available. */
694 return decompose_rpath (sp
, (const char *) (D_PTR (l
, l_info
[DT_STRTAB
])
695 + l
->l_info
[tag
]->d_un
.d_val
),
701 _dl_init_paths (const char *llp
, const char *source
,
702 const char *glibc_hwcaps_prepend
,
703 const char *glibc_hwcaps_mask
)
707 struct r_search_path_elem
*pelem
, **aelem
;
709 struct link_map
__attribute__ ((unused
)) *l
= NULL
;
710 /* Initialize to please the compiler. */
711 const char *errstring
= NULL
;
713 /* Fill in the information about the application's RPATH and the
714 directories addressed by the LD_LIBRARY_PATH environment variable. */
717 /* Get the capabilities. */
718 capstr
= _dl_important_hwcaps (glibc_hwcaps_prepend
, glibc_hwcaps_mask
,
719 &ncapstr
, &max_capstrlen
);
722 /* First set up the rest of the default search directory entries. */
723 aelem
= __rtld_search_dirs
.dirs
= (struct r_search_path_elem
**)
724 malloc ((nsystem_dirs_len
+ 1) * sizeof (struct r_search_path_elem
*));
725 if (__rtld_search_dirs
.dirs
== NULL
)
727 errstring
= N_("cannot create search path array");
729 _dl_signal_error (ENOMEM
, NULL
, NULL
, errstring
);
732 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
733 + ncapstr
* sizeof (enum r_dir_status
))
734 / sizeof (struct r_search_path_elem
));
736 __rtld_search_dirs
.dirs
[0]
737 = malloc (nsystem_dirs_len
* round_size
738 * sizeof (*__rtld_search_dirs
.dirs
[0]));
739 if (__rtld_search_dirs
.dirs
[0] == NULL
)
741 errstring
= N_("cannot create cache for search path");
745 __rtld_search_dirs
.malloced
= 0;
746 pelem
= GL(dl_all_dirs
) = __rtld_search_dirs
.dirs
[0];
756 pelem
->what
= "system search path";
759 pelem
->dirname
= strp
;
760 pelem
->dirnamelen
= system_dirs_len
[idx
];
761 strp
+= system_dirs_len
[idx
] + 1;
763 /* System paths must be absolute. */
764 assert (pelem
->dirname
[0] == '/');
765 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
766 pelem
->status
[cnt
] = unknown
;
768 pelem
->next
= (++idx
== nsystem_dirs_len
? NULL
: (pelem
+ round_size
));
772 while (idx
< nsystem_dirs_len
);
774 max_dirnamelen
= SYSTEM_DIRS_MAX_LEN
;
777 /* This points to the map of the main object. If there is no main
778 object (e.g., under --help, use the dynamic loader itself as a
780 l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
783 l
= &GL (dl_rtld_map
);
785 assert (l
->l_type
!= lt_loaded
);
787 if (l
->l_info
[DT_RUNPATH
])
789 /* Allocate room for the search path and fill in information
791 decompose_rpath (&l
->l_runpath_dirs
,
792 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
793 + l
->l_info
[DT_RUNPATH
]->d_un
.d_val
),
795 /* During rtld init the memory is allocated by the stub malloc,
796 prevent any attempt to free it by the normal malloc. */
797 l
->l_runpath_dirs
.malloced
= 0;
799 /* The RPATH is ignored. */
800 l
->l_rpath_dirs
.dirs
= (void *) -1;
804 l
->l_runpath_dirs
.dirs
= (void *) -1;
806 if (l
->l_info
[DT_RPATH
])
808 /* Allocate room for the search path and fill in information
810 decompose_rpath (&l
->l_rpath_dirs
,
811 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
812 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
814 /* During rtld init the memory is allocated by the stub
815 malloc, prevent any attempt to free it by the normal
817 l
->l_rpath_dirs
.malloced
= 0;
820 l
->l_rpath_dirs
.dirs
= (void *) -1;
823 if (llp
!= NULL
&& *llp
!= '\0')
825 char *llp_tmp
= strdupa (llp
);
827 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
830 for (const char *cp
= llp_tmp
; *cp
!= '\0'; ++cp
)
831 if (*cp
== ':' || *cp
== ';')
834 __rtld_env_path_list
.dirs
= (struct r_search_path_elem
**)
835 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
836 if (__rtld_env_path_list
.dirs
== NULL
)
838 errstring
= N_("cannot create cache for search path");
842 (void) fillin_rpath (llp_tmp
, __rtld_env_path_list
.dirs
, ":;",
845 if (__rtld_env_path_list
.dirs
[0] == NULL
)
847 free (__rtld_env_path_list
.dirs
);
848 __rtld_env_path_list
.dirs
= (void *) -1;
851 __rtld_env_path_list
.malloced
= 0;
854 __rtld_env_path_list
.dirs
= (void *) -1;
858 /* Process PT_GNU_PROPERTY program header PH in module L after
859 PT_LOAD segments are mapped. Only one NT_GNU_PROPERTY_TYPE_0
860 note is handled which contains processor specific properties.
861 FD is -1 for the kernel mapped main executable otherwise it is
862 the fd used for loading module L. */
865 _dl_process_pt_gnu_property (struct link_map
*l
, int fd
, const ElfW(Phdr
) *ph
)
867 const ElfW(Nhdr
) *note
= (const void *) (ph
->p_vaddr
+ l
->l_addr
);
868 const ElfW(Addr
) size
= ph
->p_memsz
;
869 const ElfW(Addr
) align
= ph
->p_align
;
871 /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes in
872 32-bit objects and to 8 bytes in 64-bit objects. Skip notes
873 with incorrect alignment. */
874 if (align
!= (__ELF_NATIVE_CLASS
/ 8))
877 const ElfW(Addr
) start
= (ElfW(Addr
)) note
;
878 unsigned int last_type
= 0;
880 while ((ElfW(Addr
)) (note
+ 1) - start
< size
)
882 /* Find the NT_GNU_PROPERTY_TYPE_0 note. */
883 if (note
->n_namesz
== 4
884 && note
->n_type
== NT_GNU_PROPERTY_TYPE_0
885 && memcmp (note
+ 1, "GNU", 4) == 0)
887 /* Check for invalid property. */
888 if (note
->n_descsz
< 8
889 || (note
->n_descsz
% sizeof (ElfW(Addr
))) != 0)
892 /* Start and end of property array. */
893 unsigned char *ptr
= (unsigned char *) (note
+ 1) + 4;
894 unsigned char *ptr_end
= ptr
+ note
->n_descsz
;
898 unsigned int type
= *(unsigned int *) ptr
;
899 unsigned int datasz
= *(unsigned int *) (ptr
+ 4);
901 /* Property type must be in ascending order. */
902 if (type
< last_type
)
906 if ((ptr
+ datasz
) > ptr_end
)
911 /* Target specific property processing. */
912 if (_dl_process_gnu_property (l
, fd
, type
, datasz
, ptr
) == 0)
915 /* Check the next property item. */
916 ptr
+= ALIGN_UP (datasz
, sizeof (ElfW(Addr
)));
918 while ((ptr_end
- ptr
) >= 8);
920 /* Only handle one NT_GNU_PROPERTY_TYPE_0. */
924 note
= ((const void *) note
925 + ELF_NOTE_NEXT_OFFSET (note
->n_namesz
, note
->n_descsz
,
931 /* Map in the shared object NAME, actually located in REALNAME, and already
934 #ifndef EXTERNAL_MAP_FROM_FD
938 _dl_map_object_from_fd (const char *name
, const char *origname
, int fd
,
939 struct filebuf
*fbp
, char *realname
,
940 struct link_map
*loader
, int l_type
, int mode
,
941 void **stack_endp
, Lmid_t nsid
)
943 struct link_map
*l
= NULL
;
944 const ElfW(Ehdr
) *header
;
945 const ElfW(Phdr
) *phdr
;
946 const ElfW(Phdr
) *ph
;
949 /* Initialize to keep the compiler happy. */
950 const char *errstring
= NULL
;
952 struct r_debug
*r
= _dl_debug_initialize (0, nsid
);
953 bool make_consistent
= false;
955 /* Get file information. To match the kernel behavior, do not fill
956 in this information for the executable in case of an explicit
957 loader invocation. */
959 if (mode
& __RTLD_OPENEXEC
)
961 assert (nsid
== LM_ID_BASE
);
962 memset (&id
, 0, sizeof (id
));
966 if (__glibc_unlikely (!_dl_get_file_id (fd
, &id
)))
968 errstring
= N_("cannot stat shared object");
972 /* The file might already be closed. */
974 __close_nocancel (fd
);
975 if (l
!= NULL
&& l
->l_map_start
!= 0)
976 _dl_unmap_segments (l
);
977 if (l
!= NULL
&& l
->l_origin
!= (char *) -1l)
978 free ((char *) l
->l_origin
);
979 if (l
!= NULL
&& !l
->l_libname
->dont_free
)
981 if (l
!= NULL
&& l
->l_phdr_allocated
)
982 free ((void *) l
->l_phdr
);
986 if (make_consistent
&& r
!= NULL
)
988 r
->r_state
= RT_CONSISTENT
;
990 LIBC_PROBE (map_failed
, 2, nsid
, r
);
993 _dl_signal_error (errval
, name
, NULL
, errstring
);
996 /* Look again to see if the real name matched another already loaded. */
997 for (l
= GL(dl_ns
)[nsid
]._ns_loaded
; l
!= NULL
; l
= l
->l_next
)
998 if (!l
->l_removed
&& _dl_file_id_match_p (&l
->l_file_id
, &id
))
1000 /* The object is already loaded.
1001 Just bump its reference count and return it. */
1002 __close_nocancel (fd
);
1004 /* If the name is not in the list of names for this object add
1007 add_name_to_object (l
, name
);
1014 /* When loading into a namespace other than the base one we must
1015 avoid loading ld.so since there can only be one copy. Ever. */
1016 if (__glibc_unlikely (nsid
!= LM_ID_BASE
)
1017 && (_dl_file_id_match_p (&id
, &GL(dl_rtld_map
).l_file_id
)
1018 || _dl_name_match_p (name
, &GL(dl_rtld_map
))))
1020 /* This is indeed ld.so. Create a new link_map which refers to
1021 the real one for almost everything. */
1022 l
= _dl_new_object (realname
, name
, l_type
, loader
, mode
, nsid
);
1026 /* Refer to the real descriptor. */
1027 l
->l_real
= &GL(dl_rtld_map
);
1029 /* No need to bump the refcount of the real object, ld.so will
1030 never be unloaded. */
1031 __close_nocancel (fd
);
1033 /* Add the map for the mirrored object to the object list. */
1034 _dl_add_to_namespace_list (l
, nsid
);
1040 if (mode
& RTLD_NOLOAD
)
1042 /* We are not supposed to load the object unless it is already
1043 loaded. So return now. */
1045 __close_nocancel (fd
);
1049 /* Print debugging message. */
1050 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
))
1051 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name
, nsid
);
1053 /* This is the ELF header. We read it in `open_verify'. */
1054 header
= (void *) fbp
->buf
;
1056 /* Signal that we are going to add new objects. */
1057 if (r
->r_state
== RT_CONSISTENT
)
1060 /* Auditing checkpoint: we are going to add new objects. */
1061 if ((mode
& __RTLD_AUDIT
) == 0
1062 && __glibc_unlikely (GLRO(dl_naudit
) > 0))
1064 struct link_map
*head
= GL(dl_ns
)[nsid
]._ns_loaded
;
1065 /* Do not call the functions for any auditing object. */
1066 if (head
->l_auditing
== 0)
1068 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1069 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1071 if (afct
->activity
!= NULL
)
1072 afct
->activity (&link_map_audit_state (head
, cnt
)->cookie
,
1081 /* Notify the debugger we have added some objects. We need to
1082 call _dl_debug_initialize in a static program in case dynamic
1083 linking has not been used before. */
1084 r
->r_state
= RT_ADD
;
1086 LIBC_PROBE (map_start
, 2, nsid
, r
);
1087 make_consistent
= true;
1090 assert (r
->r_state
== RT_ADD
);
1092 /* Enter the new object in the list of loaded objects. */
1093 l
= _dl_new_object (realname
, name
, l_type
, loader
, mode
, nsid
);
1094 if (__glibc_unlikely (l
== NULL
))
1099 errstring
= N_("cannot create shared object descriptor");
1103 /* Extract the remaining details we need from the ELF header
1104 and then read in the program header table. */
1105 l
->l_entry
= header
->e_entry
;
1106 type
= header
->e_type
;
1107 l
->l_phnum
= header
->e_phnum
;
1109 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
1110 if (header
->e_phoff
+ maplength
<= (size_t) fbp
->len
)
1111 phdr
= (void *) (fbp
->buf
+ header
->e_phoff
);
1114 phdr
= alloca (maplength
);
1115 if ((size_t) __pread64_nocancel (fd
, (void *) phdr
, maplength
,
1116 header
->e_phoff
) != maplength
)
1118 errstring
= N_("cannot read file data");
1123 /* On most platforms presume that PT_GNU_STACK is absent and the stack is
1124 * executable. Other platforms default to a nonexecutable stack and don't
1125 * need PT_GNU_STACK to do so. */
1126 uint_fast16_t stack_flags
= DEFAULT_STACK_PERMS
;
1129 /* Scan the program header table, collecting its load commands. */
1130 struct loadcmd loadcmds
[l
->l_phnum
];
1131 size_t nloadcmds
= 0;
1132 bool has_holes
= false;
1134 /* The struct is initialized to zero so this is not necessary:
1138 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
1141 /* These entries tell us where to find things once the file's
1142 segments are mapped in. We record the addresses it says
1143 verbatim, and later correct for the run-time load address. */
1147 /* Debuginfo only files from "objcopy --only-keep-debug"
1148 contain a PT_DYNAMIC segment with p_filesz == 0. Skip
1149 such a segment to avoid a crash later. */
1150 l
->l_ld
= (void *) ph
->p_vaddr
;
1151 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
1156 l
->l_phdr
= (void *) ph
->p_vaddr
;
1160 /* A load command tells us to map in part of the file.
1161 We record the load commands and process them all later. */
1162 if (__glibc_unlikely ((ph
->p_align
& (GLRO(dl_pagesize
) - 1)) != 0))
1164 errstring
= N_("ELF load command alignment not page-aligned");
1167 if (__glibc_unlikely (((ph
->p_vaddr
- ph
->p_offset
)
1168 & (ph
->p_align
- 1)) != 0))
1171 = N_("ELF load command address/offset not properly aligned");
1175 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
1176 c
->mapstart
= ALIGN_DOWN (ph
->p_vaddr
, GLRO(dl_pagesize
));
1177 c
->mapend
= ALIGN_UP (ph
->p_vaddr
+ ph
->p_filesz
, GLRO(dl_pagesize
));
1178 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
1179 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
1180 c
->mapoff
= ALIGN_DOWN (ph
->p_offset
, GLRO(dl_pagesize
));
1182 /* Determine whether there is a gap between the last segment
1184 if (nloadcmds
> 1 && c
[-1].mapend
!= c
->mapstart
)
1187 /* Optimize a common case. */
1188 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1189 c
->prot
= (PF_TO_PROT
1190 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
1193 if (ph
->p_flags
& PF_R
)
1194 c
->prot
|= PROT_READ
;
1195 if (ph
->p_flags
& PF_W
)
1196 c
->prot
|= PROT_WRITE
;
1197 if (ph
->p_flags
& PF_X
)
1198 c
->prot
|= PROT_EXEC
;
1203 if (ph
->p_memsz
== 0)
1204 /* Nothing to do for an empty segment. */
1207 l
->l_tls_blocksize
= ph
->p_memsz
;
1208 l
->l_tls_align
= ph
->p_align
;
1209 if (ph
->p_align
== 0)
1210 l
->l_tls_firstbyte_offset
= 0;
1212 l
->l_tls_firstbyte_offset
= ph
->p_vaddr
& (ph
->p_align
- 1);
1213 l
->l_tls_initimage_size
= ph
->p_filesz
;
1214 /* Since we don't know the load address yet only store the
1215 offset. We will adjust it later. */
1216 l
->l_tls_initimage
= (void *) ph
->p_vaddr
;
1218 /* l->l_tls_modid is assigned below, once there is no
1219 possibility for failure. */
1221 if (l
->l_type
!= lt_library
1222 && GL(dl_tls_dtv_slotinfo_list
) == NULL
)
1225 /* We are loading the executable itself when the dynamic
1226 linker was executed directly. The setup will happen
1228 assert (l
->l_prev
== NULL
|| (mode
& __RTLD_AUDIT
) != 0);
1230 assert (false && "TLS not initialized in static application");
1236 stack_flags
= ph
->p_flags
;
1240 l
->l_relro_addr
= ph
->p_vaddr
;
1241 l
->l_relro_size
= ph
->p_memsz
;
1245 if (__glibc_unlikely (nloadcmds
== 0))
1247 /* This only happens for a bogus object that will be caught with
1248 another error below. But we don't want to go through the
1249 calculations below using NLOADCMDS - 1. */
1250 errstring
= N_("object file has no loadable segments");
1254 /* dlopen of an executable is not valid because it is not possible
1255 to perform proper relocations, handle static TLS, or run the
1256 ELF constructors. For PIE, the check needs the dynamic
1257 section, so there is another check below. */
1258 if (__glibc_unlikely (type
!= ET_DYN
)
1259 && __glibc_unlikely ((mode
& __RTLD_OPENEXEC
) == 0))
1261 /* This object is loaded at a fixed address. This must never
1262 happen for objects loaded with dlopen. */
1263 errstring
= N_("cannot dynamically load executable");
1267 /* Length of the sections to be loaded. */
1268 maplength
= loadcmds
[nloadcmds
- 1].allocend
- loadcmds
[0].mapstart
;
1270 /* Now process the load commands and map segments into memory.
1271 This is responsible for filling in:
1272 l_map_start, l_map_end, l_addr, l_contiguous, l_text_end, l_phdr
1274 errstring
= _dl_map_segments (l
, fd
, header
, type
, loadcmds
, nloadcmds
,
1275 maplength
, has_holes
, loader
);
1276 if (__glibc_unlikely (errstring
!= NULL
))
1278 /* Mappings can be in an inconsistent state: avoid unmap. */
1279 l
->l_map_start
= l
->l_map_end
= 0;
1286 if (__glibc_unlikely (type
== ET_DYN
))
1288 errstring
= N_("object file has no dynamic section");
1293 l
->l_ld
= (ElfW(Dyn
) *) ((ElfW(Addr
)) l
->l_ld
+ l
->l_addr
);
1295 elf_get_dynamic_info (l
, NULL
);
1297 /* Make sure we are not dlopen'ing an object that has the
1298 DF_1_NOOPEN flag set, or a PIE object. */
1299 if ((__glibc_unlikely (l
->l_flags_1
& DF_1_NOOPEN
)
1300 && (mode
& __RTLD_DLOPEN
))
1301 || (__glibc_unlikely (l
->l_flags_1
& DF_1_PIE
)
1302 && __glibc_unlikely ((mode
& __RTLD_OPENEXEC
) == 0)))
1304 if (l
->l_flags_1
& DF_1_PIE
)
1306 = N_("cannot dynamically load position-independent executable");
1308 errstring
= N_("shared object cannot be dlopen()ed");
1312 if (l
->l_phdr
== NULL
)
1314 /* The program header is not contained in any of the segments.
1315 We have to allocate memory ourself and copy it over from out
1317 ElfW(Phdr
) *newp
= (ElfW(Phdr
) *) malloc (header
->e_phnum
1318 * sizeof (ElfW(Phdr
)));
1321 errstring
= N_("cannot allocate memory for program header");
1325 l
->l_phdr
= memcpy (newp
, phdr
,
1326 (header
->e_phnum
* sizeof (ElfW(Phdr
))));
1327 l
->l_phdr_allocated
= 1;
1330 /* Adjust the PT_PHDR value by the runtime load address. */
1331 l
->l_phdr
= (ElfW(Phdr
) *) ((ElfW(Addr
)) l
->l_phdr
+ l
->l_addr
);
1333 if (__glibc_unlikely ((stack_flags
&~ GL(dl_stack_flags
)) & PF_X
))
1335 /* The stack is presently not executable, but this module
1336 requires that it be executable. We must change the
1337 protection of the variable which contains the flags used in
1338 the mprotect calls. */
1340 if ((mode
& (__RTLD_DLOPEN
| __RTLD_AUDIT
)) == __RTLD_DLOPEN
)
1342 const uintptr_t p
= (uintptr_t) &__stack_prot
& -GLRO(dl_pagesize
);
1343 const size_t s
= (uintptr_t) (&__stack_prot
+ 1) - p
;
1345 struct link_map
*const m
= &GL(dl_rtld_map
);
1346 const uintptr_t relro_end
= ((m
->l_addr
+ m
->l_relro_addr
1348 & -GLRO(dl_pagesize
));
1349 if (__glibc_likely (p
+ s
<= relro_end
))
1351 /* The variable lies in the region protected by RELRO. */
1352 if (__mprotect ((void *) p
, s
, PROT_READ
|PROT_WRITE
) < 0)
1354 errstring
= N_("cannot change memory protections");
1357 __stack_prot
|= PROT_READ
|PROT_WRITE
|PROT_EXEC
;
1358 __mprotect ((void *) p
, s
, PROT_READ
);
1361 __stack_prot
|= PROT_READ
|PROT_WRITE
|PROT_EXEC
;
1365 __stack_prot
|= PROT_READ
|PROT_WRITE
|PROT_EXEC
;
1367 #ifdef check_consistency
1368 check_consistency ();
1371 errval
= (*GL(dl_make_stack_executable_hook
)) (stack_endp
);
1375 cannot enable executable stack as shared object requires");
1380 /* Adjust the address of the TLS initialization image. */
1381 if (l
->l_tls_initimage
!= NULL
)
1382 l
->l_tls_initimage
= (char *) l
->l_tls_initimage
+ l
->l_addr
;
1384 /* Process program headers again after load segments are mapped in
1385 case processing requires accessing those segments. Scan program
1386 headers backward so that PT_NOTE can be skipped if PT_GNU_PROPERTY
1388 for (ph
= &l
->l_phdr
[l
->l_phnum
]; ph
!= l
->l_phdr
; --ph
)
1389 switch (ph
[-1].p_type
)
1392 _dl_process_pt_note (l
, fd
, &ph
[-1]);
1394 case PT_GNU_PROPERTY
:
1395 _dl_process_pt_gnu_property (l
, fd
, &ph
[-1]);
1399 /* We are done mapping in the file. We no longer need the descriptor. */
1400 if (__glibc_unlikely (__close_nocancel (fd
) != 0))
1402 errstring
= N_("cannot close file descriptor");
1405 /* Signal that we closed the file. */
1408 /* Failures before this point are handled locally via lose.
1409 There are no more failures in this function until return,
1410 to change that the cleanup handling needs to be updated. */
1412 /* If this is ET_EXEC, we should have loaded it as lt_executable. */
1413 assert (type
!= ET_EXEC
|| l
->l_type
== lt_executable
);
1415 l
->l_entry
+= l
->l_addr
;
1417 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
))
1418 _dl_debug_printf ("\
1419 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1420 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1421 (int) sizeof (void *) * 2,
1422 (unsigned long int) l
->l_ld
,
1423 (int) sizeof (void *) * 2,
1424 (unsigned long int) l
->l_addr
,
1425 (int) sizeof (void *) * 2, maplength
,
1426 (int) sizeof (void *) * 2,
1427 (unsigned long int) l
->l_entry
,
1428 (int) sizeof (void *) * 2,
1429 (unsigned long int) l
->l_phdr
,
1430 (int) sizeof (void *) * 2, l
->l_phnum
);
1432 /* Set up the symbol hash table. */
1435 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1436 have to do this for the main map. */
1437 if ((mode
& RTLD_DEEPBIND
) == 0
1438 && __glibc_unlikely (l
->l_info
[DT_SYMBOLIC
] != NULL
)
1439 && &l
->l_searchlist
!= l
->l_scope
[0])
1441 /* Create an appropriate searchlist. It contains only this map.
1442 This is the definition of DT_SYMBOLIC in SysVr4. */
1443 l
->l_symbolic_searchlist
.r_list
[0] = l
;
1444 l
->l_symbolic_searchlist
.r_nlist
= 1;
1446 /* Now move the existing entries one back. */
1447 memmove (&l
->l_scope
[1], &l
->l_scope
[0],
1448 (l
->l_scope_max
- 1) * sizeof (l
->l_scope
[0]));
1450 /* Now add the new entry. */
1451 l
->l_scope
[0] = &l
->l_symbolic_searchlist
;
1454 /* Remember whether this object must be initialized first. */
1455 if (l
->l_flags_1
& DF_1_INITFIRST
)
1456 GL(dl_initfirst
) = l
;
1458 /* Finally the file information. */
1462 /* When auditing is used the recorded names might not include the
1463 name by which the DSO is actually known. Add that as well. */
1464 if (__glibc_unlikely (origname
!= NULL
))
1465 add_name_to_object (l
, origname
);
1467 /* Audit modules only exist when linking is dynamic so ORIGNAME
1468 cannot be non-NULL. */
1469 assert (origname
== NULL
);
1472 /* When we profile the SONAME might be needed for something else but
1473 loading. Add it right away. */
1474 if (__glibc_unlikely (GLRO(dl_profile
) != NULL
)
1475 && l
->l_info
[DT_SONAME
] != NULL
)
1476 add_name_to_object (l
, ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1477 + l
->l_info
[DT_SONAME
]->d_un
.d_val
));
1479 /* If we have newly loaded libc.so, update the namespace
1481 if (GL(dl_ns
)[nsid
].libc_map
== NULL
1482 && l
->l_info
[DT_SONAME
] != NULL
1483 && strcmp (((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1484 + l
->l_info
[DT_SONAME
]->d_un
.d_val
), LIBC_SO
) == 0)
1485 GL(dl_ns
)[nsid
].libc_map
= l
;
1487 /* _dl_close can only eventually undo the module ID assignment (via
1488 remove_slotinfo) if this function returns a pointer to a link
1489 map. Therefore, delay this step until all possibilities for
1490 failure have been excluded. */
1491 if (l
->l_tls_blocksize
> 0
1492 && (__glibc_likely (l
->l_type
== lt_library
)
1493 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1494 not set up TLS data structures, so don't use them now. */
1495 || __glibc_likely (GL(dl_tls_dtv_slotinfo_list
) != NULL
)))
1496 /* Assign the next available module ID. */
1497 l
->l_tls_modid
= _dl_next_tls_modid ();
1499 #ifdef DL_AFTER_LOAD
1503 /* Now that the object is fully initialized add it to the object list. */
1504 _dl_add_to_namespace_list (l
, nsid
);
1507 /* Auditing checkpoint: we have a new object. */
1508 if (__glibc_unlikely (GLRO(dl_naudit
) > 0)
1509 && !GL(dl_ns
)[l
->l_ns
]._ns_loaded
->l_auditing
)
1511 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1512 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1514 if (afct
->objopen
!= NULL
)
1516 struct auditstate
*state
= link_map_audit_state (l
, cnt
);
1517 state
->bindflags
= afct
->objopen (l
, nsid
, &state
->cookie
);
1518 l
->l_audit_any_plt
|= state
->bindflags
!= 0;
1529 /* Print search path. */
1531 print_search_path (struct r_search_path_elem
**list
,
1532 const char *what
, const char *name
)
1534 char buf
[max_dirnamelen
+ max_capstrlen
];
1537 _dl_debug_printf (" search path=");
1539 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
1541 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
1544 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
1545 if ((*list
)->status
[cnt
] != nonexisting
)
1548 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
1549 if (cp
== buf
|| (cp
== buf
+ 1 && buf
[0] == '/'))
1557 _dl_debug_printf_c (first
? "%s" : ":%s", buf
);
1565 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what
,
1566 DSO_FILENAME (name
));
1568 _dl_debug_printf_c ("\t\t(%s)\n", what
);
1571 /* Open a file and verify it is an ELF file for this architecture. We
1572 ignore only ELF files for other architectures. Non-ELF files and
1573 ELF files with different header information cause fatal errors since
1574 this could mean there is something wrong in the installation and the
1575 user might want to know about this.
1577 If FD is not -1, then the file is already open and FD refers to it.
1578 In that case, FD is consumed for both successful and error returns. */
1580 open_verify (const char *name
, int fd
,
1581 struct filebuf
*fbp
, struct link_map
*loader
,
1582 int whatcode
, int mode
, bool *found_other_class
, bool free_name
)
1584 /* This is the expected ELF header. */
1585 #define ELF32_CLASS ELFCLASS32
1586 #define ELF64_CLASS ELFCLASS64
1587 #ifndef VALID_ELF_HEADER
1588 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1589 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1590 # define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0)
1591 #elif defined MORE_ELF_HEADER_DATA
1592 MORE_ELF_HEADER_DATA
;
1594 static const unsigned char expected
[EI_NIDENT
] =
1596 [EI_MAG0
] = ELFMAG0
,
1597 [EI_MAG1
] = ELFMAG1
,
1598 [EI_MAG2
] = ELFMAG2
,
1599 [EI_MAG3
] = ELFMAG3
,
1600 [EI_CLASS
] = ELFW(CLASS
),
1601 [EI_DATA
] = byteorder
,
1602 [EI_VERSION
] = EV_CURRENT
,
1603 [EI_OSABI
] = ELFOSABI_SYSV
,
1608 ElfW(Word
) vendorlen
;
1612 } expected_note
= { 4, 16, 1, "GNU" };
1613 /* Initialize it to make the compiler happy. */
1614 const char *errstring
= NULL
;
1618 /* Give the auditing libraries a chance. */
1619 if (__glibc_unlikely (GLRO(dl_naudit
) > 0) && whatcode
!= 0
1620 && loader
->l_auditing
== 0)
1622 const char *original_name
= name
;
1623 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1624 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1626 if (afct
->objsearch
!= NULL
)
1628 struct auditstate
*state
= link_map_audit_state (loader
, cnt
);
1629 name
= afct
->objsearch (name
, &state
->cookie
, whatcode
);
1631 /* Ignore the path. */
1638 if (fd
!= -1 && name
!= original_name
&& strcmp (name
, original_name
))
1640 /* An audit library changed what we're supposed to open,
1641 so FD no longer matches it. */
1642 __close_nocancel (fd
);
1649 /* Open the file. We always open files read-only. */
1650 fd
= __open64_nocancel (name
, O_RDONLY
| O_CLOEXEC
);
1655 ElfW(Phdr
) *phdr
, *ph
;
1656 ElfW(Word
) *abi_note
;
1657 ElfW(Word
) *abi_note_malloced
= NULL
;
1658 unsigned int osversion
;
1661 /* We successfully opened the file. Now verify it is a file
1665 assert (sizeof (fbp
->buf
) > sizeof (ElfW(Ehdr
)));
1666 /* Read in the header. */
1669 ssize_t retlen
= __read_nocancel (fd
, fbp
->buf
+ fbp
->len
,
1670 sizeof (fbp
->buf
) - fbp
->len
);
1675 while (__glibc_unlikely (fbp
->len
< sizeof (ElfW(Ehdr
))));
1677 /* This is where the ELF header is loaded. */
1678 ehdr
= (ElfW(Ehdr
) *) fbp
->buf
;
1680 /* Now run the tests. */
1681 if (__glibc_unlikely (fbp
->len
< (ssize_t
) sizeof (ElfW(Ehdr
))))
1684 errstring
= (errval
== 0
1685 ? N_("file too short") : N_("cannot read file data"));
1689 char *realname
= (char *) name
;
1690 name
= strdupa (realname
);
1693 __close_nocancel (fd
);
1694 _dl_signal_error (errval
, name
, NULL
, errstring
);
1697 /* See whether the ELF header is what we expect. */
1698 if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr
->e_ident
, expected
,
1700 || !VALID_ELF_ABIVERSION (ehdr
->e_ident
[EI_OSABI
],
1701 ehdr
->e_ident
[EI_ABIVERSION
])
1702 || memcmp (&ehdr
->e_ident
[EI_PAD
],
1704 EI_NIDENT
- EI_PAD
) != 0))
1706 /* Something is wrong. */
1707 const Elf32_Word
*magp
= (const void *) ehdr
->e_ident
;
1709 #if BYTE_ORDER == LITTLE_ENDIAN
1710 ((ELFMAG0
<< (EI_MAG0
* 8))
1711 | (ELFMAG1
<< (EI_MAG1
* 8))
1712 | (ELFMAG2
<< (EI_MAG2
* 8))
1713 | (ELFMAG3
<< (EI_MAG3
* 8)))
1715 ((ELFMAG0
<< (EI_MAG3
* 8))
1716 | (ELFMAG1
<< (EI_MAG2
* 8))
1717 | (ELFMAG2
<< (EI_MAG1
* 8))
1718 | (ELFMAG3
<< (EI_MAG0
* 8)))
1721 errstring
= N_("invalid ELF header");
1722 else if (ehdr
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
1724 /* This is not a fatal error. On architectures where
1725 32-bit and 64-bit binaries can be run this might
1727 *found_other_class
= true;
1730 else if (ehdr
->e_ident
[EI_DATA
] != byteorder
)
1732 if (BYTE_ORDER
== BIG_ENDIAN
)
1733 errstring
= N_("ELF file data encoding not big-endian");
1735 errstring
= N_("ELF file data encoding not little-endian");
1737 else if (ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
)
1739 = N_("ELF file version ident does not match current one");
1740 /* XXX We should be able so set system specific versions which are
1742 else if (!VALID_ELF_OSABI (ehdr
->e_ident
[EI_OSABI
]))
1743 errstring
= N_("ELF file OS ABI invalid");
1744 else if (!VALID_ELF_ABIVERSION (ehdr
->e_ident
[EI_OSABI
],
1745 ehdr
->e_ident
[EI_ABIVERSION
]))
1746 errstring
= N_("ELF file ABI version invalid");
1747 else if (memcmp (&ehdr
->e_ident
[EI_PAD
], &expected
[EI_PAD
],
1748 EI_NIDENT
- EI_PAD
) != 0)
1749 errstring
= N_("nonzero padding in e_ident");
1751 /* Otherwise we don't know what went wrong. */
1752 errstring
= N_("internal error");
1757 if (__glibc_unlikely (ehdr
->e_version
!= EV_CURRENT
))
1759 errstring
= N_("ELF file version does not match current one");
1762 if (! __glibc_likely (elf_machine_matches_host (ehdr
)))
1764 else if (__glibc_unlikely (ehdr
->e_type
!= ET_DYN
1765 && ehdr
->e_type
!= ET_EXEC
))
1767 errstring
= N_("only ET_DYN and ET_EXEC can be loaded");
1770 else if (__glibc_unlikely (ehdr
->e_phentsize
!= sizeof (ElfW(Phdr
))))
1772 errstring
= N_("ELF file's phentsize not the expected size");
1776 maplength
= ehdr
->e_phnum
* sizeof (ElfW(Phdr
));
1777 if (ehdr
->e_phoff
+ maplength
<= (size_t) fbp
->len
)
1778 phdr
= (void *) (fbp
->buf
+ ehdr
->e_phoff
);
1781 phdr
= alloca (maplength
);
1782 if ((size_t) __pread64_nocancel (fd
, (void *) phdr
, maplength
,
1783 ehdr
->e_phoff
) != maplength
)
1787 errstring
= N_("cannot read file data");
1792 if (__glibc_unlikely (elf_machine_reject_phdr_p
1793 (phdr
, ehdr
->e_phnum
, fbp
->buf
, fbp
->len
,
1797 /* Check .note.ABI-tag if present. */
1798 for (ph
= phdr
; ph
< &phdr
[ehdr
->e_phnum
]; ++ph
)
1799 if (ph
->p_type
== PT_NOTE
&& ph
->p_filesz
>= 32
1800 && (ph
->p_align
== 4 || ph
->p_align
== 8))
1802 ElfW(Addr
) size
= ph
->p_filesz
;
1804 if (ph
->p_offset
+ size
<= (size_t) fbp
->len
)
1805 abi_note
= (void *) (fbp
->buf
+ ph
->p_offset
);
1808 /* Note: __libc_use_alloca is not usable here, because
1809 thread info may not have been set up yet. */
1810 if (size
< __MAX_ALLOCA_CUTOFF
)
1811 abi_note
= alloca (size
);
1814 /* There could be multiple PT_NOTEs. */
1815 abi_note_malloced
= realloc (abi_note_malloced
, size
);
1816 if (abi_note_malloced
== NULL
)
1819 abi_note
= abi_note_malloced
;
1821 if (__pread64_nocancel (fd
, (void *) abi_note
, size
,
1822 ph
->p_offset
) != size
)
1824 free (abi_note_malloced
);
1829 while (memcmp (abi_note
, &expected_note
, sizeof (expected_note
)))
1831 ElfW(Addr
) note_size
1832 = ELF_NOTE_NEXT_OFFSET (abi_note
[0], abi_note
[1],
1835 if (size
- 32 < note_size
)
1841 abi_note
= (void *) abi_note
+ note_size
;
1847 osversion
= (abi_note
[5] & 0xff) * 65536
1848 + (abi_note
[6] & 0xff) * 256
1849 + (abi_note
[7] & 0xff);
1850 if (abi_note
[4] != __ABI_TAG_OS
1851 || (GLRO(dl_osversion
) && GLRO(dl_osversion
) < osversion
))
1854 __close_nocancel (fd
);
1855 __set_errno (ENOENT
);
1861 free (abi_note_malloced
);
1867 /* Try to open NAME in one of the directories in *DIRSP.
1868 Return the fd, or -1. If successful, fill in *REALNAME
1869 with the malloc'd full directory name. If it turns out
1870 that none of the directories in *DIRSP exists, *DIRSP is
1871 replaced with (void *) -1, and the old value is free()d
1872 if MAY_FREE_DIRS is true. */
1875 open_path (const char *name
, size_t namelen
, int mode
,
1876 struct r_search_path_struct
*sps
, char **realname
,
1877 struct filebuf
*fbp
, struct link_map
*loader
, int whatcode
,
1878 bool *found_other_class
)
1880 struct r_search_path_elem
**dirs
= sps
->dirs
;
1883 const char *current_what
= NULL
;
1886 if (__glibc_unlikely (dirs
== NULL
))
1887 /* We're called before _dl_init_paths when loading the main executable
1888 given on the command line when rtld is run directly. */
1891 buf
= alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
1894 struct r_search_path_elem
*this_dir
= *dirs
;
1901 /* If we are debugging the search for libraries print the path
1902 now if it hasn't happened now. */
1903 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
)
1904 && current_what
!= this_dir
->what
)
1906 current_what
= this_dir
->what
;
1907 print_search_path (dirs
, current_what
, this_dir
->where
);
1910 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
1911 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
1913 /* Skip this directory if we know it does not exist. */
1914 if (this_dir
->status
[cnt
] == nonexisting
)
1919 ((char *) __mempcpy (__mempcpy (edp
, capstr
[cnt
].str
,
1924 buflen
= (char *) __mempcpy (edp
, name
, namelen
) - buf
;
1927 /* Print name we try if this is wanted. */
1928 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
1929 _dl_debug_printf (" trying file=%s\n", buf
);
1931 fd
= open_verify (buf
, -1, fbp
, loader
, whatcode
, mode
,
1932 found_other_class
, false);
1933 if (this_dir
->status
[cnt
] == unknown
)
1936 this_dir
->status
[cnt
] = existing
;
1937 /* Do not update the directory information when loading
1938 auditing code. We must try to disturb the program as
1939 little as possible. */
1940 else if (loader
== NULL
1941 || GL(dl_ns
)[loader
->l_ns
]._ns_loaded
->l_auditing
== 0)
1943 /* We failed to open machine dependent library. Let's
1944 test whether there is any directory at all. */
1947 buf
[buflen
- namelen
- 1] = '\0';
1949 if (__stat64 (buf
, &st
) != 0
1950 || ! S_ISDIR (st
.st_mode
))
1951 /* The directory does not exist or it is no directory. */
1952 this_dir
->status
[cnt
] = nonexisting
;
1954 this_dir
->status
[cnt
] = existing
;
1958 /* Remember whether we found any existing directory. */
1959 here_any
|= this_dir
->status
[cnt
] != nonexisting
;
1961 if (fd
!= -1 && __glibc_unlikely (mode
& __RTLD_SECURE
)
1962 && __libc_enable_secure
)
1964 /* This is an extra security effort to make sure nobody can
1965 preload broken shared objects which are in the trusted
1966 directories and so exploit the bugs. */
1969 if (__fstat64 (fd
, &st
) != 0
1970 || (st
.st_mode
& S_ISUID
) == 0)
1972 /* The shared object cannot be tested for being SUID
1973 or this bit is not set. In this case we must not
1975 __close_nocancel (fd
);
1977 /* We simply ignore the file, signal this by setting
1978 the error value which would have been set by `open'. */
1986 *realname
= (char *) malloc (buflen
);
1987 if (*realname
!= NULL
)
1989 memcpy (*realname
, buf
, buflen
);
1994 /* No memory for the name, we certainly won't be able
1995 to load and link it. */
1996 __close_nocancel (fd
);
2000 if (here_any
&& (err
= errno
) != ENOENT
&& err
!= EACCES
)
2001 /* The file exists and is readable, but something went wrong. */
2004 /* Remember whether we found anything. */
2007 while (*++dirs
!= NULL
);
2009 /* Remove the whole path if none of the directories exists. */
2010 if (__glibc_unlikely (! any
))
2012 /* Paths which were allocated using the minimal malloc() in ld.so
2013 must not be freed using the general free() in libc. */
2017 /* __rtld_search_dirs and __rtld_env_path_list are
2018 attribute_relro, therefore avoid writing to them. */
2019 if (sps
!= &__rtld_search_dirs
&& sps
!= &__rtld_env_path_list
)
2020 sps
->dirs
= (void *) -1;
2026 /* Map in the shared object file NAME. */
2029 _dl_map_object (struct link_map
*loader
, const char *name
,
2030 int type
, int trace_mode
, int mode
, Lmid_t nsid
)
2033 const char *origname
= NULL
;
2040 assert (nsid
< GL(dl_nns
));
2042 /* Look for this name among those already loaded. */
2043 for (l
= GL(dl_ns
)[nsid
]._ns_loaded
; l
; l
= l
->l_next
)
2045 /* If the requested name matches the soname of a loaded object,
2046 use that object. Elide this check for names that have not
2048 if (__glibc_unlikely ((l
->l_faked
| l
->l_removed
) != 0))
2050 if (!_dl_name_match_p (name
, l
))
2054 if (__glibc_likely (l
->l_soname_added
)
2055 || l
->l_info
[DT_SONAME
] == NULL
)
2058 soname
= ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
2059 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
2060 if (strcmp (name
, soname
) != 0)
2063 /* We have a match on a new name -- cache it. */
2064 add_name_to_object (l
, soname
);
2065 l
->l_soname_added
= 1;
2068 /* We have a match. */
2072 /* Display information if we are debugging. */
2073 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
)
2075 _dl_debug_printf ((mode
& __RTLD_CALLMAP
) == 0
2076 ? "\nfile=%s [%lu]; needed by %s [%lu]\n"
2077 : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n",
2078 name
, nsid
, DSO_FILENAME (loader
->l_name
), loader
->l_ns
);
2081 /* Give the auditing libraries a chance to change the name before we
2083 if (__glibc_unlikely (GLRO(dl_naudit
) > 0)
2084 && (loader
== NULL
|| loader
->l_auditing
== 0))
2086 struct audit_ifaces
*afct
= GLRO(dl_audit
);
2087 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
2089 if (afct
->objsearch
!= NULL
)
2091 const char *before
= name
;
2092 struct auditstate
*state
= link_map_audit_state (loader
, cnt
);
2093 name
= afct
->objsearch (name
, &state
->cookie
, LA_SER_ORIG
);
2096 /* Do not try anything further. */
2100 if (before
!= name
&& strcmp (before
, name
) != 0)
2102 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
))
2103 _dl_debug_printf ("audit changed filename %s -> %s\n",
2106 if (origname
== NULL
)
2116 /* Will be true if we found a DSO which is of the other ELF class. */
2117 bool found_other_class
= false;
2119 if (strchr (name
, '/') == NULL
)
2121 /* Search for NAME in several places. */
2123 size_t namelen
= strlen (name
) + 1;
2125 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
2126 _dl_debug_printf ("find library=%s [%lu]; searching\n", name
, nsid
);
2130 /* When the object has the RUNPATH information we don't use any
2132 if (loader
== NULL
|| loader
->l_info
[DT_RUNPATH
] == NULL
)
2134 /* This is the executable's map (if there is one). Make sure that
2135 we do not look at it twice. */
2136 struct link_map
*main_map
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2137 bool did_main_map
= false;
2139 /* First try the DT_RPATH of the dependent object that caused NAME
2140 to be loaded. Then that object's dependent, and on up. */
2141 for (l
= loader
; l
; l
= l
->l_loader
)
2142 if (cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
2144 fd
= open_path (name
, namelen
, mode
,
2146 &realname
, &fb
, loader
, LA_SER_RUNPATH
,
2147 &found_other_class
);
2151 did_main_map
|= l
== main_map
;
2154 /* If dynamically linked, try the DT_RPATH of the executable
2155 itself. NB: we do this for lookups in any namespace. */
2156 if (fd
== -1 && !did_main_map
2157 && main_map
!= NULL
&& main_map
->l_type
!= lt_loaded
2158 && cache_rpath (main_map
, &main_map
->l_rpath_dirs
, DT_RPATH
,
2160 fd
= open_path (name
, namelen
, mode
,
2161 &main_map
->l_rpath_dirs
,
2162 &realname
, &fb
, loader
?: main_map
, LA_SER_RUNPATH
,
2163 &found_other_class
);
2166 /* Try the LD_LIBRARY_PATH environment variable. */
2167 if (fd
== -1 && __rtld_env_path_list
.dirs
!= (void *) -1)
2168 fd
= open_path (name
, namelen
, mode
, &__rtld_env_path_list
,
2170 loader
?: GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
,
2171 LA_SER_LIBPATH
, &found_other_class
);
2173 /* Look at the RUNPATH information for this binary. */
2174 if (fd
== -1 && loader
!= NULL
2175 && cache_rpath (loader
, &loader
->l_runpath_dirs
,
2176 DT_RUNPATH
, "RUNPATH"))
2177 fd
= open_path (name
, namelen
, mode
,
2178 &loader
->l_runpath_dirs
, &realname
, &fb
, loader
,
2179 LA_SER_RUNPATH
, &found_other_class
);
2183 realname
= _dl_sysdep_open_object (name
, namelen
, &fd
);
2184 if (realname
!= NULL
)
2186 fd
= open_verify (realname
, fd
,
2187 &fb
, loader
?: GL(dl_ns
)[nsid
]._ns_loaded
,
2188 LA_SER_CONFIG
, mode
, &found_other_class
,
2197 && (__glibc_likely ((mode
& __RTLD_SECURE
) == 0)
2198 || ! __libc_enable_secure
)
2199 && __glibc_likely (GLRO(dl_inhibit_cache
) == 0))
2201 /* Check the list of libraries in the file /etc/ld.so.cache,
2202 for compatibility with Linux's ldconfig program. */
2203 char *cached
= _dl_load_cache_lookup (name
);
2207 // XXX Correct to unconditionally default to namespace 0?
2209 ?: GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
2215 /* If the loader has the DF_1_NODEFLIB flag set we must not
2216 use a cache entry from any of these directories. */
2217 if (__glibc_unlikely (l
->l_flags_1
& DF_1_NODEFLIB
))
2219 const char *dirp
= system_dirs
;
2220 unsigned int cnt
= 0;
2224 if (memcmp (cached
, dirp
, system_dirs_len
[cnt
]) == 0)
2226 /* The prefix matches. Don't use the entry. */
2232 dirp
+= system_dirs_len
[cnt
] + 1;
2235 while (cnt
< nsystem_dirs_len
);
2240 fd
= open_verify (cached
, -1,
2241 &fb
, loader
?: GL(dl_ns
)[nsid
]._ns_loaded
,
2242 LA_SER_CONFIG
, mode
, &found_other_class
,
2244 if (__glibc_likely (fd
!= -1))
2253 /* Finally, try the default path. */
2255 && ((l
= loader
?: GL(dl_ns
)[nsid
]._ns_loaded
) == NULL
2256 || __glibc_likely (!(l
->l_flags_1
& DF_1_NODEFLIB
)))
2257 && __rtld_search_dirs
.dirs
!= (void *) -1)
2258 fd
= open_path (name
, namelen
, mode
, &__rtld_search_dirs
,
2259 &realname
, &fb
, l
, LA_SER_DEFAULT
, &found_other_class
);
2261 /* Add another newline when we are tracing the library loading. */
2262 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
))
2263 _dl_debug_printf ("\n");
2267 /* The path may contain dynamic string tokens. */
2269 ? expand_dynamic_string_token (loader
, name
)
2271 if (realname
== NULL
)
2275 fd
= open_verify (realname
, -1, &fb
,
2276 loader
?: GL(dl_ns
)[nsid
]._ns_loaded
, 0, mode
,
2277 &found_other_class
, true);
2278 if (__glibc_unlikely (fd
== -1))
2286 /* In case the LOADER information has only been provided to get to
2287 the appropriate RUNPATH/RPATH information we do not need it
2289 if (mode
& __RTLD_CALLMAP
)
2292 if (__glibc_unlikely (fd
== -1))
2295 && __glibc_likely ((GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
) == 0))
2297 /* We haven't found an appropriate library. But since we
2298 are only interested in the list of libraries this isn't
2299 so severe. Fake an entry with all the information we
2301 static const Elf_Symndx dummy_bucket
= STN_UNDEF
;
2303 /* Allocate a new object map. */
2304 if ((name_copy
= __strdup (name
)) == NULL
2305 || (l
= _dl_new_object (name_copy
, name
, type
, loader
,
2306 mode
, nsid
)) == NULL
)
2309 _dl_signal_error (ENOMEM
, name
, NULL
,
2310 N_("cannot create shared object descriptor"));
2312 /* Signal that this is a faked entry. */
2314 /* Since the descriptor is initialized with zero we do not
2316 l->l_reserved = 0; */
2317 l
->l_buckets
= &dummy_bucket
;
2321 /* Enter the object in the object list. */
2322 _dl_add_to_namespace_list (l
, nsid
);
2326 else if (found_other_class
)
2327 _dl_signal_error (0, name
, NULL
,
2328 ELFW(CLASS
) == ELFCLASS32
2329 ? N_("wrong ELF class: ELFCLASS64")
2330 : N_("wrong ELF class: ELFCLASS32"));
2332 _dl_signal_error (errno
, name
, NULL
,
2333 N_("cannot open shared object file"));
2336 void *stack_end
= __libc_stack_end
;
2337 return _dl_map_object_from_fd (name
, origname
, fd
, &fb
, realname
, loader
,
2338 type
, mode
, &stack_end
, nsid
);
2341 struct add_path_state
2350 add_path (struct add_path_state
*p
, const struct r_search_path_struct
*sps
,
2353 if (sps
->dirs
!= (void *) -1)
2355 struct r_search_path_elem
**dirs
= sps
->dirs
;
2358 const struct r_search_path_elem
*const r
= *dirs
++;
2362 p
->si
->dls_size
+= MAX (2, r
->dirnamelen
);
2366 Dl_serpath
*const sp
= &p
->si
->dls_serpath
[p
->idx
++];
2367 sp
->dls_name
= p
->allocptr
;
2368 if (r
->dirnamelen
< 2)
2369 *p
->allocptr
++ = r
->dirnamelen
? '/' : '.';
2371 p
->allocptr
= __mempcpy (p
->allocptr
,
2372 r
->dirname
, r
->dirnamelen
- 1);
2373 *p
->allocptr
++ = '\0';
2374 sp
->dls_flags
= flags
;
2377 while (*dirs
!= NULL
);
2382 _dl_rtld_di_serinfo (struct link_map
*loader
, Dl_serinfo
*si
, bool counting
)
2390 struct add_path_state p
=
2392 .counting
= counting
,
2395 .allocptr
= (char *) &si
->dls_serpath
[si
->dls_cnt
]
2398 # define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */
2400 /* When the object has the RUNPATH information we don't use any RPATHs. */
2401 if (loader
->l_info
[DT_RUNPATH
] == NULL
)
2403 /* First try the DT_RPATH of the dependent object that caused NAME
2404 to be loaded. Then that object's dependent, and on up. */
2406 struct link_map
*l
= loader
;
2409 if (cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
2410 add_path (&p
, &l
->l_rpath_dirs
, XXX_RPATH
);
2415 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2416 if (loader
->l_ns
== LM_ID_BASE
)
2418 l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2419 if (l
!= NULL
&& l
->l_type
!= lt_loaded
&& l
!= loader
)
2420 if (cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
2421 add_path (&p
, &l
->l_rpath_dirs
, XXX_RPATH
);
2425 /* Try the LD_LIBRARY_PATH environment variable. */
2426 add_path (&p
, &__rtld_env_path_list
, XXX_ENV
);
2428 /* Look at the RUNPATH information for this binary. */
2429 if (cache_rpath (loader
, &loader
->l_runpath_dirs
, DT_RUNPATH
, "RUNPATH"))
2430 add_path (&p
, &loader
->l_runpath_dirs
, XXX_RUNPATH
);
2433 Here is where ld.so.cache gets checked, but we don't have
2434 a way to indicate that in the results for Dl_serinfo. */
2436 /* Finally, try the default path. */
2437 if (!(loader
->l_flags_1
& DF_1_NODEFLIB
))
2438 add_path (&p
, &__rtld_search_dirs
, XXX_default
);
2441 /* Count the struct size before the string area, which we didn't
2442 know before we completed dls_cnt. */
2443 si
->dls_size
+= (char *) &si
->dls_serpath
[si
->dls_cnt
] - (char *) si
;