Home | History | Annotate | Line # | Download | only in ld.elf_so
load.c revision 1.4
      1 /*	$NetBSD: load.c,v 1.4 1999/05/31 14:48:16 kleink 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/mman.h>
     50 #include <dirent.h>
     51 
     52 #include "debug.h"
     53 #include "rtld.h"
     54 
     55 /*
     56  * Load a shared object into memory, if it is not already loaded.  The
     57  * argument must be a string allocated on the heap.  This function assumes
     58  * responsibility for freeing it when necessary.
     59  *
     60  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
     61  * on failure.
     62  */
     63 Obj_Entry *
     64 _rtld_load_object(filepath, dodebug)
     65 	char *filepath;
     66 	bool dodebug;
     67 {
     68 	Obj_Entry *obj;
     69 
     70 	for (obj = _rtld_objlist->next; obj != NULL; obj = obj->next)
     71 		if (strcmp(obj->path, filepath) == 0)
     72 			break;
     73 
     74 	if (obj == NULL) { /* First use of this object, so we must map it in */
     75 		int fd;
     76 
     77 		if ((fd = open(filepath, O_RDONLY)) == -1) {
     78 			_rtld_error("Cannot open \"%s\"", filepath);
     79 			return NULL;
     80 		}
     81 		obj = _rtld_map_object(filepath, fd);
     82 		(void)close(fd);
     83 		if (obj == NULL) {
     84 			free(filepath);
     85 			return NULL;
     86 		}
     87 		obj->path = filepath;
     88 		_rtld_digest_dynamic(obj);
     89 
     90 		*_rtld_objtail = obj;
     91 		_rtld_objtail = &obj->next;
     92 #ifdef RTLD_LOADER
     93 		_rtld_linkmap_add(obj);	/* for GDB */
     94 #endif
     95 		if (dodebug) {
     96 			dbg(("  %p .. %p: %s", obj->mapbase,
     97 			    obj->mapbase + obj->mapsize - 1, obj->path));
     98 			if (obj->textrel)
     99 				dbg(("  WARNING: %s has impure text",
    100 				    obj->path));
    101 		}
    102 	} else
    103 		free(filepath);
    104 
    105 	++obj->refcount;
    106 	return obj;
    107 }
    108 
    109 /*
    110  * Given a shared object, traverse its list of needed objects, and load
    111  * each of them.  Returns 0 on success.  Generates an error message and
    112  * returns -1 on failure.
    113  */
    114 int
    115 _rtld_load_needed_objects(first)
    116 	Obj_Entry *first;
    117 {
    118 	Obj_Entry *obj;
    119 	int status = 0;
    120 
    121 	for (obj = first; obj != NULL; obj = obj->next) {
    122 		Needed_Entry *needed;
    123 
    124 		for (needed = obj->needed; needed != NULL;
    125 		    needed = needed->next) {
    126 			const char *name = obj->strtab + needed->name;
    127 			char *libpath = _rtld_find_library(name, obj);
    128 
    129 			if (libpath == NULL) {
    130 				status = -1;
    131 			} else {
    132 				needed->obj = _rtld_load_object(libpath, true);
    133 				if (needed->obj == NULL)
    134 					status = -1;	/* FIXME - cleanup */
    135 			}
    136 #ifdef RTLD_LOADER
    137 			if (status == -1)
    138 				return status;
    139 #endif
    140 		}
    141 	}
    142 
    143 	return status;
    144 }
    145 
    146 #ifdef RTLD_LOADER
    147 int
    148 _rtld_preload(preload_path, dodebug)
    149 	const char *preload_path;
    150 	bool dodebug;
    151 {
    152 	const char *path;
    153 	char *cp, *buf;
    154 	int status = 0;
    155 
    156 	if (preload_path != NULL) {
    157 		cp = buf = xstrdup(preload_path);
    158 		while ((path = strsep(&cp, " ")) != NULL && status == 0) {
    159 			if (_rtld_load_object(xstrdup(path), dodebug) == NULL)
    160 				status = -1;
    161 			else if (dodebug)
    162 				dbg((" preloaded \"%s\"", path));
    163 		}
    164 		free(buf);
    165 	}
    166 
    167 	return (status);
    168 }
    169 #endif
    170