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