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