]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/rtld.c
* elf/rtld.c (_dl_start_final): Move _begin, _end decls outside the fn.
[thirdparty/glibc.git] / elf / rtld.c
CommitLineData
d66e34cd 1/* Run time dynamic linker.
e66d0a4c 2 Copyright (C) 1995-1999, 2000, 2001, 2002 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
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
d66e34cd 19
7d0b1164 20#include <errno.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>
2064087b 26#include <sys/mman.h> /* Check if MAP_ANON is defined. */
af8bf6bd 27#include <sys/param.h>
ba9fcb3f 28#include <sys/stat.h>
a42195db 29#include <ldsodefs.h>
ce37fa88 30#include <stdio-common/_itoa.h>
f21acc89 31#include <entry.h>
c94a8080 32#include <fpu_control.h>
db276fa1 33#include <hp-timing.h>
cf197e41 34#include <bits/libc-lock.h>
f5348425 35#include "dynamic-link.h"
e2102c14 36#include "dl-librecon.h"
74955460 37#include <unsecvars.h>
5688da55
UD
38#include <dl-cache.h>
39#include <dl-procinfo.h>
f5348425 40
a853022c 41#include <assert.h>
f5348425 42
6ce3881d
RM
43/* Avoid PLT use for our local calls at startup. */
44extern __typeof (__mempcpy) __mempcpy attribute_hidden;
45
46/* GCC has mental blocks about _exit. */
47extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
48#define _exit exit_internal
49
fd26970f 50/* Helper function to handle errors while resolving symbols. */
c84142e8
UD
51static void print_unresolved (int errcode, const char *objname,
52 const char *errsting);
53
54/* Helper function to handle errors when a version is missing. */
55static void print_missing_version (int errcode, const char *objname,
56 const char *errsting);
fd26970f 57
db276fa1
UD
58/* Print the various times we collected. */
59static void print_statistics (void);
ea278354
UD
60
61/* This is a list of all the modes the dynamic loader can be in. */
62enum mode { normal, list, verify, trace };
63
64/* Process all environments variables the dynamic linker must recognize.
65 Since all of them start with `LD_' we are a bit smarter while finding
66 all the entries. */
ba9fcb3f 67static void process_envvars (enum mode *modep);
ea278354 68
5c82e15e 69int _dl_argc attribute_hidden;
e6caf4e1
UD
70char **_dl_argv = NULL;
71INTDEF(_dl_argv)
5c82e15e
UD
72
73/* Nonzero if we were run directly. */
74unsigned int _dl_skip_args attribute_hidden;
cf197e41 75
39778c6c
UD
76/* Set nonzero during loading and initialization of executable and
77 libraries, cleared before the executable's entry point runs. This
78 must not be initialized to nonzero, because the unused dynamic
79 linker loaded in for libc.so's "ld.so.1" dep will provide the
80 definition seen by libc.so's initializer; that value must be zero,
81 and will be since that dynamic linker's _dl_start and dl_main will
82 never be called. */
e6caf4e1
UD
83int _dl_starting_up = 0;
84INTVARDEF(_dl_starting_up)
39778c6c 85
d6b5d570
UD
86/* This is the structure which defines all variables global to ld.so
87 (except those which cannot be added for some reason). */
5688da55
UD
88struct rtld_global _rtld_global =
89 {
ccdf0cab
UD
90 /* Get architecture specific initializer. */
91#include <dl-procinfo.c>
5688da55
UD
92 ._dl_debug_fd = STDERR_FILENO,
93#if 1
94 /* XXX I know about at least one case where we depend on the old
95 weak behavior (it has to do with librt). Until we get DSO
96 groups implemented we have to make this the default.
97 Bummer. --drepper */
98 ._dl_dynamic_weak = 1,
99#endif
100 ._dl_lazy = 1,
101 ._dl_fpu_control = _FPU_DEFAULT,
102 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
103 ._dl_hwcap_mask = HWCAP_IMPORTANT,
ffa8d2a0 104#ifdef _LIBC_REENTRANT
5688da55 105 ._dl_load_lock = _LIBC_LOCK_RECURSIVE_INITIALIZER
ffa8d2a0 106#endif
5688da55 107 };
0d01dace 108strong_alias (_rtld_global, _rtld_local);
c0fb8a56 109
67ddea92 110static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
266180eb 111 ElfW(Addr) *user_entry);
d66e34cd 112
d6b5d570
UD
113static struct libname_list _dl_rtld_libname;
114static struct libname_list _dl_rtld_libname2;
86d2c878 115
eaad82e0
UD
116/* We expect less than a second for relocation. */
117#ifdef HP_SMALL_TIMING_AVAIL
118# undef HP_TIMING_AVAIL
119# define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
120#endif
121
db276fa1 122/* Variable for statistics. */
5732c4df 123#ifndef HP_TIMING_NONAVAIL
db276fa1
UD
124static hp_timing_t rtld_total_time;
125static hp_timing_t relocate_time;
126static hp_timing_t load_time;
01d8e36d 127static hp_timing_t start_time;
5732c4df 128#endif
db276fa1 129
2a76f7ef
UD
130/* Additional definitions needed by TLS initialization. */
131#ifdef TLS_INIT_HELPER
132TLS_INIT_HELPER
133#endif
134
01d8e36d
UD
135/* Before ld.so is relocated we must not access variables which need
136 relocations. This means variables which are exported. Variables
137 declared as static are fine. If we can mark a variable hidden this
138 is fine, too. The latter is impotant here. We can avoid setting
139 up a temporary link map for ld.so if we can mark _rtld_global as
140 hidden. */
9df89390
UD
141#if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \
142 && defined HAVE_VISIBILITY_ATTRIBUTE
01d8e36d
UD
143# define DONT_USE_BOOTSTRAP_MAP 1
144#endif
145
146#ifdef DONT_USE_BOOTSTRAP_MAP
147static ElfW(Addr) _dl_start_final (void *arg);
148#else
149static ElfW(Addr) _dl_start_final (void *arg,
150 struct link_map *bootstrap_map_p);
151#endif
6a1db4ff 152
65da9563
RM
153/* These defined magically in the linker script. */
154extern char _begin[] attribute_hidden;
155extern char _end[] attribute_hidden;
156
157
b1dbbaa4
RM
158#ifdef RTLD_START
159RTLD_START
160#else
eaad82e0 161# error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
b1dbbaa4
RM
162#endif
163
c2248c44
RM
164#ifndef VALIDX
165# define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
166 + DT_EXTRANUM + DT_VALTAGIDX (tag))
167#endif
168#ifndef ADDRIDX
169# define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
170 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
171#endif
172
173/* This is the second half of _dl_start (below). It can be inlined safely
174 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
175 references. When the tools don't permit us to avoid using a GOT entry
176 for _dl_rtld_global (no attribute_hidden support), we must make sure
177 this function is not inlined (see below). */
178
179#ifdef DONT_USE_BOOTSTRAP_MAP
180static inline ElfW(Addr) __attribute__ ((always_inline))
181_dl_start_final (void *arg)
182#else
183static ElfW(Addr) __attribute__ ((noinline))
184_dl_start_final (void *arg, struct link_map *bootstrap_map_p)
185#endif
186{
187 ElfW(Addr) start_addr;
c2248c44
RM
188
189 if (HP_TIMING_AVAIL)
190 {
191 /* If it hasn't happen yet record the startup time. */
192 if (! HP_TIMING_INLINE)
193 HP_TIMING_NOW (start_time);
194
195 /* Initialize the timing functions. */
196 HP_TIMING_DIFF_INIT ();
197 }
198
199 /* Transfer data about ourselves to the permanent link_map structure. */
200#ifndef DONT_USE_BOOTSTRAP_MAP
201 GL(dl_rtld_map).l_addr = bootstrap_map_p->l_addr;
202 GL(dl_rtld_map).l_ld = bootstrap_map_p->l_ld;
203 memcpy (GL(dl_rtld_map).l_info, bootstrap_map_p->l_info,
204 sizeof GL(dl_rtld_map).l_info);
205 GL(dl_rtld_map).l_mach = bootstrap_map_p->l_mach;
206#endif
207 _dl_setup_hash (&GL(dl_rtld_map));
208 GL(dl_rtld_map).l_opencount = 1;
209 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
210 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
211 /* Copy the TLS related data if necessary. */
212#if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
213# ifdef HAVE___THREAD
214 assert (bootstrap_map_p->l_tls_modid != 0);
215# else
216 if (bootstrap_map_p->l_tls_modid != 0)
217# endif
218 {
219 GL(dl_rtld_map).l_tls_blocksize = bootstrap_map_p->l_tls_blocksize;
220 GL(dl_rtld_map).l_tls_align = bootstrap_map_p->l_tls_align;
221 GL(dl_rtld_map).l_tls_initimage_size
222 = bootstrap_map_p->l_tls_initimage_size;
223 GL(dl_rtld_map).l_tls_initimage = bootstrap_map_p->l_tls_initimage;
224 GL(dl_rtld_map).l_tls_offset = bootstrap_map_p->l_tls_offset;
225 GL(dl_rtld_map).l_tls_modid = 1;
226 GL(dl_rtld_map).l_tls_tp_initialized
227 = bootstrap_map_p->l_tls_tp_initialized;
228 }
229#endif
230
231#if HP_TIMING_AVAIL
232 HP_TIMING_NOW (GL(dl_cpuclock_offset));
233#endif
234
235 /* Call the OS-dependent function to set up life so we can do things like
236 file access. It will call `dl_main' (below) to do all the real work
237 of the dynamic linker, and then unwind our frame and run the user
238 entry point on the same stack we entered on. */
239 start_addr = _dl_sysdep_start (arg, &dl_main);
240
241#ifndef HP_TIMING_NONAVAIL
242 if (HP_TIMING_AVAIL)
243 {
244 hp_timing_t end_time;
245
246 /* Get the current time. */
247 HP_TIMING_NOW (end_time);
248
249 /* Compute the difference. */
250 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
251 }
252#endif
253
254 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
255 print_statistics ();
256
257 return start_addr;
258}
259
50746436 260static ElfW(Addr) __attribute_used__ internal_function
d66e34cd
RM
261_dl_start (void *arg)
262{
01d8e36d
UD
263#ifdef DONT_USE_BOOTSTRAP_MAP
264# define bootstrap_map GL(dl_rtld_map)
265#else
86d2c878 266 struct link_map bootstrap_map;
01d8e36d 267#endif
739d440d 268#if !defined HAVE_BUILTIN_MEMSET || defined USE_TLS
264ec183 269 size_t cnt;
1b4575ae 270#endif
739d440d
UD
271#ifdef USE_TLS
272 ElfW(Ehdr) *ehdr;
273 ElfW(Phdr) *phdr;
274 dtv_t initdtv[3];
275#endif
d66e34cd 276
b1dbbaa4
RM
277 /* This #define produces dynamic linking inline functions for
278 bootstrap relocation instead of general-purpose relocation. */
279#define RTLD_BOOTSTRAP
c0282c06
UD
280#define RESOLVE_MAP(sym, version, flags) \
281 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
50463d27
UD
282#define RESOLVE(sym, version, flags) \
283 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
b1dbbaa4
RM
284#include "dynamic-link.h"
285
db276fa1
UD
286 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
287 HP_TIMING_NOW (start_time);
288
e66d0a4c
UD
289 /* Partly clean the `bootstrap_map' structure up. Don't use
290 `memset' since it might not be built in or inlined and we cannot
291 make function calls at this point. Use '__builtin_memset' if we
01d8e36d
UD
292 know it is available. We do not have to clear the memory if we
293 do not have to use the temporary bootstrap_map. Global variables
294 are initialized to zero by default. */
295#ifndef DONT_USE_BOOTSTRAP_MAP
296# ifdef HAVE_BUILTIN_MEMSET
e66d0a4c 297 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
01d8e36d 298# else
264ec183
UD
299 for (cnt = 0;
300 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
301 ++cnt)
302 bootstrap_map.l_info[cnt] = 0;
01d8e36d 303# endif
e66d0a4c 304#endif
264ec183 305
d66e34cd 306 /* Figure out the run-time load address of the dynamic linker itself. */
86d2c878 307 bootstrap_map.l_addr = elf_machine_load_address ();
d66e34cd 308
47707456
UD
309 /* Read our own dynamic section and fill in the info array. */
310 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
d3ac2d47 311 elf_get_dynamic_info (&bootstrap_map);
d66e34cd 312
739d440d 313#if USE_TLS
01d8e36d 314# if !defined HAVE___THREAD && !defined DONT_USE_BOOTSTRAP_MAP
739d440d
UD
315 /* Signal that we have not found TLS data so far. */
316 bootstrap_map.l_tls_modid = 0;
317# endif
318
65da9563
RM
319 /* Get the dynamic linker's own program header. First we need the ELF
320 file header. The `_begin' symbol created by the linker script points
321 to it. When we have something like GOTOFF relocs, we can use a plain
322 reference to find the runtime address. Without that, we have to rely
323 on the `l_addr' value, which is not the value we want when prelinked. */
324#ifdef DONT_USE_BOOTSTRAP_MAP
325 ehdr = (ElfW(Ehdr) *) &_begin;
326#else
739d440d 327 ehdr = (ElfW(Ehdr) *) bootstrap_map.l_addr;
65da9563
RM
328#endif
329 phdr = (ElfW(Phdr) *) ((ElfW(Addr)) ehdr + ehdr->e_phoff);
739d440d
UD
330 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
331 if (phdr[cnt].p_type == PT_TLS)
332 {
333 void *tlsblock;
334 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
f884de5b 335 char *p;
739d440d
UD
336
337 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
338 bootstrap_map.l_tls_align = phdr[cnt].p_align;
339 assert (bootstrap_map.l_tls_blocksize != 0);
340 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
341 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
a816b435 342 + phdr[cnt].p_vaddr);
739d440d
UD
343
344 /* We can now allocate the initial TLS block. This can happen
345 on the stack. We'll get the final memory later when we
346 know all about the various objects loaded at startup
347 time. */
348# if TLS_TCB_AT_TP
349 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
350 TLS_INIT_TCB_ALIGN)
351 + TLS_INIT_TCB_SIZE
352 + max_align);
353# elif TLS_DTV_AT_TP
354 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
355 bootstrap_map.l_tls_align)
356 + bootstrap_map.l_tls_blocksize
357 + max_align);
358# else
359 /* In case a model with a different layout for the TCB and DTV
360 is defined add another #elif here and in the following #ifs. */
361# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
362# endif
363 /* Align the TLS block. */
364 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
365 & ~(max_align - 1));
366
367 /* Initialize the dtv. [0] is the length, [1] the generation
368 counter. */
369 initdtv[0].counter = 1;
370 initdtv[1].counter = 0;
371
372 /* Initialize the TLS block. */
373# if TLS_TCB_AT_TP
374 initdtv[2].pointer = tlsblock;
375# elif TLS_DTV_AT_TP
376 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
377 bootstrap_map.l_tls_align);
378 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
379# else
380# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
381# endif
f884de5b
UD
382 p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
383 bootstrap_map.l_tls_initimage_size);
384# ifdef HAVE_BUILTIN_MEMSET
385 __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
386 - bootstrap_map.l_tls_initimage_size));
387# else
388 {
389 size_t remaining = (bootstrap_map.l_tls_blocksize
390 - bootstrap_map.l_tls_initimage_size);
391 while (remaining-- > 0)
392 *p++ = '\0';
393 }
394#endif
739d440d
UD
395
396 /* Install the pointer to the dtv. */
397
398 /* Initialize the thread pointer. */
399# if TLS_TCB_AT_TP
400 bootstrap_map.l_tls_offset
401 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
402
403 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
404 initdtv);
405
9d69bd24 406 if (TLS_INIT_TP ((char *) tlsblock + bootstrap_map.l_tls_offset, 0)
d78ce054 407 != 0)
2a76f7ef 408 _dl_fatal_printf ("cannot setup thread-local storage\n");
739d440d
UD
409# elif TLS_DTV_AT_TP
410 INSTALL_DTV (tlsblock, initdtv);
9d69bd24 411 if (TLS_INIT_TP (tlsblock, 0) != 0)
2a76f7ef 412 _dl_fatal_printf ("cannot setup thread-local storage\n");
739d440d
UD
413# else
414# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
415# endif
416
417 /* So far this is module number one. */
418 bootstrap_map.l_tls_modid = 1;
9d69bd24
UD
419 /* The TP got initialized. */
420 bootstrap_map.l_tls_tp_initialized = 1;
739d440d
UD
421
422 /* There can only be one PT_TLS entry. */
423 break;
424 }
425#endif /* use TLS */
426
d66e34cd 427#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
86d2c878 428 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
d66e34cd
RM
429#endif
430
32e6df36
UD
431 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
432 {
433 /* Relocate ourselves so we can do normal function calls and
434 data access using the global offset table. */
435
436 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
437 }
421f82e5 438
ea7eb7e3
UD
439 /* Please note that we don't allow profiling of this object and
440 therefore need not test whether we have to allocate the array
441 for the relocation results (as done in dl-reloc.c). */
421f82e5 442
d66e34cd
RM
443 /* Now life is sane; we can call functions and access global data.
444 Set up to use the operating system facilities, and find out from
445 the operating system's program loader where to find the program
6a1db4ff
UD
446 header table in core. Put the rest of _dl_start into a separate
447 function, that way the compiler cannot put accesses to the GOT
448 before ELF_DYNAMIC_RELOCATE. */
c0282c06 449 {
01d8e36d
UD
450#ifdef DONT_USE_BOOTSTRAP_MAP
451 ElfW(Addr) entry = _dl_start_final (arg);
452#else
453 ElfW(Addr) entry = _dl_start_final (arg, &bootstrap_map);
454#endif
c0282c06
UD
455
456#ifndef ELF_MACHINE_START_ADDRESS
457# define ELF_MACHINE_START_ADDRESS(map, start) (start)
458#endif
459
d6b5d570 460 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded), entry);
c0282c06 461 }
6a1db4ff
UD
462}
463
464
d66e34cd 465
d66e34cd
RM
466/* Now life is peachy; we can do all normal operations.
467 On to the real work. */
468
993b3242
UD
469/* Some helper functions. */
470
471/* Arguments to relocate_doit. */
472struct relocate_args
473{
474 struct link_map *l;
475 int lazy;
476};
477
478struct map_args
479{
480 /* Argument to map_doit. */
481 char *str;
482 /* Return value of map_doit. */
483 struct link_map *main_map;
484};
485
486/* Arguments to version_check_doit. */
487struct version_check_args
488{
993b3242 489 int doexit;
145b8413 490 int dotrace;
993b3242
UD
491};
492
493static void
494relocate_doit (void *a)
495{
496 struct relocate_args *args = (struct relocate_args *) a;
497
cff26a3e 498 INTUSE(_dl_relocate_object) (args->l, args->l->l_scope, args->lazy, 0);
993b3242
UD
499}
500
501static void
502map_doit (void *a)
503{
be935610 504 struct map_args *args = (struct map_args *) a;
cff26a3e 505 args->main_map = INTUSE(_dl_map_object) (NULL, args->str, 0, lt_library, 0, 0);
993b3242
UD
506}
507
508static void
509version_check_doit (void *a)
510{
be935610 511 struct version_check_args *args = (struct version_check_args *) a;
d6b5d570 512 if (_dl_check_all_versions (GL(dl_loaded), 1, args->dotrace) && args->doexit)
993b3242
UD
513 /* We cannot start the application. Abort now. */
514 _exit (1);
515}
516
ce37fa88
UD
517
518static inline struct link_map *
519find_needed (const char *name)
520{
d6b5d570 521 unsigned int n = GL(dl_loaded)->l_searchlist.r_nlist;
ce37fa88 522
be935610 523 while (n-- > 0)
d6b5d570
UD
524 if (_dl_name_match_p (name, GL(dl_loaded)->l_searchlist.r_list[n]))
525 return GL(dl_loaded)->l_searchlist.r_list[n];
ce37fa88
UD
526
527 /* Should never happen. */
528 return NULL;
529}
530
531static int
532match_version (const char *string, struct link_map *map)
533{
a42195db 534 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
ce37fa88
UD
535 ElfW(Verdef) *def;
536
b0982c4a 537#define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
ce37fa88
UD
538 if (map->l_info[VERDEFTAG] == NULL)
539 /* The file has no symbol versioning. */
540 return 0;
541
542 def = (ElfW(Verdef) *) ((char *) map->l_addr
543 + map->l_info[VERDEFTAG]->d_un.d_ptr);
544 while (1)
545 {
546 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
547
548 /* Compare the version strings. */
549 if (strcmp (string, strtab + aux->vda_name) == 0)
550 /* Bingo! */
551 return 1;
552
553 /* If no more definitions we failed to find what we want. */
554 if (def->vd_next == 0)
555 break;
556
557 /* Next definition. */
558 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
559 }
560
561 return 0;
562}
563
120b4c49
UD
564static const char *library_path; /* The library search path. */
565static const char *preloadlist; /* The list preloaded objects. */
566static int version_info; /* Nonzero if information about
567 versions has to be printed. */
a1a9d215 568
d66e34cd 569static void
266180eb 570dl_main (const ElfW(Phdr) *phdr,
72f70279 571 ElfW(Word) phnum,
266180eb 572 ElfW(Addr) *user_entry)
d66e34cd 573{
266180eb 574 const ElfW(Phdr) *ph;
ea278354 575 enum mode mode;
2064087b
RM
576 struct link_map **preloads;
577 unsigned int npreloads;
14bab8de
UD
578 size_t file_size;
579 char *file;
164a7164 580 bool has_interp = false;
77aba05b 581 unsigned int i;
164a7164
UD
582 bool prelinked = false;
583 bool rtld_is_main = false;
5732c4df 584#ifndef HP_TIMING_NONAVAIL
db276fa1
UD
585 hp_timing_t start;
586 hp_timing_t stop;
587 hp_timing_t diff;
5732c4df 588#endif
a52d1562
UD
589#ifdef USE_TLS
590 void *tcbp;
591#endif
d66e34cd 592
ea278354 593 /* Process the environment variable which control the behaviour. */
ba9fcb3f 594 process_envvars (&mode);
3996f34b 595
46ec036d 596 /* Set up a flag which tells we are just starting. */
e6caf4e1 597 INTUSE(_dl_starting_up) = 1;
46ec036d 598
a16956f3 599 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
0200214b
RM
600 {
601 /* Ho ho. We are not the program interpreter! We are the program
602 itself! This means someone ran ld.so as a command. Well, that
603 might be convenient to do sometimes. We support it by
604 interpreting the args like this:
605
606 ld.so PROGRAM ARGS...
607
608 The first argument is the name of a file containing an ELF
609 executable we will load and run with the following arguments.
610 To simplify life here, PROGRAM is searched for using the
611 normal rules for shared objects, rather than $PATH or anything
612 like that. We just load it and use its entry point; we don't
613 pay attention to its PT_INTERP command (we are the interpreter
614 ourselves). This is an easy way to test a new ld.so before
615 installing it. */
164a7164 616 rtld_is_main = true;
421f82e5 617
ffee1316 618 /* Note the place where the dynamic linker actually came from. */
e6caf4e1 619 GL(dl_rtld_map).l_name = rtld_progname;
6a76c115 620
fd26970f 621 while (_dl_argc > 1)
e6caf4e1 622 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
fd26970f
UD
623 {
624 mode = list;
5688da55 625 GL(dl_lazy) = -1; /* This means do no dependency analysis. */
61965e9b 626
fd26970f
UD
627 ++_dl_skip_args;
628 --_dl_argc;
e6caf4e1 629 ++INTUSE(_dl_argv);
fd26970f 630 }
e6caf4e1 631 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
fd26970f
UD
632 {
633 mode = verify;
6a76c115 634
fd26970f
UD
635 ++_dl_skip_args;
636 --_dl_argc;
e6caf4e1 637 ++INTUSE(_dl_argv);
fd26970f 638 }
e6caf4e1
UD
639 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
640 && _dl_argc > 2)
880f421f 641 {
e6caf4e1 642 library_path = INTUSE(_dl_argv)[2];
880f421f 643
310930c1
UD
644 _dl_skip_args += 2;
645 _dl_argc -= 2;
e6caf4e1 646 INTUSE(_dl_argv) += 2;
310930c1 647 }
e6caf4e1
UD
648 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
649 && _dl_argc > 2)
310930c1 650 {
e6caf4e1 651 GL(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
310930c1 652
880f421f
UD
653 _dl_skip_args += 2;
654 _dl_argc -= 2;
e6caf4e1 655 INTUSE(_dl_argv) += 2;
880f421f 656 }
fd26970f
UD
657 else
658 break;
d66e34cd 659
61eb22d3
UD
660 /* If we have no further argument the program was called incorrectly.
661 Grant the user some education. */
662 if (_dl_argc < 2)
35fc382a 663 _dl_fatal_printf ("\
2bcf29ba 664Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
61eb22d3
UD
665You have invoked `ld.so', the helper program for shared library executables.\n\
666This program usually lives in the file `/lib/ld.so', and special directives\n\
667in executable files using ELF shared libraries tell the system's program\n\
668loader to load the helper program from this file. This helper program loads\n\
669the shared libraries needed by the program executable, prepares the program\n\
670to run, and runs it. You may invoke this helper program directly from the\n\
671command line to load and run an ELF executable file; this is like executing\n\
672that file itself, but always uses this helper program from the file you\n\
673specified, instead of the helper program file specified in the executable\n\
674file you run. This is mostly of use for maintainers to test new versions\n\
2bcf29ba
UD
675of this helper program; chances are you did not intend to run this program.\n\
676\n\
b0a01055
UD
677 --list list all dependencies and how they are resolved\n\
678 --verify verify that given object really is a dynamically linked\n\
e8b1163e 679 object we can handle\n\
b0a01055
UD
680 --library-path PATH use given PATH instead of content of the environment\n\
681 variable LD_LIBRARY_PATH\n\
fcf70d41 682 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
35fc382a 683 in LIST\n");
61eb22d3 684
0200214b
RM
685 ++_dl_skip_args;
686 --_dl_argc;
e6caf4e1 687 ++INTUSE(_dl_argv);
91f62ce6 688
da832465
UD
689 /* Initialize the data structures for the search paths for shared
690 objects. */
691 _dl_init_paths (library_path);
692
9a821cf9 693 if (__builtin_expect (mode, normal) == verify)
2de99474 694 {
8e17ea58
UD
695 const char *objname;
696 const char *err_str = NULL;
993b3242 697 struct map_args args;
2de99474 698
e6caf4e1 699 args.str = rtld_progname;
cff26a3e 700 (void) INTUSE(_dl_catch_error) (&objname, &err_str, map_doit, &args);
8e17ea58 701 if (__builtin_expect (err_str != NULL, 0))
e6caf4e1
UD
702 /* We don't free the returned string, the programs stops
703 anyway. */
704 _exit (EXIT_FAILURE);
2de99474
UD
705 }
706 else
db276fa1
UD
707 {
708 HP_TIMING_NOW (start);
e6caf4e1 709 INTUSE(_dl_map_object) (NULL, rtld_progname, 0, lt_library, 0, 0);
db276fa1 710 HP_TIMING_NOW (stop);
61e0617a 711
db276fa1
UD
712 HP_TIMING_DIFF (load_time, start, stop);
713 }
2de99474 714
d6b5d570
UD
715 phdr = GL(dl_loaded)->l_phdr;
716 phnum = GL(dl_loaded)->l_phnum;
143e2b96
UD
717 /* We overwrite here a pointer to a malloc()ed string. But since
718 the malloc() implementation used at this point is the dummy
719 implementations which has no real free() function it does not
720 makes sense to free the old string first. */
d6b5d570
UD
721 GL(dl_loaded)->l_name = (char *) "";
722 *user_entry = GL(dl_loaded)->l_entry;
0200214b
RM
723 }
724 else
725 {
726 /* Create a link_map for the executable itself.
727 This will be what dlopen on "" returns. */
87c812c2 728 _dl_new_object ((char *) "", "", lt_executable, NULL);
d6b5d570 729 if (GL(dl_loaded) == NULL)
35fc382a 730 _dl_fatal_printf ("cannot allocate memory for link map\n");
d6b5d570
UD
731 GL(dl_loaded)->l_phdr = phdr;
732 GL(dl_loaded)->l_phnum = phnum;
733 GL(dl_loaded)->l_entry = *user_entry;
da832465 734
61e0617a
UD
735 /* At this point we are in a bit of trouble. We would have to
736 fill in the values for l_dev and l_ino. But in general we
737 do not know where the file is. We also do not handle AT_EXECFD
738 even if it would be passed up.
739
740 We leave the values here defined to 0. This is normally no
741 problem as the program code itself is normally no shared
742 object and therefore cannot be loaded dynamically. Nothing
743 prevent the use of dynamic binaries and in these situations
744 we might get problems. We might not be able to find out
745 whether the object is already loaded. But since there is no
746 easy way out and because the dynamic binary must also not
747 have an SONAME we ignore this program for now. If it becomes
748 a problem we can force people using SONAMEs. */
749
97a51d8a
UD
750 /* We delay initializing the path structure until we got the dynamic
751 information for the program. */
0200214b
RM
752 }
753
d6b5d570 754 GL(dl_loaded)->l_map_end = 0;
052b6a6c 755 /* Perhaps the executable has no PT_LOAD header entries at all. */
d6b5d570 756 GL(dl_loaded)->l_map_start = ~0;
e7beef5f 757 /* We opened the file, account for it. */
d6b5d570 758 ++GL(dl_loaded)->l_opencount;
052b6a6c 759
0200214b 760 /* Scan the program header table for the dynamic section. */
72f70279 761 for (ph = phdr; ph < &phdr[phnum]; ++ph)
0200214b
RM
762 switch (ph->p_type)
763 {
da832465
UD
764 case PT_PHDR:
765 /* Find out the load address. */
d6b5d570 766 GL(dl_loaded)->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
da832465 767 break;
0200214b
RM
768 case PT_DYNAMIC:
769 /* This tells us where to find the dynamic section,
770 which tells us everything we need to do. */
d6b5d570 771 GL(dl_loaded)->l_ld = (void *) GL(dl_loaded)->l_addr + ph->p_vaddr;
0200214b
RM
772 break;
773 case PT_INTERP:
774 /* This "interpreter segment" was used by the program loader to
775 find the program interpreter, which is this program itself, the
776 dynamic linker. We note what name finds us, so that a future
777 dlopen call or DT_NEEDED entry, for something that wants to link
778 against the dynamic linker as a shared library, will know that
779 the shared object is already loaded. */
d6b5d570 780 _dl_rtld_libname.name = ((const char *) GL(dl_loaded)->l_addr
be935610 781 + ph->p_vaddr);
752a2a50 782 /* _dl_rtld_libname.next = NULL; Already zero. */
d6b5d570 783 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
f41c8091
UD
784
785 /* Ordinarilly, we would get additional names for the loader from
786 our DT_SONAME. This can't happen if we were actually linked as
787 a static executable (detect this case when we have no DYNAMIC).
788 If so, assume the filename component of the interpreter path to
789 be our SONAME, and add it to our name list. */
d6b5d570 790 if (GL(dl_rtld_map).l_ld == NULL)
f41c8091 791 {
88794e30
UD
792 const char *p = NULL;
793 const char *cp = _dl_rtld_libname.name;
794
795 /* Find the filename part of the path. */
796 while (*cp != '\0')
797 if (*cp++ == '/')
798 p = cp;
799
800 if (p != NULL)
f41c8091 801 {
88794e30 802 _dl_rtld_libname2.name = p;
752a2a50 803 /* _dl_rtld_libname2.next = NULL; Already zero. */
f41c8091
UD
804 _dl_rtld_libname.next = &_dl_rtld_libname2;
805 }
806 }
807
164a7164 808 has_interp = true;
0200214b 809 break;
052b6a6c 810 case PT_LOAD:
052b6a6c
UD
811 {
812 ElfW(Addr) mapstart;
2373b30e
UD
813 ElfW(Addr) allocend;
814
815 /* Remember where the main program starts in memory. */
d6b5d570
UD
816 mapstart = (GL(dl_loaded)->l_addr
817 + (ph->p_vaddr & ~(ph->p_align - 1)));
818 if (GL(dl_loaded)->l_map_start > mapstart)
819 GL(dl_loaded)->l_map_start = mapstart;
2373b30e
UD
820
821 /* Also where it ends. */
d6b5d570
UD
822 allocend = GL(dl_loaded)->l_addr + ph->p_vaddr + ph->p_memsz;
823 if (GL(dl_loaded)->l_map_end < allocend)
824 GL(dl_loaded)->l_map_end = allocend;
052b6a6c
UD
825 }
826 break;
a2f1f5cb
UD
827#ifdef USE_TLS
828 case PT_TLS:
aed283dd
UD
829 if (ph->p_memsz > 0)
830 {
831 /* Note that in the case the dynamic linker we duplicate work
832 here since we read the PT_TLS entry already in
833 _dl_start_final. But the result is repeatable so do not
834 check for this special but unimportant case. */
835 GL(dl_loaded)->l_tls_blocksize = ph->p_memsz;
836 GL(dl_loaded)->l_tls_align = ph->p_align;
837 GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz;
838 GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr;
839
840 /* This image gets the ID one. */
841 GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1;
842 }
a2f1f5cb
UD
843 break;
844#endif
0200214b 845 }
75aafc71
RM
846#ifdef USE_TLS
847 /* Adjust the address of the TLS initialization image in case
848 the executable is actually an ET_DYN object. */
849 if (GL(dl_loaded)->l_tls_initimage != NULL)
850 GL(dl_loaded)->l_tls_initimage
851 = (char *) GL(dl_loaded)->l_tls_initimage + GL(dl_loaded)->l_addr;
852#endif
d6b5d570
UD
853 if (! GL(dl_loaded)->l_map_end)
854 GL(dl_loaded)->l_map_end = ~0;
855 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
c84142e8
UD
856 {
857 /* We were invoked directly, so the program might not have a
858 PT_INTERP. */
d6b5d570 859 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
3fb55878 860 /* _dl_rtld_libname.next = NULL; Already zero. */
d6b5d570 861 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
c84142e8 862 }
ffee1316 863 else
d6b5d570 864 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
0200214b 865
9a51759b
UD
866 if (! rtld_is_main)
867 {
868 /* Extract the contents of the dynamic section for easy access. */
d6b5d570
UD
869 elf_get_dynamic_info (GL(dl_loaded));
870 if (GL(dl_loaded)->l_info[DT_HASH])
9a51759b 871 /* Set up our cache of pointers into the hash table. */
d6b5d570 872 _dl_setup_hash (GL(dl_loaded));
9a51759b 873 }
0200214b 874
9a821cf9 875 if (__builtin_expect (mode, normal) == verify)
e2102c14
UD
876 {
877 /* We were called just to verify that this is a dynamic
878 executable using us as the program interpreter. Exit with an
879 error if we were not able to load the binary or no interpreter
880 is specified (i.e., this is no dynamically linked binary. */
d6b5d570 881 if (GL(dl_loaded)->l_ld == NULL)
e2102c14 882 _exit (1);
e2102c14
UD
883
884 /* We allow here some platform specific code. */
885#ifdef DISTINGUISH_LIB_VERSIONS
886 DISTINGUISH_LIB_VERSIONS;
887#endif
eb406346 888 _exit (has_interp ? 0 : 2);
e2102c14
UD
889 }
890
9a51759b 891 if (! rtld_is_main)
97a51d8a
UD
892 /* Initialize the data structures for the search paths for shared
893 objects. */
120b4c49 894 _dl_init_paths (library_path);
97a51d8a 895
0200214b 896 /* Put the link_map for ourselves on the chain so it can be found by
ceb2d9aa 897 name. Note that at this point the global chain of link maps contains
d6b5d570
UD
898 exactly one element, which is pointed to by dl_loaded. */
899 if (! GL(dl_rtld_map).l_name)
ffee1316
RM
900 /* If not invoked directly, the dynamic linker shared object file was
901 found by the PT_INTERP name. */
d6b5d570
UD
902 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
903 GL(dl_rtld_map).l_type = lt_library;
904 GL(dl_loaded)->l_next = &GL(dl_rtld_map);
905 GL(dl_rtld_map).l_prev = GL(dl_loaded);
906 ++GL(dl_nloaded);
0200214b 907
14bab8de 908 /* We have two ways to specify objects to preload: via environment
49c091e5 909 variable and via the file /etc/ld.so.preload. The latter can also
14bab8de 910 be used when security is enabled. */
2064087b
RM
911 preloads = NULL;
912 npreloads = 0;
14bab8de 913
db33f7d4 914 if (__builtin_expect (preloadlist != NULL, 0))
c4029823 915 {
566efee2
UD
916 /* The LD_PRELOAD environment variable gives list of libraries
917 separated by white space or colons that are loaded before the
fd26970f
UD
918 executable's dependencies and prepended to the global scope
919 list. If the binary is running setuid all elements
920 containing a '/' are ignored since it is insecure. */
921 char *list = strdupa (preloadlist);
922 char *p;
db276fa1
UD
923
924 HP_TIMING_NOW (start);
925
9710f75d
UD
926 /* Prevent optimizing strsep. Speed is not important here. */
927 while ((p = (strsep) (&list, " :")) != NULL)
e2102c14 928 if (p[0] != '\0'
e6caf4e1 929 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
db33f7d4 930 || strchr (p, '/') == NULL))
fd26970f 931 {
87837aac
UD
932 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
933 p, 1,
934 lt_library,
cff26a3e 935 0, 0);
42c4f32a 936 if (++new_map->l_opencount == 1)
bd355af0
UD
937 /* It is no duplicate. */
938 ++npreloads;
fd26970f 939 }
db276fa1
UD
940
941 HP_TIMING_NOW (stop);
942 HP_TIMING_DIFF (diff, start, stop);
943 HP_TIMING_ACCUM_NT (load_time, diff);
c4029823
UD
944 }
945
14bab8de
UD
946 /* Read the contents of the file. */
947 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
948 PROT_READ | PROT_WRITE);
40b07f5b 949 if (__builtin_expect (file != MAP_FAILED, 0))
14bab8de
UD
950 {
951 /* Parse the file. It contains names of libraries to be loaded,
952 separated by white spaces or `:'. It may also contain
953 comments introduced by `#'. */
954 char *problem;
955 char *runp;
956 size_t rest;
957
958 /* Eliminate comments. */
959 runp = file;
960 rest = file_size;
961 while (rest > 0)
962 {
963 char *comment = memchr (runp, '#', rest);
964 if (comment == NULL)
965 break;
966
967 rest -= comment - runp;
968 do
969 *comment = ' ';
970 while (--rest > 0 && *++comment != '\n');
971 }
972
973 /* We have one problematic case: if we have a name at the end of
974 the file without a trailing terminating characters, we cannot
975 place the \0. Handle the case separately. */
49891c10
UD
976 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
977 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
14bab8de
UD
978 {
979 problem = &file[file_size];
980 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
49891c10 981 && problem[-1] != '\n' && problem[-1] != ':')
14bab8de
UD
982 --problem;
983
984 if (problem > file)
985 problem[-1] = '\0';
986 }
987 else
49891c10
UD
988 {
989 problem = NULL;
990 file[file_size - 1] = '\0';
991 }
14bab8de 992
db276fa1
UD
993 HP_TIMING_NOW (start);
994
14bab8de
UD
995 if (file != problem)
996 {
997 char *p;
e2102c14 998 runp = file;
14bab8de 999 while ((p = strsep (&runp, ": \t\n")) != NULL)
e2102c14
UD
1000 if (p[0] != '\0')
1001 {
cff26a3e
AJ
1002 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
1003 p, 1,
1004 lt_library,
1005 0, 0);
42c4f32a 1006 if (++new_map->l_opencount == 1)
e2102c14
UD
1007 /* It is no duplicate. */
1008 ++npreloads;
1009 }
14bab8de
UD
1010 }
1011
1012 if (problem != NULL)
1013 {
1014 char *p = strndupa (problem, file_size - (problem - file));
87837aac
UD
1015 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded), p,
1016 1, lt_library,
1017 0, 0);
42c4f32a 1018 if (++new_map->l_opencount == 1)
bd355af0
UD
1019 /* It is no duplicate. */
1020 ++npreloads;
14bab8de
UD
1021 }
1022
db276fa1
UD
1023 HP_TIMING_NOW (stop);
1024 HP_TIMING_DIFF (diff, start, stop);
1025 HP_TIMING_ACCUM_NT (load_time, diff);
1026
14bab8de
UD
1027 /* We don't need the file anymore. */
1028 __munmap (file, file_size);
1029 }
1030
db33f7d4 1031 if (__builtin_expect (npreloads, 0) != 0)
14bab8de
UD
1032 {
1033 /* Set up PRELOADS with a vector of the preloaded libraries. */
1034 struct link_map *l;
14bab8de 1035 preloads = __alloca (npreloads * sizeof preloads[0]);
d6b5d570 1036 l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */
14bab8de
UD
1037 i = 0;
1038 do
1039 {
1040 preloads[i++] = l;
1041 l = l->l_next;
1042 } while (l);
1043 assert (i == npreloads);
1044 }
1045
2064087b
RM
1046 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1047 specified some libraries to load, these are inserted before the actual
1048 dependencies in the executable's searchlist for symbol resolution. */
db276fa1 1049 HP_TIMING_NOW (start);
87837aac
UD
1050 INTUSE(_dl_map_object_deps) (GL(dl_loaded), preloads, npreloads,
1051 mode == trace, 0);
db276fa1
UD
1052 HP_TIMING_NOW (stop);
1053 HP_TIMING_DIFF (diff, start, stop);
1054 HP_TIMING_ACCUM_NT (load_time, diff);
e3e35cfc 1055
42c4f32a
UD
1056 /* Mark all objects as being in the global scope and set the open
1057 counter. */
d6b5d570 1058 for (i = GL(dl_loaded)->l_searchlist.r_nlist; i > 0; )
42c4f32a
UD
1059 {
1060 --i;
d6b5d570
UD
1061 GL(dl_loaded)->l_searchlist.r_list[i]->l_global = 1;
1062 ++GL(dl_loaded)->l_searchlist.r_list[i]->l_opencount;
42c4f32a 1063 }
d66e34cd 1064
2064087b 1065#ifndef MAP_ANON
f332db02
RM
1066 /* We are done mapping things, so close the zero-fill descriptor. */
1067 __close (_dl_zerofd);
1068 _dl_zerofd = -1;
2064087b 1069#endif
f332db02 1070
f9496a7b 1071 /* Remove _dl_rtld_map from the chain. */
d6b5d570
UD
1072 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1073 if (GL(dl_rtld_map).l_next)
1074 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
f9496a7b 1075
d6b5d570 1076 if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1))
0200214b 1077 {
f9496a7b
RM
1078 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1079 put it back in the list of visible objects. We insert it into the
1080 chain in symbol search order because gdb uses the chain's order as
1081 its symbol search order. */
77aba05b 1082 i = 1;
d6b5d570 1083 while (GL(dl_loaded)->l_searchlist.r_list[i] != &GL(dl_rtld_map))
f9496a7b 1084 ++i;
d6b5d570 1085 GL(dl_rtld_map).l_prev = GL(dl_loaded)->l_searchlist.r_list[i - 1];
b2bcd61a 1086 if (__builtin_expect (mode, normal) == normal)
d6b5d570
UD
1087 GL(dl_rtld_map).l_next = (i + 1 < GL(dl_loaded)->l_searchlist.r_nlist
1088 ? GL(dl_loaded)->l_searchlist.r_list[i + 1]
1089 : NULL);
b2bcd61a
UD
1090 else
1091 /* In trace mode there might be an invisible object (which we
1092 could not find) after the previous one in the search list.
1093 In this case it doesn't matter much where we put the
1094 interpreter object, so we just initialize the list pointer so
1095 that the assertion below holds. */
d6b5d570 1096 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
b2bcd61a 1097
d6b5d570
UD
1098 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1099 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
3fb55878 1100 if (GL(dl_rtld_map).l_next != NULL)
f9496a7b 1101 {
d6b5d570
UD
1102 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1103 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
f9496a7b 1104 }
0200214b 1105 }
d66e34cd 1106
c84142e8
UD
1107 /* Now let us see whether all libraries are available in the
1108 versions we need. */
1109 {
993b3242
UD
1110 struct version_check_args args;
1111 args.doexit = mode == normal;
145b8413 1112 args.dotrace = mode == trace;
993b3242 1113 _dl_receive_error (print_missing_version, version_check_doit, &args);
c84142e8
UD
1114 }
1115
bff334e0
UD
1116#ifdef USE_TLS
1117 /* Now it is time to determine the layout of the static TLS block
1118 and allocate it for the initial thread. Note that we always
1119 allocate the static block, we never defer it even if no
1120 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1121 use the static model. First add the dynamic linker to the list
1122 if it also uses TLS. */
1123 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1124 /* Assign a module ID. */
1125 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1126
1127# ifndef SHARED
1128 /* If dynamic loading of modules with TLS is impossible we do not
1129 have to initialize any of the TLS functionality unless any of the
1130 initial modules uses TLS. */
1131 if (GL(dl_tls_max_dtv_idx) > 0)
1132# endif
1133 {
1134 struct link_map *l;
1135 size_t nelem;
1136 struct dtv_slotinfo *slotinfo;
1137
1138 /* Number of elements in the static TLS block. */
1139 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
1140
1141 /* Allocate the array which contains the information about the
1142 dtv slots. We allocate a few entries more than needed to
1143 avoid the need for reallocation. */
1144 nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
1145
1146 /* Allocate. */
1147 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
1148 malloc (sizeof (struct dtv_slotinfo_list)
1149 + nelem * sizeof (struct dtv_slotinfo));
1150 /* No need to check the return value. If memory allocation failed
1151 the program would have been terminated. */
1152
1153 slotinfo = memset (GL(dl_tls_dtv_slotinfo_list)->slotinfo, '\0',
1154 nelem * sizeof (struct dtv_slotinfo));
1155 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
1156 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
1157
1158 /* Fill in the information from the loaded modules. */
1159 for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next)
1160 if (l->l_tls_blocksize != 0)
1161 /* This is a module with TLS data. Store the map reference.
1162 The generation counter is zero. */
1163 slotinfo[++i].map = l;
1164 assert (i == GL(dl_tls_max_dtv_idx));
1165
935f95dd 1166 /* Compute the TLS offsets for the various blocks. We call this
bff334e0
UD
1167 function even if none of the modules available at startup time
1168 uses TLS to initialize some variables. */
1169 _dl_determine_tlsoffset ();
1170
1171 /* Construct the static TLS block and the dtv for the initial
1172 thread. For some platforms this will include allocating memory
1173 for the thread descriptor. The memory for the TLS block will
1174 never be freed. It should be allocated accordingly. The dtv
1175 array can be changed if dynamic loading requires it. */
a816b435 1176 tcbp = _dl_allocate_tls_storage ();
bff334e0
UD
1177 if (tcbp == NULL)
1178 _dl_fatal_printf ("\
935f95dd 1179cannot allocate TLS data structures for initial thread");
08da0621
RM
1180
1181 /* Store for detection of the special case by __tls_get_addr
1182 so it knows not to pass this dtv to the normal realloc. */
8784cc18 1183 GL(dl_initial_dtv) = GET_DTV (tcbp);
bff334e0
UD
1184 }
1185#endif
1186
9a821cf9 1187 if (__builtin_expect (mode, normal) != normal)
0200214b
RM
1188 {
1189 /* We were run just to list the shared libraries. It is
1190 important that we do this before real relocation, because the
1191 functions we call below for output may no longer work properly
1192 after relocation. */
d6b5d570 1193 if (! GL(dl_loaded)->l_info[DT_NEEDED])
35fc382a 1194 _dl_printf ("\tstatically linked\n");
0200214b 1195 else
ceb2d9aa
UD
1196 {
1197 struct link_map *l;
1198
d6b5d570 1199 if (GL(dl_debug_mask) & DL_DEBUG_PRELINK)
32e6df36 1200 {
d6b5d570 1201 struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist;
32e6df36
UD
1202
1203 for (i = 0; i < scope->r_nlist; i++)
1204 {
1205 l = scope->r_list [i];
1206 if (l->l_faked)
1207 {
1208 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1209 continue;
1210 }
d6b5d570
UD
1211 if (_dl_name_match_p (GL(dl_trace_prelink), l))
1212 GL(dl_trace_prelink_map) = l;
bff334e0 1213 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
32e6df36 1214 l->l_libname->name[0] ? l->l_libname->name
e6caf4e1 1215 : rtld_progname ?: "<main program>",
32e6df36 1216 l->l_name[0] ? l->l_name
e6caf4e1 1217 : rtld_progname ?: "<main program>",
32e6df36
UD
1218 (int) sizeof l->l_map_start * 2,
1219 l->l_map_start,
1220 (int) sizeof l->l_addr * 2,
1221 l->l_addr);
bff334e0
UD
1222#ifdef USE_TLS
1223 if (l->l_tls_modid)
1224 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1225 (int) sizeof l->l_tls_offset * 2,
1226 l->l_tls_offset);
1227 else
1228#endif
1229 _dl_printf ("\n");
32e6df36
UD
1230 }
1231 }
1232 else
1233 {
d6b5d570 1234 for (l = GL(dl_loaded)->l_next; l; l = l->l_next)
32e6df36
UD
1235 if (l->l_faked)
1236 /* The library was not found. */
1237 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1238 else
1239 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1240 l->l_name, (int) sizeof l->l_map_start * 2,
1241 l->l_map_start);
1242 }
ceb2d9aa 1243 }
1a3a58fd 1244
9a821cf9 1245 if (__builtin_expect (mode, trace) != trace)
5a47e7f2 1246 for (i = 1; i < (unsigned int) _dl_argc; ++i)
cddcfecf
RM
1247 {
1248 const ElfW(Sym) *ref = NULL;
c0282c06
UD
1249 ElfW(Addr) loadbase;
1250 lookup_t result;
c0282c06 1251
e6caf4e1
UD
1252 result = INTUSE(_dl_lookup_symbol) (INTUSE(_dl_argv)[i],
1253 GL(dl_loaded),
cff26a3e
AJ
1254 &ref, GL(dl_loaded)->l_scope,
1255 ELF_RTYPE_CLASS_PLT, 1);
c0282c06
UD
1256
1257 loadbase = LOOKUP_VALUE_ADDRESS (result);
1258
35fc382a 1259 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
e6caf4e1 1260 INTUSE(_dl_argv)[i],
8a0746ae
RM
1261 (int) sizeof ref->st_value * 2, ref->st_value,
1262 (int) sizeof loadbase * 2, loadbase);
cddcfecf 1263 }
ce37fa88 1264 else
fd26970f 1265 {
667b0577 1266 /* If LD_WARN is set warn about undefined symbols. */
5688da55 1267 if (GL(dl_lazy) >= 0 && GL(dl_verbose))
ce37fa88
UD
1268 {
1269 /* We have to do symbol dependency testing. */
1270 struct relocate_args args;
1271 struct link_map *l;
993b3242 1272
5688da55 1273 args.lazy = GL(dl_lazy);
fd26970f 1274
d6b5d570 1275 l = GL(dl_loaded);
ce37fa88
UD
1276 while (l->l_next)
1277 l = l->l_next;
1278 do
1279 {
d6b5d570 1280 if (l != &GL(dl_rtld_map) && ! l->l_faked)
ce37fa88
UD
1281 {
1282 args.l = l;
1283 _dl_receive_error (print_unresolved, relocate_doit,
1284 &args);
ce37fa88
UD
1285 }
1286 l = l->l_prev;
1287 } while (l);
32e6df36 1288
d6b5d570
UD
1289 if ((GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1290 && GL(dl_rtld_map).l_opencount > 1)
cff26a3e
AJ
1291 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map),
1292 GL(dl_loaded)->l_scope, 0, 0);
ce37fa88
UD
1293 }
1294
b0982c4a 1295#define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
120b4c49 1296 if (version_info)
fd26970f 1297 {
ce37fa88
UD
1298 /* Print more information. This means here, print information
1299 about the versions needed. */
1300 int first = 1;
d6b5d570 1301 struct link_map *map = GL(dl_loaded);
ce37fa88 1302
d6b5d570 1303 for (map = GL(dl_loaded); map != NULL; map = map->l_next)
fd26970f 1304 {
f41c8091 1305 const char *strtab;
ce37fa88 1306 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
f41c8091
UD
1307 ElfW(Verneed) *ent;
1308
1309 if (dyn == NULL)
1310 continue;
1311
a42195db 1312 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
f41c8091 1313 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
ce37fa88 1314
f41c8091 1315 if (first)
ce37fa88 1316 {
35fc382a 1317 _dl_printf ("\n\tVersion information:\n");
f41c8091
UD
1318 first = 0;
1319 }
ce37fa88 1320
35fc382a 1321 _dl_printf ("\t%s:\n",
e6caf4e1 1322 map->l_name[0] ? map->l_name : rtld_progname);
f41c8091
UD
1323
1324 while (1)
1325 {
1326 ElfW(Vernaux) *aux;
1327 struct link_map *needed;
ce37fa88 1328
f41c8091
UD
1329 needed = find_needed (strtab + ent->vn_file);
1330 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
ce37fa88
UD
1331
1332 while (1)
1333 {
f41c8091
UD
1334 const char *fname = NULL;
1335
f41c8091 1336 if (needed != NULL
ba9fcb3f
UD
1337 && match_version (strtab + aux->vna_name,
1338 needed))
f41c8091
UD
1339 fname = needed->l_name;
1340
35fc382a
UD
1341 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1342 strtab + ent->vn_file,
1343 strtab + aux->vna_name,
1344 aux->vna_flags & VER_FLG_WEAK
1345 ? "[WEAK] " : "",
1346 fname ?: "not found");
ce37fa88 1347
f41c8091
UD
1348 if (aux->vna_next == 0)
1349 /* No more symbols. */
ce37fa88
UD
1350 break;
1351
f41c8091
UD
1352 /* Next symbol. */
1353 aux = (ElfW(Vernaux) *) ((char *) aux
1354 + aux->vna_next);
ce37fa88 1355 }
f41c8091
UD
1356
1357 if (ent->vn_next == 0)
1358 /* No more dependencies. */
1359 break;
1360
1361 /* Next dependency. */
1362 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
ce37fa88 1363 }
fd26970f 1364 }
ce37fa88 1365 }
fd26970f 1366 }
d66e34cd 1367
0200214b
RM
1368 _exit (0);
1369 }
86d2c878 1370
d6b5d570
UD
1371 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]
1372 && ! __builtin_expect (GL(dl_profile) != NULL, 0))
32e6df36
UD
1373 {
1374 ElfW(Lib) *liblist, *liblistend;
1375 struct link_map **r_list, **r_listend, *l;
9710f75d
UD
1376 const char *strtab = (const void *) D_PTR (GL(dl_loaded),
1377 l_info[DT_STRTAB]);
32e6df36 1378
d6b5d570 1379 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
32e6df36 1380 liblist = (ElfW(Lib) *)
d6b5d570 1381 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
32e6df36
UD
1382 liblistend = (ElfW(Lib) *)
1383 ((char *) liblist
d6b5d570
UD
1384 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1385 r_list = GL(dl_loaded)->l_searchlist.r_list;
1386 r_listend = r_list + GL(dl_loaded)->l_searchlist.r_nlist;
32e6df36
UD
1387
1388 for (; r_list < r_listend && liblist < liblistend; r_list++)
1389 {
1390 l = *r_list;
1391
d6b5d570 1392 if (l == GL(dl_loaded))
32e6df36
UD
1393 continue;
1394
1395 /* If the library is not mapped where it should, fail. */
1396 if (l->l_addr)
1397 break;
1398
1399 /* Next, check if checksum matches. */
1400 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1401 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1402 != liblist->l_checksum)
1403 break;
1404
1405 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1406 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1407 != liblist->l_time_stamp)
1408 break;
1409
1410 if (! _dl_name_match_p (strtab + liblist->l_name, l))
1411 break;
1412
1413 ++liblist;
1414 }
1415
1416
1417 if (r_list == r_listend && liblist == liblistend)
164a7164 1418 prelinked = true;
32e6df36 1419
d6b5d570 1420 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0))
32e6df36
UD
1421 _dl_printf ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
1422 }
1423
1424 if (prelinked)
1425 {
d89ae1d5
RM
1426 struct link_map *l;
1427
d6b5d570 1428 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
32e6df36
UD
1429 {
1430 ElfW(Rela) *conflict, *conflictend;
1431#ifndef HP_TIMING_NONAVAIL
1432 hp_timing_t start;
1433 hp_timing_t stop;
1434#endif
1435
1436 HP_TIMING_NOW (start);
d6b5d570 1437 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
32e6df36 1438 conflict = (ElfW(Rela) *)
d89ae1d5 1439 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
32e6df36 1440 conflictend = (ElfW(Rela) *)
d89ae1d5
RM
1441 ((char *) conflict
1442 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
d6b5d570 1443 _dl_resolve_conflicts (GL(dl_loaded), conflict, conflictend);
32e6df36
UD
1444 HP_TIMING_NOW (stop);
1445 HP_TIMING_DIFF (relocate_time, start, stop);
1446 }
1447
d89ae1d5
RM
1448
1449 /* Mark all the objects so we know they have been already relocated. */
1450 for (l = GL(dl_loaded); l != NULL; l = l->l_next)
1451 l->l_relocated = 1;
1452
32e6df36
UD
1453 _dl_sysdep_start_cleanup ();
1454 }
1455 else
164a7164
UD
1456 {
1457 /* Now we have all the objects loaded. Relocate them all except for
1458 the dynamic linker itself. We do this in reverse order so that copy
1459 relocs of earlier objects overwrite the data written by later
1460 objects. We do not re-relocate the dynamic linker itself in this
1461 loop because that could result in the GOT entries for functions we
1462 call being changed, and that would break us. It is safe to relocate
1463 the dynamic linker out of order because it has no copy relocs (we
1464 know that because it is self-contained). */
1465
1466 struct link_map *l;
1467 int consider_profiling = GL(dl_profile) != NULL;
8b07d6a8 1468#ifndef HP_TIMING_NONAVAIL
164a7164
UD
1469 hp_timing_t start;
1470 hp_timing_t stop;
1471 hp_timing_t add;
8b07d6a8 1472#endif
c0fb8a56 1473
164a7164
UD
1474 /* If we are profiling we also must do lazy reloaction. */
1475 GL(dl_lazy) |= consider_profiling;
c0fb8a56 1476
164a7164
UD
1477 l = GL(dl_loaded);
1478 while (l->l_next)
1479 l = l->l_next;
db276fa1 1480
164a7164
UD
1481 HP_TIMING_NOW (start);
1482 do
1483 {
1484 /* While we are at it, help the memory handling a bit. We have to
1485 mark some data structures as allocated with the fake malloc()
1486 implementation in ld.so. */
1487 struct libname_list *lnp = l->l_libname->next;
752a2a50 1488
164a7164
UD
1489 while (__builtin_expect (lnp != NULL, 0))
1490 {
1491 lnp->dont_free = 1;
1492 lnp = lnp->next;
1493 }
752a2a50 1494
164a7164 1495 if (l != &GL(dl_rtld_map))
cff26a3e
AJ
1496 INTUSE(_dl_relocate_object) (l, l->l_scope, GL(dl_lazy),
1497 consider_profiling);
be935610 1498
164a7164
UD
1499 l = l->l_prev;
1500 }
1501 while (l);
1502 HP_TIMING_NOW (stop);
1503
1504 HP_TIMING_DIFF (relocate_time, start, stop);
1505
1506 /* Do any necessary cleanups for the startup OS interface code.
1507 We do these now so that no calls are made after rtld re-relocation
1508 which might be resolved to different functions than we expect.
1509 We cannot do this before relocating the other objects because
1510 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1511 _dl_sysdep_start_cleanup ();
1512
1513 /* Now enable profiling if needed. Like the previous call,
1514 this has to go here because the calls it makes should use the
1515 rtld versions of the functions (particularly calloc()), but it
1516 needs to have _dl_profile_map set up by the relocator. */
1517 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
1518 /* We must prepare the profiling. */
cff26a3e 1519 INTUSE(_dl_start_profile) (GL(dl_profile_map), GL(dl_profile_output));
164a7164
UD
1520
1521 if (GL(dl_rtld_map).l_opencount > 1)
1522 {
1523 /* There was an explicit ref to the dynamic linker as a shared lib.
1524 Re-relocate ourselves with user-controlled symbol definitions. */
1525 HP_TIMING_NOW (start);
cff26a3e
AJ
1526 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map), GL(dl_loaded)->l_scope,
1527 0, 0);
164a7164
UD
1528 HP_TIMING_NOW (stop);
1529 HP_TIMING_DIFF (add, start, stop);
1530 HP_TIMING_ACCUM_NT (relocate_time, add);
1531 }
1532 }
ac16e905 1533
be935610 1534 /* Now set up the variable which helps the assembler startup code. */
d6b5d570
UD
1535 GL(dl_main_searchlist) = &GL(dl_loaded)->l_searchlist;
1536 GL(dl_global_scope)[0] = &GL(dl_loaded)->l_searchlist;
be935610 1537
32e6df36 1538 /* Save the information about the original global scope list since
604510f7 1539 we need it in the memory handling later. */
d6b5d570 1540 GL(dl_initial_searchlist) = *GL(dl_main_searchlist);
604510f7 1541
a816b435
RM
1542#ifdef USE_TLS
1543# ifndef SHARED
1544 if (GL(dl_tls_max_dtv_idx) > 0)
1545# endif
1546 {
1547 /* Now that we have completed relocation, the initializer data
1548 for the TLS blocks has its final values and we can copy them
1549 into the main thread's TLS area, which we allocated above. */
1550 _dl_allocate_tls_init (tcbp);
1551
1552 /* And finally install it for the main thread. */
9d69bd24
UD
1553# ifndef HAVE___THREAD
1554 TLS_INIT_TP (tcbp, GL(dl_rtld_map).l_tls_tp_initialized);
1555# else
1556 /* If the compiler supports the __thread keyword we know that
1557 at least ld.so itself uses TLS and therefore the thread
1558 pointer was initialized earlier. */
1559 assert (GL(dl_rtld_map).l_tls_tp_initialized != 0);
1560 TLS_INIT_TP (tcbp, 1);
1561# endif
a816b435
RM
1562 }
1563#endif
1564
4d6acc61
RM
1565 {
1566 /* Initialize _r_debug. */
d6b5d570 1567 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr);
ceb2d9aa 1568 struct link_map *l;
4d6acc61 1569
d6b5d570 1570 l = GL(dl_loaded);
ec42724d
RM
1571
1572#ifdef ELF_MACHINE_DEBUG_SETUP
1573
1574 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1575
1576 ELF_MACHINE_DEBUG_SETUP (l, r);
d6b5d570 1577 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
ec42724d
RM
1578
1579#else
1580
3fb55878 1581 if (l->l_info[DT_DEBUG] != NULL)
4d6acc61
RM
1582 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1583 with the run-time address of the r_debug structure */
1584 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1585
d746b89c
RM
1586 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1587 case you run gdb on the dynamic linker directly. */
3fb55878 1588 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
d6b5d570 1589 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
d746b89c 1590
ec42724d
RM
1591#endif
1592
4d6acc61
RM
1593 /* Notify the debugger that all objects are now mapped in. */
1594 r->r_state = RT_ADD;
cff26a3e 1595 INTUSE(_dl_debug_state) ();
4d6acc61 1596 }
0200214b 1597
08cac4ac
UD
1598#ifndef MAP_COPY
1599 /* We must munmap() the cache file. */
cff26a3e 1600 INTUSE(_dl_unload_cache) ();
08cac4ac
UD
1601#endif
1602
d66e34cd
RM
1603 /* Once we return, _dl_sysdep_start will invoke
1604 the DT_INIT functions and then *USER_ENTRY. */
1605}
fd26970f
UD
1606\f
1607/* This is a little helper function for resolving symbols while
1608 tracing the binary. */
1609static void
c84142e8
UD
1610print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1611 const char *errstring)
fd26970f 1612{
3996f34b 1613 if (objname[0] == '\0')
e6caf4e1 1614 objname = rtld_progname ?: "<main program>";
35fc382a 1615 _dl_error_printf ("%s (%s)\n", errstring, objname);
fd26970f 1616}
c84142e8
UD
1617\f
1618/* This is a little helper function for resolving symbols while
1619 tracing the binary. */
1620static void
1621print_missing_version (int errcode __attribute__ ((unused)),
1622 const char *objname, const char *errstring)
1623{
e6caf4e1 1624 _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
35fc382a 1625 objname, errstring);
c84142e8 1626}
ea278354 1627\f
7dea968e
UD
1628/* Nonzero if any of the debugging options is enabled. */
1629static int any_debug;
1630
b5efde2f
UD
1631/* Process the string given as the parameter which explains which debugging
1632 options are enabled. */
1633static void
14c44e2e 1634process_dl_debug (const char *dl_debug)
b5efde2f 1635{
3e2040c8
UD
1636 /* When adding new entries make sure that the maximal length of a name
1637 is correctly handled in the LD_DEBUG_HELP code below. */
1638 static const struct
1639 {
379d4ec4
UD
1640 unsigned char len;
1641 const char name[10];
3e2040c8
UD
1642 const char helptext[41];
1643 unsigned short int mask;
1644 } debopts[] =
1645 {
379d4ec4
UD
1646#define LEN_AND_STR(str) sizeof (str) - 1, str
1647 { LEN_AND_STR ("libs"), "display library search paths",
3e2040c8 1648 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
379d4ec4 1649 { LEN_AND_STR ("reloc"), "display relocation processing",
3e2040c8 1650 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
379d4ec4 1651 { LEN_AND_STR ("files"), "display progress for input file",
3e2040c8 1652 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
379d4ec4 1653 { LEN_AND_STR ("symbols"), "display symbol table processing",
3e2040c8 1654 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
379d4ec4 1655 { LEN_AND_STR ("bindings"), "display information about symbol binding",
3e2040c8 1656 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
379d4ec4 1657 { LEN_AND_STR ("versions"), "display version dependencies",
3e2040c8 1658 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
379d4ec4 1659 { LEN_AND_STR ("all"), "all previous options combined",
3e2040c8
UD
1660 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
1661 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
379d4ec4 1662 { LEN_AND_STR ("statistics"), "display relocation statistics",
3e2040c8 1663 DL_DEBUG_STATISTICS },
379d4ec4 1664 { LEN_AND_STR ("help"), "display this help message and exit",
3e2040c8
UD
1665 DL_DEBUG_HELP },
1666 };
1667#define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
3e2040c8 1668
379d4ec4
UD
1669 /* Skip separating white spaces and commas. */
1670 while (*dl_debug != '\0')
b5efde2f 1671 {
379d4ec4 1672 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
b5efde2f 1673 {
3e2040c8 1674 size_t cnt;
379d4ec4 1675 size_t len = 1;
77aba05b 1676
379d4ec4
UD
1677 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
1678 && dl_debug[len] != ',' && dl_debug[len] != ':')
1679 ++len;
14c44e2e 1680
3e2040c8 1681 for (cnt = 0; cnt < ndebopts; ++cnt)
379d4ec4
UD
1682 if (debopts[cnt].len == len
1683 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
3e2040c8 1684 {
d6b5d570 1685 GL(dl_debug_mask) |= debopts[cnt].mask;
5688da55 1686 any_debug = 1;
3e2040c8
UD
1687 break;
1688 }
77aba05b 1689
3e2040c8
UD
1690 if (cnt == ndebopts)
1691 {
1692 /* Display a warning and skip everything until next
1693 separator. */
1694 char *copy = strndupa (dl_debug, len);
1695 _dl_error_printf ("\
1696warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
379d4ec4
UD
1697 }
1698
1699 dl_debug += len;
1700 continue;
3e2040c8 1701 }
379d4ec4
UD
1702
1703 ++dl_debug;
3e2040c8 1704 }
77aba05b 1705
d6b5d570 1706 if (GL(dl_debug_mask) & DL_DEBUG_HELP)
3e2040c8
UD
1707 {
1708 size_t cnt;
14c44e2e 1709
3e2040c8
UD
1710 _dl_printf ("\
1711Valid options for the LD_DEBUG environment variable are:\n\n");
db276fa1 1712
3e2040c8 1713 for (cnt = 0; cnt < ndebopts; ++cnt)
37d8b778
UD
1714 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
1715 " " + debopts[cnt].len - 3,
3e2040c8 1716 debopts[cnt].helptext);
14c44e2e 1717
3e2040c8
UD
1718 _dl_printf ("\n\
1719To direct the debugging output into a file instead of standard output\n\
1720a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1721 _exit (0);
b5efde2f 1722 }
b5efde2f
UD
1723}
1724\f
ea278354
UD
1725/* Process all environments variables the dynamic linker must recognize.
1726 Since all of them start with `LD_' we are a bit smarter while finding
1727 all the entries. */
9360906d 1728extern char **_environ attribute_hidden;
67c94753 1729
d6b5d570 1730
ea278354 1731static void
ba9fcb3f 1732process_envvars (enum mode *modep)
ea278354 1733{
67c94753 1734 char **runp = _environ;
ea278354
UD
1735 char *envline;
1736 enum mode mode = normal;
7dea968e 1737 char *debug_output = NULL;
ea278354
UD
1738
1739 /* This is the default place for profiling data file. */
e6caf4e1
UD
1740 GL(dl_profile_output)
1741 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
ea278354
UD
1742
1743 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1744 {
379d4ec4
UD
1745 size_t len = 0;
1746
1747 while (envline[len] != '\0' && envline[len] != '=')
1748 ++len;
ea278354 1749
75e8d1f5
UD
1750 if (envline[len] != '=')
1751 /* This is a "LD_" variable at the end of the string without
1752 a '=' character. Ignore it since otherwise we will access
1753 invalid memory below. */
67c94753 1754 continue;
75e8d1f5 1755
67c94753 1756 switch (len)
ea278354 1757 {
14c44e2e
UD
1758 case 4:
1759 /* Warning level, verbose or not. */
67c94753 1760 if (memcmp (envline, "WARN", 4) == 0)
d6b5d570 1761 GL(dl_verbose) = envline[5] != '\0';
14c44e2e 1762 break;
ea278354 1763
14c44e2e
UD
1764 case 5:
1765 /* Debugging of the dynamic linker? */
67c94753
UD
1766 if (memcmp (envline, "DEBUG", 5) == 0)
1767 process_dl_debug (&envline[6]);
14c44e2e 1768 break;
b5efde2f 1769
14c44e2e
UD
1770 case 7:
1771 /* Print information about versions. */
67c94753 1772 if (memcmp (envline, "VERBOSE", 7) == 0)
14c44e2e 1773 {
67c94753 1774 version_info = envline[8] != '\0';
14c44e2e
UD
1775 break;
1776 }
7dea968e 1777
14c44e2e 1778 /* List of objects to be preloaded. */
67c94753 1779 if (memcmp (envline, "PRELOAD", 7) == 0)
14c44e2e 1780 {
67c94753 1781 preloadlist = &envline[8];
14c44e2e
UD
1782 break;
1783 }
120b4c49 1784
14c44e2e 1785 /* Which shared object shall be profiled. */
c95f3fd4 1786 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
d6b5d570 1787 GL(dl_profile) = &envline[8];
14c44e2e 1788 break;
120b4c49 1789
14c44e2e
UD
1790 case 8:
1791 /* Do we bind early? */
67c94753 1792 if (memcmp (envline, "BIND_NOW", 8) == 0)
f53c03c2 1793 {
5688da55 1794 GL(dl_lazy) = envline[9] == '\0';
f53c03c2
UD
1795 break;
1796 }
67c94753 1797 if (memcmp (envline, "BIND_NOT", 8) == 0)
d6b5d570 1798 GL(dl_bind_not) = envline[9] != '\0';
14c44e2e 1799 break;
ea278354 1800
14c44e2e
UD
1801 case 9:
1802 /* Test whether we want to see the content of the auxiliary
1803 array passed up from the kernel. */
67c94753 1804 if (memcmp (envline, "SHOW_AUXV", 9) == 0)
14c44e2e
UD
1805 _dl_show_auxv ();
1806 break;
ea278354 1807
12264bd7 1808 case 10:
3081378b 1809 /* Mask for the important hardware capabilities. */
67c94753 1810 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
d6b5d570 1811 GL(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, 0, 0);
12264bd7
UD
1812 break;
1813
f787edde
UD
1814 case 11:
1815 /* Path where the binary is found. */
e6caf4e1 1816 if (!INTUSE(__libc_enable_secure)
67c94753 1817 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
d6b5d570 1818 GL(dl_origin_path) = &envline[12];
f787edde
UD
1819 break;
1820
14c44e2e 1821 case 12:
dec126b4 1822 /* The library search path. */
67c94753 1823 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
dec126b4 1824 {
67c94753 1825 library_path = &envline[13];
dec126b4
UD
1826 break;
1827 }
1828
14c44e2e 1829 /* Where to place the profiling data file. */
67c94753 1830 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
14c44e2e 1831 {
67c94753 1832 debug_output = &envline[13];
14c44e2e
UD
1833 break;
1834 }
ea278354 1835
67c94753 1836 if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
5688da55 1837 GL(dl_dynamic_weak) = 1;
14c44e2e 1838 break;
ea278354 1839
14c44e2e
UD
1840 case 14:
1841 /* Where to place the profiling data file. */
e6caf4e1 1842 if (!INTUSE(__libc_enable_secure)
3e2040c8
UD
1843 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
1844 && envline[15] != '\0')
d6b5d570 1845 GL(dl_profile_output) = &envline[15];
14c44e2e 1846 break;
120b4c49 1847
32e6df36
UD
1848 case 16:
1849 /* The mode of the dynamic linker can be set. */
1850 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
1851 {
1852 mode = trace;
d6b5d570
UD
1853 GL(dl_verbose) = 1;
1854 GL(dl_debug_mask) |= DL_DEBUG_PRELINK;
1855 GL(dl_trace_prelink) = &envline[17];
32e6df36
UD
1856 }
1857 break;
1858
14c44e2e
UD
1859 case 20:
1860 /* The mode of the dynamic linker can be set. */
67c94753 1861 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
14c44e2e
UD
1862 mode = trace;
1863 break;
e2102c14
UD
1864
1865 /* We might have some extra environment variable to handle. This
1866 is tricky due to the pre-processing of the length of the name
1867 in the switch statement here. The code here assumes that added
1868 environment variables have a different length. */
1869#ifdef EXTRA_LD_ENVVARS
1870 EXTRA_LD_ENVVARS
1871#endif
ea278354
UD
1872 }
1873 }
1874
3e2040c8
UD
1875 /* The caller wants this information. */
1876 *modep = mode;
1877
4bae5567
UD
1878 /* Extra security for SUID binaries. Remove all dangerous environment
1879 variables. */
e6caf4e1 1880 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
4bae5567 1881 {
c95f3fd4 1882 static const char unsecure_envvars[] =
4bae5567
UD
1883#ifdef EXTRA_UNSECURE_ENVVARS
1884 EXTRA_UNSECURE_ENVVARS
1885#endif
c95f3fd4
UD
1886 UNSECURE_ENVVARS;
1887 const char *nextp;
1888
1889 nextp = unsecure_envvars;
1890 do
1891 {
1892 unsetenv (nextp);
9710f75d
UD
1893 /* We could use rawmemchr but this need not be fast. */
1894 nextp = (char *) (strchr) (nextp, '\0') + 1;
c95f3fd4
UD
1895 }
1896 while (*nextp != '\0');
74955460
UD
1897
1898 if (__access ("/etc/suid-debug", F_OK) != 0)
1899 unsetenv ("MALLOC_CHECK_");
4bae5567 1900 }
7dea968e
UD
1901 /* If we have to run the dynamic linker in debugging mode and the
1902 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1903 messages to this file. */
3e2040c8 1904 else if (any_debug && debug_output != NULL)
7dea968e 1905 {
5f2de337
UD
1906#ifdef O_NOFOLLOW
1907 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
1908#else
1909 const int flags = O_WRONLY | O_APPEND | O_CREAT;
1910#endif
7a2fd787
UD
1911 size_t name_len = strlen (debug_output);
1912 char buf[name_len + 12];
1913 char *startp;
1914
1915 buf[name_len + 11] = '\0';
9710f75d 1916 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
7a2fd787
UD
1917 *--startp = '.';
1918 startp = memcpy (startp - name_len, debug_output, name_len);
1919
5688da55
UD
1920 GL(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
1921 if (GL(dl_debug_fd) == -1)
7dea968e 1922 /* We use standard output if opening the file failed. */
5688da55 1923 GL(dl_debug_fd) = STDOUT_FILENO;
7dea968e 1924 }
ea278354 1925}
db276fa1
UD
1926
1927
1928/* Print the various times we collected. */
1929static void
1930print_statistics (void)
1931{
8b07d6a8 1932#ifndef HP_TIMING_NONAVAIL
f457369d 1933 char buf[200];
db276fa1
UD
1934 char *cp;
1935 char *wp;
1936
1937 /* Total time rtld used. */
1938 if (HP_TIMING_AVAIL)
1939 {
1940 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
cff26a3e
AJ
1941 INTUSE(_dl_debug_printf) ("\nruntime linker statistics:\n"
1942 " total startup time in dynamic loader: %s\n",
1943 buf);
db276fa1
UD
1944 }
1945
1946 /* Print relocation statistics. */
1947 if (HP_TIMING_AVAIL)
1948 {
35fc382a 1949 char pbuf[30];
db276fa1 1950 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
9710f75d
UD
1951 cp = _itoa ((1000ULL * relocate_time) / rtld_total_time,
1952 pbuf + sizeof (pbuf), 10, 0);
35fc382a
UD
1953 wp = pbuf;
1954 switch (pbuf + sizeof (pbuf) - cp)
db276fa1
UD
1955 {
1956 case 3:
1957 *wp++ = *cp++;
1958 case 2:
1959 *wp++ = *cp++;
1960 case 1:
1961 *wp++ = '.';
1962 *wp++ = *cp++;
1963 }
1964 *wp = '\0';
cff26a3e 1965 INTUSE(_dl_debug_printf) ("\
7969407a 1966 time needed for relocation: %s (%s%%)\n",
cff26a3e 1967 buf, pbuf);
db276fa1 1968 }
1531e094 1969#endif
cff26a3e
AJ
1970 INTUSE(_dl_debug_printf) (" number of relocations: %lu\n",
1971 GL(dl_num_relocations));
1972 INTUSE(_dl_debug_printf) (" number of relocations from cache: %lu\n",
1973 GL(dl_num_cache_relocations));
db276fa1 1974
1531e094 1975#ifndef HP_TIMING_NONAVAIL
db276fa1
UD
1976 /* Time spend while loading the object and the dependencies. */
1977 if (HP_TIMING_AVAIL)
1978 {
35fc382a 1979 char pbuf[30];
db276fa1 1980 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
9710f75d
UD
1981 cp = _itoa ((1000ULL * load_time) / rtld_total_time,
1982 pbuf + sizeof (pbuf), 10, 0);
35fc382a
UD
1983 wp = pbuf;
1984 switch (pbuf + sizeof (pbuf) - cp)
db276fa1
UD
1985 {
1986 case 3:
1987 *wp++ = *cp++;
1988 case 2:
1989 *wp++ = *cp++;
1990 case 1:
1991 *wp++ = '.';
1992 *wp++ = *cp++;
1993 }
1994 *wp = '\0';
cff26a3e 1995 INTUSE(_dl_debug_printf) ("\
7969407a 1996 time needed to load objects: %s (%s%%)\n",
cff26a3e 1997 buf, pbuf);
db276fa1 1998 }
1531e094 1999#endif
db276fa1 2000}