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