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