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