sod.c revision 1.8
1/* $OpenBSD: sod.c,v 1.8 2002/02/21 23:17:53 drahn Exp $ */ 2/* 3 * Copyright (c) 1993 Paul Kranenburg 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Paul Kranenburg. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33#include <sys/types.h> 34#include <sys/syslimits.h> 35#include <stdio.h> 36#include <fcntl.h> 37#include <nlist.h> 38#include <link.h> 39#include <machine/exec.h> 40#include <sys/mman.h> 41#include <string.h> 42#include <stdlib.h> 43#include <unistd.h> 44#include <syscall.h> 45 46#include "archdep.h" 47#include "util.h" 48 49#define PAGSIZ __LDPGSZ 50int _dl_hinthash(char *cp, int vmajor, int vminor); 51 52/* 53 * Populate sod struct for dlopen's call to map_object 54 */ 55void 56_dl_build_sod(name, sodp) 57 const char *name; 58 struct sod *sodp; 59{ 60 unsigned int tuplet; 61 int major, minor; 62 char *realname, *tok, *etok, *cp; 63 64 /* default is an absolute or relative path */ 65 sodp->sod_name = (long)_dl_strdup(name); /* strtok is destructive */ 66 sodp->sod_library = 0; 67 sodp->sod_major = sodp->sod_minor = 0; 68 69 /* does it look like /^lib/ ? */ 70 if (_dl_strncmp((char *)sodp->sod_name, "lib", 3) != 0) 71 return; 72 73 /* is this a filename? */ 74 if (strchr((char *)sodp->sod_name, '/')) 75 return; 76 77 /* skip over 'lib' */ 78 cp = (char *)sodp->sod_name + 3; 79 80 /* dot guardian */ 81 if ((strchr(cp, '.') == NULL) || (*(cp+_dl_strlen(cp)-1) == '.')) 82 return; 83 84 /* default */ 85 major = minor = -1; 86 87 /* loop through name - parse skipping name */ 88 for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) { 89 switch (tuplet) { 90 case 0: 91 /* removed 'lib' and extensions from name */ 92 realname = tok; 93 break; 94 case 1: 95 /* 'so' extension */ 96 if (strcmp(tok, "so") != 0) 97 goto backout; 98 break; 99 case 2: 100 /* major version extension */ 101 major = strtol(tok, &etok, 10); 102 if (*tok == '\0' || *etok != '\0') 103 goto backout; 104 break; 105 case 3: 106 /* minor version extension */ 107 minor = strtol(tok, &etok, 10); 108 if (*tok == '\0' || *etok != '\0') 109 goto backout; 110 break; 111 /* if we get here, it must be weird */ 112 default: 113 goto backout; 114 } 115 } 116 cp = (char *)sodp->sod_name; 117 sodp->sod_name = (long)_dl_strdup(realname); 118 _dl_free(cp); 119 sodp->sod_library = 1; 120 sodp->sod_major = major; 121 sodp->sod_minor = minor; 122 return; 123 124backout: 125 _dl_free((char *)sodp->sod_name); 126 sodp->sod_name = (long)_dl_strdup(name); 127} 128 129static int hfd; 130static long hsize; 131static struct hints_header *hheader = NULL; 132static struct hints_bucket *hbuckets; 133static char *hstrtab; 134static char *hint_search_path = ""; 135 136#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1) 137 138void 139_dl_maphints() 140{ 141 caddr_t addr; 142 143 if ((hfd = _dl_open(_PATH_LD_HINTS, O_RDONLY)) < 0) { 144 hheader = (struct hints_header *)-1; 145 return; 146 } 147 148 hsize = PAGSIZ; 149 addr = (void *) _dl_mmap(0, hsize, PROT_READ, MAP_PRIVATE, hfd, 0); 150 151 if (addr == MAP_FAILED) { 152 _dl_close(hfd); 153 hheader = (struct hints_header *)-1; 154 return; 155 } 156 157 hheader = (struct hints_header *)addr; 158 if (HH_BADMAG(*hheader)) { 159 _dl_munmap(addr, hsize); 160 _dl_close(hfd); 161 hheader = (struct hints_header *)-1; 162 return; 163 } 164 165 if (hheader->hh_version != LD_HINTS_VERSION_1 && 166 hheader->hh_version != LD_HINTS_VERSION_2) { 167 _dl_munmap(addr, hsize); 168 _dl_close(hfd); 169 hheader = (struct hints_header *)-1; 170 return; 171 } 172 173 if (hheader->hh_ehints > hsize) { 174 if ((caddr_t)_dl_mmap(addr+hsize, hheader->hh_ehints - hsize, 175 PROT_READ, MAP_PRIVATE|MAP_FIXED, 176 hfd, hsize) != (caddr_t)(addr+hsize)) { 177 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 210 /* if it failed to map, return failure */ 211 if (!(HINTS_VALID)) { 212 return NULL; 213 } 214 215 bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket); 216 217 while (1) { 218 /* Sanity check */ 219 if (bp->hi_namex >= hheader->hh_strtab_sz) { 220 _dl_printf("Bad name index: %#x\n", bp->hi_namex); 221 _dl_exit(7); 222 break; 223 } 224 if (bp->hi_pathx >= hheader->hh_strtab_sz) { 225 _dl_printf("Bad path index: %#x\n", bp->hi_pathx); 226 _dl_exit(7); 227 break; 228 } 229 230 if (strcmp(name, hstrtab + bp->hi_namex) == 0) { 231 /* It's `name', check version numbers */ 232 if (bp->hi_major == major && 233 (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) { 234 if (prefered_path == NULL || 235 strncmp(prefered_path, 236 hstrtab + bp->hi_pathx, 237 _dl_strlen(prefered_path)) == 0) { 238 return hstrtab + bp->hi_pathx; 239 } 240 } 241 } 242 243 if (bp->hi_next == -1) 244 break; 245 246 /* Move on to next in bucket */ 247 bp = &hbuckets[bp->hi_next]; 248 } 249 250 /* No hints available for name */ 251 return NULL; 252} 253int 254_dl_hinthash(cp, vmajor, vminor) 255 char *cp; 256 int vmajor, vminor; 257{ 258 int k = 0; 259 260 while (*cp) 261 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 262 263 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 264 if (hheader->hh_version == LD_HINTS_VERSION_1) 265 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff; 266 267 return k; 268} 269