Replace FSF snail mail address with URLs.
[glibc.git] / elf / rtld.c
blob2e4f97ffed58306e722ea64c3d2182e6ed4757b2
1 /* Run time dynamic linker.
2 Copyright (C) 1995-2010, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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 <stdio-common/_itoa.h>
31 #include <entry.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <bits/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 <stackinfo.h>
44 #include <assert.h>
46 /* Avoid PLT use for our local calls at startup. */
47 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
49 /* GCC has mental blocks about _exit. */
50 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
51 #define _exit exit_internal
53 /* Helper function to handle errors while resolving symbols. */
54 static void print_unresolved (int errcode, const char *objname,
55 const char *errsting);
57 /* Helper function to handle errors when a version is missing. */
58 static void print_missing_version (int errcode, const char *objname,
59 const char *errsting);
61 /* Print the various times we collected. */
62 static void print_statistics (hp_timing_t *total_timep);
64 /* Add audit objects. */
65 static void process_dl_audit (char *str);
67 /* This is a list of all the modes the dynamic loader can be in. */
68 enum mode { normal, list, verify, trace };
70 /* Process all environments variables the dynamic linker must recognize.
71 Since all of them start with `LD_' we are a bit smarter while finding
72 all the entries. */
73 static void process_envvars (enum mode *modep);
75 #ifdef DL_ARGV_NOT_RELRO
76 int _dl_argc attribute_hidden;
77 char **_dl_argv = NULL;
78 /* Nonzero if we were run directly. */
79 unsigned int _dl_skip_args attribute_hidden;
80 #else
81 int _dl_argc attribute_relro attribute_hidden;
82 char **_dl_argv attribute_relro = NULL;
83 unsigned int _dl_skip_args attribute_relro attribute_hidden;
84 #endif
85 INTDEF(_dl_argv)
87 #ifndef THREAD_SET_STACK_GUARD
88 /* Only exported for architectures that don't store the stack guard canary
89 in thread local area. */
90 uintptr_t __stack_chk_guard attribute_relro;
91 #endif
93 /* Only exported for architectures that don't store the pointer guard
94 value in thread local area. */
95 uintptr_t __pointer_chk_guard_local
96 attribute_relro attribute_hidden __attribute__ ((nocommon));
97 #ifndef THREAD_SET_POINTER_GUARD
98 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
99 #endif
102 /* List of auditing DSOs. */
103 static struct audit_list
105 const char *name;
106 struct audit_list *next;
107 } *audit_list;
109 #ifndef HAVE_INLINED_SYSCALLS
110 /* Set nonzero during loading and initialization of executable and
111 libraries, cleared before the executable's entry point runs. This
112 must not be initialized to nonzero, because the unused dynamic
113 linker loaded in for libc.so's "ld.so.1" dep will provide the
114 definition seen by libc.so's initializer; that value must be zero,
115 and will be since that dynamic linker's _dl_start and dl_main will
116 never be called. */
117 int _dl_starting_up = 0;
118 INTVARDEF(_dl_starting_up)
119 #endif
121 /* This is the structure which defines all variables global to ld.so
122 (except those which cannot be added for some reason). */
123 struct rtld_global _rtld_global =
125 /* Generally the default presumption without further information is an
126 * executable stack but this is not true for all platforms. */
127 ._dl_stack_flags = DEFAULT_STACK_PERMS,
128 #ifdef _LIBC_REENTRANT
129 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
130 ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
131 #endif
132 ._dl_nns = 1,
133 ._dl_ns =
135 [LM_ID_BASE] = { ._ns_unique_sym_table
136 = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } }
139 /* If we would use strong_alias here the compiler would see a
140 non-hidden definition. This would undo the effect of the previous
141 declaration. So spell out was strong_alias does plus add the
142 visibility attribute. */
143 extern struct rtld_global _rtld_local
144 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
147 /* This variable is similar to _rtld_local, but all values are
148 read-only after relocation. */
149 struct rtld_global_ro _rtld_global_ro attribute_relro =
151 /* Get architecture specific initializer. */
152 #include <dl-procinfo.c>
153 #ifdef NEED_DL_SYSINFO
154 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
155 #endif
156 ._dl_debug_fd = STDERR_FILENO,
157 ._dl_use_load_bias = -2,
158 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
159 ._dl_hwcap_mask = HWCAP_IMPORTANT,
160 ._dl_lazy = 1,
161 ._dl_fpu_control = _FPU_DEFAULT,
162 ._dl_pointer_guard = 1,
163 ._dl_pagesize = EXEC_PAGESIZE,
165 /* Function pointers. */
166 ._dl_debug_printf = _dl_debug_printf,
167 ._dl_catch_error = _dl_catch_error,
168 ._dl_signal_error = _dl_signal_error,
169 ._dl_mcount = _dl_mcount_internal,
170 ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
171 ._dl_check_caller = _dl_check_caller,
172 ._dl_open = _dl_open,
173 ._dl_close = _dl_close,
174 ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
175 #ifdef HAVE_DL_DISCOVER_OSVERSION
176 ._dl_discover_osversion = _dl_discover_osversion
177 #endif
179 /* If we would use strong_alias here the compiler would see a
180 non-hidden definition. This would undo the effect of the previous
181 declaration. So spell out was strong_alias does plus add the
182 visibility attribute. */
183 extern struct rtld_global_ro _rtld_local_ro
184 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
187 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
188 ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv);
190 /* These two variables cannot be moved into .data.rel.ro. */
191 static struct libname_list _dl_rtld_libname;
192 static struct libname_list _dl_rtld_libname2;
194 /* We expect less than a second for relocation. */
195 #ifdef HP_SMALL_TIMING_AVAIL
196 # undef HP_TIMING_AVAIL
197 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
198 #endif
200 /* Variable for statistics. */
201 #ifndef HP_TIMING_NONAVAIL
202 static hp_timing_t relocate_time;
203 static hp_timing_t load_time attribute_relro;
204 static hp_timing_t start_time attribute_relro;
205 #endif
207 /* Additional definitions needed by TLS initialization. */
208 #ifdef TLS_INIT_HELPER
209 TLS_INIT_HELPER
210 #endif
212 /* Helper function for syscall implementation. */
213 #ifdef DL_SYSINFO_IMPLEMENTATION
214 DL_SYSINFO_IMPLEMENTATION
215 #endif
217 /* Before ld.so is relocated we must not access variables which need
218 relocations. This means variables which are exported. Variables
219 declared as static are fine. If we can mark a variable hidden this
220 is fine, too. The latter is important here. We can avoid setting
221 up a temporary link map for ld.so if we can mark _rtld_global as
222 hidden. */
223 #ifdef PI_STATIC_AND_HIDDEN
224 # define DONT_USE_BOOTSTRAP_MAP 1
225 #endif
227 #ifdef DONT_USE_BOOTSTRAP_MAP
228 static ElfW(Addr) _dl_start_final (void *arg);
229 #else
230 struct dl_start_final_info
232 struct link_map l;
233 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
234 hp_timing_t start_time;
235 #endif
237 static ElfW(Addr) _dl_start_final (void *arg,
238 struct dl_start_final_info *info);
239 #endif
241 /* These defined magically in the linker script. */
242 extern char _begin[] attribute_hidden;
243 extern char _etext[] attribute_hidden;
244 extern char _end[] attribute_hidden;
247 #ifdef RTLD_START
248 RTLD_START
249 #else
250 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
251 #endif
253 #ifndef VALIDX
254 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
255 + DT_EXTRANUM + DT_VALTAGIDX (tag))
256 #endif
257 #ifndef ADDRIDX
258 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
259 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
260 #endif
262 /* This is the second half of _dl_start (below). It can be inlined safely
263 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
264 references. When the tools don't permit us to avoid using a GOT entry
265 for _dl_rtld_global (no attribute_hidden support), we must make sure
266 this function is not inlined (see below). */
268 #ifdef DONT_USE_BOOTSTRAP_MAP
269 static inline ElfW(Addr) __attribute__ ((always_inline))
270 _dl_start_final (void *arg)
271 #else
272 static ElfW(Addr) __attribute__ ((noinline))
273 _dl_start_final (void *arg, struct dl_start_final_info *info)
274 #endif
276 ElfW(Addr) start_addr;
278 if (HP_TIMING_AVAIL)
280 /* If it hasn't happen yet record the startup time. */
281 if (! HP_TIMING_INLINE)
282 HP_TIMING_NOW (start_time);
283 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
284 else
285 start_time = info->start_time;
286 #endif
288 /* Initialize the timing functions. */
289 HP_TIMING_DIFF_INIT ();
292 /* Transfer data about ourselves to the permanent link_map structure. */
293 #ifndef DONT_USE_BOOTSTRAP_MAP
294 GL(dl_rtld_map).l_addr = info->l.l_addr;
295 GL(dl_rtld_map).l_ld = info->l.l_ld;
296 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
297 sizeof GL(dl_rtld_map).l_info);
298 GL(dl_rtld_map).l_mach = info->l.l_mach;
299 GL(dl_rtld_map).l_relocated = 1;
300 #endif
301 _dl_setup_hash (&GL(dl_rtld_map));
302 GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
303 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
304 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
305 GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
306 /* Copy the TLS related data if necessary. */
307 #ifndef DONT_USE_BOOTSTRAP_MAP
308 # if USE___THREAD
309 assert (info->l.l_tls_modid != 0);
310 GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize;
311 GL(dl_rtld_map).l_tls_align = info->l.l_tls_align;
312 GL(dl_rtld_map).l_tls_firstbyte_offset = info->l.l_tls_firstbyte_offset;
313 GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size;
314 GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage;
315 GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset;
316 GL(dl_rtld_map).l_tls_modid = 1;
317 # else
318 # if NO_TLS_OFFSET != 0
319 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
320 # endif
321 # endif
323 #endif
325 #if HP_TIMING_AVAIL
326 HP_TIMING_NOW (GL(dl_cpuclock_offset));
327 #endif
329 /* Initialize the stack end variable. */
330 __libc_stack_end = __builtin_frame_address (0);
332 /* Call the OS-dependent function to set up life so we can do things like
333 file access. It will call `dl_main' (below) to do all the real work
334 of the dynamic linker, and then unwind our frame and run the user
335 entry point on the same stack we entered on. */
336 start_addr = _dl_sysdep_start (arg, &dl_main);
338 #ifndef HP_TIMING_NONAVAIL
339 hp_timing_t rtld_total_time;
340 if (HP_TIMING_AVAIL)
342 hp_timing_t end_time;
344 /* Get the current time. */
345 HP_TIMING_NOW (end_time);
347 /* Compute the difference. */
348 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
350 #endif
352 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
354 #ifndef HP_TIMING_NONAVAIL
355 print_statistics (&rtld_total_time);
356 #else
357 print_statistics (NULL);
358 #endif
361 return start_addr;
364 static ElfW(Addr) __attribute_used__ internal_function
365 _dl_start (void *arg)
367 #ifdef DONT_USE_BOOTSTRAP_MAP
368 # define bootstrap_map GL(dl_rtld_map)
369 #else
370 struct dl_start_final_info info;
371 # define bootstrap_map info.l
372 #endif
374 /* This #define produces dynamic linking inline functions for
375 bootstrap relocation instead of general-purpose relocation.
376 Since ld.so must not have any undefined symbols the result
377 is trivial: always the map of ld.so itself. */
378 #define RTLD_BOOTSTRAP
379 #define RESOLVE_MAP(sym, version, flags) (&bootstrap_map)
380 #include "dynamic-link.h"
382 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
383 #ifdef DONT_USE_BOOTSTRAP_MAP
384 HP_TIMING_NOW (start_time);
385 #else
386 HP_TIMING_NOW (info.start_time);
387 #endif
389 /* Partly clean the `bootstrap_map' structure up. Don't use
390 `memset' since it might not be built in or inlined and we cannot
391 make function calls at this point. Use '__builtin_memset' if we
392 know it is available. We do not have to clear the memory if we
393 do not have to use the temporary bootstrap_map. Global variables
394 are initialized to zero by default. */
395 #ifndef DONT_USE_BOOTSTRAP_MAP
396 # ifdef HAVE_BUILTIN_MEMSET
397 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
398 # else
399 for (size_t cnt = 0;
400 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
401 ++cnt)
402 bootstrap_map.l_info[cnt] = 0;
403 # endif
404 # if USE___THREAD
405 bootstrap_map.l_tls_modid = 0;
406 # endif
407 #endif
409 /* Figure out the run-time load address of the dynamic linker itself. */
410 bootstrap_map.l_addr = elf_machine_load_address ();
412 /* Read our own dynamic section and fill in the info array. */
413 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
414 elf_get_dynamic_info (&bootstrap_map, NULL);
416 #if NO_TLS_OFFSET != 0
417 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
418 #endif
420 /* Get the dynamic linker's own program header. First we need the ELF
421 file header. The `_begin' symbol created by the linker script points
422 to it. When we have something like GOTOFF relocs, we can use a plain
423 reference to find the runtime address. Without that, we have to rely
424 on the `l_addr' value, which is not the value we want when prelinked. */
425 #if USE___THREAD
426 dtv_t initdtv[3];
427 ElfW(Ehdr) *ehdr
428 # ifdef DONT_USE_BOOTSTRAP_MAP
429 = (ElfW(Ehdr) *) &_begin;
430 # else
431 # error This will not work with prelink.
432 = (ElfW(Ehdr) *) bootstrap_map.l_addr;
433 # endif
434 ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff);
435 size_t cnt = ehdr->e_phnum; /* PT_TLS is usually the last phdr. */
436 while (cnt-- > 0)
437 if (phdr[cnt].p_type == PT_TLS)
439 void *tlsblock;
440 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
441 char *p;
443 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
444 bootstrap_map.l_tls_align = phdr[cnt].p_align;
445 if (phdr[cnt].p_align == 0)
446 bootstrap_map.l_tls_firstbyte_offset = 0;
447 else
448 bootstrap_map.l_tls_firstbyte_offset = (phdr[cnt].p_vaddr
449 & (phdr[cnt].p_align - 1));
450 assert (bootstrap_map.l_tls_blocksize != 0);
451 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
452 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
453 + phdr[cnt].p_vaddr);
455 /* We can now allocate the initial TLS block. This can happen
456 on the stack. We'll get the final memory later when we
457 know all about the various objects loaded at startup
458 time. */
459 # if TLS_TCB_AT_TP
460 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
461 TLS_INIT_TCB_ALIGN)
462 + TLS_INIT_TCB_SIZE
463 + max_align);
464 # elif TLS_DTV_AT_TP
465 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
466 bootstrap_map.l_tls_align)
467 + bootstrap_map.l_tls_blocksize
468 + max_align);
469 # else
470 /* In case a model with a different layout for the TCB and DTV
471 is defined add another #elif here and in the following #ifs. */
472 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
473 # endif
474 /* Align the TLS block. */
475 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
476 & ~(max_align - 1));
478 /* Initialize the dtv. [0] is the length, [1] the generation
479 counter. */
480 initdtv[0].counter = 1;
481 initdtv[1].counter = 0;
483 /* Initialize the TLS block. */
484 # if TLS_TCB_AT_TP
485 initdtv[2].pointer = tlsblock;
486 # elif TLS_DTV_AT_TP
487 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
488 bootstrap_map.l_tls_align);
489 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
490 # else
491 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
492 # endif
493 p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
494 bootstrap_map.l_tls_initimage_size);
495 # ifdef HAVE_BUILTIN_MEMSET
496 __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
497 - bootstrap_map.l_tls_initimage_size));
498 # else
500 size_t remaining = (bootstrap_map.l_tls_blocksize
501 - bootstrap_map.l_tls_initimage_size);
502 while (remaining-- > 0)
503 *p++ = '\0';
505 # endif
507 /* Install the pointer to the dtv. */
509 /* Initialize the thread pointer. */
510 # if TLS_TCB_AT_TP
511 bootstrap_map.l_tls_offset
512 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
514 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
515 initdtv);
517 const char *lossage = TLS_INIT_TP ((char *) tlsblock
518 + bootstrap_map.l_tls_offset, 0);
519 # elif TLS_DTV_AT_TP
520 INSTALL_DTV (tlsblock, initdtv);
521 const char *lossage = TLS_INIT_TP (tlsblock, 0);
522 # else
523 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
524 # endif
525 if (__builtin_expect (lossage != NULL, 0))
526 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
527 lossage);
529 /* So far this is module number one. */
530 bootstrap_map.l_tls_modid = 1;
532 /* There can only be one PT_TLS entry. */
533 break;
535 #endif /* USE___THREAD */
537 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
538 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
539 #endif
541 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
543 /* Relocate ourselves so we can do normal function calls and
544 data access using the global offset table. */
546 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0, 0);
548 bootstrap_map.l_relocated = 1;
550 /* Please note that we don't allow profiling of this object and
551 therefore need not test whether we have to allocate the array
552 for the relocation results (as done in dl-reloc.c). */
554 /* Now life is sane; we can call functions and access global data.
555 Set up to use the operating system facilities, and find out from
556 the operating system's program loader where to find the program
557 header table in core. Put the rest of _dl_start into a separate
558 function, that way the compiler cannot put accesses to the GOT
559 before ELF_DYNAMIC_RELOCATE. */
561 #ifdef DONT_USE_BOOTSTRAP_MAP
562 ElfW(Addr) entry = _dl_start_final (arg);
563 #else
564 ElfW(Addr) entry = _dl_start_final (arg, &info);
565 #endif
567 #ifndef ELF_MACHINE_START_ADDRESS
568 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
569 #endif
571 return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
577 /* Now life is peachy; we can do all normal operations.
578 On to the real work. */
580 /* Some helper functions. */
582 /* Arguments to relocate_doit. */
583 struct relocate_args
585 struct link_map *l;
586 int reloc_mode;
589 struct map_args
591 /* Argument to map_doit. */
592 char *str;
593 struct link_map *loader;
594 int mode;
595 /* Return value of map_doit. */
596 struct link_map *map;
599 struct dlmopen_args
601 const char *fname;
602 struct link_map *map;
605 struct lookup_args
607 const char *name;
608 struct link_map *map;
609 void *result;
612 /* Arguments to version_check_doit. */
613 struct version_check_args
615 int doexit;
616 int dotrace;
619 static void
620 relocate_doit (void *a)
622 struct relocate_args *args = (struct relocate_args *) a;
624 _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0);
627 static void
628 map_doit (void *a)
630 struct map_args *args = (struct map_args *) a;
631 args->map = _dl_map_object (args->loader, args->str, lt_library, 0,
632 args->mode, LM_ID_BASE);
635 static void
636 dlmopen_doit (void *a)
638 struct dlmopen_args *args = (struct dlmopen_args *) a;
639 args->map = _dl_open (args->fname,
640 (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT
641 | __RTLD_SECURE),
642 dl_main, LM_ID_NEWLM, _dl_argc, INTUSE(_dl_argv),
643 __environ);
646 static void
647 lookup_doit (void *a)
649 struct lookup_args *args = (struct lookup_args *) a;
650 const ElfW(Sym) *ref = NULL;
651 args->result = NULL;
652 lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
653 args->map->l_local_scope, NULL, 0,
654 DL_LOOKUP_RETURN_NEWEST, NULL);
655 if (ref != NULL)
656 args->result = DL_SYMBOL_ADDRESS (l, ref);
659 static void
660 version_check_doit (void *a)
662 struct version_check_args *args = (struct version_check_args *) a;
663 if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
664 args->dotrace) && args->doexit)
665 /* We cannot start the application. Abort now. */
666 _exit (1);
670 static inline struct link_map *
671 find_needed (const char *name)
673 struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
674 unsigned int n = scope->r_nlist;
676 while (n-- > 0)
677 if (_dl_name_match_p (name, scope->r_list[n]))
678 return scope->r_list[n];
680 /* Should never happen. */
681 return NULL;
684 static int
685 match_version (const char *string, struct link_map *map)
687 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
688 ElfW(Verdef) *def;
690 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
691 if (map->l_info[VERDEFTAG] == NULL)
692 /* The file has no symbol versioning. */
693 return 0;
695 def = (ElfW(Verdef) *) ((char *) map->l_addr
696 + map->l_info[VERDEFTAG]->d_un.d_ptr);
697 while (1)
699 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
701 /* Compare the version strings. */
702 if (strcmp (string, strtab + aux->vda_name) == 0)
703 /* Bingo! */
704 return 1;
706 /* If no more definitions we failed to find what we want. */
707 if (def->vd_next == 0)
708 break;
710 /* Next definition. */
711 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
714 return 0;
717 static bool tls_init_tp_called;
719 static void *
720 init_tls (void)
722 /* Number of elements in the static TLS block. */
723 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
725 /* Do not do this twice. The audit interface might have required
726 the DTV interfaces to be set up early. */
727 if (GL(dl_initial_dtv) != NULL)
728 return NULL;
730 /* Allocate the array which contains the information about the
731 dtv slots. We allocate a few entries more than needed to
732 avoid the need for reallocation. */
733 size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
735 /* Allocate. */
736 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
737 calloc (sizeof (struct dtv_slotinfo_list)
738 + nelem * sizeof (struct dtv_slotinfo), 1);
739 /* No need to check the return value. If memory allocation failed
740 the program would have been terminated. */
742 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
743 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
744 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
746 /* Fill in the information from the loaded modules. No namespace
747 but the base one can be filled at this time. */
748 assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
749 int i = 0;
750 for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
751 l = l->l_next)
752 if (l->l_tls_blocksize != 0)
754 /* This is a module with TLS data. Store the map reference.
755 The generation counter is zero. */
756 slotinfo[i].map = l;
757 /* slotinfo[i].gen = 0; */
758 ++i;
760 assert (i == GL(dl_tls_max_dtv_idx));
762 /* Compute the TLS offsets for the various blocks. */
763 _dl_determine_tlsoffset ();
765 /* Construct the static TLS block and the dtv for the initial
766 thread. For some platforms this will include allocating memory
767 for the thread descriptor. The memory for the TLS block will
768 never be freed. It should be allocated accordingly. The dtv
769 array can be changed if dynamic loading requires it. */
770 void *tcbp = _dl_allocate_tls_storage ();
771 if (tcbp == NULL)
772 _dl_fatal_printf ("\
773 cannot allocate TLS data structures for initial thread");
775 /* Store for detection of the special case by __tls_get_addr
776 so it knows not to pass this dtv to the normal realloc. */
777 GL(dl_initial_dtv) = GET_DTV (tcbp);
779 /* And finally install it for the main thread. If ld.so itself uses
780 TLS we know the thread pointer was initialized earlier. */
781 const char *lossage
782 #ifdef USE___THREAD
783 = TLS_INIT_TP (tcbp, USE___THREAD);
784 #else
785 = TLS_INIT_TP (tcbp, 0);
786 #endif
787 if (__builtin_expect (lossage != NULL, 0))
788 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
789 tls_init_tp_called = true;
791 return tcbp;
794 #ifdef _LIBC_REENTRANT
795 /* _dl_error_catch_tsd points to this for the single-threaded case.
796 It's reset by the thread library for multithreaded programs. */
797 void ** __attribute__ ((const))
798 _dl_initial_error_catch_tsd (void)
800 static void *data;
801 return &data;
803 #endif
806 static unsigned int
807 do_preload (char *fname, struct link_map *main_map, const char *where)
809 const char *objname;
810 const char *err_str = NULL;
811 struct map_args args;
812 bool malloced;
814 args.str = fname;
815 args.loader = main_map;
816 args.mode = __RTLD_SECURE;
818 unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
820 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
821 if (__builtin_expect (err_str != NULL, 0))
823 _dl_error_printf ("\
824 ERROR: ld.so: object '%s' from %s cannot be preloaded: ignored.\n",
825 fname, where);
826 /* No need to call free, this is still before
827 the libc's malloc is used. */
829 else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
830 /* It is no duplicate. */
831 return 1;
833 /* Nothing loaded. */
834 return 0;
837 #if defined SHARED && defined _LIBC_REENTRANT \
838 && defined __rtld_lock_default_lock_recursive
839 static void
840 rtld_lock_default_lock_recursive (void *lock)
842 __rtld_lock_default_lock_recursive (lock);
845 static void
846 rtld_lock_default_unlock_recursive (void *lock)
848 __rtld_lock_default_unlock_recursive (lock);
850 #endif
853 static void
854 security_init (void)
856 /* Set up the stack checker's canary. */
857 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
858 #ifdef THREAD_SET_STACK_GUARD
859 THREAD_SET_STACK_GUARD (stack_chk_guard);
860 #else
861 __stack_chk_guard = stack_chk_guard;
862 #endif
864 /* Set up the pointer guard as well, if necessary. */
865 if (GLRO(dl_pointer_guard))
867 uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
868 stack_chk_guard);
869 #ifdef THREAD_SET_POINTER_GUARD
870 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
871 #endif
872 __pointer_chk_guard_local = pointer_chk_guard;
875 /* We do not need the _dl_random value anymore. The less
876 information we leave behind, the better, so clear the
877 variable. */
878 _dl_random = NULL;
882 /* The library search path. */
883 static const char *library_path attribute_relro;
884 /* The list preloaded objects. */
885 static const char *preloadlist attribute_relro;
886 /* Nonzero if information about versions has to be printed. */
887 static int version_info attribute_relro;
889 static void
890 dl_main (const ElfW(Phdr) *phdr,
891 ElfW(Word) phnum,
892 ElfW(Addr) *user_entry,
893 ElfW(auxv_t) *auxv)
895 const ElfW(Phdr) *ph;
896 enum mode mode;
897 struct link_map *main_map;
898 size_t file_size;
899 char *file;
900 bool has_interp = false;
901 unsigned int i;
902 bool prelinked = false;
903 bool rtld_is_main = false;
904 #ifndef HP_TIMING_NONAVAIL
905 hp_timing_t start;
906 hp_timing_t stop;
907 hp_timing_t diff;
908 #endif
909 void *tcbp = NULL;
911 #ifdef _LIBC_REENTRANT
912 /* Explicit initialization since the reloc would just be more work. */
913 GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
914 #endif
916 GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
918 #if defined SHARED && defined _LIBC_REENTRANT \
919 && defined __rtld_lock_default_lock_recursive
920 GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
921 GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
922 #endif
924 /* The explicit initialization here is cheaper than processing the reloc
925 in the _rtld_local definition's initializer. */
926 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
928 /* Process the environment variable which control the behaviour. */
929 process_envvars (&mode);
931 #ifndef HAVE_INLINED_SYSCALLS
932 /* Set up a flag which tells we are just starting. */
933 INTUSE(_dl_starting_up) = 1;
934 #endif
936 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
938 /* Ho ho. We are not the program interpreter! We are the program
939 itself! This means someone ran ld.so as a command. Well, that
940 might be convenient to do sometimes. We support it by
941 interpreting the args like this:
943 ld.so PROGRAM ARGS...
945 The first argument is the name of a file containing an ELF
946 executable we will load and run with the following arguments.
947 To simplify life here, PROGRAM is searched for using the
948 normal rules for shared objects, rather than $PATH or anything
949 like that. We just load it and use its entry point; we don't
950 pay attention to its PT_INTERP command (we are the interpreter
951 ourselves). This is an easy way to test a new ld.so before
952 installing it. */
953 rtld_is_main = true;
955 /* Note the place where the dynamic linker actually came from. */
956 GL(dl_rtld_map).l_name = rtld_progname;
958 while (_dl_argc > 1)
959 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
961 mode = list;
962 GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */
964 ++_dl_skip_args;
965 --_dl_argc;
966 ++INTUSE(_dl_argv);
968 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
970 mode = verify;
972 ++_dl_skip_args;
973 --_dl_argc;
974 ++INTUSE(_dl_argv);
976 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
977 && _dl_argc > 2)
979 library_path = INTUSE(_dl_argv)[2];
981 _dl_skip_args += 2;
982 _dl_argc -= 2;
983 INTUSE(_dl_argv) += 2;
985 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
986 && _dl_argc > 2)
988 GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
990 _dl_skip_args += 2;
991 _dl_argc -= 2;
992 INTUSE(_dl_argv) += 2;
994 else if (! strcmp (INTUSE(_dl_argv)[1], "--audit") && _dl_argc > 2)
996 process_dl_audit (INTUSE(_dl_argv)[2]);
998 _dl_skip_args += 2;
999 _dl_argc -= 2;
1000 INTUSE(_dl_argv) += 2;
1002 else
1003 break;
1005 /* If we have no further argument the program was called incorrectly.
1006 Grant the user some education. */
1007 if (_dl_argc < 2)
1008 _dl_fatal_printf ("\
1009 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
1010 You have invoked `ld.so', the helper program for shared library executables.\n\
1011 This program usually lives in the file `/lib/ld.so', and special directives\n\
1012 in executable files using ELF shared libraries tell the system's program\n\
1013 loader to load the helper program from this file. This helper program loads\n\
1014 the shared libraries needed by the program executable, prepares the program\n\
1015 to run, and runs it. You may invoke this helper program directly from the\n\
1016 command line to load and run an ELF executable file; this is like executing\n\
1017 that file itself, but always uses this helper program from the file you\n\
1018 specified, instead of the helper program file specified in the executable\n\
1019 file you run. This is mostly of use for maintainers to test new versions\n\
1020 of this helper program; chances are you did not intend to run this program.\n\
1022 --list list all dependencies and how they are resolved\n\
1023 --verify verify that given object really is a dynamically linked\n\
1024 object we can handle\n\
1025 --library-path PATH use given PATH instead of content of the environment\n\
1026 variable LD_LIBRARY_PATH\n\
1027 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
1028 in LIST\n\
1029 --audit LIST use objects named in LIST as auditors\n");
1031 ++_dl_skip_args;
1032 --_dl_argc;
1033 ++INTUSE(_dl_argv);
1035 /* The initialization of _dl_stack_flags done below assumes the
1036 executable's PT_GNU_STACK may have been honored by the kernel, and
1037 so a PT_GNU_STACK with PF_X set means the stack started out with
1038 execute permission. However, this is not really true if the
1039 dynamic linker is the executable the kernel loaded. For this
1040 case, we must reinitialize _dl_stack_flags to match the dynamic
1041 linker itself. If the dynamic linker was built with a
1042 PT_GNU_STACK, then the kernel may have loaded us with a
1043 nonexecutable stack that we will have to make executable when we
1044 load the program below unless it has a PT_GNU_STACK indicating
1045 nonexecutable stack is ok. */
1047 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1048 if (ph->p_type == PT_GNU_STACK)
1050 GL(dl_stack_flags) = ph->p_flags;
1051 break;
1054 if (__builtin_expect (mode, normal) == verify)
1056 const char *objname;
1057 const char *err_str = NULL;
1058 struct map_args args;
1059 bool malloced;
1061 args.str = rtld_progname;
1062 args.loader = NULL;
1063 args.mode = __RTLD_OPENEXEC;
1064 (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
1065 &args);
1066 if (__builtin_expect (err_str != NULL, 0))
1067 /* We don't free the returned string, the programs stops
1068 anyway. */
1069 _exit (EXIT_FAILURE);
1071 else
1073 HP_TIMING_NOW (start);
1074 _dl_map_object (NULL, rtld_progname, lt_library, 0,
1075 __RTLD_OPENEXEC, LM_ID_BASE);
1076 HP_TIMING_NOW (stop);
1078 HP_TIMING_DIFF (load_time, start, stop);
1081 /* Now the map for the main executable is available. */
1082 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
1084 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1085 && main_map->l_info[DT_SONAME] != NULL
1086 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1087 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
1088 (const char *) D_PTR (main_map, l_info[DT_STRTAB])
1089 + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
1090 _dl_fatal_printf ("loader cannot load itself\n");
1092 phdr = main_map->l_phdr;
1093 phnum = main_map->l_phnum;
1094 /* We overwrite here a pointer to a malloc()ed string. But since
1095 the malloc() implementation used at this point is the dummy
1096 implementations which has no real free() function it does not
1097 makes sense to free the old string first. */
1098 main_map->l_name = (char *) "";
1099 *user_entry = main_map->l_entry;
1101 #ifdef HAVE_AUX_VECTOR
1102 /* Adjust the on-stack auxiliary vector so that it looks like the
1103 binary was executed directly. */
1104 for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
1105 switch (av->a_type)
1107 case AT_PHDR:
1108 av->a_un.a_val = (uintptr_t) phdr;
1109 break;
1110 case AT_PHNUM:
1111 av->a_un.a_val = phnum;
1112 break;
1113 case AT_ENTRY:
1114 av->a_un.a_val = *user_entry;
1115 break;
1117 #endif
1119 else
1121 /* Create a link_map for the executable itself.
1122 This will be what dlopen on "" returns. */
1123 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
1124 __RTLD_OPENEXEC, LM_ID_BASE);
1125 assert (main_map != NULL);
1126 main_map->l_phdr = phdr;
1127 main_map->l_phnum = phnum;
1128 main_map->l_entry = *user_entry;
1130 /* Even though the link map is not yet fully initialized we can add
1131 it to the map list since there are no possible users running yet. */
1132 _dl_add_to_namespace_list (main_map, LM_ID_BASE);
1133 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1135 /* At this point we are in a bit of trouble. We would have to
1136 fill in the values for l_dev and l_ino. But in general we
1137 do not know where the file is. We also do not handle AT_EXECFD
1138 even if it would be passed up.
1140 We leave the values here defined to 0. This is normally no
1141 problem as the program code itself is normally no shared
1142 object and therefore cannot be loaded dynamically. Nothing
1143 prevent the use of dynamic binaries and in these situations
1144 we might get problems. We might not be able to find out
1145 whether the object is already loaded. But since there is no
1146 easy way out and because the dynamic binary must also not
1147 have an SONAME we ignore this program for now. If it becomes
1148 a problem we can force people using SONAMEs. */
1150 /* We delay initializing the path structure until we got the dynamic
1151 information for the program. */
1154 main_map->l_map_end = 0;
1155 main_map->l_text_end = 0;
1156 /* Perhaps the executable has no PT_LOAD header entries at all. */
1157 main_map->l_map_start = ~0;
1158 /* And it was opened directly. */
1159 ++main_map->l_direct_opencount;
1161 /* Scan the program header table for the dynamic section. */
1162 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1163 switch (ph->p_type)
1165 case PT_PHDR:
1166 /* Find out the load address. */
1167 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1168 break;
1169 case PT_DYNAMIC:
1170 /* This tells us where to find the dynamic section,
1171 which tells us everything we need to do. */
1172 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1173 break;
1174 case PT_INTERP:
1175 /* This "interpreter segment" was used by the program loader to
1176 find the program interpreter, which is this program itself, the
1177 dynamic linker. We note what name finds us, so that a future
1178 dlopen call or DT_NEEDED entry, for something that wants to link
1179 against the dynamic linker as a shared library, will know that
1180 the shared object is already loaded. */
1181 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1182 + ph->p_vaddr);
1183 /* _dl_rtld_libname.next = NULL; Already zero. */
1184 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1186 /* Ordinarilly, we would get additional names for the loader from
1187 our DT_SONAME. This can't happen if we were actually linked as
1188 a static executable (detect this case when we have no DYNAMIC).
1189 If so, assume the filename component of the interpreter path to
1190 be our SONAME, and add it to our name list. */
1191 if (GL(dl_rtld_map).l_ld == NULL)
1193 const char *p = NULL;
1194 const char *cp = _dl_rtld_libname.name;
1196 /* Find the filename part of the path. */
1197 while (*cp != '\0')
1198 if (*cp++ == '/')
1199 p = cp;
1201 if (p != NULL)
1203 _dl_rtld_libname2.name = p;
1204 /* _dl_rtld_libname2.next = NULL; Already zero. */
1205 _dl_rtld_libname.next = &_dl_rtld_libname2;
1209 has_interp = true;
1210 break;
1211 case PT_LOAD:
1213 ElfW(Addr) mapstart;
1214 ElfW(Addr) allocend;
1216 /* Remember where the main program starts in memory. */
1217 mapstart = (main_map->l_addr
1218 + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1219 if (main_map->l_map_start > mapstart)
1220 main_map->l_map_start = mapstart;
1222 /* Also where it ends. */
1223 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1224 if (main_map->l_map_end < allocend)
1225 main_map->l_map_end = allocend;
1226 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1227 main_map->l_text_end = allocend;
1229 break;
1231 case PT_TLS:
1232 if (ph->p_memsz > 0)
1234 /* Note that in the case the dynamic linker we duplicate work
1235 here since we read the PT_TLS entry already in
1236 _dl_start_final. But the result is repeatable so do not
1237 check for this special but unimportant case. */
1238 main_map->l_tls_blocksize = ph->p_memsz;
1239 main_map->l_tls_align = ph->p_align;
1240 if (ph->p_align == 0)
1241 main_map->l_tls_firstbyte_offset = 0;
1242 else
1243 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1244 & (ph->p_align - 1));
1245 main_map->l_tls_initimage_size = ph->p_filesz;
1246 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1248 /* This image gets the ID one. */
1249 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1251 break;
1253 case PT_GNU_STACK:
1254 GL(dl_stack_flags) = ph->p_flags;
1255 break;
1257 case PT_GNU_RELRO:
1258 main_map->l_relro_addr = ph->p_vaddr;
1259 main_map->l_relro_size = ph->p_memsz;
1260 break;
1263 /* Adjust the address of the TLS initialization image in case
1264 the executable is actually an ET_DYN object. */
1265 if (main_map->l_tls_initimage != NULL)
1266 main_map->l_tls_initimage
1267 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1268 if (! main_map->l_map_end)
1269 main_map->l_map_end = ~0;
1270 if (! main_map->l_text_end)
1271 main_map->l_text_end = ~0;
1272 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1274 /* We were invoked directly, so the program might not have a
1275 PT_INTERP. */
1276 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1277 /* _dl_rtld_libname.next = NULL; Already zero. */
1278 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1280 else
1281 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1283 /* If the current libname is different from the SONAME, add the
1284 latter as well. */
1285 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1286 && strcmp (GL(dl_rtld_map).l_libname->name,
1287 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1288 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1290 static struct libname_list newname;
1291 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1292 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1293 newname.next = NULL;
1294 newname.dont_free = 1;
1296 assert (GL(dl_rtld_map).l_libname->next == NULL);
1297 GL(dl_rtld_map).l_libname->next = &newname;
1299 /* The ld.so must be relocated since otherwise loading audit modules
1300 will fail since they reuse the very same ld.so. */
1301 assert (GL(dl_rtld_map).l_relocated);
1303 if (! rtld_is_main)
1305 /* Extract the contents of the dynamic section for easy access. */
1306 elf_get_dynamic_info (main_map, NULL);
1307 /* Set up our cache of pointers into the hash table. */
1308 _dl_setup_hash (main_map);
1311 if (__builtin_expect (mode, normal) == verify)
1313 /* We were called just to verify that this is a dynamic
1314 executable using us as the program interpreter. Exit with an
1315 error if we were not able to load the binary or no interpreter
1316 is specified (i.e., this is no dynamically linked binary. */
1317 if (main_map->l_ld == NULL)
1318 _exit (1);
1320 /* We allow here some platform specific code. */
1321 #ifdef DISTINGUISH_LIB_VERSIONS
1322 DISTINGUISH_LIB_VERSIONS;
1323 #endif
1324 _exit (has_interp ? 0 : 2);
1327 struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1328 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1329 /* Set up the data structures for the system-supplied DSO early,
1330 so they can influence _dl_init_paths. */
1331 if (GLRO(dl_sysinfo_dso) != NULL)
1333 /* Do an abridged version of the work _dl_map_object_from_fd would do
1334 to map in the object. It's already mapped and prelinked (and
1335 better be, since it's read-only and so we couldn't relocate it).
1336 We just want our data structures to describe it as if we had just
1337 mapped and relocated it normally. */
1338 struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
1339 0, LM_ID_BASE);
1340 if (__builtin_expect (l != NULL, 1))
1342 static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro;
1344 l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
1345 + GLRO(dl_sysinfo_dso)->e_phoff);
1346 l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum;
1347 for (uint_fast16_t i = 0; i < l->l_phnum; ++i)
1349 const ElfW(Phdr) *const ph = &l->l_phdr[i];
1350 if (ph->p_type == PT_DYNAMIC)
1352 l->l_ld = (void *) ph->p_vaddr;
1353 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1355 else if (ph->p_type == PT_LOAD)
1357 if (! l->l_addr)
1358 l->l_addr = ph->p_vaddr;
1359 if (ph->p_vaddr + ph->p_memsz >= l->l_map_end)
1360 l->l_map_end = ph->p_vaddr + ph->p_memsz;
1361 if ((ph->p_flags & PF_X)
1362 && ph->p_vaddr + ph->p_memsz >= l->l_text_end)
1363 l->l_text_end = ph->p_vaddr + ph->p_memsz;
1365 else
1366 /* There must be no TLS segment. */
1367 assert (ph->p_type != PT_TLS);
1369 l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso);
1370 l->l_addr = l->l_map_start - l->l_addr;
1371 l->l_map_end += l->l_addr;
1372 l->l_text_end += l->l_addr;
1373 l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1374 elf_get_dynamic_info (l, dyn_temp);
1375 _dl_setup_hash (l);
1376 l->l_relocated = 1;
1378 /* Initialize l_local_scope to contain just this map. This allows
1379 the use of dl_lookup_symbol_x to resolve symbols within the vdso.
1380 So we create a single entry list pointing to l_real as its only
1381 element */
1382 l->l_local_scope[0]->r_nlist = 1;
1383 l->l_local_scope[0]->r_list = &l->l_real;
1385 /* Now that we have the info handy, use the DSO image's soname
1386 so this object can be looked up by name. Note that we do not
1387 set l_name here. That field gives the file name of the DSO,
1388 and this DSO is not associated with any file. */
1389 if (l->l_info[DT_SONAME] != NULL)
1391 /* Work around a kernel problem. The kernel cannot handle
1392 addresses in the vsyscall DSO pages in writev() calls. */
1393 const char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB])
1394 + l->l_info[DT_SONAME]->d_un.d_val);
1395 size_t len = strlen (dsoname);
1396 char *copy = malloc (len);
1397 if (copy == NULL)
1398 _dl_fatal_printf ("out of memory\n");
1399 l->l_libname->name = l->l_name = memcpy (copy, dsoname, len);
1402 /* Add the vDSO to the object list. */
1403 _dl_add_to_namespace_list (l, LM_ID_BASE);
1405 /* Rearrange the list so this DSO appears after rtld_map. */
1406 assert (l->l_next == NULL);
1407 assert (l->l_prev == main_map);
1408 GL(dl_rtld_map).l_next = l;
1409 l->l_prev = &GL(dl_rtld_map);
1410 first_preload = &l->l_next;
1412 /* We have a prelinked DSO preloaded by the system. */
1413 GLRO(dl_sysinfo_map) = l;
1414 # ifdef NEED_DL_SYSINFO
1415 if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT)
1416 GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr;
1417 # endif
1420 #endif
1422 #ifdef DL_SYSDEP_OSCHECK
1423 DL_SYSDEP_OSCHECK (dl_fatal);
1424 #endif
1426 /* Initialize the data structures for the search paths for shared
1427 objects. */
1428 _dl_init_paths (library_path);
1430 /* Initialize _r_debug. */
1431 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1432 LM_ID_BASE);
1433 r->r_state = RT_CONSISTENT;
1435 /* Put the link_map for ourselves on the chain so it can be found by
1436 name. Note that at this point the global chain of link maps contains
1437 exactly one element, which is pointed to by dl_loaded. */
1438 if (! GL(dl_rtld_map).l_name)
1439 /* If not invoked directly, the dynamic linker shared object file was
1440 found by the PT_INTERP name. */
1441 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1442 GL(dl_rtld_map).l_type = lt_library;
1443 main_map->l_next = &GL(dl_rtld_map);
1444 GL(dl_rtld_map).l_prev = main_map;
1445 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1446 ++GL(dl_load_adds);
1448 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1449 to not using bias for non-prelinked PIEs and libraries
1450 and using it for executables or prelinked PIEs or libraries. */
1451 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1452 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1454 /* Set up the program header information for the dynamic linker
1455 itself. It is needed in the dl_iterate_phdr() callbacks. */
1456 ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start;
1457 ElfW(Phdr) *rtld_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start
1458 + rtld_ehdr->e_phoff);
1459 GL(dl_rtld_map).l_phdr = rtld_phdr;
1460 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1463 /* PT_GNU_RELRO is usually the last phdr. */
1464 size_t cnt = rtld_ehdr->e_phnum;
1465 while (cnt-- > 0)
1466 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1468 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1469 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1470 break;
1473 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1474 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1475 /* Assign a module ID. Do this before loading any audit modules. */
1476 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1478 /* If we have auditing DSOs to load, do it now. */
1479 if (__builtin_expect (audit_list != NULL, 0))
1481 /* Iterate over all entries in the list. The order is important. */
1482 struct audit_ifaces *last_audit = NULL;
1483 struct audit_list *al = audit_list->next;
1485 /* Since we start using the auditing DSOs right away we need to
1486 initialize the data structures now. */
1487 tcbp = init_tls ();
1489 /* Initialize security features. We need to do it this early
1490 since otherwise the constructors of the audit libraries will
1491 use different values (especially the pointer guard) and will
1492 fail later on. */
1493 security_init ();
1497 int tls_idx = GL(dl_tls_max_dtv_idx);
1499 /* Now it is time to determine the layout of the static TLS
1500 block and allocate it for the initial thread. Note that we
1501 always allocate the static block, we never defer it even if
1502 no DF_STATIC_TLS bit is set. The reason is that we know
1503 glibc will use the static model. */
1504 struct dlmopen_args dlmargs;
1505 dlmargs.fname = al->name;
1506 dlmargs.map = NULL;
1508 const char *objname;
1509 const char *err_str = NULL;
1510 bool malloced;
1511 (void) _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit,
1512 &dlmargs);
1513 if (__builtin_expect (err_str != NULL, 0))
1515 not_loaded:
1516 _dl_error_printf ("\
1517 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
1518 al->name, err_str);
1519 if (malloced)
1520 free ((char *) err_str);
1522 else
1524 struct lookup_args largs;
1525 largs.name = "la_version";
1526 largs.map = dlmargs.map;
1528 /* Check whether the interface version matches. */
1529 (void) _dl_catch_error (&objname, &err_str, &malloced,
1530 lookup_doit, &largs);
1532 unsigned int (*laversion) (unsigned int);
1533 unsigned int lav;
1534 if (err_str == NULL
1535 && (laversion = largs.result) != NULL
1536 && (lav = laversion (LAV_CURRENT)) > 0
1537 && lav <= LAV_CURRENT)
1539 /* Allocate structure for the callback function pointers.
1540 This call can never fail. */
1541 union
1543 struct audit_ifaces ifaces;
1544 #define naudit_ifaces 8
1545 void (*fptr[naudit_ifaces]) (void);
1546 } *newp = malloc (sizeof (*newp));
1548 /* Names of the auditing interfaces. All in one
1549 long string. */
1550 static const char audit_iface_names[] =
1551 "la_activity\0"
1552 "la_objsearch\0"
1553 "la_objopen\0"
1554 "la_preinit\0"
1555 #if __ELF_NATIVE_CLASS == 32
1556 "la_symbind32\0"
1557 #elif __ELF_NATIVE_CLASS == 64
1558 "la_symbind64\0"
1559 #else
1560 # error "__ELF_NATIVE_CLASS must be defined"
1561 #endif
1562 #define STRING(s) __STRING (s)
1563 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1564 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1565 "la_objclose\0";
1566 unsigned int cnt = 0;
1567 const char *cp = audit_iface_names;
1570 largs.name = cp;
1571 (void) _dl_catch_error (&objname, &err_str, &malloced,
1572 lookup_doit, &largs);
1574 /* Store the pointer. */
1575 if (err_str == NULL && largs.result != NULL)
1577 newp->fptr[cnt] = largs.result;
1579 /* The dynamic linker link map is statically
1580 allocated, initialize the data now. */
1581 GL(dl_rtld_map).l_audit[cnt].cookie
1582 = (intptr_t) &GL(dl_rtld_map);
1584 else
1585 newp->fptr[cnt] = NULL;
1586 ++cnt;
1588 cp = (char *) rawmemchr (cp, '\0') + 1;
1590 while (*cp != '\0');
1591 assert (cnt == naudit_ifaces);
1593 /* Now append the new auditing interface to the list. */
1594 newp->ifaces.next = NULL;
1595 if (last_audit == NULL)
1596 last_audit = GLRO(dl_audit) = &newp->ifaces;
1597 else
1598 last_audit = last_audit->next = &newp->ifaces;
1599 ++GLRO(dl_naudit);
1601 /* Mark the DSO as being used for auditing. */
1602 dlmargs.map->l_auditing = 1;
1604 else
1606 /* We cannot use the DSO, it does not have the
1607 appropriate interfaces or it expects something
1608 more recent. */
1609 #ifndef NDEBUG
1610 Lmid_t ns = dlmargs.map->l_ns;
1611 #endif
1612 _dl_close (dlmargs.map);
1614 /* Make sure the namespace has been cleared entirely. */
1615 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
1616 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
1618 GL(dl_tls_max_dtv_idx) = tls_idx;
1619 goto not_loaded;
1623 al = al->next;
1625 while (al != audit_list->next);
1627 /* If we have any auditing modules, announce that we already
1628 have two objects loaded. */
1629 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
1631 struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) };
1633 for (unsigned int outer = 0; outer < 2; ++outer)
1635 struct audit_ifaces *afct = GLRO(dl_audit);
1636 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1638 if (afct->objopen != NULL)
1640 ls[outer]->l_audit[cnt].bindflags
1641 = afct->objopen (ls[outer], LM_ID_BASE,
1642 &ls[outer]->l_audit[cnt].cookie);
1644 ls[outer]->l_audit_any_plt
1645 |= ls[outer]->l_audit[cnt].bindflags != 0;
1648 afct = afct->next;
1654 /* Set up debugging before the debugger is notified for the first time. */
1655 #ifdef ELF_MACHINE_DEBUG_SETUP
1656 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1657 ELF_MACHINE_DEBUG_SETUP (main_map, r);
1658 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1659 #else
1660 if (main_map->l_info[DT_DEBUG] != NULL)
1661 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1662 with the run-time address of the r_debug structure */
1663 main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1665 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1666 case you run gdb on the dynamic linker directly. */
1667 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1668 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1669 #endif
1671 /* We start adding objects. */
1672 r->r_state = RT_ADD;
1673 _dl_debug_state ();
1675 /* Auditing checkpoint: we are ready to signal that the initial map
1676 is being constructed. */
1677 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
1679 struct audit_ifaces *afct = GLRO(dl_audit);
1680 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1682 if (afct->activity != NULL)
1683 afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD);
1685 afct = afct->next;
1689 /* We have two ways to specify objects to preload: via environment
1690 variable and via the file /etc/ld.so.preload. The latter can also
1691 be used when security is enabled. */
1692 assert (*first_preload == NULL);
1693 struct link_map **preloads = NULL;
1694 unsigned int npreloads = 0;
1696 if (__builtin_expect (preloadlist != NULL, 0))
1698 /* The LD_PRELOAD environment variable gives list of libraries
1699 separated by white space or colons that are loaded before the
1700 executable's dependencies and prepended to the global scope
1701 list. If the binary is running setuid all elements
1702 containing a '/' are ignored since it is insecure. */
1703 char *list = strdupa (preloadlist);
1704 char *p;
1706 HP_TIMING_NOW (start);
1708 /* Prevent optimizing strsep. Speed is not important here. */
1709 while ((p = (strsep) (&list, " :")) != NULL)
1710 if (p[0] != '\0'
1711 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
1712 || strchr (p, '/') == NULL))
1713 npreloads += do_preload (p, main_map, "LD_PRELOAD");
1715 HP_TIMING_NOW (stop);
1716 HP_TIMING_DIFF (diff, start, stop);
1717 HP_TIMING_ACCUM_NT (load_time, diff);
1720 /* There usually is no ld.so.preload file, it should only be used
1721 for emergencies and testing. So the open call etc should usually
1722 fail. Using access() on a non-existing file is faster than using
1723 open(). So we do this first. If it succeeds we do almost twice
1724 the work but this does not matter, since it is not for production
1725 use. */
1726 static const char preload_file[] = "/etc/ld.so.preload";
1727 if (__builtin_expect (__access (preload_file, R_OK) == 0, 0))
1729 /* Read the contents of the file. */
1730 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1731 PROT_READ | PROT_WRITE);
1732 if (__builtin_expect (file != MAP_FAILED, 0))
1734 /* Parse the file. It contains names of libraries to be loaded,
1735 separated by white spaces or `:'. It may also contain
1736 comments introduced by `#'. */
1737 char *problem;
1738 char *runp;
1739 size_t rest;
1741 /* Eliminate comments. */
1742 runp = file;
1743 rest = file_size;
1744 while (rest > 0)
1746 char *comment = memchr (runp, '#', rest);
1747 if (comment == NULL)
1748 break;
1750 rest -= comment - runp;
1752 *comment = ' ';
1753 while (--rest > 0 && *++comment != '\n');
1756 /* We have one problematic case: if we have a name at the end of
1757 the file without a trailing terminating characters, we cannot
1758 place the \0. Handle the case separately. */
1759 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1760 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1762 problem = &file[file_size];
1763 while (problem > file && problem[-1] != ' '
1764 && problem[-1] != '\t'
1765 && problem[-1] != '\n' && problem[-1] != ':')
1766 --problem;
1768 if (problem > file)
1769 problem[-1] = '\0';
1771 else
1773 problem = NULL;
1774 file[file_size - 1] = '\0';
1777 HP_TIMING_NOW (start);
1779 if (file != problem)
1781 char *p;
1782 runp = file;
1783 while ((p = strsep (&runp, ": \t\n")) != NULL)
1784 if (p[0] != '\0')
1785 npreloads += do_preload (p, main_map, preload_file);
1788 if (problem != NULL)
1790 char *p = strndupa (problem, file_size - (problem - file));
1792 npreloads += do_preload (p, main_map, preload_file);
1795 HP_TIMING_NOW (stop);
1796 HP_TIMING_DIFF (diff, start, stop);
1797 HP_TIMING_ACCUM_NT (load_time, diff);
1799 /* We don't need the file anymore. */
1800 __munmap (file, file_size);
1804 if (__builtin_expect (*first_preload != NULL, 0))
1806 /* Set up PRELOADS with a vector of the preloaded libraries. */
1807 struct link_map *l = *first_preload;
1808 preloads = __alloca (npreloads * sizeof preloads[0]);
1809 i = 0;
1812 preloads[i++] = l;
1813 l = l->l_next;
1814 } while (l);
1815 assert (i == npreloads);
1818 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1819 specified some libraries to load, these are inserted before the actual
1820 dependencies in the executable's searchlist for symbol resolution. */
1821 HP_TIMING_NOW (start);
1822 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
1823 HP_TIMING_NOW (stop);
1824 HP_TIMING_DIFF (diff, start, stop);
1825 HP_TIMING_ACCUM_NT (load_time, diff);
1827 /* Mark all objects as being in the global scope. */
1828 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1829 main_map->l_searchlist.r_list[--i]->l_global = 1;
1831 /* Remove _dl_rtld_map from the chain. */
1832 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1833 if (GL(dl_rtld_map).l_next != NULL)
1834 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1836 for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1837 if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1838 break;
1840 bool rtld_multiple_ref = false;
1841 if (__builtin_expect (i < main_map->l_searchlist.r_nlist, 1))
1843 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1844 put it back in the list of visible objects. We insert it into the
1845 chain in symbol search order because gdb uses the chain's order as
1846 its symbol search order. */
1847 rtld_multiple_ref = true;
1849 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1850 if (__builtin_expect (mode, normal) == normal)
1852 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1853 ? main_map->l_searchlist.r_list[i + 1]
1854 : NULL);
1855 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1856 if (GLRO(dl_sysinfo_map) != NULL
1857 && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
1858 && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
1859 GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
1860 #endif
1862 else
1863 /* In trace mode there might be an invisible object (which we
1864 could not find) after the previous one in the search list.
1865 In this case it doesn't matter much where we put the
1866 interpreter object, so we just initialize the list pointer so
1867 that the assertion below holds. */
1868 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1870 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1871 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1872 if (GL(dl_rtld_map).l_next != NULL)
1874 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1875 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1879 /* Now let us see whether all libraries are available in the
1880 versions we need. */
1882 struct version_check_args args;
1883 args.doexit = mode == normal;
1884 args.dotrace = mode == trace;
1885 _dl_receive_error (print_missing_version, version_check_doit, &args);
1888 /* We do not initialize any of the TLS functionality unless any of the
1889 initial modules uses TLS. This makes dynamic loading of modules with
1890 TLS impossible, but to support it requires either eagerly doing setup
1891 now or lazily doing it later. Doing it now makes us incompatible with
1892 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1893 used. Trying to do it lazily is too hairy to try when there could be
1894 multiple threads (from a non-TLS-using libpthread). */
1895 bool was_tls_init_tp_called = tls_init_tp_called;
1896 if (tcbp == NULL)
1897 tcbp = init_tls ();
1899 if (__builtin_expect (audit_list == NULL, 1))
1900 /* Initialize security features. But only if we have not done it
1901 earlier. */
1902 security_init ();
1904 if (__builtin_expect (mode, normal) != normal)
1906 /* We were run just to list the shared libraries. It is
1907 important that we do this before real relocation, because the
1908 functions we call below for output may no longer work properly
1909 after relocation. */
1910 struct link_map *l;
1912 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1914 struct r_scope_elem *scope = &main_map->l_searchlist;
1916 for (i = 0; i < scope->r_nlist; i++)
1918 l = scope->r_list [i];
1919 if (l->l_faked)
1921 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1922 continue;
1924 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
1925 GLRO(dl_trace_prelink_map) = l;
1926 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1927 l->l_libname->name[0] ? l->l_libname->name
1928 : rtld_progname ?: "<main program>",
1929 l->l_name[0] ? l->l_name
1930 : rtld_progname ?: "<main program>",
1931 (int) sizeof l->l_map_start * 2,
1932 (size_t) l->l_map_start,
1933 (int) sizeof l->l_addr * 2,
1934 (size_t) l->l_addr);
1936 if (l->l_tls_modid)
1937 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1938 (int) sizeof l->l_tls_offset * 2,
1939 (size_t) l->l_tls_offset);
1940 else
1941 _dl_printf ("\n");
1944 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
1946 /* Look through the dependencies of the main executable
1947 and determine which of them is not actually
1948 required. */
1949 struct link_map *l = main_map;
1951 /* Relocate the main executable. */
1952 struct relocate_args args = { .l = l,
1953 .reloc_mode = ((GLRO(dl_lazy)
1954 ? RTLD_LAZY : 0)
1955 | __RTLD_NOIFUNC) };
1956 _dl_receive_error (print_unresolved, relocate_doit, &args);
1958 /* This loop depends on the dependencies of the executable to
1959 correspond in number and order to the DT_NEEDED entries. */
1960 ElfW(Dyn) *dyn = main_map->l_ld;
1961 bool first = true;
1962 while (dyn->d_tag != DT_NULL)
1964 if (dyn->d_tag == DT_NEEDED)
1966 l = l->l_next;
1968 if (!l->l_used)
1970 if (first)
1972 _dl_printf ("Unused direct dependencies:\n");
1973 first = false;
1976 _dl_printf ("\t%s\n", l->l_name);
1980 ++dyn;
1983 _exit (first != true);
1985 else if (! main_map->l_info[DT_NEEDED])
1986 _dl_printf ("\tstatically linked\n");
1987 else
1989 for (l = main_map->l_next; l; l = l->l_next)
1990 if (l->l_faked)
1991 /* The library was not found. */
1992 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1993 else if (strcmp (l->l_libname->name, l->l_name) == 0)
1994 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
1995 (int) sizeof l->l_map_start * 2,
1996 (size_t) l->l_map_start);
1997 else
1998 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1999 l->l_name, (int) sizeof l->l_map_start * 2,
2000 (size_t) l->l_map_start);
2003 if (__builtin_expect (mode, trace) != trace)
2004 for (i = 1; i < (unsigned int) _dl_argc; ++i)
2006 const ElfW(Sym) *ref = NULL;
2007 ElfW(Addr) loadbase;
2008 lookup_t result;
2010 result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map,
2011 &ref, main_map->l_scope,
2012 NULL, ELF_RTYPE_CLASS_PLT,
2013 DL_LOOKUP_ADD_DEPENDENCY, NULL);
2015 loadbase = LOOKUP_VALUE_ADDRESS (result);
2017 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
2018 INTUSE(_dl_argv)[i],
2019 (int) sizeof ref->st_value * 2,
2020 (size_t) ref->st_value,
2021 (int) sizeof loadbase * 2, (size_t) loadbase);
2023 else
2025 /* If LD_WARN is set, warn about undefined symbols. */
2026 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
2028 /* We have to do symbol dependency testing. */
2029 struct relocate_args args;
2030 unsigned int i;
2032 args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
2033 | __RTLD_NOIFUNC);
2035 i = main_map->l_searchlist.r_nlist;
2036 while (i-- > 0)
2038 struct link_map *l = main_map->l_initfini[i];
2039 if (l != &GL(dl_rtld_map) && ! l->l_faked)
2041 args.l = l;
2042 _dl_receive_error (print_unresolved, relocate_doit,
2043 &args);
2047 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
2048 && rtld_multiple_ref)
2050 /* Mark the link map as not yet relocated again. */
2051 GL(dl_rtld_map).l_relocated = 0;
2052 _dl_relocate_object (&GL(dl_rtld_map),
2053 main_map->l_scope, __RTLD_NOIFUNC, 0);
2056 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2057 if (version_info)
2059 /* Print more information. This means here, print information
2060 about the versions needed. */
2061 int first = 1;
2062 struct link_map *map;
2064 for (map = main_map; map != NULL; map = map->l_next)
2066 const char *strtab;
2067 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
2068 ElfW(Verneed) *ent;
2070 if (dyn == NULL)
2071 continue;
2073 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
2074 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
2076 if (first)
2078 _dl_printf ("\n\tVersion information:\n");
2079 first = 0;
2082 _dl_printf ("\t%s:\n",
2083 map->l_name[0] ? map->l_name : rtld_progname);
2085 while (1)
2087 ElfW(Vernaux) *aux;
2088 struct link_map *needed;
2090 needed = find_needed (strtab + ent->vn_file);
2091 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
2093 while (1)
2095 const char *fname = NULL;
2097 if (needed != NULL
2098 && match_version (strtab + aux->vna_name,
2099 needed))
2100 fname = needed->l_name;
2102 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2103 strtab + ent->vn_file,
2104 strtab + aux->vna_name,
2105 aux->vna_flags & VER_FLG_WEAK
2106 ? "[WEAK] " : "",
2107 fname ?: "not found");
2109 if (aux->vna_next == 0)
2110 /* No more symbols. */
2111 break;
2113 /* Next symbol. */
2114 aux = (ElfW(Vernaux) *) ((char *) aux
2115 + aux->vna_next);
2118 if (ent->vn_next == 0)
2119 /* No more dependencies. */
2120 break;
2122 /* Next dependency. */
2123 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
2129 _exit (0);
2132 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
2133 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
2134 && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
2136 ElfW(Lib) *liblist, *liblistend;
2137 struct link_map **r_list, **r_listend, *l;
2138 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
2140 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
2141 liblist = (ElfW(Lib) *)
2142 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
2143 liblistend = (ElfW(Lib) *)
2144 ((char *) liblist +
2145 main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
2146 r_list = main_map->l_searchlist.r_list;
2147 r_listend = r_list + main_map->l_searchlist.r_nlist;
2149 for (; r_list < r_listend && liblist < liblistend; r_list++)
2151 l = *r_list;
2153 if (l == main_map)
2154 continue;
2156 /* If the library is not mapped where it should, fail. */
2157 if (l->l_addr)
2158 break;
2160 /* Next, check if checksum matches. */
2161 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
2162 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
2163 != liblist->l_checksum)
2164 break;
2166 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
2167 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
2168 != liblist->l_time_stamp)
2169 break;
2171 if (! _dl_name_match_p (strtab + liblist->l_name, l))
2172 break;
2174 ++liblist;
2178 if (r_list == r_listend && liblist == liblistend)
2179 prelinked = true;
2181 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2182 _dl_debug_printf ("\nprelink checking: %s\n",
2183 prelinked ? "ok" : "failed");
2187 /* Now set up the variable which helps the assembler startup code. */
2188 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
2190 /* Save the information about the original global scope list since
2191 we need it in the memory handling later. */
2192 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
2194 /* Remember the last search directory added at startup, now that
2195 malloc will no longer be the one from dl-minimal.c. */
2196 GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
2198 /* Print scope information. */
2199 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
2201 _dl_debug_printf ("\nInitial object scopes\n");
2203 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2204 _dl_show_scope (l, 0);
2207 if (prelinked)
2209 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2211 ElfW(Rela) *conflict, *conflictend;
2212 #ifndef HP_TIMING_NONAVAIL
2213 hp_timing_t start;
2214 hp_timing_t stop;
2215 #endif
2217 HP_TIMING_NOW (start);
2218 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2219 conflict = (ElfW(Rela) *)
2220 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2221 conflictend = (ElfW(Rela) *)
2222 ((char *) conflict
2223 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2224 _dl_resolve_conflicts (main_map, conflict, conflictend);
2225 HP_TIMING_NOW (stop);
2226 HP_TIMING_DIFF (relocate_time, start, stop);
2230 /* Mark all the objects so we know they have been already relocated. */
2231 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2233 l->l_relocated = 1;
2234 if (l->l_relro_size)
2235 _dl_protect_relro (l);
2237 /* Add object to slot information data if necessasy. */
2238 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2239 _dl_add_to_slotinfo (l);
2242 else
2244 /* Now we have all the objects loaded. Relocate them all except for
2245 the dynamic linker itself. We do this in reverse order so that copy
2246 relocs of earlier objects overwrite the data written by later
2247 objects. We do not re-relocate the dynamic linker itself in this
2248 loop because that could result in the GOT entries for functions we
2249 call being changed, and that would break us. It is safe to relocate
2250 the dynamic linker out of order because it has no copy relocs (we
2251 know that because it is self-contained). */
2253 int consider_profiling = GLRO(dl_profile) != NULL;
2254 #ifndef HP_TIMING_NONAVAIL
2255 hp_timing_t start;
2256 hp_timing_t stop;
2257 #endif
2259 /* If we are profiling we also must do lazy reloaction. */
2260 GLRO(dl_lazy) |= consider_profiling;
2262 HP_TIMING_NOW (start);
2263 unsigned i = main_map->l_searchlist.r_nlist;
2264 while (i-- > 0)
2266 struct link_map *l = main_map->l_initfini[i];
2268 /* While we are at it, help the memory handling a bit. We have to
2269 mark some data structures as allocated with the fake malloc()
2270 implementation in ld.so. */
2271 struct libname_list *lnp = l->l_libname->next;
2273 while (__builtin_expect (lnp != NULL, 0))
2275 lnp->dont_free = 1;
2276 lnp = lnp->next;
2279 if (l != &GL(dl_rtld_map))
2280 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2281 consider_profiling);
2283 /* Add object to slot information data if necessasy. */
2284 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2285 _dl_add_to_slotinfo (l);
2287 HP_TIMING_NOW (stop);
2289 HP_TIMING_DIFF (relocate_time, start, stop);
2291 /* Now enable profiling if needed. Like the previous call,
2292 this has to go here because the calls it makes should use the
2293 rtld versions of the functions (particularly calloc()), but it
2294 needs to have _dl_profile_map set up by the relocator. */
2295 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
2296 /* We must prepare the profiling. */
2297 _dl_start_profile ();
2300 #ifndef NONTLS_INIT_TP
2301 # define NONTLS_INIT_TP do { } while (0)
2302 #endif
2304 if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2305 ++GL(dl_tls_generation);
2307 /* Now that we have completed relocation, the initializer data
2308 for the TLS blocks has its final values and we can copy them
2309 into the main thread's TLS area, which we allocated above. */
2310 _dl_allocate_tls_init (tcbp);
2312 /* And finally install it for the main thread. If ld.so itself uses
2313 TLS we know the thread pointer was initialized earlier. */
2314 if (! tls_init_tp_called)
2316 const char *lossage
2317 #ifdef USE___THREAD
2318 = TLS_INIT_TP (tcbp, USE___THREAD);
2319 #else
2320 = TLS_INIT_TP (tcbp, 0);
2321 #endif
2322 if (__builtin_expect (lossage != NULL, 0))
2323 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2324 lossage);
2327 /* Make sure no new search directories have been added. */
2328 assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2330 if (! prelinked && rtld_multiple_ref)
2332 /* There was an explicit ref to the dynamic linker as a shared lib.
2333 Re-relocate ourselves with user-controlled symbol definitions.
2335 We must do this after TLS initialization in case after this
2336 re-relocation, we might call a user-supplied function
2337 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2339 #ifndef HP_TIMING_NONAVAIL
2340 hp_timing_t start;
2341 hp_timing_t stop;
2342 hp_timing_t add;
2343 #endif
2345 HP_TIMING_NOW (start);
2346 /* Mark the link map as not yet relocated again. */
2347 GL(dl_rtld_map).l_relocated = 0;
2348 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2349 HP_TIMING_NOW (stop);
2350 HP_TIMING_DIFF (add, start, stop);
2351 HP_TIMING_ACCUM_NT (relocate_time, add);
2354 /* Do any necessary cleanups for the startup OS interface code.
2355 We do these now so that no calls are made after rtld re-relocation
2356 which might be resolved to different functions than we expect.
2357 We cannot do this before relocating the other objects because
2358 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2359 _dl_sysdep_start_cleanup ();
2361 #ifdef SHARED
2362 /* Auditing checkpoint: we have added all objects. */
2363 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
2365 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2366 /* Do not call the functions for any auditing object. */
2367 if (head->l_auditing == 0)
2369 struct audit_ifaces *afct = GLRO(dl_audit);
2370 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2372 if (afct->activity != NULL)
2373 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
2375 afct = afct->next;
2379 #endif
2381 /* Notify the debugger all new objects are now ready to go. We must re-get
2382 the address since by now the variable might be in another object. */
2383 r = _dl_debug_initialize (0, LM_ID_BASE);
2384 r->r_state = RT_CONSISTENT;
2385 _dl_debug_state ();
2387 #ifndef MAP_COPY
2388 /* We must munmap() the cache file. */
2389 _dl_unload_cache ();
2390 #endif
2392 /* Once we return, _dl_sysdep_start will invoke
2393 the DT_INIT functions and then *USER_ENTRY. */
2396 /* This is a little helper function for resolving symbols while
2397 tracing the binary. */
2398 static void
2399 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2400 const char *errstring)
2402 if (objname[0] == '\0')
2403 objname = rtld_progname ?: "<main program>";
2404 _dl_error_printf ("%s (%s)\n", errstring, objname);
2407 /* This is a little helper function for resolving symbols while
2408 tracing the binary. */
2409 static void
2410 print_missing_version (int errcode __attribute__ ((unused)),
2411 const char *objname, const char *errstring)
2413 _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
2414 objname, errstring);
2417 /* Nonzero if any of the debugging options is enabled. */
2418 static int any_debug attribute_relro;
2420 /* Process the string given as the parameter which explains which debugging
2421 options are enabled. */
2422 static void
2423 process_dl_debug (const char *dl_debug)
2425 /* When adding new entries make sure that the maximal length of a name
2426 is correctly handled in the LD_DEBUG_HELP code below. */
2427 static const struct
2429 unsigned char len;
2430 const char name[10];
2431 const char helptext[41];
2432 unsigned short int mask;
2433 } debopts[] =
2435 #define LEN_AND_STR(str) sizeof (str) - 1, str
2436 { LEN_AND_STR ("libs"), "display library search paths",
2437 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2438 { LEN_AND_STR ("reloc"), "display relocation processing",
2439 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2440 { LEN_AND_STR ("files"), "display progress for input file",
2441 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2442 { LEN_AND_STR ("symbols"), "display symbol table processing",
2443 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2444 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2445 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2446 { LEN_AND_STR ("versions"), "display version dependencies",
2447 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2448 { LEN_AND_STR ("scopes"), "display scope information",
2449 DL_DEBUG_SCOPES },
2450 { LEN_AND_STR ("all"), "all previous options combined",
2451 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2452 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2453 | DL_DEBUG_SCOPES },
2454 { LEN_AND_STR ("statistics"), "display relocation statistics",
2455 DL_DEBUG_STATISTICS },
2456 { LEN_AND_STR ("unused"), "determined unused DSOs",
2457 DL_DEBUG_UNUSED },
2458 { LEN_AND_STR ("help"), "display this help message and exit",
2459 DL_DEBUG_HELP },
2461 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2463 /* Skip separating white spaces and commas. */
2464 while (*dl_debug != '\0')
2466 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2468 size_t cnt;
2469 size_t len = 1;
2471 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2472 && dl_debug[len] != ',' && dl_debug[len] != ':')
2473 ++len;
2475 for (cnt = 0; cnt < ndebopts; ++cnt)
2476 if (debopts[cnt].len == len
2477 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2479 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2480 any_debug = 1;
2481 break;
2484 if (cnt == ndebopts)
2486 /* Display a warning and skip everything until next
2487 separator. */
2488 char *copy = strndupa (dl_debug, len);
2489 _dl_error_printf ("\
2490 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2493 dl_debug += len;
2494 continue;
2497 ++dl_debug;
2500 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2502 size_t cnt;
2504 _dl_printf ("\
2505 Valid options for the LD_DEBUG environment variable are:\n\n");
2507 for (cnt = 0; cnt < ndebopts; ++cnt)
2508 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2509 " " + debopts[cnt].len - 3,
2510 debopts[cnt].helptext);
2512 _dl_printf ("\n\
2513 To direct the debugging output into a file instead of standard output\n\
2514 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2515 _exit (0);
2519 static void
2520 process_dl_audit (char *str)
2522 /* The parameter is a colon separated list of DSO names. */
2523 char *p;
2525 while ((p = (strsep) (&str, ":")) != NULL)
2526 if (p[0] != '\0'
2527 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
2528 || strchr (p, '/') == NULL))
2530 /* This is using the local malloc, not the system malloc. The
2531 memory can never be freed. */
2532 struct audit_list *newp = malloc (sizeof (*newp));
2533 newp->name = p;
2535 if (audit_list == NULL)
2536 audit_list = newp->next = newp;
2537 else
2539 newp->next = audit_list->next;
2540 audit_list = audit_list->next = newp;
2545 /* Process all environments variables the dynamic linker must recognize.
2546 Since all of them start with `LD_' we are a bit smarter while finding
2547 all the entries. */
2548 extern char **_environ attribute_hidden;
2551 static void
2552 process_envvars (enum mode *modep)
2554 char **runp = _environ;
2555 char *envline;
2556 enum mode mode = normal;
2557 char *debug_output = NULL;
2559 /* This is the default place for profiling data file. */
2560 GLRO(dl_profile_output)
2561 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
2563 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2565 size_t len = 0;
2567 while (envline[len] != '\0' && envline[len] != '=')
2568 ++len;
2570 if (envline[len] != '=')
2571 /* This is a "LD_" variable at the end of the string without
2572 a '=' character. Ignore it since otherwise we will access
2573 invalid memory below. */
2574 continue;
2576 switch (len)
2578 case 4:
2579 /* Warning level, verbose or not. */
2580 if (memcmp (envline, "WARN", 4) == 0)
2581 GLRO(dl_verbose) = envline[5] != '\0';
2582 break;
2584 case 5:
2585 /* Debugging of the dynamic linker? */
2586 if (memcmp (envline, "DEBUG", 5) == 0)
2588 process_dl_debug (&envline[6]);
2589 break;
2591 if (memcmp (envline, "AUDIT", 5) == 0)
2592 process_dl_audit (&envline[6]);
2593 break;
2595 case 7:
2596 /* Print information about versions. */
2597 if (memcmp (envline, "VERBOSE", 7) == 0)
2599 version_info = envline[8] != '\0';
2600 break;
2603 /* List of objects to be preloaded. */
2604 if (memcmp (envline, "PRELOAD", 7) == 0)
2606 preloadlist = &envline[8];
2607 break;
2610 /* Which shared object shall be profiled. */
2611 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2612 GLRO(dl_profile) = &envline[8];
2613 break;
2615 case 8:
2616 /* Do we bind early? */
2617 if (memcmp (envline, "BIND_NOW", 8) == 0)
2619 GLRO(dl_lazy) = envline[9] == '\0';
2620 break;
2622 if (memcmp (envline, "BIND_NOT", 8) == 0)
2623 GLRO(dl_bind_not) = envline[9] != '\0';
2624 break;
2626 case 9:
2627 /* Test whether we want to see the content of the auxiliary
2628 array passed up from the kernel. */
2629 if (!INTUSE(__libc_enable_secure)
2630 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2631 _dl_show_auxv ();
2632 break;
2634 case 10:
2635 /* Mask for the important hardware capabilities. */
2636 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
2637 GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL,
2638 0, 0);
2639 break;
2641 case 11:
2642 /* Path where the binary is found. */
2643 if (!INTUSE(__libc_enable_secure)
2644 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2645 GLRO(dl_origin_path) = &envline[12];
2646 break;
2648 case 12:
2649 /* The library search path. */
2650 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
2652 library_path = &envline[13];
2653 break;
2656 /* Where to place the profiling data file. */
2657 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2659 debug_output = &envline[13];
2660 break;
2663 if (!INTUSE(__libc_enable_secure)
2664 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2665 GLRO(dl_dynamic_weak) = 1;
2666 break;
2668 case 13:
2669 /* We might have some extra environment variable with length 13
2670 to handle. */
2671 #ifdef EXTRA_LD_ENVVARS_13
2672 EXTRA_LD_ENVVARS_13
2673 #endif
2674 if (!INTUSE(__libc_enable_secure)
2675 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2677 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2678 break;
2681 if (memcmp (envline, "POINTER_GUARD", 13) == 0)
2682 GLRO(dl_pointer_guard) = envline[14] != '0';
2683 break;
2685 case 14:
2686 /* Where to place the profiling data file. */
2687 if (!INTUSE(__libc_enable_secure)
2688 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2689 && envline[15] != '\0')
2690 GLRO(dl_profile_output) = &envline[15];
2691 break;
2693 case 16:
2694 /* The mode of the dynamic linker can be set. */
2695 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2697 mode = trace;
2698 GLRO(dl_verbose) = 1;
2699 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2700 GLRO(dl_trace_prelink) = &envline[17];
2702 break;
2704 case 20:
2705 /* The mode of the dynamic linker can be set. */
2706 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2707 mode = trace;
2708 break;
2710 /* We might have some extra environment variable to handle. This
2711 is tricky due to the pre-processing of the length of the name
2712 in the switch statement here. The code here assumes that added
2713 environment variables have a different length. */
2714 #ifdef EXTRA_LD_ENVVARS
2715 EXTRA_LD_ENVVARS
2716 #endif
2720 /* The caller wants this information. */
2721 *modep = mode;
2723 /* Extra security for SUID binaries. Remove all dangerous environment
2724 variables. */
2725 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
2727 static const char unsecure_envvars[] =
2728 #ifdef EXTRA_UNSECURE_ENVVARS
2729 EXTRA_UNSECURE_ENVVARS
2730 #endif
2731 UNSECURE_ENVVARS;
2732 const char *nextp;
2734 nextp = unsecure_envvars;
2737 unsetenv (nextp);
2738 /* We could use rawmemchr but this need not be fast. */
2739 nextp = (char *) (strchr) (nextp, '\0') + 1;
2741 while (*nextp != '\0');
2743 if (__access ("/etc/suid-debug", F_OK) != 0)
2745 unsetenv ("MALLOC_CHECK_");
2746 GLRO(dl_debug_mask) = 0;
2749 if (mode != normal)
2750 _exit (5);
2752 /* If we have to run the dynamic linker in debugging mode and the
2753 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2754 messages to this file. */
2755 else if (any_debug && debug_output != NULL)
2757 #ifdef O_NOFOLLOW
2758 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2759 #else
2760 const int flags = O_WRONLY | O_APPEND | O_CREAT;
2761 #endif
2762 size_t name_len = strlen (debug_output);
2763 char buf[name_len + 12];
2764 char *startp;
2766 buf[name_len + 11] = '\0';
2767 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2768 *--startp = '.';
2769 startp = memcpy (startp - name_len, debug_output, name_len);
2771 GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
2772 if (GLRO(dl_debug_fd) == -1)
2773 /* We use standard output if opening the file failed. */
2774 GLRO(dl_debug_fd) = STDOUT_FILENO;
2779 /* Print the various times we collected. */
2780 static void
2781 __attribute ((noinline))
2782 print_statistics (hp_timing_t *rtld_total_timep)
2784 #ifndef HP_TIMING_NONAVAIL
2785 char buf[200];
2786 char *cp;
2787 char *wp;
2789 /* Total time rtld used. */
2790 if (HP_TIMING_AVAIL)
2792 HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
2793 _dl_debug_printf ("\nruntime linker statistics:\n"
2794 " total startup time in dynamic loader: %s\n", buf);
2796 /* Print relocation statistics. */
2797 char pbuf[30];
2798 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
2799 cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
2800 pbuf + sizeof (pbuf), 10, 0);
2801 wp = pbuf;
2802 switch (pbuf + sizeof (pbuf) - cp)
2804 case 3:
2805 *wp++ = *cp++;
2806 case 2:
2807 *wp++ = *cp++;
2808 case 1:
2809 *wp++ = '.';
2810 *wp++ = *cp++;
2812 *wp = '\0';
2813 _dl_debug_printf ("\
2814 time needed for relocation: %s (%s%%)\n", buf, pbuf);
2816 #endif
2818 unsigned long int num_relative_relocations = 0;
2819 for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2821 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2822 continue;
2824 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2826 for (unsigned int i = 0; i < scope->r_nlist; i++)
2828 struct link_map *l = scope->r_list [i];
2830 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2831 num_relative_relocations
2832 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2833 #ifndef ELF_MACHINE_REL_RELATIVE
2834 /* Relative relocations are processed on these architectures if
2835 library is loaded to different address than p_vaddr or
2836 if not prelinked. */
2837 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2838 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2839 #else
2840 /* On e.g. IA-64 or Alpha, relative relocations are processed
2841 only if library is loaded to different address than p_vaddr. */
2842 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2843 #endif
2844 num_relative_relocations
2845 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2849 _dl_debug_printf (" number of relocations: %lu\n"
2850 " number of relocations from cache: %lu\n"
2851 " number of relative relocations: %lu\n",
2852 GL(dl_num_relocations),
2853 GL(dl_num_cache_relocations),
2854 num_relative_relocations);
2856 #ifndef HP_TIMING_NONAVAIL
2857 /* Time spend while loading the object and the dependencies. */
2858 if (HP_TIMING_AVAIL)
2860 char pbuf[30];
2861 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
2862 cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
2863 pbuf + sizeof (pbuf), 10, 0);
2864 wp = pbuf;
2865 switch (pbuf + sizeof (pbuf) - cp)
2867 case 3:
2868 *wp++ = *cp++;
2869 case 2:
2870 *wp++ = *cp++;
2871 case 1:
2872 *wp++ = '.';
2873 *wp++ = *cp++;
2875 *wp = '\0';
2876 _dl_debug_printf ("\
2877 time needed to load objects: %s (%s%%)\n",
2878 buf, pbuf);
2880 #endif