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