ldconfig.c revision 1.9 1 1.1 pk /*
2 1.9 pk * Copyright (c) 1993,1995 Paul Kranenburg
3 1.1 pk * All rights reserved.
4 1.1 pk *
5 1.1 pk * Redistribution and use in source and binary forms, with or without
6 1.1 pk * modification, are permitted provided that the following conditions
7 1.1 pk * are met:
8 1.1 pk * 1. Redistributions of source code must retain the above copyright
9 1.1 pk * notice, this list of conditions and the following disclaimer.
10 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 pk * notice, this list of conditions and the following disclaimer in the
12 1.1 pk * documentation and/or other materials provided with the distribution.
13 1.1 pk * 3. All advertising materials mentioning features or use of this software
14 1.1 pk * must display the following acknowledgement:
15 1.1 pk * This product includes software developed by Paul Kranenburg.
16 1.1 pk * 4. The name of the author may not be used to endorse or promote products
17 1.5 jtc * derived from this software without specific prior written permission
18 1.1 pk *
19 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 pk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 pk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 pk * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 pk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 pk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 pk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 pk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 pk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 pk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 pk *
30 1.9 pk * $Id: ldconfig.c,v 1.9 1995/06/20 23:12:35 pk Exp $
31 1.1 pk */
32 1.1 pk
33 1.1 pk #include <sys/param.h>
34 1.1 pk #include <sys/types.h>
35 1.1 pk #include <sys/stat.h>
36 1.1 pk #include <sys/file.h>
37 1.1 pk #include <sys/time.h>
38 1.1 pk #include <sys/mman.h>
39 1.1 pk #include <sys/resource.h>
40 1.7 pk #include <dirent.h>
41 1.7 pk #include <errno.h>
42 1.1 pk #include <fcntl.h>
43 1.1 pk #include <ar.h>
44 1.1 pk #include <ranlib.h>
45 1.1 pk #include <a.out.h>
46 1.1 pk #include <stab.h>
47 1.7 pk #include <stdio.h>
48 1.7 pk #include <stdlib.h>
49 1.1 pk #include <string.h>
50 1.7 pk #include <unistd.h>
51 1.1 pk
52 1.1 pk #include "ld.h"
53 1.1 pk
54 1.1 pk #undef major
55 1.1 pk #undef minor
56 1.1 pk
57 1.8 pk extern char *__progname;
58 1.8 pk
59 1.1 pk static int verbose;
60 1.1 pk static int nostd;
61 1.1 pk static int justread;
62 1.9 pk static int merge;
63 1.1 pk
64 1.1 pk struct shlib_list {
65 1.1 pk /* Internal list of shared libraries found */
66 1.1 pk char *name;
67 1.1 pk char *path;
68 1.1 pk int dewey[MAXDEWEY];
69 1.1 pk int ndewey;
70 1.1 pk #define major dewey[0]
71 1.1 pk #define minor dewey[1]
72 1.1 pk struct shlib_list *next;
73 1.1 pk };
74 1.1 pk
75 1.1 pk static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
76 1.1 pk
77 1.1 pk static void enter __P((char *, char *, char *, int *, int));
78 1.3 pk static int dodir __P((char *, int));
79 1.9 pk static int buildhints __P((void));
80 1.9 pk static int readhints __P((void));
81 1.9 pk static void listhints __P((void));
82 1.1 pk
83 1.1 pk int
84 1.1 pk main(argc, argv)
85 1.1 pk int argc;
86 1.1 pk char *argv[];
87 1.1 pk {
88 1.1 pk int i, c;
89 1.1 pk int rval = 0;
90 1.1 pk
91 1.9 pk while ((c = getopt(argc, argv, "mrsv")) != EOF) {
92 1.1 pk switch (c) {
93 1.9 pk case 'm':
94 1.9 pk merge = 1;
95 1.9 pk break;
96 1.9 pk case 'r':
97 1.9 pk justread = 1;
98 1.1 pk break;
99 1.1 pk case 's':
100 1.1 pk nostd = 1;
101 1.1 pk break;
102 1.9 pk case 'v':
103 1.9 pk verbose = 1;
104 1.1 pk break;
105 1.1 pk default:
106 1.9 pk errx(1, "Usage: %s [-r][-s][-v][dir ...]",
107 1.8 pk __progname);
108 1.1 pk break;
109 1.1 pk }
110 1.1 pk }
111 1.1 pk
112 1.9 pk if (justread || merge) {
113 1.9 pk if ((rval = readhints()) != 0)
114 1.9 pk return rval;
115 1.9 pk if (justread) {
116 1.9 pk listhints();
117 1.9 pk return;
118 1.9 pk }
119 1.9 pk }
120 1.1 pk
121 1.9 pk if (!nostd && !merge)
122 1.7 pk std_search_path();
123 1.1 pk
124 1.1 pk for (i = 0; i < n_search_dirs; i++)
125 1.3 pk rval |= dodir(search_dirs[i], 1);
126 1.1 pk
127 1.1 pk for (i = optind; i < argc; i++)
128 1.3 pk rval |= dodir(argv[i], 0);
129 1.1 pk
130 1.9 pk rval |= buildhints();
131 1.1 pk
132 1.1 pk return rval;
133 1.1 pk }
134 1.1 pk
135 1.1 pk int
136 1.3 pk dodir(dir, silent)
137 1.1 pk char *dir;
138 1.3 pk int silent;
139 1.1 pk {
140 1.1 pk DIR *dd;
141 1.1 pk struct dirent *dp;
142 1.1 pk char name[MAXPATHLEN], rest[MAXPATHLEN];
143 1.1 pk int dewey[MAXDEWEY], ndewey;
144 1.1 pk
145 1.1 pk if ((dd = opendir(dir)) == NULL) {
146 1.3 pk if (!silent || errno != ENOENT)
147 1.9 pk warn("%s", dir);
148 1.1 pk return -1;
149 1.1 pk }
150 1.1 pk
151 1.1 pk while ((dp = readdir(dd)) != NULL) {
152 1.1 pk int n;
153 1.1 pk
154 1.1 pk name[0] = rest[0] = '\0';
155 1.1 pk
156 1.1 pk n = sscanf(dp->d_name, "lib%[^.].so.%s",
157 1.1 pk name, rest);
158 1.1 pk
159 1.1 pk if (n < 2 || rest[0] == '\0')
160 1.1 pk continue;
161 1.1 pk
162 1.1 pk ndewey = getdewey(dewey, rest);
163 1.1 pk enter(dir, dp->d_name, name, dewey, ndewey);
164 1.1 pk }
165 1.1 pk
166 1.1 pk return 0;
167 1.1 pk }
168 1.1 pk
169 1.1 pk static void
170 1.1 pk enter(dir, file, name, dewey, ndewey)
171 1.1 pk char *dir, *file, *name;
172 1.1 pk int dewey[], ndewey;
173 1.1 pk {
174 1.1 pk struct shlib_list *shp;
175 1.1 pk
176 1.1 pk for (shp = shlib_head; shp; shp = shp->next) {
177 1.1 pk if (strcmp(name, shp->name) != 0 || major != shp->major)
178 1.1 pk continue;
179 1.1 pk
180 1.1 pk /* Name matches existing entry */
181 1.1 pk if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
182 1.1 pk
183 1.1 pk /* Update this entry with higher versioned lib */
184 1.1 pk if (verbose)
185 1.1 pk printf("Updating lib%s.%d.%d to %s/%s\n",
186 1.1 pk shp->name, shp->major, shp->minor,
187 1.1 pk dir, file);
188 1.1 pk
189 1.1 pk free(shp->name);
190 1.1 pk shp->name = strdup(name);
191 1.1 pk free(shp->path);
192 1.1 pk shp->path = concat(dir, "/", file);
193 1.1 pk bcopy(dewey, shp->dewey, sizeof(shp->dewey));
194 1.1 pk shp->ndewey = ndewey;
195 1.1 pk }
196 1.1 pk break;
197 1.1 pk }
198 1.1 pk
199 1.1 pk if (shp)
200 1.1 pk /* Name exists: older version or just updated */
201 1.1 pk return;
202 1.1 pk
203 1.1 pk /* Allocate new list element */
204 1.1 pk if (verbose)
205 1.1 pk printf("Adding %s/%s\n", dir, file);
206 1.1 pk
207 1.1 pk shp = (struct shlib_list *)xmalloc(sizeof *shp);
208 1.1 pk shp->name = strdup(name);
209 1.1 pk shp->path = concat(dir, "/", file);
210 1.1 pk bcopy(dewey, shp->dewey, MAXDEWEY);
211 1.1 pk shp->ndewey = ndewey;
212 1.1 pk shp->next = NULL;
213 1.1 pk
214 1.1 pk *shlib_tail = shp;
215 1.1 pk shlib_tail = &shp->next;
216 1.1 pk }
217 1.1 pk
218 1.1 pk
219 1.1 pk #if DEBUG
220 1.1 pk /* test */
221 1.1 pk #undef _PATH_LD_HINTS
222 1.1 pk #define _PATH_LD_HINTS "./ld.so.hints"
223 1.1 pk #endif
224 1.1 pk
225 1.1 pk int
226 1.1 pk hinthash(cp, vmajor, vminor)
227 1.1 pk char *cp;
228 1.1 pk int vmajor, vminor;
229 1.1 pk {
230 1.1 pk int k = 0;
231 1.1 pk
232 1.1 pk while (*cp)
233 1.1 pk k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
234 1.1 pk
235 1.1 pk k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
236 1.1 pk k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
237 1.1 pk
238 1.1 pk return k;
239 1.1 pk }
240 1.1 pk
241 1.1 pk int
242 1.9 pk buildhints()
243 1.1 pk {
244 1.1 pk struct hints_header hdr;
245 1.1 pk struct hints_bucket *blist;
246 1.1 pk struct shlib_list *shp;
247 1.1 pk char *strtab;
248 1.1 pk int i, n, str_index = 0;
249 1.1 pk int strtab_sz = 0; /* Total length of strings */
250 1.1 pk int nhints = 0; /* Total number of hints */
251 1.1 pk int fd;
252 1.1 pk char *tmpfile;
253 1.1 pk
254 1.1 pk for (shp = shlib_head; shp; shp = shp->next) {
255 1.1 pk strtab_sz += 1 + strlen(shp->name);
256 1.1 pk strtab_sz += 1 + strlen(shp->path);
257 1.1 pk nhints++;
258 1.1 pk }
259 1.1 pk
260 1.1 pk /* Fill hints file header */
261 1.1 pk hdr.hh_magic = HH_MAGIC;
262 1.1 pk hdr.hh_version = LD_HINTS_VERSION_1;
263 1.1 pk hdr.hh_nbucket = 1 * nhints;
264 1.1 pk n = hdr.hh_nbucket * sizeof(struct hints_bucket);
265 1.1 pk hdr.hh_hashtab = sizeof(struct hints_header);
266 1.1 pk hdr.hh_strtab = hdr.hh_hashtab + n;
267 1.1 pk hdr.hh_strtab_sz = strtab_sz;
268 1.1 pk hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
269 1.1 pk
270 1.1 pk if (verbose)
271 1.1 pk printf("Totals: entries %d, buckets %d, string size %d\n",
272 1.1 pk nhints, hdr.hh_nbucket, strtab_sz);
273 1.1 pk
274 1.1 pk /* Allocate buckets and string table */
275 1.1 pk blist = (struct hints_bucket *)xmalloc(n);
276 1.1 pk bzero((char *)blist, n);
277 1.1 pk for (i = 0; i < hdr.hh_nbucket; i++)
278 1.1 pk /* Empty all buckets */
279 1.1 pk blist[i].hi_next = -1;
280 1.1 pk
281 1.1 pk strtab = (char *)xmalloc(strtab_sz);
282 1.1 pk
283 1.1 pk /* Enter all */
284 1.1 pk for (shp = shlib_head; shp; shp = shp->next) {
285 1.1 pk struct hints_bucket *bp;
286 1.1 pk
287 1.1 pk bp = blist +
288 1.1 pk (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
289 1.1 pk
290 1.1 pk if (bp->hi_pathx) {
291 1.1 pk int i;
292 1.1 pk
293 1.1 pk for (i = 0; i < hdr.hh_nbucket; i++) {
294 1.1 pk if (blist[i].hi_pathx == 0)
295 1.1 pk break;
296 1.1 pk }
297 1.1 pk if (i == hdr.hh_nbucket) {
298 1.9 pk warnx("Bummer!");
299 1.1 pk return -1;
300 1.1 pk }
301 1.1 pk while (bp->hi_next != -1)
302 1.1 pk bp = &blist[bp->hi_next];
303 1.1 pk bp->hi_next = i;
304 1.1 pk bp = blist + i;
305 1.1 pk }
306 1.1 pk
307 1.1 pk /* Insert strings in string table */
308 1.1 pk bp->hi_namex = str_index;
309 1.1 pk strcpy(strtab + str_index, shp->name);
310 1.1 pk str_index += 1 + strlen(shp->name);
311 1.1 pk
312 1.1 pk bp->hi_pathx = str_index;
313 1.1 pk strcpy(strtab + str_index, shp->path);
314 1.1 pk str_index += 1 + strlen(shp->path);
315 1.1 pk
316 1.1 pk /* Copy versions */
317 1.1 pk bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
318 1.1 pk bp->hi_ndewey = shp->ndewey;
319 1.1 pk }
320 1.1 pk
321 1.1 pk tmpfile = concat(_PATH_LD_HINTS, "+", "");
322 1.1 pk if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
323 1.9 pk warn("%s", _PATH_LD_HINTS);
324 1.1 pk return -1;
325 1.1 pk }
326 1.1 pk
327 1.4 pk if (write(fd, &hdr, sizeof(struct hints_header)) !=
328 1.4 pk sizeof(struct hints_header)) {
329 1.9 pk warn("%s", _PATH_LD_HINTS);
330 1.4 pk return -1;
331 1.4 pk }
332 1.4 pk if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
333 1.4 pk hdr.hh_nbucket * sizeof(struct hints_bucket)) {
334 1.9 pk warn("%s", _PATH_LD_HINTS);
335 1.4 pk return -1;
336 1.4 pk }
337 1.4 pk if (write(fd, strtab, strtab_sz) != strtab_sz) {
338 1.9 pk warn("%s", _PATH_LD_HINTS);
339 1.4 pk return -1;
340 1.4 pk }
341 1.1 pk if (close(fd) != 0) {
342 1.9 pk warn("%s", _PATH_LD_HINTS);
343 1.1 pk return -1;
344 1.1 pk }
345 1.1 pk
346 1.4 pk /* Install it */
347 1.1 pk if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
348 1.9 pk warn("%s", _PATH_LD_HINTS);
349 1.1 pk return -1;
350 1.1 pk }
351 1.1 pk
352 1.1 pk if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
353 1.9 pk warn("%s", _PATH_LD_HINTS);
354 1.1 pk return -1;
355 1.1 pk }
356 1.1 pk
357 1.1 pk return 0;
358 1.1 pk }
359 1.1 pk
360 1.7 pk static int
361 1.9 pk readhints()
362 1.1 pk {
363 1.1 pk int fd;
364 1.1 pk caddr_t addr;
365 1.1 pk long msize;
366 1.1 pk struct hints_header *hdr;
367 1.1 pk struct hints_bucket *blist;
368 1.1 pk char *strtab;
369 1.9 pk struct shlib_list *shp;
370 1.1 pk int i;
371 1.1 pk
372 1.1 pk if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
373 1.9 pk warn("%s", _PATH_LD_HINTS);
374 1.1 pk return -1;
375 1.1 pk }
376 1.1 pk
377 1.1 pk msize = PAGSIZ;
378 1.6 cgd addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
379 1.1 pk
380 1.1 pk if (addr == (caddr_t)-1) {
381 1.9 pk warn("%s", _PATH_LD_HINTS);
382 1.1 pk return -1;
383 1.1 pk }
384 1.1 pk
385 1.1 pk hdr = (struct hints_header *)addr;
386 1.1 pk if (HH_BADMAG(*hdr)) {
387 1.9 pk warnx("%s: Bad magic: %o",
388 1.7 pk _PATH_LD_HINTS, hdr->hh_magic);
389 1.1 pk return -1;
390 1.1 pk }
391 1.1 pk
392 1.1 pk if (hdr->hh_version != LD_HINTS_VERSION_1) {
393 1.9 pk warnx("Unsupported version: %d", hdr->hh_version);
394 1.1 pk return -1;
395 1.1 pk }
396 1.1 pk
397 1.1 pk if (hdr->hh_ehints > msize) {
398 1.1 pk if (mmap(addr+msize, hdr->hh_ehints - msize,
399 1.6 cgd PROT_READ, MAP_COPY|MAP_FIXED,
400 1.1 pk fd, msize) != (caddr_t)(addr+msize)) {
401 1.1 pk
402 1.9 pk warn("%s", _PATH_LD_HINTS);
403 1.1 pk return -1;
404 1.1 pk }
405 1.1 pk }
406 1.1 pk close(fd);
407 1.1 pk
408 1.1 pk blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
409 1.1 pk strtab = (char *)(addr + hdr->hh_strtab);
410 1.1 pk
411 1.1 pk for (i = 0; i < hdr->hh_nbucket; i++) {
412 1.1 pk struct hints_bucket *bp = &blist[i];
413 1.1 pk
414 1.1 pk /* Sanity check */
415 1.1 pk if (bp->hi_namex >= hdr->hh_strtab_sz) {
416 1.9 pk warnx("Bad name index: %#x", bp->hi_namex);
417 1.1 pk return -1;
418 1.1 pk }
419 1.1 pk if (bp->hi_pathx >= hdr->hh_strtab_sz) {
420 1.9 pk warnx("Bad path index: %#x", bp->hi_pathx);
421 1.1 pk return -1;
422 1.1 pk }
423 1.1 pk
424 1.9 pk /* Allocate new list element */
425 1.9 pk shp = (struct shlib_list *)xmalloc(sizeof *shp);
426 1.9 pk shp->name = strdup(strtab + bp->hi_namex);
427 1.9 pk shp->path = strdup(strtab + bp->hi_pathx);
428 1.9 pk bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
429 1.9 pk shp->ndewey = bp->hi_ndewey;
430 1.9 pk shp->next = NULL;
431 1.9 pk
432 1.9 pk *shlib_tail = shp;
433 1.9 pk shlib_tail = &shp->next;
434 1.1 pk }
435 1.1 pk
436 1.1 pk return 0;
437 1.1 pk }
438 1.1 pk
439 1.9 pk static void
440 1.9 pk listhints()
441 1.9 pk {
442 1.9 pk struct shlib_list *shp;
443 1.9 pk int i;
444 1.9 pk
445 1.9 pk printf("%s:\n", _PATH_LD_HINTS);
446 1.9 pk
447 1.9 pk for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
448 1.9 pk printf("\t%d:-l%s.%d.%d => %s\n",
449 1.9 pk i, shp->name, shp->major, shp->minor, shp->path);
450 1.9 pk
451 1.9 pk return;
452 1.9 pk }
453