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