]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/kldxref/kldxref.c
MFC r322124:
[FreeBSD/stable/10.git] / usr.sbin / kldxref / kldxref.c
1 /*
2  * Copyright (c) 2000, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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 Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/exec.h>
38 #include <sys/queue.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
41 #include <sys/linker.h>
42 #include <sys/stat.h>
43 #include <sys/module.h>
44 #define FREEBSD_ELF
45 #include <err.h>
46 #include <fts.h>
47 #include <string.h>
48 #include <machine/elf.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <errno.h>
53
54 #include "ef.h"
55
56 #define MAXRECSIZE      8192
57 #define check(val)      if ((error = (val)) != 0) break
58
59 static int dflag;       /* do not create a hint file, only write on stdout */
60 static int verbose;
61
62 static FILE *fxref;     /* current hints file */
63
64 static const char *xref_file = "linker.hints";
65
66 /*
67  * A record is stored in the static buffer recbuf before going to disk.
68  */
69 static char recbuf[MAXRECSIZE];
70 static int recpos;      /* current write position */
71 static int reccnt;      /* total record written to this file so far */
72
73 static void
74 intalign(void)
75 {
76         recpos = (recpos + sizeof(int) - 1) & ~(sizeof(int) - 1);
77 }
78
79 static void
80 record_start(void)
81 {
82         recpos = 0;
83         memset(recbuf, 0, MAXRECSIZE);
84 }
85
86 static int
87 record_end(void)
88 {
89         if (recpos == 0)
90                 return 0;
91         reccnt++;
92         intalign();
93         fwrite(&recpos, sizeof(recpos), 1, fxref);
94         return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0;
95 }
96
97 static int
98 record_buf(const void *buf, int size)
99 {
100         if (MAXRECSIZE - recpos < size)
101                 errx(1, "record buffer overflow");
102         memcpy(recbuf + recpos, buf, size);
103         recpos += size;
104         return 0;
105 }
106
107 /*
108  * An int is stored in host order and aligned
109  */
110 static int
111 record_int(int val)
112 {
113         intalign();
114         return record_buf(&val, sizeof(val));
115 }
116
117 /*
118  * A string is stored as 1-byte length plus data, no padding
119  */
120 static int
121 record_string(const char *str)
122 {
123         int len, error;
124         u_char val;
125         
126         if (dflag)
127                 return 0;
128         val = len = strlen(str);
129         if (len > 255)
130                 errx(1, "string %s too long", str);
131         error = record_buf(&val, sizeof(val));
132         if (error)
133                 return error;
134         return record_buf(str, len);
135 }
136
137 static int
138 parse_entry(struct mod_metadata *md, const char *cval,
139     struct elf_file *ef, const char *kldname)
140 {
141         struct mod_depend mdp;
142         struct mod_version mdv;
143         Elf_Off data = (Elf_Off)md->md_data;
144         int error = 0;
145
146         record_start();
147         switch (md->md_type) {
148         case MDT_DEPEND:
149                 if (!dflag)
150                         break;
151                 check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp));
152                 printf("  depends on %s.%d (%d,%d)\n", cval,
153                     mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum);
154                 break;
155         case MDT_VERSION:
156                 check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv));
157                 if (dflag) {
158                         printf("  interface %s.%d\n", cval, mdv.mv_version);
159                 } else {
160                         record_int(MDT_VERSION);
161                         record_string(cval);
162                         record_int(mdv.mv_version);
163                         record_string(kldname);
164                 }
165                 break;
166         case MDT_MODULE:
167                 if (dflag) {
168                         printf("  module %s\n", cval);
169                 } else {
170                         record_int(MDT_MODULE);
171                         record_string(cval);
172                         record_string(kldname);
173                 }
174                 break;
175         case MDT_PNP_INFO:
176                 if (dflag) {
177                         printf("  pnp info for bus %s\n", cval);
178                 }
179         default:
180                 warnx("unknown metadata record %d in file %s", md->md_type, kldname);
181         }
182         if (!error)
183                 record_end();
184         return error;
185 }
186
187 static int
188 read_kld(char *filename, char *kldname)
189 {
190         struct mod_metadata md;
191         struct elf_file ef;
192         void **p, **orgp;
193         int error, eftype, nmlen;
194         long start, finish, entries;
195         char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp;
196
197         if (verbose || dflag)
198                 printf("%s\n", filename);
199         error = ef_open(filename, &ef, verbose);
200         if (error) {
201                 error = ef_obj_open(filename, &ef, verbose);
202                 if (error) {
203                         if (verbose)
204                                 warnc(error, "elf_open(%s)", filename);
205                         return error;
206                 }
207         }
208         eftype = EF_GET_TYPE(&ef);
209         if (eftype != EFT_KLD && eftype != EFT_KERNEL)  {
210                 EF_CLOSE(&ef);
211                 return 0;
212         }
213         if (!dflag) {
214                 cp = strrchr(kldname, '.');
215                 nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname);
216                 if (nmlen > MAXMODNAME)
217                         nmlen = MAXMODNAME;
218                 strlcpy(kldmodname, kldname, nmlen);
219 /*              fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
220         }
221         do {
222                 check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish,
223                     &entries));
224                 check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries,
225                     (void *)&p));
226                 orgp = p;
227                 while(entries--) {
228                         check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md),
229                             &md));
230                         p++;
231                         check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval,
232                             sizeof(cval), cval));
233                         cval[MAXMODNAME] = '\0';
234                         parse_entry(&md, cval, &ef, kldname);
235                 }
236                 if (error)
237                         warnc(error, "error while reading %s", filename);
238                 free(orgp);
239         } while(0);
240         EF_CLOSE(&ef);
241         return error;
242 }
243
244 /*
245  * Create a temp file in directory root, make sure we don't
246  * overflow the buffer for the destination name
247  */
248 static FILE *
249 maketempfile(char *dest, const char *root)
250 {
251         char *p;
252         int n, fd;
253
254         p = strrchr(root, '/');
255         n = p != NULL ? p - root + 1 : 0;
256         if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >=
257             MAXPATHLEN) {
258                 errno = ENAMETOOLONG;
259                 return NULL;
260         }
261
262         fd = mkstemp(dest);
263         if (fd < 0)
264                 return NULL;
265         fchmod(fd, 0644);       /* nothing secret in the file */
266         return fdopen(fd, "w+");
267 }
268
269 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
270
271 static void
272 usage(void)
273 {
274
275         fprintf(stderr, "%s\n",
276             "usage: kldxref [-Rdv] [-f hintsfile] path ..."
277         );
278         exit(1);
279 }
280
281 static int
282 compare(const FTSENT *const *a, const FTSENT *const *b)
283 {
284         if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D)
285                 return 1;
286         if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D)
287                 return -1;
288         return strcmp((*a)->fts_name, (*b)->fts_name);
289 }
290
291 int
292 main(int argc, char *argv[])
293 {
294         FTS *ftsp;
295         FTSENT *p;
296         int opt, fts_options, ival;
297         struct stat sb;
298
299         fts_options = FTS_PHYSICAL;
300
301         while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
302                 switch (opt) {
303                 case 'd':       /* no hint file, only print on stdout */
304                         dflag = 1;
305                         break;
306                 case 'f':       /* use this name instead of linker.hints */
307                         xref_file = optarg;
308                         break;
309                 case 'v':
310                         verbose++;
311                         break;
312                 case 'R':       /* recurse on directories */
313                         fts_options |= FTS_COMFOLLOW;
314                         break;
315                 default:
316                         usage();
317                         /* NOTREACHED */
318                 }
319         }
320         if (argc - optind < 1)
321                 usage();
322         argc -= optind;
323         argv += optind;
324
325         if (stat(argv[0], &sb) != 0)
326                 err(1, "%s", argv[0]);
327         if ((sb.st_mode & S_IFDIR) == 0) {
328                 errno = ENOTDIR;
329                 err(1, "%s", argv[0]);
330         }
331
332         ftsp = fts_open(argv, fts_options, compare);
333         if (ftsp == NULL)
334                 exit(1);
335
336         for (;;) {
337                 p = fts_read(ftsp);
338                 if ((p == NULL || p->fts_info == FTS_D) && fxref) {
339                         /* close and rename the current hint file */
340                         fclose(fxref);
341                         fxref = NULL;
342                         if (reccnt) {
343                                 rename(tempname, xrefname);
344                         } else {
345                                 /* didn't find any entry, ignore this file */
346                                 unlink(tempname);
347                                 unlink(xrefname);
348                         }
349                 }
350                 if (p == NULL)
351                         break;
352                 if (p->fts_info == FTS_D && !dflag) {
353                         /* visiting a new directory, create a new hint file */
354                         snprintf(xrefname, sizeof(xrefname), "%s/%s",
355                             ftsp->fts_path, xref_file);
356                         fxref = maketempfile(tempname, ftsp->fts_path);
357                         if (fxref == NULL)
358                                 err(1, "can't create %s", tempname);
359                         ival = 1;
360                         fwrite(&ival, sizeof(ival), 1, fxref);
361                         reccnt = 0;
362                 }
363                 /* skip non-files and separate debug files */
364                 if (p->fts_info != FTS_F)
365                         continue;
366                 if (p->fts_namelen >= 6 &&
367                     strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0)
368                         continue;
369                 if (p->fts_namelen >= 8 &&
370                     strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0)
371                         continue;
372                 read_kld(p->fts_path, p->fts_name);
373         }
374         fts_close(ftsp);
375         return 0;
376 }