Update.
[glibc.git] / elf / rtld.c
blob95830c54c213cb0c2a0980a607f7845d25767831
1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <link.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
26 #include <stdio-common/_itoa.h>
27 #include <assert.h>
28 #include <entry.h>
29 #include "dynamic-link.h"
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
34 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
35 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
36 extern 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));
40 extern void _dl_sysdep_start_cleanup (void);
42 /* System-dependent function to read a file's whole contents
43 in the most convenient manner available. */
44 extern void *_dl_sysdep_read_whole_file (const char *filename,
45 size_t *filesize_ptr,
46 int mmap_prot);
48 /* Helper function to handle errors while resolving symbols. */
49 static void print_unresolved (int errcode, const char *objname,
50 const char *errsting);
52 /* Helper function to handle errors when a version is missing. */
53 static void print_missing_version (int errcode, const char *objname,
54 const char *errsting);
56 int _dl_argc;
57 char **_dl_argv;
58 const char *_dl_rpath;
59 int _dl_verbose;
60 const char *_dl_platform;
61 size_t _dl_platformlen;
62 unsigned long _dl_hwcap;
63 struct r_search_path *_dl_search_paths;
64 const char *_dl_profile;
65 const char *_dl_profile_output;
66 struct link_map *_dl_profile_map;
68 /* Set nonzero during loading and initialization of executable and
69 libraries, cleared before the executable's entry point runs. This
70 must not be initialized to nonzero, because the unused dynamic
71 linker loaded in for libc.so's "ld.so.1" dep will provide the
72 definition seen by libc.so's initializer; that value must be zero,
73 and will be since that dynamic linker's _dl_start and dl_main will
74 never be called. */
75 int _dl_starting_up;
77 static void dl_main (const ElfW(Phdr) *phdr,
78 ElfW(Half) phent,
79 ElfW(Addr) *user_entry);
81 struct link_map _dl_rtld_map;
82 struct libname_list _dl_rtld_libname;
83 struct libname_list _dl_rtld_libname2;
85 #ifdef RTLD_START
86 RTLD_START
87 #else
88 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
89 #endif
91 static ElfW(Addr)
92 _dl_start (void *arg)
94 struct link_map bootstrap_map;
96 /* This #define produces dynamic linking inline functions for
97 bootstrap relocation instead of general-purpose relocation. */
98 #define RTLD_BOOTSTRAP
99 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
100 #include "dynamic-link.h"
102 /* Figure out the run-time load address of the dynamic linker itself. */
103 bootstrap_map.l_addr = elf_machine_load_address ();
105 /* Read our own dynamic section and fill in the info array. */
106 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
107 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
109 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
110 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
111 #endif
113 /* Relocate ourselves so we can do normal function calls and
114 data access using the global offset table. */
116 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
117 /* Please note that we don't allow profiling of this object and
118 therefore need not test whether we have to allocate the array
119 for the relocation results (as done in dl-reloc.c). */
121 /* Now life is sane; we can call functions and access global data.
122 Set up to use the operating system facilities, and find out from
123 the operating system's program loader where to find the program
124 header table in core. */
126 /* Transfer data about ourselves to the permanent link_map structure. */
127 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
128 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
129 _dl_rtld_map.l_opencount = 1;
130 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
131 sizeof _dl_rtld_map.l_info);
132 _dl_setup_hash (&_dl_rtld_map);
134 /* Cache the DT_RPATH stored in ld.so itself; this will be
135 the default search path. */
136 if (_dl_rtld_map.l_info[DT_STRTAB] && _dl_rtld_map.l_info[DT_RPATH])
138 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
139 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
140 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
143 /* Call the OS-dependent function to set up life so we can do things like
144 file access. It will call `dl_main' (below) to do all the real work
145 of the dynamic linker, and then unwind our frame and run the user
146 entry point on the same stack we entered on. */
147 return _dl_sysdep_start (arg, &dl_main);
151 /* Now life is peachy; we can do all normal operations.
152 On to the real work. */
154 void ENTRY_POINT (void);
156 /* Some helper functions. */
158 /* Arguments to relocate_doit. */
159 struct relocate_args
161 struct link_map *l;
162 int lazy;
165 struct map_args
167 /* Argument to map_doit. */
168 char *str;
169 /* Return value of map_doit. */
170 struct link_map *main_map;
173 /* Arguments to version_check_doit. */
174 struct version_check_args
176 struct link_map *main_map;
177 int doexit;
180 static void
181 relocate_doit (void *a)
183 struct relocate_args *args = (struct relocate_args *) a;
185 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
186 args->lazy);
189 static void
190 map_doit (void *a)
192 struct map_args *args = (struct map_args *)a;
193 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
196 static void
197 version_check_doit (void *a)
199 struct version_check_args *args = (struct version_check_args *)a;
200 if (_dl_check_all_versions (args->main_map, 1) && args->doexit)
201 /* We cannot start the application. Abort now. */
202 _exit (1);
206 static inline struct link_map *
207 find_needed (const char *name)
209 unsigned int n;
211 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
212 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
213 return _dl_loaded->l_searchlist[n];
215 /* Should never happen. */
216 return NULL;
219 static int
220 match_version (const char *string, struct link_map *map)
222 const char *strtab = (const char *) (map->l_addr
223 + map->l_info[DT_STRTAB]->d_un.d_ptr);
224 ElfW(Verdef) *def;
226 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
227 if (map->l_info[VERDEFTAG] == NULL)
228 /* The file has no symbol versioning. */
229 return 0;
231 def = (ElfW(Verdef) *) ((char *) map->l_addr
232 + map->l_info[VERDEFTAG]->d_un.d_ptr);
233 while (1)
235 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
237 /* Compare the version strings. */
238 if (strcmp (string, strtab + aux->vda_name) == 0)
239 /* Bingo! */
240 return 1;
242 /* If no more definitions we failed to find what we want. */
243 if (def->vd_next == 0)
244 break;
246 /* Next definition. */
247 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
250 return 0;
253 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
255 static void
256 dl_main (const ElfW(Phdr) *phdr,
257 ElfW(Half) phent,
258 ElfW(Addr) *user_entry)
260 const ElfW(Phdr) *ph;
261 struct link_map *main_map;
262 int lazy;
263 enum { normal, list, verify, trace } mode;
264 struct link_map **preloads;
265 unsigned int npreloads;
266 const char *preloadlist;
267 size_t file_size;
268 char *file;
269 int has_interp = 0;
271 /* Test whether we want to see the content of the auxiliary array passed
272 up from the kernel. */
273 if (getenv ("LD_SHOW_AUXV") != NULL)
274 _dl_show_auxv ();
276 mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal;
277 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
279 /* LAZY is determined by the environment variable LD_WARN and
280 LD_BIND_NOW if we trace the binary. */
281 if (mode == trace)
282 lazy = (_dl_verbose
283 ? (*(getenv ("LD_BIND_NOW") ?: "") == '\0' ? 1 : 0) : -1);
284 else
285 lazy = !__libc_enable_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
287 /* See whether we want to use profiling. */
288 _dl_profile = getenv ("LD_PROFILE");
289 if (_dl_profile != NULL)
290 if (_dl_profile[0] == '\0')
291 /* An empty string is of not much help. Disable profiling. */
292 _dl_profile = NULL;
293 else
295 /* OK, we have the name of a shared object we want to
296 profile. It's up to the user to provide a good name, it
297 must match the file name or soname of one of the loaded
298 objects. Now let's see where we are supposed to place the
299 result. */
300 _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
302 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
303 /* This is the default place. */
304 _dl_profile_output = "/var/tmp";
307 /* Set up a flag which tells we are just starting. */
308 _dl_starting_up = 1;
310 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
312 /* Ho ho. We are not the program interpreter! We are the program
313 itself! This means someone ran ld.so as a command. Well, that
314 might be convenient to do sometimes. We support it by
315 interpreting the args like this:
317 ld.so PROGRAM ARGS...
319 The first argument is the name of a file containing an ELF
320 executable we will load and run with the following arguments.
321 To simplify life here, PROGRAM is searched for using the
322 normal rules for shared objects, rather than $PATH or anything
323 like that. We just load it and use its entry point; we don't
324 pay attention to its PT_INTERP command (we are the interpreter
325 ourselves). This is an easy way to test a new ld.so before
326 installing it. */
328 /* Overwrites LD_LIBRARY_PATH if given. */
329 const char *library_path = NULL;
331 /* Note the place where the dynamic linker actually came from. */
332 _dl_rtld_map.l_name = _dl_argv[0];
334 while (_dl_argc > 1)
335 if (! strcmp (_dl_argv[1], "--list"))
337 mode = list;
338 lazy = -1; /* This means do no dependency analysis. */
340 ++_dl_skip_args;
341 --_dl_argc;
342 ++_dl_argv;
344 else if (! strcmp (_dl_argv[1], "--verify"))
346 mode = verify;
348 ++_dl_skip_args;
349 --_dl_argc;
350 ++_dl_argv;
352 else if (! strcmp (_dl_argv[1], "--library-path")
353 && _dl_argc > 2)
355 library_path = _dl_argv[2];
357 _dl_skip_args += 2;
358 _dl_argc -= 2;
359 _dl_argv += 2;
361 else
362 break;
364 /* If we have no further argument the program was called incorrectly.
365 Grant the user some education. */
366 if (_dl_argc < 2)
367 _dl_sysdep_fatal ("\
368 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
369 You have invoked `ld.so', the helper program for shared library executables.\n\
370 This program usually lives in the file `/lib/ld.so', and special directives\n\
371 in executable files using ELF shared libraries tell the system's program\n\
372 loader to load the helper program from this file. This helper program loads\n\
373 the shared libraries needed by the program executable, prepares the program\n\
374 to run, and runs it. You may invoke this helper program directly from the\n\
375 command line to load and run an ELF executable file; this is like executing\n\
376 that file itself, but always uses this helper program from the file you\n\
377 specified, instead of the helper program file specified in the executable\n\
378 file you run. This is mostly of use for maintainers to test new versions\n\
379 of this helper program; chances are you did not intend to run this program.\n",
380 NULL);
382 ++_dl_skip_args;
383 --_dl_argc;
384 ++_dl_argv;
386 /* Initialize the data structures for the search paths for shared
387 objects. */
388 _dl_init_paths (library_path);
390 if (mode == verify)
392 char *err_str = NULL;
393 const char *obj_name __attribute__ ((unused));
394 struct map_args args;
396 args.str = _dl_argv[0];
397 (void) _dl_catch_error (&err_str, &obj_name, map_doit, &args);
398 main_map = args.main_map;
399 if (err_str != NULL)
401 free (err_str);
402 _exit (EXIT_FAILURE);
405 else
406 main_map = _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
408 phdr = main_map->l_phdr;
409 phent = main_map->l_phnum;
410 main_map->l_name = (char *) "";
411 *user_entry = main_map->l_entry;
413 else
415 /* Create a link_map for the executable itself.
416 This will be what dlopen on "" returns. */
417 main_map = _dl_new_object ((char *) "", "", lt_executable);
418 if (main_map == NULL)
419 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
420 main_map->l_phdr = phdr;
421 main_map->l_phnum = phent;
422 main_map->l_entry = *user_entry;
423 main_map->l_opencount = 1;
425 /* Initialize the data structures for the search paths for shared
426 objects. */
427 _dl_init_paths (NULL);
430 /* Scan the program header table for the dynamic section. */
431 for (ph = phdr; ph < &phdr[phent]; ++ph)
432 switch (ph->p_type)
434 case PT_PHDR:
435 /* Find out the load address. */
436 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
437 break;
438 case PT_DYNAMIC:
439 /* This tells us where to find the dynamic section,
440 which tells us everything we need to do. */
441 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
442 break;
443 case PT_INTERP:
444 /* This "interpreter segment" was used by the program loader to
445 find the program interpreter, which is this program itself, the
446 dynamic linker. We note what name finds us, so that a future
447 dlopen call or DT_NEEDED entry, for something that wants to link
448 against the dynamic linker as a shared library, will know that
449 the shared object is already loaded. */
450 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
451 _dl_rtld_libname.next = NULL;
452 _dl_rtld_map.l_libname = &_dl_rtld_libname;
454 /* Ordinarilly, we would get additional names for the loader from
455 our DT_SONAME. This can't happen if we were actually linked as
456 a static executable (detect this case when we have no DYNAMIC).
457 If so, assume the filename component of the interpreter path to
458 be our SONAME, and add it to our name list. */
459 if (_dl_rtld_map.l_ld == NULL)
461 char *p = strrchr (_dl_rtld_libname.name, '/');
462 if (p)
464 _dl_rtld_libname2.name = p+1;
465 _dl_rtld_libname2.next = NULL;
466 _dl_rtld_libname.next = &_dl_rtld_libname2;
470 has_interp = 1;
471 break;
473 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
475 /* We were invoked directly, so the program might not have a
476 PT_INTERP. */
477 _dl_rtld_libname.name = _dl_rtld_map.l_name;
478 _dl_rtld_libname.next = NULL;
479 _dl_rtld_map.l_libname = &_dl_rtld_libname;
481 else
482 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
484 if (mode == verify)
485 /* We were called just to verify that this is a dynamic executable
486 using us as the program interpreter. */
487 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
489 /* Extract the contents of the dynamic section for easy access. */
490 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
491 if (main_map->l_info[DT_HASH])
492 /* Set up our cache of pointers into the hash table. */
493 _dl_setup_hash (main_map);
495 /* Put the link_map for ourselves on the chain so it can be found by
496 name. Note that at this point the global chain of link maps contains
497 exactly one element, which is pointed to by main_map. */
498 if (! _dl_rtld_map.l_name)
499 /* If not invoked directly, the dynamic linker shared object file was
500 found by the PT_INTERP name. */
501 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
502 _dl_rtld_map.l_type = lt_library;
503 main_map->l_next = &_dl_rtld_map;
504 _dl_rtld_map.l_prev = main_map;
506 /* We have two ways to specify objects to preload: via environment
507 variable and via the file /etc/ld.so.preload. The later can also
508 be used when security is enabled. */
509 preloads = NULL;
510 npreloads = 0;
512 preloadlist = getenv ("LD_PRELOAD");
513 if (preloadlist)
515 /* The LD_PRELOAD environment variable gives list of libraries
516 separated by white space or colons that are loaded before the
517 executable's dependencies and prepended to the global scope
518 list. If the binary is running setuid all elements
519 containing a '/' are ignored since it is insecure. */
520 char *list = strdupa (preloadlist);
521 char *p;
522 while ((p = strsep (&list, " :")) != NULL)
523 if (! __libc_enable_secure || strchr (p, '/') == NULL)
525 struct link_map *new_map = _dl_map_object (NULL, p, 1,
526 lt_library, 0);
527 if (new_map->l_opencount == 1)
528 /* It is no duplicate. */
529 ++npreloads;
533 /* Read the contents of the file. */
534 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
535 PROT_READ | PROT_WRITE);
536 if (file)
538 /* Parse the file. It contains names of libraries to be loaded,
539 separated by white spaces or `:'. It may also contain
540 comments introduced by `#'. */
541 char *problem;
542 char *runp;
543 size_t rest;
545 /* Eliminate comments. */
546 runp = file;
547 rest = file_size;
548 while (rest > 0)
550 char *comment = memchr (runp, '#', rest);
551 if (comment == NULL)
552 break;
554 rest -= comment - runp;
556 *comment = ' ';
557 while (--rest > 0 && *++comment != '\n');
560 /* We have one problematic case: if we have a name at the end of
561 the file without a trailing terminating characters, we cannot
562 place the \0. Handle the case separately. */
563 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
564 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
566 problem = &file[file_size];
567 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
568 && problem[-1] != '\n' && problem[-1] != ':')
569 --problem;
571 if (problem > file)
572 problem[-1] = '\0';
574 else
576 problem = NULL;
577 file[file_size - 1] = '\0';
580 if (file != problem)
582 char *p;
583 runp = file + strspn (file, ": \t\n");
584 while ((p = strsep (&runp, ": \t\n")) != NULL)
586 struct link_map *new_map = _dl_map_object (NULL, p, 1,
587 lt_library, 0);
588 if (new_map->l_opencount == 1)
589 /* It is no duplicate. */
590 ++npreloads;
592 if (runp != NULL)
593 runp += strspn (runp, ": \t\n");
597 if (problem != NULL)
599 char *p = strndupa (problem, file_size - (problem - file));
600 struct link_map *new_map = _dl_map_object (NULL, p, 1,
601 lt_library, 0);
602 if (new_map->l_opencount == 1)
603 /* It is no duplicate. */
604 ++npreloads;
607 /* We don't need the file anymore. */
608 __munmap (file, file_size);
611 if (npreloads != 0)
613 /* Set up PRELOADS with a vector of the preloaded libraries. */
614 struct link_map *l;
615 unsigned int i;
616 preloads = __alloca (npreloads * sizeof preloads[0]);
617 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
618 i = 0;
621 preloads[i++] = l;
622 l = l->l_next;
623 } while (l);
624 assert (i == npreloads);
627 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
628 specified some libraries to load, these are inserted before the actual
629 dependencies in the executable's searchlist for symbol resolution. */
630 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
632 #ifndef MAP_ANON
633 /* We are done mapping things, so close the zero-fill descriptor. */
634 __close (_dl_zerofd);
635 _dl_zerofd = -1;
636 #endif
638 /* Remove _dl_rtld_map from the chain. */
639 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
640 if (_dl_rtld_map.l_next)
641 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
643 if (_dl_rtld_map.l_opencount)
645 /* Some DT_NEEDED entry referred to the interpreter object itself, so
646 put it back in the list of visible objects. We insert it into the
647 chain in symbol search order because gdb uses the chain's order as
648 its symbol search order. */
649 unsigned int i = 1;
650 while (main_map->l_searchlist[i] != &_dl_rtld_map)
651 ++i;
652 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
653 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
654 main_map->l_searchlist[i + 1] : NULL);
655 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
656 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
657 if (_dl_rtld_map.l_next)
659 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
660 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
664 /* Now let us see whether all libraries are available in the
665 versions we need. */
667 struct version_check_args args;
668 args.doexit = mode == normal;
669 args.main_map = main_map;
670 _dl_receive_error (print_missing_version, version_check_doit, &args);
673 if (mode != normal)
675 /* We were run just to list the shared libraries. It is
676 important that we do this before real relocation, because the
677 functions we call below for output may no longer work properly
678 after relocation. */
680 int i;
682 if (! _dl_loaded->l_info[DT_NEEDED])
683 _dl_sysdep_message ("\t", "statically linked\n", NULL);
684 else
686 struct link_map *l;
688 for (l = _dl_loaded->l_next; l; l = l->l_next)
689 if (l->l_opencount == 0)
690 /* The library was not found. */
691 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
692 NULL);
693 else
695 char buf[20], *bp;
696 buf[sizeof buf - 1] = '\0';
697 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
698 while ((size_t) (&buf[sizeof buf - 1] - bp)
699 < sizeof l->l_addr * 2)
700 *--bp = '0';
701 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
702 l->l_name, " (0x", bp, ")\n", NULL);
706 if (mode != trace)
707 for (i = 1; i < _dl_argc; ++i)
709 const ElfW(Sym) *ref = NULL;
710 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
711 &_dl_default_scope[2],
712 "argument",
713 ELF_MACHINE_JMP_SLOT);
714 char buf[20], *bp;
715 buf[sizeof buf - 1] = '\0';
716 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
717 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
718 *--bp = '0';
719 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
720 buf[sizeof buf - 1] = '\0';
721 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
722 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
723 *--bp = '0';
724 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
726 else
728 if (lazy >= 0)
730 /* We have to do symbol dependency testing. */
731 struct relocate_args args;
732 struct link_map *l;
734 args.lazy = lazy;
736 l = _dl_loaded;
737 while (l->l_next)
738 l = l->l_next;
741 if (l != &_dl_rtld_map && l->l_opencount > 0)
743 args.l = l;
744 _dl_receive_error (print_unresolved, relocate_doit,
745 &args);
746 *_dl_global_scope_end = NULL;
748 l = l->l_prev;
749 } while (l);
752 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
753 if (*(getenv ("LD_VERBOSE") ?: "") != '\0')
755 /* Print more information. This means here, print information
756 about the versions needed. */
757 int first = 1;
758 struct link_map *map = _dl_loaded;
760 for (map = _dl_loaded; map != NULL; map = map->l_next)
762 const char *strtab;
763 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
764 ElfW(Verneed) *ent;
766 if (dyn == NULL)
767 continue;
769 strtab = (const char *)
770 (map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
771 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
773 if (first)
775 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
776 first = 0;
779 _dl_sysdep_message ("\t", (map->l_name[0]
780 ? map->l_name : _dl_argv[0]),
781 ":\n", NULL);
783 while (1)
785 ElfW(Vernaux) *aux;
786 struct link_map *needed;
788 needed = find_needed (strtab + ent->vn_file);
789 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
791 while (1)
793 const char *fname = NULL;
795 _dl_sysdep_message ("\t\t",
796 strtab + ent->vn_file,
797 " (", strtab + aux->vna_name,
798 ") ",
799 (aux->vna_flags
800 & VER_FLG_WEAK
801 ? "[WEAK] " : ""),
802 "=> ", NULL);
804 if (needed != NULL
805 && match_version (strtab+aux->vna_name, needed))
806 fname = needed->l_name;
808 _dl_sysdep_message (fname ?: "not found", "\n",
809 NULL);
811 if (aux->vna_next == 0)
812 /* No more symbols. */
813 break;
815 /* Next symbol. */
816 aux = (ElfW(Vernaux) *) ((char *) aux
817 + aux->vna_next);
820 if (ent->vn_next == 0)
821 /* No more dependencies. */
822 break;
824 /* Next dependency. */
825 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
831 _exit (0);
835 /* Now we have all the objects loaded. Relocate them all except for
836 the dynamic linker itself. We do this in reverse order so that copy
837 relocs of earlier objects overwrite the data written by later
838 objects. We do not re-relocate the dynamic linker itself in this
839 loop because that could result in the GOT entries for functions we
840 call being changed, and that would break us. It is safe to relocate
841 the dynamic linker out of order because it has no copy relocs (we
842 know that because it is self-contained). */
844 struct link_map *l;
845 l = _dl_loaded;
846 while (l->l_next)
847 l = l->l_next;
850 if (l != &_dl_rtld_map)
852 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
853 *_dl_global_scope_end = NULL;
855 l = l->l_prev;
856 } while (l);
858 /* Do any necessary cleanups for the startup OS interface code.
859 We do these now so that no calls are made after rtld re-relocation
860 which might be resolved to different functions than we expect.
861 We cannot do this before relocating the other objects because
862 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
863 _dl_sysdep_start_cleanup ();
865 if (_dl_rtld_map.l_opencount > 0)
866 /* There was an explicit ref to the dynamic linker as a shared lib.
867 Re-relocate ourselves with user-controlled symbol definitions. */
868 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
872 /* Initialize _r_debug. */
873 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
874 struct link_map *l;
876 l = _dl_loaded;
878 #ifdef ELF_MACHINE_DEBUG_SETUP
880 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
882 ELF_MACHINE_DEBUG_SETUP (l, r);
883 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
885 #else
887 if (l->l_info[DT_DEBUG])
888 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
889 with the run-time address of the r_debug structure */
890 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
892 /* Fill in the pointer in the dynamic linker's own dynamic section, in
893 case you run gdb on the dynamic linker directly. */
894 if (_dl_rtld_map.l_info[DT_DEBUG])
895 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
897 #endif
899 /* Notify the debugger that all objects are now mapped in. */
900 r->r_state = RT_ADD;
901 _dl_debug_state ();
904 /* Now enable profiling if needed. */
905 if (_dl_profile_map != NULL)
906 /* We must prepare the profiling. */
907 _dl_start_profile (_dl_profile_map, _dl_profile_output);
909 /* Once we return, _dl_sysdep_start will invoke
910 the DT_INIT functions and then *USER_ENTRY. */
913 /* This is a little helper function for resolving symbols while
914 tracing the binary. */
915 static void
916 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
917 const char *errstring)
919 if (objname[0] == '\0')
920 objname = _dl_argv[0] ?: "<main program>";
921 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
924 /* This is a little helper function for resolving symbols while
925 tracing the binary. */
926 static void
927 print_missing_version (int errcode __attribute__ ((unused)),
928 const char *objname, const char *errstring)
930 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
931 objname, ": ", errstring, "\n", NULL);