rtld.c revision 1.32 1 1.32 jdolecek /* $NetBSD: rtld.c,v 1.32 2000/06/02 22:52:28 jdolecek Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 John D. Polstra.
5 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by John Polstra.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd /*
35 1.1 cgd * Dynamic linker for ELF.
36 1.1 cgd *
37 1.1 cgd * John Polstra <jdp (at) polstra.com>.
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd #include <err.h>
41 1.1 cgd #include <errno.h>
42 1.1 cgd #include <fcntl.h>
43 1.1 cgd #include <stdarg.h>
44 1.1 cgd #include <stdio.h>
45 1.1 cgd #include <stdlib.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd #include <unistd.h>
48 1.3 cgd #include <sys/param.h>
49 1.1 cgd #include <sys/mman.h>
50 1.1 cgd #include <dirent.h>
51 1.1 cgd
52 1.1 cgd #include <ctype.h>
53 1.1 cgd
54 1.2 cgd #include <dlfcn.h>
55 1.1 cgd #include "debug.h"
56 1.1 cgd #include "rtld.h"
57 1.3 cgd
58 1.20 kleink #if !defined(lint)
59 1.3 cgd #include "sysident.h"
60 1.20 kleink #endif
61 1.1 cgd
62 1.25 mycroft #define END_SYM "_end"
63 1.25 mycroft
64 1.1 cgd /*
65 1.1 cgd * Debugging support.
66 1.1 cgd */
67 1.1 cgd
68 1.15 christos typedef void (*funcptr) __P((void));
69 1.1 cgd
70 1.1 cgd /*
71 1.1 cgd * Function declarations.
72 1.1 cgd */
73 1.15 christos static void _rtld_init __P((caddr_t));
74 1.15 christos static void _rtld_exit __P((void));
75 1.15 christos
76 1.15 christos Elf_Addr _rtld __P((Elf_Word *));
77 1.15 christos
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * Data declarations.
81 1.1 cgd */
82 1.15 christos static char *error_message; /* Message for dlopen(), or NULL */
83 1.1 cgd
84 1.15 christos struct r_debug _rtld_debug; /* for GDB; */
85 1.15 christos bool _rtld_trust; /* False for setuid and setgid programs */
86 1.15 christos Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
87 1.15 christos Obj_Entry **_rtld_objtail; /* Link field of last object in list */
88 1.15 christos Obj_Entry *_rtld_objmain; /* The main program shared object */
89 1.15 christos Obj_Entry _rtld_objself; /* The dynamic linker shared object */
90 1.15 christos char _rtld_path[] = _PATH_RTLD;
91 1.27 thorpej unsigned long _rtld_curmark; /* Current mark value */
92 1.25 mycroft Elf_Sym _rtld_sym_zero; /* For resolving undefined weak refs. */
93 1.18 ws #ifdef VARPSZ
94 1.18 ws int _rtld_pagesz; /* Page size, as provided by kernel */
95 1.18 ws #endif
96 1.1 cgd
97 1.25 mycroft Objlist _rtld_list_global = /* Objects dlopened with RTLD_GLOBAL */
98 1.25 mycroft SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
99 1.25 mycroft Objlist _rtld_list_main = /* Objects loaded at program startup */
100 1.25 mycroft SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
101 1.25 mycroft
102 1.22 kleink Search_Path *_rtld_default_paths;
103 1.15 christos Search_Path *_rtld_paths;
104 1.28 christos
105 1.28 christos Library_Xform *_rtld_xforms;
106 1.28 christos
107 1.1 cgd /*
108 1.1 cgd * Global declarations normally provided by crt0.
109 1.1 cgd */
110 1.15 christos char *__progname;
111 1.15 christos char **environ;
112 1.1 cgd
113 1.9 tv #ifdef OLD_GOT
114 1.9 tv extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
115 1.9 tv #else
116 1.9 tv extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
117 1.15 christos extern Elf_Dyn _DYNAMIC;
118 1.1 cgd #endif
119 1.8 tv
120 1.15 christos static void _rtld_call_fini_functions __P((Obj_Entry *));
121 1.15 christos static void _rtld_call_init_functions __P((Obj_Entry *));
122 1.15 christos static Obj_Entry *_rtld_dlcheck __P((void *));
123 1.25 mycroft static void _rtld_init_dag __P((Obj_Entry *));
124 1.25 mycroft static void _rtld_init_dag1 __P((Obj_Entry *, Obj_Entry *));
125 1.25 mycroft static void _rtld_objlist_add __P((Objlist *, Obj_Entry *));
126 1.25 mycroft static Objlist_Entry *_rtld_objlist_find __P((Objlist *, const Obj_Entry *));
127 1.25 mycroft static void _rtld_objlist_remove __P((Objlist *, Obj_Entry *));
128 1.25 mycroft static void _rtld_unload_object __P((Obj_Entry *, bool));
129 1.25 mycroft static void _rtld_unref_dag __P((Obj_Entry *));
130 1.25 mycroft static Obj_Entry *_rtld_obj_from_addr __P((const void *));
131 1.15 christos
132 1.1 cgd static void
133 1.15 christos _rtld_call_fini_functions(first)
134 1.15 christos Obj_Entry *first;
135 1.1 cgd {
136 1.14 pk Obj_Entry *obj;
137 1.1 cgd
138 1.15 christos for (obj = first; obj != NULL; obj = obj->next)
139 1.14 pk if (obj->fini != NULL)
140 1.14 pk (*obj->fini)();
141 1.1 cgd }
142 1.1 cgd
143 1.1 cgd static void
144 1.15 christos _rtld_call_init_functions(first)
145 1.15 christos Obj_Entry *first;
146 1.1 cgd {
147 1.14 pk if (first != NULL) {
148 1.14 pk _rtld_call_init_functions(first->next);
149 1.14 pk if (first->init != NULL)
150 1.14 pk (*first->init)();
151 1.14 pk }
152 1.1 cgd }
153 1.14 pk
154 1.1 cgd /*
155 1.1 cgd * Initialize the dynamic linker. The argument is the address at which
156 1.1 cgd * the dynamic linker has been mapped into memory. The primary task of
157 1.1 cgd * this function is to relocate the dynamic linker.
158 1.1 cgd */
159 1.1 cgd static void
160 1.15 christos _rtld_init(mapbase)
161 1.15 christos caddr_t mapbase;
162 1.1 cgd {
163 1.15 christos Obj_Entry objself;/* The dynamic linker shared object */
164 1.13 christos #ifdef RTLD_RELOCATE_SELF
165 1.14 pk int dodebug = false;
166 1.13 christos #else
167 1.14 pk int dodebug = true;
168 1.13 christos #endif
169 1.1 cgd
170 1.14 pk memset(&objself, 0, sizeof objself);
171 1.14 pk
172 1.14 pk /* Conjure up an Obj_Entry structure for the dynamic linker. */
173 1.14 pk objself.path = NULL;
174 1.14 pk objself.rtld = true;
175 1.14 pk objself.mapbase = mapbase;
176 1.14 pk
177 1.13 christos #if defined(__mips__)
178 1.14 pk /*
179 1.14 pk * mips and ld.so currently linked at load address,
180 1.14 pk * so no relocation needed
181 1.14 pk */
182 1.14 pk objself.relocbase = 0;
183 1.6 mhitch #else
184 1.14 pk objself.relocbase = mapbase;
185 1.6 mhitch #endif
186 1.14 pk
187 1.14 pk objself.pltgot = NULL;
188 1.14 pk
189 1.1 cgd #ifdef OLD_GOT
190 1.15 christos objself.dynamic = (Elf_Dyn *) _GLOBAL_OFFSET_TABLE_[0];
191 1.1 cgd #else
192 1.15 christos objself.dynamic = (Elf_Dyn *) & _DYNAMIC;
193 1.1 cgd #endif
194 1.1 cgd
195 1.13 christos #ifdef RTLD_RELOCATE_SELF
196 1.14 pk /* We have not been relocated yet, so fix the dynamic address */
197 1.14 pk objself.dynamic = (Elf_Dyn *)
198 1.15 christos ((u_long) mapbase + (char *) objself.dynamic);
199 1.15 christos #endif /* RTLD_RELOCATE_SELF */
200 1.13 christos
201 1.14 pk _rtld_digest_dynamic(&objself);
202 1.14 pk
203 1.1 cgd #ifdef __alpha__
204 1.14 pk /* XXX XXX XXX */
205 1.14 pk objself.pltgot = NULL;
206 1.1 cgd #endif
207 1.14 pk assert(objself.needed == NULL);
208 1.14 pk
209 1.12 christos #if !defined(__mips__) && !defined(__i386__)
210 1.14 pk /* no relocation for mips/i386 */
211 1.14 pk assert(!objself.textrel);
212 1.6 mhitch #endif
213 1.1 cgd
214 1.14 pk _rtld_relocate_objects(&objself, true, dodebug);
215 1.13 christos
216 1.14 pk /*
217 1.14 pk * Now that we relocated ourselves, we can use globals.
218 1.14 pk */
219 1.14 pk _rtld_objself = objself;
220 1.13 christos
221 1.14 pk _rtld_objself.path = _rtld_path;
222 1.22 kleink _rtld_add_paths(&_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
223 1.13 christos
224 1.14 pk /*
225 1.14 pk * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
226 1.14 pk */
227 1.14 pk _rtld_objlist = &_rtld_objself;
228 1.1 cgd
229 1.14 pk /* Make the object list empty again. */
230 1.14 pk _rtld_objlist = NULL;
231 1.14 pk _rtld_objtail = &_rtld_objlist;
232 1.1 cgd
233 1.14 pk _rtld_debug.r_brk = _rtld_debug_state;
234 1.14 pk _rtld_debug.r_state = RT_CONSISTENT;
235 1.1 cgd }
236 1.14 pk
237 1.1 cgd /*
238 1.1 cgd * Cleanup procedure. It will be called (by the atexit() mechanism) just
239 1.1 cgd * before the process exits.
240 1.1 cgd */
241 1.1 cgd static void
242 1.15 christos _rtld_exit()
243 1.1 cgd {
244 1.15 christos dbg(("rtld_exit()"));
245 1.1 cgd
246 1.14 pk _rtld_call_fini_functions(_rtld_objlist->next);
247 1.1 cgd }
248 1.14 pk
249 1.1 cgd /*
250 1.1 cgd * Main entry point for dynamic linking. The argument is the stack
251 1.1 cgd * pointer. The stack is expected to be laid out as described in the
252 1.1 cgd * SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
253 1.1 cgd * the stack pointer points to a word containing ARGC. Following that
254 1.1 cgd * in the stack is a null-terminated sequence of pointers to argument
255 1.1 cgd * strings. Then comes a null-terminated sequence of pointers to
256 1.1 cgd * environment strings. Finally, there is a sequence of "auxiliary
257 1.1 cgd * vector" entries.
258 1.1 cgd *
259 1.17 kleink * This function returns the entry point for the main program, the dynamic
260 1.17 kleink * linker's exit procedure in sp[0], and a pointer to the main object in
261 1.17 kleink * sp[1].
262 1.1 cgd */
263 1.1 cgd Elf_Addr
264 1.15 christos _rtld(sp)
265 1.15 christos Elf_Word *sp;
266 1.1 cgd {
267 1.15 christos const AuxInfo *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
268 1.15 christos *pAUX_phent, *pAUX_phnum;
269 1.18 ws #ifdef VARPSZ
270 1.18 ws const AuxInfo *pAUX_pagesz;
271 1.18 ws #endif
272 1.15 christos char **env;
273 1.15 christos const AuxInfo *aux;
274 1.15 christos const AuxInfo *auxp;
275 1.15 christos Elf_Word *const osp = sp;
276 1.15 christos bool bind_now = 0;
277 1.15 christos const char *ld_bind_now;
278 1.15 christos const char **argv;
279 1.25 mycroft Obj_Entry *obj;
280 1.31 erh const char **real___progname;
281 1.31 erh char ***real_environ;
282 1.13 christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
283 1.15 christos int i = 0;
284 1.12 christos #endif
285 1.1 cgd
286 1.15 christos /*
287 1.15 christos * On entry, the dynamic linker itself has not been relocated yet.
288 1.15 christos * Be very careful not to reference any global data until after
289 1.15 christos * _rtld_init has returned. It is OK to reference file-scope statics
290 1.15 christos * and string constants, and to call static and global functions.
291 1.15 christos */
292 1.15 christos /* Find the auxiliary vector on the stack. */
293 1.15 christos /* first Elf_Word reserved to address of exit routine */
294 1.13 christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
295 1.16 christos dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
296 1.15 christos &sp[3], (char *) sp[3]));
297 1.15 christos dbg(("got is at %p, dynamic is at %p\n",
298 1.15 christos _GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
299 1.15 christos debug = 1;
300 1.15 christos dbg(("_ctype_ is %p\n", _ctype_));
301 1.1 cgd #endif
302 1.1 cgd
303 1.15 christos sp += 2; /* skip over return argument space */
304 1.15 christos argv = (const char **) &sp[1];
305 1.15 christos sp += sp[0] + 2; /* Skip over argc, arguments, and NULL
306 1.15 christos * terminator */
307 1.15 christos env = (char **) sp;
308 1.15 christos while (*sp++ != 0) { /* Skip over environment, and NULL terminator */
309 1.13 christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
310 1.15 christos dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
311 1.1 cgd #endif
312 1.15 christos }
313 1.15 christos aux = (const AuxInfo *) sp;
314 1.1 cgd
315 1.15 christos /* Digest the auxiliary vector. */
316 1.15 christos pAUX_base = pAUX_entry = pAUX_execfd = NULL;
317 1.15 christos pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
318 1.18 ws #ifdef VARPSZ
319 1.18 ws pAUX_pagesz = NULL;
320 1.18 ws #endif
321 1.24 kleink for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
322 1.24 kleink switch (auxp->a_type) {
323 1.24 kleink case AT_BASE:
324 1.15 christos pAUX_base = auxp;
325 1.15 christos break;
326 1.24 kleink case AT_ENTRY:
327 1.15 christos pAUX_entry = auxp;
328 1.15 christos break;
329 1.24 kleink case AT_EXECFD:
330 1.15 christos pAUX_execfd = auxp;
331 1.15 christos break;
332 1.24 kleink case AT_PHDR:
333 1.15 christos pAUX_phdr = auxp;
334 1.15 christos break;
335 1.24 kleink case AT_PHENT:
336 1.15 christos pAUX_phent = auxp;
337 1.15 christos break;
338 1.24 kleink case AT_PHNUM:
339 1.15 christos pAUX_phnum = auxp;
340 1.15 christos break;
341 1.18 ws #ifdef VARPSZ
342 1.24 kleink case AT_PAGESZ:
343 1.18 ws pAUX_pagesz = auxp;
344 1.18 ws break;
345 1.18 ws #endif
346 1.15 christos }
347 1.15 christos }
348 1.15 christos
349 1.15 christos /* Initialize and relocate ourselves. */
350 1.15 christos assert(pAUX_base != NULL);
351 1.24 kleink _rtld_init((caddr_t) pAUX_base->a_v);
352 1.18 ws
353 1.18 ws #ifdef VARPSZ
354 1.18 ws assert(pAUX_pagesz != NULL);
355 1.24 kleink _rtld_pagesz = (int)pAUX_pagesz->a_v;
356 1.18 ws #endif
357 1.1 cgd
358 1.1 cgd #ifdef RTLD_DEBUG
359 1.15 christos dbg(("_ctype_ is %p\n", _ctype_));
360 1.1 cgd #endif
361 1.1 cgd
362 1.15 christos __progname = _rtld_objself.path;
363 1.15 christos environ = env;
364 1.1 cgd
365 1.15 christos _rtld_trust = geteuid() == getuid() && getegid() == getgid();
366 1.1 cgd
367 1.15 christos ld_bind_now = getenv("LD_BIND_NOW");
368 1.15 christos if (ld_bind_now != NULL && *ld_bind_now != '\0')
369 1.15 christos bind_now = true;
370 1.15 christos if (_rtld_trust) {
371 1.6 mhitch #ifdef DEBUG
372 1.15 christos const char *ld_debug = getenv("LD_DEBUG");
373 1.15 christos if (ld_debug != NULL && *ld_debug != '\0')
374 1.15 christos debug = 1;
375 1.15 christos #endif
376 1.15 christos _rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
377 1.15 christos }
378 1.28 christos _rtld_process_hints(&_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS, true);
379 1.15 christos dbg(("%s is initialized, base address = %p", __progname,
380 1.24 kleink (void *) pAUX_base->a_v));
381 1.15 christos
382 1.15 christos /*
383 1.15 christos * Load the main program, or process its program header if it is
384 1.15 christos * already loaded.
385 1.15 christos */
386 1.15 christos if (pAUX_execfd != NULL) { /* Load the main program. */
387 1.24 kleink int fd = pAUX_execfd->a_v;
388 1.15 christos dbg(("loading main program"));
389 1.25 mycroft _rtld_objmain = _rtld_map_object(argv[0], fd, NULL);
390 1.15 christos close(fd);
391 1.15 christos if (_rtld_objmain == NULL)
392 1.15 christos _rtld_die();
393 1.15 christos } else { /* Main program already loaded. */
394 1.15 christos const Elf_Phdr *phdr;
395 1.15 christos int phnum;
396 1.15 christos caddr_t entry;
397 1.15 christos
398 1.15 christos dbg(("processing main program's program header"));
399 1.15 christos assert(pAUX_phdr != NULL);
400 1.24 kleink phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
401 1.15 christos assert(pAUX_phnum != NULL);
402 1.24 kleink phnum = pAUX_phnum->a_v;
403 1.15 christos assert(pAUX_phent != NULL);
404 1.24 kleink assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
405 1.15 christos assert(pAUX_entry != NULL);
406 1.24 kleink entry = (caddr_t) pAUX_entry->a_v;
407 1.15 christos _rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
408 1.15 christos }
409 1.15 christos
410 1.15 christos _rtld_objmain->path = xstrdup("main program");
411 1.15 christos _rtld_objmain->mainprog = true;
412 1.25 mycroft
413 1.25 mycroft /*
414 1.25 mycroft * Get the actual dynamic linker pathname from the executable if
415 1.25 mycroft * possible. (It should always be possible.) That ensures that
416 1.25 mycroft * gdb will find the right dynamic linker even if a non-standard
417 1.25 mycroft * one is being used.
418 1.25 mycroft */
419 1.25 mycroft if (_rtld_objmain->interp != NULL &&
420 1.25 mycroft strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0) {
421 1.25 mycroft free(_rtld_objself.path);
422 1.25 mycroft _rtld_objself.path = xstrdup(_rtld_objmain->interp);
423 1.25 mycroft }
424 1.25 mycroft
425 1.15 christos _rtld_digest_dynamic(_rtld_objmain);
426 1.15 christos
427 1.15 christos _rtld_linkmap_add(_rtld_objmain);
428 1.15 christos _rtld_linkmap_add(&_rtld_objself);
429 1.15 christos
430 1.15 christos /* Link the main program into the list of objects. */
431 1.15 christos *_rtld_objtail = _rtld_objmain;
432 1.15 christos _rtld_objtail = &_rtld_objmain->next;
433 1.15 christos ++_rtld_objmain->refcount;
434 1.19 kleink
435 1.25 mycroft /* Initialize a fake symbol for resolving undefined weak references. */
436 1.26 thorpej _rtld_sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
437 1.25 mycroft _rtld_sym_zero.st_shndx = SHN_ABS;
438 1.25 mycroft
439 1.19 kleink /*
440 1.19 kleink * Pre-load user-specified objects after the main program but before
441 1.19 kleink * any shared object dependencies.
442 1.19 kleink */
443 1.19 kleink dbg(("preloading objects"));
444 1.19 kleink if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
445 1.19 kleink _rtld_die();
446 1.15 christos
447 1.15 christos dbg(("loading needed objects"));
448 1.28 christos if (_rtld_load_needed_objects(_rtld_objmain, true) == -1)
449 1.15 christos _rtld_die();
450 1.15 christos
451 1.25 mycroft for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
452 1.25 mycroft _rtld_objlist_add(&_rtld_list_main, obj);
453 1.25 mycroft
454 1.15 christos dbg(("relocating objects"));
455 1.15 christos if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
456 1.15 christos _rtld_die();
457 1.15 christos
458 1.15 christos dbg(("doing copy relocations"));
459 1.15 christos if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
460 1.15 christos _rtld_die();
461 1.15 christos
462 1.31 erh /*
463 1.31 erh * Set the __progname and environ before calling
464 1.31 erh * anything that might use them.
465 1.31 erh */
466 1.31 erh real___progname = _rtld_objmain_sym("__progname");
467 1.31 erh if (real___progname) {
468 1.31 erh if ((*real___progname = strrchr(argv[0], '/')) == NULL)
469 1.31 erh (*real___progname) = argv[0];
470 1.31 erh else
471 1.31 erh (*real___progname)++;
472 1.31 erh }
473 1.31 erh real_environ = _rtld_objmain_sym("environ");
474 1.31 erh if (real_environ)
475 1.31 erh *real_environ = environ;
476 1.31 erh
477 1.15 christos dbg(("calling _init functions"));
478 1.15 christos _rtld_call_init_functions(_rtld_objmain->next);
479 1.15 christos
480 1.15 christos dbg(("control at program entry point = %p, obj = %p, exit = %p",
481 1.15 christos _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
482 1.15 christos
483 1.15 christos /*
484 1.15 christos * Return with the entry point and the exit procedure in at the top
485 1.15 christos * of stack.
486 1.15 christos */
487 1.15 christos
488 1.15 christos _rtld_debug_state(); /* say hello to gdb! */
489 1.15 christos
490 1.15 christos ((void **) osp)[0] = _rtld_exit;
491 1.15 christos ((void **) osp)[1] = _rtld_objmain;
492 1.15 christos return (Elf_Addr) _rtld_objmain->entry;
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd void
496 1.15 christos _rtld_die()
497 1.1 cgd {
498 1.15 christos const char *msg = _rtld_dlerror();
499 1.1 cgd
500 1.15 christos if (msg == NULL)
501 1.15 christos msg = "Fatal error";
502 1.15 christos xerrx(1, "%s\n", msg);
503 1.1 cgd }
504 1.1 cgd
505 1.1 cgd static Obj_Entry *
506 1.15 christos _rtld_dlcheck(handle)
507 1.15 christos void *handle;
508 1.1 cgd {
509 1.15 christos Obj_Entry *obj;
510 1.1 cgd
511 1.15 christos for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
512 1.15 christos if (obj == (Obj_Entry *) handle)
513 1.15 christos break;
514 1.15 christos
515 1.15 christos if (obj == NULL || obj->dl_refcount == 0) {
516 1.15 christos xwarnx("Invalid shared object handle %p", handle);
517 1.15 christos return NULL;
518 1.15 christos }
519 1.15 christos return obj;
520 1.1 cgd }
521 1.1 cgd
522 1.1 cgd static void
523 1.25 mycroft _rtld_init_dag(root)
524 1.25 mycroft Obj_Entry *root;
525 1.25 mycroft {
526 1.27 thorpej _rtld_curmark++;
527 1.25 mycroft _rtld_init_dag1(root, root);
528 1.25 mycroft }
529 1.25 mycroft
530 1.25 mycroft static void
531 1.25 mycroft _rtld_init_dag1(root, obj)
532 1.25 mycroft Obj_Entry *root;
533 1.25 mycroft Obj_Entry *obj;
534 1.25 mycroft {
535 1.25 mycroft const Needed_Entry *needed;
536 1.25 mycroft
537 1.27 thorpej if (obj->mark == _rtld_curmark)
538 1.25 mycroft return;
539 1.27 thorpej obj->mark = _rtld_curmark;
540 1.25 mycroft _rtld_objlist_add(&obj->dldags, root);
541 1.25 mycroft _rtld_objlist_add(&root->dagmembers, obj);
542 1.25 mycroft for (needed = obj->needed; needed != NULL; needed = needed->next)
543 1.25 mycroft if (needed->obj != NULL)
544 1.25 mycroft _rtld_init_dag1(root, needed->obj);
545 1.25 mycroft }
546 1.25 mycroft
547 1.25 mycroft /*
548 1.25 mycroft * Note, this is called only for objects loaded by dlopen().
549 1.25 mycroft */
550 1.25 mycroft static void
551 1.25 mycroft _rtld_unload_object(root, do_fini_funcs)
552 1.25 mycroft Obj_Entry *root;
553 1.25 mycroft bool do_fini_funcs;
554 1.25 mycroft {
555 1.25 mycroft _rtld_unref_dag(root);
556 1.25 mycroft if (root->refcount == 0) { /* We are finished with some objects. */
557 1.25 mycroft Obj_Entry *obj;
558 1.25 mycroft Obj_Entry **linkp;
559 1.25 mycroft Objlist_Entry *elm;
560 1.25 mycroft
561 1.25 mycroft /* Finalize objects that are about to be unmapped. */
562 1.25 mycroft if (do_fini_funcs)
563 1.25 mycroft for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
564 1.25 mycroft if (obj->refcount == 0 && obj->fini != NULL)
565 1.25 mycroft (*obj->fini)();
566 1.25 mycroft
567 1.25 mycroft /* Remove the DAG from all objects' DAG lists. */
568 1.25 mycroft for (elm = SIMPLEQ_FIRST(&root->dagmembers); elm; elm = SIMPLEQ_NEXT(elm, link))
569 1.25 mycroft _rtld_objlist_remove(&elm->obj->dldags, root);
570 1.25 mycroft
571 1.25 mycroft /* Remove the DAG from the RTLD_GLOBAL list. */
572 1.25 mycroft _rtld_objlist_remove(&_rtld_list_global, root);
573 1.25 mycroft
574 1.25 mycroft /* Unmap all objects that are no longer referenced. */
575 1.25 mycroft linkp = &_rtld_objlist->next;
576 1.25 mycroft while ((obj = *linkp) != NULL) {
577 1.25 mycroft if (obj->refcount == 0) {
578 1.25 mycroft #ifdef RTLD_DEBUG
579 1.25 mycroft dbg(("unloading \"%s\"", obj->path));
580 1.25 mycroft #endif
581 1.25 mycroft munmap(obj->mapbase, obj->mapsize);
582 1.25 mycroft _rtld_linkmap_delete(obj);
583 1.25 mycroft *linkp = obj->next;
584 1.25 mycroft _rtld_obj_free(obj);
585 1.25 mycroft } else
586 1.25 mycroft linkp = &obj->next;
587 1.25 mycroft }
588 1.25 mycroft _rtld_objtail = linkp;
589 1.25 mycroft }
590 1.25 mycroft }
591 1.25 mycroft
592 1.25 mycroft static void
593 1.25 mycroft _rtld_unref_dag(root)
594 1.15 christos Obj_Entry *root;
595 1.1 cgd {
596 1.15 christos assert(root->refcount != 0);
597 1.32 jdolecek assert(root);
598 1.15 christos --root->refcount;
599 1.15 christos if (root->refcount == 0) {
600 1.15 christos const Needed_Entry *needed;
601 1.15 christos
602 1.15 christos for (needed = root->needed; needed != NULL;
603 1.32 jdolecek needed = needed->next) {
604 1.32 jdolecek if (needed->obj != NULL)
605 1.32 jdolecek _rtld_unref_dag(needed->obj);
606 1.32 jdolecek }
607 1.15 christos }
608 1.1 cgd }
609 1.1 cgd
610 1.1 cgd int
611 1.15 christos _rtld_dlclose(handle)
612 1.15 christos void *handle;
613 1.1 cgd {
614 1.15 christos Obj_Entry *root = _rtld_dlcheck(handle);
615 1.1 cgd
616 1.15 christos if (root == NULL)
617 1.15 christos return -1;
618 1.1 cgd
619 1.15 christos _rtld_debug.r_state = RT_DELETE;
620 1.15 christos _rtld_debug_state();
621 1.15 christos
622 1.15 christos --root->dl_refcount;
623 1.29 kleink _rtld_unload_object(root, true);
624 1.15 christos
625 1.15 christos _rtld_debug.r_state = RT_CONSISTENT;
626 1.15 christos _rtld_debug_state();
627 1.1 cgd
628 1.15 christos return 0;
629 1.1 cgd }
630 1.1 cgd
631 1.1 cgd char *
632 1.15 christos _rtld_dlerror()
633 1.1 cgd {
634 1.15 christos char *msg = error_message;
635 1.15 christos error_message = NULL;
636 1.15 christos return msg;
637 1.1 cgd }
638 1.1 cgd
639 1.1 cgd void *
640 1.15 christos _rtld_dlopen(name, mode)
641 1.15 christos const char *name;
642 1.15 christos int mode;
643 1.15 christos {
644 1.15 christos Obj_Entry **old_obj_tail = _rtld_objtail;
645 1.15 christos Obj_Entry *obj = NULL;
646 1.15 christos
647 1.15 christos _rtld_debug.r_state = RT_ADD;
648 1.15 christos _rtld_debug_state();
649 1.15 christos
650 1.15 christos if (name == NULL) {
651 1.15 christos obj = _rtld_objmain;
652 1.25 mycroft obj->refcount++;
653 1.15 christos } else {
654 1.21 thorpej char *path = _rtld_find_library(name, _rtld_objmain);
655 1.15 christos if (path != NULL)
656 1.15 christos obj = _rtld_load_object(path, true);
657 1.1 cgd }
658 1.1 cgd
659 1.15 christos if (obj != NULL) {
660 1.15 christos ++obj->dl_refcount;
661 1.25 mycroft if (mode & RTLD_GLOBAL && _rtld_objlist_find(&_rtld_list_global, obj) == NULL)
662 1.25 mycroft _rtld_objlist_add(&_rtld_list_global, obj);
663 1.15 christos if (*old_obj_tail != NULL) { /* We loaded something new. */
664 1.15 christos assert(*old_obj_tail == obj);
665 1.15 christos
666 1.28 christos if (_rtld_load_needed_objects(obj, true) == -1 ||
667 1.28 christos (_rtld_init_dag(obj),
668 1.28 christos _rtld_relocate_objects(obj,
669 1.28 christos ((mode & 3) == RTLD_NOW), true)) == -1) {
670 1.25 mycroft _rtld_unload_object(obj, false);
671 1.25 mycroft obj->dl_refcount--;
672 1.15 christos obj = NULL;
673 1.25 mycroft } else
674 1.15 christos _rtld_call_init_functions(obj);
675 1.15 christos }
676 1.15 christos }
677 1.15 christos _rtld_debug.r_state = RT_CONSISTENT;
678 1.15 christos _rtld_debug_state();
679 1.1 cgd
680 1.15 christos return obj;
681 1.31 erh }
682 1.31 erh
683 1.31 erh /*
684 1.31 erh * Find a symbol in the main program.
685 1.31 erh */
686 1.31 erh void *
687 1.31 erh _rtld_objmain_sym(name)
688 1.31 erh const char *name;
689 1.31 erh {
690 1.31 erh unsigned long hash;
691 1.31 erh const Elf_Sym *def;
692 1.31 erh const Obj_Entry *obj;
693 1.31 erh
694 1.31 erh hash = _rtld_elf_hash(name);
695 1.31 erh obj = _rtld_objmain;
696 1.31 erh
697 1.31 erh _rtld_curmark++;
698 1.31 erh def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, true);
699 1.31 erh
700 1.31 erh if (def != NULL)
701 1.31 erh return obj->relocbase + def->st_value;
702 1.31 erh return(NULL);
703 1.1 cgd }
704 1.1 cgd
705 1.1 cgd void *
706 1.15 christos _rtld_dlsym(handle, name)
707 1.15 christos void *handle;
708 1.15 christos const char *name;
709 1.15 christos {
710 1.25 mycroft const Obj_Entry *obj;
711 1.25 mycroft unsigned long hash;
712 1.25 mycroft const Elf_Sym *def;
713 1.15 christos const Obj_Entry *defobj;
714 1.25 mycroft
715 1.25 mycroft hash = _rtld_elf_hash(name);
716 1.25 mycroft def = NULL;
717 1.25 mycroft defobj = NULL;
718 1.25 mycroft
719 1.30 christos if (handle == NULL
720 1.30 christos #if 0
721 1.30 christos || handle == RTLD_NEXT
722 1.30 christos #endif
723 1.30 christos ) {
724 1.30 christos void *retaddr;
725 1.30 christos
726 1.30 christos retaddr = __builtin_return_address(0); /* __GNUC__ only */
727 1.30 christos if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
728 1.30 christos _rtld_error("Cannot determine caller's shared object");
729 1.30 christos return NULL;
730 1.30 christos }
731 1.30 christos if (handle == NULL) { /* Just the caller's shared object. */
732 1.30 christos def = _rtld_symlook_obj(name, hash, obj, true);
733 1.30 christos defobj = obj;
734 1.30 christos } else { /* All the shared objects after the caller's */
735 1.30 christos while ((obj = obj->next) != NULL) {
736 1.30 christos if ((def = _rtld_symlook_obj(name, hash, obj, true)) != NULL) {
737 1.30 christos defobj = obj;
738 1.30 christos break;
739 1.25 mycroft }
740 1.25 mycroft }
741 1.30 christos }
742 1.30 christos } else {
743 1.30 christos if ((obj = _rtld_dlcheck(handle)) == NULL)
744 1.30 christos return NULL;
745 1.30 christos
746 1.30 christos if (obj->mainprog) {
747 1.30 christos /* Search main program and all libraries loaded by it. */
748 1.30 christos _rtld_curmark++;
749 1.30 christos def = _rtld_symlook_list(name, hash, &_rtld_list_main, &defobj, true);
750 1.25 mycroft } else {
751 1.30 christos /*
752 1.30 christos * XXX - This isn't correct. The search should include the whole
753 1.30 christos * DAG rooted at the given object.
754 1.30 christos */
755 1.30 christos def = _rtld_symlook_obj(name, hash, obj, true);
756 1.30 christos defobj = obj;
757 1.25 mycroft }
758 1.30 christos }
759 1.30 christos
760 1.30 christos if (def != NULL)
761 1.30 christos return defobj->relocbase + def->st_value;
762 1.30 christos
763 1.30 christos _rtld_error("Undefined symbol \"%s\"", name);
764 1.25 mycroft return NULL;
765 1.25 mycroft }
766 1.1 cgd
767 1.25 mycroft int
768 1.25 mycroft _rtld_dladdr(addr, info)
769 1.25 mycroft const void *addr;
770 1.25 mycroft Dl_info *info;
771 1.25 mycroft {
772 1.25 mycroft const Obj_Entry *obj;
773 1.25 mycroft const Elf_Sym *def;
774 1.25 mycroft void *symbol_addr;
775 1.25 mycroft unsigned long symoffset;
776 1.25 mycroft
777 1.25 mycroft obj = _rtld_obj_from_addr(addr);
778 1.25 mycroft if (obj == NULL) {
779 1.25 mycroft _rtld_error("No shared object contains address");
780 1.25 mycroft return 0;
781 1.25 mycroft }
782 1.25 mycroft info->dli_fname = obj->path;
783 1.25 mycroft info->dli_fbase = obj->mapbase;
784 1.25 mycroft info->dli_saddr = (void *)0;
785 1.25 mycroft info->dli_sname = NULL;
786 1.25 mycroft
787 1.15 christos /*
788 1.25 mycroft * Walk the symbol list looking for the symbol whose address is
789 1.25 mycroft * closest to the address sent in.
790 1.25 mycroft */
791 1.25 mycroft for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
792 1.25 mycroft def = obj->symtab + symoffset;
793 1.25 mycroft
794 1.25 mycroft /*
795 1.25 mycroft * For skip the symbol if st_shndx is either SHN_UNDEF or
796 1.25 mycroft * SHN_COMMON.
797 1.25 mycroft */
798 1.25 mycroft if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
799 1.25 mycroft continue;
800 1.25 mycroft
801 1.25 mycroft /*
802 1.25 mycroft * If the symbol is greater than the specified address, or if it
803 1.25 mycroft * is further away from addr than the current nearest symbol,
804 1.25 mycroft * then reject it.
805 1.25 mycroft */
806 1.25 mycroft symbol_addr = obj->relocbase + def->st_value;
807 1.25 mycroft if (symbol_addr > addr || symbol_addr < info->dli_saddr)
808 1.25 mycroft continue;
809 1.25 mycroft
810 1.25 mycroft /* Update our idea of the nearest symbol. */
811 1.25 mycroft info->dli_sname = obj->strtab + def->st_name;
812 1.25 mycroft info->dli_saddr = symbol_addr;
813 1.1 cgd
814 1.25 mycroft /* Exact match? */
815 1.25 mycroft if (info->dli_saddr == addr)
816 1.25 mycroft break;
817 1.25 mycroft }
818 1.25 mycroft return 1;
819 1.1 cgd }
820 1.1 cgd
821 1.1 cgd /*
822 1.1 cgd * Error reporting function. Use it like printf. If formats the message
823 1.1 cgd * into a buffer, and sets things up so that the next call to dlerror()
824 1.1 cgd * will return the message.
825 1.1 cgd */
826 1.1 cgd void
827 1.15 christos #ifdef __STDC__
828 1.15 christos _rtld_error(const char *fmt,...)
829 1.15 christos #else
830 1.15 christos _rtld_error(va_alist)
831 1.15 christos va_dcl
832 1.15 christos #endif
833 1.1 cgd {
834 1.15 christos static char buf[512];
835 1.15 christos va_list ap;
836 1.15 christos #ifdef __STDC__
837 1.15 christos va_start(ap, fmt);
838 1.15 christos #else
839 1.15 christos const char *fmt;
840 1.1 cgd
841 1.15 christos va_start(ap);
842 1.15 christos fmt = va_arg(ap, const char *);
843 1.15 christos #endif
844 1.15 christos xvsnprintf(buf, sizeof buf, fmt, ap);
845 1.15 christos error_message = buf;
846 1.15 christos va_end(ap);
847 1.1 cgd }
848 1.14 pk
849 1.1 cgd void
850 1.15 christos _rtld_debug_state()
851 1.1 cgd {
852 1.15 christos /* do nothing */
853 1.1 cgd }
854 1.1 cgd
855 1.1 cgd void
856 1.15 christos _rtld_linkmap_add(obj)
857 1.15 christos Obj_Entry *obj;
858 1.1 cgd {
859 1.15 christos struct link_map *l = &obj->linkmap;
860 1.15 christos struct link_map *prev;
861 1.1 cgd
862 1.15 christos obj->linkmap.l_name = obj->path;
863 1.15 christos obj->linkmap.l_addr = obj->mapbase;
864 1.15 christos obj->linkmap.l_ld = obj->dynamic;
865 1.6 mhitch #ifdef __mips__
866 1.15 christos /* GDB needs load offset on MIPS to use the symbols */
867 1.15 christos obj->linkmap.l_offs = obj->relocbase;
868 1.6 mhitch #endif
869 1.1 cgd
870 1.15 christos if (_rtld_debug.r_map == NULL) {
871 1.15 christos _rtld_debug.r_map = l;
872 1.15 christos return;
873 1.15 christos }
874 1.15 christos for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
875 1.15 christos l->l_prev = prev;
876 1.15 christos prev->l_next = l;
877 1.15 christos l->l_next = NULL;
878 1.1 cgd }
879 1.1 cgd
880 1.1 cgd void
881 1.15 christos _rtld_linkmap_delete(obj)
882 1.15 christos Obj_Entry *obj;
883 1.1 cgd {
884 1.15 christos struct link_map *l = &obj->linkmap;
885 1.1 cgd
886 1.15 christos if (l->l_prev == NULL) {
887 1.15 christos if ((_rtld_debug.r_map = l->l_next) != NULL)
888 1.15 christos l->l_next->l_prev = NULL;
889 1.15 christos return;
890 1.15 christos }
891 1.15 christos if ((l->l_prev->l_next = l->l_next) != NULL)
892 1.15 christos l->l_next->l_prev = l->l_prev;
893 1.25 mycroft }
894 1.25 mycroft
895 1.25 mycroft static Obj_Entry *
896 1.25 mycroft _rtld_obj_from_addr(const void *addr)
897 1.25 mycroft {
898 1.25 mycroft unsigned long endhash;
899 1.25 mycroft Obj_Entry *obj;
900 1.25 mycroft
901 1.25 mycroft endhash = _rtld_elf_hash(END_SYM);
902 1.25 mycroft for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
903 1.25 mycroft const Elf_Sym *endsym;
904 1.25 mycroft
905 1.25 mycroft if (addr < (void *) obj->mapbase)
906 1.25 mycroft continue;
907 1.25 mycroft if ((endsym = _rtld_symlook_obj(END_SYM, endhash, obj, true)) == NULL)
908 1.25 mycroft continue; /* No "end" symbol?! */
909 1.25 mycroft if (addr < (void *) (obj->relocbase + endsym->st_value))
910 1.25 mycroft return obj;
911 1.25 mycroft }
912 1.25 mycroft return NULL;
913 1.25 mycroft }
914 1.25 mycroft
915 1.25 mycroft static void
916 1.25 mycroft _rtld_objlist_add(list, obj)
917 1.25 mycroft Objlist *list;
918 1.25 mycroft Obj_Entry *obj;
919 1.25 mycroft {
920 1.25 mycroft Objlist_Entry *elm;
921 1.25 mycroft
922 1.25 mycroft elm = NEW(Objlist_Entry);
923 1.25 mycroft elm->obj = obj;
924 1.25 mycroft SIMPLEQ_INSERT_TAIL(list, elm, link);
925 1.25 mycroft }
926 1.25 mycroft
927 1.25 mycroft static Objlist_Entry *
928 1.25 mycroft _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
929 1.25 mycroft {
930 1.25 mycroft Objlist_Entry *elm;
931 1.25 mycroft
932 1.25 mycroft for (elm = SIMPLEQ_FIRST(list); elm; elm = SIMPLEQ_NEXT(elm, link)) {
933 1.25 mycroft if (elm->obj == obj)
934 1.25 mycroft return elm;
935 1.25 mycroft }
936 1.25 mycroft return NULL;
937 1.25 mycroft }
938 1.25 mycroft
939 1.25 mycroft static void
940 1.25 mycroft _rtld_objlist_remove(list, obj)
941 1.25 mycroft Objlist *list;
942 1.25 mycroft Obj_Entry *obj;
943 1.25 mycroft {
944 1.25 mycroft Objlist_Entry *elm;
945 1.25 mycroft
946 1.25 mycroft if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
947 1.25 mycroft if ((list)->sqh_first == (elm)) {
948 1.25 mycroft SIMPLEQ_REMOVE_HEAD(list, elm, link);
949 1.25 mycroft }
950 1.25 mycroft else {
951 1.25 mycroft struct Struct_Objlist_Entry *curelm = (list)->sqh_first;
952 1.25 mycroft while (curelm->link.sqe_next != (elm))
953 1.25 mycroft curelm = curelm->link.sqe_next;
954 1.25 mycroft if((curelm->link.sqe_next =
955 1.25 mycroft curelm->link.sqe_next->link.sqe_next) == NULL)
956 1.25 mycroft (list)->sqh_last = &(curelm)->link.sqe_next;
957 1.25 mycroft }
958 1.25 mycroft free(elm);
959 1.25 mycroft }
960 1.1 cgd }
961