Home | History | Annotate | Line # | Download | only in ld.elf_so
rtld.c revision 1.17.2.1
      1  1.17.2.1     perry /*	$NetBSD: rtld.c,v 1.17.2.1 1999/06/23 15:06:02 perry Exp $	 */
      2       1.1       cgd 
      3       1.1       cgd /*
      4       1.1       cgd  * Copyright 1996 John D. Polstra.
      5       1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6       1.1       cgd  * All rights reserved.
      7       1.1       cgd  *
      8       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9       1.1       cgd  * modification, are permitted provided that the following conditions
     10       1.1       cgd  * are met:
     11       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17       1.1       cgd  *    must display the following acknowledgement:
     18       1.1       cgd  *      This product includes software developed by John Polstra.
     19       1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20       1.1       cgd  *    derived from this software without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23       1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26       1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27       1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28       1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29       1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30       1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31       1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1       cgd  */
     33       1.1       cgd 
     34       1.1       cgd /*
     35       1.1       cgd  * Dynamic linker for ELF.
     36       1.1       cgd  *
     37       1.1       cgd  * John Polstra <jdp (at) polstra.com>.
     38       1.1       cgd  */
     39       1.1       cgd 
     40       1.1       cgd #include <err.h>
     41       1.1       cgd #include <errno.h>
     42       1.1       cgd #include <fcntl.h>
     43       1.1       cgd #include <stdarg.h>
     44       1.1       cgd #include <stdio.h>
     45       1.1       cgd #include <stdlib.h>
     46       1.1       cgd #include <string.h>
     47       1.1       cgd #include <unistd.h>
     48       1.3       cgd #include <sys/param.h>
     49       1.1       cgd #include <sys/mman.h>
     50       1.1       cgd #include <dirent.h>
     51       1.1       cgd 
     52       1.1       cgd #include <ctype.h>
     53       1.1       cgd 
     54       1.2       cgd #include <dlfcn.h>
     55       1.1       cgd #include "debug.h"
     56       1.1       cgd #include "rtld.h"
     57       1.3       cgd 
     58       1.3       cgd #include "sysident.h"
     59       1.1       cgd 
     60       1.1       cgd /*
     61       1.1       cgd  * Debugging support.
     62       1.1       cgd  */
     63       1.1       cgd 
     64      1.15  christos typedef void    (*funcptr) __P((void));
     65       1.1       cgd 
     66       1.1       cgd /*
     67       1.1       cgd  * Function declarations.
     68       1.1       cgd  */
     69      1.15  christos static void     _rtld_init __P((caddr_t));
     70      1.15  christos static void     _rtld_exit __P((void));
     71      1.15  christos 
     72      1.15  christos Elf_Addr        _rtld __P((Elf_Word *));
     73      1.15  christos 
     74       1.1       cgd 
     75       1.1       cgd /*
     76       1.1       cgd  * Data declarations.
     77       1.1       cgd  */
     78      1.15  christos static char    *error_message;	/* Message for dlopen(), or NULL */
     79       1.1       cgd 
     80      1.15  christos struct r_debug  _rtld_debug;	/* for GDB; */
     81      1.15  christos bool            _rtld_trust;	/* False for setuid and setgid programs */
     82      1.15  christos Obj_Entry      *_rtld_objlist;	/* Head of linked list of shared objects */
     83      1.15  christos Obj_Entry     **_rtld_objtail;	/* Link field of last object in list */
     84      1.15  christos Obj_Entry      *_rtld_objmain;	/* The main program shared object */
     85      1.15  christos Obj_Entry       _rtld_objself;	/* The dynamic linker shared object */
     86      1.15  christos char            _rtld_path[] = _PATH_RTLD;
     87       1.1       cgd 
     88      1.15  christos Search_Path    *_rtld_paths;
     89       1.1       cgd /*
     90       1.1       cgd  * Global declarations normally provided by crt0.
     91       1.1       cgd  */
     92      1.15  christos char           *__progname;
     93      1.15  christos char          **environ;
     94       1.1       cgd 
     95       1.9        tv #ifdef OLD_GOT
     96       1.9        tv extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
     97       1.9        tv #else
     98       1.9        tv extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
     99      1.15  christos extern Elf_Dyn  _DYNAMIC;
    100       1.1       cgd #endif
    101       1.8        tv 
    102      1.15  christos static void _rtld_call_fini_functions __P((Obj_Entry *));
    103      1.15  christos static void _rtld_call_init_functions __P((Obj_Entry *));
    104      1.15  christos static Obj_Entry *_rtld_dlcheck __P((void *));
    105      1.15  christos static void _rtld_unref_object_dag __P((Obj_Entry *));
    106      1.15  christos 
    107       1.1       cgd static void
    108      1.15  christos _rtld_call_fini_functions(first)
    109      1.15  christos 	Obj_Entry *first;
    110       1.1       cgd {
    111      1.14        pk 	Obj_Entry *obj;
    112       1.1       cgd 
    113      1.15  christos 	for (obj = first; obj != NULL; obj = obj->next)
    114      1.14        pk 		if (obj->fini != NULL)
    115      1.14        pk 			(*obj->fini)();
    116       1.1       cgd }
    117       1.1       cgd 
    118       1.1       cgd static void
    119      1.15  christos _rtld_call_init_functions(first)
    120      1.15  christos 	Obj_Entry *first;
    121       1.1       cgd {
    122      1.14        pk 	if (first != NULL) {
    123      1.14        pk 		_rtld_call_init_functions(first->next);
    124      1.14        pk 		if (first->init != NULL)
    125      1.14        pk 			(*first->init)();
    126      1.14        pk 	}
    127       1.1       cgd }
    128      1.14        pk 
    129       1.1       cgd /*
    130       1.1       cgd  * Initialize the dynamic linker.  The argument is the address at which
    131       1.1       cgd  * the dynamic linker has been mapped into memory.  The primary task of
    132       1.1       cgd  * this function is to relocate the dynamic linker.
    133       1.1       cgd  */
    134       1.1       cgd static void
    135      1.15  christos _rtld_init(mapbase)
    136      1.15  christos 	caddr_t mapbase;
    137       1.1       cgd {
    138      1.15  christos 	Obj_Entry objself;/* The dynamic linker shared object */
    139      1.13  christos #ifdef RTLD_RELOCATE_SELF
    140      1.14        pk 	int dodebug = false;
    141      1.13  christos #else
    142      1.14        pk 	int dodebug = true;
    143      1.13  christos #endif
    144       1.1       cgd 
    145      1.14        pk 	memset(&objself, 0, sizeof objself);
    146      1.14        pk 
    147      1.14        pk 	/* Conjure up an Obj_Entry structure for the dynamic linker. */
    148      1.14        pk 	objself.path = NULL;
    149      1.14        pk 	objself.rtld = true;
    150      1.14        pk 	objself.mapbase = mapbase;
    151      1.14        pk 
    152      1.13  christos #if defined(__mips__)
    153      1.14        pk 	/*
    154      1.14        pk 	* mips and ld.so currently linked at load address,
    155      1.14        pk 	* so no relocation needed
    156      1.14        pk 	*/
    157      1.14        pk 	objself.relocbase = 0;
    158       1.6    mhitch #else
    159      1.14        pk 	objself.relocbase = mapbase;
    160       1.6    mhitch #endif
    161      1.14        pk 
    162      1.14        pk 	objself.pltgot = NULL;
    163      1.14        pk 
    164       1.1       cgd #ifdef OLD_GOT
    165      1.15  christos 	objself.dynamic = (Elf_Dyn *) _GLOBAL_OFFSET_TABLE_[0];
    166       1.1       cgd #else
    167      1.15  christos 	objself.dynamic = (Elf_Dyn *) & _DYNAMIC;
    168       1.1       cgd #endif
    169       1.1       cgd 
    170      1.13  christos #ifdef RTLD_RELOCATE_SELF
    171      1.14        pk 	/* We have not been relocated yet, so fix the dynamic address */
    172      1.14        pk 	objself.dynamic = (Elf_Dyn *)
    173      1.15  christos 		((u_long) mapbase + (char *) objself.dynamic);
    174      1.15  christos #endif				/* RTLD_RELOCATE_SELF */
    175      1.13  christos 
    176      1.14        pk 	_rtld_digest_dynamic(&objself);
    177      1.14        pk 
    178       1.1       cgd #ifdef __alpha__
    179      1.14        pk 	/* XXX XXX XXX */
    180      1.14        pk 	objself.pltgot = NULL;
    181       1.1       cgd #endif
    182      1.14        pk 	assert(objself.needed == NULL);
    183      1.14        pk 
    184      1.12  christos #if !defined(__mips__) && !defined(__i386__)
    185      1.14        pk 	/* no relocation for mips/i386 */
    186      1.14        pk 	assert(!objself.textrel);
    187       1.6    mhitch #endif
    188       1.1       cgd 
    189      1.14        pk 	_rtld_relocate_objects(&objself, true, dodebug);
    190      1.13  christos 
    191      1.14        pk 	/*
    192      1.14        pk 	 * Now that we relocated ourselves, we can use globals.
    193      1.14        pk 	 */
    194      1.14        pk 	_rtld_objself = objself;
    195      1.13  christos 
    196      1.14        pk 	_rtld_objself.path = _rtld_path;
    197      1.14        pk 	_rtld_add_paths(&_rtld_paths, RTLD_DEFAULT_LIBRARY_PATH, true);
    198      1.13  christos 
    199      1.14        pk 	/*
    200      1.14        pk 	 * Set up the _rtld_objlist pointer, so that rtld symbols can be found.
    201      1.14        pk 	 */
    202      1.14        pk 	_rtld_objlist = &_rtld_objself;
    203       1.1       cgd 
    204      1.14        pk 	/* Make the object list empty again. */
    205      1.14        pk 	_rtld_objlist = NULL;
    206      1.14        pk 	_rtld_objtail = &_rtld_objlist;
    207       1.1       cgd 
    208      1.14        pk 	_rtld_debug.r_brk = _rtld_debug_state;
    209      1.14        pk 	_rtld_debug.r_state = RT_CONSISTENT;
    210       1.1       cgd }
    211      1.14        pk 
    212       1.1       cgd /*
    213       1.1       cgd  * Cleanup procedure.  It will be called (by the atexit() mechanism) just
    214       1.1       cgd  * before the process exits.
    215       1.1       cgd  */
    216       1.1       cgd static void
    217      1.15  christos _rtld_exit()
    218       1.1       cgd {
    219      1.15  christos 	dbg(("rtld_exit()"));
    220       1.1       cgd 
    221      1.14        pk 	_rtld_call_fini_functions(_rtld_objlist->next);
    222       1.1       cgd }
    223      1.14        pk 
    224       1.1       cgd /*
    225       1.1       cgd  * Main entry point for dynamic linking.  The argument is the stack
    226       1.1       cgd  * pointer.  The stack is expected to be laid out as described in the
    227       1.1       cgd  * SVR4 ABI specification, Intel 386 Processor Supplement.  Specifically,
    228       1.1       cgd  * the stack pointer points to a word containing ARGC.  Following that
    229       1.1       cgd  * in the stack is a null-terminated sequence of pointers to argument
    230       1.1       cgd  * strings.  Then comes a null-terminated sequence of pointers to
    231       1.1       cgd  * environment strings.  Finally, there is a sequence of "auxiliary
    232       1.1       cgd  * vector" entries.
    233       1.1       cgd  *
    234      1.17    kleink  * This function returns the entry point for the main program, the dynamic
    235      1.17    kleink  * linker's exit procedure in sp[0], and a pointer to the main object in
    236      1.17    kleink  * sp[1].
    237       1.1       cgd  */
    238       1.1       cgd Elf_Addr
    239      1.15  christos _rtld(sp)
    240      1.15  christos 	Elf_Word *sp;
    241       1.1       cgd {
    242      1.15  christos 	const AuxInfo  *pAUX_base, *pAUX_entry, *pAUX_execfd, *pAUX_phdr,
    243      1.15  christos 	               *pAUX_phent, *pAUX_phnum;
    244      1.15  christos 	char          **env;
    245      1.15  christos 	const AuxInfo  *aux;
    246      1.15  christos 	const AuxInfo  *auxp;
    247      1.15  christos 	Elf_Word       *const osp = sp;
    248      1.15  christos 	bool            bind_now = 0;
    249      1.15  christos 	const char     *ld_bind_now;
    250      1.15  christos 	const char    **argv;
    251      1.13  christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    252      1.15  christos 	int             i = 0;
    253      1.12  christos #endif
    254       1.1       cgd 
    255      1.15  christos 	/*
    256      1.15  christos          * On entry, the dynamic linker itself has not been relocated yet.
    257      1.15  christos          * Be very careful not to reference any global data until after
    258      1.15  christos          * _rtld_init has returned.  It is OK to reference file-scope statics
    259      1.15  christos          * and string constants, and to call static and global functions.
    260      1.15  christos          */
    261      1.15  christos 	/* Find the auxiliary vector on the stack. */
    262      1.15  christos 	/* first Elf_Word reserved to address of exit routine */
    263      1.13  christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    264      1.16  christos 	dbg(("sp = %p, argc = %ld, argv = %p <%s>\n", sp, (long)sp[2],
    265      1.15  christos 	     &sp[3], (char *) sp[3]));
    266      1.15  christos 	dbg(("got is at %p, dynamic is at %p\n",
    267      1.15  christos 	    _GLOBAL_OFFSET_TABLE_, &_DYNAMIC));
    268      1.15  christos 	debug = 1;
    269      1.15  christos 	dbg(("_ctype_ is %p\n", _ctype_));
    270       1.1       cgd #endif
    271       1.1       cgd 
    272      1.15  christos 	sp += 2;		/* skip over return argument space */
    273      1.15  christos 	argv = (const char **) &sp[1];
    274      1.15  christos 	sp += sp[0] + 2;	/* Skip over argc, arguments, and NULL
    275      1.15  christos 				 * terminator */
    276      1.15  christos 	env = (char **) sp;
    277      1.15  christos 	while (*sp++ != 0) {	/* Skip over environment, and NULL terminator */
    278      1.13  christos #if defined(RTLD_DEBUG) && !defined(RTLD_RELOCATE_SELF)
    279      1.15  christos 		dbg(("env[%d] = %p %s\n", i++, (void *)sp[-1], (char *)sp[-1]));
    280       1.1       cgd #endif
    281      1.15  christos 	}
    282      1.15  christos 	aux = (const AuxInfo *) sp;
    283       1.1       cgd 
    284      1.15  christos 	/* Digest the auxiliary vector. */
    285      1.15  christos 	pAUX_base = pAUX_entry = pAUX_execfd = NULL;
    286      1.15  christos 	pAUX_phdr = pAUX_phent = pAUX_phnum = NULL;
    287      1.15  christos 	for (auxp = aux; auxp->au_id != AUX_null; ++auxp) {
    288      1.15  christos 		switch (auxp->au_id) {
    289      1.15  christos 		case AUX_base:
    290      1.15  christos 			pAUX_base = auxp;
    291      1.15  christos 			break;
    292      1.15  christos 		case AUX_entry:
    293      1.15  christos 			pAUX_entry = auxp;
    294      1.15  christos 			break;
    295      1.15  christos 		case AUX_execfd:
    296      1.15  christos 			pAUX_execfd = auxp;
    297      1.15  christos 			break;
    298      1.15  christos 		case AUX_phdr:
    299      1.15  christos 			pAUX_phdr = auxp;
    300      1.15  christos 			break;
    301      1.15  christos 		case AUX_phent:
    302      1.15  christos 			pAUX_phent = auxp;
    303      1.15  christos 			break;
    304      1.15  christos 		case AUX_phnum:
    305      1.15  christos 			pAUX_phnum = auxp;
    306      1.15  christos 			break;
    307      1.15  christos 		}
    308      1.15  christos 	}
    309      1.15  christos 
    310      1.15  christos 	/* Initialize and relocate ourselves. */
    311      1.15  christos 	assert(pAUX_base != NULL);
    312      1.15  christos 	_rtld_init((caddr_t) pAUX_base->au_v);
    313       1.1       cgd 
    314       1.1       cgd #ifdef RTLD_DEBUG
    315      1.15  christos 	dbg(("_ctype_ is %p\n", _ctype_));
    316       1.1       cgd #endif
    317       1.1       cgd 
    318      1.15  christos 	__progname = _rtld_objself.path;
    319      1.15  christos 	environ = env;
    320       1.1       cgd 
    321      1.15  christos 	_rtld_trust = geteuid() == getuid() && getegid() == getgid();
    322       1.1       cgd 
    323      1.15  christos 	ld_bind_now = getenv("LD_BIND_NOW");
    324      1.15  christos 	if (ld_bind_now != NULL && *ld_bind_now != '\0')
    325      1.15  christos 		bind_now = true;
    326      1.15  christos 	if (_rtld_trust) {
    327       1.6    mhitch #ifdef DEBUG
    328      1.15  christos 		const char     *ld_debug = getenv("LD_DEBUG");
    329      1.15  christos 		if (ld_debug != NULL && *ld_debug != '\0')
    330      1.15  christos 			debug = 1;
    331      1.15  christos #endif
    332      1.15  christos 		_rtld_add_paths(&_rtld_paths, getenv("LD_LIBRARY_PATH"), true);
    333      1.15  christos 	}
    334      1.15  christos 	dbg(("%s is initialized, base address = %p", __progname,
    335      1.15  christos 	     (void *) pAUX_base->au_v));
    336      1.15  christos 
    337      1.15  christos 	/*
    338      1.15  christos          * Load the main program, or process its program header if it is
    339      1.15  christos          * already loaded.
    340      1.15  christos          */
    341      1.15  christos 	if (pAUX_execfd != NULL) {	/* Load the main program. */
    342      1.15  christos 		int             fd = pAUX_execfd->au_v;
    343      1.15  christos 		dbg(("loading main program"));
    344      1.15  christos 		_rtld_objmain = _rtld_map_object(argv[0], fd);
    345      1.15  christos 		close(fd);
    346      1.15  christos 		if (_rtld_objmain == NULL)
    347      1.15  christos 			_rtld_die();
    348      1.15  christos 	} else {		/* Main program already loaded. */
    349      1.15  christos 		const Elf_Phdr *phdr;
    350      1.15  christos 		int             phnum;
    351      1.15  christos 		caddr_t         entry;
    352      1.15  christos 
    353      1.15  christos 		dbg(("processing main program's program header"));
    354      1.15  christos 		assert(pAUX_phdr != NULL);
    355      1.15  christos 		phdr = (const Elf_Phdr *) pAUX_phdr->au_v;
    356      1.15  christos 		assert(pAUX_phnum != NULL);
    357      1.15  christos 		phnum = pAUX_phnum->au_v;
    358      1.15  christos 		assert(pAUX_phent != NULL);
    359      1.15  christos 		assert(pAUX_phent->au_v == sizeof(Elf_Phdr));
    360      1.15  christos 		assert(pAUX_entry != NULL);
    361      1.15  christos 		entry = (caddr_t) pAUX_entry->au_v;
    362      1.15  christos 		_rtld_objmain = _rtld_digest_phdr(phdr, phnum, entry);
    363      1.15  christos 	}
    364      1.15  christos 
    365      1.15  christos 	_rtld_objmain->path = xstrdup("main program");
    366      1.15  christos 	_rtld_objmain->mainprog = true;
    367      1.15  christos 	_rtld_digest_dynamic(_rtld_objmain);
    368      1.15  christos 
    369      1.15  christos 	_rtld_linkmap_add(_rtld_objmain);
    370      1.15  christos 	_rtld_linkmap_add(&_rtld_objself);
    371      1.15  christos 
    372      1.15  christos 	/* Link the main program into the list of objects. */
    373      1.15  christos 	*_rtld_objtail = _rtld_objmain;
    374      1.15  christos 	_rtld_objtail = &_rtld_objmain->next;
    375      1.15  christos 	++_rtld_objmain->refcount;
    376  1.17.2.1     perry 
    377  1.17.2.1     perry 	/*
    378  1.17.2.1     perry 	 * Pre-load user-specified objects after the main program but before
    379  1.17.2.1     perry 	 * any shared object dependencies.
    380  1.17.2.1     perry 	 */
    381  1.17.2.1     perry 	dbg(("preloading objects"));
    382  1.17.2.1     perry 	if (_rtld_trust && _rtld_preload(getenv("LD_PRELOAD"), true) == -1)
    383  1.17.2.1     perry 		_rtld_die();
    384      1.15  christos 
    385      1.15  christos 	dbg(("loading needed objects"));
    386      1.15  christos 	if (_rtld_load_needed_objects(_rtld_objmain) == -1)
    387      1.15  christos 		_rtld_die();
    388      1.15  christos 
    389      1.15  christos 	dbg(("relocating objects"));
    390      1.15  christos 	if (_rtld_relocate_objects(_rtld_objmain, bind_now, true) == -1)
    391      1.15  christos 		_rtld_die();
    392      1.15  christos 
    393      1.15  christos 	dbg(("doing copy relocations"));
    394      1.15  christos 	if (_rtld_do_copy_relocations(_rtld_objmain, true) == -1)
    395      1.15  christos 		_rtld_die();
    396      1.15  christos 
    397      1.15  christos 	dbg(("calling _init functions"));
    398      1.15  christos 	_rtld_call_init_functions(_rtld_objmain->next);
    399      1.15  christos 
    400      1.15  christos 	dbg(("control at program entry point = %p, obj = %p, exit = %p",
    401      1.15  christos 	     _rtld_objmain->entry, _rtld_objmain, _rtld_exit));
    402      1.15  christos 
    403      1.15  christos 	/*
    404      1.15  christos 	 * Return with the entry point and the exit procedure in at the top
    405      1.15  christos 	 * of stack.
    406      1.15  christos 	 */
    407      1.15  christos 
    408      1.15  christos 	_rtld_debug_state();	/* say hello to gdb! */
    409      1.15  christos 
    410      1.15  christos 	((void **) osp)[0] = _rtld_exit;
    411      1.15  christos 	((void **) osp)[1] = _rtld_objmain;
    412      1.15  christos 	return (Elf_Addr) _rtld_objmain->entry;
    413       1.1       cgd }
    414       1.1       cgd 
    415       1.1       cgd void
    416      1.15  christos _rtld_die()
    417       1.1       cgd {
    418      1.15  christos 	const char *msg = _rtld_dlerror();
    419       1.1       cgd 
    420      1.15  christos 	if (msg == NULL)
    421      1.15  christos 		msg = "Fatal error";
    422      1.15  christos 	xerrx(1, "%s\n", msg);
    423       1.1       cgd }
    424       1.1       cgd 
    425       1.1       cgd static Obj_Entry *
    426      1.15  christos _rtld_dlcheck(handle)
    427      1.15  christos 	void *handle;
    428       1.1       cgd {
    429      1.15  christos 	Obj_Entry *obj;
    430       1.1       cgd 
    431      1.15  christos 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
    432      1.15  christos 		if (obj == (Obj_Entry *) handle)
    433      1.15  christos 			break;
    434      1.15  christos 
    435      1.15  christos 	if (obj == NULL || obj->dl_refcount == 0) {
    436      1.15  christos 		xwarnx("Invalid shared object handle %p", handle);
    437      1.15  christos 		return NULL;
    438      1.15  christos 	}
    439      1.15  christos 	return obj;
    440       1.1       cgd }
    441       1.1       cgd 
    442       1.1       cgd static void
    443      1.15  christos _rtld_unref_object_dag(root)
    444      1.15  christos 	Obj_Entry *root;
    445       1.1       cgd {
    446      1.15  christos 	assert(root->refcount != 0);
    447      1.15  christos 	--root->refcount;
    448      1.15  christos 	if (root->refcount == 0) {
    449      1.15  christos 		const Needed_Entry *needed;
    450      1.15  christos 
    451      1.15  christos 		for (needed = root->needed; needed != NULL;
    452      1.15  christos 		    needed = needed->next)
    453      1.15  christos 			_rtld_unref_object_dag(needed->obj);
    454      1.15  christos 	}
    455       1.1       cgd }
    456       1.1       cgd 
    457       1.1       cgd int
    458      1.15  christos _rtld_dlclose(handle)
    459      1.15  christos 	void *handle;
    460       1.1       cgd {
    461      1.15  christos 	Obj_Entry *root = _rtld_dlcheck(handle);
    462       1.1       cgd 
    463      1.15  christos 	if (root == NULL)
    464      1.15  christos 		return -1;
    465       1.1       cgd 
    466      1.15  christos 	_rtld_debug.r_state = RT_DELETE;
    467      1.15  christos 	_rtld_debug_state();
    468      1.15  christos 
    469      1.15  christos 	--root->dl_refcount;
    470      1.15  christos 	_rtld_unref_object_dag(root);
    471      1.15  christos 	if (root->refcount == 0) {	/* We are finished with some objects. */
    472      1.15  christos 		Obj_Entry      *obj;
    473      1.15  christos 		Obj_Entry     **linkp;
    474      1.15  christos 
    475      1.15  christos 		/* Finalize objects that are about to be unmapped. */
    476      1.15  christos 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
    477      1.15  christos 			if (obj->refcount == 0 && obj->fini != NULL)
    478      1.15  christos 				(*obj->fini) ();
    479      1.15  christos 
    480      1.15  christos 		/* Unmap all objects that are no longer referenced. */
    481      1.15  christos 		linkp = &_rtld_objlist->next;
    482      1.15  christos 		while ((obj = *linkp) != NULL) {
    483      1.15  christos 			if (obj->refcount == 0) {
    484      1.15  christos 				munmap(obj->mapbase, obj->mapsize);
    485      1.15  christos 				free(obj->path);
    486      1.15  christos 				while (obj->needed != NULL) {
    487      1.15  christos 					Needed_Entry   *needed = obj->needed;
    488      1.15  christos 					obj->needed = needed->next;
    489      1.15  christos 					free(needed);
    490      1.15  christos 				}
    491      1.15  christos 				_rtld_linkmap_delete(obj);
    492      1.15  christos 				*linkp = obj->next;
    493      1.15  christos 				if (obj->next == NULL)
    494      1.15  christos 					_rtld_objtail = linkp;
    495      1.15  christos 				free(obj);
    496      1.15  christos 			} else
    497      1.15  christos 				linkp = &obj->next;
    498       1.1       cgd 		}
    499       1.1       cgd 	}
    500      1.15  christos 	_rtld_debug.r_state = RT_CONSISTENT;
    501      1.15  christos 	_rtld_debug_state();
    502       1.1       cgd 
    503      1.15  christos 	return 0;
    504       1.1       cgd }
    505       1.1       cgd 
    506       1.1       cgd char *
    507      1.15  christos _rtld_dlerror()
    508       1.1       cgd {
    509      1.15  christos 	char *msg = error_message;
    510      1.15  christos 	error_message = NULL;
    511      1.15  christos 	return msg;
    512       1.1       cgd }
    513       1.1       cgd 
    514       1.1       cgd void *
    515      1.15  christos _rtld_dlopen(name, mode)
    516      1.15  christos 	const char *name;
    517      1.15  christos 	int mode;
    518      1.15  christos {
    519      1.15  christos 	Obj_Entry **old_obj_tail = _rtld_objtail;
    520      1.15  christos 	Obj_Entry *obj = NULL;
    521      1.15  christos 
    522      1.15  christos 	_rtld_debug.r_state = RT_ADD;
    523      1.15  christos 	_rtld_debug_state();
    524      1.15  christos 
    525      1.15  christos 	if (name == NULL) {
    526      1.15  christos 		obj = _rtld_objmain;
    527      1.15  christos 	} else {
    528      1.15  christos 		char *path = _rtld_find_library(name, NULL);
    529      1.15  christos 		if (path != NULL)
    530      1.15  christos 			obj = _rtld_load_object(path, true);
    531       1.1       cgd 	}
    532       1.1       cgd 
    533      1.15  christos 	if (obj != NULL) {
    534      1.15  christos 		++obj->dl_refcount;
    535      1.15  christos 		if (*old_obj_tail != NULL) {	/* We loaded something new. */
    536      1.15  christos 			assert(*old_obj_tail == obj);
    537      1.15  christos 
    538      1.15  christos 			/* FIXME - Clean up properly after an error. */
    539      1.15  christos 			if (_rtld_load_needed_objects(obj) == -1) {
    540      1.15  christos 				--obj->dl_refcount;
    541      1.15  christos 				obj = NULL;
    542      1.15  christos 			} else if (_rtld_relocate_objects(obj,
    543      1.15  christos 			    (mode & 3) == RTLD_NOW, true) == -1) {
    544      1.15  christos 				--obj->dl_refcount;
    545      1.15  christos 				obj = NULL;
    546      1.15  christos 			} else {
    547      1.15  christos 				_rtld_call_init_functions(obj);
    548      1.15  christos 			}
    549      1.15  christos 		}
    550      1.15  christos 	}
    551      1.15  christos 	_rtld_debug.r_state = RT_CONSISTENT;
    552      1.15  christos 	_rtld_debug_state();
    553       1.1       cgd 
    554      1.15  christos 	return obj;
    555       1.1       cgd }
    556       1.1       cgd 
    557       1.1       cgd void *
    558      1.15  christos _rtld_dlsym(handle, name)
    559      1.15  christos 	void *handle;
    560      1.15  christos 	const char *name;
    561      1.15  christos {
    562      1.15  christos 	const Obj_Entry *obj = _rtld_dlcheck(handle);
    563      1.15  christos 	const Elf_Sym  *def;
    564      1.15  christos 	const Obj_Entry *defobj;
    565       1.1       cgd 
    566      1.15  christos 	if (obj == NULL)
    567      1.15  christos 		return NULL;
    568       1.1       cgd 
    569      1.15  christos 	/*
    570      1.15  christos          * FIXME - This isn't correct.  The search should include the whole
    571      1.15  christos          * DAG rooted at the given object.
    572      1.15  christos          */
    573      1.15  christos 	def = _rtld_find_symdef(_rtld_objlist, 0, name, obj, &defobj, false);
    574      1.15  christos 	if (def != NULL)
    575      1.15  christos 		return defobj->relocbase + def->st_value;
    576       1.1       cgd 
    577      1.15  christos 	_rtld_error("Undefined symbol \"%s\"", name);
    578      1.15  christos 	return NULL;
    579       1.1       cgd }
    580       1.1       cgd 
    581       1.1       cgd /*
    582       1.1       cgd  * Error reporting function.  Use it like printf.  If formats the message
    583       1.1       cgd  * into a buffer, and sets things up so that the next call to dlerror()
    584       1.1       cgd  * will return the message.
    585       1.1       cgd  */
    586       1.1       cgd void
    587      1.15  christos #ifdef __STDC__
    588      1.15  christos _rtld_error(const char *fmt,...)
    589      1.15  christos #else
    590      1.15  christos _rtld_error(va_alist)
    591      1.15  christos 	va_dcl
    592      1.15  christos #endif
    593       1.1       cgd {
    594      1.15  christos 	static char     buf[512];
    595      1.15  christos 	va_list         ap;
    596      1.15  christos #ifdef __STDC__
    597      1.15  christos 	va_start(ap, fmt);
    598      1.15  christos #else
    599      1.15  christos 	const char *fmt;
    600       1.1       cgd 
    601      1.15  christos 	va_start(ap);
    602      1.15  christos 	fmt = va_arg(ap, const char *);
    603      1.15  christos #endif
    604      1.15  christos 	xvsnprintf(buf, sizeof buf, fmt, ap);
    605      1.15  christos 	error_message = buf;
    606      1.15  christos 	va_end(ap);
    607       1.1       cgd }
    608      1.14        pk 
    609       1.1       cgd void
    610      1.15  christos _rtld_debug_state()
    611       1.1       cgd {
    612      1.15  christos 	/* do nothing */
    613       1.1       cgd }
    614       1.1       cgd 
    615       1.1       cgd void
    616      1.15  christos _rtld_linkmap_add(obj)
    617      1.15  christos 	Obj_Entry *obj;
    618       1.1       cgd {
    619      1.15  christos 	struct link_map *l = &obj->linkmap;
    620      1.15  christos 	struct link_map *prev;
    621       1.1       cgd 
    622      1.15  christos 	obj->linkmap.l_name = obj->path;
    623      1.15  christos 	obj->linkmap.l_addr = obj->mapbase;
    624      1.15  christos 	obj->linkmap.l_ld = obj->dynamic;
    625       1.6    mhitch #ifdef __mips__
    626      1.15  christos 	/* GDB needs load offset on MIPS to use the symbols */
    627      1.15  christos 	obj->linkmap.l_offs = obj->relocbase;
    628       1.6    mhitch #endif
    629       1.1       cgd 
    630      1.15  christos 	if (_rtld_debug.r_map == NULL) {
    631      1.15  christos 		_rtld_debug.r_map = l;
    632      1.15  christos 		return;
    633      1.15  christos 	}
    634      1.15  christos 	for (prev = _rtld_debug.r_map; prev->l_next != NULL; prev = prev->l_next);
    635      1.15  christos 	l->l_prev = prev;
    636      1.15  christos 	prev->l_next = l;
    637      1.15  christos 	l->l_next = NULL;
    638       1.1       cgd }
    639       1.1       cgd 
    640       1.1       cgd void
    641      1.15  christos _rtld_linkmap_delete(obj)
    642      1.15  christos 	Obj_Entry *obj;
    643       1.1       cgd {
    644      1.15  christos 	struct link_map *l = &obj->linkmap;
    645       1.1       cgd 
    646      1.15  christos 	if (l->l_prev == NULL) {
    647      1.15  christos 		if ((_rtld_debug.r_map = l->l_next) != NULL)
    648      1.15  christos 			l->l_next->l_prev = NULL;
    649      1.15  christos 		return;
    650      1.15  christos 	}
    651      1.15  christos 	if ((l->l_prev->l_next = l->l_next) != NULL)
    652      1.15  christos 		l->l_next->l_prev = l->l_prev;
    653       1.1       cgd }
    654