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