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