Home | History | Annotate | Line # | Download | only in ld.elf_so
search.c revision 1.10.2.1
      1  1.10.2.1      tron /*	$NetBSD: search.c,v 1.10.2.1 2004/05/28 08:31:22 tron 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.10.2.1      tron Search_Path    *_rtld_invalid_paths;
     59       1.1       cgd 
     60  1.10.2.1      tron static Obj_Entry *_rtld_search_library_path(const char *, size_t,
     61  1.10.2.1      tron     const char *, size_t, int);
     62       1.1       cgd 
     63  1.10.2.1      tron static Obj_Entry *
     64  1.10.2.1      tron _rtld_search_library_path(const char *name, size_t namelen,
     65  1.10.2.1      tron     const char *dir, size_t dirlen, int mode)
     66       1.1       cgd {
     67       1.3       cgd 	char *pathname;
     68  1.10.2.1      tron 	size_t pathnamelen;
     69  1.10.2.1      tron 	Obj_Entry *obj;
     70  1.10.2.1      tron 	Search_Path *sp;
     71  1.10.2.1      tron 
     72  1.10.2.1      tron 	pathnamelen = dirlen + 1 + namelen;
     73  1.10.2.1      tron 
     74  1.10.2.1      tron 	for (sp = _rtld_invalid_paths; sp != NULL; sp = sp->sp_next) {
     75  1.10.2.1      tron 		if (sp->sp_pathlen == pathnamelen &&
     76  1.10.2.1      tron 		    !memcmp(name, sp->sp_path + dirlen + 1, namelen) &&
     77  1.10.2.1      tron 		    !memcmp(dir, sp->sp_path, dirlen)) {
     78  1.10.2.1      tron 			return NULL;
     79  1.10.2.1      tron 		}
     80  1.10.2.1      tron 	}
     81       1.1       cgd 
     82  1.10.2.1      tron 	pathname = xmalloc(pathnamelen + 1);
     83       1.4  christos 	(void)strncpy(pathname, dir, dirlen);
     84       1.3       cgd 	pathname[dirlen] = '/';
     85       1.3       cgd 	strcpy(pathname + dirlen + 1, name);
     86       1.3       cgd 
     87       1.4  christos 	dbg(("  Trying \"%s\"", pathname));
     88  1.10.2.1      tron 	obj = _rtld_load_object(pathname, mode);
     89  1.10.2.1      tron 	if (obj == NULL) {
     90  1.10.2.1      tron 		Search_Path *path;
     91  1.10.2.1      tron 
     92  1.10.2.1      tron 		path = NEW(Search_Path);
     93  1.10.2.1      tron 		path->sp_pathlen = pathnamelen;
     94  1.10.2.1      tron 		path->sp_path = pathname;
     95  1.10.2.1      tron 		path->sp_next = _rtld_invalid_paths;
     96  1.10.2.1      tron 		_rtld_invalid_paths = path;
     97  1.10.2.1      tron 	}
     98  1.10.2.1      tron 	return obj;
     99       1.1       cgd }
    100       1.4  christos 
    101       1.1       cgd /*
    102       1.1       cgd  * Find the library with the given name, and return its full pathname.
    103       1.1       cgd  * The returned string is dynamically allocated.  Generates an error
    104       1.1       cgd  * message and returns NULL if the library cannot be found.
    105       1.1       cgd  *
    106       1.1       cgd  * If the second argument is non-NULL, then it refers to an already-
    107       1.1       cgd  * loaded shared object, whose library search path will be searched.
    108       1.1       cgd  */
    109  1.10.2.1      tron Obj_Entry *
    110  1.10.2.1      tron _rtld_load_library(const char *name, const Obj_Entry *refobj, int mode)
    111       1.1       cgd {
    112  1.10.2.1      tron 	char tmperror[512], *tmperrorp;
    113       1.4  christos 	Search_Path *sp;
    114       1.4  christos 	char *pathname;
    115       1.4  christos 	int namelen;
    116  1.10.2.1      tron 	Obj_Entry *obj;
    117       1.4  christos 
    118       1.4  christos 	if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
    119       1.4  christos 		if (name[0] != '/' && !_rtld_trust) {
    120       1.4  christos 			_rtld_error(
    121  1.10.2.1      tron 			"absolute pathname required for shared object \"%s\"",
    122       1.4  christos 			    name);
    123       1.4  christos 			return NULL;
    124       1.4  christos 		}
    125  1.10.2.1      tron 		pathname = xstrdup(name);
    126  1.10.2.1      tron 		goto found;
    127       1.1       cgd 	}
    128       1.4  christos 	dbg((" Searching for \"%s\" (%p)", name, refobj));
    129       1.1       cgd 
    130  1.10.2.1      tron 	tmperrorp = dlerror();
    131  1.10.2.1      tron 	if (tmperrorp != NULL) {
    132  1.10.2.1      tron 		strncpy(tmperror, tmperrorp, sizeof tmperror);
    133  1.10.2.1      tron 		tmperrorp = tmperror;
    134  1.10.2.1      tron 	}
    135  1.10.2.1      tron 
    136       1.3       cgd 	namelen = strlen(name);
    137       1.3       cgd 
    138       1.5    kleink 	for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
    139  1.10.2.1      tron 		if ((obj = _rtld_search_library_path(name, namelen,
    140  1.10.2.1      tron 		    sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    141  1.10.2.1      tron 			goto pathfound;
    142       1.5    kleink 
    143       1.4  christos 	if (refobj != NULL)
    144       1.4  christos 		for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
    145  1.10.2.1      tron 			if ((obj = _rtld_search_library_path(name,
    146  1.10.2.1      tron 			    namelen, sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    147  1.10.2.1      tron 				goto pathfound;
    148       1.3       cgd 
    149       1.5    kleink 	for (sp = _rtld_default_paths; sp != NULL; sp = sp->sp_next)
    150  1.10.2.1      tron 		if ((obj = _rtld_search_library_path(name, namelen,
    151  1.10.2.1      tron 		    sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    152  1.10.2.1      tron 			goto pathfound;
    153       1.3       cgd 
    154       1.4  christos 	_rtld_error("Shared object \"%s\" not found", name);
    155       1.4  christos 	return NULL;
    156  1.10.2.1      tron 
    157  1.10.2.1      tron pathfound:
    158  1.10.2.1      tron 	/*
    159  1.10.2.1      tron 	 * Successfully found a library; restore the dlerror state as it was
    160  1.10.2.1      tron 	 * before _rtld_load_library() was called (any failed call to
    161  1.10.2.1      tron 	 * _rtld_search_library_path() will set the dlerror state, but if the
    162  1.10.2.1      tron 	 * library was eventually found, then the error state should not
    163  1.10.2.1      tron 	 * change.
    164  1.10.2.1      tron 	 */
    165  1.10.2.1      tron 	if (tmperrorp)
    166  1.10.2.1      tron 		_rtld_error("%s", tmperror);
    167  1.10.2.1      tron 	else
    168  1.10.2.1      tron 		(void)dlerror();
    169  1.10.2.1      tron 	return obj;
    170  1.10.2.1      tron 
    171  1.10.2.1      tron found:
    172  1.10.2.1      tron 	obj = _rtld_load_object(pathname, mode);
    173  1.10.2.1      tron 	if (obj == NULL)
    174  1.10.2.1      tron 		free(pathname);
    175  1.10.2.1      tron 	return obj;
    176       1.1       cgd }
    177