1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 2000, Boris Popov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Boris Popov. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/endian.h> 40 #include <sys/exec.h> 41 #include <sys/queue.h> 42 #include <sys/kernel.h> 43 #include <sys/reboot.h> 44 #include <sys/linker.h> 45 #include <sys/stat.h> 46 #include <sys/module.h> 47 #define FREEBSD_ELF 48 #include <err.h> 49 #include <fts.h> 50 #include <string.h> 51 #include <machine/elf.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <unistd.h> 55 #include <errno.h> 56 57 #include "ef.h" 58 59 #define MAXRECSIZE (64 << 10) /* 64k */ 60 #define check(val) if ((error = (val)) != 0) break 61 62 static int dflag; /* do not create a hint file, only write on stdout */ 63 static int verbose; 64 65 static FILE *fxref; /* current hints file */ 66 67 static const char *xref_file = "linker.hints"; 68 69 /* 70 * A record is stored in the static buffer recbuf before going to disk. 71 */ 72 static char recbuf[MAXRECSIZE]; 73 static int recpos; /* current write position */ 74 static int reccnt; /* total record written to this file so far */ 75 76 static void 77 intalign(void) 78 { 79 recpos = roundup2(recpos, sizeof(int)); 80 } 81 82 static void 83 record_start(void) 84 { 85 recpos = 0; 86 memset(recbuf, 0, MAXRECSIZE); 87 } 88 89 static int 90 record_end(void) 91 { 92 if (recpos == 0) 93 return 0; 94 reccnt++; 95 intalign(); 96 fwrite(&recpos, sizeof(recpos), 1, fxref); 97 return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0; 98 } 99 100 static int 101 record_buf(const void *buf, int size) 102 { 103 if (MAXRECSIZE - recpos < size) 104 errx(1, "record buffer overflow"); 105 memcpy(recbuf + recpos, buf, size); 106 recpos += size; 107 return 0; 108 } 109 110 /* 111 * An int is stored in host order and aligned 112 */ 113 static int 114 record_int(int val) 115 { 116 intalign(); 117 return record_buf(&val, sizeof(val)); 118 } 119 120 /* 121 * A string is stored as 1-byte length plus data, no padding 122 */ 123 static int 124 record_string(const char *str) 125 { 126 int len, error; 127 u_char val; 128 129 if (dflag) 130 return 0; 131 val = len = strlen(str); 132 if (len > 255) 133 errx(1, "string %s too long", str); 134 error = record_buf(&val, sizeof(val)); 135 if (error) 136 return error; 137 return record_buf(str, len); 138 } 139 140 /* From sys/isa/pnp.c */ 141 static char * 142 pnp_eisaformat(uint32_t id) 143 { 144 uint8_t *data; 145 static char idbuf[8]; 146 const char hextoascii[] = "0123456789abcdef"; 147 148 id = htole32(id); 149 data = (uint8_t *)&id; 150 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); 151 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); 152 idbuf[2] = '@' + (data[1] & 0x1f); 153 idbuf[3] = hextoascii[(data[2] >> 4)]; 154 idbuf[4] = hextoascii[(data[2] & 0xf)]; 155 idbuf[5] = hextoascii[(data[3] >> 4)]; 156 idbuf[6] = hextoascii[(data[3] & 0xf)]; 157 idbuf[7] = 0; 158 return(idbuf); 159 } 160 161 struct pnp_elt 162 { 163 int pe_kind; /* What kind of entry */ 164 #define TYPE_SZ_MASK 0x0f 165 #define TYPE_FLAGGED 0x10 /* all f's is a wildcard */ 166 #define TYPE_INT 0x20 /* Is a number */ 167 #define TYPE_PAIRED 0x40 168 #define TYPE_LE 0x80 /* Matches <= this value */ 169 #define TYPE_GE 0x100 /* Matches >= this value */ 170 #define TYPE_MASK 0x200 /* Specifies a mask to follow */ 171 #define TYPE_U8 (1 | TYPE_INT) 172 #define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED) 173 #define TYPE_G16 (2 | TYPE_INT | TYPE_GE) 174 #define TYPE_L16 (2 | TYPE_INT | TYPE_LE) 175 #define TYPE_M16 (2 | TYPE_INT | TYPE_MASK) 176 #define TYPE_U16 (2 | TYPE_INT) 177 #define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED) 178 #define TYPE_U32 (4 | TYPE_INT) 179 #define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED) 180 #define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED) 181 #define TYPE_D 7 182 #define TYPE_Z 8 183 #define TYPE_P 9 184 #define TYPE_E 10 185 #define TYPE_T 11 186 int pe_offset; /* Offset within the element */ 187 char * pe_key; /* pnp key name */ 188 TAILQ_ENTRY(pnp_elt) next; /* Link */ 189 }; 190 typedef TAILQ_HEAD(pnp_head, pnp_elt) pnp_list; 191 192 /* 193 * this function finds the data from the pnp table, as described by the 194 * the description and creates a new output (new_desc). This output table 195 * is a form that's easier for the agent that's automatically loading the 196 * modules. 197 * 198 * The format output is the simplified string from this routine in the 199 * same basic format as the pnp string, as documented in sys/module.h. 200 * First a string describing the format is output, the a count of the 201 * number of records, then each record. The format string also describes 202 * the length of each entry (though it isn't a fixed length when strings 203 * are present). 204 * 205 * type Output Meaning 206 * I uint32_t Integer equality comparison 207 * J uint32_t Pair of uint16_t fields converted to native 208 byte order. The two fields both must match. 209 * G uint32_t Greater than or equal to 210 * L uint32_t Less than or equal to 211 * M uint32_t Mask of which fields to test. Fields that 212 take up space increment the count. This 213 field must be first, and resets the count. 214 * D string Description of the device this pnp info is for 215 * Z string pnp string must match this 216 * T nothing T fields set pnp values that must be true for 217 * the entire table. 218 * Values are packed the same way that other values are packed in this file. 219 * Strings and int32_t's start on a 32-bit boundary and are padded with 0 220 * bytes. Objects that are smaller than uint32_t are converted, without 221 * sign extension to uint32_t to simplify parsing downstream. 222 */ 223 static int 224 parse_pnp_list(const char *desc, char **new_desc, pnp_list *list) 225 { 226 const char *walker = desc, *ep = desc + strlen(desc); 227 const char *colon, *semi; 228 struct pnp_elt *elt; 229 char *nd; 230 char type[8], key[32]; 231 int off; 232 233 off = 0; 234 nd = *new_desc = malloc(strlen(desc) + 1); 235 if (verbose > 1) 236 printf("Converting %s into a list\n", desc); 237 while (walker < ep) { 238 colon = strchr(walker, ':'); 239 semi = strchr(walker, ';'); 240 if (semi != NULL && semi < colon) 241 goto err; 242 if (colon - walker > sizeof(type)) 243 goto err; 244 strncpy(type, walker, colon - walker); 245 type[colon - walker] = '\0'; 246 if (semi) { 247 if (semi - colon >= sizeof(key)) 248 goto err; 249 strncpy(key, colon + 1, semi - colon - 1); 250 key[semi - colon - 1] = '\0'; 251 walker = semi + 1; 252 } else { 253 if (strlen(colon + 1) >= sizeof(key)) 254 goto err; 255 strcpy(key, colon + 1); 256 walker = ep; 257 } 258 if (verbose > 1) 259 printf("Found type %s for name %s\n", type, key); 260 /* Skip pointer place holders */ 261 if (strcmp(type, "P") == 0) { 262 off += sizeof(void *); 263 continue; 264 } 265 266 /* 267 * Add a node of the appropriate type 268 */ 269 elt = malloc(sizeof(struct pnp_elt) + strlen(key) + 1); 270 TAILQ_INSERT_TAIL(list, elt, next); 271 elt->pe_key = (char *)(elt + 1); 272 elt->pe_offset = off; 273 if (strcmp(type, "U8") == 0) 274 elt->pe_kind = TYPE_U8; 275 else if (strcmp(type, "V8") == 0) 276 elt->pe_kind = TYPE_V8; 277 else if (strcmp(type, "G16") == 0) 278 elt->pe_kind = TYPE_G16; 279 else if (strcmp(type, "L16") == 0) 280 elt->pe_kind = TYPE_L16; 281 else if (strcmp(type, "M16") == 0) 282 elt->pe_kind = TYPE_M16; 283 else if (strcmp(type, "U16") == 0) 284 elt->pe_kind = TYPE_U16; 285 else if (strcmp(type, "V16") == 0) 286 elt->pe_kind = TYPE_V16; 287 else if (strcmp(type, "U32") == 0) 288 elt->pe_kind = TYPE_U32; 289 else if (strcmp(type, "V32") == 0) 290 elt->pe_kind = TYPE_V32; 291 else if (strcmp(type, "W32") == 0) 292 elt->pe_kind = TYPE_W32; 293 else if (strcmp(type, "D") == 0) /* description char * */ 294 elt->pe_kind = TYPE_D; 295 else if (strcmp(type, "Z") == 0) /* char * to match */ 296 elt->pe_kind = TYPE_Z; 297 else if (strcmp(type, "P") == 0) /* Pointer -- ignored */ 298 elt->pe_kind = TYPE_P; 299 else if (strcmp(type, "E") == 0) /* EISA PNP ID, as uint32_t */ 300 elt->pe_kind = TYPE_E; 301 else if (strcmp(type, "T") == 0) 302 elt->pe_kind = TYPE_T; 303 else 304 goto err; 305 /* 306 * Maybe the rounding here needs to be more nuanced and/or somehow 307 * architecture specific. Fortunately, most tables in the system 308 * have sane ordering of types. 309 */ 310 if (elt->pe_kind & TYPE_INT) { 311 elt->pe_offset = roundup2(elt->pe_offset, elt->pe_kind & TYPE_SZ_MASK); 312 off = elt->pe_offset + (elt->pe_kind & TYPE_SZ_MASK); 313 } else if (elt->pe_kind == TYPE_E) { 314 /* Type E stored as Int, displays as string */ 315 elt->pe_offset = roundup2(elt->pe_offset, sizeof(uint32_t)); 316 off = elt->pe_offset + sizeof(uint32_t); 317 } else if (elt->pe_kind == TYPE_T) { 318 /* doesn't actually consume space in the table */ 319 off = elt->pe_offset; 320 } else { 321 elt->pe_offset = roundup2(elt->pe_offset, sizeof(void *)); 322 off = elt->pe_offset + sizeof(void *); 323 } 324 if (elt->pe_kind & TYPE_PAIRED) { 325 char *word, *ctx; 326 327 for (word = strtok_r(key, "/", &ctx); 328 word; word = strtok_r(NULL, "/", &ctx)) { 329 sprintf(nd, "%c:%s;", elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I', 330 word); 331 nd += strlen(nd); 332 } 333 334 } 335 else { 336 if (elt->pe_kind & TYPE_FLAGGED) 337 *nd++ = 'J'; 338 else if (elt->pe_kind & TYPE_GE) 339 *nd++ = 'G'; 340 else if (elt->pe_kind & TYPE_LE) 341 *nd++ = 'L'; 342 else if (elt->pe_kind & TYPE_MASK) 343 *nd++ = 'M'; 344 else if (elt->pe_kind & TYPE_INT) 345 *nd++ = 'I'; 346 else if (elt->pe_kind == TYPE_D) 347 *nd++ = 'D'; 348 else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E) 349 *nd++ = 'Z'; 350 else if (elt->pe_kind == TYPE_T) 351 *nd++ = 'T'; 352 else 353 errx(1, "Impossible type %x\n", elt->pe_kind); 354 *nd++ = ':'; 355 strcpy(nd, key); 356 nd += strlen(nd); 357 *nd++ = ';'; 358 } 359 } 360 *nd++ = '\0'; 361 return 0; 362 err: 363 errx(1, "Parse error of description string %s", desc); 364 } 365 366 static int 367 parse_entry(struct mod_metadata *md, const char *cval, 368 struct elf_file *ef, const char *kldname) 369 { 370 struct mod_depend mdp; 371 struct mod_version mdv; 372 struct mod_pnp_match_info pnp; 373 char descr[1024]; 374 Elf_Off data = (Elf_Off)md->md_data; 375 int error = 0, i, len; 376 char *walker; 377 void *table; 378 379 record_start(); 380 switch (md->md_type) { 381 case MDT_DEPEND: 382 if (!dflag) 383 break; 384 check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp)); 385 printf(" depends on %s.%d (%d,%d)\n", cval, 386 mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum); 387 break; 388 case MDT_VERSION: 389 check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv)); 390 if (dflag) { 391 printf(" interface %s.%d\n", cval, mdv.mv_version); 392 } else { 393 record_int(MDT_VERSION); 394 record_string(cval); 395 record_int(mdv.mv_version); 396 record_string(kldname); 397 } 398 break; 399 case MDT_MODULE: 400 if (dflag) { 401 printf(" module %s\n", cval); 402 } else { 403 record_int(MDT_MODULE); 404 record_string(cval); 405 record_string(kldname); 406 } 407 break; 408 case MDT_PNP_INFO: 409 check(EF_SEG_READ_REL(ef, data, sizeof(pnp), &pnp)); 410 check(EF_SEG_READ(ef, (Elf_Off)pnp.descr, sizeof(descr), descr)); 411 descr[sizeof(descr) - 1] = '\0'; 412 if (dflag) { 413 printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 414 cval, descr, pnp.num_entry, pnp.entry_len); 415 } else { 416 pnp_list list; 417 struct pnp_elt *elt, *elt_tmp; 418 char *new_descr; 419 420 if (verbose > 1) 421 printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 422 cval, descr, pnp.num_entry, pnp.entry_len); 423 /* 424 * Parse descr to weed out the chaff and to create a list 425 * of offsets to output. 426 */ 427 TAILQ_INIT(&list); 428 parse_pnp_list(descr, &new_descr, &list); 429 record_int(MDT_PNP_INFO); 430 record_string(cval); 431 record_string(new_descr); 432 record_int(pnp.num_entry); 433 len = pnp.num_entry * pnp.entry_len; 434 walker = table = malloc(len); 435 check(EF_SEG_READ_REL(ef, (Elf_Off)pnp.table, len, table)); 436 437 /* 438 * Walk the list and output things. We've collapsed all the 439 * variant forms of the table down to just ints and strings. 440 */ 441 for (i = 0; i < pnp.num_entry; i++) { 442 TAILQ_FOREACH(elt, &list, next) { 443 uint8_t v1; 444 uint16_t v2; 445 uint32_t v4; 446 int value; 447 char buffer[1024]; 448 449 if (elt->pe_kind == TYPE_W32) { 450 memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 451 value = v4 & 0xffff; 452 record_int(value); 453 if (verbose > 1) 454 printf("W32:%#x", value); 455 value = (v4 >> 16) & 0xffff; 456 record_int(value); 457 if (verbose > 1) 458 printf(":%#x;", value); 459 } else if (elt->pe_kind & TYPE_INT) { 460 switch (elt->pe_kind & TYPE_SZ_MASK) { 461 case 1: 462 memcpy(&v1, walker + elt->pe_offset, sizeof(v1)); 463 if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff) 464 value = -1; 465 else 466 value = v1; 467 break; 468 case 2: 469 memcpy(&v2, walker + elt->pe_offset, sizeof(v2)); 470 if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff) 471 value = -1; 472 else 473 value = v2; 474 break; 475 case 4: 476 memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 477 if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff) 478 value = -1; 479 else 480 value = v4; 481 break; 482 default: 483 errx(1, "Invalid size somehow %#x", elt->pe_kind); 484 } 485 if (verbose > 1) 486 printf("I:%#x;", value); 487 record_int(value); 488 } else if (elt->pe_kind == TYPE_T) { 489 /* Do nothing */ 490 } else { /* E, Z or D -- P already filtered */ 491 if (elt->pe_kind == TYPE_E) { 492 memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 493 strcpy(buffer, pnp_eisaformat(v4)); 494 } else { 495 char *ptr; 496 497 ptr = *(char **)(walker + elt->pe_offset); 498 buffer[0] = '\0'; 499 if (ptr != NULL) { 500 EF_SEG_READ(ef, (Elf_Off)ptr, 501 sizeof(buffer), buffer); 502 buffer[sizeof(buffer) - 1] = '\0'; 503 } 504 } 505 if (verbose > 1) 506 printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer); 507 record_string(buffer); 508 } 509 } 510 if (verbose > 1) 511 printf("\n"); 512 walker += pnp.entry_len; 513 } 514 /* Now free it */ 515 TAILQ_FOREACH_SAFE(elt, &list, next, elt_tmp) { 516 TAILQ_REMOVE(&list, elt, next); 517 free(elt); 518 } 519 free(table); 520 } 521 break; 522 default: 523 warnx("unknown metadata record %d in file %s", md->md_type, kldname); 524 } 525 if (!error) 526 record_end(); 527 return error; 528 } 529 530 static int 531 read_kld(char *filename, char *kldname) 532 { 533 struct mod_metadata md; 534 struct elf_file ef; 535 void **p, **orgp; 536 int error, eftype, nmlen; 537 long start, finish, entries; 538 char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp; 539 540 if (verbose || dflag) 541 printf("%s\n", filename); 542 error = ef_open(filename, &ef, verbose); 543 if (error) { 544 error = ef_obj_open(filename, &ef, verbose); 545 if (error) { 546 if (verbose) 547 warnc(error, "elf_open(%s)", filename); 548 return error; 549 } 550 } 551 eftype = EF_GET_TYPE(&ef); 552 if (eftype != EFT_KLD && eftype != EFT_KERNEL) { 553 EF_CLOSE(&ef); 554 return 0; 555 } 556 if (!dflag) { 557 cp = strrchr(kldname, '.'); 558 nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname); 559 if (nmlen > MAXMODNAME) 560 nmlen = MAXMODNAME; 561 strlcpy(kldmodname, kldname, nmlen); 562 /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/ 563 } 564 do { 565 check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish, 566 &entries)); 567 check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries, 568 (void *)&p)); 569 orgp = p; 570 while(entries--) { 571 check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md), 572 &md)); 573 p++; 574 check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval, 575 sizeof(cval), cval)); 576 cval[MAXMODNAME] = '\0'; 577 parse_entry(&md, cval, &ef, kldname); 578 } 579 if (error) 580 warnc(error, "error while reading %s", filename); 581 free(orgp); 582 } while(0); 583 EF_CLOSE(&ef); 584 return error; 585 } 586 587 /* 588 * Create a temp file in directory root, make sure we don't 589 * overflow the buffer for the destination name 590 */ 591 static FILE * 592 maketempfile(char *dest, const char *root) 593 { 594 char *p; 595 int n, fd; 596 597 p = strrchr(root, '/'); 598 n = p != NULL ? p - root + 1 : 0; 599 if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >= 600 MAXPATHLEN) { 601 errno = ENAMETOOLONG; 602 return NULL; 603 } 604 605 fd = mkstemp(dest); 606 if (fd < 0) 607 return NULL; 608 fchmod(fd, 0644); /* nothing secret in the file */ 609 return fdopen(fd, "w+"); 610 } 611 612 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN]; 613 614 static void 615 usage(void) 616 { 617 618 fprintf(stderr, "%s\n", 619 "usage: kldxref [-Rdv] [-f hintsfile] path ..." 620 ); 621 exit(1); 622 } 623 624 static int 625 compare(const FTSENT *const *a, const FTSENT *const *b) 626 { 627 if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D) 628 return 1; 629 if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D) 630 return -1; 631 return strcmp((*a)->fts_name, (*b)->fts_name); 632 } 633 634 int 635 main(int argc, char *argv[]) 636 { 637 FTS *ftsp; 638 FTSENT *p; 639 int opt, fts_options, ival; 640 struct stat sb; 641 642 fts_options = FTS_PHYSICAL; 643 644 while ((opt = getopt(argc, argv, "Rdf:v")) != -1) { 645 switch (opt) { 646 case 'd': /* no hint file, only print on stdout */ 647 dflag = 1; 648 break; 649 case 'f': /* use this name instead of linker.hints */ 650 xref_file = optarg; 651 break; 652 case 'v': 653 verbose++; 654 break; 655 case 'R': /* recurse on directories */ 656 fts_options |= FTS_COMFOLLOW; 657 break; 658 default: 659 usage(); 660 /* NOTREACHED */ 661 } 662 } 663 if (argc - optind < 1) 664 usage(); 665 argc -= optind; 666 argv += optind; 667 668 if (stat(argv[0], &sb) != 0) 669 err(1, "%s", argv[0]); 670 if ((sb.st_mode & S_IFDIR) == 0) { 671 errno = ENOTDIR; 672 err(1, "%s", argv[0]); 673 } 674 675 ftsp = fts_open(argv, fts_options, compare); 676 if (ftsp == NULL) 677 exit(1); 678 679 for (;;) { 680 p = fts_read(ftsp); 681 if ((p == NULL || p->fts_info == FTS_D) && fxref) { 682 /* close and rename the current hint file */ 683 fclose(fxref); 684 fxref = NULL; 685 if (reccnt) { 686 rename(tempname, xrefname); 687 } else { 688 /* didn't find any entry, ignore this file */ 689 unlink(tempname); 690 unlink(xrefname); 691 } 692 } 693 if (p == NULL) 694 break; 695 if (p->fts_info == FTS_D && !dflag) { 696 /* visiting a new directory, create a new hint file */ 697 snprintf(xrefname, sizeof(xrefname), "%s/%s", 698 ftsp->fts_path, xref_file); 699 fxref = maketempfile(tempname, ftsp->fts_path); 700 if (fxref == NULL) 701 err(1, "can't create %s", tempname); 702 ival = 1; 703 fwrite(&ival, sizeof(ival), 1, fxref); 704 reccnt = 0; 705 } 706 /* skip non-files and separate debug files */ 707 if (p->fts_info != FTS_F) 708 continue; 709 if (p->fts_namelen >= 6 && 710 strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0) 711 continue; 712 if (p->fts_namelen >= 8 && 713 strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0) 714 continue; 715 read_kld(p->fts_path, p->fts_name); 716 } 717 fts_close(ftsp); 718 return 0; 719 } 720