]> 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.
49891c10 2 Copyright (C) 1995, 1996, 1997, 1998 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 <link.h>
d66e34cd
RM
22#include <stddef.h>
23#include <stdlib.h>
f51d1dfd 24#include <string.h>
d66e34cd 25#include <unistd.h>
2064087b 26#include <sys/mman.h> /* Check if MAP_ANON is defined. */
ce37fa88 27#include <stdio-common/_itoa.h>
b1dbbaa4 28#include <assert.h>
f21acc89 29#include <entry.h>
f5348425
RM
30#include "dynamic-link.h"
31
32
d66e34cd
RM
33/* System-specific function to do initial startup for the dynamic linker.
34 After this, file access calls and getenv must work. This is responsible
cddcfecf 35 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
d66e34cd 36 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
266180eb
RM
37extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
38 void (*dl_main) (const ElfW(Phdr) *phdr,
39 ElfW(Half) phent,
40 ElfW(Addr) *user_entry));
4cb20290 41extern void _dl_sysdep_start_cleanup (void);
d66e34cd 42
14bab8de
UD
43/* System-dependent function to read a file's whole contents
44 in the most convenient manner available. */
45extern void *_dl_sysdep_read_whole_file (const char *filename,
46 size_t *filesize_ptr,
47 int mmap_prot);
48
fd26970f 49/* Helper function to handle errors while resolving symbols. */
c84142e8
UD
50static void print_unresolved (int errcode, const char *objname,
51 const char *errsting);
52
53/* Helper function to handle errors when a version is missing. */
54static void print_missing_version (int errcode, const char *objname,
55 const char *errsting);
fd26970f 56
ea278354
UD
57
58/* This is a list of all the modes the dynamic loader can be in. */
59enum mode { normal, list, verify, trace };
60
61/* Process all environments variables the dynamic linker must recognize.
62 Since all of them start with `LD_' we are a bit smarter while finding
63 all the entries. */
64static void process_envvars (enum mode *modep, int *lazyp);
65
d66e34cd
RM
66int _dl_argc;
67char **_dl_argv;
4cb20290 68const char *_dl_rpath;
cf29ffbe 69int _dl_verbose;
0a54e401
UD
70const char *_dl_platform;
71size_t _dl_platformlen;
f41c8091 72unsigned long _dl_hwcap;
0a54e401 73struct r_search_path *_dl_search_paths;
3996f34b
UD
74const char *_dl_profile;
75const char *_dl_profile_output;
76struct link_map *_dl_profile_map;
b5efde2f 77int _dl_debug_libs;
7dea968e 78int _dl_debug_impcalls;
0c367d92 79int _dl_debug_bindings;
de100ca7 80int _dl_debug_symbols;
8193034b
UD
81int _dl_debug_versions;
82int _dl_debug_reloc;
83int _dl_debug_files;
d66e34cd 84
39778c6c
UD
85/* Set nonzero during loading and initialization of executable and
86 libraries, cleared before the executable's entry point runs. This
87 must not be initialized to nonzero, because the unused dynamic
88 linker loaded in for libc.so's "ld.so.1" dep will provide the
89 definition seen by libc.so's initializer; that value must be zero,
90 and will be since that dynamic linker's _dl_start and dl_main will
91 never be called. */
92int _dl_starting_up;
93
266180eb
RM
94static void dl_main (const ElfW(Phdr) *phdr,
95 ElfW(Half) phent,
96 ElfW(Addr) *user_entry);
d66e34cd 97
ee188d55 98struct link_map _dl_rtld_map;
c84142e8 99struct libname_list _dl_rtld_libname;
f41c8091 100struct libname_list _dl_rtld_libname2;
86d2c878 101
b1dbbaa4
RM
102#ifdef RTLD_START
103RTLD_START
104#else
105#error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
106#endif
107
ceb2d9aa 108static ElfW(Addr)
d66e34cd
RM
109_dl_start (void *arg)
110{
86d2c878 111 struct link_map bootstrap_map;
d66e34cd 112
b1dbbaa4
RM
113 /* This #define produces dynamic linking inline functions for
114 bootstrap relocation instead of general-purpose relocation. */
115#define RTLD_BOOTSTRAP
c84142e8 116#define RESOLVE(sym, version, flags) bootstrap_map.l_addr
b1dbbaa4
RM
117#include "dynamic-link.h"
118
d66e34cd 119 /* Figure out the run-time load address of the dynamic linker itself. */
86d2c878 120 bootstrap_map.l_addr = elf_machine_load_address ();
d66e34cd 121
47707456
UD
122 /* Read our own dynamic section and fill in the info array. */
123 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
86d2c878 124 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
d66e34cd
RM
125
126#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
86d2c878 127 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
d66e34cd
RM
128#endif
129
130 /* Relocate ourselves so we can do normal function calls and
131 data access using the global offset table. */
421f82e5 132
3996f34b 133 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
ea7eb7e3
UD
134 /* Please note that we don't allow profiling of this object and
135 therefore need not test whether we have to allocate the array
136 for the relocation results (as done in dl-reloc.c). */
421f82e5 137
d66e34cd
RM
138 /* Now life is sane; we can call functions and access global data.
139 Set up to use the operating system facilities, and find out from
140 the operating system's program loader where to find the program
141 header table in core. */
142
86d2c878 143 /* Transfer data about ourselves to the permanent link_map structure. */
ee188d55
RM
144 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
145 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
f41c8091 146 _dl_rtld_map.l_opencount = 1;
ee188d55
RM
147 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
148 sizeof _dl_rtld_map.l_info);
149 _dl_setup_hash (&_dl_rtld_map);
86d2c878 150
4cb20290
RM
151 /* Cache the DT_RPATH stored in ld.so itself; this will be
152 the default search path. */
f41c8091
UD
153 if (_dl_rtld_map.l_info[DT_STRTAB] && _dl_rtld_map.l_info[DT_RPATH])
154 {
155 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
156 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
157 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
158 }
d66e34cd
RM
159
160 /* Call the OS-dependent function to set up life so we can do things like
161 file access. It will call `dl_main' (below) to do all the real work
162 of the dynamic linker, and then unwind our frame and run the user
163 entry point on the same stack we entered on. */
8d6468d0 164 return _dl_sysdep_start (arg, &dl_main);
d66e34cd
RM
165}
166
d66e34cd
RM
167/* Now life is peachy; we can do all normal operations.
168 On to the real work. */
169
f21acc89 170void ENTRY_POINT (void);
d66e34cd 171
993b3242
UD
172/* Some helper functions. */
173
174/* Arguments to relocate_doit. */
175struct relocate_args
176{
177 struct link_map *l;
178 int lazy;
179};
180
181struct map_args
182{
183 /* Argument to map_doit. */
184 char *str;
185 /* Return value of map_doit. */
186 struct link_map *main_map;
187};
188
189/* Arguments to version_check_doit. */
190struct version_check_args
191{
192 struct link_map *main_map;
193 int doexit;
194};
195
196static void
197relocate_doit (void *a)
198{
199 struct relocate_args *args = (struct relocate_args *) a;
200
201 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
202 args->lazy);
203}
204
205static void
206map_doit (void *a)
207{
208 struct map_args *args = (struct map_args *)a;
c6222ab9 209 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
993b3242
UD
210}
211
212static void
213version_check_doit (void *a)
214{
215 struct version_check_args *args = (struct version_check_args *)a;
216 if (_dl_check_all_versions (args->main_map, 1) && args->doexit)
217 /* We cannot start the application. Abort now. */
218 _exit (1);
219}
220
ce37fa88
UD
221
222static inline struct link_map *
223find_needed (const char *name)
224{
225 unsigned int n;
226
227 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
228 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
229 return _dl_loaded->l_searchlist[n];
230
231 /* Should never happen. */
232 return NULL;
233}
234
235static int
236match_version (const char *string, struct link_map *map)
237{
238 const char *strtab = (const char *) (map->l_addr
239 + map->l_info[DT_STRTAB]->d_un.d_ptr);
240 ElfW(Verdef) *def;
241
242#define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
243 if (map->l_info[VERDEFTAG] == NULL)
244 /* The file has no symbol versioning. */
245 return 0;
246
247 def = (ElfW(Verdef) *) ((char *) map->l_addr
248 + map->l_info[VERDEFTAG]->d_un.d_ptr);
249 while (1)
250 {
251 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
252
253 /* Compare the version strings. */
254 if (strcmp (string, strtab + aux->vda_name) == 0)
255 /* Bingo! */
256 return 1;
257
258 /* If no more definitions we failed to find what we want. */
259 if (def->vd_next == 0)
260 break;
261
262 /* Next definition. */
263 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
264 }
265
266 return 0;
267}
268
120b4c49
UD
269static unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
270static const char *library_path; /* The library search path. */
271static const char *preloadlist; /* The list preloaded objects. */
272static int version_info; /* Nonzero if information about
273 versions has to be printed. */
a1a9d215 274
d66e34cd 275static void
266180eb
RM
276dl_main (const ElfW(Phdr) *phdr,
277 ElfW(Half) phent,
278 ElfW(Addr) *user_entry)
d66e34cd 279{
266180eb 280 const ElfW(Phdr) *ph;
ceb2d9aa 281 struct link_map *main_map;
0200214b 282 int lazy;
ea278354 283 enum mode mode;
2064087b
RM
284 struct link_map **preloads;
285 unsigned int npreloads;
14bab8de
UD
286 size_t file_size;
287 char *file;
2f6d1f1b 288 int has_interp = 0;
77aba05b 289 unsigned int i;
d66e34cd 290
ea278354
UD
291 /* Process the environment variable which control the behaviour. */
292 process_envvars (&mode, &lazy);
3996f34b 293
46ec036d
UD
294 /* Set up a flag which tells we are just starting. */
295 _dl_starting_up = 1;
296
f21acc89 297 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
0200214b
RM
298 {
299 /* Ho ho. We are not the program interpreter! We are the program
300 itself! This means someone ran ld.so as a command. Well, that
301 might be convenient to do sometimes. We support it by
302 interpreting the args like this:
303
304 ld.so PROGRAM ARGS...
305
306 The first argument is the name of a file containing an ELF
307 executable we will load and run with the following arguments.
308 To simplify life here, PROGRAM is searched for using the
309 normal rules for shared objects, rather than $PATH or anything
310 like that. We just load it and use its entry point; we don't
311 pay attention to its PT_INTERP command (we are the interpreter
312 ourselves). This is an easy way to test a new ld.so before
313 installing it. */
421f82e5 314
ffee1316
RM
315 /* Note the place where the dynamic linker actually came from. */
316 _dl_rtld_map.l_name = _dl_argv[0];
6a76c115 317
fd26970f
UD
318 while (_dl_argc > 1)
319 if (! strcmp (_dl_argv[1], "--list"))
320 {
321 mode = list;
322 lazy = -1; /* This means do no dependency analysis. */
61965e9b 323
fd26970f
UD
324 ++_dl_skip_args;
325 --_dl_argc;
326 ++_dl_argv;
327 }
328 else if (! strcmp (_dl_argv[1], "--verify"))
329 {
330 mode = verify;
6a76c115 331
fd26970f
UD
332 ++_dl_skip_args;
333 --_dl_argc;
334 ++_dl_argv;
335 }
880f421f
UD
336 else if (! strcmp (_dl_argv[1], "--library-path")
337 && _dl_argc > 2)
338 {
339 library_path = _dl_argv[2];
340
341 _dl_skip_args += 2;
342 _dl_argc -= 2;
343 _dl_argv += 2;
344 }
fd26970f
UD
345 else
346 break;
d66e34cd 347
61eb22d3
UD
348 /* If we have no further argument the program was called incorrectly.
349 Grant the user some education. */
350 if (_dl_argc < 2)
351 _dl_sysdep_fatal ("\
2bcf29ba 352Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
61eb22d3
UD
353You have invoked `ld.so', the helper program for shared library executables.\n\
354This program usually lives in the file `/lib/ld.so', and special directives\n\
355in executable files using ELF shared libraries tell the system's program\n\
356loader to load the helper program from this file. This helper program loads\n\
357the shared libraries needed by the program executable, prepares the program\n\
358to run, and runs it. You may invoke this helper program directly from the\n\
359command line to load and run an ELF executable file; this is like executing\n\
360that file itself, but always uses this helper program from the file you\n\
361specified, instead of the helper program file specified in the executable\n\
362file you run. This is mostly of use for maintainers to test new versions\n\
2bcf29ba
UD
363of this helper program; chances are you did not intend to run this program.\n\
364\n\
365 --list list all dependencies and how they are resolved\n\
366 --verify verify that given object really is a dynamically linked\n\
367 object we get handle\n\
368 --library-path PATH use given PATH instead of content of the environment\n\
369 variable LD_LIBRARY_PATH\n",
61eb22d3
UD
370 NULL);
371
0200214b
RM
372 ++_dl_skip_args;
373 --_dl_argc;
374 ++_dl_argv;
91f62ce6 375
da832465
UD
376 /* Initialize the data structures for the search paths for shared
377 objects. */
378 _dl_init_paths (library_path);
379
2de99474
UD
380 if (mode == verify)
381 {
dcf0671d 382 char *err_str = NULL;
993b3242 383 struct map_args args;
2de99474 384
993b3242 385 args.str = _dl_argv[0];
8d9618b7 386 (void) _dl_catch_error (&err_str, map_doit, &args);
993b3242 387 main_map = args.main_map;
2de99474 388 if (err_str != NULL)
dcf0671d
UD
389 {
390 free (err_str);
391 _exit (EXIT_FAILURE);
392 }
2de99474
UD
393 }
394 else
c6222ab9 395 main_map = _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
2de99474 396
ceb2d9aa
UD
397 phdr = main_map->l_phdr;
398 phent = main_map->l_phnum;
399 main_map->l_name = (char *) "";
400 *user_entry = main_map->l_entry;
0200214b
RM
401 }
402 else
403 {
404 /* Create a link_map for the executable itself.
405 This will be what dlopen on "" returns. */
1618c590 406 main_map = _dl_new_object ((char *) "", "", lt_executable);
ceb2d9aa 407 if (main_map == NULL)
762a2918 408 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
ceb2d9aa
UD
409 main_map->l_phdr = phdr;
410 main_map->l_phnum = phent;
411 main_map->l_entry = *user_entry;
3e5f5557 412 main_map->l_opencount = 1;
da832465 413
97a51d8a
UD
414 /* We delay initializing the path structure until we got the dynamic
415 information for the program. */
0200214b
RM
416 }
417
418 /* Scan the program header table for the dynamic section. */
419 for (ph = phdr; ph < &phdr[phent]; ++ph)
420 switch (ph->p_type)
421 {
da832465
UD
422 case PT_PHDR:
423 /* Find out the load address. */
424 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
425 break;
0200214b
RM
426 case PT_DYNAMIC:
427 /* This tells us where to find the dynamic section,
428 which tells us everything we need to do. */
ceb2d9aa 429 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
0200214b
RM
430 break;
431 case PT_INTERP:
432 /* This "interpreter segment" was used by the program loader to
433 find the program interpreter, which is this program itself, the
434 dynamic linker. We note what name finds us, so that a future
435 dlopen call or DT_NEEDED entry, for something that wants to link
436 against the dynamic linker as a shared library, will know that
437 the shared object is already loaded. */
ceb2d9aa 438 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
c84142e8
UD
439 _dl_rtld_libname.next = NULL;
440 _dl_rtld_map.l_libname = &_dl_rtld_libname;
f41c8091
UD
441
442 /* Ordinarilly, we would get additional names for the loader from
443 our DT_SONAME. This can't happen if we were actually linked as
444 a static executable (detect this case when we have no DYNAMIC).
445 If so, assume the filename component of the interpreter path to
446 be our SONAME, and add it to our name list. */
447 if (_dl_rtld_map.l_ld == NULL)
448 {
449 char *p = strrchr (_dl_rtld_libname.name, '/');
450 if (p)
451 {
452 _dl_rtld_libname2.name = p+1;
453 _dl_rtld_libname2.next = NULL;
454 _dl_rtld_libname.next = &_dl_rtld_libname2;
455 }
456 }
457
2f6d1f1b 458 has_interp = 1;
0200214b
RM
459 break;
460 }
ffee1316 461 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
c84142e8
UD
462 {
463 /* We were invoked directly, so the program might not have a
464 PT_INTERP. */
465 _dl_rtld_libname.name = _dl_rtld_map.l_name;
466 _dl_rtld_libname.next = NULL;
467 _dl_rtld_map.l_libname = &_dl_rtld_libname;
468 }
ffee1316
RM
469 else
470 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
0200214b 471
61965e9b
RM
472 if (mode == verify)
473 /* We were called just to verify that this is a dynamic executable
474 using us as the program interpreter. */
ceb2d9aa 475 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
61965e9b 476
0200214b 477 /* Extract the contents of the dynamic section for easy access. */
ceb2d9aa
UD
478 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
479 if (main_map->l_info[DT_HASH])
0200214b 480 /* Set up our cache of pointers into the hash table. */
ceb2d9aa 481 _dl_setup_hash (main_map);
0200214b 482
97a51d8a
UD
483 if (*user_entry != (ElfW(Addr)) &ENTRY_POINT)
484 /* Initialize the data structures for the search paths for shared
485 objects. */
120b4c49 486 _dl_init_paths (library_path);
97a51d8a 487
0200214b 488 /* Put the link_map for ourselves on the chain so it can be found by
ceb2d9aa
UD
489 name. Note that at this point the global chain of link maps contains
490 exactly one element, which is pointed to by main_map. */
ffee1316
RM
491 if (! _dl_rtld_map.l_name)
492 /* If not invoked directly, the dynamic linker shared object file was
493 found by the PT_INTERP name. */
c84142e8 494 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
ba79d61b 495 _dl_rtld_map.l_type = lt_library;
ceb2d9aa
UD
496 main_map->l_next = &_dl_rtld_map;
497 _dl_rtld_map.l_prev = main_map;
0200214b 498
14bab8de
UD
499 /* We have two ways to specify objects to preload: via environment
500 variable and via the file /etc/ld.so.preload. The later can also
501 be used when security is enabled. */
2064087b
RM
502 preloads = NULL;
503 npreloads = 0;
14bab8de 504
fd26970f 505 if (preloadlist)
c4029823 506 {
566efee2
UD
507 /* The LD_PRELOAD environment variable gives list of libraries
508 separated by white space or colons that are loaded before the
fd26970f
UD
509 executable's dependencies and prepended to the global scope
510 list. If the binary is running setuid all elements
511 containing a '/' are ignored since it is insecure. */
512 char *list = strdupa (preloadlist);
513 char *p;
566efee2 514 while ((p = strsep (&list, " :")) != NULL)
fd26970f
UD
515 if (! __libc_enable_secure || strchr (p, '/') == NULL)
516 {
2bcf29ba 517 struct link_map *new_map = _dl_map_object (main_map, p, 1,
c6222ab9 518 lt_library, 0);
bd355af0
UD
519 if (new_map->l_opencount == 1)
520 /* It is no duplicate. */
521 ++npreloads;
fd26970f 522 }
c4029823
UD
523 }
524
14bab8de
UD
525 /* Read the contents of the file. */
526 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
527 PROT_READ | PROT_WRITE);
528 if (file)
529 {
530 /* Parse the file. It contains names of libraries to be loaded,
531 separated by white spaces or `:'. It may also contain
532 comments introduced by `#'. */
533 char *problem;
534 char *runp;
535 size_t rest;
536
537 /* Eliminate comments. */
538 runp = file;
539 rest = file_size;
540 while (rest > 0)
541 {
542 char *comment = memchr (runp, '#', rest);
543 if (comment == NULL)
544 break;
545
546 rest -= comment - runp;
547 do
548 *comment = ' ';
549 while (--rest > 0 && *++comment != '\n');
550 }
551
552 /* We have one problematic case: if we have a name at the end of
553 the file without a trailing terminating characters, we cannot
554 place the \0. Handle the case separately. */
49891c10
UD
555 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
556 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
14bab8de
UD
557 {
558 problem = &file[file_size];
559 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
49891c10 560 && problem[-1] != '\n' && problem[-1] != ':')
14bab8de
UD
561 --problem;
562
563 if (problem > file)
564 problem[-1] = '\0';
565 }
566 else
49891c10
UD
567 {
568 problem = NULL;
569 file[file_size - 1] = '\0';
570 }
14bab8de
UD
571
572 if (file != problem)
573 {
574 char *p;
49891c10 575 runp = file + strspn (file, ": \t\n");
14bab8de
UD
576 while ((p = strsep (&runp, ": \t\n")) != NULL)
577 {
2bcf29ba 578 struct link_map *new_map = _dl_map_object (main_map, p, 1,
bd355af0
UD
579 lt_library, 0);
580 if (new_map->l_opencount == 1)
581 /* It is no duplicate. */
582 ++npreloads;
49891c10
UD
583
584 if (runp != NULL)
585 runp += strspn (runp, ": \t\n");
14bab8de
UD
586 }
587 }
588
589 if (problem != NULL)
590 {
591 char *p = strndupa (problem, file_size - (problem - file));
2bcf29ba 592 struct link_map *new_map = _dl_map_object (main_map, p, 1,
c6222ab9 593 lt_library, 0);
bd355af0
UD
594 if (new_map->l_opencount == 1)
595 /* It is no duplicate. */
596 ++npreloads;
14bab8de
UD
597 }
598
599 /* We don't need the file anymore. */
600 __munmap (file, file_size);
601 }
602
14bab8de
UD
603 if (npreloads != 0)
604 {
605 /* Set up PRELOADS with a vector of the preloaded libraries. */
606 struct link_map *l;
14bab8de
UD
607 preloads = __alloca (npreloads * sizeof preloads[0]);
608 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
609 i = 0;
610 do
611 {
612 preloads[i++] = l;
613 l = l->l_next;
614 } while (l);
615 assert (i == npreloads);
616 }
617
2064087b
RM
618 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
619 specified some libraries to load, these are inserted before the actual
620 dependencies in the executable's searchlist for symbol resolution. */
ceb2d9aa 621 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
d66e34cd 622
2064087b 623#ifndef MAP_ANON
f332db02
RM
624 /* We are done mapping things, so close the zero-fill descriptor. */
625 __close (_dl_zerofd);
626 _dl_zerofd = -1;
2064087b 627#endif
f332db02 628
f9496a7b
RM
629 /* Remove _dl_rtld_map from the chain. */
630 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
631 if (_dl_rtld_map.l_next)
632 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
633
634 if (_dl_rtld_map.l_opencount)
0200214b 635 {
f9496a7b
RM
636 /* Some DT_NEEDED entry referred to the interpreter object itself, so
637 put it back in the list of visible objects. We insert it into the
638 chain in symbol search order because gdb uses the chain's order as
639 its symbol search order. */
77aba05b 640 i = 1;
ceb2d9aa 641 while (main_map->l_searchlist[i] != &_dl_rtld_map)
f9496a7b 642 ++i;
ceb2d9aa
UD
643 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
644 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
645 main_map->l_searchlist[i + 1] : NULL);
f9496a7b
RM
646 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
647 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
4d02a5b1 648 if (_dl_rtld_map.l_next)
f9496a7b
RM
649 {
650 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
651 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
652 }
0200214b 653 }
d66e34cd 654
c84142e8
UD
655 /* Now let us see whether all libraries are available in the
656 versions we need. */
657 {
993b3242
UD
658 struct version_check_args args;
659 args.doexit = mode == normal;
660 args.main_map = main_map;
661 _dl_receive_error (print_missing_version, version_check_doit, &args);
c84142e8
UD
662 }
663
2de99474 664 if (mode != normal)
0200214b
RM
665 {
666 /* We were run just to list the shared libraries. It is
667 important that we do this before real relocation, because the
668 functions we call below for output may no longer work properly
669 after relocation. */
0200214b
RM
670 if (! _dl_loaded->l_info[DT_NEEDED])
671 _dl_sysdep_message ("\t", "statically linked\n", NULL);
672 else
ceb2d9aa
UD
673 {
674 struct link_map *l;
675
676 for (l = _dl_loaded->l_next; l; l = l->l_next)
677 if (l->l_opencount == 0)
678 /* The library was not found. */
679 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
680 NULL);
681 else
682 {
683 char buf[20], *bp;
684 buf[sizeof buf - 1] = '\0';
af6f3906 685 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
ceb2d9aa
UD
686 while ((size_t) (&buf[sizeof buf - 1] - bp)
687 < sizeof l->l_addr * 2)
688 *--bp = '0';
689 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
690 l->l_name, " (0x", bp, ")\n", NULL);
691 }
692 }
1a3a58fd 693
2de99474 694 if (mode != trace)
cddcfecf
RM
695 for (i = 1; i < _dl_argc; ++i)
696 {
697 const ElfW(Sym) *ref = NULL;
698 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
699 &_dl_default_scope[2],
dcf0671d 700 "argument",
a2b08ee5 701 ELF_MACHINE_JMP_SLOT);
cddcfecf
RM
702 char buf[20], *bp;
703 buf[sizeof buf - 1] = '\0';
af6f3906 704 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
14bab8de 705 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
cddcfecf
RM
706 *--bp = '0';
707 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
708 buf[sizeof buf - 1] = '\0';
af6f3906 709 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
14bab8de 710 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
cddcfecf
RM
711 *--bp = '0';
712 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
713 }
ce37fa88 714 else
fd26970f 715 {
ce37fa88
UD
716 if (lazy >= 0)
717 {
718 /* We have to do symbol dependency testing. */
719 struct relocate_args args;
720 struct link_map *l;
993b3242 721
ce37fa88 722 args.lazy = lazy;
fd26970f 723
ce37fa88
UD
724 l = _dl_loaded;
725 while (l->l_next)
726 l = l->l_next;
727 do
728 {
729 if (l != &_dl_rtld_map && l->l_opencount > 0)
730 {
731 args.l = l;
732 _dl_receive_error (print_unresolved, relocate_doit,
733 &args);
734 *_dl_global_scope_end = NULL;
735 }
736 l = l->l_prev;
737 } while (l);
738 }
739
740#define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
120b4c49 741 if (version_info)
fd26970f 742 {
ce37fa88
UD
743 /* Print more information. This means here, print information
744 about the versions needed. */
745 int first = 1;
746 struct link_map *map = _dl_loaded;
747
748 for (map = _dl_loaded; map != NULL; map = map->l_next)
fd26970f 749 {
f41c8091 750 const char *strtab;
ce37fa88 751 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
f41c8091
UD
752 ElfW(Verneed) *ent;
753
754 if (dyn == NULL)
755 continue;
756
757 strtab = (const char *)
758 (map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
759 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
ce37fa88 760
f41c8091 761 if (first)
ce37fa88 762 {
f41c8091
UD
763 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
764 first = 0;
765 }
ce37fa88 766
f41c8091
UD
767 _dl_sysdep_message ("\t", (map->l_name[0]
768 ? map->l_name : _dl_argv[0]),
769 ":\n", NULL);
770
771 while (1)
772 {
773 ElfW(Vernaux) *aux;
774 struct link_map *needed;
ce37fa88 775
f41c8091
UD
776 needed = find_needed (strtab + ent->vn_file);
777 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
ce37fa88
UD
778
779 while (1)
780 {
f41c8091
UD
781 const char *fname = NULL;
782
783 _dl_sysdep_message ("\t\t",
784 strtab + ent->vn_file,
785 " (", strtab + aux->vna_name,
786 ") ",
787 (aux->vna_flags
788 & VER_FLG_WEAK
789 ? "[WEAK] " : ""),
790 "=> ", NULL);
791
792 if (needed != NULL
793 && match_version (strtab+aux->vna_name, needed))
794 fname = needed->l_name;
795
796 _dl_sysdep_message (fname ?: "not found", "\n",
797 NULL);
ce37fa88 798
f41c8091
UD
799 if (aux->vna_next == 0)
800 /* No more symbols. */
ce37fa88
UD
801 break;
802
f41c8091
UD
803 /* Next symbol. */
804 aux = (ElfW(Vernaux) *) ((char *) aux
805 + aux->vna_next);
ce37fa88 806 }
f41c8091
UD
807
808 if (ent->vn_next == 0)
809 /* No more dependencies. */
810 break;
811
812 /* Next dependency. */
813 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
ce37fa88 814 }
fd26970f 815 }
ce37fa88 816 }
fd26970f 817 }
d66e34cd 818
0200214b
RM
819 _exit (0);
820 }
86d2c878 821
ba79d61b
RM
822 {
823 /* Now we have all the objects loaded. Relocate them all except for
824 the dynamic linker itself. We do this in reverse order so that copy
825 relocs of earlier objects overwrite the data written by later
826 objects. We do not re-relocate the dynamic linker itself in this
827 loop because that could result in the GOT entries for functions we
828 call being changed, and that would break us. It is safe to relocate
829 the dynamic linker out of order because it has no copy relocs (we
830 know that because it is self-contained). */
831
ceb2d9aa 832 struct link_map *l;
ba79d61b
RM
833 l = _dl_loaded;
834 while (l->l_next)
835 l = l->l_next;
836 do
837 {
838 if (l != &_dl_rtld_map)
839 {
840 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
841 *_dl_global_scope_end = NULL;
842 }
843 l = l->l_prev;
844 } while (l);
845
846 /* Do any necessary cleanups for the startup OS interface code.
847 We do these now so that no calls are made after rtld re-relocation
848 which might be resolved to different functions than we expect.
849 We cannot do this before relocating the other objects because
850 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
851 _dl_sysdep_start_cleanup ();
852
853 if (_dl_rtld_map.l_opencount > 0)
854 /* There was an explicit ref to the dynamic linker as a shared lib.
855 Re-relocate ourselves with user-controlled symbol definitions. */
856 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
857 }
ac16e905 858
4d6acc61
RM
859 {
860 /* Initialize _r_debug. */
861 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
ceb2d9aa 862 struct link_map *l;
4d6acc61
RM
863
864 l = _dl_loaded;
ec42724d
RM
865
866#ifdef ELF_MACHINE_DEBUG_SETUP
867
868 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
869
870 ELF_MACHINE_DEBUG_SETUP (l, r);
871 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
872
873#else
874
4d6acc61
RM
875 if (l->l_info[DT_DEBUG])
876 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
877 with the run-time address of the r_debug structure */
878 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
879
d746b89c
RM
880 /* Fill in the pointer in the dynamic linker's own dynamic section, in
881 case you run gdb on the dynamic linker directly. */
882 if (_dl_rtld_map.l_info[DT_DEBUG])
883 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
884
ec42724d
RM
885#endif
886
4d6acc61
RM
887 /* Notify the debugger that all objects are now mapped in. */
888 r->r_state = RT_ADD;
889 _dl_debug_state ();
890 }
0200214b 891
3996f34b
UD
892 /* Now enable profiling if needed. */
893 if (_dl_profile_map != NULL)
894 /* We must prepare the profiling. */
895 _dl_start_profile (_dl_profile_map, _dl_profile_output);
896
d66e34cd
RM
897 /* Once we return, _dl_sysdep_start will invoke
898 the DT_INIT functions and then *USER_ENTRY. */
899}
fd26970f
UD
900\f
901/* This is a little helper function for resolving symbols while
902 tracing the binary. */
903static void
c84142e8
UD
904print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
905 const char *errstring)
fd26970f 906{
3996f34b
UD
907 if (objname[0] == '\0')
908 objname = _dl_argv[0] ?: "<main program>";
fd26970f
UD
909 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
910}
c84142e8
UD
911\f
912/* This is a little helper function for resolving symbols while
913 tracing the binary. */
914static void
915print_missing_version (int errcode __attribute__ ((unused)),
916 const char *objname, const char *errstring)
917{
918 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
919 objname, ": ", errstring, "\n", NULL);
920}
ea278354 921\f
7dea968e
UD
922/* Nonzero if any of the debugging options is enabled. */
923static int any_debug;
924
b5efde2f
UD
925/* Process the string given as the parameter which explains which debugging
926 options are enabled. */
927static void
14c44e2e 928process_dl_debug (const char *dl_debug)
b5efde2f 929{
14c44e2e
UD
930 size_t len;
931#define separators " ,:"
b5efde2f
UD
932 do
933 {
14c44e2e 934 len = 0;
b5efde2f 935 /* Skip separating white spaces and commas. */
14c44e2e 936 dl_debug += strspn (dl_debug, separators);
b5efde2f
UD
937 if (*dl_debug != '\0')
938 {
14c44e2e
UD
939 len = strcspn (dl_debug, separators);
940
941 switch (len)
b5efde2f 942 {
14c44e2e
UD
943 case 4:
944 if (memcmp (dl_debug, "help", 4) == 0)
945 {
946 _dl_sysdep_message ("\
08b511e6 947Valid options for the LD_DEBUG environment variable are:\n\
b5efde2f 948\n\
0c367d92 949 bindings display information about symbol binding\n\
8193034b 950 files display processing of files and libraries\n\
0c367d92
UD
951 help display this help message and exit\n\
952 libs display library search paths\n\
8193034b 953 reloc display relocation processing\n\
de100ca7 954 symbols display symbol table processing\n\
8193034b 955 versions display version dependencies\n\
0c367d92
UD
956\n\
957To direct the debugging output into a file instead of standard output\n\
958a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
959 NULL);
14c44e2e
UD
960 _exit (0);
961 }
77aba05b 962
14c44e2e
UD
963 if (memcmp (dl_debug, "libs", 4) == 0)
964 {
965 _dl_debug_libs = 1;
966 _dl_debug_impcalls = 1;
967 any_debug = 1;
968 continue;
969 }
970 break;
971
972 case 5:
973 if (memcmp (dl_debug, "reloc", 5) == 0)
974 {
975 _dl_debug_reloc = 1;
976 _dl_debug_impcalls = 1;
977 any_debug = 1;
978 continue;
979 }
980
981 if (memcmp (dl_debug, "files", 5) == 0)
982 {
983 _dl_debug_files = 1;
984 _dl_debug_impcalls = 1;
985 any_debug = 1;
986 continue;
987 }
988 break;
77aba05b 989
14c44e2e
UD
990 case 7:
991 if (memcmp (dl_debug, "symbols", 7) == 0)
992 {
993 _dl_debug_symbols = 1;
994 _dl_debug_impcalls = 1;
995 any_debug = 1;
996 continue;
997 }
998 break;
77aba05b 999
14c44e2e
UD
1000 case 8:
1001 if (memcmp (dl_debug, "bindings", 8) == 0)
1002 {
1003 _dl_debug_bindings = 1;
1004 _dl_debug_impcalls = 1;
1005 any_debug = 1;
1006 continue;
1007 }
1008
1009 if (memcmp (dl_debug, "versions", 8) == 0)
1010 {
1011 _dl_debug_versions = 1;
1012 _dl_debug_impcalls = 1;
1013 any_debug = 1;
1014 continue;
1015 }
1016 break;
1017
1018 default:
1019 break;
77aba05b 1020 }
14c44e2e
UD
1021
1022 {
1023 /* Display a warning and skip everything until next separator. */
1024 char *startp = strndupa (dl_debug, len);
1025 _dl_sysdep_error ("warning: debug option `", startp,
1026 "' unknown; try LD_DEBUG=help\n", NULL);
1027 }
b5efde2f
UD
1028 }
1029 }
14c44e2e 1030 while (*(dl_debug += len) != '\0');
b5efde2f
UD
1031}
1032\f
ea278354
UD
1033/* Process all environments variables the dynamic linker must recognize.
1034 Since all of them start with `LD_' we are a bit smarter while finding
1035 all the entries. */
1036static void
1037process_envvars (enum mode *modep, int *lazyp)
1038{
1039 char **runp = NULL;
1040 char *envline;
1041 enum mode mode = normal;
1042 int bind_now = 0;
7dea968e 1043 char *debug_output = NULL;
ea278354
UD
1044
1045 /* This is the default place for profiling data file. */
1046 _dl_profile_output = "/var/tmp";
1047
1048 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1049 {
14c44e2e 1050 size_t len = strcspn (envline, "=") - 3;
ea278354 1051
14c44e2e 1052 switch (len)
ea278354 1053 {
14c44e2e
UD
1054 case 4:
1055 /* Warning level, verbose or not. */
1056 if (memcmp (&envline[3], "WARN", 4) == 0)
1057 _dl_verbose = envline[8] != '\0';
1058 break;
ea278354 1059
14c44e2e
UD
1060 case 5:
1061 /* Debugging of the dynamic linker? */
1062 if (memcmp (&envline[3], "DEBUG", 5) == 0)
1063 process_dl_debug (&envline[9]);
1064 break;
b5efde2f 1065
14c44e2e
UD
1066 case 7:
1067 /* Print information about versions. */
1068 if (memcmp (&envline[3], "VERBOSE", 7) == 0)
1069 {
1070 version_info = envline[11] != '\0';
1071 break;
1072 }
7dea968e 1073
14c44e2e
UD
1074 /* List of objects to be preloaded. */
1075 if (memcmp (&envline[3], "PRELOAD", 7) == 0)
1076 {
1077 preloadlist = &envline[11];
1078 break;
1079 }
120b4c49 1080
14c44e2e
UD
1081 /* Which shared object shall be profiled. */
1082 if (memcmp (&envline[3], "PROFILE", 7) == 0)
1083 {
1084 _dl_profile = &envline[11];
1085 if (*_dl_profile == '\0')
1086 _dl_profile = NULL;
1087 }
1088 break;
120b4c49 1089
14c44e2e
UD
1090 case 8:
1091 /* Do we bind early? */
1092 if (memcmp (&envline[3], "BIND_NOW", 8) == 0)
1093 bind_now = 1;
1094 break;
ea278354 1095
14c44e2e
UD
1096 case 9:
1097 /* Test whether we want to see the content of the auxiliary
1098 array passed up from the kernel. */
1099 if (memcmp (&envline[3], "SHOW_AUXV", 9) == 0)
1100 _dl_show_auxv ();
1101 break;
ea278354 1102
14c44e2e
UD
1103 case 12:
1104 /* Where to place the profiling data file. */
1105 if (memcmp (&envline[3], "DEBUG_OUTPUT", 12) == 0)
1106 {
1107 debug_output = &envline[16];
1108 break;
1109 }
ea278354 1110
14c44e2e
UD
1111 /* The library search path. */
1112 if (memcmp (&envline[3], "LIBRARY_PATH", 12) == 0)
1113 library_path = &envline[16];
1114 break;
ea278354 1115
14c44e2e
UD
1116 case 14:
1117 /* Where to place the profiling data file. */
1118 if (memcmp (&envline[3], "PROFILE_OUTPUT", 14) == 0)
1119 {
1120 _dl_profile_output = &envline[18];
1121 if (*_dl_profile_output == '\0')
1122 _dl_profile_output = "/var/tmp";
1123 }
1124 break;
120b4c49 1125
14c44e2e
UD
1126 case 20:
1127 /* The mode of the dynamic linker can be set. */
1128 if (memcmp (&envline[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1129 mode = trace;
1130 break;
ea278354
UD
1131 }
1132 }
1133
7dea968e
UD
1134 /* If we have to run the dynamic linker in debugging mode and the
1135 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1136 messages to this file. */
14c44e2e 1137 if (any_debug && debug_output != NULL && !__libc_enable_secure)
7dea968e
UD
1138 {
1139 _dl_debug_fd = __open (debug_output, O_WRONLY | O_APPEND | O_CREAT,
1140 0666);
1141 if (_dl_debug_fd == -1)
1142 /* We use standard output if opening the file failed. */
1143 _dl_debug_fd = STDOUT_FILENO;
1144 }
1145
ea278354
UD
1146 /* LAZY is determined by the environment variable LD_WARN and
1147 LD_BIND_NOW if we trace the binary. */
1148 if (mode == trace)
1149 *lazyp = _dl_verbose ? !bind_now : -1;
1150 else
1151 *lazyp = !__libc_enable_secure && !bind_now;
1152
1153 *modep = mode;
1154}