1 /* 2 * Copyright (c) 1993,1995 Paul Kranenburg 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Paul Kranenburg. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $Id$ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <sys/file.h> 37 #include <sys/time.h> 38 #include <sys/mman.h> 39 #include <sys/resource.h> 40 #include <dirent.h> 41 #include <errno.h> 42 #include <err.h> 43 #include <ctype.h> 44 #include <fcntl.h> 45 #include <ar.h> 46 #include <ranlib.h> 47 #include <a.out.h> 48 #include <stab.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include <machine/param.h> 55 56 #include <link.h> 57 #include "shlib.h" 58 #include "support.h" 59 60 #if DEBUG 61 /* test */ 62 #undef _PATH_LD_HINTS 63 #define _PATH_LD_HINTS "./ld.so.hints" 64 #endif 65 66 #undef major 67 #undef minor 68 69 extern char *__progname; 70 71 static int verbose; 72 static int nostd; 73 static int justread; 74 static int merge; 75 static char *hints_file = _PATH_LD_HINTS; 76 77 struct shlib_list { 78 /* Internal list of shared libraries found */ 79 char *name; 80 char *path; 81 int dewey[MAXDEWEY]; 82 int ndewey; 83 #define major dewey[0] 84 #define minor dewey[1] 85 struct shlib_list *next; 86 }; 87 88 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 89 static char *dir_list; 90 91 static void enter __P((char *, char *, char *, int *, int)); 92 static int dodir __P((char *, int)); 93 static int buildhints __P((void)); 94 static int readhints __P((void)); 95 static void listhints __P((void)); 96 97 int 98 main(argc, argv) 99 int argc; 100 char *argv[]; 101 { 102 int i, c; 103 int rval = 0; 104 105 while ((c = getopt(argc, argv, "f:mrsv")) != EOF) { 106 switch (c) { 107 case 'f': 108 hints_file = optarg; 109 break; 110 case 'm': 111 merge = 1; 112 break; 113 case 'r': 114 justread = 1; 115 break; 116 case 's': 117 nostd = 1; 118 break; 119 case 'v': 120 verbose = 1; 121 break; 122 default: 123 errx(1, "Usage: %s [-mrsv] [-f hints_file] [dir ...]", 124 __progname); 125 break; 126 } 127 } 128 129 dir_list = strdup(""); 130 131 if (justread || merge) { 132 if ((rval = readhints()) != 0) 133 return rval; 134 } 135 136 if (!nostd && !merge) 137 std_search_path(); 138 139 if (!justread) { /* Add any directories from the command line */ 140 for (i = optind; i < argc; i++) { 141 if (access(argv[i], F_OK) == -1) { /* Doesn't exist */ 142 warn("%s", argv[i]); 143 rval = -1; 144 } else 145 add_search_path(argv[i]); 146 } 147 } 148 149 for (i = 0; i < n_search_dirs; i++) { 150 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 151 free(dir_list); 152 dir_list = cp; 153 } 154 155 if (justread) { 156 listhints(); 157 return 0; 158 } 159 160 for (i = 0; i < n_search_dirs; i++) 161 rval |= dodir(search_dirs[i], 1); 162 163 rval |= buildhints(); 164 165 return rval; 166 } 167 168 int 169 dodir(dir, silent) 170 char *dir; 171 int silent; 172 { 173 DIR *dd; 174 struct dirent *dp; 175 char name[MAXPATHLEN]; 176 int dewey[MAXDEWEY], ndewey; 177 178 if ((dd = opendir(dir)) == NULL) { 179 if (silent && errno == ENOENT) /* Ignore the error */ 180 return 0; 181 warn("%s", dir); 182 return -1; 183 } 184 185 while ((dp = readdir(dd)) != NULL) { 186 register int n; 187 register char *cp; 188 189 /* Check for `lib' prefix */ 190 if (dp->d_name[0] != 'l' || 191 dp->d_name[1] != 'i' || 192 dp->d_name[2] != 'b') 193 continue; 194 195 /* Copy the entry minus prefix */ 196 (void)strcpy(name, dp->d_name + 3); 197 n = strlen(name); 198 if (n < 4) 199 continue; 200 201 /* Find ".so." in name */ 202 for (cp = name + n - 4; cp > name; --cp) { 203 if (cp[0] == '.' && 204 cp[1] == 's' && 205 cp[2] == 'o' && 206 cp[3] == '.') 207 break; 208 } 209 if (cp <= name) 210 continue; 211 212 *cp = '\0'; 213 if (!isdigit(*(cp+4))) 214 continue; 215 216 bzero((caddr_t)dewey, sizeof(dewey)); 217 ndewey = getdewey(dewey, cp + 4); 218 enter(dir, dp->d_name, name, dewey, ndewey); 219 } 220 221 return 0; 222 } 223 224 static void 225 enter(dir, file, name, dewey, ndewey) 226 char *dir, *file, *name; 227 int dewey[], ndewey; 228 { 229 struct shlib_list *shp; 230 231 for (shp = shlib_head; shp; shp = shp->next) { 232 if (strcmp(name, shp->name) != 0 || major != shp->major) 233 continue; 234 235 /* Name matches existing entry */ 236 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 237 238 /* Update this entry with higher versioned lib */ 239 if (verbose) 240 printf("Updating lib%s.%d.%d to %s/%s\n", 241 shp->name, shp->major, shp->minor, 242 dir, file); 243 244 free(shp->name); 245 shp->name = strdup(name); 246 free(shp->path); 247 shp->path = concat(dir, "/", file); 248 bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 249 shp->ndewey = ndewey; 250 } 251 break; 252 } 253 254 if (shp) 255 /* Name exists: older version or just updated */ 256 return; 257 258 /* Allocate new list element */ 259 if (verbose) 260 printf("Adding %s/%s\n", dir, file); 261 262 shp = (struct shlib_list *)xmalloc(sizeof *shp); 263 shp->name = strdup(name); 264 shp->path = concat(dir, "/", file); 265 bcopy(dewey, shp->dewey, MAXDEWEY); 266 shp->ndewey = ndewey; 267 shp->next = NULL; 268 269 *shlib_tail = shp; 270 shlib_tail = &shp->next; 271 } 272 273 274 int 275 hinthash(cp, vmajor) 276 char *cp; 277 int vmajor; 278 { 279 int k = 0; 280 281 while (*cp) 282 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 283 284 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 285 286 return k; 287 } 288 289 int 290 buildhints() 291 { 292 struct hints_header hdr; 293 struct hints_bucket *blist; 294 struct shlib_list *shp; 295 char *strtab; 296 int i, n, str_index = 0; 297 int strtab_sz = 0; /* Total length of strings */ 298 int nhints = 0; /* Total number of hints */ 299 int fd; 300 char *tmpfile; 301 302 for (shp = shlib_head; shp; shp = shp->next) { 303 strtab_sz += 1 + strlen(shp->name); 304 strtab_sz += 1 + strlen(shp->path); 305 nhints++; 306 } 307 308 /* Fill hints file header */ 309 hdr.hh_magic = HH_MAGIC; 310 hdr.hh_version = LD_HINTS_VERSION_2; 311 hdr.hh_nbucket = 1 * nhints; 312 n = hdr.hh_nbucket * sizeof(struct hints_bucket); 313 hdr.hh_hashtab = sizeof(struct hints_header); 314 hdr.hh_strtab = hdr.hh_hashtab + n; 315 hdr.hh_dirlist = strtab_sz; 316 strtab_sz += 1 + strlen(dir_list); 317 hdr.hh_strtab_sz = strtab_sz; 318 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 319 320 if (verbose) 321 printf("Totals: entries %d, buckets %ld, string size %d\n", 322 nhints, hdr.hh_nbucket, strtab_sz); 323 324 /* Allocate buckets and string table */ 325 blist = (struct hints_bucket *)xmalloc(n); 326 bzero((char *)blist, n); 327 for (i = 0; i < hdr.hh_nbucket; i++) 328 /* Empty all buckets */ 329 blist[i].hi_next = -1; 330 331 strtab = (char *)xmalloc(strtab_sz); 332 333 /* Enter all */ 334 for (shp = shlib_head; shp; shp = shp->next) { 335 struct hints_bucket *bp; 336 337 bp = blist + 338 (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 339 340 if (bp->hi_pathx) { 341 int i; 342 343 for (i = 0; i < hdr.hh_nbucket; i++) { 344 if (blist[i].hi_pathx == 0) 345 break; 346 } 347 if (i == hdr.hh_nbucket) { 348 warnx("Bummer!"); 349 return -1; 350 } 351 while (bp->hi_next != -1) 352 bp = &blist[bp->hi_next]; 353 bp->hi_next = i; 354 bp = blist + i; 355 } 356 357 /* Insert strings in string table */ 358 bp->hi_namex = str_index; 359 strcpy(strtab + str_index, shp->name); 360 str_index += 1 + strlen(shp->name); 361 362 bp->hi_pathx = str_index; 363 strcpy(strtab + str_index, shp->path); 364 str_index += 1 + strlen(shp->path); 365 366 /* Copy versions */ 367 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 368 bp->hi_ndewey = shp->ndewey; 369 } 370 371 /* Copy search directories */ 372 strcpy(strtab + str_index, dir_list); 373 str_index += 1 + strlen(dir_list); 374 375 /* Sanity check */ 376 if (str_index != strtab_sz) { 377 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 378 } 379 380 tmpfile = concat(hints_file, ".XXXXXX", ""); 381 if ((tmpfile = mktemp(tmpfile)) == NULL) { 382 warn("%s", tmpfile); 383 return -1; 384 } 385 386 umask(0); /* Create with exact permissions */ 387 if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) { 388 warn("%s", hints_file); 389 return -1; 390 } 391 392 if (write(fd, &hdr, sizeof(struct hints_header)) != 393 sizeof(struct hints_header)) { 394 warn("%s", hints_file); 395 return -1; 396 } 397 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 398 hdr.hh_nbucket * sizeof(struct hints_bucket)) { 399 warn("%s", hints_file); 400 return -1; 401 } 402 if (write(fd, strtab, strtab_sz) != strtab_sz) { 403 warn("%s", hints_file); 404 return -1; 405 } 406 if (close(fd) != 0) { 407 warn("%s", hints_file); 408 return -1; 409 } 410 411 /* Install it */ 412 if (unlink(hints_file) != 0 && errno != ENOENT) { 413 warn("%s", hints_file); 414 return -1; 415 } 416 417 if (rename(tmpfile, hints_file) != 0) { 418 warn("%s", hints_file); 419 return -1; 420 } 421 422 return 0; 423 } 424 425 static int 426 readhints() 427 { 428 int fd; 429 caddr_t addr; 430 long msize; 431 struct hints_header *hdr; 432 struct hints_bucket *blist; 433 char *strtab; 434 struct shlib_list *shp; 435 int i; 436 437 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) { 438 warn("%s", hints_file); 439 return -1; 440 } 441 442 msize = PAGE_SIZE; 443 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0); 444 445 if (addr == (caddr_t)-1) { 446 warn("%s", hints_file); 447 return -1; 448 } 449 450 hdr = (struct hints_header *)addr; 451 if (HH_BADMAG(*hdr)) { 452 warnx("%s: Bad magic: %o", 453 hints_file, hdr->hh_magic); 454 return -1; 455 } 456 457 if (hdr->hh_version != LD_HINTS_VERSION_1 && 458 hdr->hh_version != LD_HINTS_VERSION_2) { 459 warnx("Unsupported version: %d", hdr->hh_version); 460 return -1; 461 } 462 463 if (hdr->hh_ehints > msize) { 464 if (mmap(addr+msize, hdr->hh_ehints - msize, 465 PROT_READ, MAP_COPY|MAP_FIXED, 466 fd, msize) != (caddr_t)(addr+msize)) { 467 468 warn("%s", hints_file); 469 return -1; 470 } 471 } 472 close(fd); 473 474 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab); 475 strtab = (char *)(addr + hdr->hh_strtab); 476 477 for (i = 0; i < hdr->hh_nbucket; i++) { 478 struct hints_bucket *bp = &blist[i]; 479 480 /* Sanity check */ 481 if (bp->hi_namex >= hdr->hh_strtab_sz) { 482 warnx("Bad name index: %#x", bp->hi_namex); 483 return -1; 484 } 485 if (bp->hi_pathx >= hdr->hh_strtab_sz) { 486 warnx("Bad path index: %#x", bp->hi_pathx); 487 return -1; 488 } 489 490 /* Allocate new list element */ 491 shp = (struct shlib_list *)xmalloc(sizeof *shp); 492 shp->name = strdup(strtab + bp->hi_namex); 493 shp->path = strdup(strtab + bp->hi_pathx); 494 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 495 shp->ndewey = bp->hi_ndewey; 496 shp->next = NULL; 497 498 *shlib_tail = shp; 499 shlib_tail = &shp->next; 500 } 501 if (hdr->hh_version >= LD_HINTS_VERSION_2) 502 add_search_path(strtab + hdr->hh_dirlist); 503 504 return 0; 505 } 506 507 static void 508 listhints() 509 { 510 struct shlib_list *shp; 511 int i; 512 513 printf("%s:\n", hints_file); 514 printf("\tsearch directories: %s\n", dir_list); 515 516 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 517 printf("\t%d:-l%s.%d.%d => %s\n", 518 i, shp->name, shp->major, shp->minor, shp->path); 519 520 return; 521 } 522