Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.c revision 1.54
      1 /*	$NetBSD: rtld.c,v 1.54 2002/09/05 21:57:09 mycroft Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Dynamic linker for ELF.
     36  *
     37  * John Polstra <jdp (at) polstra.com>.
     38  */
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <sys/param.h>
     49 #include <sys/mman.h>
     50 #include <dirent.h>
     51 
     52 #include <ctype.h>
     53 
     54 #include <dlfcn.h>
     55 #include "debug.h"
     56 #include "rtld.h"
     57 
     58 #if !defined(lint)
     59 #include "sysident.h"
     60 #endif
     61 
     62 #define END_SYM		"_end"
     63 
     64 /*
     65  * Debugging support.
     66  */
     67 
     68 typedef void    (*funcptr) __P((void));
     69 
     70 /*
     71  * Function declarations.
     72  */
     73 static void     _rtld_init __P((caddr_t, int));
     74 static void     _rtld_exit __P((void));
     75 
     76 Elf_Addr        _rtld __P((Elf_Addr *));
     77 
     78 
     79 /*
     80  * Data declarations.
     81  */
     82 static char    *error_message;	/* Message for dlopen(), or NULL */
     83 
     84 struct r_debug  _rtld_debug;	/* for GDB; */
     85 bool            _rtld_trust;	/* False for setuid and setgid programs */
     86 Obj_Entry      *_rtld_objlist;	/* Head of linked list of shared objects */
     87 Obj_Entry     **_rtld_objtail;	/* Link field of last object in list */
     88 Obj_Entry      *_rtld_objmain;	/* The main program shared object */
     89 Obj_Entry       _rtld_objself;	/* The dynamic linker shared object */
     90 char            _rtld_path[] = _PATH_RTLD;
     91 Elf_Sym         _rtld_sym_zero;	/* For resolving undefined weak refs. */
     92 #ifdef	VARPSZ
     93 int		_rtld_pagesz;	/* Page size, as provided by kernel */
     94 #endif
     95 
     96 Objlist _rtld_list_main =	/* Objects loaded at program startup */
     97   SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
     98 
     99 Search_Path    *_rtld_default_paths;
    100 Search_Path    *_rtld_paths;
    101 
    102 Library_Xform  *_rtld_xforms;
    103 
    104 /*
    105  * Global declarations normally provided by crt0.
    106  */
    107 char           *__progname;
    108 char          **environ;
    109 
    110 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
    111 extern Elf_Dyn  _DYNAMIC;
    112 
    113 static void _rtld_call_fini_functions __P((Obj_Entry *));
    114 static void _rtld_call_init_functions __P((Obj_Entry *));
    115 static Obj_Entry *_rtld_dlcheck __P((void *));
    116 static void _rtld_init_dag __P((Obj_Entry *));
    117 static void _rtld_init_dag1 __P((Obj_Entry *, Obj_Entry *));
    118 static void _rtld_objlist_remove __P((Objlist *, Obj_Entry *));
    119 static void _rtld_unload_object __P((Obj_Entry *, bool));
    120 static void _rtld_unref_dag __P((Obj_Entry *));
    121 static Obj_Entry *_rtld_obj_from_addr __P((const void *));
    122 
    123 static void
    124 _rtld_call_fini_functions(first)
    125 	Obj_Entry *first;
    126 {
    127 	Obj_Entry *obj;
    128 
    129 	for (obj = first; obj != NULL; obj = obj->next)
    130 		if (obj->fini != NULL)
    131 			(*obj->fini)();
    132 }
    133 
    134 static void
    135 _rtld_call_init_functions(first)
    136 	Obj_Entry *first;
    137 {
    138 	if (first != NULL) {
    139 		_rtld_call_init_functions(first->next);
    140 		if (first->init != NULL)
    141 			(*first->init)();
    142 	}
    143 }
    144 
    145 /*
    146  * Initialize the dynamic linker.  The argument is the address at which
    147  * the dynamic linker has been mapped into memory.  The primary task of
    148  * this function is to relocate the dynamic linker.
    149  */
    150 static void
    151 _rtld_init(mapbase, pagesz)
    152 	caddr_t mapbase;
    153 	int pagesz;
    154 {
    155 	Obj_Entry objself;/* The dynamic linker shared object */
    156 	const Elf_Ehdr *hdr = (Elf_Ehdr *) mapbase;
    157 #ifdef RTLD_RELOCATE_SELF
    158 	int dodebug = false;
    159 #else
    160 	int dodebug = true;
    161 #endif
    162 	int i;
    163 
    164 	memset(&objself, 0, sizeof objself);
    165 
    166 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
    167 	objself.path = NULL;
    168 	objself.rtld = true;
    169 	objself.mapbase = mapbase;
    170 	objself.phdr = (Elf_Phdr *) (mapbase + hdr->e_phoff);
    171 	for (i = 0; i < hdr->e_phnum; i++) {
    172 		if (objself.phdr[i].p_type == PT_LOAD) {
    173 #ifdef	VARPSZ
    174 			/* We can't touch _rtld_pagesz yet so we can't use round_*() */
    175 #define	_rnd_down(x)	((x) & ~((long)pagesz-1))
    176 #define	_rnd_up(x)	_rnd_down((x) + pagesz - 1)
    177 			objself.textsize = _rnd_up(objself.phdr[i].p_vaddr + objself.phdr[i].p_memsz) - _rnd_down(objself.phdr[i].p_vaddr);
    178 #undef	_rnd_down
    179 #undef	_rnd_up
    180 #else
    181 			objself.textsize = round_up(objself.phdr[i].p_vaddr + objself.phdr[i].p_memsz) - round_down(objself.phdr[i].p_vaddr);
    182 #endif
    183 			break;
    184 		}
    185 	}
    186 
    187 #if defined(__mips__)
    188 	/*
    189 	* mips and ld.so currently linked at load address,
    190 	* so no relocation needed
    191 	*/
    192 	objself.relocbase = 0;
    193 #else
    194 	objself.relocbase = mapbase;
    195 #endif
    196 
    197 	objself.pltgot = NULL;
    198 
    199 	objself.dynamic = (Elf_Dyn *) &_DYNAMIC;
    200 
    201 #ifdef RTLD_RELOCATE_SELF
    202 	/* We have not been relocated yet, so fix the dynamic address */
    203 	objself.dynamic = (Elf_Dyn *)
    204 		((u_long) mapbase + (char *) objself.dynamic);
    205 #endif				/* RTLD_RELOCATE_SELF */
    206 
    207 	_rtld_digest_dynamic(&objself);
    208 
    209 #ifdef __alpha__
    210 	/* XXX XXX XXX */
    211 	objself.pltgot = NULL;
    212 #endif
    213 	assert(objself.needed == NULL);
    214 
    215 #if !defined(__arm__) && !defined(__mips__) && !defined(__i386__) && \
    216     !defined(__sh__) && !defined(__vax__)
    217 	/* no relocation for mips/i386 */
    218 	assert(!objself.textrel);
    219 #endif
    220 
    221 	_rtld_relocate_objects(&objself, true, dodebug);
    222 
    223 	/*
    224 	 * Now that we relocated ourselves, we can use globals.
    225 	 */
    226 	_rtld_objself = objself;
    227 
    228 	_rtld_objself.path = _rtld_path;
    229 	_rtld_add_paths(&_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
    230 
    231 	/*
    232 	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
    233 	 */
    234 	_rtld_objlist = &_rtld_objself;
    235 
    236 	/* Make the object list empty again. */
    237 	_rtld_objlist = NULL;
    238 	_rtld_objtail = &_rtld_objlist;
    239 
    240 	_rtld_debug.r_brk = _rtld_debug_state;
    241 	_rtld_debug.r_state = RT_CONSISTENT;
    242 }
    243 
    244 /*
    245  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
    246  * before the process exits.
    247  */
    248 static void
    249 _rtld_exit()
    250 {
    251 	dbg(("rtld_exit()"));
    252 
    253 	_rtld_call_fini_functions(_rtld_objlist->next);
    254 }
    255 
    256 /*
    257  * Main entry point for dynamic linking.  The argument is the stack
    258  * pointer.  The stack is expected to be laid out as described in the
    259  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
    260  * the stack pointer points to a word containing ARGC.  Following that
    261  * in the stack is a null-terminated sequence of pointers to argument
    262  * strings.  Then comes a null-terminated sequence of pointers to
    263  * environment strings.  Finally, there is a sequence of "auxiliary
    264  * vector" entries.
    265  *
    266  * This function returns the entry point for the main program, the dynamic
    267  * linker's exit procedure in sp[0], and a pointer to the main object in
    268  * sp[1].
    269  */
    270 Elf_Addr
    271 _rtld(sp)
    272 	Elf_Addr *sp;
    273 {
    274 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
    275 	               *pAUX_phent, *pAUX_phnum, *pAUX_euid, *pAUX_egid,
    276 		       *pAUX_ruid, *pAUX_rgid;
    277 #ifdef	VARPSZ
    278 	const AuxInfo  *pAUX_pagesz;
    279 #endif
    280 	char          **env;
    281 	const AuxInfo  *aux;
    282 	const AuxInfo  *auxp;
    283 	Elf_Addr       *const osp = sp;
    284 	bool            bind_now = 0;
    285 	const char     *ld_bind_now;
    286 	const char    **argv;
    287 	long		argc;
    288 	Obj_Entry	*obj;
    289 	const char **real___progname;
    290 	const Obj_Entry **real___mainprog_obj;
    291 	char ***real_environ;
    292 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    293 	int             i = 0;
    294 #endif
    295 
    296 	/*
    297          * On entry, the dynamic linker itself has not been relocated yet.
    298          * Be very careful not to reference any global data until after
    299          * _rtld_init has returned.  It is OK to reference file-scope statics
    300          * and string constants, and to call static and global functions.
    301          */
    302 	/* Find the auxiliary vector on the stack. */
    303 	/* first Elf_Word reserved to address of exit routine */
    304 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    305 	dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
    306 	     &sp[3], (char *) sp[3]));
    307 	dbg(("got is at %p, dynamic is at %p\n",
    308 	    _GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
    309 	debug = 1;
    310 	dbg(("_ctype_ is %p\n", _ctype_));
    311 #endif
    312 
    313 	sp += 2;		/* skip over return argument space */
    314 	argv = (const char **) &sp[1];
    315 	argc = *(long *)sp;
    316 #ifdef __sparc_v9__
    317 	/* XXX Temporary hack for argc format conversion. */
    318 	argc = (argc >> 32) | (argc & 0xffffffff);
    319 #endif
    320 	sp += 2 + argc;		/* Skip over argc, arguments, and NULL
    321 				 * terminator */
    322 	env = (char **) sp;
    323 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
    324 #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    325 		dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
    326 #endif
    327 	}
    328 	aux = (const AuxInfo *) sp;
    329 
    330 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
    331 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
    332 	pAUX_euid = pAUX_ruid = pAUX_egid = pAUX_rgid = NULL;
    333 #ifdef	VARPSZ
    334 	pAUX_pagesz = NULL;
    335 #endif
    336 	/*
    337 	 * First pass through the the auxiliary vector, avoiding the use
    338 	 * of a `switch() {}' statement at this stage. A `switch()' may
    339 	 * be translated into code utilizing a jump table approach which
    340 	 * references the equivalent of a global variable. This must be
    341 	 * avoided until _rtld_init() has done its job.
    342 	 *
    343 	 * _rtld_init() only needs `pAUX_base' and possibly `pAUX_pagesz',
    344 	 * so we look for just those in this pass.
    345 	 */
    346 	for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
    347 		if (auxp->a_type == AT_BASE)
    348 			pAUX_base = auxp;
    349 #ifdef	VARPSZ
    350 		if (auxp->a_type == AT_PAGESZ)
    351 			pAUX_pagesz = auxp;
    352 #endif
    353 	}
    354 
    355 	/* Initialize and relocate ourselves. */
    356 	assert(pAUX_base != NULL);
    357 #ifdef	VARPSZ
    358 	assert(pAUX_pagesz != NULL);
    359 	_rtld_init((caddr_t) pAUX_base->a_v, (int)pAUX_pagesz->a_v);
    360 #else
    361 	_rtld_init((caddr_t) pAUX_base->a_v, 0);
    362 #endif
    363 
    364 	/* Digest the auxiliary vector (full pass now that we can afford it). */
    365 	for (auxp = aux; auxp->a_type != AT_NULL; ++auxp) {
    366 		switch (auxp->a_type) {
    367 		case AT_BASE:
    368 			pAUX_base = auxp;
    369 			break;
    370 		case AT_ENTRY:
    371 			pAUX_entry = auxp;
    372 			break;
    373 		case AT_EXECFD:
    374 			pAUX_execfd = auxp;
    375 			break;
    376 		case AT_PHDR:
    377 			pAUX_phdr = auxp;
    378 			break;
    379 		case AT_PHENT:
    380 			pAUX_phent = auxp;
    381 			break;
    382 		case AT_PHNUM:
    383 			pAUX_phnum = auxp;
    384 			break;
    385 #ifdef AT_EUID
    386 		case AT_EUID:
    387 			pAUX_euid = auxp;
    388 			break;
    389 		case AT_RUID:
    390 			pAUX_ruid = auxp;
    391 			break;
    392 		case AT_EGID:
    393 			pAUX_egid = auxp;
    394 			break;
    395 		case AT_RGID:
    396 			pAUX_rgid = auxp;
    397 			break;
    398 #endif
    399 #ifdef	VARPSZ
    400 		case AT_PAGESZ:
    401 			pAUX_pagesz = auxp;
    402 			break;
    403 #endif
    404 		}
    405 	}
    406 
    407 #ifdef	VARPSZ
    408 	_rtld_pagesz = (int)pAUX_pagesz->a_v;
    409 #endif
    410 
    411 #ifdef RTLD_DEBUG
    412 	dbg(("_ctype_ is %p\n", _ctype_));
    413 #endif
    414 
    415 	__progname = _rtld_objself.path;
    416 	environ = env;
    417 
    418 	_rtld_trust = ((pAUX_euid ? (uid_t)pAUX_euid->a_v : geteuid()) ==
    419 	    (pAUX_ruid ? (uid_t)pAUX_ruid->a_v : getuid())) &&
    420 	    ((pAUX_egid ? (gid_t)pAUX_egid->a_v : getegid()) ==
    421 	    (pAUX_rgid ? (gid_t)pAUX_rgid->a_v : getgid()));
    422 
    423 	ld_bind_now = getenv("LD_BIND_NOW");
    424 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
    425 		bind_now = true;
    426 	if (_rtld_trust) {
    427 #ifdef DEBUG
    428 		const char     *ld_debug = getenv("LD_DEBUG");
    429 		if (ld_debug != NULL && *ld_debug != '\0')
    430 			debug = 1;
    431 #endif
    432 		_rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
    433 	}
    434 	_rtld_process_hints(&_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS, true);
    435 	dbg(("%s is initialized, base address = %p", __progname,
    436 	     (void *) pAUX_base->a_v));
    437 
    438 	/*
    439          * Load the main program, or process its program header if it is
    440          * already loaded.
    441          */
    442 	if (pAUX_execfd != NULL) {	/* Load the main program. */
    443 		int             fd = pAUX_execfd->a_v;
    444 		dbg(("loading main program"));
    445 		_rtld_objmain = _rtld_map_object(argv[0], fd, NULL);
    446 		close(fd);
    447 		if (_rtld_objmain == NULL)
    448 			_rtld_die();
    449 	} else {		/* Main program already loaded. */
    450 		const Elf_Phdr *phdr;
    451 		int             phnum;
    452 		caddr_t         entry;
    453 
    454 		dbg(("processing main program's program header"));
    455 		assert(pAUX_phdr != NULL);
    456 		phdr = (const Elf_Phdr *) pAUX_phdr->a_v;
    457 		assert(pAUX_phnum != NULL);
    458 		phnum = pAUX_phnum->a_v;
    459 		assert(pAUX_phent != NULL);
    460 		assert(pAUX_phent->a_v == sizeof(Elf_Phdr));
    461 		assert(pAUX_entry != NULL);
    462 		entry = (caddr_t) pAUX_entry->a_v;
    463 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
    464 	}
    465 
    466 	if (argv[0] != NULL)
    467 		_rtld_objmain->path = xstrdup(argv[0]);
    468 	else
    469 		_rtld_objmain->path = xstrdup("main program");
    470 	_rtld_objmain->mainprog = true;
    471 
    472 	/*
    473 	 * Get the actual dynamic linker pathname from the executable if
    474 	 * possible.  (It should always be possible.)  That ensures that
    475 	 * gdb will find the right dynamic linker even if a non-standard
    476 	 * one is being used.
    477 	 */
    478 	if (_rtld_objmain->interp != NULL &&
    479 	    strcmp(_rtld_objmain->interp, _rtld_objself.path) != 0) {
    480 		free(_rtld_objself.path);
    481 		_rtld_objself.path = xstrdup(_rtld_objmain->interp);
    482 	}
    483 
    484 	_rtld_digest_dynamic(_rtld_objmain);
    485 
    486 	_rtld_linkmap_add(_rtld_objmain);
    487 	_rtld_linkmap_add(&_rtld_objself);
    488 
    489 	/* Link the main program into the list of objects. */
    490 	*_rtld_objtail = _rtld_objmain;
    491 	_rtld_objtail = &_rtld_objmain->next;
    492 	++_rtld_objmain->refcount;
    493 
    494 	/* Initialize a fake symbol for resolving undefined weak references. */
    495 	_rtld_sym_zero.st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
    496 	_rtld_sym_zero.st_shndx = SHN_ABS;
    497 
    498 	/*
    499 	 * Pre-load user-specified objects after the main program but before
    500 	 * any shared object dependencies.
    501 	 */
    502 	dbg(("preloading objects"));
    503 	if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
    504 		_rtld_die();
    505 
    506 	dbg(("loading needed objects"));
    507 	if (_rtld_load_needed_objects(_rtld_objmain, RTLD_GLOBAL, true) == -1)
    508 		_rtld_die();
    509 
    510 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next)
    511 		_rtld_objlist_add(&_rtld_list_main, obj);
    512 
    513 	dbg(("relocating objects"));
    514 	if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
    515 		_rtld_die();
    516 
    517 	dbg(("doing copy relocations"));
    518 	if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
    519 		_rtld_die();
    520 
    521 	/*
    522 	 * Set the __progname,  environ and, __mainprog_obj before
    523 	 * calling anything that might use them.
    524 	 */
    525 	real___progname = _rtld_objmain_sym("__progname");
    526 	if (real___progname) {
    527 		if ((*real___progname = strrchr(argv[0], '/')) == NULL)
    528 			(*real___progname) = argv[0];
    529 		else
    530 			(*real___progname)++;
    531 	}
    532 	real_environ = _rtld_objmain_sym("environ");
    533 	if (real_environ)
    534 		*real_environ = environ;
    535 	real___mainprog_obj = _rtld_objmain_sym("__mainprog_obj");
    536 	if (real___mainprog_obj)
    537 		*real___mainprog_obj = _rtld_objmain;
    538 
    539 	dbg(("calling _init functions"));
    540 	_rtld_call_init_functions(_rtld_objmain->next);
    541 
    542 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
    543 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
    544 
    545 	/*
    546 	 * Return with the entry point and the exit procedure in at the top
    547 	 * of stack.
    548 	 */
    549 
    550 	_rtld_debug_state();	/* say hello to gdb! */
    551 
    552 	((void **) osp)[0] = _rtld_exit;
    553 	((void **) osp)[1] = _rtld_objmain;
    554 	return (Elf_Addr) _rtld_objmain->entry;
    555 }
    556 
    557 void
    558 _rtld_die()
    559 {
    560 	const char *msg = _rtld_dlerror();
    561 
    562 	if (msg == NULL)
    563 		msg = "Fatal error";
    564 	xerrx(1, "%s", msg);
    565 }
    566 
    567 static Obj_Entry *
    568 _rtld_dlcheck(handle)
    569 	void *handle;
    570 {
    571 	Obj_Entry *obj;
    572 
    573 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
    574 		if (obj == (Obj_Entry *) handle)
    575 			break;
    576 
    577 	if (obj == NULL || obj->dl_refcount == 0) {
    578 		xwarnx("Invalid shared object handle %p", handle);
    579 		return NULL;
    580 	}
    581 	return obj;
    582 }
    583 
    584 static void
    585 _rtld_init_dag(root)
    586 	Obj_Entry *root;
    587 {
    588 	_rtld_init_dag1(root, root);
    589 }
    590 
    591 static void
    592 _rtld_init_dag1(root, obj)
    593 	Obj_Entry *root;
    594 	Obj_Entry *obj;
    595 {
    596 	const Needed_Entry *needed;
    597 
    598 	_rtld_objlist_add(&obj->dldags, root);
    599 	_rtld_objlist_add(&root->dagmembers, obj);
    600 	for (needed = obj->needed; needed != NULL; needed = needed->next)
    601 		if (needed->obj != NULL)
    602 			_rtld_init_dag1(root, needed->obj);
    603 }
    604 
    605 /*
    606  * Note, this is called only for objects loaded by dlopen().
    607  */
    608 static void
    609 _rtld_unload_object(root, do_fini_funcs)
    610 	Obj_Entry *root;
    611 	bool do_fini_funcs;
    612 {
    613 	_rtld_unref_dag(root);
    614 	if (root->refcount == 0) { /* We are finished with some objects. */
    615 		Obj_Entry *obj;
    616 		Obj_Entry **linkp;
    617 		Objlist_Entry *elm;
    618 
    619 		/* Finalize objects that are about to be unmapped. */
    620 		if (do_fini_funcs)
    621 			for (obj = _rtld_objlist->next;  obj != NULL;  obj = obj->next)
    622 				if (obj->refcount == 0 && obj->fini != NULL)
    623 					(*obj->fini)();
    624 
    625 		/* Remove the DAG from all objects' DAG lists. */
    626 		SIMPLEQ_FOREACH(elm, &root->dagmembers, link)
    627 			_rtld_objlist_remove(&elm->obj->dldags, root);
    628 
    629 		/* Remove the DAG from the RTLD_GLOBAL list. */
    630 		_rtld_objlist_remove(&_rtld_list_global, root);
    631 
    632 		/* Unmap all objects that are no longer referenced. */
    633 		linkp = &_rtld_objlist->next;
    634 		while ((obj = *linkp) != NULL) {
    635 			if (obj->refcount == 0) {
    636 #ifdef RTLD_DEBUG
    637 				dbg(("unloading \"%s\"", obj->path));
    638 #endif
    639 				munmap(obj->mapbase, obj->mapsize);
    640 				_rtld_objlist_remove(&_rtld_list_global, obj);
    641 				_rtld_linkmap_delete(obj);
    642 				*linkp = obj->next;
    643 				_rtld_obj_free(obj);
    644 			} else
    645 				linkp = &obj->next;
    646 		}
    647 		_rtld_objtail = linkp;
    648 	}
    649 }
    650 
    651 static void
    652 _rtld_unref_dag(root)
    653 	Obj_Entry *root;
    654 {
    655 	assert(root);
    656 	assert(root->refcount != 0);
    657 	--root->refcount;
    658 	if (root->refcount == 0) {
    659 		const Needed_Entry *needed;
    660 
    661 		for (needed = root->needed; needed != NULL;
    662 		     needed = needed->next) {
    663 			if (needed->obj != NULL)
    664 				_rtld_unref_dag(needed->obj);
    665 		}
    666 	}
    667 }
    668 
    669 int
    670 _rtld_dlclose(handle)
    671 	void *handle;
    672 {
    673 	Obj_Entry *root = _rtld_dlcheck(handle);
    674 
    675 	if (root == NULL)
    676 		return -1;
    677 
    678 	_rtld_debug.r_state = RT_DELETE;
    679 	_rtld_debug_state();
    680 
    681 	--root->dl_refcount;
    682 	_rtld_unload_object(root, true);
    683 
    684 	_rtld_debug.r_state = RT_CONSISTENT;
    685 	_rtld_debug_state();
    686 
    687 	return 0;
    688 }
    689 
    690 char *
    691 _rtld_dlerror()
    692 {
    693 	char *msg = error_message;
    694 	error_message = NULL;
    695 	return msg;
    696 }
    697 
    698 void *
    699 _rtld_dlopen(name, mode)
    700 	const char *name;
    701 	int mode;
    702 {
    703 	Obj_Entry **old_obj_tail = _rtld_objtail;
    704 	Obj_Entry *obj = NULL;
    705 
    706 	_rtld_debug.r_state = RT_ADD;
    707 	_rtld_debug_state();
    708 
    709 	if (name == NULL) {
    710 		obj = _rtld_objmain;
    711 		obj->refcount++;
    712 	} else {
    713 		char *path = _rtld_find_library(name, _rtld_objmain);
    714 		if (path != NULL)
    715 			obj = _rtld_load_object(path, mode, true);
    716 	}
    717 
    718 	if (obj != NULL) {
    719 		++obj->dl_refcount;
    720 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
    721 			assert(*old_obj_tail == obj);
    722 
    723 			if (_rtld_load_needed_objects(obj, mode, true) == -1 ||
    724 			    (_rtld_init_dag(obj),
    725 			    _rtld_relocate_objects(obj,
    726 			    ((mode & 3) == RTLD_NOW), true)) == -1) {
    727 				_rtld_unload_object(obj, false);
    728 				obj->dl_refcount--;
    729 				obj = NULL;
    730 			} else
    731 				_rtld_call_init_functions(obj);
    732 		}
    733 	}
    734 	_rtld_debug.r_state = RT_CONSISTENT;
    735 	_rtld_debug_state();
    736 
    737 	return obj;
    738 }
    739 
    740 /*
    741  * Find a symbol in the main program.
    742  */
    743 void *
    744 _rtld_objmain_sym(name)
    745 	const char *name;
    746 {
    747 	unsigned long hash;
    748 	const Elf_Sym *def;
    749 	const Obj_Entry *obj;
    750 
    751 	hash = _rtld_elf_hash(name);
    752 	obj = _rtld_objmain;
    753 
    754 	def = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, true);
    755 
    756 	if (def != NULL)
    757 		return obj->relocbase + def->st_value;
    758 	return(NULL);
    759 }
    760 
    761 void *
    762 _rtld_dlsym(handle, name)
    763 	void *handle;
    764 	const char *name;
    765 {
    766 	const Obj_Entry *obj;
    767 	unsigned long hash;
    768 	const Elf_Sym *def;
    769 	const Obj_Entry *defobj;
    770 
    771 	hash = _rtld_elf_hash(name);
    772 	def = NULL;
    773 	defobj = NULL;
    774 
    775 	if (handle == NULL
    776 #if 0
    777 	    || handle == RTLD_NEXT
    778 #endif
    779 	) {
    780 		void *retaddr;
    781 
    782 		retaddr = __builtin_return_address(0); /* __GNUC__ only */
    783 		if ((obj = _rtld_obj_from_addr(retaddr)) == NULL) {
    784 			_rtld_error("Cannot determine caller's shared object");
    785 			return NULL;
    786 		}
    787 		if (handle == NULL) { /* Just the caller's shared object. */
    788 			def = _rtld_symlook_obj(name, hash, obj, true);
    789 			defobj = obj;
    790 		} else { /* All the shared objects after the caller's */
    791 			while ((obj = obj->next) != NULL) {
    792 				if ((def = _rtld_symlook_obj(name, hash, obj, true)) != NULL) {
    793 					defobj = obj;
    794 					break;
    795 				}
    796 			}
    797 		}
    798 	} else {
    799 		if ((obj = _rtld_dlcheck(handle)) == NULL)
    800 			return NULL;
    801 
    802 		if (obj->mainprog) {
    803 			/* Search main program and all libraries loaded by it. */
    804 			def = _rtld_symlook_list(name, hash, &_rtld_list_main, &defobj, true);
    805 		} else {
    806 			/*
    807 			 * XXX - This isn't correct.  The search should include the whole
    808 			 * DAG rooted at the given object.
    809 			 */
    810 			def = _rtld_symlook_obj(name, hash, obj, true);
    811 			defobj = obj;
    812 		}
    813 	}
    814 
    815 	if (def != NULL) {
    816 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    817 		if (ELF_ST_TYPE(def->st_info) == STT_FUNC)
    818 			return (void *)_rtld_function_descriptor_alloc(defobj,
    819 			    def, 0);
    820 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    821 		return defobj->relocbase + def->st_value;
    822 	}
    823 
    824 	_rtld_error("Undefined symbol \"%s\"", name);
    825 	return NULL;
    826 }
    827 
    828 int
    829 _rtld_dladdr(addr, info)
    830 	const void *addr;
    831 	Dl_info *info;
    832 {
    833 	const Obj_Entry *obj;
    834 	const Elf_Sym *def, *best_def;
    835 	void *symbol_addr;
    836 	unsigned long symoffset;
    837 
    838 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    839 	addr = _rtld_function_descriptor_function(addr);
    840 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    841 
    842 	obj = _rtld_obj_from_addr(addr);
    843 	if (obj == NULL) {
    844 		_rtld_error("No shared object contains address");
    845 		return 0;
    846 	}
    847 	info->dli_fname = obj->path;
    848 	info->dli_fbase = obj->mapbase;
    849 	info->dli_saddr = (void *)0;
    850 	info->dli_sname = NULL;
    851 
    852 	/*
    853 	 * Walk the symbol list looking for the symbol whose address is
    854 	 * closest to the address sent in.
    855 	 */
    856 	best_def = NULL;
    857 	for (symoffset = 0; symoffset < obj->nchains; symoffset++) {
    858 		def = obj->symtab + symoffset;
    859 
    860 		/*
    861 		 * For skip the symbol if st_shndx is either SHN_UNDEF or
    862 		 * SHN_COMMON.
    863 		 */
    864 		if (def->st_shndx == SHN_UNDEF || def->st_shndx == SHN_COMMON)
    865 			continue;
    866 
    867 		/*
    868 		 * If the symbol is greater than the specified address, or if it
    869 		 * is further away from addr than the current nearest symbol,
    870 		 * then reject it.
    871 		 */
    872 		symbol_addr = obj->relocbase + def->st_value;
    873 		if (symbol_addr > addr || symbol_addr < info->dli_saddr)
    874 			continue;
    875 
    876 		/* Update our idea of the nearest symbol. */
    877 		info->dli_sname = obj->strtab + def->st_name;
    878 		info->dli_saddr = symbol_addr;
    879 		best_def = def;
    880 
    881 		/* Exact match? */
    882 		if (info->dli_saddr == addr)
    883 			break;
    884 	}
    885 
    886 #ifdef __HAVE_FUNCTION_DESCRIPTORS
    887 	if (best_def != NULL && ELF_ST_TYPE(best_def->st_info) == STT_FUNC)
    888 		info->dli_saddr = (void *)_rtld_function_descriptor_alloc(obj,
    889 		    best_def, 0);
    890 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
    891 
    892 	return 1;
    893 }
    894 
    895 /*
    896  * Error reporting function.  Use it like printf.  If formats the message
    897  * into a buffer, and sets things up so that the next call to dlerror()
    898  * will return the message.
    899  */
    900 void
    901 _rtld_error(const char *fmt,...)
    902 {
    903 	static char     buf[512];
    904 	va_list         ap;
    905 
    906 	va_start(ap, fmt);
    907 	xvsnprintf(buf, sizeof buf, fmt, ap);
    908 	error_message = buf;
    909 	va_end(ap);
    910 }
    911 
    912 void
    913 _rtld_debug_state()
    914 {
    915 	/* do nothing */
    916 }
    917 
    918 void
    919 _rtld_linkmap_add(obj)
    920 	Obj_Entry *obj;
    921 {
    922 	struct link_map *l = &obj->linkmap;
    923 	struct link_map *prev;
    924 
    925 	obj->linkmap.l_name = obj->path;
    926 	obj->linkmap.l_addr = obj->mapbase;
    927 	obj->linkmap.l_ld = obj->dynamic;
    928 #ifdef __mips__
    929 	/* GDB needs load offset on MIPS to use the symbols */
    930 	obj->linkmap.l_offs = obj->relocbase;
    931 #endif
    932 #ifdef __vax__
    933 	/* VAX shared libaries don't start at a vaddr of 0 */
    934 	obj->linkmap.l_addr -= obj->vaddrbase;
    935 #endif
    936 
    937 	if (_rtld_debug.r_map == NULL) {
    938 		_rtld_debug.r_map = l;
    939 		return;
    940 	}
    941 	for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
    942 	l->l_prev = prev;
    943 	prev->l_next = l;
    944 	l->l_next = NULL;
    945 }
    946 
    947 void
    948 _rtld_linkmap_delete(obj)
    949 	Obj_Entry *obj;
    950 {
    951 	struct link_map *l = &obj->linkmap;
    952 
    953 	if (l->l_prev == NULL) {
    954 		if ((_rtld_debug.r_map = l->l_next) != NULL)
    955 			l->l_next->l_prev = NULL;
    956 		return;
    957 	}
    958 	if ((l->l_prev->l_next = l->l_next) != NULL)
    959 		l->l_next->l_prev = l->l_prev;
    960 }
    961 
    962 static Obj_Entry *
    963 _rtld_obj_from_addr(const void *addr)
    964 {
    965 	unsigned long endhash;
    966 	Obj_Entry *obj;
    967 
    968 	endhash = _rtld_elf_hash(END_SYM);
    969 	for (obj = _rtld_objlist;  obj != NULL;  obj = obj->next) {
    970 		const Elf_Sym *endsym;
    971 
    972 		if (addr < (void *) obj->mapbase)
    973 			continue;
    974 		if ((endsym = _rtld_symlook_obj(END_SYM, endhash, obj, true)) == NULL)
    975 			continue; /* No "end" symbol?! */
    976 		if (addr < (void *) (obj->relocbase + endsym->st_value))
    977 			return obj;
    978 	}
    979 	return NULL;
    980 }
    981 
    982 static void
    983 _rtld_objlist_remove(list, obj)
    984 	Objlist *list;
    985 	Obj_Entry *obj;
    986 {
    987 	Objlist_Entry *elm;
    988 
    989 	if ((elm = _rtld_objlist_find(list, obj)) != NULL) {
    990 		SIMPLEQ_REMOVE(list, elm, Struct_Objlist_Entry, link);
    991 		free(elm);
    992 	}
    993 }
    994