1/* Run time dynamic linker.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
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 <dl-librecon.h>
36#include <unsecvars.h>
37#include <dl-cache.h>
38#include <dl-osinfo.h>
39#include <dl-procinfo.h>
40#include <dl-prop.h>
41#include <dl-vdso.h>
42#include <dl-vdso-setup.h>
43#include <tls.h>
44#include <stap-probe.h>
45#include <stackinfo.h>
46#include <not-cancel.h>
47#include <array_length.h>
48#include <libc-early-init.h>
49#include <dl-main.h>
50#include <gnu/lib-names.h>
51#include <dl-tunables.h>
52#include <get-dynamic-info.h>
53#include <dl-execve.h>
54#include <dl-find_object.h>
55#include <dl-audit-check.h>
56
57#include <assert.h>
58
59/* This #define produces dynamic linking inline functions for
60 bootstrap relocation instead of general-purpose relocation.
61 Since ld.so must not have any undefined symbols the result
62 is trivial: always the map of ld.so itself. */
63#define RTLD_BOOTSTRAP
64#define RESOLVE_MAP(map, scope, sym, version, flags) map
65#include "dynamic-link.h"
66
67/* Must include after <dl-machine.h> for DT_MIPS definition. */
68#include <dl-debug.h>
69
70/* Only enables rtld profiling for architectures which provides non generic
71 hp-timing support. The generic support requires either syscall
72 (clock_gettime), which will incur in extra overhead on loading time.
73 Using vDSO is also an option, but it will require extra support on loader
74 to setup the vDSO pointer before its usage. */
75#if HP_TIMING_INLINE
76# define RLTD_TIMING_DECLARE(var, classifier,...) \
77 classifier hp_timing_t var __VA_ARGS__
78# define RTLD_TIMING_VAR(var) RLTD_TIMING_DECLARE (var, )
79# define RTLD_TIMING_SET(var, value) (var) = (value)
80# define RTLD_TIMING_REF(var) &(var)
81
82static inline void
83rtld_timer_start (hp_timing_t *var)
84{
85 HP_TIMING_NOW (*var);
86}
87
88static inline void
89rtld_timer_stop (hp_timing_t *var, hp_timing_t start)
90{
91 hp_timing_t stop;
92 HP_TIMING_NOW (stop);
93 HP_TIMING_DIFF (*var, start, stop);
94}
95
96static inline void
97rtld_timer_accum (hp_timing_t *sum, hp_timing_t start)
98{
99 hp_timing_t stop;
100 rtld_timer_stop (&stop, start);
101 HP_TIMING_ACCUM_NT(*sum, stop);
102}
103#else
104# define RLTD_TIMING_DECLARE(var, classifier...)
105# define RTLD_TIMING_SET(var, value)
106# define RTLD_TIMING_VAR(var)
107# define RTLD_TIMING_REF(var) 0
108# define rtld_timer_start(var)
109# define rtld_timer_stop(var, start)
110# define rtld_timer_accum(sum, start)
111#endif
112
113/* Avoid PLT use for our local calls at startup. */
114extern __typeof (__mempcpy) __mempcpy attribute_hidden;
115
116/* GCC has mental blocks about _exit. */
117extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
118#define _exit exit_internal
119
120/* Helper function to handle errors while resolving symbols. */
121static void print_unresolved (int errcode, const char *objname,
122 const char *errsting);
123
124/* Helper function to handle errors when a version is missing. */
125static void print_missing_version (int errcode, const char *objname,
126 const char *errsting);
127
128/* Print the various times we collected. */
129static void print_statistics (const hp_timing_t *total_timep);
130
131/* Creates an empty audit list. */
132static void audit_list_init (struct audit_list *);
133
134/* Add a string to the end of the audit list, for later parsing. Must
135 not be called after audit_list_next. */
136static void audit_list_add_string (struct audit_list *, const char *);
137
138/* Add the audit strings from the link map, found in the dynamic
139 segment at TG (either DT_AUDIT and DT_DEPAUDIT). Must be called
140 before audit_list_next. */
141static void audit_list_add_dynamic_tag (struct audit_list *,
142 struct link_map *,
143 unsigned int tag);
144
145/* Extract the next audit module from the audit list. Only modules
146 for which dso_name_valid_for_suid is true are returned. Must be
147 called after all the audit_list_add_string,
148 audit_list_add_dynamic_tags calls. */
149static const char *audit_list_next (struct audit_list *);
150
151/* Initialize *STATE with the defaults. */
152static void dl_main_state_init (struct dl_main_state *state);
153
154/* Process all environments variables the dynamic linker must recognize.
155 Since all of them start with `LD_' we are a bit smarter while finding
156 all the entries. */
157extern char **_environ attribute_hidden;
158static void process_envvars (struct dl_main_state *state);
159
160#ifdef DL_ARGV_NOT_RELRO
161int _dl_argc attribute_hidden;
162char **_dl_argv = NULL;
163/* Nonzero if we were run directly. */
164unsigned int _dl_skip_args attribute_hidden;
165#else
166int _dl_argc attribute_relro attribute_hidden;
167char **_dl_argv attribute_relro = NULL;
168unsigned int _dl_skip_args attribute_relro attribute_hidden;
169#endif
170rtld_hidden_data_def (_dl_argv)
171
172#ifndef THREAD_SET_STACK_GUARD
173/* Only exported for architectures that don't store the stack guard canary
174 in thread local area. */
175uintptr_t __stack_chk_guard attribute_relro;
176#endif
177
178/* Only exported for architectures that don't store the pointer guard
179 value in thread local area. */
180uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
181#ifndef THREAD_SET_POINTER_GUARD
182strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
183#endif
184
185/* Check that AT_SECURE=0, or that the passed name does not contain
186 directories and is not overly long. Reject empty names
187 unconditionally. */
188static bool
189dso_name_valid_for_suid (const char *p)
190{
191 if (__glibc_unlikely (__libc_enable_secure))
192 {
193 /* Ignore pathnames with directories for AT_SECURE=1
194 programs, and also skip overlong names. */
195 size_t len = strlen (p);
196 if (len >= SECURE_NAME_LIMIT || memchr (p, '/', len) != NULL)
197 return false;
198 }
199 return *p != '\0';
200}
201
202static void
203audit_list_init (struct audit_list *list)
204{
205 list->length = 0;
206 list->current_index = 0;
207 list->current_tail = NULL;
208}
209
210static void
211audit_list_add_string (struct audit_list *list, const char *string)
212{
213 /* Empty strings do not load anything. */
214 if (*string == '\0')
215 return;
216
217 if (list->length == array_length (list->audit_strings))
218 _dl_fatal_printf ("Fatal glibc error: Too many audit modules requested\n");
219
220 list->audit_strings[list->length++] = string;
221
222 /* Initialize processing of the first string for
223 audit_list_next. */
224 if (list->length == 1)
225 list->current_tail = string;
226}
227
228static void
229audit_list_add_dynamic_tag (struct audit_list *list, struct link_map *main_map,
230 unsigned int tag)
231{
232 ElfW(Dyn) *info = main_map->l_info[ADDRIDX (tag)];
233 const char *strtab = (const char *) D_PTR (main_map, l_info[DT_STRTAB]);
234 if (info != NULL)
235 audit_list_add_string (list, strtab + info->d_un.d_val);
236}
237
238static const char *
239audit_list_next (struct audit_list *list)
240{
241 if (list->current_tail == NULL)
242 return NULL;
243
244 while (true)
245 {
246 /* Advance to the next string in audit_strings if the current
247 string has been exhausted. */
248 while (*list->current_tail == '\0')
249 {
250 ++list->current_index;
251 if (list->current_index == list->length)
252 {
253 list->current_tail = NULL;
254 return NULL;
255 }
256 list->current_tail = list->audit_strings[list->current_index];
257 }
258
259 /* Split the in-string audit list at the next colon colon. */
260 size_t len = strcspn (list->current_tail, ":");
261 if (len > 0 && len < sizeof (list->fname))
262 {
263 memcpy (list->fname, list->current_tail, len);
264 list->fname[len] = '\0';
265 }
266 else
267 /* Mark the name as unusable for dso_name_valid_for_suid. */
268 list->fname[0] = '\0';
269
270 /* Skip over the substring and the following delimiter. */
271 list->current_tail += len;
272 if (*list->current_tail == ':')
273 ++list->current_tail;
274
275 /* If the name is valid, return it. */
276 if (dso_name_valid_for_suid (list->fname))
277 return list->fname;
278
279 /* Otherwise wrap around to find the next list element. . */
280 }
281}
282
283/* Count audit modules before they are loaded so GLRO(dl_naudit)
284 is not yet usable. */
285static size_t
286audit_list_count (struct audit_list *list)
287{
288 /* Restore the audit_list iterator state at the end. */
289 const char *saved_tail = list->current_tail;
290 size_t naudit = 0;
291
292 assert (list->current_index == 0);
293 while (audit_list_next (list) != NULL)
294 naudit++;
295 list->current_tail = saved_tail;
296 list->current_index = 0;
297 return naudit;
298}
299
300static void
301dl_main_state_init (struct dl_main_state *state)
302{
303 audit_list_init (&state->audit_list);
304 state->library_path = NULL;
305 state->library_path_source = NULL;
306 state->preloadlist = NULL;
307 state->preloadarg = NULL;
308 state->glibc_hwcaps_prepend = NULL;
309 state->glibc_hwcaps_mask = NULL;
310 state->mode = rtld_mode_normal;
311 state->any_debug = false;
312 state->version_info = false;
313}
314
315#ifndef HAVE_INLINED_SYSCALLS
316/* Set nonzero during loading and initialization of executable and
317 libraries, cleared before the executable's entry point runs. This
318 must not be initialized to nonzero, because the unused dynamic
319 linker loaded in for libc.so's "ld.so.1" dep will provide the
320 definition seen by libc.so's initializer; that value must be zero,
321 and will be since that dynamic linker's _dl_start and dl_main will
322 never be called. */
323int _dl_starting_up = 0;
324rtld_hidden_def (_dl_starting_up)
325#endif
326
327/* This is the structure which defines all variables global to ld.so
328 (except those which cannot be added for some reason). */
329struct rtld_global _rtld_global =
330 {
331 /* Get architecture specific initializer. */
332#include <dl-procruntime.c>
333 /* Generally the default presumption without further information is an
334 * executable stack but this is not true for all platforms. */
335 ._dl_stack_flags = DEFAULT_STACK_PERMS,
336#ifdef _LIBC_REENTRANT
337 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
338 ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
339 ._dl_load_tls_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
340#endif
341 ._dl_nns = 1,
342 ._dl_ns =
343 {
344#ifdef _LIBC_REENTRANT
345 [LM_ID_BASE] = { ._ns_unique_sym_table
346 = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } }
347#endif
348 }
349 };
350/* If we would use strong_alias here the compiler would see a
351 non-hidden definition. This would undo the effect of the previous
352 declaration. So spell out what strong_alias does plus add the
353 visibility attribute. */
354extern struct rtld_global _rtld_local
355 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
356
357
358/* This variable is similar to _rtld_local, but all values are
359 read-only after relocation. */
360struct rtld_global_ro _rtld_global_ro attribute_relro =
361 {
362 /* Get architecture specific initializer. */
363#include <dl-procinfo.c>
364#ifdef NEED_DL_SYSINFO
365 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
366#endif
367 ._dl_debug_fd = STDERR_FILENO,
368 ._dl_use_load_bias = -2,
369 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
370#if !HAVE_TUNABLES
371 ._dl_hwcap_mask = HWCAP_IMPORTANT,
372#endif
373 ._dl_lazy = 1,
374 ._dl_fpu_control = _FPU_DEFAULT,
375 ._dl_pagesize = EXEC_PAGESIZE,
376 ._dl_inhibit_cache = 0,
377
378 /* Function pointers. */
379 ._dl_debug_printf = _dl_debug_printf,
380 ._dl_mcount = _dl_mcount,
381 ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
382 ._dl_open = _dl_open,
383 ._dl_close = _dl_close,
384 ._dl_catch_error = _rtld_catch_error,
385 ._dl_error_free = _dl_error_free,
386 ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
387 ._dl_libc_freeres = __rtld_libc_freeres,
388#ifdef HAVE_DL_DISCOVER_OSVERSION
389 ._dl_discover_osversion = _dl_discover_osversion
390#endif
391 };
392/* If we would use strong_alias here the compiler would see a
393 non-hidden definition. This would undo the effect of the previous
394 declaration. So spell out was strong_alias does plus add the
395 visibility attribute. */
396extern struct rtld_global_ro _rtld_local_ro
397 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
398
399
400static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
401 ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv);
402
403/* These two variables cannot be moved into .data.rel.ro. */
404static struct libname_list _dl_rtld_libname;
405static struct libname_list _dl_rtld_libname2;
406
407/* Variable for statistics. */
408RLTD_TIMING_DECLARE (relocate_time, static);
409RLTD_TIMING_DECLARE (load_time, static, attribute_relro);
410RLTD_TIMING_DECLARE (start_time, static, attribute_relro);
411
412/* Additional definitions needed by TLS initialization. */
413#ifdef TLS_INIT_HELPER
414TLS_INIT_HELPER
415#endif
416
417/* Helper function for syscall implementation. */
418#ifdef DL_SYSINFO_IMPLEMENTATION
419DL_SYSINFO_IMPLEMENTATION
420#endif
421
422/* Before ld.so is relocated we must not access variables which need
423 relocations. This means variables which are exported. Variables
424 declared as static are fine. If we can mark a variable hidden this
425 is fine, too. The latter is important here. We can avoid setting
426 up a temporary link map for ld.so if we can mark _rtld_global as
427 hidden. */
428#ifdef PI_STATIC_AND_HIDDEN
429# define DONT_USE_BOOTSTRAP_MAP 1
430#endif
431
432#ifdef DONT_USE_BOOTSTRAP_MAP
433static ElfW(Addr) _dl_start_final (void *arg);
434#else
435struct dl_start_final_info
436{
437 struct link_map l;
438 RTLD_TIMING_VAR (start_time);
439};
440static ElfW(Addr) _dl_start_final (void *arg,
441 struct dl_start_final_info *info);
442#endif
443
444/* These defined magically in the linker script. */
445extern char _begin[] attribute_hidden;
446extern char _etext[] attribute_hidden;
447extern char _end[] attribute_hidden;
448
449
450#ifdef RTLD_START
451RTLD_START
452#else
453# error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
454#endif
455
456/* This is the second half of _dl_start (below). It can be inlined safely
457 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
458 references. When the tools don't permit us to avoid using a GOT entry
459 for _dl_rtld_global (no attribute_hidden support), we must make sure
460 this function is not inlined (see below). */
461
462#ifdef DONT_USE_BOOTSTRAP_MAP
463static inline ElfW(Addr) __attribute__ ((always_inline))
464_dl_start_final (void *arg)
465#else
466static ElfW(Addr) __attribute__ ((noinline))
467_dl_start_final (void *arg, struct dl_start_final_info *info)
468#endif
469{
470 ElfW(Addr) start_addr;
471
472 /* If it hasn't happen yet record the startup time. */
473 rtld_timer_start (&start_time);
474#if !defined DONT_USE_BOOTSTRAP_MAP
475 RTLD_TIMING_SET (start_time, info->start_time);
476#endif
477
478 /* Transfer data about ourselves to the permanent link_map structure. */
479#ifndef DONT_USE_BOOTSTRAP_MAP
480 GL(dl_rtld_map).l_addr = info->l.l_addr;
481 GL(dl_rtld_map).l_ld = info->l.l_ld;
482 GL(dl_rtld_map).l_ld_readonly = info->l.l_ld_readonly;
483 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
484 sizeof GL(dl_rtld_map).l_info);
485 GL(dl_rtld_map).l_mach = info->l.l_mach;
486 GL(dl_rtld_map).l_relocated = 1;
487#endif
488 _dl_setup_hash (&GL(dl_rtld_map));
489 GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
490 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
491 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
492 GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
493 /* Copy the TLS related data if necessary. */
494#ifndef DONT_USE_BOOTSTRAP_MAP
495# if NO_TLS_OFFSET != 0
496 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
497# endif
498#endif
499
500 /* Initialize the stack end variable. */
501 __libc_stack_end = __builtin_frame_address (0);
502
503 /* Call the OS-dependent function to set up life so we can do things like
504 file access. It will call `dl_main' (below) to do all the real work
505 of the dynamic linker, and then unwind our frame and run the user
506 entry point on the same stack we entered on. */
507 start_addr = _dl_sysdep_start (arg, &dl_main);
508
509 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
510 {
511 RTLD_TIMING_VAR (rtld_total_time);
512 rtld_timer_stop (&rtld_total_time, start_time);
513 print_statistics (RTLD_TIMING_REF(rtld_total_time));
514 }
515
516 return start_addr;
517}
518
519#ifdef DONT_USE_BOOTSTRAP_MAP
520# define bootstrap_map GL(dl_rtld_map)
521#else
522# define bootstrap_map info.l
523#endif
524
525static ElfW(Addr) __attribute_used__
526_dl_start (void *arg)
527{
528#ifdef DONT_USE_BOOTSTRAP_MAP
529 rtld_timer_start (&start_time);
530#else
531 struct dl_start_final_info info;
532 rtld_timer_start (&info.start_time);
533#endif
534
535 /* Partly clean the `bootstrap_map' structure up. Don't use
536 `memset' since it might not be built in or inlined and we cannot
537 make function calls at this point. Use '__builtin_memset' if we
538 know it is available. We do not have to clear the memory if we
539 do not have to use the temporary bootstrap_map. Global variables
540 are initialized to zero by default. */
541#ifndef DONT_USE_BOOTSTRAP_MAP
542# ifdef HAVE_BUILTIN_MEMSET
543 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
544# else
545 for (size_t cnt = 0;
546 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
547 ++cnt)
548 bootstrap_map.l_info[cnt] = 0;
549# endif
550#endif
551
552 /* Figure out the run-time load address of the dynamic linker itself. */
553 bootstrap_map.l_addr = elf_machine_load_address ();
554
555 /* Read our own dynamic section and fill in the info array. */
556 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
557 bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION;
558 elf_get_dynamic_info (&bootstrap_map, true, false);
559
560#if NO_TLS_OFFSET != 0
561 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
562#endif
563
564#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
565 ELF_MACHINE_BEFORE_RTLD_RELOC (&bootstrap_map, bootstrap_map.l_info);
566#endif
567
568 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
569 {
570 /* Relocate ourselves so we can do normal function calls and
571 data access using the global offset table. */
572
573 ELF_DYNAMIC_RELOCATE (&bootstrap_map, NULL, 0, 0, 0);
574 }
575 bootstrap_map.l_relocated = 1;
576
577 /* Please note that we don't allow profiling of this object and
578 therefore need not test whether we have to allocate the array
579 for the relocation results (as done in dl-reloc.c). */
580
581 /* Now life is sane; we can call functions and access global data.
582 Set up to use the operating system facilities, and find out from
583 the operating system's program loader where to find the program
584 header table in core. Put the rest of _dl_start into a separate
585 function, that way the compiler cannot put accesses to the GOT
586 before ELF_DYNAMIC_RELOCATE. */
587
588 __rtld_malloc_init_stubs ();
589
590 /* Do not use an initializer for these members because it would
591 intefere with __rtld_static_init. */
592 GLRO (dl_find_object) = &_dl_find_object;
593
594 {
595#ifdef DONT_USE_BOOTSTRAP_MAP
596 ElfW(Addr) entry = _dl_start_final (arg);
597#else
598 ElfW(Addr) entry = _dl_start_final (arg, &info);
599#endif
600
601#ifndef ELF_MACHINE_START_ADDRESS
602# define ELF_MACHINE_START_ADDRESS(map, start) (start)
603#endif
604
605 return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
606 }
607}
608
609
610
611/* Now life is peachy; we can do all normal operations.
612 On to the real work. */
613
614/* Some helper functions. */
615
616/* Arguments to relocate_doit. */
617struct relocate_args
618{
619 struct link_map *l;
620 int reloc_mode;
621};
622
623struct map_args
624{
625 /* Argument to map_doit. */
626 const char *str;
627 struct link_map *loader;
628 int mode;
629 /* Return value of map_doit. */
630 struct link_map *map;
631};
632
633struct dlmopen_args
634{
635 const char *fname;
636 struct link_map *map;
637};
638
639struct lookup_args
640{
641 const char *name;
642 struct link_map *map;
643 void *result;
644};
645
646/* Arguments to version_check_doit. */
647struct version_check_args
648{
649 int doexit;
650 int dotrace;
651};
652
653static void
654relocate_doit (void *a)
655{
656 struct relocate_args *args = (struct relocate_args *) a;
657
658 _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0);
659}
660
661static void
662map_doit (void *a)
663{
664 struct map_args *args = (struct map_args *) a;
665 int type = (args->mode == __RTLD_OPENEXEC) ? lt_executable : lt_library;
666 args->map = _dl_map_object (args->loader, args->str, type, 0,
667 args->mode, LM_ID_BASE);
668}
669
670static void
671dlmopen_doit (void *a)
672{
673 struct dlmopen_args *args = (struct dlmopen_args *) a;
674 args->map = _dl_open (args->fname,
675 (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT
676 | __RTLD_SECURE),
677 dl_main, LM_ID_NEWLM, _dl_argc, _dl_argv,
678 __environ);
679}
680
681static void
682lookup_doit (void *a)
683{
684 struct lookup_args *args = (struct lookup_args *) a;
685 const ElfW(Sym) *ref = NULL;
686 args->result = NULL;
687 lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
688 args->map->l_local_scope, NULL, 0,
689 DL_LOOKUP_RETURN_NEWEST, NULL);
690 if (ref != NULL)
691 args->result = DL_SYMBOL_ADDRESS (l, ref);
692}
693
694static void
695version_check_doit (void *a)
696{
697 struct version_check_args *args = (struct version_check_args *) a;
698 if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
699 args->dotrace) && args->doexit)
700 /* We cannot start the application. Abort now. */
701 _exit (1);
702}
703
704
705static inline struct link_map *
706find_needed (const char *name)
707{
708 struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
709 unsigned int n = scope->r_nlist;
710
711 while (n-- > 0)
712 if (_dl_name_match_p (name, scope->r_list[n]))
713 return scope->r_list[n];
714
715 /* Should never happen. */
716 return NULL;
717}
718
719static int
720match_version (const char *string, struct link_map *map)
721{
722 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
723 ElfW(Verdef) *def;
724
725#define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
726 if (map->l_info[VERDEFTAG] == NULL)
727 /* The file has no symbol versioning. */
728 return 0;
729
730 def = (ElfW(Verdef) *) ((char *) map->l_addr
731 + map->l_info[VERDEFTAG]->d_un.d_ptr);
732 while (1)
733 {
734 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
735
736 /* Compare the version strings. */
737 if (strcmp (string, strtab + aux->vda_name) == 0)
738 /* Bingo! */
739 return 1;
740
741 /* If no more definitions we failed to find what we want. */
742 if (def->vd_next == 0)
743 break;
744
745 /* Next definition. */
746 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
747 }
748
749 return 0;
750}
751
752static bool tls_init_tp_called;
753
754static void *
755init_tls (size_t naudit)
756{
757 /* Number of elements in the static TLS block. */
758 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
759
760 /* Do not do this twice. The audit interface might have required
761 the DTV interfaces to be set up early. */
762 if (GL(dl_initial_dtv) != NULL)
763 return NULL;
764
765 /* Allocate the array which contains the information about the
766 dtv slots. We allocate a few entries more than needed to
767 avoid the need for reallocation. */
768 size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
769
770 /* Allocate. */
771 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
772 calloc (sizeof (struct dtv_slotinfo_list)
773 + nelem * sizeof (struct dtv_slotinfo), 1);
774 /* No need to check the return value. If memory allocation failed
775 the program would have been terminated. */
776
777 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
778 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
779 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
780
781 /* Fill in the information from the loaded modules. No namespace
782 but the base one can be filled at this time. */
783 assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
784 int i = 0;
785 for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
786 l = l->l_next)
787 if (l->l_tls_blocksize != 0)
788 {
789 /* This is a module with TLS data. Store the map reference.
790 The generation counter is zero. */
791 slotinfo[i].map = l;
792 /* slotinfo[i].gen = 0; */
793 ++i;
794 }
795 assert (i == GL(dl_tls_max_dtv_idx));
796
797 /* Calculate the size of the static TLS surplus. */
798 _dl_tls_static_surplus_init (naudit);
799
800 /* Compute the TLS offsets for the various blocks. */
801 _dl_determine_tlsoffset ();
802
803 /* Construct the static TLS block and the dtv for the initial
804 thread. For some platforms this will include allocating memory
805 for the thread descriptor. The memory for the TLS block will
806 never be freed. It should be allocated accordingly. The dtv
807 array can be changed if dynamic loading requires it. */
808 void *tcbp = _dl_allocate_tls_storage ();
809 if (tcbp == NULL)
810 _dl_fatal_printf ("\
811cannot allocate TLS data structures for initial thread\n");
812
813 /* Store for detection of the special case by __tls_get_addr
814 so it knows not to pass this dtv to the normal realloc. */
815 GL(dl_initial_dtv) = GET_DTV (tcbp);
816
817 /* And finally install it for the main thread. */
818 const char *lossage = TLS_INIT_TP (tcbp);
819 if (__glibc_unlikely (lossage != NULL))
820 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
821 __tls_init_tp ();
822 tls_init_tp_called = true;
823
824 return tcbp;
825}
826
827static unsigned int
828do_preload (const char *fname, struct link_map *main_map, const char *where)
829{
830 const char *objname;
831 const char *err_str = NULL;
832 struct map_args args;
833 bool malloced;
834
835 args.str = fname;
836 args.loader = main_map;
837 args.mode = __RTLD_SECURE;
838
839 unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
840
841 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
842 if (__glibc_unlikely (err_str != NULL))
843 {
844 _dl_error_printf ("\
845ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
846 fname, where, err_str);
847 /* No need to call free, this is still before
848 the libc's malloc is used. */
849 }
850 else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
851 /* It is no duplicate. */
852 return 1;
853
854 /* Nothing loaded. */
855 return 0;
856}
857
858static void
859security_init (void)
860{
861 /* Set up the stack checker's canary. */
862 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
863#ifdef THREAD_SET_STACK_GUARD
864 THREAD_SET_STACK_GUARD (stack_chk_guard);
865#else
866 __stack_chk_guard = stack_chk_guard;
867#endif
868
869 /* Set up the pointer guard as well, if necessary. */
870 uintptr_t pointer_chk_guard
871 = _dl_setup_pointer_guard (_dl_random, stack_chk_guard);
872#ifdef THREAD_SET_POINTER_GUARD
873 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
874#endif
875 __pointer_chk_guard_local = pointer_chk_guard;
876
877 /* We do not need the _dl_random value anymore. The less
878 information we leave behind, the better, so clear the
879 variable. */
880 _dl_random = NULL;
881}
882
883#include <setup-vdso.h>
884
885/* The LD_PRELOAD environment variable gives list of libraries
886 separated by white space or colons that are loaded before the
887 executable's dependencies and prepended to the global scope list.
888 (If the binary is running setuid all elements containing a '/' are
889 ignored since it is insecure.) Return the number of preloads
890 performed. Ditto for --preload command argument. */
891unsigned int
892handle_preload_list (const char *preloadlist, struct link_map *main_map,
893 const char *where)
894{
895 unsigned int npreloads = 0;
896 const char *p = preloadlist;
897 char fname[SECURE_PATH_LIMIT];
898
899 while (*p != '\0')
900 {
901 /* Split preload list at space/colon. */
902 size_t len = strcspn (p, " :");
903 if (len > 0 && len < sizeof (fname))
904 {
905 memcpy (fname, p, len);
906 fname[len] = '\0';
907 }
908 else
909 fname[0] = '\0';
910
911 /* Skip over the substring and the following delimiter. */
912 p += len;
913 if (*p != '\0')
914 ++p;
915
916 if (dso_name_valid_for_suid (fname))
917 npreloads += do_preload (fname, main_map, where);
918 }
919 return npreloads;
920}
921
922/* Called if the audit DSO cannot be used: if it does not have the
923 appropriate interfaces, or it expects a more recent version library
924 version than what the dynamic linker provides. */
925static void
926unload_audit_module (struct link_map *map, int original_tls_idx)
927{
928#ifndef NDEBUG
929 Lmid_t ns = map->l_ns;
930#endif
931 _dl_close (map);
932
933 /* Make sure the namespace has been cleared entirely. */
934 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
935 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
936
937 GL(dl_tls_max_dtv_idx) = original_tls_idx;
938}
939
940/* Called to print an error message if loading of an audit module
941 failed. */
942static void
943report_audit_module_load_error (const char *name, const char *err_str,
944 bool malloced)
945{
946 _dl_error_printf ("\
947ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
948 name, err_str);
949 if (malloced)
950 free ((char *) err_str);
951}
952
953/* Load one audit module. */
954static void
955load_audit_module (const char *name, struct audit_ifaces **last_audit)
956{
957 int original_tls_idx = GL(dl_tls_max_dtv_idx);
958
959 struct dlmopen_args dlmargs;
960 dlmargs.fname = name;
961 dlmargs.map = NULL;
962
963 const char *objname;
964 const char *err_str = NULL;
965 bool malloced;
966 _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit, &dlmargs);
967 if (__glibc_unlikely (err_str != NULL))
968 {
969 report_audit_module_load_error (name, err_str, malloced);
970 return;
971 }
972
973 struct lookup_args largs;
974 largs.name = "la_version";
975 largs.map = dlmargs.map;
976 _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs);
977 if (__glibc_likely (err_str != NULL))
978 {
979 unload_audit_module (dlmargs.map, original_tls_idx);
980 report_audit_module_load_error (name, err_str, malloced);
981 return;
982 }
983
984 unsigned int (*laversion) (unsigned int) = largs.result;
985
986 /* A null symbol indicates that something is very wrong with the
987 loaded object because defined symbols are supposed to have a
988 valid, non-null address. */
989 assert (laversion != NULL);
990
991 unsigned int lav = laversion (LAV_CURRENT);
992 if (lav == 0)
993 {
994 /* Only print an error message if debugging because this can
995 happen deliberately. */
996 if (GLRO(dl_debug_mask) & DL_DEBUG_FILES)
997 _dl_debug_printf ("\
998file=%s [%lu]; audit interface function la_version returned zero; ignored.\n",
999 dlmargs.map->l_name, dlmargs.map->l_ns);
1000 unload_audit_module (dlmargs.map, original_tls_idx);
1001 return;
1002 }
1003
1004 if (!_dl_audit_check_version (lav))
1005 {
1006 _dl_debug_printf ("\
1007ERROR: audit interface '%s' requires version %d (maximum supported version %d); ignored.\n",
1008 name, lav, LAV_CURRENT);
1009 unload_audit_module (dlmargs.map, original_tls_idx);
1010 return;
1011 }
1012
1013 enum { naudit_ifaces = 8 };
1014 union
1015 {
1016 struct audit_ifaces ifaces;
1017 void (*fptr[naudit_ifaces]) (void);
1018 } *newp = malloc (sizeof (*newp));
1019 if (newp == NULL)
1020 _dl_fatal_printf ("Out of memory while loading audit modules\n");
1021
1022 /* Names of the auditing interfaces. All in one
1023 long string. */
1024 static const char audit_iface_names[] =
1025 "la_activity\0"
1026 "la_objsearch\0"
1027 "la_objopen\0"
1028 "la_preinit\0"
1029 LA_SYMBIND "\0"
1030#define STRING(s) __STRING (s)
1031 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1032 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1033 "la_objclose\0";
1034 unsigned int cnt = 0;
1035 const char *cp = audit_iface_names;
1036 do
1037 {
1038 largs.name = cp;
1039 _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs);
1040
1041 /* Store the pointer. */
1042 if (err_str == NULL && largs.result != NULL)
1043 newp->fptr[cnt] = largs.result;
1044 else
1045 newp->fptr[cnt] = NULL;
1046 ++cnt;
1047
1048 cp = rawmemchr (cp, '\0') + 1;
1049 }
1050 while (*cp != '\0');
1051 assert (cnt == naudit_ifaces);
1052
1053 /* Now append the new auditing interface to the list. */
1054 newp->ifaces.next = NULL;
1055 if (*last_audit == NULL)
1056 *last_audit = GLRO(dl_audit) = &newp->ifaces;
1057 else
1058 *last_audit = (*last_audit)->next = &newp->ifaces;
1059
1060 /* The dynamic linker link map is statically allocated, so the
1061 cookie in _dl_new_object has not happened. */
1062 link_map_audit_state (&GL (dl_rtld_map), GLRO (dl_naudit))->cookie
1063 = (intptr_t) &GL (dl_rtld_map);
1064
1065 ++GLRO(dl_naudit);
1066
1067 /* Mark the DSO as being used for auditing. */
1068 dlmargs.map->l_auditing = 1;
1069}
1070
1071/* Load all audit modules. */
1072static void
1073load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
1074{
1075 struct audit_ifaces *last_audit = NULL;
1076
1077 while (true)
1078 {
1079 const char *name = audit_list_next (audit_list);
1080 if (name == NULL)
1081 break;
1082 load_audit_module (name, &last_audit);
1083 }
1084
1085 /* Notify audit modules of the initially loaded modules (the main
1086 program and the dynamic linker itself). */
1087 if (GLRO(dl_naudit) > 0)
1088 {
1089 _dl_audit_objopen (main_map, LM_ID_BASE);
1090 _dl_audit_objopen (&GL(dl_rtld_map), LM_ID_BASE);
1091 }
1092}
1093
1094/* Check if the executable is not actualy dynamically linked, and
1095 invoke it directly in that case. */
1096static void
1097rtld_chain_load (struct link_map *main_map, char *argv0)
1098{
1099 /* The dynamic loader run against itself. */
1100 const char *rtld_soname
1101 = ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1102 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val);
1103 if (main_map->l_info[DT_SONAME] != NULL
1104 && strcmp (rtld_soname,
1105 ((const char *) D_PTR (main_map, l_info[DT_STRTAB])
1106 + main_map->l_info[DT_SONAME]->d_un.d_val)) == 0)
1107 _dl_fatal_printf ("%s: loader cannot load itself\n", rtld_soname);
1108
1109 /* With DT_NEEDED dependencies, the executable is dynamically
1110 linked. */
1111 if (__glibc_unlikely (main_map->l_info[DT_NEEDED] != NULL))
1112 return;
1113
1114 /* If the executable has program interpreter, it is dynamically
1115 linked. */
1116 for (size_t i = 0; i < main_map->l_phnum; ++i)
1117 if (main_map->l_phdr[i].p_type == PT_INTERP)
1118 return;
1119
1120 const char *pathname = _dl_argv[0];
1121 if (argv0 != NULL)
1122 _dl_argv[0] = argv0;
1123 int errcode = __rtld_execve (pathname, _dl_argv, _environ);
1124 const char *errname = strerrorname_np (errcode);
1125 if (errname != NULL)
1126 _dl_fatal_printf("%s: cannot execute %s: %s\n",
1127 rtld_soname, pathname, errname);
1128 else
1129 _dl_fatal_printf("%s: cannot execute %s: %d\n",
1130 rtld_soname, pathname, errcode);
1131}
1132
1133/* Called to complete the initialization of the link map for the main
1134 executable. Returns true if there is a PT_INTERP segment. */
1135static bool
1136rtld_setup_main_map (struct link_map *main_map)
1137{
1138 /* This have already been filled in right after _dl_new_object, or
1139 as part of _dl_map_object. */
1140 const ElfW(Phdr) *phdr = main_map->l_phdr;
1141 ElfW(Word) phnum = main_map->l_phnum;
1142
1143 bool has_interp = false;
1144
1145 main_map->l_map_end = 0;
1146 main_map->l_text_end = 0;
1147 /* Perhaps the executable has no PT_LOAD header entries at all. */
1148 main_map->l_map_start = ~0;
1149 /* And it was opened directly. */
1150 ++main_map->l_direct_opencount;
1151 main_map->l_contiguous = 1;
1152
1153 /* A PT_LOAD segment at an unexpected address will clear the
1154 l_contiguous flag. The ELF specification says that PT_LOAD
1155 segments need to be sorted in in increasing order, but perhaps
1156 not all executables follow this requirement. Having l_contiguous
1157 equal to 1 is just an optimization, so the code below does not
1158 try to sort the segments in case they are unordered.
1159
1160 There is one corner case in which l_contiguous is not set to 1,
1161 but where it could be set: If a PIE (ET_DYN) binary is loaded by
1162 glibc itself (not the kernel), it is always contiguous due to the
1163 way the glibc loader works. However, the kernel loader may still
1164 create holes in this case, and the code here still uses 0
1165 conservatively for the glibc-loaded case, too. */
1166 ElfW(Addr) expected_load_address = 0;
1167
1168 /* Scan the program header table for the dynamic section. */
1169 for (const ElfW(Phdr) *ph = phdr; ph < &phdr[phnum]; ++ph)
1170 switch (ph->p_type)
1171 {
1172 case PT_PHDR:
1173 /* Find out the load address. */
1174 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1175 break;
1176 case PT_DYNAMIC:
1177 /* This tells us where to find the dynamic section,
1178 which tells us everything we need to do. */
1179 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1180 main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0;
1181 break;
1182 case PT_INTERP:
1183 /* This "interpreter segment" was used by the program loader to
1184 find the program interpreter, which is this program itself, the
1185 dynamic linker. We note what name finds us, so that a future
1186 dlopen call or DT_NEEDED entry, for something that wants to link
1187 against the dynamic linker as a shared library, will know that
1188 the shared object is already loaded. */
1189 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1190 + ph->p_vaddr);
1191 /* _dl_rtld_libname.next = NULL; Already zero. */
1192 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1193
1194 /* Ordinarilly, we would get additional names for the loader from
1195 our DT_SONAME. This can't happen if we were actually linked as
1196 a static executable (detect this case when we have no DYNAMIC).
1197 If so, assume the filename component of the interpreter path to
1198 be our SONAME, and add it to our name list. */
1199 if (GL(dl_rtld_map).l_ld == NULL)
1200 {
1201 const char *p = NULL;
1202 const char *cp = _dl_rtld_libname.name;
1203
1204 /* Find the filename part of the path. */
1205 while (*cp != '\0')
1206 if (*cp++ == '/')
1207 p = cp;
1208
1209 if (p != NULL)
1210 {
1211 _dl_rtld_libname2.name = p;
1212 /* _dl_rtld_libname2.next = NULL; Already zero. */
1213 _dl_rtld_libname.next = &_dl_rtld_libname2;
1214 }
1215 }
1216
1217 has_interp = true;
1218 break;
1219 case PT_LOAD:
1220 {
1221 ElfW(Addr) mapstart;
1222 ElfW(Addr) allocend;
1223
1224 /* Remember where the main program starts in memory. */
1225 mapstart = (main_map->l_addr
1226 + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1227 if (main_map->l_map_start > mapstart)
1228 main_map->l_map_start = mapstart;
1229
1230 if (main_map->l_contiguous && expected_load_address != 0
1231 && expected_load_address != mapstart)
1232 main_map->l_contiguous = 0;
1233
1234 /* Also where it ends. */
1235 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1236 if (main_map->l_map_end < allocend)
1237 main_map->l_map_end = allocend;
1238 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1239 main_map->l_text_end = allocend;
1240
1241 /* The next expected address is the page following this load
1242 segment. */
1243 expected_load_address = ((allocend + GLRO(dl_pagesize) - 1)
1244 & ~(GLRO(dl_pagesize) - 1));
1245 }
1246 break;
1247
1248 case PT_TLS:
1249 if (ph->p_memsz > 0)
1250 {
1251 /* Note that in the case the dynamic linker we duplicate work
1252 here since we read the PT_TLS entry already in
1253 _dl_start_final. But the result is repeatable so do not
1254 check for this special but unimportant case. */
1255 main_map->l_tls_blocksize = ph->p_memsz;
1256 main_map->l_tls_align = ph->p_align;
1257 if (ph->p_align == 0)
1258 main_map->l_tls_firstbyte_offset = 0;
1259 else
1260 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1261 & (ph->p_align - 1));
1262 main_map->l_tls_initimage_size = ph->p_filesz;
1263 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1264
1265 /* This image gets the ID one. */
1266 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1267 }
1268 break;
1269
1270 case PT_GNU_STACK:
1271 GL(dl_stack_flags) = ph->p_flags;
1272 break;
1273
1274 case PT_GNU_RELRO:
1275 main_map->l_relro_addr = ph->p_vaddr;
1276 main_map->l_relro_size = ph->p_memsz;
1277 break;
1278 }
1279 /* Process program headers again, but scan them backwards so
1280 that PT_NOTE can be skipped if PT_GNU_PROPERTY exits. */
1281 for (const ElfW(Phdr) *ph = &phdr[phnum]; ph != phdr; --ph)
1282 switch (ph[-1].p_type)
1283 {
1284 case PT_NOTE:
1285 _dl_process_pt_note (main_map, -1, &ph[-1]);
1286 break;
1287 case PT_GNU_PROPERTY:
1288 _dl_process_pt_gnu_property (main_map, -1, &ph[-1]);
1289 break;
1290 }
1291
1292 /* Adjust the address of the TLS initialization image in case
1293 the executable is actually an ET_DYN object. */
1294 if (main_map->l_tls_initimage != NULL)
1295 main_map->l_tls_initimage
1296 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1297 if (! main_map->l_map_end)
1298 main_map->l_map_end = ~0;
1299 if (! main_map->l_text_end)
1300 main_map->l_text_end = ~0;
1301 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1302 {
1303 /* We were invoked directly, so the program might not have a
1304 PT_INTERP. */
1305 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1306 /* _dl_rtld_libname.next = NULL; Already zero. */
1307 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1308 }
1309 else
1310 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1311
1312 return has_interp;
1313}
1314
1315static void
1316dl_main (const ElfW(Phdr) *phdr,
1317 ElfW(Word) phnum,
1318 ElfW(Addr) *user_entry,
1319 ElfW(auxv_t) *auxv)
1320{
1321 struct link_map *main_map;
1322 size_t file_size;
1323 char *file;
1324 unsigned int i;
1325 bool prelinked = false;
1326 bool rtld_is_main = false;
1327 void *tcbp = NULL;
1328
1329 struct dl_main_state state;
1330 dl_main_state_init (&state);
1331
1332 __tls_pre_init_tp ();
1333
1334#if !PTHREAD_IN_LIBC
1335 /* The explicit initialization here is cheaper than processing the reloc
1336 in the _rtld_local definition's initializer. */
1337 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
1338#endif
1339
1340 /* Process the environment variable which control the behaviour. */
1341 process_envvars (&state);
1342
1343#ifndef HAVE_INLINED_SYSCALLS
1344 /* Set up a flag which tells we are just starting. */
1345 _dl_starting_up = 1;
1346#endif
1347
1348 const char *ld_so_name = _dl_argv[0];
1349 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
1350 {
1351 /* Ho ho. We are not the program interpreter! We are the program
1352 itself! This means someone ran ld.so as a command. Well, that
1353 might be convenient to do sometimes. We support it by
1354 interpreting the args like this:
1355
1356 ld.so PROGRAM ARGS...
1357
1358 The first argument is the name of a file containing an ELF
1359 executable we will load and run with the following arguments.
1360 To simplify life here, PROGRAM is searched for using the
1361 normal rules for shared objects, rather than $PATH or anything
1362 like that. We just load it and use its entry point; we don't
1363 pay attention to its PT_INTERP command (we are the interpreter
1364 ourselves). This is an easy way to test a new ld.so before
1365 installing it. */
1366 rtld_is_main = true;
1367
1368 char *argv0 = NULL;
1369
1370 /* Note the place where the dynamic linker actually came from. */
1371 GL(dl_rtld_map).l_name = rtld_progname;
1372
1373 while (_dl_argc > 1)
1374 if (! strcmp (_dl_argv[1], "--list"))
1375 {
1376 if (state.mode != rtld_mode_help)
1377 {
1378 state.mode = rtld_mode_list;
1379 /* This means do no dependency analysis. */
1380 GLRO(dl_lazy) = -1;
1381 }
1382
1383 ++_dl_skip_args;
1384 --_dl_argc;
1385 ++_dl_argv;
1386 }
1387 else if (! strcmp (_dl_argv[1], "--verify"))
1388 {
1389 if (state.mode != rtld_mode_help)
1390 state.mode = rtld_mode_verify;
1391
1392 ++_dl_skip_args;
1393 --_dl_argc;
1394 ++_dl_argv;
1395 }
1396 else if (! strcmp (_dl_argv[1], "--inhibit-cache"))
1397 {
1398 GLRO(dl_inhibit_cache) = 1;
1399 ++_dl_skip_args;
1400 --_dl_argc;
1401 ++_dl_argv;
1402 }
1403 else if (! strcmp (_dl_argv[1], "--library-path")
1404 && _dl_argc > 2)
1405 {
1406 state.library_path = _dl_argv[2];
1407 state.library_path_source = "--library-path";
1408
1409 _dl_skip_args += 2;
1410 _dl_argc -= 2;
1411 _dl_argv += 2;
1412 }
1413 else if (! strcmp (_dl_argv[1], "--inhibit-rpath")
1414 && _dl_argc > 2)
1415 {
1416 GLRO(dl_inhibit_rpath) = _dl_argv[2];
1417
1418 _dl_skip_args += 2;
1419 _dl_argc -= 2;
1420 _dl_argv += 2;
1421 }
1422 else if (! strcmp (_dl_argv[1], "--audit") && _dl_argc > 2)
1423 {
1424 audit_list_add_string (&state.audit_list, _dl_argv[2]);
1425
1426 _dl_skip_args += 2;
1427 _dl_argc -= 2;
1428 _dl_argv += 2;
1429 }
1430 else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2)
1431 {
1432 state.preloadarg = _dl_argv[2];
1433 _dl_skip_args += 2;
1434 _dl_argc -= 2;
1435 _dl_argv += 2;
1436 }
1437 else if (! strcmp (_dl_argv[1], "--argv0") && _dl_argc > 2)
1438 {
1439 argv0 = _dl_argv[2];
1440
1441 _dl_skip_args += 2;
1442 _dl_argc -= 2;
1443 _dl_argv += 2;
1444 }
1445 else if (strcmp (_dl_argv[1], "--glibc-hwcaps-prepend") == 0
1446 && _dl_argc > 2)
1447 {
1448 state.glibc_hwcaps_prepend = _dl_argv[2];
1449 _dl_skip_args += 2;
1450 _dl_argc -= 2;
1451 _dl_argv += 2;
1452 }
1453 else if (strcmp (_dl_argv[1], "--glibc-hwcaps-mask") == 0
1454 && _dl_argc > 2)
1455 {
1456 state.glibc_hwcaps_mask = _dl_argv[2];
1457 _dl_skip_args += 2;
1458 _dl_argc -= 2;
1459 _dl_argv += 2;
1460 }
1461#if HAVE_TUNABLES
1462 else if (! strcmp (_dl_argv[1], "--list-tunables"))
1463 {
1464 state.mode = rtld_mode_list_tunables;
1465
1466 ++_dl_skip_args;
1467 --_dl_argc;
1468 ++_dl_argv;
1469 }
1470#endif
1471 else if (! strcmp (_dl_argv[1], "--list-diagnostics"))
1472 {
1473 state.mode = rtld_mode_list_diagnostics;
1474
1475 ++_dl_skip_args;
1476 --_dl_argc;
1477 ++_dl_argv;
1478 }
1479 else if (strcmp (_dl_argv[1], "--help") == 0)
1480 {
1481 state.mode = rtld_mode_help;
1482 --_dl_argc;
1483 ++_dl_argv;
1484 }
1485 else if (strcmp (_dl_argv[1], "--version") == 0)
1486 _dl_version ();
1487 else if (_dl_argv[1][0] == '-' && _dl_argv[1][1] == '-')
1488 {
1489 if (_dl_argv[1][1] == '\0')
1490 /* End of option list. */
1491 break;
1492 else
1493 /* Unrecognized option. */
1494 _dl_usage (ld_so_name, _dl_argv[1]);
1495 }
1496 else
1497 break;
1498
1499#if HAVE_TUNABLES
1500 if (__glibc_unlikely (state.mode == rtld_mode_list_tunables))
1501 {
1502 __tunables_print ();
1503 _exit (0);
1504 }
1505#endif
1506
1507 if (state.mode == rtld_mode_list_diagnostics)
1508 _dl_print_diagnostics (_environ);
1509
1510 /* If we have no further argument the program was called incorrectly.
1511 Grant the user some education. */
1512 if (_dl_argc < 2)
1513 {
1514 if (state.mode == rtld_mode_help)
1515 /* --help without an executable is not an error. */
1516 _dl_help (ld_so_name, &state);
1517 else
1518 _dl_usage (ld_so_name, NULL);
1519 }
1520
1521 ++_dl_skip_args;
1522 --_dl_argc;
1523 ++_dl_argv;
1524
1525 /* The initialization of _dl_stack_flags done below assumes the
1526 executable's PT_GNU_STACK may have been honored by the kernel, and
1527 so a PT_GNU_STACK with PF_X set means the stack started out with
1528 execute permission. However, this is not really true if the
1529 dynamic linker is the executable the kernel loaded. For this
1530 case, we must reinitialize _dl_stack_flags to match the dynamic
1531 linker itself. If the dynamic linker was built with a
1532 PT_GNU_STACK, then the kernel may have loaded us with a
1533 nonexecutable stack that we will have to make executable when we
1534 load the program below unless it has a PT_GNU_STACK indicating
1535 nonexecutable stack is ok. */
1536
1537 for (const ElfW(Phdr) *ph = phdr; ph < &phdr[phnum]; ++ph)
1538 if (ph->p_type == PT_GNU_STACK)
1539 {
1540 GL(dl_stack_flags) = ph->p_flags;
1541 break;
1542 }
1543
1544 if (__glibc_unlikely (state.mode == rtld_mode_verify
1545 || state.mode == rtld_mode_help))
1546 {
1547 const char *objname;
1548 const char *err_str = NULL;
1549 struct map_args args;
1550 bool malloced;
1551
1552 args.str = rtld_progname;
1553 args.loader = NULL;
1554 args.mode = __RTLD_OPENEXEC;
1555 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
1556 &args);
1557 if (__glibc_unlikely (err_str != NULL))
1558 {
1559 /* We don't free the returned string, the programs stops
1560 anyway. */
1561 if (state.mode == rtld_mode_help)
1562 /* Mask the failure to load the main object. The help
1563 message contains less information in this case. */
1564 _dl_help (ld_so_name, &state);
1565 else
1566 _exit (EXIT_FAILURE);
1567 }
1568 }
1569 else
1570 {
1571 RTLD_TIMING_VAR (start);
1572 rtld_timer_start (&start);
1573 _dl_map_object (NULL, rtld_progname, lt_executable, 0,
1574 __RTLD_OPENEXEC, LM_ID_BASE);
1575 rtld_timer_stop (&load_time, start);
1576 }
1577
1578 /* Now the map for the main executable is available. */
1579 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1580
1581 if (__glibc_likely (state.mode == rtld_mode_normal))
1582 rtld_chain_load (main_map, argv0);
1583
1584 phdr = main_map->l_phdr;
1585 phnum = main_map->l_phnum;
1586 /* We overwrite here a pointer to a malloc()ed string. But since
1587 the malloc() implementation used at this point is the dummy
1588 implementations which has no real free() function it does not
1589 makes sense to free the old string first. */
1590 main_map->l_name = (char *) "";
1591 *user_entry = main_map->l_entry;
1592
1593 /* Set bit indicating this is the main program map. */
1594 main_map->l_main_map = 1;
1595
1596#ifdef HAVE_AUX_VECTOR
1597 /* Adjust the on-stack auxiliary vector so that it looks like the
1598 binary was executed directly. */
1599 for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
1600 switch (av->a_type)
1601 {
1602 case AT_PHDR:
1603 av->a_un.a_val = (uintptr_t) phdr;
1604 break;
1605 case AT_PHNUM:
1606 av->a_un.a_val = phnum;
1607 break;
1608 case AT_ENTRY:
1609 av->a_un.a_val = *user_entry;
1610 break;
1611 case AT_EXECFN:
1612 av->a_un.a_val = (uintptr_t) _dl_argv[0];
1613 break;
1614 }
1615#endif
1616
1617 /* Set the argv[0] string now that we've processed the executable. */
1618 if (argv0 != NULL)
1619 _dl_argv[0] = argv0;
1620 }
1621 else
1622 {
1623 /* Create a link_map for the executable itself.
1624 This will be what dlopen on "" returns. */
1625 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
1626 __RTLD_OPENEXEC, LM_ID_BASE);
1627 assert (main_map != NULL);
1628 main_map->l_phdr = phdr;
1629 main_map->l_phnum = phnum;
1630 main_map->l_entry = *user_entry;
1631
1632 /* Even though the link map is not yet fully initialized we can add
1633 it to the map list since there are no possible users running yet. */
1634 _dl_add_to_namespace_list (main_map, LM_ID_BASE);
1635 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1636
1637 /* At this point we are in a bit of trouble. We would have to
1638 fill in the values for l_dev and l_ino. But in general we
1639 do not know where the file is. We also do not handle AT_EXECFD
1640 even if it would be passed up.
1641
1642 We leave the values here defined to 0. This is normally no
1643 problem as the program code itself is normally no shared
1644 object and therefore cannot be loaded dynamically. Nothing
1645 prevent the use of dynamic binaries and in these situations
1646 we might get problems. We might not be able to find out
1647 whether the object is already loaded. But since there is no
1648 easy way out and because the dynamic binary must also not
1649 have an SONAME we ignore this program for now. If it becomes
1650 a problem we can force people using SONAMEs. */
1651
1652 /* We delay initializing the path structure until we got the dynamic
1653 information for the program. */
1654 }
1655
1656 bool has_interp = rtld_setup_main_map (main_map);
1657
1658 /* If the current libname is different from the SONAME, add the
1659 latter as well. */
1660 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1661 && strcmp (GL(dl_rtld_map).l_libname->name,
1662 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1663 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1664 {
1665 static struct libname_list newname;
1666 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1667 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1668 newname.next = NULL;
1669 newname.dont_free = 1;
1670
1671 assert (GL(dl_rtld_map).l_libname->next == NULL);
1672 GL(dl_rtld_map).l_libname->next = &newname;
1673 }
1674 /* The ld.so must be relocated since otherwise loading audit modules
1675 will fail since they reuse the very same ld.so. */
1676 assert (GL(dl_rtld_map).l_relocated);
1677
1678 if (! rtld_is_main)
1679 {
1680 /* Extract the contents of the dynamic section for easy access. */
1681 elf_get_dynamic_info (main_map, false, false);
1682
1683 /* If the main map is libc.so, update the base namespace to
1684 refer to this map. If libc.so is loaded later, this happens
1685 in _dl_map_object_from_fd. */
1686 if (main_map->l_info[DT_SONAME] != NULL
1687 && (strcmp (((const char *) D_PTR (main_map, l_info[DT_STRTAB])
1688 + main_map->l_info[DT_SONAME]->d_un.d_val), LIBC_SO)
1689 == 0))
1690 GL(dl_ns)[LM_ID_BASE].libc_map = main_map;
1691
1692 /* Set up our cache of pointers into the hash table. */
1693 _dl_setup_hash (main_map);
1694 }
1695
1696 if (__glibc_unlikely (state.mode == rtld_mode_verify))
1697 {
1698 /* We were called just to verify that this is a dynamic
1699 executable using us as the program interpreter. Exit with an
1700 error if we were not able to load the binary or no interpreter
1701 is specified (i.e., this is no dynamically linked binary. */
1702 if (main_map->l_ld == NULL)
1703 _exit (1);
1704
1705 /* We allow here some platform specific code. */
1706#ifdef DISTINGUISH_LIB_VERSIONS
1707 DISTINGUISH_LIB_VERSIONS;
1708#endif
1709 _exit (has_interp ? 0 : 2);
1710 }
1711
1712 struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1713 /* Set up the data structures for the system-supplied DSO early,
1714 so they can influence _dl_init_paths. */
1715 setup_vdso (main_map, &first_preload);
1716
1717 /* With vDSO setup we can initialize the function pointers. */
1718 setup_vdso_pointers ();
1719
1720#ifdef DL_SYSDEP_OSCHECK
1721 DL_SYSDEP_OSCHECK (_dl_fatal_printf);
1722#endif
1723
1724 /* Initialize the data structures for the search paths for shared
1725 objects. */
1726 call_init_paths (&state);
1727
1728 /* Initialize _r_debug_extended. */
1729 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1730 LM_ID_BASE);
1731 r->r_state = RT_CONSISTENT;
1732
1733 /* Put the link_map for ourselves on the chain so it can be found by
1734 name. Note that at this point the global chain of link maps contains
1735 exactly one element, which is pointed to by dl_loaded. */
1736 if (! GL(dl_rtld_map).l_name)
1737 /* If not invoked directly, the dynamic linker shared object file was
1738 found by the PT_INTERP name. */
1739 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1740 GL(dl_rtld_map).l_type = lt_library;
1741 main_map->l_next = &GL(dl_rtld_map);
1742 GL(dl_rtld_map).l_prev = main_map;
1743 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1744 ++GL(dl_load_adds);
1745
1746 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1747 to not using bias for non-prelinked PIEs and libraries
1748 and using it for executables or prelinked PIEs or libraries. */
1749 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1750 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1751
1752 /* Starting from binutils-2.23, the linker will define the magic symbol
1753 __ehdr_start to point to our own ELF header if it is visible in a
1754 segment that also includes the phdrs. If that's not available, we use
1755 the old method that assumes the beginning of the file is part of the
1756 lowest-addressed PT_LOAD segment. */
1757 extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden")));
1758
1759 /* Set up the program header information for the dynamic linker
1760 itself. It is needed in the dl_iterate_phdr callbacks. */
1761 const ElfW(Ehdr) *rtld_ehdr = &__ehdr_start;
1762 assert (rtld_ehdr->e_ehsize == sizeof *rtld_ehdr);
1763 assert (rtld_ehdr->e_phentsize == sizeof (ElfW(Phdr)));
1764
1765 const ElfW(Phdr) *rtld_phdr = (const void *) rtld_ehdr + rtld_ehdr->e_phoff;
1766
1767 GL(dl_rtld_map).l_phdr = rtld_phdr;
1768 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1769
1770
1771 /* PT_GNU_RELRO is usually the last phdr. */
1772 size_t cnt = rtld_ehdr->e_phnum;
1773 while (cnt-- > 0)
1774 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1775 {
1776 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1777 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1778 break;
1779 }
1780
1781 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1782 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1783 /* Assign a module ID. Do this before loading any audit modules. */
1784 _dl_assign_tls_modid (&GL(dl_rtld_map));
1785
1786 audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_AUDIT);
1787 audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_DEPAUDIT);
1788
1789 /* At this point, all data has been obtained that is included in the
1790 --help output. */
1791 if (__glibc_unlikely (state.mode == rtld_mode_help))
1792 _dl_help (ld_so_name, &state);
1793
1794 /* If we have auditing DSOs to load, do it now. */
1795 bool need_security_init = true;
1796 if (state.audit_list.length > 0)
1797 {
1798 size_t naudit = audit_list_count (&state.audit_list);
1799
1800 /* Since we start using the auditing DSOs right away we need to
1801 initialize the data structures now. */
1802 tcbp = init_tls (naudit);
1803
1804 /* Initialize security features. We need to do it this early
1805 since otherwise the constructors of the audit libraries will
1806 use different values (especially the pointer guard) and will
1807 fail later on. */
1808 security_init ();
1809 need_security_init = false;
1810
1811 load_audit_modules (main_map, &state.audit_list);
1812
1813 /* The count based on audit strings may overestimate the number
1814 of audit modules that got loaded, but not underestimate. */
1815 assert (GLRO(dl_naudit) <= naudit);
1816 }
1817
1818 /* Keep track of the currently loaded modules to count how many
1819 non-audit modules which use TLS are loaded. */
1820 size_t count_modids = _dl_count_modids ();
1821
1822 /* Set up debugging before the debugger is notified for the first time. */
1823 elf_setup_debug_entry (main_map, r);
1824
1825 /* We start adding objects. */
1826 r->r_state = RT_ADD;
1827 _dl_debug_state ();
1828 LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
1829
1830 /* Auditing checkpoint: we are ready to signal that the initial map
1831 is being constructed. */
1832 _dl_audit_activity_map (main_map, LA_ACT_ADD);
1833
1834 /* We have two ways to specify objects to preload: via environment
1835 variable and via the file /etc/ld.so.preload. The latter can also
1836 be used when security is enabled. */
1837 assert (*first_preload == NULL);
1838 struct link_map **preloads = NULL;
1839 unsigned int npreloads = 0;
1840
1841 if (__glibc_unlikely (state.preloadlist != NULL))
1842 {
1843 RTLD_TIMING_VAR (start);
1844 rtld_timer_start (&start);
1845 npreloads += handle_preload_list (state.preloadlist, main_map,
1846 "LD_PRELOAD");
1847 rtld_timer_accum (&load_time, start);
1848 }
1849
1850 if (__glibc_unlikely (state.preloadarg != NULL))
1851 {
1852 RTLD_TIMING_VAR (start);
1853 rtld_timer_start (&start);
1854 npreloads += handle_preload_list (state.preloadarg, main_map,
1855 "--preload");
1856 rtld_timer_accum (&load_time, start);
1857 }
1858
1859 /* There usually is no ld.so.preload file, it should only be used
1860 for emergencies and testing. So the open call etc should usually
1861 fail. Using access() on a non-existing file is faster than using
1862 open(). So we do this first. If it succeeds we do almost twice
1863 the work but this does not matter, since it is not for production
1864 use. */
1865 static const char preload_file[] = "/etc/ld.so.preload";
1866 if (__glibc_unlikely (__access (preload_file, R_OK) == 0))
1867 {
1868 /* Read the contents of the file. */
1869 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1870 PROT_READ | PROT_WRITE);
1871 if (__glibc_unlikely (file != MAP_FAILED))
1872 {
1873 /* Parse the file. It contains names of libraries to be loaded,
1874 separated by white spaces or `:'. It may also contain
1875 comments introduced by `#'. */
1876 char *problem;
1877 char *runp;
1878 size_t rest;
1879
1880 /* Eliminate comments. */
1881 runp = file;
1882 rest = file_size;
1883 while (rest > 0)
1884 {
1885 char *comment = memchr (runp, '#', rest);
1886 if (comment == NULL)
1887 break;
1888
1889 rest -= comment - runp;
1890 do
1891 *comment = ' ';
1892 while (--rest > 0 && *++comment != '\n');
1893 }
1894
1895 /* We have one problematic case: if we have a name at the end of
1896 the file without a trailing terminating characters, we cannot
1897 place the \0. Handle the case separately. */
1898 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1899 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1900 {
1901 problem = &file[file_size];
1902 while (problem > file && problem[-1] != ' '
1903 && problem[-1] != '\t'
1904 && problem[-1] != '\n' && problem[-1] != ':')
1905 --problem;
1906
1907 if (problem > file)
1908 problem[-1] = '\0';
1909 }
1910 else
1911 {
1912 problem = NULL;
1913 file[file_size - 1] = '\0';
1914 }
1915
1916 RTLD_TIMING_VAR (start);
1917 rtld_timer_start (&start);
1918
1919 if (file != problem)
1920 {
1921 char *p;
1922 runp = file;
1923 while ((p = strsep (&runp, ": \t\n")) != NULL)
1924 if (p[0] != '\0')
1925 npreloads += do_preload (p, main_map, preload_file);
1926 }
1927
1928 if (problem != NULL)
1929 {
1930 char *p = strndupa (problem, file_size - (problem - file));
1931
1932 npreloads += do_preload (p, main_map, preload_file);
1933 }
1934
1935 rtld_timer_accum (&load_time, start);
1936
1937 /* We don't need the file anymore. */
1938 __munmap (file, file_size);
1939 }
1940 }
1941
1942 if (__glibc_unlikely (*first_preload != NULL))
1943 {
1944 /* Set up PRELOADS with a vector of the preloaded libraries. */
1945 struct link_map *l = *first_preload;
1946 preloads = __alloca (npreloads * sizeof preloads[0]);
1947 i = 0;
1948 do
1949 {
1950 preloads[i++] = l;
1951 l = l->l_next;
1952 } while (l);
1953 assert (i == npreloads);
1954 }
1955
1956#ifdef NEED_DL_SYSINFO_DSO
1957 /* Now that the audit modules are opened, call la_objopen for the vDSO. */
1958 if (GLRO(dl_sysinfo_map) != NULL)
1959 _dl_audit_objopen (GLRO(dl_sysinfo_map), LM_ID_BASE);
1960#endif
1961
1962 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1963 specified some libraries to load, these are inserted before the actual
1964 dependencies in the executable's searchlist for symbol resolution. */
1965 {
1966 RTLD_TIMING_VAR (start);
1967 rtld_timer_start (&start);
1968 _dl_map_object_deps (main_map, preloads, npreloads,
1969 state.mode == rtld_mode_trace, 0);
1970 rtld_timer_accum (&load_time, start);
1971 }
1972
1973 /* Mark all objects as being in the global scope. */
1974 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1975 main_map->l_searchlist.r_list[--i]->l_global = 1;
1976
1977 /* Remove _dl_rtld_map from the chain. */
1978 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1979 if (GL(dl_rtld_map).l_next != NULL)
1980 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1981
1982 for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1983 if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1984 break;
1985
1986 bool rtld_multiple_ref = false;
1987 if (__glibc_likely (i < main_map->l_searchlist.r_nlist))
1988 {
1989 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1990 put it back in the list of visible objects. We insert it into the
1991 chain in symbol search order because gdb uses the chain's order as
1992 its symbol search order. */
1993 rtld_multiple_ref = true;
1994
1995 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1996 if (__glibc_likely (state.mode == rtld_mode_normal))
1997 {
1998 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1999 ? main_map->l_searchlist.r_list[i + 1]
2000 : NULL);
2001#ifdef NEED_DL_SYSINFO_DSO
2002 if (GLRO(dl_sysinfo_map) != NULL
2003 && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
2004 && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
2005 GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
2006#endif
2007 }
2008 else
2009 /* In trace mode there might be an invisible object (which we
2010 could not find) after the previous one in the search list.
2011 In this case it doesn't matter much where we put the
2012 interpreter object, so we just initialize the list pointer so
2013 that the assertion below holds. */
2014 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
2015
2016 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
2017 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
2018 if (GL(dl_rtld_map).l_next != NULL)
2019 {
2020 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
2021 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
2022 }
2023 }
2024
2025 /* Now let us see whether all libraries are available in the
2026 versions we need. */
2027 {
2028 struct version_check_args args;
2029 args.doexit = state.mode == rtld_mode_normal;
2030 args.dotrace = state.mode == rtld_mode_trace;
2031 _dl_receive_error (print_missing_version, version_check_doit, &args);
2032 }
2033
2034 /* We do not initialize any of the TLS functionality unless any of the
2035 initial modules uses TLS. This makes dynamic loading of modules with
2036 TLS impossible, but to support it requires either eagerly doing setup
2037 now or lazily doing it later. Doing it now makes us incompatible with
2038 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
2039 used. Trying to do it lazily is too hairy to try when there could be
2040 multiple threads (from a non-TLS-using libpthread). */
2041 bool was_tls_init_tp_called = tls_init_tp_called;
2042 if (tcbp == NULL)
2043 tcbp = init_tls (0);
2044
2045 if (__glibc_likely (need_security_init))
2046 /* Initialize security features. But only if we have not done it
2047 earlier. */
2048 security_init ();
2049
2050 if (__glibc_unlikely (state.mode != rtld_mode_normal))
2051 {
2052 /* We were run just to list the shared libraries. It is
2053 important that we do this before real relocation, because the
2054 functions we call below for output may no longer work properly
2055 after relocation. */
2056 struct link_map *l;
2057
2058 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
2059 {
2060 struct r_scope_elem *scope = &main_map->l_searchlist;
2061
2062 for (i = 0; i < scope->r_nlist; i++)
2063 {
2064 l = scope->r_list [i];
2065 if (l->l_faked)
2066 {
2067 _dl_printf ("\t%s => not found\n", l->l_libname->name);
2068 continue;
2069 }
2070 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
2071 GLRO(dl_trace_prelink_map) = l;
2072 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
2073 DSO_FILENAME (l->l_libname->name),
2074 DSO_FILENAME (l->l_name),
2075 (int) sizeof l->l_map_start * 2,
2076 (size_t) l->l_map_start,
2077 (int) sizeof l->l_addr * 2,
2078 (size_t) l->l_addr);
2079
2080 if (l->l_tls_modid)
2081 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
2082 (int) sizeof l->l_tls_offset * 2,
2083 (size_t) l->l_tls_offset);
2084 else
2085 _dl_printf ("\n");
2086 }
2087 }
2088 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2089 {
2090 /* Look through the dependencies of the main executable
2091 and determine which of them is not actually
2092 required. */
2093 struct link_map *l = main_map;
2094
2095 /* Relocate the main executable. */
2096 struct relocate_args args = { .l = l,
2097 .reloc_mode = ((GLRO(dl_lazy)
2098 ? RTLD_LAZY : 0)
2099 | __RTLD_NOIFUNC) };
2100 _dl_receive_error (print_unresolved, relocate_doit, &args);
2101
2102 /* This loop depends on the dependencies of the executable to
2103 correspond in number and order to the DT_NEEDED entries. */
2104 ElfW(Dyn) *dyn = main_map->l_ld;
2105 bool first = true;
2106 while (dyn->d_tag != DT_NULL)
2107 {
2108 if (dyn->d_tag == DT_NEEDED)
2109 {
2110 l = l->l_next;
2111#ifdef NEED_DL_SYSINFO_DSO
2112 /* Skip the VDSO since it's not part of the list
2113 of objects we brought in via DT_NEEDED entries. */
2114 if (l == GLRO(dl_sysinfo_map))
2115 l = l->l_next;
2116#endif
2117 if (!l->l_used)
2118 {
2119 if (first)
2120 {
2121 _dl_printf ("Unused direct dependencies:\n");
2122 first = false;
2123 }
2124
2125 _dl_printf ("\t%s\n", l->l_name);
2126 }
2127 }
2128
2129 ++dyn;
2130 }
2131
2132 _exit (first != true);
2133 }
2134 else if (! main_map->l_info[DT_NEEDED])
2135 _dl_printf ("\tstatically linked\n");
2136 else
2137 {
2138 for (l = main_map->l_next; l; l = l->l_next)
2139 if (l->l_faked)
2140 /* The library was not found. */
2141 _dl_printf ("\t%s => not found\n", l->l_libname->name);
2142 else if (strcmp (l->l_libname->name, l->l_name) == 0)
2143 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
2144 (int) sizeof l->l_map_start * 2,
2145 (size_t) l->l_map_start);
2146 else
2147 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
2148 l->l_name, (int) sizeof l->l_map_start * 2,
2149 (size_t) l->l_map_start);
2150 }
2151
2152 if (__glibc_unlikely (state.mode != rtld_mode_trace))
2153 for (i = 1; i < (unsigned int) _dl_argc; ++i)
2154 {
2155 const ElfW(Sym) *ref = NULL;
2156 ElfW(Addr) loadbase;
2157 lookup_t result;
2158
2159 result = _dl_lookup_symbol_x (_dl_argv[i], main_map,
2160 &ref, main_map->l_scope,
2161 NULL, ELF_RTYPE_CLASS_PLT,
2162 DL_LOOKUP_ADD_DEPENDENCY, NULL);
2163
2164 loadbase = LOOKUP_VALUE_ADDRESS (result, false);
2165
2166 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
2167 _dl_argv[i],
2168 (int) sizeof ref->st_value * 2,
2169 (size_t) ref->st_value,
2170 (int) sizeof loadbase * 2, (size_t) loadbase);
2171 }
2172 else
2173 {
2174 /* If LD_WARN is set, warn about undefined symbols. */
2175 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
2176 {
2177 /* We have to do symbol dependency testing. */
2178 struct relocate_args args;
2179 unsigned int i;
2180
2181 args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
2182 | __RTLD_NOIFUNC);
2183
2184 i = main_map->l_searchlist.r_nlist;
2185 while (i-- > 0)
2186 {
2187 struct link_map *l = main_map->l_initfini[i];
2188 if (l != &GL(dl_rtld_map) && ! l->l_faked)
2189 {
2190 args.l = l;
2191 _dl_receive_error (print_unresolved, relocate_doit,
2192 &args);
2193 }
2194 }
2195
2196 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
2197 && rtld_multiple_ref)
2198 {
2199 /* Mark the link map as not yet relocated again. */
2200 GL(dl_rtld_map).l_relocated = 0;
2201 _dl_relocate_object (&GL(dl_rtld_map),
2202 main_map->l_scope, __RTLD_NOIFUNC, 0);
2203 }
2204 }
2205#define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2206 if (state.version_info)
2207 {
2208 /* Print more information. This means here, print information
2209 about the versions needed. */
2210 int first = 1;
2211 struct link_map *map;
2212
2213 for (map = main_map; map != NULL; map = map->l_next)
2214 {
2215 const char *strtab;
2216 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
2217 ElfW(Verneed) *ent;
2218
2219 if (dyn == NULL)
2220 continue;
2221
2222 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
2223 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
2224
2225 if (first)
2226 {
2227 _dl_printf ("\n\tVersion information:\n");
2228 first = 0;
2229 }
2230
2231 _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name));
2232
2233 while (1)
2234 {
2235 ElfW(Vernaux) *aux;
2236 struct link_map *needed;
2237
2238 needed = find_needed (strtab + ent->vn_file);
2239 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
2240
2241 while (1)
2242 {
2243 const char *fname = NULL;
2244
2245 if (needed != NULL
2246 && match_version (strtab + aux->vna_name,
2247 needed))
2248 fname = needed->l_name;
2249
2250 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2251 strtab + ent->vn_file,
2252 strtab + aux->vna_name,
2253 aux->vna_flags & VER_FLG_WEAK
2254 ? "[WEAK] " : "",
2255 fname ?: "not found");
2256
2257 if (aux->vna_next == 0)
2258 /* No more symbols. */
2259 break;
2260
2261 /* Next symbol. */
2262 aux = (ElfW(Vernaux) *) ((char *) aux
2263 + aux->vna_next);
2264 }
2265
2266 if (ent->vn_next == 0)
2267 /* No more dependencies. */
2268 break;
2269
2270 /* Next dependency. */
2271 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
2272 }
2273 }
2274 }
2275 }
2276
2277 _exit (0);
2278 }
2279
2280 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
2281 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
2282 && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
2283 {
2284 ElfW(Lib) *liblist, *liblistend;
2285 struct link_map **r_list, **r_listend, *l;
2286 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
2287
2288 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
2289 liblist = (ElfW(Lib) *)
2290 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
2291 liblistend = (ElfW(Lib) *)
2292 ((char *) liblist
2293 + main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
2294 r_list = main_map->l_searchlist.r_list;
2295 r_listend = r_list + main_map->l_searchlist.r_nlist;
2296
2297 for (; r_list < r_listend && liblist < liblistend; r_list++)
2298 {
2299 l = *r_list;
2300
2301 if (l == main_map)
2302 continue;
2303
2304 /* If the library is not mapped where it should, fail. */
2305 if (l->l_addr)
2306 break;
2307
2308 /* Next, check if checksum matches. */
2309 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
2310 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
2311 != liblist->l_checksum)
2312 break;
2313
2314 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
2315 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
2316 != liblist->l_time_stamp)
2317 break;
2318
2319 if (! _dl_name_match_p (strtab + liblist->l_name, l))
2320 break;
2321
2322 ++liblist;
2323 }
2324
2325
2326 if (r_list == r_listend && liblist == liblistend)
2327 prelinked = true;
2328
2329 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
2330 _dl_debug_printf ("\nprelink checking: %s\n",
2331 prelinked ? "ok" : "failed");
2332 }
2333
2334
2335 /* Now set up the variable which helps the assembler startup code. */
2336 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
2337
2338 /* Save the information about the original global scope list since
2339 we need it in the memory handling later. */
2340 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
2341
2342 /* Remember the last search directory added at startup, now that
2343 malloc will no longer be the one from dl-minimal.c. As a side
2344 effect, this marks ld.so as initialized, so that the rtld_active
2345 function returns true from now on. */
2346 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
2347
2348 /* Print scope information. */
2349 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
2350 {
2351 _dl_debug_printf ("\nInitial object scopes\n");
2352
2353 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2354 _dl_show_scope (l, 0);
2355 }
2356
2357 _rtld_main_check (main_map, _dl_argv[0]);
2358
2359 if (prelinked)
2360 {
2361 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2362 {
2363 ElfW(Rela) *conflict, *conflictend;
2364
2365 RTLD_TIMING_VAR (start);
2366 rtld_timer_start (&start);
2367
2368 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2369 conflict = (ElfW(Rela) *)
2370 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2371 conflictend = (ElfW(Rela) *)
2372 ((char *) conflict
2373 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2374 _dl_resolve_conflicts (main_map, conflict, conflictend);
2375
2376 rtld_timer_stop (&relocate_time, start);
2377 }
2378
2379 /* Set up the object lookup structures. */
2380 _dl_find_object_init ();
2381
2382 /* The library defining malloc has already been relocated due to
2383 prelinking. Resolve the malloc symbols for the dynamic
2384 loader. */
2385 __rtld_malloc_init_real (main_map);
2386
2387 /* Likewise for the locking implementation. */
2388 __rtld_mutex_init ();
2389
2390 /* Mark all the objects so we know they have been already relocated. */
2391 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2392 {
2393 l->l_relocated = 1;
2394 if (l->l_relro_size)
2395 _dl_protect_relro (l);
2396
2397 /* Add object to slot information data if necessasy. */
2398 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2399 _dl_add_to_slotinfo (l, true);
2400 }
2401 }
2402 else
2403 {
2404 /* Now we have all the objects loaded. Relocate them all except for
2405 the dynamic linker itself. We do this in reverse order so that copy
2406 relocs of earlier objects overwrite the data written by later
2407 objects. We do not re-relocate the dynamic linker itself in this
2408 loop because that could result in the GOT entries for functions we
2409 call being changed, and that would break us. It is safe to relocate
2410 the dynamic linker out of order because it has no copy relocs (we
2411 know that because it is self-contained). */
2412
2413 int consider_profiling = GLRO(dl_profile) != NULL;
2414
2415 /* If we are profiling we also must do lazy reloaction. */
2416 GLRO(dl_lazy) |= consider_profiling;
2417
2418 RTLD_TIMING_VAR (start);
2419 rtld_timer_start (&start);
2420 unsigned i = main_map->l_searchlist.r_nlist;
2421 while (i-- > 0)
2422 {
2423 struct link_map *l = main_map->l_initfini[i];
2424
2425 /* While we are at it, help the memory handling a bit. We have to
2426 mark some data structures as allocated with the fake malloc()
2427 implementation in ld.so. */
2428 struct libname_list *lnp = l->l_libname->next;
2429
2430 while (__builtin_expect (lnp != NULL, 0))
2431 {
2432 lnp->dont_free = 1;
2433 lnp = lnp->next;
2434 }
2435 /* Also allocated with the fake malloc(). */
2436 l->l_free_initfini = 0;
2437
2438 if (l != &GL(dl_rtld_map))
2439 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2440 consider_profiling);
2441
2442 /* Add object to slot information data if necessasy. */
2443 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2444 _dl_add_to_slotinfo (l, true);
2445 }
2446 rtld_timer_stop (&relocate_time, start);
2447
2448 /* Now enable profiling if needed. Like the previous call,
2449 this has to go here because the calls it makes should use the
2450 rtld versions of the functions (particularly calloc()), but it
2451 needs to have _dl_profile_map set up by the relocator. */
2452 if (__glibc_unlikely (GL(dl_profile_map) != NULL))
2453 /* We must prepare the profiling. */
2454 _dl_start_profile ();
2455 }
2456
2457 if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2458 || count_modids != _dl_count_modids ())
2459 ++GL(dl_tls_generation);
2460
2461 /* Now that we have completed relocation, the initializer data
2462 for the TLS blocks has its final values and we can copy them
2463 into the main thread's TLS area, which we allocated above.
2464 Note: thread-local variables must only be accessed after completing
2465 the next step. */
2466 _dl_allocate_tls_init (tcbp, false);
2467
2468 /* And finally install it for the main thread. */
2469 if (! tls_init_tp_called)
2470 {
2471 const char *lossage = TLS_INIT_TP (tcbp);
2472 if (__glibc_unlikely (lossage != NULL))
2473 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2474 lossage);
2475 __tls_init_tp ();
2476 }
2477
2478 /* Make sure no new search directories have been added. */
2479 assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2480
2481 if (! prelinked && rtld_multiple_ref)
2482 {
2483 /* There was an explicit ref to the dynamic linker as a shared lib.
2484 Re-relocate ourselves with user-controlled symbol definitions.
2485
2486 We must do this after TLS initialization in case after this
2487 re-relocation, we might call a user-supplied function
2488 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2489
2490 /* Set up the object lookup structures. */
2491 _dl_find_object_init ();
2492
2493 /* The malloc implementation has been relocated, so resolving
2494 its symbols (and potentially calling IFUNC resolvers) is safe
2495 at this point. */
2496 __rtld_malloc_init_real (main_map);
2497
2498 /* Likewise for the locking implementation. */
2499 __rtld_mutex_init ();
2500
2501 RTLD_TIMING_VAR (start);
2502 rtld_timer_start (&start);
2503
2504 /* Mark the link map as not yet relocated again. */
2505 GL(dl_rtld_map).l_relocated = 0;
2506 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2507
2508 rtld_timer_accum (&relocate_time, start);
2509 }
2510
2511 /* Relocation is complete. Perform early libc initialization. This
2512 is the initial libc, even if audit modules have been loaded with
2513 other libcs. */
2514 _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map, true);
2515
2516 /* Do any necessary cleanups for the startup OS interface code.
2517 We do these now so that no calls are made after rtld re-relocation
2518 which might be resolved to different functions than we expect.
2519 We cannot do this before relocating the other objects because
2520 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2521 _dl_sysdep_start_cleanup ();
2522
2523#ifdef SHARED
2524 /* Auditing checkpoint: we have added all objects. */
2525 _dl_audit_activity_nsid (LM_ID_BASE, LA_ACT_CONSISTENT);
2526#endif
2527
2528 /* Notify the debugger all new objects are now ready to go. We must re-get
2529 the address since by now the variable might be in another object. */
2530 r = _dl_debug_update (LM_ID_BASE);
2531 r->r_state = RT_CONSISTENT;
2532 _dl_debug_state ();
2533 LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
2534
2535#if defined USE_LDCONFIG && !defined MAP_COPY
2536 /* We must munmap() the cache file. */
2537 _dl_unload_cache ();
2538#endif
2539
2540 /* Once we return, _dl_sysdep_start will invoke
2541 the DT_INIT functions and then *USER_ENTRY. */
2542}
2543
2544/* This is a little helper function for resolving symbols while
2545 tracing the binary. */
2546static void
2547print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2548 const char *errstring)
2549{
2550 if (objname[0] == '\0')
2551 objname = RTLD_PROGNAME;
2552 _dl_error_printf ("%s (%s)\n", errstring, objname);
2553}
2554
2555/* This is a little helper function for resolving symbols while
2556 tracing the binary. */
2557static void
2558print_missing_version (int errcode __attribute__ ((unused)),
2559 const char *objname, const char *errstring)
2560{
2561 _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME,
2562 objname, errstring);
2563}
2564
2565/* Process the string given as the parameter which explains which debugging
2566 options are enabled. */
2567static void
2568process_dl_debug (struct dl_main_state *state, const char *dl_debug)
2569{
2570 /* When adding new entries make sure that the maximal length of a name
2571 is correctly handled in the LD_DEBUG_HELP code below. */
2572 static const struct
2573 {
2574 unsigned char len;
2575 const char name[10];
2576 const char helptext[41];
2577 unsigned short int mask;
2578 } debopts[] =
2579 {
2580#define LEN_AND_STR(str) sizeof (str) - 1, str
2581 { LEN_AND_STR ("libs"), "display library search paths",
2582 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2583 { LEN_AND_STR ("reloc"), "display relocation processing",
2584 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2585 { LEN_AND_STR ("files"), "display progress for input file",
2586 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2587 { LEN_AND_STR ("symbols"), "display symbol table processing",
2588 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2589 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2590 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2591 { LEN_AND_STR ("versions"), "display version dependencies",
2592 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2593 { LEN_AND_STR ("scopes"), "display scope information",
2594 DL_DEBUG_SCOPES },
2595 { LEN_AND_STR ("all"), "all previous options combined",
2596 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2597 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2598 | DL_DEBUG_SCOPES },
2599 { LEN_AND_STR ("statistics"), "display relocation statistics",
2600 DL_DEBUG_STATISTICS },
2601 { LEN_AND_STR ("unused"), "determined unused DSOs",
2602 DL_DEBUG_UNUSED },
2603 { LEN_AND_STR ("help"), "display this help message and exit",
2604 DL_DEBUG_HELP },
2605 };
2606#define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2607
2608 /* Skip separating white spaces and commas. */
2609 while (*dl_debug != '\0')
2610 {
2611 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2612 {
2613 size_t cnt;
2614 size_t len = 1;
2615
2616 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2617 && dl_debug[len] != ',' && dl_debug[len] != ':')
2618 ++len;
2619
2620 for (cnt = 0; cnt < ndebopts; ++cnt)
2621 if (debopts[cnt].len == len
2622 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2623 {
2624 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2625 state->any_debug = true;
2626 break;
2627 }
2628
2629 if (cnt == ndebopts)
2630 {
2631 /* Display a warning and skip everything until next
2632 separator. */
2633 char *copy = strndupa (dl_debug, len);
2634 _dl_error_printf ("\
2635warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2636 }
2637
2638 dl_debug += len;
2639 continue;
2640 }
2641
2642 ++dl_debug;
2643 }
2644
2645 if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2646 {
2647 /* In order to get an accurate picture of whether a particular
2648 DT_NEEDED entry is actually used we have to process both
2649 the PLT and non-PLT relocation entries. */
2650 GLRO(dl_lazy) = 0;
2651 }
2652
2653 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2654 {
2655 size_t cnt;
2656
2657 _dl_printf ("\
2658Valid options for the LD_DEBUG environment variable are:\n\n");
2659
2660 for (cnt = 0; cnt < ndebopts; ++cnt)
2661 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2662 " " + debopts[cnt].len - 3,
2663 debopts[cnt].helptext);
2664
2665 _dl_printf ("\n\
2666To direct the debugging output into a file instead of standard output\n\
2667a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2668 _exit (0);
2669 }
2670}
2671
2672static void
2673process_envvars (struct dl_main_state *state)
2674{
2675 char **runp = _environ;
2676 char *envline;
2677 char *debug_output = NULL;
2678
2679 /* This is the default place for profiling data file. */
2680 GLRO(dl_profile_output)
2681 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
2682
2683 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2684 {
2685 size_t len = 0;
2686
2687 while (envline[len] != '\0' && envline[len] != '=')
2688 ++len;
2689
2690 if (envline[len] != '=')
2691 /* This is a "LD_" variable at the end of the string without
2692 a '=' character. Ignore it since otherwise we will access
2693 invalid memory below. */
2694 continue;
2695
2696 switch (len)
2697 {
2698 case 4:
2699 /* Warning level, verbose or not. */
2700 if (memcmp (envline, "WARN", 4) == 0)
2701 GLRO(dl_verbose) = envline[5] != '\0';
2702 break;
2703
2704 case 5:
2705 /* Debugging of the dynamic linker? */
2706 if (memcmp (envline, "DEBUG", 5) == 0)
2707 {
2708 process_dl_debug (state, &envline[6]);
2709 break;
2710 }
2711 if (memcmp (envline, "AUDIT", 5) == 0)
2712 audit_list_add_string (&state->audit_list, &envline[6]);
2713 break;
2714
2715 case 7:
2716 /* Print information about versions. */
2717 if (memcmp (envline, "VERBOSE", 7) == 0)
2718 {
2719 state->version_info = envline[8] != '\0';
2720 break;
2721 }
2722
2723 /* List of objects to be preloaded. */
2724 if (memcmp (envline, "PRELOAD", 7) == 0)
2725 {
2726 state->preloadlist = &envline[8];
2727 break;
2728 }
2729
2730 /* Which shared object shall be profiled. */
2731 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2732 GLRO(dl_profile) = &envline[8];
2733 break;
2734
2735 case 8:
2736 /* Do we bind early? */
2737 if (memcmp (envline, "BIND_NOW", 8) == 0)
2738 {
2739 GLRO(dl_lazy) = envline[9] == '\0';
2740 break;
2741 }
2742 if (memcmp (envline, "BIND_NOT", 8) == 0)
2743 GLRO(dl_bind_not) = envline[9] != '\0';
2744 break;
2745
2746 case 9:
2747 /* Test whether we want to see the content of the auxiliary
2748 array passed up from the kernel. */
2749 if (!__libc_enable_secure
2750 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2751 _dl_show_auxv ();
2752 break;
2753
2754#if !HAVE_TUNABLES
2755 case 10:
2756 /* Mask for the important hardware capabilities. */
2757 if (!__libc_enable_secure
2758 && memcmp (envline, "HWCAP_MASK", 10) == 0)
2759 GLRO(dl_hwcap_mask) = _dl_strtoul (&envline[11], NULL);
2760 break;
2761#endif
2762
2763 case 11:
2764 /* Path where the binary is found. */
2765 if (!__libc_enable_secure
2766 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2767 GLRO(dl_origin_path) = &envline[12];
2768 break;
2769
2770 case 12:
2771 /* The library search path. */
2772 if (!__libc_enable_secure
2773 && memcmp (envline, "LIBRARY_PATH", 12) == 0)
2774 {
2775 state->library_path = &envline[13];
2776 state->library_path_source = "LD_LIBRARY_PATH";
2777 break;
2778 }
2779
2780 /* Where to place the profiling data file. */
2781 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2782 {
2783 debug_output = &envline[13];
2784 break;
2785 }
2786
2787 if (!__libc_enable_secure
2788 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2789 GLRO(dl_dynamic_weak) = 1;
2790 break;
2791
2792 case 13:
2793 /* We might have some extra environment variable with length 13
2794 to handle. */
2795#ifdef EXTRA_LD_ENVVARS_13
2796 EXTRA_LD_ENVVARS_13
2797#endif
2798 if (!__libc_enable_secure
2799 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2800 {
2801 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2802 break;
2803 }
2804 break;
2805
2806 case 14:
2807 /* Where to place the profiling data file. */
2808 if (!__libc_enable_secure
2809 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2810 && envline[15] != '\0')
2811 GLRO(dl_profile_output) = &envline[15];
2812 break;
2813
2814 case 16:
2815 /* The mode of the dynamic linker can be set. */
2816 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2817 {
2818 state->mode = rtld_mode_trace;
2819 GLRO(dl_verbose) = 1;
2820 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2821 GLRO(dl_trace_prelink) = &envline[17];
2822 }
2823 break;
2824
2825 case 20:
2826 /* The mode of the dynamic linker can be set. */
2827 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2828 state->mode = rtld_mode_trace;
2829 break;
2830
2831 /* We might have some extra environment variable to handle. This
2832 is tricky due to the pre-processing of the length of the name
2833 in the switch statement here. The code here assumes that added
2834 environment variables have a different length. */
2835#ifdef EXTRA_LD_ENVVARS
2836 EXTRA_LD_ENVVARS
2837#endif
2838 }
2839 }
2840
2841 /* Extra security for SUID binaries. Remove all dangerous environment
2842 variables. */
2843 if (__builtin_expect (__libc_enable_secure, 0))
2844 {
2845 static const char unsecure_envvars[] =
2846#ifdef EXTRA_UNSECURE_ENVVARS
2847 EXTRA_UNSECURE_ENVVARS
2848#endif
2849 UNSECURE_ENVVARS;
2850 const char *nextp;
2851
2852 nextp = unsecure_envvars;
2853 do
2854 {
2855 unsetenv (nextp);
2856 /* We could use rawmemchr but this need not be fast. */
2857 nextp = (char *) (strchr) (nextp, '\0') + 1;
2858 }
2859 while (*nextp != '\0');
2860
2861 if (__access ("/etc/suid-debug", F_OK) != 0)
2862 {
2863#if !HAVE_TUNABLES
2864 unsetenv ("MALLOC_CHECK_");
2865#endif
2866 GLRO(dl_debug_mask) = 0;
2867 }
2868
2869 if (state->mode != rtld_mode_normal)
2870 _exit (5);
2871 }
2872 /* If we have to run the dynamic linker in debugging mode and the
2873 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2874 messages to this file. */
2875 else if (state->any_debug && debug_output != NULL)
2876 {
2877 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2878 size_t name_len = strlen (debug_output);
2879 char buf[name_len + 12];
2880 char *startp;
2881
2882 buf[name_len + 11] = '\0';
2883 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2884 *--startp = '.';
2885 startp = memcpy (startp - name_len, debug_output, name_len);
2886
2887 GLRO(dl_debug_fd) = __open64_nocancel (startp, flags, DEFFILEMODE);
2888 if (GLRO(dl_debug_fd) == -1)
2889 /* We use standard output if opening the file failed. */
2890 GLRO(dl_debug_fd) = STDOUT_FILENO;
2891 }
2892}
2893
2894#if HP_TIMING_INLINE
2895static void
2896print_statistics_item (const char *title, hp_timing_t time,
2897 hp_timing_t total)
2898{
2899 char cycles[HP_TIMING_PRINT_SIZE];
2900 HP_TIMING_PRINT (cycles, sizeof (cycles), time);
2901
2902 char relative[3 * sizeof (hp_timing_t) + 2];
2903 char *cp = _itoa ((1000ULL * time) / total, relative + sizeof (relative),
2904 10, 0);
2905 /* Sets the decimal point. */
2906 char *wp = relative;
2907 switch (relative + sizeof (relative) - cp)
2908 {
2909 case 3:
2910 *wp++ = *cp++;
2911 /* Fall through. */
2912 case 2:
2913 *wp++ = *cp++;
2914 /* Fall through. */
2915 case 1:
2916 *wp++ = '.';
2917 *wp++ = *cp++;
2918 }
2919 *wp = '\0';
2920 _dl_debug_printf ("%s: %s cycles (%s%%)\n", title, cycles, relative);
2921}
2922#endif
2923
2924/* Print the various times we collected. */
2925static void
2926__attribute ((noinline))
2927print_statistics (const hp_timing_t *rtld_total_timep)
2928{
2929#if HP_TIMING_INLINE
2930 {
2931 char cycles[HP_TIMING_PRINT_SIZE];
2932 HP_TIMING_PRINT (cycles, sizeof (cycles), *rtld_total_timep);
2933 _dl_debug_printf ("\nruntime linker statistics:\n"
2934 " total startup time in dynamic loader: %s cycles\n",
2935 cycles);
2936 print_statistics_item (" time needed for relocation",
2937 relocate_time, *rtld_total_timep);
2938 }
2939#endif
2940
2941 unsigned long int num_relative_relocations = 0;
2942 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2943 {
2944 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2945 continue;
2946
2947 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2948
2949 for (unsigned int i = 0; i < scope->r_nlist; i++)
2950 {
2951 struct link_map *l = scope->r_list [i];
2952
2953 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2954 num_relative_relocations
2955 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2956#ifndef ELF_MACHINE_REL_RELATIVE
2957 /* Relative relocations are processed on these architectures if
2958 library is loaded to different address than p_vaddr or
2959 if not prelinked. */
2960 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2961 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2962#else
2963 /* On e.g. IA-64 or Alpha, relative relocations are processed
2964 only if library is loaded to different address than p_vaddr. */
2965 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2966#endif
2967 num_relative_relocations
2968 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2969 }
2970 }
2971
2972 _dl_debug_printf (" number of relocations: %lu\n"
2973 " number of relocations from cache: %lu\n"
2974 " number of relative relocations: %lu\n",
2975 GL(dl_num_relocations),
2976 GL(dl_num_cache_relocations),
2977 num_relative_relocations);
2978
2979#if HP_TIMING_INLINE
2980 print_statistics_item (" time needed to load objects",
2981 load_time, *rtld_total_timep);
2982#endif
2983}
2984