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