2 * Copyright (c) 2000, Boris Popov
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 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.
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
35 #include <sys/types.h>
36 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
41 #include <sys/linker.h>
43 #include <sys/module.h>
48 #include <machine/elf.h>
56 #define MAXRECSIZE 8192
57 #define check(val) if ((error = (val)) != 0) break
59 static int dflag; /* do not create a hint file, only write on stdout */
62 static FILE *fxref; /* current hints file */
64 static const char *xref_file = "linker.hints";
67 * A record is stored in the static buffer recbuf before going to disk.
69 static char recbuf[MAXRECSIZE];
70 static int recpos; /* current write position */
71 static int reccnt; /* total record written to this file so far */
76 recpos = (recpos + sizeof(int) - 1) & ~(sizeof(int) - 1);
83 memset(recbuf, 0, MAXRECSIZE);
93 fwrite(&recpos, sizeof(recpos), 1, fxref);
94 return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0;
98 record_buf(const void *buf, int size)
100 if (MAXRECSIZE - recpos < size)
101 errx(1, "record buffer overflow");
102 memcpy(recbuf + recpos, buf, size);
108 * An int is stored in host order and aligned
114 return record_buf(&val, sizeof(val));
118 * A string is stored as 1-byte length plus data, no padding
121 record_string(const char *str)
128 val = len = strlen(str);
130 errx(1, "string %s too long", str);
131 error = record_buf(&val, sizeof(val));
134 return record_buf(str, len);
138 parse_entry(struct mod_metadata *md, const char *cval,
139 struct elf_file *ef, const char *kldname)
141 struct mod_depend mdp;
142 struct mod_version mdv;
143 Elf_Off data = (Elf_Off)md->md_data;
147 switch (md->md_type) {
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);
156 check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv));
158 printf(" interface %s.%d\n", cval, mdv.mv_version);
160 record_int(MDT_VERSION);
162 record_int(mdv.mv_version);
163 record_string(kldname);
168 printf(" module %s\n", cval);
170 record_int(MDT_MODULE);
172 record_string(kldname);
177 printf(" pnp info for bus %s\n", cval);
180 warnx("unknown metadata record %d in file %s", md->md_type, kldname);
188 read_kld(char *filename, char *kldname)
190 struct mod_metadata md;
193 int error, eftype, nmlen;
194 long start, finish, entries;
195 char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp;
197 if (verbose || dflag)
198 printf("%s\n", filename);
199 error = ef_open(filename, &ef, verbose);
201 error = ef_obj_open(filename, &ef, verbose);
204 warnc(error, "elf_open(%s)", filename);
208 eftype = EF_GET_TYPE(&ef);
209 if (eftype != EFT_KLD && eftype != EFT_KERNEL) {
214 cp = strrchr(kldname, '.');
215 nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname);
216 if (nmlen > MAXMODNAME)
218 strlcpy(kldmodname, kldname, nmlen);
219 /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
222 check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish,
224 check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries,
228 check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md),
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);
237 warnc(error, "error while reading %s", filename);
245 * Create a temp file in directory root, make sure we don't
246 * overflow the buffer for the destination name
249 maketempfile(char *dest, const char *root)
254 p = strrchr(root, '/');
255 n = p != NULL ? p - root + 1 : 0;
256 if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >=
258 errno = ENAMETOOLONG;
265 fchmod(fd, 0644); /* nothing secret in the file */
266 return fdopen(fd, "w+");
269 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
275 fprintf(stderr, "%s\n",
276 "usage: kldxref [-Rdv] [-f hintsfile] path ..."
282 compare(const FTSENT *const *a, const FTSENT *const *b)
284 if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D)
286 if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D)
288 return strcmp((*a)->fts_name, (*b)->fts_name);
292 main(int argc, char *argv[])
296 int opt, fts_options, ival;
299 fts_options = FTS_PHYSICAL;
301 while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
303 case 'd': /* no hint file, only print on stdout */
306 case 'f': /* use this name instead of linker.hints */
312 case 'R': /* recurse on directories */
313 fts_options |= FTS_COMFOLLOW;
320 if (argc - optind < 1)
325 if (stat(argv[0], &sb) != 0)
326 err(1, "%s", argv[0]);
327 if ((sb.st_mode & S_IFDIR) == 0) {
329 err(1, "%s", argv[0]);
332 ftsp = fts_open(argv, fts_options, compare);
338 if ((p == NULL || p->fts_info == FTS_D) && fxref) {
339 /* close and rename the current hint file */
343 rename(tempname, xrefname);
345 /* didn't find any entry, ignore this file */
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);
358 err(1, "can't create %s", tempname);
360 fwrite(&ival, sizeof(ival), 1, fxref);
363 /* skip non-files and separate debug files */
364 if (p->fts_info != FTS_F)
366 if (p->fts_namelen >= 6 &&
367 strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0)
369 if (p->fts_namelen >= 8 &&
370 strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0)
372 read_kld(p->fts_path, p->fts_name);