xref: /minix3/libexec/ld.elf_so/rtld.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: rtld.c,v 1.177 2015/04/06 09:34:15 yamt Exp $	 */
2e83f7ba2SBen Gras 
3e83f7ba2SBen Gras /*
4e83f7ba2SBen Gras  * Copyright 1996 John D. Polstra.
5e83f7ba2SBen Gras  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6e83f7ba2SBen Gras  * Copyright 2002 Charles M. Hannum <root@ihack.net>
7e83f7ba2SBen Gras  * All rights reserved.
8e83f7ba2SBen Gras  *
9e83f7ba2SBen Gras  * Redistribution and use in source and binary forms, with or without
10e83f7ba2SBen Gras  * modification, are permitted provided that the following conditions
11e83f7ba2SBen Gras  * are met:
12e83f7ba2SBen Gras  * 1. Redistributions of source code must retain the above copyright
13e83f7ba2SBen Gras  *    notice, this list of conditions and the following disclaimer.
14e83f7ba2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
15e83f7ba2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
16e83f7ba2SBen Gras  *    documentation and/or other materials provided with the distribution.
17e83f7ba2SBen Gras  * 3. All advertising materials mentioning features or use of this software
18e83f7ba2SBen Gras  *    must display the following acknowledgement:
19e83f7ba2SBen Gras  *      This product includes software developed by John Polstra.
20e83f7ba2SBen Gras  * 4. The name of the author may not be used to endorse or promote products
21e83f7ba2SBen Gras  *    derived from this software without specific prior written permission.
22e83f7ba2SBen Gras  *
23e83f7ba2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24e83f7ba2SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25e83f7ba2SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26e83f7ba2SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27e83f7ba2SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28e83f7ba2SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29e83f7ba2SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30e83f7ba2SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31e83f7ba2SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32e83f7ba2SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33e83f7ba2SBen Gras  */
34e83f7ba2SBen Gras 
35e83f7ba2SBen Gras /*
36e83f7ba2SBen Gras  * Dynamic linker for ELF.
37e83f7ba2SBen Gras  *
38e83f7ba2SBen Gras  * John Polstra <jdp@polstra.com>.
39e83f7ba2SBen Gras  */
40e83f7ba2SBen Gras 
41e83f7ba2SBen Gras #include <sys/cdefs.h>
42e83f7ba2SBen Gras #ifndef lint
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: rtld.c,v 1.177 2015/04/06 09:34:15 yamt Exp $");
44e83f7ba2SBen Gras #endif /* not lint */
45e83f7ba2SBen Gras 
46f14fb602SLionel Sambuc #include <sys/param.h>
47f14fb602SLionel Sambuc #include <sys/atomic.h>
48f14fb602SLionel Sambuc #include <sys/mman.h>
49e83f7ba2SBen Gras #include <err.h>
50e83f7ba2SBen Gras #include <errno.h>
51e83f7ba2SBen Gras #include <fcntl.h>
52f14fb602SLionel Sambuc #include <lwp.h>
53e83f7ba2SBen Gras #include <stdarg.h>
54e83f7ba2SBen Gras #include <stdio.h>
55e83f7ba2SBen Gras #include <stdlib.h>
56e83f7ba2SBen Gras #include <string.h>
57e83f7ba2SBen Gras #include <unistd.h>
58e83f7ba2SBen Gras #include <dirent.h>
59e83f7ba2SBen Gras 
60e83f7ba2SBen Gras #include <ctype.h>
61e83f7ba2SBen Gras 
62e83f7ba2SBen Gras #include <dlfcn.h>
63e83f7ba2SBen Gras #include "debug.h"
64e83f7ba2SBen Gras #include "rtld.h"
65e83f7ba2SBen Gras 
66e83f7ba2SBen Gras #if !defined(lint)
67e83f7ba2SBen Gras #include "sysident.h"
68e83f7ba2SBen Gras #endif
69e83f7ba2SBen Gras 
70e83f7ba2SBen Gras /*
71e83f7ba2SBen Gras  * Function declarations.
72e83f7ba2SBen Gras  */
73e83f7ba2SBen Gras static void     _rtld_init(caddr_t, caddr_t, const char *);
74e83f7ba2SBen Gras static void     _rtld_exit(void);
75e83f7ba2SBen Gras 
76e83f7ba2SBen Gras Elf_Addr        _rtld(Elf_Addr *, Elf_Addr);
77e83f7ba2SBen Gras 
78e83f7ba2SBen Gras 
79e83f7ba2SBen Gras /*
80e83f7ba2SBen Gras  * Data declarations.
81e83f7ba2SBen Gras  */
82e83f7ba2SBen Gras static char    *error_message;	/* Message for dlopen(), or NULL */
83e83f7ba2SBen Gras 
84e83f7ba2SBen Gras struct r_debug  _rtld_debug;	/* for GDB; */
85e83f7ba2SBen Gras bool            _rtld_trust;	/* False for setuid and setgid programs */
86e83f7ba2SBen Gras Obj_Entry      *_rtld_objlist;	/* Head of linked list of shared objects */
87e83f7ba2SBen Gras Obj_Entry     **_rtld_objtail;	/* Link field of last object in list */
88e83f7ba2SBen Gras Obj_Entry      *_rtld_objmain;	/* The main program shared object */
89e83f7ba2SBen Gras Obj_Entry       _rtld_objself;	/* The dynamic linker shared object */
90e83f7ba2SBen Gras u_int		_rtld_objcount;	/* Number of objects in _rtld_objlist */
91e83f7ba2SBen Gras u_int		_rtld_objloads;	/* Number of objects loaded in _rtld_objlist */
92f14fb602SLionel Sambuc u_int		_rtld_objgen;	/* Generation count for _rtld_objlist */
93e83f7ba2SBen Gras const char	_rtld_path[] = _PATH_RTLD;
94e83f7ba2SBen Gras 
95e83f7ba2SBen Gras /* Initialize a fake symbol for resolving undefined weak references. */
96e83f7ba2SBen Gras Elf_Sym		_rtld_sym_zero = {
97e83f7ba2SBen Gras     .st_info	= ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
98e83f7ba2SBen Gras     .st_shndx	= SHN_ABS,
99e83f7ba2SBen Gras };
100e83f7ba2SBen Gras size_t	_rtld_pagesz;	/* Page size, as provided by kernel */
101e83f7ba2SBen Gras 
102e83f7ba2SBen Gras Search_Path    *_rtld_default_paths;
103e83f7ba2SBen Gras Search_Path    *_rtld_paths;
104e83f7ba2SBen Gras 
105e83f7ba2SBen Gras Library_Xform  *_rtld_xforms;
106f14fb602SLionel Sambuc static void    *auxinfo;
107e83f7ba2SBen Gras 
108e83f7ba2SBen Gras /*
109e83f7ba2SBen Gras  * Global declarations normally provided by crt0.
110e83f7ba2SBen Gras  */
111e83f7ba2SBen Gras char           *__progname;
112e83f7ba2SBen Gras char          **environ;
113e83f7ba2SBen Gras 
114f14fb602SLionel Sambuc #if !defined(__minix)
115f14fb602SLionel Sambuc static volatile bool _rtld_mutex_may_recurse;
116f14fb602SLionel Sambuc #endif /* !defined(__minix) */
117f14fb602SLionel Sambuc 
118e83f7ba2SBen Gras #if defined(RTLD_DEBUG)
119e83f7ba2SBen Gras #ifndef __sh__
120e83f7ba2SBen Gras extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
121e83f7ba2SBen Gras #else  /* 32-bit SuperH */
122e83f7ba2SBen Gras register Elf_Addr *_GLOBAL_OFFSET_TABLE_ asm("r12");
123e83f7ba2SBen Gras #endif
124e83f7ba2SBen Gras #endif /* RTLD_DEBUG */
125e83f7ba2SBen Gras extern Elf_Dyn  _DYNAMIC;
126e83f7ba2SBen Gras 
127f14fb602SLionel Sambuc static void _rtld_call_fini_functions(sigset_t *, int);
128f14fb602SLionel Sambuc static void _rtld_call_init_functions(sigset_t *);
129e83f7ba2SBen Gras static void _rtld_initlist_visit(Objlist *, Obj_Entry *, int);
130e83f7ba2SBen Gras static void _rtld_initlist_tsort(Objlist *, int);
131e83f7ba2SBen Gras static Obj_Entry *_rtld_dlcheck(void *);
132e83f7ba2SBen Gras static void _rtld_init_dag(Obj_Entry *);
133e83f7ba2SBen Gras static void _rtld_init_dag1(Obj_Entry *, Obj_Entry *);
134e83f7ba2SBen Gras static void _rtld_objlist_remove(Objlist *, Obj_Entry *);
135e83f7ba2SBen Gras static void _rtld_objlist_clear(Objlist *);
136f14fb602SLionel Sambuc static void _rtld_unload_object(sigset_t *, Obj_Entry *, bool);
137e83f7ba2SBen Gras static void _rtld_unref_dag(Obj_Entry *);
138e83f7ba2SBen Gras static Obj_Entry *_rtld_obj_from_addr(const void *);
139e83f7ba2SBen Gras 
140f14fb602SLionel Sambuc static inline void
_rtld_call_initfini_function(const Obj_Entry * obj,Elf_Addr func,sigset_t * mask)141*0a6a1f1dSLionel Sambuc _rtld_call_initfini_function(const Obj_Entry *obj, Elf_Addr func, sigset_t *mask)
142f14fb602SLionel Sambuc {
143f14fb602SLionel Sambuc 	_rtld_exclusive_exit(mask);
144*0a6a1f1dSLionel Sambuc 	_rtld_call_function_void(obj, func);
145f14fb602SLionel Sambuc 	_rtld_exclusive_enter(mask);
146f14fb602SLionel Sambuc }
147f14fb602SLionel Sambuc 
148e83f7ba2SBen Gras static void
_rtld_call_fini_function(Obj_Entry * obj,sigset_t * mask,u_int cur_objgen)149f14fb602SLionel Sambuc _rtld_call_fini_function(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
150f14fb602SLionel Sambuc {
151*0a6a1f1dSLionel Sambuc 	if (obj->fini_arraysz == 0 && (obj->fini == 0 || obj->fini_called))
152f14fb602SLionel Sambuc 		return;
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc 	if (obj->fini != 0 && !obj->fini_called) {
155f14fb602SLionel Sambuc 		dbg (("calling fini function %s at %p%s", obj->path,
156f14fb602SLionel Sambuc 		    (void *)obj->fini,
157f14fb602SLionel Sambuc 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
158f14fb602SLionel Sambuc 		obj->fini_called = 1;
159*0a6a1f1dSLionel Sambuc 		_rtld_call_initfini_function(obj, obj->fini, mask);
160f14fb602SLionel Sambuc 	}
161f14fb602SLionel Sambuc #ifdef HAVE_INITFINI_ARRAY
162f14fb602SLionel Sambuc 	/*
163f14fb602SLionel Sambuc 	 * Now process the fini_array if it exists.  Simply go from
164f14fb602SLionel Sambuc 	 * start to end.  We need to make restartable so just advance
165f14fb602SLionel Sambuc 	 * the array pointer and decrement the size each time through
166f14fb602SLionel Sambuc 	 * the loop.
167f14fb602SLionel Sambuc 	 */
168f14fb602SLionel Sambuc 	while (obj->fini_arraysz > 0 && _rtld_objgen == cur_objgen) {
169*0a6a1f1dSLionel Sambuc 		Elf_Addr fini = *obj->fini_array++;
170f14fb602SLionel Sambuc 		obj->fini_arraysz--;
171f14fb602SLionel Sambuc 		dbg (("calling fini array function %s at %p%s", obj->path,
172f14fb602SLionel Sambuc 		    (void *)fini,
173f14fb602SLionel Sambuc 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
174*0a6a1f1dSLionel Sambuc 		_rtld_call_initfini_function(obj, fini, mask);
175f14fb602SLionel Sambuc 	}
176f14fb602SLionel Sambuc #endif /* HAVE_INITFINI_ARRAY */
177f14fb602SLionel Sambuc }
178f14fb602SLionel Sambuc 
179f14fb602SLionel Sambuc static void
_rtld_call_fini_functions(sigset_t * mask,int force)180f14fb602SLionel Sambuc _rtld_call_fini_functions(sigset_t *mask, int force)
181e83f7ba2SBen Gras {
182e83f7ba2SBen Gras 	Objlist_Entry *elm;
183e83f7ba2SBen Gras 	Objlist finilist;
184f14fb602SLionel Sambuc 	u_int cur_objgen;
185e83f7ba2SBen Gras 
186e83f7ba2SBen Gras 	dbg(("_rtld_call_fini_functions(%d)", force));
187e83f7ba2SBen Gras 
188f14fb602SLionel Sambuc restart:
189f14fb602SLionel Sambuc 	cur_objgen = ++_rtld_objgen;
190e83f7ba2SBen Gras 	SIMPLEQ_INIT(&finilist);
191e83f7ba2SBen Gras 	_rtld_initlist_tsort(&finilist, 1);
192e83f7ba2SBen Gras 
193e83f7ba2SBen Gras 	/* First pass: objects _not_ marked with DF_1_INITFIRST. */
194e83f7ba2SBen Gras 	SIMPLEQ_FOREACH(elm, &finilist, link) {
195f14fb602SLionel Sambuc 		Obj_Entry * const obj = elm->obj;
196f14fb602SLionel Sambuc 		if (!obj->z_initfirst) {
197e83f7ba2SBen Gras 			if (obj->refcount > 0 && !force) {
198e83f7ba2SBen Gras 				continue;
199e83f7ba2SBen Gras 			}
200f14fb602SLionel Sambuc 			/*
201f14fb602SLionel Sambuc 			 * XXX This can race against a concurrent dlclose().
202f14fb602SLionel Sambuc 			 * XXX In that case, the object could be unmapped before
203f14fb602SLionel Sambuc 			 * XXX the fini() call or the fini_array has completed.
204f14fb602SLionel Sambuc 			 */
205f14fb602SLionel Sambuc 			_rtld_call_fini_function(obj, mask, cur_objgen);
206f14fb602SLionel Sambuc 			if (_rtld_objgen != cur_objgen) {
207f14fb602SLionel Sambuc 				dbg(("restarting fini iteration"));
208f14fb602SLionel Sambuc 				_rtld_objlist_clear(&finilist);
209f14fb602SLionel Sambuc 				goto restart;
210e83f7ba2SBen Gras 		}
211f14fb602SLionel Sambuc 		}
212e83f7ba2SBen Gras 	}
213e83f7ba2SBen Gras 
214e83f7ba2SBen Gras 	/* Second pass: objects marked with DF_1_INITFIRST. */
215e83f7ba2SBen Gras 	SIMPLEQ_FOREACH(elm, &finilist, link) {
216f14fb602SLionel Sambuc 		Obj_Entry * const obj = elm->obj;
217e83f7ba2SBen Gras 		if (obj->refcount > 0 && !force) {
218e83f7ba2SBen Gras 			continue;
219e83f7ba2SBen Gras 		}
220f14fb602SLionel Sambuc 		/* XXX See above for the race condition here */
221f14fb602SLionel Sambuc 		_rtld_call_fini_function(obj, mask, cur_objgen);
222f14fb602SLionel Sambuc 		if (_rtld_objgen != cur_objgen) {
223f14fb602SLionel Sambuc 			dbg(("restarting fini iteration"));
224f14fb602SLionel Sambuc 			_rtld_objlist_clear(&finilist);
225f14fb602SLionel Sambuc 			goto restart;
226e83f7ba2SBen Gras 		}
227e83f7ba2SBen Gras 	}
228e83f7ba2SBen Gras 
229e83f7ba2SBen Gras         _rtld_objlist_clear(&finilist);
230e83f7ba2SBen Gras }
231e83f7ba2SBen Gras 
232e83f7ba2SBen Gras static void
_rtld_call_init_function(Obj_Entry * obj,sigset_t * mask,u_int cur_objgen)233f14fb602SLionel Sambuc _rtld_call_init_function(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
234f14fb602SLionel Sambuc {
235*0a6a1f1dSLionel Sambuc 	if (obj->init_arraysz == 0 && (obj->init_called || obj->init == 0))
236f14fb602SLionel Sambuc 		return;
237*0a6a1f1dSLionel Sambuc 
238*0a6a1f1dSLionel Sambuc 	if (!obj->init_called && obj->init != 0) {
239f14fb602SLionel Sambuc 		dbg (("calling init function %s at %p%s",
240f14fb602SLionel Sambuc 		    obj->path, (void *)obj->init,
241f14fb602SLionel Sambuc 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
242f14fb602SLionel Sambuc 		obj->init_called = 1;
243*0a6a1f1dSLionel Sambuc 		_rtld_call_initfini_function(obj, obj->init, mask);
244f14fb602SLionel Sambuc 	}
245f14fb602SLionel Sambuc 
246f14fb602SLionel Sambuc #ifdef HAVE_INITFINI_ARRAY
247f14fb602SLionel Sambuc 	/*
248f14fb602SLionel Sambuc 	 * Now process the init_array if it exists.  Simply go from
249f14fb602SLionel Sambuc 	 * start to end.  We need to make restartable so just advance
250f14fb602SLionel Sambuc 	 * the array pointer and decrement the size each time through
251f14fb602SLionel Sambuc 	 * the loop.
252f14fb602SLionel Sambuc 	 */
253f14fb602SLionel Sambuc 	while (obj->init_arraysz > 0 && _rtld_objgen == cur_objgen) {
254*0a6a1f1dSLionel Sambuc 		Elf_Addr init = *obj->init_array++;
255f14fb602SLionel Sambuc 		obj->init_arraysz--;
256f14fb602SLionel Sambuc 		dbg (("calling init_array function %s at %p%s",
257f14fb602SLionel Sambuc 		    obj->path, (void *)init,
258f14fb602SLionel Sambuc 		    obj->z_initfirst ? " (DF_1_INITFIRST)" : ""));
259*0a6a1f1dSLionel Sambuc 		_rtld_call_initfini_function(obj, init, mask);
260f14fb602SLionel Sambuc 	}
261f14fb602SLionel Sambuc #endif /* HAVE_INITFINI_ARRAY */
262f14fb602SLionel Sambuc }
263f14fb602SLionel Sambuc 
264f14fb602SLionel Sambuc static void
_rtld_call_init_functions(sigset_t * mask)265f14fb602SLionel Sambuc _rtld_call_init_functions(sigset_t *mask)
266e83f7ba2SBen Gras {
267e83f7ba2SBen Gras 	Objlist_Entry *elm;
268e83f7ba2SBen Gras 	Objlist initlist;
269f14fb602SLionel Sambuc 	u_int cur_objgen;
270e83f7ba2SBen Gras 
271e83f7ba2SBen Gras 	dbg(("_rtld_call_init_functions()"));
272f14fb602SLionel Sambuc 
273f14fb602SLionel Sambuc restart:
274f14fb602SLionel Sambuc 	cur_objgen = ++_rtld_objgen;
275e83f7ba2SBen Gras 	SIMPLEQ_INIT(&initlist);
276e83f7ba2SBen Gras 	_rtld_initlist_tsort(&initlist, 0);
277e83f7ba2SBen Gras 
278e83f7ba2SBen Gras 	/* First pass: objects marked with DF_1_INITFIRST. */
279e83f7ba2SBen Gras 	SIMPLEQ_FOREACH(elm, &initlist, link) {
280f14fb602SLionel Sambuc 		Obj_Entry * const obj = elm->obj;
281f14fb602SLionel Sambuc 		if (obj->z_initfirst) {
282f14fb602SLionel Sambuc 			_rtld_call_init_function(obj, mask, cur_objgen);
283f14fb602SLionel Sambuc 			if (_rtld_objgen != cur_objgen) {
284f14fb602SLionel Sambuc 				dbg(("restarting init iteration"));
285f14fb602SLionel Sambuc 				_rtld_objlist_clear(&initlist);
286f14fb602SLionel Sambuc 				goto restart;
287e83f7ba2SBen Gras 			}
288f14fb602SLionel Sambuc 		}
289e83f7ba2SBen Gras 	}
290e83f7ba2SBen Gras 
291e83f7ba2SBen Gras 	/* Second pass: all other objects. */
292e83f7ba2SBen Gras 	SIMPLEQ_FOREACH(elm, &initlist, link) {
293f14fb602SLionel Sambuc 		_rtld_call_init_function(elm->obj, mask, cur_objgen);
294f14fb602SLionel Sambuc 		if (_rtld_objgen != cur_objgen) {
295f14fb602SLionel Sambuc 			dbg(("restarting init iteration"));
296f14fb602SLionel Sambuc 			_rtld_objlist_clear(&initlist);
297f14fb602SLionel Sambuc 			goto restart;
298e83f7ba2SBen Gras 		}
299e83f7ba2SBen Gras 	}
300e83f7ba2SBen Gras 
301e83f7ba2SBen Gras         _rtld_objlist_clear(&initlist);
302e83f7ba2SBen Gras }
303e83f7ba2SBen Gras 
304e83f7ba2SBen Gras /*
305e83f7ba2SBen Gras  * Initialize the dynamic linker.  The argument is the address at which
306e83f7ba2SBen Gras  * the dynamic linker has been mapped into memory.  The primary task of
307e83f7ba2SBen Gras  * this function is to create an Obj_Entry for the dynamic linker and
308e83f7ba2SBen Gras  * to resolve the PLT relocation for platforms that need it (those that
309e83f7ba2SBen Gras  * define __HAVE_FUNCTION_DESCRIPTORS
310e83f7ba2SBen Gras  */
311e83f7ba2SBen Gras static void
_rtld_init(caddr_t mapbase,caddr_t relocbase,const char * execname)312e83f7ba2SBen Gras _rtld_init(caddr_t mapbase, caddr_t relocbase, const char *execname)
313e83f7ba2SBen Gras {
314e83f7ba2SBen Gras 
315e83f7ba2SBen Gras 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
316e83f7ba2SBen Gras 	_rtld_objself.path = __UNCONST(_rtld_path);
317e83f7ba2SBen Gras 	_rtld_objself.pathlen = sizeof(_rtld_path)-1;
318e83f7ba2SBen Gras 	_rtld_objself.rtld = true;
319e83f7ba2SBen Gras 	_rtld_objself.mapbase = mapbase;
320e83f7ba2SBen Gras 	_rtld_objself.relocbase = relocbase;
321e83f7ba2SBen Gras 	_rtld_objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
322e83f7ba2SBen Gras 	_rtld_objself.strtab = "_rtld_sym_zero";
323e83f7ba2SBen Gras 
324e83f7ba2SBen Gras 	/*
325e83f7ba2SBen Gras 	 * Set value to -relocbase so that
326e83f7ba2SBen Gras 	 *
327e83f7ba2SBen Gras 	 *     _rtld_objself.relocbase + _rtld_sym_zero.st_value == 0
328e83f7ba2SBen Gras 	 *
329e83f7ba2SBen Gras 	 * This allows unresolved references to weak symbols to be computed
330e83f7ba2SBen Gras 	 * to a value of 0.
331e83f7ba2SBen Gras 	 */
332e83f7ba2SBen Gras 	_rtld_sym_zero.st_value = -(uintptr_t)relocbase;
333e83f7ba2SBen Gras 
334e83f7ba2SBen Gras 	_rtld_digest_dynamic(_rtld_path, &_rtld_objself);
335e83f7ba2SBen Gras 	assert(!_rtld_objself.needed);
336e83f7ba2SBen Gras #if !defined(__hppa__)
337e83f7ba2SBen Gras 	assert(!_rtld_objself.pltrel && !_rtld_objself.pltrela);
338e83f7ba2SBen Gras #else
339e83f7ba2SBen Gras 	_rtld_relocate_plt_objects(&_rtld_objself);
340e83f7ba2SBen Gras #endif
34184d9c625SLionel Sambuc #if !defined(__mips__) && !defined(__hppa__)
342e83f7ba2SBen Gras 	assert(!_rtld_objself.pltgot);
343e83f7ba2SBen Gras #endif
344e83f7ba2SBen Gras #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
345e83f7ba2SBen Gras 	/* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
346e83f7ba2SBen Gras 	assert(!_rtld_objself.textrel);
347e83f7ba2SBen Gras #endif
348e83f7ba2SBen Gras 
349e83f7ba2SBen Gras 	_rtld_add_paths(execname, &_rtld_default_paths,
350e83f7ba2SBen Gras 	    RTLD_DEFAULT_LIBRARY_PATH);
351e83f7ba2SBen Gras 
352e83f7ba2SBen Gras #ifdef RTLD_ARCH_SUBDIR
353e83f7ba2SBen Gras 	_rtld_add_paths(execname, &_rtld_default_paths,
354e83f7ba2SBen Gras 	    RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
355e83f7ba2SBen Gras #endif
356e83f7ba2SBen Gras 
35784d9c625SLionel Sambuc 	/* Make the object list empty. */
358e83f7ba2SBen Gras 	_rtld_objlist = NULL;
359e83f7ba2SBen Gras 	_rtld_objtail = &_rtld_objlist;
360e83f7ba2SBen Gras 	_rtld_objcount = 0;
361e83f7ba2SBen Gras 
362e83f7ba2SBen Gras 	_rtld_debug.r_brk = _rtld_debug_state;
363e83f7ba2SBen Gras 	_rtld_debug.r_state = RT_CONSISTENT;
364e83f7ba2SBen Gras }
365e83f7ba2SBen Gras 
366e83f7ba2SBen Gras /*
367e83f7ba2SBen Gras  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
368e83f7ba2SBen Gras  * before the process exits.
369e83f7ba2SBen Gras  */
370e83f7ba2SBen Gras static void
_rtld_exit(void)371e83f7ba2SBen Gras _rtld_exit(void)
372e83f7ba2SBen Gras {
373f14fb602SLionel Sambuc 	sigset_t mask;
374f14fb602SLionel Sambuc 
375e83f7ba2SBen Gras 	dbg(("rtld_exit()"));
376e83f7ba2SBen Gras 
377f14fb602SLionel Sambuc 	_rtld_exclusive_enter(&mask);
378f14fb602SLionel Sambuc 
379f14fb602SLionel Sambuc 	_rtld_call_fini_functions(&mask, 1);
380f14fb602SLionel Sambuc 
381f14fb602SLionel Sambuc 	_rtld_exclusive_exit(&mask);
382f14fb602SLionel Sambuc }
383f14fb602SLionel Sambuc 
384f14fb602SLionel Sambuc __dso_public void *
_dlauxinfo(void)385f14fb602SLionel Sambuc _dlauxinfo(void)
386f14fb602SLionel Sambuc {
387f14fb602SLionel Sambuc 	return auxinfo;
388e83f7ba2SBen Gras }
389e83f7ba2SBen Gras 
390e83f7ba2SBen Gras /*
391e83f7ba2SBen Gras  * Main entry point for dynamic linking.  The argument is the stack
392e83f7ba2SBen Gras  * pointer.  The stack is expected to be laid out as described in the
393e83f7ba2SBen Gras  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
394e83f7ba2SBen Gras  * the stack pointer points to a word containing ARGC.  Following that
395e83f7ba2SBen Gras  * in the stack is a null-terminated sequence of pointers to argument
396e83f7ba2SBen Gras  * strings.  Then comes a null-terminated sequence of pointers to
397e83f7ba2SBen Gras  * environment strings.  Finally, there is a sequence of "auxiliary
398e83f7ba2SBen Gras  * vector" entries.
399e83f7ba2SBen Gras  *
400e83f7ba2SBen Gras  * This function returns the entry point for the main program, the dynamic
401e83f7ba2SBen Gras  * linker's exit procedure in sp[0], and a pointer to the main object in
402e83f7ba2SBen Gras  * sp[1].
403e83f7ba2SBen Gras  */
404e83f7ba2SBen Gras Elf_Addr
_rtld(Elf_Addr * sp,Elf_Addr relocbase)405e83f7ba2SBen Gras _rtld(Elf_Addr *sp, Elf_Addr relocbase)
406e83f7ba2SBen Gras {
407e83f7ba2SBen Gras 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
408e83f7ba2SBen Gras 	               *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
409e83f7ba2SBen Gras 		       *pAUX_ruid, *pAUX_rgid;
410e83f7ba2SBen Gras 	const AuxInfo  *pAUX_pagesz;
411e83f7ba2SBen Gras 	char          **env, **oenvp;
412e83f7ba2SBen Gras 	const AuxInfo  *auxp;
413f14fb602SLionel Sambuc 	Obj_Entry      *obj;
414e83f7ba2SBen Gras 	Elf_Addr       *const osp = sp;
415e83f7ba2SBen Gras 	bool            bind_now = 0;
416e83f7ba2SBen Gras 	const char     *ld_bind_now, *ld_preload, *ld_library_path;
417e83f7ba2SBen Gras 	const char    **argv;
418e83f7ba2SBen Gras 	const char     *execname;
419e83f7ba2SBen Gras 	long		argc;
420e83f7ba2SBen Gras 	const char **real___progname;
421e83f7ba2SBen Gras 	const Obj_Entry **real___mainprog_obj;
422e83f7ba2SBen Gras 	char ***real_environ;
423f14fb602SLionel Sambuc 	sigset_t        mask;
424e83f7ba2SBen Gras #ifdef DEBUG
425e83f7ba2SBen Gras 	const char     *ld_debug;
426e83f7ba2SBen Gras #endif
427f14fb602SLionel Sambuc #ifdef RTLD_DEBUG
428f14fb602SLionel Sambuc 	int i = 0;
429f14fb602SLionel Sambuc #endif
430e83f7ba2SBen Gras 
431e83f7ba2SBen Gras 	/*
432e83f7ba2SBen Gras          * On entry, the dynamic linker itself has not been relocated yet.
433e83f7ba2SBen Gras          * Be very careful not to reference any global data until after
434e83f7ba2SBen Gras          * _rtld_init has returned.  It is OK to reference file-scope statics
435e83f7ba2SBen Gras          * and string constants, and to call static and global functions.
436e83f7ba2SBen Gras          */
437e83f7ba2SBen Gras 	/* Find the auxiliary vector on the stack. */
438e83f7ba2SBen Gras 	/* first Elf_Word reserved to address of exit routine */
439e83f7ba2SBen Gras #if defined(RTLD_DEBUG)
440e83f7ba2SBen Gras 	debug = 1;
441e83f7ba2SBen Gras 	dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp,
442e83f7ba2SBen Gras 	    (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
44384d9c625SLionel Sambuc #ifndef __x86_64__
444e83f7ba2SBen Gras 	dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_,
445e83f7ba2SBen Gras 	    &_DYNAMIC));
446f14fb602SLionel Sambuc #endif
447e83f7ba2SBen Gras #endif
448e83f7ba2SBen Gras 
449e83f7ba2SBen Gras 	sp += 2;		/* skip over return argument space */
450e83f7ba2SBen Gras 	argv = (const char **) &sp[1];
451e83f7ba2SBen Gras 	argc = *(long *)sp;
452e83f7ba2SBen Gras 	sp += 2 + argc;		/* Skip over argc, arguments, and NULL
453e83f7ba2SBen Gras 				 * terminator */
454e83f7ba2SBen Gras 	env = (char **) sp;
455e83f7ba2SBen Gras 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
456e83f7ba2SBen Gras #if defined(RTLD_DEBUG)
457e83f7ba2SBen Gras 		dbg(("env[%d] = %p %s", i++, (void *)sp[-1], (char *)sp[-1]));
458e83f7ba2SBen Gras #endif
459e83f7ba2SBen Gras 	}
460f14fb602SLionel Sambuc 	auxinfo = (AuxInfo *) sp;
461e83f7ba2SBen Gras 
462e83f7ba2SBen Gras 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
463e83f7ba2SBen Gras 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
464e83f7ba2SBen Gras 	pAUX_euid = pAUX_ruid = pAUX_egid = pAUX_rgid = NULL;
465e83f7ba2SBen Gras 	pAUX_pagesz = NULL;
466e83f7ba2SBen Gras 
467e83f7ba2SBen Gras 	execname = NULL;
468e83f7ba2SBen Gras 
469e83f7ba2SBen Gras 	/* Digest the auxiliary vector. */
470f14fb602SLionel Sambuc 	for (auxp = auxinfo; auxp->a_type != AT_NULL; ++auxp) {
471e83f7ba2SBen Gras 		switch (auxp->a_type) {
472e83f7ba2SBen Gras 		case AT_BASE:
473e83f7ba2SBen Gras 			pAUX_base = auxp;
474e83f7ba2SBen Gras 			break;
475e83f7ba2SBen Gras 		case AT_ENTRY:
476e83f7ba2SBen Gras 			pAUX_entry = auxp;
477e83f7ba2SBen Gras 			break;
478e83f7ba2SBen Gras 		case AT_EXECFD:
479e83f7ba2SBen Gras 			pAUX_execfd = auxp;
480e83f7ba2SBen Gras 			break;
481e83f7ba2SBen Gras 		case AT_PHDR:
482e83f7ba2SBen Gras 			pAUX_phdr = auxp;
483e83f7ba2SBen Gras 			break;
484e83f7ba2SBen Gras 		case AT_PHENT:
485e83f7ba2SBen Gras 			pAUX_phent = auxp;
486e83f7ba2SBen Gras 			break;
487e83f7ba2SBen Gras 		case AT_PHNUM:
488e83f7ba2SBen Gras 			pAUX_phnum = auxp;
489e83f7ba2SBen Gras 			break;
490e83f7ba2SBen Gras #ifdef AT_EUID
491e83f7ba2SBen Gras 		case AT_EUID:
492e83f7ba2SBen Gras 			pAUX_euid = auxp;
493e83f7ba2SBen Gras 			break;
494e83f7ba2SBen Gras 		case AT_RUID:
495e83f7ba2SBen Gras 			pAUX_ruid = auxp;
496e83f7ba2SBen Gras 			break;
497e83f7ba2SBen Gras 		case AT_EGID:
498e83f7ba2SBen Gras 			pAUX_egid = auxp;
499e83f7ba2SBen Gras 			break;
500e83f7ba2SBen Gras 		case AT_RGID:
501e83f7ba2SBen Gras 			pAUX_rgid = auxp;
502e83f7ba2SBen Gras 			break;
503e83f7ba2SBen Gras #endif
504e83f7ba2SBen Gras #ifdef AT_SUN_EXECNAME
505e83f7ba2SBen Gras 		case AT_SUN_EXECNAME:
506e83f7ba2SBen Gras 			execname = (const char *)(const void *)auxp->a_v;
507e83f7ba2SBen Gras 			break;
508e83f7ba2SBen Gras #endif
509e83f7ba2SBen Gras 		case AT_PAGESZ:
510e83f7ba2SBen Gras 			pAUX_pagesz = auxp;
511e83f7ba2SBen Gras 			break;
512e83f7ba2SBen Gras 		}
513e83f7ba2SBen Gras 	}
514e83f7ba2SBen Gras 
515e83f7ba2SBen Gras 	/* Initialize and relocate ourselves. */
516e83f7ba2SBen Gras 	if (pAUX_base == NULL) {
517e83f7ba2SBen Gras 		_rtld_error("Bad pAUX_base");
518e83f7ba2SBen Gras 		_rtld_die();
519e83f7ba2SBen Gras 	}
520e83f7ba2SBen Gras 	assert(pAUX_pagesz != NULL);
521e83f7ba2SBen Gras 	_rtld_pagesz = (int)pAUX_pagesz->a_v;
522e83f7ba2SBen Gras 	_rtld_init((caddr_t)pAUX_base->a_v, (caddr_t)relocbase, execname);
523e83f7ba2SBen Gras 
524e83f7ba2SBen Gras 	__progname = _rtld_objself.path;
525e83f7ba2SBen Gras 	environ = env;
526e83f7ba2SBen Gras 
527e83f7ba2SBen Gras 	_rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
528e83f7ba2SBen Gras 	    (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
529e83f7ba2SBen Gras 	    ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
530e83f7ba2SBen Gras 	    (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
531e83f7ba2SBen Gras 
532e83f7ba2SBen Gras #ifdef DEBUG
533e83f7ba2SBen Gras 	ld_debug = NULL;
534e83f7ba2SBen Gras #endif
535e83f7ba2SBen Gras 	ld_bind_now = NULL;
536e83f7ba2SBen Gras 	ld_library_path = NULL;
537e83f7ba2SBen Gras 	ld_preload = NULL;
538e83f7ba2SBen Gras 	/*
539e83f7ba2SBen Gras 	 * Inline avoid using normal getenv/unsetenv here as the libc
540e83f7ba2SBen Gras 	 * code is quite a bit more complicated.
541e83f7ba2SBen Gras 	 */
542e83f7ba2SBen Gras 	for (oenvp = env; *env != NULL; ++env) {
543e83f7ba2SBen Gras 		static const char bind_var[] = "LD_BIND_NOW=";
544e83f7ba2SBen Gras 		static const char debug_var[] =  "LD_DEBUG=";
545e83f7ba2SBen Gras 		static const char path_var[] = "LD_LIBRARY_PATH=";
546e83f7ba2SBen Gras 		static const char preload_var[] = "LD_PRELOAD=";
547e83f7ba2SBen Gras #define LEN(x)	(sizeof(x) - 1)
548e83f7ba2SBen Gras 
549e83f7ba2SBen Gras 		if ((*env)[0] != 'L' || (*env)[1] != 'D') {
550e83f7ba2SBen Gras 			/*
551e83f7ba2SBen Gras 			 * Special case to skip most entries without
552e83f7ba2SBen Gras 			 * the more expensive calls to strncmp.
553e83f7ba2SBen Gras 			 */
554e83f7ba2SBen Gras 			*oenvp++ = *env;
555e83f7ba2SBen Gras 		} else if (strncmp(*env, debug_var, LEN(debug_var)) == 0) {
556e83f7ba2SBen Gras 			if (_rtld_trust) {
557e83f7ba2SBen Gras #ifdef DEBUG
558e83f7ba2SBen Gras 				ld_debug = *env + LEN(debug_var);
559e83f7ba2SBen Gras #endif
560e83f7ba2SBen Gras 				*oenvp++ = *env;
561e83f7ba2SBen Gras 			}
562e83f7ba2SBen Gras 		} else if (strncmp(*env, bind_var, LEN(bind_var)) == 0) {
563*0a6a1f1dSLionel Sambuc 			if (_rtld_trust) {
564e83f7ba2SBen Gras 				ld_bind_now = *env + LEN(bind_var);
565*0a6a1f1dSLionel Sambuc 				*oenvp++ = *env;
566*0a6a1f1dSLionel Sambuc 			}
567e83f7ba2SBen Gras 		} else if (strncmp(*env, path_var, LEN(path_var)) == 0) {
568e83f7ba2SBen Gras 			if (_rtld_trust) {
569e83f7ba2SBen Gras 				ld_library_path = *env + LEN(path_var);
570e83f7ba2SBen Gras 				*oenvp++ = *env;
571e83f7ba2SBen Gras 			}
572e83f7ba2SBen Gras 		} else if (strncmp(*env, preload_var, LEN(preload_var)) == 0) {
573e83f7ba2SBen Gras 			if (_rtld_trust) {
574e83f7ba2SBen Gras 				ld_preload = *env + LEN(preload_var);
575e83f7ba2SBen Gras 				*oenvp++ = *env;
576e83f7ba2SBen Gras 			}
577e83f7ba2SBen Gras 		} else {
578e83f7ba2SBen Gras 			*oenvp++ = *env;
579e83f7ba2SBen Gras 		}
580e83f7ba2SBen Gras #undef LEN
581e83f7ba2SBen Gras 	}
582e83f7ba2SBen Gras 	*oenvp++ = NULL;
583e83f7ba2SBen Gras 
584e83f7ba2SBen Gras 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
585e83f7ba2SBen Gras 		bind_now = true;
586e83f7ba2SBen Gras 	if (_rtld_trust) {
587e83f7ba2SBen Gras #ifdef DEBUG
588e83f7ba2SBen Gras #ifdef RTLD_DEBUG
589e83f7ba2SBen Gras 		debug = 0;
590e83f7ba2SBen Gras #endif
591e83f7ba2SBen Gras 		if (ld_debug != NULL && *ld_debug != '\0')
592e83f7ba2SBen Gras 			debug = 1;
593e83f7ba2SBen Gras #endif
594e83f7ba2SBen Gras 		_rtld_add_paths(execname, &_rtld_paths, ld_library_path);
595e83f7ba2SBen Gras 	} else {
596e83f7ba2SBen Gras 		execname = NULL;
597e83f7ba2SBen Gras 	}
598e83f7ba2SBen Gras 	_rtld_process_hints(execname, &_rtld_paths, &_rtld_xforms,
599e83f7ba2SBen Gras 	    _PATH_LD_HINTS);
600e83f7ba2SBen Gras 	dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
601e83f7ba2SBen Gras 	     _rtld_objself.mapbase, _rtld_objself.relocbase));
602e83f7ba2SBen Gras 
603e83f7ba2SBen Gras 	/*
604e83f7ba2SBen Gras          * Load the main program, or process its program header if it is
605e83f7ba2SBen Gras          * already loaded.
606e83f7ba2SBen Gras          */
607e83f7ba2SBen Gras 	if (pAUX_execfd != NULL) {	/* Load the main program. */
608e83f7ba2SBen Gras 		int             fd = pAUX_execfd->a_v;
609e83f7ba2SBen Gras 		const char *obj_name = argv[0] ? argv[0] : "main program";
610e83f7ba2SBen Gras 		dbg(("loading main program"));
611e83f7ba2SBen Gras 		_rtld_objmain = _rtld_map_object(obj_name, fd, NULL);
612e83f7ba2SBen Gras 		close(fd);
613e83f7ba2SBen Gras 		if (_rtld_objmain == NULL)
614e83f7ba2SBen Gras 			_rtld_die();
615e83f7ba2SBen Gras 	} else {		/* Main program already loaded. */
616e83f7ba2SBen Gras 		const Elf_Phdr *phdr;
617e83f7ba2SBen Gras 		int             phnum;
618e83f7ba2SBen Gras 		caddr_t         entry;
619e83f7ba2SBen Gras 
620e83f7ba2SBen Gras 		dbg(("processing main program's program header"));
621e83f7ba2SBen Gras 		assert(pAUX_phdr != NULL);
622e83f7ba2SBen Gras 		phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
623e83f7ba2SBen Gras 		assert(pAUX_phnum != NULL);
624e83f7ba2SBen Gras 		phnum = pAUX_phnum->a_v;
625e83f7ba2SBen Gras 		assert(pAUX_phent != NULL);
626e83f7ba2SBen Gras 		assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
627e83f7ba2SBen Gras 		assert(pAUX_entry != NULL);
628e83f7ba2SBen Gras 		entry = (caddr_t) pAUX_entry->a_v;
629e83f7ba2SBen Gras 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
630e83f7ba2SBen Gras 		_rtld_objmain->path = xstrdup(argv[0] ? argv[0] :
631e83f7ba2SBen Gras 		    "main program");
632e83f7ba2SBen Gras 		_rtld_objmain->pathlen = strlen(_rtld_objmain->path);
633e83f7ba2SBen Gras 	}
634e83f7ba2SBen Gras 
635e83f7ba2SBen Gras 	_rtld_objmain->mainprog = true;
636e83f7ba2SBen Gras 
637e83f7ba2SBen Gras 	/*
638e83f7ba2SBen Gras 	 * Get the actual dynamic linker pathname from the executable if
639e83f7ba2SBen Gras 	 * possible.  (It should always be possible.)  That ensures that
640e83f7ba2SBen Gras 	 * gdb will find the right dynamic linker even if a non-standard
641e83f7ba2SBen Gras 	 * one is being used.
642e83f7ba2SBen Gras 	 */
643e83f7ba2SBen Gras 	if (_rtld_objmain->interp != NULL &&
64484d9c625SLionel Sambuc 	    strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0) {
645e83f7ba2SBen Gras 		_rtld_objself.path = xstrdup(_rtld_objmain->interp);
64684d9c625SLionel Sambuc 		_rtld_objself.pathlen = strlen(_rtld_objself.path);
64784d9c625SLionel Sambuc 	}
648e83f7ba2SBen Gras 	dbg(("actual dynamic linker is %s", _rtld_objself.path));
649e83f7ba2SBen Gras 
650e83f7ba2SBen Gras 	_rtld_digest_dynamic(execname, _rtld_objmain);
651e83f7ba2SBen Gras 
652e83f7ba2SBen Gras 	/* Link the main program into the list of objects. */
653e83f7ba2SBen Gras 	*_rtld_objtail = _rtld_objmain;
654e83f7ba2SBen Gras 	_rtld_objtail = &_rtld_objmain->next;
655e83f7ba2SBen Gras 	_rtld_objcount++;
656e83f7ba2SBen Gras 	_rtld_objloads++;
657e83f7ba2SBen Gras 
658e83f7ba2SBen Gras 	_rtld_linkmap_add(_rtld_objmain);
659e83f7ba2SBen Gras 	_rtld_linkmap_add(&_rtld_objself);
660e83f7ba2SBen Gras 
661e83f7ba2SBen Gras 	++_rtld_objmain->refcount;
662e83f7ba2SBen Gras 	_rtld_objmain->mainref = 1;
663e83f7ba2SBen Gras 	_rtld_objlist_push_tail(&_rtld_list_main, _rtld_objmain);
664e83f7ba2SBen Gras 
665e83f7ba2SBen Gras 	if (ld_preload) {
666e83f7ba2SBen Gras 		/*
667e83f7ba2SBen Gras 		 * Pre-load user-specified objects after the main program
668e83f7ba2SBen Gras 		 * but before any shared object dependencies.
669e83f7ba2SBen Gras 		 */
670e83f7ba2SBen Gras 		dbg(("preloading objects"));
671e83f7ba2SBen Gras 		if (_rtld_preload(ld_preload) == -1)
672e83f7ba2SBen Gras 			_rtld_die();
673e83f7ba2SBen Gras 	}
674e83f7ba2SBen Gras 
675e83f7ba2SBen Gras 	dbg(("loading needed objects"));
676e83f7ba2SBen Gras 	if (_rtld_load_needed_objects(_rtld_objmain, _RTLD_MAIN) == -1)
677e83f7ba2SBen Gras 		_rtld_die();
678e83f7ba2SBen Gras 
679f14fb602SLionel Sambuc 	dbg(("checking for required versions"));
680f14fb602SLionel Sambuc 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
681f14fb602SLionel Sambuc 		if (_rtld_verify_object_versions(obj) == -1)
682f14fb602SLionel Sambuc 			_rtld_die();
683f14fb602SLionel Sambuc 	}
684f14fb602SLionel Sambuc 
685f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
686f14fb602SLionel Sambuc 	dbg(("initializing initial Thread Local Storage offsets"));
687f14fb602SLionel Sambuc 	/*
688f14fb602SLionel Sambuc 	 * All initial objects get the TLS space from the static block.
689f14fb602SLionel Sambuc 	 */
690f14fb602SLionel Sambuc 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
691f14fb602SLionel Sambuc 		_rtld_tls_offset_allocate(obj);
692f14fb602SLionel Sambuc #endif
693f14fb602SLionel Sambuc 
694e83f7ba2SBen Gras 	dbg(("relocating objects"));
695e83f7ba2SBen Gras 	if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
696e83f7ba2SBen Gras 		_rtld_die();
697e83f7ba2SBen Gras 
698e83f7ba2SBen Gras 	dbg(("doing copy relocations"));
699e83f7ba2SBen Gras 	if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
700e83f7ba2SBen Gras 		_rtld_die();
701e83f7ba2SBen Gras 
702f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
703f14fb602SLionel Sambuc 	dbg(("initializing Thread Local Storage for main thread"));
704f14fb602SLionel Sambuc 	/*
705f14fb602SLionel Sambuc 	 * Set up TLS area for the main thread.
706f14fb602SLionel Sambuc 	 * This has to be done after all relocations are processed,
707f14fb602SLionel Sambuc 	 * since .tdata may contain relocations.
708f14fb602SLionel Sambuc 	 */
709f14fb602SLionel Sambuc 	_rtld_tls_initial_allocation();
710f14fb602SLionel Sambuc #endif
711f14fb602SLionel Sambuc 
712e83f7ba2SBen Gras 	/*
713e83f7ba2SBen Gras 	 * Set the __progname,  environ and, __mainprog_obj before
714e83f7ba2SBen Gras 	 * calling anything that might use them.
715e83f7ba2SBen Gras 	 */
716e83f7ba2SBen Gras 	real___progname = _rtld_objmain_sym("__progname");
717e83f7ba2SBen Gras 	if (real___progname) {
718e83f7ba2SBen Gras 		if (argv[0] != NULL) {
719e83f7ba2SBen Gras 			if ((*real___progname = strrchr(argv[0], '/')) == NULL)
720e83f7ba2SBen Gras 				(*real___progname) = argv[0];
721e83f7ba2SBen Gras 			else
722e83f7ba2SBen Gras 				(*real___progname)++;
723e83f7ba2SBen Gras 		} else {
724e83f7ba2SBen Gras 			(*real___progname) = NULL;
725e83f7ba2SBen Gras 		}
726e83f7ba2SBen Gras 	}
727e83f7ba2SBen Gras 	real_environ = _rtld_objmain_sym("environ");
728e83f7ba2SBen Gras 	if (real_environ)
729e83f7ba2SBen Gras 		*real_environ = environ;
730e83f7ba2SBen Gras 	/*
731e83f7ba2SBen Gras 	 * Set __mainprog_obj for old binaries.
732e83f7ba2SBen Gras 	 */
733e83f7ba2SBen Gras 	real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
734e83f7ba2SBen Gras 	if (real___mainprog_obj)
735e83f7ba2SBen Gras 		*real___mainprog_obj = _rtld_objmain;
736e83f7ba2SBen Gras 
737f14fb602SLionel Sambuc 	_rtld_exclusive_enter(&mask);
738f14fb602SLionel Sambuc 
739e83f7ba2SBen Gras 	dbg(("calling _init functions"));
740f14fb602SLionel Sambuc 	_rtld_call_init_functions(&mask);
741e83f7ba2SBen Gras 
742e83f7ba2SBen Gras 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
743e83f7ba2SBen Gras 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
744e83f7ba2SBen Gras 
745f14fb602SLionel Sambuc 	_rtld_exclusive_exit(&mask);
746f14fb602SLionel Sambuc 
747e83f7ba2SBen Gras 	/*
748e83f7ba2SBen Gras 	 * Return with the entry point and the exit procedure in at the top
749e83f7ba2SBen Gras 	 * of stack.
750e83f7ba2SBen Gras 	 */
751e83f7ba2SBen Gras 
752e83f7ba2SBen Gras 	_rtld_debug_state();	/* say hello to gdb! */
753e83f7ba2SBen Gras 
754e83f7ba2SBen Gras 	((void **) osp)[0] = _rtld_exit;
755e83f7ba2SBen Gras 	((void **) osp)[1] = _rtld_objmain;
756e83f7ba2SBen Gras 	return (Elf_Addr) _rtld_objmain->entry;
757e83f7ba2SBen Gras }
758e83f7ba2SBen Gras 
759e83f7ba2SBen Gras void
_rtld_die(void)760e83f7ba2SBen Gras _rtld_die(void)
761e83f7ba2SBen Gras {
762e83f7ba2SBen Gras 	const char *msg = dlerror();
763e83f7ba2SBen Gras 
764e83f7ba2SBen Gras 	if (msg == NULL)
765e83f7ba2SBen Gras 		msg = "Fatal error";
766e83f7ba2SBen Gras 	xerrx(1, "%s", msg);
767e83f7ba2SBen Gras }
768e83f7ba2SBen Gras 
769e83f7ba2SBen Gras static Obj_Entry *
_rtld_dlcheck(void * handle)770e83f7ba2SBen Gras _rtld_dlcheck(void *handle)
771e83f7ba2SBen Gras {
772e83f7ba2SBen Gras 	Obj_Entry *obj;
773e83f7ba2SBen Gras 
774e83f7ba2SBen Gras 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
775e83f7ba2SBen Gras 		if (obj == (Obj_Entry *) handle)
776e83f7ba2SBen Gras 			break;
777e83f7ba2SBen Gras 
778e83f7ba2SBen Gras 	if (obj == NULL || obj->dl_refcount == 0) {
779f14fb602SLionel Sambuc 		_rtld_error("Invalid shared object handle %p", handle);
780e83f7ba2SBen Gras 		return NULL;
781e83f7ba2SBen Gras 	}
782e83f7ba2SBen Gras 	return obj;
783e83f7ba2SBen Gras }
784e83f7ba2SBen Gras 
785e83f7ba2SBen Gras static void
_rtld_initlist_visit(Objlist * list,Obj_Entry * obj,int rev)786e83f7ba2SBen Gras _rtld_initlist_visit(Objlist* list, Obj_Entry *obj, int rev)
787e83f7ba2SBen Gras {
788e83f7ba2SBen Gras 	Needed_Entry* elm;
789e83f7ba2SBen Gras 
790e83f7ba2SBen Gras 	/* dbg(("_rtld_initlist_visit(%s)", obj->path)); */
791e83f7ba2SBen Gras 
792e83f7ba2SBen Gras 	if (obj->init_done)
793e83f7ba2SBen Gras 		return;
794e83f7ba2SBen Gras 	obj->init_done = 1;
795e83f7ba2SBen Gras 
796e83f7ba2SBen Gras 	for (elm = obj->needed; elm != NULL; elm = elm->next) {
797e83f7ba2SBen Gras 		if (elm->obj != NULL) {
798e83f7ba2SBen Gras 			_rtld_initlist_visit(list, elm->obj, rev);
799e83f7ba2SBen Gras 		}
800e83f7ba2SBen Gras 	}
801e83f7ba2SBen Gras 
802e83f7ba2SBen Gras 	if (rev) {
803e83f7ba2SBen Gras 		_rtld_objlist_push_head(list, obj);
804e83f7ba2SBen Gras 	} else {
805e83f7ba2SBen Gras 		_rtld_objlist_push_tail(list, obj);
806e83f7ba2SBen Gras 	}
807e83f7ba2SBen Gras }
808e83f7ba2SBen Gras 
809e83f7ba2SBen Gras static void
_rtld_initlist_tsort(Objlist * list,int rev)810e83f7ba2SBen Gras _rtld_initlist_tsort(Objlist* list, int rev)
811e83f7ba2SBen Gras {
812e83f7ba2SBen Gras 	dbg(("_rtld_initlist_tsort"));
813e83f7ba2SBen Gras 
814e83f7ba2SBen Gras 	Obj_Entry* obj;
815e83f7ba2SBen Gras 
816e83f7ba2SBen Gras 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
817e83f7ba2SBen Gras 		obj->init_done = 0;
818e83f7ba2SBen Gras 	}
819e83f7ba2SBen Gras 
820e83f7ba2SBen Gras 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
821e83f7ba2SBen Gras 		_rtld_initlist_visit(list, obj, rev);
822e83f7ba2SBen Gras 	}
823e83f7ba2SBen Gras }
824e83f7ba2SBen Gras 
825e83f7ba2SBen Gras static void
_rtld_init_dag(Obj_Entry * root)826e83f7ba2SBen Gras _rtld_init_dag(Obj_Entry *root)
827e83f7ba2SBen Gras {
828e83f7ba2SBen Gras 
829e83f7ba2SBen Gras 	_rtld_init_dag1(root, root);
830e83f7ba2SBen Gras }
831e83f7ba2SBen Gras 
832e83f7ba2SBen Gras static void
_rtld_init_dag1(Obj_Entry * root,Obj_Entry * obj)833e83f7ba2SBen Gras _rtld_init_dag1(Obj_Entry *root, Obj_Entry *obj)
834e83f7ba2SBen Gras {
835e83f7ba2SBen Gras 	const Needed_Entry *needed;
836e83f7ba2SBen Gras 
837e83f7ba2SBen Gras 	if (!obj->mainref) {
838e83f7ba2SBen Gras 		if (_rtld_objlist_find(&obj->dldags, root))
839e83f7ba2SBen Gras 			return;
840e83f7ba2SBen Gras 		dbg(("add %p (%s) to %p (%s) DAG", obj, obj->path, root,
841e83f7ba2SBen Gras 		    root->path));
842e83f7ba2SBen Gras 		_rtld_objlist_push_tail(&obj->dldags, root);
843e83f7ba2SBen Gras 		_rtld_objlist_push_tail(&root->dagmembers, obj);
844e83f7ba2SBen Gras 	}
845e83f7ba2SBen Gras 	for (needed = obj->needed; needed != NULL; needed = needed->next)
846e83f7ba2SBen Gras 		if (needed->obj != NULL)
847e83f7ba2SBen Gras 			_rtld_init_dag1(root, needed->obj);
848e83f7ba2SBen Gras }
849e83f7ba2SBen Gras 
850e83f7ba2SBen Gras /*
851e83f7ba2SBen Gras  * Note, this is called only for objects loaded by dlopen().
852e83f7ba2SBen Gras  */
853e83f7ba2SBen Gras static void
_rtld_unload_object(sigset_t * mask,Obj_Entry * root,bool do_fini_funcs)854f14fb602SLionel Sambuc _rtld_unload_object(sigset_t *mask, Obj_Entry *root, bool do_fini_funcs)
855e83f7ba2SBen Gras {
856e83f7ba2SBen Gras 
857e83f7ba2SBen Gras 	_rtld_unref_dag(root);
858e83f7ba2SBen Gras 	if (root->refcount == 0) { /* We are finished with some objects. */
859e83f7ba2SBen Gras 		Obj_Entry *obj;
860e83f7ba2SBen Gras 		Obj_Entry **linkp;
861e83f7ba2SBen Gras 		Objlist_Entry *elm;
862e83f7ba2SBen Gras 
863e83f7ba2SBen Gras 		/* Finalize objects that are about to be unmapped. */
864e83f7ba2SBen Gras 		if (do_fini_funcs)
865f14fb602SLionel Sambuc 			_rtld_call_fini_functions(mask, 0);
866e83f7ba2SBen Gras 
867e83f7ba2SBen Gras 		/* Remove the DAG from all objects' DAG lists. */
868e83f7ba2SBen Gras 		SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
869e83f7ba2SBen Gras 			_rtld_objlist_remove(&elm->obj->dldags, root);
870e83f7ba2SBen Gras 
871e83f7ba2SBen Gras 		/* Remove the DAG from the RTLD_GLOBAL list. */
872e83f7ba2SBen Gras 		if (root->globalref) {
873e83f7ba2SBen Gras 			root->globalref = 0;
874e83f7ba2SBen Gras 			_rtld_objlist_remove(&_rtld_list_global, root);
875e83f7ba2SBen Gras 		}
876e83f7ba2SBen Gras 
877e83f7ba2SBen Gras 		/* Unmap all objects that are no longer referenced. */
878e83f7ba2SBen Gras 		linkp = &_rtld_objlist->next;
879e83f7ba2SBen Gras 		while ((obj = *linkp) != NULL) {
880e83f7ba2SBen Gras 			if (obj->refcount == 0) {
881e83f7ba2SBen Gras 				dbg(("unloading \"%s\"", obj->path));
882e83f7ba2SBen Gras 				if (obj->ehdr != MAP_FAILED)
883e83f7ba2SBen Gras 					munmap(obj->ehdr, _rtld_pagesz);
884e83f7ba2SBen Gras 				munmap(obj->mapbase, obj->mapsize);
885e83f7ba2SBen Gras 				_rtld_objlist_remove(&_rtld_list_global, obj);
886e83f7ba2SBen Gras 				_rtld_linkmap_delete(obj);
887e83f7ba2SBen Gras 				*linkp = obj->next;
888e83f7ba2SBen Gras 				_rtld_objcount--;
889e83f7ba2SBen Gras 				_rtld_obj_free(obj);
890e83f7ba2SBen Gras 			} else
891e83f7ba2SBen Gras 				linkp = &obj->next;
892e83f7ba2SBen Gras 		}
893e83f7ba2SBen Gras 		_rtld_objtail = linkp;
894e83f7ba2SBen Gras 	}
895e83f7ba2SBen Gras }
896e83f7ba2SBen Gras 
897e83f7ba2SBen Gras void
_rtld_ref_dag(Obj_Entry * root)898e83f7ba2SBen Gras _rtld_ref_dag(Obj_Entry *root)
899e83f7ba2SBen Gras {
900e83f7ba2SBen Gras 	const Needed_Entry *needed;
901e83f7ba2SBen Gras 
902e83f7ba2SBen Gras 	assert(root);
903e83f7ba2SBen Gras 
904e83f7ba2SBen Gras 	++root->refcount;
905e83f7ba2SBen Gras 
906e83f7ba2SBen Gras 	dbg(("incremented reference on \"%s\" (%d)", root->path,
907e83f7ba2SBen Gras 	    root->refcount));
908e83f7ba2SBen Gras 	for (needed = root->needed; needed != NULL;
909e83f7ba2SBen Gras 	     needed = needed->next) {
910e83f7ba2SBen Gras 		if (needed->obj != NULL)
911e83f7ba2SBen Gras 			_rtld_ref_dag(needed->obj);
912e83f7ba2SBen Gras 	}
913e83f7ba2SBen Gras }
914e83f7ba2SBen Gras 
915e83f7ba2SBen Gras static void
_rtld_unref_dag(Obj_Entry * root)916e83f7ba2SBen Gras _rtld_unref_dag(Obj_Entry *root)
917e83f7ba2SBen Gras {
918e83f7ba2SBen Gras 
919e83f7ba2SBen Gras 	assert(root);
920e83f7ba2SBen Gras 	assert(root->refcount != 0);
921e83f7ba2SBen Gras 
922e83f7ba2SBen Gras 	--root->refcount;
923e83f7ba2SBen Gras 	dbg(("decremented reference on \"%s\" (%d)", root->path,
924e83f7ba2SBen Gras 	    root->refcount));
925e83f7ba2SBen Gras 
926e83f7ba2SBen Gras 	if (root->refcount == 0) {
927e83f7ba2SBen Gras 		const Needed_Entry *needed;
928e83f7ba2SBen Gras 
929e83f7ba2SBen Gras 		for (needed = root->needed; needed != NULL;
930e83f7ba2SBen Gras 		     needed = needed->next) {
931e83f7ba2SBen Gras 			if (needed->obj != NULL)
932e83f7ba2SBen Gras 				_rtld_unref_dag(needed->obj);
933e83f7ba2SBen Gras 		}
934e83f7ba2SBen Gras 	}
935e83f7ba2SBen Gras }
936e83f7ba2SBen Gras 
__strong_alias(__dlclose,dlclose)937e83f7ba2SBen Gras __strong_alias(__dlclose,dlclose)
938e83f7ba2SBen Gras int
939e83f7ba2SBen Gras dlclose(void *handle)
940e83f7ba2SBen Gras {
941f14fb602SLionel Sambuc 	Obj_Entry *root;
942f14fb602SLionel Sambuc 	sigset_t mask;
943e83f7ba2SBen Gras 
944f14fb602SLionel Sambuc 	dbg(("dlclose of %p", handle));
945f14fb602SLionel Sambuc 
946f14fb602SLionel Sambuc 	_rtld_exclusive_enter(&mask);
947f14fb602SLionel Sambuc 
948f14fb602SLionel Sambuc 	root = _rtld_dlcheck(handle);
949f14fb602SLionel Sambuc 
950f14fb602SLionel Sambuc 	if (root == NULL) {
951f14fb602SLionel Sambuc 		_rtld_exclusive_exit(&mask);
952e83f7ba2SBen Gras 		return -1;
953f14fb602SLionel Sambuc 	}
954e83f7ba2SBen Gras 
955e83f7ba2SBen Gras 	_rtld_debug.r_state = RT_DELETE;
956e83f7ba2SBen Gras 	_rtld_debug_state();
957e83f7ba2SBen Gras 
958e83f7ba2SBen Gras 	--root->dl_refcount;
959f14fb602SLionel Sambuc 	_rtld_unload_object(&mask, root, true);
960e83f7ba2SBen Gras 
961e83f7ba2SBen Gras 	_rtld_debug.r_state = RT_CONSISTENT;
962e83f7ba2SBen Gras 	_rtld_debug_state();
963e83f7ba2SBen Gras 
964f14fb602SLionel Sambuc 	_rtld_exclusive_exit(&mask);
965f14fb602SLionel Sambuc 
966e83f7ba2SBen Gras 	return 0;
967e83f7ba2SBen Gras }
968e83f7ba2SBen Gras 
__strong_alias(__dlerror,dlerror)969e83f7ba2SBen Gras __strong_alias(__dlerror,dlerror)
970e83f7ba2SBen Gras char *
971e83f7ba2SBen Gras dlerror(void)
972e83f7ba2SBen Gras {
973e83f7ba2SBen Gras 	char *msg = error_message;
974e83f7ba2SBen Gras 
975e83f7ba2SBen Gras 	error_message = NULL;
976e83f7ba2SBen Gras 	return msg;
977e83f7ba2SBen Gras }
978e83f7ba2SBen Gras 
__strong_alias(__dlopen,dlopen)979e83f7ba2SBen Gras __strong_alias(__dlopen,dlopen)
980e83f7ba2SBen Gras void *
981e83f7ba2SBen Gras dlopen(const char *name, int mode)
982e83f7ba2SBen Gras {
983e83f7ba2SBen Gras 	Obj_Entry **old_obj_tail = _rtld_objtail;
984e83f7ba2SBen Gras 	Obj_Entry *obj = NULL;
985e83f7ba2SBen Gras 	int flags = _RTLD_DLOPEN;
986e83f7ba2SBen Gras 	bool nodelete;
987e83f7ba2SBen Gras 	bool now;
988f14fb602SLionel Sambuc 	sigset_t mask;
989f14fb602SLionel Sambuc 	int result;
990f14fb602SLionel Sambuc 
991f14fb602SLionel Sambuc 	dbg(("dlopen of %s %d", name, mode));
992f14fb602SLionel Sambuc 
993f14fb602SLionel Sambuc 	_rtld_exclusive_enter(&mask);
994e83f7ba2SBen Gras 
995e83f7ba2SBen Gras 	flags |= (mode & RTLD_GLOBAL) ? _RTLD_GLOBAL : 0;
996e83f7ba2SBen Gras 	flags |= (mode & RTLD_NOLOAD) ? _RTLD_NOLOAD : 0;
997e83f7ba2SBen Gras 
998e83f7ba2SBen Gras 	nodelete = (mode & RTLD_NODELETE) ? true : false;
999e83f7ba2SBen Gras 	now = ((mode & RTLD_MODEMASK) == RTLD_NOW) ? true : false;
1000e83f7ba2SBen Gras 
1001e83f7ba2SBen Gras 	_rtld_debug.r_state = RT_ADD;
1002e83f7ba2SBen Gras 	_rtld_debug_state();
1003e83f7ba2SBen Gras 
1004e83f7ba2SBen Gras 	if (name == NULL) {
1005e83f7ba2SBen Gras 		obj = _rtld_objmain;
1006e83f7ba2SBen Gras 		obj->refcount++;
1007e83f7ba2SBen Gras 	} else
1008e83f7ba2SBen Gras 		obj = _rtld_load_library(name, _rtld_objmain, flags);
1009e83f7ba2SBen Gras 
1010e83f7ba2SBen Gras 
1011e83f7ba2SBen Gras 	if (obj != NULL) {
1012e83f7ba2SBen Gras 		++obj->dl_refcount;
1013e83f7ba2SBen Gras 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
1014e83f7ba2SBen Gras 			assert(*old_obj_tail == obj);
1015e83f7ba2SBen Gras 
1016f14fb602SLionel Sambuc 			result = _rtld_load_needed_objects(obj, flags);
1017f14fb602SLionel Sambuc 			if (result != -1) {
1018f14fb602SLionel Sambuc 				Objlist_Entry *entry;
1019f14fb602SLionel Sambuc 				_rtld_init_dag(obj);
1020f14fb602SLionel Sambuc 				SIMPLEQ_FOREACH(entry, &obj->dagmembers, link) {
1021f14fb602SLionel Sambuc 					result = _rtld_verify_object_versions(entry->obj);
1022f14fb602SLionel Sambuc 					if (result == -1)
1023f14fb602SLionel Sambuc 						break;
1024f14fb602SLionel Sambuc 				}
1025f14fb602SLionel Sambuc 			}
1026f14fb602SLionel Sambuc 			if (result == -1 || _rtld_relocate_objects(obj,
1027f14fb602SLionel Sambuc 			    (now || obj->z_now)) == -1) {
1028f14fb602SLionel Sambuc 				_rtld_unload_object(&mask, obj, false);
1029e83f7ba2SBen Gras 				obj->dl_refcount--;
1030e83f7ba2SBen Gras 				obj = NULL;
1031e83f7ba2SBen Gras 			} else {
1032f14fb602SLionel Sambuc 				_rtld_call_init_functions(&mask);
1033e83f7ba2SBen Gras 			}
1034e83f7ba2SBen Gras 		}
1035e83f7ba2SBen Gras 		if (obj != NULL) {
1036e83f7ba2SBen Gras 			if ((nodelete || obj->z_nodelete) && !obj->ref_nodel) {
1037e83f7ba2SBen Gras 				dbg(("dlopen obj %s nodelete", obj->path));
1038e83f7ba2SBen Gras 				_rtld_ref_dag(obj);
1039e83f7ba2SBen Gras 				obj->z_nodelete = obj->ref_nodel = true;
1040e83f7ba2SBen Gras 			}
1041e83f7ba2SBen Gras 		}
1042e83f7ba2SBen Gras 	}
1043e83f7ba2SBen Gras 	_rtld_debug.r_state = RT_CONSISTENT;
1044e83f7ba2SBen Gras 	_rtld_debug_state();
1045e83f7ba2SBen Gras 
1046f14fb602SLionel Sambuc 	_rtld_exclusive_exit(&mask);
1047f14fb602SLionel Sambuc 
1048e83f7ba2SBen Gras 	return obj;
1049e83f7ba2SBen Gras }
1050e83f7ba2SBen Gras 
1051e83f7ba2SBen Gras /*
1052e83f7ba2SBen Gras  * Find a symbol in the main program.
1053e83f7ba2SBen Gras  */
1054e83f7ba2SBen Gras void *
_rtld_objmain_sym(const char * name)1055e83f7ba2SBen Gras _rtld_objmain_sym(const char *name)
1056e83f7ba2SBen Gras {
1057e83f7ba2SBen Gras 	unsigned long hash;
1058e83f7ba2SBen Gras 	const Elf_Sym *def;
1059e83f7ba2SBen Gras 	const Obj_Entry *obj;
1060e83f7ba2SBen Gras 	DoneList donelist;
1061e83f7ba2SBen Gras 
1062e83f7ba2SBen Gras 	hash = _rtld_elf_hash(name);
1063e83f7ba2SBen Gras 	obj = _rtld_objmain;
1064e83f7ba2SBen Gras 	_rtld_donelist_init(&donelist);
1065e83f7ba2SBen Gras 
1066f14fb602SLionel Sambuc 	def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, 0,
1067f14fb602SLionel Sambuc 	    NULL, &donelist);
1068e83f7ba2SBen Gras 
1069e83f7ba2SBen Gras 	if (def != NULL)
1070e83f7ba2SBen Gras 		return obj->relocbase + def->st_value;
1071e83f7ba2SBen Gras 	return NULL;
1072e83f7ba2SBen Gras }
1073e83f7ba2SBen Gras 
1074e83f7ba2SBen Gras #ifdef __powerpc__
1075e83f7ba2SBen Gras static void *
hackish_return_address(void)1076e83f7ba2SBen Gras hackish_return_address(void)
1077e83f7ba2SBen Gras {
1078e83f7ba2SBen Gras 	return __builtin_return_address(1);
1079e83f7ba2SBen Gras }
1080e83f7ba2SBen Gras #endif
1081e83f7ba2SBen Gras 
1082f14fb602SLionel Sambuc #ifdef __HAVE_FUNCTION_DESCRIPTORS
1083f14fb602SLionel Sambuc #define	lookup_mutex_enter()	_rtld_exclusive_enter(&mask)
1084f14fb602SLionel Sambuc #define	lookup_mutex_exit()	_rtld_exclusive_exit(&mask)
1085f14fb602SLionel Sambuc #else
1086f14fb602SLionel Sambuc #define	lookup_mutex_enter()	_rtld_shared_enter()
1087f14fb602SLionel Sambuc #define	lookup_mutex_exit()	_rtld_shared_exit()
1088f14fb602SLionel Sambuc #endif
1089f14fb602SLionel Sambuc 
1090f14fb602SLionel Sambuc static void *
do_dlsym(void * handle,const char * name,const Ver_Entry * ventry,void * retaddr)1091f14fb602SLionel Sambuc do_dlsym(void *handle, const char *name, const Ver_Entry *ventry, void *retaddr)
1092e83f7ba2SBen Gras {
1093e83f7ba2SBen Gras 	const Obj_Entry *obj;
1094e83f7ba2SBen Gras 	unsigned long hash;
1095e83f7ba2SBen Gras 	const Elf_Sym *def;
1096e83f7ba2SBen Gras 	const Obj_Entry *defobj;
1097e83f7ba2SBen Gras 	DoneList donelist;
1098f14fb602SLionel Sambuc 	const u_int flags = SYMLOOK_DLSYM | SYMLOOK_IN_PLT;
1099f14fb602SLionel Sambuc #ifdef __HAVE_FUNCTION_DESCRIPTORS
1100f14fb602SLionel Sambuc 	sigset_t mask;
1101f14fb602SLionel Sambuc #endif
1102f14fb602SLionel Sambuc 
1103f14fb602SLionel Sambuc 	lookup_mutex_enter();
1104e83f7ba2SBen Gras 
1105e83f7ba2SBen Gras 	hash = _rtld_elf_hash(name);
1106e83f7ba2SBen Gras 	def = NULL;
1107e83f7ba2SBen Gras 	defobj = NULL;
1108e83f7ba2SBen Gras 
1109e83f7ba2SBen Gras 	switch ((intptr_t)handle) {
1110e83f7ba2SBen Gras 	case (intptr_t)NULL:
1111e83f7ba2SBen Gras 	case (intptr_t)RTLD_NEXT:
1112e83f7ba2SBen Gras 	case (intptr_t)RTLD_DEFAULT:
1113e83f7ba2SBen Gras 	case (intptr_t)RTLD_SELF:
1114e83f7ba2SBen Gras 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
1115e83f7ba2SBen Gras 			_rtld_error("Cannot determine caller's shared object");
1116f14fb602SLionel Sambuc 			lookup_mutex_exit();
1117e83f7ba2SBen Gras 			return NULL;
1118e83f7ba2SBen Gras 		}
1119e83f7ba2SBen Gras 
1120e83f7ba2SBen Gras 		switch ((intptr_t)handle) {
1121e83f7ba2SBen Gras 		case (intptr_t)NULL:	 /* Just the caller's shared object. */
1122f14fb602SLionel Sambuc 			def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
1123e83f7ba2SBen Gras 			defobj = obj;
1124e83f7ba2SBen Gras 			break;
1125e83f7ba2SBen Gras 
1126e83f7ba2SBen Gras 		case (intptr_t)RTLD_NEXT:	/* Objects after callers */
1127e83f7ba2SBen Gras 			obj = obj->next;
1128e83f7ba2SBen Gras 			/*FALLTHROUGH*/
1129e83f7ba2SBen Gras 
1130e83f7ba2SBen Gras 		case (intptr_t)RTLD_SELF:	/* Caller included */
1131e83f7ba2SBen Gras 			for (; obj; obj = obj->next) {
1132e83f7ba2SBen Gras 				if ((def = _rtld_symlook_obj(name, hash, obj,
1133f14fb602SLionel Sambuc 				    flags, ventry)) != NULL) {
1134e83f7ba2SBen Gras 					defobj = obj;
1135e83f7ba2SBen Gras 					break;
1136e83f7ba2SBen Gras 				}
1137e83f7ba2SBen Gras 			}
1138e83f7ba2SBen Gras 			break;
1139e83f7ba2SBen Gras 
1140e83f7ba2SBen Gras 		case (intptr_t)RTLD_DEFAULT:
1141e83f7ba2SBen Gras 			def = _rtld_symlook_default(name, hash, obj, &defobj,
1142f14fb602SLionel Sambuc 			    flags, ventry);
1143e83f7ba2SBen Gras 			break;
1144e83f7ba2SBen Gras 
1145e83f7ba2SBen Gras 		default:
1146e83f7ba2SBen Gras 			abort();
1147e83f7ba2SBen Gras 		}
1148e83f7ba2SBen Gras 		break;
1149e83f7ba2SBen Gras 
1150e83f7ba2SBen Gras 	default:
1151f14fb602SLionel Sambuc 		if ((obj = _rtld_dlcheck(handle)) == NULL) {
1152f14fb602SLionel Sambuc 			lookup_mutex_exit();
1153e83f7ba2SBen Gras 			return NULL;
1154f14fb602SLionel Sambuc 		}
1155e83f7ba2SBen Gras 
1156e83f7ba2SBen Gras 		_rtld_donelist_init(&donelist);
1157e83f7ba2SBen Gras 
1158e83f7ba2SBen Gras 		if (obj->mainprog) {
1159e83f7ba2SBen Gras 			/* Search main program and all libraries loaded by it */
1160e83f7ba2SBen Gras 			def = _rtld_symlook_list(name, hash, &_rtld_list_main,
1161f14fb602SLionel Sambuc 			    &defobj, flags, ventry, &donelist);
1162e83f7ba2SBen Gras 		} else {
1163e83f7ba2SBen Gras 			Needed_Entry fake;
1164e83f7ba2SBen Gras 			DoneList depth;
1165e83f7ba2SBen Gras 
1166e83f7ba2SBen Gras 			/* Search the object and all the libraries loaded by it. */
1167e83f7ba2SBen Gras 			fake.next = NULL;
1168e83f7ba2SBen Gras 			fake.obj = __UNCONST(obj);
1169e83f7ba2SBen Gras 			fake.name = 0;
1170e83f7ba2SBen Gras 
1171e83f7ba2SBen Gras 			_rtld_donelist_init(&depth);
1172e83f7ba2SBen Gras 			def = _rtld_symlook_needed(name, hash, &fake, &defobj,
1173f14fb602SLionel Sambuc 			    flags, ventry, &donelist, &depth);
1174e83f7ba2SBen Gras 		}
1175e83f7ba2SBen Gras 
1176e83f7ba2SBen Gras 		break;
1177e83f7ba2SBen Gras 	}
1178e83f7ba2SBen Gras 
1179e83f7ba2SBen Gras 	if (def != NULL) {
1180f14fb602SLionel Sambuc 		void *p;
1181*0a6a1f1dSLionel Sambuc 
1182*0a6a1f1dSLionel Sambuc 		if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
1183*0a6a1f1dSLionel Sambuc #ifdef __HAVE_FUNCTION_DESCRIPTORS
1184*0a6a1f1dSLionel Sambuc 			lookup_mutex_exit();
1185*0a6a1f1dSLionel Sambuc 			_rtld_shared_enter();
1186*0a6a1f1dSLionel Sambuc #endif
1187*0a6a1f1dSLionel Sambuc 			p = (void *)_rtld_resolve_ifunc(defobj, def);
1188*0a6a1f1dSLionel Sambuc 			_rtld_shared_exit();
1189*0a6a1f1dSLionel Sambuc 			return p;
1190*0a6a1f1dSLionel Sambuc 		}
1191*0a6a1f1dSLionel Sambuc 
1192e83f7ba2SBen Gras #ifdef __HAVE_FUNCTION_DESCRIPTORS
1193f14fb602SLionel Sambuc 		if (ELF_ST_TYPE(def->st_info) == STT_FUNC) {
1194f14fb602SLionel Sambuc 			p = (void *)_rtld_function_descriptor_alloc(defobj,
1195e83f7ba2SBen Gras 			    def, 0);
1196f14fb602SLionel Sambuc 			lookup_mutex_exit();
1197f14fb602SLionel Sambuc 			return p;
1198f14fb602SLionel Sambuc 		}
1199e83f7ba2SBen Gras #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1200f14fb602SLionel Sambuc 		p = defobj->relocbase + def->st_value;
1201f14fb602SLionel Sambuc 		lookup_mutex_exit();
1202f14fb602SLionel Sambuc 		return p;
1203e83f7ba2SBen Gras 	}
1204e83f7ba2SBen Gras 
1205e83f7ba2SBen Gras 	_rtld_error("Undefined symbol \"%s\"", name);
1206f14fb602SLionel Sambuc 	lookup_mutex_exit();
1207e83f7ba2SBen Gras 	return NULL;
1208e83f7ba2SBen Gras }
1209e83f7ba2SBen Gras 
__strong_alias(__dlsym,dlsym)1210f14fb602SLionel Sambuc __strong_alias(__dlsym,dlsym)
1211f14fb602SLionel Sambuc void *
1212f14fb602SLionel Sambuc dlsym(void *handle, const char *name)
1213f14fb602SLionel Sambuc {
1214f14fb602SLionel Sambuc 	void *retaddr;
1215f14fb602SLionel Sambuc 
1216f14fb602SLionel Sambuc 	dbg(("dlsym of %s in %p", name, handle));
1217f14fb602SLionel Sambuc 
1218f14fb602SLionel Sambuc #ifdef __powerpc__
1219f14fb602SLionel Sambuc 	retaddr = hackish_return_address();
1220f14fb602SLionel Sambuc #else
1221f14fb602SLionel Sambuc 	retaddr = __builtin_return_address(0);
1222f14fb602SLionel Sambuc #endif
1223f14fb602SLionel Sambuc 	return do_dlsym(handle, name, NULL, retaddr);
1224f14fb602SLionel Sambuc }
1225f14fb602SLionel Sambuc 
__strong_alias(__dlvsym,dlvsym)1226f14fb602SLionel Sambuc __strong_alias(__dlvsym,dlvsym)
1227f14fb602SLionel Sambuc void *
1228f14fb602SLionel Sambuc dlvsym(void *handle, const char *name, const char *version)
1229f14fb602SLionel Sambuc {
1230f14fb602SLionel Sambuc 	Ver_Entry *ventry = NULL;
1231f14fb602SLionel Sambuc 	Ver_Entry ver_entry;
1232f14fb602SLionel Sambuc 	void *retaddr;
1233f14fb602SLionel Sambuc 
1234f14fb602SLionel Sambuc 	dbg(("dlvsym of %s@%s in %p", name, version ? version : NULL, handle));
1235f14fb602SLionel Sambuc 
1236f14fb602SLionel Sambuc 	if (version != NULL) {
1237f14fb602SLionel Sambuc 		ver_entry.name = version;
1238f14fb602SLionel Sambuc 		ver_entry.file = NULL;
1239f14fb602SLionel Sambuc 		ver_entry.hash = _rtld_elf_hash(version);
1240f14fb602SLionel Sambuc 		ver_entry.flags = 0;
1241f14fb602SLionel Sambuc 		ventry = &ver_entry;
1242f14fb602SLionel Sambuc 	}
1243f14fb602SLionel Sambuc #ifdef __powerpc__
1244f14fb602SLionel Sambuc 	retaddr = hackish_return_address();
1245f14fb602SLionel Sambuc #else
1246f14fb602SLionel Sambuc 	retaddr = __builtin_return_address(0);
1247f14fb602SLionel Sambuc #endif
1248f14fb602SLionel Sambuc 	return do_dlsym(handle, name, ventry, retaddr);
1249f14fb602SLionel Sambuc }
1250f14fb602SLionel Sambuc 
__strong_alias(__dladdr,dladdr)1251e83f7ba2SBen Gras __strong_alias(__dladdr,dladdr)
1252e83f7ba2SBen Gras int
1253e83f7ba2SBen Gras dladdr(const void *addr, Dl_info *info)
1254e83f7ba2SBen Gras {
1255e83f7ba2SBen Gras 	const Obj_Entry *obj;
1256e83f7ba2SBen Gras 	const Elf_Sym *def, *best_def;
1257e83f7ba2SBen Gras 	void *symbol_addr;
1258e83f7ba2SBen Gras 	unsigned long symoffset;
1259f14fb602SLionel Sambuc #ifdef __HAVE_FUNCTION_DESCRIPTORS
1260f14fb602SLionel Sambuc 	sigset_t mask;
1261f14fb602SLionel Sambuc #endif
1262f14fb602SLionel Sambuc 
1263f14fb602SLionel Sambuc 	dbg(("dladdr of %p", addr));
1264f14fb602SLionel Sambuc 
1265f14fb602SLionel Sambuc 	lookup_mutex_enter();
1266e83f7ba2SBen Gras 
1267e83f7ba2SBen Gras #ifdef __HAVE_FUNCTION_DESCRIPTORS
1268e83f7ba2SBen Gras 	addr = _rtld_function_descriptor_function(addr);
1269e83f7ba2SBen Gras #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1270e83f7ba2SBen Gras 
1271e83f7ba2SBen Gras 	obj = _rtld_obj_from_addr(addr);
1272e83f7ba2SBen Gras 	if (obj == NULL) {
1273e83f7ba2SBen Gras 		_rtld_error("No shared object contains address");
1274*0a6a1f1dSLionel Sambuc 		lookup_mutex_exit();
1275e83f7ba2SBen Gras 		return 0;
1276e83f7ba2SBen Gras 	}
1277e83f7ba2SBen Gras 	info->dli_fname = obj->path;
1278e83f7ba2SBen Gras 	info->dli_fbase = obj->mapbase;
1279e83f7ba2SBen Gras 	info->dli_saddr = (void *)0;
1280e83f7ba2SBen Gras 	info->dli_sname = NULL;
1281e83f7ba2SBen Gras 
1282e83f7ba2SBen Gras 	/*
1283e83f7ba2SBen Gras 	 * Walk the symbol list looking for the symbol whose address is
1284e83f7ba2SBen Gras 	 * closest to the address sent in.
1285e83f7ba2SBen Gras 	 */
1286e83f7ba2SBen Gras 	best_def = NULL;
1287e83f7ba2SBen Gras 	for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
1288e83f7ba2SBen Gras 		def = obj->symtab + symoffset;
1289e83f7ba2SBen Gras 
1290e83f7ba2SBen Gras 		/*
1291e83f7ba2SBen Gras 		 * For skip the symbol if st_shndx is either SHN_UNDEF or
1292e83f7ba2SBen Gras 		 * SHN_COMMON.
1293e83f7ba2SBen Gras 		 */
1294e83f7ba2SBen Gras 		if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
1295e83f7ba2SBen Gras 			continue;
1296e83f7ba2SBen Gras 
1297e83f7ba2SBen Gras 		/*
1298e83f7ba2SBen Gras 		 * If the symbol is greater than the specified address, or if it
1299e83f7ba2SBen Gras 		 * is further away from addr than the current nearest symbol,
1300e83f7ba2SBen Gras 		 * then reject it.
1301e83f7ba2SBen Gras 		 */
1302e83f7ba2SBen Gras 		symbol_addr = obj->relocbase + def->st_value;
1303e83f7ba2SBen Gras 		if (symbol_addr > addr || symbol_addr < info->dli_saddr)
1304e83f7ba2SBen Gras 			continue;
1305e83f7ba2SBen Gras 
1306e83f7ba2SBen Gras 		/* Update our idea of the nearest symbol. */
1307e83f7ba2SBen Gras 		info->dli_sname = obj->strtab + def->st_name;
1308e83f7ba2SBen Gras 		info->dli_saddr = symbol_addr;
1309e83f7ba2SBen Gras 		best_def = def;
1310e83f7ba2SBen Gras 
131184d9c625SLionel Sambuc 
1312e83f7ba2SBen Gras 		/* Exact match? */
1313e83f7ba2SBen Gras 		if (info->dli_saddr == addr)
1314e83f7ba2SBen Gras 			break;
1315e83f7ba2SBen Gras 	}
1316e83f7ba2SBen Gras 
1317e83f7ba2SBen Gras #ifdef __HAVE_FUNCTION_DESCRIPTORS
1318e83f7ba2SBen Gras 	if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
1319e83f7ba2SBen Gras 		info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
1320e83f7ba2SBen Gras 		    best_def, 0);
132184d9c625SLionel Sambuc #else
132284d9c625SLionel Sambuc 	__USE(best_def);
1323e83f7ba2SBen Gras #endif /* __HAVE_FUNCTION_DESCRIPTORS */
1324e83f7ba2SBen Gras 
1325f14fb602SLionel Sambuc 	lookup_mutex_exit();
1326e83f7ba2SBen Gras 	return 1;
1327e83f7ba2SBen Gras }
1328e83f7ba2SBen Gras 
__strong_alias(__dlinfo,dlinfo)1329e83f7ba2SBen Gras __strong_alias(__dlinfo,dlinfo)
1330e83f7ba2SBen Gras int
1331e83f7ba2SBen Gras dlinfo(void *handle, int req, void *v)
1332e83f7ba2SBen Gras {
1333e83f7ba2SBen Gras 	const Obj_Entry *obj;
1334e83f7ba2SBen Gras 	void *retaddr;
1335e83f7ba2SBen Gras 
1336f14fb602SLionel Sambuc 	dbg(("dlinfo for %p %d", handle, req));
1337f14fb602SLionel Sambuc 
1338f14fb602SLionel Sambuc 	_rtld_shared_enter();
1339f14fb602SLionel Sambuc 
1340e83f7ba2SBen Gras 	if (handle == RTLD_SELF) {
1341e83f7ba2SBen Gras #ifdef __powerpc__
1342e83f7ba2SBen Gras 		retaddr = hackish_return_address();
1343e83f7ba2SBen Gras #else
1344e83f7ba2SBen Gras 		retaddr = __builtin_return_address(0);
1345e83f7ba2SBen Gras #endif
1346e83f7ba2SBen Gras 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
1347e83f7ba2SBen Gras 			_rtld_error("Cannot determine caller's shared object");
1348f14fb602SLionel Sambuc 			_rtld_shared_exit();
1349e83f7ba2SBen Gras 			return -1;
1350e83f7ba2SBen Gras 		}
1351e83f7ba2SBen Gras 	} else {
1352e83f7ba2SBen Gras 		if ((obj = _rtld_dlcheck(handle)) == NULL) {
1353f14fb602SLionel Sambuc 			_rtld_shared_exit();
1354e83f7ba2SBen Gras 			return -1;
1355e83f7ba2SBen Gras 		}
1356e83f7ba2SBen Gras 	}
1357e83f7ba2SBen Gras 
1358e83f7ba2SBen Gras 	switch (req) {
1359e83f7ba2SBen Gras 	case RTLD_DI_LINKMAP:
1360e83f7ba2SBen Gras 		{
1361e83f7ba2SBen Gras 		const struct link_map **map = v;
1362e83f7ba2SBen Gras 
1363e83f7ba2SBen Gras 		*map = &obj->linkmap;
1364e83f7ba2SBen Gras 		break;
1365e83f7ba2SBen Gras 		}
1366e83f7ba2SBen Gras 
1367e83f7ba2SBen Gras 	default:
1368e83f7ba2SBen Gras 		_rtld_error("Invalid request");
1369f14fb602SLionel Sambuc 		_rtld_shared_exit();
1370e83f7ba2SBen Gras 		return -1;
1371e83f7ba2SBen Gras 	}
1372e83f7ba2SBen Gras 
1373f14fb602SLionel Sambuc 	_rtld_shared_exit();
1374e83f7ba2SBen Gras 	return 0;
1375e83f7ba2SBen Gras }
1376e83f7ba2SBen Gras 
1377e83f7ba2SBen Gras __strong_alias(__dl_iterate_phdr,dl_iterate_phdr);
1378e83f7ba2SBen Gras int
dl_iterate_phdr(int (* callback)(struct dl_phdr_info *,size_t,void *),void * param)1379e83f7ba2SBen Gras dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *), void *param)
1380e83f7ba2SBen Gras {
1381e83f7ba2SBen Gras 	struct dl_phdr_info phdr_info;
1382e83f7ba2SBen Gras 	const Obj_Entry *obj;
1383e83f7ba2SBen Gras 	int error = 0;
1384e83f7ba2SBen Gras 
1385f14fb602SLionel Sambuc 	dbg(("dl_iterate_phdr"));
1386f14fb602SLionel Sambuc 
1387f14fb602SLionel Sambuc 	_rtld_shared_enter();
1388f14fb602SLionel Sambuc 
1389e83f7ba2SBen Gras 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
1390e83f7ba2SBen Gras 		phdr_info.dlpi_addr = (Elf_Addr)obj->relocbase;
139184d9c625SLionel Sambuc 		/* XXX: wrong but not fixing it yet */
139284d9c625SLionel Sambuc 		phdr_info.dlpi_name = SIMPLEQ_FIRST(&obj->names) ?
139384d9c625SLionel Sambuc 		    SIMPLEQ_FIRST(&obj->names)->name : obj->path;
1394e83f7ba2SBen Gras 		phdr_info.dlpi_phdr = obj->phdr;
1395e83f7ba2SBen Gras 		phdr_info.dlpi_phnum = obj->phsize / sizeof(obj->phdr[0]);
1396f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
1397e83f7ba2SBen Gras 		phdr_info.dlpi_tls_modid = obj->tlsindex;
1398e83f7ba2SBen Gras 		phdr_info.dlpi_tls_data = obj->tlsinit;
1399f14fb602SLionel Sambuc #else
1400f14fb602SLionel Sambuc 		phdr_info.dlpi_tls_modid = 0;
1401f14fb602SLionel Sambuc 		phdr_info.dlpi_tls_data = 0;
1402e83f7ba2SBen Gras #endif
1403e83f7ba2SBen Gras 		phdr_info.dlpi_adds = _rtld_objloads;
1404e83f7ba2SBen Gras 		phdr_info.dlpi_subs = _rtld_objloads - _rtld_objcount;
1405e83f7ba2SBen Gras 
1406f14fb602SLionel Sambuc 		/* XXXlocking: exit point */
1407e83f7ba2SBen Gras 		error = callback(&phdr_info, sizeof(phdr_info), param);
1408e83f7ba2SBen Gras 		if (error)
1409e83f7ba2SBen Gras 			break;
1410e83f7ba2SBen Gras 	}
1411e83f7ba2SBen Gras 
1412f14fb602SLionel Sambuc 	_rtld_shared_exit();
1413e83f7ba2SBen Gras 	return error;
1414e83f7ba2SBen Gras }
1415e83f7ba2SBen Gras 
1416e83f7ba2SBen Gras /*
1417e83f7ba2SBen Gras  * Error reporting function.  Use it like printf.  If formats the message
1418e83f7ba2SBen Gras  * into a buffer, and sets things up so that the next call to dlerror()
1419e83f7ba2SBen Gras  * will return the message.
1420e83f7ba2SBen Gras  */
1421e83f7ba2SBen Gras void
_rtld_error(const char * fmt,...)1422e83f7ba2SBen Gras _rtld_error(const char *fmt,...)
1423e83f7ba2SBen Gras {
1424e83f7ba2SBen Gras 	static char     buf[512];
1425e83f7ba2SBen Gras 	va_list         ap;
1426e83f7ba2SBen Gras 
1427e83f7ba2SBen Gras 	va_start(ap, fmt);
1428e83f7ba2SBen Gras 	xvsnprintf(buf, sizeof buf, fmt, ap);
1429e83f7ba2SBen Gras 	error_message = buf;
1430e83f7ba2SBen Gras 	va_end(ap);
1431e83f7ba2SBen Gras }
1432e83f7ba2SBen Gras 
1433e83f7ba2SBen Gras void
_rtld_debug_state(void)1434e83f7ba2SBen Gras _rtld_debug_state(void)
1435e83f7ba2SBen Gras {
1436*0a6a1f1dSLionel Sambuc #if defined(__hppa__)
1437*0a6a1f1dSLionel Sambuc 	__asm volatile("nop" ::: "memory");
1438*0a6a1f1dSLionel Sambuc #endif
1439e83f7ba2SBen Gras 
1440f14fb602SLionel Sambuc 	/* Prevent optimizer from removing calls to this function */
1441f14fb602SLionel Sambuc 	__insn_barrier();
1442e83f7ba2SBen Gras }
1443e83f7ba2SBen Gras 
1444e83f7ba2SBen Gras void
_rtld_linkmap_add(Obj_Entry * obj)1445e83f7ba2SBen Gras _rtld_linkmap_add(Obj_Entry *obj)
1446e83f7ba2SBen Gras {
1447e83f7ba2SBen Gras 	struct link_map *l = &obj->linkmap;
1448e83f7ba2SBen Gras 	struct link_map *prev;
1449e83f7ba2SBen Gras 
1450e83f7ba2SBen Gras 	obj->linkmap.l_name = obj->path;
1451e83f7ba2SBen Gras 	obj->linkmap.l_addr = obj->relocbase;
1452e83f7ba2SBen Gras 	obj->linkmap.l_ld = obj->dynamic;
1453e83f7ba2SBen Gras #ifdef __mips__
1454e83f7ba2SBen Gras 	/* XXX This field is not standard and will be removed eventually. */
1455e83f7ba2SBen Gras 	obj->linkmap.l_offs = obj->relocbase;
1456e83f7ba2SBen Gras #endif
1457e83f7ba2SBen Gras 
1458e83f7ba2SBen Gras 	if (_rtld_debug.r_map == NULL) {
1459e83f7ba2SBen Gras 		_rtld_debug.r_map = l;
1460e83f7ba2SBen Gras 		return;
1461e83f7ba2SBen Gras 	}
1462e83f7ba2SBen Gras 
1463e83f7ba2SBen Gras 	/*
1464e83f7ba2SBen Gras 	 * Scan to the end of the list, but not past the entry for the
1465e83f7ba2SBen Gras 	 * dynamic linker, which we want to keep at the very end.
1466e83f7ba2SBen Gras 	 */
1467e83f7ba2SBen Gras 	for (prev = _rtld_debug.r_map;
1468e83f7ba2SBen Gras 	    prev->l_next != NULL && prev->l_next != &_rtld_objself.linkmap;
1469e83f7ba2SBen Gras 	    prev = prev->l_next);
1470e83f7ba2SBen Gras 
1471e83f7ba2SBen Gras 	l->l_prev = prev;
1472e83f7ba2SBen Gras 	l->l_next = prev->l_next;
1473e83f7ba2SBen Gras 	if (l->l_next != NULL)
1474e83f7ba2SBen Gras 		l->l_next->l_prev = l;
1475e83f7ba2SBen Gras 	prev->l_next = l;
1476e83f7ba2SBen Gras }
1477e83f7ba2SBen Gras 
1478e83f7ba2SBen Gras void
_rtld_linkmap_delete(Obj_Entry * obj)1479e83f7ba2SBen Gras _rtld_linkmap_delete(Obj_Entry *obj)
1480e83f7ba2SBen Gras {
1481e83f7ba2SBen Gras 	struct link_map *l = &obj->linkmap;
1482e83f7ba2SBen Gras 
1483e83f7ba2SBen Gras 	if (l->l_prev == NULL) {
1484e83f7ba2SBen Gras 		if ((_rtld_debug.r_map = l->l_next) != NULL)
1485e83f7ba2SBen Gras 			l->l_next->l_prev = NULL;
1486e83f7ba2SBen Gras 		return;
1487e83f7ba2SBen Gras 	}
1488e83f7ba2SBen Gras 	if ((l->l_prev->l_next = l->l_next) != NULL)
1489e83f7ba2SBen Gras 		l->l_next->l_prev = l->l_prev;
1490e83f7ba2SBen Gras }
1491e83f7ba2SBen Gras 
1492e83f7ba2SBen Gras static Obj_Entry *
_rtld_obj_from_addr(const void * addr)1493e83f7ba2SBen Gras _rtld_obj_from_addr(const void *addr)
1494e83f7ba2SBen Gras {
1495e83f7ba2SBen Gras 	Obj_Entry *obj;
1496e83f7ba2SBen Gras 
1497e83f7ba2SBen Gras 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
1498e83f7ba2SBen Gras 		if (addr < (void *) obj->mapbase)
1499e83f7ba2SBen Gras 			continue;
1500e83f7ba2SBen Gras 		if (addr < (void *) (obj->mapbase + obj->mapsize))
1501e83f7ba2SBen Gras 			return obj;
1502e83f7ba2SBen Gras 	}
1503e83f7ba2SBen Gras 	return NULL;
1504e83f7ba2SBen Gras }
1505e83f7ba2SBen Gras 
1506e83f7ba2SBen Gras static void
_rtld_objlist_clear(Objlist * list)1507e83f7ba2SBen Gras _rtld_objlist_clear(Objlist *list)
1508e83f7ba2SBen Gras {
1509e83f7ba2SBen Gras 	while (!SIMPLEQ_EMPTY(list)) {
1510e83f7ba2SBen Gras 		Objlist_Entry* elm = SIMPLEQ_FIRST(list);
1511e83f7ba2SBen Gras 		SIMPLEQ_REMOVE_HEAD(list, link);
1512e83f7ba2SBen Gras 		xfree(elm);
1513e83f7ba2SBen Gras 	}
1514e83f7ba2SBen Gras }
1515e83f7ba2SBen Gras 
1516e83f7ba2SBen Gras static void
_rtld_objlist_remove(Objlist * list,Obj_Entry * obj)1517e83f7ba2SBen Gras _rtld_objlist_remove(Objlist *list, Obj_Entry *obj)
1518e83f7ba2SBen Gras {
1519e83f7ba2SBen Gras 	Objlist_Entry *elm;
1520e83f7ba2SBen Gras 
1521e83f7ba2SBen Gras 	if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
1522e83f7ba2SBen Gras 		SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
1523e83f7ba2SBen Gras 		xfree(elm);
1524e83f7ba2SBen Gras 	}
1525e83f7ba2SBen Gras }
1526f14fb602SLionel Sambuc 
1527f14fb602SLionel Sambuc #if defined(__minix)
_rtld_shared_enter(void)1528f14fb602SLionel Sambuc void _rtld_shared_enter(void) {}
_rtld_shared_exit(void)1529f14fb602SLionel Sambuc void _rtld_shared_exit(void) {}
_rtld_exclusive_enter(sigset_t * mask)1530f14fb602SLionel Sambuc void _rtld_exclusive_enter(sigset_t *mask) {}
_rtld_exclusive_exit(sigset_t * mask)1531f14fb602SLionel Sambuc void _rtld_exclusive_exit(sigset_t *mask) {}
1532f14fb602SLionel Sambuc #else
1533f14fb602SLionel Sambuc #define	RTLD_EXCLUSIVE_MASK	0x80000000U
1534f14fb602SLionel Sambuc static volatile unsigned int _rtld_mutex;
1535f14fb602SLionel Sambuc static volatile unsigned int _rtld_waiter_exclusive;
1536f14fb602SLionel Sambuc static volatile unsigned int _rtld_waiter_shared;
1537f14fb602SLionel Sambuc 
1538f14fb602SLionel Sambuc void
_rtld_shared_enter(void)1539f14fb602SLionel Sambuc _rtld_shared_enter(void)
1540f14fb602SLionel Sambuc {
1541f14fb602SLionel Sambuc 	unsigned int cur;
1542f14fb602SLionel Sambuc 	lwpid_t waiter, self = 0;
1543f14fb602SLionel Sambuc 
1544f14fb602SLionel Sambuc 	membar_enter();
1545f14fb602SLionel Sambuc 
1546f14fb602SLionel Sambuc 	for (;;) {
1547f14fb602SLionel Sambuc 		cur = _rtld_mutex;
1548f14fb602SLionel Sambuc 		/*
1549f14fb602SLionel Sambuc 		 * First check if we are currently not exclusively locked.
1550f14fb602SLionel Sambuc 		 */
1551f14fb602SLionel Sambuc 		if ((cur & RTLD_EXCLUSIVE_MASK) == 0) {
1552f14fb602SLionel Sambuc 			/* Yes, so increment use counter */
1553f14fb602SLionel Sambuc 			if (atomic_cas_uint(&_rtld_mutex, cur, cur + 1) != cur)
1554f14fb602SLionel Sambuc 				continue;
1555*0a6a1f1dSLionel Sambuc 			membar_enter();
1556f14fb602SLionel Sambuc 			return;
1557f14fb602SLionel Sambuc 		}
1558f14fb602SLionel Sambuc 		/*
1559f14fb602SLionel Sambuc 		 * Someone has an exclusive lock.  Puts us on the waiter list.
1560f14fb602SLionel Sambuc 		 */
1561f14fb602SLionel Sambuc 		if (!self)
1562f14fb602SLionel Sambuc 			self = _lwp_self();
1563f14fb602SLionel Sambuc 		if (cur == (self | RTLD_EXCLUSIVE_MASK)) {
1564f14fb602SLionel Sambuc 			if (_rtld_mutex_may_recurse)
1565f14fb602SLionel Sambuc 				return;
1566f14fb602SLionel Sambuc 			_rtld_error("dead lock detected");
1567f14fb602SLionel Sambuc 			_rtld_die();
1568f14fb602SLionel Sambuc 		}
1569f14fb602SLionel Sambuc 		waiter = atomic_swap_uint(&_rtld_waiter_shared, self);
1570f14fb602SLionel Sambuc 		/*
1571f14fb602SLionel Sambuc 		 * Check for race against _rtld_exclusive_exit before sleeping.
1572f14fb602SLionel Sambuc 		 */
1573*0a6a1f1dSLionel Sambuc 		membar_sync();
1574f14fb602SLionel Sambuc 		if ((_rtld_mutex & RTLD_EXCLUSIVE_MASK) ||
1575f14fb602SLionel Sambuc 		    _rtld_waiter_exclusive)
1576*0a6a1f1dSLionel Sambuc 			_lwp_park(CLOCK_REALTIME, 0, NULL, 0,
1577*0a6a1f1dSLionel Sambuc 			    __UNVOLATILE(&_rtld_mutex), NULL);
1578f14fb602SLionel Sambuc 		/* Try to remove us from the waiter list. */
1579f14fb602SLionel Sambuc 		atomic_cas_uint(&_rtld_waiter_shared, self, 0);
1580f14fb602SLionel Sambuc 		if (waiter)
1581f14fb602SLionel Sambuc 			_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1582f14fb602SLionel Sambuc 	}
1583f14fb602SLionel Sambuc }
1584f14fb602SLionel Sambuc 
1585f14fb602SLionel Sambuc void
_rtld_shared_exit(void)1586f14fb602SLionel Sambuc _rtld_shared_exit(void)
1587f14fb602SLionel Sambuc {
1588f14fb602SLionel Sambuc 	lwpid_t waiter;
1589f14fb602SLionel Sambuc 
1590f14fb602SLionel Sambuc 	/*
1591f14fb602SLionel Sambuc 	 * Shared lock taken after an exclusive lock.
1592f14fb602SLionel Sambuc 	 * Just assume this is a partial recursion.
1593f14fb602SLionel Sambuc 	 */
1594f14fb602SLionel Sambuc 	if (_rtld_mutex & RTLD_EXCLUSIVE_MASK)
1595f14fb602SLionel Sambuc 		return;
1596f14fb602SLionel Sambuc 
1597f14fb602SLionel Sambuc 	/*
1598f14fb602SLionel Sambuc 	 * Wakeup LWPs waiting for an exclusive lock if this is the last
1599f14fb602SLionel Sambuc 	 * LWP on the shared lock.
1600f14fb602SLionel Sambuc 	 */
1601*0a6a1f1dSLionel Sambuc 	membar_exit();
1602f14fb602SLionel Sambuc 	if (atomic_dec_uint_nv(&_rtld_mutex))
1603f14fb602SLionel Sambuc 		return;
1604*0a6a1f1dSLionel Sambuc 	membar_sync();
1605f14fb602SLionel Sambuc 	if ((waiter = _rtld_waiter_exclusive) != 0)
1606f14fb602SLionel Sambuc 		_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1607f14fb602SLionel Sambuc }
1608f14fb602SLionel Sambuc 
1609f14fb602SLionel Sambuc void
_rtld_exclusive_enter(sigset_t * mask)1610f14fb602SLionel Sambuc _rtld_exclusive_enter(sigset_t *mask)
1611f14fb602SLionel Sambuc {
1612f14fb602SLionel Sambuc 	lwpid_t waiter, self = _lwp_self();
1613f14fb602SLionel Sambuc 	unsigned int locked_value = (unsigned int)self | RTLD_EXCLUSIVE_MASK;
1614f14fb602SLionel Sambuc 	unsigned int cur;
1615f14fb602SLionel Sambuc 	sigset_t blockmask;
1616f14fb602SLionel Sambuc 
1617f14fb602SLionel Sambuc 	sigfillset(&blockmask);
1618f14fb602SLionel Sambuc 	sigdelset(&blockmask, SIGTRAP);	/* Allow the debugger */
1619f14fb602SLionel Sambuc 	sigprocmask(SIG_BLOCK, &blockmask, mask);
1620f14fb602SLionel Sambuc 
1621f14fb602SLionel Sambuc 	for (;;) {
1622*0a6a1f1dSLionel Sambuc 		if (atomic_cas_uint(&_rtld_mutex, 0, locked_value) == 0) {
1623*0a6a1f1dSLionel Sambuc 			membar_enter();
1624f14fb602SLionel Sambuc 			break;
1625*0a6a1f1dSLionel Sambuc 		}
1626f14fb602SLionel Sambuc 		waiter = atomic_swap_uint(&_rtld_waiter_exclusive, self);
1627*0a6a1f1dSLionel Sambuc 		membar_sync();
1628f14fb602SLionel Sambuc 		cur = _rtld_mutex;
1629f14fb602SLionel Sambuc 		if (cur == locked_value) {
1630f14fb602SLionel Sambuc 			_rtld_error("dead lock detected");
1631f14fb602SLionel Sambuc 			_rtld_die();
1632f14fb602SLionel Sambuc 		}
1633f14fb602SLionel Sambuc 		if (cur)
1634*0a6a1f1dSLionel Sambuc 			_lwp_park(CLOCK_REALTIME, 0, NULL, 0,
1635*0a6a1f1dSLionel Sambuc 			    __UNVOLATILE(&_rtld_mutex), NULL);
1636f14fb602SLionel Sambuc 		atomic_cas_uint(&_rtld_waiter_exclusive, self, 0);
1637f14fb602SLionel Sambuc 		if (waiter)
1638f14fb602SLionel Sambuc 			_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1639f14fb602SLionel Sambuc 	}
1640f14fb602SLionel Sambuc }
1641f14fb602SLionel Sambuc 
1642f14fb602SLionel Sambuc void
_rtld_exclusive_exit(sigset_t * mask)1643f14fb602SLionel Sambuc _rtld_exclusive_exit(sigset_t *mask)
1644f14fb602SLionel Sambuc {
1645f14fb602SLionel Sambuc 	lwpid_t waiter;
1646f14fb602SLionel Sambuc 
1647*0a6a1f1dSLionel Sambuc 	membar_exit();
1648f14fb602SLionel Sambuc 	_rtld_mutex = 0;
1649*0a6a1f1dSLionel Sambuc 	membar_sync();
1650f14fb602SLionel Sambuc 	if ((waiter = _rtld_waiter_exclusive) != 0)
1651f14fb602SLionel Sambuc 		_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1652f14fb602SLionel Sambuc 
1653f14fb602SLionel Sambuc 	if ((waiter = _rtld_waiter_shared) != 0)
1654f14fb602SLionel Sambuc 		_lwp_unpark(waiter, __UNVOLATILE(&_rtld_mutex));
1655f14fb602SLionel Sambuc 
1656f14fb602SLionel Sambuc 	sigprocmask(SIG_SETMASK, mask, NULL);
1657f14fb602SLionel Sambuc }
1658f14fb602SLionel Sambuc #endif /* !defined(__minix) */
1659