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