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>
37 #include <sys/endian.h>
39 #include <sys/queue.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
42 #include <sys/linker.h>
44 #include <sys/module.h>
49 #include <machine/elf.h>
57 #define MAXRECSIZE (64 << 10) /* 64k */
58 #define check(val) if ((error = (val)) != 0) break
60 static int dflag
; /* do not create a hint file, only write on stdout */
63 static FILE *fxref
; /* current hints file */
65 static const char *xref_file
= "linker.hints";
68 * A record is stored in the static buffer recbuf before going to disk.
70 static char recbuf
[MAXRECSIZE
];
71 static int recpos
; /* current write position */
72 static int reccnt
; /* total record written to this file so far */
77 recpos
= (recpos
+ sizeof(int) - 1) & ~(sizeof(int) - 1);
84 memset(recbuf
, 0, MAXRECSIZE
);
94 fwrite(&recpos
, sizeof(recpos
), 1, fxref
);
95 return fwrite(recbuf
, recpos
, 1, fxref
) != 1 ? errno
: 0;
99 record_buf(const void *buf
, int size
)
101 if (MAXRECSIZE
- recpos
< size
)
102 errx(1, "record buffer overflow");
103 memcpy(recbuf
+ recpos
, buf
, size
);
109 * An int is stored in host order and aligned
115 return record_buf(&val
, sizeof(val
));
119 * A string is stored as 1-byte length plus data, no padding
122 record_string(const char *str
)
129 val
= len
= strlen(str
);
131 errx(1, "string %s too long", str
);
132 error
= record_buf(&val
, sizeof(val
));
135 return record_buf(str
, len
);
138 /* From sys/isa/pnp.c */
140 pnp_eisaformat(uint32_t id
)
143 static char idbuf
[8];
144 const char hextoascii
[] = "0123456789abcdef";
147 data
= (uint8_t *)&id
;
148 idbuf
[0] = '@' + ((data
[0] & 0x7c) >> 2);
149 idbuf
[1] = '@' + (((data
[0] & 0x3) << 3) + ((data
[1] & 0xe0) >> 5));
150 idbuf
[2] = '@' + (data
[1] & 0x1f);
151 idbuf
[3] = hextoascii
[(data
[2] >> 4)];
152 idbuf
[4] = hextoascii
[(data
[2] & 0xf)];
153 idbuf
[5] = hextoascii
[(data
[3] >> 4)];
154 idbuf
[6] = hextoascii
[(data
[3] & 0xf)];
161 int pe_kind
; /* What kind of entry */
162 #define TYPE_SZ_MASK 0x0f
163 #define TYPE_FLAGGED 0x10 /* all f's is a wildcard */
164 #define TYPE_INT 0x20 /* Is a number */
165 #define TYPE_PAIRED 0x40
166 #define TYPE_LE 0x80 /* Matches <= this value */
167 #define TYPE_GE 0x100 /* Matches >= this value */
168 #define TYPE_MASK 0x200 /* Specifies a mask to follow */
169 #define TYPE_U8 (1 | TYPE_INT)
170 #define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED)
171 #define TYPE_G16 (2 | TYPE_INT | TYPE_GE)
172 #define TYPE_L16 (2 | TYPE_INT | TYPE_LE)
173 #define TYPE_M16 (2 | TYPE_INT | TYPE_MASK)
174 #define TYPE_U16 (2 | TYPE_INT)
175 #define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED)
176 #define TYPE_U32 (4 | TYPE_INT)
177 #define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED)
178 #define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED)
184 int pe_offset
; /* Offset within the element */
185 char * pe_key
; /* pnp key name */
186 TAILQ_ENTRY(pnp_elt
) next
; /* Link */
188 typedef TAILQ_HEAD(pnp_head
, pnp_elt
) pnp_list
;
191 * this function finds the data from the pnp table, as described by the
192 * the description and creates a new output (new_desc). This output table
193 * is a form that's easier for the agent that's automatically loading the
196 * The format output is the simplified string from this routine in the
197 * same basic format as the pnp string, as documented in sys/module.h.
198 * First a string describing the format is output, the a count of the
199 * number of records, then each record. The format string also describes
200 * the length of each entry (though it isn't a fixed length when strings
203 * type Output Meaning
204 * I uint32_t Integer equality comparison
205 * J uint32_t Pair of uint16_t fields converted to native
206 byte order. The two fields both must match.
207 * G uint32_t Greater than or equal to
208 * L uint32_t Less than or equal to
209 * M uint32_t Mask of which fields to test. Fields that
210 take up space increment the count. This
211 field must be first, and resets the count.
212 * D string Description of the device this pnp info is for
213 * Z string pnp string must match this
214 * T nothing T fields set pnp values that must be true for
216 * Values are packed the same way that other values are packed in this file.
217 * Strings and int32_t's start on a 32-bit boundary and are padded with 0
218 * bytes. Objects that are smaller than uint32_t are converted, without
219 * sign extension to uint32_t to simplify parsing downstream.
222 parse_pnp_list(const char *desc
, char **new_desc
, pnp_list
*list
)
224 const char *walker
= desc
, *ep
= desc
+ strlen(desc
);
225 const char *colon
, *semi
;
228 char type
[8], key
[32];
232 nd
= *new_desc
= malloc(strlen(desc
) + 1);
234 printf("Converting %s into a list\n", desc
);
235 while (walker
< ep
) {
236 colon
= strchr(walker
, ':');
237 semi
= strchr(walker
, ';');
238 if (semi
!= NULL
&& semi
< colon
)
240 if (colon
- walker
> sizeof(type
))
242 strncpy(type
, walker
, colon
- walker
);
243 type
[colon
- walker
] = '\0';
245 if (semi
- colon
>= sizeof(key
))
247 strncpy(key
, colon
+ 1, semi
- colon
- 1);
248 key
[semi
- colon
- 1] = '\0';
251 if (strlen(colon
+ 1) >= sizeof(key
))
253 strcpy(key
, colon
+ 1);
257 printf("Found type %s for name %s\n", type
, key
);
258 /* Skip pointer place holders */
259 if (strcmp(type
, "P") == 0) {
260 off
+= sizeof(void *);
265 * Add a node of the appropriate type
267 elt
= malloc(sizeof(struct pnp_elt
) + strlen(key
) + 1);
268 TAILQ_INSERT_TAIL(list
, elt
, next
);
269 elt
->pe_key
= (char *)(elt
+ 1);
270 elt
->pe_offset
= off
;
271 if (strcmp(type
, "U8") == 0)
272 elt
->pe_kind
= TYPE_U8
;
273 else if (strcmp(type
, "V8") == 0)
274 elt
->pe_kind
= TYPE_V8
;
275 else if (strcmp(type
, "G16") == 0)
276 elt
->pe_kind
= TYPE_G16
;
277 else if (strcmp(type
, "L16") == 0)
278 elt
->pe_kind
= TYPE_L16
;
279 else if (strcmp(type
, "M16") == 0)
280 elt
->pe_kind
= TYPE_M16
;
281 else if (strcmp(type
, "U16") == 0)
282 elt
->pe_kind
= TYPE_U16
;
283 else if (strcmp(type
, "V16") == 0)
284 elt
->pe_kind
= TYPE_V16
;
285 else if (strcmp(type
, "U32") == 0)
286 elt
->pe_kind
= TYPE_U32
;
287 else if (strcmp(type
, "V32") == 0)
288 elt
->pe_kind
= TYPE_V32
;
289 else if (strcmp(type
, "W32") == 0)
290 elt
->pe_kind
= TYPE_W32
;
291 else if (strcmp(type
, "D") == 0) /* description char * */
292 elt
->pe_kind
= TYPE_D
;
293 else if (strcmp(type
, "Z") == 0) /* char * to match */
294 elt
->pe_kind
= TYPE_Z
;
295 else if (strcmp(type
, "P") == 0) /* Pointer -- ignored */
296 elt
->pe_kind
= TYPE_P
;
297 else if (strcmp(type
, "E") == 0) /* EISA PNP ID, as uint32_t */
298 elt
->pe_kind
= TYPE_E
;
299 else if (strcmp(type
, "T") == 0)
300 elt
->pe_kind
= TYPE_T
;
304 * Maybe the rounding here needs to be more nuanced and/or somehow
305 * architecture specific. Fortunately, most tables in the system
306 * have sane ordering of types.
308 if (elt
->pe_kind
& TYPE_INT
) {
309 elt
->pe_offset
= roundup2(elt
->pe_offset
, elt
->pe_kind
& TYPE_SZ_MASK
);
310 off
= elt
->pe_offset
+ (elt
->pe_kind
& TYPE_SZ_MASK
);
311 } else if (elt
->pe_kind
== TYPE_E
) {
312 /* Type E stored as Int, displays as string */
313 elt
->pe_offset
= roundup2(elt
->pe_offset
, sizeof(uint32_t));
314 off
= elt
->pe_offset
+ sizeof(uint32_t);
315 } else if (elt
->pe_kind
== TYPE_T
) {
316 /* doesn't actually consume space in the table */
317 off
= elt
->pe_offset
;
319 elt
->pe_offset
= roundup2(elt
->pe_offset
, sizeof(void *));
320 off
= elt
->pe_offset
+ sizeof(void *);
322 if (elt
->pe_kind
& TYPE_PAIRED
) {
325 for (word
= strtok_r(key
, "/", &ctx
);
326 word
; word
= strtok_r(NULL
, "/", &ctx
)) {
327 sprintf(nd
, "%c:%s;", elt
->pe_kind
& TYPE_FLAGGED
? 'J' : 'I',
334 if (elt
->pe_kind
& TYPE_FLAGGED
)
336 else if (elt
->pe_kind
& TYPE_GE
)
338 else if (elt
->pe_kind
& TYPE_LE
)
340 else if (elt
->pe_kind
& TYPE_MASK
)
342 else if (elt
->pe_kind
& TYPE_INT
)
344 else if (elt
->pe_kind
== TYPE_D
)
346 else if (elt
->pe_kind
== TYPE_Z
|| elt
->pe_kind
== TYPE_E
)
348 else if (elt
->pe_kind
== TYPE_T
)
351 errx(1, "Impossible type %x\n", elt
->pe_kind
);
361 errx(1, "Parse error of description string %s", desc
);
365 parse_entry(struct mod_metadata
*md
, const char *cval
,
366 struct elf_file
*ef
, const char *kldname
)
368 struct mod_depend mdp
;
369 struct mod_version mdv
;
370 struct mod_pnp_match_info pnp
;
372 Elf_Off data
= (Elf_Off
)md
->md_data
;
373 int error
= 0, i
, len
;
378 switch (md
->md_type
) {
382 check(EF_SEG_READ(ef
, data
, sizeof(mdp
), &mdp
));
383 printf(" depends on %s.%d (%d,%d)\n", cval
,
384 mdp
.md_ver_preferred
, mdp
.md_ver_minimum
, mdp
.md_ver_maximum
);
387 check(EF_SEG_READ(ef
, data
, sizeof(mdv
), &mdv
));
389 printf(" interface %s.%d\n", cval
, mdv
.mv_version
);
391 record_int(MDT_VERSION
);
393 record_int(mdv
.mv_version
);
394 record_string(kldname
);
399 printf(" module %s\n", cval
);
401 record_int(MDT_MODULE
);
403 record_string(kldname
);
407 check(EF_SEG_READ_REL(ef
, data
, sizeof(pnp
), &pnp
));
408 check(EF_SEG_READ(ef
, (Elf_Off
)pnp
.descr
, sizeof(descr
), descr
));
409 descr
[sizeof(descr
) - 1] = '\0';
411 printf(" pnp info for bus %s format %s %d entries of %d bytes\n",
412 cval
, descr
, pnp
.num_entry
, pnp
.entry_len
);
415 struct pnp_elt
*elt
, *elt_tmp
;
419 printf(" pnp info for bus %s format %s %d entries of %d bytes\n",
420 cval
, descr
, pnp
.num_entry
, pnp
.entry_len
);
422 * Parse descr to weed out the chaff and to create a list
423 * of offsets to output.
426 parse_pnp_list(descr
, &new_descr
, &list
);
427 record_int(MDT_PNP_INFO
);
429 record_string(new_descr
);
430 record_int(pnp
.num_entry
);
431 len
= pnp
.num_entry
* pnp
.entry_len
;
432 walker
= table
= malloc(len
);
433 check(EF_SEG_READ_REL(ef
, (Elf_Off
)pnp
.table
, len
, table
));
436 * Walk the list and output things. We've collapsed all the
437 * variant forms of the table down to just ints and strings.
439 for (i
= 0; i
< pnp
.num_entry
; i
++) {
440 TAILQ_FOREACH(elt
, &list
, next
) {
447 if (elt
->pe_kind
== TYPE_W32
) {
448 memcpy(&v4
, walker
+ elt
->pe_offset
, sizeof(v4
));
452 printf("W32:%#x", value
);
453 value
= (v4
>> 16) & 0xffff;
456 printf(":%#x;", value
);
457 } else if (elt
->pe_kind
& TYPE_INT
) {
458 switch (elt
->pe_kind
& TYPE_SZ_MASK
) {
460 memcpy(&v1
, walker
+ elt
->pe_offset
, sizeof(v1
));
461 if ((elt
->pe_kind
& TYPE_FLAGGED
) && v1
== 0xff)
467 memcpy(&v2
, walker
+ elt
->pe_offset
, sizeof(v2
));
468 if ((elt
->pe_kind
& TYPE_FLAGGED
) && v2
== 0xffff)
474 memcpy(&v4
, walker
+ elt
->pe_offset
, sizeof(v4
));
475 if ((elt
->pe_kind
& TYPE_FLAGGED
) && v4
== 0xffffffff)
481 errx(1, "Invalid size somehow %#x", elt
->pe_kind
);
484 printf("I:%#x;", value
);
486 } else if (elt
->pe_kind
== TYPE_T
) {
488 } else { /* E, Z or D -- P already filtered */
489 if (elt
->pe_kind
== TYPE_E
) {
490 memcpy(&v4
, walker
+ elt
->pe_offset
, sizeof(v4
));
491 strcpy(buffer
, pnp_eisaformat(v4
));
495 ptr
= *(char **)(walker
+ elt
->pe_offset
);
498 EF_SEG_READ(ef
, (Elf_Off
)ptr
,
499 sizeof(buffer
), buffer
);
500 buffer
[sizeof(buffer
) - 1] = '\0';
504 printf("%c:%s;", elt
->pe_kind
== TYPE_E
? 'E' : (elt
->pe_kind
== TYPE_Z
? 'Z' : 'D'), buffer
);
505 record_string(buffer
);
510 walker
+= pnp
.entry_len
;
513 TAILQ_FOREACH_SAFE(elt
, &list
, next
, elt_tmp
) {
514 TAILQ_REMOVE(&list
, elt
, next
);
521 warnx("unknown metadata record %d in file %s", md
->md_type
, kldname
);
529 read_kld(char *filename
, char *kldname
)
531 struct mod_metadata md
;
534 int error
, eftype
, nmlen
;
535 long start
, finish
, entries
;
536 char kldmodname
[MAXMODNAME
+ 1], cval
[MAXMODNAME
+ 1], *cp
;
538 if (verbose
|| dflag
)
539 printf("%s\n", filename
);
540 error
= ef_open(filename
, &ef
, verbose
);
542 error
= ef_obj_open(filename
, &ef
, verbose
);
545 warnc(error
, "elf_open(%s)", filename
);
549 eftype
= EF_GET_TYPE(&ef
);
550 if (eftype
!= EFT_KLD
&& eftype
!= EFT_KERNEL
) {
555 cp
= strrchr(kldname
, '.');
556 nmlen
= (cp
!= NULL
) ? cp
- kldname
: (int)strlen(kldname
);
557 if (nmlen
> MAXMODNAME
)
559 strlcpy(kldmodname
, kldname
, nmlen
);
560 /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/
563 check(EF_LOOKUP_SET(&ef
, MDT_SETNAME
, &start
, &finish
,
565 check(EF_SEG_READ_ENTRY_REL(&ef
, start
, sizeof(*p
) * entries
,
569 check(EF_SEG_READ_REL(&ef
, (Elf_Off
)*p
, sizeof(md
),
572 check(EF_SEG_READ(&ef
, (Elf_Off
)md
.md_cval
,
573 sizeof(cval
), cval
));
574 cval
[MAXMODNAME
] = '\0';
575 parse_entry(&md
, cval
, &ef
, kldname
);
578 warnc(error
, "error while reading %s", filename
);
586 * Create a temp file in directory root, make sure we don't
587 * overflow the buffer for the destination name
590 maketempfile(char *dest
, const char *root
)
595 p
= strrchr(root
, '/');
596 n
= p
!= NULL
? p
- root
+ 1 : 0;
597 if (snprintf(dest
, MAXPATHLEN
, "%.*slhint.XXXXXX", n
, root
) >=
599 errno
= ENAMETOOLONG
;
606 fchmod(fd
, 0644); /* nothing secret in the file */
607 return fdopen(fd
, "w+");
610 static char xrefname
[MAXPATHLEN
], tempname
[MAXPATHLEN
];
616 fprintf(stderr
, "%s\n",
617 "usage: kldxref [-Rdv] [-f hintsfile] path ..."
623 compare(const FTSENT
*const *a
, const FTSENT
*const *b
)
625 if ((*a
)->fts_info
== FTS_D
&& (*b
)->fts_info
!= FTS_D
)
627 if ((*a
)->fts_info
!= FTS_D
&& (*b
)->fts_info
== FTS_D
)
629 return strcmp((*a
)->fts_name
, (*b
)->fts_name
);
633 main(int argc
, char *argv
[])
637 int opt
, fts_options
, ival
;
640 fts_options
= FTS_PHYSICAL
;
642 while ((opt
= getopt(argc
, argv
, "Rdf:v")) != -1) {
644 case 'd': /* no hint file, only print on stdout */
647 case 'f': /* use this name instead of linker.hints */
653 case 'R': /* recurse on directories */
654 fts_options
|= FTS_COMFOLLOW
;
661 if (argc
- optind
< 1)
666 if (stat(argv
[0], &sb
) != 0)
667 err(1, "%s", argv
[0]);
668 if ((sb
.st_mode
& S_IFDIR
) == 0) {
670 err(1, "%s", argv
[0]);
673 ftsp
= fts_open(argv
, fts_options
, compare
);
679 if ((p
== NULL
|| p
->fts_info
== FTS_D
) && fxref
) {
680 /* close and rename the current hint file */
684 rename(tempname
, xrefname
);
686 /* didn't find any entry, ignore this file */
693 if (p
->fts_info
== FTS_D
&& !dflag
) {
694 /* visiting a new directory, create a new hint file */
695 snprintf(xrefname
, sizeof(xrefname
), "%s/%s",
696 ftsp
->fts_path
, xref_file
);
697 fxref
= maketempfile(tempname
, ftsp
->fts_path
);
699 err(1, "can't create %s", tempname
);
701 fwrite(&ival
, sizeof(ival
), 1, fxref
);
704 /* skip non-files and separate debug files */
705 if (p
->fts_info
!= FTS_F
)
707 if (p
->fts_namelen
>= 6 &&
708 strcmp(p
->fts_name
+ p
->fts_namelen
- 6, ".debug") == 0)
710 if (p
->fts_namelen
>= 8 &&
711 strcmp(p
->fts_name
+ p
->fts_namelen
- 8, ".symbols") == 0)
713 read_kld(p
->fts_path
, p
->fts_name
);