search.c revision 1.17
1/* $NetBSD: search.c,v 1.17 2003/07/24 10:12:26 skrll Exp $ */ 2 3/* 4 * Copyright 1996 Matt Thomas <matt@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@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 */ 58Search_Path *_rtld_invalid_paths; 59 60static Obj_Entry *_rtld_search_library_path(const char *, size_t, 61 const char *, size_t, int); 62 63static Obj_Entry * 64_rtld_search_library_path(const char *name, size_t namelen, 65 const char *dir, size_t dirlen, int mode) 66{ 67 char *pathname; 68 size_t pathnamelen; 69 Obj_Entry *obj; 70 Search_Path *sp; 71 72 pathnamelen = dirlen + 1 + namelen; 73 74 for (sp = _rtld_invalid_paths; sp != NULL; sp = sp->sp_next) { 75 if (sp->sp_pathlen == pathnamelen && 76 !memcmp(name, sp->sp_path + dirlen + 1, namelen) && 77 !memcmp(dir, sp->sp_path, dirlen)) { 78 return NULL; 79 } 80 } 81 82 pathname = xmalloc(pathnamelen + 1); 83 (void)strncpy(pathname, dir, dirlen); 84 pathname[dirlen] = '/'; 85 strcpy(pathname + dirlen + 1, name); 86 87 dbg((" Trying \"%s\"", pathname)); 88 obj = _rtld_load_object(pathname, mode); 89 if (obj == NULL) { 90 Search_Path *path; 91 92 path = NEW(Search_Path); 93 path->sp_pathlen = pathnamelen; 94 path->sp_path = pathname; 95 path->sp_next = _rtld_invalid_paths; 96 _rtld_invalid_paths = path; 97 } 98 return obj; 99} 100 101/* 102 * Find the library with the given name, and return its full pathname. 103 * The returned string is dynamically allocated. Generates an error 104 * message and returns NULL if the library cannot be found. 105 * 106 * If the second argument is non-NULL, then it refers to an already- 107 * loaded shared object, whose library search path will be searched. 108 */ 109Obj_Entry * 110_rtld_load_library(const char *name, const Obj_Entry *refobj, int mode) 111{ 112 char tmperror[512], *tmperrorp; 113 Search_Path *sp; 114 char *pathname; 115 int namelen; 116 Obj_Entry *obj; 117 118 if (strchr(name, '/') != NULL) { /* Hard coded pathname */ 119 if (name[0] != '/' && !_rtld_trust) { 120 _rtld_error( 121 "absolute pathname required for shared object \"%s\"", 122 name); 123 return NULL; 124 } 125 pathname = xstrdup(name); 126 goto found; 127 } 128 dbg((" Searching for \"%s\" (%p)", name, refobj)); 129 130 tmperrorp = _rtld_dlerror(); 131 if (tmperrorp != NULL) { 132 strncpy(tmperror, tmperrorp, sizeof tmperror); 133 tmperrorp = tmperror; 134 } 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 goto pathfound; 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 goto pathfound; 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 goto pathfound; 153 154 _rtld_error("Shared object \"%s\" not found", name); 155 return NULL; 156 157pathfound: 158 /* 159 * Successfully found a library; restore the dlerror state as it was 160 * before _rtld_load_library() was called (any failed call to 161 * _rtld_search_library_path() will set the dlerror state, but if the 162 * library was eventually found, then the error state should not 163 * change. 164 */ 165 if (tmperrorp) 166 _rtld_error("%s", tmperror); 167 else 168 (void)_rtld_dlerror(); 169 return obj; 170 171found: 172 obj = _rtld_load_object(pathname, mode); 173 if (obj == NULL) 174 free(pathname); 175 return obj; 176} 177