1 /* $NetBSD: rtld.c,v 1.137 2010/12/24 12:41:43 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>.
42 #define munmap minix_munmap
45 #include <sys/cdefs.h>
47 __RCSID("$NetBSD: rtld.c,v 1.137 2010/12/24 12:41:43 skrll Exp $");
58 #include <sys/param.h>
73 * Function declarations.
75 static void _rtld_init(caddr_t
, caddr_t
, const char *);
76 static void _rtld_exit(void);
78 Elf_Addr
_rtld(Elf_Addr
*, Elf_Addr
);
84 static char *error_message
; /* Message for dlopen(), or NULL */
86 struct r_debug _rtld_debug
; /* for GDB; */
87 bool _rtld_trust
; /* False for setuid and setgid programs */
88 Obj_Entry
*_rtld_objlist
; /* Head of linked list of shared objects */
89 Obj_Entry
**_rtld_objtail
; /* Link field of last object in list */
90 Obj_Entry
*_rtld_objmain
; /* The main program shared object */
91 Obj_Entry _rtld_objself
; /* The dynamic linker shared object */
92 u_int _rtld_objcount
; /* Number of objects in _rtld_objlist */
93 u_int _rtld_objloads
; /* Number of objects loaded in _rtld_objlist */
94 const char _rtld_path
[] = _PATH_RTLD
;
96 /* Initialize a fake symbol for resolving undefined weak references. */
97 Elf_Sym _rtld_sym_zero
= {
98 .st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_NOTYPE
),
101 size_t _rtld_pagesz
; /* Page size, as provided by kernel */
103 Search_Path
*_rtld_default_paths
;
104 Search_Path
*_rtld_paths
;
106 Library_Xform
*_rtld_xforms
;
109 * Global declarations normally provided by crt0.
114 #if defined(RTLD_DEBUG)
116 extern Elf_Addr _GLOBAL_OFFSET_TABLE_
[];
117 #else /* 32-bit SuperH */
118 register Elf_Addr
*_GLOBAL_OFFSET_TABLE_
asm("r12");
120 #endif /* RTLD_DEBUG */
121 extern Elf_Dyn _DYNAMIC
;
123 static void _rtld_call_fini_functions(int);
124 static void _rtld_call_init_functions(void);
125 static void _rtld_initlist_visit(Objlist
*, Obj_Entry
*, int);
126 static void _rtld_initlist_tsort(Objlist
*, int);
127 static Obj_Entry
*_rtld_dlcheck(void *);
128 static void _rtld_init_dag(Obj_Entry
*);
129 static void _rtld_init_dag1(Obj_Entry
*, Obj_Entry
*);
130 static void _rtld_objlist_remove(Objlist
*, Obj_Entry
*);
131 static void _rtld_objlist_clear(Objlist
*);
132 static void _rtld_unload_object(Obj_Entry
*, bool);
133 static void _rtld_unref_dag(Obj_Entry
*);
134 static Obj_Entry
*_rtld_obj_from_addr(const void *);
137 _rtld_call_fini_functions(int force
)
143 dbg(("_rtld_call_fini_functions(%d)", force
));
145 SIMPLEQ_INIT(&finilist
);
146 _rtld_initlist_tsort(&finilist
, 1);
148 /* First pass: objects _not_ marked with DF_1_INITFIRST. */
149 SIMPLEQ_FOREACH(elm
, &finilist
, link
) {
151 if (obj
->refcount
> 0 && !force
) {
154 if (obj
->fini
== NULL
|| obj
->fini_called
|| obj
->z_initfirst
) {
157 dbg (("calling fini function %s at %p", obj
->path
,
159 obj
->fini_called
= 1;
163 /* Second pass: objects marked with DF_1_INITFIRST. */
164 SIMPLEQ_FOREACH(elm
, &finilist
, link
) {
166 if (obj
->refcount
> 0 && !force
) {
169 if (obj
->fini
== NULL
|| obj
->fini_called
) {
172 dbg (("calling fini function %s at %p (DF_1_INITFIRST)",
173 obj
->path
, (void *)obj
->fini
));
174 obj
->fini_called
= 1;
178 _rtld_objlist_clear(&finilist
);
182 _rtld_call_init_functions()
188 dbg(("_rtld_call_init_functions()"));
189 SIMPLEQ_INIT(&initlist
);
190 _rtld_initlist_tsort(&initlist
, 0);
192 /* First pass: objects marked with DF_1_INITFIRST. */
193 SIMPLEQ_FOREACH(elm
, &initlist
, link
) {
195 if (obj
->init
== NULL
|| obj
->init_called
|| !obj
->z_initfirst
) {
198 dbg (("calling init function %s at %p (DF_1_INITFIRST)",
199 obj
->path
, (void *)obj
->init
));
200 obj
->init_called
= 1;
204 /* Second pass: all other objects. */
205 SIMPLEQ_FOREACH(elm
, &initlist
, link
) {
207 if (obj
->init
== NULL
|| obj
->init_called
) {
210 dbg (("calling init function %s at %p", obj
->path
,
212 obj
->init_called
= 1;
216 _rtld_objlist_clear(&initlist
);
220 * Initialize the dynamic linker. The argument is the address at which
221 * the dynamic linker has been mapped into memory. The primary task of
222 * this function is to create an Obj_Entry for the dynamic linker and
223 * to resolve the PLT relocation for platforms that need it (those that
224 * define __HAVE_FUNCTION_DESCRIPTORS
227 _rtld_init(caddr_t mapbase
, caddr_t relocbase
, const char *execname
)
230 /* Conjure up an Obj_Entry structure for the dynamic linker. */
231 _rtld_objself
.path
= __UNCONST(_rtld_path
);
232 _rtld_objself
.pathlen
= sizeof(_rtld_path
)-1;
233 _rtld_objself
.rtld
= true;
234 _rtld_objself
.mapbase
= mapbase
;
235 _rtld_objself
.relocbase
= relocbase
;
236 _rtld_objself
.dynamic
= (Elf_Dyn
*) &_DYNAMIC
;
237 _rtld_objself
.strtab
= "_rtld_sym_zero";
240 * Set value to -relocbase so that
242 * _rtld_objself.relocbase + _rtld_sym_zero.st_value == 0
244 * This allows unresolved references to weak symbols to be computed
247 _rtld_sym_zero
.st_value
= -(uintptr_t)relocbase
;
249 _rtld_digest_dynamic(_rtld_path
, &_rtld_objself
);
250 assert(!_rtld_objself
.needed
);
251 #if !defined(__hppa__)
252 assert(!_rtld_objself
.pltrel
&& !_rtld_objself
.pltrela
);
254 _rtld_relocate_plt_objects(&_rtld_objself
);
256 #if !defined(__mips__) && !defined(__hppa__)
257 assert(!_rtld_objself
.pltgot
);
259 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
260 /* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
261 assert(!_rtld_objself
.textrel
);
264 _rtld_add_paths(execname
, &_rtld_default_paths
,
265 RTLD_DEFAULT_LIBRARY_PATH
);
267 #ifdef RTLD_ARCH_SUBDIR
268 _rtld_add_paths(execname
, &_rtld_default_paths
,
269 RTLD_DEFAULT_LIBRARY_PATH
"/" RTLD_ARCH_SUBDIR
);
273 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
275 _rtld_objlist
= &_rtld_objself
;
277 /* Make the object list empty again. */
278 _rtld_objlist
= NULL
;
279 _rtld_objtail
= &_rtld_objlist
;
282 _rtld_debug
.r_brk
= _rtld_debug_state
;
283 _rtld_debug
.r_state
= RT_CONSISTENT
;
287 * Cleanup procedure. It will be called (by the atexit() mechanism) just
288 * before the process exits.
293 dbg(("rtld_exit()"));
295 _rtld_call_fini_functions(1);
299 * Main entry point for dynamic linking. The argument is the stack
300 * pointer. The stack is expected to be laid out as described in the
301 * SVR4 ABI specification, Intel 386 Processor Supplement. Specifically,
302 * the stack pointer points to a word containing ARGC. Following that
303 * in the stack is a null-terminated sequence of pointers to argument
304 * strings. Then comes a null-terminated sequence of pointers to
305 * environment strings. Finally, there is a sequence of "auxiliary
308 * This function returns the entry point for the main program, the dynamic
309 * linker's exit procedure in sp[0], and a pointer to the main object in
313 _rtld(Elf_Addr
*sp
, Elf_Addr relocbase
)
315 const AuxInfo
*pAUX_base
, *pAUX_entry
, *pAUX_execfd
, *pAUX_phdr
,
316 *pAUX_phent
, *pAUX_phnum
, *pAUX_euid
, *pAUX_egid
,
317 *pAUX_ruid
, *pAUX_rgid
;
318 const AuxInfo
*pAUX_pagesz
;
322 Elf_Addr
*const osp
= sp
;
324 const char *ld_bind_now
, *ld_preload
, *ld_library_path
;
326 const char *execname
;
328 const char **real___progname
;
329 const Obj_Entry
**real___mainprog_obj
;
330 char ***real_environ
;
333 const char *ld_debug
;
337 * On entry, the dynamic linker itself has not been relocated yet.
338 * Be very careful not to reference any global data until after
339 * _rtld_init has returned. It is OK to reference file-scope statics
340 * and string constants, and to call static and global functions.
342 /* Find the auxiliary vector on the stack. */
343 /* first Elf_Word reserved to address of exit routine */
344 #if defined(RTLD_DEBUG)
346 dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp
,
347 (long)sp
[2], &sp
[3], (char *) sp
[3], (void *)relocbase
));
348 dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_
,
350 dbg(("_ctype_ is %p", _ctype_
));
353 sp
+= 2; /* skip over return argument space */
354 argv
= (const char **) &sp
[1];
356 sp
+= 2 + argc
; /* Skip over argc, arguments, and NULL
359 while (*sp
++ != 0) { /* Skip over environment, and NULL terminator */
360 #if defined(RTLD_DEBUG)
361 dbg(("env[%d] = %p %s", i
++, (void *)sp
[-1], (char *)sp
[-1]));
364 aux
= (const AuxInfo
*) sp
;
366 pAUX_base
= pAUX_entry
= pAUX_execfd
= NULL
;
367 pAUX_phdr
= pAUX_phent
= pAUX_phnum
= NULL
;
368 pAUX_euid
= pAUX_ruid
= pAUX_egid
= pAUX_rgid
= NULL
;
373 /* Digest the auxiliary vector. */
374 for (auxp
= aux
; auxp
->a_type
!= AT_NULL
; ++auxp
) {
375 switch (auxp
->a_type
) {
408 #ifdef AT_SUN_EXECNAME
409 case AT_SUN_EXECNAME
:
410 execname
= (const char *)(const void *)auxp
->a_v
;
419 /* Initialize and relocate ourselves. */
420 if (pAUX_base
== NULL
) {
421 _rtld_error("Bad pAUX_base");
424 assert(pAUX_pagesz
!= NULL
);
425 _rtld_pagesz
= (int)pAUX_pagesz
->a_v
;
426 _rtld_init((caddr_t
)pAUX_base
->a_v
, (caddr_t
)relocbase
, execname
);
428 __progname
= _rtld_objself
.path
;
431 _rtld_trust
= ((pAUX_euid
? (uid_t
)pAUX_euid
->a_v
: geteuid()) ==
432 (pAUX_ruid
? (uid_t
)pAUX_ruid
->a_v
: getuid())) &&
433 ((pAUX_egid
? (gid_t
)pAUX_egid
->a_v
: getegid()) ==
434 (pAUX_rgid
? (gid_t
)pAUX_rgid
->a_v
: getgid()));
440 ld_library_path
= NULL
;
443 * Inline avoid using normal getenv/unsetenv here as the libc
444 * code is quite a bit more complicated.
446 for (oenvp
= env
; *env
!= NULL
; ++env
) {
447 static const char bind_var
[] = "LD_BIND_NOW=";
448 static const char debug_var
[] = "LD_DEBUG=";
449 static const char path_var
[] = "LD_LIBRARY_PATH=";
450 static const char preload_var
[] = "LD_PRELOAD=";
451 #define LEN(x) (sizeof(x) - 1)
453 if ((*env
)[0] != 'L' || (*env
)[1] != 'D') {
455 * Special case to skip most entries without
456 * the more expensive calls to strncmp.
459 } else if (strncmp(*env
, debug_var
, LEN(debug_var
)) == 0) {
462 ld_debug
= *env
+ LEN(debug_var
);
466 } else if (strncmp(*env
, bind_var
, LEN(bind_var
)) == 0) {
467 ld_bind_now
= *env
+ LEN(bind_var
);
468 } else if (strncmp(*env
, path_var
, LEN(path_var
)) == 0) {
470 ld_library_path
= *env
+ LEN(path_var
);
473 } else if (strncmp(*env
, preload_var
, LEN(preload_var
)) == 0) {
475 ld_preload
= *env
+ LEN(preload_var
);
485 if (ld_bind_now
!= NULL
&& *ld_bind_now
!= '\0')
492 if (ld_debug
!= NULL
&& *ld_debug
!= '\0')
495 _rtld_add_paths(execname
, &_rtld_paths
, ld_library_path
);
499 _rtld_process_hints(execname
, &_rtld_paths
, &_rtld_xforms
,
501 dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
502 _rtld_objself
.mapbase
, _rtld_objself
.relocbase
));
505 * Load the main program, or process its program header if it is
508 if (pAUX_execfd
!= NULL
) { /* Load the main program. */
509 int fd
= pAUX_execfd
->a_v
;
510 const char *obj_name
= argv
[0] ? argv
[0] : "main program";
511 dbg(("loading main program"));
512 _rtld_objmain
= _rtld_map_object(obj_name
, fd
, NULL
);
514 if (_rtld_objmain
== NULL
)
516 } else { /* Main program already loaded. */
517 const Elf_Phdr
*phdr
;
521 dbg(("processing main program's program header"));
522 assert(pAUX_phdr
!= NULL
);
523 phdr
= (const Elf_Phdr
*) pAUX_phdr
->a_v
;
524 assert(pAUX_phnum
!= NULL
);
525 phnum
= pAUX_phnum
->a_v
;
526 assert(pAUX_phent
!= NULL
);
527 assert(pAUX_phent
->a_v
== sizeof(Elf_Phdr
));
528 assert(pAUX_entry
!= NULL
);
529 entry
= (caddr_t
) pAUX_entry
->a_v
;
530 _rtld_objmain
= _rtld_digest_phdr(phdr
, phnum
, entry
);
531 _rtld_objmain
->path
= xstrdup(argv
[0] ? argv
[0] :
533 _rtld_objmain
->pathlen
= strlen(_rtld_objmain
->path
);
536 _rtld_objmain
->mainprog
= true;
539 * Get the actual dynamic linker pathname from the executable if
540 * possible. (It should always be possible.) That ensures that
541 * gdb will find the right dynamic linker even if a non-standard
544 if (_rtld_objmain
->interp
!= NULL
&&
545 strcmp(_rtld_objmain
->interp
, _rtld_objself
.path
) != 0)
546 _rtld_objself
.path
= xstrdup(_rtld_objmain
->interp
);
547 dbg(("actual dynamic linker is %s", _rtld_objself
.path
));
549 _rtld_digest_dynamic(execname
, _rtld_objmain
);
551 /* Link the main program into the list of objects. */
552 *_rtld_objtail
= _rtld_objmain
;
553 _rtld_objtail
= &_rtld_objmain
->next
;
557 _rtld_linkmap_add(_rtld_objmain
);
558 _rtld_linkmap_add(&_rtld_objself
);
560 ++_rtld_objmain
->refcount
;
561 _rtld_objmain
->mainref
= 1;
562 _rtld_objlist_push_tail(&_rtld_list_main
, _rtld_objmain
);
566 * Pre-load user-specified objects after the main program
567 * but before any shared object dependencies.
569 dbg(("preloading objects"));
570 if (_rtld_preload(ld_preload
) == -1)
574 dbg(("loading needed objects"));
575 if (_rtld_load_needed_objects(_rtld_objmain
, _RTLD_MAIN
) == -1)
578 dbg(("relocating objects"));
579 if (_rtld_relocate_objects(_rtld_objmain
, bind_now
) == -1)
582 dbg(("doing copy relocations"));
583 if (_rtld_do_copy_relocations(_rtld_objmain
) == -1)
587 * Set the __progname, environ and, __mainprog_obj before
588 * calling anything that might use them.
590 real___progname
= _rtld_objmain_sym("__progname");
591 if (real___progname
) {
592 if (argv
[0] != NULL
) {
593 if ((*real___progname
= strrchr(argv
[0], '/')) == NULL
)
594 (*real___progname
) = argv
[0];
596 (*real___progname
)++;
598 (*real___progname
) = NULL
;
601 real_environ
= _rtld_objmain_sym("environ");
603 *real_environ
= environ
;
605 * Set __mainprog_obj for old binaries.
607 real___mainprog_obj
= _rtld_objmain_sym("__mainprog_obj");
608 if (real___mainprog_obj
)
609 *real___mainprog_obj
= _rtld_objmain
;
611 dbg(("calling _init functions"));
612 _rtld_call_init_functions();
614 dbg(("control at program entry point = %p, obj = %p, exit = %p",
615 _rtld_objmain
->entry
, _rtld_objmain
, _rtld_exit
));
618 * Return with the entry point and the exit procedure in at the top
622 _rtld_debug_state(); /* say hello to gdb! */
624 ((void **) osp
)[0] = _rtld_exit
;
625 ((void **) osp
)[1] = _rtld_objmain
;
626 return (Elf_Addr
) _rtld_objmain
->entry
;
632 const char *msg
= dlerror();
640 _rtld_dlcheck(void *handle
)
644 for (obj
= _rtld_objlist
; obj
!= NULL
; obj
= obj
->next
)
645 if (obj
== (Obj_Entry
*) handle
)
648 if (obj
== NULL
|| obj
->dl_refcount
== 0) {
649 xwarnx("Invalid shared object handle %p", handle
);
656 _rtld_initlist_visit(Objlist
* list
, Obj_Entry
*obj
, int rev
)
660 /* dbg(("_rtld_initlist_visit(%s)", obj->path)); */
666 for (elm
= obj
->needed
; elm
!= NULL
; elm
= elm
->next
) {
667 if (elm
->obj
!= NULL
) {
668 _rtld_initlist_visit(list
, elm
->obj
, rev
);
673 _rtld_objlist_push_head(list
, obj
);
675 _rtld_objlist_push_tail(list
, obj
);
680 _rtld_initlist_tsort(Objlist
* list
, int rev
)
682 dbg(("_rtld_initlist_tsort"));
686 for (obj
= _rtld_objlist
->next
; obj
; obj
= obj
->next
) {
690 for (obj
= _rtld_objlist
->next
; obj
; obj
= obj
->next
) {
691 _rtld_initlist_visit(list
, obj
, rev
);
696 _rtld_init_dag(Obj_Entry
*root
)
699 _rtld_init_dag1(root
, root
);
703 _rtld_init_dag1(Obj_Entry
*root
, Obj_Entry
*obj
)
705 const Needed_Entry
*needed
;
708 if (_rtld_objlist_find(&obj
->dldags
, root
))
710 dbg(("add %p (%s) to %p (%s) DAG", obj
, obj
->path
, root
,
712 _rtld_objlist_push_tail(&obj
->dldags
, root
);
713 _rtld_objlist_push_tail(&root
->dagmembers
, obj
);
715 for (needed
= obj
->needed
; needed
!= NULL
; needed
= needed
->next
)
716 if (needed
->obj
!= NULL
)
717 _rtld_init_dag1(root
, needed
->obj
);
721 * Note, this is called only for objects loaded by dlopen().
724 _rtld_unload_object(Obj_Entry
*root
, bool do_fini_funcs
)
727 _rtld_unref_dag(root
);
728 if (root
->refcount
== 0) { /* We are finished with some objects. */
733 /* Finalize objects that are about to be unmapped. */
735 _rtld_call_fini_functions(0);
737 /* Remove the DAG from all objects' DAG lists. */
738 SIMPLEQ_FOREACH(elm
, &root
->dagmembers
, link
)
739 _rtld_objlist_remove(&elm
->obj
->dldags
, root
);
741 /* Remove the DAG from the RTLD_GLOBAL list. */
742 if (root
->globalref
) {
744 _rtld_objlist_remove(&_rtld_list_global
, root
);
747 /* Unmap all objects that are no longer referenced. */
748 linkp
= &_rtld_objlist
->next
;
749 while ((obj
= *linkp
) != NULL
) {
750 if (obj
->refcount
== 0) {
751 dbg(("unloading \"%s\"", obj
->path
));
752 if (obj
->ehdr
!= MAP_FAILED
)
753 munmap(obj
->ehdr
, _rtld_pagesz
);
754 munmap(obj
->mapbase
, obj
->mapsize
);
755 _rtld_objlist_remove(&_rtld_list_global
, obj
);
756 _rtld_linkmap_delete(obj
);
763 _rtld_objtail
= linkp
;
768 _rtld_ref_dag(Obj_Entry
*root
)
770 const Needed_Entry
*needed
;
776 dbg(("incremented reference on \"%s\" (%d)", root
->path
,
778 for (needed
= root
->needed
; needed
!= NULL
;
779 needed
= needed
->next
) {
780 if (needed
->obj
!= NULL
)
781 _rtld_ref_dag(needed
->obj
);
786 _rtld_unref_dag(Obj_Entry
*root
)
790 assert(root
->refcount
!= 0);
793 dbg(("decremented reference on \"%s\" (%d)", root
->path
,
796 if (root
->refcount
== 0) {
797 const Needed_Entry
*needed
;
799 for (needed
= root
->needed
; needed
!= NULL
;
800 needed
= needed
->next
) {
801 if (needed
->obj
!= NULL
)
802 _rtld_unref_dag(needed
->obj
);
807 __strong_alias(__dlclose
,dlclose
)
809 dlclose(void *handle
)
811 Obj_Entry
*root
= _rtld_dlcheck(handle
);
816 _rtld_debug
.r_state
= RT_DELETE
;
820 _rtld_unload_object(root
, true);
822 _rtld_debug
.r_state
= RT_CONSISTENT
;
828 __strong_alias(__dlerror
,dlerror
)
832 char *msg
= error_message
;
834 error_message
= NULL
;
838 __strong_alias(__dlopen
,dlopen
)
840 dlopen(const char *name
, int mode
)
842 Obj_Entry
**old_obj_tail
= _rtld_objtail
;
843 Obj_Entry
*obj
= NULL
;
844 int flags
= _RTLD_DLOPEN
;
848 flags
|= (mode
& RTLD_GLOBAL
) ? _RTLD_GLOBAL
: 0;
849 flags
|= (mode
& RTLD_NOLOAD
) ? _RTLD_NOLOAD
: 0;
851 nodelete
= (mode
& RTLD_NODELETE
) ? true : false;
852 now
= ((mode
& RTLD_MODEMASK
) == RTLD_NOW
) ? true : false;
854 _rtld_debug
.r_state
= RT_ADD
;
861 obj
= _rtld_load_library(name
, _rtld_objmain
, flags
);
866 if (*old_obj_tail
!= NULL
) { /* We loaded something new. */
867 assert(*old_obj_tail
== obj
);
869 if (_rtld_load_needed_objects(obj
, flags
) == -1 ||
870 (_rtld_init_dag(obj
),
871 _rtld_relocate_objects(obj
,
872 (now
|| obj
->z_now
))) == -1) {
873 _rtld_unload_object(obj
, false);
877 _rtld_call_init_functions();
881 if ((nodelete
|| obj
->z_nodelete
) && !obj
->ref_nodel
) {
882 dbg(("dlopen obj %s nodelete", obj
->path
));
884 obj
->z_nodelete
= obj
->ref_nodel
= true;
888 _rtld_debug
.r_state
= RT_CONSISTENT
;
895 * Find a symbol in the main program.
898 _rtld_objmain_sym(const char *name
)
902 const Obj_Entry
*obj
;
905 hash
= _rtld_elf_hash(name
);
907 _rtld_donelist_init(&donelist
);
909 def
= _rtld_symlook_list(name
, hash
, &_rtld_list_main
, &obj
, false,
913 return obj
->relocbase
+ def
->st_value
;
919 hackish_return_address(void)
921 return __builtin_return_address(1);
925 __strong_alias(__dlsym
,dlsym
)
927 dlsym(void *handle
, const char *name
)
929 const Obj_Entry
*obj
;
932 const Obj_Entry
*defobj
;
936 hash
= _rtld_elf_hash(name
);
940 switch ((intptr_t)handle
) {
942 case (intptr_t)RTLD_NEXT
:
943 case (intptr_t)RTLD_DEFAULT
:
944 case (intptr_t)RTLD_SELF
:
946 retaddr
= hackish_return_address();
948 retaddr
= __builtin_return_address(0);
950 if ((obj
= _rtld_obj_from_addr(retaddr
)) == NULL
) {
951 _rtld_error("Cannot determine caller's shared object");
955 switch ((intptr_t)handle
) {
956 case (intptr_t)NULL
: /* Just the caller's shared object. */
957 def
= _rtld_symlook_obj(name
, hash
, obj
, false);
961 case (intptr_t)RTLD_NEXT
: /* Objects after callers */
965 case (intptr_t)RTLD_SELF
: /* Caller included */
966 for (; obj
; obj
= obj
->next
) {
967 if ((def
= _rtld_symlook_obj(name
, hash
, obj
,
975 case (intptr_t)RTLD_DEFAULT
:
976 def
= _rtld_symlook_default(name
, hash
, obj
, &defobj
,
986 if ((obj
= _rtld_dlcheck(handle
)) == NULL
)
989 _rtld_donelist_init(&donelist
);
992 /* Search main program and all libraries loaded by it */
993 def
= _rtld_symlook_list(name
, hash
, &_rtld_list_main
,
994 &defobj
, false, &donelist
);
999 /* Search the object and all the libraries loaded by it. */
1001 fake
.obj
= __UNCONST(obj
);
1004 _rtld_donelist_init(&depth
);
1005 def
= _rtld_symlook_needed(name
, hash
, &fake
, &defobj
,
1006 false, &donelist
, &depth
);
1013 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1014 if (ELF_ST_TYPE(def
->st_info
) == STT_FUNC
)
1015 return (void *)_rtld_function_descriptor_alloc(defobj
,
1017 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1018 return defobj
->relocbase
+ def
->st_value
;
1021 _rtld_error("Undefined symbol \"%s\"", name
);
1025 __strong_alias(__dladdr
,dladdr
)
1027 dladdr(const void *addr
, Dl_info
*info
)
1029 const Obj_Entry
*obj
;
1030 const Elf_Sym
*def
, *best_def
;
1032 unsigned long symoffset
;
1034 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1035 addr
= _rtld_function_descriptor_function(addr
);
1036 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1038 obj
= _rtld_obj_from_addr(addr
);
1040 _rtld_error("No shared object contains address");
1043 info
->dli_fname
= obj
->path
;
1044 info
->dli_fbase
= obj
->mapbase
;
1045 info
->dli_saddr
= (void *)0;
1046 info
->dli_sname
= NULL
;
1049 * Walk the symbol list looking for the symbol whose address is
1050 * closest to the address sent in.
1053 for (symoffset
= 0; symoffset
< obj
->nchains
; symoffset
++) {
1054 def
= obj
->symtab
+ symoffset
;
1057 * For skip the symbol if st_shndx is either SHN_UNDEF or
1060 if (def
->st_shndx
== SHN_UNDEF
|| def
->st_shndx
== SHN_COMMON
)
1064 * If the symbol is greater than the specified address, or if it
1065 * is further away from addr than the current nearest symbol,
1068 symbol_addr
= obj
->relocbase
+ def
->st_value
;
1069 if (symbol_addr
> addr
|| symbol_addr
< info
->dli_saddr
)
1072 /* Update our idea of the nearest symbol. */
1073 info
->dli_sname
= obj
->strtab
+ def
->st_name
;
1074 info
->dli_saddr
= symbol_addr
;
1078 if (info
->dli_saddr
== addr
)
1082 #ifdef __HAVE_FUNCTION_DESCRIPTORS
1083 if (best_def
!= NULL
&& ELF_ST_TYPE(best_def
->st_info
) == STT_FUNC
)
1084 info
->dli_saddr
= (void *)_rtld_function_descriptor_alloc(obj
,
1086 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1091 __strong_alias(__dlinfo
,dlinfo
)
1093 dlinfo(void *handle
, int req
, void *v
)
1095 const Obj_Entry
*obj
;
1098 if (handle
== RTLD_SELF
) {
1100 retaddr
= hackish_return_address();
1102 retaddr
= __builtin_return_address(0);
1104 if ((obj
= _rtld_obj_from_addr(retaddr
)) == NULL
) {
1105 _rtld_error("Cannot determine caller's shared object");
1109 if ((obj
= _rtld_dlcheck(handle
)) == NULL
) {
1110 _rtld_error("Invalid handle");
1116 case RTLD_DI_LINKMAP
:
1118 const struct link_map
**map
= v
;
1120 *map
= &obj
->linkmap
;
1125 _rtld_error("Invalid request");
1132 __strong_alias(__dl_iterate_phdr
,dl_iterate_phdr
);
1134 dl_iterate_phdr(int (*callback
)(struct dl_phdr_info
*, size_t, void *), void *param
)
1136 struct dl_phdr_info phdr_info
;
1137 const Obj_Entry
*obj
;
1140 for (obj
= _rtld_objlist
; obj
!= NULL
; obj
= obj
->next
) {
1141 phdr_info
.dlpi_addr
= (Elf_Addr
)obj
->relocbase
;
1142 phdr_info
.dlpi_name
= STAILQ_FIRST(&obj
->names
) ?
1143 STAILQ_FIRST(&obj
->names
)->name
: obj
->path
;
1144 phdr_info
.dlpi_phdr
= obj
->phdr
;
1145 phdr_info
.dlpi_phnum
= obj
->phsize
/ sizeof(obj
->phdr
[0]);
1147 phdr_info
.dlpi_tls_modid
= 0;
1148 phdr_info
.dlpi_tls_data
= 0;
1150 phdr_info
.dlpi_tls_modid
= obj
->tlsindex
;
1151 phdr_info
.dlpi_tls_data
= obj
->tlsinit
;
1153 phdr_info
.dlpi_adds
= _rtld_objloads
;
1154 phdr_info
.dlpi_subs
= _rtld_objloads
- _rtld_objcount
;
1156 error
= callback(&phdr_info
, sizeof(phdr_info
), param
);
1165 * Error reporting function. Use it like printf. If formats the message
1166 * into a buffer, and sets things up so that the next call to dlerror()
1167 * will return the message.
1170 _rtld_error(const char *fmt
,...)
1172 static char buf
[512];
1176 xvsnprintf(buf
, sizeof buf
, fmt
, ap
);
1177 error_message
= buf
;
1182 _rtld_debug_state(void)
1189 _rtld_linkmap_add(Obj_Entry
*obj
)
1191 struct link_map
*l
= &obj
->linkmap
;
1192 struct link_map
*prev
;
1194 obj
->linkmap
.l_name
= obj
->path
;
1195 obj
->linkmap
.l_addr
= obj
->relocbase
;
1196 obj
->linkmap
.l_ld
= obj
->dynamic
;
1198 /* XXX This field is not standard and will be removed eventually. */
1199 obj
->linkmap
.l_offs
= obj
->relocbase
;
1202 if (_rtld_debug
.r_map
== NULL
) {
1203 _rtld_debug
.r_map
= l
;
1208 * Scan to the end of the list, but not past the entry for the
1209 * dynamic linker, which we want to keep at the very end.
1211 for (prev
= _rtld_debug
.r_map
;
1212 prev
->l_next
!= NULL
&& prev
->l_next
!= &_rtld_objself
.linkmap
;
1213 prev
= prev
->l_next
);
1216 l
->l_next
= prev
->l_next
;
1217 if (l
->l_next
!= NULL
)
1218 l
->l_next
->l_prev
= l
;
1223 _rtld_linkmap_delete(Obj_Entry
*obj
)
1225 struct link_map
*l
= &obj
->linkmap
;
1227 if (l
->l_prev
== NULL
) {
1228 if ((_rtld_debug
.r_map
= l
->l_next
) != NULL
)
1229 l
->l_next
->l_prev
= NULL
;
1232 if ((l
->l_prev
->l_next
= l
->l_next
) != NULL
)
1233 l
->l_next
->l_prev
= l
->l_prev
;
1237 _rtld_obj_from_addr(const void *addr
)
1241 for (obj
= _rtld_objlist
; obj
!= NULL
; obj
= obj
->next
) {
1242 if (addr
< (void *) obj
->mapbase
)
1244 if (addr
< (void *) (obj
->mapbase
+ obj
->mapsize
))
1251 _rtld_objlist_clear(Objlist
*list
)
1253 while (!SIMPLEQ_EMPTY(list
)) {
1254 Objlist_Entry
* elm
= SIMPLEQ_FIRST(list
);
1255 SIMPLEQ_REMOVE_HEAD(list
, link
);
1261 _rtld_objlist_remove(Objlist
*list
, Obj_Entry
*obj
)
1265 if ((elm
= _rtld_objlist_find(list
, obj
)) != NULL
) {
1266 SIMPLEQ_REMOVE(list
, elm
, Struct_Objlist_Entry
, link
);