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