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