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