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.
30 * $FreeBSD: src/sbin/ldconfig/ldconfig.c,v 1.31.2.3 2001/07/11 23:59:10 obrien Exp $
31 * $DragonFly: src/sbin/ldconfig/ldconfig.c,v 1.2 2003/06/17 04:27:33 dillon Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
41 #include <elf-hints.h>
46 #include <objformat.h>
59 #define _PATH_LD_HINTS "./ld.so.hints"
60 #undef _PATH_ELF_HINTS
61 #define _PATH_ELF_HINTS "./ld-elf.so.hints"
72 static char *hints_file;
75 /* Internal list of shared libraries found */
80 #define major dewey[0]
81 #define minor dewey[1]
82 struct shlib_list *next;
85 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
86 static char *dir_list;
88 static int buildhints __P((void));
89 static int dodir __P((char *, int));
90 int dofile __P((char *, int));
91 static void enter __P((char *, char *, char *, int *, int));
92 static void listhints __P((void));
93 static int readhints __P((void));
94 static void usage __P((void));
106 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
107 errx(1, "getobjformat failed: name too long");
108 if (strcmp(objformat, "aout") == 0)
110 else if (strcmp(objformat, "elf") == 0)
113 errx(1, "unknown object format \"%s\"", objformat);
115 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
118 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
149 list_elf_hints(hints_file);
151 update_elf_hints(hints_file, argc - optind,
152 argv + optind, merge || rescan);
156 /* Here begins the aout libs processing */
157 dir_list = strdup("");
159 if (justread || merge || rescan) {
160 if ((rval = readhints()) != 0)
164 if (!nostd && !merge && !rescan)
167 /* Add any directories/files from the command line */
169 for (i = optind; i < argc; i++) {
172 if (stat(argv[i], &stbuf) == -1) {
175 } else if (strcmp(argv[i], "/usr/lib") == 0) {
176 warnx("WARNING! '%s' can not be used", argv[i]);
180 * See if this is a directory-containing
181 * file instead of a directory
183 if (S_ISREG(stbuf.st_mode))
184 rval |= dofile(argv[i], 0);
186 add_search_path(argv[i]);
191 for (i = 0; i < n_search_dirs; i++) {
192 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
202 for (i = 0; i < n_search_dirs; i++)
203 rval |= dodir(search_dirs[i], 1);
205 rval |= buildhints();
214 "usage: ldconfig [-aout | -elf] [-Rimrsv] [-f hints_file] [dir | file ...]\n");
219 dofile(fname, silent)
224 char buf[MAXPATHLEN];
228 if ((hfp = fopen(fname, "r")) == NULL) {
233 while (fgets(buf, sizeof(buf), hfp)) {
237 if (*cp == '#' || *cp == '\0')
240 while (!isspace(*cp) && *cp != '\0')
245 warnx("%s: trailing characters ignored", sp);
250 rval |= dodir(sp, silent);
264 char name[MAXPATHLEN];
265 int dewey[MAXDEWEY], ndewey;
267 if ((dd = opendir(dir)) == NULL) {
268 if (silent && errno == ENOENT) /* Ignore the error */
274 while ((dp = readdir(dd)) != NULL) {
278 /* Check for `lib' prefix */
279 if (dp->d_name[0] != 'l' ||
280 dp->d_name[1] != 'i' ||
281 dp->d_name[2] != 'b')
284 /* Copy the entry minus prefix */
285 (void)strcpy(name, dp->d_name + 3);
290 /* Find ".so." in name */
291 for (cp = name + n - 4; cp > name; --cp) {
302 if (!isdigit(*(cp+4)))
305 bzero((caddr_t)dewey, sizeof(dewey));
306 ndewey = getdewey(dewey, cp + 4);
309 enter(dir, dp->d_name, name, dewey, ndewey);
317 enter(dir, file, name, dewey, ndewey)
318 char *dir, *file, *name;
321 struct shlib_list *shp;
323 for (shp = shlib_head; shp; shp = shp->next) {
324 if (strcmp(name, shp->name) != 0 || major != shp->major)
327 /* Name matches existing entry */
328 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
330 /* Update this entry with higher versioned lib */
332 printf("Updating lib%s.%d.%d to %s/%s\n",
333 shp->name, shp->major, shp->minor,
337 shp->name = strdup(name);
339 shp->path = concat(dir, "/", file);
340 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
341 shp->ndewey = ndewey;
347 /* Name exists: older version or just updated */
350 /* Allocate new list element */
352 printf("Adding %s/%s\n", dir, file);
354 shp = (struct shlib_list *)xmalloc(sizeof *shp);
355 shp->name = strdup(name);
356 shp->path = concat(dir, "/", file);
357 bcopy(dewey, shp->dewey, MAXDEWEY);
358 shp->ndewey = ndewey;
362 shlib_tail = &shp->next;
374 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
376 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
384 struct hints_header hdr;
385 struct hints_bucket *blist;
386 struct shlib_list *shp;
388 int i, n, str_index = 0;
389 int strtab_sz = 0; /* Total length of strings */
390 int nhints = 0; /* Total number of hints */
394 for (shp = shlib_head; shp; shp = shp->next) {
395 strtab_sz += 1 + strlen(shp->name);
396 strtab_sz += 1 + strlen(shp->path);
400 /* Fill hints file header */
401 hdr.hh_magic = HH_MAGIC;
402 hdr.hh_version = LD_HINTS_VERSION_2;
403 hdr.hh_nbucket = 1 * nhints;
404 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
405 hdr.hh_hashtab = sizeof(struct hints_header);
406 hdr.hh_strtab = hdr.hh_hashtab + n;
407 hdr.hh_dirlist = strtab_sz;
408 strtab_sz += 1 + strlen(dir_list);
409 hdr.hh_strtab_sz = strtab_sz;
410 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
413 printf("Totals: entries %d, buckets %ld, string size %d\n",
414 nhints, (long)hdr.hh_nbucket, strtab_sz);
416 /* Allocate buckets and string table */
417 blist = (struct hints_bucket *)xmalloc(n);
418 bzero((char *)blist, n);
419 for (i = 0; i < hdr.hh_nbucket; i++)
420 /* Empty all buckets */
421 blist[i].hi_next = -1;
423 strtab = (char *)xmalloc(strtab_sz);
426 for (shp = shlib_head; shp; shp = shp->next) {
427 struct hints_bucket *bp;
430 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
435 for (i = 0; i < hdr.hh_nbucket; i++) {
436 if (blist[i].hi_pathx == 0)
439 if (i == hdr.hh_nbucket) {
443 while (bp->hi_next != -1)
444 bp = &blist[bp->hi_next];
449 /* Insert strings in string table */
450 bp->hi_namex = str_index;
451 strcpy(strtab + str_index, shp->name);
452 str_index += 1 + strlen(shp->name);
454 bp->hi_pathx = str_index;
455 strcpy(strtab + str_index, shp->path);
456 str_index += 1 + strlen(shp->path);
459 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
460 bp->hi_ndewey = shp->ndewey;
463 /* Copy search directories */
464 strcpy(strtab + str_index, dir_list);
465 str_index += 1 + strlen(dir_list);
468 if (str_index != strtab_sz) {
469 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
472 tmpfile = concat(hints_file, ".XXXXXXXXXX", "");
473 umask(0); /* Create with exact permissions */
474 if ((fd = mkstemp(tmpfile)) == -1) {
480 if (write(fd, &hdr, sizeof(struct hints_header)) !=
481 sizeof(struct hints_header)) {
482 warn("%s", hints_file);
485 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
486 hdr.hh_nbucket * sizeof(struct hints_bucket)) {
487 warn("%s", hints_file);
490 if (write(fd, strtab, strtab_sz) != strtab_sz) {
491 warn("%s", hints_file);
494 if (close(fd) != 0) {
495 warn("%s", hints_file);
500 if (unlink(hints_file) != 0 && errno != ENOENT) {
501 warn("%s", hints_file);
505 if (rename(tmpfile, hints_file) != 0) {
506 warn("%s", hints_file);
520 struct hints_header *hdr;
521 struct hints_bucket *blist;
523 struct shlib_list *shp;
526 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
527 warn("%s", hints_file);
532 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
534 if (addr == MAP_FAILED) {
535 warn("%s", hints_file);
539 hdr = (struct hints_header *)addr;
540 if (HH_BADMAG(*hdr)) {
541 warnx("%s: bad magic: %lo", hints_file,
542 (unsigned long)hdr->hh_magic);
546 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
547 hdr->hh_version != LD_HINTS_VERSION_2) {
548 warnx("unsupported version: %ld", (long)hdr->hh_version);
552 if (hdr->hh_ehints > msize) {
553 fsize = hdr->hh_ehints;
555 addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
556 if (addr == MAP_FAILED) {
557 warn("%s", hints_file);
560 hdr = (struct hints_header *)addr;
564 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
565 strtab = (char *)(addr + hdr->hh_strtab);
567 if (hdr->hh_version >= LD_HINTS_VERSION_2)
568 add_search_path(strtab + hdr->hh_dirlist);
570 errx(1, "%s too old and does not contain the search path",
576 for (i = 0; i < hdr->hh_nbucket; i++) {
577 struct hints_bucket *bp = &blist[i];
580 if (bp->hi_namex >= hdr->hh_strtab_sz) {
581 warnx("bad name index: %#x", bp->hi_namex);
584 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
585 warnx("bad path index: %#x", bp->hi_pathx);
589 /* Allocate new list element */
590 shp = (struct shlib_list *)xmalloc(sizeof *shp);
591 shp->name = strdup(strtab + bp->hi_namex);
592 shp->path = strdup(strtab + bp->hi_pathx);
593 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
594 shp->ndewey = bp->hi_ndewey;
598 shlib_tail = &shp->next;
607 struct shlib_list *shp;
610 printf("%s:\n", hints_file);
611 printf("\tsearch directories: %s\n", dir_list);
613 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
614 printf("\t%d:-l%s.%d.%d => %s\n",
615 i, shp->name, shp->major, shp->minor, shp->path);