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