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