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