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