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