]>
Commit | Line | Data |
---|---|---|
d66e34cd RM |
1 | /* Run time dynamic linker. |
2 | Copyright (C) 1995 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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 | |
17 | not, write to the Free Software Foundation, Inc., 675 Mass Ave, | |
18 | Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include <link.h> | |
21 | #include "dynamic-link.h" | |
22 | #include <stddef.h> | |
23 | #include <stdlib.h> | |
24 | #include <unistd.h> | |
21ee7166 | 25 | #include "../stdio-common/_itoa.h" |
d66e34cd RM |
26 | |
27 | ||
28 | #ifdef RTLD_START | |
29 | RTLD_START | |
30 | #else | |
31 | #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START" | |
32 | #endif | |
33 | ||
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 _dl_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 Elf32_Addr _dl_sysdep_start (void **start_argptr, | |
39 | void (*dl_main) (const Elf32_Phdr *phdr, | |
40 | Elf32_Word phent, | |
41 | Elf32_Addr *user_entry)); | |
42 | ||
43 | int _dl_secure; | |
44 | int _dl_argc; | |
45 | char **_dl_argv; | |
46 | ||
47 | struct r_debug dl_r_debug; | |
48 | ||
49 | static void dl_main (const Elf32_Phdr *phdr, | |
50 | Elf32_Word phent, | |
51 | Elf32_Addr *user_entry); | |
52 | ||
86d2c878 RM |
53 | static struct link_map rtld_map; |
54 | ||
d66e34cd RM |
55 | Elf32_Addr |
56 | _dl_start (void *arg) | |
57 | { | |
86d2c878 | 58 | struct link_map bootstrap_map; |
d66e34cd RM |
59 | |
60 | /* Figure out the run-time load address of the dynamic linker itself. */ | |
86d2c878 | 61 | bootstrap_map.l_addr = elf_machine_load_address (); |
d66e34cd RM |
62 | |
63 | /* Read our own dynamic section and fill in the info array. | |
64 | Conveniently, the first element of the GOT contains the | |
65 | offset of _DYNAMIC relative to the run-time load address. */ | |
86d2c878 RM |
66 | bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + *elf_machine_got (); |
67 | elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info); | |
d66e34cd RM |
68 | |
69 | #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC | |
86d2c878 | 70 | ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info); |
d66e34cd RM |
71 | #endif |
72 | ||
73 | /* Relocate ourselves so we can do normal function calls and | |
74 | data access using the global offset table. */ | |
421f82e5 | 75 | |
ded29119 RM |
76 | /* We must initialize `l_type' to make sure it is not `lt_interpreter'. |
77 | That is the type to describe us, but not during bootstrapping--it | |
78 | indicates to elf_machine_rel{,a} that we were already relocated during | |
79 | bootstrapping, so it must anti-perform each bootstrapping relocation | |
80 | before applying the final relocation when ld.so is linked in as | |
81 | normal a shared library. */ | |
86d2c878 RM |
82 | bootstrap_map.l_type = lt_library; |
83 | ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, NULL); | |
421f82e5 | 84 | |
d66e34cd RM |
85 | |
86 | /* Now life is sane; we can call functions and access global data. | |
87 | Set up to use the operating system facilities, and find out from | |
88 | the operating system's program loader where to find the program | |
89 | header table in core. */ | |
90 | ||
86d2c878 RM |
91 | |
92 | /* Transfer data about ourselves to the permanent link_map structure. */ | |
93 | rtld_map.l_addr = bootstrap_map.l_addr; | |
94 | rtld_map.l_ld = bootstrap_map.l_ld; | |
95 | memcpy (rtld_map.l_info, bootstrap_map.l_info, sizeof rtld_map.l_info); | |
96 | ||
d66e34cd RM |
97 | |
98 | /* Call the OS-dependent function to set up life so we can do things like | |
99 | file access. It will call `dl_main' (below) to do all the real work | |
100 | of the dynamic linker, and then unwind our frame and run the user | |
101 | entry point on the same stack we entered on. */ | |
102 | return _dl_sysdep_start (&arg, &dl_main); | |
103 | } | |
104 | ||
105 | ||
106 | /* Now life is peachy; we can do all normal operations. | |
107 | On to the real work. */ | |
108 | ||
109 | void _start (void); | |
110 | ||
91f62ce6 | 111 | unsigned int _dl_skip_args; /* Nonzero if we were run directly. */ |
a1a9d215 | 112 | |
d66e34cd RM |
113 | static void |
114 | dl_main (const Elf32_Phdr *phdr, | |
115 | Elf32_Word phent, | |
116 | Elf32_Addr *user_entry) | |
117 | { | |
118 | void doit (void) | |
119 | { | |
421f82e5 RM |
120 | const Elf32_Phdr *ph; |
121 | struct link_map *l; | |
122 | const char *interpreter_name; | |
123 | int lazy; | |
6a76c115 | 124 | int list_only = 0; |
d66e34cd | 125 | |
421f82e5 RM |
126 | if (*user_entry == (Elf32_Addr) &_start) |
127 | { | |
128 | /* Ho ho. We are not the program interpreter! We are the program | |
129 | itself! This means someone ran ld.so as a command. Well, that | |
130 | might be convenient to do sometimes. We support it by | |
131 | interpreting the args like this: | |
86d2c878 | 132 | |
421f82e5 | 133 | ld.so PROGRAM ARGS... |
86d2c878 | 134 | |
421f82e5 RM |
135 | The first argument is the name of a file containing an ELF |
136 | executable we will load and run with the following arguments. | |
137 | To simplify life here, PROGRAM is searched for using the | |
138 | normal rules for shared objects, rather than $PATH or anything | |
139 | like that. We just load it and use its entry point; we don't | |
140 | pay attention to its PT_INTERP command (we are the interpreter | |
141 | ourselves). This is an easy way to test a new ld.so before | |
142 | installing it. */ | |
143 | if (_dl_argc < 2) | |
144 | _dl_sysdep_fatal ("\ | |
6a76c115 | 145 | Usage: ld.so [--list] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ |
d66e34cd RM |
146 | You have invoked `ld.so', the helper program for shared library executables.\n\ |
147 | This program usually lives in the file `/lib/ld.so', and special directives\n\ | |
148 | in executable files using ELF shared libraries tell the system's program\n\ | |
149 | loader to load the helper program from this file. This helper program loads\n\ | |
150 | the shared libraries needed by the program executable, prepares the program\n\ | |
151 | to run, and runs it. You may invoke this helper program directly from the\n\ | |
152 | command line to load and run an ELF executable file; this is like executing\n\ | |
153 | that file itself, but always uses this helper program from the file you\n\ | |
154 | specified, instead of the helper program file specified in the executable\n\ | |
155 | file you run. This is mostly of use for maintainers to test new versions\n\ | |
5bf62f2d RM |
156 | of this helper program; chances are you did not intend to run this program.\n", |
157 | NULL); | |
421f82e5 RM |
158 | |
159 | interpreter_name = _dl_argv[0]; | |
6a76c115 RM |
160 | |
161 | if (! strcmp (_dl_argv[1], "--list")) | |
162 | { | |
163 | list_only = 1; | |
164 | ||
165 | ++_dl_skip_args; | |
166 | --_dl_argc; | |
167 | ++_dl_argv; | |
168 | } | |
169 | ||
170 | ++_dl_skip_args; | |
421f82e5 RM |
171 | --_dl_argc; |
172 | ++_dl_argv; | |
6a76c115 | 173 | |
879bf2e6 | 174 | l = _dl_map_object (NULL, _dl_argv[0]); |
421f82e5 RM |
175 | phdr = l->l_phdr; |
176 | phent = l->l_phnum; | |
177 | l->l_type = lt_executable; | |
178 | l->l_libname = (char *) ""; | |
879bf2e6 | 179 | *user_entry = l->l_entry; |
421f82e5 RM |
180 | } |
181 | else | |
182 | { | |
183 | /* Create a link_map for the executable itself. | |
184 | This will be what dlopen on "" returns. */ | |
185 | l = _dl_new_object ((char *) "", "", lt_executable); | |
186 | l->l_phdr = phdr; | |
187 | l->l_phnum = phent; | |
188 | interpreter_name = 0; | |
879bf2e6 | 189 | l->l_entry = *user_entry; |
421f82e5 | 190 | } |
d66e34cd | 191 | |
91f62ce6 RM |
192 | if (l != _dl_loaded) |
193 | { | |
194 | /* GDB assumes that the first element on the chain is the | |
195 | link_map for the executable itself, and always skips it. | |
196 | Make sure the first one is indeed that one. */ | |
197 | l->l_prev->l_next = l->l_next; | |
198 | if (l->l_next) | |
199 | l->l_next->l_prev = l->l_prev; | |
200 | l->l_prev = NULL; | |
201 | l->l_next = _dl_loaded; | |
202 | _dl_loaded->l_prev = l; | |
203 | _dl_loaded = l; | |
204 | } | |
205 | ||
421f82e5 RM |
206 | /* Scan the program header table for the dynamic section. */ |
207 | for (ph = phdr; ph < &phdr[phent]; ++ph) | |
208 | switch (ph->p_type) | |
209 | { | |
210 | case PT_DYNAMIC: | |
211 | /* This tells us where to find the dynamic section, | |
212 | which tells us everything we need to do. */ | |
a1a9d215 | 213 | l->l_ld = (void *) l->l_addr + ph->p_vaddr; |
421f82e5 RM |
214 | break; |
215 | case PT_INTERP: | |
216 | /* This "interpreter segment" was used by the program loader to | |
217 | find the program interpreter, which is this program itself, the | |
218 | dynamic linker. We note what name finds us, so that a future | |
219 | dlopen call or DT_NEEDED entry, for something that wants to link | |
220 | against the dynamic linker as a shared library, will know that | |
221 | the shared object is already loaded. */ | |
a1a9d215 | 222 | interpreter_name = (void *) l->l_addr + ph->p_vaddr; |
421f82e5 RM |
223 | break; |
224 | } | |
225 | assert (interpreter_name); /* How else did we get here? */ | |
226 | ||
227 | /* Extract the contents of the dynamic section for easy access. */ | |
228 | elf_get_dynamic_info (l->l_ld, l->l_info); | |
229 | /* Set up our cache of pointers into the hash table. */ | |
230 | _dl_setup_hash (l); | |
231 | ||
232 | if (l->l_info[DT_DEBUG]) | |
233 | /* There is a DT_DEBUG entry in the dynamic section. Fill it in | |
234 | with the run-time address of the r_debug structure, which we | |
235 | will set up later to communicate with the debugger. */ | |
236 | l->l_info[DT_DEBUG]->d_un.d_ptr = (Elf32_Addr) &dl_r_debug; | |
237 | ||
86d2c878 RM |
238 | /* Put the link_map for ourselves on the chain so it can be found by |
239 | name. */ | |
240 | rtld_map.l_name = (char *) rtld_map.l_libname = interpreter_name; | |
241 | rtld_map.l_type = lt_interpreter; | |
242 | while (l->l_next) | |
243 | l = l->l_next; | |
244 | l->l_next = &rtld_map; | |
245 | rtld_map.l_prev = l; | |
421f82e5 RM |
246 | |
247 | /* Now process all the DT_NEEDED entries and map in the objects. | |
248 | Each new link_map will go on the end of the chain, so we will | |
249 | come across it later in the loop to map in its dependencies. */ | |
250 | for (l = _dl_loaded; l; l = l->l_next) | |
d66e34cd | 251 | { |
421f82e5 RM |
252 | if (l->l_info[DT_NEEDED]) |
253 | { | |
254 | const char *strtab | |
255 | = (void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr; | |
256 | const Elf32_Dyn *d; | |
257 | for (d = l->l_ld; d->d_tag != DT_NULL; ++d) | |
258 | if (d->d_tag == DT_NEEDED) | |
879bf2e6 | 259 | _dl_map_object (l, strtab + d->d_un.d_val); |
421f82e5 RM |
260 | } |
261 | l->l_deps_loaded = 1; | |
d66e34cd | 262 | } |
d66e34cd | 263 | |
86d2c878 | 264 | if (rtld_map.l_opencount == 0) |
421f82e5 RM |
265 | { |
266 | /* No DT_NEEDED entry referred to the interpreter object itself. | |
267 | Remove it from the maps we will use for symbol resolution. */ | |
86d2c878 RM |
268 | rtld_map.l_prev->l_next = rtld_map.l_next; |
269 | if (rtld_map.l_next) | |
270 | rtld_map.l_next->l_prev = rtld_map.l_prev; | |
421f82e5 | 271 | } |
d66e34cd | 272 | |
a1a9d215 | 273 | lazy = !_dl_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0'; |
d66e34cd | 274 | |
421f82e5 RM |
275 | /* Now we have all the objects loaded. Relocate them all. |
276 | We do this in reverse order so that copy relocs of earlier | |
277 | objects overwrite the data written by later objects. */ | |
278 | l = _dl_loaded; | |
279 | while (l->l_next) | |
280 | l = l->l_next; | |
281 | do | |
282 | { | |
283 | _dl_relocate_object (l, lazy); | |
284 | l = l->l_prev; | |
285 | } while (l); | |
286 | ||
287 | /* Tell the debugger where to find the map of loaded objects. */ | |
288 | dl_r_debug.r_version = 1 /* R_DEBUG_VERSION XXX */; | |
86d2c878 | 289 | dl_r_debug.r_ldbase = rtld_map.l_addr; /* Record our load address. */ |
421f82e5 RM |
290 | dl_r_debug.r_map = _dl_loaded; |
291 | dl_r_debug.r_brk = (Elf32_Addr) &_dl_r_debug_state; | |
6a76c115 RM |
292 | |
293 | if (list_only) | |
294 | { | |
b122c703 RM |
295 | if (! _dl_loaded->l_info[DT_NEEDED]) |
296 | { | |
297 | _dl_sysdep_message (_dl_loaded->l_name, ": statically linked\n", | |
298 | NULL); | |
299 | _exit (1); | |
300 | } | |
301 | ||
6a76c115 RM |
302 | for (l = _dl_loaded->l_next; l; l = l->l_next) |
303 | { | |
304 | char buf[20], *bp; | |
305 | buf[sizeof buf - 1] = '\0'; | |
306 | bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0); | |
307 | while (&buf[sizeof buf - 1] - bp < sizeof l->l_addr * 2) | |
308 | *--bp = '0'; | |
309 | _dl_sysdep_message ("\t", l->l_libname, " => ", l->l_name, | |
310 | " (0x", bp, ")\n", NULL); | |
311 | } | |
312 | ||
313 | _exit (0); | |
314 | } | |
86d2c878 RM |
315 | |
316 | if (rtld_map.l_info[DT_INIT]) | |
317 | { | |
318 | /* Call the initializer for the compatibility version of the | |
319 | dynamic linker. There is no additional initialization | |
320 | required for the ABI-compliant dynamic linker. */ | |
321 | ||
322 | (*(void (*) (void)) (rtld_map.l_addr + | |
323 | rtld_map.l_info[DT_INIT]->d_un.d_ptr)) (); | |
324 | ||
325 | /* Clear the field so a future dlopen won't run it again. */ | |
326 | rtld_map.l_info[DT_INIT] = NULL; | |
327 | } | |
421f82e5 | 328 | } |
d66e34cd | 329 | const char *errstring; |
421f82e5 | 330 | const char *errobj; |
d66e34cd RM |
331 | int err; |
332 | ||
421f82e5 | 333 | err = _dl_catch_error (&errstring, &errobj, &doit); |
d66e34cd RM |
334 | if (errstring) |
335 | _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>", | |
336 | ": error in loading shared libraries\n", | |
421f82e5 | 337 | errobj ?: "", errobj ? ": " : "", |
f2b0f935 RM |
338 | errstring, err ? ": " : "", |
339 | err ? strerror (err) : "", "\n", NULL); | |
d66e34cd RM |
340 | |
341 | /* Once we return, _dl_sysdep_start will invoke | |
342 | the DT_INIT functions and then *USER_ENTRY. */ | |
343 | } | |
344 | ||
86d2c878 | 345 | /* This function exists solely to have a breakpoint set on it by the |
d66e34cd RM |
346 | debugger. */ |
347 | void | |
348 | _dl_r_debug_state (void) | |
349 | { | |
350 | } | |
273d56ce | 351 | \f |
86d2c878 RM |
352 | /* Define our own stub for the localization function used by strerror. |
353 | English-only in the dynamic linker keeps it smaller. */ | |
354 | ||
355 | char * | |
356 | __dgettext (const char *domainname, const char *msgid) | |
357 | { | |
358 | assert (domainname == _libc_intl_domainname); | |
359 | return (char *) msgid; | |
360 | } | |
361 | weak_symbol (__dgettext) | |
362 | weak_alias (__dgettext, dgettext) | |
363 | ||
273d56ce RM |
364 | #ifndef NDEBUG |
365 | ||
366 | /* Define (weakly) our own assert failure function which doesn't use stdio. | |
367 | If we are linked into the user program (-ldl), the normal __assert_fail | |
368 | defn can override this one. */ | |
369 | ||
273d56ce RM |
370 | void |
371 | __assert_fail (const char *assertion, | |
372 | const char *file, unsigned int line, const char *function) | |
373 | { | |
374 | char buf[64]; | |
375 | buf[sizeof buf - 1] = '\0'; | |
376 | _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ", | |
377 | file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0), | |
378 | ": ", function ?: "", function ? ": " : "", | |
5bf62f2d RM |
379 | "Assertion `", assertion, "' failed!\n", |
380 | NULL); | |
273d56ce RM |
381 | |
382 | } | |
383 | weak_symbol (__assert_fail) | |
384 | ||
5bf62f2d RM |
385 | void |
386 | __assert_perror_fail (int errnum, | |
387 | const char *file, unsigned int line, | |
388 | const char *function) | |
389 | { | |
390 | char buf[64]; | |
391 | buf[sizeof buf - 1] = '\0'; | |
392 | _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ", | |
393 | file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0), | |
394 | ": ", function ?: "", function ? ": " : "", | |
395 | "Unexpected error: ", strerror (errnum), "\n", NULL); | |
396 | ||
397 | } | |
398 | weak_symbol (__assert_perror_fail) | |
399 | ||
273d56ce | 400 | #endif |