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