Home | History | Annotate | Line # | Download | only in ld.elf_so
load.c revision 1.12
      1  1.12   mycroft /*	$NetBSD: load.c,v 1.12 2000/11/10 21:31:30 mycroft Exp $	 */
      2   1.1       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright 1996 John D. Polstra.
      5   1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6   1.1       cgd  * All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18   1.1       cgd  *      This product includes software developed by John Polstra.
     19   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       cgd  *    derived from this software without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34   1.1       cgd /*
     35   1.1       cgd  * Dynamic linker for ELF.
     36   1.1       cgd  *
     37   1.1       cgd  * John Polstra <jdp (at) polstra.com>.
     38   1.1       cgd  */
     39   1.1       cgd 
     40   1.1       cgd #include <err.h>
     41   1.1       cgd #include <errno.h>
     42   1.1       cgd #include <fcntl.h>
     43   1.1       cgd #include <stdarg.h>
     44   1.1       cgd #include <stdio.h>
     45   1.1       cgd #include <stdlib.h>
     46   1.1       cgd #include <string.h>
     47   1.1       cgd #include <unistd.h>
     48   1.1       cgd #include <sys/types.h>
     49   1.6  christos #include <sys/param.h>
     50   1.1       cgd #include <sys/mman.h>
     51   1.6  christos #include <sys/sysctl.h>
     52   1.1       cgd #include <dirent.h>
     53   1.1       cgd 
     54   1.1       cgd #include "debug.h"
     55   1.1       cgd #include "rtld.h"
     56   1.1       cgd 
     57   1.7  christos static bool _rtld_load_by_name __P((const char *, Obj_Entry *, Needed_Entry **,
     58  1.12   mycroft     int, bool));
     59   1.6  christos 
     60   1.1       cgd /*
     61   1.1       cgd  * Load a shared object into memory, if it is not already loaded.  The
     62   1.1       cgd  * argument must be a string allocated on the heap.  This function assumes
     63   1.1       cgd  * responsibility for freeing it when necessary.
     64   1.1       cgd  *
     65   1.1       cgd  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
     66   1.1       cgd  * on failure.
     67   1.1       cgd  */
     68   1.1       cgd Obj_Entry *
     69  1.12   mycroft _rtld_load_object(filepath, mode, dodebug)
     70   1.3  christos 	char *filepath;
     71  1.12   mycroft 	int mode;
     72   1.3  christos 	bool dodebug;
     73   1.1       cgd {
     74   1.3  christos 	Obj_Entry *obj;
     75   1.5   mycroft 	int fd = -1;
     76   1.5   mycroft 	struct stat sb;
     77   1.1       cgd 
     78   1.3  christos 	for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
     79   1.3  christos 		if (strcmp(obj->path, filepath) == 0)
     80   1.3  christos 			break;
     81   1.3  christos 
     82   1.5   mycroft 	/*
     83   1.5   mycroft 	 * If we didn't find a match by pathname, open the file and check
     84   1.5   mycroft 	 * again by device and inode.  This avoids false mismatches caused
     85   1.5   mycroft 	 * by multiple links or ".." in pathnames.
     86   1.5   mycroft 	 *
     87   1.5   mycroft 	 * To avoid a race, we open the file and use fstat() rather than
     88   1.5   mycroft 	 * using stat().
     89   1.5   mycroft 	 */
     90   1.5   mycroft 	if (obj == NULL) {
     91   1.3  christos 		if ((fd = open(filepath, O_RDONLY)) == -1) {
     92   1.3  christos 			_rtld_error("Cannot open \"%s\"", filepath);
     93   1.3  christos 			return NULL;
     94   1.3  christos 		}
     95   1.5   mycroft 		if (fstat(fd, &sb) == -1) {
     96   1.5   mycroft 			_rtld_error("Cannot fstat \"%s\"", filepath);
     97   1.5   mycroft 			close(fd);
     98   1.5   mycroft 			return NULL;
     99   1.5   mycroft 		}
    100   1.5   mycroft 		for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next) {
    101   1.5   mycroft 			if (obj->ino == sb.st_ino && obj->dev == sb.st_dev) {
    102   1.5   mycroft 				close(fd);
    103   1.5   mycroft 				break;
    104   1.5   mycroft 			}
    105   1.5   mycroft 		}
    106   1.5   mycroft 	}
    107   1.5   mycroft 
    108   1.5   mycroft 	if (obj == NULL) { /* First use of this object, so we must map it in */
    109   1.5   mycroft 		obj = _rtld_map_object(filepath, fd, &sb);
    110   1.3  christos 		(void)close(fd);
    111   1.3  christos 		if (obj == NULL) {
    112   1.3  christos 			free(filepath);
    113   1.3  christos 			return NULL;
    114   1.3  christos 		}
    115   1.3  christos 		obj->path = filepath;
    116   1.3  christos 		_rtld_digest_dynamic(obj);
    117   1.1       cgd 
    118   1.3  christos 		*_rtld_objtail = obj;
    119   1.3  christos 		_rtld_objtail = &obj->next;
    120   1.1       cgd #ifdef RTLD_LOADER
    121   1.3  christos 		_rtld_linkmap_add(obj);	/* for GDB */
    122   1.1       cgd #endif
    123   1.3  christos 		if (dodebug) {
    124   1.3  christos 			dbg(("  %p .. %p: %s", obj->mapbase,
    125   1.3  christos 			    obj->mapbase + obj->mapsize - 1, obj->path));
    126   1.3  christos 			if (obj->textrel)
    127   1.3  christos 				dbg(("  WARNING: %s has impure text",
    128   1.3  christos 				    obj->path));
    129   1.3  christos 		}
    130   1.3  christos 	} else
    131   1.3  christos 		free(filepath);
    132   1.1       cgd 
    133   1.3  christos 	++obj->refcount;
    134  1.12   mycroft 	if ((mode & RTLD_GLOBAL) &&
    135  1.12   mycroft 	    _rtld_objlist_find(&_rtld_list_global, obj) == NULL)
    136  1.12   mycroft 		_rtld_objlist_add(&_rtld_list_global, obj);
    137   1.3  christos 	return obj;
    138   1.1       cgd }
    139   1.1       cgd 
    140   1.7  christos static bool
    141  1.12   mycroft _rtld_load_by_name(name, obj, needed, mode, dodebug)
    142   1.6  christos 	const char *name;
    143   1.6  christos 	Obj_Entry *obj;
    144   1.7  christos 	Needed_Entry **needed;
    145  1.12   mycroft 	int mode;
    146   1.6  christos 	bool dodebug;
    147   1.6  christos {
    148   1.6  christos 	Library_Xform *x = _rtld_xforms;
    149   1.6  christos 	Obj_Entry *o = NULL;
    150   1.6  christos 	size_t i, j;
    151   1.6  christos 	char *libpath;
    152   1.7  christos 	bool got = false;
    153   1.6  christos 	union {
    154   1.6  christos 		int i;
    155   1.6  christos 		char s[16];
    156   1.6  christos 	} val;
    157   1.6  christos 
    158   1.6  christos 	if (dodebug)
    159   1.6  christos 		dbg(("load by name %s %p", name, x));
    160   1.6  christos 	for (; x; x = x->next) {
    161   1.6  christos 		if (strcmp(x->name, name) != 0)
    162   1.6  christos 			continue;
    163   1.6  christos 
    164   1.6  christos 		i = sizeof(val);
    165   1.6  christos 
    166   1.9  christos 		if (sysctl(x->ctl, x->ctlmax, &val, &i, NULL, 0) == -1) {
    167   1.6  christos 			warn("sysctl");
    168   1.6  christos 			break;
    169   1.6  christos 		}
    170   1.6  christos 
    171   1.9  christos 		switch (x->ctltype[x->ctlmax - 1]) {
    172   1.6  christos 		case CTLTYPE_INT:
    173   1.6  christos 			xsnprintf(val.s, sizeof(val.s), "%d", val.i);
    174   1.6  christos 			break;
    175   1.6  christos 		case CTLTYPE_STRING:
    176   1.6  christos 			break;
    177   1.6  christos 		default:
    178  1.11  christos 			xwarnx("unsupported sysctl type %d",
    179   1.9  christos 			    x->ctltype[x->ctlmax - 1]);
    180   1.6  christos 			break;
    181   1.6  christos 		}
    182   1.6  christos 
    183   1.6  christos 		if (dodebug)
    184   1.6  christos 			dbg(("sysctl returns %s", val.s));
    185   1.6  christos 
    186   1.6  christos 		for (i = 0; i < RTLD_MAX_ENTRY && x->entry[i].value != NULL;
    187   1.6  christos 		    i++) {
    188   1.8  christos 			if (dodebug)
    189  1.10  christos 				dbg(("entry %ld", (unsigned long)i));
    190   1.6  christos 			if (strcmp(x->entry[i].value, val.s) == 0)
    191   1.6  christos 				break;
    192   1.6  christos 		}
    193   1.6  christos 
    194   1.6  christos 		if (i == RTLD_MAX_ENTRY) {
    195  1.11  christos 			xwarnx("sysctl value %s not found for lib%s",
    196   1.6  christos 			    val.s, name);
    197   1.6  christos 			break;
    198   1.6  christos 		}
    199   1.6  christos 		/* XXX: This can mess up debuggers, cause we lie about
    200   1.6  christos 		 * what we loaded in the needed objects */
    201   1.6  christos 		for (j = 0; j < RTLD_MAX_LIBRARY &&
    202   1.6  christos 		    x->entry[i].library[j] != NULL; j++) {
    203   1.6  christos 			libpath = _rtld_find_library(
    204   1.6  christos 			    x->entry[i].library[j], obj);
    205   1.6  christos 			if (libpath == NULL) {
    206  1.11  christos 				xwarnx("could not load %s for %s",
    207   1.6  christos 				    x->entry[i].library[j], name);
    208   1.6  christos 				continue;
    209   1.6  christos 			}
    210  1.12   mycroft 			o = _rtld_load_object(libpath, mode, true);
    211   1.6  christos 			if (o == NULL)
    212   1.6  christos 				continue;
    213   1.7  christos 			got = true;
    214   1.7  christos 			if (j == 0)
    215   1.7  christos 				(*needed)->obj = o;
    216   1.7  christos 			else {
    217   1.7  christos 				/* make a new one and put it in the chain */
    218   1.7  christos 				Needed_Entry *ne = xmalloc(sizeof(*ne));
    219   1.7  christos 				ne->name = (*needed)->name;
    220   1.7  christos 				ne->obj = o;
    221   1.7  christos 				ne->next = (*needed)->next;
    222   1.7  christos 				(*needed)->next = ne;
    223   1.7  christos 				*needed = ne;
    224   1.7  christos 			}
    225   1.7  christos 
    226   1.6  christos 		}
    227   1.6  christos 
    228   1.6  christos 	}
    229   1.6  christos 
    230   1.7  christos 	if (got)
    231   1.7  christos 		return true;
    232   1.6  christos 
    233   1.6  christos 	libpath = _rtld_find_library(name, obj);
    234   1.6  christos 	if (libpath == NULL)
    235   1.7  christos 		return false;
    236  1.12   mycroft 	return ((*needed)->obj = _rtld_load_object(libpath, mode, true)) != NULL;
    237   1.6  christos }
    238   1.6  christos 
    239   1.6  christos 
    240   1.1       cgd /*
    241   1.1       cgd  * Given a shared object, traverse its list of needed objects, and load
    242   1.1       cgd  * each of them.  Returns 0 on success.  Generates an error message and
    243   1.1       cgd  * returns -1 on failure.
    244   1.1       cgd  */
    245   1.1       cgd int
    246  1.12   mycroft _rtld_load_needed_objects(first, mode, dodebug)
    247   1.3  christos 	Obj_Entry *first;
    248  1.12   mycroft 	int mode;
    249   1.6  christos 	bool dodebug;
    250   1.1       cgd {
    251   1.3  christos 	Obj_Entry *obj;
    252   1.3  christos 	int status = 0;
    253   1.1       cgd 
    254   1.3  christos 	for (obj = first; obj != NULL; obj = obj->next) {
    255   1.3  christos 		Needed_Entry *needed;
    256   1.1       cgd 
    257   1.3  christos 		for (needed = obj->needed; needed != NULL;
    258   1.3  christos 		    needed = needed->next) {
    259   1.3  christos 			const char *name = obj->strtab + needed->name;
    260  1.12   mycroft 			if (!_rtld_load_by_name(name, obj, &needed, mode,
    261  1.12   mycroft 			    dodebug))
    262   1.6  christos 				status = -1;	/* FIXME - cleanup */
    263   1.1       cgd #ifdef RTLD_LOADER
    264   1.3  christos 			if (status == -1)
    265   1.3  christos 				return status;
    266   1.1       cgd #endif
    267   1.3  christos 		}
    268   1.1       cgd 	}
    269   1.1       cgd 
    270   1.3  christos 	return status;
    271   1.1       cgd }
    272   1.4    kleink 
    273   1.4    kleink #ifdef RTLD_LOADER
    274   1.4    kleink int
    275   1.4    kleink _rtld_preload(preload_path, dodebug)
    276   1.4    kleink 	const char *preload_path;
    277   1.4    kleink 	bool dodebug;
    278   1.4    kleink {
    279   1.4    kleink 	const char *path;
    280   1.4    kleink 	char *cp, *buf;
    281   1.4    kleink 	int status = 0;
    282   1.4    kleink 
    283   1.4    kleink 	if (preload_path != NULL) {
    284   1.4    kleink 		cp = buf = xstrdup(preload_path);
    285   1.4    kleink 		while ((path = strsep(&cp, " ")) != NULL && status == 0) {
    286  1.12   mycroft 			if (_rtld_load_object(xstrdup(path), RTLD_GLOBAL,
    287  1.12   mycroft 			    dodebug) == NULL)
    288   1.4    kleink 				status = -1;
    289   1.4    kleink 			else if (dodebug)
    290   1.4    kleink 				dbg((" preloaded \"%s\"", path));
    291   1.4    kleink 		}
    292   1.4    kleink 		free(buf);
    293   1.4    kleink 	}
    294   1.4    kleink 
    295   1.4    kleink 	return (status);
    296   1.4    kleink }
    297   1.4    kleink #endif
    298