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