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. */
24 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
25 #include <elf/ldsodefs.h>
26 #include <stdio-common/_itoa.h>
28 #include <fpu_control.h>
29 #include "dynamic-link.h"
30 #include "dl-librecon.h"
34 /* System-specific function to do initial startup for the dynamic linker.
35 After this, file access calls and getenv must work. This is responsible
36 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
37 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
38 extern ElfW(Addr
) _dl_sysdep_start (void **start_argptr
,
39 void (*dl_main
) (const ElfW(Phdr
) *phdr
,
41 ElfW(Addr
) *user_entry
));
42 extern void _dl_sysdep_start_cleanup (void);
44 /* This function is used to unload the cache file if necessary. */
45 extern void _dl_unload_cache (void);
47 /* System-dependent function to read a file's whole contents
48 in the most convenient manner available. */
49 extern void *_dl_sysdep_read_whole_file (const char *filename
,
53 /* Helper function to handle errors while resolving symbols. */
54 static void print_unresolved (int errcode
, const char *objname
,
55 const char *errsting
);
57 /* Helper function to handle errors when a version is missing. */
58 static void print_missing_version (int errcode
, const char *objname
,
59 const char *errsting
);
62 /* This is a list of all the modes the dynamic loader can be in. */
63 enum mode
{ normal
, list
, verify
, trace
};
65 /* Process all environments variables the dynamic linker must recognize.
66 Since all of them start with `LD_' we are a bit smarter while finding
68 static void process_envvars (enum mode
*modep
, int *lazyp
);
72 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
74 const char *_dl_platform
;
75 size_t _dl_platformlen
;
76 unsigned long _dl_hwcap
;
77 fpu_control_t _dl_fpu_control
= _FPU_DEFAULT
;
78 struct r_search_path
*_dl_search_paths
;
79 const char *_dl_profile
;
80 const char *_dl_profile_output
;
81 struct link_map
*_dl_profile_map
;
83 int _dl_debug_impcalls
;
84 int _dl_debug_bindings
;
85 int _dl_debug_symbols
;
86 int _dl_debug_versions
;
89 const char *_dl_inhibit_rpath
; /* RPATH values which should be
91 const char *_dl_origin_path
;
93 /* This is a pointer to the map for the main object and through it to
94 all loaded objects. */
95 struct link_map
*_dl_loaded
;
96 /* Pointer to the l_searchlist element of the link map of the main object. */
97 struct r_scope_elem
*_dl_main_searchlist
;
98 /* Copy of the content of `_dl_main_searchlist'. */
99 struct r_scope_elem _dl_initial_searchlist
;
100 /* Array which is used when looking up in the global scope. */
101 struct r_scope_elem
*_dl_global_scope
[2];
103 /* Set nonzero during loading and initialization of executable and
104 libraries, cleared before the executable's entry point runs. This
105 must not be initialized to nonzero, because the unused dynamic
106 linker loaded in for libc.so's "ld.so.1" dep will provide the
107 definition seen by libc.so's initializer; that value must be zero,
108 and will be since that dynamic linker's _dl_start and dl_main will
113 static void dl_main (const ElfW(Phdr
) *phdr
,
115 ElfW(Addr
) *user_entry
);
117 struct link_map _dl_rtld_map
;
118 struct libname_list _dl_rtld_libname
;
119 struct libname_list _dl_rtld_libname2
;
124 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
128 _dl_start (void *arg
)
130 struct link_map bootstrap_map
;
132 /* This #define produces dynamic linking inline functions for
133 bootstrap relocation instead of general-purpose relocation. */
134 #define RTLD_BOOTSTRAP
135 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
136 #include "dynamic-link.h"
138 /* Figure out the run-time load address of the dynamic linker itself. */
139 bootstrap_map
.l_addr
= elf_machine_load_address ();
141 /* Read our own dynamic section and fill in the info array. */
142 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
143 elf_get_dynamic_info (bootstrap_map
.l_ld
, bootstrap_map
.l_info
);
145 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
146 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
149 /* Relocate ourselves so we can do normal function calls and
150 data access using the global offset table. */
152 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
153 /* Please note that we don't allow profiling of this object and
154 therefore need not test whether we have to allocate the array
155 for the relocation results (as done in dl-reloc.c). */
157 /* Now life is sane; we can call functions and access global data.
158 Set up to use the operating system facilities, and find out from
159 the operating system's program loader where to find the program
160 header table in core. */
162 /* Transfer data about ourselves to the permanent link_map structure. */
163 _dl_rtld_map
.l_addr
= bootstrap_map
.l_addr
;
164 _dl_rtld_map
.l_ld
= bootstrap_map
.l_ld
;
165 _dl_rtld_map
.l_opencount
= 1;
166 memcpy (_dl_rtld_map
.l_info
, bootstrap_map
.l_info
,
167 sizeof _dl_rtld_map
.l_info
);
168 _dl_setup_hash (&_dl_rtld_map
);
170 /* Don't bother trying to work out how ld.so is mapped in memory. */
171 _dl_rtld_map
.l_map_start
= ~0;
172 _dl_rtld_map
.l_map_end
= ~0;
174 /* Call the OS-dependent function to set up life so we can do things like
175 file access. It will call `dl_main' (below) to do all the real work
176 of the dynamic linker, and then unwind our frame and run the user
177 entry point on the same stack we entered on. */
178 return _dl_sysdep_start (arg
, &dl_main
);
181 /* Now life is peachy; we can do all normal operations.
182 On to the real work. */
184 void ENTRY_POINT (void);
186 /* Some helper functions. */
188 /* Arguments to relocate_doit. */
197 /* Argument to map_doit. */
199 /* Return value of map_doit. */
200 struct link_map
*main_map
;
203 /* Arguments to version_check_doit. */
204 struct version_check_args
210 relocate_doit (void *a
)
212 struct relocate_args
*args
= (struct relocate_args
*) a
;
214 _dl_relocate_object (args
->l
, args
->l
->l_scope
,
221 struct map_args
*args
= (struct map_args
*) a
;
222 args
->main_map
= _dl_map_object (NULL
, args
->str
, 0, lt_library
, 0);
226 version_check_doit (void *a
)
228 struct version_check_args
*args
= (struct version_check_args
*) a
;
229 if (_dl_check_all_versions (_dl_loaded
, 1) && args
->doexit
)
230 /* We cannot start the application. Abort now. */
235 static inline struct link_map
*
236 find_needed (const char *name
)
238 unsigned int n
= _dl_loaded
->l_searchlist
.r_nlist
;
241 if (_dl_name_match_p (name
, _dl_loaded
->l_searchlist
.r_list
[n
]))
242 return _dl_loaded
->l_searchlist
.r_list
[n
];
244 /* Should never happen. */
249 match_version (const char *string
, struct link_map
*map
)
251 const char *strtab
= (const char *) (map
->l_addr
252 + map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
255 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
256 if (map
->l_info
[VERDEFTAG
] == NULL
)
257 /* The file has no symbol versioning. */
260 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
261 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
264 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
266 /* Compare the version strings. */
267 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
271 /* If no more definitions we failed to find what we want. */
272 if (def
->vd_next
== 0)
275 /* Next definition. */
276 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
282 static const char *library_path
; /* The library search path. */
283 static const char *preloadlist
; /* The list preloaded objects. */
284 static int version_info
; /* Nonzero if information about
285 versions has to be printed. */
288 dl_main (const ElfW(Phdr
) *phdr
,
290 ElfW(Addr
) *user_entry
)
292 const ElfW(Phdr
) *ph
;
295 struct link_map
**preloads
;
296 unsigned int npreloads
;
301 int paths_initialized
= 0;
303 /* Process the environment variable which control the behaviour. */
304 process_envvars (&mode
, &lazy
);
306 /* Set up a flag which tells we are just starting. */
309 if (*user_entry
== (ElfW(Addr
)) &ENTRY_POINT
)
311 /* Ho ho. We are not the program interpreter! We are the program
312 itself! This means someone ran ld.so as a command. Well, that
313 might be convenient to do sometimes. We support it by
314 interpreting the args like this:
316 ld.so PROGRAM ARGS...
318 The first argument is the name of a file containing an ELF
319 executable we will load and run with the following arguments.
320 To simplify life here, PROGRAM is searched for using the
321 normal rules for shared objects, rather than $PATH or anything
322 like that. We just load it and use its entry point; we don't
323 pay attention to its PT_INTERP command (we are the interpreter
324 ourselves). This is an easy way to test a new ld.so before
327 /* Note the place where the dynamic linker actually came from. */
328 _dl_rtld_map
.l_name
= _dl_argv
[0];
331 if (! strcmp (_dl_argv
[1], "--list"))
334 lazy
= -1; /* This means do no dependency analysis. */
340 else if (! strcmp (_dl_argv
[1], "--verify"))
348 else if (! strcmp (_dl_argv
[1], "--library-path") && _dl_argc
> 2)
350 library_path
= _dl_argv
[2];
356 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath") && _dl_argc
> 2)
358 _dl_inhibit_rpath
= _dl_argv
[2];
367 /* If we have no further argument the program was called incorrectly.
368 Grant the user some education. */
371 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
372 You have invoked `ld.so', the helper program for shared library executables.\n\
373 This program usually lives in the file `/lib/ld.so', and special directives\n\
374 in executable files using ELF shared libraries tell the system's program\n\
375 loader to load the helper program from this file. This helper program loads\n\
376 the shared libraries needed by the program executable, prepares the program\n\
377 to run, and runs it. You may invoke this helper program directly from the\n\
378 command line to load and run an ELF executable file; this is like executing\n\
379 that file itself, but always uses this helper program from the file you\n\
380 specified, instead of the helper program file specified in the executable\n\
381 file you run. This is mostly of use for maintainers to test new versions\n\
382 of this helper program; chances are you did not intend to run this program.\n\
384 --list list all dependencies and how they are resolved\n\
385 --verify verify that given object really is a dynamically linked\n\
386 object we get handle\n\
387 --library-path PATH use given PATH instead of content of the environment\n\
388 variable LD_LIBRARY_PATH\n\
389 --inhibit-rpath LIST ignore RPATH information in object names in LIST\n",
396 /* Initialize the data structures for the search paths for shared
398 _dl_init_paths (library_path
);
399 paths_initialized
= 1;
403 char *err_str
= NULL
;
404 struct map_args args
;
406 args
.str
= _dl_argv
[0];
407 (void) _dl_catch_error (&err_str
, map_doit
, &args
);
411 _exit (EXIT_FAILURE
);
415 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0);
417 phdr
= _dl_loaded
->l_phdr
;
418 phent
= _dl_loaded
->l_phnum
;
419 /* We overwrite here a pointer to a malloc()ed string. But since
420 the malloc() implementation used at this point is the dummy
421 implementations which has no real free() function it does not
422 makes sense to free the old string first. */
423 _dl_loaded
->l_name
= (char *) "";
424 *user_entry
= _dl_loaded
->l_entry
;
428 /* Create a link_map for the executable itself.
429 This will be what dlopen on "" returns. */
430 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
431 if (_dl_loaded
== NULL
)
432 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL
);
433 _dl_loaded
->l_phdr
= phdr
;
434 _dl_loaded
->l_phnum
= phent
;
435 _dl_loaded
->l_entry
= *user_entry
;
436 _dl_loaded
->l_opencount
= 1;
438 /* We delay initializing the path structure until we got the dynamic
439 information for the program. */
442 /* It is not safe to load stuff after the main program. */
443 _dl_loaded
->l_map_end
= ~0;
444 /* Perhaps the executable has no PT_LOAD header entries at all. */
445 _dl_loaded
->l_map_start
= ~0;
447 /* Scan the program header table for the dynamic section. */
448 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
452 /* Find out the load address. */
453 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
456 /* This tells us where to find the dynamic section,
457 which tells us everything we need to do. */
458 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
461 /* This "interpreter segment" was used by the program loader to
462 find the program interpreter, which is this program itself, the
463 dynamic linker. We note what name finds us, so that a future
464 dlopen call or DT_NEEDED entry, for something that wants to link
465 against the dynamic linker as a shared library, will know that
466 the shared object is already loaded. */
467 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
469 _dl_rtld_libname
.next
= NULL
;
470 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
472 /* Ordinarilly, we would get additional names for the loader from
473 our DT_SONAME. This can't happen if we were actually linked as
474 a static executable (detect this case when we have no DYNAMIC).
475 If so, assume the filename component of the interpreter path to
476 be our SONAME, and add it to our name list. */
477 if (_dl_rtld_map
.l_ld
== NULL
)
479 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
482 _dl_rtld_libname2
.name
= p
+1;
483 _dl_rtld_libname2
.next
= NULL
;
484 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
491 /* Remember where the main program starts in memory. */
494 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
495 if (_dl_loaded
->l_map_start
> mapstart
)
496 _dl_loaded
->l_map_start
= mapstart
;
500 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
502 /* We were invoked directly, so the program might not have a
504 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
505 _dl_rtld_libname
.next
= NULL
;
506 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
509 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
511 /* Extract the contents of the dynamic section for easy access. */
512 elf_get_dynamic_info (_dl_loaded
->l_ld
, _dl_loaded
->l_info
);
513 if (_dl_loaded
->l_info
[DT_HASH
])
514 /* Set up our cache of pointers into the hash table. */
515 _dl_setup_hash (_dl_loaded
);
519 /* We were called just to verify that this is a dynamic
520 executable using us as the program interpreter. Exit with an
521 error if we were not able to load the binary or no interpreter
522 is specified (i.e., this is no dynamically linked binary. */
523 if (_dl_loaded
->l_ld
== NULL
)
526 /* We allow here some platform specific code. */
527 #ifdef DISTINGUISH_LIB_VERSIONS
528 DISTINGUISH_LIB_VERSIONS
;
530 _exit (has_interp
? 0 : 2);
533 if (! paths_initialized
)
534 /* Initialize the data structures for the search paths for shared
536 _dl_init_paths (library_path
);
538 /* Put the link_map for ourselves on the chain so it can be found by
539 name. Note that at this point the global chain of link maps contains
540 exactly one element, which is pointed to by _dl_loaded. */
541 if (! _dl_rtld_map
.l_name
)
542 /* If not invoked directly, the dynamic linker shared object file was
543 found by the PT_INTERP name. */
544 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
545 _dl_rtld_map
.l_type
= lt_library
;
546 _dl_loaded
->l_next
= &_dl_rtld_map
;
547 _dl_rtld_map
.l_prev
= _dl_loaded
;
549 /* We have two ways to specify objects to preload: via environment
550 variable and via the file /etc/ld.so.preload. The later can also
551 be used when security is enabled. */
557 /* The LD_PRELOAD environment variable gives list of libraries
558 separated by white space or colons that are loaded before the
559 executable's dependencies and prepended to the global scope
560 list. If the binary is running setuid all elements
561 containing a '/' are ignored since it is insecure. */
562 char *list
= strdupa (preloadlist
);
564 while ((p
= strsep (&list
, " :")) != NULL
)
566 && (! __libc_enable_secure
|| strchr (p
, '/') == NULL
))
568 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
570 if (new_map
->l_opencount
== 1)
571 /* It is no duplicate. */
576 /* Read the contents of the file. */
577 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
578 PROT_READ
| PROT_WRITE
);
581 /* Parse the file. It contains names of libraries to be loaded,
582 separated by white spaces or `:'. It may also contain
583 comments introduced by `#'. */
588 /* Eliminate comments. */
593 char *comment
= memchr (runp
, '#', rest
);
597 rest
-= comment
- runp
;
600 while (--rest
> 0 && *++comment
!= '\n');
603 /* We have one problematic case: if we have a name at the end of
604 the file without a trailing terminating characters, we cannot
605 place the \0. Handle the case separately. */
606 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
607 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
609 problem
= &file
[file_size
];
610 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
611 && problem
[-1] != '\n' && problem
[-1] != ':')
620 file
[file_size
- 1] = '\0';
627 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
630 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
632 if (new_map
->l_opencount
== 1)
633 /* It is no duplicate. */
640 char *p
= strndupa (problem
, file_size
- (problem
- file
));
641 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
643 if (new_map
->l_opencount
== 1)
644 /* It is no duplicate. */
648 /* We don't need the file anymore. */
649 __munmap (file
, file_size
);
654 /* Set up PRELOADS with a vector of the preloaded libraries. */
656 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
657 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
664 assert (i
== npreloads
);
667 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
668 specified some libraries to load, these are inserted before the actual
669 dependencies in the executable's searchlist for symbol resolution. */
670 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
, 0);
672 /* Mark all objects as being in the global scope. */
673 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
674 _dl_loaded
->l_searchlist
.r_list
[--i
]->l_global
= 1;
677 /* We are done mapping things, so close the zero-fill descriptor. */
678 __close (_dl_zerofd
);
682 /* Remove _dl_rtld_map from the chain. */
683 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
684 if (_dl_rtld_map
.l_next
)
685 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
687 if (_dl_rtld_map
.l_opencount
> 1)
689 /* Some DT_NEEDED entry referred to the interpreter object itself, so
690 put it back in the list of visible objects. We insert it into the
691 chain in symbol search order because gdb uses the chain's order as
692 its symbol search order. */
694 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
696 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
697 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
698 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
700 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
701 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
702 if (_dl_rtld_map
.l_next
)
704 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
705 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
709 /* Now let us see whether all libraries are available in the
712 struct version_check_args args
;
713 args
.doexit
= mode
== normal
;
714 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
719 /* We were run just to list the shared libraries. It is
720 important that we do this before real relocation, because the
721 functions we call below for output may no longer work properly
723 if (! _dl_loaded
->l_info
[DT_NEEDED
])
724 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
729 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
730 if (l
->l_opencount
== 0)
731 /* The library was not found. */
732 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
737 buf
[sizeof buf
- 1] = '\0';
738 bp
= _itoa_word (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
739 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
740 < sizeof l
->l_addr
* 2)
742 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
743 l
->l_name
, " (0x", bp
, ")\n", NULL
);
748 for (i
= 1; i
< _dl_argc
; ++i
)
750 const ElfW(Sym
) *ref
= NULL
;
751 ElfW(Addr
) loadbase
= _dl_lookup_symbol (_dl_argv
[i
], &ref
,
754 ELF_MACHINE_JMP_SLOT
);
756 buf
[sizeof buf
- 1] = '\0';
757 bp
= _itoa_word (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
758 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
760 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
761 buf
[sizeof buf
- 1] = '\0';
762 bp
= _itoa_word (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
763 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
765 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
771 /* We have to do symbol dependency testing. */
772 struct relocate_args args
;
782 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
785 _dl_receive_error (print_unresolved
, relocate_doit
,
792 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
795 /* Print more information. This means here, print information
796 about the versions needed. */
798 struct link_map
*map
= _dl_loaded
;
800 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
803 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
809 strtab
= (const char *)
810 (map
->l_addr
+ map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
811 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
815 _dl_sysdep_message ("\n\tVersion information:\n", NULL
);
819 _dl_sysdep_message ("\t", (map
->l_name
[0]
820 ? map
->l_name
: _dl_argv
[0]),
826 struct link_map
*needed
;
828 needed
= find_needed (strtab
+ ent
->vn_file
);
829 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
833 const char *fname
= NULL
;
835 _dl_sysdep_message ("\t\t",
836 strtab
+ ent
->vn_file
,
837 " (", strtab
+ aux
->vna_name
,
845 && match_version (strtab
+aux
->vna_name
, needed
))
846 fname
= needed
->l_name
;
848 _dl_sysdep_message (fname
?: "not found", "\n",
851 if (aux
->vna_next
== 0)
852 /* No more symbols. */
856 aux
= (ElfW(Vernaux
) *) ((char *) aux
860 if (ent
->vn_next
== 0)
861 /* No more dependencies. */
864 /* Next dependency. */
865 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
875 /* Now we have all the objects loaded. Relocate them all except for
876 the dynamic linker itself. We do this in reverse order so that copy
877 relocs of earlier objects overwrite the data written by later
878 objects. We do not re-relocate the dynamic linker itself in this
879 loop because that could result in the GOT entries for functions we
880 call being changed, and that would break us. It is safe to relocate
881 the dynamic linker out of order because it has no copy relocs (we
882 know that because it is self-contained). */
885 int consider_profiling
= _dl_profile
!= NULL
;
887 /* If we are profiling we also must do lazy reloaction. */
888 lazy
|= consider_profiling
;
895 if (l
!= &_dl_rtld_map
)
896 _dl_relocate_object (l
, l
->l_scope
, lazy
, consider_profiling
);
901 /* Do any necessary cleanups for the startup OS interface code.
902 We do these now so that no calls are made after rtld re-relocation
903 which might be resolved to different functions than we expect.
904 We cannot do this before relocating the other objects because
905 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
906 _dl_sysdep_start_cleanup ();
908 if (_dl_rtld_map
.l_opencount
> 0)
909 /* There was an explicit ref to the dynamic linker as a shared lib.
910 Re-relocate ourselves with user-controlled symbol definitions. */
911 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
914 /* Now set up the variable which helps the assembler startup code. */
915 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
916 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
918 /* Safe the information about the original global scope list since
919 we need it in the memory handling later. */
920 _dl_initial_searchlist
= *_dl_main_searchlist
;
923 /* Initialize _r_debug. */
924 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
929 #ifdef ELF_MACHINE_DEBUG_SETUP
931 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
933 ELF_MACHINE_DEBUG_SETUP (l
, r
);
934 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
938 if (l
->l_info
[DT_DEBUG
])
939 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
940 with the run-time address of the r_debug structure */
941 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
943 /* Fill in the pointer in the dynamic linker's own dynamic section, in
944 case you run gdb on the dynamic linker directly. */
945 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
946 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
950 /* Notify the debugger that all objects are now mapped in. */
956 /* We must munmap() the cache file. */
960 /* Now enable profiling if needed. */
961 if (_dl_profile_map
!= NULL
)
962 /* We must prepare the profiling. */
963 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
965 /* Once we return, _dl_sysdep_start will invoke
966 the DT_INIT functions and then *USER_ENTRY. */
969 /* This is a little helper function for resolving symbols while
970 tracing the binary. */
972 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
973 const char *errstring
)
975 if (objname
[0] == '\0')
976 objname
= _dl_argv
[0] ?: "<main program>";
977 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
980 /* This is a little helper function for resolving symbols while
981 tracing the binary. */
983 print_missing_version (int errcode
__attribute__ ((unused
)),
984 const char *objname
, const char *errstring
)
986 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
987 objname
, ": ", errstring
, "\n", NULL
);
990 /* Nonzero if any of the debugging options is enabled. */
991 static int any_debug
;
993 /* Process the string given as the parameter which explains which debugging
994 options are enabled. */
996 process_dl_debug (const char *dl_debug
)
999 #define separators " ,:"
1003 /* Skip separating white spaces and commas. */
1004 dl_debug
+= strspn (dl_debug
, separators
);
1005 if (*dl_debug
!= '\0')
1007 len
= strcspn (dl_debug
, separators
);
1012 /* This option is not documented since it is not generally
1014 if (memcmp (dl_debug
, "all", 3) == 0)
1017 _dl_debug_impcalls
= 1;
1018 _dl_debug_reloc
= 1;
1019 _dl_debug_files
= 1;
1020 _dl_debug_symbols
= 1;
1021 _dl_debug_bindings
= 1;
1022 _dl_debug_versions
= 1;
1029 if (memcmp (dl_debug
, "help", 4) == 0)
1031 _dl_sysdep_message ("\
1032 Valid options for the LD_DEBUG environment variable are:\n\
1034 bindings display information about symbol binding\n\
1035 files display processing of files and libraries\n\
1036 help display this help message and exit\n\
1037 libs display library search paths\n\
1038 reloc display relocation processing\n\
1039 symbols display symbol table processing\n\
1040 versions display version dependencies\n\
1042 To direct the debugging output into a file instead of standard output\n\
1043 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1048 if (memcmp (dl_debug
, "libs", 4) == 0)
1051 _dl_debug_impcalls
= 1;
1058 if (memcmp (dl_debug
, "reloc", 5) == 0)
1060 _dl_debug_reloc
= 1;
1061 _dl_debug_impcalls
= 1;
1066 if (memcmp (dl_debug
, "files", 5) == 0)
1068 _dl_debug_files
= 1;
1069 _dl_debug_impcalls
= 1;
1076 if (memcmp (dl_debug
, "symbols", 7) == 0)
1078 _dl_debug_symbols
= 1;
1079 _dl_debug_impcalls
= 1;
1086 if (memcmp (dl_debug
, "bindings", 8) == 0)
1088 _dl_debug_bindings
= 1;
1089 _dl_debug_impcalls
= 1;
1094 if (memcmp (dl_debug
, "versions", 8) == 0)
1096 _dl_debug_versions
= 1;
1097 _dl_debug_impcalls
= 1;
1108 /* Display a warning and skip everything until next separator. */
1109 char *startp
= strndupa (dl_debug
, len
);
1110 _dl_sysdep_error ("warning: debug option `", startp
,
1111 "' unknown; try LD_DEBUG=help\n", NULL
);
1115 while (*(dl_debug
+= len
) != '\0');
1118 /* Process all environments variables the dynamic linker must recognize.
1119 Since all of them start with `LD_' we are a bit smarter while finding
1122 process_envvars (enum mode
*modep
, int *lazyp
)
1126 enum mode mode
= normal
;
1128 char *debug_output
= NULL
;
1130 /* This is the default place for profiling data file. */
1131 _dl_profile_output
= "/var/tmp";
1133 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1135 size_t len
= strcspn (envline
, "=") - 3;
1140 /* Warning level, verbose or not. */
1141 if (memcmp (&envline
[3], "WARN", 4) == 0)
1142 _dl_verbose
= envline
[8] != '\0';
1146 /* Debugging of the dynamic linker? */
1147 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1148 process_dl_debug (&envline
[9]);
1152 /* Print information about versions. */
1153 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1155 version_info
= envline
[11] != '\0';
1159 /* List of objects to be preloaded. */
1160 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1162 preloadlist
= &envline
[11];
1166 /* Which shared object shall be profiled. */
1167 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1169 _dl_profile
= &envline
[11];
1170 if (*_dl_profile
== '\0')
1176 /* Do we bind early? */
1177 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1178 bind_now
= envline
[12] != '\0';
1182 /* Test whether we want to see the content of the auxiliary
1183 array passed up from the kernel. */
1184 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1189 /* Mask for the important hardware capabilities. */
1190 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1191 _dl_hwcap_mask
= strtoul (&envline
[14], NULL
, 0);
1195 /* Path where the binary is found. */
1196 if (!__libc_enable_secure
1197 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1198 _dl_origin_path
= &envline
[15];
1202 /* Where to place the profiling data file. */
1203 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1205 debug_output
= &envline
[16];
1209 /* The library search path. */
1210 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1211 library_path
= &envline
[16];
1215 /* Where to place the profiling data file. */
1216 if (!__libc_enable_secure
1217 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1219 _dl_profile_output
= &envline
[18];
1220 if (*_dl_profile_output
== '\0')
1221 _dl_profile_output
= "/var/tmp";
1226 /* The mode of the dynamic linker can be set. */
1227 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1231 /* We might have some extra environment variable to handle. This
1232 is tricky due to the pre-processing of the length of the name
1233 in the switch statement here. The code here assumes that added
1234 environment variables have a different length. */
1235 #ifdef EXTRA_LD_ENVVARS
1241 /* Extra security for SUID binaries. Remove all dangerous environment
1243 if (__libc_enable_secure
)
1245 static const char *unsecure_envvars
[] =
1247 #ifdef EXTRA_UNSECURE_ENVVARS
1248 EXTRA_UNSECURE_ENVVARS
1253 if (preloadlist
!= NULL
)
1254 unsetenv ("LD_PRELOAD");
1255 if (library_path
!= NULL
)
1256 unsetenv ("LD_LIBRARY_PATH");
1259 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1261 unsetenv (unsecure_envvars
[cnt
]);
1264 /* If we have to run the dynamic linker in debugging mode and the
1265 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1266 messages to this file. */
1267 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1269 size_t name_len
= strlen (debug_output
);
1270 char buf
[name_len
+ 12];
1273 buf
[name_len
+ 11] = '\0';
1274 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1276 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1278 _dl_debug_fd
= __open (startp
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
1279 if (_dl_debug_fd
== -1)
1280 /* We use standard output if opening the file failed. */
1281 _dl_debug_fd
= STDOUT_FILENO
;
1284 /* LAZY is determined by the environment variable LD_WARN and
1285 LD_BIND_NOW if we trace the binary. */
1287 *lazyp
= _dl_verbose
? !bind_now
: -1;
1289 *lazyp
= !__libc_enable_secure
&& !bind_now
;