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