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