rtld.c revision 1.48.2.2 1 /* $NetBSD: rtld.c,v 1.48.2.2 2004/05/28 08:31:22 tron 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 create an Obj_Entry for the dynamic linker and
136 * to resolve the PLT relocation for platforms that need it (those that
137 * define __HAVE_FUNCTION_DESCRIPTORS
138 */
139 static void
140 _rtld_init(caddr_t mapbase, caddr_t relocbase)
141 {
142
143 /* Conjure up an Obj_Entry structure for the dynamic linker. */
144 _rtld_objself.path = _rtld_path;
145 _rtld_objself.pathlen = strlen(_rtld_path);
146 _rtld_objself.rtld = true;
147 _rtld_objself.mapbase = mapbase;
148 _rtld_objself.relocbase = relocbase;
149 _rtld_objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
150
151 _rtld_digest_dynamic(&_rtld_objself);
152 assert(!_rtld_objself.needed);
153 assert(!_rtld_objself.pltrel && !_rtld_objself.pltrela);
154 #if !defined(__mips__)
155 assert(!_rtld_objself.pltgot);
156 #endif
157 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
158 /* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
159 assert(!_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(void)
183 {
184
185 dbg(("rtld_exit()"));
186
187 _rtld_call_fini_functions(_rtld_objlist->next);
188 }
189
190 /*
191 * Main entry point for dynamic linking. The argument is the stack
192 * pointer. The stack is expected to be laid out as described in the
193 * SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
194 * the stack pointer points to a word containing ARGC. Following that
195 * in the stack is a null-terminated sequence of pointers to argument
196 * strings. Then comes a null-terminated sequence of pointers to
197 * environment strings. Finally, there is a sequence of "auxiliary
198 * vector" entries.
199 *
200 * This function returns the entry point for the main program, the dynamic
201 * linker's exit procedure in sp[0], and a pointer to the main object in
202 * sp[1].
203 */
204 Elf_Addr
205 _rtld(Elf_Addr *sp, 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> relocbase %p", sp,
237 (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
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 if (pAUX_base == NULL) {
304 _rtld_error("Bad pAUX_base");
305 _rtld_die();
306 }
307 assert(pAUX_pagesz != NULL);
308 _rtld_pagesz = (int)pAUX_pagesz->a_v;
309 _rtld_init((caddr_t)pAUX_base->a_v, (caddr_t)relocbase);
310
311 __progname = _rtld_objself.path;
312 environ = env;
313
314 _rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
315 (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
316 ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
317 (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
318
319 ld_bind_now = getenv("LD_BIND_NOW");
320 if (ld_bind_now != NULL && *ld_bind_now != '\0')
321 bind_now = true;
322 if (_rtld_trust) {
323 #ifdef DEBUG
324 const char *ld_debug = getenv("LD_DEBUG");
325 #ifdef RTLD_DEBUG
326 debug = 0;
327 #endif
328 if (ld_debug != NULL && *ld_debug != '\0')
329 debug = 1;
330 #endif
331 _rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"));
332 }
333 _rtld_process_hints(&_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
334 dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
335 _rtld_objself.mapbase, _rtld_objself.relocbase));
336
337 /*
338 * Load the main program, or process its program header if it is
339 * already loaded.
340 */
341 if (pAUX_execfd != NULL) { /* Load the main program. */
342 int fd = pAUX_execfd->a_v;
343 dbg(("loading main program"));
344 _rtld_objmain = _rtld_map_object(xstrdup(argv[0] ? argv[0] :
345 "main program"), fd, NULL);
346 close(fd);
347 if (_rtld_objmain == NULL)
348 _rtld_die();
349 } else { /* Main program already loaded. */
350 const Elf_Phdr *phdr;
351 int phnum;
352 caddr_t entry;
353
354 dbg(("processing main program's program header"));
355 assert(pAUX_phdr != NULL);
356 phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
357 assert(pAUX_phnum != NULL);
358 phnum = pAUX_phnum->a_v;
359 assert(pAUX_phent != NULL);
360 assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
361 assert(pAUX_entry != NULL);
362 entry = (caddr_t) pAUX_entry->a_v;
363 _rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
364 _rtld_objmain->path = xstrdup(argv[0] ? argv[0] :
365 "main program");
366 _rtld_objmain->pathlen = strlen(_rtld_objmain->path);
367 }
368
369 _rtld_objmain->mainprog = true;
370
371 /*
372 * Get the actual dynamic linker pathname from the executable if
373 * possible. (It should always be possible.) That ensures that
374 * gdb will find the right dynamic linker even if a non-standard
375 * one is being used.
376 */
377 if (_rtld_objmain->interp != NULL &&
378 strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0)
379 _rtld_objself.path = xstrdup(_rtld_objmain->interp);
380 dbg(("actual dynamic linker is %s", _rtld_objself.path));
381
382 _rtld_digest_dynamic(_rtld_objmain);
383
384 /* Link the main program into the list of objects. */
385 *_rtld_objtail = _rtld_objmain;
386 _rtld_objtail = &_rtld_objmain->next;
387
388 _rtld_linkmap_add(_rtld_objmain);
389 _rtld_linkmap_add(&_rtld_objself);
390
391 ++_rtld_objmain->refcount;
392 _rtld_objmain->mainref = 1;
393 _rtld_objlist_add(&_rtld_list_main, _rtld_objmain);
394
395 /* Initialize a fake symbol for resolving undefined weak references. */
396 _rtld_sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
397 _rtld_sym_zero.st_shndx = SHN_ABS;
398
399 /*
400 * Pre-load user-specified objects after the main program but before
401 * any shared object dependencies.
402 */
403 dbg(("preloading objects"));
404 if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD")) == -1)
405 _rtld_die();
406
407 dbg(("loading needed objects"));
408 if (_rtld_load_needed_objects(_rtld_objmain, RTLD_MAIN) == -1)
409 _rtld_die();
410
411 dbg(("relocating objects"));
412 if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
413 _rtld_die();
414
415 dbg(("doing copy relocations"));
416 if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
417 _rtld_die();
418
419 /*
420 * Set the __progname, environ and, __mainprog_obj before
421 * calling anything that might use them.
422 */
423 real___progname = _rtld_objmain_sym("__progname");
424 if (real___progname) {
425 if (argv[0] != NULL) {
426 if ((*real___progname = strrchr(argv[0], '/')) == NULL)
427 (*real___progname) = argv[0];
428 else
429 (*real___progname)++;
430 } else {
431 (*real___progname) = NULL;
432 }
433 }
434 real_environ = _rtld_objmain_sym("environ");
435 if (real_environ)
436 *real_environ = environ;
437 /*
438 * Set __mainprog_obj for old binaries.
439 */
440 real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
441 if (real___mainprog_obj)
442 *real___mainprog_obj = _rtld_objmain;
443
444 dbg(("calling _init functions"));
445 _rtld_call_init_functions(_rtld_objmain->next);
446
447 dbg(("control at program entry point = %p, obj = %p, exit = %p",
448 _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
449
450 /*
451 * Return with the entry point and the exit procedure in at the top
452 * of stack.
453 */
454
455 _rtld_debug_state(); /* say hello to gdb! */
456
457 ((void **) osp)[0] = _rtld_exit;
458 ((void **) osp)[1] = _rtld_objmain;
459 return (Elf_Addr) _rtld_objmain->entry;
460 }
461
462 void
463 _rtld_die(void)
464 {
465 const char *msg = dlerror();
466
467 if (msg == NULL)
468 msg = "Fatal error";
469 xerrx(1, "%s", msg);
470 }
471
472 static Obj_Entry *
473 _rtld_dlcheck(void *handle)
474 {
475 Obj_Entry *obj;
476
477 for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
478 if (obj == (Obj_Entry *) handle)
479 break;
480
481 if (obj == NULL || obj->dl_refcount == 0) {
482 xwarnx("Invalid shared object handle %p", handle);
483 return NULL;
484 }
485 return obj;
486 }
487
488 static void
489 _rtld_init_dag(Obj_Entry *root)
490 {
491
492 _rtld_init_dag1(root, root);
493 }
494
495 static void
496 _rtld_init_dag1(Obj_Entry *root, Obj_Entry *obj)
497 {
498 const Needed_Entry *needed;
499
500 if (!obj->mainref) {
501 if (_rtld_objlist_find(&obj->dldags, root))
502 return;
503 rdbg(("add %p (%s) to %p (%s) DAG", obj, obj->path, root,
504 root->path));
505 _rtld_objlist_add(&obj->dldags, root);
506 _rtld_objlist_add(&root->dagmembers, obj);
507 }
508 for (needed = obj->needed; needed != NULL; needed = needed->next)
509 if (needed->obj != NULL)
510 _rtld_init_dag1(root, needed->obj);
511 }
512
513 /*
514 * Note, this is called only for objects loaded by dlopen().
515 */
516 static void
517 _rtld_unload_object(Obj_Entry *root, bool do_fini_funcs)
518 {
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 for (elm = SIMPLEQ_FIRST(&root->dagmembers); elm; elm = SIMPLEQ_NEXT(elm, 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(Obj_Entry *root)
563 {
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 dlclose(void *handle)
581 {
582 Obj_Entry *root = _rtld_dlcheck(handle);
583
584 if (root == NULL)
585 return -1;
586
587 _rtld_debug.r_state = RT_DELETE;
588 _rtld_debug_state();
589
590 --root->dl_refcount;
591 _rtld_unload_object(root, true);
592
593 _rtld_debug.r_state = RT_CONSISTENT;
594 _rtld_debug_state();
595
596 return 0;
597 }
598
599 char *
600 dlerror(void)
601 {
602 char *msg = error_message;
603
604 error_message = NULL;
605 return msg;
606 }
607
608 void *
609 dlopen(const char *name, int mode)
610 {
611 Obj_Entry **old_obj_tail = _rtld_objtail;
612 Obj_Entry *obj = NULL;
613
614 _rtld_debug.r_state = RT_ADD;
615 _rtld_debug_state();
616
617 if (name == NULL) {
618 obj = _rtld_objmain;
619 obj->refcount++;
620 } else
621 obj = _rtld_load_library(name, _rtld_objmain, mode);
622
623 if (obj != NULL) {
624 ++obj->dl_refcount;
625 if (*old_obj_tail != NULL) { /* We loaded something new. */
626 assert(*old_obj_tail == obj);
627
628 if (_rtld_load_needed_objects(obj, mode) == -1 ||
629 (_rtld_init_dag(obj),
630 _rtld_relocate_objects(obj,
631 ((mode & 3) == RTLD_NOW))) == -1) {
632 _rtld_unload_object(obj, false);
633 obj->dl_refcount--;
634 obj = NULL;
635 } else
636 _rtld_call_init_functions(obj);
637 }
638 }
639 _rtld_debug.r_state = RT_CONSISTENT;
640 _rtld_debug_state();
641
642 return obj;
643 }
644
645 /*
646 * Find a symbol in the main program.
647 */
648 void *
649 _rtld_objmain_sym(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 dlsym(void *handle, const char *name)
667 {
668 const Obj_Entry *obj;
669 unsigned long hash;
670 const Elf_Sym *def;
671 const Obj_Entry *defobj;
672 void *retaddr;
673
674 hash = _rtld_elf_hash(name);
675 def = NULL;
676 defobj = NULL;
677
678 switch ((intptr_t)handle) {
679 case (intptr_t)NULL:
680 case (intptr_t)RTLD_NEXT:
681 case (intptr_t)RTLD_DEFAULT:
682 case (intptr_t)RTLD_SELF:
683 retaddr = __builtin_return_address(0); /* __GNUC__ only */
684 if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
685 _rtld_error("Cannot determine caller's shared object");
686 return NULL;
687 }
688
689 switch ((intptr_t)handle) {
690 case (intptr_t)NULL: /* Just the caller's shared object. */
691 def = _rtld_symlook_obj(name, hash, obj, false);
692 defobj = obj;
693 break;
694
695 case (intptr_t)RTLD_NEXT: /* Objects after callers */
696 obj = obj->next;
697 /*FALLTHROUGH*/
698
699 case (intptr_t)RTLD_SELF: /* Caller included */
700 for (; obj; obj = obj->next) {
701 if ((def = _rtld_symlook_obj(name, hash, obj,
702 false)) != NULL) {
703 defobj = obj;
704 break;
705 }
706 }
707 break;
708
709 case (intptr_t)RTLD_DEFAULT:
710 def = _rtld_symlook_default(name, hash, obj, &defobj,
711 false);
712 break;
713
714 default:
715 abort();
716 }
717 break;
718
719 default:
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,
726 &defobj, false);
727 } else {
728 /*
729 * XXX - This isn't correct. The search should include
730 * the whole DAG rooted at the given object.
731 */
732 def = _rtld_symlook_obj(name, hash, obj, false);
733 defobj = obj;
734 }
735 break;
736 }
737
738 if (def != NULL) {
739 #ifdef __HAVE_FUNCTION_DESCRIPTORS
740 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
741 return (void *)_rtld_function_descriptor_alloc(defobj,
742 def, 0);
743 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
744 return defobj->relocbase + def->st_value;
745 }
746
747 _rtld_error("Undefined symbol \"%s\"", name);
748 return NULL;
749 }
750
751 int
752 dladdr(const void *addr, 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(void)
835 {
836
837 /* do nothing */
838 }
839
840 void
841 _rtld_linkmap_add(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_Entry *obj)
866 {
867 struct link_map *l = &obj->linkmap;
868
869 if (l->l_prev == NULL) {
870 if ((_rtld_debug.r_map = l->l_next) != NULL)
871 l->l_next->l_prev = NULL;
872 return;
873 }
874 if ((l->l_prev->l_next = l->l_next) != NULL)
875 l->l_next->l_prev = l->l_prev;
876 }
877
878 static Obj_Entry *
879 _rtld_obj_from_addr(const void *addr)
880 {
881 Obj_Entry *obj;
882
883 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
884 if (addr < (void *) obj->mapbase)
885 continue;
886 if (addr < (void *) (obj->mapbase + obj->mapsize))
887 return obj;
888 }
889 return NULL;
890 }
891
892 static void
893 _rtld_objlist_remove(Objlist *list, Obj_Entry *obj)
894 {
895 Objlist_Entry *elm;
896
897 if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
898 if ((list)->sqh_first == (elm)) {
899 SIMPLEQ_REMOVE_HEAD(list, elm, link);
900 }
901 else {
902 struct Struct_Objlist_Entry *curelm = (list)->sqh_first;
903 while (curelm->link.sqe_next != (elm))
904 curelm = curelm->link.sqe_next;
905 if((curelm->link.sqe_next =
906 curelm->link.sqe_next->link.sqe_next) == NULL)
907 (list)->sqh_last = &(curelm)->link.sqe_next;
908 }
909 free(elm);
910 }
911 }
912