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) {
124 } else if (strcmp(argv[1], "-elf") == 0) {
128 } else if (strcmp(argv[1], "-32") == 0) {
132 } else if (strcmp(argv[1], "-soft") == 0) {
142 hints_file = _PATH_ELFSOFT_HINTS; /* Never will have a.out softfloat */
144 hints_file = is_aout ? _PATH_LD32_HINTS : _PATH_ELF32_HINTS;
146 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
149 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
180 list_elf_hints(hints_file);
182 update_elf_hints(hints_file, argc - optind,
183 argv + optind, merge || rescan);
187 /* Here begins the aout libs processing */
188 dir_list = strdup("");
190 if (justread || merge || rescan) {
191 if ((rval = readhints()) != 0)
195 if (!nostd && !merge && !rescan)
198 /* Add any directories/files from the command line */
200 for (i = optind; i < argc; i++) {
203 if (stat(argv[i], &stbuf) == -1) {
206 } else if (strcmp(argv[i], "/usr/lib") == 0) {
207 warnx("WARNING! '%s' can not be used", argv[i]);
211 * See if this is a directory-containing
212 * file instead of a directory
214 if (S_ISREG(stbuf.st_mode))
215 rval |= dofile(argv[i], 0);
217 add_search_path(argv[i]);
222 for (i = 0; i < n_search_dirs; i++) {
223 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
233 for (i = 0; i < n_search_dirs; i++)
234 rval |= dodir(search_dirs[i], 1);
236 rval |= buildhints();
245 "usage: ldconfig [-32] [-aout | -elf] [-Rimrsv] [-f hints_file] [directory | file ...]\n");
250 dofile(char *fname, int silent)
253 char buf[MAXPATHLEN];
257 if ((hfp = fopen(fname, "r")) == NULL) {
262 while (fgets(buf, sizeof(buf), hfp)) {
266 if (*cp == '#' || *cp == '\0')
269 while (!isspace(*cp) && *cp != '\0')
274 warnx("%s: trailing characters ignored", sp);
279 rval |= dodir(sp, silent);
287 dodir(char *dir, int silent)
291 char name[MAXPATHLEN];
292 int dewey[MAXDEWEY], ndewey;
294 if ((dd = opendir(dir)) == NULL) {
295 if (silent && errno == ENOENT) /* Ignore the error */
301 while ((dp = readdir(dd)) != NULL) {
305 /* Check for `lib' prefix */
306 if (dp->d_name[0] != 'l' ||
307 dp->d_name[1] != 'i' ||
308 dp->d_name[2] != 'b')
311 /* Copy the entry minus prefix */
312 (void)strcpy(name, dp->d_name + 3);
317 /* Find ".so." in name */
318 for (cp = name + n - 4; cp > name; --cp) {
329 if (!isdigit(*(cp+4)))
332 bzero((caddr_t)dewey, sizeof(dewey));
333 ndewey = getdewey(dewey, cp + 4);
336 enter(dir, dp->d_name, name, dewey, ndewey);
344 enter(char *dir, char *file, char *name, int dewey[], int ndewey)
346 struct shlib_list *shp;
348 for (shp = shlib_head; shp; shp = shp->next) {
349 if (strcmp(name, shp->name) != 0 || major != shp->major)
352 /* Name matches existing entry */
353 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
355 /* Update this entry with higher versioned lib */
357 printf("Updating lib%s.%d.%d to %s/%s\n",
358 shp->name, shp->major, shp->minor,
362 shp->name = strdup(name);
364 shp->path = concat(dir, "/", file);
365 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
366 shp->ndewey = ndewey;
372 /* Name exists: older version or just updated */
375 /* Allocate new list element */
377 printf("Adding %s/%s\n", dir, file);
379 shp = (struct shlib_list *)xmalloc(sizeof *shp);
380 shp->name = strdup(name);
381 shp->path = concat(dir, "/", file);
382 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
383 shp->ndewey = ndewey;
387 shlib_tail = &shp->next;
392 hinthash(char *cp, int vmajor)
397 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
399 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
407 struct hints_header hdr;
408 struct hints_bucket *blist;
409 struct shlib_list *shp;
411 int i, n, str_index = 0;
412 int strtab_sz = 0; /* Total length of strings */
413 int nhints = 0; /* Total number of hints */
417 for (shp = shlib_head; shp; shp = shp->next) {
418 strtab_sz += 1 + strlen(shp->name);
419 strtab_sz += 1 + strlen(shp->path);
423 /* Fill hints file header */
424 hdr.hh_magic = HH_MAGIC;
425 hdr.hh_version = LD_HINTS_VERSION_2;
426 hdr.hh_nbucket = 1 * nhints;
427 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
428 hdr.hh_hashtab = sizeof(struct hints_header);
429 hdr.hh_strtab = hdr.hh_hashtab + n;
430 hdr.hh_dirlist = strtab_sz;
431 strtab_sz += 1 + strlen(dir_list);
432 hdr.hh_strtab_sz = strtab_sz;
433 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
436 printf("Totals: entries %d, buckets %ld, string size %d\n",
437 nhints, (long)hdr.hh_nbucket, strtab_sz);
439 /* Allocate buckets and string table */
440 blist = (struct hints_bucket *)xmalloc(n);
441 bzero((char *)blist, n);
442 for (i = 0; i < hdr.hh_nbucket; i++)
443 /* Empty all buckets */
444 blist[i].hi_next = -1;
446 strtab = (char *)xmalloc(strtab_sz);
449 for (shp = shlib_head; shp; shp = shp->next) {
450 struct hints_bucket *bp;
453 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
458 for (j = 0; j < hdr.hh_nbucket; j++) {
459 if (blist[j].hi_pathx == 0)
462 if (j == hdr.hh_nbucket) {
466 while (bp->hi_next != -1)
467 bp = &blist[bp->hi_next];
472 /* Insert strings in string table */
473 bp->hi_namex = str_index;
474 strcpy(strtab + str_index, shp->name);
475 str_index += 1 + strlen(shp->name);
477 bp->hi_pathx = str_index;
478 strcpy(strtab + str_index, shp->path);
479 str_index += 1 + strlen(shp->path);
482 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
483 bp->hi_ndewey = shp->ndewey;
486 /* Copy search directories */
487 strcpy(strtab + str_index, dir_list);
488 str_index += 1 + strlen(dir_list);
491 if (str_index != strtab_sz) {
492 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
495 tmpfilename = concat(hints_file, ".XXXXXXXXXX", "");
496 umask(0); /* Create with exact permissions */
497 if ((fd = mkstemp(tmpfilename)) == -1) {
498 warn("%s", tmpfilename);
503 if (write(fd, &hdr, sizeof(struct hints_header)) !=
504 sizeof(struct hints_header)) {
505 warn("%s", hints_file);
508 if (write(fd, blist, hdr.hh_nbucket * sizeof(*blist)) !=
509 (ssize_t)(hdr.hh_nbucket * sizeof(*blist))) {
510 warn("%s", hints_file);
513 if (write(fd, strtab, strtab_sz) != strtab_sz) {
514 warn("%s", hints_file);
517 if (close(fd) != 0) {
518 warn("%s", hints_file);
521 if (rename(tmpfilename, hints_file) != 0) {
522 warn("%s", hints_file);
536 struct hints_header *hdr;
537 struct hints_bucket *blist;
539 struct shlib_list *shp;
542 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
543 warn("%s", hints_file);
548 addr = mmap(0, msize, PROT_READ, MAP_PRIVATE, fd, 0);
550 if (addr == MAP_FAILED) {
551 warn("%s", hints_file);
555 hdr = (struct hints_header *)addr;
556 if (HH_BADMAG(*hdr)) {
557 warnx("%s: bad magic: %lo", hints_file,
558 (unsigned long)hdr->hh_magic);
562 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
563 hdr->hh_version != LD_HINTS_VERSION_2) {
564 warnx("unsupported version: %ld", (long)hdr->hh_version);
568 if (hdr->hh_ehints > msize) {
569 fsize = hdr->hh_ehints;
571 addr = mmap(0, fsize, PROT_READ, MAP_PRIVATE, fd, 0);
572 if (addr == MAP_FAILED) {
573 warn("%s", hints_file);
576 hdr = (struct hints_header *)addr;
580 strtab = (char *)addr + hdr->hh_strtab;
582 if (hdr->hh_version >= LD_HINTS_VERSION_2)
583 add_search_path(strtab + hdr->hh_dirlist);
585 errx(1, "%s too old and does not contain the search path",
591 blist = malloc(sizeof(*blist) * hdr->hh_nbucket);
594 memcpy(blist, (char *)addr + hdr->hh_hashtab,
595 sizeof(*blist) * hdr->hh_nbucket);
598 for (i = 0; i < hdr->hh_nbucket; i++) {
599 struct hints_bucket *bp = &blist[i];
602 if (bp->hi_namex >= hdr->hh_strtab_sz) {
603 warnx("bad name index: %#x", bp->hi_namex);
607 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
608 warnx("bad path index: %#x", bp->hi_pathx);
613 /* Allocate new list element */
614 shp = (struct shlib_list *)xmalloc(sizeof *shp);
615 shp->name = strdup(strtab + bp->hi_namex);
616 shp->path = strdup(strtab + bp->hi_pathx);
617 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
618 shp->ndewey = bp->hi_ndewey;
622 shlib_tail = &shp->next;
632 struct shlib_list *shp;
635 printf("%s:\n", hints_file);
636 printf("\tsearch directories: %s\n", dir_list);
638 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
639 printf("\t%d:-l%s.%d.%d => %s\n",
640 i, shp->name, shp->major, shp->minor, shp->path);