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