1 /* Run time dynamic linker.
2 Copyright (C) 1995-2017 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 <http://www.gnu.org/licenses/>. */
27 #include <sys/param.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>
39 #include <dl-osinfo.h>
40 #include <dl-procinfo.h>
42 #include <stap-probe.h>
43 #include <stackinfo.h>
47 /* Avoid PLT use for our local calls at startup. */
48 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
50 /* GCC has mental blocks about _exit. */
51 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
52 #define _exit exit_internal
54 /* Helper function to handle errors while resolving symbols. */
55 static void print_unresolved (int errcode, const char *objname,
56 const char *errsting);
58 /* Helper function to handle errors when a version is missing. */
59 static void print_missing_version (int errcode, const char *objname,
60 const char *errsting);
62 /* Print the various times we collected. */
63 static void print_statistics (hp_timing_t *total_timep);
65 /* Add audit objects. */
66 static void process_dl_audit (char *str);
68 /* This is a list of all the modes the dynamic loader can be in. */
69 enum mode { normal, list, verify, trace };
71 /* Process all environments variables the dynamic linker must recognize.
72 Since all of them start with `LD_' we are a bit smarter while finding
74 static void process_envvars (enum mode *modep);
76 #ifdef DL_ARGV_NOT_RELRO
77 int _dl_argc attribute_hidden;
78 char **_dl_argv = NULL;
79 /* Nonzero if we were run directly. */
80 unsigned int _dl_skip_args attribute_hidden;
82 int _dl_argc attribute_relro attribute_hidden;
83 char **_dl_argv attribute_relro = NULL;
84 unsigned int _dl_skip_args attribute_relro attribute_hidden;
86 rtld_hidden_data_def (_dl_argv)
88 #ifndef THREAD_SET_STACK_GUARD
89 /* Only exported for architectures that don't store the stack guard canary
90 in thread local area. */
91 uintptr_t __stack_chk_guard attribute_relro;
94 /* Only exported for architectures that don't store the pointer guard
95 value in thread local area. */
96 uintptr_t __pointer_chk_guard_local
97 attribute_relro attribute_hidden __attribute__ ((nocommon));
98 #ifndef THREAD_SET_POINTER_GUARD
99 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
103 /* List of auditing DSOs. */
104 static struct audit_list
107 struct audit_list *next;
110 #ifndef HAVE_INLINED_SYSCALLS
111 /* Set nonzero during loading and initialization of executable and
112 libraries, cleared before the executable's entry point runs. This
113 must not be initialized to nonzero, because the unused dynamic
114 linker loaded in for libc.so's "ld.so.1" dep will provide the
115 definition seen by libc.so's initializer; that value must be zero,
116 and will be since that dynamic linker's _dl_start and dl_main will
118 int _dl_starting_up = 0;
119 rtld_hidden_def (_dl_starting_up)
122 /* This is the structure which defines all variables global to ld.so
123 (except those which cannot be added for some reason). */
124 struct rtld_global _rtld_global =
126 /* Generally the default presumption without further information is an
127 * executable stack but this is not true for all platforms. */
128 ._dl_stack_flags = DEFAULT_STACK_PERMS,
129 #ifdef _LIBC_REENTRANT
130 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
131 ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
136 #ifdef _LIBC_REENTRANT
137 [LM_ID_BASE] = { ._ns_unique_sym_table
138 = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } }
142 /* If we would use strong_alias here the compiler would see a
143 non-hidden definition. This would undo the effect of the previous
144 declaration. So spell out was strong_alias does plus add the
145 visibility attribute. */
146 extern struct rtld_global _rtld_local
147 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
150 /* This variable is similar to _rtld_local, but all values are
151 read-only after relocation. */
152 struct rtld_global_ro _rtld_global_ro attribute_relro =
154 /* Get architecture specific initializer. */
155 #include <dl-procinfo.c>
156 #ifdef NEED_DL_SYSINFO
157 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
159 ._dl_debug_fd = STDERR_FILENO,
160 ._dl_use_load_bias = -2,
161 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
163 ._dl_hwcap_mask = HWCAP_IMPORTANT,
166 ._dl_fpu_control = _FPU_DEFAULT,
167 ._dl_pagesize = EXEC_PAGESIZE,
168 ._dl_inhibit_cache = 0,
170 /* Function pointers. */
171 ._dl_debug_printf = _dl_debug_printf,
172 ._dl_mcount = _dl_mcount,
173 ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
174 ._dl_check_caller = _dl_check_caller,
175 ._dl_open = _dl_open,
176 ._dl_close = _dl_close,
177 ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
178 #ifdef HAVE_DL_DISCOVER_OSVERSION
179 ._dl_discover_osversion = _dl_discover_osversion
182 /* If we would use strong_alias here the compiler would see a
183 non-hidden definition. This would undo the effect of the previous
184 declaration. So spell out was strong_alias does plus add the
185 visibility attribute. */
186 extern struct rtld_global_ro _rtld_local_ro
187 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
190 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
191 ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv);
193 /* These two variables cannot be moved into .data.rel.ro. */
194 static struct libname_list _dl_rtld_libname;
195 static struct libname_list _dl_rtld_libname2;
197 /* Variable for statistics. */
198 #ifndef HP_TIMING_NONAVAIL
199 static hp_timing_t relocate_time;
200 static hp_timing_t load_time attribute_relro;
201 static hp_timing_t start_time attribute_relro;
204 /* Additional definitions needed by TLS initialization. */
205 #ifdef TLS_INIT_HELPER
209 /* Helper function for syscall implementation. */
210 #ifdef DL_SYSINFO_IMPLEMENTATION
211 DL_SYSINFO_IMPLEMENTATION
214 /* Before ld.so is relocated we must not access variables which need
215 relocations. This means variables which are exported. Variables
216 declared as static are fine. If we can mark a variable hidden this
217 is fine, too. The latter is important here. We can avoid setting
218 up a temporary link map for ld.so if we can mark _rtld_global as
220 #ifdef PI_STATIC_AND_HIDDEN
221 # define DONT_USE_BOOTSTRAP_MAP 1
224 #ifdef DONT_USE_BOOTSTRAP_MAP
225 static ElfW(Addr) _dl_start_final (void *arg);
227 struct dl_start_final_info
230 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
231 hp_timing_t start_time;
234 static ElfW(Addr) _dl_start_final (void *arg,
235 struct dl_start_final_info *info);
238 /* These defined magically in the linker script. */
239 extern char _begin[] attribute_hidden;
240 extern char _etext[] attribute_hidden;
241 extern char _end[] attribute_hidden;
247 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
250 /* This is the second half of _dl_start (below). It can be inlined safely
251 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
252 references. When the tools don't permit us to avoid using a GOT entry
253 for _dl_rtld_global (no attribute_hidden support), we must make sure
254 this function is not inlined (see below). */
256 #ifdef DONT_USE_BOOTSTRAP_MAP
257 static inline ElfW(Addr) __attribute__ ((always_inline))
258 _dl_start_final (void *arg)
260 static ElfW(Addr) __attribute__ ((noinline))
261 _dl_start_final (void *arg, struct dl_start_final_info *info)
264 ElfW(Addr) start_addr;
266 if (HP_SMALL_TIMING_AVAIL)
268 /* If it hasn't happen yet record the startup time. */
269 if (! HP_TIMING_INLINE)
270 HP_TIMING_NOW (start_time);
271 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
273 start_time = info->start_time;
277 /* Transfer data about ourselves to the permanent link_map structure. */
278 #ifndef DONT_USE_BOOTSTRAP_MAP
279 GL(dl_rtld_map).l_addr = info->l.l_addr;
280 GL(dl_rtld_map).l_ld = info->l.l_ld;
281 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
282 sizeof GL(dl_rtld_map).l_info);
283 GL(dl_rtld_map).l_mach = info->l.l_mach;
284 GL(dl_rtld_map).l_relocated = 1;
286 _dl_setup_hash (&GL(dl_rtld_map));
287 GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
288 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
289 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
290 GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
291 /* Copy the TLS related data if necessary. */
292 #ifndef DONT_USE_BOOTSTRAP_MAP
293 # if NO_TLS_OFFSET != 0
294 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
298 HP_TIMING_NOW (GL(dl_cpuclock_offset));
300 /* Initialize the stack end variable. */
301 __libc_stack_end = __builtin_frame_address (0);
303 /* Call the OS-dependent function to set up life so we can do things like
304 file access. It will call `dl_main' (below) to do all the real work
305 of the dynamic linker, and then unwind our frame and run the user
306 entry point on the same stack we entered on. */
307 start_addr = _dl_sysdep_start (arg, &dl_main);
309 #ifndef HP_TIMING_NONAVAIL
310 hp_timing_t rtld_total_time;
311 if (HP_SMALL_TIMING_AVAIL)
313 hp_timing_t end_time;
315 /* Get the current time. */
316 HP_TIMING_NOW (end_time);
318 /* Compute the difference. */
319 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
323 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
325 #ifndef HP_TIMING_NONAVAIL
326 print_statistics (&rtld_total_time);
328 print_statistics (NULL);
335 static ElfW(Addr) __attribute_used__ internal_function
336 _dl_start (void *arg)
338 #ifdef DONT_USE_BOOTSTRAP_MAP
339 # define bootstrap_map GL(dl_rtld_map)
341 struct dl_start_final_info info;
342 # define bootstrap_map info.l
345 /* This #define produces dynamic linking inline functions for
346 bootstrap relocation instead of general-purpose relocation.
347 Since ld.so must not have any undefined symbols the result
348 is trivial: always the map of ld.so itself. */
349 #define RTLD_BOOTSTRAP
350 #define RESOLVE_MAP(sym, version, flags) (&bootstrap_map)
351 #include "dynamic-link.h"
353 if (HP_TIMING_INLINE && HP_SMALL_TIMING_AVAIL)
354 #ifdef DONT_USE_BOOTSTRAP_MAP
355 HP_TIMING_NOW (start_time);
357 HP_TIMING_NOW (info.start_time);
360 /* Partly clean the `bootstrap_map' structure up. Don't use
361 `memset' since it might not be built in or inlined and we cannot
362 make function calls at this point. Use '__builtin_memset' if we
363 know it is available. We do not have to clear the memory if we
364 do not have to use the temporary bootstrap_map. Global variables
365 are initialized to zero by default. */
366 #ifndef DONT_USE_BOOTSTRAP_MAP
367 # ifdef HAVE_BUILTIN_MEMSET
368 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
371 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
373 bootstrap_map.l_info[cnt] = 0;
377 /* Figure out the run-time load address of the dynamic linker itself. */
378 bootstrap_map.l_addr = elf_machine_load_address ();
380 /* Read our own dynamic section and fill in the info array. */
381 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
382 elf_get_dynamic_info (&bootstrap_map, NULL);
384 #if NO_TLS_OFFSET != 0
385 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
388 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
389 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
392 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
394 /* Relocate ourselves so we can do normal function calls and
395 data access using the global offset table. */
397 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0, 0);
399 bootstrap_map.l_relocated = 1;
401 /* Please note that we don't allow profiling of this object and
402 therefore need not test whether we have to allocate the array
403 for the relocation results (as done in dl-reloc.c). */
405 /* Now life is sane; we can call functions and access global data.
406 Set up to use the operating system facilities, and find out from
407 the operating system's program loader where to find the program
408 header table in core. Put the rest of _dl_start into a separate
409 function, that way the compiler cannot put accesses to the GOT
410 before ELF_DYNAMIC_RELOCATE. */
412 #ifdef DONT_USE_BOOTSTRAP_MAP
413 ElfW(Addr) entry = _dl_start_final (arg);
415 ElfW(Addr) entry = _dl_start_final (arg, &info);
418 #ifndef ELF_MACHINE_START_ADDRESS
419 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
422 return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
428 /* Now life is peachy; we can do all normal operations.
429 On to the real work. */
431 /* Some helper functions. */
433 /* Arguments to relocate_doit. */
442 /* Argument to map_doit. */
444 struct link_map *loader;
446 /* Return value of map_doit. */
447 struct link_map *map;
453 struct link_map *map;
459 struct link_map *map;
463 /* Arguments to version_check_doit. */
464 struct version_check_args
471 relocate_doit (void *a)
473 struct relocate_args *args = (struct relocate_args *) a;
475 _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0);
481 struct map_args *args = (struct map_args *) a;
482 int type = (args->mode == __RTLD_OPENEXEC) ? lt_executable : lt_library;
483 args->map = _dl_map_object (args->loader, args->str, type, 0,
484 args->mode, LM_ID_BASE);
488 dlmopen_doit (void *a)
490 struct dlmopen_args *args = (struct dlmopen_args *) a;
491 args->map = _dl_open (args->fname,
492 (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT
494 dl_main, LM_ID_NEWLM, _dl_argc, _dl_argv,
499 lookup_doit (void *a)
501 struct lookup_args *args = (struct lookup_args *) a;
502 const ElfW(Sym) *ref = NULL;
504 lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
505 args->map->l_local_scope, NULL, 0,
506 DL_LOOKUP_RETURN_NEWEST, NULL);
508 args->result = DL_SYMBOL_ADDRESS (l, ref);
512 version_check_doit (void *a)
514 struct version_check_args *args = (struct version_check_args *) a;
515 if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
516 args->dotrace) && args->doexit)
517 /* We cannot start the application. Abort now. */
522 static inline struct link_map *
523 find_needed (const char *name)
525 struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
526 unsigned int n = scope->r_nlist;
529 if (_dl_name_match_p (name, scope->r_list[n]))
530 return scope->r_list[n];
532 /* Should never happen. */
537 match_version (const char *string, struct link_map *map)
539 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
542 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
543 if (map->l_info[VERDEFTAG] == NULL)
544 /* The file has no symbol versioning. */
547 def = (ElfW(Verdef) *) ((char *) map->l_addr
548 + map->l_info[VERDEFTAG]->d_un.d_ptr);
551 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
553 /* Compare the version strings. */
554 if (strcmp (string, strtab + aux->vda_name) == 0)
558 /* If no more definitions we failed to find what we want. */
559 if (def->vd_next == 0)
562 /* Next definition. */
563 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
569 static bool tls_init_tp_called;
574 /* Number of elements in the static TLS block. */
575 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
577 /* Do not do this twice. The audit interface might have required
578 the DTV interfaces to be set up early. */
579 if (GL(dl_initial_dtv) != NULL)
582 /* Allocate the array which contains the information about the
583 dtv slots. We allocate a few entries more than needed to
584 avoid the need for reallocation. */
585 size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
588 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
589 calloc (sizeof (struct dtv_slotinfo_list)
590 + nelem * sizeof (struct dtv_slotinfo), 1);
591 /* No need to check the return value. If memory allocation failed
592 the program would have been terminated. */
594 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
595 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
596 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
598 /* Fill in the information from the loaded modules. No namespace
599 but the base one can be filled at this time. */
600 assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
602 for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
604 if (l->l_tls_blocksize != 0)
606 /* This is a module with TLS data. Store the map reference.
607 The generation counter is zero. */
609 /* slotinfo[i].gen = 0; */
612 assert (i == GL(dl_tls_max_dtv_idx));
614 /* Compute the TLS offsets for the various blocks. */
615 _dl_determine_tlsoffset ();
617 /* Construct the static TLS block and the dtv for the initial
618 thread. For some platforms this will include allocating memory
619 for the thread descriptor. The memory for the TLS block will
620 never be freed. It should be allocated accordingly. The dtv
621 array can be changed if dynamic loading requires it. */
622 void *tcbp = _dl_allocate_tls_storage ();
625 cannot allocate TLS data structures for initial thread");
627 /* Store for detection of the special case by __tls_get_addr
628 so it knows not to pass this dtv to the normal realloc. */
629 GL(dl_initial_dtv) = GET_DTV (tcbp);
631 /* And finally install it for the main thread. */
632 const char *lossage = TLS_INIT_TP (tcbp);
633 if (__glibc_unlikely (lossage != NULL))
634 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
635 tls_init_tp_called = true;
641 do_preload (const char *fname, struct link_map *main_map, const char *where)
644 const char *err_str = NULL;
645 struct map_args args;
649 args.loader = main_map;
650 args.mode = __RTLD_SECURE;
652 unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
654 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
655 if (__glibc_unlikely (err_str != NULL))
658 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
659 fname, where, err_str);
660 /* No need to call free, this is still before
661 the libc's malloc is used. */
663 else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
664 /* It is no duplicate. */
667 /* Nothing loaded. */
671 #if defined SHARED && defined _LIBC_REENTRANT \
672 && defined __rtld_lock_default_lock_recursive
674 rtld_lock_default_lock_recursive (void *lock)
676 __rtld_lock_default_lock_recursive (lock);
680 rtld_lock_default_unlock_recursive (void *lock)
682 __rtld_lock_default_unlock_recursive (lock);
690 /* Set up the stack checker's canary. */
691 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
692 #ifdef THREAD_SET_STACK_GUARD
693 THREAD_SET_STACK_GUARD (stack_chk_guard);
695 __stack_chk_guard = stack_chk_guard;
698 /* Set up the pointer guard as well, if necessary. */
699 uintptr_t pointer_chk_guard
700 = _dl_setup_pointer_guard (_dl_random, stack_chk_guard);
701 #ifdef THREAD_SET_POINTER_GUARD
702 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
704 __pointer_chk_guard_local = pointer_chk_guard;
706 /* We do not need the _dl_random value anymore. The less
707 information we leave behind, the better, so clear the
712 #include "setup-vdso.h"
714 /* The library search path. */
715 static const char *library_path attribute_relro;
716 /* The list preloaded objects. */
717 static const char *preloadlist attribute_relro;
718 /* Nonzero if information about versions has to be printed. */
719 static int version_info attribute_relro;
722 dl_main (const ElfW(Phdr) *phdr,
724 ElfW(Addr) *user_entry,
727 const ElfW(Phdr) *ph;
729 struct link_map *main_map;
732 bool has_interp = false;
734 bool prelinked = false;
735 bool rtld_is_main = false;
736 #ifndef HP_TIMING_NONAVAIL
743 GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
745 #if defined SHARED && defined _LIBC_REENTRANT \
746 && defined __rtld_lock_default_lock_recursive
747 GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
748 GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
751 /* The explicit initialization here is cheaper than processing the reloc
752 in the _rtld_local definition's initializer. */
753 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
755 /* Process the environment variable which control the behaviour. */
756 process_envvars (&mode);
758 #ifndef HAVE_INLINED_SYSCALLS
759 /* Set up a flag which tells we are just starting. */
763 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
765 /* Ho ho. We are not the program interpreter! We are the program
766 itself! This means someone ran ld.so as a command. Well, that
767 might be convenient to do sometimes. We support it by
768 interpreting the args like this:
770 ld.so PROGRAM ARGS...
772 The first argument is the name of a file containing an ELF
773 executable we will load and run with the following arguments.
774 To simplify life here, PROGRAM is searched for using the
775 normal rules for shared objects, rather than $PATH or anything
776 like that. We just load it and use its entry point; we don't
777 pay attention to its PT_INTERP command (we are the interpreter
778 ourselves). This is an easy way to test a new ld.so before
782 /* Note the place where the dynamic linker actually came from. */
783 GL(dl_rtld_map).l_name = rtld_progname;
786 if (! strcmp (_dl_argv[1], "--list"))
789 GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */
795 else if (! strcmp (_dl_argv[1], "--verify"))
803 else if (! strcmp (_dl_argv[1], "--inhibit-cache"))
805 GLRO(dl_inhibit_cache) = 1;
810 else if (! strcmp (_dl_argv[1], "--library-path")
813 library_path = _dl_argv[2];
819 else if (! strcmp (_dl_argv[1], "--inhibit-rpath")
822 GLRO(dl_inhibit_rpath) = _dl_argv[2];
828 else if (! strcmp (_dl_argv[1], "--audit") && _dl_argc > 2)
830 process_dl_audit (_dl_argv[2]);
839 /* If we have no further argument the program was called incorrectly.
840 Grant the user some education. */
843 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
844 You have invoked `ld.so', the helper program for shared library executables.\n\
845 This program usually lives in the file `/lib/ld.so', and special directives\n\
846 in executable files using ELF shared libraries tell the system's program\n\
847 loader to load the helper program from this file. This helper program loads\n\
848 the shared libraries needed by the program executable, prepares the program\n\
849 to run, and runs it. You may invoke this helper program directly from the\n\
850 command line to load and run an ELF executable file; this is like executing\n\
851 that file itself, but always uses this helper program from the file you\n\
852 specified, instead of the helper program file specified in the executable\n\
853 file you run. This is mostly of use for maintainers to test new versions\n\
854 of this helper program; chances are you did not intend to run this program.\n\
856 --list list all dependencies and how they are resolved\n\
857 --verify verify that given object really is a dynamically linked\n\
858 object we can handle\n\
859 --inhibit-cache Do not use " LD_SO_CACHE "\n\
860 --library-path PATH use given PATH instead of content of the environment\n\
861 variable LD_LIBRARY_PATH\n\
862 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
864 --audit LIST use objects named in LIST as auditors\n");
870 /* The initialization of _dl_stack_flags done below assumes the
871 executable's PT_GNU_STACK may have been honored by the kernel, and
872 so a PT_GNU_STACK with PF_X set means the stack started out with
873 execute permission. However, this is not really true if the
874 dynamic linker is the executable the kernel loaded. For this
875 case, we must reinitialize _dl_stack_flags to match the dynamic
876 linker itself. If the dynamic linker was built with a
877 PT_GNU_STACK, then the kernel may have loaded us with a
878 nonexecutable stack that we will have to make executable when we
879 load the program below unless it has a PT_GNU_STACK indicating
880 nonexecutable stack is ok. */
882 for (ph = phdr; ph < &phdr[phnum]; ++ph)
883 if (ph->p_type == PT_GNU_STACK)
885 GL(dl_stack_flags) = ph->p_flags;
889 if (__builtin_expect (mode, normal) == verify)
892 const char *err_str = NULL;
893 struct map_args args;
896 args.str = rtld_progname;
898 args.mode = __RTLD_OPENEXEC;
899 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
901 if (__glibc_unlikely (err_str != NULL))
902 /* We don't free the returned string, the programs stops
904 _exit (EXIT_FAILURE);
908 HP_TIMING_NOW (start);
909 _dl_map_object (NULL, rtld_progname, lt_executable, 0,
910 __RTLD_OPENEXEC, LM_ID_BASE);
911 HP_TIMING_NOW (stop);
913 HP_TIMING_DIFF (load_time, start, stop);
916 /* Now the map for the main executable is available. */
917 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
919 if (__builtin_expect (mode, normal) == normal
920 && GL(dl_rtld_map).l_info[DT_SONAME] != NULL
921 && main_map->l_info[DT_SONAME] != NULL
922 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
923 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
924 (const char *) D_PTR (main_map, l_info[DT_STRTAB])
925 + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
926 _dl_fatal_printf ("loader cannot load itself\n");
928 phdr = main_map->l_phdr;
929 phnum = main_map->l_phnum;
930 /* We overwrite here a pointer to a malloc()ed string. But since
931 the malloc() implementation used at this point is the dummy
932 implementations which has no real free() function it does not
933 makes sense to free the old string first. */
934 main_map->l_name = (char *) "";
935 *user_entry = main_map->l_entry;
937 #ifdef HAVE_AUX_VECTOR
938 /* Adjust the on-stack auxiliary vector so that it looks like the
939 binary was executed directly. */
940 for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
944 av->a_un.a_val = (uintptr_t) phdr;
947 av->a_un.a_val = phnum;
950 av->a_un.a_val = *user_entry;
953 av->a_un.a_val = (uintptr_t) _dl_argv[0];
960 /* Create a link_map for the executable itself.
961 This will be what dlopen on "" returns. */
962 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
963 __RTLD_OPENEXEC, LM_ID_BASE);
964 assert (main_map != NULL);
965 main_map->l_phdr = phdr;
966 main_map->l_phnum = phnum;
967 main_map->l_entry = *user_entry;
969 /* Even though the link map is not yet fully initialized we can add
970 it to the map list since there are no possible users running yet. */
971 _dl_add_to_namespace_list (main_map, LM_ID_BASE);
972 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
974 /* At this point we are in a bit of trouble. We would have to
975 fill in the values for l_dev and l_ino. But in general we
976 do not know where the file is. We also do not handle AT_EXECFD
977 even if it would be passed up.
979 We leave the values here defined to 0. This is normally no
980 problem as the program code itself is normally no shared
981 object and therefore cannot be loaded dynamically. Nothing
982 prevent the use of dynamic binaries and in these situations
983 we might get problems. We might not be able to find out
984 whether the object is already loaded. But since there is no
985 easy way out and because the dynamic binary must also not
986 have an SONAME we ignore this program for now. If it becomes
987 a problem we can force people using SONAMEs. */
989 /* We delay initializing the path structure until we got the dynamic
990 information for the program. */
993 main_map->l_map_end = 0;
994 main_map->l_text_end = 0;
995 /* Perhaps the executable has no PT_LOAD header entries at all. */
996 main_map->l_map_start = ~0;
997 /* And it was opened directly. */
998 ++main_map->l_direct_opencount;
1000 /* Scan the program header table for the dynamic section. */
1001 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1005 /* Find out the load address. */
1006 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1009 /* This tells us where to find the dynamic section,
1010 which tells us everything we need to do. */
1011 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1014 /* This "interpreter segment" was used by the program loader to
1015 find the program interpreter, which is this program itself, the
1016 dynamic linker. We note what name finds us, so that a future
1017 dlopen call or DT_NEEDED entry, for something that wants to link
1018 against the dynamic linker as a shared library, will know that
1019 the shared object is already loaded. */
1020 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1022 /* _dl_rtld_libname.next = NULL; Already zero. */
1023 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1025 /* Ordinarilly, we would get additional names for the loader from
1026 our DT_SONAME. This can't happen if we were actually linked as
1027 a static executable (detect this case when we have no DYNAMIC).
1028 If so, assume the filename component of the interpreter path to
1029 be our SONAME, and add it to our name list. */
1030 if (GL(dl_rtld_map).l_ld == NULL)
1032 const char *p = NULL;
1033 const char *cp = _dl_rtld_libname.name;
1035 /* Find the filename part of the path. */
1042 _dl_rtld_libname2.name = p;
1043 /* _dl_rtld_libname2.next = NULL; Already zero. */
1044 _dl_rtld_libname.next = &_dl_rtld_libname2;
1052 ElfW(Addr) mapstart;
1053 ElfW(Addr) allocend;
1055 /* Remember where the main program starts in memory. */
1056 mapstart = (main_map->l_addr
1057 + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1058 if (main_map->l_map_start > mapstart)
1059 main_map->l_map_start = mapstart;
1061 /* Also where it ends. */
1062 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1063 if (main_map->l_map_end < allocend)
1064 main_map->l_map_end = allocend;
1065 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1066 main_map->l_text_end = allocend;
1071 if (ph->p_memsz > 0)
1073 /* Note that in the case the dynamic linker we duplicate work
1074 here since we read the PT_TLS entry already in
1075 _dl_start_final. But the result is repeatable so do not
1076 check for this special but unimportant case. */
1077 main_map->l_tls_blocksize = ph->p_memsz;
1078 main_map->l_tls_align = ph->p_align;
1079 if (ph->p_align == 0)
1080 main_map->l_tls_firstbyte_offset = 0;
1082 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1083 & (ph->p_align - 1));
1084 main_map->l_tls_initimage_size = ph->p_filesz;
1085 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1087 /* This image gets the ID one. */
1088 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1093 GL(dl_stack_flags) = ph->p_flags;
1097 main_map->l_relro_addr = ph->p_vaddr;
1098 main_map->l_relro_size = ph->p_memsz;
1102 /* Adjust the address of the TLS initialization image in case
1103 the executable is actually an ET_DYN object. */
1104 if (main_map->l_tls_initimage != NULL)
1105 main_map->l_tls_initimage
1106 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1107 if (! main_map->l_map_end)
1108 main_map->l_map_end = ~0;
1109 if (! main_map->l_text_end)
1110 main_map->l_text_end = ~0;
1111 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1113 /* We were invoked directly, so the program might not have a
1115 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1116 /* _dl_rtld_libname.next = NULL; Already zero. */
1117 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1120 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1122 /* If the current libname is different from the SONAME, add the
1124 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1125 && strcmp (GL(dl_rtld_map).l_libname->name,
1126 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1127 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1129 static struct libname_list newname;
1130 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1131 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1132 newname.next = NULL;
1133 newname.dont_free = 1;
1135 assert (GL(dl_rtld_map).l_libname->next == NULL);
1136 GL(dl_rtld_map).l_libname->next = &newname;
1138 /* The ld.so must be relocated since otherwise loading audit modules
1139 will fail since they reuse the very same ld.so. */
1140 assert (GL(dl_rtld_map).l_relocated);
1144 /* Extract the contents of the dynamic section for easy access. */
1145 elf_get_dynamic_info (main_map, NULL);
1146 /* Set up our cache of pointers into the hash table. */
1147 _dl_setup_hash (main_map);
1150 if (__builtin_expect (mode, normal) == verify)
1152 /* We were called just to verify that this is a dynamic
1153 executable using us as the program interpreter. Exit with an
1154 error if we were not able to load the binary or no interpreter
1155 is specified (i.e., this is no dynamically linked binary. */
1156 if (main_map->l_ld == NULL)
1159 /* We allow here some platform specific code. */
1160 #ifdef DISTINGUISH_LIB_VERSIONS
1161 DISTINGUISH_LIB_VERSIONS;
1163 _exit (has_interp ? 0 : 2);
1166 struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1167 /* Set up the data structures for the system-supplied DSO early,
1168 so they can influence _dl_init_paths. */
1169 setup_vdso (main_map, &first_preload);
1171 #ifdef DL_SYSDEP_OSCHECK
1172 DL_SYSDEP_OSCHECK (_dl_fatal_printf);
1175 /* Initialize the data structures for the search paths for shared
1177 _dl_init_paths (library_path);
1179 /* Initialize _r_debug. */
1180 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1182 r->r_state = RT_CONSISTENT;
1184 /* Put the link_map for ourselves on the chain so it can be found by
1185 name. Note that at this point the global chain of link maps contains
1186 exactly one element, which is pointed to by dl_loaded. */
1187 if (! GL(dl_rtld_map).l_name)
1188 /* If not invoked directly, the dynamic linker shared object file was
1189 found by the PT_INTERP name. */
1190 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1191 GL(dl_rtld_map).l_type = lt_library;
1192 main_map->l_next = &GL(dl_rtld_map);
1193 GL(dl_rtld_map).l_prev = main_map;
1194 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1197 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1198 to not using bias for non-prelinked PIEs and libraries
1199 and using it for executables or prelinked PIEs or libraries. */
1200 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1201 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1203 /* Set up the program header information for the dynamic linker
1204 itself. It is needed in the dl_iterate_phdr callbacks. */
1205 const ElfW(Ehdr) *rtld_ehdr;
1207 /* Starting from binutils-2.23, the linker will define the magic symbol
1208 __ehdr_start to point to our own ELF header if it is visible in a
1209 segment that also includes the phdrs. If that's not available, we use
1210 the old method that assumes the beginning of the file is part of the
1211 lowest-addressed PT_LOAD segment. */
1212 #ifdef HAVE_EHDR_START
1213 extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden")));
1214 rtld_ehdr = &__ehdr_start;
1216 rtld_ehdr = (void *) GL(dl_rtld_map).l_map_start;
1218 assert (rtld_ehdr->e_ehsize == sizeof *rtld_ehdr);
1219 assert (rtld_ehdr->e_phentsize == sizeof (ElfW(Phdr)));
1221 const ElfW(Phdr) *rtld_phdr = (const void *) rtld_ehdr + rtld_ehdr->e_phoff;
1223 GL(dl_rtld_map).l_phdr = rtld_phdr;
1224 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1227 /* PT_GNU_RELRO is usually the last phdr. */
1228 size_t cnt = rtld_ehdr->e_phnum;
1230 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1232 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1233 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1237 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1238 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1239 /* Assign a module ID. Do this before loading any audit modules. */
1240 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1242 /* If we have auditing DSOs to load, do it now. */
1243 if (__glibc_unlikely (audit_list != NULL))
1245 /* Iterate over all entries in the list. The order is important. */
1246 struct audit_ifaces *last_audit = NULL;
1247 struct audit_list *al = audit_list->next;
1249 /* Since we start using the auditing DSOs right away we need to
1250 initialize the data structures now. */
1253 /* Initialize security features. We need to do it this early
1254 since otherwise the constructors of the audit libraries will
1255 use different values (especially the pointer guard) and will
1261 int tls_idx = GL(dl_tls_max_dtv_idx);
1263 /* Now it is time to determine the layout of the static TLS
1264 block and allocate it for the initial thread. Note that we
1265 always allocate the static block, we never defer it even if
1266 no DF_STATIC_TLS bit is set. The reason is that we know
1267 glibc will use the static model. */
1268 struct dlmopen_args dlmargs;
1269 dlmargs.fname = al->name;
1272 const char *objname;
1273 const char *err_str = NULL;
1275 (void) _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit,
1277 if (__glibc_unlikely (err_str != NULL))
1280 _dl_error_printf ("\
1281 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
1284 free ((char *) err_str);
1288 struct lookup_args largs;
1289 largs.name = "la_version";
1290 largs.map = dlmargs.map;
1292 /* Check whether the interface version matches. */
1293 (void) _dl_catch_error (&objname, &err_str, &malloced,
1294 lookup_doit, &largs);
1296 unsigned int (*laversion) (unsigned int);
1299 && (laversion = largs.result) != NULL
1300 && (lav = laversion (LAV_CURRENT)) > 0
1301 && lav <= LAV_CURRENT)
1303 /* Allocate structure for the callback function pointers.
1304 This call can never fail. */
1307 struct audit_ifaces ifaces;
1308 #define naudit_ifaces 8
1309 void (*fptr[naudit_ifaces]) (void);
1310 } *newp = malloc (sizeof (*newp));
1312 /* Names of the auditing interfaces. All in one
1314 static const char audit_iface_names[] =
1319 #if __ELF_NATIVE_CLASS == 32
1321 #elif __ELF_NATIVE_CLASS == 64
1324 # error "__ELF_NATIVE_CLASS must be defined"
1326 #define STRING(s) __STRING (s)
1327 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1328 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1330 unsigned int cnt = 0;
1331 const char *cp = audit_iface_names;
1335 (void) _dl_catch_error (&objname, &err_str, &malloced,
1336 lookup_doit, &largs);
1338 /* Store the pointer. */
1339 if (err_str == NULL && largs.result != NULL)
1341 newp->fptr[cnt] = largs.result;
1343 /* The dynamic linker link map is statically
1344 allocated, initialize the data now. */
1345 GL(dl_rtld_map).l_audit[cnt].cookie
1346 = (intptr_t) &GL(dl_rtld_map);
1349 newp->fptr[cnt] = NULL;
1352 cp = (char *) rawmemchr (cp, '\0') + 1;
1354 while (*cp != '\0');
1355 assert (cnt == naudit_ifaces);
1357 /* Now append the new auditing interface to the list. */
1358 newp->ifaces.next = NULL;
1359 if (last_audit == NULL)
1360 last_audit = GLRO(dl_audit) = &newp->ifaces;
1362 last_audit = last_audit->next = &newp->ifaces;
1365 /* Mark the DSO as being used for auditing. */
1366 dlmargs.map->l_auditing = 1;
1370 /* We cannot use the DSO, it does not have the
1371 appropriate interfaces or it expects something
1374 Lmid_t ns = dlmargs.map->l_ns;
1376 _dl_close (dlmargs.map);
1378 /* Make sure the namespace has been cleared entirely. */
1379 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
1380 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
1382 GL(dl_tls_max_dtv_idx) = tls_idx;
1389 while (al != audit_list->next);
1391 /* If we have any auditing modules, announce that we already
1392 have two objects loaded. */
1393 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
1395 struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) };
1397 for (unsigned int outer = 0; outer < 2; ++outer)
1399 struct audit_ifaces *afct = GLRO(dl_audit);
1400 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1402 if (afct->objopen != NULL)
1404 ls[outer]->l_audit[cnt].bindflags
1405 = afct->objopen (ls[outer], LM_ID_BASE,
1406 &ls[outer]->l_audit[cnt].cookie);
1408 ls[outer]->l_audit_any_plt
1409 |= ls[outer]->l_audit[cnt].bindflags != 0;
1418 /* Keep track of the currently loaded modules to count how many
1419 non-audit modules which use TLS are loaded. */
1420 size_t count_modids = _dl_count_modids ();
1422 /* Set up debugging before the debugger is notified for the first time. */
1423 #ifdef ELF_MACHINE_DEBUG_SETUP
1424 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1425 ELF_MACHINE_DEBUG_SETUP (main_map, r);
1426 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1428 if (main_map->l_info[DT_DEBUG] != NULL)
1429 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1430 with the run-time address of the r_debug structure */
1431 main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1433 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1434 case you run gdb on the dynamic linker directly. */
1435 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1436 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1439 /* We start adding objects. */
1440 r->r_state = RT_ADD;
1442 LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
1444 /* Auditing checkpoint: we are ready to signal that the initial map
1445 is being constructed. */
1446 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
1448 struct audit_ifaces *afct = GLRO(dl_audit);
1449 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1451 if (afct->activity != NULL)
1452 afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD);
1458 /* We have two ways to specify objects to preload: via environment
1459 variable and via the file /etc/ld.so.preload. The latter can also
1460 be used when security is enabled. */
1461 assert (*first_preload == NULL);
1462 struct link_map **preloads = NULL;
1463 unsigned int npreloads = 0;
1465 if (__glibc_unlikely (preloadlist != NULL))
1467 /* The LD_PRELOAD environment variable gives list of libraries
1468 separated by white space or colons that are loaded before the
1469 executable's dependencies and prepended to the global scope
1470 list. If the binary is running setuid all elements
1471 containing a '/' are ignored since it is insecure. */
1472 char *list = strdupa (preloadlist);
1475 HP_TIMING_NOW (start);
1477 /* Prevent optimizing strsep. Speed is not important here. */
1478 while ((p = (strsep) (&list, " :")) != NULL)
1480 && (__builtin_expect (! __libc_enable_secure, 1)
1481 || strchr (p, '/') == NULL))
1482 npreloads += do_preload (p, main_map, "LD_PRELOAD");
1484 HP_TIMING_NOW (stop);
1485 HP_TIMING_DIFF (diff, start, stop);
1486 HP_TIMING_ACCUM_NT (load_time, diff);
1489 /* There usually is no ld.so.preload file, it should only be used
1490 for emergencies and testing. So the open call etc should usually
1491 fail. Using access() on a non-existing file is faster than using
1492 open(). So we do this first. If it succeeds we do almost twice
1493 the work but this does not matter, since it is not for production
1495 static const char preload_file[] = "/etc/ld.so.preload";
1496 if (__glibc_unlikely (__access (preload_file, R_OK) == 0))
1498 /* Read the contents of the file. */
1499 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1500 PROT_READ | PROT_WRITE);
1501 if (__glibc_unlikely (file != MAP_FAILED))
1503 /* Parse the file. It contains names of libraries to be loaded,
1504 separated by white spaces or `:'. It may also contain
1505 comments introduced by `#'. */
1510 /* Eliminate comments. */
1515 char *comment = memchr (runp, '#', rest);
1516 if (comment == NULL)
1519 rest -= comment - runp;
1522 while (--rest > 0 && *++comment != '\n');
1525 /* We have one problematic case: if we have a name at the end of
1526 the file without a trailing terminating characters, we cannot
1527 place the \0. Handle the case separately. */
1528 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1529 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1531 problem = &file[file_size];
1532 while (problem > file && problem[-1] != ' '
1533 && problem[-1] != '\t'
1534 && problem[-1] != '\n' && problem[-1] != ':')
1543 file[file_size - 1] = '\0';
1546 HP_TIMING_NOW (start);
1548 if (file != problem)
1552 while ((p = strsep (&runp, ": \t\n")) != NULL)
1554 npreloads += do_preload (p, main_map, preload_file);
1557 if (problem != NULL)
1559 char *p = strndupa (problem, file_size - (problem - file));
1561 npreloads += do_preload (p, main_map, preload_file);
1564 HP_TIMING_NOW (stop);
1565 HP_TIMING_DIFF (diff, start, stop);
1566 HP_TIMING_ACCUM_NT (load_time, diff);
1568 /* We don't need the file anymore. */
1569 __munmap (file, file_size);
1573 if (__glibc_unlikely (*first_preload != NULL))
1575 /* Set up PRELOADS with a vector of the preloaded libraries. */
1576 struct link_map *l = *first_preload;
1577 preloads = __alloca (npreloads * sizeof preloads[0]);
1584 assert (i == npreloads);
1587 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1588 specified some libraries to load, these are inserted before the actual
1589 dependencies in the executable's searchlist for symbol resolution. */
1590 HP_TIMING_NOW (start);
1591 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
1592 HP_TIMING_NOW (stop);
1593 HP_TIMING_DIFF (diff, start, stop);
1594 HP_TIMING_ACCUM_NT (load_time, diff);
1596 /* Mark all objects as being in the global scope. */
1597 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1598 main_map->l_searchlist.r_list[--i]->l_global = 1;
1600 /* Remove _dl_rtld_map from the chain. */
1601 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1602 if (GL(dl_rtld_map).l_next != NULL)
1603 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1605 for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1606 if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1609 bool rtld_multiple_ref = false;
1610 if (__glibc_likely (i < main_map->l_searchlist.r_nlist))
1612 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1613 put it back in the list of visible objects. We insert it into the
1614 chain in symbol search order because gdb uses the chain's order as
1615 its symbol search order. */
1616 rtld_multiple_ref = true;
1618 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1619 if (__builtin_expect (mode, normal) == normal)
1621 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1622 ? main_map->l_searchlist.r_list[i + 1]
1624 #ifdef NEED_DL_SYSINFO_DSO
1625 if (GLRO(dl_sysinfo_map) != NULL
1626 && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
1627 && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
1628 GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
1632 /* In trace mode there might be an invisible object (which we
1633 could not find) after the previous one in the search list.
1634 In this case it doesn't matter much where we put the
1635 interpreter object, so we just initialize the list pointer so
1636 that the assertion below holds. */
1637 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1639 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1640 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1641 if (GL(dl_rtld_map).l_next != NULL)
1643 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1644 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1648 /* Now let us see whether all libraries are available in the
1649 versions we need. */
1651 struct version_check_args args;
1652 args.doexit = mode == normal;
1653 args.dotrace = mode == trace;
1654 _dl_receive_error (print_missing_version, version_check_doit, &args);
1657 /* We do not initialize any of the TLS functionality unless any of the
1658 initial modules uses TLS. This makes dynamic loading of modules with
1659 TLS impossible, but to support it requires either eagerly doing setup
1660 now or lazily doing it later. Doing it now makes us incompatible with
1661 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1662 used. Trying to do it lazily is too hairy to try when there could be
1663 multiple threads (from a non-TLS-using libpthread). */
1664 bool was_tls_init_tp_called = tls_init_tp_called;
1668 if (__glibc_likely (audit_list == NULL))
1669 /* Initialize security features. But only if we have not done it
1673 if (__builtin_expect (mode, normal) != normal)
1675 /* We were run just to list the shared libraries. It is
1676 important that we do this before real relocation, because the
1677 functions we call below for output may no longer work properly
1678 after relocation. */
1681 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1683 struct r_scope_elem *scope = &main_map->l_searchlist;
1685 for (i = 0; i < scope->r_nlist; i++)
1687 l = scope->r_list [i];
1690 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1693 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
1694 GLRO(dl_trace_prelink_map) = l;
1695 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1696 DSO_FILENAME (l->l_libname->name),
1697 DSO_FILENAME (l->l_name),
1698 (int) sizeof l->l_map_start * 2,
1699 (size_t) l->l_map_start,
1700 (int) sizeof l->l_addr * 2,
1701 (size_t) l->l_addr);
1704 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1705 (int) sizeof l->l_tls_offset * 2,
1706 (size_t) l->l_tls_offset);
1711 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
1713 /* Look through the dependencies of the main executable
1714 and determine which of them is not actually
1716 struct link_map *l = main_map;
1718 /* Relocate the main executable. */
1719 struct relocate_args args = { .l = l,
1720 .reloc_mode = ((GLRO(dl_lazy)
1722 | __RTLD_NOIFUNC) };
1723 _dl_receive_error (print_unresolved, relocate_doit, &args);
1725 /* This loop depends on the dependencies of the executable to
1726 correspond in number and order to the DT_NEEDED entries. */
1727 ElfW(Dyn) *dyn = main_map->l_ld;
1729 while (dyn->d_tag != DT_NULL)
1731 if (dyn->d_tag == DT_NEEDED)
1734 #ifdef NEED_DL_SYSINFO_DSO
1735 /* Skip the VDSO since it's not part of the list
1736 of objects we brought in via DT_NEEDED entries. */
1737 if (l == GLRO(dl_sysinfo_map))
1744 _dl_printf ("Unused direct dependencies:\n");
1748 _dl_printf ("\t%s\n", l->l_name);
1755 _exit (first != true);
1757 else if (! main_map->l_info[DT_NEEDED])
1758 _dl_printf ("\tstatically linked\n");
1761 for (l = main_map->l_next; l; l = l->l_next)
1763 /* The library was not found. */
1764 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1765 else if (strcmp (l->l_libname->name, l->l_name) == 0)
1766 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
1767 (int) sizeof l->l_map_start * 2,
1768 (size_t) l->l_map_start);
1770 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1771 l->l_name, (int) sizeof l->l_map_start * 2,
1772 (size_t) l->l_map_start);
1775 if (__builtin_expect (mode, trace) != trace)
1776 for (i = 1; i < (unsigned int) _dl_argc; ++i)
1778 const ElfW(Sym) *ref = NULL;
1779 ElfW(Addr) loadbase;
1782 result = _dl_lookup_symbol_x (_dl_argv[i], main_map,
1783 &ref, main_map->l_scope,
1784 NULL, ELF_RTYPE_CLASS_PLT,
1785 DL_LOOKUP_ADD_DEPENDENCY, NULL);
1787 loadbase = LOOKUP_VALUE_ADDRESS (result);
1789 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1791 (int) sizeof ref->st_value * 2,
1792 (size_t) ref->st_value,
1793 (int) sizeof loadbase * 2, (size_t) loadbase);
1797 /* If LD_WARN is set, warn about undefined symbols. */
1798 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
1800 /* We have to do symbol dependency testing. */
1801 struct relocate_args args;
1804 args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
1807 i = main_map->l_searchlist.r_nlist;
1810 struct link_map *l = main_map->l_initfini[i];
1811 if (l != &GL(dl_rtld_map) && ! l->l_faked)
1814 _dl_receive_error (print_unresolved, relocate_doit,
1819 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1820 && rtld_multiple_ref)
1822 /* Mark the link map as not yet relocated again. */
1823 GL(dl_rtld_map).l_relocated = 0;
1824 _dl_relocate_object (&GL(dl_rtld_map),
1825 main_map->l_scope, __RTLD_NOIFUNC, 0);
1828 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1831 /* Print more information. This means here, print information
1832 about the versions needed. */
1834 struct link_map *map;
1836 for (map = main_map; map != NULL; map = map->l_next)
1839 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1845 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1846 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1850 _dl_printf ("\n\tVersion information:\n");
1854 _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name));
1859 struct link_map *needed;
1861 needed = find_needed (strtab + ent->vn_file);
1862 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1866 const char *fname = NULL;
1869 && match_version (strtab + aux->vna_name,
1871 fname = needed->l_name;
1873 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1874 strtab + ent->vn_file,
1875 strtab + aux->vna_name,
1876 aux->vna_flags & VER_FLG_WEAK
1878 fname ?: "not found");
1880 if (aux->vna_next == 0)
1881 /* No more symbols. */
1885 aux = (ElfW(Vernaux) *) ((char *) aux
1889 if (ent->vn_next == 0)
1890 /* No more dependencies. */
1893 /* Next dependency. */
1894 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1903 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
1904 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
1905 && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
1907 ElfW(Lib) *liblist, *liblistend;
1908 struct link_map **r_list, **r_listend, *l;
1909 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
1911 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
1912 liblist = (ElfW(Lib) *)
1913 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
1914 liblistend = (ElfW(Lib) *)
1916 main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1917 r_list = main_map->l_searchlist.r_list;
1918 r_listend = r_list + main_map->l_searchlist.r_nlist;
1920 for (; r_list < r_listend && liblist < liblistend; r_list++)
1927 /* If the library is not mapped where it should, fail. */
1931 /* Next, check if checksum matches. */
1932 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1933 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1934 != liblist->l_checksum)
1937 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1938 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1939 != liblist->l_time_stamp)
1942 if (! _dl_name_match_p (strtab + liblist->l_name, l))
1949 if (r_list == r_listend && liblist == liblistend)
1952 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1953 _dl_debug_printf ("\nprelink checking: %s\n",
1954 prelinked ? "ok" : "failed");
1958 /* Now set up the variable which helps the assembler startup code. */
1959 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
1961 /* Save the information about the original global scope list since
1962 we need it in the memory handling later. */
1963 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
1965 /* Remember the last search directory added at startup, now that
1966 malloc will no longer be the one from dl-minimal.c. */
1967 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
1969 /* Print scope information. */
1970 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
1972 _dl_debug_printf ("\nInitial object scopes\n");
1974 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
1975 _dl_show_scope (l, 0);
1980 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
1982 ElfW(Rela) *conflict, *conflictend;
1983 #ifndef HP_TIMING_NONAVAIL
1988 HP_TIMING_NOW (start);
1989 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
1990 conflict = (ElfW(Rela) *)
1991 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
1992 conflictend = (ElfW(Rela) *)
1994 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
1995 _dl_resolve_conflicts (main_map, conflict, conflictend);
1996 HP_TIMING_NOW (stop);
1997 HP_TIMING_DIFF (relocate_time, start, stop);
2001 /* Mark all the objects so we know they have been already relocated. */
2002 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2005 if (l->l_relro_size)
2006 _dl_protect_relro (l);
2008 /* Add object to slot information data if necessasy. */
2009 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2010 _dl_add_to_slotinfo (l);
2015 /* Now we have all the objects loaded. Relocate them all except for
2016 the dynamic linker itself. We do this in reverse order so that copy
2017 relocs of earlier objects overwrite the data written by later
2018 objects. We do not re-relocate the dynamic linker itself in this
2019 loop because that could result in the GOT entries for functions we
2020 call being changed, and that would break us. It is safe to relocate
2021 the dynamic linker out of order because it has no copy relocs (we
2022 know that because it is self-contained). */
2024 int consider_profiling = GLRO(dl_profile) != NULL;
2025 #ifndef HP_TIMING_NONAVAIL
2030 /* If we are profiling we also must do lazy reloaction. */
2031 GLRO(dl_lazy) |= consider_profiling;
2033 HP_TIMING_NOW (start);
2034 unsigned i = main_map->l_searchlist.r_nlist;
2037 struct link_map *l = main_map->l_initfini[i];
2039 /* While we are at it, help the memory handling a bit. We have to
2040 mark some data structures as allocated with the fake malloc()
2041 implementation in ld.so. */
2042 struct libname_list *lnp = l->l_libname->next;
2044 while (__builtin_expect (lnp != NULL, 0))
2049 /* Also allocated with the fake malloc(). */
2050 l->l_free_initfini = 0;
2052 if (l != &GL(dl_rtld_map))
2053 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2054 consider_profiling);
2056 /* Add object to slot information data if necessasy. */
2057 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2058 _dl_add_to_slotinfo (l);
2060 HP_TIMING_NOW (stop);
2062 HP_TIMING_DIFF (relocate_time, start, stop);
2064 /* Now enable profiling if needed. Like the previous call,
2065 this has to go here because the calls it makes should use the
2066 rtld versions of the functions (particularly calloc()), but it
2067 needs to have _dl_profile_map set up by the relocator. */
2068 if (__glibc_unlikely (GL(dl_profile_map) != NULL))
2069 /* We must prepare the profiling. */
2070 _dl_start_profile ();
2073 if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2074 || count_modids != _dl_count_modids ())
2075 ++GL(dl_tls_generation);
2077 /* Now that we have completed relocation, the initializer data
2078 for the TLS blocks has its final values and we can copy them
2079 into the main thread's TLS area, which we allocated above. */
2080 _dl_allocate_tls_init (tcbp);
2082 /* And finally install it for the main thread. */
2083 if (! tls_init_tp_called)
2085 const char *lossage = TLS_INIT_TP (tcbp);
2086 if (__glibc_unlikely (lossage != NULL))
2087 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2091 /* Make sure no new search directories have been added. */
2092 assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2094 if (! prelinked && rtld_multiple_ref)
2096 /* There was an explicit ref to the dynamic linker as a shared lib.
2097 Re-relocate ourselves with user-controlled symbol definitions.
2099 We must do this after TLS initialization in case after this
2100 re-relocation, we might call a user-supplied function
2101 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2103 #ifndef HP_TIMING_NONAVAIL
2109 HP_TIMING_NOW (start);
2110 /* Mark the link map as not yet relocated again. */
2111 GL(dl_rtld_map).l_relocated = 0;
2112 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2113 HP_TIMING_NOW (stop);
2114 HP_TIMING_DIFF (add, start, stop);
2115 HP_TIMING_ACCUM_NT (relocate_time, add);
2118 /* Do any necessary cleanups for the startup OS interface code.
2119 We do these now so that no calls are made after rtld re-relocation
2120 which might be resolved to different functions than we expect.
2121 We cannot do this before relocating the other objects because
2122 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2123 _dl_sysdep_start_cleanup ();
2126 /* Auditing checkpoint: we have added all objects. */
2127 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
2129 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2130 /* Do not call the functions for any auditing object. */
2131 if (head->l_auditing == 0)
2133 struct audit_ifaces *afct = GLRO(dl_audit);
2134 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2136 if (afct->activity != NULL)
2137 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
2145 /* Notify the debugger all new objects are now ready to go. We must re-get
2146 the address since by now the variable might be in another object. */
2147 r = _dl_debug_initialize (0, LM_ID_BASE);
2148 r->r_state = RT_CONSISTENT;
2150 LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
2152 #if defined USE_LDCONFIG && !defined MAP_COPY
2153 /* We must munmap() the cache file. */
2154 _dl_unload_cache ();
2157 /* Once we return, _dl_sysdep_start will invoke
2158 the DT_INIT functions and then *USER_ENTRY. */
2161 /* This is a little helper function for resolving symbols while
2162 tracing the binary. */
2164 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2165 const char *errstring)
2167 if (objname[0] == '\0')
2168 objname = RTLD_PROGNAME;
2169 _dl_error_printf ("%s (%s)\n", errstring, objname);
2172 /* This is a little helper function for resolving symbols while
2173 tracing the binary. */
2175 print_missing_version (int errcode __attribute__ ((unused)),
2176 const char *objname, const char *errstring)
2178 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME,
2179 objname, errstring);
2182 /* Nonzero if any of the debugging options is enabled. */
2183 static int any_debug attribute_relro;
2185 /* Process the string given as the parameter which explains which debugging
2186 options are enabled. */
2188 process_dl_debug (const char *dl_debug)
2190 /* When adding new entries make sure that the maximal length of a name
2191 is correctly handled in the LD_DEBUG_HELP code below. */
2195 const char name[10];
2196 const char helptext[41];
2197 unsigned short int mask;
2200 #define LEN_AND_STR(str) sizeof (str) - 1, str
2201 { LEN_AND_STR ("libs"), "display library search paths",
2202 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2203 { LEN_AND_STR ("reloc"), "display relocation processing",
2204 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2205 { LEN_AND_STR ("files"), "display progress for input file",
2206 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2207 { LEN_AND_STR ("symbols"), "display symbol table processing",
2208 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2209 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2210 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2211 { LEN_AND_STR ("versions"), "display version dependencies",
2212 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2213 { LEN_AND_STR ("scopes"), "display scope information",
2215 { LEN_AND_STR ("all"), "all previous options combined",
2216 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2217 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2218 | DL_DEBUG_SCOPES },
2219 { LEN_AND_STR ("statistics"), "display relocation statistics",
2220 DL_DEBUG_STATISTICS },
2221 { LEN_AND_STR ("unused"), "determined unused DSOs",
2223 { LEN_AND_STR ("help"), "display this help message and exit",
2226 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2228 /* Skip separating white spaces and commas. */
2229 while (*dl_debug != '\0')
2231 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2236 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2237 && dl_debug[len] != ',' && dl_debug[len] != ':')
2240 for (cnt = 0; cnt < ndebopts; ++cnt)
2241 if (debopts[cnt].len == len
2242 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2244 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2249 if (cnt == ndebopts)
2251 /* Display a warning and skip everything until next
2253 char *copy = strndupa (dl_debug, len);
2254 _dl_error_printf ("\
2255 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2265 if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2267 /* In order to get an accurate picture of whether a particular
2268 DT_NEEDED entry is actually used we have to process both
2269 the PLT and non-PLT relocation entries. */
2273 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2278 Valid options for the LD_DEBUG environment variable are:\n\n");
2280 for (cnt = 0; cnt < ndebopts; ++cnt)
2281 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2282 " " + debopts[cnt].len - 3,
2283 debopts[cnt].helptext);
2286 To direct the debugging output into a file instead of standard output\n\
2287 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2293 process_dl_audit (char *str)
2295 /* The parameter is a colon separated list of DSO names. */
2298 while ((p = (strsep) (&str, ":")) != NULL)
2300 && (__builtin_expect (! __libc_enable_secure, 1)
2301 || strchr (p, '/') == NULL))
2303 /* This is using the local malloc, not the system malloc. The
2304 memory can never be freed. */
2305 struct audit_list *newp = malloc (sizeof (*newp));
2308 if (audit_list == NULL)
2309 audit_list = newp->next = newp;
2312 newp->next = audit_list->next;
2313 audit_list = audit_list->next = newp;
2318 /* Process all environments variables the dynamic linker must recognize.
2319 Since all of them start with `LD_' we are a bit smarter while finding
2321 extern char **_environ attribute_hidden;
2325 process_envvars (enum mode *modep)
2327 char **runp = _environ;
2329 enum mode mode = normal;
2330 char *debug_output = NULL;
2332 /* This is the default place for profiling data file. */
2333 GLRO(dl_profile_output)
2334 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
2336 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2340 while (envline[len] != '\0' && envline[len] != '=')
2343 if (envline[len] != '=')
2344 /* This is a "LD_" variable at the end of the string without
2345 a '=' character. Ignore it since otherwise we will access
2346 invalid memory below. */
2352 /* Warning level, verbose or not. */
2353 if (memcmp (envline, "WARN", 4) == 0)
2354 GLRO(dl_verbose) = envline[5] != '\0';
2358 /* Debugging of the dynamic linker? */
2359 if (memcmp (envline, "DEBUG", 5) == 0)
2361 process_dl_debug (&envline[6]);
2364 if (memcmp (envline, "AUDIT", 5) == 0)
2365 process_dl_audit (&envline[6]);
2369 /* Print information about versions. */
2370 if (memcmp (envline, "VERBOSE", 7) == 0)
2372 version_info = envline[8] != '\0';
2376 /* List of objects to be preloaded. */
2377 if (memcmp (envline, "PRELOAD", 7) == 0)
2379 preloadlist = &envline[8];
2383 /* Which shared object shall be profiled. */
2384 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2385 GLRO(dl_profile) = &envline[8];
2389 /* Do we bind early? */
2390 if (memcmp (envline, "BIND_NOW", 8) == 0)
2392 GLRO(dl_lazy) = envline[9] == '\0';
2395 if (memcmp (envline, "BIND_NOT", 8) == 0)
2396 GLRO(dl_bind_not) = envline[9] != '\0';
2400 /* Test whether we want to see the content of the auxiliary
2401 array passed up from the kernel. */
2402 if (!__libc_enable_secure
2403 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2409 /* Mask for the important hardware capabilities. */
2410 if (!__libc_enable_secure
2411 && memcmp (envline, "HWCAP_MASK", 10) == 0)
2412 GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL,
2418 /* Path where the binary is found. */
2419 if (!__libc_enable_secure
2420 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2421 GLRO(dl_origin_path) = &envline[12];
2425 /* The library search path. */
2426 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
2428 library_path = &envline[13];
2432 /* Where to place the profiling data file. */
2433 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2435 debug_output = &envline[13];
2439 if (!__libc_enable_secure
2440 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2441 GLRO(dl_dynamic_weak) = 1;
2445 /* We might have some extra environment variable with length 13
2447 #ifdef EXTRA_LD_ENVVARS_13
2450 if (!__libc_enable_secure
2451 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2453 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2459 /* Where to place the profiling data file. */
2460 if (!__libc_enable_secure
2461 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2462 && envline[15] != '\0')
2463 GLRO(dl_profile_output) = &envline[15];
2467 /* The mode of the dynamic linker can be set. */
2468 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2471 GLRO(dl_verbose) = 1;
2472 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2473 GLRO(dl_trace_prelink) = &envline[17];
2478 /* The mode of the dynamic linker can be set. */
2479 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2483 /* We might have some extra environment variable to handle. This
2484 is tricky due to the pre-processing of the length of the name
2485 in the switch statement here. The code here assumes that added
2486 environment variables have a different length. */
2487 #ifdef EXTRA_LD_ENVVARS
2493 /* The caller wants this information. */
2496 /* Extra security for SUID binaries. Remove all dangerous environment
2498 if (__builtin_expect (__libc_enable_secure, 0))
2500 static const char unsecure_envvars[] =
2501 #ifdef EXTRA_UNSECURE_ENVVARS
2502 EXTRA_UNSECURE_ENVVARS
2507 nextp = unsecure_envvars;
2511 /* We could use rawmemchr but this need not be fast. */
2512 nextp = (char *) (strchr) (nextp, '\0') + 1;
2514 while (*nextp != '\0');
2516 if (__access ("/etc/suid-debug", F_OK) != 0)
2519 unsetenv ("MALLOC_CHECK_");
2521 GLRO(dl_debug_mask) = 0;
2527 /* If we have to run the dynamic linker in debugging mode and the
2528 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2529 messages to this file. */
2530 else if (any_debug && debug_output != NULL)
2532 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2533 size_t name_len = strlen (debug_output);
2534 char buf[name_len + 12];
2537 buf[name_len + 11] = '\0';
2538 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2540 startp = memcpy (startp - name_len, debug_output, name_len);
2542 GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
2543 if (GLRO(dl_debug_fd) == -1)
2544 /* We use standard output if opening the file failed. */
2545 GLRO(dl_debug_fd) = STDOUT_FILENO;
2550 /* Print the various times we collected. */
2552 __attribute ((noinline))
2553 print_statistics (hp_timing_t *rtld_total_timep)
2555 #ifndef HP_TIMING_NONAVAIL
2560 /* Total time rtld used. */
2561 if (HP_SMALL_TIMING_AVAIL)
2563 HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
2564 _dl_debug_printf ("\nruntime linker statistics:\n"
2565 " total startup time in dynamic loader: %s\n", buf);
2567 /* Print relocation statistics. */
2569 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
2570 cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
2571 pbuf + sizeof (pbuf), 10, 0);
2573 switch (pbuf + sizeof (pbuf) - cp)
2584 _dl_debug_printf ("\
2585 time needed for relocation: %s (%s%%)\n", buf, pbuf);
2589 unsigned long int num_relative_relocations = 0;
2590 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2592 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2595 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2597 for (unsigned int i = 0; i < scope->r_nlist; i++)
2599 struct link_map *l = scope->r_list [i];
2601 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2602 num_relative_relocations
2603 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2604 #ifndef ELF_MACHINE_REL_RELATIVE
2605 /* Relative relocations are processed on these architectures if
2606 library is loaded to different address than p_vaddr or
2607 if not prelinked. */
2608 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2609 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2611 /* On e.g. IA-64 or Alpha, relative relocations are processed
2612 only if library is loaded to different address than p_vaddr. */
2613 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2615 num_relative_relocations
2616 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2620 _dl_debug_printf (" number of relocations: %lu\n"
2621 " number of relocations from cache: %lu\n"
2622 " number of relative relocations: %lu\n",
2623 GL(dl_num_relocations),
2624 GL(dl_num_cache_relocations),
2625 num_relative_relocations);
2627 #ifndef HP_TIMING_NONAVAIL
2628 /* Time spend while loading the object and the dependencies. */
2629 if (HP_SMALL_TIMING_AVAIL)
2632 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
2633 cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
2634 pbuf + sizeof (pbuf), 10, 0);
2636 switch (pbuf + sizeof (pbuf) - cp)
2647 _dl_debug_printf ("\
2648 time needed to load objects: %s (%s%%)\n",