Home | History | Annotate | Line # | Download | only in ld.elf_so
load.c revision 1.6
      1 /*	$NetBSD: load.c,v 1.6 1999/12/13 09:09:34 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 Obj_Entry *_rtld_load_by_name __P((const char *, Obj_Entry *, bool));
     58 
     59 /*
     60  * Load a shared object into memory, if it is not already loaded.  The
     61  * argument must be a string allocated on the heap.  This function assumes
     62  * responsibility for freeing it when necessary.
     63  *
     64  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
     65  * on failure.
     66  */
     67 Obj_Entry *
     68 _rtld_load_object(filepath, dodebug)
     69 	char *filepath;
     70 	bool dodebug;
     71 {
     72 	Obj_Entry *obj;
     73 	int fd = -1;
     74 	struct stat sb;
     75 
     76 	for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
     77 		if (strcmp(obj->path, filepath) == 0)
     78 			break;
     79 
     80 	/*
     81 	 * If we didn't find a match by pathname, open the file and check
     82 	 * again by device and inode.  This avoids false mismatches caused
     83 	 * by multiple links or ".." in pathnames.
     84 	 *
     85 	 * To avoid a race, we open the file and use fstat() rather than
     86 	 * using stat().
     87 	 */
     88 	if (obj == NULL) {
     89 		if ((fd = open(filepath, O_RDONLY)) == -1) {
     90 			_rtld_error("Cannot open \"%s\"", filepath);
     91 			return NULL;
     92 		}
     93 		if (fstat(fd, &sb) == -1) {
     94 			_rtld_error("Cannot fstat \"%s\"", filepath);
     95 			close(fd);
     96 			return NULL;
     97 		}
     98 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
     99 			if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
    100 				close(fd);
    101 				break;
    102 			}
    103 		}
    104 	}
    105 
    106 	if (obj == NULL) { /* First use of this object, so we must map it in */
    107 		obj = _rtld_map_object(filepath, fd, &sb);
    108 		(void)close(fd);
    109 		if (obj == NULL) {
    110 			free(filepath);
    111 			return NULL;
    112 		}
    113 		obj->path = filepath;
    114 		_rtld_digest_dynamic(obj);
    115 
    116 		*_rtld_objtail = obj;
    117 		_rtld_objtail = &obj->next;
    118 #ifdef RTLD_LOADER
    119 		_rtld_linkmap_add(obj);	/* for GDB */
    120 #endif
    121 		if (dodebug) {
    122 			dbg(("  %p .. %p: %s", obj->mapbase,
    123 			    obj->mapbase + obj->mapsize - 1, obj->path));
    124 			if (obj->textrel)
    125 				dbg(("  WARNING: %s has impure text",
    126 				    obj->path));
    127 		}
    128 	} else
    129 		free(filepath);
    130 
    131 	++obj->refcount;
    132 	return obj;
    133 }
    134 
    135 static Obj_Entry *
    136 _rtld_load_by_name(name, obj, dodebug)
    137 	const char *name;
    138 	Obj_Entry *obj;
    139 	bool dodebug;
    140 {
    141 	Library_Xform *x = _rtld_xforms;
    142 	Obj_Entry *o = NULL;
    143 	size_t i, j;
    144 	int ctlname[2];
    145 	char *libpath;
    146 	union {
    147 		int i;
    148 		char s[16];
    149 	} val;
    150 
    151 	ctlname[0] = CTL_MACHDEP;
    152 	if (dodebug)
    153 		dbg(("load by name %s %p", name, x));
    154 	for (; x; x = x->next) {
    155 		if (strcmp(x->name, name) != 0)
    156 			continue;
    157 
    158 		ctlname[1] = x->ctl;
    159 
    160 		i = sizeof(val);
    161 
    162 		if (sysctl(ctlname, 2, &val, &i, NULL, 0) == -1) {
    163 			warn("sysctl");
    164 			break;
    165 		}
    166 
    167 		switch (x->ctltype) {
    168 		case CTLTYPE_INT:
    169 			xsnprintf(val.s, sizeof(val.s), "%d", val.i);
    170 			break;
    171 		case CTLTYPE_STRING:
    172 			break;
    173 		default:
    174 			warnx("unsupport sysctl type %d", x->ctltype);
    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 		}
    209 
    210 	}
    211 
    212 	if (o != NULL)
    213 		return o;
    214 
    215 	libpath = _rtld_find_library(name, obj);
    216 	if (libpath == NULL)
    217 		return NULL;
    218 	return _rtld_load_object(libpath, true);
    219 
    220 }
    221 
    222 
    223 /*
    224  * Given a shared object, traverse its list of needed objects, and load
    225  * each of them.  Returns 0 on success.  Generates an error message and
    226  * returns -1 on failure.
    227  */
    228 int
    229 _rtld_load_needed_objects(first, dodebug)
    230 	Obj_Entry *first;
    231 	bool dodebug;
    232 {
    233 	Obj_Entry *obj;
    234 	int status = 0;
    235 
    236 	for (obj = first; obj != NULL; obj = obj->next) {
    237 		Needed_Entry *needed;
    238 
    239 		for (needed = obj->needed; needed != NULL;
    240 		    needed = needed->next) {
    241 			const char *name = obj->strtab + needed->name;
    242 			needed->obj = _rtld_load_by_name(name, obj, dodebug);
    243 			if (needed->obj == NULL)
    244 				status = -1;	/* FIXME - cleanup */
    245 #ifdef RTLD_LOADER
    246 			if (status == -1)
    247 				return status;
    248 #endif
    249 		}
    250 	}
    251 
    252 	return status;
    253 }
    254 
    255 #ifdef RTLD_LOADER
    256 int
    257 _rtld_preload(preload_path, dodebug)
    258 	const char *preload_path;
    259 	bool dodebug;
    260 {
    261 	const char *path;
    262 	char *cp, *buf;
    263 	int status = 0;
    264 
    265 	if (preload_path != NULL) {
    266 		cp = buf = xstrdup(preload_path);
    267 		while ((path = strsep(&cp, " ")) != NULL && status == 0) {
    268 			if (_rtld_load_object(xstrdup(path), dodebug) == NULL)
    269 				status = -1;
    270 			else if (dodebug)
    271 				dbg((" preloaded \"%s\"", path));
    272 		}
    273 		free(buf);
    274 	}
    275 
    276 	return (status);
    277 }
    278 #endif
    279