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