]> sourceware.org Git - glibc.git/blob - elf/rtld.c
* sysdeps/mach/i386/sysdep.h (SNARF_ARGS, CALL_WITH_SP): Rewritten.
[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 Elf32_Addr rtld_loadaddr;
56 Elf32_Dyn *dynamic_section;
57 Elf32_Dyn *dynamic_info[DT_NUM];
58
59 /* Figure out the run-time load address of the dynamic linker itself. */
60 rtld_loadaddr = elf_machine_load_address ();
61
62 /* Read our own dynamic section and fill in the info array.
63 Conveniently, the first element of the GOT contains the
64 offset of _DYNAMIC relative to the run-time load address. */
65 dynamic_section = (void *) rtld_loadaddr + *elf_machine_got ();
66 elf_get_dynamic_info (dynamic_section, dynamic_info);
67
68 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
69 ELF_MACHINE_BEFORE_RTLD_RELOC (dynamic_info);
70 #endif
71
72 /* Relocate ourselves so we can do normal function calls and
73 data access using the global offset table. */
74 {
75 Elf32_Addr resolve (const Elf32_Sym **ref)
76 {
77 assert ((*ref)->st_shndx != SHN_UNDEF);
78 return rtld_loadaddr;
79 }
80 elf_dynamic_relocate (dynamic_info, rtld_loadaddr, 0, resolve);
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_loadaddr; /* 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 static void
104 dl_main (const Elf32_Phdr *phdr,
105 Elf32_Word phent,
106 Elf32_Addr *user_entry)
107 {
108 void doit (void)
109 {
110 const Elf32_Phdr *ph;
111 struct link_map *l;
112 const char *interpreter_name;
113 int lazy;
114
115 if (*user_entry == (Elf32_Addr) &_start)
116 {
117 /* Ho ho. We are not the program interpreter! We are the program
118 itself! This means someone ran ld.so as a command. Well, that
119 might be convenient to do sometimes. We support it by
120 interpreting the args like this:
121
122 ld.so PROGRAM ARGS...
123
124 The first argument is the name of a file containing an ELF
125 executable we will load and run with the following arguments. To
126 simplify life here, PROGRAM is searched for using the normal rules
127 for shared objects, rather than $PATH or anything like that. We
128 just load it and use its entry point; we don't pay attention to
129 its PT_INTERP command (we are the interpreter ourselves). This is
130 an easy way to test a new ld.so before installing it. */
131 if (_dl_argc < 2)
132 _dl_sysdep_fatal ("\
133 Usage: ld.so EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
134 You have invoked `ld.so', the helper program for shared library executables.\n\
135 This program usually lives in the file `/lib/ld.so', and special directives\n\
136 in executable files using ELF shared libraries tell the system's program\n\
137 loader to load the helper program from this file. This helper program loads\n\
138 the shared libraries needed by the program executable, prepares the program\n\
139 to run, and runs it. You may invoke this helper program directly from the\n\
140 command line to load and run an ELF executable file; this is like executing\n\
141 that file itself, but always uses this helper program from the file you\n\
142 specified, instead of the helper program file specified in the executable\n\
143 file you run. This is mostly of use for maintainers to test new versions\n\
144 of this helper program; chances are you did not intend to run this program.\n"
145 );
146
147 interpreter_name = _dl_argv[0];
148 --_dl_argc;
149 ++_dl_argv;
150 l = _dl_map_object (NULL, _dl_argv[0], user_entry);
151 phdr = l->l_phdr;
152 phent = l->l_phnum;
153 l->l_type = lt_executable;
154 l->l_libname = (char *) "";
155 }
156 else
157 {
158 /* Create a link_map for the executable itself.
159 This will be what dlopen on "" returns. */
160 l = _dl_new_object ((char *) "", "", lt_executable);
161 l->l_phdr = phdr;
162 l->l_phnum = phent;
163 interpreter_name = 0;
164 }
165
166 /* Scan the program header table for the dynamic section. */
167 for (ph = phdr; ph < &phdr[phent]; ++ph)
168 switch (ph->p_type)
169 {
170 case PT_DYNAMIC:
171 /* This tells us where to find the dynamic section,
172 which tells us everything we need to do. */
173 l->l_ld = (void *) ph->p_vaddr;
174 break;
175 case PT_INTERP:
176 /* This "interpreter segment" was used by the program loader to
177 find the program interpreter, which is this program itself, the
178 dynamic linker. We note what name finds us, so that a future
179 dlopen call or DT_NEEDED entry, for something that wants to link
180 against the dynamic linker as a shared library, will know that
181 the shared object is already loaded. */
182 interpreter_name = (void *) ph->p_vaddr;
183 break;
184 }
185 assert (interpreter_name); /* How else did we get here? */
186
187 /* Extract the contents of the dynamic section for easy access. */
188 elf_get_dynamic_info (l->l_ld, l->l_info);
189 /* Set up our cache of pointers into the hash table. */
190 _dl_setup_hash (l);
191
192 if (l->l_info[DT_DEBUG])
193 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
194 with the run-time address of the r_debug structure, which we
195 will set up later to communicate with the debugger. */
196 l->l_info[DT_DEBUG]->d_un.d_ptr = (Elf32_Addr) &dl_r_debug;
197
198 l = _dl_new_object ((char *) interpreter_name, interpreter_name,
199 lt_interpreter);
200
201 /* Now process all the DT_NEEDED entries and map in the objects.
202 Each new link_map will go on the end of the chain, so we will
203 come across it later in the loop to map in its dependencies. */
204 for (l = _dl_loaded; l; l = l->l_next)
205 {
206 if (l->l_info[DT_NEEDED])
207 {
208 const char *strtab
209 = (void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr;
210 const Elf32_Dyn *d;
211 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
212 if (d->d_tag == DT_NEEDED)
213 _dl_map_object (l, strtab + d->d_un.d_val, NULL);
214 }
215 l->l_deps_loaded = 1;
216 }
217
218 l = _dl_loaded->l_next;
219 assert (l->l_type == lt_interpreter);
220 if (l->l_opencount == 0)
221 {
222 /* No DT_NEEDED entry referred to the interpreter object itself.
223 Remove it from the maps we will use for symbol resolution. */
224 l->l_prev->l_next = l->l_next;
225 if (l->l_next)
226 l->l_next->l_prev = l->l_prev;
227 }
228
229 lazy = _dl_secure || *(getenv ("LD_BIND_NOW") ?: "");
230
231 /* Now we have all the objects loaded. Relocate them all.
232 We do this in reverse order so that copy relocs of earlier
233 objects overwrite the data written by later objects. */
234 l = _dl_loaded;
235 while (l->l_next)
236 l = l->l_next;
237 do
238 {
239 _dl_relocate_object (l, lazy);
240 l = l->l_prev;
241 } while (l);
242
243 /* Tell the debugger where to find the map of loaded objects. */
244 dl_r_debug.r_version = 1 /* R_DEBUG_VERSION XXX */;
245 dl_r_debug.r_map = _dl_loaded;
246 dl_r_debug.r_brk = (Elf32_Addr) &_dl_r_debug_state;
247 }
248 const char *errstring;
249 int err;
250
251 err = _dl_catch_error (&errstring, &doit);
252 if (errstring)
253 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
254 ": error in loading shared libraries\n",
255 errstring, err ? ": " : NULL,
256 err ? strerror (err) : NULL, NULL);
257
258 /* Once we return, _dl_sysdep_start will invoke
259 the DT_INIT functions and then *USER_ENTRY. */
260 }
261
262 /* This function exists solely to have a breakpoint set on it by the
263 debugger. */
264 void
265 _dl_r_debug_state (void)
266 {
267 }
This page took 0.055091 seconds and 6 git commands to generate.