Mention GCC 10 attribute access.
[glibc.git] / elf / rtld.c
blob882b070cc01f227a5e44e82223b6b6af13afdf0b
1 /* Run time dynamic linker.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <dlfcn.h>
21 #include <fcntl.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <ldsodefs.h>
30 #include <_itoa.h>
31 #include <entry.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <libc-lock.h>
35 #include "dynamic-link.h"
36 #include <dl-librecon.h>
37 #include <unsecvars.h>
38 #include <dl-cache.h>
39 #include <dl-osinfo.h>
40 #include <dl-procinfo.h>
41 #include <dl-prop.h>
42 #include <dl-vdso.h>
43 #include <dl-vdso-setup.h>
44 #include <tls.h>
45 #include <stap-probe.h>
46 #include <stackinfo.h>
47 #include <not-cancel.h>
48 #include <array_length.h>
49 #include <libc-early-init.h>
51 #include <assert.h>
53 /* Only enables rtld profiling for architectures which provides non generic
54 hp-timing support. The generic support requires either syscall
55 (clock_gettime), which will incur in extra overhead on loading time.
56 Using vDSO is also an option, but it will require extra support on loader
57 to setup the vDSO pointer before its usage. */
58 #if HP_TIMING_INLINE
59 # define RLTD_TIMING_DECLARE(var, classifier,...) \
60 classifier hp_timing_t var __VA_ARGS__
61 # define RTLD_TIMING_VAR(var) RLTD_TIMING_DECLARE (var, )
62 # define RTLD_TIMING_SET(var, value) (var) = (value)
63 # define RTLD_TIMING_REF(var) &(var)
65 static inline void
66 rtld_timer_start (hp_timing_t *var)
68 HP_TIMING_NOW (*var);
71 static inline void
72 rtld_timer_stop (hp_timing_t *var, hp_timing_t start)
74 hp_timing_t stop;
75 HP_TIMING_NOW (stop);
76 HP_TIMING_DIFF (*var, start, stop);
79 static inline void
80 rtld_timer_accum (hp_timing_t *sum, hp_timing_t start)
82 hp_timing_t stop;
83 rtld_timer_stop (&stop, start);
84 HP_TIMING_ACCUM_NT(*sum, stop);
86 #else
87 # define RLTD_TIMING_DECLARE(var, classifier...)
88 # define RTLD_TIMING_SET(var, value)
89 # define RTLD_TIMING_VAR(var)
90 # define RTLD_TIMING_REF(var) 0
91 # define rtld_timer_start(var)
92 # define rtld_timer_stop(var, start)
93 # define rtld_timer_accum(sum, start)
94 #endif
96 /* Avoid PLT use for our local calls at startup. */
97 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
99 /* GCC has mental blocks about _exit. */
100 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
101 #define _exit exit_internal
103 /* Helper function to handle errors while resolving symbols. */
104 static void print_unresolved (int errcode, const char *objname,
105 const char *errsting);
107 /* Helper function to handle errors when a version is missing. */
108 static void print_missing_version (int errcode, const char *objname,
109 const char *errsting);
111 /* Print the various times we collected. */
112 static void print_statistics (const hp_timing_t *total_timep);
114 /* Length limits for names and paths, to protect the dynamic linker,
115 particularly when __libc_enable_secure is active. */
116 #ifdef NAME_MAX
117 # define SECURE_NAME_LIMIT NAME_MAX
118 #else
119 # define SECURE_NAME_LIMIT 255
120 #endif
121 #ifdef PATH_MAX
122 # define SECURE_PATH_LIMIT PATH_MAX
123 #else
124 # define SECURE_PATH_LIMIT 1024
125 #endif
127 /* Strings containing colon-separated lists of audit modules. */
128 struct audit_list
130 /* Array of strings containing colon-separated path lists. Each
131 audit module needs its own namespace, so pre-allocate the largest
132 possible list. */
133 const char *audit_strings[DL_NNS];
135 /* Number of entries added to audit_strings. */
136 size_t length;
138 /* Index into the audit_strings array (for the iteration phase). */
139 size_t current_index;
141 /* Tail of audit_strings[current_index] which still needs
142 processing. */
143 const char *current_tail;
145 /* Scratch buffer for returning a name which is part of the strings
146 in audit_strings. */
147 char fname[SECURE_NAME_LIMIT];
150 /* Creates an empty audit list. */
151 static void audit_list_init (struct audit_list *);
153 /* Add a string to the end of the audit list, for later parsing. Must
154 not be called after audit_list_next. */
155 static void audit_list_add_string (struct audit_list *, const char *);
157 /* Add the audit strings from the link map, found in the dynamic
158 segment at TG (either DT_AUDIT and DT_DEPAUDIT). Must be called
159 before audit_list_next. */
160 static void audit_list_add_dynamic_tag (struct audit_list *,
161 struct link_map *,
162 unsigned int tag);
164 /* Extract the next audit module from the audit list. Only modules
165 for which dso_name_valid_for_suid is true are returned. Must be
166 called after all the audit_list_add_string,
167 audit_list_add_dynamic_tags calls. */
168 static const char *audit_list_next (struct audit_list *);
170 /* This is a list of all the modes the dynamic loader can be in. */
171 enum mode { normal, list, verify, trace };
173 /* Process all environments variables the dynamic linker must recognize.
174 Since all of them start with `LD_' we are a bit smarter while finding
175 all the entries. */
176 static void process_envvars (enum mode *modep, struct audit_list *);
178 #ifdef DL_ARGV_NOT_RELRO
179 int _dl_argc attribute_hidden;
180 char **_dl_argv = NULL;
181 /* Nonzero if we were run directly. */
182 unsigned int _dl_skip_args attribute_hidden;
183 #else
184 int _dl_argc attribute_relro attribute_hidden;
185 char **_dl_argv attribute_relro = NULL;
186 unsigned int _dl_skip_args attribute_relro attribute_hidden;
187 #endif
188 rtld_hidden_data_def (_dl_argv)
190 #ifndef THREAD_SET_STACK_GUARD
191 /* Only exported for architectures that don't store the stack guard canary
192 in thread local area. */
193 uintptr_t __stack_chk_guard attribute_relro;
194 #endif
196 /* Only exported for architectures that don't store the pointer guard
197 value in thread local area. */
198 uintptr_t __pointer_chk_guard_local
199 attribute_relro attribute_hidden __attribute__ ((nocommon));
200 #ifndef THREAD_SET_POINTER_GUARD
201 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
202 #endif
204 /* Check that AT_SECURE=0, or that the passed name does not contain
205 directories and is not overly long. Reject empty names
206 unconditionally. */
207 static bool
208 dso_name_valid_for_suid (const char *p)
210 if (__glibc_unlikely (__libc_enable_secure))
212 /* Ignore pathnames with directories for AT_SECURE=1
213 programs, and also skip overlong names. */
214 size_t len = strlen (p);
215 if (len >= SECURE_NAME_LIMIT || memchr (p, '/', len) != NULL)
216 return false;
218 return *p != '\0';
221 static void
222 audit_list_init (struct audit_list *list)
224 list->length = 0;
225 list->current_index = 0;
226 list->current_tail = NULL;
229 static void
230 audit_list_add_string (struct audit_list *list, const char *string)
232 /* Empty strings do not load anything. */
233 if (*string == '\0')
234 return;
236 if (list->length == array_length (list->audit_strings))
237 _dl_fatal_printf ("Fatal glibc error: Too many audit modules requested\n");
239 list->audit_strings[list->length++] = string;
241 /* Initialize processing of the first string for
242 audit_list_next. */
243 if (list->length == 1)
244 list->current_tail = string;
247 static void
248 audit_list_add_dynamic_tag (struct audit_list *list, struct link_map *main_map,
249 unsigned int tag)
251 ElfW(Dyn) *info = main_map->l_info[ADDRIDX (tag)];
252 const char *strtab = (const char *) D_PTR (main_map, l_info[DT_STRTAB]);
253 if (info != NULL)
254 audit_list_add_string (list, strtab + info->d_un.d_val);
257 static const char *
258 audit_list_next (struct audit_list *list)
260 if (list->current_tail == NULL)
261 return NULL;
263 while (true)
265 /* Advance to the next string in audit_strings if the current
266 string has been exhausted. */
267 while (*list->current_tail == '\0')
269 ++list->current_index;
270 if (list->current_index == list->length)
272 list->current_tail = NULL;
273 return NULL;
275 list->current_tail = list->audit_strings[list->current_index];
278 /* Split the in-string audit list at the next colon colon. */
279 size_t len = strcspn (list->current_tail, ":");
280 if (len > 0 && len < sizeof (list->fname))
282 memcpy (list->fname, list->current_tail, len);
283 list->fname[len] = '\0';
285 else
286 /* Mark the name as unusable for dso_name_valid_for_suid. */
287 list->fname[0] = '\0';
289 /* Skip over the substring and the following delimiter. */
290 list->current_tail += len;
291 if (*list->current_tail == ':')
292 ++list->current_tail;
294 /* If the name is valid, return it. */
295 if (dso_name_valid_for_suid (list->fname))
296 return list->fname;
298 /* Otherwise wrap around to find the next list element. . */
302 #ifndef HAVE_INLINED_SYSCALLS
303 /* Set nonzero during loading and initialization of executable and
304 libraries, cleared before the executable's entry point runs. This
305 must not be initialized to nonzero, because the unused dynamic
306 linker loaded in for libc.so's "ld.so.1" dep will provide the
307 definition seen by libc.so's initializer; that value must be zero,
308 and will be since that dynamic linker's _dl_start and dl_main will
309 never be called. */
310 int _dl_starting_up = 0;
311 rtld_hidden_def (_dl_starting_up)
312 #endif
314 /* This is the structure which defines all variables global to ld.so
315 (except those which cannot be added for some reason). */
316 struct rtld_global _rtld_global =
318 /* Get architecture specific initializer. */
319 #include <dl-procruntime.c>
320 /* Generally the default presumption without further information is an
321 * executable stack but this is not true for all platforms. */
322 ._dl_stack_flags = DEFAULT_STACK_PERMS,
323 #ifdef _LIBC_REENTRANT
324 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
325 ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
326 #endif
327 ._dl_nns = 1,
328 ._dl_ns =
330 #ifdef _LIBC_REENTRANT
331 [LM_ID_BASE] = { ._ns_unique_sym_table
332 = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } }
333 #endif
336 /* If we would use strong_alias here the compiler would see a
337 non-hidden definition. This would undo the effect of the previous
338 declaration. So spell out was strong_alias does plus add the
339 visibility attribute. */
340 extern struct rtld_global _rtld_local
341 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
344 /* This variable is similar to _rtld_local, but all values are
345 read-only after relocation. */
346 struct rtld_global_ro _rtld_global_ro attribute_relro =
348 /* Get architecture specific initializer. */
349 #include <dl-procinfo.c>
350 #ifdef NEED_DL_SYSINFO
351 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
352 #endif
353 ._dl_debug_fd = STDERR_FILENO,
354 ._dl_use_load_bias = -2,
355 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
356 #if !HAVE_TUNABLES
357 ._dl_hwcap_mask = HWCAP_IMPORTANT,
358 #endif
359 ._dl_lazy = 1,
360 ._dl_fpu_control = _FPU_DEFAULT,
361 ._dl_pagesize = EXEC_PAGESIZE,
362 ._dl_inhibit_cache = 0,
364 /* Function pointers. */
365 ._dl_debug_printf = _dl_debug_printf,
366 ._dl_mcount = _dl_mcount,
367 ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
368 ._dl_open = _dl_open,
369 ._dl_close = _dl_close,
370 ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
371 #ifdef HAVE_DL_DISCOVER_OSVERSION
372 ._dl_discover_osversion = _dl_discover_osversion
373 #endif
375 /* If we would use strong_alias here the compiler would see a
376 non-hidden definition. This would undo the effect of the previous
377 declaration. So spell out was strong_alias does plus add the
378 visibility attribute. */
379 extern struct rtld_global_ro _rtld_local_ro
380 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
383 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
384 ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv);
386 /* These two variables cannot be moved into .data.rel.ro. */
387 static struct libname_list _dl_rtld_libname;
388 static struct libname_list _dl_rtld_libname2;
390 /* Variable for statistics. */
391 RLTD_TIMING_DECLARE (relocate_time, static);
392 RLTD_TIMING_DECLARE (load_time, static, attribute_relro);
393 RLTD_TIMING_DECLARE (start_time, static, attribute_relro);
395 /* Additional definitions needed by TLS initialization. */
396 #ifdef TLS_INIT_HELPER
397 TLS_INIT_HELPER
398 #endif
400 /* Helper function for syscall implementation. */
401 #ifdef DL_SYSINFO_IMPLEMENTATION
402 DL_SYSINFO_IMPLEMENTATION
403 #endif
405 /* Before ld.so is relocated we must not access variables which need
406 relocations. This means variables which are exported. Variables
407 declared as static are fine. If we can mark a variable hidden this
408 is fine, too. The latter is important here. We can avoid setting
409 up a temporary link map for ld.so if we can mark _rtld_global as
410 hidden. */
411 #ifdef PI_STATIC_AND_HIDDEN
412 # define DONT_USE_BOOTSTRAP_MAP 1
413 #endif
415 #ifdef DONT_USE_BOOTSTRAP_MAP
416 static ElfW(Addr) _dl_start_final (void *arg);
417 #else
418 struct dl_start_final_info
420 struct link_map l;
421 RTLD_TIMING_VAR (start_time);
423 static ElfW(Addr) _dl_start_final (void *arg,
424 struct dl_start_final_info *info);
425 #endif
427 /* These defined magically in the linker script. */
428 extern char _begin[] attribute_hidden;
429 extern char _etext[] attribute_hidden;
430 extern char _end[] attribute_hidden;
433 #ifdef RTLD_START
434 RTLD_START
435 #else
436 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
437 #endif
439 /* This is the second half of _dl_start (below). It can be inlined safely
440 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
441 references. When the tools don't permit us to avoid using a GOT entry
442 for _dl_rtld_global (no attribute_hidden support), we must make sure
443 this function is not inlined (see below). */
445 #ifdef DONT_USE_BOOTSTRAP_MAP
446 static inline ElfW(Addr) __attribute__ ((always_inline))
447 _dl_start_final (void *arg)
448 #else
449 static ElfW(Addr) __attribute__ ((noinline))
450 _dl_start_final (void *arg, struct dl_start_final_info *info)
451 #endif
453 ElfW(Addr) start_addr;
455 /* If it hasn't happen yet record the startup time. */
456 rtld_timer_start (&start_time);
457 #if !defined DONT_USE_BOOTSTRAP_MAP
458 RTLD_TIMING_SET (start_time, info->start_time);
459 #endif
461 /* Transfer data about ourselves to the permanent link_map structure. */
462 #ifndef DONT_USE_BOOTSTRAP_MAP
463 GL(dl_rtld_map).l_addr = info->l.l_addr;
464 GL(dl_rtld_map).l_ld = info->l.l_ld;
465 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
466 sizeof GL(dl_rtld_map).l_info);
467 GL(dl_rtld_map).l_mach = info->l.l_mach;
468 GL(dl_rtld_map).l_relocated = 1;
469 #endif
470 _dl_setup_hash (&GL(dl_rtld_map));
471 GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
472 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
473 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
474 GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
475 /* Copy the TLS related data if necessary. */
476 #ifndef DONT_USE_BOOTSTRAP_MAP
477 # if NO_TLS_OFFSET != 0
478 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
479 # endif
480 #endif
482 /* Initialize the stack end variable. */
483 __libc_stack_end = __builtin_frame_address (0);
485 /* Call the OS-dependent function to set up life so we can do things like
486 file access. It will call `dl_main' (below) to do all the real work
487 of the dynamic linker, and then unwind our frame and run the user
488 entry point on the same stack we entered on. */
489 start_addr = _dl_sysdep_start (arg, &dl_main);
491 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
493 RTLD_TIMING_VAR (rtld_total_time);
494 rtld_timer_stop (&rtld_total_time, start_time);
495 print_statistics (RTLD_TIMING_REF(rtld_total_time));
498 return start_addr;
501 static ElfW(Addr) __attribute_used__
502 _dl_start (void *arg)
504 #ifdef DONT_USE_BOOTSTRAP_MAP
505 # define bootstrap_map GL(dl_rtld_map)
506 #else
507 struct dl_start_final_info info;
508 # define bootstrap_map info.l
509 #endif
511 /* This #define produces dynamic linking inline functions for
512 bootstrap relocation instead of general-purpose relocation.
513 Since ld.so must not have any undefined symbols the result
514 is trivial: always the map of ld.so itself. */
515 #define RTLD_BOOTSTRAP
516 #define BOOTSTRAP_MAP (&bootstrap_map)
517 #define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP
518 #include "dynamic-link.h"
520 #ifdef DONT_USE_BOOTSTRAP_MAP
521 rtld_timer_start (&start_time);
522 #else
523 rtld_timer_start (&info.start_time);
524 #endif
526 /* Partly clean the `bootstrap_map' structure up. Don't use
527 `memset' since it might not be built in or inlined and we cannot
528 make function calls at this point. Use '__builtin_memset' if we
529 know it is available. We do not have to clear the memory if we
530 do not have to use the temporary bootstrap_map. Global variables
531 are initialized to zero by default. */
532 #ifndef DONT_USE_BOOTSTRAP_MAP
533 # ifdef HAVE_BUILTIN_MEMSET
534 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
535 # else
536 for (size_t cnt = 0;
537 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
538 ++cnt)
539 bootstrap_map.l_info[cnt] = 0;
540 # endif
541 #endif
543 /* Figure out the run-time load address of the dynamic linker itself. */
544 bootstrap_map.l_addr = elf_machine_load_address ();
546 /* Read our own dynamic section and fill in the info array. */
547 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
548 elf_get_dynamic_info (&bootstrap_map, NULL);
550 #if NO_TLS_OFFSET != 0
551 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
552 #endif
554 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
555 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
556 #endif
558 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
560 /* Relocate ourselves so we can do normal function calls and
561 data access using the global offset table. */
563 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0, 0);
565 bootstrap_map.l_relocated = 1;
567 /* Please note that we don't allow profiling of this object and
568 therefore need not test whether we have to allocate the array
569 for the relocation results (as done in dl-reloc.c). */
571 /* Now life is sane; we can call functions and access global data.
572 Set up to use the operating system facilities, and find out from
573 the operating system's program loader where to find the program
574 header table in core. Put the rest of _dl_start into a separate
575 function, that way the compiler cannot put accesses to the GOT
576 before ELF_DYNAMIC_RELOCATE. */
578 __rtld_malloc_init_stubs ();
581 #ifdef DONT_USE_BOOTSTRAP_MAP
582 ElfW(Addr) entry = _dl_start_final (arg);
583 #else
584 ElfW(Addr) entry = _dl_start_final (arg, &info);
585 #endif
587 #ifndef ELF_MACHINE_START_ADDRESS
588 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
589 #endif
591 return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
597 /* Now life is peachy; we can do all normal operations.
598 On to the real work. */
600 /* Some helper functions. */
602 /* Arguments to relocate_doit. */
603 struct relocate_args
605 struct link_map *l;
606 int reloc_mode;
609 struct map_args
611 /* Argument to map_doit. */
612 const char *str;
613 struct link_map *loader;
614 int mode;
615 /* Return value of map_doit. */
616 struct link_map *map;
619 struct dlmopen_args
621 const char *fname;
622 struct link_map *map;
625 struct lookup_args
627 const char *name;
628 struct link_map *map;
629 void *result;
632 /* Arguments to version_check_doit. */
633 struct version_check_args
635 int doexit;
636 int dotrace;
639 static void
640 relocate_doit (void *a)
642 struct relocate_args *args = (struct relocate_args *) a;
644 _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0);
647 static void
648 map_doit (void *a)
650 struct map_args *args = (struct map_args *) a;
651 int type = (args->mode == __RTLD_OPENEXEC) ? lt_executable : lt_library;
652 args->map = _dl_map_object (args->loader, args->str, type, 0,
653 args->mode, LM_ID_BASE);
656 static void
657 dlmopen_doit (void *a)
659 struct dlmopen_args *args = (struct dlmopen_args *) a;
660 args->map = _dl_open (args->fname,
661 (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT
662 | __RTLD_SECURE),
663 dl_main, LM_ID_NEWLM, _dl_argc, _dl_argv,
664 __environ);
667 static void
668 lookup_doit (void *a)
670 struct lookup_args *args = (struct lookup_args *) a;
671 const ElfW(Sym) *ref = NULL;
672 args->result = NULL;
673 lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
674 args->map->l_local_scope, NULL, 0,
675 DL_LOOKUP_RETURN_NEWEST, NULL);
676 if (ref != NULL)
677 args->result = DL_SYMBOL_ADDRESS (l, ref);
680 static void
681 version_check_doit (void *a)
683 struct version_check_args *args = (struct version_check_args *) a;
684 if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
685 args->dotrace) && args->doexit)
686 /* We cannot start the application. Abort now. */
687 _exit (1);
691 static inline struct link_map *
692 find_needed (const char *name)
694 struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
695 unsigned int n = scope->r_nlist;
697 while (n-- > 0)
698 if (_dl_name_match_p (name, scope->r_list[n]))
699 return scope->r_list[n];
701 /* Should never happen. */
702 return NULL;
705 static int
706 match_version (const char *string, struct link_map *map)
708 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
709 ElfW(Verdef) *def;
711 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
712 if (map->l_info[VERDEFTAG] == NULL)
713 /* The file has no symbol versioning. */
714 return 0;
716 def = (ElfW(Verdef) *) ((char *) map->l_addr
717 + map->l_info[VERDEFTAG]->d_un.d_ptr);
718 while (1)
720 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
722 /* Compare the version strings. */
723 if (strcmp (string, strtab + aux->vda_name) == 0)
724 /* Bingo! */
725 return 1;
727 /* If no more definitions we failed to find what we want. */
728 if (def->vd_next == 0)
729 break;
731 /* Next definition. */
732 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
735 return 0;
738 static bool tls_init_tp_called;
740 static void *
741 init_tls (void)
743 /* Number of elements in the static TLS block. */
744 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
746 /* Do not do this twice. The audit interface might have required
747 the DTV interfaces to be set up early. */
748 if (GL(dl_initial_dtv) != NULL)
749 return NULL;
751 /* Allocate the array which contains the information about the
752 dtv slots. We allocate a few entries more than needed to
753 avoid the need for reallocation. */
754 size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
756 /* Allocate. */
757 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
758 calloc (sizeof (struct dtv_slotinfo_list)
759 + nelem * sizeof (struct dtv_slotinfo), 1);
760 /* No need to check the return value. If memory allocation failed
761 the program would have been terminated. */
763 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
764 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
765 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
767 /* Fill in the information from the loaded modules. No namespace
768 but the base one can be filled at this time. */
769 assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
770 int i = 0;
771 for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
772 l = l->l_next)
773 if (l->l_tls_blocksize != 0)
775 /* This is a module with TLS data. Store the map reference.
776 The generation counter is zero. */
777 slotinfo[i].map = l;
778 /* slotinfo[i].gen = 0; */
779 ++i;
781 assert (i == GL(dl_tls_max_dtv_idx));
783 /* Compute the TLS offsets for the various blocks. */
784 _dl_determine_tlsoffset ();
786 /* Construct the static TLS block and the dtv for the initial
787 thread. For some platforms this will include allocating memory
788 for the thread descriptor. The memory for the TLS block will
789 never be freed. It should be allocated accordingly. The dtv
790 array can be changed if dynamic loading requires it. */
791 void *tcbp = _dl_allocate_tls_storage ();
792 if (tcbp == NULL)
793 _dl_fatal_printf ("\
794 cannot allocate TLS data structures for initial thread\n");
796 /* Store for detection of the special case by __tls_get_addr
797 so it knows not to pass this dtv to the normal realloc. */
798 GL(dl_initial_dtv) = GET_DTV (tcbp);
800 /* And finally install it for the main thread. */
801 const char *lossage = TLS_INIT_TP (tcbp);
802 if (__glibc_unlikely (lossage != NULL))
803 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
804 tls_init_tp_called = true;
806 return tcbp;
809 static unsigned int
810 do_preload (const char *fname, struct link_map *main_map, const char *where)
812 const char *objname;
813 const char *err_str = NULL;
814 struct map_args args;
815 bool malloced;
817 args.str = fname;
818 args.loader = main_map;
819 args.mode = __RTLD_SECURE;
821 unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
823 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
824 if (__glibc_unlikely (err_str != NULL))
826 _dl_error_printf ("\
827 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
828 fname, where, err_str);
829 /* No need to call free, this is still before
830 the libc's malloc is used. */
832 else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
833 /* It is no duplicate. */
834 return 1;
836 /* Nothing loaded. */
837 return 0;
840 #if defined SHARED && defined _LIBC_REENTRANT \
841 && defined __rtld_lock_default_lock_recursive
842 static void
843 rtld_lock_default_lock_recursive (void *lock)
845 __rtld_lock_default_lock_recursive (lock);
848 static void
849 rtld_lock_default_unlock_recursive (void *lock)
851 __rtld_lock_default_unlock_recursive (lock);
853 #endif
856 static void
857 security_init (void)
859 /* Set up the stack checker's canary. */
860 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
861 #ifdef THREAD_SET_STACK_GUARD
862 THREAD_SET_STACK_GUARD (stack_chk_guard);
863 #else
864 __stack_chk_guard = stack_chk_guard;
865 #endif
867 /* Set up the pointer guard as well, if necessary. */
868 uintptr_t pointer_chk_guard
869 = _dl_setup_pointer_guard (_dl_random, stack_chk_guard);
870 #ifdef THREAD_SET_POINTER_GUARD
871 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
872 #endif
873 __pointer_chk_guard_local = pointer_chk_guard;
875 /* We do not need the _dl_random value anymore. The less
876 information we leave behind, the better, so clear the
877 variable. */
878 _dl_random = NULL;
881 #include <setup-vdso.h>
883 /* The library search path. */
884 static const char *library_path attribute_relro;
885 /* The list preloaded objects. */
886 static const char *preloadlist attribute_relro;
887 /* Nonzero if information about versions has to be printed. */
888 static int version_info attribute_relro;
889 /* The preload list passed as a command argument. */
890 static const char *preloadarg attribute_relro;
892 /* The LD_PRELOAD environment variable gives list of libraries
893 separated by white space or colons that are loaded before the
894 executable's dependencies and prepended to the global scope list.
895 (If the binary is running setuid all elements containing a '/' are
896 ignored since it is insecure.) Return the number of preloads
897 performed. Ditto for --preload command argument. */
898 unsigned int
899 handle_preload_list (const char *preloadlist, struct link_map *main_map,
900 const char *where)
902 unsigned int npreloads = 0;
903 const char *p = preloadlist;
904 char fname[SECURE_PATH_LIMIT];
906 while (*p != '\0')
908 /* Split preload list at space/colon. */
909 size_t len = strcspn (p, " :");
910 if (len > 0 && len < sizeof (fname))
912 memcpy (fname, p, len);
913 fname[len] = '\0';
915 else
916 fname[0] = '\0';
918 /* Skip over the substring and the following delimiter. */
919 p += len;
920 if (*p != '\0')
921 ++p;
923 if (dso_name_valid_for_suid (fname))
924 npreloads += do_preload (fname, main_map, where);
926 return npreloads;
929 /* Called if the audit DSO cannot be used: if it does not have the
930 appropriate interfaces, or it expects a more recent version library
931 version than what the dynamic linker provides. */
932 static void
933 unload_audit_module (struct link_map *map, int original_tls_idx)
935 #ifndef NDEBUG
936 Lmid_t ns = map->l_ns;
937 #endif
938 _dl_close (map);
940 /* Make sure the namespace has been cleared entirely. */
941 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
942 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
944 GL(dl_tls_max_dtv_idx) = original_tls_idx;
947 /* Called to print an error message if loading of an audit module
948 failed. */
949 static void
950 report_audit_module_load_error (const char *name, const char *err_str,
951 bool malloced)
953 _dl_error_printf ("\
954 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
955 name, err_str);
956 if (malloced)
957 free ((char *) err_str);
960 /* Load one audit module. */
961 static void
962 load_audit_module (const char *name, struct audit_ifaces **last_audit)
964 int original_tls_idx = GL(dl_tls_max_dtv_idx);
966 struct dlmopen_args dlmargs;
967 dlmargs.fname = name;
968 dlmargs.map = NULL;
970 const char *objname;
971 const char *err_str = NULL;
972 bool malloced;
973 _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit, &dlmargs);
974 if (__glibc_unlikely (err_str != NULL))
976 report_audit_module_load_error (name, err_str, malloced);
977 return;
980 struct lookup_args largs;
981 largs.name = "la_version";
982 largs.map = dlmargs.map;
983 _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs);
984 if (__glibc_likely (err_str != NULL))
986 unload_audit_module (dlmargs.map, original_tls_idx);
987 report_audit_module_load_error (name, err_str, malloced);
988 return;
991 unsigned int (*laversion) (unsigned int) = largs.result;
993 /* A null symbol indicates that something is very wrong with the
994 loaded object because defined symbols are supposed to have a
995 valid, non-null address. */
996 assert (laversion != NULL);
998 unsigned int lav = laversion (LAV_CURRENT);
999 if (lav == 0)
1001 /* Only print an error message if debugging because this can
1002 happen deliberately. */
1003 if (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
1004 _dl_debug_printf ("\
1005 file=%s [%lu]; audit interface function la_version returned zero; ignored.\n",
1006 dlmargs.map->l_name, dlmargs.map->l_ns);
1007 unload_audit_module (dlmargs.map, original_tls_idx);
1008 return;
1011 if (lav > LAV_CURRENT)
1013 _dl_debug_printf ("\
1014 ERROR: audit interface '%s' requires version %d (maximum supported version %d); ignored.\n",
1015 name, lav, LAV_CURRENT);
1016 unload_audit_module (dlmargs.map, original_tls_idx);
1017 return;
1020 enum { naudit_ifaces = 8 };
1021 union
1023 struct audit_ifaces ifaces;
1024 void (*fptr[naudit_ifaces]) (void);
1025 } *newp = malloc (sizeof (*newp));
1026 if (newp == NULL)
1027 _dl_fatal_printf ("Out of memory while loading audit modules\n");
1029 /* Names of the auditing interfaces. All in one
1030 long string. */
1031 static const char audit_iface_names[] =
1032 "la_activity\0"
1033 "la_objsearch\0"
1034 "la_objopen\0"
1035 "la_preinit\0"
1036 #if __ELF_NATIVE_CLASS == 32
1037 "la_symbind32\0"
1038 #elif __ELF_NATIVE_CLASS == 64
1039 "la_symbind64\0"
1040 #else
1041 # error "__ELF_NATIVE_CLASS must be defined"
1042 #endif
1043 #define STRING(s) __STRING (s)
1044 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1045 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1046 "la_objclose\0";
1047 unsigned int cnt = 0;
1048 const char *cp = audit_iface_names;
1051 largs.name = cp;
1052 _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs);
1054 /* Store the pointer. */
1055 if (err_str == NULL && largs.result != NULL)
1056 newp->fptr[cnt] = largs.result;
1057 else
1058 newp->fptr[cnt] = NULL;
1059 ++cnt;
1061 cp = rawmemchr (cp, '\0') + 1;
1063 while (*cp != '\0');
1064 assert (cnt == naudit_ifaces);
1066 /* Now append the new auditing interface to the list. */
1067 newp->ifaces.next = NULL;
1068 if (*last_audit == NULL)
1069 *last_audit = GLRO(dl_audit) = &newp->ifaces;
1070 else
1071 *last_audit = (*last_audit)->next = &newp->ifaces;
1073 /* The dynamic linker link map is statically allocated, so the
1074 cookie in _dl_new_object has not happened. */
1075 link_map_audit_state (&GL (dl_rtld_map), GLRO (dl_naudit))->cookie
1076 = (intptr_t) &GL (dl_rtld_map);
1078 ++GLRO(dl_naudit);
1080 /* Mark the DSO as being used for auditing. */
1081 dlmargs.map->l_auditing = 1;
1084 /* Notify the the audit modules that the object MAP has already been
1085 loaded. */
1086 static void
1087 notify_audit_modules_of_loaded_object (struct link_map *map)
1089 struct audit_ifaces *afct = GLRO(dl_audit);
1090 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1092 if (afct->objopen != NULL)
1094 struct auditstate *state = link_map_audit_state (map, cnt);
1095 state->bindflags = afct->objopen (map, LM_ID_BASE, &state->cookie);
1096 map->l_audit_any_plt |= state->bindflags != 0;
1099 afct = afct->next;
1103 /* Load all audit modules. */
1104 static void
1105 load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
1107 struct audit_ifaces *last_audit = NULL;
1109 while (true)
1111 const char *name = audit_list_next (audit_list);
1112 if (name == NULL)
1113 break;
1114 load_audit_module (name, &last_audit);
1117 /* Notify audit modules of the initially loaded modules (the main
1118 program and the dynamic linker itself). */
1119 if (GLRO(dl_naudit) > 0)
1121 notify_audit_modules_of_loaded_object (main_map);
1122 notify_audit_modules_of_loaded_object (&GL(dl_rtld_map));
1126 static void
1127 dl_main (const ElfW(Phdr) *phdr,
1128 ElfW(Word) phnum,
1129 ElfW(Addr) *user_entry,
1130 ElfW(auxv_t) *auxv)
1132 const ElfW(Phdr) *ph;
1133 enum mode mode;
1134 struct link_map *main_map;
1135 size_t file_size;
1136 char *file;
1137 bool has_interp = false;
1138 unsigned int i;
1139 bool prelinked = false;
1140 bool rtld_is_main = false;
1141 void *tcbp = NULL;
1143 struct audit_list audit_list;
1144 audit_list_init (&audit_list);
1146 GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
1148 #if defined SHARED && defined _LIBC_REENTRANT \
1149 && defined __rtld_lock_default_lock_recursive
1150 GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
1151 GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
1152 #endif
1154 /* The explicit initialization here is cheaper than processing the reloc
1155 in the _rtld_local definition's initializer. */
1156 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
1158 /* Process the environment variable which control the behaviour. */
1159 process_envvars (&mode, &audit_list);
1161 #ifndef HAVE_INLINED_SYSCALLS
1162 /* Set up a flag which tells we are just starting. */
1163 _dl_starting_up = 1;
1164 #endif
1166 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
1168 /* Ho ho. We are not the program interpreter! We are the program
1169 itself! This means someone ran ld.so as a command. Well, that
1170 might be convenient to do sometimes. We support it by
1171 interpreting the args like this:
1173 ld.so PROGRAM ARGS...
1175 The first argument is the name of a file containing an ELF
1176 executable we will load and run with the following arguments.
1177 To simplify life here, PROGRAM is searched for using the
1178 normal rules for shared objects, rather than $PATH or anything
1179 like that. We just load it and use its entry point; we don't
1180 pay attention to its PT_INTERP command (we are the interpreter
1181 ourselves). This is an easy way to test a new ld.so before
1182 installing it. */
1183 rtld_is_main = true;
1185 /* Note the place where the dynamic linker actually came from. */
1186 GL(dl_rtld_map).l_name = rtld_progname;
1188 while (_dl_argc > 1)
1189 if (! strcmp (_dl_argv[1], "--list"))
1191 mode = list;
1192 GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */
1194 ++_dl_skip_args;
1195 --_dl_argc;
1196 ++_dl_argv;
1198 else if (! strcmp (_dl_argv[1], "--verify"))
1200 mode = verify;
1202 ++_dl_skip_args;
1203 --_dl_argc;
1204 ++_dl_argv;
1206 else if (! strcmp (_dl_argv[1], "--inhibit-cache"))
1208 GLRO(dl_inhibit_cache) = 1;
1209 ++_dl_skip_args;
1210 --_dl_argc;
1211 ++_dl_argv;
1213 else if (! strcmp (_dl_argv[1], "--library-path")
1214 && _dl_argc > 2)
1216 library_path = _dl_argv[2];
1218 _dl_skip_args += 2;
1219 _dl_argc -= 2;
1220 _dl_argv += 2;
1222 else if (! strcmp (_dl_argv[1], "--inhibit-rpath")
1223 && _dl_argc > 2)
1225 GLRO(dl_inhibit_rpath) = _dl_argv[2];
1227 _dl_skip_args += 2;
1228 _dl_argc -= 2;
1229 _dl_argv += 2;
1231 else if (! strcmp (_dl_argv[1], "--audit") && _dl_argc > 2)
1233 audit_list_add_string (&audit_list, _dl_argv[2]);
1235 _dl_skip_args += 2;
1236 _dl_argc -= 2;
1237 _dl_argv += 2;
1239 else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2)
1241 preloadarg = _dl_argv[2];
1242 _dl_skip_args += 2;
1243 _dl_argc -= 2;
1244 _dl_argv += 2;
1246 else
1247 break;
1249 /* If we have no further argument the program was called incorrectly.
1250 Grant the user some education. */
1251 if (_dl_argc < 2)
1252 _dl_fatal_printf ("\
1253 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
1254 You have invoked `ld.so', the helper program for shared library executables.\n\
1255 This program usually lives in the file `/lib/ld.so', and special directives\n\
1256 in executable files using ELF shared libraries tell the system's program\n\
1257 loader to load the helper program from this file. This helper program loads\n\
1258 the shared libraries needed by the program executable, prepares the program\n\
1259 to run, and runs it. You may invoke this helper program directly from the\n\
1260 command line to load and run an ELF executable file; this is like executing\n\
1261 that file itself, but always uses this helper program from the file you\n\
1262 specified, instead of the helper program file specified in the executable\n\
1263 file you run. This is mostly of use for maintainers to test new versions\n\
1264 of this helper program; chances are you did not intend to run this program.\n\
1266 --list list all dependencies and how they are resolved\n\
1267 --verify verify that given object really is a dynamically linked\n\
1268 object we can handle\n\
1269 --inhibit-cache Do not use " LD_SO_CACHE "\n\
1270 --library-path PATH use given PATH instead of content of the environment\n\
1271 variable LD_LIBRARY_PATH\n\
1272 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
1273 in LIST\n\
1274 --audit LIST use objects named in LIST as auditors\n\
1275 --preload LIST preload objects named in LIST\n");
1277 ++_dl_skip_args;
1278 --_dl_argc;
1279 ++_dl_argv;
1281 /* The initialization of _dl_stack_flags done below assumes the
1282 executable's PT_GNU_STACK may have been honored by the kernel, and
1283 so a PT_GNU_STACK with PF_X set means the stack started out with
1284 execute permission. However, this is not really true if the
1285 dynamic linker is the executable the kernel loaded. For this
1286 case, we must reinitialize _dl_stack_flags to match the dynamic
1287 linker itself. If the dynamic linker was built with a
1288 PT_GNU_STACK, then the kernel may have loaded us with a
1289 nonexecutable stack that we will have to make executable when we
1290 load the program below unless it has a PT_GNU_STACK indicating
1291 nonexecutable stack is ok. */
1293 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1294 if (ph->p_type == PT_GNU_STACK)
1296 GL(dl_stack_flags) = ph->p_flags;
1297 break;
1300 if (__builtin_expect (mode, normal) == verify)
1302 const char *objname;
1303 const char *err_str = NULL;
1304 struct map_args args;
1305 bool malloced;
1307 args.str = rtld_progname;
1308 args.loader = NULL;
1309 args.mode = __RTLD_OPENEXEC;
1310 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
1311 &args);
1312 if (__glibc_unlikely (err_str != NULL))
1313 /* We don't free the returned string, the programs stops
1314 anyway. */
1315 _exit (EXIT_FAILURE);
1317 else
1319 RTLD_TIMING_VAR (start);
1320 rtld_timer_start (&start);
1321 _dl_map_object (NULL, rtld_progname, lt_executable, 0,
1322 __RTLD_OPENEXEC, LM_ID_BASE);
1323 rtld_timer_stop (&load_time, start);
1326 /* Now the map for the main executable is available. */
1327 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1329 if (__builtin_expect (mode, normal) == normal
1330 && GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1331 && main_map->l_info[DT_SONAME] != NULL
1332 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1333 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
1334 (const char *) D_PTR (main_map, l_info[DT_STRTAB])
1335 + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
1336 _dl_fatal_printf ("loader cannot load itself\n");
1338 phdr = main_map->l_phdr;
1339 phnum = main_map->l_phnum;
1340 /* We overwrite here a pointer to a malloc()ed string. But since
1341 the malloc() implementation used at this point is the dummy
1342 implementations which has no real free() function it does not
1343 makes sense to free the old string first. */
1344 main_map->l_name = (char *) "";
1345 *user_entry = main_map->l_entry;
1347 #ifdef HAVE_AUX_VECTOR
1348 /* Adjust the on-stack auxiliary vector so that it looks like the
1349 binary was executed directly. */
1350 for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
1351 switch (av->a_type)
1353 case AT_PHDR:
1354 av->a_un.a_val = (uintptr_t) phdr;
1355 break;
1356 case AT_PHNUM:
1357 av->a_un.a_val = phnum;
1358 break;
1359 case AT_ENTRY:
1360 av->a_un.a_val = *user_entry;
1361 break;
1362 case AT_EXECFN:
1363 av->a_un.a_val = (uintptr_t) _dl_argv[0];
1364 break;
1366 #endif
1368 else
1370 /* Create a link_map for the executable itself.
1371 This will be what dlopen on "" returns. */
1372 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
1373 __RTLD_OPENEXEC, LM_ID_BASE);
1374 assert (main_map != NULL);
1375 main_map->l_phdr = phdr;
1376 main_map->l_phnum = phnum;
1377 main_map->l_entry = *user_entry;
1379 /* Even though the link map is not yet fully initialized we can add
1380 it to the map list since there are no possible users running yet. */
1381 _dl_add_to_namespace_list (main_map, LM_ID_BASE);
1382 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1384 /* At this point we are in a bit of trouble. We would have to
1385 fill in the values for l_dev and l_ino. But in general we
1386 do not know where the file is. We also do not handle AT_EXECFD
1387 even if it would be passed up.
1389 We leave the values here defined to 0. This is normally no
1390 problem as the program code itself is normally no shared
1391 object and therefore cannot be loaded dynamically. Nothing
1392 prevent the use of dynamic binaries and in these situations
1393 we might get problems. We might not be able to find out
1394 whether the object is already loaded. But since there is no
1395 easy way out and because the dynamic binary must also not
1396 have an SONAME we ignore this program for now. If it becomes
1397 a problem we can force people using SONAMEs. */
1399 /* We delay initializing the path structure until we got the dynamic
1400 information for the program. */
1403 main_map->l_map_end = 0;
1404 main_map->l_text_end = 0;
1405 /* Perhaps the executable has no PT_LOAD header entries at all. */
1406 main_map->l_map_start = ~0;
1407 /* And it was opened directly. */
1408 ++main_map->l_direct_opencount;
1410 /* Scan the program header table for the dynamic section. */
1411 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1412 switch (ph->p_type)
1414 case PT_PHDR:
1415 /* Find out the load address. */
1416 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1417 break;
1418 case PT_DYNAMIC:
1419 /* This tells us where to find the dynamic section,
1420 which tells us everything we need to do. */
1421 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1422 break;
1423 case PT_INTERP:
1424 /* This "interpreter segment" was used by the program loader to
1425 find the program interpreter, which is this program itself, the
1426 dynamic linker. We note what name finds us, so that a future
1427 dlopen call or DT_NEEDED entry, for something that wants to link
1428 against the dynamic linker as a shared library, will know that
1429 the shared object is already loaded. */
1430 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1431 + ph->p_vaddr);
1432 /* _dl_rtld_libname.next = NULL; Already zero. */
1433 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1435 /* Ordinarilly, we would get additional names for the loader from
1436 our DT_SONAME. This can't happen if we were actually linked as
1437 a static executable (detect this case when we have no DYNAMIC).
1438 If so, assume the filename component of the interpreter path to
1439 be our SONAME, and add it to our name list. */
1440 if (GL(dl_rtld_map).l_ld == NULL)
1442 const char *p = NULL;
1443 const char *cp = _dl_rtld_libname.name;
1445 /* Find the filename part of the path. */
1446 while (*cp != '\0')
1447 if (*cp++ == '/')
1448 p = cp;
1450 if (p != NULL)
1452 _dl_rtld_libname2.name = p;
1453 /* _dl_rtld_libname2.next = NULL; Already zero. */
1454 _dl_rtld_libname.next = &_dl_rtld_libname2;
1458 has_interp = true;
1459 break;
1460 case PT_LOAD:
1462 ElfW(Addr) mapstart;
1463 ElfW(Addr) allocend;
1465 /* Remember where the main program starts in memory. */
1466 mapstart = (main_map->l_addr
1467 + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1468 if (main_map->l_map_start > mapstart)
1469 main_map->l_map_start = mapstart;
1471 /* Also where it ends. */
1472 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1473 if (main_map->l_map_end < allocend)
1474 main_map->l_map_end = allocend;
1475 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1476 main_map->l_text_end = allocend;
1478 break;
1480 case PT_TLS:
1481 if (ph->p_memsz > 0)
1483 /* Note that in the case the dynamic linker we duplicate work
1484 here since we read the PT_TLS entry already in
1485 _dl_start_final. But the result is repeatable so do not
1486 check for this special but unimportant case. */
1487 main_map->l_tls_blocksize = ph->p_memsz;
1488 main_map->l_tls_align = ph->p_align;
1489 if (ph->p_align == 0)
1490 main_map->l_tls_firstbyte_offset = 0;
1491 else
1492 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1493 & (ph->p_align - 1));
1494 main_map->l_tls_initimage_size = ph->p_filesz;
1495 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1497 /* This image gets the ID one. */
1498 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1500 break;
1502 case PT_GNU_STACK:
1503 GL(dl_stack_flags) = ph->p_flags;
1504 break;
1506 case PT_GNU_RELRO:
1507 main_map->l_relro_addr = ph->p_vaddr;
1508 main_map->l_relro_size = ph->p_memsz;
1509 break;
1511 case PT_NOTE:
1512 if (_rtld_process_pt_note (main_map, ph))
1513 _dl_error_printf ("\
1514 ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
1515 break;
1518 /* Adjust the address of the TLS initialization image in case
1519 the executable is actually an ET_DYN object. */
1520 if (main_map->l_tls_initimage != NULL)
1521 main_map->l_tls_initimage
1522 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1523 if (! main_map->l_map_end)
1524 main_map->l_map_end = ~0;
1525 if (! main_map->l_text_end)
1526 main_map->l_text_end = ~0;
1527 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1529 /* We were invoked directly, so the program might not have a
1530 PT_INTERP. */
1531 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1532 /* _dl_rtld_libname.next = NULL; Already zero. */
1533 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1535 else
1536 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1538 /* If the current libname is different from the SONAME, add the
1539 latter as well. */
1540 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1541 && strcmp (GL(dl_rtld_map).l_libname->name,
1542 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1543 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1545 static struct libname_list newname;
1546 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1547 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1548 newname.next = NULL;
1549 newname.dont_free = 1;
1551 assert (GL(dl_rtld_map).l_libname->next == NULL);
1552 GL(dl_rtld_map).l_libname->next = &newname;
1554 /* The ld.so must be relocated since otherwise loading audit modules
1555 will fail since they reuse the very same ld.so. */
1556 assert (GL(dl_rtld_map).l_relocated);
1558 if (! rtld_is_main)
1560 /* Extract the contents of the dynamic section for easy access. */
1561 elf_get_dynamic_info (main_map, NULL);
1562 /* Set up our cache of pointers into the hash table. */
1563 _dl_setup_hash (main_map);
1566 if (__builtin_expect (mode, normal) == verify)
1568 /* We were called just to verify that this is a dynamic
1569 executable using us as the program interpreter. Exit with an
1570 error if we were not able to load the binary or no interpreter
1571 is specified (i.e., this is no dynamically linked binary. */
1572 if (main_map->l_ld == NULL)
1573 _exit (1);
1575 /* We allow here some platform specific code. */
1576 #ifdef DISTINGUISH_LIB_VERSIONS
1577 DISTINGUISH_LIB_VERSIONS;
1578 #endif
1579 _exit (has_interp ? 0 : 2);
1582 struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1583 /* Set up the data structures for the system-supplied DSO early,
1584 so they can influence _dl_init_paths. */
1585 setup_vdso (main_map, &first_preload);
1587 /* With vDSO setup we can initialize the function pointers. */
1588 setup_vdso_pointers ();
1590 #ifdef DL_SYSDEP_OSCHECK
1591 DL_SYSDEP_OSCHECK (_dl_fatal_printf);
1592 #endif
1594 /* Initialize the data structures for the search paths for shared
1595 objects. */
1596 _dl_init_paths (library_path);
1598 /* Initialize _r_debug. */
1599 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1600 LM_ID_BASE);
1601 r->r_state = RT_CONSISTENT;
1603 /* Put the link_map for ourselves on the chain so it can be found by
1604 name. Note that at this point the global chain of link maps contains
1605 exactly one element, which is pointed to by dl_loaded. */
1606 if (! GL(dl_rtld_map).l_name)
1607 /* If not invoked directly, the dynamic linker shared object file was
1608 found by the PT_INTERP name. */
1609 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1610 GL(dl_rtld_map).l_type = lt_library;
1611 main_map->l_next = &GL(dl_rtld_map);
1612 GL(dl_rtld_map).l_prev = main_map;
1613 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1614 ++GL(dl_load_adds);
1616 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1617 to not using bias for non-prelinked PIEs and libraries
1618 and using it for executables or prelinked PIEs or libraries. */
1619 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1620 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1622 /* Set up the program header information for the dynamic linker
1623 itself. It is needed in the dl_iterate_phdr callbacks. */
1624 const ElfW(Ehdr) *rtld_ehdr;
1626 /* Starting from binutils-2.23, the linker will define the magic symbol
1627 __ehdr_start to point to our own ELF header if it is visible in a
1628 segment that also includes the phdrs. If that's not available, we use
1629 the old method that assumes the beginning of the file is part of the
1630 lowest-addressed PT_LOAD segment. */
1631 #ifdef HAVE_EHDR_START
1632 extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden")));
1633 rtld_ehdr = &__ehdr_start;
1634 #else
1635 rtld_ehdr = (void *) GL(dl_rtld_map).l_map_start;
1636 #endif
1637 assert (rtld_ehdr->e_ehsize == sizeof *rtld_ehdr);
1638 assert (rtld_ehdr->e_phentsize == sizeof (ElfW(Phdr)));
1640 const ElfW(Phdr) *rtld_phdr = (const void *) rtld_ehdr + rtld_ehdr->e_phoff;
1642 GL(dl_rtld_map).l_phdr = rtld_phdr;
1643 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1646 /* PT_GNU_RELRO is usually the last phdr. */
1647 size_t cnt = rtld_ehdr->e_phnum;
1648 while (cnt-- > 0)
1649 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1651 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1652 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1653 break;
1656 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1657 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1658 /* Assign a module ID. Do this before loading any audit modules. */
1659 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1661 audit_list_add_dynamic_tag (&audit_list, main_map, DT_AUDIT);
1662 audit_list_add_dynamic_tag (&audit_list, main_map, DT_DEPAUDIT);
1664 /* If we have auditing DSOs to load, do it now. */
1665 bool need_security_init = true;
1666 if (audit_list.length > 0)
1668 /* Since we start using the auditing DSOs right away we need to
1669 initialize the data structures now. */
1670 tcbp = init_tls ();
1672 /* Initialize security features. We need to do it this early
1673 since otherwise the constructors of the audit libraries will
1674 use different values (especially the pointer guard) and will
1675 fail later on. */
1676 security_init ();
1677 need_security_init = false;
1679 load_audit_modules (main_map, &audit_list);
1682 /* Keep track of the currently loaded modules to count how many
1683 non-audit modules which use TLS are loaded. */
1684 size_t count_modids = _dl_count_modids ();
1686 /* Set up debugging before the debugger is notified for the first time. */
1687 #ifdef ELF_MACHINE_DEBUG_SETUP
1688 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1689 ELF_MACHINE_DEBUG_SETUP (main_map, r);
1690 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1691 #else
1692 if (main_map->l_info[DT_DEBUG] != NULL)
1693 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1694 with the run-time address of the r_debug structure */
1695 main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1697 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1698 case you run gdb on the dynamic linker directly. */
1699 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1700 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1701 #endif
1703 /* We start adding objects. */
1704 r->r_state = RT_ADD;
1705 _dl_debug_state ();
1706 LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
1708 /* Auditing checkpoint: we are ready to signal that the initial map
1709 is being constructed. */
1710 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
1712 struct audit_ifaces *afct = GLRO(dl_audit);
1713 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1715 if (afct->activity != NULL)
1716 afct->activity (&link_map_audit_state (main_map, cnt)->cookie,
1717 LA_ACT_ADD);
1719 afct = afct->next;
1723 /* We have two ways to specify objects to preload: via environment
1724 variable and via the file /etc/ld.so.preload. The latter can also
1725 be used when security is enabled. */
1726 assert (*first_preload == NULL);
1727 struct link_map **preloads = NULL;
1728 unsigned int npreloads = 0;
1730 if (__glibc_unlikely (preloadlist != NULL))
1732 RTLD_TIMING_VAR (start);
1733 rtld_timer_start (&start);
1734 npreloads += handle_preload_list (preloadlist, main_map, "LD_PRELOAD");
1735 rtld_timer_accum (&load_time, start);
1738 if (__glibc_unlikely (preloadarg != NULL))
1740 RTLD_TIMING_VAR (start);
1741 rtld_timer_start (&start);
1742 npreloads += handle_preload_list (preloadarg, main_map, "--preload");
1743 rtld_timer_accum (&load_time, start);
1746 /* There usually is no ld.so.preload file, it should only be used
1747 for emergencies and testing. So the open call etc should usually
1748 fail. Using access() on a non-existing file is faster than using
1749 open(). So we do this first. If it succeeds we do almost twice
1750 the work but this does not matter, since it is not for production
1751 use. */
1752 static const char preload_file[] = "/etc/ld.so.preload";
1753 if (__glibc_unlikely (__access (preload_file, R_OK) == 0))
1755 /* Read the contents of the file. */
1756 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1757 PROT_READ | PROT_WRITE);
1758 if (__glibc_unlikely (file != MAP_FAILED))
1760 /* Parse the file. It contains names of libraries to be loaded,
1761 separated by white spaces or `:'. It may also contain
1762 comments introduced by `#'. */
1763 char *problem;
1764 char *runp;
1765 size_t rest;
1767 /* Eliminate comments. */
1768 runp = file;
1769 rest = file_size;
1770 while (rest > 0)
1772 char *comment = memchr (runp, '#', rest);
1773 if (comment == NULL)
1774 break;
1776 rest -= comment - runp;
1778 *comment = ' ';
1779 while (--rest > 0 && *++comment != '\n');
1782 /* We have one problematic case: if we have a name at the end of
1783 the file without a trailing terminating characters, we cannot
1784 place the \0. Handle the case separately. */
1785 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1786 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1788 problem = &file[file_size];
1789 while (problem > file && problem[-1] != ' '
1790 && problem[-1] != '\t'
1791 && problem[-1] != '\n' && problem[-1] != ':')
1792 --problem;
1794 if (problem > file)
1795 problem[-1] = '\0';
1797 else
1799 problem = NULL;
1800 file[file_size - 1] = '\0';
1803 RTLD_TIMING_VAR (start);
1804 rtld_timer_start (&start);
1806 if (file != problem)
1808 char *p;
1809 runp = file;
1810 while ((p = strsep (&runp, ": \t\n")) != NULL)
1811 if (p[0] != '\0')
1812 npreloads += do_preload (p, main_map, preload_file);
1815 if (problem != NULL)
1817 char *p = strndupa (problem, file_size - (problem - file));
1819 npreloads += do_preload (p, main_map, preload_file);
1822 rtld_timer_accum (&load_time, start);
1824 /* We don't need the file anymore. */
1825 __munmap (file, file_size);
1829 if (__glibc_unlikely (*first_preload != NULL))
1831 /* Set up PRELOADS with a vector of the preloaded libraries. */
1832 struct link_map *l = *first_preload;
1833 preloads = __alloca (npreloads * sizeof preloads[0]);
1834 i = 0;
1837 preloads[i++] = l;
1838 l = l->l_next;
1839 } while (l);
1840 assert (i == npreloads);
1843 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1844 specified some libraries to load, these are inserted before the actual
1845 dependencies in the executable's searchlist for symbol resolution. */
1847 RTLD_TIMING_VAR (start);
1848 rtld_timer_start (&start);
1849 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
1850 rtld_timer_accum (&load_time, start);
1853 /* Mark all objects as being in the global scope. */
1854 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1855 main_map->l_searchlist.r_list[--i]->l_global = 1;
1857 /* Remove _dl_rtld_map from the chain. */
1858 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1859 if (GL(dl_rtld_map).l_next != NULL)
1860 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1862 for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1863 if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1864 break;
1866 bool rtld_multiple_ref = false;
1867 if (__glibc_likely (i < main_map->l_searchlist.r_nlist))
1869 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1870 put it back in the list of visible objects. We insert it into the
1871 chain in symbol search order because gdb uses the chain's order as
1872 its symbol search order. */
1873 rtld_multiple_ref = true;
1875 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1876 if (__builtin_expect (mode, normal) == normal)
1878 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1879 ? main_map->l_searchlist.r_list[i + 1]
1880 : NULL);
1881 #ifdef NEED_DL_SYSINFO_DSO
1882 if (GLRO(dl_sysinfo_map) != NULL
1883 && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
1884 && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
1885 GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
1886 #endif
1888 else
1889 /* In trace mode there might be an invisible object (which we
1890 could not find) after the previous one in the search list.
1891 In this case it doesn't matter much where we put the
1892 interpreter object, so we just initialize the list pointer so
1893 that the assertion below holds. */
1894 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1896 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1897 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1898 if (GL(dl_rtld_map).l_next != NULL)
1900 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1901 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1905 /* Now let us see whether all libraries are available in the
1906 versions we need. */
1908 struct version_check_args args;
1909 args.doexit = mode == normal;
1910 args.dotrace = mode == trace;
1911 _dl_receive_error (print_missing_version, version_check_doit, &args);
1914 /* We do not initialize any of the TLS functionality unless any of the
1915 initial modules uses TLS. This makes dynamic loading of modules with
1916 TLS impossible, but to support it requires either eagerly doing setup
1917 now or lazily doing it later. Doing it now makes us incompatible with
1918 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1919 used. Trying to do it lazily is too hairy to try when there could be
1920 multiple threads (from a non-TLS-using libpthread). */
1921 bool was_tls_init_tp_called = tls_init_tp_called;
1922 if (tcbp == NULL)
1923 tcbp = init_tls ();
1925 if (__glibc_likely (need_security_init))
1926 /* Initialize security features. But only if we have not done it
1927 earlier. */
1928 security_init ();
1930 if (__builtin_expect (mode, normal) != normal)
1932 /* We were run just to list the shared libraries. It is
1933 important that we do this before real relocation, because the
1934 functions we call below for output may no longer work properly
1935 after relocation. */
1936 struct link_map *l;
1938 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1940 struct r_scope_elem *scope = &main_map->l_searchlist;
1942 for (i = 0; i < scope->r_nlist; i++)
1944 l = scope->r_list [i];
1945 if (l->l_faked)
1947 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1948 continue;
1950 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
1951 GLRO(dl_trace_prelink_map) = l;
1952 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1953 DSO_FILENAME (l->l_libname->name),
1954 DSO_FILENAME (l->l_name),
1955 (int) sizeof l->l_map_start * 2,
1956 (size_t) l->l_map_start,
1957 (int) sizeof l->l_addr * 2,
1958 (size_t) l->l_addr);
1960 if (l->l_tls_modid)
1961 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1962 (int) sizeof l->l_tls_offset * 2,
1963 (size_t) l->l_tls_offset);
1964 else
1965 _dl_printf ("\n");
1968 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
1970 /* Look through the dependencies of the main executable
1971 and determine which of them is not actually
1972 required. */
1973 struct link_map *l = main_map;
1975 /* Relocate the main executable. */
1976 struct relocate_args args = { .l = l,
1977 .reloc_mode = ((GLRO(dl_lazy)
1978 ? RTLD_LAZY : 0)
1979 | __RTLD_NOIFUNC) };
1980 _dl_receive_error (print_unresolved, relocate_doit, &args);
1982 /* This loop depends on the dependencies of the executable to
1983 correspond in number and order to the DT_NEEDED entries. */
1984 ElfW(Dyn) *dyn = main_map->l_ld;
1985 bool first = true;
1986 while (dyn->d_tag != DT_NULL)
1988 if (dyn->d_tag == DT_NEEDED)
1990 l = l->l_next;
1991 #ifdef NEED_DL_SYSINFO_DSO
1992 /* Skip the VDSO since it's not part of the list
1993 of objects we brought in via DT_NEEDED entries. */
1994 if (l == GLRO(dl_sysinfo_map))
1995 l = l->l_next;
1996 #endif
1997 if (!l->l_used)
1999 if (first)
2001 _dl_printf ("Unused direct dependencies:\n");
2002 first = false;
2005 _dl_printf ("\t%s\n", l->l_name);
2009 ++dyn;
2012 _exit (first != true);
2014 else if (! main_map->l_info[DT_NEEDED])
2015 _dl_printf ("\tstatically linked\n");
2016 else
2018 for (l = main_map->l_next; l; l = l->l_next)
2019 if (l->l_faked)
2020 /* The library was not found. */
2021 _dl_printf ("\t%s => not found\n", l->l_libname->name);
2022 else if (strcmp (l->l_libname->name, l->l_name) == 0)
2023 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
2024 (int) sizeof l->l_map_start * 2,
2025 (size_t) l->l_map_start);
2026 else
2027 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
2028 l->l_name, (int) sizeof l->l_map_start * 2,
2029 (size_t) l->l_map_start);
2032 if (__builtin_expect (mode, trace) != trace)
2033 for (i = 1; i < (unsigned int) _dl_argc; ++i)
2035 const ElfW(Sym) *ref = NULL;
2036 ElfW(Addr) loadbase;
2037 lookup_t result;
2039 result = _dl_lookup_symbol_x (_dl_argv[i], main_map,
2040 &ref, main_map->l_scope,
2041 NULL, ELF_RTYPE_CLASS_PLT,
2042 DL_LOOKUP_ADD_DEPENDENCY, NULL);
2044 loadbase = LOOKUP_VALUE_ADDRESS (result, false);
2046 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
2047 _dl_argv[i],
2048 (int) sizeof ref->st_value * 2,
2049 (size_t) ref->st_value,
2050 (int) sizeof loadbase * 2, (size_t) loadbase);
2052 else
2054 /* If LD_WARN is set, warn about undefined symbols. */
2055 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
2057 /* We have to do symbol dependency testing. */
2058 struct relocate_args args;
2059 unsigned int i;
2061 args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
2062 | __RTLD_NOIFUNC);
2064 i = main_map->l_searchlist.r_nlist;
2065 while (i-- > 0)
2067 struct link_map *l = main_map->l_initfini[i];
2068 if (l != &GL(dl_rtld_map) && ! l->l_faked)
2070 args.l = l;
2071 _dl_receive_error (print_unresolved, relocate_doit,
2072 &args);
2076 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
2077 && rtld_multiple_ref)
2079 /* Mark the link map as not yet relocated again. */
2080 GL(dl_rtld_map).l_relocated = 0;
2081 _dl_relocate_object (&GL(dl_rtld_map),
2082 main_map->l_scope, __RTLD_NOIFUNC, 0);
2085 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2086 if (version_info)
2088 /* Print more information. This means here, print information
2089 about the versions needed. */
2090 int first = 1;
2091 struct link_map *map;
2093 for (map = main_map; map != NULL; map = map->l_next)
2095 const char *strtab;
2096 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
2097 ElfW(Verneed) *ent;
2099 if (dyn == NULL)
2100 continue;
2102 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
2103 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
2105 if (first)
2107 _dl_printf ("\n\tVersion information:\n");
2108 first = 0;
2111 _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name));
2113 while (1)
2115 ElfW(Vernaux) *aux;
2116 struct link_map *needed;
2118 needed = find_needed (strtab + ent->vn_file);
2119 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
2121 while (1)
2123 const char *fname = NULL;
2125 if (needed != NULL
2126 && match_version (strtab + aux->vna_name,
2127 needed))
2128 fname = needed->l_name;
2130 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2131 strtab + ent->vn_file,
2132 strtab + aux->vna_name,
2133 aux->vna_flags & VER_FLG_WEAK
2134 ? "[WEAK] " : "",
2135 fname ?: "not found");
2137 if (aux->vna_next == 0)
2138 /* No more symbols. */
2139 break;
2141 /* Next symbol. */
2142 aux = (ElfW(Vernaux) *) ((char *) aux
2143 + aux->vna_next);
2146 if (ent->vn_next == 0)
2147 /* No more dependencies. */
2148 break;
2150 /* Next dependency. */
2151 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
2157 _exit (0);
2160 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
2161 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
2162 && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
2164 ElfW(Lib) *liblist, *liblistend;
2165 struct link_map **r_list, **r_listend, *l;
2166 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
2168 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
2169 liblist = (ElfW(Lib) *)
2170 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
2171 liblistend = (ElfW(Lib) *)
2172 ((char *) liblist
2173 + main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
2174 r_list = main_map->l_searchlist.r_list;
2175 r_listend = r_list + main_map->l_searchlist.r_nlist;
2177 for (; r_list < r_listend && liblist < liblistend; r_list++)
2179 l = *r_list;
2181 if (l == main_map)
2182 continue;
2184 /* If the library is not mapped where it should, fail. */
2185 if (l->l_addr)
2186 break;
2188 /* Next, check if checksum matches. */
2189 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
2190 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
2191 != liblist->l_checksum)
2192 break;
2194 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
2195 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
2196 != liblist->l_time_stamp)
2197 break;
2199 if (! _dl_name_match_p (strtab + liblist->l_name, l))
2200 break;
2202 ++liblist;
2206 if (r_list == r_listend && liblist == liblistend)
2207 prelinked = true;
2209 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2210 _dl_debug_printf ("\nprelink checking: %s\n",
2211 prelinked ? "ok" : "failed");
2215 /* Now set up the variable which helps the assembler startup code. */
2216 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
2218 /* Save the information about the original global scope list since
2219 we need it in the memory handling later. */
2220 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
2222 /* Remember the last search directory added at startup, now that
2223 malloc will no longer be the one from dl-minimal.c. As a side
2224 effect, this marks ld.so as initialized, so that the rtld_active
2225 function returns true from now on. */
2226 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
2228 /* Print scope information. */
2229 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
2231 _dl_debug_printf ("\nInitial object scopes\n");
2233 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2234 _dl_show_scope (l, 0);
2237 _rtld_main_check (main_map, _dl_argv[0]);
2239 if (prelinked)
2241 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2243 ElfW(Rela) *conflict, *conflictend;
2245 RTLD_TIMING_VAR (start);
2246 rtld_timer_start (&start);
2248 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2249 conflict = (ElfW(Rela) *)
2250 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2251 conflictend = (ElfW(Rela) *)
2252 ((char *) conflict
2253 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2254 _dl_resolve_conflicts (main_map, conflict, conflictend);
2256 rtld_timer_stop (&relocate_time, start);
2259 /* The library defining malloc has already been relocated due to
2260 prelinking. Resolve the malloc symbols for the dynamic
2261 loader. */
2262 __rtld_malloc_init_real (main_map);
2264 /* Mark all the objects so we know they have been already relocated. */
2265 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2267 l->l_relocated = 1;
2268 if (l->l_relro_size)
2269 _dl_protect_relro (l);
2271 /* Add object to slot information data if necessasy. */
2272 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2273 _dl_add_to_slotinfo (l, true);
2276 else
2278 /* Now we have all the objects loaded. Relocate them all except for
2279 the dynamic linker itself. We do this in reverse order so that copy
2280 relocs of earlier objects overwrite the data written by later
2281 objects. We do not re-relocate the dynamic linker itself in this
2282 loop because that could result in the GOT entries for functions we
2283 call being changed, and that would break us. It is safe to relocate
2284 the dynamic linker out of order because it has no copy relocs (we
2285 know that because it is self-contained). */
2287 int consider_profiling = GLRO(dl_profile) != NULL;
2289 /* If we are profiling we also must do lazy reloaction. */
2290 GLRO(dl_lazy) |= consider_profiling;
2292 RTLD_TIMING_VAR (start);
2293 rtld_timer_start (&start);
2294 unsigned i = main_map->l_searchlist.r_nlist;
2295 while (i-- > 0)
2297 struct link_map *l = main_map->l_initfini[i];
2299 /* While we are at it, help the memory handling a bit. We have to
2300 mark some data structures as allocated with the fake malloc()
2301 implementation in ld.so. */
2302 struct libname_list *lnp = l->l_libname->next;
2304 while (__builtin_expect (lnp != NULL, 0))
2306 lnp->dont_free = 1;
2307 lnp = lnp->next;
2309 /* Also allocated with the fake malloc(). */
2310 l->l_free_initfini = 0;
2312 if (l != &GL(dl_rtld_map))
2313 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2314 consider_profiling);
2316 /* Add object to slot information data if necessasy. */
2317 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2318 _dl_add_to_slotinfo (l, true);
2320 rtld_timer_stop (&relocate_time, start);
2322 /* Now enable profiling if needed. Like the previous call,
2323 this has to go here because the calls it makes should use the
2324 rtld versions of the functions (particularly calloc()), but it
2325 needs to have _dl_profile_map set up by the relocator. */
2326 if (__glibc_unlikely (GL(dl_profile_map) != NULL))
2327 /* We must prepare the profiling. */
2328 _dl_start_profile ();
2331 if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2332 || count_modids != _dl_count_modids ())
2333 ++GL(dl_tls_generation);
2335 /* Now that we have completed relocation, the initializer data
2336 for the TLS blocks has its final values and we can copy them
2337 into the main thread's TLS area, which we allocated above.
2338 Note: thread-local variables must only be accessed after completing
2339 the next step. */
2340 _dl_allocate_tls_init (tcbp);
2342 /* And finally install it for the main thread. */
2343 if (! tls_init_tp_called)
2345 const char *lossage = TLS_INIT_TP (tcbp);
2346 if (__glibc_unlikely (lossage != NULL))
2347 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2348 lossage);
2351 /* Make sure no new search directories have been added. */
2352 assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2354 if (! prelinked && rtld_multiple_ref)
2356 /* There was an explicit ref to the dynamic linker as a shared lib.
2357 Re-relocate ourselves with user-controlled symbol definitions.
2359 We must do this after TLS initialization in case after this
2360 re-relocation, we might call a user-supplied function
2361 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2363 /* The malloc implementation has been relocated, so resolving
2364 its symbols (and potentially calling IFUNC resolvers) is safe
2365 at this point. */
2366 __rtld_malloc_init_real (main_map);
2368 RTLD_TIMING_VAR (start);
2369 rtld_timer_start (&start);
2371 /* Mark the link map as not yet relocated again. */
2372 GL(dl_rtld_map).l_relocated = 0;
2373 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2375 rtld_timer_accum (&relocate_time, start);
2378 /* Relocation is complete. Perform early libc initialization. This
2379 is the initial libc, even if audit modules have been loaded with
2380 other libcs. */
2381 _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map, true);
2383 /* Do any necessary cleanups for the startup OS interface code.
2384 We do these now so that no calls are made after rtld re-relocation
2385 which might be resolved to different functions than we expect.
2386 We cannot do this before relocating the other objects because
2387 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2388 _dl_sysdep_start_cleanup ();
2390 #ifdef SHARED
2391 /* Auditing checkpoint: we have added all objects. */
2392 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
2394 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2395 /* Do not call the functions for any auditing object. */
2396 if (head->l_auditing == 0)
2398 struct audit_ifaces *afct = GLRO(dl_audit);
2399 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2401 if (afct->activity != NULL)
2402 afct->activity (&link_map_audit_state (head, cnt)->cookie,
2403 LA_ACT_CONSISTENT);
2405 afct = afct->next;
2409 #endif
2411 /* Notify the debugger all new objects are now ready to go. We must re-get
2412 the address since by now the variable might be in another object. */
2413 r = _dl_debug_initialize (0, LM_ID_BASE);
2414 r->r_state = RT_CONSISTENT;
2415 _dl_debug_state ();
2416 LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
2418 #if defined USE_LDCONFIG && !defined MAP_COPY
2419 /* We must munmap() the cache file. */
2420 _dl_unload_cache ();
2421 #endif
2423 /* Once we return, _dl_sysdep_start will invoke
2424 the DT_INIT functions and then *USER_ENTRY. */
2427 /* This is a little helper function for resolving symbols while
2428 tracing the binary. */
2429 static void
2430 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2431 const char *errstring)
2433 if (objname[0] == '\0')
2434 objname = RTLD_PROGNAME;
2435 _dl_error_printf ("%s (%s)\n", errstring, objname);
2438 /* This is a little helper function for resolving symbols while
2439 tracing the binary. */
2440 static void
2441 print_missing_version (int errcode __attribute__ ((unused)),
2442 const char *objname, const char *errstring)
2444 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME,
2445 objname, errstring);
2448 /* Nonzero if any of the debugging options is enabled. */
2449 static int any_debug attribute_relro;
2451 /* Process the string given as the parameter which explains which debugging
2452 options are enabled. */
2453 static void
2454 process_dl_debug (const char *dl_debug)
2456 /* When adding new entries make sure that the maximal length of a name
2457 is correctly handled in the LD_DEBUG_HELP code below. */
2458 static const struct
2460 unsigned char len;
2461 const char name[10];
2462 const char helptext[41];
2463 unsigned short int mask;
2464 } debopts[] =
2466 #define LEN_AND_STR(str) sizeof (str) - 1, str
2467 { LEN_AND_STR ("libs"), "display library search paths",
2468 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2469 { LEN_AND_STR ("reloc"), "display relocation processing",
2470 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2471 { LEN_AND_STR ("files"), "display progress for input file",
2472 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2473 { LEN_AND_STR ("symbols"), "display symbol table processing",
2474 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2475 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2476 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2477 { LEN_AND_STR ("versions"), "display version dependencies",
2478 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2479 { LEN_AND_STR ("scopes"), "display scope information",
2480 DL_DEBUG_SCOPES },
2481 { LEN_AND_STR ("all"), "all previous options combined",
2482 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2483 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2484 | DL_DEBUG_SCOPES },
2485 { LEN_AND_STR ("statistics"), "display relocation statistics",
2486 DL_DEBUG_STATISTICS },
2487 { LEN_AND_STR ("unused"), "determined unused DSOs",
2488 DL_DEBUG_UNUSED },
2489 { LEN_AND_STR ("help"), "display this help message and exit",
2490 DL_DEBUG_HELP },
2492 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2494 /* Skip separating white spaces and commas. */
2495 while (*dl_debug != '\0')
2497 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2499 size_t cnt;
2500 size_t len = 1;
2502 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2503 && dl_debug[len] != ',' && dl_debug[len] != ':')
2504 ++len;
2506 for (cnt = 0; cnt < ndebopts; ++cnt)
2507 if (debopts[cnt].len == len
2508 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2510 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2511 any_debug = 1;
2512 break;
2515 if (cnt == ndebopts)
2517 /* Display a warning and skip everything until next
2518 separator. */
2519 char *copy = strndupa (dl_debug, len);
2520 _dl_error_printf ("\
2521 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2524 dl_debug += len;
2525 continue;
2528 ++dl_debug;
2531 if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2533 /* In order to get an accurate picture of whether a particular
2534 DT_NEEDED entry is actually used we have to process both
2535 the PLT and non-PLT relocation entries. */
2536 GLRO(dl_lazy) = 0;
2539 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2541 size_t cnt;
2543 _dl_printf ("\
2544 Valid options for the LD_DEBUG environment variable are:\n\n");
2546 for (cnt = 0; cnt < ndebopts; ++cnt)
2547 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2548 " " + debopts[cnt].len - 3,
2549 debopts[cnt].helptext);
2551 _dl_printf ("\n\
2552 To direct the debugging output into a file instead of standard output\n\
2553 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2554 _exit (0);
2558 /* Process all environments variables the dynamic linker must recognize.
2559 Since all of them start with `LD_' we are a bit smarter while finding
2560 all the entries. */
2561 extern char **_environ attribute_hidden;
2564 static void
2565 process_envvars (enum mode *modep, struct audit_list *audit_list)
2567 char **runp = _environ;
2568 char *envline;
2569 enum mode mode = normal;
2570 char *debug_output = NULL;
2572 /* This is the default place for profiling data file. */
2573 GLRO(dl_profile_output)
2574 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
2576 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2578 size_t len = 0;
2580 while (envline[len] != '\0' && envline[len] != '=')
2581 ++len;
2583 if (envline[len] != '=')
2584 /* This is a "LD_" variable at the end of the string without
2585 a '=' character. Ignore it since otherwise we will access
2586 invalid memory below. */
2587 continue;
2589 switch (len)
2591 case 4:
2592 /* Warning level, verbose or not. */
2593 if (memcmp (envline, "WARN", 4) == 0)
2594 GLRO(dl_verbose) = envline[5] != '\0';
2595 break;
2597 case 5:
2598 /* Debugging of the dynamic linker? */
2599 if (memcmp (envline, "DEBUG", 5) == 0)
2601 process_dl_debug (&envline[6]);
2602 break;
2604 if (memcmp (envline, "AUDIT", 5) == 0)
2605 audit_list_add_string (audit_list, &envline[6]);
2606 break;
2608 case 7:
2609 /* Print information about versions. */
2610 if (memcmp (envline, "VERBOSE", 7) == 0)
2612 version_info = envline[8] != '\0';
2613 break;
2616 /* List of objects to be preloaded. */
2617 if (memcmp (envline, "PRELOAD", 7) == 0)
2619 preloadlist = &envline[8];
2620 break;
2623 /* Which shared object shall be profiled. */
2624 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2625 GLRO(dl_profile) = &envline[8];
2626 break;
2628 case 8:
2629 /* Do we bind early? */
2630 if (memcmp (envline, "BIND_NOW", 8) == 0)
2632 GLRO(dl_lazy) = envline[9] == '\0';
2633 break;
2635 if (memcmp (envline, "BIND_NOT", 8) == 0)
2636 GLRO(dl_bind_not) = envline[9] != '\0';
2637 break;
2639 case 9:
2640 /* Test whether we want to see the content of the auxiliary
2641 array passed up from the kernel. */
2642 if (!__libc_enable_secure
2643 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2644 _dl_show_auxv ();
2645 break;
2647 #if !HAVE_TUNABLES
2648 case 10:
2649 /* Mask for the important hardware capabilities. */
2650 if (!__libc_enable_secure
2651 && memcmp (envline, "HWCAP_MASK", 10) == 0)
2652 GLRO(dl_hwcap_mask) = _dl_strtoul (&envline[11], NULL);
2653 break;
2654 #endif
2656 case 11:
2657 /* Path where the binary is found. */
2658 if (!__libc_enable_secure
2659 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2660 GLRO(dl_origin_path) = &envline[12];
2661 break;
2663 case 12:
2664 /* The library search path. */
2665 if (!__libc_enable_secure
2666 && memcmp (envline, "LIBRARY_PATH", 12) == 0)
2668 library_path = &envline[13];
2669 break;
2672 /* Where to place the profiling data file. */
2673 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2675 debug_output = &envline[13];
2676 break;
2679 if (!__libc_enable_secure
2680 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2681 GLRO(dl_dynamic_weak) = 1;
2682 break;
2684 case 13:
2685 /* We might have some extra environment variable with length 13
2686 to handle. */
2687 #ifdef EXTRA_LD_ENVVARS_13
2688 EXTRA_LD_ENVVARS_13
2689 #endif
2690 if (!__libc_enable_secure
2691 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2693 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2694 break;
2696 break;
2698 case 14:
2699 /* Where to place the profiling data file. */
2700 if (!__libc_enable_secure
2701 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2702 && envline[15] != '\0')
2703 GLRO(dl_profile_output) = &envline[15];
2704 break;
2706 case 16:
2707 /* The mode of the dynamic linker can be set. */
2708 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2710 mode = trace;
2711 GLRO(dl_verbose) = 1;
2712 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2713 GLRO(dl_trace_prelink) = &envline[17];
2715 break;
2717 case 20:
2718 /* The mode of the dynamic linker can be set. */
2719 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2720 mode = trace;
2721 break;
2723 /* We might have some extra environment variable to handle. This
2724 is tricky due to the pre-processing of the length of the name
2725 in the switch statement here. The code here assumes that added
2726 environment variables have a different length. */
2727 #ifdef EXTRA_LD_ENVVARS
2728 EXTRA_LD_ENVVARS
2729 #endif
2733 /* The caller wants this information. */
2734 *modep = mode;
2736 /* Extra security for SUID binaries. Remove all dangerous environment
2737 variables. */
2738 if (__builtin_expect (__libc_enable_secure, 0))
2740 static const char unsecure_envvars[] =
2741 #ifdef EXTRA_UNSECURE_ENVVARS
2742 EXTRA_UNSECURE_ENVVARS
2743 #endif
2744 UNSECURE_ENVVARS;
2745 const char *nextp;
2747 nextp = unsecure_envvars;
2750 unsetenv (nextp);
2751 /* We could use rawmemchr but this need not be fast. */
2752 nextp = (char *) (strchr) (nextp, '\0') + 1;
2754 while (*nextp != '\0');
2756 if (__access ("/etc/suid-debug", F_OK) != 0)
2758 #if !HAVE_TUNABLES
2759 unsetenv ("MALLOC_CHECK_");
2760 #endif
2761 GLRO(dl_debug_mask) = 0;
2764 if (mode != normal)
2765 _exit (5);
2767 /* If we have to run the dynamic linker in debugging mode and the
2768 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2769 messages to this file. */
2770 else if (any_debug && debug_output != NULL)
2772 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2773 size_t name_len = strlen (debug_output);
2774 char buf[name_len + 12];
2775 char *startp;
2777 buf[name_len + 11] = '\0';
2778 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2779 *--startp = '.';
2780 startp = memcpy (startp - name_len, debug_output, name_len);
2782 GLRO(dl_debug_fd) = __open64_nocancel (startp, flags, DEFFILEMODE);
2783 if (GLRO(dl_debug_fd) == -1)
2784 /* We use standard output if opening the file failed. */
2785 GLRO(dl_debug_fd) = STDOUT_FILENO;
2789 #if HP_TIMING_INLINE
2790 static void
2791 print_statistics_item (const char *title, hp_timing_t time,
2792 hp_timing_t total)
2794 char cycles[HP_TIMING_PRINT_SIZE];
2795 HP_TIMING_PRINT (cycles, sizeof (cycles), time);
2797 char relative[3 * sizeof (hp_timing_t) + 2];
2798 char *cp = _itoa ((1000ULL * time) / total, relative + sizeof (relative),
2799 10, 0);
2800 /* Sets the decimal point. */
2801 char *wp = relative;
2802 switch (relative + sizeof (relative) - cp)
2804 case 3:
2805 *wp++ = *cp++;
2806 /* Fall through. */
2807 case 2:
2808 *wp++ = *cp++;
2809 /* Fall through. */
2810 case 1:
2811 *wp++ = '.';
2812 *wp++ = *cp++;
2814 *wp = '\0';
2815 _dl_debug_printf ("%s: %s cycles (%s%%)\n", title, cycles, relative);
2817 #endif
2819 /* Print the various times we collected. */
2820 static void
2821 __attribute ((noinline))
2822 print_statistics (const hp_timing_t *rtld_total_timep)
2824 #if HP_TIMING_INLINE
2826 char cycles[HP_TIMING_PRINT_SIZE];
2827 HP_TIMING_PRINT (cycles, sizeof (cycles), *rtld_total_timep);
2828 _dl_debug_printf ("\nruntime linker statistics:\n"
2829 " total startup time in dynamic loader: %s cycles\n",
2830 cycles);
2831 print_statistics_item (" time needed for relocation",
2832 relocate_time, *rtld_total_timep);
2834 #endif
2836 unsigned long int num_relative_relocations = 0;
2837 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2839 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2840 continue;
2842 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2844 for (unsigned int i = 0; i < scope->r_nlist; i++)
2846 struct link_map *l = scope->r_list [i];
2848 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2849 num_relative_relocations
2850 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2851 #ifndef ELF_MACHINE_REL_RELATIVE
2852 /* Relative relocations are processed on these architectures if
2853 library is loaded to different address than p_vaddr or
2854 if not prelinked. */
2855 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2856 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2857 #else
2858 /* On e.g. IA-64 or Alpha, relative relocations are processed
2859 only if library is loaded to different address than p_vaddr. */
2860 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2861 #endif
2862 num_relative_relocations
2863 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2867 _dl_debug_printf (" number of relocations: %lu\n"
2868 " number of relocations from cache: %lu\n"
2869 " number of relative relocations: %lu\n",
2870 GL(dl_num_relocations),
2871 GL(dl_num_cache_relocations),
2872 num_relative_relocations);
2874 #if HP_TIMING_INLINE
2875 print_statistics_item (" time needed to load objects",
2876 load_time, *rtld_total_timep);
2877 #endif