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