Home | History | Annotate | Line # | Download | only in ld.elf_so
search.c revision 1.9
      1 /*	$NetBSD: search.c,v 1.9 1999/11/04 02:00:18 erh Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by John Polstra.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Dynamic linker for ELF.
     35  *
     36  * John Polstra <jdp (at) polstra.com>.
     37  */
     38 
     39 #include <err.h>
     40 #include <errno.h>
     41 #include <fcntl.h>
     42 #include <stdarg.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 #include <sys/types.h>
     48 #include <sys/mman.h>
     49 #include <sys/stat.h>
     50 #include <dirent.h>
     51 
     52 #include "debug.h"
     53 #include "rtld.h"
     54 
     55 /*
     56  * Data declarations.
     57  */
     58 static bool _rtld_check_library __P((const char *));
     59 static char *_rtld_search_library_path __P((const char *, size_t, const char *,
     60     size_t));
     61 
     62 static bool
     63 _rtld_check_library(pathname)
     64 	const char *pathname;
     65 {
     66 	struct stat mystat;
     67 	Elf_Ehdr ehdr;
     68 	int fd;
     69 
     70 	if (stat(pathname, &mystat) == -1 || !S_ISREG(mystat.st_mode))
     71 		return false;
     72 
     73 	if ((fd = open(pathname, O_RDONLY)) == -1)
     74 		return false;
     75 
     76 	if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
     77 		goto lose;
     78 
     79 	/* Elf_e_ident includes class */
     80 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
     81 	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
     82 		goto lose;
     83 
     84 	switch (ehdr.e_machine) {
     85 		ELFDEFNNAME(MACHDEP_ID_CASES)
     86 	default:
     87 		goto lose;
     88 	}
     89 
     90 	if (ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
     91 	    ehdr.e_version != EV_CURRENT ||
     92 	    ehdr.e_ident[EI_DATA] != ELFDEFNNAME(MACHDEP_ENDIANNESS) ||
     93 	    ehdr.e_type != ET_DYN)
     94 		goto lose;
     95 
     96 	close(fd);
     97 	return true;
     98 
     99 lose:
    100 	close(fd);
    101 	return false;
    102 }
    103 
    104 
    105 static char *
    106 _rtld_search_library_path(name, namelen, dir, dirlen)
    107 	const char *name;
    108 	size_t namelen;
    109 	const char *dir;
    110 	size_t dirlen;
    111 {
    112 	char *pathname;
    113 
    114 	pathname = xmalloc(dirlen + 1 + namelen + 1);
    115 	(void)strncpy(pathname, dir, dirlen);
    116 	pathname[dirlen] = '/';
    117 	strcpy(pathname + dirlen + 1, name);
    118 
    119 	dbg(("  Trying \"%s\"", pathname));
    120 	if (_rtld_check_library(pathname))	/* We found it */
    121 		return pathname;
    122 
    123 	free(pathname);
    124 	return NULL;
    125 }
    126 
    127 /*
    128  * Find the library with the given name, and return its full pathname.
    129  * The returned string is dynamically allocated.  Generates an error
    130  * message and returns NULL if the library cannot be found.
    131  *
    132  * If the second argument is non-NULL, then it refers to an already-
    133  * loaded shared object, whose library search path will be searched.
    134  */
    135 char *
    136 _rtld_find_library(name, refobj)
    137 	const char *name;
    138 	const Obj_Entry *refobj;
    139 {
    140 	Search_Path *sp;
    141 	char *pathname;
    142 	int namelen;
    143 
    144 	if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
    145 		if (name[0] != '/' && !_rtld_trust) {
    146 			_rtld_error(
    147 			"Absolute pathname required for shared object \"%s\"",
    148 			    name);
    149 			return NULL;
    150 		}
    151 #ifdef SVR4_LIBDIR
    152 		if (strncmp(name, SVR4_LIBDIR, SVR4_LIBDIRLEN) == 0
    153 		    && name[SVR4_LIBDIRLEN] == '/') {	/* In "/usr/lib" */
    154 			/*
    155 			 * Map hard-coded "/usr/lib" onto our ELF library
    156 			 * directory.
    157 			 */
    158 			pathname = xmalloc(strlen(name) + LIBDIRLEN -
    159 			    SVR4_LIBDIRLEN + 1);
    160 			(void)strcpy(pathname, LIBDIR);
    161 			(void)strcpy(pathname + LIBDIRLEN, name +
    162 			    SVR4_LIBDIRLEN);
    163 			return pathname;
    164 		}
    165 #endif /* SVR4_LIBDIR */
    166 		return xstrdup(name);
    167 	}
    168 	dbg((" Searching for \"%s\" (%p)", name, refobj));
    169 
    170 	namelen = strlen(name);
    171 
    172 	for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
    173 		if ((pathname = _rtld_search_library_path(name, namelen,
    174 		    sp->sp_path, sp->sp_pathlen)) != NULL)
    175 			return (pathname);
    176 
    177 	if (refobj != NULL)
    178 		for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
    179 			if ((pathname = _rtld_search_library_path(name,
    180 			    namelen, sp->sp_path, sp->sp_pathlen)) != NULL)
    181 				return (pathname);
    182 
    183 	for (sp = _rtld_default_paths; sp != NULL; sp = sp->sp_next)
    184 		if ((pathname = _rtld_search_library_path(name, namelen,
    185 		    sp->sp_path, sp->sp_pathlen)) != NULL)
    186 			return (pathname);
    187 
    188 #if 0
    189 	if ((refobj != NULL &&
    190 	    (pathname = _rtld_search_library_path(name,
    191 	    refobj->rpath)) != NULL) ||
    192 	    (pathname = _rtld_search_library_path(name,
    193 	    ld_library_path)) != NULL
    194 #ifdef SVR4_LIBDIR
    195 	    LOSE !
    196 	    ||(pathname = _rtld_search_library_path(name, SVR4_LIBDIR)) != NULL
    197 #endif
    198 	    )
    199 		return pathname;
    200 #endif
    201 
    202 	_rtld_error("Shared object \"%s\" not found", name);
    203 	return NULL;
    204 }
    205