Home | History | Annotate | Line # | Download | only in ld.elf_so
load.c revision 1.19
      1 /*	$NetBSD: load.c,v 1.19 2002/09/23 23:56:46 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))
    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 		_rtld_digest_dynamic(obj);
    142 
    143 		*_rtld_objtail = obj;
    144 		_rtld_objtail = &obj->next;
    145 #ifdef RTLD_LOADER
    146 		_rtld_linkmap_add(obj);	/* for GDB */
    147 #endif
    148 		dbg(("  %p .. %p: %s", obj->mapbase,
    149 		    obj->mapbase + obj->mapsize - 1, obj->path));
    150 		if (obj->textrel)
    151 			dbg(("  WARNING: %s has impure text", obj->path));
    152 	} else
    153 		free(filepath);
    154 
    155 	++obj->refcount;
    156 	if ((mode & RTLD_GLOBAL) &&
    157 	    _rtld_objlist_find(&_rtld_list_global, obj) == NULL)
    158 		_rtld_objlist_add(&_rtld_list_global, obj);
    159 	return obj;
    160 }
    161 
    162 static bool
    163 _rtld_load_by_name(name, obj, needed, mode)
    164 	const char *name;
    165 	Obj_Entry *obj;
    166 	Needed_Entry **needed;
    167 	int mode;
    168 {
    169 	Library_Xform *x = _rtld_xforms;
    170 	Obj_Entry *o = NULL;
    171 	size_t i, j;
    172 	bool got = false;
    173 	union {
    174 		int i;
    175 		char s[16];
    176 	} val;
    177 
    178 	dbg(("load by name %s %p", name, x));
    179 	for (; x; x = x->next) {
    180 		if (strcmp(x->name, name) != 0)
    181 			continue;
    182 
    183 		i = sizeof(val);
    184 
    185 		if (sysctl(x->ctl, x->ctlmax, &val, &i, NULL, 0) == -1) {
    186 			xwarnx(_PATH_LD_HINTS ": unknown sysctl for %s", name);
    187 			break;
    188 		}
    189 
    190 		switch (x->ctltype[x->ctlmax - 1]) {
    191 		case CTLTYPE_INT:
    192 			xsnprintf(val.s, sizeof(val.s), "%d", val.i);
    193 			break;
    194 		case CTLTYPE_STRING:
    195 			break;
    196 		default:
    197 			xwarnx("unsupported sysctl type %d",
    198 			    x->ctltype[x->ctlmax - 1]);
    199 			break;
    200 		}
    201 
    202 		dbg(("sysctl returns %s", val.s));
    203 
    204 		for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
    205 		    i++) {
    206 			dbg(("entry %ld", (unsigned long)i));
    207 			if (strcmp(x->entry[i].value, val.s) == 0)
    208 				break;
    209 		}
    210 
    211 		if (i == RTLD_MAX_ENTRY) {
    212 			xwarnx("sysctl value %s not found for lib%s",
    213 			    val.s, name);
    214 			break;
    215 		}
    216 		/* XXX: This can mess up debuggers, cause we lie about
    217 		 * what we loaded in the needed objects */
    218 		for (j = 0; j < RTLD_MAX_LIBRARY &&
    219 		    x->entry[i].library[j] != NULL; j++) {
    220 			o = _rtld_load_library(x->entry[i].library[j], obj,
    221 			    mode);
    222 			if (o == NULL) {
    223 				xwarnx("could not load %s for %s",
    224 				    x->entry[i].library[j], name);
    225 				continue;
    226 			}
    227 			got = true;
    228 			if (j == 0)
    229 				(*needed)->obj = o;
    230 			else {
    231 				/* make a new one and put it in the chain */
    232 				Needed_Entry *ne = xmalloc(sizeof(*ne));
    233 				ne->name = (*needed)->name;
    234 				ne->obj = o;
    235 				ne->next = (*needed)->next;
    236 				(*needed)->next = ne;
    237 				*needed = ne;
    238 			}
    239 
    240 		}
    241 
    242 	}
    243 
    244 	if (got)
    245 		return true;
    246 
    247 	return ((*needed)->obj = _rtld_load_library(name, obj, mode)) != NULL;
    248 }
    249 
    250 
    251 /*
    252  * Given a shared object, traverse its list of needed objects, and load
    253  * each of them.  Returns 0 on success.  Generates an error message and
    254  * returns -1 on failure.
    255  */
    256 int
    257 _rtld_load_needed_objects(first, mode)
    258 	Obj_Entry *first;
    259 	int mode;
    260 {
    261 	Obj_Entry *obj;
    262 	int status = 0;
    263 
    264 	for (obj = first; obj != NULL; obj = obj->next) {
    265 		Needed_Entry *needed;
    266 
    267 		for (needed = obj->needed; needed != NULL;
    268 		    needed = needed->next) {
    269 			const char *name = obj->strtab + needed->name;
    270 			if (!_rtld_load_by_name(name, obj, &needed, mode))
    271 				status = -1;	/* FIXME - cleanup */
    272 #ifdef RTLD_LOADER
    273 			if (status == -1)
    274 				return status;
    275 #endif
    276 		}
    277 	}
    278 
    279 	return status;
    280 }
    281 
    282 #ifdef RTLD_LOADER
    283 int
    284 _rtld_preload(preload_path)
    285 	const char *preload_path;
    286 {
    287 	const char *path;
    288 	char *cp, *buf;
    289 	int status = 0;
    290 
    291 	if (preload_path != NULL) {
    292 		cp = buf = xstrdup(preload_path);
    293 		while ((path = strsep(&cp, " :")) != NULL && status == 0) {
    294 			if (!_rtld_load_object(xstrdup(path), RTLD_GLOBAL))
    295 				status = -1;
    296 			else
    297 				dbg((" preloaded \"%s\"", path));
    298 		}
    299 		free(buf);
    300 	}
    301 
    302 	return (status);
    303 }
    304 #endif
    305