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