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