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