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