]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/rtld.c
Update.
[thirdparty/glibc.git] / elf / rtld.c
CommitLineData
d66e34cd 1/* Run time dynamic linker.
1ca7cbcb 2 Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37
UD
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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
13 Library General Public License for more details.
d66e34cd 14
afd4eb37
UD
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
d66e34cd 19
7dea968e 20#include <fcntl.h>
d66e34cd 21#include <stdlib.h>
f51d1dfd 22#include <string.h>
d66e34cd 23#include <unistd.h>
2064087b 24#include <sys/mman.h> /* Check if MAP_ANON is defined. */
a42195db 25#include <ldsodefs.h>
ce37fa88 26#include <stdio-common/_itoa.h>
f21acc89 27#include <entry.h>
c94a8080 28#include <fpu_control.h>
db276fa1 29#include <hp-timing.h>
cf197e41 30#include <bits/libc-lock.h>
f5348425 31#include "dynamic-link.h"
e2102c14 32#include "dl-librecon.h"
f5348425 33
a853022c 34#include <assert.h>
f5348425 35
d66e34cd
RM
36/* System-specific function to do initial startup for the dynamic linker.
37 After this, file access calls and getenv must work. This is responsible
cddcfecf 38 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
d66e34cd 39 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
266180eb
RM
40extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
41 void (*dl_main) (const ElfW(Phdr) *phdr,
42 ElfW(Half) phent,
43 ElfW(Addr) *user_entry));
4cb20290 44extern void _dl_sysdep_start_cleanup (void);
d66e34cd 45
08cac4ac
UD
46/* This function is used to unload the cache file if necessary. */
47extern void _dl_unload_cache (void);
48
14bab8de
UD
49/* System-dependent function to read a file's whole contents
50 in the most convenient manner available. */
51extern void *_dl_sysdep_read_whole_file (const char *filename,
52 size_t *filesize_ptr,
53 int mmap_prot);
54
fd26970f 55/* Helper function to handle errors while resolving symbols. */
c84142e8
UD
56static void print_unresolved (int errcode, const char *objname,
57 const char *errsting);
58
59/* Helper function to handle errors when a version is missing. */
60static void print_missing_version (int errcode, const char *objname,
61 const char *errsting);
fd26970f 62
db276fa1
UD
63/* Print the various times we collected. */
64static void print_statistics (void);
ea278354
UD
65
66/* This is a list of all the modes the dynamic loader can be in. */
67enum mode { normal, list, verify, trace };
68
69/* Process all environments variables the dynamic linker must recognize.
70 Since all of them start with `LD_' we are a bit smarter while finding
71 all the entries. */
72static void process_envvars (enum mode *modep, int *lazyp);
73
d66e34cd
RM
74int _dl_argc;
75char **_dl_argv;
ceb27555 76unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
cf29ffbe 77int _dl_verbose;
0a54e401
UD
78const char *_dl_platform;
79size_t _dl_platformlen;
f41c8091 80unsigned long _dl_hwcap;
c94a8080 81fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
0a54e401 82struct r_search_path *_dl_search_paths;
3996f34b
UD
83const char *_dl_profile;
84const char *_dl_profile_output;
a3d6fb9b 85struct link_map *_dl_profile_map;
12b5b6b7 86int _dl_lazy;
b5efde2f 87int _dl_debug_libs;
7dea968e 88int _dl_debug_impcalls;
0c367d92 89int _dl_debug_bindings;
de100ca7 90int _dl_debug_symbols;
8193034b
UD
91int _dl_debug_versions;
92int _dl_debug_reloc;
93int _dl_debug_files;
db276fa1 94int _dl_debug_statistics;
b0a01055 95const char *_dl_inhibit_rpath; /* RPATH values which should be
310930c1 96 ignored. */
f787edde 97const char *_dl_origin_path;
d66e34cd 98
be935610
UD
99/* This is a pointer to the map for the main object and through it to
100 all loaded objects. */
101struct link_map *_dl_loaded;
102/* Pointer to the l_searchlist element of the link map of the main object. */
103struct r_scope_elem *_dl_main_searchlist;
604510f7
UD
104/* Copy of the content of `_dl_main_searchlist'. */
105struct r_scope_elem _dl_initial_searchlist;
be935610
UD
106/* Array which is used when looking up in the global scope. */
107struct r_scope_elem *_dl_global_scope[2];
108
cf197e41
UD
109/* During the program run we must not modify the global data of
110 loaded shared object simultanously in two threads. Therefore we
111 protect `_dl_open' and `_dl_close' in dl-close.c.
112
113 This must be a recursive lock since the initializer function of
114 the loaded object might as well require a call to this function.
115 At this time it is not anymore a problem to modify the tables. */
116__libc_lock_define_initialized_recursive (, _dl_load_lock)
117
39778c6c
UD
118/* Set nonzero during loading and initialization of executable and
119 libraries, cleared before the executable's entry point runs. This
120 must not be initialized to nonzero, because the unused dynamic
121 linker loaded in for libc.so's "ld.so.1" dep will provide the
122 definition seen by libc.so's initializer; that value must be zero,
123 and will be since that dynamic linker's _dl_start and dl_main will
124 never be called. */
125int _dl_starting_up;
126
c0fb8a56 127
266180eb
RM
128static void dl_main (const ElfW(Phdr) *phdr,
129 ElfW(Half) phent,
130 ElfW(Addr) *user_entry);
d66e34cd 131
ee188d55 132struct link_map _dl_rtld_map;
c84142e8 133struct libname_list _dl_rtld_libname;
f41c8091 134struct libname_list _dl_rtld_libname2;
86d2c878 135
db276fa1
UD
136/* Variable for statistics. */
137static hp_timing_t rtld_total_time;
138static hp_timing_t relocate_time;
139static hp_timing_t load_time;
140extern unsigned long int _dl_num_relocations; /* in dl-lookup.c */
141
6a1db4ff
UD
142static ElfW(Addr) _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
143 hp_timing_t start_time);
144
b1dbbaa4
RM
145#ifdef RTLD_START
146RTLD_START
147#else
148#error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
149#endif
150
ceb2d9aa 151static ElfW(Addr)
d66e34cd
RM
152_dl_start (void *arg)
153{
86d2c878 154 struct link_map bootstrap_map;
db276fa1 155 hp_timing_t start_time;
264ec183 156 size_t cnt;
d66e34cd 157
b1dbbaa4
RM
158 /* This #define produces dynamic linking inline functions for
159 bootstrap relocation instead of general-purpose relocation. */
160#define RTLD_BOOTSTRAP
50463d27
UD
161#define RESOLVE(sym, version, flags) \
162 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
b1dbbaa4
RM
163#include "dynamic-link.h"
164
db276fa1
UD
165 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
166 HP_TIMING_NOW (start_time);
167
264ec183
UD
168 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
169 since it might nor be built in or inlined and we cannot make function
170 calls at this point. */
171 for (cnt = 0;
172 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
173 ++cnt)
174 bootstrap_map.l_info[cnt] = 0;
175
d66e34cd 176 /* Figure out the run-time load address of the dynamic linker itself. */
86d2c878 177 bootstrap_map.l_addr = elf_machine_load_address ();
d66e34cd 178
47707456
UD
179 /* Read our own dynamic section and fill in the info array. */
180 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
d3ac2d47 181 elf_get_dynamic_info (&bootstrap_map);
d66e34cd
RM
182
183#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
86d2c878 184 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
d66e34cd
RM
185#endif
186
187 /* Relocate ourselves so we can do normal function calls and
188 data access using the global offset table. */
421f82e5 189
3996f34b 190 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
ea7eb7e3
UD
191 /* Please note that we don't allow profiling of this object and
192 therefore need not test whether we have to allocate the array
193 for the relocation results (as done in dl-reloc.c). */
421f82e5 194
d66e34cd
RM
195 /* Now life is sane; we can call functions and access global data.
196 Set up to use the operating system facilities, and find out from
197 the operating system's program loader where to find the program
6a1db4ff
UD
198 header table in core. Put the rest of _dl_start into a separate
199 function, that way the compiler cannot put accesses to the GOT
200 before ELF_DYNAMIC_RELOCATE. */
201 return _dl_start_final (arg, &bootstrap_map, start_time);
202}
203
204
205static ElfW(Addr)
206_dl_start_final (void *arg, struct link_map *bootstrap_map_p,
207 hp_timing_t start_time)
208{
209 /* The use of `alloca' here looks ridiculous but it helps. The goal
210 is to avoid the function from being inlined. There is no official
211 way to do this so we use this trick. gcc never inlines functions
212 which use `alloca'. */
213 ElfW(Addr) *start_addr = alloca (sizeof (ElfW(Addr)));
d66e34cd 214
db276fa1
UD
215 if (HP_TIMING_AVAIL)
216 {
217 /* If it hasn't happen yet record the startup time. */
218 if (! HP_TIMING_INLINE)
219 HP_TIMING_NOW (start_time);
220
221 /* Initialize the timing functions. */
222 HP_TIMING_DIFF_INIT ();
223 }
224
86d2c878 225 /* Transfer data about ourselves to the permanent link_map structure. */
6a1db4ff
UD
226 _dl_rtld_map.l_addr = bootstrap_map_p->l_addr;
227 _dl_rtld_map.l_ld = bootstrap_map_p->l_ld;
f41c8091 228 _dl_rtld_map.l_opencount = 1;
6a1db4ff 229 memcpy (_dl_rtld_map.l_info, bootstrap_map_p->l_info,
ee188d55
RM
230 sizeof _dl_rtld_map.l_info);
231 _dl_setup_hash (&_dl_rtld_map);
86d2c878 232
052b6a6c
UD
233/* Don't bother trying to work out how ld.so is mapped in memory. */
234 _dl_rtld_map.l_map_start = ~0;
235 _dl_rtld_map.l_map_end = ~0;
236
d66e34cd
RM
237 /* Call the OS-dependent function to set up life so we can do things like
238 file access. It will call `dl_main' (below) to do all the real work
239 of the dynamic linker, and then unwind our frame and run the user
240 entry point on the same stack we entered on. */
6a1db4ff 241 *start_addr = _dl_sysdep_start (arg, &dl_main);
db276fa1
UD
242
243 if (HP_TIMING_AVAIL)
244 {
245 hp_timing_t end_time;
246
247 /* Get the current time. */
248 HP_TIMING_NOW (end_time);
249
250 /* Compute the difference. */
251 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
252 }
253
254 if (_dl_debug_statistics)
255 print_statistics ();
256
6a1db4ff 257 return *start_addr;
d66e34cd
RM
258}
259
d66e34cd
RM
260/* Now life is peachy; we can do all normal operations.
261 On to the real work. */
262
f21acc89 263void ENTRY_POINT (void);
d66e34cd 264
993b3242
UD
265/* Some helper functions. */
266
267/* Arguments to relocate_doit. */
268struct relocate_args
269{
270 struct link_map *l;
271 int lazy;
272};
273
274struct map_args
275{
276 /* Argument to map_doit. */
277 char *str;
278 /* Return value of map_doit. */
279 struct link_map *main_map;
280};
281
282/* Arguments to version_check_doit. */
283struct version_check_args
284{
993b3242
UD
285 int doexit;
286};
287
288static void
289relocate_doit (void *a)
290{
291 struct relocate_args *args = (struct relocate_args *) a;
292
be935610 293 _dl_relocate_object (args->l, args->l->l_scope,
c0fb8a56 294 args->lazy, 0);
993b3242
UD
295}
296
297static void
298map_doit (void *a)
299{
be935610 300 struct map_args *args = (struct map_args *) a;
c6222ab9 301 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
993b3242
UD
302}
303
304static void
305version_check_doit (void *a)
306{
be935610
UD
307 struct version_check_args *args = (struct version_check_args *) a;
308 if (_dl_check_all_versions (_dl_loaded, 1) && args->doexit)
993b3242
UD
309 /* We cannot start the application. Abort now. */
310 _exit (1);
311}
312
ce37fa88
UD
313
314static inline struct link_map *
315find_needed (const char *name)
316{
be935610 317 unsigned int n = _dl_loaded->l_searchlist.r_nlist;
ce37fa88 318
be935610
UD
319 while (n-- > 0)
320 if (_dl_name_match_p (name, _dl_loaded->l_searchlist.r_list[n]))
321 return _dl_loaded->l_searchlist.r_list[n];
ce37fa88
UD
322
323 /* Should never happen. */
324 return NULL;
325}
326
327static int
328match_version (const char *string, struct link_map *map)
329{
a42195db 330 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
ce37fa88
UD
331 ElfW(Verdef) *def;
332
333#define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
334 if (map->l_info[VERDEFTAG] == NULL)
335 /* The file has no symbol versioning. */
336 return 0;
337
338 def = (ElfW(Verdef) *) ((char *) map->l_addr
339 + map->l_info[VERDEFTAG]->d_un.d_ptr);
340 while (1)
341 {
342 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
343
344 /* Compare the version strings. */
345 if (strcmp (string, strtab + aux->vda_name) == 0)
346 /* Bingo! */
347 return 1;
348
349 /* If no more definitions we failed to find what we want. */
350 if (def->vd_next == 0)
351 break;
352
353 /* Next definition. */
354 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
355 }
356
357 return 0;
358}
359
120b4c49
UD
360static const char *library_path; /* The library search path. */
361static const char *preloadlist; /* The list preloaded objects. */
362static int version_info; /* Nonzero if information about
363 versions has to be printed. */
a1a9d215 364
d66e34cd 365static void
266180eb
RM
366dl_main (const ElfW(Phdr) *phdr,
367 ElfW(Half) phent,
368 ElfW(Addr) *user_entry)
d66e34cd 369{
266180eb 370 const ElfW(Phdr) *ph;
ea278354 371 enum mode mode;
2064087b
RM
372 struct link_map **preloads;
373 unsigned int npreloads;
14bab8de
UD
374 size_t file_size;
375 char *file;
2f6d1f1b 376 int has_interp = 0;
77aba05b 377 unsigned int i;
9a51759b 378 int rtld_is_main = 0;
db276fa1
UD
379 hp_timing_t start;
380 hp_timing_t stop;
381 hp_timing_t diff;
d66e34cd 382
ea278354 383 /* Process the environment variable which control the behaviour. */
12b5b6b7 384 process_envvars (&mode, &_dl_lazy);
3996f34b 385
46ec036d
UD
386 /* Set up a flag which tells we are just starting. */
387 _dl_starting_up = 1;
388
f21acc89 389 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
0200214b
RM
390 {
391 /* Ho ho. We are not the program interpreter! We are the program
392 itself! This means someone ran ld.so as a command. Well, that
393 might be convenient to do sometimes. We support it by
394 interpreting the args like this:
395
396 ld.so PROGRAM ARGS...
397
398 The first argument is the name of a file containing an ELF
399 executable we will load and run with the following arguments.
400 To simplify life here, PROGRAM is searched for using the
401 normal rules for shared objects, rather than $PATH or anything
402 like that. We just load it and use its entry point; we don't
403 pay attention to its PT_INTERP command (we are the interpreter
404 ourselves). This is an easy way to test a new ld.so before
405 installing it. */
9a51759b 406 rtld_is_main = 1;
421f82e5 407
ffee1316
RM
408 /* Note the place where the dynamic linker actually came from. */
409 _dl_rtld_map.l_name = _dl_argv[0];
6a76c115 410
fd26970f
UD
411 while (_dl_argc > 1)
412 if (! strcmp (_dl_argv[1], "--list"))
413 {
414 mode = list;
12b5b6b7 415 _dl_lazy = -1; /* This means do no dependency analysis. */
61965e9b 416
fd26970f
UD
417 ++_dl_skip_args;
418 --_dl_argc;
419 ++_dl_argv;
420 }
421 else if (! strcmp (_dl_argv[1], "--verify"))
422 {
423 mode = verify;
6a76c115 424
fd26970f
UD
425 ++_dl_skip_args;
426 --_dl_argc;
427 ++_dl_argv;
428 }
310930c1 429 else if (! strcmp (_dl_argv[1], "--library-path") && _dl_argc > 2)
880f421f
UD
430 {
431 library_path = _dl_argv[2];
432
310930c1
UD
433 _dl_skip_args += 2;
434 _dl_argc -= 2;
435 _dl_argv += 2;
436 }
b0a01055 437 else if (! strcmp (_dl_argv[1], "--inhibit-rpath") && _dl_argc > 2)
310930c1 438 {
b0a01055 439 _dl_inhibit_rpath = _dl_argv[2];
310930c1 440
880f421f
UD
441 _dl_skip_args += 2;
442 _dl_argc -= 2;
443 _dl_argv += 2;
444 }
fd26970f
UD
445 else
446 break;
d66e34cd 447
61eb22d3
UD
448 /* If we have no further argument the program was called incorrectly.
449 Grant the user some education. */
450 if (_dl_argc < 2)
451 _dl_sysdep_fatal ("\
2bcf29ba 452Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
61eb22d3
UD
453You have invoked `ld.so', the helper program for shared library executables.\n\
454This program usually lives in the file `/lib/ld.so', and special directives\n\
455in executable files using ELF shared libraries tell the system's program\n\
456loader to load the helper program from this file. This helper program loads\n\
457the shared libraries needed by the program executable, prepares the program\n\
458to run, and runs it. You may invoke this helper program directly from the\n\
459command line to load and run an ELF executable file; this is like executing\n\
460that file itself, but always uses this helper program from the file you\n\
461specified, instead of the helper program file specified in the executable\n\
462file you run. This is mostly of use for maintainers to test new versions\n\
2bcf29ba
UD
463of this helper program; chances are you did not intend to run this program.\n\
464\n\
b0a01055
UD
465 --list list all dependencies and how they are resolved\n\
466 --verify verify that given object really is a dynamically linked\n\
e8b1163e 467 object we can handle\n\
b0a01055
UD
468 --library-path PATH use given PATH instead of content of the environment\n\
469 variable LD_LIBRARY_PATH\n\
fcf70d41
UD
470 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
471 in LIST\n",
61eb22d3
UD
472 NULL);
473
0200214b
RM
474 ++_dl_skip_args;
475 --_dl_argc;
476 ++_dl_argv;
91f62ce6 477
da832465
UD
478 /* Initialize the data structures for the search paths for shared
479 objects. */
480 _dl_init_paths (library_path);
481
9a821cf9 482 if (__builtin_expect (mode, normal) == verify)
2de99474 483 {
dcf0671d 484 char *err_str = NULL;
993b3242 485 struct map_args args;
2de99474 486
993b3242 487 args.str = _dl_argv[0];
8d9618b7 488 (void) _dl_catch_error (&err_str, map_doit, &args);
2de99474 489 if (err_str != NULL)
dcf0671d
UD
490 {
491 free (err_str);
492 _exit (EXIT_FAILURE);
493 }
2de99474
UD
494 }
495 else
db276fa1
UD
496 {
497 HP_TIMING_NOW (start);
498 _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
499 HP_TIMING_NOW (stop);
61e0617a 500
db276fa1
UD
501 HP_TIMING_DIFF (load_time, start, stop);
502 }
2de99474 503
be935610
UD
504 phdr = _dl_loaded->l_phdr;
505 phent = _dl_loaded->l_phnum;
143e2b96
UD
506 /* We overwrite here a pointer to a malloc()ed string. But since
507 the malloc() implementation used at this point is the dummy
508 implementations which has no real free() function it does not
509 makes sense to free the old string first. */
be935610
UD
510 _dl_loaded->l_name = (char *) "";
511 *user_entry = _dl_loaded->l_entry;
0200214b
RM
512 }
513 else
514 {
515 /* Create a link_map for the executable itself.
516 This will be what dlopen on "" returns. */
87c812c2 517 _dl_new_object ((char *) "", "", lt_executable, NULL);
be935610 518 if (_dl_loaded == NULL)
762a2918 519 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
be935610
UD
520 _dl_loaded->l_phdr = phdr;
521 _dl_loaded->l_phnum = phent;
522 _dl_loaded->l_entry = *user_entry;
523 _dl_loaded->l_opencount = 1;
da832465 524
61e0617a
UD
525 /* At this point we are in a bit of trouble. We would have to
526 fill in the values for l_dev and l_ino. But in general we
527 do not know where the file is. We also do not handle AT_EXECFD
528 even if it would be passed up.
529
530 We leave the values here defined to 0. This is normally no
531 problem as the program code itself is normally no shared
532 object and therefore cannot be loaded dynamically. Nothing
533 prevent the use of dynamic binaries and in these situations
534 we might get problems. We might not be able to find out
535 whether the object is already loaded. But since there is no
536 easy way out and because the dynamic binary must also not
537 have an SONAME we ignore this program for now. If it becomes
538 a problem we can force people using SONAMEs. */
539
97a51d8a
UD
540 /* We delay initializing the path structure until we got the dynamic
541 information for the program. */
0200214b
RM
542 }
543
052b6a6c 544 /* It is not safe to load stuff after the main program. */
be935610 545 _dl_loaded->l_map_end = ~0;
052b6a6c 546 /* Perhaps the executable has no PT_LOAD header entries at all. */
be935610 547 _dl_loaded->l_map_start = ~0;
052b6a6c 548
0200214b
RM
549 /* Scan the program header table for the dynamic section. */
550 for (ph = phdr; ph < &phdr[phent]; ++ph)
551 switch (ph->p_type)
552 {
da832465
UD
553 case PT_PHDR:
554 /* Find out the load address. */
be935610 555 _dl_loaded->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
da832465 556 break;
0200214b
RM
557 case PT_DYNAMIC:
558 /* This tells us where to find the dynamic section,
559 which tells us everything we need to do. */
be935610 560 _dl_loaded->l_ld = (void *) _dl_loaded->l_addr + ph->p_vaddr;
0200214b
RM
561 break;
562 case PT_INTERP:
563 /* This "interpreter segment" was used by the program loader to
564 find the program interpreter, which is this program itself, the
565 dynamic linker. We note what name finds us, so that a future
566 dlopen call or DT_NEEDED entry, for something that wants to link
567 against the dynamic linker as a shared library, will know that
568 the shared object is already loaded. */
be935610
UD
569 _dl_rtld_libname.name = ((const char *) _dl_loaded->l_addr
570 + ph->p_vaddr);
c84142e8
UD
571 _dl_rtld_libname.next = NULL;
572 _dl_rtld_map.l_libname = &_dl_rtld_libname;
f41c8091
UD
573
574 /* Ordinarilly, we would get additional names for the loader from
575 our DT_SONAME. This can't happen if we were actually linked as
576 a static executable (detect this case when we have no DYNAMIC).
577 If so, assume the filename component of the interpreter path to
578 be our SONAME, and add it to our name list. */
579 if (_dl_rtld_map.l_ld == NULL)
580 {
581 char *p = strrchr (_dl_rtld_libname.name, '/');
582 if (p)
583 {
584 _dl_rtld_libname2.name = p+1;
585 _dl_rtld_libname2.next = NULL;
586 _dl_rtld_libname.next = &_dl_rtld_libname2;
587 }
588 }
589
2f6d1f1b 590 has_interp = 1;
0200214b 591 break;
052b6a6c
UD
592 case PT_LOAD:
593 /* Remember where the main program starts in memory. */
594 {
595 ElfW(Addr) mapstart;
be935610
UD
596 mapstart = _dl_loaded->l_addr + (ph->p_vaddr & ~(ph->p_align - 1));
597 if (_dl_loaded->l_map_start > mapstart)
598 _dl_loaded->l_map_start = mapstart;
052b6a6c
UD
599 }
600 break;
0200214b 601 }
ffee1316 602 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
c84142e8
UD
603 {
604 /* We were invoked directly, so the program might not have a
605 PT_INTERP. */
606 _dl_rtld_libname.name = _dl_rtld_map.l_name;
607 _dl_rtld_libname.next = NULL;
608 _dl_rtld_map.l_libname = &_dl_rtld_libname;
609 }
ffee1316
RM
610 else
611 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
0200214b 612
9a51759b
UD
613 if (! rtld_is_main)
614 {
615 /* Extract the contents of the dynamic section for easy access. */
d3ac2d47 616 elf_get_dynamic_info (_dl_loaded);
9a51759b
UD
617 if (_dl_loaded->l_info[DT_HASH])
618 /* Set up our cache of pointers into the hash table. */
619 _dl_setup_hash (_dl_loaded);
620 }
0200214b 621
9a821cf9 622 if (__builtin_expect (mode, normal) == verify)
e2102c14
UD
623 {
624 /* We were called just to verify that this is a dynamic
625 executable using us as the program interpreter. Exit with an
626 error if we were not able to load the binary or no interpreter
627 is specified (i.e., this is no dynamically linked binary. */
be935610 628 if (_dl_loaded->l_ld == NULL)
e2102c14 629 _exit (1);
e2102c14
UD
630
631 /* We allow here some platform specific code. */
632#ifdef DISTINGUISH_LIB_VERSIONS
633 DISTINGUISH_LIB_VERSIONS;
634#endif
eb406346 635 _exit (has_interp ? 0 : 2);
e2102c14
UD
636 }
637
9a51759b 638 if (! rtld_is_main)
97a51d8a
UD
639 /* Initialize the data structures for the search paths for shared
640 objects. */
120b4c49 641 _dl_init_paths (library_path);
97a51d8a 642
0200214b 643 /* Put the link_map for ourselves on the chain so it can be found by
ceb2d9aa 644 name. Note that at this point the global chain of link maps contains
be935610 645 exactly one element, which is pointed to by _dl_loaded. */
ffee1316
RM
646 if (! _dl_rtld_map.l_name)
647 /* If not invoked directly, the dynamic linker shared object file was
648 found by the PT_INTERP name. */
c84142e8 649 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
ba79d61b 650 _dl_rtld_map.l_type = lt_library;
be935610
UD
651 _dl_loaded->l_next = &_dl_rtld_map;
652 _dl_rtld_map.l_prev = _dl_loaded;
0200214b 653
14bab8de 654 /* We have two ways to specify objects to preload: via environment
49c091e5 655 variable and via the file /etc/ld.so.preload. The latter can also
14bab8de 656 be used when security is enabled. */
2064087b
RM
657 preloads = NULL;
658 npreloads = 0;
14bab8de 659
fd26970f 660 if (preloadlist)
c4029823 661 {
566efee2
UD
662 /* The LD_PRELOAD environment variable gives list of libraries
663 separated by white space or colons that are loaded before the
fd26970f
UD
664 executable's dependencies and prepended to the global scope
665 list. If the binary is running setuid all elements
666 containing a '/' are ignored since it is insecure. */
667 char *list = strdupa (preloadlist);
668 char *p;
db276fa1
UD
669
670 HP_TIMING_NOW (start);
671
566efee2 672 while ((p = strsep (&list, " :")) != NULL)
e2102c14
UD
673 if (p[0] != '\0'
674 && (! __libc_enable_secure || strchr (p, '/') == NULL))
fd26970f 675 {
be935610 676 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
c6222ab9 677 lt_library, 0);
bd355af0
UD
678 if (new_map->l_opencount == 1)
679 /* It is no duplicate. */
680 ++npreloads;
fd26970f 681 }
db276fa1
UD
682
683 HP_TIMING_NOW (stop);
684 HP_TIMING_DIFF (diff, start, stop);
685 HP_TIMING_ACCUM_NT (load_time, diff);
c4029823
UD
686 }
687
14bab8de
UD
688 /* Read the contents of the file. */
689 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
690 PROT_READ | PROT_WRITE);
691 if (file)
692 {
693 /* Parse the file. It contains names of libraries to be loaded,
694 separated by white spaces or `:'. It may also contain
695 comments introduced by `#'. */
696 char *problem;
697 char *runp;
698 size_t rest;
699
700 /* Eliminate comments. */
701 runp = file;
702 rest = file_size;
703 while (rest > 0)
704 {
705 char *comment = memchr (runp, '#', rest);
706 if (comment == NULL)
707 break;
708
709 rest -= comment - runp;
710 do
711 *comment = ' ';
712 while (--rest > 0 && *++comment != '\n');
713 }
714
715 /* We have one problematic case: if we have a name at the end of
716 the file without a trailing terminating characters, we cannot
717 place the \0. Handle the case separately. */
49891c10
UD
718 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
719 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
14bab8de
UD
720 {
721 problem = &file[file_size];
722 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
49891c10 723 && problem[-1] != '\n' && problem[-1] != ':')
14bab8de
UD
724 --problem;
725
726 if (problem > file)
727 problem[-1] = '\0';
728 }
729 else
49891c10
UD
730 {
731 problem = NULL;
732 file[file_size - 1] = '\0';
733 }
14bab8de 734
db276fa1
UD
735 HP_TIMING_NOW (start);
736
14bab8de
UD
737 if (file != problem)
738 {
739 char *p;
e2102c14 740 runp = file;
14bab8de 741 while ((p = strsep (&runp, ": \t\n")) != NULL)
e2102c14
UD
742 if (p[0] != '\0')
743 {
be935610 744 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
e2102c14
UD
745 lt_library, 0);
746 if (new_map->l_opencount == 1)
747 /* It is no duplicate. */
748 ++npreloads;
749 }
14bab8de
UD
750 }
751
752 if (problem != NULL)
753 {
754 char *p = strndupa (problem, file_size - (problem - file));
be935610 755 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
c6222ab9 756 lt_library, 0);
bd355af0
UD
757 if (new_map->l_opencount == 1)
758 /* It is no duplicate. */
759 ++npreloads;
14bab8de
UD
760 }
761
db276fa1
UD
762 HP_TIMING_NOW (stop);
763 HP_TIMING_DIFF (diff, start, stop);
764 HP_TIMING_ACCUM_NT (load_time, diff);
765
14bab8de
UD
766 /* We don't need the file anymore. */
767 __munmap (file, file_size);
768 }
769
14bab8de
UD
770 if (npreloads != 0)
771 {
772 /* Set up PRELOADS with a vector of the preloaded libraries. */
773 struct link_map *l;
14bab8de
UD
774 preloads = __alloca (npreloads * sizeof preloads[0]);
775 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
776 i = 0;
777 do
778 {
779 preloads[i++] = l;
780 l = l->l_next;
781 } while (l);
782 assert (i == npreloads);
783 }
784
2064087b
RM
785 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
786 specified some libraries to load, these are inserted before the actual
787 dependencies in the executable's searchlist for symbol resolution. */
db276fa1 788 HP_TIMING_NOW (start);
d9cb1a7d 789 _dl_map_object_deps (_dl_loaded, preloads, npreloads, mode == trace);
db276fa1
UD
790 HP_TIMING_NOW (stop);
791 HP_TIMING_DIFF (diff, start, stop);
792 HP_TIMING_ACCUM_NT (load_time, diff);
e3e35cfc
UD
793
794 /* Mark all objects as being in the global scope. */
795 for (i = _dl_loaded->l_searchlist.r_nlist; i > 0; )
796 _dl_loaded->l_searchlist.r_list[--i]->l_global = 1;
d66e34cd 797
2064087b 798#ifndef MAP_ANON
f332db02
RM
799 /* We are done mapping things, so close the zero-fill descriptor. */
800 __close (_dl_zerofd);
801 _dl_zerofd = -1;
2064087b 802#endif
f332db02 803
f9496a7b
RM
804 /* Remove _dl_rtld_map from the chain. */
805 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
806 if (_dl_rtld_map.l_next)
807 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
808
9a821cf9 809 if (__builtin_expect (_dl_rtld_map.l_opencount, 2) > 1)
0200214b 810 {
f9496a7b
RM
811 /* Some DT_NEEDED entry referred to the interpreter object itself, so
812 put it back in the list of visible objects. We insert it into the
813 chain in symbol search order because gdb uses the chain's order as
814 its symbol search order. */
77aba05b 815 i = 1;
be935610 816 while (_dl_loaded->l_searchlist.r_list[i] != &_dl_rtld_map)
f9496a7b 817 ++i;
be935610 818 _dl_rtld_map.l_prev = _dl_loaded->l_searchlist.r_list[i - 1];
b2bcd61a
UD
819 if (__builtin_expect (mode, normal) == normal)
820 _dl_rtld_map.l_next = (i + 1 < _dl_loaded->l_searchlist.r_nlist
821 ? _dl_loaded->l_searchlist.r_list[i + 1]
822 : NULL);
823 else
824 /* In trace mode there might be an invisible object (which we
825 could not find) after the previous one in the search list.
826 In this case it doesn't matter much where we put the
827 interpreter object, so we just initialize the list pointer so
828 that the assertion below holds. */
829 _dl_rtld_map.l_next = _dl_rtld_map.l_prev->l_next;
830
f9496a7b
RM
831 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
832 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
4d02a5b1 833 if (_dl_rtld_map.l_next)
f9496a7b
RM
834 {
835 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
836 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
837 }
0200214b 838 }
d66e34cd 839
c84142e8
UD
840 /* Now let us see whether all libraries are available in the
841 versions we need. */
842 {
993b3242
UD
843 struct version_check_args args;
844 args.doexit = mode == normal;
993b3242 845 _dl_receive_error (print_missing_version, version_check_doit, &args);
c84142e8
UD
846 }
847
9a821cf9 848 if (__builtin_expect (mode, normal) != normal)
0200214b
RM
849 {
850 /* We were run just to list the shared libraries. It is
851 important that we do this before real relocation, because the
852 functions we call below for output may no longer work properly
853 after relocation. */
0200214b
RM
854 if (! _dl_loaded->l_info[DT_NEEDED])
855 _dl_sysdep_message ("\t", "statically linked\n", NULL);
856 else
ceb2d9aa
UD
857 {
858 struct link_map *l;
859
860 for (l = _dl_loaded->l_next; l; l = l->l_next)
861 if (l->l_opencount == 0)
862 /* The library was not found. */
863 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
864 NULL);
865 else
866 {
867 char buf[20], *bp;
868 buf[sizeof buf - 1] = '\0';
af6f3906 869 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
ceb2d9aa
UD
870 while ((size_t) (&buf[sizeof buf - 1] - bp)
871 < sizeof l->l_addr * 2)
872 *--bp = '0';
873 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
874 l->l_name, " (0x", bp, ")\n", NULL);
875 }
876 }
1a3a58fd 877
9a821cf9 878 if (__builtin_expect (mode, trace) != trace)
cddcfecf
RM
879 for (i = 1; i < _dl_argc; ++i)
880 {
881 const ElfW(Sym) *ref = NULL;
d3ac2d47
UD
882 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], _dl_loaded,
883 &ref, _dl_loaded->l_scope,
a2b08ee5 884 ELF_MACHINE_JMP_SLOT);
cddcfecf
RM
885 char buf[20], *bp;
886 buf[sizeof buf - 1] = '\0';
af6f3906 887 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
14bab8de 888 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
cddcfecf
RM
889 *--bp = '0';
890 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
891 buf[sizeof buf - 1] = '\0';
af6f3906 892 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
14bab8de 893 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
cddcfecf
RM
894 *--bp = '0';
895 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
896 }
ce37fa88 897 else
fd26970f 898 {
12b5b6b7 899 if (_dl_lazy >= 0)
ce37fa88
UD
900 {
901 /* We have to do symbol dependency testing. */
902 struct relocate_args args;
903 struct link_map *l;
993b3242 904
12b5b6b7 905 args.lazy = _dl_lazy;
fd26970f 906
ce37fa88
UD
907 l = _dl_loaded;
908 while (l->l_next)
909 l = l->l_next;
910 do
911 {
912 if (l != &_dl_rtld_map && l->l_opencount > 0)
913 {
914 args.l = l;
915 _dl_receive_error (print_unresolved, relocate_doit,
916 &args);
ce37fa88
UD
917 }
918 l = l->l_prev;
919 } while (l);
920 }
921
922#define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
120b4c49 923 if (version_info)
fd26970f 924 {
ce37fa88
UD
925 /* Print more information. This means here, print information
926 about the versions needed. */
927 int first = 1;
928 struct link_map *map = _dl_loaded;
929
930 for (map = _dl_loaded; map != NULL; map = map->l_next)
fd26970f 931 {
f41c8091 932 const char *strtab;
ce37fa88 933 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
f41c8091
UD
934 ElfW(Verneed) *ent;
935
936 if (dyn == NULL)
937 continue;
938
a42195db 939 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
f41c8091 940 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
ce37fa88 941
f41c8091 942 if (first)
ce37fa88 943 {
f41c8091
UD
944 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
945 first = 0;
946 }
ce37fa88 947
f41c8091
UD
948 _dl_sysdep_message ("\t", (map->l_name[0]
949 ? map->l_name : _dl_argv[0]),
950 ":\n", NULL);
951
952 while (1)
953 {
954 ElfW(Vernaux) *aux;
955 struct link_map *needed;
ce37fa88 956
f41c8091
UD
957 needed = find_needed (strtab + ent->vn_file);
958 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
ce37fa88
UD
959
960 while (1)
961 {
f41c8091
UD
962 const char *fname = NULL;
963
964 _dl_sysdep_message ("\t\t",
965 strtab + ent->vn_file,
966 " (", strtab + aux->vna_name,
967 ") ",
968 (aux->vna_flags
969 & VER_FLG_WEAK
970 ? "[WEAK] " : ""),
971 "=> ", NULL);
972
973 if (needed != NULL
974 && match_version (strtab+aux->vna_name, needed))
975 fname = needed->l_name;
976
977 _dl_sysdep_message (fname ?: "not found", "\n",
978 NULL);
ce37fa88 979
f41c8091
UD
980 if (aux->vna_next == 0)
981 /* No more symbols. */
ce37fa88
UD
982 break;
983
f41c8091
UD
984 /* Next symbol. */
985 aux = (ElfW(Vernaux) *) ((char *) aux
986 + aux->vna_next);
ce37fa88 987 }
f41c8091
UD
988
989 if (ent->vn_next == 0)
990 /* No more dependencies. */
991 break;
992
993 /* Next dependency. */
994 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
ce37fa88 995 }
fd26970f 996 }
ce37fa88 997 }
fd26970f 998 }
d66e34cd 999
0200214b
RM
1000 _exit (0);
1001 }
86d2c878 1002
ba79d61b
RM
1003 {
1004 /* Now we have all the objects loaded. Relocate them all except for
1005 the dynamic linker itself. We do this in reverse order so that copy
1006 relocs of earlier objects overwrite the data written by later
1007 objects. We do not re-relocate the dynamic linker itself in this
1008 loop because that could result in the GOT entries for functions we
1009 call being changed, and that would break us. It is safe to relocate
1010 the dynamic linker out of order because it has no copy relocs (we
1011 know that because it is self-contained). */
1012
ceb2d9aa 1013 struct link_map *l;
c0fb8a56 1014 int consider_profiling = _dl_profile != NULL;
db276fa1
UD
1015 hp_timing_t start;
1016 hp_timing_t stop;
1017 hp_timing_t add;
c0fb8a56
UD
1018
1019 /* If we are profiling we also must do lazy reloaction. */
12b5b6b7 1020 _dl_lazy |= consider_profiling;
c0fb8a56 1021
ba79d61b
RM
1022 l = _dl_loaded;
1023 while (l->l_next)
1024 l = l->l_next;
db276fa1
UD
1025
1026 HP_TIMING_NOW (start);
ba79d61b
RM
1027 do
1028 {
1029 if (l != &_dl_rtld_map)
12b5b6b7 1030 _dl_relocate_object (l, l->l_scope, _dl_lazy, consider_profiling);
be935610 1031
ba79d61b 1032 l = l->l_prev;
db276fa1
UD
1033 }
1034 while (l);
1035 HP_TIMING_NOW (stop);
1036
1037 HP_TIMING_DIFF (relocate_time, start, stop);
ba79d61b
RM
1038
1039 /* Do any necessary cleanups for the startup OS interface code.
1040 We do these now so that no calls are made after rtld re-relocation
1041 which might be resolved to different functions than we expect.
1042 We cannot do this before relocating the other objects because
1043 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1044 _dl_sysdep_start_cleanup ();
1045
722c33bb
UD
1046 /* Now enable profiling if needed. Like the previous call,
1047 this has to go here because the calls it makes should use the
1048 rtld versions of the functions (particularly calloc()), but it
1049 needs to have _dl_profile_map set up by the relocator. */
1050 if (_dl_profile_map != NULL)
1051 /* We must prepare the profiling. */
1052 _dl_start_profile (_dl_profile_map, _dl_profile_output);
1053
1ca7cbcb 1054 if (_dl_rtld_map.l_opencount > 1)
db276fa1
UD
1055 {
1056 /* There was an explicit ref to the dynamic linker as a shared lib.
1057 Re-relocate ourselves with user-controlled symbol definitions. */
1058 HP_TIMING_NOW (start);
1059 _dl_relocate_object (&_dl_rtld_map, _dl_loaded->l_scope, 0, 0);
1060 HP_TIMING_NOW (stop);
1061 HP_TIMING_DIFF (add, start, stop);
1062 HP_TIMING_ACCUM_NT (relocate_time, add);
1063 }
ba79d61b 1064 }
ac16e905 1065
be935610
UD
1066 /* Now set up the variable which helps the assembler startup code. */
1067 _dl_main_searchlist = &_dl_loaded->l_searchlist;
1068 _dl_global_scope[0] = &_dl_loaded->l_searchlist;
1069
604510f7
UD
1070 /* Safe the information about the original global scope list since
1071 we need it in the memory handling later. */
1072 _dl_initial_searchlist = *_dl_main_searchlist;
1073
4d6acc61
RM
1074 {
1075 /* Initialize _r_debug. */
1076 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
ceb2d9aa 1077 struct link_map *l;
4d6acc61
RM
1078
1079 l = _dl_loaded;
ec42724d
RM
1080
1081#ifdef ELF_MACHINE_DEBUG_SETUP
1082
1083 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1084
1085 ELF_MACHINE_DEBUG_SETUP (l, r);
1086 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
1087
1088#else
1089
4d6acc61
RM
1090 if (l->l_info[DT_DEBUG])
1091 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1092 with the run-time address of the r_debug structure */
1093 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1094
d746b89c
RM
1095 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1096 case you run gdb on the dynamic linker directly. */
1097 if (_dl_rtld_map.l_info[DT_DEBUG])
1098 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1099
ec42724d
RM
1100#endif
1101
4d6acc61
RM
1102 /* Notify the debugger that all objects are now mapped in. */
1103 r->r_state = RT_ADD;
1104 _dl_debug_state ();
1105 }
0200214b 1106
08cac4ac
UD
1107#ifndef MAP_COPY
1108 /* We must munmap() the cache file. */
1109 _dl_unload_cache ();
1110#endif
1111
d66e34cd
RM
1112 /* Once we return, _dl_sysdep_start will invoke
1113 the DT_INIT functions and then *USER_ENTRY. */
1114}
fd26970f
UD
1115\f
1116/* This is a little helper function for resolving symbols while
1117 tracing the binary. */
1118static void
c84142e8
UD
1119print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1120 const char *errstring)
fd26970f 1121{
3996f34b
UD
1122 if (objname[0] == '\0')
1123 objname = _dl_argv[0] ?: "<main program>";
fd26970f
UD
1124 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
1125}
c84142e8
UD
1126\f
1127/* This is a little helper function for resolving symbols while
1128 tracing the binary. */
1129static void
1130print_missing_version (int errcode __attribute__ ((unused)),
1131 const char *objname, const char *errstring)
1132{
1133 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
1134 objname, ": ", errstring, "\n", NULL);
1135}
ea278354 1136\f
7dea968e
UD
1137/* Nonzero if any of the debugging options is enabled. */
1138static int any_debug;
1139
b5efde2f
UD
1140/* Process the string given as the parameter which explains which debugging
1141 options are enabled. */
1142static void
14c44e2e 1143process_dl_debug (const char *dl_debug)
b5efde2f 1144{
14c44e2e
UD
1145 size_t len;
1146#define separators " ,:"
b5efde2f
UD
1147 do
1148 {
14c44e2e 1149 len = 0;
b5efde2f 1150 /* Skip separating white spaces and commas. */
14c44e2e 1151 dl_debug += strspn (dl_debug, separators);
b5efde2f
UD
1152 if (*dl_debug != '\0')
1153 {
14c44e2e
UD
1154 len = strcspn (dl_debug, separators);
1155
1156 switch (len)
b5efde2f 1157 {
b0b67c47
UD
1158 case 3:
1159 /* This option is not documented since it is not generally
1160 useful. */
1161 if (memcmp (dl_debug, "all", 3) == 0)
1162 {
1163 _dl_debug_libs = 1;
1164 _dl_debug_impcalls = 1;
1165 _dl_debug_reloc = 1;
1166 _dl_debug_files = 1;
1167 _dl_debug_symbols = 1;
1168 _dl_debug_bindings = 1;
1169 _dl_debug_versions = 1;
1170 any_debug = 1;
ceb27555 1171 continue;
b0b67c47
UD
1172 }
1173 break;
1174
14c44e2e
UD
1175 case 4:
1176 if (memcmp (dl_debug, "help", 4) == 0)
1177 {
1178 _dl_sysdep_message ("\
08b511e6 1179Valid options for the LD_DEBUG environment variable are:\n\
b5efde2f 1180\n\
0c367d92 1181 bindings display information about symbol binding\n\
8193034b 1182 files display processing of files and libraries\n\
0c367d92
UD
1183 help display this help message and exit\n\
1184 libs display library search paths\n\
8193034b 1185 reloc display relocation processing\n\
de100ca7 1186 symbols display symbol table processing\n\
8193034b 1187 versions display version dependencies\n\
0c367d92
UD
1188\n\
1189To direct the debugging output into a file instead of standard output\n\
1190a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1191 NULL);
14c44e2e
UD
1192 _exit (0);
1193 }
77aba05b 1194
14c44e2e
UD
1195 if (memcmp (dl_debug, "libs", 4) == 0)
1196 {
1197 _dl_debug_libs = 1;
1198 _dl_debug_impcalls = 1;
1199 any_debug = 1;
1200 continue;
1201 }
1202 break;
1203
1204 case 5:
1205 if (memcmp (dl_debug, "reloc", 5) == 0)
1206 {
1207 _dl_debug_reloc = 1;
1208 _dl_debug_impcalls = 1;
1209 any_debug = 1;
1210 continue;
1211 }
1212
1213 if (memcmp (dl_debug, "files", 5) == 0)
1214 {
1215 _dl_debug_files = 1;
1216 _dl_debug_impcalls = 1;
1217 any_debug = 1;
1218 continue;
1219 }
1220 break;
77aba05b 1221
14c44e2e
UD
1222 case 7:
1223 if (memcmp (dl_debug, "symbols", 7) == 0)
1224 {
1225 _dl_debug_symbols = 1;
1226 _dl_debug_impcalls = 1;
1227 any_debug = 1;
1228 continue;
1229 }
1230 break;
77aba05b 1231
14c44e2e
UD
1232 case 8:
1233 if (memcmp (dl_debug, "bindings", 8) == 0)
1234 {
1235 _dl_debug_bindings = 1;
1236 _dl_debug_impcalls = 1;
1237 any_debug = 1;
1238 continue;
1239 }
1240
1241 if (memcmp (dl_debug, "versions", 8) == 0)
1242 {
1243 _dl_debug_versions = 1;
1244 _dl_debug_impcalls = 1;
1245 any_debug = 1;
1246 continue;
1247 }
1248 break;
1249
db276fa1
UD
1250 case 10:
1251 if (memcmp (dl_debug, "statistics", 10) == 0)
1252 {
1253 _dl_debug_statistics = 1;
1254 continue;
1255 }
1256 break;
1257
14c44e2e
UD
1258 default:
1259 break;
77aba05b 1260 }
14c44e2e
UD
1261
1262 {
1263 /* Display a warning and skip everything until next separator. */
1264 char *startp = strndupa (dl_debug, len);
1265 _dl_sysdep_error ("warning: debug option `", startp,
1266 "' unknown; try LD_DEBUG=help\n", NULL);
db276fa1 1267 break;
14c44e2e 1268 }
b5efde2f
UD
1269 }
1270 }
14c44e2e 1271 while (*(dl_debug += len) != '\0');
b5efde2f
UD
1272}
1273\f
ea278354
UD
1274/* Process all environments variables the dynamic linker must recognize.
1275 Since all of them start with `LD_' we are a bit smarter while finding
1276 all the entries. */
1277static void
1278process_envvars (enum mode *modep, int *lazyp)
1279{
1280 char **runp = NULL;
1281 char *envline;
1282 enum mode mode = normal;
1283 int bind_now = 0;
7dea968e 1284 char *debug_output = NULL;
ea278354
UD
1285
1286 /* This is the default place for profiling data file. */
1287 _dl_profile_output = "/var/tmp";
1288
1289 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1290 {
14c44e2e 1291 size_t len = strcspn (envline, "=") - 3;
ea278354 1292
14c44e2e 1293 switch (len)
ea278354 1294 {
14c44e2e
UD
1295 case 4:
1296 /* Warning level, verbose or not. */
1297 if (memcmp (&envline[3], "WARN", 4) == 0)
1298 _dl_verbose = envline[8] != '\0';
1299 break;
ea278354 1300
14c44e2e
UD
1301 case 5:
1302 /* Debugging of the dynamic linker? */
1303 if (memcmp (&envline[3], "DEBUG", 5) == 0)
1304 process_dl_debug (&envline[9]);
1305 break;
b5efde2f 1306
14c44e2e
UD
1307 case 7:
1308 /* Print information about versions. */
1309 if (memcmp (&envline[3], "VERBOSE", 7) == 0)
1310 {
1311 version_info = envline[11] != '\0';
1312 break;
1313 }
7dea968e 1314
14c44e2e
UD
1315 /* List of objects to be preloaded. */
1316 if (memcmp (&envline[3], "PRELOAD", 7) == 0)
1317 {
1318 preloadlist = &envline[11];
1319 break;
1320 }
120b4c49 1321
14c44e2e
UD
1322 /* Which shared object shall be profiled. */
1323 if (memcmp (&envline[3], "PROFILE", 7) == 0)
722c33bb 1324 _dl_profile = &envline[11];
14c44e2e 1325 break;
120b4c49 1326
14c44e2e
UD
1327 case 8:
1328 /* Do we bind early? */
0e103c6d
UD
1329 if (memcmp (&envline[3], "BIND_NOW", 8) == 0)
1330 bind_now = envline[12] != '\0';
14c44e2e 1331 break;
ea278354 1332
14c44e2e
UD
1333 case 9:
1334 /* Test whether we want to see the content of the auxiliary
1335 array passed up from the kernel. */
1336 if (memcmp (&envline[3], "SHOW_AUXV", 9) == 0)
1337 _dl_show_auxv ();
1338 break;
ea278354 1339
12264bd7 1340 case 10:
3081378b 1341 /* Mask for the important hardware capabilities. */
12264bd7
UD
1342 if (memcmp (&envline[3], "HWCAP_MASK", 10) == 0)
1343 _dl_hwcap_mask = strtoul (&envline[14], NULL, 0);
1344 break;
1345
f787edde
UD
1346 case 11:
1347 /* Path where the binary is found. */
45769315
UD
1348 if (!__libc_enable_secure
1349 && memcmp (&envline[3], "ORIGIN_PATH", 11) == 0)
997a4165 1350 _dl_origin_path = &envline[15];
f787edde
UD
1351 break;
1352
14c44e2e
UD
1353 case 12:
1354 /* Where to place the profiling data file. */
1355 if (memcmp (&envline[3], "DEBUG_OUTPUT", 12) == 0)
1356 {
1357 debug_output = &envline[16];
1358 break;
1359 }
ea278354 1360
14c44e2e
UD
1361 /* The library search path. */
1362 if (memcmp (&envline[3], "LIBRARY_PATH", 12) == 0)
1363 library_path = &envline[16];
1364 break;
ea278354 1365
14c44e2e
UD
1366 case 14:
1367 /* Where to place the profiling data file. */
3081378b
UD
1368 if (!__libc_enable_secure
1369 && memcmp (&envline[3], "PROFILE_OUTPUT", 14) == 0)
14c44e2e
UD
1370 {
1371 _dl_profile_output = &envline[18];
1372 if (*_dl_profile_output == '\0')
1373 _dl_profile_output = "/var/tmp";
1374 }
1375 break;
120b4c49 1376
14c44e2e
UD
1377 case 20:
1378 /* The mode of the dynamic linker can be set. */
1379 if (memcmp (&envline[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1380 mode = trace;
1381 break;
e2102c14
UD
1382
1383 /* We might have some extra environment variable to handle. This
1384 is tricky due to the pre-processing of the length of the name
1385 in the switch statement here. The code here assumes that added
1386 environment variables have a different length. */
1387#ifdef EXTRA_LD_ENVVARS
1388 EXTRA_LD_ENVVARS
1389#endif
ea278354
UD
1390 }
1391 }
1392
4bae5567
UD
1393 /* Extra security for SUID binaries. Remove all dangerous environment
1394 variables. */
1395 if (__libc_enable_secure)
1396 {
1397 static const char *unsecure_envvars[] =
1398 {
1399#ifdef EXTRA_UNSECURE_ENVVARS
1400 EXTRA_UNSECURE_ENVVARS
1401#endif
1402 };
1403 size_t cnt;
1404
1405 if (preloadlist != NULL)
1406 unsetenv ("LD_PRELOAD");
1407 if (library_path != NULL)
1408 unsetenv ("LD_LIBRARY_PATH");
722c33bb
UD
1409 if (_dl_origin_path != NULL)
1410 unsetenv ("LD_ORIGIN_PATH");
1411 if (debug_output != NULL)
1412 unsetenv ("LD_DEBUG_OUTPUT");
1413 if (_dl_profile != NULL)
1414 unsetenv ("LD_PROFILE");
4bae5567
UD
1415
1416 for (cnt = 0;
1417 cnt < sizeof (unsecure_envvars) / sizeof (unsecure_envvars[0]);
1418 ++cnt)
1419 unsetenv (unsecure_envvars[cnt]);
1420 }
1421
722c33bb
UD
1422 /* The name of the object to profile cannot be empty. */
1423 if (_dl_profile != NULL && *_dl_profile == '\0')
1424 _dl_profile = NULL;
1425
7dea968e
UD
1426 /* If we have to run the dynamic linker in debugging mode and the
1427 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1428 messages to this file. */
14c44e2e 1429 if (any_debug && debug_output != NULL && !__libc_enable_secure)
7dea968e 1430 {
7a2fd787
UD
1431 size_t name_len = strlen (debug_output);
1432 char buf[name_len + 12];
1433 char *startp;
1434
1435 buf[name_len + 11] = '\0';
1436 startp = _itoa_word (__getpid (), &buf[name_len + 11], 10, 0);
1437 *--startp = '.';
1438 startp = memcpy (startp - name_len, debug_output, name_len);
1439
1440 _dl_debug_fd = __open (startp, O_WRONLY | O_APPEND | O_CREAT, 0666);
7dea968e
UD
1441 if (_dl_debug_fd == -1)
1442 /* We use standard output if opening the file failed. */
1443 _dl_debug_fd = STDOUT_FILENO;
1444 }
1445
ea278354
UD
1446 /* LAZY is determined by the environment variable LD_WARN and
1447 LD_BIND_NOW if we trace the binary. */
9a821cf9 1448 if (__builtin_expect (mode, normal) == trace)
ea278354
UD
1449 *lazyp = _dl_verbose ? !bind_now : -1;
1450 else
57de5037 1451 *lazyp = !bind_now;
ea278354
UD
1452
1453 *modep = mode;
1454}
db276fa1
UD
1455
1456
1457/* Print the various times we collected. */
1458static void
1459print_statistics (void)
1460{
1461 char buf[200];
1462 char *cp;
1463 char *wp;
1464
1465 /* Total time rtld used. */
1466 if (HP_TIMING_AVAIL)
1467 {
1468 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1469 _dl_debug_message (1, "\nruntime linker statistics:\n"
1470 " total startup time in dynamic loader: ",
1471 buf, "\n", NULL);
1472 }
1473
1474 /* Print relocation statistics. */
1475 if (HP_TIMING_AVAIL)
1476 {
1477 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1478 _dl_debug_message (1, " time needed for relocation: ", buf,
1479 NULL);
1480 cp = _itoa_word ((1000 * relocate_time) / rtld_total_time,
1481 buf + sizeof (buf), 10, 0);
1482 wp = buf;
1483 switch (buf + sizeof (buf) - cp)
1484 {
1485 case 3:
1486 *wp++ = *cp++;
1487 case 2:
1488 *wp++ = *cp++;
1489 case 1:
1490 *wp++ = '.';
1491 *wp++ = *cp++;
1492 }
1493 *wp = '\0';
1494 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1495 }
1496
1497 buf[sizeof (buf) - 1] = '\0';
1498 _dl_debug_message (1, " number of relocations: ",
1499 _itoa_word (_dl_num_relocations,
1500 buf + sizeof (buf) - 1, 10, 0),
1501 "\n", NULL);
1502
1503 /* Time spend while loading the object and the dependencies. */
1504 if (HP_TIMING_AVAIL)
1505 {
1506 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1507 _dl_debug_message (1, " time needed to load objects: ", buf,
1508 NULL);
1509 cp = _itoa_word ((1000 * load_time) / rtld_total_time,
1510 buf + sizeof (buf), 10, 0);
1511 wp = buf;
1512 switch (buf + sizeof (buf) - cp)
1513 {
1514 case 3:
1515 *wp++ = *cp++;
1516 case 2:
1517 *wp++ = *cp++;
1518 case 1:
1519 *wp++ = '.';
1520 *wp++ = *cp++;
1521 }
1522 *wp = '\0';
1523 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1524 }
1525}