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