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 1024
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);
176 warnx("unknown metadata record %d in file %s", md->md_type, kldname);
184 read_kld(char *filename, char *kldname)
186 struct mod_metadata md;
189 int error, eftype, nmlen;
190 long start, finish, entries;
191 char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp;
193 if (verbose || dflag)
194 printf("%s\n", filename);
195 error = ef_open(filename, &ef, verbose);
197 error = ef_obj_open(filename, &ef, verbose);
200 warnc(error, "elf_open(%s)", filename);
204 eftype = EF_GET_TYPE(&ef);
205 if (eftype != EFT_KLD && eftype != EFT_KERNEL) {
210 cp = strrchr(kldname, '.');
211 nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname);
212 if (nmlen > MAXMODNAME)
214 strlcpy(kldmodname, kldname, nmlen);
215 /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
218 check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish,
220 check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries,
224 check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md),
227 check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval,
228 sizeof(cval), cval));
229 cval[MAXMODNAME] = '\0';
230 parse_entry(&md, cval, &ef, kldname);
233 warnc(error, "error while reading %s", filename);
241 * Create a temp file in directory root, make sure we don't
242 * overflow the buffer for the destination name
245 maketempfile(char *dest, const char *root)
250 p = strrchr(root, '/');
251 n = p != NULL ? p - root + 1 : 0;
252 if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >=
254 errno = ENAMETOOLONG;
261 fchmod(fd, 0644); /* nothing secret in the file */
262 return fdopen(fd, "w+");
265 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
271 fprintf(stderr, "%s\n",
272 "usage: kldxref [-Rdv] [-f hintsfile] path ..."
278 main(int argc, char *argv[])
282 int opt, fts_options, ival;
285 fts_options = FTS_PHYSICAL;
287 while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
289 case 'd': /* no hint file, only print on stdout */
292 case 'f': /* use this name instead of linker.hints */
298 case 'R': /* recurse on directories */
299 fts_options |= FTS_COMFOLLOW;
306 if (argc - optind < 1)
311 if (stat(argv[0], &sb) != 0)
312 err(1, "%s", argv[0]);
313 if ((sb.st_mode & S_IFDIR) == 0) {
315 err(1, "%s", argv[0]);
318 ftsp = fts_open(argv, fts_options, 0);
324 if ((p == NULL || p->fts_info == FTS_D) && fxref) {
325 /* close and rename the current hint file */
329 rename(tempname, xrefname);
331 /* didn't find any entry, ignore this file */
338 if (p->fts_info == FTS_D && !dflag) {
339 /* visiting a new directory, create a new hint file */
340 snprintf(xrefname, sizeof(xrefname), "%s/%s",
341 ftsp->fts_path, xref_file);
342 fxref = maketempfile(tempname, ftsp->fts_path);
344 err(1, "can't create %s", tempname);
346 fwrite(&ival, sizeof(ival), 1, fxref);
349 /* skip non-files or .symbols entries */
350 if (p->fts_info != FTS_F)
352 if (p->fts_namelen >= 8 &&
353 strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0)
355 read_kld(p->fts_path, p->fts_name);