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