1 /* $NetBSD: rtld.c,v 1.126 2009/11/17 18:44:33 skrll Exp $ */
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
36 * Dynamic linker for ELF.
38 * John Polstra <jdp@polstra.com>.
41 #include <sys/cdefs.h>
43 __RCSID("$NetBSD: rtld.c,v 1.126 2009/11/17 18:44:33 skrll Exp $");
54 #include <sys/param.h>
69 * Function declarations.
71 static void _rtld_init(caddr_t
, caddr_t
, const char *);
72 static void _rtld_exit(void);
74 Elf_Addr
_rtld(Elf_Addr
*, Elf_Addr
);
80 static char *error_message
; /* Message for dlopen(), or NULL */
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 const char _rtld_path
[] = _PATH_RTLD
;
90 /* Initialize a fake symbol for resolving undefined weak references. */
91 Elf_Sym _rtld_sym_zero
= {
92 .st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_NOTYPE
),
95 size_t _rtld_pagesz
; /* Page size, as provided by kernel */
97 Search_Path
*_rtld_default_paths
;
98 Search_Path
*_rtld_paths
;
100 Library_Xform
*_rtld_xforms
;
103 * Global declarations normally provided by crt0.
108 #if defined(RTLD_DEBUG)
110 extern Elf_Addr _GLOBAL_OFFSET_TABLE_
[];
111 #else /* 32-bit SuperH */
112 register Elf_Addr
*_GLOBAL_OFFSET_TABLE_
asm("r12");
114 #endif /* RTLD_DEBUG */
115 extern Elf_Dyn _DYNAMIC
;
117 static void _rtld_call_fini_functions(int);
118 static void _rtld_call_init_functions(void);
119 static void _rtld_initlist_visit(Objlist
*, Obj_Entry
*, int);
120 static void _rtld_initlist_tsort(Objlist
*, int);
121 static Obj_Entry
*_rtld_dlcheck(void *);
122 static void _rtld_init_dag(Obj_Entry
*);
123 static void _rtld_init_dag1(Obj_Entry
*, Obj_Entry
*);
124 static void _rtld_objlist_remove(Objlist
*, Obj_Entry
*);
125 static void _rtld_objlist_clear(Objlist
*);
126 static void _rtld_unload_object(Obj_Entry
*, bool);
127 static void _rtld_unref_dag(Obj_Entry
*);
128 static Obj_Entry
*_rtld_obj_from_addr(const void *);
131 _rtld_call_fini_functions(int force
)
137 dbg(("_rtld_call_fini_functions(%d)", force
));
139 SIMPLEQ_INIT(&finilist
);
140 _rtld_initlist_tsort(&finilist
, 1);
142 /* First pass: objects _not_ marked with DF_1_INITFIRST. */
143 SIMPLEQ_FOREACH(elm
, &finilist
, link
) {
145 if (obj
->refcount
> 0 && !force
) {
148 if (obj
->fini
== NULL
|| obj
->fini_called
|| obj
->initfirst
) {
151 dbg (("calling fini function %s at %p", obj
->path
,
153 obj
->fini_called
= 1;
157 /* Second pass: objects marked with DF_1_INITFIRST. */
158 SIMPLEQ_FOREACH(elm
, &finilist
, link
) {
160 if (obj
->refcount
> 0 && !force
) {
163 if (obj
->fini
== NULL
|| obj
->fini_called
) {
166 dbg (("calling fini function %s at %p (DF_1_INITFIRST)",
167 obj
->path
, (void *)obj
->fini
));
168 obj
->fini_called
= 1;
172 _rtld_objlist_clear(&finilist
);
176 _rtld_call_init_functions()
182 dbg(("_rtld_call_init_functions()"));
183 SIMPLEQ_INIT(&initlist
);
184 _rtld_initlist_tsort(&initlist
, 0);
186 /* First pass: objects marked with DF_1_INITFIRST. */
187 SIMPLEQ_FOREACH(elm
, &initlist
, link
) {
189 if (obj
->init
== NULL
|| obj
->init_called
|| !obj
->initfirst
) {
192 dbg (("calling init function %s at %p (DF_1_INITFIRST)",
193 obj
->path
, (void *)obj
->init
));
194 obj
->init_called
= 1;
198 /* Second pass: all other objects. */
199 SIMPLEQ_FOREACH(elm
, &initlist
, link
) {
201 if (obj
->init
== NULL
|| obj
->init_called
) {
204 dbg (("calling init function %s at %p", obj
->path
,
206 obj
->init_called
= 1;
210 _rtld_objlist_clear(&initlist
);
214 * Initialize the dynamic linker. The argument is the address at which
215 * the dynamic linker has been mapped into memory. The primary task of
216 * this function is to create an Obj_Entry for the dynamic linker and
217 * to resolve the PLT relocation for platforms that need it (those that
218 * define __HAVE_FUNCTION_DESCRIPTORS
221 _rtld_init(caddr_t mapbase
, caddr_t relocbase
, const char *execname
)
224 /* Conjure up an Obj_Entry structure for the dynamic linker. */
225 _rtld_objself
.path
= __UNCONST(_rtld_path
);
226 _rtld_objself
.pathlen
= sizeof(_rtld_path
)-1;
227 _rtld_objself
.rtld
= true;
228 _rtld_objself
.mapbase
= mapbase
;
229 _rtld_objself
.relocbase
= relocbase
;
230 _rtld_objself
.dynamic
= (Elf_Dyn
*) &_DYNAMIC
;
231 _rtld_objself
.strtab
= "_rtld_sym_zero";
234 * Set value to -relocabase so that
235 * _rtld_objself.relocbase + _rtld_smy_zero.st_value == 0
236 * This allows unresolved references to weak symbols to be computed
237 * to value a value of 0.
239 _rtld_sym_zero
.st_value
= -(uintptr_t)relocbase
;
241 _rtld_digest_dynamic(_rtld_path
, &_rtld_objself
);
242 assert(!_rtld_objself
.needed
);
243 #if !defined(__hppa__)
244 assert(!_rtld_objself
.pltrel
&& !_rtld_objself
.pltrela
);
246 _rtld_relocate_plt_objects(&_rtld_objself
);
248 #if !defined(__mips__) && !defined(__hppa__)
249 assert(!_rtld_objself
.pltgot
);
251 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
252 /* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
253 assert(!_rtld_objself
.textrel
);
256 _rtld_add_paths(execname
, &_rtld_default_paths
,
257 RTLD_DEFAULT_LIBRARY_PATH
);
259 #ifdef RTLD_ARCH_SUBDIR
260 _rtld_add_paths(execname
, &_rtld_default_paths
,
261 RTLD_DEFAULT_LIBRARY_PATH
"/" RTLD_ARCH_SUBDIR
);
265 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
267 _rtld_objlist
= &_rtld_objself
;
269 /* Make the object list empty again. */
270 _rtld_objlist
= NULL
;
271 _rtld_objtail
= &_rtld_objlist
;
273 _rtld_debug
.r_brk
= _rtld_debug_state
;
274 _rtld_debug
.r_state
= RT_CONSISTENT
;
278 * Cleanup procedure. It will be called (by the atexit() mechanism) just
279 * before the process exits.
284 dbg(("rtld_exit()"));
286 _rtld_call_fini_functions(1);
290 * Main entry point for dynamic linking. The argument is the stack
291 * pointer. The stack is expected to be laid out as described in the
292 * SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
293 * the stack pointer points to a word containing ARGC. Following that
294 * in the stack is a null-terminated sequence of pointers to argument
295 * strings. Then comes a null-terminated sequence of pointers to
296 * environment strings. Finally, there is a sequence of "auxiliary
299 * This function returns the entry point for the main program, the dynamic
300 * linker's exit procedure in sp[0], and a pointer to the main object in
304 _rtld(Elf_Addr
*sp
, Elf_Addr relocbase
)
306 const AuxInfo
*pAUX_base
, *pAUX_entry
, *pAUX_execfd
, *pAUX_phdr
,
307 *pAUX_phent
, *pAUX_phnum
, *pAUX_euid
, *pAUX_egid
,
308 *pAUX_ruid
, *pAUX_rgid
;
309 const AuxInfo
*pAUX_pagesz
;
313 Elf_Addr
*const osp
= sp
;
315 const char *ld_bind_now
;
317 const char *execname
;
319 const char **real___progname
;
320 const Obj_Entry
**real___mainprog_obj
;
321 char ***real_environ
;
322 #if defined(RTLD_DEBUG)
327 * On entry, the dynamic linker itself has not been relocated yet.
328 * Be very careful not to reference any global data until after
329 * _rtld_init has returned. It is OK to reference file-scope statics
330 * and string constants, and to call static and global functions.
332 /* Find the auxiliary vector on the stack. */
333 /* first Elf_Word reserved to address of exit routine */
334 #if defined(RTLD_DEBUG)
336 dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp
,
337 (long)sp
[2], &sp
[3], (char *) sp
[3], (void *)relocbase
));
338 dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_
,
340 dbg(("_ctype_ is %p", _ctype_
));
343 sp
+= 2; /* skip over return argument space */
344 argv
= (const char **) &sp
[1];
346 sp
+= 2 + argc
; /* Skip over argc, arguments, and NULL
349 while (*sp
++ != 0) { /* Skip over environment, and NULL terminator */
350 #if defined(RTLD_DEBUG)
351 dbg(("env[%d] = %p %s", i
++, (void *)sp
[-1], (char *)sp
[-1]));
354 aux
= (const AuxInfo
*) sp
;
356 pAUX_base
= pAUX_entry
= pAUX_execfd
= NULL
;
357 pAUX_phdr
= pAUX_phent
= pAUX_phnum
= NULL
;
358 pAUX_euid
= pAUX_ruid
= pAUX_egid
= pAUX_rgid
= NULL
;
363 /* Digest the auxiliary vector. */
364 for (auxp
= aux
; auxp
->a_type
!= AT_NULL
; ++auxp
) {
365 switch (auxp
->a_type
) {
398 #ifdef AT_SUN_EXECNAME
399 case AT_SUN_EXECNAME
:
400 execname
= (const char *)(const void *)auxp
->a_v
;
409 /* Initialize and relocate ourselves. */
410 if (pAUX_base
== NULL
) {
411 _rtld_error("Bad pAUX_base");
414 assert(pAUX_pagesz
!= NULL
);
415 _rtld_pagesz
= (int)pAUX_pagesz
->a_v
;
416 _rtld_init((caddr_t
)pAUX_base
->a_v
, (caddr_t
)relocbase
, execname
);
418 __progname
= _rtld_objself
.path
;
421 _rtld_trust
= ((pAUX_euid
? (uid_t
)pAUX_euid
->a_v
: geteuid()) ==
422 (pAUX_ruid
? (uid_t
)pAUX_ruid
->a_v
: getuid())) &&
423 ((pAUX_egid
? (gid_t
)pAUX_egid
->a_v
: getegid()) ==
424 (pAUX_rgid
? (gid_t
)pAUX_rgid
->a_v
: getgid()));
426 ld_bind_now
= getenv("LD_BIND_NOW");
427 if (ld_bind_now
!= NULL
&& *ld_bind_now
!= '\0')
431 const char *ld_debug
= getenv("LD_DEBUG");
435 if (ld_debug
!= NULL
&& *ld_debug
!= '\0')
438 _rtld_add_paths(execname
, &_rtld_paths
,
439 getenv("LD_LIBRARY_PATH"));
442 if (unsetenv("LD_DEBUG") || unsetenv("LD_LIBRARY_PATH"))
445 _rtld_process_hints(execname
, &_rtld_paths
, &_rtld_xforms
,
447 dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
448 _rtld_objself
.mapbase
, _rtld_objself
.relocbase
));
451 * Load the main program, or process its program header if it is
454 if (pAUX_execfd
!= NULL
) { /* Load the main program. */
455 int fd
= pAUX_execfd
->a_v
;
456 const char *obj_name
= argv
[0] ? argv
[0] : "main program";
457 dbg(("loading main program"));
458 _rtld_objmain
= _rtld_map_object(obj_name
, fd
, NULL
);
460 if (_rtld_objmain
== NULL
)
462 } else { /* Main program already loaded. */
463 const Elf_Phdr
*phdr
;
467 dbg(("processing main program's program header"));
468 assert(pAUX_phdr
!= NULL
);
469 phdr
= (const Elf_Phdr
*) pAUX_phdr
->a_v
;
470 assert(pAUX_phnum
!= NULL
);
471 phnum
= pAUX_phnum
->a_v
;
472 assert(pAUX_phent
!= NULL
);
473 assert(pAUX_phent
->a_v
== sizeof(Elf_Phdr
));
474 assert(pAUX_entry
!= NULL
);
475 entry
= (caddr_t
) pAUX_entry
->a_v
;
476 _rtld_objmain
= _rtld_digest_phdr(phdr
, phnum
, entry
);
477 _rtld_objmain
->path
= xstrdup(argv
[0] ? argv
[0] :
479 _rtld_objmain
->pathlen
= strlen(_rtld_objmain
->path
);
482 _rtld_objmain
->mainprog
= true;
485 * Get the actual dynamic linker pathname from the executable if
486 * possible. (It should always be possible.) That ensures that
487 * gdb will find the right dynamic linker even if a non-standard
490 if (_rtld_objmain
->interp
!= NULL
&&
491 strcmp(_rtld_objmain
->interp
, _rtld_objself
.path
) != 0)
492 _rtld_objself
.path
= xstrdup(_rtld_objmain
->interp
);
493 dbg(("actual dynamic linker is %s", _rtld_objself
.path
));
495 _rtld_digest_dynamic(execname
, _rtld_objmain
);
497 /* Link the main program into the list of objects. */
498 *_rtld_objtail
= _rtld_objmain
;
499 _rtld_objtail
= &_rtld_objmain
->next
;
501 _rtld_linkmap_add(_rtld_objmain
);
502 _rtld_linkmap_add(&_rtld_objself
);
504 ++_rtld_objmain
->refcount
;
505 _rtld_objmain
->mainref
= 1;
506 _rtld_objlist_push_tail(&_rtld_list_main
, _rtld_objmain
);
510 * Pre-load user-specified objects after the main program
511 * but before any shared object dependencies.
513 dbg(("preloading objects"));
514 if (_rtld_preload(getenv("LD_PRELOAD")) == -1)
517 if (unsetenv("LD_PRELOAD"))
520 dbg(("loading needed objects"));
521 if (_rtld_load_needed_objects(_rtld_objmain
, RTLD_MAIN
) == -1)
524 dbg(("relocating objects"));
525 if (_rtld_relocate_objects(_rtld_objmain
, bind_now
) == -1)
528 dbg(("doing copy relocations"));
529 if (_rtld_do_copy_relocations(_rtld_objmain
) == -1)
533 * Set the __progname, environ and, __mainprog_obj before
534 * calling anything that might use them.
536 real___progname
= _rtld_objmain_sym("__progname");
537 if (real___progname
) {
538 if (argv
[0] != NULL
) {
539 if ((*real___progname
= strrchr(argv
[0], '/')) == NULL
)
540 (*real___progname
) = argv
[0];
542 (*real___progname
)++;
544 (*real___progname
) = NULL
;
547 real_environ
= _rtld_objmain_sym("environ");
549 *real_environ
= environ
;
551 * Set __mainprog_obj for old binaries.
553 real___mainprog_obj
= _rtld_objmain_sym("__mainprog_obj");
554 if (real___mainprog_obj
)
555 *real___mainprog_obj
= _rtld_objmain
;
557 dbg(("calling _init functions"));
558 _rtld_call_init_functions();
560 dbg(("control at program entry point = %p, obj = %p, exit = %p",
561 _rtld_objmain
->entry
, _rtld_objmain
, _rtld_exit
));
564 * Return with the entry point and the exit procedure in at the top
568 _rtld_debug_state(); /* say hello to gdb! */
570 ((void **) osp
)[0] = _rtld_exit
;
571 ((void **) osp
)[1] = _rtld_objmain
;
572 return (Elf_Addr
) _rtld_objmain
->entry
;
578 const char *msg
= dlerror();
586 _rtld_dlcheck(void *handle
)
590 for (obj
= _rtld_objlist
; obj
!= NULL
; obj
= obj
->next
)
591 if (obj
== (Obj_Entry
*) handle
)
594 if (obj
== NULL
|| obj
->dl_refcount
== 0) {
595 xwarnx("Invalid shared object handle %p", handle
);
602 _rtld_initlist_visit(Objlist
* list
, Obj_Entry
*obj
, int rev
)
606 /* dbg(("_rtld_initlist_visit(%s)", obj->path)); */
612 for (elm
= obj
->needed
; elm
!= NULL
; elm
= elm
->next
) {
613 if (elm
->obj
!= NULL
) {
614 _rtld_initlist_visit(list
, elm
->obj
, rev
);
619 _rtld_objlist_push_head(list
, obj
);
621 _rtld_objlist_push_tail(list
, obj
);
626 _rtld_initlist_tsort(Objlist
* list
, int rev
)
628 dbg(("_rtld_initlist_tsort"));
632 for (obj
= _rtld_objlist
->next
; obj
; obj
= obj
->next
) {
636 for (obj
= _rtld_objlist
->next
; obj
; obj
= obj
->next
) {
637 _rtld_initlist_visit(list
, obj
, rev
);
642 _rtld_init_dag(Obj_Entry
*root
)
645 _rtld_init_dag1(root
, root
);
649 _rtld_init_dag1(Obj_Entry
*root
, Obj_Entry
*obj
)
651 const Needed_Entry
*needed
;
654 if (_rtld_objlist_find(&obj
->dldags
, root
))
656 rdbg(("add %p (%s) to %p (%s) DAG", obj
, obj
->path
, root
,
658 _rtld_objlist_push_tail(&obj
->dldags
, root
);
659 _rtld_objlist_push_tail(&root
->dagmembers
, obj
);
661 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
)
662 if (needed
->obj
!= NULL
)
663 _rtld_init_dag1(root
, needed
->obj
);
667 * Note, this is called only for objects loaded by dlopen().
670 _rtld_unload_object(Obj_Entry
*root
, bool do_fini_funcs
)
673 _rtld_unref_dag(root
);
674 if (root
->refcount
== 0) { /* We are finished with some objects. */
679 /* Finalize objects that are about to be unmapped. */
681 _rtld_call_fini_functions(0);
683 /* Remove the DAG from all objects' DAG lists. */
684 SIMPLEQ_FOREACH(elm
, &root
->dagmembers
, link
)
685 _rtld_objlist_remove(&elm
->obj
->dldags
, root
);
687 /* Remove the DAG from the RTLD_GLOBAL list. */
688 if (root
->globalref
) {
690 _rtld_objlist_remove(&_rtld_list_global
, root
);
693 /* Unmap all objects that are no longer referenced. */
694 linkp
= &_rtld_objlist
->next
;
695 while ((obj
= *linkp
) != NULL
) {
696 if (obj
->refcount
== 0) {
698 dbg(("unloading \"%s\"", obj
->path
));
700 if (obj
->ehdr
!= MAP_FAILED
)
701 munmap(obj
->ehdr
, _rtld_pagesz
);
702 munmap(obj
->mapbase
, obj
->mapsize
);
703 _rtld_objlist_remove(&_rtld_list_global
, obj
);
704 _rtld_linkmap_delete(obj
);
710 _rtld_objtail
= linkp
;
715 _rtld_unref_dag(Obj_Entry
*root
)
719 assert(root
->refcount
!= 0);
721 if (root
->refcount
== 0) {
722 const Needed_Entry
*needed
;
724 for (needed
= root
->needed
; needed
!= NULL
;
725 needed
= needed
->next
) {
726 if (needed
->obj
!= NULL
)
727 _rtld_unref_dag(needed
->obj
);
732 __strong_alias(__dlclose
,dlclose
)
734 dlclose(void *handle
)
736 Obj_Entry
*root
= _rtld_dlcheck(handle
);
741 _rtld_debug
.r_state
= RT_DELETE
;
745 _rtld_unload_object(root
, true);
747 _rtld_debug
.r_state
= RT_CONSISTENT
;
753 __strong_alias(__dlerror
,dlerror
)
757 char *msg
= error_message
;
759 error_message
= NULL
;
763 __strong_alias(__dlopen
,dlopen
)
765 dlopen(const char *name
, int mode
)
767 Obj_Entry
**old_obj_tail
= _rtld_objtail
;
768 Obj_Entry
*obj
= NULL
;
770 _rtld_debug
.r_state
= RT_ADD
;
777 obj
= _rtld_load_library(name
, _rtld_objmain
, mode
);
781 if (*old_obj_tail
!= NULL
) { /* We loaded something new. */
782 assert(*old_obj_tail
== obj
);
784 if (_rtld_load_needed_objects(obj
, mode
) == -1 ||
785 (_rtld_init_dag(obj
),
786 _rtld_relocate_objects(obj
,
787 ((mode
& 3) == RTLD_NOW
))) == -1) {
788 _rtld_unload_object(obj
, false);
792 _rtld_call_init_functions();
796 _rtld_debug
.r_state
= RT_CONSISTENT
;
803 * Find a symbol in the main program.
806 _rtld_objmain_sym(const char *name
)
810 const Obj_Entry
*obj
;
812 hash
= _rtld_elf_hash(name
);
815 def
= _rtld_symlook_list(name
, hash
, &_rtld_list_main
, &obj
, false);
818 return obj
->relocbase
+ def
->st_value
;
824 hackish_return_address(void)
826 return __builtin_return_address(1);
830 __strong_alias(__dlsym
,dlsym
)
832 dlsym(void *handle
, const char *name
)
834 const Obj_Entry
*obj
;
837 const Obj_Entry
*defobj
;
840 hash
= _rtld_elf_hash(name
);
844 switch ((intptr_t)handle
) {
846 case (intptr_t)RTLD_NEXT
:
847 case (intptr_t)RTLD_DEFAULT
:
848 case (intptr_t)RTLD_SELF
:
850 retaddr
= hackish_return_address();
852 retaddr
= __builtin_return_address(0);
854 if ((obj
= _rtld_obj_from_addr(retaddr
)) == NULL
) {
855 _rtld_error("Cannot determine caller's shared object");
859 switch ((intptr_t)handle
) {
860 case (intptr_t)NULL
: /* Just the caller's shared object. */
861 def
= _rtld_symlook_obj(name
, hash
, obj
, false);
865 case (intptr_t)RTLD_NEXT
: /* Objects after callers */
869 case (intptr_t)RTLD_SELF
: /* Caller included */
870 for (; obj
; obj
= obj
->next
) {
871 if ((def
= _rtld_symlook_obj(name
, hash
, obj
,
879 case (intptr_t)RTLD_DEFAULT
:
880 def
= _rtld_symlook_default(name
, hash
, obj
, &defobj
,
890 if ((obj
= _rtld_dlcheck(handle
)) == NULL
)
894 /* Search main program and all libraries loaded by it */
895 def
= _rtld_symlook_list(name
, hash
, &_rtld_list_main
,
900 /* Search the object and all the libraries loaded by it. */
902 fake
.obj
= __UNCONST(obj
);
904 def
= _rtld_symlook_needed(name
, hash
, &fake
, &defobj
,
911 #ifdef __HAVE_FUNCTION_DESCRIPTORS
912 if (ELF_ST_TYPE(def
->st_info
) == STT_FUNC
)
913 return (void *)_rtld_function_descriptor_alloc(defobj
,
915 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
916 return defobj
->relocbase
+ def
->st_value
;
919 _rtld_error("Undefined symbol \"%s\"", name
);
923 __strong_alias(__dladdr
,dladdr
)
925 dladdr(const void *addr
, Dl_info
*info
)
927 const Obj_Entry
*obj
;
928 const Elf_Sym
*def
, *best_def
;
930 unsigned long symoffset
;
932 #ifdef __HAVE_FUNCTION_DESCRIPTORS
933 addr
= _rtld_function_descriptor_function(addr
);
934 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
936 obj
= _rtld_obj_from_addr(addr
);
938 _rtld_error("No shared object contains address");
941 info
->dli_fname
= obj
->path
;
942 info
->dli_fbase
= obj
->mapbase
;
943 info
->dli_saddr
= (void *)0;
944 info
->dli_sname
= NULL
;
947 * Walk the symbol list looking for the symbol whose address is
948 * closest to the address sent in.
951 for (symoffset
= 0; symoffset
< obj
->nchains
; symoffset
++) {
952 def
= obj
->symtab
+ symoffset
;
955 * For skip the symbol if st_shndx is either SHN_UNDEF or
958 if (def
->st_shndx
== SHN_UNDEF
|| def
->st_shndx
== SHN_COMMON
)
962 * If the symbol is greater than the specified address, or if it
963 * is further away from addr than the current nearest symbol,
966 symbol_addr
= obj
->relocbase
+ def
->st_value
;
967 if (symbol_addr
> addr
|| symbol_addr
< info
->dli_saddr
)
970 /* Update our idea of the nearest symbol. */
971 info
->dli_sname
= obj
->strtab
+ def
->st_name
;
972 info
->dli_saddr
= symbol_addr
;
976 if (info
->dli_saddr
== addr
)
980 #ifdef __HAVE_FUNCTION_DESCRIPTORS
981 if (best_def
!= NULL
&& ELF_ST_TYPE(best_def
->st_info
) == STT_FUNC
)
982 info
->dli_saddr
= (void *)_rtld_function_descriptor_alloc(obj
,
984 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
989 __strong_alias(__dlinfo
,dlinfo
)
991 dlinfo(void *handle
, int req
, void *v
)
993 const Obj_Entry
*obj
;
996 if (handle
== RTLD_SELF
) {
998 retaddr
= hackish_return_address();
1000 retaddr
= __builtin_return_address(0);
1002 if ((obj
= _rtld_obj_from_addr(retaddr
)) == NULL
) {
1003 _rtld_error("Cannot determine caller's shared object");
1007 if ((obj
= _rtld_dlcheck(handle
)) == NULL
) {
1008 _rtld_error("Invalid handle");
1014 case RTLD_DI_LINKMAP
:
1016 const struct link_map
**map
= v
;
1018 *map
= &obj
->linkmap
;
1023 _rtld_error("Invalid request");
1031 * Error reporting function. Use it like printf. If formats the message
1032 * into a buffer, and sets things up so that the next call to dlerror()
1033 * will return the message.
1036 _rtld_error(const char *fmt
,...)
1038 static char buf
[512];
1042 xvsnprintf(buf
, sizeof buf
, fmt
, ap
);
1043 error_message
= buf
;
1048 _rtld_debug_state(void)
1055 _rtld_linkmap_add(Obj_Entry
*obj
)
1057 struct link_map
*l
= &obj
->linkmap
;
1058 struct link_map
*prev
;
1060 obj
->linkmap
.l_name
= obj
->path
;
1061 obj
->linkmap
.l_addr
= obj
->relocbase
;
1062 obj
->linkmap
.l_ld
= obj
->dynamic
;
1064 /* XXX This field is not standard and will be removed eventually. */
1065 obj
->linkmap
.l_offs
= obj
->relocbase
;
1068 if (_rtld_debug
.r_map
== NULL
) {
1069 _rtld_debug
.r_map
= l
;
1074 * Scan to the end of the list, but not past the entry for the
1075 * dynamic linker, which we want to keep at the very end.
1077 for (prev
= _rtld_debug
.r_map
;
1078 prev
->l_next
!= NULL
&& prev
->l_next
!= &_rtld_objself
.linkmap
;
1079 prev
= prev
->l_next
);
1082 l
->l_next
= prev
->l_next
;
1083 if (l
->l_next
!= NULL
)
1084 l
->l_next
->l_prev
= l
;
1089 _rtld_linkmap_delete(Obj_Entry
*obj
)
1091 struct link_map
*l
= &obj
->linkmap
;
1093 if (l
->l_prev
== NULL
) {
1094 if ((_rtld_debug
.r_map
= l
->l_next
) != NULL
)
1095 l
->l_next
->l_prev
= NULL
;
1098 if ((l
->l_prev
->l_next
= l
->l_next
) != NULL
)
1099 l
->l_next
->l_prev
= l
->l_prev
;
1103 _rtld_obj_from_addr(const void *addr
)
1107 for (obj
= _rtld_objlist
; obj
!= NULL
; obj
= obj
->next
) {
1108 if (addr
< (void *) obj
->mapbase
)
1110 if (addr
< (void *) (obj
->mapbase
+ obj
->mapsize
))
1117 _rtld_objlist_clear(Objlist
*list
)
1119 while (!SIMPLEQ_EMPTY(list
)) {
1120 Objlist_Entry
* elm
= SIMPLEQ_FIRST(list
);
1121 SIMPLEQ_REMOVE_HEAD(list
, link
);
1127 _rtld_objlist_remove(Objlist
*list
, Obj_Entry
*obj
)
1131 if ((elm
= _rtld_objlist_find(list
, obj
)) != NULL
) {
1132 SIMPLEQ_REMOVE(list
, elm
, Struct_Objlist_Entry
, link
);