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