Home | History | Annotate | Line # | Download | only in ld.elf_so
search.c revision 1.3
      1 /*	$NetBSD: search.c,v 1.3 1997/02/17 19:32:05 cgd 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 #define CONCAT(x,y)     __CONCAT(x,y)
     56 #define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
     57 #define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
     58 #define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
     59 #define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
     60 
     61 /*
     62  * Data declarations.
     63  */
     64 
     65 static bool
     66 _rtld_check_library(
     67     const char *pathname)
     68 {
     69     struct stat mystat;
     70     Elf_Ehdr ehdr;
     71     int fd;
     72 
     73     if (stat(pathname, &mystat) >= 0 && S_ISREG(mystat.st_mode)) {
     74 	if ((fd = open(pathname, O_RDONLY)) >= 0) {
     75 	    if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
     76 		goto lose;
     77 
     78 	    /* Elf_e_ident includes class */
     79 	    if (memcmp(Elf_e_ident, ehdr.e_ident, Elf_e_siz) != 0)
     80 		goto lose;
     81 
     82 	    switch (ehdr.e_machine) {
     83 	    ELFDEFNNAME(MACHDEP_ID_CASES)
     84 
     85 	    default:
     86 		goto lose;
     87 	    }
     88 
     89 	    if (ehdr.e_ident[Elf_ei_version] != Elf_ev_current ||
     90 	      ehdr.e_version != Elf_ev_current ||
     91 	      ehdr.e_ident[Elf_ei_data] != ELFDEFNNAME(MACHDEP_ENDIANNESS) ||
     92 	      ehdr.e_type != Elf_et_dyn)
     93 		goto lose;
     94 
     95 	    close(fd);
     96 	    return true;
     97 
     98 lose:
     99 	    close(fd);
    100 	}
    101     }
    102 
    103     return false;
    104 }
    105 
    106 
    107 static char *
    108 _rtld_search_library_path(const char *name, int namelen, const char *dir, int dirlen)
    109 {
    110 	char *pathname;
    111 
    112 	pathname = xmalloc(dirlen + 1 + namelen + 1);
    113 	strncpy(pathname, dir, dirlen);
    114 	pathname[dirlen] = '/';
    115 	strcpy(pathname + dirlen + 1, name);
    116 
    117 	dbg("  Trying \"%s\"", pathname);
    118 	if(_rtld_check_library(pathname))		/* We found it */
    119 		return pathname;
    120 
    121 	free(pathname);
    122 	return NULL;
    123 }
    124 /*
    125  * Find the library with the given name, and return its full pathname.
    126  * The returned string is dynamically allocated.  Generates an error
    127  * message and returns NULL if the library cannot be found.
    128  *
    129  * If the second argument is non-NULL, then it refers to an already-
    130  * loaded shared object, whose library search path will be searched.
    131  */
    132 char *
    133 _rtld_find_library(
    134     const char *name,
    135     const Obj_Entry *refobj)
    136 {
    137     Search_Path *sp;
    138     char *pathname;
    139     int namelen;
    140 
    141     if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
    142 	if (name[0] != '/' && !_rtld_trust) {
    143 	    _rtld_error("Absolute pathname required for shared object \"%s\"",
    144 			name);
    145 	    return NULL;
    146 	}
    147 #ifdef SVR4_LIBDIR
    148 	if (strncmp(name, SVR4_LIBDIR, SVR4_LIBDIRLEN) == 0
    149 	        && name[SVR4_LIBDIRLEN] == '/') {	/* In "/usr/lib" */
    150 	    /* Map hard-coded "/usr/lib" onto our ELF library directory. */
    151 	    pathname = xmalloc(strlen(name) + LIBDIRLEN - SVR4_LIBDIRLEN + 1);
    152 	    strcpy(pathname, LIBDIR);
    153 	    strcpy(pathname + LIBDIRLEN, name + SVR4_LIBDIRLEN);
    154 	    return pathname;
    155 	}
    156 #endif /* SVR4_LIBDIR */
    157 	return xstrdup(name);
    158     }
    159 
    160     dbg(" Searching for \"%s\" (%p)", name, refobj);
    161 
    162 	namelen = strlen(name);
    163 
    164     if (refobj != NULL)
    165 	for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
    166 		if ((pathname = _rtld_search_library_path(name, namelen,
    167 		    sp->sp_path, sp->sp_pathlen)) != NULL)
    168 			return (pathname);
    169 
    170 	for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
    171 		if ((pathname = _rtld_search_library_path(name, namelen,
    172 		    sp->sp_path, sp->sp_pathlen)) != NULL)
    173 			return (pathname);
    174 
    175 #if 0
    176     if((refobj != NULL &&
    177 	(pathname = _rtld_search_library_path(name, refobj->rpath)) != NULL) ||
    178         (pathname = _rtld_search_library_path(name, ld_library_path)) != NULL
    179 #ifdef SVR4_LIBDIR
    180 	LOSE!
    181 	|| (pathname = _rtld_search_library_path(name, SVR4_LIBDIR)) != NULL
    182 #endif
    183 	)
    184 		return pathname;
    185 #endif
    186 
    187     _rtld_error("Shared object \"%s\" not found", name);
    188     return NULL;
    189 }
    190