Home | History | Annotate | Line # | Download | only in ld.elf_so
search.c revision 1.14
      1 /*	$NetBSD: search.c,v 1.14 2002/10/01 14:16:53 junyoung 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 Search_Path    *_rtld_invalid_paths;
     59 
     60 static Obj_Entry *_rtld_search_library_path __P((const char *, size_t,
     61     const char *, size_t, int));
     62 
     63 static Obj_Entry *
     64 _rtld_search_library_path(name, namelen, dir, dirlen, mode)
     65 	const char *name;
     66 	size_t namelen;
     67 	const char *dir;
     68 	size_t dirlen;
     69 	int mode;
     70 {
     71 	char *pathname;
     72 	size_t pathnamelen;
     73 	Obj_Entry *obj;
     74 	Search_Path *sp;
     75 
     76 	pathnamelen = dirlen + 1 + namelen;
     77 
     78 	for (sp = _rtld_invalid_paths; sp != NULL; sp = sp->sp_next) {
     79 		if (sp->sp_pathlen == pathnamelen &&
     80 		    !strncmp(name, sp->sp_path + dirlen + 1, namelen) &&
     81 		    !strncmp(dir, sp->sp_path, dirlen)) {
     82 			return NULL;
     83 		}
     84 	}
     85 
     86 	pathname = xmalloc(pathnamelen + 1);
     87 	(void)strncpy(pathname, dir, dirlen);
     88 	pathname[dirlen] = '/';
     89 	strcpy(pathname + dirlen + 1, name);
     90 
     91 	dbg(("  Trying \"%s\"", pathname));
     92 	obj = _rtld_load_object(pathname, mode);
     93 	if (obj == NULL) {
     94 		Search_Path *path;
     95 
     96 		path = NEW(Search_Path);
     97 		path->sp_pathlen = pathnamelen;
     98 		path->sp_path = pathname;
     99 		path->sp_next = _rtld_invalid_paths;
    100 		_rtld_invalid_paths = path;
    101 	}
    102 	return obj;
    103 }
    104 
    105 /*
    106  * Find the library with the given name, and return its full pathname.
    107  * The returned string is dynamically allocated.  Generates an error
    108  * message and returns NULL if the library cannot be found.
    109  *
    110  * If the second argument is non-NULL, then it refers to an already-
    111  * loaded shared object, whose library search path will be searched.
    112  */
    113 Obj_Entry *
    114 _rtld_load_library(name, refobj, mode)
    115 	const char *name;
    116 	const Obj_Entry *refobj;
    117 	int mode;
    118 {
    119 	Search_Path *sp;
    120 	char *pathname;
    121 	int namelen;
    122 	Obj_Entry *obj;
    123 
    124 	if (strchr(name, '/') != NULL) {	/* Hard coded pathname */
    125 		if (name[0] != '/' && !_rtld_trust) {
    126 			_rtld_error(
    127 			"absolute pathname required for shared object \"%s\"",
    128 			    name);
    129 			return NULL;
    130 		}
    131 		pathname = xstrdup(name);
    132 		goto found;
    133 	}
    134 	dbg((" Searching for \"%s\" (%p)", name, refobj));
    135 
    136 	namelen = strlen(name);
    137 
    138 	for (sp = _rtld_paths; sp != NULL; sp = sp->sp_next)
    139 		if ((obj = _rtld_search_library_path(name, namelen,
    140 		    sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    141 			return obj;
    142 
    143 	if (refobj != NULL)
    144 		for (sp = refobj->rpaths; sp != NULL; sp = sp->sp_next)
    145 			if ((obj = _rtld_search_library_path(name,
    146 			    namelen, sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    147 				return obj;
    148 
    149 	for (sp = _rtld_default_paths; sp != NULL; sp = sp->sp_next)
    150 		if ((obj = _rtld_search_library_path(name, namelen,
    151 		    sp->sp_path, sp->sp_pathlen, mode)) != NULL)
    152 			return obj;
    153 
    154 	_rtld_error("Shared object \"%s\" not found", name);
    155 	return NULL;
    156 
    157 found:
    158 	obj = _rtld_load_object(pathname, mode);
    159 	if (obj == NULL)
    160 		free(pathname);
    161 	return obj;
    162 }
    163