]> sourceware.org Git - glibc.git/blame - elf/dl-load.c
support: Use macros for *stat wrappers
[glibc.git] / elf / dl-load.c
CommitLineData
0a54e401 1/* Map in a shared object's segments from the file.
dff8da6b 2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
718fdd87 3 Copyright The GNU Toolchain Authors.
afd4eb37 4 This file is part of the GNU C Library.
d66e34cd 5
afd4eb37 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
d66e34cd 10
afd4eb37
UD
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
41bdb6e2 14 Lesser General Public License for more details.
d66e34cd 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
d66e34cd 19
14e9dd67 20#include <elf.h>
0a54e401
UD
21#include <errno.h>
22#include <fcntl.h>
8e17ea58 23#include <libintl.h>
379d4ec4 24#include <stdbool.h>
0a54e401 25#include <stdlib.h>
d66e34cd 26#include <string.h>
d66e34cd 27#include <unistd.h>
a42195db 28#include <ldsodefs.h>
ca97fb53 29#include <bits/wordsize.h>
0a54e401 30#include <sys/mman.h>
f8f7e090 31#include <sys/param.h>
0a54e401
UD
32#include <sys/stat.h>
33#include <sys/types.h>
89baed0b 34#include <gnu/lib-names.h>
f753fa7d
L
35
36/* Type for the buffer we put the ELF header and hopefully the program
37 header. This buffer does not really have to be too large. In most
38 cases the program header follows the ELF header directly. If this
39 is not the case all bets are off and we can make the header
40 arbitrarily large and still won't get it read. This means the only
41 question is how large are the ELF and program header combined. The
42 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
43 bytes long. Each program header entry is again 32 and 56 bytes
44 long respectively. I.e., even with a file which has 10 program
45 header entries we only have to read 372B/624B respectively. Add to
46 this a bit of margin for program notes and reading 512B and 832B
630da022 47 for 32-bit and 64-bit files respectively is enough. If this
f753fa7d
L
48 heuristic should really fail for some file the code in
49 `_dl_map_object_from_fd' knows how to recover. */
50struct filebuf
51{
52 ssize_t len;
53#if __WORDSIZE == 32
54# define FILEBUF_SIZE 512
55#else
56# define FILEBUF_SIZE 832
57#endif
58 char buf[FILEBUF_SIZE] __attribute__ ((aligned (__alignof (ElfW(Ehdr)))));
59};
60
d66e34cd 61#include "dynamic-link.h"
4af6982e 62#include "get-dynamic-info.h"
a986484f 63#include <abi-tag.h>
664e7d93 64#include <stackinfo.h>
606832e6 65#include <sysdep.h>
815e6fa3 66#include <stap-probe.h>
9090848d 67#include <libc-pointer-arith.h>
8a0b17e4 68#include <array_length.h>
d66e34cd 69
dc5efe83 70#include <dl-dst.h>
fcccd512
RM
71#include <dl-load.h>
72#include <dl-map-segments.h>
73#include <dl-unmap-segments.h>
d6f373d2 74#include <dl-machine-reject-phdr.h>
f753fa7d 75#include <dl-prop.h>
329ea513 76#include <not-cancel.h>
9b8a44cd 77
d66e34cd
RM
78#include <endian.h>
79#if BYTE_ORDER == BIG_ENDIAN
fcf70d41 80# define byteorder ELFDATA2MSB
d66e34cd 81#elif BYTE_ORDER == LITTLE_ENDIAN
fcf70d41 82# define byteorder ELFDATA2LSB
d66e34cd 83#else
fcf70d41
UD
84# error "Unknown BYTE_ORDER " BYTE_ORDER
85# define byteorder ELFDATANONE
d66e34cd
RM
86#endif
87
14e9dd67 88#define STRING(x) __STRING (x)
d66e34cd 89
dcca3fe2 90
7ef90c15 91/* This is the decomposed LD_LIBRARY_PATH search path. */
50b1b7a3 92struct r_search_path_struct __rtld_env_path_list attribute_relro;
40a55d20 93
12264bd7 94/* List of the hardware capabilities we might end up using. */
56f8d442 95#ifdef SHARED
392a6b52
UD
96static const struct r_strlenpair *capstr attribute_relro;
97static size_t ncapstr attribute_relro;
98static size_t max_capstrlen attribute_relro;
56f8d442
FW
99#else
100enum { ncapstr = 1, max_capstrlen = 0 };
101#endif
12264bd7 102
40a55d20 103
8a0b17e4
FW
104/* Get the generated information about the trusted directories. Use
105 an array of concatenated strings to avoid relocations. See
106 gen-trusted-dirs.awk. */
ab7eb292
UD
107#include "trusted-dirs.h"
108
109static const char system_dirs[] = SYSTEM_DIRS;
110static const size_t system_dirs_len[] =
111{
112 SYSTEM_DIRS_LEN
113};
8a0b17e4 114#define nsystem_dirs_len array_length (system_dirs_len)
32c85e43 115
47c3cd7a
UD
116static bool
117is_trusted_path_normalize (const char *path, size_t len)
118{
22836f52
UD
119 if (len == 0)
120 return false;
121
47c3cd7a
UD
122 char *npath = (char *) alloca (len + 2);
123 char *wnp = npath;
47c3cd7a
UD
124 while (*path != '\0')
125 {
126 if (path[0] == '/')
127 {
128 if (path[1] == '.')
129 {
130 if (path[2] == '.' && (path[3] == '/' || path[3] == '\0'))
131 {
132 while (wnp > npath && *--wnp != '/')
133 ;
134 path += 3;
135 continue;
136 }
137 else if (path[2] == '/' || path[2] == '\0')
138 {
139 path += 2;
140 continue;
141 }
142 }
143
144 if (wnp > npath && wnp[-1] == '/')
145 {
146 ++path;
147 continue;
148 }
149 }
150
151 *wnp++ = *path++;
152 }
22836f52
UD
153
154 if (wnp == npath || wnp[-1] != '/')
47c3cd7a 155 *wnp++ = '/';
47c3cd7a 156
22836f52
UD
157 const char *trun = system_dirs;
158
159 for (size_t idx = 0; idx < nsystem_dirs_len; ++idx)
160 {
161 if (wnp - npath >= system_dirs_len[idx]
162 && memcmp (trun, npath, system_dirs_len[idx]) == 0)
163 /* Found it. */
164 return true;
165
166 trun += system_dirs_len[idx] + 1;
167 }
168
169 return false;
47c3cd7a
UD
170}
171
5aad5f61
CD
172/* Given a substring starting at INPUT, just after the DST '$' start
173 token, determine if INPUT contains DST token REF, following the
174 ELF gABI rules for DSTs:
175
176 * Longest possible sequence using the rules (greedy).
177
178 * Must start with a $ (enforced by caller).
179
180 * Must follow $ with one underscore or ASCII [A-Za-z] (caller
181 follows these rules for REF) or '{' (start curly quoted name).
47c3cd7a 182
5aad5f61
CD
183 * Must follow first two characters with zero or more [A-Za-z0-9_]
184 (enforced by caller) or '}' (end curly quoted name).
185
186 If the sequence is a DST matching REF then the length of the DST
187 (excluding the $ sign but including curly braces, if any) is
188 returned, otherwise 0. */
6d5d3ae3 189static size_t
5aad5f61 190is_dst (const char *input, const char *ref)
6d5d3ae3 191{
379d4ec4 192 bool is_curly = false;
6d5d3ae3 193
5aad5f61
CD
194 /* Is a ${...} input sequence? */
195 if (input[0] == '{')
379d4ec4
UD
196 {
197 is_curly = true;
5aad5f61 198 ++input;
379d4ec4 199 }
6d5d3ae3 200
5aad5f61
CD
201 /* Check for matching name, following closing curly brace (if
202 required), or trailing characters which are part of an
203 identifier. */
204 size_t rlen = strlen (ref);
205 if (strncmp (input, ref, rlen) != 0
206 || (is_curly && input[rlen] != '}')
207 || ((input[rlen] >= 'A' && input[rlen] <= 'Z')
208 || (input[rlen] >= 'a' && input[rlen] <= 'z')
209 || (input[rlen] >= '0' && input[rlen] <= '9')
210 || (input[rlen] == '_')))
6d5d3ae3
UD
211 return 0;
212
5aad5f61
CD
213 if (is_curly)
214 /* Count the two curly braces. */
215 return rlen + 2;
216 else
217 return rlen;
6d5d3ae3
UD
218}
219
a745c837
CD
220/* INPUT should be the start of a path e.g DT_RPATH or name e.g.
221 DT_NEEDED. The return value is the number of known DSTs found. We
222 count all known DSTs regardless of __libc_enable_secure; the caller
223 is responsible for enforcing the security of the substitution rules
224 (usually _dl_dst_substitute). */
dc5efe83 225size_t
5aad5f61 226_dl_dst_count (const char *input)
f787edde 227{
f787edde 228 size_t cnt = 0;
f787edde 229
5aad5f61
CD
230 input = strchr (input, '$');
231
232 /* Most likely there is no DST. */
233 if (__glibc_likely (input == NULL))
234 return 0;
235
dc5efe83 236 do
f787edde 237 {
379d4ec4 238 size_t len;
f787edde 239
5aad5f61
CD
240 ++input;
241 /* All DSTs must follow ELF gABI rules, see is_dst (). */
242 if ((len = is_dst (input, "ORIGIN")) != 0
243 || (len = is_dst (input, "PLATFORM")) != 0
244 || (len = is_dst (input, "LIB")) != 0)
f787edde
UD
245 ++cnt;
246
5aad5f61
CD
247 /* There may be more than one DST in the input. */
248 input = strchr (input + len, '$');
f787edde 249 }
5aad5f61 250 while (input != NULL);
f787edde 251
dc5efe83
UD
252 return cnt;
253}
f787edde 254
5aad5f61
CD
255/* Process INPUT for DSTs and store in RESULT using the information
256 from link map L to resolve the DSTs. This function only handles one
257 path at a time and does not handle colon-separated path lists (see
258 fillin_rpath ()). Lastly the size of result in bytes should be at
259 least equal to the value returned by DL_DST_REQUIRED. Note that it
260 is possible for a DT_NEEDED, DT_AUXILIARY, and DT_FILTER entries to
261 have colons, but we treat those as literal colons here, not as path
630da022 262 list delimiters. */
dc5efe83 263char *
5aad5f61 264_dl_dst_substitute (struct link_map *l, const char *input, char *result)
dc5efe83 265{
5aad5f61
CD
266 /* Copy character-by-character from input into the working pointer
267 looking for any DSTs. We track the start of input and if we are
268 going to check for trusted paths, all of which are part of $ORIGIN
269 handling in SUID/SGID cases (see below). In some cases, like when
270 a DST cannot be replaced, we may set result to an empty string and
271 return. */
47c3cd7a 272 char *wp = result;
5aad5f61 273 const char *start = input;
47c3cd7a 274 bool check_for_trusted = false;
dc5efe83 275
f787edde
UD
276 do
277 {
5aad5f61 278 if (__glibc_unlikely (*input == '$'))
f787edde 279 {
2541eda0 280 const char *repl = NULL;
379d4ec4 281 size_t len;
2541eda0 282
5aad5f61
CD
283 ++input;
284 if ((len = is_dst (input, "ORIGIN")) != 0)
e7c8359e 285 {
5aad5f61
CD
286 /* For SUID/GUID programs we normally ignore the path with
287 $ORIGIN in DT_RUNPATH, or DT_RPATH. However, there is
288 one exception to this rule, and it is:
289
290 * $ORIGIN appears as the first path element, and is
291 the only string in the path or is immediately
292 followed by a path separator and the rest of the
a745c837
CD
293 path,
294
295 and ...
5aad5f61
CD
296
297 * The path is rooted in a trusted directory.
298
299 This exception allows such programs to reference
300 shared libraries in subdirectories of trusted
301 directories. The use case is one of general
302 organization and deployment flexibility.
303 Trusted directories are usually such paths as "/lib64"
304 or "/usr/lib64", and the usual RPATHs take the form of
305 [$ORIGIN/../$LIB/somedir]. */
306 if (__glibc_unlikely (__libc_enable_secure)
307 && !(input == start + 1
308 && (input[len] == '\0' || input[len] == '/')))
309 repl = (const char *) -1;
310 else
311 repl = l->l_origin;
312
6bc6bd3b 313 check_for_trusted = (__libc_enable_secure
47c3cd7a 314 && l->l_type == lt_executable);
e7c8359e 315 }
5aad5f61 316 else if ((len = is_dst (input, "PLATFORM")) != 0)
afdca0f2 317 repl = GLRO(dl_platform);
5aad5f61 318 else if ((len = is_dst (input, "LIB")) != 0)
27af5372 319 repl = DL_DST_LIB;
2541eda0 320
2541eda0
UD
321 if (repl != NULL && repl != (const char *) -1)
322 {
323 wp = __stpcpy (wp, repl);
5aad5f61 324 input += len;
2541eda0 325 }
5aad5f61 326 else if (len != 0)
f787edde 327 {
5aad5f61
CD
328 /* We found a valid DST that we know about, but we could
329 not find a replacement value for it, therefore we
330 cannot use this path and discard it. */
331 *result = '\0';
332 return result;
f787edde
UD
333 }
334 else
27aa0631 335 /* No DST we recognize. */
379d4ec4 336 *wp++ = '$';
f787edde 337 }
2541eda0 338 else
f787edde 339 {
5aad5f61 340 *wp++ = *input++;
f787edde 341 }
f787edde 342 }
5aad5f61 343 while (*input != '\0');
f787edde 344
47c3cd7a 345 /* In SUID/SGID programs, after $ORIGIN expansion the normalized
5aad5f61
CD
346 path must be rooted in one of the trusted directories. The $LIB
347 and $PLATFORM DST cannot in any way be manipulated by the caller
348 because they are fixed values that are set by the dynamic loader
349 and therefore any paths using just $LIB or $PLATFORM need not be
350 checked for trust, the authors of the binaries themselves are
351 trusted to have designed this correctly. Only $ORIGIN is tested in
352 this way because it may be manipulated in some ways with hard
353 links. */
1b26b855 354 if (__glibc_unlikely (check_for_trusted)
5aad5f61
CD
355 && !is_trusted_path_normalize (result, wp - result))
356 {
357 *result = '\0';
358 return result;
359 }
47c3cd7a 360
f787edde
UD
361 *wp = '\0';
362
363 return result;
364}
365
dc5efe83 366
5aad5f61
CD
367/* Return a malloc allocated copy of INPUT with all recognized DSTs
368 replaced. On some platforms it might not be possible to determine the
369 path from which the object belonging to the map is loaded. In this
370 case the path containing the DST is left out. On error NULL
371 is returned. */
dc5efe83 372static char *
5aad5f61 373expand_dynamic_string_token (struct link_map *l, const char *input)
dc5efe83
UD
374{
375 /* We make two runs over the string. First we determine how large the
844c394a 376 resulting string is and then we copy it over. Since this is no
dc5efe83
UD
377 frequently executed operation we are looking here not for performance
378 but rather for code size. */
379 size_t cnt;
380 size_t total;
381 char *result;
382
5aad5f61
CD
383 /* Determine the number of DSTs. */
384 cnt = _dl_dst_count (input);
dc5efe83
UD
385
386 /* If we do not have to replace anything simply copy the string. */
1b26b855 387 if (__glibc_likely (cnt == 0))
5aad5f61 388 return __strdup (input);
dc5efe83
UD
389
390 /* Determine the length of the substituted string. */
5aad5f61 391 total = DL_DST_REQUIRED (l, input, strlen (input), cnt);
dc5efe83
UD
392
393 /* Allocate the necessary memory. */
394 result = (char *) malloc (total + 1);
395 if (result == NULL)
396 return NULL;
397
5aad5f61 398 return _dl_dst_substitute (l, input, result);
dc5efe83
UD
399}
400
401
0413b54c
UD
402/* Add `name' to the list of names for a particular shared object.
403 `name' is expected to have been allocated with malloc and will
404 be freed if the shared object already has this name.
405 Returns false if the object already had this name. */
76156ea1 406static void
76156ea1 407add_name_to_object (struct link_map *l, const char *name)
0a54e401 408{
0413b54c
UD
409 struct libname_list *lnp, *lastp;
410 struct libname_list *newname;
76156ea1 411 size_t name_len;
0413b54c
UD
412
413 lastp = NULL;
414 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
415 if (strcmp (name, lnp->name) == 0)
76156ea1 416 return;
0413b54c 417
76156ea1 418 name_len = strlen (name) + 1;
839be784 419 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
0413b54c 420 if (newname == NULL)
da832465
UD
421 {
422 /* No more memory. */
154d10bd 423 _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
76156ea1 424 return;
da832465 425 }
0413b54c
UD
426 /* The object should have a libname set from _dl_new_object. */
427 assert (lastp != NULL);
428
76156ea1 429 newname->name = memcpy (newname + 1, name, name_len);
0413b54c 430 newname->next = NULL;
752a2a50 431 newname->dont_free = 0;
395be7c2
MS
432 /* CONCURRENCY NOTES:
433
434 Make sure the initialization of newname happens before its address is
435 read from the lastp->next store below.
436
437 GL(dl_load_lock) is held here (and by other writers, e.g. dlclose), so
438 readers of libname_list->next (e.g. _dl_check_caller or the reads above)
439 can use that for synchronization, however the read in _dl_name_match_p
440 may be executed without holding the lock during _dl_runtime_resolve
441 (i.e. lazy symbol resolution when a function of library l is called).
442
443 The release MO store below synchronizes with the acquire MO load in
444 _dl_name_match_p. Other writes need to synchronize with that load too,
445 however those happen either early when the process is single threaded
446 (dl_main) or when the library is unloaded (dlclose) and the user has to
447 synchronize library calls with unloading. */
448 atomic_store_release (&lastp->next, newname);
0413b54c
UD
449}
450
12264bd7 451/* Standard search directories. */
50b1b7a3 452struct r_search_path_struct __rtld_search_dirs attribute_relro;
0a54e401 453
a658675d 454static size_t max_dirnamelen;
0a54e401 455
dd9423a6 456static struct r_search_path_elem **
0a54e401 457fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
10e93d96 458 const char *what, const char *where, struct link_map *l)
0a54e401
UD
459{
460 char *cp;
461 size_t nelems = 0;
462
463 while ((cp = __strsep (&rpath, sep)) != NULL)
464 {
465 struct r_search_path_elem *dirp;
3e3c904d
AJ
466 char *to_free = NULL;
467 size_t len = 0;
2a939a7e 468
3e3c904d
AJ
469 /* `strsep' can pass an empty string. */
470 if (*cp != '\0')
471 {
472 to_free = cp = expand_dynamic_string_token (l, cp);
2a939a7e 473
3e3c904d
AJ
474 /* expand_dynamic_string_token can return NULL in case of empty
475 path or memory allocation failure. */
476 if (cp == NULL)
477 continue;
12264bd7 478
3e3c904d
AJ
479 /* Compute the length after dynamic string token expansion and
480 ignore empty paths. */
481 len = strlen (cp);
482 if (len == 0)
483 {
484 free (to_free);
485 continue;
486 }
12264bd7 487
3e3c904d
AJ
488 /* Remove trailing slashes (except for "/"). */
489 while (len > 1 && cp[len - 1] == '/')
490 --len;
0a54e401 491
3e3c904d
AJ
492 /* Now add one if there is none so far. */
493 if (len > 0 && cp[len - 1] != '/')
494 cp[len++] = '/';
495 }
ab7eb292 496
0a54e401 497 /* See if this directory is already known. */
d6b5d570 498 for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
12264bd7 499 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
0a54e401
UD
500 break;
501
502 if (dirp != NULL)
503 {
12264bd7 504 /* It is available, see whether it's on our own list. */
0a54e401
UD
505 size_t cnt;
506 for (cnt = 0; cnt < nelems; ++cnt)
507 if (result[cnt] == dirp)
508 break;
509
510 if (cnt == nelems)
511 result[nelems++] = dirp;
512 }
513 else
514 {
12264bd7 515 size_t cnt;
839be784 516 enum r_dir_status init_val;
4a6d1198 517 size_t where_len = where ? strlen (where) + 1 : 0;
12264bd7 518
0a54e401 519 /* It's a new directory. Create an entry and add it. */
12264bd7 520 dirp = (struct r_search_path_elem *)
32ee8d95 521 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
f55727ca 522 + where_len + len + 1);
0a54e401 523 if (dirp == NULL)
154d10bd
UD
524 _dl_signal_error (ENOMEM, NULL, NULL,
525 N_("cannot create cache for search path"));
0a54e401 526
f55727ca
UD
527 dirp->dirname = ((char *) dirp + sizeof (*dirp)
528 + ncapstr * sizeof (enum r_dir_status));
104d0bd3 529 *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
0a54e401 530 dirp->dirnamelen = len;
12264bd7
UD
531
532 if (len > max_dirnamelen)
533 max_dirnamelen = len;
534
07ba7349
UD
535 /* We have to make sure all the relative directories are
536 never ignored. The current directory might change and
537 all our saved information would be void. */
538 init_val = cp[0] != '/' ? existing : unknown;
839be784
UD
539 for (cnt = 0; cnt < ncapstr; ++cnt)
540 dirp->status[cnt] = init_val;
0a54e401 541
b5efde2f 542 dirp->what = what;
a1ffb40e 543 if (__glibc_likely (where != NULL))
f55727ca 544 dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
d6b5d570 545 + (ncapstr * sizeof (enum r_dir_status)),
4a6d1198
UD
546 where, where_len);
547 else
548 dirp->where = NULL;
b5efde2f 549
d6b5d570
UD
550 dirp->next = GL(dl_all_dirs);
551 GL(dl_all_dirs) = dirp;
0a54e401
UD
552
553 /* Put it in the result array. */
554 result[nelems++] = dirp;
555 }
2a939a7e 556 free (to_free);
0a54e401
UD
557 }
558
559 /* Terminate the array. */
560 result[nelems] = NULL;
561
562 return result;
563}
564
565
2692deea 566static bool
f55727ca
UD
567decompose_rpath (struct r_search_path_struct *sps,
568 const char *rpath, struct link_map *l, const char *what)
0a54e401
UD
569{
570 /* Make a copy we can work with. */
f787edde 571 const char *where = l->l_name;
0a54e401
UD
572 char *cp;
573 struct r_search_path_elem **result;
310930c1 574 size_t nelems;
39b3385d
UD
575 /* Initialize to please the compiler. */
576 const char *errstring = NULL;
310930c1 577
fcf70d41
UD
578 /* First see whether we must forget the RUNPATH and RPATH from this
579 object. */
1b26b855 580 if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL)
6bc6bd3b 581 && !__libc_enable_secure)
310930c1 582 {
afdca0f2 583 const char *inhp = GLRO(dl_inhibit_rpath);
9710f75d
UD
584
585 do
310930c1 586 {
9710f75d
UD
587 const char *wp = where;
588
589 while (*inhp == *wp && *wp != '\0')
590 {
591 ++inhp;
592 ++wp;
593 }
594
595 if (*wp == '\0' && (*inhp == '\0' || *inhp == ':'))
310930c1 596 {
fcf70d41
UD
597 /* This object is on the list of objects for which the
598 RUNPATH and RPATH must not be used. */
2692deea
UD
599 sps->dirs = (void *) -1;
600 return false;
310930c1 601 }
9710f75d
UD
602
603 while (*inhp != '\0')
604 if (*inhp++ == ':')
605 break;
310930c1 606 }
9710f75d 607 while (*inhp != '\0');
310930c1 608 }
0a54e401 609
dbba87d5
DL
610 /* Ignore empty rpaths. */
611 if (*rpath == '\0')
612 {
613 sps->dirs = (struct r_search_path_elem **) -1;
614 return false;
615 }
616
2a939a7e 617 /* Make a writable copy. */
dbba87d5 618 char *copy = __strdup (rpath);
f787edde 619 if (copy == NULL)
39b3385d
UD
620 {
621 errstring = N_("cannot create RUNPATH/RPATH copy");
622 goto signal_error;
623 }
f787edde 624
310930c1 625 /* Count the number of necessary elements in the result array. */
310930c1 626 nelems = 0;
0a54e401
UD
627 for (cp = copy; *cp != '\0'; ++cp)
628 if (*cp == ':')
629 ++nelems;
630
7ef90c15
UD
631 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
632 number of necessary entries. */
633 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
0a54e401
UD
634 * sizeof (*result));
635 if (result == NULL)
2692deea 636 {
ce31a3b1 637 free (copy);
2692deea
UD
638 errstring = N_("cannot create cache for search path");
639 signal_error:
640 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
641 }
0a54e401 642
10e93d96 643 fillin_rpath (copy, result, ":", what, where, l);
f55727ca
UD
644
645 /* Free the copied RPATH string. `fillin_rpath' make own copies if
646 necessary. */
647 free (copy);
648
3e3c904d
AJ
649 /* There is no path after expansion. */
650 if (result[0] == NULL)
651 {
652 free (result);
653 sps->dirs = (struct r_search_path_elem **) -1;
654 return false;
655 }
656
f55727ca
UD
657 sps->dirs = result;
658 /* The caller will change this value if we haven't used a real malloc. */
659 sps->malloced = 1;
2692deea 660 return true;
0a54e401
UD
661}
662
45e4762c
RM
663/* Make sure cached path information is stored in *SP
664 and return true if there are any paths to search there. */
25337753 665static bool
45e4762c
RM
666cache_rpath (struct link_map *l,
667 struct r_search_path_struct *sp,
668 int tag,
669 const char *what)
670{
671 if (sp->dirs == (void *) -1)
672 return false;
673
674 if (sp->dirs != NULL)
675 return true;
676
677 if (l->l_info[tag] == NULL)
678 {
679 /* There is no path. */
680 sp->dirs = (void *) -1;
681 return false;
682 }
683
684 /* Make sure the cache information is available. */
2692deea
UD
685 return decompose_rpath (sp, (const char *) (D_PTR (l, l_info[DT_STRTAB])
686 + l->l_info[tag]->d_un.d_val),
687 l, what);
45e4762c
RM
688}
689
0a54e401
UD
690
691void
dad90d52
FW
692_dl_init_paths (const char *llp, const char *source,
693 const char *glibc_hwcaps_prepend,
694 const char *glibc_hwcaps_mask)
0a54e401 695{
ab7eb292
UD
696 size_t idx;
697 const char *strp;
12264bd7
UD
698 struct r_search_path_elem *pelem, **aelem;
699 size_t round_size;
2a939a7e 700 struct link_map __attribute__ ((unused)) *l = NULL;
39b3385d
UD
701 /* Initialize to please the compiler. */
702 const char *errstring = NULL;
0a54e401 703
7ef90c15
UD
704 /* Fill in the information about the application's RPATH and the
705 directories addressed by the LD_LIBRARY_PATH environment variable. */
0a54e401 706
56f8d442 707#ifdef SHARED
4317f9e1 708 /* Get the capabilities. */
dad90d52
FW
709 capstr = _dl_important_hwcaps (glibc_hwcaps_prepend, glibc_hwcaps_mask,
710 &ncapstr, &max_capstrlen);
56f8d442 711#endif
12264bd7
UD
712
713 /* First set up the rest of the default search directory entries. */
50b1b7a3 714 aelem = __rtld_search_dirs.dirs = (struct r_search_path_elem **)
4a6d1198 715 malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
50b1b7a3 716 if (__rtld_search_dirs.dirs == NULL)
39b3385d
UD
717 {
718 errstring = N_("cannot create search path array");
719 signal_error:
154d10bd 720 _dl_signal_error (ENOMEM, NULL, NULL, errstring);
39b3385d 721 }
12264bd7
UD
722
723 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
724 + ncapstr * sizeof (enum r_dir_status))
725 / sizeof (struct r_search_path_elem));
726
50b1b7a3
FW
727 __rtld_search_dirs.dirs[0]
728 = malloc (nsystem_dirs_len * round_size
729 * sizeof (*__rtld_search_dirs.dirs[0]));
730 if (__rtld_search_dirs.dirs[0] == NULL)
39b3385d
UD
731 {
732 errstring = N_("cannot create cache for search path");
733 goto signal_error;
734 }
12264bd7 735
50b1b7a3
FW
736 __rtld_search_dirs.malloced = 0;
737 pelem = GL(dl_all_dirs) = __rtld_search_dirs.dirs[0];
ab7eb292
UD
738 strp = system_dirs;
739 idx = 0;
740
741 do
12264bd7
UD
742 {
743 size_t cnt;
744
745 *aelem++ = pelem;
746
12264bd7
UD
747 pelem->what = "system search path";
748 pelem->where = NULL;
749
ab7eb292
UD
750 pelem->dirname = strp;
751 pelem->dirnamelen = system_dirs_len[idx];
752 strp += system_dirs_len[idx] + 1;
12264bd7 753
f55727ca
UD
754 /* System paths must be absolute. */
755 assert (pelem->dirname[0] == '/');
756 for (cnt = 0; cnt < ncapstr; ++cnt)
757 pelem->status[cnt] = unknown;
ab7eb292 758
4a6d1198 759 pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
ab7eb292
UD
760
761 pelem += round_size;
12264bd7 762 }
4a6d1198 763 while (idx < nsystem_dirs_len);
ab7eb292
UD
764
765 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
12264bd7 766 *aelem = NULL;
4317f9e1 767
4e6db99c
FW
768 /* This points to the map of the main object. If there is no main
769 object (e.g., under --help, use the dynamic loader itself as a
770 stand-in. */
c0f62c56 771 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
4e6db99c
FW
772#ifdef SHARED
773 if (l == NULL)
774 l = &GL (dl_rtld_map);
775#endif
33242131
CD
776 assert (l->l_type != lt_loaded);
777
778 if (l->l_info[DT_RUNPATH])
779 {
780 /* Allocate room for the search path and fill in information
781 from RUNPATH. */
782 decompose_rpath (&l->l_runpath_dirs,
783 (const void *) (D_PTR (l, l_info[DT_STRTAB])
784 + l->l_info[DT_RUNPATH]->d_un.d_val),
785 l, "RUNPATH");
786 /* During rtld init the memory is allocated by the stub malloc,
787 prevent any attempt to free it by the normal malloc. */
788 l->l_runpath_dirs.malloced = 0;
789
790 /* The RPATH is ignored. */
791 l->l_rpath_dirs.dirs = (void *) -1;
792 }
793 else
011ce8ed 794 {
33242131 795 l->l_runpath_dirs.dirs = (void *) -1;
40a55d20 796
33242131 797 if (l->l_info[DT_RPATH])
fcf70d41
UD
798 {
799 /* Allocate room for the search path and fill in information
33242131
CD
800 from RPATH. */
801 decompose_rpath (&l->l_rpath_dirs,
f55727ca 802 (const void *) (D_PTR (l, l_info[DT_STRTAB])
33242131
CD
803 + l->l_info[DT_RPATH]->d_un.d_val),
804 l, "RPATH");
805 /* During rtld init the memory is allocated by the stub
806 malloc, prevent any attempt to free it by the normal
807 malloc. */
808 l->l_rpath_dirs.malloced = 0;
fcf70d41 809 }
011ce8ed 810 else
33242131 811 l->l_rpath_dirs.dirs = (void *) -1;
0a54e401 812 }
7ef90c15
UD
813
814 if (llp != NULL && *llp != '\0')
0a54e401 815 {
bb195224 816 char *llp_tmp = strdupa (llp);
011ce8ed 817
7ef90c15
UD
818 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
819 elements it has. */
3ff3dfa5
FW
820 size_t nllp = 1;
821 for (const char *cp = llp_tmp; *cp != '\0'; ++cp)
822 if (*cp == ':' || *cp == ';')
823 ++nllp;
7ef90c15 824
50b1b7a3 825 __rtld_env_path_list.dirs = (struct r_search_path_elem **)
7ef90c15 826 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
50b1b7a3 827 if (__rtld_env_path_list.dirs == NULL)
39b3385d
UD
828 {
829 errstring = N_("cannot create cache for search path");
830 goto signal_error;
831 }
7ef90c15 832
50b1b7a3 833 (void) fillin_rpath (llp_tmp, __rtld_env_path_list.dirs, ":;",
27316f4a 834 source, NULL, l);
6a7c9bb4 835
50b1b7a3 836 if (__rtld_env_path_list.dirs[0] == NULL)
6a7c9bb4 837 {
50b1b7a3
FW
838 free (__rtld_env_path_list.dirs);
839 __rtld_env_path_list.dirs = (void *) -1;
6a7c9bb4 840 }
f55727ca 841
50b1b7a3 842 __rtld_env_path_list.malloced = 0;
81e0cb2d 843 }
152e7964 844 else
50b1b7a3 845 __rtld_env_path_list.dirs = (void *) -1;
0a54e401
UD
846}
847
848
c7aa8596
SN
849/* Process PT_GNU_PROPERTY program header PH in module L after
850 PT_LOAD segments are mapped. Only one NT_GNU_PROPERTY_TYPE_0
c00452d7
SN
851 note is handled which contains processor specific properties.
852 FD is -1 for the kernel mapped main executable otherwise it is
853 the fd used for loading module L. */
c7aa8596
SN
854
855void
c00452d7 856_dl_process_pt_gnu_property (struct link_map *l, int fd, const ElfW(Phdr) *ph)
c7aa8596
SN
857{
858 const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
859 const ElfW(Addr) size = ph->p_memsz;
860 const ElfW(Addr) align = ph->p_align;
861
862 /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes in
863 32-bit objects and to 8 bytes in 64-bit objects. Skip notes
864 with incorrect alignment. */
865 if (align != (__ELF_NATIVE_CLASS / 8))
866 return;
867
868 const ElfW(Addr) start = (ElfW(Addr)) note;
869 unsigned int last_type = 0;
870
871 while ((ElfW(Addr)) (note + 1) - start < size)
872 {
873 /* Find the NT_GNU_PROPERTY_TYPE_0 note. */
874 if (note->n_namesz == 4
875 && note->n_type == NT_GNU_PROPERTY_TYPE_0
876 && memcmp (note + 1, "GNU", 4) == 0)
877 {
878 /* Check for invalid property. */
879 if (note->n_descsz < 8
880 || (note->n_descsz % sizeof (ElfW(Addr))) != 0)
881 return;
882
883 /* Start and end of property array. */
884 unsigned char *ptr = (unsigned char *) (note + 1) + 4;
885 unsigned char *ptr_end = ptr + note->n_descsz;
886
887 do
888 {
889 unsigned int type = *(unsigned int *) ptr;
890 unsigned int datasz = *(unsigned int *) (ptr + 4);
891
892 /* Property type must be in ascending order. */
893 if (type < last_type)
894 return;
895
896 ptr += 8;
897 if ((ptr + datasz) > ptr_end)
898 return;
899
900 last_type = type;
901
902 /* Target specific property processing. */
c00452d7 903 if (_dl_process_gnu_property (l, fd, type, datasz, ptr) == 0)
c7aa8596
SN
904 return;
905
906 /* Check the next property item. */
907 ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
908 }
909 while ((ptr_end - ptr) >= 8);
910
911 /* Only handle one NT_GNU_PROPERTY_TYPE_0. */
912 return;
913 }
914
915 note = ((const void *) note
916 + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz,
917 align));
918 }
919}
920
921
ea03559a
RM
922/* Map in the shared object NAME, actually located in REALNAME, and already
923 opened on FD. */
924
f787edde
UD
925#ifndef EXTERNAL_MAP_FROM_FD
926static
927#endif
ea03559a 928struct link_map *
a1b85ae8
FW
929_dl_map_object_from_fd (const char *name, const char *origname, int fd,
930 struct filebuf *fbp, char *realname,
931 struct link_map *loader, int l_type, int mode,
932 void **stack_endp, Lmid_t nsid)
ea03559a 933{
622586fb 934 struct link_map *l = NULL;
266180eb
RM
935 const ElfW(Ehdr) *header;
936 const ElfW(Phdr) *phdr;
937 const ElfW(Phdr) *ph;
8193034b 938 size_t maplength;
b122c703 939 int type;
39b3385d
UD
940 /* Initialize to keep the compiler happy. */
941 const char *errstring = NULL;
942 int errval = 0;
61e0617a 943
2b26b084
FW
944 /* Get file information. To match the kernel behavior, do not fill
945 in this information for the executable in case of an explicit
946 loader invocation. */
c01ae97e 947 struct r_file_id id;
2b26b084 948 if (mode & __RTLD_OPENEXEC)
39b3385d 949 {
2b26b084
FW
950 assert (nsid == LM_ID_BASE);
951 memset (&id, 0, sizeof (id));
39b3385d 952 }
2b26b084
FW
953 else
954 {
955 if (__glibc_unlikely (!_dl_get_file_id (fd, &id)))
956 {
957 errstring = N_("cannot stat shared object");
cb5648b0 958 lose_errno:
2b26b084 959 errval = errno;
cb5648b0
SN
960 lose:
961 /* The file might already be closed. */
962 if (fd != -1)
963 __close_nocancel (fd);
c6b01653
SN
964 if (l != NULL && l->l_map_start != 0)
965 _dl_unmap_segments (l);
cb5648b0
SN
966 if (l != NULL && l->l_origin != (char *) -1l)
967 free ((char *) l->l_origin);
c6b01653
SN
968 if (l != NULL && !l->l_libname->dont_free)
969 free (l->l_libname);
970 if (l != NULL && l->l_phdr_allocated)
971 free ((void *) l->l_phdr);
cb5648b0
SN
972 free (l);
973 free (realname);
cb5648b0 974 _dl_signal_error (errval, name, NULL, errstring);
2b26b084 975 }
d66e34cd 976
2b26b084
FW
977 /* Look again to see if the real name matched another already loaded. */
978 for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
979 if (!l->l_removed && _dl_file_id_match_p (&l->l_file_id, &id))
980 {
981 /* The object is already loaded.
982 Just bump its reference count and return it. */
983 __close_nocancel (fd);
984
985 /* If the name is not in the list of names for this object add
986 it. */
987 free (realname);
988 add_name_to_object (l, name);
989
990 return l;
991 }
992 }
d66e34cd 993
c0f62c56
UD
994#ifdef SHARED
995 /* When loading into a namespace other than the base one we must
996 avoid loading ld.so since there can only be one copy. Ever. */
1b26b855 997 if (__glibc_unlikely (nsid != LM_ID_BASE)
c01ae97e 998 && (_dl_file_id_match_p (&id, &GL(dl_rtld_map).l_file_id)
c0f62c56
UD
999 || _dl_name_match_p (name, &GL(dl_rtld_map))))
1000 {
1001 /* This is indeed ld.so. Create a new link_map which refers to
1002 the real one for almost everything. */
1003 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
1004 if (l == NULL)
1005 goto fail_new;
1006
1007 /* Refer to the real descriptor. */
1008 l->l_real = &GL(dl_rtld_map);
1009
88361b40
L
1010 /* Copy l_addr and l_ld to avoid a GDB warning with dlmopen(). */
1011 l->l_addr = l->l_real->l_addr;
1012 l->l_ld = l->l_real->l_ld;
1013
c0f62c56
UD
1014 /* No need to bump the refcount of the real object, ld.so will
1015 never be unloaded. */
329ea513 1016 __close_nocancel (fd);
c0f62c56 1017
f0967738
AK
1018 /* Add the map for the mirrored object to the object list. */
1019 _dl_add_to_namespace_list (l, nsid);
1020
c0f62c56
UD
1021 return l;
1022 }
1023#endif
1024
2f54c82d 1025 if (mode & RTLD_NOLOAD)
96961bf7
AS
1026 {
1027 /* We are not supposed to load the object unless it is already
1028 loaded. So return now. */
4bff6e01 1029 free (realname);
329ea513 1030 __close_nocancel (fd);
96961bf7
AS
1031 return NULL;
1032 }
bf8b3e74 1033
8193034b 1034 /* Print debugging message. */
a1ffb40e 1035 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
c0f62c56 1036 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name, nsid);
8193034b 1037
a35e137a
UD
1038 /* This is the ELF header. We read it in `open_verify'. */
1039 header = (void *) fbp->buf;
d66e34cd 1040
ba79d61b 1041 /* Enter the new object in the list of loaded objects. */
c0f62c56 1042 l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
a1ffb40e 1043 if (__glibc_unlikely (l == NULL))
39b3385d 1044 {
7cb92a99 1045#ifdef SHARED
c0f62c56 1046 fail_new:
7cb92a99 1047#endif
39b3385d 1048 errstring = N_("cannot create shared object descriptor");
cb5648b0 1049 goto lose_errno;
39b3385d 1050 }
ba79d61b 1051
b122c703 1052 /* Extract the remaining details we need from the ELF header
32c85e43 1053 and then read in the program header table. */
b122c703
RM
1054 l->l_entry = header->e_entry;
1055 type = header->e_type;
1056 l->l_phnum = header->e_phnum;
32c85e43
UD
1057
1058 maplength = header->e_phnum * sizeof (ElfW(Phdr));
6dd67bd5 1059 if (header->e_phoff + maplength <= (size_t) fbp->len)
a35e137a 1060 phdr = (void *) (fbp->buf + header->e_phoff);
32c85e43
UD
1061 else
1062 {
1063 phdr = alloca (maplength);
95c10569
LP
1064 if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength,
1065 header->e_phoff) != maplength)
39b3385d
UD
1066 {
1067 errstring = N_("cannot read file data");
cb5648b0 1068 goto lose_errno;
39b3385d 1069 }
32c85e43 1070 }
879bf2e6 1071
30950a5f
RA
1072 /* On most platforms presume that PT_GNU_STACK is absent and the stack is
1073 * executable. Other platforms default to a nonexecutable stack and don't
1074 * need PT_GNU_STACK to do so. */
535e935a 1075 unsigned int stack_flags = DEFAULT_STACK_PERMS;
ecdeaac0 1076
b122c703
RM
1077 {
1078 /* Scan the program header table, collecting its load commands. */
fcccd512 1079 struct loadcmd loadcmds[l->l_phnum];
b122c703 1080 size_t nloadcmds = 0;
6fffb9a2 1081 bool has_holes = false;
ea32ec35 1082 bool empty_dynamic = false;
e22a4557 1083 ElfW(Addr) p_align_max = 0;
d66e34cd 1084
126b06f9 1085 /* The struct is initialized to zero so this is not necessary:
d66e34cd 1086 l->l_ld = 0;
b122c703 1087 l->l_phdr = 0;
126b06f9 1088 l->l_addr = 0; */
d66e34cd
RM
1089 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
1090 switch (ph->p_type)
1091 {
1092 /* These entries tell us where to find things once the file's
1093 segments are mapped in. We record the addresses it says
1094 verbatim, and later correct for the run-time load address. */
1095 case PT_DYNAMIC:
ea32ec35
FW
1096 if (ph->p_filesz == 0)
1097 empty_dynamic = true; /* Usually separate debuginfo. */
1098 else
592d5c75
L
1099 {
1100 /* Debuginfo only files from "objcopy --only-keep-debug"
1101 contain a PT_DYNAMIC segment with p_filesz == 0. Skip
1102 such a segment to avoid a crash later. */
1103 l->l_ld = (void *) ph->p_vaddr;
1104 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
b413280c 1105 l->l_ld_readonly = (ph->p_flags & PF_W) == 0;
592d5c75 1106 }
d66e34cd 1107 break;
39b3385d 1108
d66e34cd
RM
1109 case PT_PHDR:
1110 l->l_phdr = (void *) ph->p_vaddr;
1111 break;
1112
1113 case PT_LOAD:
b122c703
RM
1114 /* A load command tells us to map in part of the file.
1115 We record the load commands and process them all later. */
1b26b855 1116 if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
163f625c 1117 & (GLRO(dl_pagesize) - 1)) != 0))
39b3385d
UD
1118 {
1119 errstring
163f625c 1120 = N_("ELF load command address/offset not page-aligned");
cb5648b0 1121 goto lose;
39b3385d
UD
1122 }
1123
fcccd512 1124 struct loadcmd *c = &loadcmds[nloadcmds++];
60084e34
CD
1125 c->mapstart = ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize));
1126 c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
cc12f2a4
UD
1127 c->dataend = ph->p_vaddr + ph->p_filesz;
1128 c->allocend = ph->p_vaddr + ph->p_memsz;
e22a4557
L
1129 /* Remember the maximum p_align. */
1130 if (powerof2 (ph->p_align) && ph->p_align > p_align_max)
1131 p_align_max = ph->p_align;
60084e34 1132 c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
cc12f2a4 1133
33ead027
L
1134 DIAG_PUSH_NEEDS_COMMENT;
1135
1136#if __GNUC_PREREQ (11, 0)
1137 /* Suppress invalid GCC warning:
1138 ‘(((char *)loadcmds.113_68 + _933 + 16))[329406144173384849].mapend’ may be used uninitialized [-Wmaybe-uninitialized]
1139 See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106008
1140 */
1141 DIAG_IGNORE_NEEDS_COMMENT (11, "-Wmaybe-uninitialized");
1142#endif
6fffb9a2
UD
1143 /* Determine whether there is a gap between the last segment
1144 and this one. */
1145 if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
1146 has_holes = true;
33ead027 1147 DIAG_POP_NEEDS_COMMENT;
6fffb9a2 1148
cc12f2a4 1149 /* Optimize a common case. */
94a758fe 1150#if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
cc12f2a4
UD
1151 c->prot = (PF_TO_PROT
1152 >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
94a758fe 1153#else
cc12f2a4
UD
1154 c->prot = 0;
1155 if (ph->p_flags & PF_R)
1156 c->prot |= PROT_READ;
1157 if (ph->p_flags & PF_W)
1158 c->prot |= PROT_WRITE;
1159 if (ph->p_flags & PF_X)
1160 c->prot |= PROT_EXEC;
94a758fe 1161#endif
55c91021 1162 break;
96f208a4 1163
96f208a4 1164 case PT_TLS:
2d148689
RM
1165 if (ph->p_memsz == 0)
1166 /* Nothing to do for an empty segment. */
1167 break;
1168
216455bc
RM
1169 l->l_tls_blocksize = ph->p_memsz;
1170 l->l_tls_align = ph->p_align;
99fe3b0e
UD
1171 if (ph->p_align == 0)
1172 l->l_tls_firstbyte_offset = 0;
1173 else
1174 l->l_tls_firstbyte_offset = ph->p_vaddr & (ph->p_align - 1);
216455bc
RM
1175 l->l_tls_initimage_size = ph->p_filesz;
1176 /* Since we don't know the load address yet only store the
1177 offset. We will adjust it later. */
1178 l->l_tls_initimage = (void *) ph->p_vaddr;
1179
77523d5e
FW
1180 /* l->l_tls_modid is assigned below, once there is no
1181 possibility for failure. */
2d148689 1182
77523d5e
FW
1183 if (l->l_type != lt_library
1184 && GL(dl_tls_dtv_slotinfo_list) == NULL)
1185 {
11bf311e 1186#ifdef SHARED
77523d5e
FW
1187 /* We are loading the executable itself when the dynamic
1188 linker was executed directly. The setup will happen
1189 later. */
1190 assert (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0);
7a5e3d9d 1191#else
77523d5e 1192 assert (false && "TLS not initialized in static application");
11bf311e 1193#endif
77523d5e 1194 }
807bce82 1195 break;
ecdeaac0
RM
1196
1197 case PT_GNU_STACK:
1198 stack_flags = ph->p_flags;
1199 break;
ed20b3d9
UD
1200
1201 case PT_GNU_RELRO:
1202 l->l_relro_addr = ph->p_vaddr;
1203 l->l_relro_size = ph->p_memsz;
1204 break;
b122c703 1205 }
d66e34cd 1206
a1ffb40e 1207 if (__glibc_unlikely (nloadcmds == 0))
d8a5edc2
RM
1208 {
1209 /* This only happens for a bogus object that will be caught with
1210 another error below. But we don't want to go through the
1211 calculations below using NLOADCMDS - 1. */
1212 errstring = N_("object file has no loadable segments");
cb5648b0 1213 goto lose;
d8a5edc2
RM
1214 }
1215
e22a4557
L
1216 /* Align all PT_LOAD segments to the maximum p_align. */
1217 for (size_t i = 0; i < nloadcmds; i++)
1218 loadcmds[i].mapalign = p_align_max;
1219
2c75b545
FW
1220 /* dlopen of an executable is not valid because it is not possible
1221 to perform proper relocations, handle static TLS, or run the
1222 ELF constructors. For PIE, the check needs the dynamic
1223 section, so there is another check below. */
fcccd512
RM
1224 if (__glibc_unlikely (type != ET_DYN)
1225 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))
efec5079 1226 {
fcccd512
RM
1227 /* This object is loaded at a fixed address. This must never
1228 happen for objects loaded with dlopen. */
efec5079 1229 errstring = N_("cannot dynamically load executable");
cb5648b0 1230 goto lose;
4cca6b86 1231 }
b122c703 1232
ea32ec35
FW
1233 /* This check recognizes most separate debuginfo files. */
1234 if (__glibc_unlikely ((l->l_ld == 0 && type == ET_DYN) || empty_dynamic))
1235 {
1236 errstring = N_("object file has no dynamic section");
1237 goto lose;
1238 }
1239
fcccd512
RM
1240 /* Length of the sections to be loaded. */
1241 maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
1242
1243 /* Now process the load commands and map segments into memory.
1244 This is responsible for filling in:
53df2ce6 1245 l_map_start, l_map_end, l_addr, l_contiguous, l_phdr
fcccd512
RM
1246 */
1247 errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds,
c01ae97e 1248 maplength, has_holes, loader);
fcccd512 1249 if (__glibc_unlikely (errstring != NULL))
c6b01653
SN
1250 {
1251 /* Mappings can be in an inconsistent state: avoid unmap. */
1252 l->l_map_start = l->l_map_end = 0;
1253 goto lose;
1254 }
b122c703 1255 }
d66e34cd 1256
ea32ec35 1257 if (l->l_ld != 0)
14755b91 1258 l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
879bf2e6 1259
5118dcac 1260 elf_get_dynamic_info (l, false, false);
2f54c82d 1261
efec5079 1262 /* Make sure we are not dlopen'ing an object that has the
2c75b545
FW
1263 DF_1_NOOPEN flag set, or a PIE object. */
1264 if ((__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN)
1265 && (mode & __RTLD_DLOPEN))
1266 || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE)
1267 && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0)))
2f54c82d 1268 {
2c75b545
FW
1269 if (l->l_flags_1 & DF_1_PIE)
1270 errstring
1271 = N_("cannot dynamically load position-independent executable");
1272 else
1273 errstring = N_("shared object cannot be dlopen()ed");
cb5648b0 1274 goto lose;
2f54c82d
UD
1275 }
1276
efec5079
UD
1277 if (l->l_phdr == NULL)
1278 {
1279 /* The program header is not contained in any of the segments.
1280 We have to allocate memory ourself and copy it over from out
1281 temporary place. */
1282 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1283 * sizeof (ElfW(Phdr)));
1284 if (newp == NULL)
1285 {
1286 errstring = N_("cannot allocate memory for program header");
cb5648b0 1287 goto lose_errno;
efec5079
UD
1288 }
1289
1290 l->l_phdr = memcpy (newp, phdr,
1291 (header->e_phnum * sizeof (ElfW(Phdr))));
1292 l->l_phdr_allocated = 1;
1293 }
1294 else
1295 /* Adjust the PT_PHDR value by the runtime load address. */
1296 l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
1297
a1ffb40e 1298 if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X))
dcca3fe2
UD
1299 {
1300 /* The stack is presently not executable, but this module
7edd3814 1301 requires that it be executable. */
2dd87703
FW
1302#if PTHREAD_IN_LIBC
1303 errval = _dl_make_stacks_executable (stack_endp);
1304#else
dcca3fe2 1305 errval = (*GL(dl_make_stack_executable_hook)) (stack_endp);
2dd87703 1306#endif
dcca3fe2
UD
1307 if (errval)
1308 {
1309 errstring = N_("\
1310cannot enable executable stack as shared object requires");
cb5648b0 1311 goto lose;
dcca3fe2
UD
1312 }
1313 }
1314
efec5079
UD
1315 /* Adjust the address of the TLS initialization image. */
1316 if (l->l_tls_initimage != NULL)
1317 l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
efec5079 1318
38a38360
SN
1319 /* Process program headers again after load segments are mapped in
1320 case processing requires accessing those segments. Scan program
1321 headers backward so that PT_NOTE can be skipped if PT_GNU_PROPERTY
1322 exits. */
1323 for (ph = &l->l_phdr[l->l_phnum]; ph != l->l_phdr; --ph)
1324 switch (ph[-1].p_type)
1325 {
1326 case PT_NOTE:
c00452d7 1327 _dl_process_pt_note (l, fd, &ph[-1]);
38a38360
SN
1328 break;
1329 case PT_GNU_PROPERTY:
c00452d7 1330 _dl_process_pt_gnu_property (l, fd, &ph[-1]);
38a38360
SN
1331 break;
1332 }
1333
efec5079 1334 /* We are done mapping in the file. We no longer need the descriptor. */
329ea513 1335 if (__glibc_unlikely (__close_nocancel (fd) != 0))
efec5079
UD
1336 {
1337 errstring = N_("cannot close file descriptor");
cb5648b0 1338 goto lose_errno;
efec5079
UD
1339 }
1340 /* Signal that we closed the file. */
1341 fd = -1;
1342
c6b01653
SN
1343 /* Failures before this point are handled locally via lose.
1344 There are no more failures in this function until return,
1345 to change that the cleanup handling needs to be updated. */
1346
798212a0
PP
1347 /* If this is ET_EXEC, we should have loaded it as lt_executable. */
1348 assert (type != ET_EXEC || l->l_type == lt_executable);
efec5079
UD
1349
1350 l->l_entry += l->l_addr;
1351
a1ffb40e 1352 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
efec5079 1353 _dl_debug_printf ("\
de477abc 1354 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*zx\n\
efec5079
UD
1355 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1356 (int) sizeof (void *) * 2,
1357 (unsigned long int) l->l_ld,
1358 (int) sizeof (void *) * 2,
1359 (unsigned long int) l->l_addr,
1360 (int) sizeof (void *) * 2, maplength,
1361 (int) sizeof (void *) * 2,
1362 (unsigned long int) l->l_entry,
1363 (int) sizeof (void *) * 2,
1364 (unsigned long int) l->l_phdr,
1365 (int) sizeof (void *) * 2, l->l_phnum);
1366
1367 /* Set up the symbol hash table. */
1368 _dl_setup_hash (l);
d66e34cd 1369
be935610
UD
1370 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1371 have to do this for the main map. */
1fc07491 1372 if ((mode & RTLD_DEEPBIND) == 0
1b26b855 1373 && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL)
c0a777e8 1374 && &l->l_searchlist != l->l_scope[0])
be935610
UD
1375 {
1376 /* Create an appropriate searchlist. It contains only this map.
1fc07491 1377 This is the definition of DT_SYMBOLIC in SysVr4. */
be935610
UD
1378 l->l_symbolic_searchlist.r_list[0] = l;
1379 l->l_symbolic_searchlist.r_nlist = 1;
be935610
UD
1380
1381 /* Now move the existing entries one back. */
c0a777e8
UD
1382 memmove (&l->l_scope[1], &l->l_scope[0],
1383 (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
be935610
UD
1384
1385 /* Now add the new entry. */
c0a777e8 1386 l->l_scope[0] = &l->l_symbolic_searchlist;
be935610
UD
1387 }
1388
5d916713 1389 /* Remember whether this object must be initialized first. */
39b3385d 1390 if (l->l_flags_1 & DF_1_INITFIRST)
d6b5d570 1391 GL(dl_initfirst) = l;
5d916713 1392
61e0617a 1393 /* Finally the file information. */
c01ae97e 1394 l->l_file_id = id;
61e0617a 1395
a1b85ae8
FW
1396#ifdef SHARED
1397 /* When auditing is used the recorded names might not include the
1398 name by which the DSO is actually known. Add that as well. */
1399 if (__glibc_unlikely (origname != NULL))
1400 add_name_to_object (l, origname);
a1b85ae8 1401
51f38e87
UD
1402 /* When we profile the SONAME might be needed for something else but
1403 loading. Add it right away. */
1b26b855 1404 if (__glibc_unlikely (GLRO(dl_profile) != NULL)
51f38e87
UD
1405 && l->l_info[DT_SONAME] != NULL)
1406 add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB])
1407 + l->l_info[DT_SONAME]->d_un.d_val));
55f41ef8
AZ
1408#else
1409 /* Audit modules only exist when linking is dynamic so ORIGNAME
1410 cannot be non-NULL. */
1411 assert (origname == NULL);
1412#endif
51f38e87 1413
89baed0b
FW
1414 /* If we have newly loaded libc.so, update the namespace
1415 description. */
1416 if (GL(dl_ns)[nsid].libc_map == NULL
1417 && l->l_info[DT_SONAME] != NULL
1418 && strcmp (((const char *) D_PTR (l, l_info[DT_STRTAB])
1419 + l->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) == 0)
1420 GL(dl_ns)[nsid].libc_map = l;
1421
77523d5e
FW
1422 /* _dl_close can only eventually undo the module ID assignment (via
1423 remove_slotinfo) if this function returns a pointer to a link
1424 map. Therefore, delay this step until all possibilities for
1425 failure have been excluded. */
1426 if (l->l_tls_blocksize > 0
1427 && (__glibc_likely (l->l_type == lt_library)
1428 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1429 not set up TLS data structures, so don't use them now. */
1430 || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL)))
1431 /* Assign the next available module ID. */
ba33937b 1432 _dl_assign_tls_modid (l);
77523d5e 1433
47cc1490
CM
1434#ifdef DL_AFTER_LOAD
1435 DL_AFTER_LOAD (l);
1436#endif
1437
f0967738
AK
1438 /* Now that the object is fully initialized add it to the object list. */
1439 _dl_add_to_namespace_list (l, nsid);
1440
ab5aa2ee
AS
1441 /* Skip auditing and debugger notification when called from 'sprof'. */
1442 if (mode & __RTLD_SPROF)
1443 return l;
1444
ed3ce71f 1445 /* Signal that we are going to add new objects. */
152f8639 1446 struct r_debug *r = _dl_debug_update (nsid);
ed3ce71f
AZ
1447 if (r->r_state == RT_CONSISTENT)
1448 {
1449#ifdef SHARED
1450 /* Auditing checkpoint: we are going to add new objects. Since this
1451 is called after _dl_add_to_namespace_list the namespace is guaranteed
1452 to not be empty. */
3dac3959
AZ
1453 if ((mode & __RTLD_AUDIT) == 0)
1454 _dl_audit_activity_nsid (nsid, LA_ACT_ADD);
ed3ce71f
AZ
1455#endif
1456
1457 /* Notify the debugger we have added some objects. We need to
1458 call _dl_debug_initialize in a static program in case dynamic
1459 linking has not been used before. */
1460 r->r_state = RT_ADD;
1461 _dl_debug_state ();
1462 LIBC_PROBE (map_start, 2, nsid, r);
ed3ce71f
AZ
1463 }
1464 else
1465 assert (r->r_state == RT_ADD);
1466
9dcafc55
UD
1467#ifdef SHARED
1468 /* Auditing checkpoint: we have a new object. */
aee6e90f
AZ
1469 if (!GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing)
1470 _dl_audit_objopen (l, nsid);
9dcafc55
UD
1471#endif
1472
d66e34cd
RM
1473 return l;
1474}
ba79d61b 1475\f
b5efde2f
UD
1476/* Print search path. */
1477static void
1478print_search_path (struct r_search_path_elem **list,
844c394a 1479 const char *what, const char *name)
b5efde2f 1480{
12264bd7 1481 char buf[max_dirnamelen + max_capstrlen];
b5efde2f
UD
1482 int first = 1;
1483
154d10bd 1484 _dl_debug_printf (" search path=");
b5efde2f
UD
1485
1486 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1487 {
12264bd7
UD
1488 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1489 size_t cnt;
1490
1491 for (cnt = 0; cnt < ncapstr; ++cnt)
1492 if ((*list)->status[cnt] != nonexisting)
1493 {
56f8d442 1494#ifdef SHARED
12264bd7 1495 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
143e2b96
UD
1496 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1497 cp[0] = '\0';
1498 else
1499 cp[-1] = '\0';
56f8d442
FW
1500#else
1501 *endp = '\0';
1502#endif
fb0356b9
UD
1503
1504 _dl_debug_printf_c (first ? "%s" : ":%s", buf);
1505 first = 0;
12264bd7 1506 }
b5efde2f 1507
b5efde2f
UD
1508 ++list;
1509 }
1510
1511 if (name != NULL)
35fc382a 1512 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
b9375348 1513 DSO_FILENAME (name));
b5efde2f 1514 else
35fc382a 1515 _dl_debug_printf_c ("\t\t(%s)\n", what);
b5efde2f
UD
1516}
1517\f
a35e137a
UD
1518/* Open a file and verify it is an ELF file for this architecture. We
1519 ignore only ELF files for other architectures. Non-ELF files and
1520 ELF files with different header information cause fatal errors since
1521 this could mean there is something wrong in the installation and the
c0d6f2a3
RM
1522 user might want to know about this.
1523
1524 If FD is not -1, then the file is already open and FD refers to it.
1525 In that case, FD is consumed for both successful and error returns. */
a35e137a 1526static int
c0d6f2a3
RM
1527open_verify (const char *name, int fd,
1528 struct filebuf *fbp, struct link_map *loader,
a42faf59 1529 int whatcode, int mode, bool *found_other_class, bool free_name)
a35e137a
UD
1530{
1531 /* This is the expected ELF header. */
1532#define ELF32_CLASS ELFCLASS32
1533#define ELF64_CLASS ELFCLASS64
1534#ifndef VALID_ELF_HEADER
1535# define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1536# define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
d8c47894 1537# define VALID_ELF_ABIVERSION(osabi,ver) (ver == 0)
40e2fc8b
UD
1538#elif defined MORE_ELF_HEADER_DATA
1539 MORE_ELF_HEADER_DATA;
a35e137a 1540#endif
04f2902d 1541 static const unsigned char expected[EI_NIDENT] =
a35e137a
UD
1542 {
1543 [EI_MAG0] = ELFMAG0,
1544 [EI_MAG1] = ELFMAG1,
1545 [EI_MAG2] = ELFMAG2,
1546 [EI_MAG3] = ELFMAG3,
1547 [EI_CLASS] = ELFW(CLASS),
1548 [EI_DATA] = byteorder,
1549 [EI_VERSION] = EV_CURRENT,
1550 [EI_OSABI] = ELFOSABI_SYSV,
1551 [EI_ABIVERSION] = 0
1552 };
39b3385d
UD
1553 /* Initialize it to make the compiler happy. */
1554 const char *errstring = NULL;
1555 int errval = 0;
a35e137a 1556
9dcafc55
UD
1557#ifdef SHARED
1558 /* Give the auditing libraries a chance. */
c91008d3 1559 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
9dcafc55 1560 {
c0d6f2a3 1561 const char *original_name = name;
c91008d3
AZ
1562 name = _dl_audit_objsearch (name, loader, whatcode);
1563 if (name == NULL)
1564 return -1;
c0d6f2a3
RM
1565
1566 if (fd != -1 && name != original_name && strcmp (name, original_name))
c91008d3
AZ
1567 {
1568 /* An audit library changed what we're supposed to open,
1569 so FD no longer matches it. */
1570 __close_nocancel (fd);
1571 fd = -1;
1572 }
9dcafc55
UD
1573 }
1574#endif
1575
c0d6f2a3
RM
1576 if (fd == -1)
1577 /* Open the file. We always open files read-only. */
329ea513 1578 fd = __open64_nocancel (name, O_RDONLY | O_CLOEXEC);
c0d6f2a3 1579
a35e137a
UD
1580 if (fd != -1)
1581 {
1582 ElfW(Ehdr) *ehdr;
b46d2506 1583 ElfW(Phdr) *phdr;
a986484f 1584 size_t maplength;
a35e137a 1585
9f236c49 1586 /* We successfully opened the file. Now verify it is a file
a35e137a
UD
1587 we can use. */
1588 __set_errno (0);
88481c16
SP
1589 fbp->len = 0;
1590 assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1591 /* Read in the header. */
1592 do
fcccd512 1593 {
329ea513
ZW
1594 ssize_t retlen = __read_nocancel (fd, fbp->buf + fbp->len,
1595 sizeof (fbp->buf) - fbp->len);
88481c16
SP
1596 if (retlen <= 0)
1597 break;
1598 fbp->len += retlen;
1599 }
1b26b855 1600 while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr))));
a35e137a
UD
1601
1602 /* This is where the ELF header is loaded. */
a35e137a
UD
1603 ehdr = (ElfW(Ehdr) *) fbp->buf;
1604
1605 /* Now run the tests. */
a1ffb40e 1606 if (__glibc_unlikely (fbp->len < (ssize_t) sizeof (ElfW(Ehdr))))
39b3385d
UD
1607 {
1608 errval = errno;
1609 errstring = (errval == 0
1610 ? N_("file too short") : N_("cannot read file data"));
cb5648b0 1611 lose:
2e0fc40c
UD
1612 if (free_name)
1613 {
1614 char *realname = (char *) name;
1615 name = strdupa (realname);
1616 free (realname);
1617 }
cb5648b0
SN
1618 __close_nocancel (fd);
1619 _dl_signal_error (errval, name, NULL, errstring);
39b3385d 1620 }
a35e137a
UD
1621
1622 /* See whether the ELF header is what we expect. */
277ae3f1 1623 if (__glibc_unlikely (! VALID_ELF_HEADER (ehdr->e_ident, expected,
92ad15a8 1624 EI_ABIVERSION)
d8c47894 1625 || !VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
04f2902d
UD
1626 ehdr->e_ident[EI_ABIVERSION])
1627 || memcmp (&ehdr->e_ident[EI_PAD],
1628 &expected[EI_PAD],
277ae3f1 1629 EI_NIDENT - EI_PAD) != 0))
a35e137a
UD
1630 {
1631 /* Something is wrong. */
6cc8844f
UD
1632 const Elf32_Word *magp = (const void *) ehdr->e_ident;
1633 if (*magp !=
a35e137a 1634#if BYTE_ORDER == LITTLE_ENDIAN
34a5a146
JM
1635 ((ELFMAG0 << (EI_MAG0 * 8))
1636 | (ELFMAG1 << (EI_MAG1 * 8))
1637 | (ELFMAG2 << (EI_MAG2 * 8))
1638 | (ELFMAG3 << (EI_MAG3 * 8)))
a35e137a 1639#else
34a5a146
JM
1640 ((ELFMAG0 << (EI_MAG3 * 8))
1641 | (ELFMAG1 << (EI_MAG2 * 8))
1642 | (ELFMAG2 << (EI_MAG1 * 8))
1643 | (ELFMAG3 << (EI_MAG0 * 8)))
a35e137a
UD
1644#endif
1645 )
39b3385d 1646 errstring = N_("invalid ELF header");
b46d2506 1647
39b3385d 1648 else if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
7f71c55d
UD
1649 {
1650 /* This is not a fatal error. On architectures where
1651 32-bit and 64-bit binaries can be run this might
1652 happen. */
1653 *found_other_class = true;
b46d2506
AZ
1654 __close_nocancel (fd);
1655 __set_errno (ENOENT);
1656 return -1;
7f71c55d 1657 }
39b3385d 1658 else if (ehdr->e_ident[EI_DATA] != byteorder)
a35e137a
UD
1659 {
1660 if (BYTE_ORDER == BIG_ENDIAN)
39b3385d 1661 errstring = N_("ELF file data encoding not big-endian");
a35e137a 1662 else
39b3385d 1663 errstring = N_("ELF file data encoding not little-endian");
a35e137a 1664 }
39b3385d
UD
1665 else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1666 errstring
1667 = N_("ELF file version ident does not match current one");
a35e137a
UD
1668 /* XXX We should be able so set system specific versions which are
1669 allowed here. */
39b3385d
UD
1670 else if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1671 errstring = N_("ELF file OS ABI invalid");
d8c47894
UD
1672 else if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_OSABI],
1673 ehdr->e_ident[EI_ABIVERSION]))
39b3385d 1674 errstring = N_("ELF file ABI version invalid");
04f2902d
UD
1675 else if (memcmp (&ehdr->e_ident[EI_PAD], &expected[EI_PAD],
1676 EI_NIDENT - EI_PAD) != 0)
1677 errstring = N_("nonzero padding in e_ident");
39b3385d
UD
1678 else
1679 /* Otherwise we don't know what went wrong. */
1680 errstring = N_("internal error");
1681
cb5648b0 1682 goto lose;
a35e137a
UD
1683 }
1684
1b26b855 1685 if (__glibc_unlikely (ehdr->e_version != EV_CURRENT))
39b3385d
UD
1686 {
1687 errstring = N_("ELF file version does not match current one");
cb5648b0 1688 goto lose;
39b3385d 1689 }
277ae3f1 1690 if (! __glibc_likely (elf_machine_matches_host (ehdr)))
b46d2506
AZ
1691 {
1692 __close_nocancel (fd);
1693 __set_errno (ENOENT);
1694 return -1;
1695 }
277ae3f1
PP
1696 else if (__glibc_unlikely (ehdr->e_type != ET_DYN
1697 && ehdr->e_type != ET_EXEC))
39b3385d
UD
1698 {
1699 errstring = N_("only ET_DYN and ET_EXEC can be loaded");
cb5648b0 1700 goto lose;
39b3385d 1701 }
1b26b855 1702 else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr))))
5ce98c3f
UD
1703 {
1704 errstring = N_("ELF file's phentsize not the expected size");
cb5648b0 1705 goto lose;
5ce98c3f 1706 }
a986484f
UD
1707
1708 maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
6dd67bd5 1709 if (ehdr->e_phoff + maplength <= (size_t) fbp->len)
a986484f
UD
1710 phdr = (void *) (fbp->buf + ehdr->e_phoff);
1711 else
1712 {
1713 phdr = alloca (maplength);
95c10569
LP
1714 if ((size_t) __pread64_nocancel (fd, (void *) phdr, maplength,
1715 ehdr->e_phoff) != maplength)
39b3385d 1716 {
39b3385d
UD
1717 errval = errno;
1718 errstring = N_("cannot read file data");
cb5648b0 1719 goto lose;
39b3385d 1720 }
a986484f
UD
1721 }
1722
d6f373d2
MF
1723 if (__glibc_unlikely (elf_machine_reject_phdr_p
1724 (phdr, ehdr->e_phnum, fbp->buf, fbp->len,
1725 loader, fd)))
b46d2506
AZ
1726 {
1727 __close_nocancel (fd);
1728 __set_errno (ENOENT);
1729 return -1;
1730 }
a986484f 1731
a35e137a
UD
1732 }
1733
1734 return fd;
1735}
1736\f
44d4d3bd
SP
1737/* Try to open NAME in one of the directories in SPS. Return the fd, or -1.
1738 If successful, fill in *REALNAME with the malloc'd full directory name. If
1739 it turns out that none of the directories in SPS exists, SPS->DIRS is
1740 replaced with (void *) -1, and the old value is free()d if SPS->MALLOCED is
1741 true. */
ba79d61b
RM
1742
1743static int
a42faf59 1744open_path (const char *name, size_t namelen, int mode,
a35e137a 1745 struct r_search_path_struct *sps, char **realname,
7f71c55d
UD
1746 struct filebuf *fbp, struct link_map *loader, int whatcode,
1747 bool *found_other_class)
ba79d61b 1748{
f55727ca 1749 struct r_search_path_elem **dirs = sps->dirs;
ba79d61b 1750 char *buf;
0a54e401 1751 int fd = -1;
b5efde2f 1752 const char *current_what = NULL;
152e7964 1753 int any = 0;
ba79d61b 1754
a1ffb40e 1755 if (__glibc_unlikely (dirs == NULL))
ab1d521d
RM
1756 /* We're called before _dl_init_paths when loading the main executable
1757 given on the command line when rtld is run directly. */
1758 return -1;
1759
5431ece5 1760 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
ba79d61b
RM
1761 do
1762 {
0a54e401
UD
1763 struct r_search_path_elem *this_dir = *dirs;
1764 size_t buflen = 0;
12264bd7 1765 size_t cnt;
b0b67c47 1766 char *edp;
f5858039 1767 int here_any = 0;
ba79d61b 1768
b5efde2f
UD
1769 /* If we are debugging the search for libraries print the path
1770 now if it hasn't happened now. */
1b26b855 1771 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)
cf197e41 1772 && current_what != this_dir->what)
b5efde2f
UD
1773 {
1774 current_what = this_dir->what;
1775 print_search_path (dirs, current_what, this_dir->where);
1776 }
1777
b0b67c47 1778 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
12264bd7 1779 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
fd26970f 1780 {
12264bd7
UD
1781 /* Skip this directory if we know it does not exist. */
1782 if (this_dir->status[cnt] == nonexisting)
1783 continue;
0a54e401 1784
56f8d442 1785#ifdef SHARED
12264bd7 1786 buflen =
d6b5d570
UD
1787 ((char *) __mempcpy (__mempcpy (edp, capstr[cnt].str,
1788 capstr[cnt].len),
12264bd7
UD
1789 name, namelen)
1790 - buf);
56f8d442
FW
1791#else
1792 buflen = (char *) __mempcpy (edp, name, namelen) - buf;
1793#endif
12264bd7
UD
1794
1795 /* Print name we try if this is wanted. */
a1ffb40e 1796 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
154d10bd 1797 _dl_debug_printf (" trying file=%s\n", buf);
b5efde2f 1798
c0d6f2a3 1799 fd = open_verify (buf, -1, fbp, loader, whatcode, mode,
a42faf59 1800 found_other_class, false);
12264bd7 1801 if (this_dir->status[cnt] == unknown)
6e4c40ba
UD
1802 {
1803 if (fd != -1)
1804 this_dir->status[cnt] = existing;
9dcafc55
UD
1805 /* Do not update the directory information when loading
1806 auditing code. We must try to disturb the program as
1807 little as possible. */
1808 else if (loader == NULL
361a3706 1809 || GL(dl_ns)[loader->l_ns]._ns_loaded->l_auditing == 0)
6e4c40ba
UD
1810 {
1811 /* We failed to open machine dependent library. Let's
1812 test whether there is any directory at all. */
52a5fe70 1813 struct __stat64_t64 st;
6e4c40ba 1814
e0189b25 1815 buf[buflen - namelen] = '\0';
6e4c40ba 1816
52a5fe70 1817 if (__stat64_time64 (buf, &st) != 0
6e4c40ba
UD
1818 || ! S_ISDIR (st.st_mode))
1819 /* The directory does not exist or it is no directory. */
1820 this_dir->status[cnt] = nonexisting;
1821 else
1822 this_dir->status[cnt] = existing;
1823 }
1824 }
fd26970f 1825
152e7964 1826 /* Remember whether we found any existing directory. */
9dcafc55 1827 here_any |= this_dir->status[cnt] != nonexisting;
152e7964 1828
1b26b855 1829 if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE)
6bc6bd3b 1830 && __libc_enable_secure)
c6222ab9
UD
1831 {
1832 /* This is an extra security effort to make sure nobody can
1833 preload broken shared objects which are in the trusted
1834 directories and so exploit the bugs. */
52a5fe70 1835 struct __stat64_t64 st;
c6222ab9 1836
52a5fe70 1837 if (__fstat64_time64 (fd, &st) != 0
c6222ab9
UD
1838 || (st.st_mode & S_ISUID) == 0)
1839 {
1840 /* The shared object cannot be tested for being SUID
1841 or this bit is not set. In this case we must not
1842 use this object. */
329ea513 1843 __close_nocancel (fd);
c6222ab9
UD
1844 fd = -1;
1845 /* We simply ignore the file, signal this by setting
1846 the error value which would have been set by `open'. */
1847 errno = ENOENT;
1848 }
1849 }
ba79d61b
RM
1850 }
1851
ba79d61b
RM
1852 if (fd != -1)
1853 {
2f653c01 1854 *realname = (char *) malloc (buflen);
c6222ab9 1855 if (*realname != NULL)
ba79d61b
RM
1856 {
1857 memcpy (*realname, buf, buflen);
1858 return fd;
1859 }
1860 else
1861 {
1862 /* No memory for the name, we certainly won't be able
1863 to load and link it. */
329ea513 1864 __close_nocancel (fd);
ba79d61b
RM
1865 return -1;
1866 }
1867 }
a8dcffb3
AZ
1868
1869 /* Continue the search if the file does not exist (ENOENT), if it can
1870 not be accessed (EACCES), or if the a component in the path is not a
1871 directory (for instance, if the component is a existing file meaning
1872 essentially that the pathname is invalid - ENOTDIR). */
1873 if (here_any && errno != ENOENT && errno != EACCES && errno != ENOTDIR)
ba79d61b 1874 return -1;
f5858039
UD
1875
1876 /* Remember whether we found anything. */
1877 any |= here_any;
ba79d61b 1878 }
0a54e401 1879 while (*++dirs != NULL);
ba79d61b 1880
152e7964 1881 /* Remove the whole path if none of the directories exists. */
a1ffb40e 1882 if (__glibc_unlikely (! any))
152e7964 1883 {
04ea3b0f
UD
1884 /* Paths which were allocated using the minimal malloc() in ld.so
1885 must not be freed using the general free() in libc. */
f55727ca
UD
1886 if (sps->malloced)
1887 free (sps->dirs);
11bf311e 1888
50b1b7a3
FW
1889 /* __rtld_search_dirs and __rtld_env_path_list are
1890 attribute_relro, therefore avoid writing to them. */
1891 if (sps != &__rtld_search_dirs && sps != &__rtld_env_path_list)
ecc1d0c3 1892 sps->dirs = (void *) -1;
152e7964
UD
1893 }
1894
ba79d61b
RM
1895 return -1;
1896}
1897
1898/* Map in the shared object file NAME. */
1899
1900struct link_map *
8e9f92e9 1901_dl_map_object (struct link_map *loader, const char *name,
c0f62c56 1902 int type, int trace_mode, int mode, Lmid_t nsid)
ba79d61b
RM
1903{
1904 int fd;
a1b85ae8 1905 const char *origname = NULL;
ba79d61b 1906 char *realname;
14bab8de 1907 char *name_copy;
ba79d61b 1908 struct link_map *l;
a35e137a 1909 struct filebuf fb;
ba79d61b 1910
c0f62c56 1911 assert (nsid >= 0);
22c83193 1912 assert (nsid < GL(dl_nns));
c0f62c56 1913
ba79d61b 1914 /* Look for this name among those already loaded. */
c0f62c56 1915 for (l = GL(dl_ns)[nsid]._ns_loaded; l; l = l->l_next)
f41c8091
UD
1916 {
1917 /* If the requested name matches the soname of a loaded object,
1918 use that object. Elide this check for names that have not
1919 yet been opened. */
277ae3f1 1920 if (__glibc_unlikely ((l->l_faked | l->l_removed) != 0))
f41c8091
UD
1921 continue;
1922 if (!_dl_name_match_p (name, l))
1923 {
1924 const char *soname;
1925
1b26b855 1926 if (__glibc_likely (l->l_soname_added)
c91bc73e 1927 || l->l_info[DT_SONAME] == NULL)
f41c8091
UD
1928 continue;
1929
8699e7b1
UD
1930 soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1931 + l->l_info[DT_SONAME]->d_un.d_val);
f41c8091
UD
1932 if (strcmp (name, soname) != 0)
1933 continue;
1934
1935 /* We have a match on a new name -- cache it. */
76156ea1 1936 add_name_to_object (l, soname);
c91bc73e 1937 l->l_soname_added = 1;
f41c8091
UD
1938 }
1939
42c4f32a 1940 /* We have a match. */
f41c8091
UD
1941 return l;
1942 }
ba79d61b 1943
8193034b 1944 /* Display information if we are debugging. */
1b26b855 1945 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
7969407a 1946 && loader != NULL)
07df30d9 1947 _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0
9ac533d3
UD
1948 ? "\nfile=%s [%lu]; needed by %s [%lu]\n"
1949 : "\nfile=%s [%lu]; dynamically loaded by %s [%lu]\n",
b9375348 1950 name, nsid, DSO_FILENAME (loader->l_name), loader->l_ns);
8193034b 1951
9dcafc55
UD
1952#ifdef SHARED
1953 /* Give the auditing libraries a chance to change the name before we
1954 try anything. */
c91008d3 1955 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
9dcafc55 1956 {
c91008d3
AZ
1957 const char *before = name;
1958 name = _dl_audit_objsearch (name, loader, LA_SER_ORIG);
1959 if (name == NULL)
9dcafc55 1960 {
c91008d3
AZ
1961 fd = -1;
1962 goto no_file;
9dcafc55 1963 }
c91008d3
AZ
1964 if (before != name && strcmp (before, name) != 0)
1965 origname = before;
9dcafc55
UD
1966 }
1967#endif
1968
7f71c55d
UD
1969 /* Will be true if we found a DSO which is of the other ELF class. */
1970 bool found_other_class = false;
1971
ba79d61b
RM
1972 if (strchr (name, '/') == NULL)
1973 {
1974 /* Search for NAME in several places. */
1975
1976 size_t namelen = strlen (name) + 1;
1977
a1ffb40e 1978 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
c0f62c56 1979 _dl_debug_printf ("find library=%s [%lu]; searching\n", name, nsid);
b5efde2f 1980
ba79d61b 1981 fd = -1;
a23db8e4 1982
fcf70d41 1983 /* When the object has the RUNPATH information we don't use any
844c394a 1984 RPATHs. */
4c540916 1985 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
fcf70d41 1986 {
a1f0de82
UD
1987 /* This is the executable's map (if there is one). Make sure that
1988 we do not look at it twice. */
1989 struct link_map *main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1990 bool did_main_map = false;
1991
fcf70d41
UD
1992 /* First try the DT_RPATH of the dependent object that caused NAME
1993 to be loaded. Then that object's dependent, and on up. */
a1f0de82 1994 for (l = loader; l; l = l->l_loader)
45e4762c 1995 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
a1f0de82 1996 {
a42faf59 1997 fd = open_path (name, namelen, mode,
8e9f92e9 1998 &l->l_rpath_dirs,
a1f0de82
UD
1999 &realname, &fb, loader, LA_SER_RUNPATH,
2000 &found_other_class);
2001 if (fd != -1)
2002 break;
2003
2004 did_main_map |= l == main_map;
2005 }
0a54e401 2006
fcf70d41 2007 /* If dynamically linked, try the DT_RPATH of the executable
844c394a 2008 itself. NB: we do this for lookups in any namespace. */
a1f0de82
UD
2009 if (fd == -1 && !did_main_map
2010 && main_map != NULL && main_map->l_type != lt_loaded
2011 && cache_rpath (main_map, &main_map->l_rpath_dirs, DT_RPATH,
2012 "RPATH"))
a42faf59 2013 fd = open_path (name, namelen, mode,
8e9f92e9 2014 &main_map->l_rpath_dirs,
a1f0de82
UD
2015 &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
2016 &found_other_class);
bfb5ed5d
L
2017
2018 /* Also try DT_RUNPATH in the executable for LD_AUDIT dlopen
2019 call. */
2020 if (__glibc_unlikely (mode & __RTLD_AUDIT)
2021 && fd == -1 && !did_main_map
2022 && main_map != NULL && main_map->l_type != lt_loaded)
2023 {
2024 struct r_search_path_struct l_rpath_dirs;
2025 l_rpath_dirs.dirs = NULL;
2026 if (cache_rpath (main_map, &l_rpath_dirs,
2027 DT_RUNPATH, "RUNPATH"))
2028 fd = open_path (name, namelen, mode, &l_rpath_dirs,
2029 &realname, &fb, loader ?: main_map,
2030 LA_SER_RUNPATH, &found_other_class);
2031 }
fcf70d41 2032 }
fd26970f 2033
7ef90c15 2034 /* Try the LD_LIBRARY_PATH environment variable. */
50b1b7a3
FW
2035 if (fd == -1 && __rtld_env_path_list.dirs != (void *) -1)
2036 fd = open_path (name, namelen, mode, &__rtld_env_path_list,
9dcafc55
UD
2037 &realname, &fb,
2038 loader ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded,
7f71c55d 2039 LA_SER_LIBPATH, &found_other_class);
40a55d20 2040
45e4762c
RM
2041 /* Look at the RUNPATH information for this binary. */
2042 if (fd == -1 && loader != NULL
2043 && cache_rpath (loader, &loader->l_runpath_dirs,
2044 DT_RUNPATH, "RUNPATH"))
a42faf59 2045 fd = open_path (name, namelen, mode,
9dcafc55 2046 &loader->l_runpath_dirs, &realname, &fb, loader,
7f71c55d 2047 LA_SER_RUNPATH, &found_other_class);
fcf70d41 2048
f57f8055 2049#ifdef USE_LDCONFIG
55c91021 2050 if (fd == -1
277ae3f1 2051 && (__glibc_likely ((mode & __RTLD_SECURE) == 0)
6bc6bd3b 2052 || ! __libc_enable_secure)
1b26b855 2053 && __glibc_likely (GLRO(dl_inhibit_cache) == 0))
f18edac3
RM
2054 {
2055 /* Check the list of libraries in the file /etc/ld.so.cache,
2056 for compatibility with Linux's ldconfig program. */
ccdb048d 2057 char *cached = _dl_load_cache_lookup (name);
0f6b172f 2058
2f653c01
UD
2059 if (cached != NULL)
2060 {
c0f62c56 2061 // XXX Correct to unconditionally default to namespace 0?
6a5ee102
UD
2062 l = (loader
2063 ?: GL(dl_ns)[LM_ID_BASE]._ns_loaded
0d23a5c1
MR
2064# ifdef SHARED
2065 ?: &GL(dl_rtld_map)
f57f8055 2066# endif
0d23a5c1 2067 );
0f6b172f 2068
266bb989
UD
2069 /* If the loader has the DF_1_NODEFLIB flag set we must not
2070 use a cache entry from any of these directories. */
1b26b855 2071 if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB))
266bb989
UD
2072 {
2073 const char *dirp = system_dirs;
2e47aff5 2074 unsigned int cnt = 0;
266bb989
UD
2075
2076 do
2077 {
2078 if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
2079 {
2080 /* The prefix matches. Don't use the entry. */
ccdb048d 2081 free (cached);
266bb989
UD
2082 cached = NULL;
2083 break;
2084 }
2085
2086 dirp += system_dirs_len[cnt] + 1;
2087 ++cnt;
2088 }
4a6d1198 2089 while (cnt < nsystem_dirs_len);
266bb989
UD
2090 }
2091
2f653c01 2092 if (cached != NULL)
f18edac3 2093 {
c0d6f2a3 2094 fd = open_verify (cached, -1,
9dcafc55 2095 &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded,
a42faf59
PP
2096 LA_SER_CONFIG, mode, &found_other_class,
2097 false);
a1ffb40e 2098 if (__glibc_likely (fd != -1))
ccdb048d
CD
2099 realname = cached;
2100 else
2101 free (cached);
f18edac3
RM
2102 }
2103 }
2104 }
f57f8055 2105#endif
0a54e401 2106
a23db8e4 2107 /* Finally, try the default path. */
266bb989 2108 if (fd == -1
c0f62c56 2109 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL
1b26b855 2110 || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB)))
50b1b7a3
FW
2111 && __rtld_search_dirs.dirs != (void *) -1)
2112 fd = open_path (name, namelen, mode, &__rtld_search_dirs,
7f71c55d 2113 &realname, &fb, l, LA_SER_DEFAULT, &found_other_class);
b5efde2f 2114
fd77c361 2115 /* Add another newline when we are tracing the library loading. */
a1ffb40e 2116 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
844c394a 2117 _dl_debug_printf ("\n");
ba79d61b
RM
2118 }
2119 else
2120 {
f787edde
UD
2121 /* The path may contain dynamic string tokens. */
2122 realname = (loader
2bd86632 2123 ? expand_dynamic_string_token (loader, name)
cc00cece 2124 : __strdup (name));
f787edde
UD
2125 if (realname == NULL)
2126 fd = -1;
2127 else
ba79d61b 2128 {
c0d6f2a3 2129 fd = open_verify (realname, -1, &fb,
a42faf59 2130 loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode,
2e0fc40c 2131 &found_other_class, true);
1b26b855 2132 if (__glibc_unlikely (fd == -1))
f787edde 2133 free (realname);
ba79d61b
RM
2134 }
2135 }
2136
9dcafc55
UD
2137#ifdef SHARED
2138 no_file:
2139#endif
c14e9135
UD
2140 /* In case the LOADER information has only been provided to get to
2141 the appropriate RUNPATH/RPATH information we do not need it
2142 anymore. */
2143 if (mode & __RTLD_CALLMAP)
2144 loader = NULL;
2145
1b26b855 2146 if (__glibc_unlikely (fd == -1))
46ec036d 2147 {
6628c742 2148 if (trace_mode)
46ec036d
UD
2149 {
2150 /* We haven't found an appropriate library. But since we
2151 are only interested in the list of libraries this isn't
2152 so severe. Fake an entry with all the information we
1ef32c3d 2153 have. */
a1eca9f3 2154 static const Elf_Symndx dummy_bucket = STN_UNDEF;
46ec036d 2155
f0967738 2156 /* Allocate a new object map. */
cc00cece 2157 if ((name_copy = __strdup (name)) == NULL
1fc07491 2158 || (l = _dl_new_object (name_copy, name, type, loader,
c0f62c56 2159 mode, nsid)) == NULL)
2e0fc40c
UD
2160 {
2161 free (name_copy);
2162 _dl_signal_error (ENOMEM, name, NULL,
2163 N_("cannot create shared object descriptor"));
2164 }
a881e0a0
UD
2165 /* Signal that this is a faked entry. */
2166 l->l_faked = 1;
2167 /* Since the descriptor is initialized with zero we do not
126b06f9 2168 have do this here.
126b06f9 2169 l->l_reserved = 0; */
fd26970f
UD
2170 l->l_buckets = &dummy_bucket;
2171 l->l_nbuckets = 1;
2172 l->l_relocated = 1;
2173
f0967738
AK
2174 /* Enter the object in the object list. */
2175 _dl_add_to_namespace_list (l, nsid);
2176
fd26970f 2177 return l;
46ec036d 2178 }
7f71c55d
UD
2179 else if (found_other_class)
2180 _dl_signal_error (0, name, NULL,
2181 ELFW(CLASS) == ELFCLASS32
2182 ? N_("wrong ELF class: ELFCLASS64")
2183 : N_("wrong ELF class: ELFCLASS32"));
46ec036d 2184 else
154d10bd
UD
2185 _dl_signal_error (errno, name, NULL,
2186 N_("cannot open shared object file"));
46ec036d 2187 }
ba79d61b 2188
d1fc817e 2189 void *stack_end = __libc_stack_end;
a1b85ae8
FW
2190 return _dl_map_object_from_fd (name, origname, fd, &fb, realname, loader,
2191 type, mode, &stack_end, nsid);
ba79d61b 2192}
154d10bd 2193
b8c80a7e
KS
2194struct add_path_state
2195{
2196 bool counting;
2197 unsigned int idx;
2198 Dl_serinfo *si;
2199 char *allocptr;
2200};
2201
2202static void
2203add_path (struct add_path_state *p, const struct r_search_path_struct *sps,
2204 unsigned int flags)
2205{
2206 if (sps->dirs != (void *) -1)
2207 {
2208 struct r_search_path_elem **dirs = sps->dirs;
2209 do
2210 {
2211 const struct r_search_path_elem *const r = *dirs++;
2212 if (p->counting)
2213 {
2214 p->si->dls_cnt++;
2215 p->si->dls_size += MAX (2, r->dirnamelen);
2216 }
2217 else
2218 {
2219 Dl_serpath *const sp = &p->si->dls_serpath[p->idx++];
2220 sp->dls_name = p->allocptr;
2221 if (r->dirnamelen < 2)
2222 *p->allocptr++ = r->dirnamelen ? '/' : '.';
2223 else
2224 p->allocptr = __mempcpy (p->allocptr,
2225 r->dirname, r->dirnamelen - 1);
2226 *p->allocptr++ = '\0';
2227 sp->dls_flags = flags;
2228 }
2229 }
2230 while (*dirs != NULL);
2231 }
2232}
45e4762c
RM
2233
2234void
45e4762c
RM
2235_dl_rtld_di_serinfo (struct link_map *loader, Dl_serinfo *si, bool counting)
2236{
2237 if (counting)
2238 {
2239 si->dls_cnt = 0;
2240 si->dls_size = 0;
2241 }
2242
b8c80a7e 2243 struct add_path_state p =
45e4762c 2244 {
b8c80a7e
KS
2245 .counting = counting,
2246 .idx = 0,
2247 .si = si,
2248 .allocptr = (char *) &si->dls_serpath[si->dls_cnt]
2249 };
2250
2251# define add_path(p, sps, flags) add_path(p, sps, 0) /* XXX */
45e4762c
RM
2252
2253 /* When the object has the RUNPATH information we don't use any RPATHs. */
2254 if (loader->l_info[DT_RUNPATH] == NULL)
2255 {
2256 /* First try the DT_RPATH of the dependent object that caused NAME
2257 to be loaded. Then that object's dependent, and on up. */
2258
2259 struct link_map *l = loader;
2260 do
2261 {
2262 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
b8c80a7e 2263 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
45e4762c
RM
2264 l = l->l_loader;
2265 }
2266 while (l != NULL);
2267
2268 /* If dynamically linked, try the DT_RPATH of the executable itself. */
c0f62c56
UD
2269 if (loader->l_ns == LM_ID_BASE)
2270 {
2271 l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2272 if (l != NULL && l->l_type != lt_loaded && l != loader)
2273 if (cache_rpath (l, &l->l_rpath_dirs, DT_RPATH, "RPATH"))
b8c80a7e 2274 add_path (&p, &l->l_rpath_dirs, XXX_RPATH);
c0f62c56 2275 }
45e4762c
RM
2276 }
2277
2278 /* Try the LD_LIBRARY_PATH environment variable. */
50b1b7a3 2279 add_path (&p, &__rtld_env_path_list, XXX_ENV);
45e4762c
RM
2280
2281 /* Look at the RUNPATH information for this binary. */
2282 if (cache_rpath (loader, &loader->l_runpath_dirs, DT_RUNPATH, "RUNPATH"))
b8c80a7e 2283 add_path (&p, &loader->l_runpath_dirs, XXX_RUNPATH);
45e4762c
RM
2284
2285 /* XXX
2286 Here is where ld.so.cache gets checked, but we don't have
2287 a way to indicate that in the results for Dl_serinfo. */
2288
2289 /* Finally, try the default path. */
2290 if (!(loader->l_flags_1 & DF_1_NODEFLIB))
50b1b7a3 2291 add_path (&p, &__rtld_search_dirs, XXX_default);
45e4762c
RM
2292
2293 if (counting)
2294 /* Count the struct size before the string area, which we didn't
2295 know before we completed dls_cnt. */
2296 si->dls_size += (char *) &si->dls_serpath[si->dls_cnt] - (char *) si;
2297}
This page took 1.028945 seconds and 6 git commands to generate.