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