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