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