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