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