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