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