Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.c revision 1.119
      1 /*	$NetBSD: rtld.c,v 1.119 2008/07/23 18:16:42 christos 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.119 2008/07/23 18:16:42 christos 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 char           *_rtld_path = _PATH_RTLD;
     89 Elf_Sym         _rtld_sym_zero;	/* For resolving undefined weak refs. */
     90 int		_rtld_pagesz;	/* Page size, as provided by kernel */
     91 
     92 Search_Path    *_rtld_default_paths;
     93 Search_Path    *_rtld_paths;
     94 
     95 Library_Xform  *_rtld_xforms;
     96 
     97 /*
     98  * Global declarations normally provided by crt0.
     99  */
    100 char           *__progname;
    101 char          **environ;
    102 
    103 #if defined(RTLD_DEBUG)
    104 #ifndef __sh__
    105 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    106 #else  /* 32-bit SuperH */
    107 register Elf_Addr *_GLOBAL_OFFSET_TABLE_ asm("r12");
    108 #endif
    109 #endif /* RTLD_DEBUG */
    110 extern Elf_Dyn  _DYNAMIC;
    111 
    112 static void _rtld_call_fini_functions(int);
    113 static void _rtld_call_init_functions(void);
    114 static void _rtld_initlist_visit(Objlist *, Obj_Entry *, int);
    115 static void _rtld_initlist_tsort(Objlist *, int);
    116 static Obj_Entry *_rtld_dlcheck(void *);
    117 static void _rtld_init_dag(Obj_Entry *);
    118 static void _rtld_init_dag1(Obj_Entry *, Obj_Entry *);
    119 static void _rtld_objlist_remove(Objlist *, Obj_Entry *);
    120 static void _rtld_objlist_clear(Objlist *);
    121 static void _rtld_unload_object(Obj_Entry *, bool);
    122 static void _rtld_unref_dag(Obj_Entry *);
    123 static Obj_Entry *_rtld_obj_from_addr(const void *);
    124 
    125 static void
    126 _rtld_call_fini_functions(int force)
    127 {
    128 	Objlist_Entry *elm;
    129 	Objlist finilist;
    130 	Obj_Entry *obj;
    131 
    132 	dbg(("_rtld_call_fini_functions(%d)", force));
    133 
    134 	SIMPLEQ_INIT(&finilist);
    135 	_rtld_initlist_tsort(&finilist, 1);
    136 
    137 	/* First pass: objects _not_ marked with DF_1_INITFIRST. */
    138 	SIMPLEQ_FOREACH(elm, &finilist, link) {
    139 		obj = elm->obj;
    140 		if (obj->refcount > 0 && !force) {
    141 			continue;
    142 		}
    143 		if (obj->fini == NULL || obj->fini_called || obj->initfirst) {
    144 		    	continue;
    145 		}
    146 		dbg (("calling fini function %s at %p",  obj->path,
    147 		    (void *)obj->fini));
    148 		obj->fini_called = 1;
    149 		(*obj->fini)();
    150 	}
    151 
    152 	/* Second pass: objects marked with DF_1_INITFIRST. */
    153 	SIMPLEQ_FOREACH(elm, &finilist, link) {
    154 		obj = elm->obj;
    155 		if (obj->refcount > 0 && !force) {
    156 			continue;
    157 		}
    158 		if (obj->fini == NULL || obj->fini_called) {
    159 		    	continue;
    160 		}
    161 		dbg (("calling fini function %s at %p (DF_1_INITFIRST)",
    162 		    obj->path, (void *)obj->fini));
    163 		obj->fini_called = 1;
    164 		(*obj->fini)();
    165 	}
    166 
    167         _rtld_objlist_clear(&finilist);
    168 }
    169 
    170 static void
    171 _rtld_call_init_functions()
    172 {
    173 	Objlist_Entry *elm;
    174 	Objlist initlist;
    175 	Obj_Entry *obj;
    176 
    177 	dbg(("_rtld_call_init_functions()"));
    178 	SIMPLEQ_INIT(&initlist);
    179 	_rtld_initlist_tsort(&initlist, 0);
    180 
    181 	/* First pass: objects marked with DF_1_INITFIRST. */
    182 	SIMPLEQ_FOREACH(elm, &initlist, link) {
    183 		obj = elm->obj;
    184 		if (obj->init == NULL || obj->init_called || !obj->initfirst) {
    185 			continue;
    186 		}
    187 		dbg (("calling init function %s at %p (DF_1_INITFIRST)",
    188 		    obj->path, (void *)obj->init));
    189 		obj->init_called = 1;
    190 		(*obj->init)();
    191 	}
    192 
    193 	/* Second pass: all other objects. */
    194 	SIMPLEQ_FOREACH(elm, &initlist, link) {
    195 		obj = elm->obj;
    196 		if (obj->init == NULL || obj->init_called) {
    197 			continue;
    198 		}
    199 		dbg (("calling init function %s at %p",  obj->path,
    200 		    (void *)obj->init));
    201 		obj->init_called = 1;
    202 		(*obj->init)();
    203 	}
    204 
    205         _rtld_objlist_clear(&initlist);
    206 }
    207 
    208 /*
    209  * Initialize the dynamic linker.  The argument is the address at which
    210  * the dynamic linker has been mapped into memory.  The primary task of
    211  * this function is to create an Obj_Entry for the dynamic linker and
    212  * to resolve the PLT relocation for platforms that need it (those that
    213  * define __HAVE_FUNCTION_DESCRIPTORS
    214  */
    215 static void
    216 _rtld_init(caddr_t mapbase, caddr_t relocbase, const char *execname)
    217 {
    218 
    219 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
    220 	_rtld_objself.path = _rtld_path;
    221 	_rtld_objself.pathlen = strlen(_rtld_path);
    222 	_rtld_objself.rtld = true;
    223 	_rtld_objself.mapbase = mapbase;
    224 	_rtld_objself.relocbase = relocbase;
    225 	_rtld_objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
    226 	_rtld_objself.strtab = "_rtld_sym_zero";
    227 
    228 	_rtld_digest_dynamic(_rtld_path, &_rtld_objself);
    229 	assert(!_rtld_objself.needed);
    230 #if !defined(__hppa__)
    231 	assert(!_rtld_objself.pltrel && !_rtld_objself.pltrela);
    232 #else
    233 	_rtld_relocate_plt_objects(&_rtld_objself);
    234 #endif
    235 #if !defined(__mips__) && !defined(__hppa__)
    236 	assert(!_rtld_objself.pltgot);
    237 #endif
    238 #if !defined(__arm__) && !defined(__mips__) && !defined(__sh__)
    239 	/* ARM, MIPS and SH{3,5} have a bogus DT_TEXTREL. */
    240 	assert(!_rtld_objself.textrel);
    241 #endif
    242 
    243 	_rtld_add_paths(execname, &_rtld_default_paths,
    244 	    RTLD_DEFAULT_LIBRARY_PATH);
    245 
    246 	/*
    247 	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
    248 	 */
    249 	_rtld_objlist = &_rtld_objself;
    250 
    251 	/* Make the object list empty again. */
    252 	_rtld_objlist = NULL;
    253 	_rtld_objtail = &_rtld_objlist;
    254 
    255 	_rtld_debug.r_brk = _rtld_debug_state;
    256 	_rtld_debug.r_state = RT_CONSISTENT;
    257 }
    258 
    259 /*
    260  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
    261  * before the process exits.
    262  */
    263 static void
    264 _rtld_exit(void)
    265 {
    266 	dbg(("rtld_exit()"));
    267 
    268 	_rtld_call_fini_functions(1);
    269 }
    270 
    271 /*
    272  * Main entry point for dynamic linking.  The argument is the stack
    273  * pointer.  The stack is expected to be laid out as described in the
    274  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
    275  * the stack pointer points to a word containing ARGC.  Following that
    276  * in the stack is a null-terminated sequence of pointers to argument
    277  * strings.  Then comes a null-terminated sequence of pointers to
    278  * environment strings.  Finally, there is a sequence of "auxiliary
    279  * vector" entries.
    280  *
    281  * This function returns the entry point for the main program, the dynamic
    282  * linker's exit procedure in sp[0], and a pointer to the main object in
    283  * sp[1].
    284  */
    285 Elf_Addr
    286 _rtld(Elf_Addr *sp, Elf_Addr relocbase)
    287 {
    288 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
    289 	               *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
    290 		       *pAUX_ruid, *pAUX_rgid;
    291 	const AuxInfo  *pAUX_pagesz;
    292 	char          **env;
    293 	const AuxInfo  *aux;
    294 	const AuxInfo  *auxp;
    295 	Elf_Addr       *const osp = sp;
    296 	bool            bind_now = 0;
    297 	const char     *ld_bind_now;
    298 	const char    **argv;
    299 	const char     *execname;
    300 	long		argc;
    301 	const char **real___progname;
    302 	const Obj_Entry **real___mainprog_obj;
    303 	char ***real_environ;
    304 #if defined(RTLD_DEBUG)
    305 	int i = 0;
    306 #endif
    307 
    308 	/*
    309          * On entry, the dynamic linker itself has not been relocated yet.
    310          * Be very careful not to reference any global data until after
    311          * _rtld_init has returned.  It is OK to reference file-scope statics
    312          * and string constants, and to call static and global functions.
    313          */
    314 	/* Find the auxiliary vector on the stack. */
    315 	/* first Elf_Word reserved to address of exit routine */
    316 #if defined(RTLD_DEBUG)
    317 	debug = 1;
    318 	dbg(("sp = %p, argc = %ld, argv = %p <%s> relocbase %p", sp,
    319 	    (long)sp[2], &sp[3], (char *) sp[3], (void *)relocbase));
    320 	dbg(("got is at %p, dynamic is at %p", _GLOBAL_OFFSET_TABLE_,
    321 	    &_DYNAMIC));
    322 	dbg(("_ctype_ is %p", _ctype_));
    323 #endif
    324 
    325 	sp += 2;		/* skip over return argument space */
    326 	argv = (const char **) &sp[1];
    327 	argc = *(long *)sp;
    328 	sp += 2 + argc;		/* Skip over argc, arguments, and NULL
    329 				 * terminator */
    330 	env = (char **) sp;
    331 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
    332 #if defined(RTLD_DEBUG)
    333 		dbg(("env[%d] = %p %s", i++, (void *)sp[-1], (char *)sp[-1]));
    334 #endif
    335 	}
    336 	aux = (const AuxInfo *) sp;
    337 
    338 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
    339 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
    340 	pAUX_euid = pAUX_ruid = pAUX_egid = pAUX_rgid = NULL;
    341 	pAUX_pagesz = NULL;
    342 
    343 	execname = NULL;
    344 
    345 	/* Digest the auxiliary vector. */
    346 	for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
    347 		switch (auxp->a_type) {
    348 		case AT_BASE:
    349 			pAUX_base = auxp;
    350 			break;
    351 		case AT_ENTRY:
    352 			pAUX_entry = auxp;
    353 			break;
    354 		case AT_EXECFD:
    355 			pAUX_execfd = auxp;
    356 			break;
    357 		case AT_PHDR:
    358 			pAUX_phdr = auxp;
    359 			break;
    360 		case AT_PHENT:
    361 			pAUX_phent = auxp;
    362 			break;
    363 		case AT_PHNUM:
    364 			pAUX_phnum = auxp;
    365 			break;
    366 #ifdef AT_EUID
    367 		case AT_EUID:
    368 			pAUX_euid = auxp;
    369 			break;
    370 		case AT_RUID:
    371 			pAUX_ruid = auxp;
    372 			break;
    373 		case AT_EGID:
    374 			pAUX_egid = auxp;
    375 			break;
    376 		case AT_RGID:
    377 			pAUX_rgid = auxp;
    378 			break;
    379 #endif
    380 #ifdef AT_SUN_EXECNAME
    381 		case AT_SUN_EXECNAME:
    382 			execname = (const char *)(const void *)auxp->a_v;
    383 			break;
    384 #endif
    385 		case AT_PAGESZ:
    386 			pAUX_pagesz = auxp;
    387 			break;
    388 		}
    389 	}
    390 
    391 	/* Initialize and relocate ourselves. */
    392 	if (pAUX_base == NULL) {
    393 		_rtld_error("Bad pAUX_base");
    394 		_rtld_die();
    395 	}
    396 	assert(pAUX_pagesz != NULL);
    397 	_rtld_pagesz = (int)pAUX_pagesz->a_v;
    398 	_rtld_init((caddr_t)pAUX_base->a_v, (caddr_t)relocbase, execname);
    399 
    400 	__progname = _rtld_objself.path;
    401 	environ = env;
    402 
    403 	_rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
    404 	    (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
    405 	    ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
    406 	    (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
    407 
    408 	ld_bind_now = getenv("LD_BIND_NOW");
    409 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
    410 		bind_now = true;
    411 	if (_rtld_trust) {
    412 #ifdef DEBUG
    413 		const char     *ld_debug = getenv("LD_DEBUG");
    414 #ifdef RTLD_DEBUG
    415 		debug = 0;
    416 #endif
    417 		if (ld_debug != NULL && *ld_debug != '\0')
    418 			debug = 1;
    419 #endif
    420 		_rtld_add_paths(execname, &_rtld_paths,
    421 		    getenv("LD_LIBRARY_PATH"));
    422 	} else {
    423 		execname = NULL;
    424 		unsetenv("LD_DEBUG");
    425 		unsetenv("LD_LIBRARY_PATH");
    426 	}
    427 	_rtld_process_hints(execname, &_rtld_paths, &_rtld_xforms,
    428 	    _PATH_LD_HINTS);
    429 	dbg(("dynamic linker is initialized, mapbase=%p, relocbase=%p",
    430 	     _rtld_objself.mapbase, _rtld_objself.relocbase));
    431 
    432 	/*
    433          * Load the main program, or process its program header if it is
    434          * already loaded.
    435          */
    436 	if (pAUX_execfd != NULL) {	/* Load the main program. */
    437 		int             fd = pAUX_execfd->a_v;
    438 		const char *obj_name = argv[0] ? argv[0] : "main program";
    439 		dbg(("loading main program"));
    440 		_rtld_objmain = _rtld_map_object(obj_name, fd, NULL);
    441 		close(fd);
    442 		if (_rtld_objmain == NULL)
    443 			_rtld_die();
    444 	} else {		/* Main program already loaded. */
    445 		const Elf_Phdr *phdr;
    446 		int             phnum;
    447 		caddr_t         entry;
    448 
    449 		dbg(("processing main program's program header"));
    450 		assert(pAUX_phdr != NULL);
    451 		phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
    452 		assert(pAUX_phnum != NULL);
    453 		phnum = pAUX_phnum->a_v;
    454 		assert(pAUX_phent != NULL);
    455 		assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
    456 		assert(pAUX_entry != NULL);
    457 		entry = (caddr_t) pAUX_entry->a_v;
    458 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
    459 		_rtld_objmain->path = xstrdup(argv[0] ? argv[0] :
    460 		    "main program");
    461 		_rtld_objmain->pathlen = strlen(_rtld_objmain->path);
    462 	}
    463 
    464 	_rtld_objmain->mainprog = true;
    465 
    466 	/*
    467 	 * Get the actual dynamic linker pathname from the executable if
    468 	 * possible.  (It should always be possible.)  That ensures that
    469 	 * gdb will find the right dynamic linker even if a non-standard
    470 	 * one is being used.
    471 	 */
    472 	if (_rtld_objmain->interp != NULL &&
    473 	    strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0)
    474 		_rtld_objself.path = xstrdup(_rtld_objmain->interp);
    475 	dbg(("actual dynamic linker is %s", _rtld_objself.path));
    476 
    477 	_rtld_digest_dynamic(execname, _rtld_objmain);
    478 
    479 	/* Link the main program into the list of objects. */
    480 	*_rtld_objtail = _rtld_objmain;
    481 	_rtld_objtail = &_rtld_objmain->next;
    482 
    483 	_rtld_linkmap_add(_rtld_objmain);
    484 	_rtld_linkmap_add(&_rtld_objself);
    485 
    486 	++_rtld_objmain->refcount;
    487 	_rtld_objmain->mainref = 1;
    488 	_rtld_objlist_push_tail(&_rtld_list_main, _rtld_objmain);
    489 
    490 	/* Initialize a fake symbol for resolving undefined weak references. */
    491 	_rtld_sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
    492 	_rtld_sym_zero.st_shndx = SHN_ABS;
    493 
    494 	if (_rtld_trust) {
    495 		/*
    496 		 * Pre-load user-specified objects after the main program
    497 		 * but before any shared object dependencies.
    498 		 */
    499 		dbg(("preloading objects"));
    500 		if (_rtld_preload(getenv("LD_PRELOAD")) == -1)
    501 			_rtld_die();
    502 	} else
    503 		unsetenv("LD_PRELOAD");
    504 
    505 	dbg(("loading needed objects"));
    506 	if (_rtld_load_needed_objects(_rtld_objmain, RTLD_MAIN) == -1)
    507 		_rtld_die();
    508 
    509 	dbg(("relocating objects"));
    510 	if (_rtld_relocate_objects(_rtld_objmain, bind_now) == -1)
    511 		_rtld_die();
    512 
    513 	dbg(("doing copy relocations"));
    514 	if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
    515 		_rtld_die();
    516 
    517 	/*
    518 	 * Set the __progname,  environ and, __mainprog_obj before
    519 	 * calling anything that might use them.
    520 	 */
    521 	real___progname = _rtld_objmain_sym("__progname");
    522 	if (real___progname) {
    523 		if (argv[0] != NULL) {
    524 			if ((*real___progname = strrchr(argv[0], '/')) == NULL)
    525 				(*real___progname) = argv[0];
    526 			else
    527 				(*real___progname)++;
    528 		} else {
    529 			(*real___progname) = NULL;
    530 		}
    531 	}
    532 	real_environ = _rtld_objmain_sym("environ");
    533 	if (real_environ)
    534 		*real_environ = environ;
    535 	/*
    536 	 * Set __mainprog_obj for old binaries.
    537 	 */
    538 	real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
    539 	if (real___mainprog_obj)
    540 		*real___mainprog_obj = _rtld_objmain;
    541 
    542 	dbg(("calling _init functions"));
    543 	_rtld_call_init_functions();
    544 
    545 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
    546 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
    547 
    548 	/*
    549 	 * Return with the entry point and the exit procedure in at the top
    550 	 * of stack.
    551 	 */
    552 
    553 	_rtld_debug_state();	/* say hello to gdb! */
    554 
    555 	((void **) osp)[0] = _rtld_exit;
    556 	((void **) osp)[1] = _rtld_objmain;
    557 	return (Elf_Addr) _rtld_objmain->entry;
    558 }
    559 
    560 void
    561 _rtld_die(void)
    562 {
    563 	const char *msg = dlerror();
    564 
    565 	if (msg == NULL)
    566 		msg = "Fatal error";
    567 	xerrx(1, "%s", msg);
    568 }
    569 
    570 static Obj_Entry *
    571 _rtld_dlcheck(void *handle)
    572 {
    573 	Obj_Entry *obj;
    574 
    575 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
    576 		if (obj == (Obj_Entry *) handle)
    577 			break;
    578 
    579 	if (obj == NULL || obj->dl_refcount == 0) {
    580 		xwarnx("Invalid shared object handle %p", handle);
    581 		return NULL;
    582 	}
    583 	return obj;
    584 }
    585 
    586 static void
    587 _rtld_initlist_visit(Objlist* list, Obj_Entry *obj, int rev)
    588 {
    589 	Needed_Entry* elm;
    590 
    591 	/* dbg(("_rtld_initlist_visit(%s)", obj->path)); */
    592 
    593 	if (obj->init_done)
    594 		return;
    595 	obj->init_done = 1;
    596 
    597 	for (elm = obj->needed; elm != NULL; elm = elm->next) {
    598 		if (elm->obj != NULL) {
    599 			_rtld_initlist_visit(list, elm->obj, rev);
    600 		}
    601 	}
    602 
    603 	if (rev) {
    604 		_rtld_objlist_push_head(list, obj);
    605 	} else {
    606 		_rtld_objlist_push_tail(list, obj);
    607 	}
    608 }
    609 
    610 static void
    611 _rtld_initlist_tsort(Objlist* list, int rev)
    612 {
    613 	dbg(("_rtld_initlist_tsort"));
    614 
    615 	Obj_Entry* obj;
    616 
    617 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
    618 		obj->init_done = 0;
    619 	}
    620 
    621 	for (obj = _rtld_objlist->next; obj; obj = obj->next) {
    622 		_rtld_initlist_visit(list, obj, rev);
    623 	}
    624 }
    625 
    626 static void
    627 _rtld_init_dag(Obj_Entry *root)
    628 {
    629 
    630 	_rtld_init_dag1(root, root);
    631 }
    632 
    633 static void
    634 _rtld_init_dag1(Obj_Entry *root, Obj_Entry *obj)
    635 {
    636 	const Needed_Entry *needed;
    637 
    638 	if (!obj->mainref) {
    639 		if (_rtld_objlist_find(&obj->dldags, root))
    640 			return;
    641 		rdbg(("add %p (%s) to %p (%s) DAG", obj, obj->path, root,
    642 		    root->path));
    643 		_rtld_objlist_push_tail(&obj->dldags, root);
    644 		_rtld_objlist_push_tail(&root->dagmembers, obj);
    645 	}
    646 	for (needed = obj->needed; needed != NULL; needed = needed->next)
    647 		if (needed->obj != NULL)
    648 			_rtld_init_dag1(root, needed->obj);
    649 }
    650 
    651 /*
    652  * Note, this is called only for objects loaded by dlopen().
    653  */
    654 static void
    655 _rtld_unload_object(Obj_Entry *root, bool do_fini_funcs)
    656 {
    657 
    658 	_rtld_unref_dag(root);
    659 	if (root->refcount == 0) { /* We are finished with some objects. */
    660 		Obj_Entry *obj;
    661 		Obj_Entry **linkp;
    662 		Objlist_Entry *elm;
    663 
    664 		/* Finalize objects that are about to be unmapped. */
    665 		if (do_fini_funcs)
    666 			_rtld_call_fini_functions(0);
    667 
    668 		/* Remove the DAG from all objects' DAG lists. */
    669 		SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
    670 			_rtld_objlist_remove(&elm->obj->dldags, root);
    671 
    672 		/* Remove the DAG from the RTLD_GLOBAL list. */
    673 		if (root->globalref) {
    674 			root->globalref = 0;
    675 			_rtld_objlist_remove(&_rtld_list_global, root);
    676 		}
    677 
    678 		/* Unmap all objects that are no longer referenced. */
    679 		linkp = &_rtld_objlist->next;
    680 		while ((obj = *linkp) != NULL) {
    681 			if (obj->refcount == 0) {
    682 #ifdef RTLD_DEBUG
    683 				dbg(("unloading \"%s\"", obj->path));
    684 #endif
    685 				if (obj->ehdr != MAP_FAILED)
    686 					munmap(obj->ehdr, _rtld_pagesz);
    687 				munmap(obj->mapbase, obj->mapsize);
    688 				_rtld_objlist_remove(&_rtld_list_global, obj);
    689 				_rtld_linkmap_delete(obj);
    690 				*linkp = obj->next;
    691 				_rtld_obj_free(obj);
    692 			} else
    693 				linkp = &obj->next;
    694 		}
    695 		_rtld_objtail = linkp;
    696 	}
    697 }
    698 
    699 static void
    700 _rtld_unref_dag(Obj_Entry *root)
    701 {
    702 
    703 	assert(root);
    704 	assert(root->refcount != 0);
    705 	--root->refcount;
    706 	if (root->refcount == 0) {
    707 		const Needed_Entry *needed;
    708 
    709 		for (needed = root->needed; needed != NULL;
    710 		     needed = needed->next) {
    711 			if (needed->obj != NULL)
    712 				_rtld_unref_dag(needed->obj);
    713 		}
    714 	}
    715 }
    716 
    717 __strong_alias(__dlclose,dlclose)
    718 int
    719 dlclose(void *handle)
    720 {
    721 	Obj_Entry *root = _rtld_dlcheck(handle);
    722 
    723 	if (root == NULL)
    724 		return -1;
    725 
    726 	_rtld_debug.r_state = RT_DELETE;
    727 	_rtld_debug_state();
    728 
    729 	--root->dl_refcount;
    730 	_rtld_unload_object(root, true);
    731 
    732 	_rtld_debug.r_state = RT_CONSISTENT;
    733 	_rtld_debug_state();
    734 
    735 	return 0;
    736 }
    737 
    738 __strong_alias(__dlerror,dlerror)
    739 char *
    740 dlerror(void)
    741 {
    742 	char *msg = error_message;
    743 
    744 	error_message = NULL;
    745 	return msg;
    746 }
    747 
    748 __strong_alias(__dlopen,dlopen)
    749 void *
    750 dlopen(const char *name, int mode)
    751 {
    752 	Obj_Entry **old_obj_tail = _rtld_objtail;
    753 	Obj_Entry *obj = NULL;
    754 
    755 	_rtld_debug.r_state = RT_ADD;
    756 	_rtld_debug_state();
    757 
    758 	if (name == NULL) {
    759 		obj = _rtld_objmain;
    760 		obj->refcount++;
    761 	} else
    762 		obj = _rtld_load_library(name, _rtld_objmain, mode);
    763 
    764 	if (obj != NULL) {
    765 		++obj->dl_refcount;
    766 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
    767 			assert(*old_obj_tail == obj);
    768 
    769 			if (_rtld_load_needed_objects(obj, mode) == -1 ||
    770 			    (_rtld_init_dag(obj),
    771 			    _rtld_relocate_objects(obj,
    772 			    ((mode & 3) == RTLD_NOW))) == -1) {
    773 				_rtld_unload_object(obj, false);
    774 				obj->dl_refcount--;
    775 				obj = NULL;
    776 			} else {
    777 				_rtld_call_init_functions();
    778 			}
    779 		}
    780 	}
    781 	_rtld_debug.r_state = RT_CONSISTENT;
    782 	_rtld_debug_state();
    783 
    784 	return obj;
    785 }
    786 
    787 /*
    788  * Find a symbol in the main program.
    789  */
    790 void *
    791 _rtld_objmain_sym(const char *name)
    792 {
    793 	unsigned long hash;
    794 	const Elf_Sym *def;
    795 	const Obj_Entry *obj;
    796 
    797 	hash = _rtld_elf_hash(name);
    798 	obj = _rtld_objmain;
    799 
    800 	def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, false);
    801 
    802 	if (def != NULL)
    803 		return obj->relocbase + def->st_value;
    804 	return(NULL);
    805 }
    806 
    807 __strong_alias(__dlsym,dlsym)
    808 void *
    809 dlsym(void *handle, const char *name)
    810 {
    811 	const Obj_Entry *obj;
    812 	unsigned long hash;
    813 	const Elf_Sym *def;
    814 	const Obj_Entry *defobj;
    815 	void *retaddr;
    816 
    817 	hash = _rtld_elf_hash(name);
    818 	def = NULL;
    819 	defobj = NULL;
    820 
    821 	switch ((intptr_t)handle) {
    822 	case (intptr_t)NULL:
    823 	case (intptr_t)RTLD_NEXT:
    824 	case (intptr_t)RTLD_DEFAULT:
    825 	case (intptr_t)RTLD_SELF:
    826 		retaddr = __builtin_return_address(0); /* __GNUC__ only */
    827 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
    828 			_rtld_error("Cannot determine caller's shared object");
    829 			return NULL;
    830 		}
    831 
    832 		switch ((intptr_t)handle) {
    833 		case (intptr_t)NULL:	 /* Just the caller's shared object. */
    834 			def = _rtld_symlook_obj(name, hash, obj, false);
    835 			defobj = obj;
    836 			break;
    837 
    838 		case (intptr_t)RTLD_NEXT:	/* Objects after callers */
    839 			obj = obj->next;
    840 			/*FALLTHROUGH*/
    841 
    842 		case (intptr_t)RTLD_SELF:	/* Caller included */
    843 			for (; obj; obj = obj->next) {
    844 				if ((def = _rtld_symlook_obj(name, hash, obj,
    845 				    false)) != NULL) {
    846 					defobj = obj;
    847 					break;
    848 				}
    849 			}
    850 			break;
    851 
    852 		case (intptr_t)RTLD_DEFAULT:
    853 			def = _rtld_symlook_default(name, hash, obj, &defobj,
    854 			    false);
    855 			break;
    856 
    857 		default:
    858 			abort();
    859 		}
    860 		break;
    861 
    862 	default:
    863 		if ((obj = _rtld_dlcheck(handle)) == NULL)
    864 			return NULL;
    865 
    866 		if (obj->mainprog) {
    867 			/* Search main program and all libraries loaded by it */
    868 			def = _rtld_symlook_list(name, hash, &_rtld_list_main,
    869 			    &defobj, false);
    870 		} else {
    871 			/*
    872 			 * XXX - This isn't correct.  The search should include
    873 			 * the whole DAG rooted at the given object.
    874 			 */
    875 			def = _rtld_symlook_obj(name, hash, obj, false);
    876 			defobj = obj;
    877 		}
    878 		break;
    879 	}
    880 
    881 	if (def != NULL) {
    882 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    883 		if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
    884 			return (void *)_rtld_function_descriptor_alloc(defobj,
    885 			    def, 0);
    886 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    887 		return defobj->relocbase + def->st_value;
    888 	}
    889 
    890 	_rtld_error("Undefined symbol \"%s\"", name);
    891 	return NULL;
    892 }
    893 
    894 __strong_alias(__dladdr,dladdr)
    895 int
    896 dladdr(const void *addr, Dl_info *info)
    897 {
    898 	const Obj_Entry *obj;
    899 	const Elf_Sym *def, *best_def;
    900 	void *symbol_addr;
    901 	unsigned long symoffset;
    902 
    903 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    904 	addr = _rtld_function_descriptor_function(addr);
    905 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    906 
    907 	obj = _rtld_obj_from_addr(addr);
    908 	if (obj == NULL) {
    909 		_rtld_error("No shared object contains address");
    910 		return 0;
    911 	}
    912 	info->dli_fname = obj->path;
    913 	info->dli_fbase = obj->mapbase;
    914 	info->dli_saddr = (void *)0;
    915 	info->dli_sname = NULL;
    916 
    917 	/*
    918 	 * Walk the symbol list looking for the symbol whose address is
    919 	 * closest to the address sent in.
    920 	 */
    921 	best_def = NULL;
    922 	for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
    923 		def = obj->symtab + symoffset;
    924 
    925 		/*
    926 		 * For skip the symbol if st_shndx is either SHN_UNDEF or
    927 		 * SHN_COMMON.
    928 		 */
    929 		if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
    930 			continue;
    931 
    932 		/*
    933 		 * If the symbol is greater than the specified address, or if it
    934 		 * is further away from addr than the current nearest symbol,
    935 		 * then reject it.
    936 		 */
    937 		symbol_addr = obj->relocbase + def->st_value;
    938 		if (symbol_addr > addr || symbol_addr < info->dli_saddr)
    939 			continue;
    940 
    941 		/* Update our idea of the nearest symbol. */
    942 		info->dli_sname = obj->strtab + def->st_name;
    943 		info->dli_saddr = symbol_addr;
    944 		best_def = def;
    945 
    946 		/* Exact match? */
    947 		if (info->dli_saddr == addr)
    948 			break;
    949 	}
    950 
    951 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    952 	if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
    953 		info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
    954 		    best_def, 0);
    955 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    956 
    957 	return 1;
    958 }
    959 
    960 /*
    961  * Error reporting function.  Use it like printf.  If formats the message
    962  * into a buffer, and sets things up so that the next call to dlerror()
    963  * will return the message.
    964  */
    965 void
    966 _rtld_error(const char *fmt,...)
    967 {
    968 	static char     buf[512];
    969 	va_list         ap;
    970 
    971 	va_start(ap, fmt);
    972 	xvsnprintf(buf, sizeof buf, fmt, ap);
    973 	error_message = buf;
    974 	va_end(ap);
    975 }
    976 
    977 void
    978 _rtld_debug_state(void)
    979 {
    980 
    981 	/* do nothing */
    982 }
    983 
    984 void
    985 _rtld_linkmap_add(Obj_Entry *obj)
    986 {
    987 	struct link_map *l = &obj->linkmap;
    988 	struct link_map *prev;
    989 
    990 	obj->linkmap.l_name = obj->path;
    991 	obj->linkmap.l_addr = obj->relocbase;
    992 	obj->linkmap.l_ld = obj->dynamic;
    993 #ifdef __mips__
    994 	/* XXX This field is not standard and will be removed eventually. */
    995 	obj->linkmap.l_offs = obj->relocbase;
    996 #endif
    997 
    998 	if (_rtld_debug.r_map == NULL) {
    999 		_rtld_debug.r_map = l;
   1000 		return;
   1001 	}
   1002 
   1003 	/*
   1004 	 * Scan to the end of the list, but not past the entry for the
   1005 	 * dynamic linker, which we want to keep at the very end.
   1006 	 */
   1007 	for (prev = _rtld_debug.r_map;
   1008 	    prev->l_next != NULL && prev->l_next != &_rtld_objself.linkmap;
   1009 	    prev = prev->l_next);
   1010 
   1011 	l->l_prev = prev;
   1012 	l->l_next = prev->l_next;
   1013 	if (l->l_next != NULL)
   1014 		l->l_next->l_prev = l;
   1015 	prev->l_next = l;
   1016 }
   1017 
   1018 void
   1019 _rtld_linkmap_delete(Obj_Entry *obj)
   1020 {
   1021 	struct link_map *l = &obj->linkmap;
   1022 
   1023 	if (l->l_prev == NULL) {
   1024 		if ((_rtld_debug.r_map = l->l_next) != NULL)
   1025 			l->l_next->l_prev = NULL;
   1026 		return;
   1027 	}
   1028 	if ((l->l_prev->l_next = l->l_next) != NULL)
   1029 		l->l_next->l_prev = l->l_prev;
   1030 }
   1031 
   1032 static Obj_Entry *
   1033 _rtld_obj_from_addr(const void *addr)
   1034 {
   1035 	Obj_Entry *obj;
   1036 
   1037 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
   1038 		if (addr < (void *) obj->mapbase)
   1039 			continue;
   1040 		if (addr < (void *) (obj->mapbase + obj->mapsize))
   1041 			return obj;
   1042 	}
   1043 	return NULL;
   1044 }
   1045 
   1046 static void
   1047 _rtld_objlist_clear(Objlist *list)
   1048 {
   1049 	while (!SIMPLEQ_EMPTY(list)) {
   1050 		Objlist_Entry* elm = SIMPLEQ_FIRST(list);
   1051 		SIMPLEQ_REMOVE_HEAD(list, link);
   1052 		xfree(elm);
   1053 	}
   1054 }
   1055 
   1056 static void
   1057 _rtld_objlist_remove(Objlist *list, Obj_Entry *obj)
   1058 {
   1059 	Objlist_Entry *elm;
   1060 
   1061 	if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
   1062 		SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
   1063 		xfree(elm);
   1064 	}
   1065 }
   1066