]>
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> | |
25 | ||
26 | ||
27 | #ifdef RTLD_START | |
28 | RTLD_START | |
29 | #else | |
30 | #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START" | |
31 | #endif | |
32 | ||
33 | /* System-specific function to do initial startup for the dynamic linker. | |
34 | After this, file access calls and getenv must work. This is responsible | |
35 | for setting _dl_secure if we need to be secure (e.g. setuid), | |
36 | and for setting _dl_argc and _dl_argv, and then calling _dl_main. */ | |
37 | extern Elf32_Addr _dl_sysdep_start (void **start_argptr, | |
38 | void (*dl_main) (const Elf32_Phdr *phdr, | |
39 | Elf32_Word phent, | |
40 | Elf32_Addr *user_entry)); | |
41 | ||
42 | int _dl_secure; | |
43 | int _dl_argc; | |
44 | char **_dl_argv; | |
45 | ||
46 | struct r_debug dl_r_debug; | |
47 | ||
48 | static void dl_main (const Elf32_Phdr *phdr, | |
49 | Elf32_Word phent, | |
50 | Elf32_Addr *user_entry); | |
51 | ||
52 | Elf32_Addr | |
53 | _dl_start (void *arg) | |
54 | { | |
421f82e5 | 55 | struct link_map rtld_map; |
d66e34cd RM |
56 | |
57 | /* Figure out the run-time load address of the dynamic linker itself. */ | |
421f82e5 | 58 | rtld_map.l_addr = elf_machine_load_address (); |
d66e34cd RM |
59 | |
60 | /* Read our own dynamic section and fill in the info array. | |
61 | Conveniently, the first element of the GOT contains the | |
62 | offset of _DYNAMIC relative to the run-time load address. */ | |
421f82e5 RM |
63 | rtld_map.l_ld = (void *) rtld_map.l_addr + *elf_machine_got (); |
64 | elf_get_dynamic_info (rtld_map.l_ld, rtld_map.l_info); | |
d66e34cd RM |
65 | |
66 | #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC | |
421f82e5 | 67 | ELF_MACHINE_BEFORE_RTLD_RELOC (rtld_map.l_info); |
d66e34cd RM |
68 | #endif |
69 | ||
70 | /* Relocate ourselves so we can do normal function calls and | |
71 | data access using the global offset table. */ | |
421f82e5 RM |
72 | |
73 | ELF_DYNAMIC_RELOCATE (&rtld_map, 0, NULL); | |
74 | ||
d66e34cd RM |
75 | |
76 | /* Now life is sane; we can call functions and access global data. | |
77 | Set up to use the operating system facilities, and find out from | |
78 | the operating system's program loader where to find the program | |
79 | header table in core. */ | |
80 | ||
421f82e5 | 81 | dl_r_debug.r_ldbase = rtld_map.l_addr; /* Record our load address. */ |
d66e34cd RM |
82 | |
83 | /* Call the OS-dependent function to set up life so we can do things like | |
84 | file access. It will call `dl_main' (below) to do all the real work | |
85 | of the dynamic linker, and then unwind our frame and run the user | |
86 | entry point on the same stack we entered on. */ | |
87 | return _dl_sysdep_start (&arg, &dl_main); | |
88 | } | |
89 | ||
90 | ||
91 | /* Now life is peachy; we can do all normal operations. | |
92 | On to the real work. */ | |
93 | ||
94 | void _start (void); | |
95 | ||
a1a9d215 RM |
96 | static int rtld_command; /* Nonzero if we were run directly. */ |
97 | ||
d66e34cd RM |
98 | static void |
99 | dl_main (const Elf32_Phdr *phdr, | |
100 | Elf32_Word phent, | |
101 | Elf32_Addr *user_entry) | |
102 | { | |
103 | void doit (void) | |
104 | { | |
421f82e5 RM |
105 | const Elf32_Phdr *ph; |
106 | struct link_map *l; | |
107 | const char *interpreter_name; | |
108 | int lazy; | |
d66e34cd | 109 | |
421f82e5 RM |
110 | if (*user_entry == (Elf32_Addr) &_start) |
111 | { | |
112 | /* Ho ho. We are not the program interpreter! We are the program | |
113 | itself! This means someone ran ld.so as a command. Well, that | |
114 | might be convenient to do sometimes. We support it by | |
115 | interpreting the args like this: | |
116 | ||
117 | ld.so PROGRAM ARGS... | |
118 | ||
119 | The first argument is the name of a file containing an ELF | |
120 | executable we will load and run with the following arguments. | |
121 | To simplify life here, PROGRAM is searched for using the | |
122 | normal rules for shared objects, rather than $PATH or anything | |
123 | like that. We just load it and use its entry point; we don't | |
124 | pay attention to its PT_INTERP command (we are the interpreter | |
125 | ourselves). This is an easy way to test a new ld.so before | |
126 | installing it. */ | |
127 | if (_dl_argc < 2) | |
128 | _dl_sysdep_fatal ("\ | |
d66e34cd RM |
129 | Usage: ld.so EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\ |
130 | You have invoked `ld.so', the helper program for shared library executables.\n\ | |
131 | This program usually lives in the file `/lib/ld.so', and special directives\n\ | |
132 | in executable files using ELF shared libraries tell the system's program\n\ | |
133 | loader to load the helper program from this file. This helper program loads\n\ | |
134 | the shared libraries needed by the program executable, prepares the program\n\ | |
135 | to run, and runs it. You may invoke this helper program directly from the\n\ | |
136 | command line to load and run an ELF executable file; this is like executing\n\ | |
137 | that file itself, but always uses this helper program from the file you\n\ | |
138 | specified, instead of the helper program file specified in the executable\n\ | |
139 | file you run. This is mostly of use for maintainers to test new versions\n\ | |
140 | of this helper program; chances are you did not intend to run this program.\n" | |
421f82e5 RM |
141 | ); |
142 | ||
a1a9d215 | 143 | rtld_command = 1; |
421f82e5 RM |
144 | interpreter_name = _dl_argv[0]; |
145 | --_dl_argc; | |
146 | ++_dl_argv; | |
147 | l = _dl_map_object (NULL, _dl_argv[0], user_entry); | |
148 | phdr = l->l_phdr; | |
149 | phent = l->l_phnum; | |
150 | l->l_type = lt_executable; | |
151 | l->l_libname = (char *) ""; | |
152 | } | |
153 | else | |
154 | { | |
155 | /* Create a link_map for the executable itself. | |
156 | This will be what dlopen on "" returns. */ | |
157 | l = _dl_new_object ((char *) "", "", lt_executable); | |
158 | l->l_phdr = phdr; | |
159 | l->l_phnum = phent; | |
160 | interpreter_name = 0; | |
161 | } | |
d66e34cd | 162 | |
421f82e5 RM |
163 | /* Scan the program header table for the dynamic section. */ |
164 | for (ph = phdr; ph < &phdr[phent]; ++ph) | |
165 | switch (ph->p_type) | |
166 | { | |
167 | case PT_DYNAMIC: | |
168 | /* This tells us where to find the dynamic section, | |
169 | which tells us everything we need to do. */ | |
a1a9d215 | 170 | l->l_ld = (void *) l->l_addr + ph->p_vaddr; |
421f82e5 RM |
171 | break; |
172 | case PT_INTERP: | |
173 | /* This "interpreter segment" was used by the program loader to | |
174 | find the program interpreter, which is this program itself, the | |
175 | dynamic linker. We note what name finds us, so that a future | |
176 | dlopen call or DT_NEEDED entry, for something that wants to link | |
177 | against the dynamic linker as a shared library, will know that | |
178 | the shared object is already loaded. */ | |
a1a9d215 | 179 | interpreter_name = (void *) l->l_addr + ph->p_vaddr; |
421f82e5 RM |
180 | break; |
181 | } | |
182 | assert (interpreter_name); /* How else did we get here? */ | |
183 | ||
184 | /* Extract the contents of the dynamic section for easy access. */ | |
185 | elf_get_dynamic_info (l->l_ld, l->l_info); | |
186 | /* Set up our cache of pointers into the hash table. */ | |
187 | _dl_setup_hash (l); | |
188 | ||
189 | if (l->l_info[DT_DEBUG]) | |
190 | /* There is a DT_DEBUG entry in the dynamic section. Fill it in | |
191 | with the run-time address of the r_debug structure, which we | |
192 | will set up later to communicate with the debugger. */ | |
193 | l->l_info[DT_DEBUG]->d_un.d_ptr = (Elf32_Addr) &dl_r_debug; | |
194 | ||
195 | l = _dl_new_object ((char *) interpreter_name, interpreter_name, | |
196 | lt_interpreter); | |
197 | ||
198 | /* Now process all the DT_NEEDED entries and map in the objects. | |
199 | Each new link_map will go on the end of the chain, so we will | |
200 | come across it later in the loop to map in its dependencies. */ | |
201 | for (l = _dl_loaded; l; l = l->l_next) | |
d66e34cd | 202 | { |
421f82e5 RM |
203 | if (l->l_info[DT_NEEDED]) |
204 | { | |
205 | const char *strtab | |
206 | = (void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr; | |
207 | const Elf32_Dyn *d; | |
208 | for (d = l->l_ld; d->d_tag != DT_NULL; ++d) | |
209 | if (d->d_tag == DT_NEEDED) | |
210 | _dl_map_object (l, strtab + d->d_un.d_val, NULL); | |
211 | } | |
212 | l->l_deps_loaded = 1; | |
d66e34cd | 213 | } |
d66e34cd | 214 | |
421f82e5 RM |
215 | l = _dl_loaded->l_next; |
216 | assert (l->l_type == lt_interpreter); | |
217 | if (l->l_opencount == 0) | |
218 | { | |
219 | /* No DT_NEEDED entry referred to the interpreter object itself. | |
220 | Remove it from the maps we will use for symbol resolution. */ | |
221 | l->l_prev->l_next = l->l_next; | |
222 | if (l->l_next) | |
223 | l->l_next->l_prev = l->l_prev; | |
224 | } | |
d66e34cd | 225 | |
a1a9d215 | 226 | lazy = !_dl_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0'; |
d66e34cd | 227 | |
421f82e5 RM |
228 | /* Now we have all the objects loaded. Relocate them all. |
229 | We do this in reverse order so that copy relocs of earlier | |
230 | objects overwrite the data written by later objects. */ | |
231 | l = _dl_loaded; | |
232 | while (l->l_next) | |
233 | l = l->l_next; | |
234 | do | |
235 | { | |
236 | _dl_relocate_object (l, lazy); | |
237 | l = l->l_prev; | |
238 | } while (l); | |
239 | ||
240 | /* Tell the debugger where to find the map of loaded objects. */ | |
241 | dl_r_debug.r_version = 1 /* R_DEBUG_VERSION XXX */; | |
242 | dl_r_debug.r_map = _dl_loaded; | |
243 | dl_r_debug.r_brk = (Elf32_Addr) &_dl_r_debug_state; | |
244 | } | |
d66e34cd | 245 | const char *errstring; |
421f82e5 | 246 | const char *errobj; |
d66e34cd RM |
247 | int err; |
248 | ||
421f82e5 | 249 | err = _dl_catch_error (&errstring, &errobj, &doit); |
d66e34cd RM |
250 | if (errstring) |
251 | _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>", | |
252 | ": error in loading shared libraries\n", | |
421f82e5 | 253 | errobj ?: "", errobj ? ": " : "", |
d66e34cd RM |
254 | errstring, err ? ": " : NULL, |
255 | err ? strerror (err) : NULL, NULL); | |
256 | ||
257 | /* Once we return, _dl_sysdep_start will invoke | |
258 | the DT_INIT functions and then *USER_ENTRY. */ | |
259 | } | |
260 | ||
261 | /* This function exists solely to have a breakpoint set on it by the | |
262 | debugger. */ | |
263 | void | |
264 | _dl_r_debug_state (void) | |
265 | { | |
266 | } | |
273d56ce RM |
267 | \f |
268 | #ifndef NDEBUG | |
269 | ||
270 | /* Define (weakly) our own assert failure function which doesn't use stdio. | |
271 | If we are linked into the user program (-ldl), the normal __assert_fail | |
272 | defn can override this one. */ | |
273 | ||
274 | #include "../stdio/_itoa.h" | |
275 | ||
276 | void | |
277 | __assert_fail (const char *assertion, | |
278 | const char *file, unsigned int line, const char *function) | |
279 | { | |
280 | char buf[64]; | |
281 | buf[sizeof buf - 1] = '\0'; | |
282 | _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ", | |
283 | file, ": ", _itoa (line, buf + sizeof buf - 1, 10, 0), | |
284 | ": ", function ?: "", function ? ": " : "", | |
285 | "Assertion `", assertion, "' failed!\n"); | |
286 | ||
287 | } | |
288 | weak_symbol (__assert_fail) | |
289 | ||
290 | #endif |