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