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