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