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