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