sod.c revision 1.7
1/* $OpenBSD: sod.c,v 1.7 2001/09/22 04:58:18 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 48#define PAGSIZ __LDPGSZ 49char * _dl_strdup(const char *); 50void _dl_free(void *); 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 (strncmp((char *)sodp->sod_name, "lib", 3) != 0) 72 return; 73 74 /* is this a filename? */ 75 if (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 ((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 (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 179 _dl_munmap((caddr_t)hheader, hsize); 180 _dl_close(hfd); 181 hheader = (struct hints_header *)-1; 182 return; 183 } 184 } 185 186 hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab); 187 hstrtab = (char *)(addr + hheader->hh_strtab); 188 if (hheader->hh_version >= LD_HINTS_VERSION_2) 189 hint_search_path = hstrtab + hheader->hh_dirlist; 190 191 /* close the file descriptor, leaving the hints mapped */ 192 _dl_close(hfd); 193} 194 195char * 196_dl_findhint(name, major, minor, prefered_path) 197 char *name; 198 int major, minor; 199 char *prefered_path; 200{ 201 struct hints_bucket *bp; 202 203 /* If not mapped, and we have not tried before, try to map the 204 * hints, if previous attempts failed hheader is -1 and we 205 * do not wish to retry it. 206 */ 207 if (hheader == NULL) { 208 _dl_maphints(); 209 } 210 211 /* if it failed to map, return failure */ 212 if (!(HINTS_VALID)) { 213 return NULL; 214 } 215 216 bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket); 217 218 while (1) { 219 /* Sanity check */ 220 if (bp->hi_namex >= hheader->hh_strtab_sz) { 221 _dl_printf("Bad name index: %#x\n", bp->hi_namex); 222 _dl_exit(7); 223 break; 224 } 225 if (bp->hi_pathx >= hheader->hh_strtab_sz) { 226 _dl_printf("Bad path index: %#x\n", bp->hi_pathx); 227 _dl_exit(7); 228 break; 229 } 230 231 if (strcmp(name, hstrtab + bp->hi_namex) == 0) { 232 /* It's `name', check version numbers */ 233 if (bp->hi_major == major && 234 (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) { 235 if (prefered_path == NULL || 236 strncmp(prefered_path, 237 hstrtab + bp->hi_pathx, 238 _dl_strlen(prefered_path)) == 0) { 239 return hstrtab + bp->hi_pathx; 240 } 241 } 242 } 243 244 if (bp->hi_next == -1) 245 break; 246 247 /* Move on to next in bucket */ 248 bp = &hbuckets[bp->hi_next]; 249 } 250 251 /* No hints available for name */ 252 return NULL; 253} 254int 255_dl_hinthash(cp, vmajor, vminor) 256 char *cp; 257 int vmajor, vminor; 258{ 259 int k = 0; 260 261 while (*cp) 262 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 263 264 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 265 if (hheader->hh_version == LD_HINTS_VERSION_1) 266 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff; 267 268 return k; 269} 270