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