ldconfig.c revision 1.2 1 1.1 pk /*
2 1.1 pk * Copyright (c) 1993 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.1 pk * derived from this software withough 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.1 pk * $Id: ldconfig.c,v 1.2 1993/12/08 10:16:37 pk Exp $
31 1.1 pk */
32 1.1 pk
33 1.1 pk #include <sys/param.h>
34 1.1 pk #include <stdio.h>
35 1.1 pk #include <stdlib.h>
36 1.1 pk #include <sys/types.h>
37 1.1 pk #include <sys/stat.h>
38 1.1 pk #include <sys/file.h>
39 1.1 pk #include <sys/time.h>
40 1.1 pk #include <sys/mman.h>
41 1.1 pk #include <sys/resource.h>
42 1.1 pk #include <fcntl.h>
43 1.1 pk #include <errno.h>
44 1.1 pk #include <ar.h>
45 1.1 pk #include <ranlib.h>
46 1.1 pk #include <a.out.h>
47 1.1 pk #include <stab.h>
48 1.1 pk #include <string.h>
49 1.1 pk #include <dirent.h>
50 1.1 pk
51 1.1 pk #include "ld.h"
52 1.1 pk
53 1.1 pk #undef major
54 1.1 pk #undef minor
55 1.1 pk
56 1.1 pk char *progname;
57 1.1 pk static int verbose;
58 1.1 pk static int nostd;
59 1.1 pk static int justread;
60 1.1 pk
61 1.1 pk struct shlib_list {
62 1.1 pk /* Internal list of shared libraries found */
63 1.1 pk char *name;
64 1.1 pk char *path;
65 1.1 pk int dewey[MAXDEWEY];
66 1.1 pk int ndewey;
67 1.1 pk #define major dewey[0]
68 1.1 pk #define minor dewey[1]
69 1.1 pk struct shlib_list *next;
70 1.1 pk };
71 1.1 pk
72 1.1 pk static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
73 1.1 pk
74 1.1 pk static void enter __P((char *, char *, char *, int *, int));
75 1.1 pk static int dodir __P((char *));
76 1.1 pk static int build_hints __P((void));
77 1.1 pk
78 1.1 pk int
79 1.1 pk main(argc, argv)
80 1.1 pk int argc;
81 1.1 pk char *argv[];
82 1.1 pk {
83 1.1 pk int i, c;
84 1.1 pk int rval = 0;
85 1.1 pk extern int optind;
86 1.1 pk
87 1.1 pk if ((progname = strrchr(argv[0], '/')) == NULL)
88 1.1 pk progname = argv[0];
89 1.1 pk else
90 1.1 pk progname++;
91 1.1 pk
92 1.1 pk while ((c = getopt(argc, argv, "rsv")) != EOF) {
93 1.1 pk switch (c) {
94 1.1 pk case 'v':
95 1.1 pk verbose = 1;
96 1.1 pk break;
97 1.1 pk case 's':
98 1.1 pk nostd = 1;
99 1.1 pk break;
100 1.1 pk case 'r':
101 1.1 pk justread = 1;
102 1.1 pk break;
103 1.1 pk default:
104 1.1 pk fprintf(stderr, "Usage: %s [-v] [dir ...]\n", progname);
105 1.1 pk exit(1);
106 1.1 pk break;
107 1.1 pk }
108 1.1 pk }
109 1.1 pk
110 1.1 pk if (justread)
111 1.1 pk return listhints();
112 1.1 pk
113 1.1 pk if (!nostd)
114 1.1 pk std_search_dirs(NULL);
115 1.1 pk
116 1.1 pk for (i = 0; i < n_search_dirs; i++)
117 1.1 pk rval |= dodir(search_dirs[i]);
118 1.1 pk
119 1.1 pk for (i = optind; i < argc; i++)
120 1.1 pk rval |= dodir(argv[i]);
121 1.1 pk
122 1.1 pk rval |= build_hints();
123 1.1 pk
124 1.1 pk return rval;
125 1.1 pk }
126 1.1 pk
127 1.1 pk int
128 1.1 pk dodir(dir)
129 1.1 pk char *dir;
130 1.1 pk {
131 1.1 pk DIR *dd;
132 1.1 pk struct dirent *dp;
133 1.1 pk char name[MAXPATHLEN], rest[MAXPATHLEN];
134 1.1 pk int dewey[MAXDEWEY], ndewey;
135 1.1 pk
136 1.1 pk if ((dd = opendir(dir)) == NULL) {
137 1.1 pk perror(dir);
138 1.1 pk return -1;
139 1.1 pk }
140 1.1 pk
141 1.1 pk while ((dp = readdir(dd)) != NULL) {
142 1.1 pk int n;
143 1.1 pk
144 1.1 pk name[0] = rest[0] = '\0';
145 1.1 pk
146 1.1 pk n = sscanf(dp->d_name, "lib%[^.].so.%s",
147 1.1 pk name, rest);
148 1.1 pk
149 1.1 pk if (n < 2 || rest[0] == '\0')
150 1.1 pk continue;
151 1.1 pk
152 1.1 pk ndewey = getdewey(dewey, rest);
153 1.1 pk enter(dir, dp->d_name, name, dewey, ndewey);
154 1.1 pk }
155 1.1 pk
156 1.1 pk return 0;
157 1.1 pk }
158 1.1 pk
159 1.1 pk static void
160 1.1 pk enter(dir, file, name, dewey, ndewey)
161 1.1 pk char *dir, *file, *name;
162 1.1 pk int dewey[], ndewey;
163 1.1 pk {
164 1.1 pk struct shlib_list *shp;
165 1.1 pk
166 1.1 pk for (shp = shlib_head; shp; shp = shp->next) {
167 1.1 pk if (strcmp(name, shp->name) != 0 || major != shp->major)
168 1.1 pk continue;
169 1.1 pk
170 1.1 pk /* Name matches existing entry */
171 1.1 pk if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
172 1.1 pk
173 1.1 pk /* Update this entry with higher versioned lib */
174 1.1 pk if (verbose)
175 1.1 pk printf("Updating lib%s.%d.%d to %s/%s\n",
176 1.1 pk shp->name, shp->major, shp->minor,
177 1.1 pk dir, file);
178 1.1 pk
179 1.1 pk free(shp->name);
180 1.1 pk shp->name = strdup(name);
181 1.1 pk free(shp->path);
182 1.1 pk shp->path = concat(dir, "/", file);
183 1.1 pk bcopy(dewey, shp->dewey, sizeof(shp->dewey));
184 1.1 pk shp->ndewey = ndewey;
185 1.1 pk }
186 1.1 pk break;
187 1.1 pk }
188 1.1 pk
189 1.1 pk if (shp)
190 1.1 pk /* Name exists: older version or just updated */
191 1.1 pk return;
192 1.1 pk
193 1.1 pk /* Allocate new list element */
194 1.1 pk if (verbose)
195 1.1 pk printf("Adding %s/%s\n", dir, file);
196 1.1 pk
197 1.1 pk shp = (struct shlib_list *)xmalloc(sizeof *shp);
198 1.1 pk shp->name = strdup(name);
199 1.1 pk shp->path = concat(dir, "/", file);
200 1.1 pk bcopy(dewey, shp->dewey, MAXDEWEY);
201 1.1 pk shp->ndewey = ndewey;
202 1.1 pk shp->next = NULL;
203 1.1 pk
204 1.1 pk *shlib_tail = shp;
205 1.1 pk shlib_tail = &shp->next;
206 1.1 pk }
207 1.1 pk
208 1.1 pk
209 1.1 pk #if DEBUG
210 1.1 pk /* test */
211 1.1 pk #undef _PATH_LD_HINTS
212 1.1 pk #define _PATH_LD_HINTS "./ld.so.hints"
213 1.1 pk #endif
214 1.1 pk
215 1.1 pk int
216 1.1 pk hinthash(cp, vmajor, vminor)
217 1.1 pk char *cp;
218 1.1 pk int vmajor, vminor;
219 1.1 pk {
220 1.1 pk int k = 0;
221 1.1 pk
222 1.1 pk while (*cp)
223 1.1 pk k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
224 1.1 pk
225 1.1 pk k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
226 1.1 pk k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
227 1.1 pk
228 1.1 pk return k;
229 1.1 pk }
230 1.1 pk
231 1.1 pk int
232 1.1 pk build_hints()
233 1.1 pk {
234 1.1 pk struct hints_header hdr;
235 1.1 pk struct hints_bucket *blist;
236 1.1 pk struct shlib_list *shp;
237 1.1 pk char *strtab;
238 1.1 pk int i, n, str_index = 0;
239 1.1 pk int strtab_sz = 0; /* Total length of strings */
240 1.1 pk int nhints = 0; /* Total number of hints */
241 1.1 pk int fd;
242 1.1 pk char *tmpfile;
243 1.1 pk
244 1.1 pk for (shp = shlib_head; shp; shp = shp->next) {
245 1.1 pk strtab_sz += 1 + strlen(shp->name);
246 1.1 pk strtab_sz += 1 + strlen(shp->path);
247 1.1 pk nhints++;
248 1.1 pk }
249 1.1 pk
250 1.1 pk /* Fill hints file header */
251 1.1 pk hdr.hh_magic = HH_MAGIC;
252 1.1 pk hdr.hh_version = LD_HINTS_VERSION_1;
253 1.1 pk hdr.hh_nbucket = 1 * nhints;
254 1.1 pk n = hdr.hh_nbucket * sizeof(struct hints_bucket);
255 1.1 pk hdr.hh_hashtab = sizeof(struct hints_header);
256 1.1 pk hdr.hh_strtab = hdr.hh_hashtab + n;
257 1.1 pk hdr.hh_strtab_sz = strtab_sz;
258 1.1 pk hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
259 1.1 pk
260 1.1 pk if (verbose)
261 1.1 pk printf("Totals: entries %d, buckets %d, string size %d\n",
262 1.1 pk nhints, hdr.hh_nbucket, strtab_sz);
263 1.1 pk
264 1.1 pk /* Allocate buckets and string table */
265 1.1 pk blist = (struct hints_bucket *)xmalloc(n);
266 1.1 pk bzero((char *)blist, n);
267 1.1 pk for (i = 0; i < hdr.hh_nbucket; i++)
268 1.1 pk /* Empty all buckets */
269 1.1 pk blist[i].hi_next = -1;
270 1.1 pk
271 1.1 pk strtab = (char *)xmalloc(strtab_sz);
272 1.1 pk
273 1.1 pk /* Enter all */
274 1.1 pk for (shp = shlib_head; shp; shp = shp->next) {
275 1.1 pk struct hints_bucket *bp;
276 1.1 pk
277 1.1 pk bp = blist +
278 1.1 pk (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
279 1.1 pk
280 1.1 pk if (bp->hi_pathx) {
281 1.1 pk int i;
282 1.1 pk
283 1.1 pk for (i = 0; i < hdr.hh_nbucket; i++) {
284 1.1 pk if (blist[i].hi_pathx == 0)
285 1.1 pk break;
286 1.1 pk }
287 1.1 pk if (i == hdr.hh_nbucket) {
288 1.1 pk fprintf(stderr, "Bummer!\n");
289 1.1 pk return -1;
290 1.1 pk }
291 1.1 pk while (bp->hi_next != -1)
292 1.1 pk bp = &blist[bp->hi_next];
293 1.1 pk bp->hi_next = i;
294 1.1 pk bp = blist + i;
295 1.1 pk }
296 1.1 pk
297 1.1 pk /* Insert strings in string table */
298 1.1 pk bp->hi_namex = str_index;
299 1.1 pk strcpy(strtab + str_index, shp->name);
300 1.1 pk str_index += 1 + strlen(shp->name);
301 1.1 pk
302 1.1 pk bp->hi_pathx = str_index;
303 1.1 pk strcpy(strtab + str_index, shp->path);
304 1.1 pk str_index += 1 + strlen(shp->path);
305 1.1 pk
306 1.1 pk /* Copy versions */
307 1.1 pk bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
308 1.1 pk bp->hi_ndewey = shp->ndewey;
309 1.1 pk }
310 1.1 pk
311 1.1 pk tmpfile = concat(_PATH_LD_HINTS, "+", "");
312 1.1 pk if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
313 1.1 pk perror(_PATH_LD_HINTS);
314 1.1 pk return -1;
315 1.1 pk }
316 1.1 pk
317 1.1 pk mywrite(&hdr, 1, sizeof(struct hints_header), fd);
318 1.1 pk mywrite(blist, hdr.hh_nbucket, sizeof(struct hints_bucket), fd);
319 1.1 pk mywrite(strtab, strtab_sz, 1, fd);
320 1.1 pk
321 1.1 pk if (close(fd) != 0) {
322 1.1 pk perror(_PATH_LD_HINTS);
323 1.1 pk return -1;
324 1.1 pk }
325 1.1 pk
326 1.1 pk /* Now, install real file */
327 1.1 pk if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
328 1.1 pk perror(_PATH_LD_HINTS);
329 1.1 pk return -1;
330 1.1 pk }
331 1.1 pk
332 1.1 pk if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
333 1.1 pk perror(_PATH_LD_HINTS);
334 1.1 pk return -1;
335 1.1 pk }
336 1.1 pk
337 1.1 pk return 0;
338 1.1 pk }
339 1.1 pk
340 1.1 pk int
341 1.1 pk listhints()
342 1.1 pk {
343 1.1 pk int fd;
344 1.1 pk caddr_t addr;
345 1.1 pk long msize;
346 1.1 pk struct hints_header *hdr;
347 1.1 pk struct hints_bucket *blist;
348 1.1 pk char *strtab;
349 1.1 pk int i;
350 1.1 pk
351 1.1 pk if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
352 1.1 pk perror(_PATH_LD_HINTS);
353 1.1 pk return -1;
354 1.1 pk }
355 1.1 pk
356 1.1 pk msize = PAGSIZ;
357 1.1 pk addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
358 1.1 pk
359 1.1 pk if (addr == (caddr_t)-1) {
360 1.1 pk perror(_PATH_LD_HINTS);
361 1.1 pk return -1;
362 1.1 pk }
363 1.1 pk
364 1.1 pk hdr = (struct hints_header *)addr;
365 1.1 pk if (HH_BADMAG(*hdr)) {
366 1.1 pk fprintf(stderr, "%s: Bad magic: %d\n");
367 1.1 pk return -1;
368 1.1 pk }
369 1.1 pk
370 1.1 pk if (hdr->hh_version != LD_HINTS_VERSION_1) {
371 1.1 pk fprintf(stderr, "Unsupported version: %d\n", hdr->hh_version);
372 1.1 pk return -1;
373 1.1 pk }
374 1.1 pk
375 1.1 pk if (hdr->hh_ehints > msize) {
376 1.1 pk if (mmap(addr+msize, hdr->hh_ehints - msize,
377 1.1 pk PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
378 1.1 pk fd, msize) != (caddr_t)(addr+msize)) {
379 1.1 pk
380 1.1 pk perror(_PATH_LD_HINTS);
381 1.1 pk return -1;
382 1.1 pk }
383 1.1 pk }
384 1.1 pk close(fd);
385 1.1 pk
386 1.1 pk blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
387 1.1 pk strtab = (char *)(addr + hdr->hh_strtab);
388 1.1 pk
389 1.1 pk printf("%s:\n", _PATH_LD_HINTS);
390 1.1 pk for (i = 0; i < hdr->hh_nbucket; i++) {
391 1.1 pk struct hints_bucket *bp = &blist[i];
392 1.1 pk
393 1.1 pk /* Sanity check */
394 1.1 pk if (bp->hi_namex >= hdr->hh_strtab_sz) {
395 1.1 pk fprintf(stderr, "Bad name index: %#x\n", bp->hi_namex);
396 1.1 pk return -1;
397 1.1 pk }
398 1.1 pk if (bp->hi_pathx >= hdr->hh_strtab_sz) {
399 1.1 pk fprintf(stderr, "Bad path index: %#x\n", bp->hi_pathx);
400 1.1 pk return -1;
401 1.1 pk }
402 1.1 pk
403 1.2 pk printf("\t%d:-l%s.%d.%d => %s (%d -> %d)\n",
404 1.2 pk i,
405 1.1 pk strtab + bp->hi_namex, bp->hi_major, bp->hi_minor,
406 1.2 pk strtab + bp->hi_pathx,
407 1.2 pk hinthash(strtab+bp->hi_namex, bp->hi_major, bp->hi_minor)
408 1.2 pk % hdr->hh_nbucket,
409 1.2 pk bp->hi_next);
410 1.1 pk }
411 1.1 pk
412 1.1 pk return 0;
413 1.1 pk }
414 1.1 pk
415