rtld.c revision 1.89 1 /* $NetBSD: rtld.c,v 1.89 2002/12/05 04:56:57 junyoung 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 assert(pAUX_base != NULL);
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 real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
436 if (real___mainprog_obj)
437 *real___mainprog_obj = _rtld_objmain;
438
439 dbg(("calling _init functions"));
440 _rtld_call_init_functions(_rtld_objmain->next);
441
442 dbg(("control at program entry point = %p, obj = %p, exit = %p",
443 _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
444
445 /*
446 * Return with the entry point and the exit procedure in at the top
447 * of stack.
448 */
449
450 _rtld_debug_state(); /* say hello to gdb! */
451
452 ((void **) osp)[0] = _rtld_exit;
453 ((void **) osp)[1] = _rtld_objmain;
454 return (Elf_Addr) _rtld_objmain->entry;
455 }
456
457 void
458 _rtld_die()
459 {
460 const char *msg = _rtld_dlerror();
461
462 if (msg == NULL)
463 msg = "Fatal error";
464 xerrx(1, "%s", msg);
465 }
466
467 static Obj_Entry *
468 _rtld_dlcheck(handle)
469 void *handle;
470 {
471 Obj_Entry *obj;
472
473 for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
474 if (obj == (Obj_Entry *) handle)
475 break;
476
477 if (obj == NULL || obj->dl_refcount == 0) {
478 xwarnx("Invalid shared object handle %p", handle);
479 return NULL;
480 }
481 return obj;
482 }
483
484 static void
485 _rtld_init_dag(root)
486 Obj_Entry *root;
487 {
488 _rtld_init_dag1(root, root);
489 }
490
491 static void
492 _rtld_init_dag1(root, obj)
493 Obj_Entry *root;
494 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(root, do_fini_funcs)
516 Obj_Entry *root;
517 bool do_fini_funcs;
518 {
519 _rtld_unref_dag(root);
520 if (root->refcount == 0) { /* We are finished with some objects. */
521 Obj_Entry *obj;
522 Obj_Entry **linkp;
523 Objlist_Entry *elm;
524
525 /* Finalize objects that are about to be unmapped. */
526 if (do_fini_funcs)
527 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
528 if (obj->refcount == 0 && obj->fini != NULL)
529 (*obj->fini)();
530
531 /* Remove the DAG from all objects' DAG lists. */
532 SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
533 _rtld_objlist_remove(&elm->obj->dldags, root);
534
535 /* Remove the DAG from the RTLD_GLOBAL list. */
536 if (root->globalref) {
537 root->globalref = 0;
538 _rtld_objlist_remove(&_rtld_list_global, root);
539 }
540
541 /* Unmap all objects that are no longer referenced. */
542 linkp = &_rtld_objlist->next;
543 while ((obj = *linkp) != NULL) {
544 if (obj->refcount == 0) {
545 #ifdef RTLD_DEBUG
546 dbg(("unloading \"%s\"", obj->path));
547 #endif
548 munmap(obj->mapbase, obj->mapsize);
549 _rtld_objlist_remove(&_rtld_list_global, obj);
550 _rtld_linkmap_delete(obj);
551 *linkp = obj->next;
552 _rtld_obj_free(obj);
553 } else
554 linkp = &obj->next;
555 }
556 _rtld_objtail = linkp;
557 }
558 }
559
560 static void
561 _rtld_unref_dag(root)
562 Obj_Entry *root;
563 {
564 assert(root);
565 assert(root->refcount != 0);
566 --root->refcount;
567 if (root->refcount == 0) {
568 const Needed_Entry *needed;
569
570 for (needed = root->needed; needed != NULL;
571 needed = needed->next) {
572 if (needed->obj != NULL)
573 _rtld_unref_dag(needed->obj);
574 }
575 }
576 }
577
578 int
579 _rtld_dlclose(handle)
580 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 _rtld_dlerror()
601 {
602 char *msg = error_message;
603 error_message = NULL;
604 return msg;
605 }
606
607 void *
608 _rtld_dlopen(name, mode)
609 const char *name;
610 int mode;
611 {
612 Obj_Entry **old_obj_tail = _rtld_objtail;
613 Obj_Entry *obj = NULL;
614
615 _rtld_debug.r_state = RT_ADD;
616 _rtld_debug_state();
617
618 if (name == NULL) {
619 obj = _rtld_objmain;
620 obj->refcount++;
621 } else
622 obj = _rtld_load_library(name, _rtld_objmain, mode);
623
624 if (obj != NULL) {
625 ++obj->dl_refcount;
626 if (*old_obj_tail != NULL) { /* We loaded something new. */
627 assert(*old_obj_tail == obj);
628
629 if (_rtld_load_needed_objects(obj, mode) == -1 ||
630 (_rtld_init_dag(obj),
631 _rtld_relocate_objects(obj,
632 ((mode & 3) == RTLD_NOW))) == -1) {
633 _rtld_unload_object(obj, false);
634 obj->dl_refcount--;
635 obj = NULL;
636 } else
637 _rtld_call_init_functions(obj);
638 }
639 }
640 _rtld_debug.r_state = RT_CONSISTENT;
641 _rtld_debug_state();
642
643 return obj;
644 }
645
646 /*
647 * Find a symbol in the main program.
648 */
649 void *
650 _rtld_objmain_sym(name)
651 const char *name;
652 {
653 unsigned long hash;
654 const Elf_Sym *def;
655 const Obj_Entry *obj;
656
657 hash = _rtld_elf_hash(name);
658 obj = _rtld_objmain;
659
660 def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, false);
661
662 if (def != NULL)
663 return obj->relocbase + def->st_value;
664 return(NULL);
665 }
666
667 void *
668 _rtld_dlsym(handle, name)
669 void *handle;
670 const char *name;
671 {
672 const Obj_Entry *obj;
673 unsigned long hash;
674 const Elf_Sym *def;
675 const Obj_Entry *defobj;
676
677 hash = _rtld_elf_hash(name);
678 def = NULL;
679 defobj = NULL;
680
681 if (handle == NULL
682 #if 0
683 || handle == RTLD_NEXT
684 #endif
685 ) {
686 void *retaddr;
687
688 retaddr = __builtin_return_address(0); /* __GNUC__ only */
689 if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
690 _rtld_error("Cannot determine caller's shared object");
691 return NULL;
692 }
693 if (handle == NULL) { /* Just the caller's shared object. */
694 def = _rtld_symlook_obj(name, hash, obj, false);
695 defobj = obj;
696 } else { /* All the shared objects after the caller's */
697 while ((obj = obj->next) != NULL) {
698 if ((def = _rtld_symlook_obj(name, hash, obj, false)) != NULL) {
699 defobj = obj;
700 break;
701 }
702 }
703 }
704 } else {
705 if ((obj = _rtld_dlcheck(handle)) == NULL)
706 return NULL;
707
708 if (obj->mainprog) {
709 /* Search main program and all libraries loaded by it. */
710 def = _rtld_symlook_list(name, hash, &_rtld_list_main, &defobj, false);
711 } else {
712 /*
713 * XXX - This isn't correct. The search should include the whole
714 * DAG rooted at the given object.
715 */
716 def = _rtld_symlook_obj(name, hash, obj, false);
717 defobj = obj;
718 }
719 }
720
721 if (def != NULL) {
722 #ifdef __HAVE_FUNCTION_DESCRIPTORS
723 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
724 return (void *)_rtld_function_descriptor_alloc(defobj,
725 def, 0);
726 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
727 return defobj->relocbase + def->st_value;
728 }
729
730 _rtld_error("Undefined symbol \"%s\"", name);
731 return NULL;
732 }
733
734 int
735 _rtld_dladdr(addr, info)
736 const void *addr;
737 Dl_info *info;
738 {
739 const Obj_Entry *obj;
740 const Elf_Sym *def, *best_def;
741 void *symbol_addr;
742 unsigned long symoffset;
743
744 #ifdef __HAVE_FUNCTION_DESCRIPTORS
745 addr = _rtld_function_descriptor_function(addr);
746 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
747
748 obj = _rtld_obj_from_addr(addr);
749 if (obj == NULL) {
750 _rtld_error("No shared object contains address");
751 return 0;
752 }
753 info->dli_fname = obj->path;
754 info->dli_fbase = obj->mapbase;
755 info->dli_saddr = (void *)0;
756 info->dli_sname = NULL;
757
758 /*
759 * Walk the symbol list looking for the symbol whose address is
760 * closest to the address sent in.
761 */
762 best_def = NULL;
763 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
764 def = obj->symtab + symoffset;
765
766 /*
767 * For skip the symbol if st_shndx is either SHN_UNDEF or
768 * SHN_COMMON.
769 */
770 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
771 continue;
772
773 /*
774 * If the symbol is greater than the specified address, or if it
775 * is further away from addr than the current nearest symbol,
776 * then reject it.
777 */
778 symbol_addr = obj->relocbase + def->st_value;
779 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
780 continue;
781
782 /* Update our idea of the nearest symbol. */
783 info->dli_sname = obj->strtab + def->st_name;
784 info->dli_saddr = symbol_addr;
785 best_def = def;
786
787 /* Exact match? */
788 if (info->dli_saddr == addr)
789 break;
790 }
791
792 #ifdef __HAVE_FUNCTION_DESCRIPTORS
793 if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
794 info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
795 best_def, 0);
796 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
797
798 return 1;
799 }
800
801 /*
802 * Error reporting function. Use it like printf. If formats the message
803 * into a buffer, and sets things up so that the next call to dlerror()
804 * will return the message.
805 */
806 void
807 _rtld_error(const char *fmt,...)
808 {
809 static char buf[512];
810 va_list ap;
811
812 va_start(ap, fmt);
813 xvsnprintf(buf, sizeof buf, fmt, ap);
814 error_message = buf;
815 va_end(ap);
816 }
817
818 void
819 _rtld_debug_state()
820 {
821 /* do nothing */
822 }
823
824 void
825 _rtld_linkmap_add(obj)
826 Obj_Entry *obj;
827 {
828 struct link_map *l = &obj->linkmap;
829 struct link_map *prev;
830
831 obj->linkmap.l_name = obj->path;
832 obj->linkmap.l_addr = obj->relocbase;
833 obj->linkmap.l_ld = obj->dynamic;
834 #ifdef __mips__
835 /* XXX This field is not standard and will be removed eventually. */
836 obj->linkmap.l_offs = obj->relocbase;
837 #endif
838
839 if (_rtld_debug.r_map == NULL) {
840 _rtld_debug.r_map = l;
841 return;
842 }
843 for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
844 l->l_prev = prev;
845 prev->l_next = l;
846 l->l_next = NULL;
847 }
848
849 void
850 _rtld_linkmap_delete(obj)
851 Obj_Entry *obj;
852 {
853 struct link_map *l = &obj->linkmap;
854
855 if (l->l_prev == NULL) {
856 if ((_rtld_debug.r_map = l->l_next) != NULL)
857 l->l_next->l_prev = NULL;
858 return;
859 }
860 if ((l->l_prev->l_next = l->l_next) != NULL)
861 l->l_next->l_prev = l->l_prev;
862 }
863
864 static Obj_Entry *
865 _rtld_obj_from_addr(const void *addr)
866 {
867 Obj_Entry *obj;
868
869 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
870 if (addr < (void *) obj->mapbase)
871 continue;
872 if (addr < (void *) (obj->mapbase + obj->mapsize))
873 return obj;
874 }
875 return NULL;
876 }
877
878 static void
879 _rtld_objlist_remove(list, obj)
880 Objlist *list;
881 Obj_Entry *obj;
882 {
883 Objlist_Entry *elm;
884
885 if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
886 SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
887 free(elm);
888 }
889 }
890