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