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