rtld.c revision 1.23 1 1.23 christos /* $NetBSD: rtld.c,v 1.23 1999/08/19 23:42:15 christos 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.1 cgd /*
63 1.1 cgd * Debugging support.
64 1.1 cgd */
65 1.1 cgd
66 1.15 christos typedef void (*funcptr) __P((void));
67 1.1 cgd
68 1.1 cgd /*
69 1.1 cgd * Function declarations.
70 1.1 cgd */
71 1.15 christos static void _rtld_init __P((caddr_t));
72 1.15 christos static void _rtld_exit __P((void));
73 1.15 christos
74 1.15 christos Elf_Addr _rtld __P((Elf_Word *));
75 1.15 christos
76 1.1 cgd
77 1.1 cgd /*
78 1.1 cgd * Data declarations.
79 1.1 cgd */
80 1.15 christos static char *error_message; /* Message for dlopen(), or NULL */
81 1.1 cgd
82 1.15 christos struct r_debug _rtld_debug; /* for GDB; */
83 1.15 christos bool _rtld_trust; /* False for setuid and setgid programs */
84 1.15 christos Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
85 1.15 christos Obj_Entry **_rtld_objtail; /* Link field of last object in list */
86 1.15 christos Obj_Entry *_rtld_objmain; /* The main program shared object */
87 1.15 christos Obj_Entry _rtld_objself; /* The dynamic linker shared object */
88 1.15 christos char _rtld_path[] = _PATH_RTLD;
89 1.18 ws #ifdef VARPSZ
90 1.18 ws int _rtld_pagesz; /* Page size, as provided by kernel */
91 1.18 ws #endif
92 1.1 cgd
93 1.22 kleink Search_Path *_rtld_default_paths;
94 1.15 christos Search_Path *_rtld_paths;
95 1.1 cgd /*
96 1.1 cgd * Global declarations normally provided by crt0.
97 1.1 cgd */
98 1.15 christos char *__progname;
99 1.15 christos char **environ;
100 1.1 cgd
101 1.9 tv #ifdef OLD_GOT
102 1.9 tv extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
103 1.9 tv #else
104 1.9 tv extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
105 1.15 christos extern Elf_Dyn _DYNAMIC;
106 1.1 cgd #endif
107 1.8 tv
108 1.15 christos static void _rtld_call_fini_functions __P((Obj_Entry *));
109 1.15 christos static void _rtld_call_init_functions __P((Obj_Entry *));
110 1.15 christos static Obj_Entry *_rtld_dlcheck __P((void *));
111 1.15 christos static void _rtld_unref_object_dag __P((Obj_Entry *));
112 1.15 christos
113 1.1 cgd static void
114 1.15 christos _rtld_call_fini_functions(first)
115 1.15 christos Obj_Entry *first;
116 1.1 cgd {
117 1.14 pk Obj_Entry *obj;
118 1.1 cgd
119 1.15 christos for (obj = first; obj != NULL; obj = obj->next)
120 1.14 pk if (obj->fini != NULL)
121 1.14 pk (*obj->fini)();
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd static void
125 1.15 christos _rtld_call_init_functions(first)
126 1.15 christos Obj_Entry *first;
127 1.1 cgd {
128 1.14 pk if (first != NULL) {
129 1.14 pk _rtld_call_init_functions(first->next);
130 1.14 pk if (first->init != NULL)
131 1.14 pk (*first->init)();
132 1.14 pk }
133 1.1 cgd }
134 1.14 pk
135 1.1 cgd /*
136 1.1 cgd * Initialize the dynamic linker. The argument is the address at which
137 1.1 cgd * the dynamic linker has been mapped into memory. The primary task of
138 1.1 cgd * this function is to relocate the dynamic linker.
139 1.1 cgd */
140 1.1 cgd static void
141 1.15 christos _rtld_init(mapbase)
142 1.15 christos caddr_t mapbase;
143 1.1 cgd {
144 1.15 christos Obj_Entry objself;/* The dynamic linker shared object */
145 1.13 christos #ifdef RTLD_RELOCATE_SELF
146 1.14 pk int dodebug = false;
147 1.13 christos #else
148 1.14 pk int dodebug = true;
149 1.13 christos #endif
150 1.1 cgd
151 1.14 pk memset(&objself, 0, sizeof objself);
152 1.14 pk
153 1.14 pk /* Conjure up an Obj_Entry structure for the dynamic linker. */
154 1.14 pk objself.path = NULL;
155 1.14 pk objself.rtld = true;
156 1.14 pk objself.mapbase = mapbase;
157 1.14 pk
158 1.13 christos #if defined(__mips__)
159 1.14 pk /*
160 1.14 pk * mips and ld.so currently linked at load address,
161 1.14 pk * so no relocation needed
162 1.14 pk */
163 1.14 pk objself.relocbase = 0;
164 1.6 mhitch #else
165 1.14 pk objself.relocbase = mapbase;
166 1.6 mhitch #endif
167 1.14 pk
168 1.14 pk objself.pltgot = NULL;
169 1.14 pk
170 1.1 cgd #ifdef OLD_GOT
171 1.15 christos objself.dynamic = (Elf_Dyn *) _GLOBAL_OFFSET_TABLE_[0];
172 1.1 cgd #else
173 1.15 christos objself.dynamic = (Elf_Dyn *) & _DYNAMIC;
174 1.1 cgd #endif
175 1.1 cgd
176 1.13 christos #ifdef RTLD_RELOCATE_SELF
177 1.14 pk /* We have not been relocated yet, so fix the dynamic address */
178 1.14 pk objself.dynamic = (Elf_Dyn *)
179 1.15 christos ((u_long) mapbase + (char *) objself.dynamic);
180 1.15 christos #endif /* RTLD_RELOCATE_SELF */
181 1.13 christos
182 1.14 pk _rtld_digest_dynamic(&objself);
183 1.14 pk
184 1.1 cgd #ifdef __alpha__
185 1.14 pk /* XXX XXX XXX */
186 1.14 pk objself.pltgot = NULL;
187 1.1 cgd #endif
188 1.14 pk assert(objself.needed == NULL);
189 1.14 pk
190 1.12 christos #if !defined(__mips__) && !defined(__i386__)
191 1.14 pk /* no relocation for mips/i386 */
192 1.14 pk assert(!objself.textrel);
193 1.6 mhitch #endif
194 1.1 cgd
195 1.14 pk _rtld_relocate_objects(&objself, true, dodebug);
196 1.13 christos
197 1.14 pk /*
198 1.14 pk * Now that we relocated ourselves, we can use globals.
199 1.14 pk */
200 1.14 pk _rtld_objself = objself;
201 1.13 christos
202 1.14 pk _rtld_objself.path = _rtld_path;
203 1.22 kleink _rtld_add_paths(&_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
204 1.13 christos
205 1.14 pk /*
206 1.14 pk * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
207 1.14 pk */
208 1.14 pk _rtld_objlist = &_rtld_objself;
209 1.1 cgd
210 1.14 pk /* Make the object list empty again. */
211 1.14 pk _rtld_objlist = NULL;
212 1.14 pk _rtld_objtail = &_rtld_objlist;
213 1.1 cgd
214 1.14 pk _rtld_debug.r_brk = _rtld_debug_state;
215 1.14 pk _rtld_debug.r_state = RT_CONSISTENT;
216 1.1 cgd }
217 1.14 pk
218 1.1 cgd /*
219 1.1 cgd * Cleanup procedure. It will be called (by the atexit() mechanism) just
220 1.1 cgd * before the process exits.
221 1.1 cgd */
222 1.1 cgd static void
223 1.15 christos _rtld_exit()
224 1.1 cgd {
225 1.15 christos dbg(("rtld_exit()"));
226 1.1 cgd
227 1.14 pk _rtld_call_fini_functions(_rtld_objlist->next);
228 1.1 cgd }
229 1.14 pk
230 1.1 cgd /*
231 1.1 cgd * Main entry point for dynamic linking. The argument is the stack
232 1.1 cgd * pointer. The stack is expected to be laid out as described in the
233 1.1 cgd * SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
234 1.1 cgd * the stack pointer points to a word containing ARGC. Following that
235 1.1 cgd * in the stack is a null-terminated sequence of pointers to argument
236 1.1 cgd * strings. Then comes a null-terminated sequence of pointers to
237 1.1 cgd * environment strings. Finally, there is a sequence of "auxiliary
238 1.1 cgd * vector" entries.
239 1.1 cgd *
240 1.17 kleink * This function returns the entry point for the main program, the dynamic
241 1.17 kleink * linker's exit procedure in sp[0], and a pointer to the main object in
242 1.17 kleink * sp[1].
243 1.1 cgd */
244 1.1 cgd Elf_Addr
245 1.15 christos _rtld(sp)
246 1.15 christos Elf_Word *sp;
247 1.1 cgd {
248 1.15 christos const AuxInfo *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
249 1.15 christos *pAUX_phent, *pAUX_phnum;
250 1.18 ws #ifdef VARPSZ
251 1.18 ws const AuxInfo *pAUX_pagesz;
252 1.18 ws #endif
253 1.15 christos char **env;
254 1.15 christos const AuxInfo *aux;
255 1.15 christos const AuxInfo *auxp;
256 1.15 christos Elf_Word *const osp = sp;
257 1.15 christos bool bind_now = 0;
258 1.15 christos const char *ld_bind_now;
259 1.15 christos const char **argv;
260 1.13 christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
261 1.15 christos int i = 0;
262 1.12 christos #endif
263 1.1 cgd
264 1.15 christos /*
265 1.15 christos * On entry, the dynamic linker itself has not been relocated yet.
266 1.15 christos * Be very careful not to reference any global data until after
267 1.15 christos * _rtld_init has returned. It is OK to reference file-scope statics
268 1.15 christos * and string constants, and to call static and global functions.
269 1.15 christos */
270 1.15 christos /* Find the auxiliary vector on the stack. */
271 1.15 christos /* first Elf_Word reserved to address of exit routine */
272 1.13 christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
273 1.16 christos dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
274 1.15 christos &sp[3], (char *) sp[3]));
275 1.15 christos dbg(("got is at %p, dynamic is at %p\n",
276 1.15 christos _GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
277 1.15 christos debug = 1;
278 1.15 christos dbg(("_ctype_ is %p\n", _ctype_));
279 1.1 cgd #endif
280 1.1 cgd
281 1.15 christos sp += 2; /* skip over return argument space */
282 1.15 christos argv = (const char **) &sp[1];
283 1.15 christos sp += sp[0] + 2; /* Skip over argc, arguments, and NULL
284 1.15 christos * terminator */
285 1.15 christos env = (char **) sp;
286 1.15 christos while (*sp++ != 0) { /* Skip over environment, and NULL terminator */
287 1.13 christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
288 1.15 christos dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
289 1.1 cgd #endif
290 1.15 christos }
291 1.15 christos aux = (const AuxInfo *) sp;
292 1.1 cgd
293 1.15 christos /* Digest the auxiliary vector. */
294 1.15 christos pAUX_base = pAUX_entry = pAUX_execfd = NULL;
295 1.15 christos pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
296 1.18 ws #ifdef VARPSZ
297 1.18 ws pAUX_pagesz = NULL;
298 1.18 ws #endif
299 1.15 christos for (auxp = aux; auxp->au_id != AUX_null; ++auxp) {
300 1.15 christos switch (auxp->au_id) {
301 1.15 christos case AUX_base:
302 1.15 christos pAUX_base = auxp;
303 1.15 christos break;
304 1.15 christos case AUX_entry:
305 1.15 christos pAUX_entry = auxp;
306 1.15 christos break;
307 1.15 christos case AUX_execfd:
308 1.15 christos pAUX_execfd = auxp;
309 1.15 christos break;
310 1.15 christos case AUX_phdr:
311 1.15 christos pAUX_phdr = auxp;
312 1.15 christos break;
313 1.15 christos case AUX_phent:
314 1.15 christos pAUX_phent = auxp;
315 1.15 christos break;
316 1.15 christos case AUX_phnum:
317 1.15 christos pAUX_phnum = auxp;
318 1.15 christos break;
319 1.18 ws #ifdef VARPSZ
320 1.18 ws case AUX_pagesz:
321 1.18 ws pAUX_pagesz = auxp;
322 1.18 ws break;
323 1.18 ws #endif
324 1.15 christos }
325 1.15 christos }
326 1.15 christos
327 1.15 christos /* Initialize and relocate ourselves. */
328 1.15 christos assert(pAUX_base != NULL);
329 1.15 christos _rtld_init((caddr_t) pAUX_base->au_v);
330 1.18 ws
331 1.18 ws #ifdef VARPSZ
332 1.18 ws assert(pAUX_pagesz != NULL);
333 1.18 ws _rtld_pagesz = (int)pAUX_pagesz->au_v;
334 1.18 ws #endif
335 1.1 cgd
336 1.1 cgd #ifdef RTLD_DEBUG
337 1.15 christos dbg(("_ctype_ is %p\n", _ctype_));
338 1.1 cgd #endif
339 1.1 cgd
340 1.15 christos __progname = _rtld_objself.path;
341 1.15 christos environ = env;
342 1.1 cgd
343 1.15 christos _rtld_trust = geteuid() == getuid() && getegid() == getgid();
344 1.1 cgd
345 1.15 christos ld_bind_now = getenv("LD_BIND_NOW");
346 1.15 christos if (ld_bind_now != NULL && *ld_bind_now != '\0')
347 1.15 christos bind_now = true;
348 1.15 christos if (_rtld_trust) {
349 1.6 mhitch #ifdef DEBUG
350 1.15 christos const char *ld_debug = getenv("LD_DEBUG");
351 1.15 christos if (ld_debug != NULL && *ld_debug != '\0')
352 1.15 christos debug = 1;
353 1.15 christos #endif
354 1.15 christos _rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
355 1.15 christos }
356 1.23 christos _rtld_process_hints(&_rtld_paths, _PATH_LD_HINTS, true);
357 1.15 christos dbg(("%s is initialized, base address = %p", __progname,
358 1.15 christos (void *) pAUX_base->au_v));
359 1.15 christos
360 1.15 christos /*
361 1.15 christos * Load the main program, or process its program header if it is
362 1.15 christos * already loaded.
363 1.15 christos */
364 1.15 christos if (pAUX_execfd != NULL) { /* Load the main program. */
365 1.15 christos int fd = pAUX_execfd->au_v;
366 1.15 christos dbg(("loading main program"));
367 1.15 christos _rtld_objmain = _rtld_map_object(argv[0], fd);
368 1.15 christos close(fd);
369 1.15 christos if (_rtld_objmain == NULL)
370 1.15 christos _rtld_die();
371 1.15 christos } else { /* Main program already loaded. */
372 1.15 christos const Elf_Phdr *phdr;
373 1.15 christos int phnum;
374 1.15 christos caddr_t entry;
375 1.15 christos
376 1.15 christos dbg(("processing main program's program header"));
377 1.15 christos assert(pAUX_phdr != NULL);
378 1.15 christos phdr = (const Elf_Phdr *) pAUX_phdr->au_v;
379 1.15 christos assert(pAUX_phnum != NULL);
380 1.15 christos phnum = pAUX_phnum->au_v;
381 1.15 christos assert(pAUX_phent != NULL);
382 1.15 christos assert(pAUX_phent->au_v == sizeof(Elf_Phdr));
383 1.15 christos assert(pAUX_entry != NULL);
384 1.15 christos entry = (caddr_t) pAUX_entry->au_v;
385 1.15 christos _rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
386 1.15 christos }
387 1.15 christos
388 1.15 christos _rtld_objmain->path = xstrdup("main program");
389 1.15 christos _rtld_objmain->mainprog = true;
390 1.15 christos _rtld_digest_dynamic(_rtld_objmain);
391 1.15 christos
392 1.15 christos _rtld_linkmap_add(_rtld_objmain);
393 1.15 christos _rtld_linkmap_add(&_rtld_objself);
394 1.15 christos
395 1.15 christos /* Link the main program into the list of objects. */
396 1.15 christos *_rtld_objtail = _rtld_objmain;
397 1.15 christos _rtld_objtail = &_rtld_objmain->next;
398 1.15 christos ++_rtld_objmain->refcount;
399 1.19 kleink
400 1.19 kleink /*
401 1.19 kleink * Pre-load user-specified objects after the main program but before
402 1.19 kleink * any shared object dependencies.
403 1.19 kleink */
404 1.19 kleink dbg(("preloading objects"));
405 1.19 kleink if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
406 1.19 kleink _rtld_die();
407 1.15 christos
408 1.15 christos dbg(("loading needed objects"));
409 1.15 christos if (_rtld_load_needed_objects(_rtld_objmain) == -1)
410 1.15 christos _rtld_die();
411 1.15 christos
412 1.15 christos dbg(("relocating objects"));
413 1.15 christos if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
414 1.15 christos _rtld_die();
415 1.15 christos
416 1.15 christos dbg(("doing copy relocations"));
417 1.15 christos if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
418 1.15 christos _rtld_die();
419 1.15 christos
420 1.15 christos dbg(("calling _init functions"));
421 1.15 christos _rtld_call_init_functions(_rtld_objmain->next);
422 1.15 christos
423 1.15 christos dbg(("control at program entry point = %p, obj = %p, exit = %p",
424 1.15 christos _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
425 1.15 christos
426 1.15 christos /*
427 1.15 christos * Return with the entry point and the exit procedure in at the top
428 1.15 christos * of stack.
429 1.15 christos */
430 1.15 christos
431 1.15 christos _rtld_debug_state(); /* say hello to gdb! */
432 1.15 christos
433 1.15 christos ((void **) osp)[0] = _rtld_exit;
434 1.15 christos ((void **) osp)[1] = _rtld_objmain;
435 1.15 christos return (Elf_Addr) _rtld_objmain->entry;
436 1.1 cgd }
437 1.1 cgd
438 1.1 cgd void
439 1.15 christos _rtld_die()
440 1.1 cgd {
441 1.15 christos const char *msg = _rtld_dlerror();
442 1.1 cgd
443 1.15 christos if (msg == NULL)
444 1.15 christos msg = "Fatal error";
445 1.15 christos xerrx(1, "%s\n", msg);
446 1.1 cgd }
447 1.1 cgd
448 1.1 cgd static Obj_Entry *
449 1.15 christos _rtld_dlcheck(handle)
450 1.15 christos void *handle;
451 1.1 cgd {
452 1.15 christos Obj_Entry *obj;
453 1.1 cgd
454 1.15 christos for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
455 1.15 christos if (obj == (Obj_Entry *) handle)
456 1.15 christos break;
457 1.15 christos
458 1.15 christos if (obj == NULL || obj->dl_refcount == 0) {
459 1.15 christos xwarnx("Invalid shared object handle %p", handle);
460 1.15 christos return NULL;
461 1.15 christos }
462 1.15 christos return obj;
463 1.1 cgd }
464 1.1 cgd
465 1.1 cgd static void
466 1.15 christos _rtld_unref_object_dag(root)
467 1.15 christos Obj_Entry *root;
468 1.1 cgd {
469 1.15 christos assert(root->refcount != 0);
470 1.15 christos --root->refcount;
471 1.15 christos if (root->refcount == 0) {
472 1.15 christos const Needed_Entry *needed;
473 1.15 christos
474 1.15 christos for (needed = root->needed; needed != NULL;
475 1.15 christos needed = needed->next)
476 1.15 christos _rtld_unref_object_dag(needed->obj);
477 1.15 christos }
478 1.1 cgd }
479 1.1 cgd
480 1.1 cgd int
481 1.15 christos _rtld_dlclose(handle)
482 1.15 christos void *handle;
483 1.1 cgd {
484 1.15 christos Obj_Entry *root = _rtld_dlcheck(handle);
485 1.1 cgd
486 1.15 christos if (root == NULL)
487 1.15 christos return -1;
488 1.1 cgd
489 1.15 christos _rtld_debug.r_state = RT_DELETE;
490 1.15 christos _rtld_debug_state();
491 1.15 christos
492 1.15 christos --root->dl_refcount;
493 1.15 christos _rtld_unref_object_dag(root);
494 1.15 christos if (root->refcount == 0) { /* We are finished with some objects. */
495 1.15 christos Obj_Entry *obj;
496 1.15 christos Obj_Entry **linkp;
497 1.15 christos
498 1.15 christos /* Finalize objects that are about to be unmapped. */
499 1.15 christos for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
500 1.15 christos if (obj->refcount == 0 && obj->fini != NULL)
501 1.15 christos (*obj->fini) ();
502 1.15 christos
503 1.15 christos /* Unmap all objects that are no longer referenced. */
504 1.15 christos linkp = &_rtld_objlist->next;
505 1.15 christos while ((obj = *linkp) != NULL) {
506 1.15 christos if (obj->refcount == 0) {
507 1.15 christos munmap(obj->mapbase, obj->mapsize);
508 1.15 christos free(obj->path);
509 1.15 christos while (obj->needed != NULL) {
510 1.15 christos Needed_Entry *needed = obj->needed;
511 1.15 christos obj->needed = needed->next;
512 1.15 christos free(needed);
513 1.15 christos }
514 1.15 christos _rtld_linkmap_delete(obj);
515 1.15 christos *linkp = obj->next;
516 1.15 christos if (obj->next == NULL)
517 1.15 christos _rtld_objtail = linkp;
518 1.15 christos free(obj);
519 1.15 christos } else
520 1.15 christos linkp = &obj->next;
521 1.1 cgd }
522 1.1 cgd }
523 1.15 christos _rtld_debug.r_state = RT_CONSISTENT;
524 1.15 christos _rtld_debug_state();
525 1.1 cgd
526 1.15 christos return 0;
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd char *
530 1.15 christos _rtld_dlerror()
531 1.1 cgd {
532 1.15 christos char *msg = error_message;
533 1.15 christos error_message = NULL;
534 1.15 christos return msg;
535 1.1 cgd }
536 1.1 cgd
537 1.1 cgd void *
538 1.15 christos _rtld_dlopen(name, mode)
539 1.15 christos const char *name;
540 1.15 christos int mode;
541 1.15 christos {
542 1.15 christos Obj_Entry **old_obj_tail = _rtld_objtail;
543 1.15 christos Obj_Entry *obj = NULL;
544 1.15 christos
545 1.15 christos _rtld_debug.r_state = RT_ADD;
546 1.15 christos _rtld_debug_state();
547 1.15 christos
548 1.15 christos if (name == NULL) {
549 1.15 christos obj = _rtld_objmain;
550 1.15 christos } else {
551 1.21 thorpej char *path = _rtld_find_library(name, _rtld_objmain);
552 1.15 christos if (path != NULL)
553 1.15 christos obj = _rtld_load_object(path, true);
554 1.1 cgd }
555 1.1 cgd
556 1.15 christos if (obj != NULL) {
557 1.15 christos ++obj->dl_refcount;
558 1.15 christos if (*old_obj_tail != NULL) { /* We loaded something new. */
559 1.15 christos assert(*old_obj_tail == obj);
560 1.15 christos
561 1.15 christos /* FIXME - Clean up properly after an error. */
562 1.15 christos if (_rtld_load_needed_objects(obj) == -1) {
563 1.15 christos --obj->dl_refcount;
564 1.15 christos obj = NULL;
565 1.15 christos } else if (_rtld_relocate_objects(obj,
566 1.15 christos (mode & 3) == RTLD_NOW, true) == -1) {
567 1.15 christos --obj->dl_refcount;
568 1.15 christos obj = NULL;
569 1.15 christos } else {
570 1.15 christos _rtld_call_init_functions(obj);
571 1.15 christos }
572 1.15 christos }
573 1.15 christos }
574 1.15 christos _rtld_debug.r_state = RT_CONSISTENT;
575 1.15 christos _rtld_debug_state();
576 1.1 cgd
577 1.15 christos return obj;
578 1.1 cgd }
579 1.1 cgd
580 1.1 cgd void *
581 1.15 christos _rtld_dlsym(handle, name)
582 1.15 christos void *handle;
583 1.15 christos const char *name;
584 1.15 christos {
585 1.15 christos const Obj_Entry *obj = _rtld_dlcheck(handle);
586 1.15 christos const Elf_Sym *def;
587 1.15 christos const Obj_Entry *defobj;
588 1.1 cgd
589 1.15 christos if (obj == NULL)
590 1.15 christos return NULL;
591 1.1 cgd
592 1.15 christos /*
593 1.15 christos * FIXME - This isn't correct. The search should include the whole
594 1.15 christos * DAG rooted at the given object.
595 1.15 christos */
596 1.15 christos def = _rtld_find_symdef(_rtld_objlist, 0, name, obj, &defobj, false);
597 1.15 christos if (def != NULL)
598 1.15 christos return defobj->relocbase + def->st_value;
599 1.1 cgd
600 1.15 christos _rtld_error("Undefined symbol \"%s\"", name);
601 1.15 christos return NULL;
602 1.1 cgd }
603 1.1 cgd
604 1.1 cgd /*
605 1.1 cgd * Error reporting function. Use it like printf. If formats the message
606 1.1 cgd * into a buffer, and sets things up so that the next call to dlerror()
607 1.1 cgd * will return the message.
608 1.1 cgd */
609 1.1 cgd void
610 1.15 christos #ifdef __STDC__
611 1.15 christos _rtld_error(const char *fmt,...)
612 1.15 christos #else
613 1.15 christos _rtld_error(va_alist)
614 1.15 christos va_dcl
615 1.15 christos #endif
616 1.1 cgd {
617 1.15 christos static char buf[512];
618 1.15 christos va_list ap;
619 1.15 christos #ifdef __STDC__
620 1.15 christos va_start(ap, fmt);
621 1.15 christos #else
622 1.15 christos const char *fmt;
623 1.1 cgd
624 1.15 christos va_start(ap);
625 1.15 christos fmt = va_arg(ap, const char *);
626 1.15 christos #endif
627 1.15 christos xvsnprintf(buf, sizeof buf, fmt, ap);
628 1.15 christos error_message = buf;
629 1.15 christos va_end(ap);
630 1.1 cgd }
631 1.14 pk
632 1.1 cgd void
633 1.15 christos _rtld_debug_state()
634 1.1 cgd {
635 1.15 christos /* do nothing */
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd void
639 1.15 christos _rtld_linkmap_add(obj)
640 1.15 christos Obj_Entry *obj;
641 1.1 cgd {
642 1.15 christos struct link_map *l = &obj->linkmap;
643 1.15 christos struct link_map *prev;
644 1.1 cgd
645 1.15 christos obj->linkmap.l_name = obj->path;
646 1.15 christos obj->linkmap.l_addr = obj->mapbase;
647 1.15 christos obj->linkmap.l_ld = obj->dynamic;
648 1.6 mhitch #ifdef __mips__
649 1.15 christos /* GDB needs load offset on MIPS to use the symbols */
650 1.15 christos obj->linkmap.l_offs = obj->relocbase;
651 1.6 mhitch #endif
652 1.1 cgd
653 1.15 christos if (_rtld_debug.r_map == NULL) {
654 1.15 christos _rtld_debug.r_map = l;
655 1.15 christos return;
656 1.15 christos }
657 1.15 christos for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
658 1.15 christos l->l_prev = prev;
659 1.15 christos prev->l_next = l;
660 1.15 christos l->l_next = NULL;
661 1.1 cgd }
662 1.1 cgd
663 1.1 cgd void
664 1.15 christos _rtld_linkmap_delete(obj)
665 1.15 christos Obj_Entry *obj;
666 1.1 cgd {
667 1.15 christos struct link_map *l = &obj->linkmap;
668 1.1 cgd
669 1.15 christos if (l->l_prev == NULL) {
670 1.15 christos if ((_rtld_debug.r_map = l->l_next) != NULL)
671 1.15 christos l->l_next->l_prev = NULL;
672 1.15 christos return;
673 1.15 christos }
674 1.15 christos if ((l->l_prev->l_next = l->l_next) != NULL)
675 1.15 christos l->l_next->l_prev = l->l_prev;
676 1.1 cgd }
677