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