Home | History | Annotate | Line # | Download | only in ld.elf_so
search.c revision 1.2
      1  1.2  cgd /*	$NetBSD: search.c,v 1.2 1997/02/03 19:45:02 cgd 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.1  cgd 
     55  1.1  cgd #define CONCAT(x,y)     __CONCAT(x,y)
     56  1.1  cgd #define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
     57  1.1  cgd #define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
     58  1.1  cgd #define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
     59  1.1  cgd #define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
     60  1.1  cgd 
     61  1.1  cgd /*
     62  1.1  cgd  * Data declarations.
     63  1.1  cgd  */
     64  1.1  cgd 
     65  1.1  cgd typedef struct {
     66  1.1  cgd     const char *si_name;
     67  1.1  cgd     const char *si_best_name;
     68  1.1  cgd     char *si_best_fullpath;
     69  1.1  cgd     const Search_Path *si_best_path;
     70  1.1  cgd     size_t si_namelen;
     71  1.1  cgd     int si_desired_major;
     72  1.1  cgd     int si_desired_minor;
     73  1.1  cgd     int si_best_major;
     74  1.1  cgd     int si_best_minor;
     75  1.1  cgd     unsigned si_exact : 1;
     76  1.1  cgd } Search_Info;
     77  1.1  cgd 
     78  1.1  cgd typedef enum {
     79  1.1  cgd     Search_FoundNothing,
     80  1.1  cgd     Search_FoundLower,
     81  1.1  cgd     Search_FoundHigher,
     82  1.1  cgd     Search_FoundExact
     83  1.1  cgd } Search_Result;
     84  1.1  cgd 
     85  1.1  cgd static bool
     86  1.1  cgd _rtld_check_library(
     87  1.1  cgd     const Search_Path *sp,
     88  1.1  cgd     const char *name,
     89  1.1  cgd     size_t namelen,
     90  1.1  cgd     char **fullpath_p)
     91  1.1  cgd {
     92  1.1  cgd     struct stat mystat;
     93  1.1  cgd     char *fullpath;
     94  1.1  cgd     Elf_Ehdr ehdr;
     95  1.1  cgd     int fd;
     96  1.1  cgd 
     97  1.1  cgd     fullpath = xmalloc(sp->sp_pathlen + 1 + namelen + 1);
     98  1.1  cgd     strncpy(fullpath, sp->sp_path, sp->sp_pathlen);
     99  1.1  cgd     fullpath[sp->sp_pathlen] = '/';
    100  1.1  cgd     strcpy(&fullpath[sp->sp_pathlen + 1], name);
    101  1.1  cgd 
    102  1.1  cgd     dbg("  Trying \"%s\"", fullpath);
    103  1.1  cgd     if (stat(fullpath, &mystat) >= 0 && S_ISREG(mystat.st_mode)) {
    104  1.1  cgd 	if ((fd = open(fullpath, O_RDONLY)) >= 0) {
    105  1.1  cgd 	    if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
    106  1.1  cgd 		goto lose;
    107  1.1  cgd 
    108  1.1  cgd 	    /* Elf_e_ident includes class */
    109  1.1  cgd 	    if (memcmp(Elf_e_ident, ehdr.e_ident, Elf_e_siz) != 0)
    110  1.1  cgd 		goto lose;
    111  1.1  cgd 
    112  1.1  cgd 	    switch (ehdr.e_machine) {
    113  1.1  cgd 	    ELFDEFNNAME(MACHDEP_ID_CASES)
    114  1.1  cgd 
    115  1.1  cgd 	    default:
    116  1.1  cgd 		goto lose;
    117  1.1  cgd 	    }
    118  1.1  cgd 
    119  1.1  cgd 	    if (ehdr.e_ident[Elf_ei_version] != Elf_ev_current ||
    120  1.1  cgd 	      ehdr.e_version != Elf_ev_current ||
    121  1.1  cgd 	      ehdr.e_ident[Elf_ei_data] != ELFDEFNNAME(MACHDEP_ENDIANNESS) ||
    122  1.1  cgd 	      ehdr.e_type != Elf_et_dyn)
    123  1.1  cgd 		goto lose;
    124  1.1  cgd 
    125  1.1  cgd 	    if (*fullpath_p != NULL)
    126  1.1  cgd 		free(*fullpath_p);
    127  1.1  cgd 	    *fullpath_p = fullpath;
    128  1.2  cgd 	    close(fd);
    129  1.1  cgd 	    return true;
    130  1.1  cgd 
    131  1.1  cgd lose:
    132  1.1  cgd 	    close(fd);
    133  1.1  cgd 	}
    134  1.1  cgd     }
    135  1.1  cgd 
    136  1.1  cgd     free(fullpath);
    137  1.1  cgd     return false;
    138  1.1  cgd }
    139  1.1  cgd 
    140  1.1  cgd static Search_Result
    141  1.1  cgd _rtld_search_directory(
    142  1.1  cgd     const Search_Path *sp,
    143  1.1  cgd     Search_Info *si)
    144  1.1  cgd {
    145  1.1  cgd     struct dirent *entry;
    146  1.1  cgd     DIR *dirp;
    147  1.1  cgd     Search_Result result = Search_FoundNothing;
    148  1.1  cgd 
    149  1.1  cgd dbg("_rtld_search_directory");
    150  1.1  cgd     if (sp->sp_path == NULL || sp->sp_path[0] == '\0')
    151  1.1  cgd 	return result;
    152  1.1  cgd 
    153  1.1  cgd dbg("_rtld_search_directory 2");
    154  1.1  cgd     if ((dirp = opendir(sp->sp_path)) == NULL) {
    155  1.1  cgd 	dbg("_rtld_search_directory 2.1");
    156  1.1  cgd 	return result;
    157  1.1  cgd     }
    158  1.1  cgd 
    159  1.1  cgd dbg("_rtld_search_directory 3");
    160  1.1  cgd     while ((entry = readdir(dirp)) != NULL) {
    161  1.1  cgd 	long major = -1;
    162  1.1  cgd 	long minor = -1;
    163  1.1  cgd 	if (strncmp(entry->d_name, si->si_name, si->si_namelen))
    164  1.1  cgd 	    continue;
    165  1.1  cgd 	/*
    166  1.1  cgd 	 * We are matching libfoo.so only (no more info).  Only take
    167  1.1  cgd 	 * it as a last resort.
    168  1.1  cgd 	 */
    169  1.1  cgd 	if (si->si_exact) {
    170  1.1  cgd 	    if (strcmp(entry->d_name, si->si_name))
    171  1.1  cgd 		continue;
    172  1.1  cgd #ifdef notyet
    173  1.1  cgd 	} else if (entry->d_namlen == si->si_namelen) {
    174  1.1  cgd 	    if (si->si_best_path != NULL || si->si_best_major != -1)
    175  1.1  cgd 		continue;
    176  1.1  cgd #endif
    177  1.1  cgd 	} else {
    178  1.1  cgd 	    char *cp;
    179  1.1  cgd 	    /*
    180  1.1  cgd 	     * We expect (demand!) that it be of the form
    181  1.1  cgd 	     * "libfoo.so.<something>"
    182  1.1  cgd 	     */
    183  1.1  cgd 	    if (entry->d_name[si->si_namelen] != '.')
    184  1.1  cgd 		continue;
    185  1.1  cgd 	    /*
    186  1.1  cgd 	     * This file has a least a major number (well, maybe not if it
    187  1.1  cgd 	     * has a name of "libfoo.so." but treat that as equivalent to 0.
    188  1.1  cgd 	     * It had better match what we are looking for.
    189  1.1  cgd 	     */
    190  1.1  cgd 	    major = strtol(&entry->d_name[si->si_namelen+1], &cp, 10);
    191  1.1  cgd 	    if (major < 0 || (cp[0] != '\0' && cp[0] != '.')
    192  1.1  cgd 		    || &entry->d_name[si->si_namelen+1] == cp)
    193  1.1  cgd 		continue;
    194  1.1  cgd 	    if (cp[0] == '.') {
    195  1.1  cgd 		char *cp2;
    196  1.1  cgd 		minor = strtol(&cp[1], &cp2, 10);
    197  1.1  cgd 		if (minor < 0 || cp2[0] != '\0' || cp == cp2)
    198  1.1  cgd 		    continue;
    199  1.1  cgd 	    } else {
    200  1.1  cgd 		minor = 0;
    201  1.1  cgd 	    }
    202  1.1  cgd 	    if (major != si->si_desired_major || minor <= si->si_best_minor)
    203  1.1  cgd 		continue;
    204  1.1  cgd 	}
    205  1.1  cgd 	/*
    206  1.1  cgd 	 * We have a better candidate...
    207  1.1  cgd 	 */
    208  1.1  cgd 	if (!_rtld_check_library(sp, entry->d_name, entry->d_namlen,
    209  1.1  cgd 			   &si->si_best_fullpath))
    210  1.1  cgd 	    continue;
    211  1.1  cgd 
    212  1.1  cgd 	si->si_best_name = &si->si_best_fullpath[sp->sp_pathlen + 1];
    213  1.1  cgd 	si->si_best_major = major;
    214  1.1  cgd 	si->si_best_minor = minor;
    215  1.1  cgd 	si->si_best_path = sp;
    216  1.1  cgd 
    217  1.1  cgd 	if (si->si_exact || si->si_best_minor == si->si_desired_minor)
    218  1.1  cgd 	    result = Search_FoundExact;
    219  1.1  cgd 	else if (si->si_best_minor > si->si_desired_minor)
    220  1.1  cgd 	    result = Search_FoundHigher;
    221  1.1  cgd 	else
    222  1.1  cgd 	    result = Search_FoundLower;
    223  1.1  cgd 
    224  1.1  cgd 	/*
    225  1.1  cgd 	 * We were looking for, and found, an exact match.  We're done.
    226  1.1  cgd 	 */
    227  1.1  cgd 	if (si->si_exact)
    228  1.1  cgd 	    break;
    229  1.1  cgd     }
    230  1.1  cgd 
    231  1.1  cgd     dbg("found %s (%d.%d) match for %s (%d.%d) -> %s",
    232  1.1  cgd 	result == Search_FoundNothing ? "no"
    233  1.1  cgd 	: result == Search_FoundLower ? "lower"
    234  1.1  cgd 	: result == Search_FoundExact ? "exact" : "higher",
    235  1.1  cgd 	si->si_best_major, si->si_best_minor,
    236  1.1  cgd 	si->si_name,
    237  1.1  cgd 	si->si_desired_major, si->si_desired_minor,
    238  1.1  cgd 	si->si_best_fullpath ? si->si_best_fullpath : sp->sp_path);
    239  1.1  cgd 
    240  1.1  cgd     closedir(dirp);
    241  1.1  cgd     return result;
    242  1.1  cgd }
    243  1.1  cgd 
    244  1.1  cgd static char *
    246  1.1  cgd _rtld_search_library_paths(
    247  1.1  cgd     const char *name,
    248  1.1  cgd     Search_Path *paths,
    249  1.1  cgd     const Search_Path *rpaths)
    250  1.1  cgd {
    251  1.1  cgd     Search_Info info;
    252  1.1  cgd     Search_Path *path;
    253  1.1  cgd     const char *cp;
    254  1.1  cgd     Search_Result result = Search_FoundNothing;
    255  1.1  cgd 
    256  1.1  cgd     memset(&info, 0, sizeof(info));
    257  1.1  cgd     info.si_name = name;
    258  1.1  cgd     info.si_desired_major = -1;
    259  1.1  cgd     info.si_desired_minor = -1;
    260  1.1  cgd     info.si_best_major = -1;
    261  1.1  cgd     info.si_best_minor = -1;
    262  1.1  cgd 
    263  1.1  cgd     cp = strstr(name, ".so");
    264  1.1  cgd     if (cp == NULL) {
    265  1.1  cgd 	info.si_exact = true;
    266  1.1  cgd     } else {
    267  1.1  cgd 	cp += sizeof(".so") - 1;
    268  1.1  cgd 	info.si_namelen = cp - name;
    269  1.1  cgd 	if (cp[0] != '.') {
    270  1.1  cgd 	    info.si_exact = true;
    271  1.1  cgd 	} else {
    272  1.1  cgd 	    info.si_desired_major = atoi(&cp[1]);
    273  1.1  cgd 	    if ((cp = strchr(&cp[1], '.')) != NULL) {
    274  1.1  cgd 		info.si_desired_minor = atoi(&cp[1]);
    275  1.1  cgd 	    } else {
    276  1.1  cgd 		info.si_desired_minor = 0;
    277  1.1  cgd 	    }
    278  1.1  cgd 	}
    279  1.1  cgd     }
    280  1.1  cgd 
    281  1.1  cgd     if (rpaths != NULL && result < Search_FoundHigher) {	/* Exact? */
    282  1.1  cgd 	dbg("  checking rpaths..");
    283  1.1  cgd 	for (; rpaths != NULL; rpaths = rpaths->sp_next) {
    284  1.1  cgd 	    dbg("   in \"%s\"", rpaths->sp_path);
    285  1.1  cgd 	    result = _rtld_search_directory(rpaths, &info);
    286  1.1  cgd 	    if (result >= Search_FoundHigher)	/* Exact? */
    287  1.1  cgd 		break;
    288  1.1  cgd 	}
    289  1.1  cgd     }
    290  1.1  cgd     if (result < Search_FoundHigher) {	/* Exact? */
    291  1.1  cgd 	dbg("  checking default paths..");
    292  1.1  cgd 	for (path = paths; path != NULL; path = path->sp_next) {
    293  1.1  cgd 	    dbg("   in \"%s\"", path->sp_path);
    294  1.1  cgd 	    result = _rtld_search_directory(path, &info);
    295  1.1  cgd 	    if (result >= Search_FoundHigher)	/* Exact? */
    296  1.1  cgd 		break;
    297  1.1  cgd 	}
    298  1.1  cgd     }
    299  1.1  cgd 
    300  1.1  cgd     if (result >= Search_FoundHigher)
    301  1.1  cgd 	return info.si_best_fullpath;
    302  1.1  cgd 
    303  1.1  cgd     if (info.si_best_fullpath != NULL)
    304  1.1  cgd 	free(info.si_best_fullpath);
    305  1.1  cgd     return NULL;
    306  1.1  cgd }
    307  1.1  cgd 
    308  1.1  cgd /*
    309  1.1  cgd  * Find the library with the given name, and return its full pathname.
    310  1.1  cgd  * The returned string is dynamically allocated.  Generates an error
    311  1.1  cgd  * message and returns NULL if the library cannot be found.
    312  1.1  cgd  *
    313  1.1  cgd  * If the second argument is non-NULL, then it refers to an already-
    314  1.1  cgd  * loaded shared object, whose library search path will be searched.
    315  1.1  cgd  */
    316  1.1  cgd char *
    317  1.1  cgd _rtld_find_library(
    318  1.1  cgd     const char *name,
    319  1.1  cgd     const Obj_Entry *refobj)
    320  1.1  cgd {
    321  1.1  cgd     char *pathname;
    322  1.1  cgd 
    323  1.1  cgd     if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
    324  1.1  cgd 	if (name[0] != '/' && !_rtld_trust) {
    325  1.1  cgd 	    _rtld_error("Absolute pathname required for shared object \"%s\"",
    326  1.1  cgd 			name);
    327  1.1  cgd 	    return NULL;
    328  1.1  cgd 	}
    329  1.1  cgd #ifdef SVR4_LIBDIR
    330  1.1  cgd 	if (strncmp(name, SVR4_LIBDIR, SVR4_LIBDIRLEN) == 0
    331  1.1  cgd 	        && name[SVR4_LIBDIRLEN] == '/') {	/* In "/usr/lib" */
    332  1.1  cgd 	    /* Map hard-coded "/usr/lib" onto our ELF library directory. */
    333  1.1  cgd 	    pathname = xmalloc(strlen(name) + LIBDIRLEN - SVR4_LIBDIRLEN + 1);
    334  1.1  cgd 	    strcpy(pathname, LIBDIR);
    335  1.1  cgd 	    strcpy(pathname + LIBDIRLEN, name + SVR4_LIBDIRLEN);
    336  1.1  cgd 	    return pathname;
    337  1.1  cgd 	}
    338  1.1  cgd #endif /* SVR4_LIBDIR */
    339  1.1  cgd 	return xstrdup(name);
    340  1.1  cgd     }
    341  1.1  cgd 
    342  1.1  cgd     dbg(" Searching for \"%s\" (%p)", name, refobj);
    343  1.1  cgd 
    344  1.1  cgd     pathname = _rtld_search_library_paths(name, _rtld_paths,
    345  1.1  cgd 					  refobj ? refobj->rpaths : NULL);
    346  1.1  cgd     if (pathname == NULL)
    347  1.1  cgd 	_rtld_error("Shared object \"%s\" not found", name);
    348  1.1  cgd     return pathname;
    349           }
    350