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