* time/tzfile.h, time/private.h, time/zdump.c, time/zic.c,
[glibc.git] / elf / rtld.c
blob032bb8ee87ceef7ed7839aeff37e09ad00a8940c
1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <link.h>
21 #include "dynamic-link.h"
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include "../stdio-common/_itoa.h"
28 #ifdef RTLD_START
29 RTLD_START
30 #else
31 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
32 #endif
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 extern void _dl_sysdep_start_cleanup (void);
44 int _dl_secure;
45 int _dl_argc;
46 char **_dl_argv;
47 const char *_dl_rpath;
49 struct r_debug dl_r_debug;
51 static void dl_main (const Elf32_Phdr *phdr,
52 Elf32_Word phent,
53 Elf32_Addr *user_entry);
55 struct link_map _dl_rtld_map;
57 Elf32_Addr
58 _dl_start (void *arg)
60 struct link_map bootstrap_map;
62 /* Figure out the run-time load address of the dynamic linker itself. */
63 bootstrap_map.l_addr = elf_machine_load_address ();
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. */
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);
71 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
72 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
73 #endif
75 /* Relocate ourselves so we can do normal function calls and
76 data access using the global offset table. */
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. */
84 bootstrap_map.l_type = lt_library;
85 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, NULL);
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. */
94 /* Transfer data about ourselves to the permanent link_map structure. */
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);
101 /* Cache the DT_RPATH stored in ld.so itself; this will be
102 the default search path. */
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);
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);
115 /* Now life is peachy; we can do all normal operations.
116 On to the real work. */
118 void _start (void);
120 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
122 static void
123 dl_main (const Elf32_Phdr *phdr,
124 Elf32_Word phent,
125 Elf32_Addr *user_entry)
127 const Elf32_Phdr *ph;
128 struct link_map *l;
129 const char *interpreter_name;
130 int lazy;
131 int list_only = 0;
133 if (*user_entry == (Elf32_Addr) &_start)
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:
140 ld.so PROGRAM ARGS...
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 ("\
152 Usage: ld.so [--list] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
153 You have invoked `ld.so', the helper program for shared library executables.\n\
154 This program usually lives in the file `/lib/ld.so', and special directives\n\
155 in executable files using ELF shared libraries tell the system's program\n\
156 loader to load the helper program from this file. This helper program loads\n\
157 the shared libraries needed by the program executable, prepares the program\n\
158 to run, and runs it. You may invoke this helper program directly from the\n\
159 command line to load and run an ELF executable file; this is like executing\n\
160 that file itself, but always uses this helper program from the file you\n\
161 specified, instead of the helper program file specified in the executable\n\
162 file you run. This is mostly of use for maintainers to test new versions\n\
163 of this helper program; chances are you did not intend to run this program.\n",
164 NULL);
166 interpreter_name = _dl_argv[0];
168 if (! strcmp (_dl_argv[1], "--list"))
170 list_only = 1;
172 ++_dl_skip_args;
173 --_dl_argc;
174 ++_dl_argv;
177 ++_dl_skip_args;
178 --_dl_argc;
179 ++_dl_argv;
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;
187 else
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;
198 if (l != _dl_loaded)
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;
212 /* Scan the program header table for the dynamic section. */
213 for (ph = phdr; ph < &phdr[phent]; ++ph)
214 switch (ph->p_type)
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;
231 assert (interpreter_name); /* How else did we get here? */
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);
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;
245 /* Put the link_map for ourselves on the chain so it can be found by
246 name. */
247 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname = interpreter_name;
248 _dl_rtld_map.l_type = lt_interpreter;
249 while (l->l_next)
250 l = l->l_next;
251 l->l_next = &_dl_rtld_map;
252 _dl_rtld_map.l_prev = l;
254 /* Load all the libraries specified by DT_NEEDED entries. */
255 _dl_map_object_deps (l);
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. */
261 if (_dl_rtld_map.l_opencount == 0)
263 /* No DT_NEEDED entry referred to the interpreter object itself,
264 so remove it from the list of visible objects. */
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;
269 if (list_only)
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. */
276 int i;
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)
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);
292 for (i = 1; i < _dl_argc; ++i)
294 const Elf32_Sym *ref = NULL;
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);
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);
311 _exit (0);
314 lazy = !_dl_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
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;
329 if (l != &_dl_rtld_map)
330 _dl_relocate_object (l, lazy);
331 l = l->l_prev;
332 } while (l);
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 ();
341 if (_dl_rtld_map.l_opencount > 0)
342 /* There was an explicit ref to the dynamic linker as a shared lib.
343 Re-relocate ourselves with user-controlled symbol definitions. */
344 _dl_relocate_object (&_dl_rtld_map, lazy);
346 /* Tell the debugger where to find the map of loaded objects. */
347 dl_r_debug.r_version = 1 /* R_DEBUG_VERSION XXX */;
348 dl_r_debug.r_ldbase = _dl_rtld_map.l_addr; /* Record our load address. */
349 dl_r_debug.r_map = _dl_loaded;
350 dl_r_debug.r_brk = (Elf32_Addr) &_dl_r_debug_state;
352 if (_dl_rtld_map.l_info[DT_INIT])
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. */
358 (*(void (*) (void)) (_dl_rtld_map.l_addr +
359 _dl_rtld_map.l_info[DT_INIT]->d_un.d_ptr)) ();
361 /* Clear the field so a future dlopen won't run it again. */
362 _dl_rtld_map.l_info[DT_INIT] = NULL;
365 /* Once we return, _dl_sysdep_start will invoke
366 the DT_INIT functions and then *USER_ENTRY. */
369 /* This function exists solely to have a breakpoint set on it by the
370 debugger. */
371 void
372 _dl_r_debug_state (void)