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