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[] =
33 "$FreeBSD: src/sbin/ldconfig/ldconfig.c,v 1.31.2.3 2001/07/11 23:59:10 obrien Exp $";
36 #include <sys/param.h>
37 #include <sys/types.h>
43 #include <elf-hints.h>
48 #include <objformat.h>
61 #define _PATH_LD_HINTS "./ld.so.hints"
62 #undef _PATH_ELF_HINTS
63 #define _PATH_ELF_HINTS "./ld-elf.so.hints"
74 static char *hints_file;
77 /* Internal list of shared libraries found */
82 #define major dewey[0]
83 #define minor dewey[1]
84 struct shlib_list *next;
87 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
88 static char *dir_list;
90 static int buildhints __P((void));
91 static int dodir __P((char *, int));
92 int dofile __P((char *, int));
93 static void enter __P((char *, char *, char *, int *, int));
94 static void listhints __P((void));
95 static int readhints __P((void));
96 static void usage __P((void));
108 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
109 errx(1, "getobjformat failed: name too long");
110 if (strcmp(objformat, "aout") == 0)
112 else if (strcmp(objformat, "elf") == 0)
115 errx(1, "unknown object format \"%s\"", objformat);
117 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
120 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
151 list_elf_hints(hints_file);
153 update_elf_hints(hints_file, argc - optind,
154 argv + optind, merge || rescan);
158 /* Here begins the aout libs processing */
159 dir_list = strdup("");
161 if (justread || merge || rescan) {
162 if ((rval = readhints()) != 0)
166 if (!nostd && !merge && !rescan)
169 /* Add any directories/files from the command line */
171 for (i = optind; i < argc; i++) {
174 if (stat(argv[i], &stbuf) == -1) {
177 } else if (strcmp(argv[i], "/usr/lib") == 0) {
178 warnx("WARNING! '%s' can not be used", argv[i]);
182 * See if this is a directory-containing
183 * file instead of a directory
185 if (S_ISREG(stbuf.st_mode))
186 rval |= dofile(argv[i], 0);
188 add_search_path(argv[i]);
193 for (i = 0; i < n_search_dirs; i++) {
194 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
204 for (i = 0; i < n_search_dirs; i++)
205 rval |= dodir(search_dirs[i], 1);
207 rval |= buildhints();
216 "usage: ldconfig [-aout | -elf] [-Rimrsv] [-f hints_file] [dir | file ...]\n");
221 dofile(fname, silent)
226 char buf[MAXPATHLEN];
230 if ((hfp = fopen(fname, "r")) == NULL) {
235 while (fgets(buf, sizeof(buf), hfp)) {
239 if (*cp == '#' || *cp == '\0')
242 while (!isspace(*cp) && *cp != '\0')
247 warnx("%s: trailing characters ignored", sp);
252 rval |= dodir(sp, silent);
266 char name[MAXPATHLEN];
267 int dewey[MAXDEWEY], ndewey;
269 if ((dd = opendir(dir)) == NULL) {
270 if (silent && errno == ENOENT) /* Ignore the error */
276 while ((dp = readdir(dd)) != NULL) {
280 /* Check for `lib' prefix */
281 if (dp->d_name[0] != 'l' ||
282 dp->d_name[1] != 'i' ||
283 dp->d_name[2] != 'b')
286 /* Copy the entry minus prefix */
287 (void)strcpy(name, dp->d_name + 3);
292 /* Find ".so." in name */
293 for (cp = name + n - 4; cp > name; --cp) {
304 if (!isdigit(*(cp+4)))
307 bzero((caddr_t)dewey, sizeof(dewey));
308 ndewey = getdewey(dewey, cp + 4);
311 enter(dir, dp->d_name, name, dewey, ndewey);
319 enter(dir, file, name, dewey, ndewey)
320 char *dir, *file, *name;
323 struct shlib_list *shp;
325 for (shp = shlib_head; shp; shp = shp->next) {
326 if (strcmp(name, shp->name) != 0 || major != shp->major)
329 /* Name matches existing entry */
330 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
332 /* Update this entry with higher versioned lib */
334 printf("Updating lib%s.%d.%d to %s/%s\n",
335 shp->name, shp->major, shp->minor,
339 shp->name = strdup(name);
341 shp->path = concat(dir, "/", file);
342 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
343 shp->ndewey = ndewey;
349 /* Name exists: older version or just updated */
352 /* Allocate new list element */
354 printf("Adding %s/%s\n", dir, file);
356 shp = (struct shlib_list *)xmalloc(sizeof *shp);
357 shp->name = strdup(name);
358 shp->path = concat(dir, "/", file);
359 bcopy(dewey, shp->dewey, MAXDEWEY);
360 shp->ndewey = ndewey;
364 shlib_tail = &shp->next;
376 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
378 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
386 struct hints_header hdr;
387 struct hints_bucket *blist;
388 struct shlib_list *shp;
390 int i, n, str_index = 0;
391 int strtab_sz = 0; /* Total length of strings */
392 int nhints = 0; /* Total number of hints */
396 for (shp = shlib_head; shp; shp = shp->next) {
397 strtab_sz += 1 + strlen(shp->name);
398 strtab_sz += 1 + strlen(shp->path);
402 /* Fill hints file header */
403 hdr.hh_magic = HH_MAGIC;
404 hdr.hh_version = LD_HINTS_VERSION_2;
405 hdr.hh_nbucket = 1 * nhints;
406 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
407 hdr.hh_hashtab = sizeof(struct hints_header);
408 hdr.hh_strtab = hdr.hh_hashtab + n;
409 hdr.hh_dirlist = strtab_sz;
410 strtab_sz += 1 + strlen(dir_list);
411 hdr.hh_strtab_sz = strtab_sz;
412 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
415 printf("Totals: entries %d, buckets %ld, string size %d\n",
416 nhints, (long)hdr.hh_nbucket, strtab_sz);
418 /* Allocate buckets and string table */
419 blist = (struct hints_bucket *)xmalloc(n);
420 bzero((char *)blist, n);
421 for (i = 0; i < hdr.hh_nbucket; i++)
422 /* Empty all buckets */
423 blist[i].hi_next = -1;
425 strtab = (char *)xmalloc(strtab_sz);
428 for (shp = shlib_head; shp; shp = shp->next) {
429 struct hints_bucket *bp;
432 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
437 for (i = 0; i < hdr.hh_nbucket; i++) {
438 if (blist[i].hi_pathx == 0)
441 if (i == hdr.hh_nbucket) {
445 while (bp->hi_next != -1)
446 bp = &blist[bp->hi_next];
451 /* Insert strings in string table */
452 bp->hi_namex = str_index;
453 strcpy(strtab + str_index, shp->name);
454 str_index += 1 + strlen(shp->name);
456 bp->hi_pathx = str_index;
457 strcpy(strtab + str_index, shp->path);
458 str_index += 1 + strlen(shp->path);
461 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
462 bp->hi_ndewey = shp->ndewey;
465 /* Copy search directories */
466 strcpy(strtab + str_index, dir_list);
467 str_index += 1 + strlen(dir_list);
470 if (str_index != strtab_sz) {
471 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
474 tmpfile = concat(hints_file, ".XXXXXXXXXX", "");
475 umask(0); /* Create with exact permissions */
476 if ((fd = mkstemp(tmpfile)) == -1) {
482 if (write(fd, &hdr, sizeof(struct hints_header)) !=
483 sizeof(struct hints_header)) {
484 warn("%s", hints_file);
487 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
488 hdr.hh_nbucket * sizeof(struct hints_bucket)) {
489 warn("%s", hints_file);
492 if (write(fd, strtab, strtab_sz) != strtab_sz) {
493 warn("%s", hints_file);
496 if (close(fd) != 0) {
497 warn("%s", hints_file);
502 if (unlink(hints_file) != 0 && errno != ENOENT) {
503 warn("%s", hints_file);
507 if (rename(tmpfile, hints_file) != 0) {
508 warn("%s", hints_file);
522 struct hints_header *hdr;
523 struct hints_bucket *blist;
525 struct shlib_list *shp;
528 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
529 warn("%s", hints_file);
534 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
536 if (addr == MAP_FAILED) {
537 warn("%s", hints_file);
541 hdr = (struct hints_header *)addr;
542 if (HH_BADMAG(*hdr)) {
543 warnx("%s: bad magic: %lo", hints_file,
544 (unsigned long)hdr->hh_magic);
548 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
549 hdr->hh_version != LD_HINTS_VERSION_2) {
550 warnx("unsupported version: %ld", (long)hdr->hh_version);
554 if (hdr->hh_ehints > msize) {
555 fsize = hdr->hh_ehints;
557 addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
558 if (addr == MAP_FAILED) {
559 warn("%s", hints_file);
562 hdr = (struct hints_header *)addr;
566 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
567 strtab = (char *)(addr + hdr->hh_strtab);
569 if (hdr->hh_version >= LD_HINTS_VERSION_2)
570 add_search_path(strtab + hdr->hh_dirlist);
572 errx(1, "%s too old and does not contain the search path",
578 for (i = 0; i < hdr->hh_nbucket; i++) {
579 struct hints_bucket *bp = &blist[i];
582 if (bp->hi_namex >= hdr->hh_strtab_sz) {
583 warnx("bad name index: %#x", bp->hi_namex);
586 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
587 warnx("bad path index: %#x", bp->hi_pathx);
591 /* Allocate new list element */
592 shp = (struct shlib_list *)xmalloc(sizeof *shp);
593 shp->name = strdup(strtab + bp->hi_namex);
594 shp->path = strdup(strtab + bp->hi_pathx);
595 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
596 shp->ndewey = bp->hi_ndewey;
600 shlib_tail = &shp->next;
609 struct shlib_list *shp;
612 printf("%s:\n", hints_file);
613 printf("\tsearch directories: %s\n", dir_list);
615 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
616 printf("\t%d:-l%s.%d.%d => %s\n",
617 i, shp->name, shp->major, shp->minor, shp->path);