xref: /freebsd/sbin/ldconfig/ldconfig.c (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
1b9ae52e3SPaul Richards /*
20f6b2cb3SPaul Traina  * Copyright (c) 1993,1995 Paul Kranenburg
3b9ae52e3SPaul Richards  * All rights reserved.
4b9ae52e3SPaul Richards  *
5b9ae52e3SPaul Richards  * Redistribution and use in source and binary forms, with or without
6b9ae52e3SPaul Richards  * modification, are permitted provided that the following conditions
7b9ae52e3SPaul Richards  * are met:
8b9ae52e3SPaul Richards  * 1. Redistributions of source code must retain the above copyright
9b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer.
10b9ae52e3SPaul Richards  * 2. Redistributions in binary form must reproduce the above copyright
11b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer in the
12b9ae52e3SPaul Richards  *    documentation and/or other materials provided with the distribution.
13b9ae52e3SPaul Richards  * 3. All advertising materials mentioning features or use of this software
14b9ae52e3SPaul Richards  *    must display the following acknowledgement:
15b9ae52e3SPaul Richards  *      This product includes software developed by Paul Kranenburg.
16b9ae52e3SPaul Richards  * 4. The name of the author may not be used to endorse or promote products
1709e3d49dSJordan K. Hubbard  *    derived from this software without specific prior written permission
18b9ae52e3SPaul Richards  *
19b9ae52e3SPaul Richards  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20b9ae52e3SPaul Richards  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21b9ae52e3SPaul Richards  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22b9ae52e3SPaul Richards  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23b9ae52e3SPaul Richards  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24b9ae52e3SPaul Richards  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b9ae52e3SPaul Richards  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b9ae52e3SPaul Richards  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b9ae52e3SPaul Richards  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28b9ae52e3SPaul Richards  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b9ae52e3SPaul Richards  */
30b9ae52e3SPaul Richards 
31b50d7faeSPhilippe Charnier #ifndef lint
32b50d7faeSPhilippe Charnier static const char rcsid[] =
337f3dea24SPeter Wemm   "$FreeBSD$";
34b50d7faeSPhilippe Charnier #endif /* not lint */
35b50d7faeSPhilippe Charnier 
36b9ae52e3SPaul Richards #include <sys/param.h>
37b9ae52e3SPaul Richards #include <sys/types.h>
38b9ae52e3SPaul Richards #include <sys/stat.h>
39b9ae52e3SPaul Richards #include <sys/mman.h>
40b9ae52e3SPaul Richards #include <a.out.h>
41b50d7faeSPhilippe Charnier #include <ctype.h>
42b50d7faeSPhilippe Charnier #include <dirent.h>
43a565ca59SJohn Polstra #include <elf.h>
44b50d7faeSPhilippe Charnier #include <err.h>
45b50d7faeSPhilippe Charnier #include <errno.h>
46b50d7faeSPhilippe Charnier #include <fcntl.h>
47b50d7faeSPhilippe Charnier #include <link.h>
48cfa4d739SJohn Polstra #include <objformat.h>
49699e1b82SRich Murphey #include <stdio.h>
50699e1b82SRich Murphey #include <stdlib.h>
51b9ae52e3SPaul Richards #include <string.h>
52699e1b82SRich Murphey #include <unistd.h>
53b9ae52e3SPaul Richards 
54a565ca59SJohn Polstra #include "ldconfig.h"
5580c71499SPeter Wemm #include "shlib.h"
5680c71499SPeter Wemm #include "support.h"
5780c71499SPeter Wemm 
587c6da7dcSJohn Polstra #if DEBUG
597c6da7dcSJohn Polstra /* test */
607c6da7dcSJohn Polstra #undef _PATH_LD_HINTS
617c6da7dcSJohn Polstra #define _PATH_LD_HINTS		"./ld.so.hints"
62a565ca59SJohn Polstra #undef _PATH_ELF_HINTS
63a565ca59SJohn Polstra #define _PATH_ELF_HINTS		"./ld-elf.so.hints"
647c6da7dcSJohn Polstra #endif
65b9ae52e3SPaul Richards 
66b9ae52e3SPaul Richards #undef major
67b9ae52e3SPaul Richards #undef minor
68b9ae52e3SPaul Richards 
69b9ae52e3SPaul Richards static int			verbose;
70b9ae52e3SPaul Richards static int			nostd;
71b9ae52e3SPaul Richards static int			justread;
72f606c848SSatoshi Asami static int			merge;
73d4ba5766SPeter Wemm static int			rescan;
74a565ca59SJohn Polstra static char			*hints_file;
75b9ae52e3SPaul Richards 
76b9ae52e3SPaul Richards struct shlib_list {
77b9ae52e3SPaul Richards 	/* Internal list of shared libraries found */
78b9ae52e3SPaul Richards 	char			*name;
79b9ae52e3SPaul Richards 	char			*path;
80b9ae52e3SPaul Richards 	int			dewey[MAXDEWEY];
81b9ae52e3SPaul Richards 	int			ndewey;
82b9ae52e3SPaul Richards #define major dewey[0]
83b9ae52e3SPaul Richards #define minor dewey[1]
84b9ae52e3SPaul Richards 	struct shlib_list	*next;
85b9ae52e3SPaul Richards };
86b9ae52e3SPaul Richards 
87b9ae52e3SPaul Richards static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
8880c71499SPeter Wemm static char			*dir_list;
89b9ae52e3SPaul Richards 
90a565ca59SJohn Polstra static int		buildhints __P((void));
9109e3d49dSJordan K. Hubbard static int		dodir __P((char *, int));
92b50d7faeSPhilippe Charnier int			dofile __P((char *, int));
93a565ca59SJohn Polstra static void		enter __P((char *, char *, char *, int *, int));
94f606c848SSatoshi Asami static void		listhints __P((void));
95a565ca59SJohn Polstra static int		readhints __P((void));
96b50d7faeSPhilippe Charnier static void		usage __P((void));
97b9ae52e3SPaul Richards 
98b9ae52e3SPaul Richards int
99b9ae52e3SPaul Richards main(argc, argv)
100b9ae52e3SPaul Richards int	argc;
101b9ae52e3SPaul Richards char	*argv[];
102b9ae52e3SPaul Richards {
103b9ae52e3SPaul Richards 	int		i, c;
104b9ae52e3SPaul Richards 	int		rval = 0;
105cfa4d739SJohn Polstra 	char		objformat[32];
106cfa4d739SJohn Polstra 	int		is_aout;
107b9ae52e3SPaul Richards 
108cfa4d739SJohn Polstra 	if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
109cfa4d739SJohn Polstra 		errx(1, "getobjformat failed: name too long");
110cfa4d739SJohn Polstra 	if (strcmp(objformat, "aout") == 0)
111cfa4d739SJohn Polstra 		is_aout = 1;
112cfa4d739SJohn Polstra 	else if (strcmp(objformat, "elf") == 0)
113cfa4d739SJohn Polstra 		is_aout = 0;
114cfa4d739SJohn Polstra 	else
115cfa4d739SJohn Polstra 		errx(1, "unknown object format \"%s\"", objformat);
116cfa4d739SJohn Polstra 
117cfa4d739SJohn Polstra 	hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
118b50d7faeSPhilippe Charnier 	while ((c = getopt(argc, argv, "Rf:mrsv")) != -1) {
119b9ae52e3SPaul Richards 		switch (c) {
120d4ba5766SPeter Wemm 		case 'R':
121d4ba5766SPeter Wemm 			rescan = 1;
122d4ba5766SPeter Wemm 			break;
1237c6da7dcSJohn Polstra 		case 'f':
1247c6da7dcSJohn Polstra 			hints_file = optarg;
1257c6da7dcSJohn Polstra 			break;
126f606c848SSatoshi Asami 		case 'm':
127f606c848SSatoshi Asami 			merge = 1;
128b9ae52e3SPaul Richards 			break;
129b9ae52e3SPaul Richards 		case 'r':
130b9ae52e3SPaul Richards 			justread = 1;
131b9ae52e3SPaul Richards 			break;
132f606c848SSatoshi Asami 		case 's':
133f606c848SSatoshi Asami 			nostd = 1;
134f606c848SSatoshi Asami 			break;
135f606c848SSatoshi Asami 		case 'v':
136f606c848SSatoshi Asami 			verbose = 1;
137f606c848SSatoshi Asami 			break;
138b9ae52e3SPaul Richards 		default:
139b50d7faeSPhilippe Charnier 			usage();
140b9ae52e3SPaul Richards 			break;
141b9ae52e3SPaul Richards 		}
142b9ae52e3SPaul Richards 	}
143b9ae52e3SPaul Richards 
144cfa4d739SJohn Polstra 	if (!is_aout) {
145a565ca59SJohn Polstra 		if (justread)
146a565ca59SJohn Polstra 			list_elf_hints(hints_file);
147a565ca59SJohn Polstra 		else
148a565ca59SJohn Polstra 			update_elf_hints(hints_file, argc - optind,
149a565ca59SJohn Polstra 			    argv + optind, merge || rescan);
150a565ca59SJohn Polstra 		return 0;
151a565ca59SJohn Polstra 	}
152a565ca59SJohn Polstra 
15380c71499SPeter Wemm 	dir_list = strdup("");
15480c71499SPeter Wemm 
155d4ba5766SPeter Wemm 	if (justread || merge || rescan) {
156f606c848SSatoshi Asami 		if ((rval = readhints()) != 0)
157f606c848SSatoshi Asami 			return rval;
15880c71499SPeter Wemm 	}
15980c71499SPeter Wemm 
160d4ba5766SPeter Wemm 	if (!nostd && !merge && !rescan)
16180c71499SPeter Wemm 		std_search_path();
16280c71499SPeter Wemm 
163571b472bSJordan K. Hubbard 	/* Add any directories/files from the command line */
164571b472bSJordan K. Hubbard 	if (!justread) {
165d66f9d22SJohn Polstra 		for (i = optind; i < argc; i++) {
166571b472bSJordan K. Hubbard 			struct stat stbuf;
167571b472bSJordan K. Hubbard 
168571b472bSJordan K. Hubbard 			if (stat(argv[i], &stbuf) == -1) {
169d66f9d22SJohn Polstra 				warn("%s", argv[i]);
170d66f9d22SJohn Polstra 				rval = -1;
171614d19caSJohn Polstra 			} else if (strcmp(argv[i], "/usr/lib") == 0) {
172cabb97dcSSøren Schmidt 				warnx("WARNING! '%s' can not be used", argv[i]);
173cabb97dcSSøren Schmidt 				rval = -1;
174614d19caSJohn Polstra 			} else {
175571b472bSJordan K. Hubbard 				/*
176571b472bSJordan K. Hubbard 				 * See if this is a directory-containing
177571b472bSJordan K. Hubbard 				 * file instead of a directory
178571b472bSJordan K. Hubbard 				 */
179571b472bSJordan K. Hubbard 				if (S_ISREG(stbuf.st_mode))
180571b472bSJordan K. Hubbard 					rval |= dofile(argv[i], 0);
181571b472bSJordan K. Hubbard 				else
18280c71499SPeter Wemm 					add_search_path(argv[i]);
183d66f9d22SJohn Polstra 			}
184d66f9d22SJohn Polstra 		}
185571b472bSJordan K. Hubbard 	}
18680c71499SPeter Wemm 
18780c71499SPeter Wemm 	for (i = 0; i < n_search_dirs; i++) {
18880c71499SPeter Wemm 		char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
18980c71499SPeter Wemm 		free(dir_list);
19080c71499SPeter Wemm 		dir_list = cp;
19180c71499SPeter Wemm 	}
19280c71499SPeter Wemm 
193f606c848SSatoshi Asami 	if (justread) {
194f606c848SSatoshi Asami 		listhints();
195526195adSJordan K. Hubbard 		return 0;
196f606c848SSatoshi Asami 	}
197b9ae52e3SPaul Richards 
198b9ae52e3SPaul Richards 	for (i = 0; i < n_search_dirs; i++)
19909e3d49dSJordan K. Hubbard 		rval |= dodir(search_dirs[i], 1);
200b9ae52e3SPaul Richards 
201f606c848SSatoshi Asami 	rval |= buildhints();
202b9ae52e3SPaul Richards 
203b9ae52e3SPaul Richards 	return rval;
204b9ae52e3SPaul Richards }
205b9ae52e3SPaul Richards 
206b50d7faeSPhilippe Charnier static void
207b50d7faeSPhilippe Charnier usage()
208b50d7faeSPhilippe Charnier {
209b50d7faeSPhilippe Charnier 	fprintf(stderr,
21065a74e45SBill Fumerola 	"usage: ldconfig [-aout | -elf] [-Rmrsv] [-f hints_file] [dir | file ...]\n");
211b50d7faeSPhilippe Charnier 	exit(1);
212b50d7faeSPhilippe Charnier }
213b50d7faeSPhilippe Charnier 
214b9ae52e3SPaul Richards int
215571b472bSJordan K. Hubbard dofile(fname, silent)
216571b472bSJordan K. Hubbard char	*fname;
217571b472bSJordan K. Hubbard int	silent;
218571b472bSJordan K. Hubbard {
219571b472bSJordan K. Hubbard 	FILE *hfp;
220571b472bSJordan K. Hubbard 	char buf[MAXPATHLEN];
221571b472bSJordan K. Hubbard 	int rval = 0;
222571b472bSJordan K. Hubbard 	char *cp, *sp;
223571b472bSJordan K. Hubbard 
224571b472bSJordan K. Hubbard 	if ((hfp = fopen(fname, "r")) == NULL) {
225571b472bSJordan K. Hubbard 		warn("%s", fname);
226571b472bSJordan K. Hubbard 		return -1;
227571b472bSJordan K. Hubbard 	}
228571b472bSJordan K. Hubbard 
229571b472bSJordan K. Hubbard 	while (fgets(buf, sizeof(buf), hfp)) {
230571b472bSJordan K. Hubbard 		cp = buf;
231571b472bSJordan K. Hubbard 		while (isspace(*cp))
232571b472bSJordan K. Hubbard 			cp++;
233571b472bSJordan K. Hubbard 		if (*cp == '#' || *cp == '\0')
234571b472bSJordan K. Hubbard 			continue;
235571b472bSJordan K. Hubbard 		sp = cp;
236571b472bSJordan K. Hubbard 		while (!isspace(*cp) && *cp != '\0')
237571b472bSJordan K. Hubbard 			cp++;
238571b472bSJordan K. Hubbard 
239571b472bSJordan K. Hubbard 		if (*cp != '\n') {
240571b472bSJordan K. Hubbard 			*cp = '\0';
241b50d7faeSPhilippe Charnier 			warnx("%s: trailing characters ignored", sp);
242571b472bSJordan K. Hubbard 		}
243571b472bSJordan K. Hubbard 
244571b472bSJordan K. Hubbard 		*cp = '\0';
245571b472bSJordan K. Hubbard 
246571b472bSJordan K. Hubbard 		rval |= dodir(sp, silent);
247571b472bSJordan K. Hubbard 	}
248571b472bSJordan K. Hubbard 
249571b472bSJordan K. Hubbard 	(void)fclose(hfp);
250571b472bSJordan K. Hubbard 	return rval;
251571b472bSJordan K. Hubbard }
252571b472bSJordan K. Hubbard 
253571b472bSJordan K. Hubbard int
25409e3d49dSJordan K. Hubbard dodir(dir, silent)
255b9ae52e3SPaul Richards char	*dir;
25609e3d49dSJordan K. Hubbard int	silent;
257b9ae52e3SPaul Richards {
258b9ae52e3SPaul Richards 	DIR		*dd;
259b9ae52e3SPaul Richards 	struct dirent	*dp;
2600f6b2cb3SPaul Traina 	char		name[MAXPATHLEN];
261b9ae52e3SPaul Richards 	int		dewey[MAXDEWEY], ndewey;
262b9ae52e3SPaul Richards 
263b9ae52e3SPaul Richards 	if ((dd = opendir(dir)) == NULL) {
264d66f9d22SJohn Polstra 		if (silent && errno == ENOENT)	/* Ignore the error */
265d66f9d22SJohn Polstra 			return 0;
266f606c848SSatoshi Asami 		warn("%s", dir);
267b9ae52e3SPaul Richards 		return -1;
268b9ae52e3SPaul Richards 	}
269b9ae52e3SPaul Richards 
270b9ae52e3SPaul Richards 	while ((dp = readdir(dd)) != NULL) {
2710f6b2cb3SPaul Traina 		register int n;
2720f6b2cb3SPaul Traina 		register char *cp;
273b9ae52e3SPaul Richards 
2740f6b2cb3SPaul Traina 		/* Check for `lib' prefix */
2750f6b2cb3SPaul Traina 		if (dp->d_name[0] != 'l' ||
2760f6b2cb3SPaul Traina 		    dp->d_name[1] != 'i' ||
2770f6b2cb3SPaul Traina 		    dp->d_name[2] != 'b')
278b9ae52e3SPaul Richards 			continue;
279b9ae52e3SPaul Richards 
2800f6b2cb3SPaul Traina 		/* Copy the entry minus prefix */
2810f6b2cb3SPaul Traina 		(void)strcpy(name, dp->d_name + 3);
2820f6b2cb3SPaul Traina 		n = strlen(name);
2830f6b2cb3SPaul Traina 		if (n < 4)
2840f6b2cb3SPaul Traina 			continue;
2850f6b2cb3SPaul Traina 
2860f6b2cb3SPaul Traina 		/* Find ".so." in name */
2870f6b2cb3SPaul Traina 		for (cp = name + n - 4; cp > name; --cp) {
2880f6b2cb3SPaul Traina 			if (cp[0] == '.' &&
2890f6b2cb3SPaul Traina 			    cp[1] == 's' &&
2900f6b2cb3SPaul Traina 			    cp[2] == 'o' &&
2910f6b2cb3SPaul Traina 			    cp[3] == '.')
2920f6b2cb3SPaul Traina 				break;
2930f6b2cb3SPaul Traina 		}
2940f6b2cb3SPaul Traina 		if (cp <= name)
2950f6b2cb3SPaul Traina 			continue;
2960f6b2cb3SPaul Traina 
2970f6b2cb3SPaul Traina 		*cp = '\0';
2980f6b2cb3SPaul Traina 		if (!isdigit(*(cp+4)))
2990f6b2cb3SPaul Traina 			continue;
3000f6b2cb3SPaul Traina 
3010f6b2cb3SPaul Traina 		bzero((caddr_t)dewey, sizeof(dewey));
3020f6b2cb3SPaul Traina 		ndewey = getdewey(dewey, cp + 4);
3035f8d88ddSJohn Polstra 		if (ndewey < 2)
3045f8d88ddSJohn Polstra 			continue;
305b9ae52e3SPaul Richards 		enter(dir, dp->d_name, name, dewey, ndewey);
306b9ae52e3SPaul Richards 	}
307b9ae52e3SPaul Richards 
308571b472bSJordan K. Hubbard 	closedir(dd);
309b9ae52e3SPaul Richards 	return 0;
310b9ae52e3SPaul Richards }
311b9ae52e3SPaul Richards 
312b9ae52e3SPaul Richards static void
313b9ae52e3SPaul Richards enter(dir, file, name, dewey, ndewey)
314b9ae52e3SPaul Richards char	*dir, *file, *name;
315b9ae52e3SPaul Richards int	dewey[], ndewey;
316b9ae52e3SPaul Richards {
317b9ae52e3SPaul Richards 	struct shlib_list	*shp;
318b9ae52e3SPaul Richards 
319b9ae52e3SPaul Richards 	for (shp = shlib_head; shp; shp = shp->next) {
320b9ae52e3SPaul Richards 		if (strcmp(name, shp->name) != 0 || major != shp->major)
321b9ae52e3SPaul Richards 			continue;
322b9ae52e3SPaul Richards 
323b9ae52e3SPaul Richards 		/* Name matches existing entry */
324b9ae52e3SPaul Richards 		if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
325b9ae52e3SPaul Richards 
326b9ae52e3SPaul Richards 			/* Update this entry with higher versioned lib */
327b9ae52e3SPaul Richards 			if (verbose)
328b9ae52e3SPaul Richards 				printf("Updating lib%s.%d.%d to %s/%s\n",
329b9ae52e3SPaul Richards 					shp->name, shp->major, shp->minor,
330b9ae52e3SPaul Richards 					dir, file);
331b9ae52e3SPaul Richards 
332b9ae52e3SPaul Richards 			free(shp->name);
333b9ae52e3SPaul Richards 			shp->name = strdup(name);
334b9ae52e3SPaul Richards 			free(shp->path);
335b9ae52e3SPaul Richards 			shp->path = concat(dir, "/", file);
336b9ae52e3SPaul Richards 			bcopy(dewey, shp->dewey, sizeof(shp->dewey));
337b9ae52e3SPaul Richards 			shp->ndewey = ndewey;
338b9ae52e3SPaul Richards 		}
339b9ae52e3SPaul Richards 		break;
340b9ae52e3SPaul Richards 	}
341b9ae52e3SPaul Richards 
342b9ae52e3SPaul Richards 	if (shp)
343b9ae52e3SPaul Richards 		/* Name exists: older version or just updated */
344b9ae52e3SPaul Richards 		return;
345b9ae52e3SPaul Richards 
346b9ae52e3SPaul Richards 	/* Allocate new list element */
347b9ae52e3SPaul Richards 	if (verbose)
348b9ae52e3SPaul Richards 		printf("Adding %s/%s\n", dir, file);
349b9ae52e3SPaul Richards 
350b9ae52e3SPaul Richards 	shp = (struct shlib_list *)xmalloc(sizeof *shp);
351b9ae52e3SPaul Richards 	shp->name = strdup(name);
352b9ae52e3SPaul Richards 	shp->path = concat(dir, "/", file);
353b9ae52e3SPaul Richards 	bcopy(dewey, shp->dewey, MAXDEWEY);
354b9ae52e3SPaul Richards 	shp->ndewey = ndewey;
355b9ae52e3SPaul Richards 	shp->next = NULL;
356b9ae52e3SPaul Richards 
357b9ae52e3SPaul Richards 	*shlib_tail = shp;
358b9ae52e3SPaul Richards 	shlib_tail = &shp->next;
359b9ae52e3SPaul Richards }
360b9ae52e3SPaul Richards 
361b9ae52e3SPaul Richards 
362b9ae52e3SPaul Richards int
363d5453ba5SJoerg Wunsch hinthash(cp, vmajor)
364b9ae52e3SPaul Richards char	*cp;
365d5453ba5SJoerg Wunsch int	vmajor;
366b9ae52e3SPaul Richards {
367b9ae52e3SPaul Richards 	int	k = 0;
368b9ae52e3SPaul Richards 
369b9ae52e3SPaul Richards 	while (*cp)
370b9ae52e3SPaul Richards 		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
371b9ae52e3SPaul Richards 
372b9ae52e3SPaul Richards 	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
373b9ae52e3SPaul Richards 
374b9ae52e3SPaul Richards 	return k;
375b9ae52e3SPaul Richards }
376b9ae52e3SPaul Richards 
377b9ae52e3SPaul Richards int
378f606c848SSatoshi Asami buildhints()
379b9ae52e3SPaul Richards {
380b9ae52e3SPaul Richards 	struct hints_header	hdr;
381b9ae52e3SPaul Richards 	struct hints_bucket	*blist;
382b9ae52e3SPaul Richards 	struct shlib_list	*shp;
383b9ae52e3SPaul Richards 	char			*strtab;
384b9ae52e3SPaul Richards 	int			i, n, str_index = 0;
385b9ae52e3SPaul Richards 	int			strtab_sz = 0;	/* Total length of strings */
386b9ae52e3SPaul Richards 	int			nhints = 0;	/* Total number of hints */
387b9ae52e3SPaul Richards 	int			fd;
388b9ae52e3SPaul Richards 	char			*tmpfile;
389b9ae52e3SPaul Richards 
390b9ae52e3SPaul Richards 	for (shp = shlib_head; shp; shp = shp->next) {
391b9ae52e3SPaul Richards 		strtab_sz += 1 + strlen(shp->name);
392b9ae52e3SPaul Richards 		strtab_sz += 1 + strlen(shp->path);
393b9ae52e3SPaul Richards 		nhints++;
394b9ae52e3SPaul Richards 	}
395b9ae52e3SPaul Richards 
396b9ae52e3SPaul Richards 	/* Fill hints file header */
397b9ae52e3SPaul Richards 	hdr.hh_magic = HH_MAGIC;
39880c71499SPeter Wemm 	hdr.hh_version = LD_HINTS_VERSION_2;
399b9ae52e3SPaul Richards 	hdr.hh_nbucket = 1 * nhints;
400b9ae52e3SPaul Richards 	n = hdr.hh_nbucket * sizeof(struct hints_bucket);
401b9ae52e3SPaul Richards 	hdr.hh_hashtab = sizeof(struct hints_header);
402b9ae52e3SPaul Richards 	hdr.hh_strtab = hdr.hh_hashtab + n;
40380c71499SPeter Wemm 	hdr.hh_dirlist = strtab_sz;
40480c71499SPeter Wemm 	strtab_sz += 1 + strlen(dir_list);
405b9ae52e3SPaul Richards 	hdr.hh_strtab_sz = strtab_sz;
406b9ae52e3SPaul Richards 	hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
407b9ae52e3SPaul Richards 
408b9ae52e3SPaul Richards 	if (verbose)
409526195adSJordan K. Hubbard 		printf("Totals: entries %d, buckets %ld, string size %d\n",
410ab845347SBruce Evans 			nhints, (long)hdr.hh_nbucket, strtab_sz);
411b9ae52e3SPaul Richards 
412b9ae52e3SPaul Richards 	/* Allocate buckets and string table */
413b9ae52e3SPaul Richards 	blist = (struct hints_bucket *)xmalloc(n);
414b9ae52e3SPaul Richards 	bzero((char *)blist, n);
415b9ae52e3SPaul Richards 	for (i = 0; i < hdr.hh_nbucket; i++)
416b9ae52e3SPaul Richards 		/* Empty all buckets */
417b9ae52e3SPaul Richards 		blist[i].hi_next = -1;
418b9ae52e3SPaul Richards 
419b9ae52e3SPaul Richards 	strtab = (char *)xmalloc(strtab_sz);
420b9ae52e3SPaul Richards 
421b9ae52e3SPaul Richards 	/* Enter all */
422b9ae52e3SPaul Richards 	for (shp = shlib_head; shp; shp = shp->next) {
423b9ae52e3SPaul Richards 		struct hints_bucket	*bp;
424b9ae52e3SPaul Richards 
425b9ae52e3SPaul Richards 		bp = blist +
426d5453ba5SJoerg Wunsch 		  (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
427b9ae52e3SPaul Richards 
428b9ae52e3SPaul Richards 		if (bp->hi_pathx) {
429b9ae52e3SPaul Richards 			int	i;
430b9ae52e3SPaul Richards 
431b9ae52e3SPaul Richards 			for (i = 0; i < hdr.hh_nbucket; i++) {
432b9ae52e3SPaul Richards 				if (blist[i].hi_pathx == 0)
433b9ae52e3SPaul Richards 					break;
434b9ae52e3SPaul Richards 			}
435b9ae52e3SPaul Richards 			if (i == hdr.hh_nbucket) {
436b50d7faeSPhilippe Charnier 				warnx("bummer!");
437b9ae52e3SPaul Richards 				return -1;
438b9ae52e3SPaul Richards 			}
439b9ae52e3SPaul Richards 			while (bp->hi_next != -1)
440b9ae52e3SPaul Richards 				bp = &blist[bp->hi_next];
441b9ae52e3SPaul Richards 			bp->hi_next = i;
442b9ae52e3SPaul Richards 			bp = blist + i;
443b9ae52e3SPaul Richards 		}
444b9ae52e3SPaul Richards 
445b9ae52e3SPaul Richards 		/* Insert strings in string table */
446b9ae52e3SPaul Richards 		bp->hi_namex = str_index;
447b9ae52e3SPaul Richards 		strcpy(strtab + str_index, shp->name);
448b9ae52e3SPaul Richards 		str_index += 1 + strlen(shp->name);
449b9ae52e3SPaul Richards 
450b9ae52e3SPaul Richards 		bp->hi_pathx = str_index;
451b9ae52e3SPaul Richards 		strcpy(strtab + str_index, shp->path);
452b9ae52e3SPaul Richards 		str_index += 1 + strlen(shp->path);
453b9ae52e3SPaul Richards 
454b9ae52e3SPaul Richards 		/* Copy versions */
455b9ae52e3SPaul Richards 		bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
456b9ae52e3SPaul Richards 		bp->hi_ndewey = shp->ndewey;
457b9ae52e3SPaul Richards 	}
458b9ae52e3SPaul Richards 
45980c71499SPeter Wemm 	/* Copy search directories */
46080c71499SPeter Wemm 	strcpy(strtab + str_index, dir_list);
46180c71499SPeter Wemm 	str_index += 1 + strlen(dir_list);
46280c71499SPeter Wemm 
46380c71499SPeter Wemm 	/* Sanity check */
46480c71499SPeter Wemm 	if (str_index != strtab_sz) {
46580c71499SPeter Wemm 		errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
46680c71499SPeter Wemm 	}
46780c71499SPeter Wemm 
4687c6da7dcSJohn Polstra 	tmpfile = concat(hints_file, ".XXXXXX", "");
46980c71499SPeter Wemm 	if ((tmpfile = mktemp(tmpfile)) == NULL) {
47080c71499SPeter Wemm 		warn("%s", tmpfile);
47180c71499SPeter Wemm 		return -1;
47280c71499SPeter Wemm 	}
47380c71499SPeter Wemm 
47480c71499SPeter Wemm 	umask(0);	/* Create with exact permissions */
475b9ae52e3SPaul Richards 	if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
4767c6da7dcSJohn Polstra 		warn("%s", hints_file);
477b9ae52e3SPaul Richards 		return -1;
478b9ae52e3SPaul Richards 	}
479b9ae52e3SPaul Richards 
48009e3d49dSJordan K. Hubbard 	if (write(fd, &hdr, sizeof(struct hints_header)) !=
48109e3d49dSJordan K. Hubbard 						sizeof(struct hints_header)) {
4827c6da7dcSJohn Polstra 		warn("%s", hints_file);
48309e3d49dSJordan K. Hubbard 		return -1;
48409e3d49dSJordan K. Hubbard 	}
48509e3d49dSJordan K. Hubbard 	if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
48609e3d49dSJordan K. Hubbard 				hdr.hh_nbucket * sizeof(struct hints_bucket)) {
4877c6da7dcSJohn Polstra 		warn("%s", hints_file);
48809e3d49dSJordan K. Hubbard 		return -1;
48909e3d49dSJordan K. Hubbard 	}
49009e3d49dSJordan K. Hubbard 	if (write(fd, strtab, strtab_sz) != strtab_sz) {
4917c6da7dcSJohn Polstra 		warn("%s", hints_file);
49209e3d49dSJordan K. Hubbard 		return -1;
49309e3d49dSJordan K. Hubbard 	}
494b9ae52e3SPaul Richards 	if (close(fd) != 0) {
4957c6da7dcSJohn Polstra 		warn("%s", hints_file);
496b9ae52e3SPaul Richards 		return -1;
497b9ae52e3SPaul Richards 	}
498b9ae52e3SPaul Richards 
49909e3d49dSJordan K. Hubbard 	/* Install it */
5007c6da7dcSJohn Polstra 	if (unlink(hints_file) != 0 && errno != ENOENT) {
5017c6da7dcSJohn Polstra 		warn("%s", hints_file);
502b9ae52e3SPaul Richards 		return -1;
503b9ae52e3SPaul Richards 	}
504b9ae52e3SPaul Richards 
5057c6da7dcSJohn Polstra 	if (rename(tmpfile, hints_file) != 0) {
5067c6da7dcSJohn Polstra 		warn("%s", hints_file);
507b9ae52e3SPaul Richards 		return -1;
508b9ae52e3SPaul Richards 	}
509b9ae52e3SPaul Richards 
510b9ae52e3SPaul Richards 	return 0;
511b9ae52e3SPaul Richards }
512b9ae52e3SPaul Richards 
513699e1b82SRich Murphey static int
514f606c848SSatoshi Asami readhints()
515b9ae52e3SPaul Richards {
516b9ae52e3SPaul Richards 	int			fd;
517614d19caSJohn Polstra 	void			*addr;
518614d19caSJohn Polstra 	long			fsize;
519b9ae52e3SPaul Richards 	long			msize;
520b9ae52e3SPaul Richards 	struct hints_header	*hdr;
521b9ae52e3SPaul Richards 	struct hints_bucket	*blist;
522b9ae52e3SPaul Richards 	char			*strtab;
523f606c848SSatoshi Asami 	struct shlib_list	*shp;
524b9ae52e3SPaul Richards 	int			i;
525b9ae52e3SPaul Richards 
5267c6da7dcSJohn Polstra 	if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
5277c6da7dcSJohn Polstra 		warn("%s", hints_file);
528b9ae52e3SPaul Richards 		return -1;
529b9ae52e3SPaul Richards 	}
530b9ae52e3SPaul Richards 
53180c71499SPeter Wemm 	msize = PAGE_SIZE;
53261f9ce8dSNate Williams 	addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
533b9ae52e3SPaul Richards 
534614d19caSJohn Polstra 	if (addr == MAP_FAILED) {
5357c6da7dcSJohn Polstra 		warn("%s", hints_file);
536b9ae52e3SPaul Richards 		return -1;
537b9ae52e3SPaul Richards 	}
538b9ae52e3SPaul Richards 
539b9ae52e3SPaul Richards 	hdr = (struct hints_header *)addr;
540b9ae52e3SPaul Richards 	if (HH_BADMAG(*hdr)) {
541ab845347SBruce Evans 		warnx("%s: bad magic: %lo", hints_file,
542ab845347SBruce Evans 			(unsigned long)hdr->hh_magic);
543b9ae52e3SPaul Richards 		return -1;
544b9ae52e3SPaul Richards 	}
545b9ae52e3SPaul Richards 
54680c71499SPeter Wemm 	if (hdr->hh_version != LD_HINTS_VERSION_1 &&
54780c71499SPeter Wemm 	    hdr->hh_version != LD_HINTS_VERSION_2) {
548ab845347SBruce Evans 		warnx("unsupported version: %ld", (long)hdr->hh_version);
549b9ae52e3SPaul Richards 		return -1;
550b9ae52e3SPaul Richards 	}
551b9ae52e3SPaul Richards 
552b9ae52e3SPaul Richards 	if (hdr->hh_ehints > msize) {
553614d19caSJohn Polstra 		fsize = hdr->hh_ehints;
554614d19caSJohn Polstra 		munmap(addr, msize);
555614d19caSJohn Polstra 		addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
556614d19caSJohn Polstra 		if (addr == MAP_FAILED) {
5577c6da7dcSJohn Polstra 			warn("%s", hints_file);
558b9ae52e3SPaul Richards 			return -1;
559b9ae52e3SPaul Richards 		}
560614d19caSJohn Polstra 		hdr = (struct hints_header *)addr;
561b9ae52e3SPaul Richards 	}
562b9ae52e3SPaul Richards 	close(fd);
563b9ae52e3SPaul Richards 
564b9ae52e3SPaul Richards 	blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
565b9ae52e3SPaul Richards 	strtab = (char *)(addr + hdr->hh_strtab);
566b9ae52e3SPaul Richards 
567d4ba5766SPeter Wemm 	if (hdr->hh_version >= LD_HINTS_VERSION_2)
568d4ba5766SPeter Wemm 		add_search_path(strtab + hdr->hh_dirlist);
569d4ba5766SPeter Wemm 	else if (rescan)
570d4ba5766SPeter Wemm 		errx(1, "%s too old and does not contain the search path",
571d4ba5766SPeter Wemm 			hints_file);
572d4ba5766SPeter Wemm 
573d4ba5766SPeter Wemm 	if (rescan)
574d4ba5766SPeter Wemm 		return 0;
575d4ba5766SPeter Wemm 
576b9ae52e3SPaul Richards 	for (i = 0; i < hdr->hh_nbucket; i++) {
577b9ae52e3SPaul Richards 		struct hints_bucket	*bp = &blist[i];
578b9ae52e3SPaul Richards 
579b9ae52e3SPaul Richards 		/* Sanity check */
580b9ae52e3SPaul Richards 		if (bp->hi_namex >= hdr->hh_strtab_sz) {
581b50d7faeSPhilippe Charnier 			warnx("bad name index: %#x", bp->hi_namex);
582b9ae52e3SPaul Richards 			return -1;
583b9ae52e3SPaul Richards 		}
584b9ae52e3SPaul Richards 		if (bp->hi_pathx >= hdr->hh_strtab_sz) {
585b50d7faeSPhilippe Charnier 			warnx("bad path index: %#x", bp->hi_pathx);
586b9ae52e3SPaul Richards 			return -1;
587b9ae52e3SPaul Richards 		}
588b9ae52e3SPaul Richards 
589f606c848SSatoshi Asami 		/* Allocate new list element */
590f606c848SSatoshi Asami 		shp = (struct shlib_list *)xmalloc(sizeof *shp);
591f606c848SSatoshi Asami 		shp->name = strdup(strtab + bp->hi_namex);
592f606c848SSatoshi Asami 		shp->path = strdup(strtab + bp->hi_pathx);
593f606c848SSatoshi Asami 		bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
594f606c848SSatoshi Asami 		shp->ndewey = bp->hi_ndewey;
595f606c848SSatoshi Asami 		shp->next = NULL;
596f606c848SSatoshi Asami 
597f606c848SSatoshi Asami 		*shlib_tail = shp;
598f606c848SSatoshi Asami 		shlib_tail = &shp->next;
599b9ae52e3SPaul Richards 	}
600b9ae52e3SPaul Richards 
601b9ae52e3SPaul Richards 	return 0;
602b9ae52e3SPaul Richards }
603b9ae52e3SPaul Richards 
604f606c848SSatoshi Asami static void
605f606c848SSatoshi Asami listhints()
606f606c848SSatoshi Asami {
607f606c848SSatoshi Asami 	struct shlib_list	*shp;
608f606c848SSatoshi Asami 	int			i;
609f606c848SSatoshi Asami 
6107c6da7dcSJohn Polstra 	printf("%s:\n", hints_file);
61180c71499SPeter Wemm 	printf("\tsearch directories: %s\n", dir_list);
612f606c848SSatoshi Asami 
613f606c848SSatoshi Asami 	for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
614f606c848SSatoshi Asami 		printf("\t%d:-l%s.%d.%d => %s\n",
615f606c848SSatoshi Asami 			i, shp->name, shp->major, shp->minor, shp->path);
616f606c848SSatoshi Asami 
617f606c848SSatoshi Asami 	return;
618f606c848SSatoshi Asami }
619