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