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>
49 #include <machine/elf.h>
57 #define MAXRECSIZE 1024
58 #define check(val) if ((error = (val)) != 0) break
61 #define min(a,b) (((a)<(b)) ? (a) : (b))
67 SLIST_ENTRY(mod_info) mi_next;
73 SLIST_HEAD(mod_list_head, mod_info) k_modules;
74 SLIST_ENTRY(kld_info) k_next;
77 SLIST_HEAD(kld_list_head, kld_info) kldlist;
80 static int dflag, verbose;
84 static const char *xref_file = "linker.hints";
86 static char recbuf[MAXRECSIZE];
87 static int recpos, reccnt;
89 FILE *maketempfile(char *, const char *);
90 static void usage(void);
95 recpos = (recpos + sizeof(int) - 1) & ~(sizeof(int) - 1);
102 memset(recbuf, 0, MAXRECSIZE);
108 if (dflag || recpos == 0)
112 fwrite(&recpos, sizeof(recpos), 1, fxref);
113 return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0;
117 record_buf(const void *buf, int size)
119 if (MAXRECSIZE - recpos < size)
120 errx(1, "record buffer overflow");
121 memcpy(recbuf + recpos, buf, size);
130 return record_buf(&val, sizeof(val));
134 record_byte(u_char val)
136 return record_buf(&val, sizeof(val));
140 record_string(const char *str)
142 int len = strlen(str);
147 error = record_byte(len);
150 return record_buf(str, len);
154 parse_entry(struct mod_metadata *md, const char *cval,
155 struct elf_file *ef, const char *kldname)
157 struct mod_depend mdp;
158 struct mod_version mdv;
159 Elf_Off data = (Elf_Off)md->md_data;
163 switch (md->md_type) {
167 check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp));
168 printf(" depends on %s.%d (%d,%d)\n", cval,
169 mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum);
172 check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv));
173 record_int(MDT_VERSION);
175 record_int(mdv.mv_version);
176 record_string(kldname);
179 printf(" interface %s.%d\n", cval, mdv.mv_version);
182 record_int(MDT_MODULE);
184 record_string(kldname);
187 printf(" module %s\n", cval);
190 warnx("unknown metadata record %d in file %s", md->md_type, kldname);
198 read_kld(char *filename, char *kldname)
200 struct mod_metadata md;
202 /* struct kld_info *kip;
203 struct mod_info *mip;*/
205 int error, eftype, nmlen;
206 long start, finish, entries;
207 char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp;
209 if (verbose || dflag)
210 printf("%s\n", filename);
211 error = ef_open(filename, &ef, verbose);
213 error = ef_obj_open(filename, &ef, verbose);
216 warnc(error, "elf_open(%s)", filename);
220 eftype = EF_GET_TYPE(&ef);
221 if (eftype != EFT_KLD && eftype != EFT_KERNEL) {
226 cp = strrchr(kldname, '.');
227 nmlen = cp ? min(MAXMODNAME, cp - kldname) :
228 min(MAXMODNAME, strlen(kldname));
229 strlcpy(kldmodname, kldname, nmlen);
230 /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
233 check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish,
235 check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries,
239 check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md),
242 check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval,
243 sizeof(cval), cval));
244 cval[MAXMODNAME] = '\0';
245 parse_entry(&md, cval, &ef, kldname);
248 warnc(error, "error while reading %s", filename);
256 maketempfile(char *dest, const char *root)
261 strlcpy(dest, root, MAXPATHLEN);
263 if ((p = strrchr(dest, '/')) != 0)
267 strcpy(p, "lhint.XXXXXX");
269 return ((fd == -1) ? NULL : fdopen(fd, "w+"));
272 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
275 main(int argc, char *argv[])
279 int opt, fts_options, ival;
282 fts_options = FTS_PHYSICAL;
283 /* SLIST_INIT(&kldlist);*/
285 while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
297 fts_options |= FTS_COMFOLLOW;
304 if (argc - optind < 1)
309 if (stat(argv[0], &sb) != 0)
310 err(1, "%s", argv[0]);
311 if ((sb.st_mode & S_IFDIR) == 0) {
313 err(1, "%s", argv[0]);
316 ftsp = fts_open(argv, fts_options, 0);
322 if ((p == NULL || p->fts_info == FTS_D) && !dflag && fxref) {
326 rename(tempname, xrefname);
334 if (p && p->fts_info == FTS_D && !dflag) {
335 snprintf(xrefname, sizeof(xrefname), "%s/%s",
336 ftsp->fts_path, xref_file);
337 fxref = maketempfile(tempname, ftsp->fts_path);
339 err(1, "can't create %s", tempname);
341 fwrite(&ival, sizeof(ival), 1, fxref);
344 if (p->fts_info != FTS_F)
346 if (p->fts_namelen >= 8 &&
347 strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0)
349 read_kld(p->fts_path, p->fts_name);
359 fprintf(stderr, "%s\n",
360 "usage: kldxref [-Rdv] [-f hintsfile] path ..."