rtld.c revision 1.83 1 /* $NetBSD: rtld.c,v 1.83 2002/10/03 01:09:21 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 obj->main = 1;
410 _rtld_objlist_add(&_rtld_list_main, obj);
411 }
412
413 dbg(("relocating objects"));
414 if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
415 _rtld_die();
416
417 dbg(("doing copy relocations"));
418 if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
419 _rtld_die();
420
421 /*
422 * Set the __progname, environ and, __mainprog_obj before
423 * calling anything that might use them.
424 */
425 real___progname = _rtld_objmain_sym("__progname");
426 if (real___progname) {
427 if (argv[0] != NULL) {
428 if ((*real___progname = strrchr(argv[0], '/')) == NULL)
429 (*real___progname) = argv[0];
430 else
431 (*real___progname)++;
432 } else {
433 (*real___progname) = NULL;
434 }
435 }
436 real_environ = _rtld_objmain_sym("environ");
437 if (real_environ)
438 *real_environ = environ;
439 real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
440 if (real___mainprog_obj)
441 *real___mainprog_obj = _rtld_objmain;
442
443 dbg(("calling _init functions"));
444 _rtld_call_init_functions(_rtld_objmain->next);
445
446 dbg(("control at program entry point = %p, obj = %p, exit = %p",
447 _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
448
449 /*
450 * Return with the entry point and the exit procedure in at the top
451 * of stack.
452 */
453
454 _rtld_debug_state(); /* say hello to gdb! */
455
456 ((void **) osp)[0] = _rtld_exit;
457 ((void **) osp)[1] = _rtld_objmain;
458 return (Elf_Addr) _rtld_objmain->entry;
459 }
460
461 void
462 _rtld_die()
463 {
464 const char *msg = _rtld_dlerror();
465
466 if (msg == NULL)
467 msg = "Fatal error";
468 xerrx(1, "%s", msg);
469 }
470
471 static Obj_Entry *
472 _rtld_dlcheck(handle)
473 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(root)
490 Obj_Entry *root;
491 {
492 _rtld_init_dag1(root, root);
493 }
494
495 static void
496 _rtld_init_dag1(root, obj)
497 Obj_Entry *root;
498 Obj_Entry *obj;
499 {
500 const Needed_Entry *needed;
501
502 _rtld_objlist_add(&obj->dldags, root);
503 if (!obj->main)
504 _rtld_objlist_add(&root->dagmembers, obj);
505 for (needed = obj->needed; needed != NULL; needed = needed->next)
506 if (needed->obj != NULL)
507 _rtld_init_dag1(root, needed->obj);
508 }
509
510 /*
511 * Note, this is called only for objects loaded by dlopen().
512 */
513 static void
514 _rtld_unload_object(root, do_fini_funcs)
515 Obj_Entry *root;
516 bool do_fini_funcs;
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 _rtld_objlist_remove(&_rtld_list_global, root);
536
537 /* Unmap all objects that are no longer referenced. */
538 linkp = &_rtld_objlist->next;
539 while ((obj = *linkp) != NULL) {
540 if (obj->refcount == 0) {
541 #ifdef RTLD_DEBUG
542 dbg(("unloading \"%s\"", obj->path));
543 #endif
544 munmap(obj->mapbase, obj->mapsize);
545 _rtld_objlist_remove(&_rtld_list_global, obj);
546 _rtld_linkmap_delete(obj);
547 *linkp = obj->next;
548 _rtld_obj_free(obj);
549 } else
550 linkp = &obj->next;
551 }
552 _rtld_objtail = linkp;
553 }
554 }
555
556 static void
557 _rtld_unref_dag(root)
558 Obj_Entry *root;
559 {
560 assert(root);
561 assert(root->refcount != 0);
562 --root->refcount;
563 if (root->refcount == 0) {
564 const Needed_Entry *needed;
565
566 for (needed = root->needed; needed != NULL;
567 needed = needed->next) {
568 if (needed->obj != NULL)
569 _rtld_unref_dag(needed->obj);
570 }
571 }
572 }
573
574 int
575 _rtld_dlclose(handle)
576 void *handle;
577 {
578 Obj_Entry *root = _rtld_dlcheck(handle);
579
580 if (root == NULL)
581 return -1;
582
583 _rtld_debug.r_state = RT_DELETE;
584 _rtld_debug_state();
585
586 --root->dl_refcount;
587 _rtld_unload_object(root, true);
588
589 _rtld_debug.r_state = RT_CONSISTENT;
590 _rtld_debug_state();
591
592 return 0;
593 }
594
595 char *
596 _rtld_dlerror()
597 {
598 char *msg = error_message;
599 error_message = NULL;
600 return msg;
601 }
602
603 void *
604 _rtld_dlopen(name, mode)
605 const char *name;
606 int mode;
607 {
608 Obj_Entry **old_obj_tail = _rtld_objtail;
609 Obj_Entry *obj = NULL;
610
611 _rtld_debug.r_state = RT_ADD;
612 _rtld_debug_state();
613
614 if (name == NULL) {
615 obj = _rtld_objmain;
616 obj->refcount++;
617 } else
618 obj = _rtld_load_library(name, _rtld_objmain, mode);
619
620 if (obj != NULL) {
621 ++obj->dl_refcount;
622 if (*old_obj_tail != NULL) { /* We loaded something new. */
623 assert(*old_obj_tail == obj);
624
625 if (_rtld_load_needed_objects(obj, mode) == -1 ||
626 (_rtld_init_dag(obj),
627 _rtld_relocate_objects(obj,
628 ((mode & 3) == RTLD_NOW))) == -1) {
629 _rtld_unload_object(obj, false);
630 obj->dl_refcount--;
631 obj = NULL;
632 } else
633 _rtld_call_init_functions(obj);
634 }
635 }
636 _rtld_debug.r_state = RT_CONSISTENT;
637 _rtld_debug_state();
638
639 return obj;
640 }
641
642 /*
643 * Find a symbol in the main program.
644 */
645 void *
646 _rtld_objmain_sym(name)
647 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 _rtld_dlsym(handle, name)
665 void *handle;
666 const char *name;
667 {
668 const Obj_Entry *obj;
669 unsigned long hash;
670 const Elf_Sym *def;
671 const Obj_Entry *defobj;
672
673 hash = _rtld_elf_hash(name);
674 def = NULL;
675 defobj = NULL;
676
677 if (handle == NULL
678 #if 0
679 || handle == RTLD_NEXT
680 #endif
681 ) {
682 void *retaddr;
683
684 retaddr = __builtin_return_address(0); /* __GNUC__ only */
685 if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
686 _rtld_error("Cannot determine caller's shared object");
687 return NULL;
688 }
689 if (handle == NULL) { /* Just the caller's shared object. */
690 def = _rtld_symlook_obj(name, hash, obj, false);
691 defobj = obj;
692 } else { /* All the shared objects after the caller's */
693 while ((obj = obj->next) != NULL) {
694 if ((def = _rtld_symlook_obj(name, hash, obj, false)) != NULL) {
695 defobj = obj;
696 break;
697 }
698 }
699 }
700 } else {
701 if ((obj = _rtld_dlcheck(handle)) == NULL)
702 return NULL;
703
704 if (obj->mainprog) {
705 /* Search main program and all libraries loaded by it. */
706 def = _rtld_symlook_list(name, hash, &_rtld_list_main, &defobj, false);
707 } else {
708 /*
709 * XXX - This isn't correct. The search should include the whole
710 * DAG rooted at the given object.
711 */
712 def = _rtld_symlook_obj(name, hash, obj, false);
713 defobj = obj;
714 }
715 }
716
717 if (def != NULL) {
718 #ifdef __HAVE_FUNCTION_DESCRIPTORS
719 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
720 return (void *)_rtld_function_descriptor_alloc(defobj,
721 def, 0);
722 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
723 return defobj->relocbase + def->st_value;
724 }
725
726 _rtld_error("Undefined symbol \"%s\"", name);
727 return NULL;
728 }
729
730 int
731 _rtld_dladdr(addr, info)
732 const void *addr;
733 Dl_info *info;
734 {
735 const Obj_Entry *obj;
736 const Elf_Sym *def, *best_def;
737 void *symbol_addr;
738 unsigned long symoffset;
739
740 #ifdef __HAVE_FUNCTION_DESCRIPTORS
741 addr = _rtld_function_descriptor_function(addr);
742 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
743
744 obj = _rtld_obj_from_addr(addr);
745 if (obj == NULL) {
746 _rtld_error("No shared object contains address");
747 return 0;
748 }
749 info->dli_fname = obj->path;
750 info->dli_fbase = obj->mapbase;
751 info->dli_saddr = (void *)0;
752 info->dli_sname = NULL;
753
754 /*
755 * Walk the symbol list looking for the symbol whose address is
756 * closest to the address sent in.
757 */
758 best_def = NULL;
759 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
760 def = obj->symtab + symoffset;
761
762 /*
763 * For skip the symbol if st_shndx is either SHN_UNDEF or
764 * SHN_COMMON.
765 */
766 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
767 continue;
768
769 /*
770 * If the symbol is greater than the specified address, or if it
771 * is further away from addr than the current nearest symbol,
772 * then reject it.
773 */
774 symbol_addr = obj->relocbase + def->st_value;
775 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
776 continue;
777
778 /* Update our idea of the nearest symbol. */
779 info->dli_sname = obj->strtab + def->st_name;
780 info->dli_saddr = symbol_addr;
781 best_def = def;
782
783 /* Exact match? */
784 if (info->dli_saddr == addr)
785 break;
786 }
787
788 #ifdef __HAVE_FUNCTION_DESCRIPTORS
789 if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
790 info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
791 best_def, 0);
792 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
793
794 return 1;
795 }
796
797 /*
798 * Error reporting function. Use it like printf. If formats the message
799 * into a buffer, and sets things up so that the next call to dlerror()
800 * will return the message.
801 */
802 void
803 _rtld_error(const char *fmt,...)
804 {
805 static char buf[512];
806 va_list ap;
807
808 va_start(ap, fmt);
809 xvsnprintf(buf, sizeof buf, fmt, ap);
810 error_message = buf;
811 va_end(ap);
812 }
813
814 void
815 _rtld_debug_state()
816 {
817 /* do nothing */
818 }
819
820 void
821 _rtld_linkmap_add(obj)
822 Obj_Entry *obj;
823 {
824 struct link_map *l = &obj->linkmap;
825 struct link_map *prev;
826
827 obj->linkmap.l_name = obj->path;
828 obj->linkmap.l_addr = obj->relocbase;
829 obj->linkmap.l_ld = obj->dynamic;
830 #ifdef __mips__
831 /* XXX This field is not standard and will be removed eventually. */
832 obj->linkmap.l_offs = obj->relocbase;
833 #endif
834
835 if (_rtld_debug.r_map == NULL) {
836 _rtld_debug.r_map = l;
837 return;
838 }
839 for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
840 l->l_prev = prev;
841 prev->l_next = l;
842 l->l_next = NULL;
843 }
844
845 void
846 _rtld_linkmap_delete(obj)
847 Obj_Entry *obj;
848 {
849 struct link_map *l = &obj->linkmap;
850
851 if (l->l_prev == NULL) {
852 if ((_rtld_debug.r_map = l->l_next) != NULL)
853 l->l_next->l_prev = NULL;
854 return;
855 }
856 if ((l->l_prev->l_next = l->l_next) != NULL)
857 l->l_next->l_prev = l->l_prev;
858 }
859
860 static Obj_Entry *
861 _rtld_obj_from_addr(const void *addr)
862 {
863 Obj_Entry *obj;
864
865 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
866 if (addr < (void *) obj->mapbase)
867 continue;
868 if (addr < (void *) (obj->mapbase + obj->mapsize))
869 return obj;
870 }
871 return NULL;
872 }
873
874 static void
875 _rtld_objlist_remove(list, obj)
876 Objlist *list;
877 Obj_Entry *obj;
878 {
879 Objlist_Entry *elm;
880
881 if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
882 SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
883 free(elm);
884 }
885 }
886