1b9ae52e3SPaul Richards /* 20f6b2cb3SPaul Traina * Copyright (c) 1993,1995 Paul Kranenburg 3b9ae52e3SPaul Richards * All rights reserved. 4b9ae52e3SPaul Richards * 5b9ae52e3SPaul Richards * Redistribution and use in source and binary forms, with or without 6b9ae52e3SPaul Richards * modification, are permitted provided that the following conditions 7b9ae52e3SPaul Richards * are met: 8b9ae52e3SPaul Richards * 1. Redistributions of source code must retain the above copyright 9b9ae52e3SPaul Richards * notice, this list of conditions and the following disclaimer. 10b9ae52e3SPaul Richards * 2. Redistributions in binary form must reproduce the above copyright 11b9ae52e3SPaul Richards * notice, this list of conditions and the following disclaimer in the 12b9ae52e3SPaul Richards * documentation and/or other materials provided with the distribution. 13b9ae52e3SPaul Richards * 3. All advertising materials mentioning features or use of this software 14b9ae52e3SPaul Richards * must display the following acknowledgement: 15b9ae52e3SPaul Richards * This product includes software developed by Paul Kranenburg. 16b9ae52e3SPaul Richards * 4. The name of the author may not be used to endorse or promote products 1709e3d49dSJordan K. Hubbard * derived from this software without specific prior written permission 18b9ae52e3SPaul Richards * 19b9ae52e3SPaul Richards * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20b9ae52e3SPaul Richards * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21b9ae52e3SPaul Richards * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22b9ae52e3SPaul Richards * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23b9ae52e3SPaul Richards * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24b9ae52e3SPaul Richards * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b9ae52e3SPaul Richards * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b9ae52e3SPaul Richards * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b9ae52e3SPaul Richards * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28b9ae52e3SPaul Richards * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29b9ae52e3SPaul Richards */ 30b9ae52e3SPaul Richards 31b50d7faeSPhilippe Charnier #ifndef lint 32b50d7faeSPhilippe Charnier static const char rcsid[] = 337f3dea24SPeter Wemm "$FreeBSD$"; 34b50d7faeSPhilippe Charnier #endif /* not lint */ 35b50d7faeSPhilippe Charnier 36b9ae52e3SPaul Richards #include <sys/param.h> 37b9ae52e3SPaul Richards #include <sys/types.h> 38b9ae52e3SPaul Richards #include <sys/stat.h> 39b9ae52e3SPaul Richards #include <sys/mman.h> 40b9ae52e3SPaul Richards #include <a.out.h> 41b50d7faeSPhilippe Charnier #include <ctype.h> 42b50d7faeSPhilippe Charnier #include <dirent.h> 435e6220d9SDavid E. O'Brien #include <elf-hints.h> 44b50d7faeSPhilippe Charnier #include <err.h> 45b50d7faeSPhilippe Charnier #include <errno.h> 46b50d7faeSPhilippe Charnier #include <fcntl.h> 4766422f5bSPeter Wemm #include <sys/link_aout.h> 48699e1b82SRich Murphey #include <stdio.h> 49699e1b82SRich Murphey #include <stdlib.h> 50b9ae52e3SPaul Richards #include <string.h> 51699e1b82SRich Murphey #include <unistd.h> 52b9ae52e3SPaul Richards 53a565ca59SJohn Polstra #include "ldconfig.h" 5480c71499SPeter Wemm #include "shlib.h" 5580c71499SPeter Wemm #include "support.h" 5680c71499SPeter Wemm 577c6da7dcSJohn Polstra #if DEBUG 587c6da7dcSJohn Polstra /* test */ 597c6da7dcSJohn Polstra #undef _PATH_LD_HINTS 607c6da7dcSJohn Polstra #define _PATH_LD_HINTS "./ld.so.hints" 61a565ca59SJohn Polstra #undef _PATH_ELF_HINTS 62a565ca59SJohn Polstra #define _PATH_ELF_HINTS "./ld-elf.so.hints" 637c6da7dcSJohn Polstra #endif 64b9ae52e3SPaul Richards 65c905e45dSPeter Wemm #define _PATH_LD32_HINTS "/var/run/ld32.so.hints" 66c905e45dSPeter Wemm #define _PATH_ELF32_HINTS "/var/run/ld-elf32.so.hints" 674153c211SWarner Losh #define _PATH_ELFSOFT_HINTS "/var/run/ld-elf-soft.so.hints" 68c905e45dSPeter Wemm 69b9ae52e3SPaul Richards #undef major 70b9ae52e3SPaul Richards #undef minor 71b9ae52e3SPaul Richards 72b9ae52e3SPaul Richards static int verbose; 73b9ae52e3SPaul Richards static int nostd; 74b9ae52e3SPaul Richards static int justread; 75f606c848SSatoshi Asami static int merge; 76d4ba5766SPeter Wemm static int rescan; 7706eda379SXin LI static const char *hints_file; 78b9ae52e3SPaul Richards 79b9ae52e3SPaul Richards struct shlib_list { 80b9ae52e3SPaul Richards /* Internal list of shared libraries found */ 81b9ae52e3SPaul Richards char *name; 82b9ae52e3SPaul Richards char *path; 83b9ae52e3SPaul Richards int dewey[MAXDEWEY]; 84b9ae52e3SPaul Richards int ndewey; 85b9ae52e3SPaul Richards #define major dewey[0] 86b9ae52e3SPaul Richards #define minor dewey[1] 87b9ae52e3SPaul Richards struct shlib_list *next; 88b9ae52e3SPaul Richards }; 89b9ae52e3SPaul Richards 90b9ae52e3SPaul Richards static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 9180c71499SPeter Wemm static char *dir_list; 92b9ae52e3SPaul Richards 9385429990SWarner Losh static int buildhints(void); 9485429990SWarner Losh static int dodir(char *, int); 9585429990SWarner Losh int dofile(char *, int); 9685429990SWarner Losh static void enter(char *, char *, char *, int *, int); 9785429990SWarner Losh static void listhints(void); 9885429990SWarner Losh static int readhints(void); 9985429990SWarner Losh static void usage(void); 100b9ae52e3SPaul Richards 1012493aadaSWarner Losh /* 1022493aadaSWarner Losh * Note on aout/a.out support. 1032493aadaSWarner Losh * To properly support shared libraries for compat2x, which are a.out, we need 1042493aadaSWarner Losh * to support a.out here. As of 2013, bug reports are still coming in for this 1052493aadaSWarner Losh * feature (on amd64 no less), so we know it is still in use. 1062493aadaSWarner Losh */ 1072493aadaSWarner Losh 108b9ae52e3SPaul Richards int 10906eda379SXin LI main(int argc, char **argv) 110b9ae52e3SPaul Richards { 111b9ae52e3SPaul Richards int i, c; 112b9ae52e3SPaul Richards int rval = 0; 113c905e45dSPeter Wemm int is_aout = 0; 114c905e45dSPeter Wemm int is_32 = 0; 1154153c211SWarner Losh int is_soft = 0; 116b9ae52e3SPaul Richards 117c905e45dSPeter Wemm while (argc > 1) { 118c905e45dSPeter Wemm if (strcmp(argv[1], "-aout") == 0) { 11966422f5bSPeter Wemm is_aout = 1; 12066422f5bSPeter Wemm argc--; 12166422f5bSPeter Wemm argv++; 122c905e45dSPeter Wemm } else if (strcmp(argv[1], "-elf") == 0) { 123c905e45dSPeter Wemm is_aout = 0; 12466422f5bSPeter Wemm argc--; 12566422f5bSPeter Wemm argv++; 126c905e45dSPeter Wemm } else if (strcmp(argv[1], "-32") == 0) { 127c905e45dSPeter Wemm is_32 = 1; 128c905e45dSPeter Wemm argc--; 129c905e45dSPeter Wemm argv++; 1304153c211SWarner Losh } else if (strcmp(argv[1], "-soft") == 0) { 1314153c211SWarner Losh is_soft = 1; 1324153c211SWarner Losh argc--; 1334153c211SWarner Losh argv++; 134c905e45dSPeter Wemm } else { 135c905e45dSPeter Wemm break; 136c905e45dSPeter Wemm } 13766422f5bSPeter Wemm } 138cfa4d739SJohn Polstra 1394153c211SWarner Losh if (is_soft) 1404153c211SWarner Losh hints_file = _PATH_ELFSOFT_HINTS; /* Never will have a.out softfloat */ 1414153c211SWarner Losh else if (is_32) 142c905e45dSPeter Wemm hints_file = is_aout ? _PATH_LD32_HINTS : _PATH_ELF32_HINTS; 143c905e45dSPeter Wemm else 144cfa4d739SJohn Polstra hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS; 14597333b9eSJohn Polstra if (argc == 1) 14697333b9eSJohn Polstra rescan = 1; 147643dcf40SJohn Polstra else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) { 148b9ae52e3SPaul Richards switch (c) { 149d4ba5766SPeter Wemm case 'R': 150d4ba5766SPeter Wemm rescan = 1; 151d4ba5766SPeter Wemm break; 1527c6da7dcSJohn Polstra case 'f': 1537c6da7dcSJohn Polstra hints_file = optarg; 1547c6da7dcSJohn Polstra break; 155643dcf40SJohn Polstra case 'i': 156643dcf40SJohn Polstra insecure = 1; 157643dcf40SJohn Polstra break; 158f606c848SSatoshi Asami case 'm': 159f606c848SSatoshi Asami merge = 1; 160b9ae52e3SPaul Richards break; 161b9ae52e3SPaul Richards case 'r': 162b9ae52e3SPaul Richards justread = 1; 163b9ae52e3SPaul Richards break; 164f606c848SSatoshi Asami case 's': 165f606c848SSatoshi Asami nostd = 1; 166f606c848SSatoshi Asami break; 167f606c848SSatoshi Asami case 'v': 168f606c848SSatoshi Asami verbose = 1; 169f606c848SSatoshi Asami break; 170b9ae52e3SPaul Richards default: 171b50d7faeSPhilippe Charnier usage(); 172b9ae52e3SPaul Richards break; 173b9ae52e3SPaul Richards } 174b9ae52e3SPaul Richards } 175b9ae52e3SPaul Richards 176cfa4d739SJohn Polstra if (!is_aout) { 177a565ca59SJohn Polstra if (justread) 178a565ca59SJohn Polstra list_elf_hints(hints_file); 179a565ca59SJohn Polstra else 180a565ca59SJohn Polstra update_elf_hints(hints_file, argc - optind, 181a565ca59SJohn Polstra argv + optind, merge || rescan); 182a565ca59SJohn Polstra return 0; 183a565ca59SJohn Polstra } 184a565ca59SJohn Polstra 185643dcf40SJohn Polstra /* Here begins the aout libs processing */ 18680c71499SPeter Wemm dir_list = strdup(""); 18780c71499SPeter Wemm 188d4ba5766SPeter Wemm if (justread || merge || rescan) { 189f606c848SSatoshi Asami if ((rval = readhints()) != 0) 190f606c848SSatoshi Asami return rval; 19180c71499SPeter Wemm } 19280c71499SPeter Wemm 193d4ba5766SPeter Wemm if (!nostd && !merge && !rescan) 19480c71499SPeter Wemm std_search_path(); 19580c71499SPeter Wemm 196571b472bSJordan K. Hubbard /* Add any directories/files from the command line */ 197571b472bSJordan K. Hubbard if (!justread) { 198d66f9d22SJohn Polstra for (i = optind; i < argc; i++) { 199571b472bSJordan K. Hubbard struct stat stbuf; 200571b472bSJordan K. Hubbard 201571b472bSJordan K. Hubbard if (stat(argv[i], &stbuf) == -1) { 202d66f9d22SJohn Polstra warn("%s", argv[i]); 203d66f9d22SJohn Polstra rval = -1; 204614d19caSJohn Polstra } else if (strcmp(argv[i], "/usr/lib") == 0) { 205cabb97dcSSøren Schmidt warnx("WARNING! '%s' can not be used", argv[i]); 206cabb97dcSSøren Schmidt rval = -1; 207614d19caSJohn Polstra } else { 208571b472bSJordan K. Hubbard /* 209571b472bSJordan K. Hubbard * See if this is a directory-containing 210571b472bSJordan K. Hubbard * file instead of a directory 211571b472bSJordan K. Hubbard */ 212571b472bSJordan K. Hubbard if (S_ISREG(stbuf.st_mode)) 213571b472bSJordan K. Hubbard rval |= dofile(argv[i], 0); 214571b472bSJordan K. Hubbard else 21580c71499SPeter Wemm add_search_path(argv[i]); 216d66f9d22SJohn Polstra } 217d66f9d22SJohn Polstra } 218571b472bSJordan K. Hubbard } 21980c71499SPeter Wemm 22080c71499SPeter Wemm for (i = 0; i < n_search_dirs; i++) { 22180c71499SPeter Wemm char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 22280c71499SPeter Wemm free(dir_list); 22380c71499SPeter Wemm dir_list = cp; 22480c71499SPeter Wemm } 22580c71499SPeter Wemm 226f606c848SSatoshi Asami if (justread) { 227f606c848SSatoshi Asami listhints(); 228526195adSJordan K. Hubbard return 0; 229f606c848SSatoshi Asami } 230b9ae52e3SPaul Richards 231b9ae52e3SPaul Richards for (i = 0; i < n_search_dirs; i++) 23209e3d49dSJordan K. Hubbard rval |= dodir(search_dirs[i], 1); 233b9ae52e3SPaul Richards 234f606c848SSatoshi Asami rval |= buildhints(); 235b9ae52e3SPaul Richards 236b9ae52e3SPaul Richards return rval; 237b9ae52e3SPaul Richards } 238b9ae52e3SPaul Richards 239b50d7faeSPhilippe Charnier static void 240f709df34SEd Schouten usage(void) 241b50d7faeSPhilippe Charnier { 242b50d7faeSPhilippe Charnier fprintf(stderr, 2436efabadaSJohn Baldwin "usage: ldconfig [-32] [-aout | -elf] [-Rimrsv] [-f hints_file] [directory | file ...]\n"); 244b50d7faeSPhilippe Charnier exit(1); 245b50d7faeSPhilippe Charnier } 246b50d7faeSPhilippe Charnier 247b9ae52e3SPaul Richards int 248f709df34SEd Schouten dofile(char *fname, int silent) 249571b472bSJordan K. Hubbard { 250571b472bSJordan K. Hubbard FILE *hfp; 251571b472bSJordan K. Hubbard char buf[MAXPATHLEN]; 252571b472bSJordan K. Hubbard int rval = 0; 253571b472bSJordan K. Hubbard char *cp, *sp; 254571b472bSJordan K. Hubbard 255571b472bSJordan K. Hubbard if ((hfp = fopen(fname, "r")) == NULL) { 256571b472bSJordan K. Hubbard warn("%s", fname); 257571b472bSJordan K. Hubbard return -1; 258571b472bSJordan K. Hubbard } 259571b472bSJordan K. Hubbard 260571b472bSJordan K. Hubbard while (fgets(buf, sizeof(buf), hfp)) { 261571b472bSJordan K. Hubbard cp = buf; 262571b472bSJordan K. Hubbard while (isspace(*cp)) 263571b472bSJordan K. Hubbard cp++; 264571b472bSJordan K. Hubbard if (*cp == '#' || *cp == '\0') 265571b472bSJordan K. Hubbard continue; 266571b472bSJordan K. Hubbard sp = cp; 267571b472bSJordan K. Hubbard while (!isspace(*cp) && *cp != '\0') 268571b472bSJordan K. Hubbard cp++; 269571b472bSJordan K. Hubbard 270571b472bSJordan K. Hubbard if (*cp != '\n') { 271571b472bSJordan K. Hubbard *cp = '\0'; 272b50d7faeSPhilippe Charnier warnx("%s: trailing characters ignored", sp); 273571b472bSJordan K. Hubbard } 274571b472bSJordan K. Hubbard 275571b472bSJordan K. Hubbard *cp = '\0'; 276571b472bSJordan K. Hubbard 277571b472bSJordan K. Hubbard rval |= dodir(sp, silent); 278571b472bSJordan K. Hubbard } 279571b472bSJordan K. Hubbard 280571b472bSJordan K. Hubbard (void)fclose(hfp); 281571b472bSJordan K. Hubbard return rval; 282571b472bSJordan K. Hubbard } 283571b472bSJordan K. Hubbard 284571b472bSJordan K. Hubbard int 285f709df34SEd Schouten dodir(char *dir, int silent) 286b9ae52e3SPaul Richards { 287b9ae52e3SPaul Richards DIR *dd; 288b9ae52e3SPaul Richards struct dirent *dp; 2890f6b2cb3SPaul Traina char name[MAXPATHLEN]; 290b9ae52e3SPaul Richards int dewey[MAXDEWEY], ndewey; 291b9ae52e3SPaul Richards 292b9ae52e3SPaul Richards if ((dd = opendir(dir)) == NULL) { 293d66f9d22SJohn Polstra if (silent && errno == ENOENT) /* Ignore the error */ 294d66f9d22SJohn Polstra return 0; 295f606c848SSatoshi Asami warn("%s", dir); 296b9ae52e3SPaul Richards return -1; 297b9ae52e3SPaul Richards } 298b9ae52e3SPaul Richards 299b9ae52e3SPaul Richards while ((dp = readdir(dd)) != NULL) { 3003d438ad6SDavid E. O'Brien int n; 3013d438ad6SDavid E. O'Brien char *cp; 302b9ae52e3SPaul Richards 3030f6b2cb3SPaul Traina /* Check for `lib' prefix */ 3040f6b2cb3SPaul Traina if (dp->d_name[0] != 'l' || 3050f6b2cb3SPaul Traina dp->d_name[1] != 'i' || 3060f6b2cb3SPaul Traina dp->d_name[2] != 'b') 307b9ae52e3SPaul Richards continue; 308b9ae52e3SPaul Richards 3090f6b2cb3SPaul Traina /* Copy the entry minus prefix */ 3100f6b2cb3SPaul Traina (void)strcpy(name, dp->d_name + 3); 3110f6b2cb3SPaul Traina n = strlen(name); 3120f6b2cb3SPaul Traina if (n < 4) 3130f6b2cb3SPaul Traina continue; 3140f6b2cb3SPaul Traina 3150f6b2cb3SPaul Traina /* Find ".so." in name */ 3160f6b2cb3SPaul Traina for (cp = name + n - 4; cp > name; --cp) { 3170f6b2cb3SPaul Traina if (cp[0] == '.' && 3180f6b2cb3SPaul Traina cp[1] == 's' && 3190f6b2cb3SPaul Traina cp[2] == 'o' && 3200f6b2cb3SPaul Traina cp[3] == '.') 3210f6b2cb3SPaul Traina break; 3220f6b2cb3SPaul Traina } 3230f6b2cb3SPaul Traina if (cp <= name) 3240f6b2cb3SPaul Traina continue; 3250f6b2cb3SPaul Traina 3260f6b2cb3SPaul Traina *cp = '\0'; 3270f6b2cb3SPaul Traina if (!isdigit(*(cp+4))) 3280f6b2cb3SPaul Traina continue; 3290f6b2cb3SPaul Traina 3300f6b2cb3SPaul Traina bzero((caddr_t)dewey, sizeof(dewey)); 3310f6b2cb3SPaul Traina ndewey = getdewey(dewey, cp + 4); 3325f8d88ddSJohn Polstra if (ndewey < 2) 3335f8d88ddSJohn Polstra continue; 334b9ae52e3SPaul Richards enter(dir, dp->d_name, name, dewey, ndewey); 335b9ae52e3SPaul Richards } 336b9ae52e3SPaul Richards 337571b472bSJordan K. Hubbard closedir(dd); 338b9ae52e3SPaul Richards return 0; 339b9ae52e3SPaul Richards } 340b9ae52e3SPaul Richards 341b9ae52e3SPaul Richards static void 342f709df34SEd Schouten enter(char *dir, char *file, char *name, int dewey[], int ndewey) 343b9ae52e3SPaul Richards { 344b9ae52e3SPaul Richards struct shlib_list *shp; 345b9ae52e3SPaul Richards 346b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 347b9ae52e3SPaul Richards if (strcmp(name, shp->name) != 0 || major != shp->major) 348b9ae52e3SPaul Richards continue; 349b9ae52e3SPaul Richards 350b9ae52e3SPaul Richards /* Name matches existing entry */ 351b9ae52e3SPaul Richards if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 352b9ae52e3SPaul Richards 353b9ae52e3SPaul Richards /* Update this entry with higher versioned lib */ 354b9ae52e3SPaul Richards if (verbose) 355b9ae52e3SPaul Richards printf("Updating lib%s.%d.%d to %s/%s\n", 356b9ae52e3SPaul Richards shp->name, shp->major, shp->minor, 357b9ae52e3SPaul Richards dir, file); 358b9ae52e3SPaul Richards 359b9ae52e3SPaul Richards free(shp->name); 360b9ae52e3SPaul Richards shp->name = strdup(name); 361b9ae52e3SPaul Richards free(shp->path); 362b9ae52e3SPaul Richards shp->path = concat(dir, "/", file); 363b9ae52e3SPaul Richards bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 364b9ae52e3SPaul Richards shp->ndewey = ndewey; 365b9ae52e3SPaul Richards } 366b9ae52e3SPaul Richards break; 367b9ae52e3SPaul Richards } 368b9ae52e3SPaul Richards 369b9ae52e3SPaul Richards if (shp) 370b9ae52e3SPaul Richards /* Name exists: older version or just updated */ 371b9ae52e3SPaul Richards return; 372b9ae52e3SPaul Richards 373b9ae52e3SPaul Richards /* Allocate new list element */ 374b9ae52e3SPaul Richards if (verbose) 375b9ae52e3SPaul Richards printf("Adding %s/%s\n", dir, file); 376b9ae52e3SPaul Richards 377b9ae52e3SPaul Richards shp = (struct shlib_list *)xmalloc(sizeof *shp); 378b9ae52e3SPaul Richards shp->name = strdup(name); 379b9ae52e3SPaul Richards shp->path = concat(dir, "/", file); 3807d85b9aaSColin Percival bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 381b9ae52e3SPaul Richards shp->ndewey = ndewey; 382b9ae52e3SPaul Richards shp->next = NULL; 383b9ae52e3SPaul Richards 384b9ae52e3SPaul Richards *shlib_tail = shp; 385b9ae52e3SPaul Richards shlib_tail = &shp->next; 386b9ae52e3SPaul Richards } 387b9ae52e3SPaul Richards 388b9ae52e3SPaul Richards 38906eda379SXin LI static int 39006eda379SXin LI hinthash(char *cp, int vmajor) 391b9ae52e3SPaul Richards { 392b9ae52e3SPaul Richards int k = 0; 393b9ae52e3SPaul Richards 394b9ae52e3SPaul Richards while (*cp) 395b9ae52e3SPaul Richards k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 396b9ae52e3SPaul Richards 397b9ae52e3SPaul Richards k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 398b9ae52e3SPaul Richards 399b9ae52e3SPaul Richards return k; 400b9ae52e3SPaul Richards } 401b9ae52e3SPaul Richards 402b9ae52e3SPaul Richards int 403f709df34SEd Schouten buildhints(void) 404b9ae52e3SPaul Richards { 405b9ae52e3SPaul Richards struct hints_header hdr; 406b9ae52e3SPaul Richards struct hints_bucket *blist; 407b9ae52e3SPaul Richards struct shlib_list *shp; 408b9ae52e3SPaul Richards char *strtab; 409b9ae52e3SPaul Richards int i, n, str_index = 0; 410b9ae52e3SPaul Richards int strtab_sz = 0; /* Total length of strings */ 411b9ae52e3SPaul Richards int nhints = 0; /* Total number of hints */ 412b9ae52e3SPaul Richards int fd; 413665fc054SXin LI char *tmpfilename; 414b9ae52e3SPaul Richards 415b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 416b9ae52e3SPaul Richards strtab_sz += 1 + strlen(shp->name); 417b9ae52e3SPaul Richards strtab_sz += 1 + strlen(shp->path); 418b9ae52e3SPaul Richards nhints++; 419b9ae52e3SPaul Richards } 420b9ae52e3SPaul Richards 421b9ae52e3SPaul Richards /* Fill hints file header */ 422b9ae52e3SPaul Richards hdr.hh_magic = HH_MAGIC; 42380c71499SPeter Wemm hdr.hh_version = LD_HINTS_VERSION_2; 424b9ae52e3SPaul Richards hdr.hh_nbucket = 1 * nhints; 425b9ae52e3SPaul Richards n = hdr.hh_nbucket * sizeof(struct hints_bucket); 426b9ae52e3SPaul Richards hdr.hh_hashtab = sizeof(struct hints_header); 427b9ae52e3SPaul Richards hdr.hh_strtab = hdr.hh_hashtab + n; 42880c71499SPeter Wemm hdr.hh_dirlist = strtab_sz; 42980c71499SPeter Wemm strtab_sz += 1 + strlen(dir_list); 430b9ae52e3SPaul Richards hdr.hh_strtab_sz = strtab_sz; 431b9ae52e3SPaul Richards hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 432b9ae52e3SPaul Richards 433b9ae52e3SPaul Richards if (verbose) 434526195adSJordan K. Hubbard printf("Totals: entries %d, buckets %ld, string size %d\n", 435ab845347SBruce Evans nhints, (long)hdr.hh_nbucket, strtab_sz); 436b9ae52e3SPaul Richards 437b9ae52e3SPaul Richards /* Allocate buckets and string table */ 438b9ae52e3SPaul Richards blist = (struct hints_bucket *)xmalloc(n); 439b9ae52e3SPaul Richards bzero((char *)blist, n); 440b9ae52e3SPaul Richards for (i = 0; i < hdr.hh_nbucket; i++) 441b9ae52e3SPaul Richards /* Empty all buckets */ 442b9ae52e3SPaul Richards blist[i].hi_next = -1; 443b9ae52e3SPaul Richards 444b9ae52e3SPaul Richards strtab = (char *)xmalloc(strtab_sz); 445b9ae52e3SPaul Richards 446b9ae52e3SPaul Richards /* Enter all */ 447b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 448b9ae52e3SPaul Richards struct hints_bucket *bp; 449b9ae52e3SPaul Richards 450b9ae52e3SPaul Richards bp = blist + 451d5453ba5SJoerg Wunsch (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 452b9ae52e3SPaul Richards 453b9ae52e3SPaul Richards if (bp->hi_pathx) { 45406eda379SXin LI int j; 455b9ae52e3SPaul Richards 45606eda379SXin LI for (j = 0; j < hdr.hh_nbucket; j++) { 45706eda379SXin LI if (blist[j].hi_pathx == 0) 458b9ae52e3SPaul Richards break; 459b9ae52e3SPaul Richards } 46006eda379SXin LI if (j == hdr.hh_nbucket) { 461b50d7faeSPhilippe Charnier warnx("bummer!"); 462b9ae52e3SPaul Richards return -1; 463b9ae52e3SPaul Richards } 464b9ae52e3SPaul Richards while (bp->hi_next != -1) 465b9ae52e3SPaul Richards bp = &blist[bp->hi_next]; 46606eda379SXin LI bp->hi_next = j; 46706eda379SXin LI bp = blist + j; 468b9ae52e3SPaul Richards } 469b9ae52e3SPaul Richards 470b9ae52e3SPaul Richards /* Insert strings in string table */ 471b9ae52e3SPaul Richards bp->hi_namex = str_index; 472b9ae52e3SPaul Richards strcpy(strtab + str_index, shp->name); 473b9ae52e3SPaul Richards str_index += 1 + strlen(shp->name); 474b9ae52e3SPaul Richards 475b9ae52e3SPaul Richards bp->hi_pathx = str_index; 476b9ae52e3SPaul Richards strcpy(strtab + str_index, shp->path); 477b9ae52e3SPaul Richards str_index += 1 + strlen(shp->path); 478b9ae52e3SPaul Richards 479b9ae52e3SPaul Richards /* Copy versions */ 480b9ae52e3SPaul Richards bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 481b9ae52e3SPaul Richards bp->hi_ndewey = shp->ndewey; 482b9ae52e3SPaul Richards } 483b9ae52e3SPaul Richards 48480c71499SPeter Wemm /* Copy search directories */ 48580c71499SPeter Wemm strcpy(strtab + str_index, dir_list); 48680c71499SPeter Wemm str_index += 1 + strlen(dir_list); 48780c71499SPeter Wemm 48880c71499SPeter Wemm /* Sanity check */ 48980c71499SPeter Wemm if (str_index != strtab_sz) { 49080c71499SPeter Wemm errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 49180c71499SPeter Wemm } 49280c71499SPeter Wemm 493665fc054SXin LI tmpfilename = concat(hints_file, ".XXXXXXXXXX", ""); 49416bd17ceSKris Kennaway umask(0); /* Create with exact permissions */ 495665fc054SXin LI if ((fd = mkstemp(tmpfilename)) == -1) { 496665fc054SXin LI warn("%s", tmpfilename); 49780c71499SPeter Wemm return -1; 49880c71499SPeter Wemm } 49916bd17ceSKris Kennaway fchmod(fd, 0444); 500b9ae52e3SPaul Richards 50109e3d49dSJordan K. Hubbard if (write(fd, &hdr, sizeof(struct hints_header)) != 50209e3d49dSJordan K. Hubbard sizeof(struct hints_header)) { 5037c6da7dcSJohn Polstra warn("%s", hints_file); 50409e3d49dSJordan K. Hubbard return -1; 50509e3d49dSJordan K. Hubbard } 50674b67e84SXin LI if (write(fd, blist, hdr.hh_nbucket * sizeof(*blist)) != 50774b67e84SXin LI (ssize_t)(hdr.hh_nbucket * sizeof(*blist))) { 5087c6da7dcSJohn Polstra warn("%s", hints_file); 50909e3d49dSJordan K. Hubbard return -1; 51009e3d49dSJordan K. Hubbard } 51109e3d49dSJordan K. Hubbard if (write(fd, strtab, strtab_sz) != strtab_sz) { 5127c6da7dcSJohn Polstra warn("%s", hints_file); 51309e3d49dSJordan K. Hubbard return -1; 51409e3d49dSJordan K. Hubbard } 515b9ae52e3SPaul Richards if (close(fd) != 0) { 5167c6da7dcSJohn Polstra warn("%s", hints_file); 517b9ae52e3SPaul Richards return -1; 518b9ae52e3SPaul Richards } 519b9ae52e3SPaul Richards 52009e3d49dSJordan K. Hubbard /* Install it */ 5217c6da7dcSJohn Polstra if (unlink(hints_file) != 0 && errno != ENOENT) { 5227c6da7dcSJohn Polstra warn("%s", hints_file); 523b9ae52e3SPaul Richards return -1; 524b9ae52e3SPaul Richards } 525b9ae52e3SPaul Richards 526665fc054SXin LI if (rename(tmpfilename, hints_file) != 0) { 5277c6da7dcSJohn Polstra warn("%s", hints_file); 528b9ae52e3SPaul Richards return -1; 529b9ae52e3SPaul Richards } 530b9ae52e3SPaul Richards 531b9ae52e3SPaul Richards return 0; 532b9ae52e3SPaul Richards } 533b9ae52e3SPaul Richards 534699e1b82SRich Murphey static int 535f709df34SEd Schouten readhints(void) 536b9ae52e3SPaul Richards { 537b9ae52e3SPaul Richards int fd; 538614d19caSJohn Polstra void *addr; 539614d19caSJohn Polstra long fsize; 540b9ae52e3SPaul Richards long msize; 541b9ae52e3SPaul Richards struct hints_header *hdr; 542b9ae52e3SPaul Richards struct hints_bucket *blist; 543b9ae52e3SPaul Richards char *strtab; 544f606c848SSatoshi Asami struct shlib_list *shp; 545b9ae52e3SPaul Richards int i; 546b9ae52e3SPaul Richards 5477c6da7dcSJohn Polstra if ((fd = open(hints_file, O_RDONLY, 0)) == -1) { 5487c6da7dcSJohn Polstra warn("%s", hints_file); 549b9ae52e3SPaul Richards return -1; 550b9ae52e3SPaul Richards } 551b9ae52e3SPaul Richards 55280c71499SPeter Wemm msize = PAGE_SIZE; 553*be8f91d3SEdward Tomasz Napierala addr = mmap(0, msize, PROT_READ, MAP_PRIVATE, fd, 0); 554b9ae52e3SPaul Richards 555614d19caSJohn Polstra if (addr == MAP_FAILED) { 5567c6da7dcSJohn Polstra warn("%s", hints_file); 557b9ae52e3SPaul Richards return -1; 558b9ae52e3SPaul Richards } 559b9ae52e3SPaul Richards 560b9ae52e3SPaul Richards hdr = (struct hints_header *)addr; 561b9ae52e3SPaul Richards if (HH_BADMAG(*hdr)) { 562ab845347SBruce Evans warnx("%s: bad magic: %lo", hints_file, 563ab845347SBruce Evans (unsigned long)hdr->hh_magic); 564b9ae52e3SPaul Richards return -1; 565b9ae52e3SPaul Richards } 566b9ae52e3SPaul Richards 56780c71499SPeter Wemm if (hdr->hh_version != LD_HINTS_VERSION_1 && 56880c71499SPeter Wemm hdr->hh_version != LD_HINTS_VERSION_2) { 569ab845347SBruce Evans warnx("unsupported version: %ld", (long)hdr->hh_version); 570b9ae52e3SPaul Richards return -1; 571b9ae52e3SPaul Richards } 572b9ae52e3SPaul Richards 573b9ae52e3SPaul Richards if (hdr->hh_ehints > msize) { 574614d19caSJohn Polstra fsize = hdr->hh_ehints; 575614d19caSJohn Polstra munmap(addr, msize); 576*be8f91d3SEdward Tomasz Napierala addr = mmap(0, fsize, PROT_READ, MAP_PRIVATE, fd, 0); 577614d19caSJohn Polstra if (addr == MAP_FAILED) { 5787c6da7dcSJohn Polstra warn("%s", hints_file); 579b9ae52e3SPaul Richards return -1; 580b9ae52e3SPaul Richards } 581614d19caSJohn Polstra hdr = (struct hints_header *)addr; 582b9ae52e3SPaul Richards } 583b9ae52e3SPaul Richards close(fd); 584b9ae52e3SPaul Richards 5854eae39bfSStefan Farfeleder strtab = (char *)addr + hdr->hh_strtab; 586b9ae52e3SPaul Richards 587d4ba5766SPeter Wemm if (hdr->hh_version >= LD_HINTS_VERSION_2) 588d4ba5766SPeter Wemm add_search_path(strtab + hdr->hh_dirlist); 589d4ba5766SPeter Wemm else if (rescan) 590d4ba5766SPeter Wemm errx(1, "%s too old and does not contain the search path", 591d4ba5766SPeter Wemm hints_file); 592d4ba5766SPeter Wemm 593d4ba5766SPeter Wemm if (rescan) 594d4ba5766SPeter Wemm return 0; 595d4ba5766SPeter Wemm 59674b67e84SXin LI blist = malloc(sizeof(*blist) * hdr->hh_nbucket); 59706eda379SXin LI if (blist == NULL) 59806eda379SXin LI err(1, "readhints"); 59906eda379SXin LI memcpy(blist, (char *)addr + hdr->hh_hashtab, 60074b67e84SXin LI sizeof(*blist) * hdr->hh_nbucket); 60106eda379SXin LI 60206eda379SXin LI 603b9ae52e3SPaul Richards for (i = 0; i < hdr->hh_nbucket; i++) { 604b9ae52e3SPaul Richards struct hints_bucket *bp = &blist[i]; 605b9ae52e3SPaul Richards 606b9ae52e3SPaul Richards /* Sanity check */ 607b9ae52e3SPaul Richards if (bp->hi_namex >= hdr->hh_strtab_sz) { 608b50d7faeSPhilippe Charnier warnx("bad name index: %#x", bp->hi_namex); 60906eda379SXin LI free(blist); 610b9ae52e3SPaul Richards return -1; 611b9ae52e3SPaul Richards } 612b9ae52e3SPaul Richards if (bp->hi_pathx >= hdr->hh_strtab_sz) { 613b50d7faeSPhilippe Charnier warnx("bad path index: %#x", bp->hi_pathx); 61406eda379SXin LI free(blist); 615b9ae52e3SPaul Richards return -1; 616b9ae52e3SPaul Richards } 617b9ae52e3SPaul Richards 618f606c848SSatoshi Asami /* Allocate new list element */ 619f606c848SSatoshi Asami shp = (struct shlib_list *)xmalloc(sizeof *shp); 620f606c848SSatoshi Asami shp->name = strdup(strtab + bp->hi_namex); 621f606c848SSatoshi Asami shp->path = strdup(strtab + bp->hi_pathx); 622f606c848SSatoshi Asami bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 623f606c848SSatoshi Asami shp->ndewey = bp->hi_ndewey; 624f606c848SSatoshi Asami shp->next = NULL; 625f606c848SSatoshi Asami 626f606c848SSatoshi Asami *shlib_tail = shp; 627f606c848SSatoshi Asami shlib_tail = &shp->next; 628b9ae52e3SPaul Richards } 629b9ae52e3SPaul Richards 63006eda379SXin LI free(blist); 631b9ae52e3SPaul Richards return 0; 632b9ae52e3SPaul Richards } 633b9ae52e3SPaul Richards 634f606c848SSatoshi Asami static void 635f709df34SEd Schouten listhints(void) 636f606c848SSatoshi Asami { 637f606c848SSatoshi Asami struct shlib_list *shp; 638f606c848SSatoshi Asami int i; 639f606c848SSatoshi Asami 6407c6da7dcSJohn Polstra printf("%s:\n", hints_file); 64180c71499SPeter Wemm printf("\tsearch directories: %s\n", dir_list); 642f606c848SSatoshi Asami 643f606c848SSatoshi Asami for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 644f606c848SSatoshi Asami printf("\t%d:-l%s.%d.%d => %s\n", 645f606c848SSatoshi Asami i, shp->name, shp->major, shp->minor, shp->path); 646f606c848SSatoshi Asami 647f606c848SSatoshi Asami return; 648f606c848SSatoshi Asami } 649