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