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