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