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