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