aarch64: Move and update the definition of MTE_ENABLED
[glibc.git] / elf / rtld.c
blob596b6ac3d99d7d8e1b51374893fe1233f7b0d70a
1 /* Run time dynamic linker.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
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>
50 #include <dl-main.h>
51 #include <list.h>
52 #include <gnu/lib-names.h>
53 #include <dl-tunables.h>
55 #include <assert.h>
57 /* Only enables rtld profiling for architectures which provides non generic
58 hp-timing support. The generic support requires either syscall
59 (clock_gettime), which will incur in extra overhead on loading time.
60 Using vDSO is also an option, but it will require extra support on loader
61 to setup the vDSO pointer before its usage. */
62 #if HP_TIMING_INLINE
63 # define RLTD_TIMING_DECLARE(var, classifier,...) \
64 classifier hp_timing_t var __VA_ARGS__
65 # define RTLD_TIMING_VAR(var) RLTD_TIMING_DECLARE (var, )
66 # define RTLD_TIMING_SET(var, value) (var) = (value)
67 # define RTLD_TIMING_REF(var) &(var)
69 static inline void
70 rtld_timer_start (hp_timing_t *var)
72 HP_TIMING_NOW (*var);
75 static inline void
76 rtld_timer_stop (hp_timing_t *var, hp_timing_t start)
78 hp_timing_t stop;
79 HP_TIMING_NOW (stop);
80 HP_TIMING_DIFF (*var, start, stop);
83 static inline void
84 rtld_timer_accum (hp_timing_t *sum, hp_timing_t start)
86 hp_timing_t stop;
87 rtld_timer_stop (&stop, start);
88 HP_TIMING_ACCUM_NT(*sum, stop);
90 #else
91 # define RLTD_TIMING_DECLARE(var, classifier...)
92 # define RTLD_TIMING_SET(var, value)
93 # define RTLD_TIMING_VAR(var)
94 # define RTLD_TIMING_REF(var) 0
95 # define rtld_timer_start(var)
96 # define rtld_timer_stop(var, start)
97 # define rtld_timer_accum(sum, start)
98 #endif
100 /* Avoid PLT use for our local calls at startup. */
101 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
103 /* GCC has mental blocks about _exit. */
104 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
105 #define _exit exit_internal
107 /* Helper function to handle errors while resolving symbols. */
108 static void print_unresolved (int errcode, const char *objname,
109 const char *errsting);
111 /* Helper function to handle errors when a version is missing. */
112 static void print_missing_version (int errcode, const char *objname,
113 const char *errsting);
115 /* Print the various times we collected. */
116 static void print_statistics (const hp_timing_t *total_timep);
118 /* Creates an empty audit list. */
119 static void audit_list_init (struct audit_list *);
121 /* Add a string to the end of the audit list, for later parsing. Must
122 not be called after audit_list_next. */
123 static void audit_list_add_string (struct audit_list *, const char *);
125 /* Add the audit strings from the link map, found in the dynamic
126 segment at TG (either DT_AUDIT and DT_DEPAUDIT). Must be called
127 before audit_list_next. */
128 static void audit_list_add_dynamic_tag (struct audit_list *,
129 struct link_map *,
130 unsigned int tag);
132 /* Extract the next audit module from the audit list. Only modules
133 for which dso_name_valid_for_suid is true are returned. Must be
134 called after all the audit_list_add_string,
135 audit_list_add_dynamic_tags calls. */
136 static const char *audit_list_next (struct audit_list *);
138 /* Initialize *STATE with the defaults. */
139 static void dl_main_state_init (struct dl_main_state *state);
141 /* Process all environments variables the dynamic linker must recognize.
142 Since all of them start with `LD_' we are a bit smarter while finding
143 all the entries. */
144 static void process_envvars (struct dl_main_state *state);
146 #ifdef DL_ARGV_NOT_RELRO
147 int _dl_argc attribute_hidden;
148 char **_dl_argv = NULL;
149 /* Nonzero if we were run directly. */
150 unsigned int _dl_skip_args attribute_hidden;
151 #else
152 int _dl_argc attribute_relro attribute_hidden;
153 char **_dl_argv attribute_relro = NULL;
154 unsigned int _dl_skip_args attribute_relro attribute_hidden;
155 #endif
156 rtld_hidden_data_def (_dl_argv)
158 #ifndef THREAD_SET_STACK_GUARD
159 /* Only exported for architectures that don't store the stack guard canary
160 in thread local area. */
161 uintptr_t __stack_chk_guard attribute_relro;
162 #endif
164 /* Only exported for architectures that don't store the pointer guard
165 value in thread local area. */
166 uintptr_t __pointer_chk_guard_local
167 attribute_relro attribute_hidden __attribute__ ((nocommon));
168 #ifndef THREAD_SET_POINTER_GUARD
169 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
170 #endif
172 /* Check that AT_SECURE=0, or that the passed name does not contain
173 directories and is not overly long. Reject empty names
174 unconditionally. */
175 static bool
176 dso_name_valid_for_suid (const char *p)
178 if (__glibc_unlikely (__libc_enable_secure))
180 /* Ignore pathnames with directories for AT_SECURE=1
181 programs, and also skip overlong names. */
182 size_t len = strlen (p);
183 if (len >= SECURE_NAME_LIMIT || memchr (p, '/', len) != NULL)
184 return false;
186 return *p != '\0';
189 static void
190 audit_list_init (struct audit_list *list)
192 list->length = 0;
193 list->current_index = 0;
194 list->current_tail = NULL;
197 static void
198 audit_list_add_string (struct audit_list *list, const char *string)
200 /* Empty strings do not load anything. */
201 if (*string == '\0')
202 return;
204 if (list->length == array_length (list->audit_strings))
205 _dl_fatal_printf ("Fatal glibc error: Too many audit modules requested\n");
207 list->audit_strings[list->length++] = string;
209 /* Initialize processing of the first string for
210 audit_list_next. */
211 if (list->length == 1)
212 list->current_tail = string;
215 static void
216 audit_list_add_dynamic_tag (struct audit_list *list, struct link_map *main_map,
217 unsigned int tag)
219 ElfW(Dyn) *info = main_map->l_info[ADDRIDX (tag)];
220 const char *strtab = (const char *) D_PTR (main_map, l_info[DT_STRTAB]);
221 if (info != NULL)
222 audit_list_add_string (list, strtab + info->d_un.d_val);
225 static const char *
226 audit_list_next (struct audit_list *list)
228 if (list->current_tail == NULL)
229 return NULL;
231 while (true)
233 /* Advance to the next string in audit_strings if the current
234 string has been exhausted. */
235 while (*list->current_tail == '\0')
237 ++list->current_index;
238 if (list->current_index == list->length)
240 list->current_tail = NULL;
241 return NULL;
243 list->current_tail = list->audit_strings[list->current_index];
246 /* Split the in-string audit list at the next colon colon. */
247 size_t len = strcspn (list->current_tail, ":");
248 if (len > 0 && len < sizeof (list->fname))
250 memcpy (list->fname, list->current_tail, len);
251 list->fname[len] = '\0';
253 else
254 /* Mark the name as unusable for dso_name_valid_for_suid. */
255 list->fname[0] = '\0';
257 /* Skip over the substring and the following delimiter. */
258 list->current_tail += len;
259 if (*list->current_tail == ':')
260 ++list->current_tail;
262 /* If the name is valid, return it. */
263 if (dso_name_valid_for_suid (list->fname))
264 return list->fname;
266 /* Otherwise wrap around to find the next list element. . */
270 /* Count audit modules before they are loaded so GLRO(dl_naudit)
271 is not yet usable. */
272 static size_t
273 audit_list_count (struct audit_list *list)
275 /* Restore the audit_list iterator state at the end. */
276 const char *saved_tail = list->current_tail;
277 size_t naudit = 0;
279 assert (list->current_index == 0);
280 while (audit_list_next (list) != NULL)
281 naudit++;
282 list->current_tail = saved_tail;
283 list->current_index = 0;
284 return naudit;
287 static void
288 dl_main_state_init (struct dl_main_state *state)
290 audit_list_init (&state->audit_list);
291 state->library_path = NULL;
292 state->library_path_source = NULL;
293 state->preloadlist = NULL;
294 state->preloadarg = NULL;
295 state->glibc_hwcaps_prepend = NULL;
296 state->glibc_hwcaps_mask = NULL;
297 state->mode = rtld_mode_normal;
298 state->any_debug = false;
299 state->version_info = false;
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 what 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 (size_t naudit)
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 /* Calculate the size of the static TLS surplus. */
784 _dl_tls_static_surplus_init (naudit);
786 /* Compute the TLS offsets for the various blocks. */
787 _dl_determine_tlsoffset ();
789 /* Construct the static TLS block and the dtv for the initial
790 thread. For some platforms this will include allocating memory
791 for the thread descriptor. The memory for the TLS block will
792 never be freed. It should be allocated accordingly. The dtv
793 array can be changed if dynamic loading requires it. */
794 void *tcbp = _dl_allocate_tls_storage ();
795 if (tcbp == NULL)
796 _dl_fatal_printf ("\
797 cannot allocate TLS data structures for initial thread\n");
799 /* Store for detection of the special case by __tls_get_addr
800 so it knows not to pass this dtv to the normal realloc. */
801 GL(dl_initial_dtv) = GET_DTV (tcbp);
803 /* And finally install it for the main thread. */
804 const char *lossage = TLS_INIT_TP (tcbp);
805 if (__glibc_unlikely (lossage != NULL))
806 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
807 #if THREAD_GSCOPE_IN_TCB
808 list_add (&THREAD_SELF->list, &GL (dl_stack_user));
809 #endif
810 tls_init_tp_called = true;
812 return tcbp;
815 static unsigned int
816 do_preload (const char *fname, struct link_map *main_map, const char *where)
818 const char *objname;
819 const char *err_str = NULL;
820 struct map_args args;
821 bool malloced;
823 args.str = fname;
824 args.loader = main_map;
825 args.mode = __RTLD_SECURE;
827 unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
829 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
830 if (__glibc_unlikely (err_str != NULL))
832 _dl_error_printf ("\
833 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
834 fname, where, err_str);
835 /* No need to call free, this is still before
836 the libc's malloc is used. */
838 else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
839 /* It is no duplicate. */
840 return 1;
842 /* Nothing loaded. */
843 return 0;
846 #if defined SHARED && defined _LIBC_REENTRANT \
847 && defined __rtld_lock_default_lock_recursive
848 static void
849 rtld_lock_default_lock_recursive (void *lock)
851 __rtld_lock_default_lock_recursive (lock);
854 static void
855 rtld_lock_default_unlock_recursive (void *lock)
857 __rtld_lock_default_unlock_recursive (lock);
859 #endif
862 static void
863 security_init (void)
865 /* Set up the stack checker's canary. */
866 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
867 #ifdef THREAD_SET_STACK_GUARD
868 THREAD_SET_STACK_GUARD (stack_chk_guard);
869 #else
870 __stack_chk_guard = stack_chk_guard;
871 #endif
873 /* Set up the pointer guard as well, if necessary. */
874 uintptr_t pointer_chk_guard
875 = _dl_setup_pointer_guard (_dl_random, stack_chk_guard);
876 #ifdef THREAD_SET_POINTER_GUARD
877 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
878 #endif
879 __pointer_chk_guard_local = pointer_chk_guard;
881 /* We do not need the _dl_random value anymore. The less
882 information we leave behind, the better, so clear the
883 variable. */
884 _dl_random = NULL;
887 #include <setup-vdso.h>
889 /* The LD_PRELOAD environment variable gives list of libraries
890 separated by white space or colons that are loaded before the
891 executable's dependencies and prepended to the global scope list.
892 (If the binary is running setuid all elements containing a '/' are
893 ignored since it is insecure.) Return the number of preloads
894 performed. Ditto for --preload command argument. */
895 unsigned int
896 handle_preload_list (const char *preloadlist, struct link_map *main_map,
897 const char *where)
899 unsigned int npreloads = 0;
900 const char *p = preloadlist;
901 char fname[SECURE_PATH_LIMIT];
903 while (*p != '\0')
905 /* Split preload list at space/colon. */
906 size_t len = strcspn (p, " :");
907 if (len > 0 && len < sizeof (fname))
909 memcpy (fname, p, len);
910 fname[len] = '\0';
912 else
913 fname[0] = '\0';
915 /* Skip over the substring and the following delimiter. */
916 p += len;
917 if (*p != '\0')
918 ++p;
920 if (dso_name_valid_for_suid (fname))
921 npreloads += do_preload (fname, main_map, where);
923 return npreloads;
926 /* Called if the audit DSO cannot be used: if it does not have the
927 appropriate interfaces, or it expects a more recent version library
928 version than what the dynamic linker provides. */
929 static void
930 unload_audit_module (struct link_map *map, int original_tls_idx)
932 #ifndef NDEBUG
933 Lmid_t ns = map->l_ns;
934 #endif
935 _dl_close (map);
937 /* Make sure the namespace has been cleared entirely. */
938 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
939 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
941 GL(dl_tls_max_dtv_idx) = original_tls_idx;
944 /* Called to print an error message if loading of an audit module
945 failed. */
946 static void
947 report_audit_module_load_error (const char *name, const char *err_str,
948 bool malloced)
950 _dl_error_printf ("\
951 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
952 name, err_str);
953 if (malloced)
954 free ((char *) err_str);
957 /* Load one audit module. */
958 static void
959 load_audit_module (const char *name, struct audit_ifaces **last_audit)
961 int original_tls_idx = GL(dl_tls_max_dtv_idx);
963 struct dlmopen_args dlmargs;
964 dlmargs.fname = name;
965 dlmargs.map = NULL;
967 const char *objname;
968 const char *err_str = NULL;
969 bool malloced;
970 _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit, &dlmargs);
971 if (__glibc_unlikely (err_str != NULL))
973 report_audit_module_load_error (name, err_str, malloced);
974 return;
977 struct lookup_args largs;
978 largs.name = "la_version";
979 largs.map = dlmargs.map;
980 _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs);
981 if (__glibc_likely (err_str != NULL))
983 unload_audit_module (dlmargs.map, original_tls_idx);
984 report_audit_module_load_error (name, err_str, malloced);
985 return;
988 unsigned int (*laversion) (unsigned int) = largs.result;
990 /* A null symbol indicates that something is very wrong with the
991 loaded object because defined symbols are supposed to have a
992 valid, non-null address. */
993 assert (laversion != NULL);
995 unsigned int lav = laversion (LAV_CURRENT);
996 if (lav == 0)
998 /* Only print an error message if debugging because this can
999 happen deliberately. */
1000 if (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
1001 _dl_debug_printf ("\
1002 file=%s [%lu]; audit interface function la_version returned zero; ignored.\n",
1003 dlmargs.map->l_name, dlmargs.map->l_ns);
1004 unload_audit_module (dlmargs.map, original_tls_idx);
1005 return;
1008 if (lav > LAV_CURRENT)
1010 _dl_debug_printf ("\
1011 ERROR: audit interface '%s' requires version %d (maximum supported version %d); ignored.\n",
1012 name, lav, LAV_CURRENT);
1013 unload_audit_module (dlmargs.map, original_tls_idx);
1014 return;
1017 enum { naudit_ifaces = 8 };
1018 union
1020 struct audit_ifaces ifaces;
1021 void (*fptr[naudit_ifaces]) (void);
1022 } *newp = malloc (sizeof (*newp));
1023 if (newp == NULL)
1024 _dl_fatal_printf ("Out of memory while loading audit modules\n");
1026 /* Names of the auditing interfaces. All in one
1027 long string. */
1028 static const char audit_iface_names[] =
1029 "la_activity\0"
1030 "la_objsearch\0"
1031 "la_objopen\0"
1032 "la_preinit\0"
1033 #if __ELF_NATIVE_CLASS == 32
1034 "la_symbind32\0"
1035 #elif __ELF_NATIVE_CLASS == 64
1036 "la_symbind64\0"
1037 #else
1038 # error "__ELF_NATIVE_CLASS must be defined"
1039 #endif
1040 #define STRING(s) __STRING (s)
1041 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1042 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1043 "la_objclose\0";
1044 unsigned int cnt = 0;
1045 const char *cp = audit_iface_names;
1048 largs.name = cp;
1049 _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs);
1051 /* Store the pointer. */
1052 if (err_str == NULL && largs.result != NULL)
1053 newp->fptr[cnt] = largs.result;
1054 else
1055 newp->fptr[cnt] = NULL;
1056 ++cnt;
1058 cp = rawmemchr (cp, '\0') + 1;
1060 while (*cp != '\0');
1061 assert (cnt == naudit_ifaces);
1063 /* Now append the new auditing interface to the list. */
1064 newp->ifaces.next = NULL;
1065 if (*last_audit == NULL)
1066 *last_audit = GLRO(dl_audit) = &newp->ifaces;
1067 else
1068 *last_audit = (*last_audit)->next = &newp->ifaces;
1070 /* The dynamic linker link map is statically allocated, so the
1071 cookie in _dl_new_object has not happened. */
1072 link_map_audit_state (&GL (dl_rtld_map), GLRO (dl_naudit))->cookie
1073 = (intptr_t) &GL (dl_rtld_map);
1075 ++GLRO(dl_naudit);
1077 /* Mark the DSO as being used for auditing. */
1078 dlmargs.map->l_auditing = 1;
1081 /* Notify the the audit modules that the object MAP has already been
1082 loaded. */
1083 static void
1084 notify_audit_modules_of_loaded_object (struct link_map *map)
1086 struct audit_ifaces *afct = GLRO(dl_audit);
1087 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1089 if (afct->objopen != NULL)
1091 struct auditstate *state = link_map_audit_state (map, cnt);
1092 state->bindflags = afct->objopen (map, LM_ID_BASE, &state->cookie);
1093 map->l_audit_any_plt |= state->bindflags != 0;
1096 afct = afct->next;
1100 /* Load all audit modules. */
1101 static void
1102 load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
1104 struct audit_ifaces *last_audit = NULL;
1106 while (true)
1108 const char *name = audit_list_next (audit_list);
1109 if (name == NULL)
1110 break;
1111 load_audit_module (name, &last_audit);
1114 /* Notify audit modules of the initially loaded modules (the main
1115 program and the dynamic linker itself). */
1116 if (GLRO(dl_naudit) > 0)
1118 notify_audit_modules_of_loaded_object (main_map);
1119 notify_audit_modules_of_loaded_object (&GL(dl_rtld_map));
1123 static void
1124 dl_main (const ElfW(Phdr) *phdr,
1125 ElfW(Word) phnum,
1126 ElfW(Addr) *user_entry,
1127 ElfW(auxv_t) *auxv)
1129 const ElfW(Phdr) *ph;
1130 struct link_map *main_map;
1131 size_t file_size;
1132 char *file;
1133 bool has_interp = false;
1134 unsigned int i;
1135 bool prelinked = false;
1136 bool rtld_is_main = false;
1137 void *tcbp = NULL;
1139 struct dl_main_state state;
1140 dl_main_state_init (&state);
1142 GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
1144 #if defined SHARED && defined _LIBC_REENTRANT \
1145 && defined __rtld_lock_default_lock_recursive
1146 GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
1147 GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
1148 #endif
1150 #if THREAD_GSCOPE_IN_TCB
1151 INIT_LIST_HEAD (&GL (dl_stack_used));
1152 INIT_LIST_HEAD (&GL (dl_stack_user));
1153 #endif
1155 /* The explicit initialization here is cheaper than processing the reloc
1156 in the _rtld_local definition's initializer. */
1157 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
1159 /* Process the environment variable which control the behaviour. */
1160 process_envvars (&state);
1162 #ifndef HAVE_INLINED_SYSCALLS
1163 /* Set up a flag which tells we are just starting. */
1164 _dl_starting_up = 1;
1165 #endif
1167 const char *ld_so_name = _dl_argv[0];
1168 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
1170 /* Ho ho. We are not the program interpreter! We are the program
1171 itself! This means someone ran ld.so as a command. Well, that
1172 might be convenient to do sometimes. We support it by
1173 interpreting the args like this:
1175 ld.so PROGRAM ARGS...
1177 The first argument is the name of a file containing an ELF
1178 executable we will load and run with the following arguments.
1179 To simplify life here, PROGRAM is searched for using the
1180 normal rules for shared objects, rather than $PATH or anything
1181 like that. We just load it and use its entry point; we don't
1182 pay attention to its PT_INTERP command (we are the interpreter
1183 ourselves). This is an easy way to test a new ld.so before
1184 installing it. */
1185 rtld_is_main = true;
1187 char *argv0 = NULL;
1189 /* Note the place where the dynamic linker actually came from. */
1190 GL(dl_rtld_map).l_name = rtld_progname;
1192 while (_dl_argc > 1)
1193 if (! strcmp (_dl_argv[1], "--list"))
1195 if (state.mode != rtld_mode_help)
1197 state.mode = rtld_mode_list;
1198 /* This means do no dependency analysis. */
1199 GLRO(dl_lazy) = -1;
1202 ++_dl_skip_args;
1203 --_dl_argc;
1204 ++_dl_argv;
1206 else if (! strcmp (_dl_argv[1], "--verify"))
1208 if (state.mode != rtld_mode_help)
1209 state.mode = rtld_mode_verify;
1211 ++_dl_skip_args;
1212 --_dl_argc;
1213 ++_dl_argv;
1215 else if (! strcmp (_dl_argv[1], "--inhibit-cache"))
1217 GLRO(dl_inhibit_cache) = 1;
1218 ++_dl_skip_args;
1219 --_dl_argc;
1220 ++_dl_argv;
1222 else if (! strcmp (_dl_argv[1], "--library-path")
1223 && _dl_argc > 2)
1225 state.library_path = _dl_argv[2];
1226 state.library_path_source = "--library-path";
1228 _dl_skip_args += 2;
1229 _dl_argc -= 2;
1230 _dl_argv += 2;
1232 else if (! strcmp (_dl_argv[1], "--inhibit-rpath")
1233 && _dl_argc > 2)
1235 GLRO(dl_inhibit_rpath) = _dl_argv[2];
1237 _dl_skip_args += 2;
1238 _dl_argc -= 2;
1239 _dl_argv += 2;
1241 else if (! strcmp (_dl_argv[1], "--audit") && _dl_argc > 2)
1243 audit_list_add_string (&state.audit_list, _dl_argv[2]);
1245 _dl_skip_args += 2;
1246 _dl_argc -= 2;
1247 _dl_argv += 2;
1249 else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2)
1251 state.preloadarg = _dl_argv[2];
1252 _dl_skip_args += 2;
1253 _dl_argc -= 2;
1254 _dl_argv += 2;
1256 else if (! strcmp (_dl_argv[1], "--argv0") && _dl_argc > 2)
1258 argv0 = _dl_argv[2];
1260 _dl_skip_args += 2;
1261 _dl_argc -= 2;
1262 _dl_argv += 2;
1264 else if (strcmp (_dl_argv[1], "--glibc-hwcaps-prepend") == 0
1265 && _dl_argc > 2)
1267 state.glibc_hwcaps_prepend = _dl_argv[2];
1268 _dl_skip_args += 2;
1269 _dl_argc -= 2;
1270 _dl_argv += 2;
1272 else if (strcmp (_dl_argv[1], "--glibc-hwcaps-mask") == 0
1273 && _dl_argc > 2)
1275 state.glibc_hwcaps_mask = _dl_argv[2];
1276 _dl_skip_args += 2;
1277 _dl_argc -= 2;
1278 _dl_argv += 2;
1280 #if HAVE_TUNABLES
1281 else if (! strcmp (_dl_argv[1], "--list-tunables"))
1283 state.mode = rtld_mode_list_tunables;
1285 ++_dl_skip_args;
1286 --_dl_argc;
1287 ++_dl_argv;
1289 #endif
1290 else if (strcmp (_dl_argv[1], "--help") == 0)
1292 state.mode = rtld_mode_help;
1293 --_dl_argc;
1294 ++_dl_argv;
1296 else if (strcmp (_dl_argv[1], "--version") == 0)
1297 _dl_version ();
1298 else if (_dl_argv[1][0] == '-' && _dl_argv[1][1] == '-')
1300 if (_dl_argv[1][1] == '\0')
1301 /* End of option list. */
1302 break;
1303 else
1304 /* Unrecognized option. */
1305 _dl_usage (ld_so_name, _dl_argv[1]);
1307 else
1308 break;
1310 #if HAVE_TUNABLES
1311 if (__glibc_unlikely (state.mode == rtld_mode_list_tunables))
1313 __tunables_print ();
1314 _exit (0);
1316 #endif
1318 /* If we have no further argument the program was called incorrectly.
1319 Grant the user some education. */
1320 if (_dl_argc < 2)
1322 if (state.mode == rtld_mode_help)
1323 /* --help without an executable is not an error. */
1324 _dl_help (ld_so_name, &state);
1325 else
1326 _dl_usage (ld_so_name, NULL);
1329 ++_dl_skip_args;
1330 --_dl_argc;
1331 ++_dl_argv;
1333 /* The initialization of _dl_stack_flags done below assumes the
1334 executable's PT_GNU_STACK may have been honored by the kernel, and
1335 so a PT_GNU_STACK with PF_X set means the stack started out with
1336 execute permission. However, this is not really true if the
1337 dynamic linker is the executable the kernel loaded. For this
1338 case, we must reinitialize _dl_stack_flags to match the dynamic
1339 linker itself. If the dynamic linker was built with a
1340 PT_GNU_STACK, then the kernel may have loaded us with a
1341 nonexecutable stack that we will have to make executable when we
1342 load the program below unless it has a PT_GNU_STACK indicating
1343 nonexecutable stack is ok. */
1345 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1346 if (ph->p_type == PT_GNU_STACK)
1348 GL(dl_stack_flags) = ph->p_flags;
1349 break;
1352 if (__glibc_unlikely (state.mode == rtld_mode_verify
1353 || state.mode == rtld_mode_help))
1355 const char *objname;
1356 const char *err_str = NULL;
1357 struct map_args args;
1358 bool malloced;
1360 args.str = rtld_progname;
1361 args.loader = NULL;
1362 args.mode = __RTLD_OPENEXEC;
1363 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
1364 &args);
1365 if (__glibc_unlikely (err_str != NULL))
1367 /* We don't free the returned string, the programs stops
1368 anyway. */
1369 if (state.mode == rtld_mode_help)
1370 /* Mask the failure to load the main object. The help
1371 message contains less information in this case. */
1372 _dl_help (ld_so_name, &state);
1373 else
1374 _exit (EXIT_FAILURE);
1377 else
1379 RTLD_TIMING_VAR (start);
1380 rtld_timer_start (&start);
1381 _dl_map_object (NULL, rtld_progname, lt_executable, 0,
1382 __RTLD_OPENEXEC, LM_ID_BASE);
1383 rtld_timer_stop (&load_time, start);
1386 /* Now the map for the main executable is available. */
1387 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1389 if (__glibc_likely (state.mode == rtld_mode_normal)
1390 && GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1391 && main_map->l_info[DT_SONAME] != NULL
1392 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1393 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
1394 (const char *) D_PTR (main_map, l_info[DT_STRTAB])
1395 + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
1396 _dl_fatal_printf ("loader cannot load itself\n");
1398 phdr = main_map->l_phdr;
1399 phnum = main_map->l_phnum;
1400 /* We overwrite here a pointer to a malloc()ed string. But since
1401 the malloc() implementation used at this point is the dummy
1402 implementations which has no real free() function it does not
1403 makes sense to free the old string first. */
1404 main_map->l_name = (char *) "";
1405 *user_entry = main_map->l_entry;
1407 #ifdef HAVE_AUX_VECTOR
1408 /* Adjust the on-stack auxiliary vector so that it looks like the
1409 binary was executed directly. */
1410 for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
1411 switch (av->a_type)
1413 case AT_PHDR:
1414 av->a_un.a_val = (uintptr_t) phdr;
1415 break;
1416 case AT_PHNUM:
1417 av->a_un.a_val = phnum;
1418 break;
1419 case AT_ENTRY:
1420 av->a_un.a_val = *user_entry;
1421 break;
1422 case AT_EXECFN:
1423 av->a_un.a_val = (uintptr_t) _dl_argv[0];
1424 break;
1426 #endif
1428 /* Set the argv[0] string now that we've processed the executable. */
1429 if (argv0 != NULL)
1430 _dl_argv[0] = argv0;
1432 else
1434 /* Create a link_map for the executable itself.
1435 This will be what dlopen on "" returns. */
1436 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
1437 __RTLD_OPENEXEC, LM_ID_BASE);
1438 assert (main_map != NULL);
1439 main_map->l_phdr = phdr;
1440 main_map->l_phnum = phnum;
1441 main_map->l_entry = *user_entry;
1443 /* Even though the link map is not yet fully initialized we can add
1444 it to the map list since there are no possible users running yet. */
1445 _dl_add_to_namespace_list (main_map, LM_ID_BASE);
1446 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1448 /* At this point we are in a bit of trouble. We would have to
1449 fill in the values for l_dev and l_ino. But in general we
1450 do not know where the file is. We also do not handle AT_EXECFD
1451 even if it would be passed up.
1453 We leave the values here defined to 0. This is normally no
1454 problem as the program code itself is normally no shared
1455 object and therefore cannot be loaded dynamically. Nothing
1456 prevent the use of dynamic binaries and in these situations
1457 we might get problems. We might not be able to find out
1458 whether the object is already loaded. But since there is no
1459 easy way out and because the dynamic binary must also not
1460 have an SONAME we ignore this program for now. If it becomes
1461 a problem we can force people using SONAMEs. */
1463 /* We delay initializing the path structure until we got the dynamic
1464 information for the program. */
1467 main_map->l_map_end = 0;
1468 main_map->l_text_end = 0;
1469 /* Perhaps the executable has no PT_LOAD header entries at all. */
1470 main_map->l_map_start = ~0;
1471 /* And it was opened directly. */
1472 ++main_map->l_direct_opencount;
1474 /* Scan the program header table for the dynamic section. */
1475 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1476 switch (ph->p_type)
1478 case PT_PHDR:
1479 /* Find out the load address. */
1480 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1481 break;
1482 case PT_DYNAMIC:
1483 /* This tells us where to find the dynamic section,
1484 which tells us everything we need to do. */
1485 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1486 break;
1487 case PT_INTERP:
1488 /* This "interpreter segment" was used by the program loader to
1489 find the program interpreter, which is this program itself, the
1490 dynamic linker. We note what name finds us, so that a future
1491 dlopen call or DT_NEEDED entry, for something that wants to link
1492 against the dynamic linker as a shared library, will know that
1493 the shared object is already loaded. */
1494 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1495 + ph->p_vaddr);
1496 /* _dl_rtld_libname.next = NULL; Already zero. */
1497 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1499 /* Ordinarilly, we would get additional names for the loader from
1500 our DT_SONAME. This can't happen if we were actually linked as
1501 a static executable (detect this case when we have no DYNAMIC).
1502 If so, assume the filename component of the interpreter path to
1503 be our SONAME, and add it to our name list. */
1504 if (GL(dl_rtld_map).l_ld == NULL)
1506 const char *p = NULL;
1507 const char *cp = _dl_rtld_libname.name;
1509 /* Find the filename part of the path. */
1510 while (*cp != '\0')
1511 if (*cp++ == '/')
1512 p = cp;
1514 if (p != NULL)
1516 _dl_rtld_libname2.name = p;
1517 /* _dl_rtld_libname2.next = NULL; Already zero. */
1518 _dl_rtld_libname.next = &_dl_rtld_libname2;
1522 has_interp = true;
1523 break;
1524 case PT_LOAD:
1526 ElfW(Addr) mapstart;
1527 ElfW(Addr) allocend;
1529 /* Remember where the main program starts in memory. */
1530 mapstart = (main_map->l_addr
1531 + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1532 if (main_map->l_map_start > mapstart)
1533 main_map->l_map_start = mapstart;
1535 /* Also where it ends. */
1536 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1537 if (main_map->l_map_end < allocend)
1538 main_map->l_map_end = allocend;
1539 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1540 main_map->l_text_end = allocend;
1542 break;
1544 case PT_TLS:
1545 if (ph->p_memsz > 0)
1547 /* Note that in the case the dynamic linker we duplicate work
1548 here since we read the PT_TLS entry already in
1549 _dl_start_final. But the result is repeatable so do not
1550 check for this special but unimportant case. */
1551 main_map->l_tls_blocksize = ph->p_memsz;
1552 main_map->l_tls_align = ph->p_align;
1553 if (ph->p_align == 0)
1554 main_map->l_tls_firstbyte_offset = 0;
1555 else
1556 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1557 & (ph->p_align - 1));
1558 main_map->l_tls_initimage_size = ph->p_filesz;
1559 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1561 /* This image gets the ID one. */
1562 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1564 break;
1566 case PT_GNU_STACK:
1567 GL(dl_stack_flags) = ph->p_flags;
1568 break;
1570 case PT_GNU_RELRO:
1571 main_map->l_relro_addr = ph->p_vaddr;
1572 main_map->l_relro_size = ph->p_memsz;
1573 break;
1575 /* Process program headers again, but scan them backwards so
1576 that PT_NOTE can be skipped if PT_GNU_PROPERTY exits. */
1577 for (ph = &phdr[phnum]; ph != phdr; --ph)
1578 switch (ph[-1].p_type)
1580 case PT_NOTE:
1581 _dl_process_pt_note (main_map, -1, &ph[-1]);
1582 break;
1583 case PT_GNU_PROPERTY:
1584 _dl_process_pt_gnu_property (main_map, -1, &ph[-1]);
1585 break;
1588 /* Adjust the address of the TLS initialization image in case
1589 the executable is actually an ET_DYN object. */
1590 if (main_map->l_tls_initimage != NULL)
1591 main_map->l_tls_initimage
1592 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1593 if (! main_map->l_map_end)
1594 main_map->l_map_end = ~0;
1595 if (! main_map->l_text_end)
1596 main_map->l_text_end = ~0;
1597 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1599 /* We were invoked directly, so the program might not have a
1600 PT_INTERP. */
1601 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1602 /* _dl_rtld_libname.next = NULL; Already zero. */
1603 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1605 else
1606 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1608 /* If the current libname is different from the SONAME, add the
1609 latter as well. */
1610 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1611 && strcmp (GL(dl_rtld_map).l_libname->name,
1612 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1613 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1615 static struct libname_list newname;
1616 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1617 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1618 newname.next = NULL;
1619 newname.dont_free = 1;
1621 assert (GL(dl_rtld_map).l_libname->next == NULL);
1622 GL(dl_rtld_map).l_libname->next = &newname;
1624 /* The ld.so must be relocated since otherwise loading audit modules
1625 will fail since they reuse the very same ld.so. */
1626 assert (GL(dl_rtld_map).l_relocated);
1628 if (! rtld_is_main)
1630 /* Extract the contents of the dynamic section for easy access. */
1631 elf_get_dynamic_info (main_map, NULL);
1633 /* If the main map is libc.so, update the base namespace to
1634 refer to this map. If libc.so is loaded later, this happens
1635 in _dl_map_object_from_fd. */
1636 if (main_map->l_info[DT_SONAME] != NULL
1637 && (strcmp (((const char *) D_PTR (main_map, l_info[DT_STRTAB])
1638 + main_map->l_info[DT_SONAME]->d_un.d_val), LIBC_SO)
1639 == 0))
1640 GL(dl_ns)[LM_ID_BASE].libc_map = main_map;
1642 /* Set up our cache of pointers into the hash table. */
1643 _dl_setup_hash (main_map);
1646 if (__glibc_unlikely (state.mode == rtld_mode_verify))
1648 /* We were called just to verify that this is a dynamic
1649 executable using us as the program interpreter. Exit with an
1650 error if we were not able to load the binary or no interpreter
1651 is specified (i.e., this is no dynamically linked binary. */
1652 if (main_map->l_ld == NULL)
1653 _exit (1);
1655 /* We allow here some platform specific code. */
1656 #ifdef DISTINGUISH_LIB_VERSIONS
1657 DISTINGUISH_LIB_VERSIONS;
1658 #endif
1659 _exit (has_interp ? 0 : 2);
1662 struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1663 /* Set up the data structures for the system-supplied DSO early,
1664 so they can influence _dl_init_paths. */
1665 setup_vdso (main_map, &first_preload);
1667 /* With vDSO setup we can initialize the function pointers. */
1668 setup_vdso_pointers ();
1670 #ifdef DL_SYSDEP_OSCHECK
1671 DL_SYSDEP_OSCHECK (_dl_fatal_printf);
1672 #endif
1674 /* Initialize the data structures for the search paths for shared
1675 objects. */
1676 call_init_paths (&state);
1678 /* Initialize _r_debug. */
1679 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1680 LM_ID_BASE);
1681 r->r_state = RT_CONSISTENT;
1683 /* Put the link_map for ourselves on the chain so it can be found by
1684 name. Note that at this point the global chain of link maps contains
1685 exactly one element, which is pointed to by dl_loaded. */
1686 if (! GL(dl_rtld_map).l_name)
1687 /* If not invoked directly, the dynamic linker shared object file was
1688 found by the PT_INTERP name. */
1689 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1690 GL(dl_rtld_map).l_type = lt_library;
1691 main_map->l_next = &GL(dl_rtld_map);
1692 GL(dl_rtld_map).l_prev = main_map;
1693 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1694 ++GL(dl_load_adds);
1696 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1697 to not using bias for non-prelinked PIEs and libraries
1698 and using it for executables or prelinked PIEs or libraries. */
1699 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1700 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1702 /* Set up the program header information for the dynamic linker
1703 itself. It is needed in the dl_iterate_phdr callbacks. */
1704 const ElfW(Ehdr) *rtld_ehdr;
1706 /* Starting from binutils-2.23, the linker will define the magic symbol
1707 __ehdr_start to point to our own ELF header if it is visible in a
1708 segment that also includes the phdrs. If that's not available, we use
1709 the old method that assumes the beginning of the file is part of the
1710 lowest-addressed PT_LOAD segment. */
1711 #ifdef HAVE_EHDR_START
1712 extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden")));
1713 rtld_ehdr = &__ehdr_start;
1714 #else
1715 rtld_ehdr = (void *) GL(dl_rtld_map).l_map_start;
1716 #endif
1717 assert (rtld_ehdr->e_ehsize == sizeof *rtld_ehdr);
1718 assert (rtld_ehdr->e_phentsize == sizeof (ElfW(Phdr)));
1720 const ElfW(Phdr) *rtld_phdr = (const void *) rtld_ehdr + rtld_ehdr->e_phoff;
1722 GL(dl_rtld_map).l_phdr = rtld_phdr;
1723 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1726 /* PT_GNU_RELRO is usually the last phdr. */
1727 size_t cnt = rtld_ehdr->e_phnum;
1728 while (cnt-- > 0)
1729 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1731 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1732 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1733 break;
1736 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1737 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1738 /* Assign a module ID. Do this before loading any audit modules. */
1739 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1741 audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_AUDIT);
1742 audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_DEPAUDIT);
1744 /* At this point, all data has been obtained that is included in the
1745 --help output. */
1746 if (__glibc_unlikely (state.mode == rtld_mode_help))
1747 _dl_help (ld_so_name, &state);
1749 /* If we have auditing DSOs to load, do it now. */
1750 bool need_security_init = true;
1751 if (state.audit_list.length > 0)
1753 size_t naudit = audit_list_count (&state.audit_list);
1755 /* Since we start using the auditing DSOs right away we need to
1756 initialize the data structures now. */
1757 tcbp = init_tls (naudit);
1759 /* Initialize security features. We need to do it this early
1760 since otherwise the constructors of the audit libraries will
1761 use different values (especially the pointer guard) and will
1762 fail later on. */
1763 security_init ();
1764 need_security_init = false;
1766 load_audit_modules (main_map, &state.audit_list);
1768 /* The count based on audit strings may overestimate the number
1769 of audit modules that got loaded, but not underestimate. */
1770 assert (GLRO(dl_naudit) <= naudit);
1773 /* Keep track of the currently loaded modules to count how many
1774 non-audit modules which use TLS are loaded. */
1775 size_t count_modids = _dl_count_modids ();
1777 /* Set up debugging before the debugger is notified for the first time. */
1778 #ifdef ELF_MACHINE_DEBUG_SETUP
1779 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1780 ELF_MACHINE_DEBUG_SETUP (main_map, r);
1781 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1782 #else
1783 if (main_map->l_info[DT_DEBUG] != NULL)
1784 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1785 with the run-time address of the r_debug structure */
1786 main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1788 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1789 case you run gdb on the dynamic linker directly. */
1790 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1791 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1792 #endif
1794 /* We start adding objects. */
1795 r->r_state = RT_ADD;
1796 _dl_debug_state ();
1797 LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
1799 /* Auditing checkpoint: we are ready to signal that the initial map
1800 is being constructed. */
1801 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
1803 struct audit_ifaces *afct = GLRO(dl_audit);
1804 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1806 if (afct->activity != NULL)
1807 afct->activity (&link_map_audit_state (main_map, cnt)->cookie,
1808 LA_ACT_ADD);
1810 afct = afct->next;
1814 /* We have two ways to specify objects to preload: via environment
1815 variable and via the file /etc/ld.so.preload. The latter can also
1816 be used when security is enabled. */
1817 assert (*first_preload == NULL);
1818 struct link_map **preloads = NULL;
1819 unsigned int npreloads = 0;
1821 if (__glibc_unlikely (state.preloadlist != NULL))
1823 RTLD_TIMING_VAR (start);
1824 rtld_timer_start (&start);
1825 npreloads += handle_preload_list (state.preloadlist, main_map,
1826 "LD_PRELOAD");
1827 rtld_timer_accum (&load_time, start);
1830 if (__glibc_unlikely (state.preloadarg != NULL))
1832 RTLD_TIMING_VAR (start);
1833 rtld_timer_start (&start);
1834 npreloads += handle_preload_list (state.preloadarg, main_map,
1835 "--preload");
1836 rtld_timer_accum (&load_time, start);
1839 /* There usually is no ld.so.preload file, it should only be used
1840 for emergencies and testing. So the open call etc should usually
1841 fail. Using access() on a non-existing file is faster than using
1842 open(). So we do this first. If it succeeds we do almost twice
1843 the work but this does not matter, since it is not for production
1844 use. */
1845 static const char preload_file[] = "/etc/ld.so.preload";
1846 if (__glibc_unlikely (__access (preload_file, R_OK) == 0))
1848 /* Read the contents of the file. */
1849 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1850 PROT_READ | PROT_WRITE);
1851 if (__glibc_unlikely (file != MAP_FAILED))
1853 /* Parse the file. It contains names of libraries to be loaded,
1854 separated by white spaces or `:'. It may also contain
1855 comments introduced by `#'. */
1856 char *problem;
1857 char *runp;
1858 size_t rest;
1860 /* Eliminate comments. */
1861 runp = file;
1862 rest = file_size;
1863 while (rest > 0)
1865 char *comment = memchr (runp, '#', rest);
1866 if (comment == NULL)
1867 break;
1869 rest -= comment - runp;
1871 *comment = ' ';
1872 while (--rest > 0 && *++comment != '\n');
1875 /* We have one problematic case: if we have a name at the end of
1876 the file without a trailing terminating characters, we cannot
1877 place the \0. Handle the case separately. */
1878 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1879 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1881 problem = &file[file_size];
1882 while (problem > file && problem[-1] != ' '
1883 && problem[-1] != '\t'
1884 && problem[-1] != '\n' && problem[-1] != ':')
1885 --problem;
1887 if (problem > file)
1888 problem[-1] = '\0';
1890 else
1892 problem = NULL;
1893 file[file_size - 1] = '\0';
1896 RTLD_TIMING_VAR (start);
1897 rtld_timer_start (&start);
1899 if (file != problem)
1901 char *p;
1902 runp = file;
1903 while ((p = strsep (&runp, ": \t\n")) != NULL)
1904 if (p[0] != '\0')
1905 npreloads += do_preload (p, main_map, preload_file);
1908 if (problem != NULL)
1910 char *p = strndupa (problem, file_size - (problem - file));
1912 npreloads += do_preload (p, main_map, preload_file);
1915 rtld_timer_accum (&load_time, start);
1917 /* We don't need the file anymore. */
1918 __munmap (file, file_size);
1922 if (__glibc_unlikely (*first_preload != NULL))
1924 /* Set up PRELOADS with a vector of the preloaded libraries. */
1925 struct link_map *l = *first_preload;
1926 preloads = __alloca (npreloads * sizeof preloads[0]);
1927 i = 0;
1930 preloads[i++] = l;
1931 l = l->l_next;
1932 } while (l);
1933 assert (i == npreloads);
1936 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1937 specified some libraries to load, these are inserted before the actual
1938 dependencies in the executable's searchlist for symbol resolution. */
1940 RTLD_TIMING_VAR (start);
1941 rtld_timer_start (&start);
1942 _dl_map_object_deps (main_map, preloads, npreloads,
1943 state.mode == rtld_mode_trace, 0);
1944 rtld_timer_accum (&load_time, start);
1947 /* Mark all objects as being in the global scope. */
1948 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1949 main_map->l_searchlist.r_list[--i]->l_global = 1;
1951 /* Remove _dl_rtld_map from the chain. */
1952 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1953 if (GL(dl_rtld_map).l_next != NULL)
1954 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1956 for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1957 if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1958 break;
1960 bool rtld_multiple_ref = false;
1961 if (__glibc_likely (i < main_map->l_searchlist.r_nlist))
1963 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1964 put it back in the list of visible objects. We insert it into the
1965 chain in symbol search order because gdb uses the chain's order as
1966 its symbol search order. */
1967 rtld_multiple_ref = true;
1969 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1970 if (__glibc_likely (state.mode == rtld_mode_normal))
1972 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1973 ? main_map->l_searchlist.r_list[i + 1]
1974 : NULL);
1975 #ifdef NEED_DL_SYSINFO_DSO
1976 if (GLRO(dl_sysinfo_map) != NULL
1977 && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
1978 && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
1979 GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
1980 #endif
1982 else
1983 /* In trace mode there might be an invisible object (which we
1984 could not find) after the previous one in the search list.
1985 In this case it doesn't matter much where we put the
1986 interpreter object, so we just initialize the list pointer so
1987 that the assertion below holds. */
1988 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1990 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1991 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1992 if (GL(dl_rtld_map).l_next != NULL)
1994 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1995 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1999 /* Now let us see whether all libraries are available in the
2000 versions we need. */
2002 struct version_check_args args;
2003 args.doexit = state.mode == rtld_mode_normal;
2004 args.dotrace = state.mode == rtld_mode_trace;
2005 _dl_receive_error (print_missing_version, version_check_doit, &args);
2008 /* We do not initialize any of the TLS functionality unless any of the
2009 initial modules uses TLS. This makes dynamic loading of modules with
2010 TLS impossible, but to support it requires either eagerly doing setup
2011 now or lazily doing it later. Doing it now makes us incompatible with
2012 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
2013 used. Trying to do it lazily is too hairy to try when there could be
2014 multiple threads (from a non-TLS-using libpthread). */
2015 bool was_tls_init_tp_called = tls_init_tp_called;
2016 if (tcbp == NULL)
2017 tcbp = init_tls (0);
2019 if (__glibc_likely (need_security_init))
2020 /* Initialize security features. But only if we have not done it
2021 earlier. */
2022 security_init ();
2024 if (__glibc_unlikely (state.mode != rtld_mode_normal))
2026 /* We were run just to list the shared libraries. It is
2027 important that we do this before real relocation, because the
2028 functions we call below for output may no longer work properly
2029 after relocation. */
2030 struct link_map *l;
2032 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
2034 struct r_scope_elem *scope = &main_map->l_searchlist;
2036 for (i = 0; i < scope->r_nlist; i++)
2038 l = scope->r_list [i];
2039 if (l->l_faked)
2041 _dl_printf ("\t%s => not found\n", l->l_libname->name);
2042 continue;
2044 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
2045 GLRO(dl_trace_prelink_map) = l;
2046 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
2047 DSO_FILENAME (l->l_libname->name),
2048 DSO_FILENAME (l->l_name),
2049 (int) sizeof l->l_map_start * 2,
2050 (size_t) l->l_map_start,
2051 (int) sizeof l->l_addr * 2,
2052 (size_t) l->l_addr);
2054 if (l->l_tls_modid)
2055 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
2056 (int) sizeof l->l_tls_offset * 2,
2057 (size_t) l->l_tls_offset);
2058 else
2059 _dl_printf ("\n");
2062 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2064 /* Look through the dependencies of the main executable
2065 and determine which of them is not actually
2066 required. */
2067 struct link_map *l = main_map;
2069 /* Relocate the main executable. */
2070 struct relocate_args args = { .l = l,
2071 .reloc_mode = ((GLRO(dl_lazy)
2072 ? RTLD_LAZY : 0)
2073 | __RTLD_NOIFUNC) };
2074 _dl_receive_error (print_unresolved, relocate_doit, &args);
2076 /* This loop depends on the dependencies of the executable to
2077 correspond in number and order to the DT_NEEDED entries. */
2078 ElfW(Dyn) *dyn = main_map->l_ld;
2079 bool first = true;
2080 while (dyn->d_tag != DT_NULL)
2082 if (dyn->d_tag == DT_NEEDED)
2084 l = l->l_next;
2085 #ifdef NEED_DL_SYSINFO_DSO
2086 /* Skip the VDSO since it's not part of the list
2087 of objects we brought in via DT_NEEDED entries. */
2088 if (l == GLRO(dl_sysinfo_map))
2089 l = l->l_next;
2090 #endif
2091 if (!l->l_used)
2093 if (first)
2095 _dl_printf ("Unused direct dependencies:\n");
2096 first = false;
2099 _dl_printf ("\t%s\n", l->l_name);
2103 ++dyn;
2106 _exit (first != true);
2108 else if (! main_map->l_info[DT_NEEDED])
2109 _dl_printf ("\tstatically linked\n");
2110 else
2112 for (l = main_map->l_next; l; l = l->l_next)
2113 if (l->l_faked)
2114 /* The library was not found. */
2115 _dl_printf ("\t%s => not found\n", l->l_libname->name);
2116 else if (strcmp (l->l_libname->name, l->l_name) == 0)
2117 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
2118 (int) sizeof l->l_map_start * 2,
2119 (size_t) l->l_map_start);
2120 else
2121 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
2122 l->l_name, (int) sizeof l->l_map_start * 2,
2123 (size_t) l->l_map_start);
2126 if (__glibc_unlikely (state.mode != rtld_mode_trace))
2127 for (i = 1; i < (unsigned int) _dl_argc; ++i)
2129 const ElfW(Sym) *ref = NULL;
2130 ElfW(Addr) loadbase;
2131 lookup_t result;
2133 result = _dl_lookup_symbol_x (_dl_argv[i], main_map,
2134 &ref, main_map->l_scope,
2135 NULL, ELF_RTYPE_CLASS_PLT,
2136 DL_LOOKUP_ADD_DEPENDENCY, NULL);
2138 loadbase = LOOKUP_VALUE_ADDRESS (result, false);
2140 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
2141 _dl_argv[i],
2142 (int) sizeof ref->st_value * 2,
2143 (size_t) ref->st_value,
2144 (int) sizeof loadbase * 2, (size_t) loadbase);
2146 else
2148 /* If LD_WARN is set, warn about undefined symbols. */
2149 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
2151 /* We have to do symbol dependency testing. */
2152 struct relocate_args args;
2153 unsigned int i;
2155 args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
2156 | __RTLD_NOIFUNC);
2158 i = main_map->l_searchlist.r_nlist;
2159 while (i-- > 0)
2161 struct link_map *l = main_map->l_initfini[i];
2162 if (l != &GL(dl_rtld_map) && ! l->l_faked)
2164 args.l = l;
2165 _dl_receive_error (print_unresolved, relocate_doit,
2166 &args);
2170 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
2171 && rtld_multiple_ref)
2173 /* Mark the link map as not yet relocated again. */
2174 GL(dl_rtld_map).l_relocated = 0;
2175 _dl_relocate_object (&GL(dl_rtld_map),
2176 main_map->l_scope, __RTLD_NOIFUNC, 0);
2179 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2180 if (state.version_info)
2182 /* Print more information. This means here, print information
2183 about the versions needed. */
2184 int first = 1;
2185 struct link_map *map;
2187 for (map = main_map; map != NULL; map = map->l_next)
2189 const char *strtab;
2190 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
2191 ElfW(Verneed) *ent;
2193 if (dyn == NULL)
2194 continue;
2196 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
2197 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
2199 if (first)
2201 _dl_printf ("\n\tVersion information:\n");
2202 first = 0;
2205 _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name));
2207 while (1)
2209 ElfW(Vernaux) *aux;
2210 struct link_map *needed;
2212 needed = find_needed (strtab + ent->vn_file);
2213 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
2215 while (1)
2217 const char *fname = NULL;
2219 if (needed != NULL
2220 && match_version (strtab + aux->vna_name,
2221 needed))
2222 fname = needed->l_name;
2224 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2225 strtab + ent->vn_file,
2226 strtab + aux->vna_name,
2227 aux->vna_flags & VER_FLG_WEAK
2228 ? "[WEAK] " : "",
2229 fname ?: "not found");
2231 if (aux->vna_next == 0)
2232 /* No more symbols. */
2233 break;
2235 /* Next symbol. */
2236 aux = (ElfW(Vernaux) *) ((char *) aux
2237 + aux->vna_next);
2240 if (ent->vn_next == 0)
2241 /* No more dependencies. */
2242 break;
2244 /* Next dependency. */
2245 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
2251 _exit (0);
2254 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
2255 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
2256 && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
2258 ElfW(Lib) *liblist, *liblistend;
2259 struct link_map **r_list, **r_listend, *l;
2260 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
2262 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
2263 liblist = (ElfW(Lib) *)
2264 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
2265 liblistend = (ElfW(Lib) *)
2266 ((char *) liblist
2267 + main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
2268 r_list = main_map->l_searchlist.r_list;
2269 r_listend = r_list + main_map->l_searchlist.r_nlist;
2271 for (; r_list < r_listend && liblist < liblistend; r_list++)
2273 l = *r_list;
2275 if (l == main_map)
2276 continue;
2278 /* If the library is not mapped where it should, fail. */
2279 if (l->l_addr)
2280 break;
2282 /* Next, check if checksum matches. */
2283 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
2284 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
2285 != liblist->l_checksum)
2286 break;
2288 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
2289 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
2290 != liblist->l_time_stamp)
2291 break;
2293 if (! _dl_name_match_p (strtab + liblist->l_name, l))
2294 break;
2296 ++liblist;
2300 if (r_list == r_listend && liblist == liblistend)
2301 prelinked = true;
2303 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2304 _dl_debug_printf ("\nprelink checking: %s\n",
2305 prelinked ? "ok" : "failed");
2309 /* Now set up the variable which helps the assembler startup code. */
2310 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
2312 /* Save the information about the original global scope list since
2313 we need it in the memory handling later. */
2314 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
2316 /* Remember the last search directory added at startup, now that
2317 malloc will no longer be the one from dl-minimal.c. As a side
2318 effect, this marks ld.so as initialized, so that the rtld_active
2319 function returns true from now on. */
2320 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
2322 /* Print scope information. */
2323 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
2325 _dl_debug_printf ("\nInitial object scopes\n");
2327 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2328 _dl_show_scope (l, 0);
2331 _rtld_main_check (main_map, _dl_argv[0]);
2333 if (prelinked)
2335 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2337 ElfW(Rela) *conflict, *conflictend;
2339 RTLD_TIMING_VAR (start);
2340 rtld_timer_start (&start);
2342 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2343 conflict = (ElfW(Rela) *)
2344 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2345 conflictend = (ElfW(Rela) *)
2346 ((char *) conflict
2347 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2348 _dl_resolve_conflicts (main_map, conflict, conflictend);
2350 rtld_timer_stop (&relocate_time, start);
2353 /* The library defining malloc has already been relocated due to
2354 prelinking. Resolve the malloc symbols for the dynamic
2355 loader. */
2356 __rtld_malloc_init_real (main_map);
2358 /* Mark all the objects so we know they have been already relocated. */
2359 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2361 l->l_relocated = 1;
2362 if (l->l_relro_size)
2363 _dl_protect_relro (l);
2365 /* Add object to slot information data if necessasy. */
2366 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2367 _dl_add_to_slotinfo (l, true);
2370 else
2372 /* Now we have all the objects loaded. Relocate them all except for
2373 the dynamic linker itself. We do this in reverse order so that copy
2374 relocs of earlier objects overwrite the data written by later
2375 objects. We do not re-relocate the dynamic linker itself in this
2376 loop because that could result in the GOT entries for functions we
2377 call being changed, and that would break us. It is safe to relocate
2378 the dynamic linker out of order because it has no copy relocs (we
2379 know that because it is self-contained). */
2381 int consider_profiling = GLRO(dl_profile) != NULL;
2383 /* If we are profiling we also must do lazy reloaction. */
2384 GLRO(dl_lazy) |= consider_profiling;
2386 RTLD_TIMING_VAR (start);
2387 rtld_timer_start (&start);
2388 unsigned i = main_map->l_searchlist.r_nlist;
2389 while (i-- > 0)
2391 struct link_map *l = main_map->l_initfini[i];
2393 /* While we are at it, help the memory handling a bit. We have to
2394 mark some data structures as allocated with the fake malloc()
2395 implementation in ld.so. */
2396 struct libname_list *lnp = l->l_libname->next;
2398 while (__builtin_expect (lnp != NULL, 0))
2400 lnp->dont_free = 1;
2401 lnp = lnp->next;
2403 /* Also allocated with the fake malloc(). */
2404 l->l_free_initfini = 0;
2406 if (l != &GL(dl_rtld_map))
2407 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2408 consider_profiling);
2410 /* Add object to slot information data if necessasy. */
2411 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2412 _dl_add_to_slotinfo (l, true);
2414 rtld_timer_stop (&relocate_time, start);
2416 /* Now enable profiling if needed. Like the previous call,
2417 this has to go here because the calls it makes should use the
2418 rtld versions of the functions (particularly calloc()), but it
2419 needs to have _dl_profile_map set up by the relocator. */
2420 if (__glibc_unlikely (GL(dl_profile_map) != NULL))
2421 /* We must prepare the profiling. */
2422 _dl_start_profile ();
2425 if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2426 || count_modids != _dl_count_modids ())
2427 ++GL(dl_tls_generation);
2429 /* Now that we have completed relocation, the initializer data
2430 for the TLS blocks has its final values and we can copy them
2431 into the main thread's TLS area, which we allocated above.
2432 Note: thread-local variables must only be accessed after completing
2433 the next step. */
2434 _dl_allocate_tls_init (tcbp);
2436 /* And finally install it for the main thread. */
2437 if (! tls_init_tp_called)
2439 const char *lossage = TLS_INIT_TP (tcbp);
2440 if (__glibc_unlikely (lossage != NULL))
2441 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2442 lossage);
2443 #if THREAD_GSCOPE_IN_TCB
2444 list_add (&THREAD_SELF->list, &GL (dl_stack_user));
2445 #endif
2448 /* Make sure no new search directories have been added. */
2449 assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2451 if (! prelinked && rtld_multiple_ref)
2453 /* There was an explicit ref to the dynamic linker as a shared lib.
2454 Re-relocate ourselves with user-controlled symbol definitions.
2456 We must do this after TLS initialization in case after this
2457 re-relocation, we might call a user-supplied function
2458 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2460 /* The malloc implementation has been relocated, so resolving
2461 its symbols (and potentially calling IFUNC resolvers) is safe
2462 at this point. */
2463 __rtld_malloc_init_real (main_map);
2465 RTLD_TIMING_VAR (start);
2466 rtld_timer_start (&start);
2468 /* Mark the link map as not yet relocated again. */
2469 GL(dl_rtld_map).l_relocated = 0;
2470 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2472 rtld_timer_accum (&relocate_time, start);
2475 /* Relocation is complete. Perform early libc initialization. This
2476 is the initial libc, even if audit modules have been loaded with
2477 other libcs. */
2478 _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map, true);
2480 /* Do any necessary cleanups for the startup OS interface code.
2481 We do these now so that no calls are made after rtld re-relocation
2482 which might be resolved to different functions than we expect.
2483 We cannot do this before relocating the other objects because
2484 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2485 _dl_sysdep_start_cleanup ();
2487 #ifdef SHARED
2488 /* Auditing checkpoint: we have added all objects. */
2489 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
2491 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2492 /* Do not call the functions for any auditing object. */
2493 if (head->l_auditing == 0)
2495 struct audit_ifaces *afct = GLRO(dl_audit);
2496 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2498 if (afct->activity != NULL)
2499 afct->activity (&link_map_audit_state (head, cnt)->cookie,
2500 LA_ACT_CONSISTENT);
2502 afct = afct->next;
2506 #endif
2508 /* Notify the debugger all new objects are now ready to go. We must re-get
2509 the address since by now the variable might be in another object. */
2510 r = _dl_debug_initialize (0, LM_ID_BASE);
2511 r->r_state = RT_CONSISTENT;
2512 _dl_debug_state ();
2513 LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
2515 #if defined USE_LDCONFIG && !defined MAP_COPY
2516 /* We must munmap() the cache file. */
2517 _dl_unload_cache ();
2518 #endif
2520 /* Once we return, _dl_sysdep_start will invoke
2521 the DT_INIT functions and then *USER_ENTRY. */
2524 /* This is a little helper function for resolving symbols while
2525 tracing the binary. */
2526 static void
2527 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2528 const char *errstring)
2530 if (objname[0] == '\0')
2531 objname = RTLD_PROGNAME;
2532 _dl_error_printf ("%s (%s)\n", errstring, objname);
2535 /* This is a little helper function for resolving symbols while
2536 tracing the binary. */
2537 static void
2538 print_missing_version (int errcode __attribute__ ((unused)),
2539 const char *objname, const char *errstring)
2541 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME,
2542 objname, errstring);
2545 /* Process the string given as the parameter which explains which debugging
2546 options are enabled. */
2547 static void
2548 process_dl_debug (struct dl_main_state *state, const char *dl_debug)
2550 /* When adding new entries make sure that the maximal length of a name
2551 is correctly handled in the LD_DEBUG_HELP code below. */
2552 static const struct
2554 unsigned char len;
2555 const char name[10];
2556 const char helptext[41];
2557 unsigned short int mask;
2558 } debopts[] =
2560 #define LEN_AND_STR(str) sizeof (str) - 1, str
2561 { LEN_AND_STR ("libs"), "display library search paths",
2562 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2563 { LEN_AND_STR ("reloc"), "display relocation processing",
2564 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2565 { LEN_AND_STR ("files"), "display progress for input file",
2566 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2567 { LEN_AND_STR ("symbols"), "display symbol table processing",
2568 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2569 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2570 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2571 { LEN_AND_STR ("versions"), "display version dependencies",
2572 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2573 { LEN_AND_STR ("scopes"), "display scope information",
2574 DL_DEBUG_SCOPES },
2575 { LEN_AND_STR ("all"), "all previous options combined",
2576 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2577 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2578 | DL_DEBUG_SCOPES },
2579 { LEN_AND_STR ("statistics"), "display relocation statistics",
2580 DL_DEBUG_STATISTICS },
2581 { LEN_AND_STR ("unused"), "determined unused DSOs",
2582 DL_DEBUG_UNUSED },
2583 { LEN_AND_STR ("help"), "display this help message and exit",
2584 DL_DEBUG_HELP },
2586 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2588 /* Skip separating white spaces and commas. */
2589 while (*dl_debug != '\0')
2591 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2593 size_t cnt;
2594 size_t len = 1;
2596 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2597 && dl_debug[len] != ',' && dl_debug[len] != ':')
2598 ++len;
2600 for (cnt = 0; cnt < ndebopts; ++cnt)
2601 if (debopts[cnt].len == len
2602 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2604 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2605 state->any_debug = true;
2606 break;
2609 if (cnt == ndebopts)
2611 /* Display a warning and skip everything until next
2612 separator. */
2613 char *copy = strndupa (dl_debug, len);
2614 _dl_error_printf ("\
2615 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2618 dl_debug += len;
2619 continue;
2622 ++dl_debug;
2625 if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2627 /* In order to get an accurate picture of whether a particular
2628 DT_NEEDED entry is actually used we have to process both
2629 the PLT and non-PLT relocation entries. */
2630 GLRO(dl_lazy) = 0;
2633 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2635 size_t cnt;
2637 _dl_printf ("\
2638 Valid options for the LD_DEBUG environment variable are:\n\n");
2640 for (cnt = 0; cnt < ndebopts; ++cnt)
2641 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2642 " " + debopts[cnt].len - 3,
2643 debopts[cnt].helptext);
2645 _dl_printf ("\n\
2646 To direct the debugging output into a file instead of standard output\n\
2647 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2648 _exit (0);
2652 /* Process all environments variables the dynamic linker must recognize.
2653 Since all of them start with `LD_' we are a bit smarter while finding
2654 all the entries. */
2655 extern char **_environ attribute_hidden;
2658 static void
2659 process_envvars (struct dl_main_state *state)
2661 char **runp = _environ;
2662 char *envline;
2663 char *debug_output = NULL;
2665 /* This is the default place for profiling data file. */
2666 GLRO(dl_profile_output)
2667 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
2669 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2671 size_t len = 0;
2673 while (envline[len] != '\0' && envline[len] != '=')
2674 ++len;
2676 if (envline[len] != '=')
2677 /* This is a "LD_" variable at the end of the string without
2678 a '=' character. Ignore it since otherwise we will access
2679 invalid memory below. */
2680 continue;
2682 switch (len)
2684 case 4:
2685 /* Warning level, verbose or not. */
2686 if (memcmp (envline, "WARN", 4) == 0)
2687 GLRO(dl_verbose) = envline[5] != '\0';
2688 break;
2690 case 5:
2691 /* Debugging of the dynamic linker? */
2692 if (memcmp (envline, "DEBUG", 5) == 0)
2694 process_dl_debug (state, &envline[6]);
2695 break;
2697 if (memcmp (envline, "AUDIT", 5) == 0)
2698 audit_list_add_string (&state->audit_list, &envline[6]);
2699 break;
2701 case 7:
2702 /* Print information about versions. */
2703 if (memcmp (envline, "VERBOSE", 7) == 0)
2705 state->version_info = envline[8] != '\0';
2706 break;
2709 /* List of objects to be preloaded. */
2710 if (memcmp (envline, "PRELOAD", 7) == 0)
2712 state->preloadlist = &envline[8];
2713 break;
2716 /* Which shared object shall be profiled. */
2717 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2718 GLRO(dl_profile) = &envline[8];
2719 break;
2721 case 8:
2722 /* Do we bind early? */
2723 if (memcmp (envline, "BIND_NOW", 8) == 0)
2725 GLRO(dl_lazy) = envline[9] == '\0';
2726 break;
2728 if (memcmp (envline, "BIND_NOT", 8) == 0)
2729 GLRO(dl_bind_not) = envline[9] != '\0';
2730 break;
2732 case 9:
2733 /* Test whether we want to see the content of the auxiliary
2734 array passed up from the kernel. */
2735 if (!__libc_enable_secure
2736 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2737 _dl_show_auxv ();
2738 break;
2740 #if !HAVE_TUNABLES
2741 case 10:
2742 /* Mask for the important hardware capabilities. */
2743 if (!__libc_enable_secure
2744 && memcmp (envline, "HWCAP_MASK", 10) == 0)
2745 GLRO(dl_hwcap_mask) = _dl_strtoul (&envline[11], NULL);
2746 break;
2747 #endif
2749 case 11:
2750 /* Path where the binary is found. */
2751 if (!__libc_enable_secure
2752 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2753 GLRO(dl_origin_path) = &envline[12];
2754 break;
2756 case 12:
2757 /* The library search path. */
2758 if (!__libc_enable_secure
2759 && memcmp (envline, "LIBRARY_PATH", 12) == 0)
2761 state->library_path = &envline[13];
2762 state->library_path_source = "LD_LIBRARY_PATH";
2763 break;
2766 /* Where to place the profiling data file. */
2767 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2769 debug_output = &envline[13];
2770 break;
2773 if (!__libc_enable_secure
2774 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2775 GLRO(dl_dynamic_weak) = 1;
2776 break;
2778 case 13:
2779 /* We might have some extra environment variable with length 13
2780 to handle. */
2781 #ifdef EXTRA_LD_ENVVARS_13
2782 EXTRA_LD_ENVVARS_13
2783 #endif
2784 if (!__libc_enable_secure
2785 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2787 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2788 break;
2790 break;
2792 case 14:
2793 /* Where to place the profiling data file. */
2794 if (!__libc_enable_secure
2795 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2796 && envline[15] != '\0')
2797 GLRO(dl_profile_output) = &envline[15];
2798 break;
2800 case 16:
2801 /* The mode of the dynamic linker can be set. */
2802 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2804 state->mode = rtld_mode_trace;
2805 GLRO(dl_verbose) = 1;
2806 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2807 GLRO(dl_trace_prelink) = &envline[17];
2809 break;
2811 case 20:
2812 /* The mode of the dynamic linker can be set. */
2813 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2814 state->mode = rtld_mode_trace;
2815 break;
2817 /* We might have some extra environment variable to handle. This
2818 is tricky due to the pre-processing of the length of the name
2819 in the switch statement here. The code here assumes that added
2820 environment variables have a different length. */
2821 #ifdef EXTRA_LD_ENVVARS
2822 EXTRA_LD_ENVVARS
2823 #endif
2827 /* Extra security for SUID binaries. Remove all dangerous environment
2828 variables. */
2829 if (__builtin_expect (__libc_enable_secure, 0))
2831 static const char unsecure_envvars[] =
2832 #ifdef EXTRA_UNSECURE_ENVVARS
2833 EXTRA_UNSECURE_ENVVARS
2834 #endif
2835 UNSECURE_ENVVARS;
2836 const char *nextp;
2838 nextp = unsecure_envvars;
2841 unsetenv (nextp);
2842 /* We could use rawmemchr but this need not be fast. */
2843 nextp = (char *) (strchr) (nextp, '\0') + 1;
2845 while (*nextp != '\0');
2847 if (__access ("/etc/suid-debug", F_OK) != 0)
2849 #if !HAVE_TUNABLES
2850 unsetenv ("MALLOC_CHECK_");
2851 #endif
2852 GLRO(dl_debug_mask) = 0;
2855 if (state->mode != rtld_mode_normal)
2856 _exit (5);
2858 /* If we have to run the dynamic linker in debugging mode and the
2859 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2860 messages to this file. */
2861 else if (state->any_debug && debug_output != NULL)
2863 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2864 size_t name_len = strlen (debug_output);
2865 char buf[name_len + 12];
2866 char *startp;
2868 buf[name_len + 11] = '\0';
2869 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2870 *--startp = '.';
2871 startp = memcpy (startp - name_len, debug_output, name_len);
2873 GLRO(dl_debug_fd) = __open64_nocancel (startp, flags, DEFFILEMODE);
2874 if (GLRO(dl_debug_fd) == -1)
2875 /* We use standard output if opening the file failed. */
2876 GLRO(dl_debug_fd) = STDOUT_FILENO;
2880 #if HP_TIMING_INLINE
2881 static void
2882 print_statistics_item (const char *title, hp_timing_t time,
2883 hp_timing_t total)
2885 char cycles[HP_TIMING_PRINT_SIZE];
2886 HP_TIMING_PRINT (cycles, sizeof (cycles), time);
2888 char relative[3 * sizeof (hp_timing_t) + 2];
2889 char *cp = _itoa ((1000ULL * time) / total, relative + sizeof (relative),
2890 10, 0);
2891 /* Sets the decimal point. */
2892 char *wp = relative;
2893 switch (relative + sizeof (relative) - cp)
2895 case 3:
2896 *wp++ = *cp++;
2897 /* Fall through. */
2898 case 2:
2899 *wp++ = *cp++;
2900 /* Fall through. */
2901 case 1:
2902 *wp++ = '.';
2903 *wp++ = *cp++;
2905 *wp = '\0';
2906 _dl_debug_printf ("%s: %s cycles (%s%%)\n", title, cycles, relative);
2908 #endif
2910 /* Print the various times we collected. */
2911 static void
2912 __attribute ((noinline))
2913 print_statistics (const hp_timing_t *rtld_total_timep)
2915 #if HP_TIMING_INLINE
2917 char cycles[HP_TIMING_PRINT_SIZE];
2918 HP_TIMING_PRINT (cycles, sizeof (cycles), *rtld_total_timep);
2919 _dl_debug_printf ("\nruntime linker statistics:\n"
2920 " total startup time in dynamic loader: %s cycles\n",
2921 cycles);
2922 print_statistics_item (" time needed for relocation",
2923 relocate_time, *rtld_total_timep);
2925 #endif
2927 unsigned long int num_relative_relocations = 0;
2928 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2930 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2931 continue;
2933 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2935 for (unsigned int i = 0; i < scope->r_nlist; i++)
2937 struct link_map *l = scope->r_list [i];
2939 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2940 num_relative_relocations
2941 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2942 #ifndef ELF_MACHINE_REL_RELATIVE
2943 /* Relative relocations are processed on these architectures if
2944 library is loaded to different address than p_vaddr or
2945 if not prelinked. */
2946 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2947 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2948 #else
2949 /* On e.g. IA-64 or Alpha, relative relocations are processed
2950 only if library is loaded to different address than p_vaddr. */
2951 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2952 #endif
2953 num_relative_relocations
2954 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2958 _dl_debug_printf (" number of relocations: %lu\n"
2959 " number of relocations from cache: %lu\n"
2960 " number of relative relocations: %lu\n",
2961 GL(dl_num_relocations),
2962 GL(dl_num_cache_relocations),
2963 num_relative_relocations);
2965 #if HP_TIMING_INLINE
2966 print_statistics_item (" time needed to load objects",
2967 load_time, *rtld_total_timep);
2968 #endif