sod.c revision 1.13
1/* $OpenBSD: sod.c,v 1.13 2002/05/27 23:37:25 deraadt Exp $ */ 2 3/* 4 * Copyright (c) 1993 Paul Kranenburg 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 Paul Kranenburg. 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#include <sys/types.h> 35#include <sys/syslimits.h> 36#include <stdio.h> 37#include <fcntl.h> 38#include <nlist.h> 39#include <link.h> 40#include <machine/exec.h> 41#include <sys/mman.h> 42#include <string.h> 43#include <stdlib.h> 44#include <unistd.h> 45#include <syscall.h> 46 47#include "archdep.h" 48#include "util.h" 49 50#define PAGSIZ __LDPGSZ 51int _dl_hinthash(char *cp, int vmajor, int vminor); 52 53/* 54 * Populate sod struct for dlopen's call to map_object 55 */ 56void 57_dl_build_sod(name, sodp) 58 const char *name; 59 struct sod *sodp; 60{ 61 unsigned int tuplet; 62 int major, minor; 63 char *realname, *tok, *etok, *cp; 64 65 /* default is an absolute or relative path */ 66 sodp->sod_name = (long)_dl_strdup(name); /* strtok is destructive */ 67 sodp->sod_library = 0; 68 sodp->sod_major = sodp->sod_minor = 0; 69 70 /* does it look like /^lib/ ? */ 71 if (_dl_strncmp((char *)sodp->sod_name, "lib", 3) != 0) 72 return; 73 74 /* is this a filename? */ 75 if (_dl_strchr((char *)sodp->sod_name, '/')) 76 return; 77 78 /* skip over 'lib' */ 79 cp = (char *)sodp->sod_name + 3; 80 81 /* dot guardian */ 82 if ((_dl_strchr(cp, '.') == NULL) || (*(cp+_dl_strlen(cp)-1) == '.')) 83 return; 84 85 /* default */ 86 major = minor = -1; 87 88 /* loop through name - parse skipping name */ 89 for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) { 90 switch (tuplet) { 91 case 0: 92 /* removed 'lib' and extensions from name */ 93 realname = tok; 94 break; 95 case 1: 96 /* 'so' extension */ 97 if (_dl_strcmp(tok, "so") != 0) 98 goto backout; 99 break; 100 case 2: 101 /* major version extension */ 102 major = strtol(tok, &etok, 10); 103 if (*tok == '\0' || *etok != '\0') 104 goto backout; 105 break; 106 case 3: 107 /* minor version extension */ 108 minor = strtol(tok, &etok, 10); 109 if (*tok == '\0' || *etok != '\0') 110 goto backout; 111 break; 112 /* if we get here, it must be weird */ 113 default: 114 goto backout; 115 } 116 } 117 cp = (char *)sodp->sod_name; 118 sodp->sod_name = (long)_dl_strdup(realname); 119 _dl_free(cp); 120 sodp->sod_library = 1; 121 sodp->sod_major = major; 122 sodp->sod_minor = minor; 123 return; 124 125backout: 126 _dl_free((char *)sodp->sod_name); 127 sodp->sod_name = (long)_dl_strdup(name); 128} 129 130static int hfd; 131static long hsize; 132static struct hints_header *hheader = NULL; 133static struct hints_bucket *hbuckets; 134static char *hstrtab; 135static char *hint_search_path = ""; 136 137#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1) 138 139void 140_dl_maphints() 141{ 142 caddr_t addr; 143 144 if ((hfd = _dl_open(_PATH_LD_HINTS, O_RDONLY)) < 0) { 145 hheader = (struct hints_header *)-1; 146 return; 147 } 148 149 hsize = PAGSIZ; 150 addr = (void *) _dl_mmap(0, hsize, PROT_READ, MAP_PRIVATE, hfd, 0); 151 152 if (addr == MAP_FAILED) { 153 _dl_close(hfd); 154 hheader = (struct hints_header *)-1; 155 return; 156 } 157 158 hheader = (struct hints_header *)addr; 159 if (HH_BADMAG(*hheader)) { 160 _dl_munmap(addr, hsize); 161 _dl_close(hfd); 162 hheader = (struct hints_header *)-1; 163 return; 164 } 165 166 if (hheader->hh_version != LD_HINTS_VERSION_1 && 167 hheader->hh_version != LD_HINTS_VERSION_2) { 168 _dl_munmap(addr, hsize); 169 _dl_close(hfd); 170 hheader = (struct hints_header *)-1; 171 return; 172 } 173 174 if (hheader->hh_ehints > hsize) { 175 if ((caddr_t)_dl_mmap(addr+hsize, hheader->hh_ehints - hsize, 176 PROT_READ, MAP_PRIVATE|MAP_FIXED, 177 hfd, hsize) != (caddr_t)(addr+hsize)) { 178 _dl_munmap((caddr_t)hheader, hsize); 179 _dl_close(hfd); 180 hheader = (struct hints_header *)-1; 181 return; 182 } 183 } 184 185 hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab); 186 hstrtab = (char *)(addr + hheader->hh_strtab); 187 if (hheader->hh_version >= LD_HINTS_VERSION_2) 188 hint_search_path = hstrtab + hheader->hh_dirlist; 189 190 /* close the file descriptor, leaving the hints mapped */ 191 _dl_close(hfd); 192} 193 194char * 195_dl_findhint(name, major, minor, prefered_path) 196 char *name; 197 int major, minor; 198 char *prefered_path; 199{ 200 struct hints_bucket *bp; 201 202 /* If not mapped, and we have not tried before, try to map the 203 * hints, if previous attempts failed hheader is -1 and we 204 * do not wish to retry it. 205 */ 206 if (hheader == NULL) 207 _dl_maphints(); 208 209 /* if it failed to map, return failure */ 210 if (!(HINTS_VALID)) 211 return NULL; 212 213 bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket); 214 215 while (1) { 216 /* Sanity check */ 217 if (bp->hi_namex >= hheader->hh_strtab_sz) { 218 _dl_printf("Bad name index: %#x\n", bp->hi_namex); 219 _dl_exit(7); 220 break; 221 } 222 if (bp->hi_pathx >= hheader->hh_strtab_sz) { 223 _dl_printf("Bad path index: %#x\n", bp->hi_pathx); 224 _dl_exit(7); 225 break; 226 } 227 228 if (_dl_strcmp(name, hstrtab + bp->hi_namex) == 0) { 229 /* It's `name', check version numbers */ 230 if (bp->hi_major == major && 231 (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) { 232 if (prefered_path == NULL || 233 _dl_strncmp(prefered_path, 234 hstrtab + bp->hi_pathx, 235 _dl_strlen(prefered_path)) == 0) 236 return hstrtab + bp->hi_pathx; 237 } 238 } 239 240 if (bp->hi_next == -1) 241 break; 242 243 /* Move on to next in bucket */ 244 bp = &hbuckets[bp->hi_next]; 245 } 246 247 /* No hints available for name */ 248 return NULL; 249} 250int 251_dl_hinthash(cp, vmajor, vminor) 252 char *cp; 253 int vmajor, vminor; 254{ 255 int k = 0; 256 257 while (*cp) 258 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 259 260 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 261 if (hheader->hh_version == LD_HINTS_VERSION_1) 262 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff; 263 264 return k; 265} 266