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