Home | History | Annotate | Line # | Download | only in ld.elf_so
load.c revision 1.30
      1 /*	$NetBSD: load.c,v 1.30 2006/03/18 02:34:30 matt 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.30 2006/03/18 02:34:30 matt 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_add(Objlist *list, Obj_Entry *obj)
     74 {
     75 	Objlist_Entry *elm;
     76 
     77 	elm = NEW(Objlist_Entry);
     78 	elm->obj = obj;
     79 	SIMPLEQ_INSERT_TAIL(list, elm, link);
     80 }
     81 
     82 Objlist_Entry *
     83 _rtld_objlist_find(Objlist *list, const Obj_Entry *obj)
     84 {
     85 	Objlist_Entry *elm;
     86 
     87 	SIMPLEQ_FOREACH(elm, list, link) {
     88 		if (elm->obj == obj)
     89 			return elm;
     90 	}
     91 	return NULL;
     92 }
     93 #endif
     94 
     95 /*
     96  * Load a shared object into memory, if it is not already loaded.  The
     97  * argument must be a string allocated on the heap.  This function assumes
     98  * responsibility for freeing it when necessary.
     99  *
    100  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
    101  * on failure.
    102  */
    103 Obj_Entry *
    104 _rtld_load_object(char *filepath, int mode)
    105 {
    106 	Obj_Entry *obj;
    107 	int fd = -1;
    108 	struct stat sb;
    109 	size_t pathlen = strlen(filepath);
    110 
    111 	for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
    112 		if (pathlen == obj->pathlen && !strcmp(obj->path, filepath))
    113 			break;
    114 
    115 	/*
    116 	 * If we didn't find a match by pathname, open the file and check
    117 	 * again by device and inode.  This avoids false mismatches caused
    118 	 * by multiple links or ".." in pathnames.
    119 	 *
    120 	 * To avoid a race, we open the file and use fstat() rather than
    121 	 * using stat().
    122 	 */
    123 	if (obj == NULL) {
    124 		if ((fd = open(filepath, O_RDONLY)) == -1) {
    125 			_rtld_error("Cannot open \"%s\"", filepath);
    126 			free(filepath);
    127 			return NULL;
    128 		}
    129 		if (fstat(fd, &sb) == -1) {
    130 			_rtld_error("Cannot fstat \"%s\"", filepath);
    131 			close(fd);
    132 			free(filepath);
    133 			return NULL;
    134 		}
    135 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
    136 			if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
    137 				close(fd);
    138 				break;
    139 			}
    140 		}
    141 	}
    142 
    143 	if (obj == NULL) { /* First use of this object, so we must map it in */
    144 		obj = _rtld_map_object(filepath, fd, &sb);
    145 		(void)close(fd);
    146 		if (obj == NULL) {
    147 			free(filepath);
    148 			return NULL;
    149 		}
    150 		_rtld_digest_dynamic(obj);
    151 
    152 		*_rtld_objtail = obj;
    153 		_rtld_objtail = &obj->next;
    154 #ifdef RTLD_LOADER
    155 		_rtld_linkmap_add(obj);	/* for GDB */
    156 #endif
    157 		dbg(("  %p .. %p: %s", obj->mapbase,
    158 		    obj->mapbase + obj->mapsize - 1, obj->path));
    159 		if (obj->textrel)
    160 			dbg(("  WARNING: %s has impure text", obj->path));
    161 	} else
    162 		free(filepath);
    163 
    164 	++obj->refcount;
    165 #ifdef RTLD_LOADER
    166 	if (mode & RTLD_MAIN && !obj->mainref) {
    167 		obj->mainref = 1;
    168 		rdbg(("adding %p (%s) to _rtld_list_main", obj, obj->path));
    169 		_rtld_objlist_add(&_rtld_list_main, obj);
    170 	}
    171 	if (mode & RTLD_GLOBAL && !obj->globalref) {
    172 		obj->globalref = 1;
    173 		rdbg(("adding %p (%s) to _rtld_list_global", obj, obj->path));
    174 		_rtld_objlist_add(&_rtld_list_global, obj);
    175 	}
    176 #endif
    177 	return obj;
    178 }
    179 
    180 static bool
    181 _rtld_load_by_name(const char *name, Obj_Entry *obj, Needed_Entry **needed, int mode)
    182 {
    183 	Library_Xform *x = _rtld_xforms;
    184 	Obj_Entry *o = NULL;
    185 	size_t i, j;
    186 	bool got = false;
    187 	union {
    188 		int i;
    189 		u_quad_t q;
    190 		char s[16];
    191 	} val;
    192 
    193 	dbg(("load by name %s %p", name, x));
    194 	for (; x; x = x->next) {
    195 		if (strcmp(x->name, name) != 0)
    196 			continue;
    197 
    198 		j = sizeof(val);
    199 		if ((i = _rtld_sysctl(x->ctlname, &val, &j)) == -1) {
    200 			xwarnx(_PATH_LD_HINTS ": invalid/unknown sysctl for %s (%d)",
    201 			    name, errno);
    202 			break;
    203 		}
    204 
    205 		switch (i) {
    206 		case CTLTYPE_QUAD:
    207 			xsnprintf(val.s, sizeof(val.s), "%" PRIu64, val.q);
    208 			break;
    209 		case CTLTYPE_INT:
    210 			xsnprintf(val.s, sizeof(val.s), "%d", val.i);
    211 			break;
    212 		case CTLTYPE_STRING:
    213 			break;
    214 		default:
    215 			xwarnx("unsupported sysctl type %d", (int)i);
    216 			break;
    217 		}
    218 
    219 		dbg(("sysctl returns %s", val.s));
    220 
    221 		for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
    222 		    i++) {
    223 			dbg(("entry %ld", (unsigned long)i));
    224 			if (strcmp(x->entry[i].value, val.s) == 0)
    225 				break;
    226 		}
    227 
    228 		if (i == RTLD_MAX_ENTRY) {
    229 			xwarnx("sysctl value %s not found for lib%s",
    230 			    val.s, name);
    231 			break;
    232 		}
    233 		/* XXX: This can mess up debuggers, cause we lie about
    234 		 * what we loaded in the needed objects */
    235 		for (j = 0; j < RTLD_MAX_LIBRARY &&
    236 		    x->entry[i].library[j] != NULL; j++) {
    237 			o = _rtld_load_library(x->entry[i].library[j], obj,
    238 			    mode);
    239 			if (o == NULL) {
    240 				xwarnx("could not load %s for %s",
    241 				    x->entry[i].library[j], name);
    242 				continue;
    243 			}
    244 			got = true;
    245 			if (j == 0)
    246 				(*needed)->obj = o;
    247 			else {
    248 				/* make a new one and put it in the chain */
    249 				Needed_Entry *ne = xmalloc(sizeof(*ne));
    250 				ne->name = (*needed)->name;
    251 				ne->obj = o;
    252 				ne->next = (*needed)->next;
    253 				(*needed)->next = ne;
    254 				*needed = ne;
    255 			}
    256 
    257 		}
    258 
    259 	}
    260 
    261 	if (got)
    262 		return true;
    263 
    264 	return ((*needed)->obj = _rtld_load_library(name, obj, mode)) != NULL;
    265 }
    266 
    267 
    268 /*
    269  * Given a shared object, traverse its list of needed objects, and load
    270  * each of them.  Returns 0 on success.  Generates an error message and
    271  * returns -1 on failure.
    272  */
    273 int
    274 _rtld_load_needed_objects(Obj_Entry *first, int mode)
    275 {
    276 	Obj_Entry *obj;
    277 	int status = 0;
    278 
    279 	for (obj = first; obj != NULL; obj = obj->next) {
    280 		Needed_Entry *needed;
    281 
    282 		for (needed = obj->needed; needed != NULL;
    283 		    needed = needed->next) {
    284 			const char *name = obj->strtab + needed->name;
    285 			if (!_rtld_load_by_name(name, obj, &needed, mode))
    286 				status = -1;	/* FIXME - cleanup */
    287 #ifdef RTLD_LOADER
    288 			if (status == -1)
    289 				return status;
    290 #endif
    291 		}
    292 	}
    293 
    294 	return status;
    295 }
    296 
    297 #ifdef RTLD_LOADER
    298 int
    299 _rtld_preload(const char *preload_path)
    300 {
    301 	const char *path;
    302 	char *cp, *buf;
    303 	int status = 0;
    304 
    305 	if (preload_path != NULL && *preload_path != '\0') {
    306 		cp = buf = xstrdup(preload_path);
    307 		while ((path = strsep(&cp, " :")) != NULL && status == 0) {
    308 			if (!_rtld_load_object(xstrdup(path), RTLD_MAIN))
    309 				status = -1;
    310 			else
    311 				dbg((" preloaded \"%s\"", path));
    312 		}
    313 		free(buf);
    314 	}
    315 
    316 	return (status);
    317 }
    318 #endif
    319