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