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