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"
76 static const char *hints_file;
79 /* Internal list of shared libraries found */
84 #define major dewey[0]
85 #define minor dewey[1]
86 struct shlib_list *next;
89 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
90 static char *dir_list;
92 static int buildhints(void);
93 static int dodir(char *, int);
94 int dofile(char *, int);
95 static void enter(char *, char *, char *, int *, int);
96 static void listhints(void);
97 static int readhints(void);
98 static void usage(void);
101 * Note on aout/a.out support.
102 * To properly support shared libraries for compat2x, which are a.out, we need
103 * to support a.out here. As of 2013, bug reports are still coming in for this
104 * feature (on amd64 no less), so we know it is still in use.
108 main(int argc, char **argv)
116 if (strcmp(argv[1], "-aout") == 0) {
120 } else if (strcmp(argv[1], "-elf") == 0) {
124 } else if (strcmp(argv[1], "-32") == 0) {
134 hints_file = is_aout ? _PATH_LD32_HINTS : _PATH_ELF32_HINTS;
136 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
139 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
170 list_elf_hints(hints_file);
172 update_elf_hints(hints_file, argc - optind,
173 argv + optind, merge || rescan);
177 /* Here begins the aout libs processing */
178 dir_list = strdup("");
180 if (justread || merge || rescan) {
181 if ((rval = readhints()) != 0)
185 if (!nostd && !merge && !rescan)
188 /* Add any directories/files from the command line */
190 for (i = optind; i < argc; i++) {
193 if (stat(argv[i], &stbuf) == -1) {
196 } else if (strcmp(argv[i], "/usr/lib") == 0) {
197 warnx("WARNING! '%s' can not be used", argv[i]);
201 * See if this is a directory-containing
202 * file instead of a directory
204 if (S_ISREG(stbuf.st_mode))
205 rval |= dofile(argv[i], 0);
207 add_search_path(argv[i]);
212 for (i = 0; i < n_search_dirs; i++) {
213 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
223 for (i = 0; i < n_search_dirs; i++)
224 rval |= dodir(search_dirs[i], 1);
226 rval |= buildhints();
235 "usage: ldconfig [-32] [-aout | -elf] [-Rimrsv] [-f hints_file] [directory | file ...]\n");
240 dofile(char *fname, int silent)
243 char buf[MAXPATHLEN];
247 if ((hfp = fopen(fname, "r")) == NULL) {
252 while (fgets(buf, sizeof(buf), hfp)) {
256 if (*cp == '#' || *cp == '\0')
259 while (!isspace(*cp) && *cp != '\0')
264 warnx("%s: trailing characters ignored", sp);
269 rval |= dodir(sp, silent);
277 dodir(char *dir, int silent)
281 char name[MAXPATHLEN];
282 int dewey[MAXDEWEY], ndewey;
284 if ((dd = opendir(dir)) == NULL) {
285 if (silent && errno == ENOENT) /* Ignore the error */
291 while ((dp = readdir(dd)) != NULL) {
295 /* Check for `lib' prefix */
296 if (dp->d_name[0] != 'l' ||
297 dp->d_name[1] != 'i' ||
298 dp->d_name[2] != 'b')
301 /* Copy the entry minus prefix */
302 (void)strcpy(name, dp->d_name + 3);
307 /* Find ".so." in name */
308 for (cp = name + n - 4; cp > name; --cp) {
319 if (!isdigit(*(cp+4)))
322 bzero((caddr_t)dewey, sizeof(dewey));
323 ndewey = getdewey(dewey, cp + 4);
326 enter(dir, dp->d_name, name, dewey, ndewey);
334 enter(char *dir, char *file, char *name, int dewey[], int ndewey)
336 struct shlib_list *shp;
338 for (shp = shlib_head; shp; shp = shp->next) {
339 if (strcmp(name, shp->name) != 0 || major != shp->major)
342 /* Name matches existing entry */
343 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
345 /* Update this entry with higher versioned lib */
347 printf("Updating lib%s.%d.%d to %s/%s\n",
348 shp->name, shp->major, shp->minor,
352 shp->name = strdup(name);
354 shp->path = concat(dir, "/", file);
355 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
356 shp->ndewey = ndewey;
362 /* Name exists: older version or just updated */
365 /* Allocate new list element */
367 printf("Adding %s/%s\n", dir, file);
369 shp = (struct shlib_list *)xmalloc(sizeof *shp);
370 shp->name = strdup(name);
371 shp->path = concat(dir, "/", file);
372 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
373 shp->ndewey = ndewey;
377 shlib_tail = &shp->next;
382 hinthash(char *cp, int vmajor)
387 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
389 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
397 struct hints_header hdr;
398 struct hints_bucket *blist;
399 struct shlib_list *shp;
401 int i, n, str_index = 0;
402 int strtab_sz = 0; /* Total length of strings */
403 int nhints = 0; /* Total number of hints */
407 for (shp = shlib_head; shp; shp = shp->next) {
408 strtab_sz += 1 + strlen(shp->name);
409 strtab_sz += 1 + strlen(shp->path);
413 /* Fill hints file header */
414 hdr.hh_magic = HH_MAGIC;
415 hdr.hh_version = LD_HINTS_VERSION_2;
416 hdr.hh_nbucket = 1 * nhints;
417 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
418 hdr.hh_hashtab = sizeof(struct hints_header);
419 hdr.hh_strtab = hdr.hh_hashtab + n;
420 hdr.hh_dirlist = strtab_sz;
421 strtab_sz += 1 + strlen(dir_list);
422 hdr.hh_strtab_sz = strtab_sz;
423 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
426 printf("Totals: entries %d, buckets %ld, string size %d\n",
427 nhints, (long)hdr.hh_nbucket, strtab_sz);
429 /* Allocate buckets and string table */
430 blist = (struct hints_bucket *)xmalloc(n);
431 bzero((char *)blist, n);
432 for (i = 0; i < hdr.hh_nbucket; i++)
433 /* Empty all buckets */
434 blist[i].hi_next = -1;
436 strtab = (char *)xmalloc(strtab_sz);
439 for (shp = shlib_head; shp; shp = shp->next) {
440 struct hints_bucket *bp;
443 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
448 for (j = 0; j < hdr.hh_nbucket; j++) {
449 if (blist[j].hi_pathx == 0)
452 if (j == hdr.hh_nbucket) {
456 while (bp->hi_next != -1)
457 bp = &blist[bp->hi_next];
462 /* Insert strings in string table */
463 bp->hi_namex = str_index;
464 strcpy(strtab + str_index, shp->name);
465 str_index += 1 + strlen(shp->name);
467 bp->hi_pathx = str_index;
468 strcpy(strtab + str_index, shp->path);
469 str_index += 1 + strlen(shp->path);
472 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
473 bp->hi_ndewey = shp->ndewey;
476 /* Copy search directories */
477 strcpy(strtab + str_index, dir_list);
478 str_index += 1 + strlen(dir_list);
481 if (str_index != strtab_sz) {
482 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
485 tmpfilename = concat(hints_file, ".XXXXXXXXXX", "");
486 umask(0); /* Create with exact permissions */
487 if ((fd = mkstemp(tmpfilename)) == -1) {
488 warn("%s", tmpfilename);
493 if (write(fd, &hdr, sizeof(struct hints_header)) !=
494 sizeof(struct hints_header)) {
495 warn("%s", hints_file);
498 if (write(fd, blist, hdr.hh_nbucket * sizeof(*blist)) !=
499 (ssize_t)(hdr.hh_nbucket * sizeof(*blist))) {
500 warn("%s", hints_file);
503 if (write(fd, strtab, strtab_sz) != strtab_sz) {
504 warn("%s", hints_file);
507 if (close(fd) != 0) {
508 warn("%s", hints_file);
513 if (unlink(hints_file) != 0 && errno != ENOENT) {
514 warn("%s", hints_file);
518 if (rename(tmpfilename, hints_file) != 0) {
519 warn("%s", hints_file);
533 struct hints_header *hdr;
534 struct hints_bucket *blist;
536 struct shlib_list *shp;
539 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
540 warn("%s", hints_file);
545 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
547 if (addr == MAP_FAILED) {
548 warn("%s", hints_file);
552 hdr = (struct hints_header *)addr;
553 if (HH_BADMAG(*hdr)) {
554 warnx("%s: bad magic: %lo", hints_file,
555 (unsigned long)hdr->hh_magic);
559 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
560 hdr->hh_version != LD_HINTS_VERSION_2) {
561 warnx("unsupported version: %ld", (long)hdr->hh_version);
565 if (hdr->hh_ehints > msize) {
566 fsize = hdr->hh_ehints;
568 addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
569 if (addr == MAP_FAILED) {
570 warn("%s", hints_file);
573 hdr = (struct hints_header *)addr;
577 strtab = (char *)addr + hdr->hh_strtab;
579 if (hdr->hh_version >= LD_HINTS_VERSION_2)
580 add_search_path(strtab + hdr->hh_dirlist);
582 errx(1, "%s too old and does not contain the search path",
588 blist = malloc(sizeof(*blist) * hdr->hh_nbucket);
591 memcpy(blist, (char *)addr + hdr->hh_hashtab,
592 sizeof(*blist) * hdr->hh_nbucket);
595 for (i = 0; i < hdr->hh_nbucket; i++) {
596 struct hints_bucket *bp = &blist[i];
599 if (bp->hi_namex >= hdr->hh_strtab_sz) {
600 warnx("bad name index: %#x", bp->hi_namex);
604 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
605 warnx("bad path index: %#x", bp->hi_pathx);
610 /* Allocate new list element */
611 shp = (struct shlib_list *)xmalloc(sizeof *shp);
612 shp->name = strdup(strtab + bp->hi_namex);
613 shp->path = strdup(strtab + bp->hi_pathx);
614 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
615 shp->ndewey = bp->hi_ndewey;
619 shlib_tail = &shp->next;
629 struct shlib_list *shp;
632 printf("%s:\n", hints_file);
633 printf("\tsearch directories: %s\n", dir_list);
635 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
636 printf("\t%d:-l%s.%d.%d => %s\n",
637 i, shp->name, shp->major, shp->minor, shp->path);