Home | History | Annotate | Line # | Download | only in ld.elf_so
load.c revision 1.46
      1 /*	$NetBSD: load.c,v 1.46 2013/11/20 07:18:23 skrll 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: load.c,v 1.46 2013/11/20 07:18:23 skrll 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/types.h>
     55 #include <sys/param.h>
     56 #include <sys/mman.h>
     57 #include <sys/sysctl.h>
     58 #include <dirent.h>
     59 
     60 #include "debug.h"
     61 #include "rtld.h"
     62 
     63 static bool _rtld_load_by_name(const char *, Obj_Entry *, Needed_Entry **,
     64     int);
     65 
     66 #ifdef RTLD_LOADER
     67 Objlist _rtld_list_main =	/* Objects loaded at program startup */
     68   SIMPLEQ_HEAD_INITIALIZER(_rtld_list_main);
     69 Objlist _rtld_list_global =	/* Objects dlopened with RTLD_GLOBAL */
     70   SIMPLEQ_HEAD_INITIALIZER(_rtld_list_global);
     71 
     72 void
     73 _rtld_objlist_push_head(Objlist *list, Obj_Entry *obj)
     74 {
     75 	Objlist_Entry *elm;
     76 
     77 	elm = NEW(Objlist_Entry);
     78 	elm->obj = obj;
     79 	SIMPLEQ_INSERT_HEAD(list, elm, link);
     80 }
     81 
     82 void
     83 _rtld_objlist_push_tail(Objlist *list, Obj_Entry *obj)
     84 {
     85 	Objlist_Entry *elm;
     86 
     87 	elm = NEW(Objlist_Entry);
     88 	elm->obj = obj;
     89 	SIMPLEQ_INSERT_TAIL(list, elm, link);
     90 }
     91 
     92 Objlist_Entry *
     93 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
     94 {
     95 	Objlist_Entry *elm;
     96 
     97 	SIMPLEQ_FOREACH(elm, list, link) {
     98 		if (elm->obj == obj)
     99 			return elm;
    100 	}
    101 	return NULL;
    102 }
    103 #endif
    104 
    105 /*
    106  * Load a shared object into memory, if it is not already loaded.
    107  *
    108  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
    109  * on failure.
    110  */
    111 Obj_Entry *
    112 _rtld_load_object(const char *filepath, int flags)
    113 {
    114 	Obj_Entry *obj;
    115 	int fd = -1;
    116 	struct stat sb;
    117 	size_t pathlen = strlen(filepath);
    118 
    119 	for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
    120 		if (pathlen == obj->pathlen && !strcmp(obj->path, filepath))
    121 			break;
    122 
    123 	/*
    124 	 * If we didn't find a match by pathname, open the file and check
    125 	 * again by device and inode.  This avoids false mismatches caused
    126 	 * by multiple links or ".." in pathnames.
    127 	 *
    128 	 * To avoid a race, we open the file and use fstat() rather than
    129 	 * using stat().
    130 	 */
    131 	if (obj == NULL) {
    132 		if ((fd = open(filepath, O_RDONLY)) == -1) {
    133 			_rtld_error("Cannot open \"%s\"", filepath);
    134 			return NULL;
    135 		}
    136 		if (fstat(fd, &sb) == -1) {
    137 			_rtld_error("Cannot fstat \"%s\"", filepath);
    138 			close(fd);
    139 			return NULL;
    140 		}
    141 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
    142 			if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
    143 				close(fd);
    144 				break;
    145 			}
    146 		}
    147 	}
    148 
    149 #ifdef RTLD_LOADER
    150 	if (pathlen == _rtld_objself.pathlen &&
    151 	    strcmp(_rtld_objself.path, filepath) == 0) {
    152 		return &_rtld_objself;
    153 	}
    154 #endif
    155 
    156 	if (obj == NULL) { /* First use of this object, so we must map it in */
    157 		obj = _rtld_map_object(filepath, fd, &sb);
    158 		(void)close(fd);
    159 		if (obj == NULL)
    160 			return NULL;
    161 		_rtld_digest_dynamic(filepath, obj);
    162 
    163 		if (flags & _RTLD_DLOPEN) {
    164 			if (obj->z_noopen || (flags & _RTLD_NOLOAD)) {
    165 				dbg(("refusing to load non-loadable \"%s\"",
    166 				    obj->path));
    167 				_rtld_error("Cannot dlopen non-loadable %s",
    168 				    obj->path);
    169 				munmap(obj->mapbase, obj->mapsize);
    170 				_rtld_obj_free(obj);
    171 				return OBJ_ERR;
    172 			}
    173 		}
    174 
    175 		*_rtld_objtail = obj;
    176 		_rtld_objtail = &obj->next;
    177 		_rtld_objcount++;
    178 		_rtld_objloads++;
    179 #ifdef RTLD_LOADER
    180 		_rtld_linkmap_add(obj);	/* for GDB */
    181 #endif
    182 		dbg(("  %p .. %p: %s", obj->mapbase,
    183 		    obj->mapbase + obj->mapsize - 1, obj->path));
    184 		if (obj->textrel)
    185 			dbg(("  WARNING: %s has impure text", obj->path));
    186 	}
    187 
    188 	++obj->refcount;
    189 #ifdef RTLD_LOADER
    190 	if (flags & _RTLD_MAIN && !obj->mainref) {
    191 		obj->mainref = 1;
    192 		dbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
    193 		_rtld_objlist_push_tail(&_rtld_list_main, obj);
    194 	}
    195 	if (flags & _RTLD_GLOBAL && !obj->globalref) {
    196 		obj->globalref = 1;
    197 		dbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
    198 		_rtld_objlist_push_tail(&_rtld_list_global, obj);
    199 	}
    200 #endif
    201 	return obj;
    202 }
    203 
    204 static bool
    205 _rtld_load_by_name(const char *name, Obj_Entry *obj, Needed_Entry **needed,
    206     int flags)
    207 {
    208 	Library_Xform *x = _rtld_xforms;
    209 	Obj_Entry *o;
    210 	size_t j;
    211 	ssize_t i;
    212 	bool got = false;
    213 	union {
    214 		int i;
    215 		u_quad_t q;
    216 		char s[16];
    217 	} val;
    218 
    219 	dbg(("load by name %s %p", name, x));
    220 	for (o = _rtld_objlist->next; o != NULL; o = o->next)
    221 		if (_rtld_object_match_name(o, name)) {
    222 			++o->refcount;
    223 			(*needed)->obj = o;
    224 			return true;
    225 		}
    226 
    227 	for (; x; x = x->next) {
    228 		if (strcmp(x->name, name) != 0)
    229 			continue;
    230 
    231 		j = sizeof(val);
    232 		if ((i = _rtld_sysctl(x->ctlname, &val, &j)) == -1) {
    233 			xwarnx(_PATH_LD_HINTS ": invalid/unknown sysctl for %s (%d)",
    234 			    name, errno);
    235 			break;
    236 		}
    237 
    238 		switch (i) {
    239 		case CTLTYPE_QUAD:
    240 			xsnprintf(val.s, sizeof(val.s), "%" PRIu64, val.q);
    241 			break;
    242 		case CTLTYPE_INT:
    243 			xsnprintf(val.s, sizeof(val.s), "%d", val.i);
    244 			break;
    245 		case CTLTYPE_STRING:
    246 			break;
    247 		default:
    248 			xwarnx("unsupported sysctl type %d", (int)i);
    249 			break;
    250 		}
    251 
    252 		dbg(("sysctl returns %s", val.s));
    253 
    254 		for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
    255 		    i++) {
    256 			dbg(("entry %ld", (unsigned long)i));
    257 			if (strcmp(x->entry[i].value, val.s) == 0)
    258 				break;
    259 		}
    260 
    261 		if (i == RTLD_MAX_ENTRY) {
    262 			xwarnx("sysctl value %s not found for lib%s",
    263 			    val.s, name);
    264 			break;
    265 		}
    266 
    267 		for (j = 0; j < RTLD_MAX_LIBRARY &&
    268 		    x->entry[i].library[j] != NULL; j++) {
    269 			o = _rtld_load_library(x->entry[i].library[j], obj,
    270 			    flags);
    271 			if (o == NULL) {
    272 				xwarnx("could not load %s for %s",
    273 				    x->entry[i].library[j], name);
    274 				continue;
    275 			}
    276 			got = true;
    277 			if (j == 0)
    278 				(*needed)->obj = o;
    279 			else {
    280 				/* make a new one and put it in the chain */
    281 				Needed_Entry *ne = xmalloc(sizeof(*ne));
    282 				ne->name = (*needed)->name;
    283 				ne->obj = o;
    284 				ne->next = (*needed)->next;
    285 				(*needed)->next = ne;
    286 				*needed = ne;
    287 			}
    288 
    289 		}
    290 
    291 	}
    292 
    293 	if (got)
    294 		return true;
    295 
    296 	return ((*needed)->obj = _rtld_load_library(name, obj, flags)) != NULL;
    297 }
    298 
    299 
    300 /*
    301  * Given a shared object, traverse its list of needed objects, and load
    302  * each of them.  Returns 0 on success.  Generates an error message and
    303  * returns -1 on failure.
    304  */
    305 int
    306 _rtld_load_needed_objects(Obj_Entry *first, int flags)
    307 {
    308 	Obj_Entry *obj;
    309 	int status = 0;
    310 
    311 	for (obj = first; obj != NULL; obj = obj->next) {
    312 		Needed_Entry *needed;
    313 
    314 		for (needed = obj->needed; needed != NULL;
    315 		    needed = needed->next) {
    316 			const char *name = obj->strtab + needed->name;
    317 #ifdef RTLD_LOADER
    318 			Obj_Entry *nobj;
    319 #endif
    320 			if (!_rtld_load_by_name(name, obj, &needed,
    321 			    flags & ~_RTLD_NOLOAD))
    322 				status = -1;	/* FIXME - cleanup */
    323 #ifdef RTLD_LOADER
    324 			if (status == -1)
    325 				return status;
    326 
    327 			if (flags & _RTLD_MAIN)
    328 				continue;
    329 
    330 			nobj = needed->obj;
    331 			if (nobj->z_nodelete && !obj->ref_nodel) {
    332 				dbg(("obj %s nodelete", nobj->path));
    333 				_rtld_ref_dag(nobj);
    334 				nobj->ref_nodel = true;
    335 			}
    336 #endif
    337 		}
    338 	}
    339 
    340 	return status;
    341 }
    342 
    343 #ifdef RTLD_LOADER
    344 int
    345 _rtld_preload(const char *preload_path)
    346 {
    347 	const char *path;
    348 	char *cp, *buf;
    349 	int status = 0;
    350 
    351 	if (preload_path != NULL && *preload_path != '\0') {
    352 		cp = buf = xstrdup(preload_path);
    353 		while ((path = strsep(&cp, " :")) != NULL && status == 0) {
    354 			if (!_rtld_load_object(path, _RTLD_MAIN))
    355 				status = -1;
    356 			else
    357 				dbg((" preloaded \"%s\"", path));
    358 		}
    359 		xfree(buf);
    360 	}
    361 
    362 	return status;
    363 }
    364 #endif
    365