2 * Copyright (c) 1993,1995 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
32 static const char rcsid[] =
36 #include <sys/param.h>
37 #include <sys/types.h>
43 #include <elf-hints.h>
47 #include <sys/link_aout.h>
60 #define _PATH_LD_HINTS "./ld.so.hints"
61 #undef _PATH_ELF_HINTS
62 #define _PATH_ELF_HINTS "./ld-elf.so.hints"
65 #define _PATH_LD32_HINTS "/var/run/ld32.so.hints"
66 #define _PATH_ELF32_HINTS "/var/run/ld-elf32.so.hints"
67 #define _PATH_ELFSOFT_HINTS "/var/run/ld-elf-soft.so.hints"
77 static const char *hints_file;
80 /* Internal list of shared libraries found */
85 #define major dewey[0]
86 #define minor dewey[1]
87 struct shlib_list *next;
90 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
91 static char *dir_list;
93 static int buildhints(void);
94 static int dodir(char *, int);
95 int dofile(char *, int);
96 static void enter(char *, char *, char *, int *, int);
97 static void listhints(void);
98 static int readhints(void);
99 static void usage(void);
102 * Note on aout/a.out support.
103 * To properly support shared libraries for compat2x, which are a.out, we need
104 * to support a.out here. As of 2013, bug reports are still coming in for this
105 * feature (on amd64 no less), so we know it is still in use.
109 main(int argc, char **argv)
118 if (strcmp(argv[1], "-aout") == 0) {
122 } else if (strcmp(argv[1], "-elf") == 0) {
126 } else if (strcmp(argv[1], "-32") == 0) {
130 } else if (strcmp(argv[1], "-soft") == 0) {
140 hints_file = _PATH_ELFSOFT_HINTS; /* Never will have a.out softfloat */
142 hints_file = is_aout ? _PATH_LD32_HINTS : _PATH_ELF32_HINTS;
144 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
147 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
178 list_elf_hints(hints_file);
180 update_elf_hints(hints_file, argc - optind,
181 argv + optind, merge || rescan);
185 /* Here begins the aout libs processing */
186 dir_list = strdup("");
188 if (justread || merge || rescan) {
189 if ((rval = readhints()) != 0)
193 if (!nostd && !merge && !rescan)
196 /* Add any directories/files from the command line */
198 for (i = optind; i < argc; i++) {
201 if (stat(argv[i], &stbuf) == -1) {
204 } else if (strcmp(argv[i], "/usr/lib") == 0) {
205 warnx("WARNING! '%s' can not be used", argv[i]);
209 * See if this is a directory-containing
210 * file instead of a directory
212 if (S_ISREG(stbuf.st_mode))
213 rval |= dofile(argv[i], 0);
215 add_search_path(argv[i]);
220 for (i = 0; i < n_search_dirs; i++) {
221 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
231 for (i = 0; i < n_search_dirs; i++)
232 rval |= dodir(search_dirs[i], 1);
234 rval |= buildhints();
243 "usage: ldconfig [-32] [-aout | -elf] [-Rimrsv] [-f hints_file] [directory | file ...]\n");
248 dofile(char *fname, int silent)
251 char buf[MAXPATHLEN];
255 if ((hfp = fopen(fname, "r")) == NULL) {
260 while (fgets(buf, sizeof(buf), hfp)) {
264 if (*cp == '#' || *cp == '\0')
267 while (!isspace(*cp) && *cp != '\0')
272 warnx("%s: trailing characters ignored", sp);
277 rval |= dodir(sp, silent);
285 dodir(char *dir, int silent)
289 char name[MAXPATHLEN];
290 int dewey[MAXDEWEY], ndewey;
292 if ((dd = opendir(dir)) == NULL) {
293 if (silent && errno == ENOENT) /* Ignore the error */
299 while ((dp = readdir(dd)) != NULL) {
303 /* Check for `lib' prefix */
304 if (dp->d_name[0] != 'l' ||
305 dp->d_name[1] != 'i' ||
306 dp->d_name[2] != 'b')
309 /* Copy the entry minus prefix */
310 (void)strcpy(name, dp->d_name + 3);
315 /* Find ".so." in name */
316 for (cp = name + n - 4; cp > name; --cp) {
327 if (!isdigit(*(cp+4)))
330 bzero((caddr_t)dewey, sizeof(dewey));
331 ndewey = getdewey(dewey, cp + 4);
334 enter(dir, dp->d_name, name, dewey, ndewey);
342 enter(char *dir, char *file, char *name, int dewey[], int ndewey)
344 struct shlib_list *shp;
346 for (shp = shlib_head; shp; shp = shp->next) {
347 if (strcmp(name, shp->name) != 0 || major != shp->major)
350 /* Name matches existing entry */
351 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
353 /* Update this entry with higher versioned lib */
355 printf("Updating lib%s.%d.%d to %s/%s\n",
356 shp->name, shp->major, shp->minor,
360 shp->name = strdup(name);
362 shp->path = concat(dir, "/", file);
363 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
364 shp->ndewey = ndewey;
370 /* Name exists: older version or just updated */
373 /* Allocate new list element */
375 printf("Adding %s/%s\n", dir, file);
377 shp = (struct shlib_list *)xmalloc(sizeof *shp);
378 shp->name = strdup(name);
379 shp->path = concat(dir, "/", file);
380 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
381 shp->ndewey = ndewey;
385 shlib_tail = &shp->next;
390 hinthash(char *cp, int vmajor)
395 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
397 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
405 struct hints_header hdr;
406 struct hints_bucket *blist;
407 struct shlib_list *shp;
409 int i, n, str_index = 0;
410 int strtab_sz = 0; /* Total length of strings */
411 int nhints = 0; /* Total number of hints */
415 for (shp = shlib_head; shp; shp = shp->next) {
416 strtab_sz += 1 + strlen(shp->name);
417 strtab_sz += 1 + strlen(shp->path);
421 /* Fill hints file header */
422 hdr.hh_magic = HH_MAGIC;
423 hdr.hh_version = LD_HINTS_VERSION_2;
424 hdr.hh_nbucket = 1 * nhints;
425 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
426 hdr.hh_hashtab = sizeof(struct hints_header);
427 hdr.hh_strtab = hdr.hh_hashtab + n;
428 hdr.hh_dirlist = strtab_sz;
429 strtab_sz += 1 + strlen(dir_list);
430 hdr.hh_strtab_sz = strtab_sz;
431 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
434 printf("Totals: entries %d, buckets %ld, string size %d\n",
435 nhints, (long)hdr.hh_nbucket, strtab_sz);
437 /* Allocate buckets and string table */
438 blist = (struct hints_bucket *)xmalloc(n);
439 bzero((char *)blist, n);
440 for (i = 0; i < hdr.hh_nbucket; i++)
441 /* Empty all buckets */
442 blist[i].hi_next = -1;
444 strtab = (char *)xmalloc(strtab_sz);
447 for (shp = shlib_head; shp; shp = shp->next) {
448 struct hints_bucket *bp;
451 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
456 for (j = 0; j < hdr.hh_nbucket; j++) {
457 if (blist[j].hi_pathx == 0)
460 if (j == hdr.hh_nbucket) {
464 while (bp->hi_next != -1)
465 bp = &blist[bp->hi_next];
470 /* Insert strings in string table */
471 bp->hi_namex = str_index;
472 strcpy(strtab + str_index, shp->name);
473 str_index += 1 + strlen(shp->name);
475 bp->hi_pathx = str_index;
476 strcpy(strtab + str_index, shp->path);
477 str_index += 1 + strlen(shp->path);
480 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
481 bp->hi_ndewey = shp->ndewey;
484 /* Copy search directories */
485 strcpy(strtab + str_index, dir_list);
486 str_index += 1 + strlen(dir_list);
489 if (str_index != strtab_sz) {
490 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
493 tmpfilename = concat(hints_file, ".XXXXXXXXXX", "");
494 umask(0); /* Create with exact permissions */
495 if ((fd = mkstemp(tmpfilename)) == -1) {
496 warn("%s", tmpfilename);
501 if (write(fd, &hdr, sizeof(struct hints_header)) !=
502 sizeof(struct hints_header)) {
503 warn("%s", hints_file);
506 if (write(fd, blist, hdr.hh_nbucket * sizeof(*blist)) !=
507 (ssize_t)(hdr.hh_nbucket * sizeof(*blist))) {
508 warn("%s", hints_file);
511 if (write(fd, strtab, strtab_sz) != strtab_sz) {
512 warn("%s", hints_file);
515 if (close(fd) != 0) {
516 warn("%s", hints_file);
521 if (unlink(hints_file) != 0 && errno != ENOENT) {
522 warn("%s", hints_file);
526 if (rename(tmpfilename, hints_file) != 0) {
527 warn("%s", hints_file);
541 struct hints_header *hdr;
542 struct hints_bucket *blist;
544 struct shlib_list *shp;
547 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
548 warn("%s", hints_file);
553 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
555 if (addr == MAP_FAILED) {
556 warn("%s", hints_file);
560 hdr = (struct hints_header *)addr;
561 if (HH_BADMAG(*hdr)) {
562 warnx("%s: bad magic: %lo", hints_file,
563 (unsigned long)hdr->hh_magic);
567 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
568 hdr->hh_version != LD_HINTS_VERSION_2) {
569 warnx("unsupported version: %ld", (long)hdr->hh_version);
573 if (hdr->hh_ehints > msize) {
574 fsize = hdr->hh_ehints;
576 addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
577 if (addr == MAP_FAILED) {
578 warn("%s", hints_file);
581 hdr = (struct hints_header *)addr;
585 strtab = (char *)addr + hdr->hh_strtab;
587 if (hdr->hh_version >= LD_HINTS_VERSION_2)
588 add_search_path(strtab + hdr->hh_dirlist);
590 errx(1, "%s too old and does not contain the search path",
596 blist = malloc(sizeof(*blist) * hdr->hh_nbucket);
599 memcpy(blist, (char *)addr + hdr->hh_hashtab,
600 sizeof(*blist) * hdr->hh_nbucket);
603 for (i = 0; i < hdr->hh_nbucket; i++) {
604 struct hints_bucket *bp = &blist[i];
607 if (bp->hi_namex >= hdr->hh_strtab_sz) {
608 warnx("bad name index: %#x", bp->hi_namex);
612 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
613 warnx("bad path index: %#x", bp->hi_pathx);
618 /* Allocate new list element */
619 shp = (struct shlib_list *)xmalloc(sizeof *shp);
620 shp->name = strdup(strtab + bp->hi_namex);
621 shp->path = strdup(strtab + bp->hi_pathx);
622 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
623 shp->ndewey = bp->hi_ndewey;
627 shlib_tail = &shp->next;
637 struct shlib_list *shp;
640 printf("%s:\n", hints_file);
641 printf("\tsearch directories: %s\n", dir_list);
643 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
644 printf("\t%d:-l%s.%d.%d => %s\n",
645 i, shp->name, shp->major, shp->minor, shp->path);