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