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