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