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