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