2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1993,1995 Paul Kranenburg
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
34 static const char rcsid[] =
38 #include <sys/param.h>
39 #include <sys/types.h>
45 #include <elf-hints.h>
49 #include <sys/link_aout.h>
62 #define _PATH_LD_HINTS "./ld.so.hints"
63 #undef _PATH_ELF_HINTS
64 #define _PATH_ELF_HINTS "./ld-elf.so.hints"
67 #define _PATH_LD32_HINTS "/var/run/ld32.so.hints"
68 #define _PATH_ELF32_HINTS "/var/run/ld-elf32.so.hints"
69 #define _PATH_ELFSOFT_HINTS "/var/run/ld-elf-soft.so.hints"
79 static const char *hints_file;
82 /* Internal list of shared libraries found */
87 #define major dewey[0]
88 #define minor dewey[1]
89 struct shlib_list *next;
92 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
93 static char *dir_list;
95 static int buildhints(void);
96 static int dodir(char *, int);
97 int dofile(char *, int);
98 static void enter(char *, char *, char *, int *, int);
99 static void listhints(void);
100 static int readhints(void);
101 static void usage(void);
104 * Note on aout/a.out support.
105 * To properly support shared libraries for compat2x, which are a.out, we need
106 * to support a.out here. As of 2013, bug reports are still coming in for this
107 * feature (on amd64 no less), so we know it is still in use.
111 main(int argc, char **argv)
120 if (strcmp(argv[1], "-aout") == 0) {
121 warnx("aout support is deprecated");
125 } else if (strcmp(argv[1], "-elf") == 0) {
129 } else if (strcmp(argv[1], "-32") == 0) {
133 } else if (strcmp(argv[1], "-soft") == 0) {
143 hints_file = _PATH_ELFSOFT_HINTS; /* Never will have a.out softfloat */
145 hints_file = is_aout ? _PATH_LD32_HINTS : _PATH_ELF32_HINTS;
147 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
150 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
181 list_elf_hints(hints_file);
183 update_elf_hints(hints_file, argc - optind,
184 argv + optind, merge || rescan);
188 /* Here begins the aout libs processing */
189 dir_list = strdup("");
191 if (justread || merge || rescan) {
192 if ((rval = readhints()) != 0)
196 if (!nostd && !merge && !rescan)
199 /* Add any directories/files from the command line */
201 for (i = optind; i < argc; i++) {
204 if (stat(argv[i], &stbuf) == -1) {
207 } else if (strcmp(argv[i], "/usr/lib") == 0) {
208 warnx("WARNING! '%s' can not be used", argv[i]);
212 * See if this is a directory-containing
213 * file instead of a directory
215 if (S_ISREG(stbuf.st_mode))
216 rval |= dofile(argv[i], 0);
218 add_search_path(argv[i]);
223 for (i = 0; i < n_search_dirs; i++) {
224 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
234 for (i = 0; i < n_search_dirs; i++)
235 rval |= dodir(search_dirs[i], 1);
237 rval |= buildhints();
246 "usage: ldconfig [-32] [-aout | -elf] [-Rimrsv] [-f hints_file] [directory | file ...]\n");
251 dofile(char *fname, int silent)
254 char buf[MAXPATHLEN];
258 if ((hfp = fopen(fname, "r")) == NULL) {
263 while (fgets(buf, sizeof(buf), hfp)) {
267 if (*cp == '#' || *cp == '\0')
270 while (!isspace(*cp) && *cp != '\0')
275 warnx("%s: trailing characters ignored", sp);
280 rval |= dodir(sp, silent);
288 dodir(char *dir, int silent)
292 char name[MAXPATHLEN];
293 int dewey[MAXDEWEY], ndewey;
295 if ((dd = opendir(dir)) == NULL) {
296 if (silent && errno == ENOENT) /* Ignore the error */
302 while ((dp = readdir(dd)) != NULL) {
306 /* Check for `lib' prefix */
307 if (dp->d_name[0] != 'l' ||
308 dp->d_name[1] != 'i' ||
309 dp->d_name[2] != 'b')
312 /* Copy the entry minus prefix */
313 (void)strcpy(name, dp->d_name + 3);
318 /* Find ".so." in name */
319 for (cp = name + n - 4; cp > name; --cp) {
330 if (!isdigit(*(cp+4)))
333 bzero((caddr_t)dewey, sizeof(dewey));
334 ndewey = getdewey(dewey, cp + 4);
337 enter(dir, dp->d_name, name, dewey, ndewey);
345 enter(char *dir, char *file, char *name, int dewey[], int ndewey)
347 struct shlib_list *shp;
349 for (shp = shlib_head; shp; shp = shp->next) {
350 if (strcmp(name, shp->name) != 0 || major != shp->major)
353 /* Name matches existing entry */
354 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
356 /* Update this entry with higher versioned lib */
358 printf("Updating lib%s.%d.%d to %s/%s\n",
359 shp->name, shp->major, shp->minor,
363 shp->name = strdup(name);
365 shp->path = concat(dir, "/", file);
366 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
367 shp->ndewey = ndewey;
373 /* Name exists: older version or just updated */
376 /* Allocate new list element */
378 printf("Adding %s/%s\n", dir, file);
380 shp = (struct shlib_list *)xmalloc(sizeof *shp);
381 shp->name = strdup(name);
382 shp->path = concat(dir, "/", file);
383 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
384 shp->ndewey = ndewey;
388 shlib_tail = &shp->next;
393 hinthash(char *cp, int vmajor)
398 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
400 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
408 struct hints_header hdr;
409 struct hints_bucket *blist;
410 struct shlib_list *shp;
412 int i, n, str_index = 0;
413 int strtab_sz = 0; /* Total length of strings */
414 int nhints = 0; /* Total number of hints */
418 for (shp = shlib_head; shp; shp = shp->next) {
419 strtab_sz += 1 + strlen(shp->name);
420 strtab_sz += 1 + strlen(shp->path);
424 /* Fill hints file header */
425 hdr.hh_magic = HH_MAGIC;
426 hdr.hh_version = LD_HINTS_VERSION_2;
427 hdr.hh_nbucket = 1 * nhints;
428 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
429 hdr.hh_hashtab = sizeof(struct hints_header);
430 hdr.hh_strtab = hdr.hh_hashtab + n;
431 hdr.hh_dirlist = strtab_sz;
432 strtab_sz += 1 + strlen(dir_list);
433 hdr.hh_strtab_sz = strtab_sz;
434 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
437 printf("Totals: entries %d, buckets %ld, string size %d\n",
438 nhints, (long)hdr.hh_nbucket, strtab_sz);
440 /* Allocate buckets and string table */
441 blist = (struct hints_bucket *)xmalloc(n);
442 bzero((char *)blist, n);
443 for (i = 0; i < hdr.hh_nbucket; i++)
444 /* Empty all buckets */
445 blist[i].hi_next = -1;
447 strtab = (char *)xmalloc(strtab_sz);
450 for (shp = shlib_head; shp; shp = shp->next) {
451 struct hints_bucket *bp;
454 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
459 for (j = 0; j < hdr.hh_nbucket; j++) {
460 if (blist[j].hi_pathx == 0)
463 if (j == hdr.hh_nbucket) {
467 while (bp->hi_next != -1)
468 bp = &blist[bp->hi_next];
473 /* Insert strings in string table */
474 bp->hi_namex = str_index;
475 strcpy(strtab + str_index, shp->name);
476 str_index += 1 + strlen(shp->name);
478 bp->hi_pathx = str_index;
479 strcpy(strtab + str_index, shp->path);
480 str_index += 1 + strlen(shp->path);
483 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
484 bp->hi_ndewey = shp->ndewey;
487 /* Copy search directories */
488 strcpy(strtab + str_index, dir_list);
489 str_index += 1 + strlen(dir_list);
492 if (str_index != strtab_sz) {
493 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
496 tmpfilename = concat(hints_file, ".XXXXXXXXXX", "");
497 umask(0); /* Create with exact permissions */
498 if ((fd = mkstemp(tmpfilename)) == -1) {
499 warn("%s", tmpfilename);
504 if (write(fd, &hdr, sizeof(struct hints_header)) !=
505 sizeof(struct hints_header)) {
506 warn("%s", hints_file);
509 if (write(fd, blist, hdr.hh_nbucket * sizeof(*blist)) !=
510 (ssize_t)(hdr.hh_nbucket * sizeof(*blist))) {
511 warn("%s", hints_file);
514 if (write(fd, strtab, strtab_sz) != strtab_sz) {
515 warn("%s", hints_file);
518 if (close(fd) != 0) {
519 warn("%s", hints_file);
522 if (rename(tmpfilename, hints_file) != 0) {
523 warn("%s", hints_file);
537 struct hints_header *hdr;
538 struct hints_bucket *blist;
540 struct shlib_list *shp;
543 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
544 warn("%s", hints_file);
549 addr = mmap(0, msize, PROT_READ, MAP_PRIVATE, fd, 0);
551 if (addr == MAP_FAILED) {
552 warn("%s", hints_file);
556 hdr = (struct hints_header *)addr;
557 if (HH_BADMAG(*hdr)) {
558 warnx("%s: bad magic: %lo", hints_file,
559 (unsigned long)hdr->hh_magic);
563 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
564 hdr->hh_version != LD_HINTS_VERSION_2) {
565 warnx("unsupported version: %ld", (long)hdr->hh_version);
569 if (hdr->hh_ehints > msize) {
570 fsize = hdr->hh_ehints;
572 addr = mmap(0, fsize, PROT_READ, MAP_PRIVATE, fd, 0);
573 if (addr == MAP_FAILED) {
574 warn("%s", hints_file);
577 hdr = (struct hints_header *)addr;
581 strtab = (char *)addr + hdr->hh_strtab;
583 if (hdr->hh_version >= LD_HINTS_VERSION_2)
584 add_search_path(strtab + hdr->hh_dirlist);
586 errx(1, "%s too old and does not contain the search path",
592 blist = malloc(sizeof(*blist) * hdr->hh_nbucket);
595 memcpy(blist, (char *)addr + hdr->hh_hashtab,
596 sizeof(*blist) * hdr->hh_nbucket);
599 for (i = 0; i < hdr->hh_nbucket; i++) {
600 struct hints_bucket *bp = &blist[i];
603 if (bp->hi_namex >= hdr->hh_strtab_sz) {
604 warnx("bad name index: %#x", bp->hi_namex);
608 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
609 warnx("bad path index: %#x", bp->hi_pathx);
614 /* Allocate new list element */
615 shp = (struct shlib_list *)xmalloc(sizeof *shp);
616 shp->name = strdup(strtab + bp->hi_namex);
617 shp->path = strdup(strtab + bp->hi_pathx);
618 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
619 shp->ndewey = bp->hi_ndewey;
623 shlib_tail = &shp->next;
633 struct shlib_list *shp;
636 printf("%s:\n", hints_file);
637 printf("\tsearch directories: %s\n", dir_list);
639 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
640 printf("\t%d:-l%s.%d.%d => %s\n",
641 i, shp->name, shp->major, shp->minor, shp->path);