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