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