]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Run time dynamic linker. |
0c5ecdc4 | 2 | Copyright (C) 1995, 1996, 1997 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. */ |
21ee7166 | 26 | #include "../stdio-common/_itoa.h" |
b1dbbaa4 | 27 | #include <assert.h> |
f5348425 RM |
28 | #include "dynamic-link.h" |
29 | ||
30 | ||
d66e34cd RM |
31 | /* System-specific function to do initial startup for the dynamic linker. |
32 | After this, file access calls and getenv must work. This is responsible | |
cddcfecf | 33 | for setting __libc_enable_secure if we need to be secure (e.g. setuid), |
d66e34cd | 34 | and for setting _dl_argc and _dl_argv, and then calling _dl_main. */ |
266180eb RM |
35 | extern ElfW(Addr) _dl_sysdep_start (void **start_argptr, |
36 | void (*dl_main) (const ElfW(Phdr) *phdr, | |
37 | ElfW(Half) phent, | |
38 | ElfW(Addr) *user_entry)); | |
4cb20290 | 39 | extern void _dl_sysdep_start_cleanup (void); |
d66e34cd | 40 | |
14bab8de UD |
41 | /* System-dependent function to read a file's whole contents |
42 | in the most convenient manner available. */ | |
43 | extern void *_dl_sysdep_read_whole_file (const char *filename, | |
44 | size_t *filesize_ptr, | |
45 | int mmap_prot); | |
46 | ||
fd26970f UD |
47 | /* Helper function to handle errors while resolving symbols. */ |
48 | static void print_unresolved (const char *errstring, const char *objname); | |
49 | ||
50 | ||
d66e34cd RM |
51 | int _dl_argc; |
52 | char **_dl_argv; | |
4cb20290 | 53 | const char *_dl_rpath; |
d66e34cd | 54 | |
39778c6c UD |
55 | /* Set nonzero during loading and initialization of executable and |
56 | libraries, cleared before the executable's entry point runs. This | |
57 | must not be initialized to nonzero, because the unused dynamic | |
58 | linker loaded in for libc.so's "ld.so.1" dep will provide the | |
59 | definition seen by libc.so's initializer; that value must be zero, | |
60 | and will be since that dynamic linker's _dl_start and dl_main will | |
61 | never be called. */ | |
62 | int _dl_starting_up; | |
63 | ||
266180eb RM |
64 | static void dl_main (const ElfW(Phdr) *phdr, |
65 | ElfW(Half) phent, | |
66 | ElfW(Addr) *user_entry); | |
d66e34cd | 67 | |
ee188d55 | 68 | struct link_map _dl_rtld_map; |
86d2c878 | 69 | |
b1dbbaa4 RM |
70 | #ifdef RTLD_START |
71 | RTLD_START | |
72 | #else | |
73 | #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START" | |
74 | #endif | |
75 | ||
266180eb | 76 | ElfW(Addr) |
d66e34cd RM |
77 | _dl_start (void *arg) |
78 | { | |
86d2c878 | 79 | struct link_map bootstrap_map; |
d66e34cd | 80 | |
b1dbbaa4 RM |
81 | /* This #define produces dynamic linking inline functions for |
82 | bootstrap relocation instead of general-purpose relocation. */ | |
83 | #define RTLD_BOOTSTRAP | |
706074a5 | 84 | #define RESOLVE(sym, flags) bootstrap_map.l_addr |
b1dbbaa4 RM |
85 | #include "dynamic-link.h" |
86 | ||
d66e34cd | 87 | /* Figure out the run-time load address of the dynamic linker itself. */ |
86d2c878 | 88 | bootstrap_map.l_addr = elf_machine_load_address (); |
d66e34cd | 89 | |
47707456 UD |
90 | /* Read our own dynamic section and fill in the info array. */ |
91 | bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); | |
86d2c878 | 92 | elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info); |
d66e34cd RM |
93 | |
94 | #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC | |
86d2c878 | 95 | ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info); |
d66e34cd RM |
96 | #endif |
97 | ||
98 | /* Relocate ourselves so we can do normal function calls and | |
99 | data access using the global offset table. */ | |
421f82e5 | 100 | |
f51d1dfd | 101 | ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0); |
421f82e5 | 102 | |
d66e34cd RM |
103 | |
104 | /* Now life is sane; we can call functions and access global data. | |
105 | Set up to use the operating system facilities, and find out from | |
106 | the operating system's program loader where to find the program | |
107 | header table in core. */ | |
108 | ||
86d2c878 RM |
109 | |
110 | /* Transfer data about ourselves to the permanent link_map structure. */ | |
ee188d55 RM |
111 | _dl_rtld_map.l_addr = bootstrap_map.l_addr; |
112 | _dl_rtld_map.l_ld = bootstrap_map.l_ld; | |
113 | memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info, | |
114 | sizeof _dl_rtld_map.l_info); | |
115 | _dl_setup_hash (&_dl_rtld_map); | |
86d2c878 | 116 | |
4cb20290 RM |
117 | /* Cache the DT_RPATH stored in ld.so itself; this will be |
118 | the default search path. */ | |
ee188d55 RM |
119 | _dl_rpath = (void *) (_dl_rtld_map.l_addr + |
120 | _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr + | |
121 | _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val); | |
d66e34cd RM |
122 | |
123 | /* Call the OS-dependent function to set up life so we can do things like | |
124 | file access. It will call `dl_main' (below) to do all the real work | |
125 | of the dynamic linker, and then unwind our frame and run the user | |
126 | entry point on the same stack we entered on. */ | |
8d6468d0 | 127 | return _dl_sysdep_start (arg, &dl_main); |
d66e34cd RM |
128 | } |
129 | ||
130 | ||
131 | /* Now life is peachy; we can do all normal operations. | |
132 | On to the real work. */ | |
133 | ||
134 | void _start (void); | |
135 | ||
91f62ce6 | 136 | unsigned int _dl_skip_args; /* Nonzero if we were run directly. */ |
a1a9d215 | 137 | |
d66e34cd | 138 | static void |
266180eb RM |
139 | dl_main (const ElfW(Phdr) *phdr, |
140 | ElfW(Half) phent, | |
141 | ElfW(Addr) *user_entry) | |
d66e34cd | 142 | { |
266180eb | 143 | const ElfW(Phdr) *ph; |
efec1d0c | 144 | struct link_map *l; |
0200214b | 145 | int lazy; |
2de99474 | 146 | enum { normal, list, verify, trace } mode; |
2064087b RM |
147 | struct link_map **preloads; |
148 | unsigned int npreloads; | |
fd26970f | 149 | const char *preloadlist; |
14bab8de UD |
150 | size_t file_size; |
151 | char *file; | |
d66e34cd | 152 | |
2de99474 UD |
153 | mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal; |
154 | ||
fd26970f UD |
155 | /* LAZY is determined by the parameters --datadeps and --function-deps |
156 | if we trace the binary. */ | |
157 | if (mode == trace) | |
158 | lazy = -1; | |
159 | else | |
160 | lazy = !__libc_enable_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0'; | |
161 | ||
46ec036d UD |
162 | /* Set up a flag which tells we are just starting. */ |
163 | _dl_starting_up = 1; | |
164 | ||
266180eb | 165 | if (*user_entry == (ElfW(Addr)) &_start) |
0200214b RM |
166 | { |
167 | /* Ho ho. We are not the program interpreter! We are the program | |
168 | itself! This means someone ran ld.so as a command. Well, that | |
169 | might be convenient to do sometimes. We support it by | |
170 | interpreting the args like this: | |
171 | ||
172 | ld.so PROGRAM ARGS... | |
173 | ||
174 | The first argument is the name of a file containing an ELF | |
175 | executable we will load and run with the following arguments. | |
176 | To simplify life here, PROGRAM is searched for using the | |
177 | normal rules for shared objects, rather than $PATH or anything | |
178 | like that. We just load it and use its entry point; we don't | |
179 | pay attention to its PT_INTERP command (we are the interpreter | |
180 | ourselves). This is an easy way to test a new ld.so before | |
181 | installing it. */ | |
182 | if (_dl_argc < 2) | |
183 | _dl_sysdep_fatal ("\ | |
61965e9b | 184 | Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ |
d66e34cd RM |
185 | You have invoked `ld.so', the helper program for shared library executables.\n\ |
186 | This program usually lives in the file `/lib/ld.so', and special directives\n\ | |
187 | in executable files using ELF shared libraries tell the system's program\n\ | |
188 | loader to load the helper program from this file. This helper program loads\n\ | |
189 | the shared libraries needed by the program executable, prepares the program\n\ | |
190 | to run, and runs it. You may invoke this helper program directly from the\n\ | |
191 | command line to load and run an ELF executable file; this is like executing\n\ | |
192 | that file itself, but always uses this helper program from the file you\n\ | |
193 | specified, instead of the helper program file specified in the executable\n\ | |
194 | file you run. This is mostly of use for maintainers to test new versions\n\ | |
5bf62f2d | 195 | of this helper program; chances are you did not intend to run this program.\n", |
0200214b | 196 | NULL); |
421f82e5 | 197 | |
ffee1316 RM |
198 | /* Note the place where the dynamic linker actually came from. */ |
199 | _dl_rtld_map.l_name = _dl_argv[0]; | |
6a76c115 | 200 | |
fd26970f UD |
201 | while (_dl_argc > 1) |
202 | if (! strcmp (_dl_argv[1], "--list")) | |
203 | { | |
204 | mode = list; | |
205 | lazy = -1; /* This means do no dependency analysis. */ | |
61965e9b | 206 | |
fd26970f UD |
207 | ++_dl_skip_args; |
208 | --_dl_argc; | |
209 | ++_dl_argv; | |
210 | } | |
211 | else if (! strcmp (_dl_argv[1], "--verify")) | |
212 | { | |
213 | mode = verify; | |
6a76c115 | 214 | |
fd26970f UD |
215 | ++_dl_skip_args; |
216 | --_dl_argc; | |
217 | ++_dl_argv; | |
218 | } | |
219 | else if (! strcmp (_dl_argv[1], "--data-relocs")) | |
220 | { | |
221 | mode = trace; | |
222 | lazy = 1; /* This means do only data relocation analysis. */ | |
223 | ||
224 | ++_dl_skip_args; | |
225 | --_dl_argc; | |
226 | ++_dl_argv; | |
227 | } | |
228 | else if (! strcmp (_dl_argv[1], "--function-relocs")) | |
229 | { | |
230 | mode = trace; | |
231 | lazy = 0; /* This means do also function relocation analysis. */ | |
232 | ||
233 | ++_dl_skip_args; | |
234 | --_dl_argc; | |
235 | ++_dl_argv; | |
236 | } | |
237 | else | |
238 | break; | |
d66e34cd | 239 | |
0200214b RM |
240 | ++_dl_skip_args; |
241 | --_dl_argc; | |
242 | ++_dl_argv; | |
91f62ce6 | 243 | |
2de99474 UD |
244 | if (mode == verify) |
245 | { | |
246 | void doit (void) | |
247 | { | |
46ec036d | 248 | l = _dl_map_object (NULL, _dl_argv[0], lt_library, 0); |
2de99474 | 249 | } |
dcf0671d | 250 | char *err_str = NULL; |
2de99474 UD |
251 | const char *obj_name __attribute__ ((unused)); |
252 | ||
253 | (void) _dl_catch_error (&err_str, &obj_name, doit); | |
254 | if (err_str != NULL) | |
dcf0671d UD |
255 | { |
256 | free (err_str); | |
257 | _exit (EXIT_FAILURE); | |
258 | } | |
2de99474 UD |
259 | } |
260 | else | |
46ec036d | 261 | l = _dl_map_object (NULL, _dl_argv[0], lt_library, 0); |
2de99474 | 262 | |
0200214b RM |
263 | phdr = l->l_phdr; |
264 | phent = l->l_phnum; | |
265 | l->l_name = (char *) ""; | |
266 | *user_entry = l->l_entry; | |
267 | } | |
268 | else | |
269 | { | |
270 | /* Create a link_map for the executable itself. | |
271 | This will be what dlopen on "" returns. */ | |
2de99474 | 272 | l = _dl_new_object ((char *) "", "", lt_executable); |
d38cd08c UD |
273 | if (l == NULL) |
274 | _dl_sysdep_fatal ("cannot allocate memory for link map", NULL); | |
0200214b RM |
275 | l->l_phdr = phdr; |
276 | l->l_phnum = phent; | |
0200214b RM |
277 | l->l_entry = *user_entry; |
278 | } | |
279 | ||
280 | if (l != _dl_loaded) | |
281 | { | |
282 | /* GDB assumes that the first element on the chain is the | |
283 | link_map for the executable itself, and always skips it. | |
284 | Make sure the first one is indeed that one. */ | |
285 | l->l_prev->l_next = l->l_next; | |
286 | if (l->l_next) | |
287 | l->l_next->l_prev = l->l_prev; | |
288 | l->l_prev = NULL; | |
289 | l->l_next = _dl_loaded; | |
290 | _dl_loaded->l_prev = l; | |
291 | _dl_loaded = l; | |
292 | } | |
293 | ||
294 | /* Scan the program header table for the dynamic section. */ | |
295 | for (ph = phdr; ph < &phdr[phent]; ++ph) | |
296 | switch (ph->p_type) | |
297 | { | |
298 | case PT_DYNAMIC: | |
299 | /* This tells us where to find the dynamic section, | |
300 | which tells us everything we need to do. */ | |
301 | l->l_ld = (void *) l->l_addr + ph->p_vaddr; | |
302 | break; | |
303 | case PT_INTERP: | |
304 | /* This "interpreter segment" was used by the program loader to | |
305 | find the program interpreter, which is this program itself, the | |
306 | dynamic linker. We note what name finds us, so that a future | |
307 | dlopen call or DT_NEEDED entry, for something that wants to link | |
308 | against the dynamic linker as a shared library, will know that | |
309 | the shared object is already loaded. */ | |
ffee1316 | 310 | _dl_rtld_map.l_libname = (const char *) l->l_addr + ph->p_vaddr; |
0200214b RM |
311 | break; |
312 | } | |
ffee1316 RM |
313 | if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name) |
314 | /* We were invoked directly, so the program might not have a PT_INTERP. */ | |
315 | _dl_rtld_map.l_libname = _dl_rtld_map.l_name; | |
316 | else | |
317 | assert (_dl_rtld_map.l_libname); /* How else did we get here? */ | |
0200214b | 318 | |
61965e9b RM |
319 | if (mode == verify) |
320 | /* We were called just to verify that this is a dynamic executable | |
321 | using us as the program interpreter. */ | |
ba1ffaa1 | 322 | _exit (l->l_ld == NULL ? EXIT_FAILURE : EXIT_SUCCESS); |
61965e9b | 323 | |
0200214b RM |
324 | /* Extract the contents of the dynamic section for easy access. */ |
325 | elf_get_dynamic_info (l->l_ld, l->l_info); | |
326 | if (l->l_info[DT_HASH]) | |
327 | /* Set up our cache of pointers into the hash table. */ | |
328 | _dl_setup_hash (l); | |
329 | ||
0200214b RM |
330 | /* Put the link_map for ourselves on the chain so it can be found by |
331 | name. */ | |
ffee1316 RM |
332 | if (! _dl_rtld_map.l_name) |
333 | /* If not invoked directly, the dynamic linker shared object file was | |
334 | found by the PT_INTERP name. */ | |
335 | _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname; | |
ba79d61b | 336 | _dl_rtld_map.l_type = lt_library; |
0200214b RM |
337 | while (l->l_next) |
338 | l = l->l_next; | |
ee188d55 RM |
339 | l->l_next = &_dl_rtld_map; |
340 | _dl_rtld_map.l_prev = l; | |
0200214b | 341 | |
14bab8de UD |
342 | /* We have two ways to specify objects to preload: via environment |
343 | variable and via the file /etc/ld.so.preload. The later can also | |
344 | be used when security is enabled. */ | |
2064087b RM |
345 | preloads = NULL; |
346 | npreloads = 0; | |
14bab8de | 347 | |
fd26970f UD |
348 | preloadlist = getenv ("LD_PRELOAD"); |
349 | if (preloadlist) | |
c4029823 | 350 | { |
fd26970f UD |
351 | /* The LD_PRELOAD environment variable gives a white space |
352 | separated list of libraries that are loaded before the | |
353 | executable's dependencies and prepended to the global scope | |
354 | list. If the binary is running setuid all elements | |
355 | containing a '/' are ignored since it is insecure. */ | |
356 | char *list = strdupa (preloadlist); | |
357 | char *p; | |
358 | while ((p = strsep (&list, " ")) != NULL) | |
359 | if (! __libc_enable_secure || strchr (p, '/') == NULL) | |
360 | { | |
361 | (void) _dl_map_object (NULL, p, lt_library, 0); | |
362 | ++npreloads; | |
363 | } | |
c4029823 UD |
364 | } |
365 | ||
14bab8de UD |
366 | /* Read the contents of the file. */ |
367 | file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size, | |
368 | PROT_READ | PROT_WRITE); | |
369 | if (file) | |
370 | { | |
371 | /* Parse the file. It contains names of libraries to be loaded, | |
372 | separated by white spaces or `:'. It may also contain | |
373 | comments introduced by `#'. */ | |
374 | char *problem; | |
375 | char *runp; | |
376 | size_t rest; | |
377 | ||
378 | /* Eliminate comments. */ | |
379 | runp = file; | |
380 | rest = file_size; | |
381 | while (rest > 0) | |
382 | { | |
383 | char *comment = memchr (runp, '#', rest); | |
384 | if (comment == NULL) | |
385 | break; | |
386 | ||
387 | rest -= comment - runp; | |
388 | do | |
389 | *comment = ' '; | |
390 | while (--rest > 0 && *++comment != '\n'); | |
391 | } | |
392 | ||
393 | /* We have one problematic case: if we have a name at the end of | |
394 | the file without a trailing terminating characters, we cannot | |
395 | place the \0. Handle the case separately. */ | |
396 | if (file[file_size - 1] != ' ' && file[file_size] != '\t' | |
397 | && file[file_size] != '\n') | |
398 | { | |
399 | problem = &file[file_size]; | |
400 | while (problem > file && problem[-1] != ' ' && problem[-1] != '\t' | |
401 | && problem[-1] != '\n') | |
402 | --problem; | |
403 | ||
404 | if (problem > file) | |
405 | problem[-1] = '\0'; | |
406 | } | |
407 | else | |
408 | problem = NULL; | |
409 | ||
410 | if (file != problem) | |
411 | { | |
412 | char *p; | |
413 | runp = file; | |
414 | while ((p = strsep (&runp, ": \t\n")) != NULL) | |
415 | { | |
46ec036d | 416 | (void) _dl_map_object (NULL, p, lt_library, 0); |
14bab8de UD |
417 | ++npreloads; |
418 | } | |
419 | } | |
420 | ||
421 | if (problem != NULL) | |
422 | { | |
423 | char *p = strndupa (problem, file_size - (problem - file)); | |
46ec036d | 424 | (void) _dl_map_object (NULL, p, lt_library, 0); |
14bab8de UD |
425 | } |
426 | ||
427 | /* We don't need the file anymore. */ | |
428 | __munmap (file, file_size); | |
429 | } | |
430 | ||
14bab8de UD |
431 | if (npreloads != 0) |
432 | { | |
433 | /* Set up PRELOADS with a vector of the preloaded libraries. */ | |
434 | struct link_map *l; | |
435 | unsigned int i; | |
436 | preloads = __alloca (npreloads * sizeof preloads[0]); | |
437 | l = _dl_rtld_map.l_next; /* End of the chain before preloads. */ | |
438 | i = 0; | |
439 | do | |
440 | { | |
441 | preloads[i++] = l; | |
442 | l = l->l_next; | |
443 | } while (l); | |
444 | assert (i == npreloads); | |
445 | } | |
446 | ||
2064087b RM |
447 | /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD |
448 | specified some libraries to load, these are inserted before the actual | |
449 | dependencies in the executable's searchlist for symbol resolution. */ | |
46ec036d | 450 | _dl_map_object_deps (l, preloads, npreloads, mode == trace); |
d66e34cd | 451 | |
2064087b | 452 | #ifndef MAP_ANON |
f332db02 RM |
453 | /* We are done mapping things, so close the zero-fill descriptor. */ |
454 | __close (_dl_zerofd); | |
455 | _dl_zerofd = -1; | |
2064087b | 456 | #endif |
f332db02 | 457 | |
f9496a7b RM |
458 | /* Remove _dl_rtld_map from the chain. */ |
459 | _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next; | |
460 | if (_dl_rtld_map.l_next) | |
461 | _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev; | |
462 | ||
463 | if (_dl_rtld_map.l_opencount) | |
0200214b | 464 | { |
f9496a7b RM |
465 | /* Some DT_NEEDED entry referred to the interpreter object itself, so |
466 | put it back in the list of visible objects. We insert it into the | |
467 | chain in symbol search order because gdb uses the chain's order as | |
468 | its symbol search order. */ | |
469 | unsigned int i = 1; | |
470 | while (l->l_searchlist[i] != &_dl_rtld_map) | |
471 | ++i; | |
472 | _dl_rtld_map.l_prev = l->l_searchlist[i - 1]; | |
473 | _dl_rtld_map.l_next = (i + 1 < l->l_nsearchlist ? | |
474 | l->l_searchlist[i + 1] : NULL); | |
475 | assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next); | |
476 | _dl_rtld_map.l_prev->l_next = &_dl_rtld_map; | |
4d02a5b1 | 477 | if (_dl_rtld_map.l_next) |
f9496a7b RM |
478 | { |
479 | assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev); | |
480 | _dl_rtld_map.l_next->l_prev = &_dl_rtld_map; | |
481 | } | |
0200214b | 482 | } |
d66e34cd | 483 | |
2de99474 | 484 | if (mode != normal) |
0200214b RM |
485 | { |
486 | /* We were run just to list the shared libraries. It is | |
487 | important that we do this before real relocation, because the | |
488 | functions we call below for output may no longer work properly | |
489 | after relocation. */ | |
1a3a58fd | 490 | |
0200214b | 491 | int i; |
fd861379 | 492 | |
0200214b RM |
493 | if (! _dl_loaded->l_info[DT_NEEDED]) |
494 | _dl_sysdep_message ("\t", "statically linked\n", NULL); | |
495 | else | |
496 | for (l = _dl_loaded->l_next; l; l = l->l_next) | |
46ec036d UD |
497 | if (l->l_opencount == 0) |
498 | /* The library was not found. */ | |
499 | _dl_sysdep_message ("\t", l->l_libname, " => not found\n", NULL); | |
500 | else | |
501 | { | |
502 | char buf[20], *bp; | |
503 | buf[sizeof buf - 1] = '\0'; | |
504 | bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0); | |
505 | while ((size_t) (&buf[sizeof buf - 1] - bp) | |
506 | < sizeof l->l_addr * 2) | |
507 | *--bp = '0'; | |
508 | _dl_sysdep_message ("\t", l->l_libname, " => ", l->l_name, | |
509 | " (0x", bp, ")\n", NULL); | |
510 | } | |
1a3a58fd | 511 | |
2de99474 | 512 | if (mode != trace) |
cddcfecf RM |
513 | for (i = 1; i < _dl_argc; ++i) |
514 | { | |
515 | const ElfW(Sym) *ref = NULL; | |
516 | ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref, | |
517 | &_dl_default_scope[2], | |
dcf0671d UD |
518 | "argument", |
519 | DL_LOOKUP_NOPLT); | |
cddcfecf RM |
520 | char buf[20], *bp; |
521 | buf[sizeof buf - 1] = '\0'; | |
522 | bp = _itoa (ref->st_value, &buf[sizeof buf - 1], 16, 0); | |
14bab8de | 523 | while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2) |
cddcfecf RM |
524 | *--bp = '0'; |
525 | _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL); | |
526 | buf[sizeof buf - 1] = '\0'; | |
527 | bp = _itoa (loadbase, &buf[sizeof buf - 1], 16, 0); | |
14bab8de | 528 | while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2) |
cddcfecf RM |
529 | *--bp = '0'; |
530 | _dl_sysdep_message (" in object at 0x", bp, "\n", NULL); | |
531 | } | |
fd26970f UD |
532 | else if (lazy >= 0) |
533 | { | |
534 | /* We have to do symbol dependency testing. */ | |
535 | void doit (void) | |
536 | { | |
537 | _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy); | |
538 | } | |
539 | ||
540 | l = _dl_loaded; | |
541 | while (l->l_next) | |
542 | l = l->l_next; | |
543 | do | |
544 | { | |
545 | if (l != &_dl_rtld_map && l->l_opencount > 0) | |
546 | { | |
547 | _dl_receive_error (print_unresolved, doit); | |
548 | *_dl_global_scope_end = NULL; | |
549 | } | |
550 | l = l->l_prev; | |
551 | } while (l); | |
552 | } | |
d66e34cd | 553 | |
0200214b RM |
554 | _exit (0); |
555 | } | |
86d2c878 | 556 | |
ba79d61b RM |
557 | { |
558 | /* Now we have all the objects loaded. Relocate them all except for | |
559 | the dynamic linker itself. We do this in reverse order so that copy | |
560 | relocs of earlier objects overwrite the data written by later | |
561 | objects. We do not re-relocate the dynamic linker itself in this | |
562 | loop because that could result in the GOT entries for functions we | |
563 | call being changed, and that would break us. It is safe to relocate | |
564 | the dynamic linker out of order because it has no copy relocs (we | |
565 | know that because it is self-contained). */ | |
566 | ||
567 | l = _dl_loaded; | |
568 | while (l->l_next) | |
569 | l = l->l_next; | |
570 | do | |
571 | { | |
572 | if (l != &_dl_rtld_map) | |
573 | { | |
574 | _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy); | |
575 | *_dl_global_scope_end = NULL; | |
576 | } | |
577 | l = l->l_prev; | |
578 | } while (l); | |
579 | ||
580 | /* Do any necessary cleanups for the startup OS interface code. | |
581 | We do these now so that no calls are made after rtld re-relocation | |
582 | which might be resolved to different functions than we expect. | |
583 | We cannot do this before relocating the other objects because | |
584 | _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */ | |
585 | _dl_sysdep_start_cleanup (); | |
586 | ||
587 | if (_dl_rtld_map.l_opencount > 0) | |
588 | /* There was an explicit ref to the dynamic linker as a shared lib. | |
589 | Re-relocate ourselves with user-controlled symbol definitions. */ | |
590 | _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0); | |
591 | } | |
0200214b | 592 | |
4d6acc61 RM |
593 | { |
594 | /* Initialize _r_debug. */ | |
595 | struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr); | |
596 | ||
597 | l = _dl_loaded; | |
ec42724d RM |
598 | |
599 | #ifdef ELF_MACHINE_DEBUG_SETUP | |
600 | ||
601 | /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */ | |
602 | ||
603 | ELF_MACHINE_DEBUG_SETUP (l, r); | |
604 | ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r); | |
605 | ||
606 | #else | |
607 | ||
4d6acc61 RM |
608 | if (l->l_info[DT_DEBUG]) |
609 | /* There is a DT_DEBUG entry in the dynamic section. Fill it in | |
610 | with the run-time address of the r_debug structure */ | |
611 | l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | |
612 | ||
d746b89c RM |
613 | /* Fill in the pointer in the dynamic linker's own dynamic section, in |
614 | case you run gdb on the dynamic linker directly. */ | |
615 | if (_dl_rtld_map.l_info[DT_DEBUG]) | |
616 | _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | |
617 | ||
ec42724d RM |
618 | #endif |
619 | ||
4d6acc61 RM |
620 | /* Notify the debugger that all objects are now mapped in. */ |
621 | r->r_state = RT_ADD; | |
622 | _dl_debug_state (); | |
623 | } | |
0200214b | 624 | |
d66e34cd RM |
625 | /* Once we return, _dl_sysdep_start will invoke |
626 | the DT_INIT functions and then *USER_ENTRY. */ | |
627 | } | |
fd26970f UD |
628 | \f |
629 | /* This is a little helper function for resolving symbols while | |
630 | tracing the binary. */ | |
631 | static void | |
632 | print_unresolved (const char *errstring, const char *objname) | |
633 | { | |
634 | _dl_sysdep_error (errstring, " (", objname, ")\n", NULL); | |
635 | } |