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