]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/rtld.c
Thu Jun 6 12:56:03 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[thirdparty/glibc.git] / elf / rtld.c
CommitLineData
d66e34cd 1/* Run time dynamic linker.
948c3e72 2Copyright (C) 1995, 1996 Free Software Foundation, Inc.
d66e34cd
RM
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, 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
29RTLD_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. */
38extern 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 42extern void _dl_sysdep_start_cleanup (void);
d66e34cd
RM
43
44int _dl_secure;
45int _dl_argc;
46char **_dl_argv;
4cb20290 47const char *_dl_rpath;
d66e34cd
RM
48
49struct r_debug dl_r_debug;
50
51static void dl_main (const Elf32_Phdr *phdr,
52 Elf32_Word phent,
53 Elf32_Addr *user_entry);
54
ee188d55 55struct link_map _dl_rtld_map;
86d2c878 56
d66e34cd
RM
57Elf32_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. */
ee188d55
RM
95 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
96 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
97 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
98 sizeof _dl_rtld_map.l_info);
99 _dl_setup_hash (&_dl_rtld_map);
86d2c878 100
4cb20290
RM
101 /* Cache the DT_RPATH stored in ld.so itself; this will be
102 the default search path. */
ee188d55
RM
103 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
104 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
105 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
d66e34cd
RM
106
107 /* Call the OS-dependent function to set up life so we can do things like
108 file access. It will call `dl_main' (below) to do all the real work
109 of the dynamic linker, and then unwind our frame and run the user
110 entry point on the same stack we entered on. */
111 return _dl_sysdep_start (&arg, &dl_main);
112}
113
114
115/* Now life is peachy; we can do all normal operations.
116 On to the real work. */
117
118void _start (void);
119
91f62ce6 120unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
a1a9d215 121
d66e34cd
RM
122static void
123dl_main (const Elf32_Phdr *phdr,
124 Elf32_Word phent,
125 Elf32_Addr *user_entry)
126{
0200214b 127 const Elf32_Phdr *ph;
efec1d0c 128 struct link_map *l;
0200214b
RM
129 const char *interpreter_name;
130 int lazy;
131 int list_only = 0;
d66e34cd 132
0200214b
RM
133 if (*user_entry == (Elf32_Addr) &_start)
134 {
135 /* Ho ho. We are not the program interpreter! We are the program
136 itself! This means someone ran ld.so as a command. Well, that
137 might be convenient to do sometimes. We support it by
138 interpreting the args like this:
139
140 ld.so PROGRAM ARGS...
141
142 The first argument is the name of a file containing an ELF
143 executable we will load and run with the following arguments.
144 To simplify life here, PROGRAM is searched for using the
145 normal rules for shared objects, rather than $PATH or anything
146 like that. We just load it and use its entry point; we don't
147 pay attention to its PT_INTERP command (we are the interpreter
148 ourselves). This is an easy way to test a new ld.so before
149 installing it. */
150 if (_dl_argc < 2)
151 _dl_sysdep_fatal ("\
6a76c115 152Usage: ld.so [--list] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
d66e34cd
RM
153You have invoked `ld.so', the helper program for shared library executables.\n\
154This program usually lives in the file `/lib/ld.so', and special directives\n\
155in executable files using ELF shared libraries tell the system's program\n\
156loader to load the helper program from this file. This helper program loads\n\
157the shared libraries needed by the program executable, prepares the program\n\
158to run, and runs it. You may invoke this helper program directly from the\n\
159command line to load and run an ELF executable file; this is like executing\n\
160that file itself, but always uses this helper program from the file you\n\
161specified, instead of the helper program file specified in the executable\n\
162file you run. This is mostly of use for maintainers to test new versions\n\
5bf62f2d 163of this helper program; chances are you did not intend to run this program.\n",
0200214b 164 NULL);
421f82e5 165
0200214b 166 interpreter_name = _dl_argv[0];
6a76c115 167
0200214b
RM
168 if (! strcmp (_dl_argv[1], "--list"))
169 {
170 list_only = 1;
6a76c115
RM
171
172 ++_dl_skip_args;
421f82e5
RM
173 --_dl_argc;
174 ++_dl_argv;
421f82e5 175 }
d66e34cd 176
0200214b
RM
177 ++_dl_skip_args;
178 --_dl_argc;
179 ++_dl_argv;
91f62ce6 180
0200214b
RM
181 l = _dl_map_object (NULL, _dl_argv[0]);
182 phdr = l->l_phdr;
183 phent = l->l_phnum;
184 l->l_name = (char *) "";
185 *user_entry = l->l_entry;
186 }
187 else
188 {
189 /* Create a link_map for the executable itself.
190 This will be what dlopen on "" returns. */
191 l = _dl_new_object ((char *) "", "", lt_executable);
192 l->l_phdr = phdr;
193 l->l_phnum = phent;
194 interpreter_name = 0;
195 l->l_entry = *user_entry;
196 }
197
198 if (l != _dl_loaded)
199 {
200 /* GDB assumes that the first element on the chain is the
201 link_map for the executable itself, and always skips it.
202 Make sure the first one is indeed that one. */
203 l->l_prev->l_next = l->l_next;
204 if (l->l_next)
205 l->l_next->l_prev = l->l_prev;
206 l->l_prev = NULL;
207 l->l_next = _dl_loaded;
208 _dl_loaded->l_prev = l;
209 _dl_loaded = l;
210 }
211
212 /* Scan the program header table for the dynamic section. */
213 for (ph = phdr; ph < &phdr[phent]; ++ph)
214 switch (ph->p_type)
215 {
216 case PT_DYNAMIC:
217 /* This tells us where to find the dynamic section,
218 which tells us everything we need to do. */
219 l->l_ld = (void *) l->l_addr + ph->p_vaddr;
220 break;
221 case PT_INTERP:
222 /* This "interpreter segment" was used by the program loader to
223 find the program interpreter, which is this program itself, the
224 dynamic linker. We note what name finds us, so that a future
225 dlopen call or DT_NEEDED entry, for something that wants to link
226 against the dynamic linker as a shared library, will know that
227 the shared object is already loaded. */
228 interpreter_name = (void *) l->l_addr + ph->p_vaddr;
229 break;
230 }
231 assert (interpreter_name); /* How else did we get here? */
232
233 /* Extract the contents of the dynamic section for easy access. */
234 elf_get_dynamic_info (l->l_ld, l->l_info);
235 if (l->l_info[DT_HASH])
236 /* Set up our cache of pointers into the hash table. */
237 _dl_setup_hash (l);
238
239 if (l->l_info[DT_DEBUG])
240 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
241 with the run-time address of the r_debug structure, which we
242 will set up later to communicate with the debugger. */
243 l->l_info[DT_DEBUG]->d_un.d_ptr = (Elf32_Addr) &dl_r_debug;
244
245 /* Put the link_map for ourselves on the chain so it can be found by
246 name. */
ee188d55
RM
247 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname = interpreter_name;
248 _dl_rtld_map.l_type = lt_interpreter;
0200214b
RM
249 while (l->l_next)
250 l = l->l_next;
ee188d55
RM
251 l->l_next = &_dl_rtld_map;
252 _dl_rtld_map.l_prev = l;
0200214b 253
efec1d0c
RM
254 /* Load all the libraries specified by DT_NEEDED entries. */
255 _dl_map_object_deps (l);
d66e34cd 256
efec1d0c
RM
257 /* XXX if kept, move it so l_next list is in dep order because
258 it will determine gdb's search order.
259 Perhaps do this always, so later dlopen by name finds it?
260 XXX But then gdb always considers it present. */
ee188d55 261 if (_dl_rtld_map.l_opencount == 0)
0200214b 262 {
efec1d0c
RM
263 /* No DT_NEEDED entry referred to the interpreter object itself,
264 so remove it from the list of visible objects. */
ee188d55
RM
265 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
266 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
0200214b 267 }
d66e34cd 268
0200214b
RM
269 if (list_only)
270 {
271 /* We were run just to list the shared libraries. It is
272 important that we do this before real relocation, because the
273 functions we call below for output may no longer work properly
274 after relocation. */
1a3a58fd 275
0200214b 276 int i;
fd861379 277
0200214b
RM
278 if (! _dl_loaded->l_info[DT_NEEDED])
279 _dl_sysdep_message ("\t", "statically linked\n", NULL);
280 else
281 for (l = _dl_loaded->l_next; l; l = l->l_next)
282 {
283 char buf[20], *bp;
284 buf[sizeof buf - 1] = '\0';
285 bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0);
286 while (&buf[sizeof buf - 1] - bp < sizeof l->l_addr * 2)
287 *--bp = '0';
288 _dl_sysdep_message ("\t", l->l_libname, " => ", l->l_name,
289 " (0x", bp, ")\n", NULL);
290 }
1a3a58fd 291
0200214b
RM
292 for (i = 1; i < _dl_argc; ++i)
293 {
294 const Elf32_Sym *ref = NULL;
efec1d0c
RM
295 struct link_map *scope[2] ={ _dl_loaded, NULL };
296 Elf32_Addr loadbase
297 = _dl_lookup_symbol (_dl_argv[i], &ref, scope, "argument", 0, 0);
0200214b
RM
298 char buf[20], *bp;
299 buf[sizeof buf - 1] = '\0';
300 bp = _itoa (ref->st_value, &buf[sizeof buf - 1], 16, 0);
301 while (&buf[sizeof buf - 1] - bp < sizeof loadbase * 2)
302 *--bp = '0';
303 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
304 buf[sizeof buf - 1] = '\0';
305 bp = _itoa (loadbase, &buf[sizeof buf - 1], 16, 0);
306 while (&buf[sizeof buf - 1] - bp < sizeof loadbase * 2)
307 *--bp = '0';
308 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
1a3a58fd 309 }
d66e34cd 310
0200214b
RM
311 _exit (0);
312 }
86d2c878 313
0200214b
RM
314 lazy = !_dl_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
315
316 /* Now we have all the objects loaded. Relocate them all except for
317 the dynamic linker itself. We do this in reverse order so that
318 copy relocs of earlier objects overwrite the data written by later
319 objects. We do not re-relocate the dynamic linker itself in this
320 loop because that could result in the GOT entries for functions we
321 call being changed, and that would break us. It is safe to
322 relocate the dynamic linker out of order because it has no copy
323 relocs (we know that because it is self-contained). */
324 l = _dl_loaded;
325 while (l->l_next)
326 l = l->l_next;
327 do
328 {
ee188d55 329 if (l != &_dl_rtld_map)
0200214b
RM
330 _dl_relocate_object (l, lazy);
331 l = l->l_prev;
332 } while (l);
333
334 /* Do any necessary cleanups for the startup OS interface code.
335 We do these now so that no calls are made after rtld re-relocation
336 which might be resolved to different functions than we expect.
337 We cannot do this before relocating the other objects because
338 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
339 _dl_sysdep_start_cleanup ();
340
ee188d55 341 if (_dl_rtld_map.l_opencount > 0)
0200214b
RM
342 /* There was an explicit ref to the dynamic linker as a shared lib.
343 Re-relocate ourselves with user-controlled symbol definitions. */
ee188d55 344 _dl_relocate_object (&_dl_rtld_map, lazy);
0200214b
RM
345
346 /* Tell the debugger where to find the map of loaded objects. */
347 dl_r_debug.r_version = 1 /* R_DEBUG_VERSION XXX */;
ee188d55 348 dl_r_debug.r_ldbase = _dl_rtld_map.l_addr; /* Record our load address. */
0200214b
RM
349 dl_r_debug.r_map = _dl_loaded;
350 dl_r_debug.r_brk = (Elf32_Addr) &_dl_r_debug_state;
351
ee188d55 352 if (_dl_rtld_map.l_info[DT_INIT])
0200214b
RM
353 {
354 /* Call the initializer for the compatibility version of the
355 dynamic linker. There is no additional initialization
356 required for the ABI-compliant dynamic linker. */
86d2c878 357
ee188d55
RM
358 (*(void (*) (void)) (_dl_rtld_map.l_addr +
359 _dl_rtld_map.l_info[DT_INIT]->d_un.d_ptr)) ();
0200214b
RM
360
361 /* Clear the field so a future dlopen won't run it again. */
ee188d55 362 _dl_rtld_map.l_info[DT_INIT] = NULL;
421f82e5 363 }
d66e34cd
RM
364
365 /* Once we return, _dl_sysdep_start will invoke
366 the DT_INIT functions and then *USER_ENTRY. */
367}
368
86d2c878 369/* This function exists solely to have a breakpoint set on it by the
d66e34cd
RM
370 debugger. */
371void
372_dl_r_debug_state (void)
373{
374}