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