rtld.c revision 1.63 1 /* $NetBSD: rtld.c,v 1.63 2002/09/13 15:27:30 mycroft 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 ((*real___progname = strrchr(argv[0], '/')) == NULL)
499 (*real___progname) = argv[0];
500 else
501 (*real___progname)++;
502 }
503 real_environ = _rtld_objmain_sym("environ");
504 if (real_environ)
505 *real_environ = environ;
506 real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
507 if (real___mainprog_obj)
508 *real___mainprog_obj = _rtld_objmain;
509
510 dbg(("calling _init functions"));
511 _rtld_call_init_functions(_rtld_objmain->next);
512
513 dbg(("control at program entry point = %p, obj = %p, exit = %p",
514 _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
515
516 /*
517 * Return with the entry point and the exit procedure in at the top
518 * of stack.
519 */
520
521 _rtld_debug_state(); /* say hello to gdb! */
522
523 ((void **) osp)[0] = _rtld_exit;
524 ((void **) osp)[1] = _rtld_objmain;
525 return (Elf_Addr) _rtld_objmain->entry;
526 }
527
528 void
529 _rtld_die()
530 {
531 const char *msg = _rtld_dlerror();
532
533 if (msg == NULL)
534 msg = "Fatal error";
535 xerrx(1, "%s", msg);
536 }
537
538 static Obj_Entry *
539 _rtld_dlcheck(handle)
540 void *handle;
541 {
542 Obj_Entry *obj;
543
544 for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
545 if (obj == (Obj_Entry *) handle)
546 break;
547
548 if (obj == NULL || obj->dl_refcount == 0) {
549 xwarnx("Invalid shared object handle %p", handle);
550 return NULL;
551 }
552 return obj;
553 }
554
555 static void
556 _rtld_init_dag(root)
557 Obj_Entry *root;
558 {
559 _rtld_init_dag1(root, root);
560 }
561
562 static void
563 _rtld_init_dag1(root, obj)
564 Obj_Entry *root;
565 Obj_Entry *obj;
566 {
567 const Needed_Entry *needed;
568
569 _rtld_objlist_add(&obj->dldags, root);
570 _rtld_objlist_add(&root->dagmembers, obj);
571 for (needed = obj->needed; needed != NULL; needed = needed->next)
572 if (needed->obj != NULL)
573 _rtld_init_dag1(root, needed->obj);
574 }
575
576 /*
577 * Note, this is called only for objects loaded by dlopen().
578 */
579 static void
580 _rtld_unload_object(root, do_fini_funcs)
581 Obj_Entry *root;
582 bool do_fini_funcs;
583 {
584 _rtld_unref_dag(root);
585 if (root->refcount == 0) { /* We are finished with some objects. */
586 Obj_Entry *obj;
587 Obj_Entry **linkp;
588 Objlist_Entry *elm;
589
590 /* Finalize objects that are about to be unmapped. */
591 if (do_fini_funcs)
592 for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
593 if (obj->refcount == 0 && obj->fini != NULL)
594 (*obj->fini)();
595
596 /* Remove the DAG from all objects' DAG lists. */
597 SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
598 _rtld_objlist_remove(&elm->obj->dldags, root);
599
600 /* Remove the DAG from the RTLD_GLOBAL list. */
601 _rtld_objlist_remove(&_rtld_list_global, root);
602
603 /* Unmap all objects that are no longer referenced. */
604 linkp = &_rtld_objlist->next;
605 while ((obj = *linkp) != NULL) {
606 if (obj->refcount == 0) {
607 #ifdef RTLD_DEBUG
608 dbg(("unloading \"%s\"", obj->path));
609 #endif
610 munmap(obj->mapbase, obj->mapsize);
611 _rtld_objlist_remove(&_rtld_list_global, obj);
612 _rtld_linkmap_delete(obj);
613 *linkp = obj->next;
614 _rtld_obj_free(obj);
615 } else
616 linkp = &obj->next;
617 }
618 _rtld_objtail = linkp;
619 }
620 }
621
622 static void
623 _rtld_unref_dag(root)
624 Obj_Entry *root;
625 {
626 assert(root);
627 assert(root->refcount != 0);
628 --root->refcount;
629 if (root->refcount == 0) {
630 const Needed_Entry *needed;
631
632 for (needed = root->needed; needed != NULL;
633 needed = needed->next) {
634 if (needed->obj != NULL)
635 _rtld_unref_dag(needed->obj);
636 }
637 }
638 }
639
640 int
641 _rtld_dlclose(handle)
642 void *handle;
643 {
644 Obj_Entry *root = _rtld_dlcheck(handle);
645
646 if (root == NULL)
647 return -1;
648
649 _rtld_debug.r_state = RT_DELETE;
650 _rtld_debug_state();
651
652 --root->dl_refcount;
653 _rtld_unload_object(root, true);
654
655 _rtld_debug.r_state = RT_CONSISTENT;
656 _rtld_debug_state();
657
658 return 0;
659 }
660
661 char *
662 _rtld_dlerror()
663 {
664 char *msg = error_message;
665 error_message = NULL;
666 return msg;
667 }
668
669 void *
670 _rtld_dlopen(name, mode)
671 const char *name;
672 int mode;
673 {
674 Obj_Entry **old_obj_tail = _rtld_objtail;
675 Obj_Entry *obj = NULL;
676
677 _rtld_debug.r_state = RT_ADD;
678 _rtld_debug_state();
679
680 if (name == NULL) {
681 obj = _rtld_objmain;
682 obj->refcount++;
683 } else {
684 char *path = _rtld_find_library(name, _rtld_objmain);
685 if (path != NULL)
686 obj = _rtld_load_object(path, mode);
687 }
688
689 if (obj != NULL) {
690 ++obj->dl_refcount;
691 if (*old_obj_tail != NULL) { /* We loaded something new. */
692 assert(*old_obj_tail == obj);
693
694 if (_rtld_load_needed_objects(obj, mode) == -1 ||
695 (_rtld_init_dag(obj),
696 _rtld_relocate_objects(obj,
697 ((mode & 3) == RTLD_NOW), false)) == -1) {
698 _rtld_unload_object(obj, false);
699 obj->dl_refcount--;
700 obj = NULL;
701 } else
702 _rtld_call_init_functions(obj);
703 }
704 }
705 _rtld_debug.r_state = RT_CONSISTENT;
706 _rtld_debug_state();
707
708 return obj;
709 }
710
711 /*
712 * Find a symbol in the main program.
713 */
714 void *
715 _rtld_objmain_sym(name)
716 const char *name;
717 {
718 unsigned long hash;
719 const Elf_Sym *def;
720 const Obj_Entry *obj;
721
722 hash = _rtld_elf_hash(name);
723 obj = _rtld_objmain;
724
725 def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, true);
726
727 if (def != NULL)
728 return obj->relocbase + def->st_value;
729 return(NULL);
730 }
731
732 void *
733 _rtld_dlsym(handle, name)
734 void *handle;
735 const char *name;
736 {
737 const Obj_Entry *obj;
738 unsigned long hash;
739 const Elf_Sym *def;
740 const Obj_Entry *defobj;
741
742 hash = _rtld_elf_hash(name);
743 def = NULL;
744 defobj = NULL;
745
746 if (handle == NULL
747 #if 0
748 || handle == RTLD_NEXT
749 #endif
750 ) {
751 void *retaddr;
752
753 retaddr = __builtin_return_address(0); /* __GNUC__ only */
754 if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
755 _rtld_error("Cannot determine caller's shared object");
756 return NULL;
757 }
758 if (handle == NULL) { /* Just the caller's shared object. */
759 def = _rtld_symlook_obj(name, hash, obj, true);
760 defobj = obj;
761 } else { /* All the shared objects after the caller's */
762 while ((obj = obj->next) != NULL) {
763 if ((def = _rtld_symlook_obj(name, hash, obj, true)) != NULL) {
764 defobj = obj;
765 break;
766 }
767 }
768 }
769 } else {
770 if ((obj = _rtld_dlcheck(handle)) == NULL)
771 return NULL;
772
773 if (obj->mainprog) {
774 /* Search main program and all libraries loaded by it. */
775 def = _rtld_symlook_list(name, hash, &_rtld_list_main, &defobj, true);
776 } else {
777 /*
778 * XXX - This isn't correct. The search should include the whole
779 * DAG rooted at the given object.
780 */
781 def = _rtld_symlook_obj(name, hash, obj, true);
782 defobj = obj;
783 }
784 }
785
786 if (def != NULL) {
787 #ifdef __HAVE_FUNCTION_DESCRIPTORS
788 if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
789 return (void *)_rtld_function_descriptor_alloc(defobj,
790 def, 0);
791 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
792 return defobj->relocbase + def->st_value;
793 }
794
795 _rtld_error("Undefined symbol \"%s\"", name);
796 return NULL;
797 }
798
799 int
800 _rtld_dladdr(addr, info)
801 const void *addr;
802 Dl_info *info;
803 {
804 const Obj_Entry *obj;
805 const Elf_Sym *def, *best_def;
806 void *symbol_addr;
807 unsigned long symoffset;
808
809 #ifdef __HAVE_FUNCTION_DESCRIPTORS
810 addr = _rtld_function_descriptor_function(addr);
811 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
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 best_def = NULL;
828 for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
829 def = obj->symtab + symoffset;
830
831 /*
832 * For skip the symbol if st_shndx is either SHN_UNDEF or
833 * SHN_COMMON.
834 */
835 if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
836 continue;
837
838 /*
839 * If the symbol is greater than the specified address, or if it
840 * is further away from addr than the current nearest symbol,
841 * then reject it.
842 */
843 symbol_addr = obj->relocbase + def->st_value;
844 if (symbol_addr > addr || symbol_addr < info->dli_saddr)
845 continue;
846
847 /* Update our idea of the nearest symbol. */
848 info->dli_sname = obj->strtab + def->st_name;
849 info->dli_saddr = symbol_addr;
850 best_def = def;
851
852 /* Exact match? */
853 if (info->dli_saddr == addr)
854 break;
855 }
856
857 #ifdef __HAVE_FUNCTION_DESCRIPTORS
858 if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
859 info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
860 best_def, 0);
861 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
862
863 return 1;
864 }
865
866 /*
867 * Error reporting function. Use it like printf. If formats the message
868 * into a buffer, and sets things up so that the next call to dlerror()
869 * will return the message.
870 */
871 void
872 _rtld_error(const char *fmt,...)
873 {
874 static char buf[512];
875 va_list ap;
876
877 va_start(ap, fmt);
878 xvsnprintf(buf, sizeof buf, fmt, ap);
879 error_message = buf;
880 va_end(ap);
881 }
882
883 void
884 _rtld_debug_state()
885 {
886 /* do nothing */
887 }
888
889 void
890 _rtld_linkmap_add(obj)
891 Obj_Entry *obj;
892 {
893 struct link_map *l = &obj->linkmap;
894 struct link_map *prev;
895
896 obj->linkmap.l_name = obj->path;
897 obj->linkmap.l_addr = obj->mapbase;
898 obj->linkmap.l_ld = obj->dynamic;
899 #ifdef __mips__
900 /* GDB needs load offset on MIPS to use the symbols */
901 obj->linkmap.l_offs = obj->relocbase;
902 #endif
903 #ifdef __vax__
904 /* VAX shared libaries don't start at a vaddr of 0 */
905 obj->linkmap.l_addr -= obj->vaddrbase;
906 #endif
907
908 if (_rtld_debug.r_map == NULL) {
909 _rtld_debug.r_map = l;
910 return;
911 }
912 for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
913 l->l_prev = prev;
914 prev->l_next = l;
915 l->l_next = NULL;
916 }
917
918 void
919 _rtld_linkmap_delete(obj)
920 Obj_Entry *obj;
921 {
922 struct link_map *l = &obj->linkmap;
923
924 if (l->l_prev == NULL) {
925 if ((_rtld_debug.r_map = l->l_next) != NULL)
926 l->l_next->l_prev = NULL;
927 return;
928 }
929 if ((l->l_prev->l_next = l->l_next) != NULL)
930 l->l_next->l_prev = l->l_prev;
931 }
932
933 static Obj_Entry *
934 _rtld_obj_from_addr(const void *addr)
935 {
936 unsigned long endhash;
937 Obj_Entry *obj;
938
939 endhash = _rtld_elf_hash(END_SYM);
940 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
941 const Elf_Sym *endsym;
942
943 if (addr < (void *) obj->mapbase)
944 continue;
945 if ((endsym = _rtld_symlook_obj(END_SYM, endhash, obj, true)) == NULL)
946 continue; /* No "end" symbol?! */
947 if (addr < (void *) (obj->relocbase + endsym->st_value))
948 return obj;
949 }
950 return NULL;
951 }
952
953 static void
954 _rtld_objlist_remove(list, obj)
955 Objlist *list;
956 Obj_Entry *obj;
957 {
958 Objlist_Entry *elm;
959
960 if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
961 SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
962 free(elm);
963 }
964 }
965