1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1997-2000 Doug Rabson 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ddb.h" 33 #include "opt_kld.h" 34 #include "opt_hwpmc_hooks.h" 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/sysproto.h> 41 #include <sys/sysent.h> 42 #include <sys/priv.h> 43 #include <sys/proc.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/sx.h> 47 #include <sys/module.h> 48 #include <sys/mount.h> 49 #include <sys/linker.h> 50 #include <sys/eventhandler.h> 51 #include <sys/fcntl.h> 52 #include <sys/jail.h> 53 #include <sys/libkern.h> 54 #include <sys/namei.h> 55 #include <sys/vnode.h> 56 #include <sys/syscallsubr.h> 57 #include <sys/sysctl.h> 58 59 #ifdef DDB 60 #include <ddb/ddb.h> 61 #endif 62 63 #include <net/vnet.h> 64 65 #include <security/mac/mac_framework.h> 66 67 #include "linker_if.h" 68 69 #ifdef HWPMC_HOOKS 70 #include <sys/pmckern.h> 71 #endif 72 73 #ifdef KLD_DEBUG 74 int kld_debug = 0; 75 SYSCTL_INT(_debug, OID_AUTO, kld_debug, CTLFLAG_RWTUN, 76 &kld_debug, 0, "Set various levels of KLD debug"); 77 #endif 78 79 /* These variables are used by kernel debuggers to enumerate loaded files. */ 80 const int kld_off_address = offsetof(struct linker_file, address); 81 const int kld_off_filename = offsetof(struct linker_file, filename); 82 const int kld_off_pathname = offsetof(struct linker_file, pathname); 83 const int kld_off_next = offsetof(struct linker_file, link.tqe_next); 84 85 /* 86 * static char *linker_search_path(const char *name, struct mod_depend 87 * *verinfo); 88 */ 89 static const char *linker_basename(const char *path); 90 91 /* 92 * Find a currently loaded file given its filename. 93 */ 94 static linker_file_t linker_find_file_by_name(const char* _filename); 95 96 /* 97 * Find a currently loaded file given its file id. 98 */ 99 static linker_file_t linker_find_file_by_id(int _fileid); 100 101 /* Metadata from the static kernel */ 102 SET_DECLARE(modmetadata_set, struct mod_metadata); 103 104 MALLOC_DEFINE(M_LINKER, "linker", "kernel linker"); 105 106 linker_file_t linker_kernel_file; 107 108 static struct sx kld_sx; /* kernel linker lock */ 109 110 /* 111 * Load counter used by clients to determine if a linker file has been 112 * re-loaded. This counter is incremented for each file load. 113 */ 114 static int loadcnt; 115 116 static linker_class_list_t classes; 117 static linker_file_list_t linker_files; 118 static int next_file_id = 1; 119 static int linker_no_more_classes = 0; 120 121 #define LINKER_GET_NEXT_FILE_ID(a) do { \ 122 linker_file_t lftmp; \ 123 \ 124 if (!cold) \ 125 sx_assert(&kld_sx, SA_XLOCKED); \ 126 retry: \ 127 TAILQ_FOREACH(lftmp, &linker_files, link) { \ 128 if (next_file_id == lftmp->id) { \ 129 next_file_id++; \ 130 goto retry; \ 131 } \ 132 } \ 133 (a) = next_file_id; \ 134 } while(0) 135 136 /* XXX wrong name; we're looking at version provision tags here, not modules */ 137 typedef TAILQ_HEAD(, modlist) modlisthead_t; 138 struct modlist { 139 TAILQ_ENTRY(modlist) link; /* chain together all modules */ 140 linker_file_t container; 141 const char *name; 142 int version; 143 }; 144 typedef struct modlist *modlist_t; 145 static modlisthead_t found_modules; 146 147 static int linker_file_add_dependency(linker_file_t file, 148 linker_file_t dep); 149 static caddr_t linker_file_lookup_symbol_internal(linker_file_t file, 150 const char* name, int deps); 151 static int linker_load_module(const char *kldname, 152 const char *modname, struct linker_file *parent, 153 const struct mod_depend *verinfo, struct linker_file **lfpp); 154 static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo); 155 156 static void 157 linker_init(void *arg) 158 { 159 160 sx_init(&kld_sx, "kernel linker"); 161 TAILQ_INIT(&classes); 162 TAILQ_INIT(&linker_files); 163 } 164 165 SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL); 166 167 static void 168 linker_stop_class_add(void *arg) 169 { 170 171 linker_no_more_classes = 1; 172 } 173 174 SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL); 175 176 int 177 linker_add_class(linker_class_t lc) 178 { 179 180 /* 181 * We disallow any class registration past SI_ORDER_ANY 182 * of SI_SUB_KLD. We bump the reference count to keep the 183 * ops from being freed. 184 */ 185 if (linker_no_more_classes == 1) 186 return (EPERM); 187 kobj_class_compile((kobj_class_t) lc); 188 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */ 189 TAILQ_INSERT_TAIL(&classes, lc, link); 190 return (0); 191 } 192 193 static void 194 linker_file_sysinit(linker_file_t lf) 195 { 196 struct sysinit **start, **stop, **sipp, **xipp, *save; 197 198 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 199 lf->filename)); 200 201 sx_assert(&kld_sx, SA_XLOCKED); 202 203 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 204 return; 205 /* 206 * Perform a bubble sort of the system initialization objects by 207 * their subsystem (primary key) and order (secondary key). 208 * 209 * Since some things care about execution order, this is the operation 210 * which ensures continued function. 211 */ 212 for (sipp = start; sipp < stop; sipp++) { 213 for (xipp = sipp + 1; xipp < stop; xipp++) { 214 if ((*sipp)->subsystem < (*xipp)->subsystem || 215 ((*sipp)->subsystem == (*xipp)->subsystem && 216 (*sipp)->order <= (*xipp)->order)) 217 continue; /* skip */ 218 save = *sipp; 219 *sipp = *xipp; 220 *xipp = save; 221 } 222 } 223 224 /* 225 * Traverse the (now) ordered list of system initialization tasks. 226 * Perform each task, and continue on to the next task. 227 */ 228 sx_xunlock(&kld_sx); 229 mtx_lock(&Giant); 230 for (sipp = start; sipp < stop; sipp++) { 231 if ((*sipp)->subsystem == SI_SUB_DUMMY) 232 continue; /* skip dummy task(s) */ 233 234 /* Call function */ 235 (*((*sipp)->func)) ((*sipp)->udata); 236 } 237 mtx_unlock(&Giant); 238 sx_xlock(&kld_sx); 239 } 240 241 static void 242 linker_file_sysuninit(linker_file_t lf) 243 { 244 struct sysinit **start, **stop, **sipp, **xipp, *save; 245 246 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 247 lf->filename)); 248 249 sx_assert(&kld_sx, SA_XLOCKED); 250 251 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, 252 NULL) != 0) 253 return; 254 255 /* 256 * Perform a reverse bubble sort of the system initialization objects 257 * by their subsystem (primary key) and order (secondary key). 258 * 259 * Since some things care about execution order, this is the operation 260 * which ensures continued function. 261 */ 262 for (sipp = start; sipp < stop; sipp++) { 263 for (xipp = sipp + 1; xipp < stop; xipp++) { 264 if ((*sipp)->subsystem > (*xipp)->subsystem || 265 ((*sipp)->subsystem == (*xipp)->subsystem && 266 (*sipp)->order >= (*xipp)->order)) 267 continue; /* skip */ 268 save = *sipp; 269 *sipp = *xipp; 270 *xipp = save; 271 } 272 } 273 274 /* 275 * Traverse the (now) ordered list of system initialization tasks. 276 * Perform each task, and continue on to the next task. 277 */ 278 sx_xunlock(&kld_sx); 279 mtx_lock(&Giant); 280 for (sipp = start; sipp < stop; sipp++) { 281 if ((*sipp)->subsystem == SI_SUB_DUMMY) 282 continue; /* skip dummy task(s) */ 283 284 /* Call function */ 285 (*((*sipp)->func)) ((*sipp)->udata); 286 } 287 mtx_unlock(&Giant); 288 sx_xlock(&kld_sx); 289 } 290 291 static void 292 linker_file_register_sysctls(linker_file_t lf, bool enable) 293 { 294 struct sysctl_oid **start, **stop, **oidp; 295 296 KLD_DPF(FILE, 297 ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 298 lf->filename)); 299 300 sx_assert(&kld_sx, SA_XLOCKED); 301 302 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 303 return; 304 305 sx_xunlock(&kld_sx); 306 sysctl_wlock(); 307 for (oidp = start; oidp < stop; oidp++) { 308 if (enable) 309 sysctl_register_oid(*oidp); 310 else 311 sysctl_register_disabled_oid(*oidp); 312 } 313 sysctl_wunlock(); 314 sx_xlock(&kld_sx); 315 } 316 317 static void 318 linker_file_enable_sysctls(linker_file_t lf) 319 { 320 struct sysctl_oid **start, **stop, **oidp; 321 322 KLD_DPF(FILE, 323 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n", 324 lf->filename)); 325 326 sx_assert(&kld_sx, SA_XLOCKED); 327 328 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 329 return; 330 331 sx_xunlock(&kld_sx); 332 sysctl_wlock(); 333 for (oidp = start; oidp < stop; oidp++) 334 sysctl_enable_oid(*oidp); 335 sysctl_wunlock(); 336 sx_xlock(&kld_sx); 337 } 338 339 static void 340 linker_file_unregister_sysctls(linker_file_t lf) 341 { 342 struct sysctl_oid **start, **stop, **oidp; 343 344 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs" 345 " for %s\n", lf->filename)); 346 347 sx_assert(&kld_sx, SA_XLOCKED); 348 349 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 350 return; 351 352 sx_xunlock(&kld_sx); 353 sysctl_wlock(); 354 for (oidp = start; oidp < stop; oidp++) 355 sysctl_unregister_oid(*oidp); 356 sysctl_wunlock(); 357 sx_xlock(&kld_sx); 358 } 359 360 static int 361 linker_file_register_modules(linker_file_t lf) 362 { 363 struct mod_metadata **start, **stop, **mdp; 364 const moduledata_t *moddata; 365 int first_error, error; 366 367 KLD_DPF(FILE, ("linker_file_register_modules: registering modules" 368 " in %s\n", lf->filename)); 369 370 sx_assert(&kld_sx, SA_XLOCKED); 371 372 if (linker_file_lookup_set(lf, "modmetadata_set", &start, 373 &stop, NULL) != 0) { 374 /* 375 * This fallback should be unnecessary, but if we get booted 376 * from boot2 instead of loader and we are missing our 377 * metadata then we have to try the best we can. 378 */ 379 if (lf == linker_kernel_file) { 380 start = SET_BEGIN(modmetadata_set); 381 stop = SET_LIMIT(modmetadata_set); 382 } else 383 return (0); 384 } 385 first_error = 0; 386 for (mdp = start; mdp < stop; mdp++) { 387 if ((*mdp)->md_type != MDT_MODULE) 388 continue; 389 moddata = (*mdp)->md_data; 390 KLD_DPF(FILE, ("Registering module %s in %s\n", 391 moddata->name, lf->filename)); 392 error = module_register(moddata, lf); 393 if (error) { 394 printf("Module %s failed to register: %d\n", 395 moddata->name, error); 396 if (first_error == 0) 397 first_error = error; 398 } 399 } 400 return (first_error); 401 } 402 403 static void 404 linker_init_kernel_modules(void) 405 { 406 407 sx_xlock(&kld_sx); 408 linker_file_register_modules(linker_kernel_file); 409 sx_xunlock(&kld_sx); 410 } 411 412 SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 413 NULL); 414 415 static int 416 linker_load_file(const char *filename, linker_file_t *result) 417 { 418 linker_class_t lc; 419 linker_file_t lf; 420 int foundfile, error, modules; 421 422 /* Refuse to load modules if securelevel raised */ 423 if (prison0.pr_securelevel > 0) 424 return (EPERM); 425 426 sx_assert(&kld_sx, SA_XLOCKED); 427 lf = linker_find_file_by_name(filename); 428 if (lf) { 429 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded," 430 " incrementing refs\n", filename)); 431 *result = lf; 432 lf->refs++; 433 return (0); 434 } 435 foundfile = 0; 436 error = 0; 437 438 /* 439 * We do not need to protect (lock) classes here because there is 440 * no class registration past startup (SI_SUB_KLD, SI_ORDER_ANY) 441 * and there is no class deregistration mechanism at this time. 442 */ 443 TAILQ_FOREACH(lc, &classes, link) { 444 KLD_DPF(FILE, ("linker_load_file: trying to load %s\n", 445 filename)); 446 error = LINKER_LOAD_FILE(lc, filename, &lf); 447 /* 448 * If we got something other than ENOENT, then it exists but 449 * we cannot load it for some other reason. 450 */ 451 if (error != ENOENT) 452 foundfile = 1; 453 if (lf) { 454 error = linker_file_register_modules(lf); 455 if (error == EEXIST) { 456 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 457 return (error); 458 } 459 modules = !TAILQ_EMPTY(&lf->modules); 460 linker_file_register_sysctls(lf, false); 461 linker_file_sysinit(lf); 462 lf->flags |= LINKER_FILE_LINKED; 463 464 /* 465 * If all of the modules in this file failed 466 * to load, unload the file and return an 467 * error of ENOEXEC. 468 */ 469 if (modules && TAILQ_EMPTY(&lf->modules)) { 470 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 471 return (ENOEXEC); 472 } 473 linker_file_enable_sysctls(lf); 474 EVENTHANDLER_INVOKE(kld_load, lf); 475 *result = lf; 476 return (0); 477 } 478 } 479 /* 480 * Less than ideal, but tells the user whether it failed to load or 481 * the module was not found. 482 */ 483 if (foundfile) { 484 485 /* 486 * If the file type has not been recognized by the last try 487 * printout a message before to fail. 488 */ 489 if (error == ENOSYS) 490 printf("%s: %s - unsupported file type\n", 491 __func__, filename); 492 493 /* 494 * Format not recognized or otherwise unloadable. 495 * When loading a module that is statically built into 496 * the kernel EEXIST percolates back up as the return 497 * value. Preserve this so that apps like sysinstall 498 * can recognize this special case and not post bogus 499 * dialog boxes. 500 */ 501 if (error != EEXIST) 502 error = ENOEXEC; 503 } else 504 error = ENOENT; /* Nothing found */ 505 return (error); 506 } 507 508 int 509 linker_reference_module(const char *modname, struct mod_depend *verinfo, 510 linker_file_t *result) 511 { 512 modlist_t mod; 513 int error; 514 515 sx_xlock(&kld_sx); 516 if ((mod = modlist_lookup2(modname, verinfo)) != NULL) { 517 *result = mod->container; 518 (*result)->refs++; 519 sx_xunlock(&kld_sx); 520 return (0); 521 } 522 523 error = linker_load_module(NULL, modname, NULL, verinfo, result); 524 sx_xunlock(&kld_sx); 525 return (error); 526 } 527 528 int 529 linker_release_module(const char *modname, struct mod_depend *verinfo, 530 linker_file_t lf) 531 { 532 modlist_t mod; 533 int error; 534 535 sx_xlock(&kld_sx); 536 if (lf == NULL) { 537 KASSERT(modname != NULL, 538 ("linker_release_module: no file or name")); 539 mod = modlist_lookup2(modname, verinfo); 540 if (mod == NULL) { 541 sx_xunlock(&kld_sx); 542 return (ESRCH); 543 } 544 lf = mod->container; 545 } else 546 KASSERT(modname == NULL && verinfo == NULL, 547 ("linker_release_module: both file and name")); 548 error = linker_file_unload(lf, LINKER_UNLOAD_NORMAL); 549 sx_xunlock(&kld_sx); 550 return (error); 551 } 552 553 static linker_file_t 554 linker_find_file_by_name(const char *filename) 555 { 556 linker_file_t lf; 557 char *koname; 558 559 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 560 sprintf(koname, "%s.ko", filename); 561 562 sx_assert(&kld_sx, SA_XLOCKED); 563 TAILQ_FOREACH(lf, &linker_files, link) { 564 if (strcmp(lf->filename, koname) == 0) 565 break; 566 if (strcmp(lf->filename, filename) == 0) 567 break; 568 } 569 free(koname, M_LINKER); 570 return (lf); 571 } 572 573 static linker_file_t 574 linker_find_file_by_id(int fileid) 575 { 576 linker_file_t lf; 577 578 sx_assert(&kld_sx, SA_XLOCKED); 579 TAILQ_FOREACH(lf, &linker_files, link) 580 if (lf->id == fileid && lf->flags & LINKER_FILE_LINKED) 581 break; 582 return (lf); 583 } 584 585 int 586 linker_file_foreach(linker_predicate_t *predicate, void *context) 587 { 588 linker_file_t lf; 589 int retval = 0; 590 591 sx_xlock(&kld_sx); 592 TAILQ_FOREACH(lf, &linker_files, link) { 593 retval = predicate(lf, context); 594 if (retval != 0) 595 break; 596 } 597 sx_xunlock(&kld_sx); 598 return (retval); 599 } 600 601 linker_file_t 602 linker_make_file(const char *pathname, linker_class_t lc) 603 { 604 linker_file_t lf; 605 const char *filename; 606 607 if (!cold) 608 sx_assert(&kld_sx, SA_XLOCKED); 609 filename = linker_basename(pathname); 610 611 KLD_DPF(FILE, ("linker_make_file: new file, filename='%s' for pathname='%s'\n", filename, pathname)); 612 lf = (linker_file_t)kobj_create((kobj_class_t)lc, M_LINKER, M_WAITOK); 613 if (lf == NULL) 614 return (NULL); 615 lf->ctors_addr = 0; 616 lf->ctors_size = 0; 617 lf->refs = 1; 618 lf->userrefs = 0; 619 lf->flags = 0; 620 lf->filename = strdup(filename, M_LINKER); 621 lf->pathname = strdup(pathname, M_LINKER); 622 LINKER_GET_NEXT_FILE_ID(lf->id); 623 lf->ndeps = 0; 624 lf->deps = NULL; 625 lf->loadcnt = ++loadcnt; 626 #ifdef __arm__ 627 lf->exidx_addr = 0; 628 lf->exidx_size = 0; 629 #endif 630 STAILQ_INIT(&lf->common); 631 TAILQ_INIT(&lf->modules); 632 TAILQ_INSERT_TAIL(&linker_files, lf, link); 633 return (lf); 634 } 635 636 int 637 linker_file_unload(linker_file_t file, int flags) 638 { 639 module_t mod, next; 640 modlist_t ml, nextml; 641 struct common_symbol *cp; 642 int error, i; 643 644 /* Refuse to unload modules if securelevel raised. */ 645 if (prison0.pr_securelevel > 0) 646 return (EPERM); 647 648 sx_assert(&kld_sx, SA_XLOCKED); 649 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 650 651 /* Easy case of just dropping a reference. */ 652 if (file->refs > 1) { 653 file->refs--; 654 return (0); 655 } 656 657 /* Give eventhandlers a chance to prevent the unload. */ 658 error = 0; 659 EVENTHANDLER_INVOKE(kld_unload_try, file, &error); 660 if (error != 0) 661 return (EBUSY); 662 663 KLD_DPF(FILE, ("linker_file_unload: file is unloading," 664 " informing modules\n")); 665 666 /* 667 * Quiesce all the modules to give them a chance to veto the unload. 668 */ 669 MOD_SLOCK; 670 for (mod = TAILQ_FIRST(&file->modules); mod; 671 mod = module_getfnext(mod)) { 672 673 error = module_quiesce(mod); 674 if (error != 0 && flags != LINKER_UNLOAD_FORCE) { 675 KLD_DPF(FILE, ("linker_file_unload: module %s" 676 " vetoed unload\n", module_getname(mod))); 677 /* 678 * XXX: Do we need to tell all the quiesced modules 679 * that they can resume work now via a new module 680 * event? 681 */ 682 MOD_SUNLOCK; 683 return (error); 684 } 685 } 686 MOD_SUNLOCK; 687 688 /* 689 * Inform any modules associated with this file that they are 690 * being unloaded. 691 */ 692 MOD_XLOCK; 693 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 694 next = module_getfnext(mod); 695 MOD_XUNLOCK; 696 697 /* 698 * Give the module a chance to veto the unload. 699 */ 700 if ((error = module_unload(mod)) != 0) { 701 #ifdef KLD_DEBUG 702 MOD_SLOCK; 703 KLD_DPF(FILE, ("linker_file_unload: module %s" 704 " failed unload\n", module_getname(mod))); 705 MOD_SUNLOCK; 706 #endif 707 return (error); 708 } 709 MOD_XLOCK; 710 module_release(mod); 711 } 712 MOD_XUNLOCK; 713 714 TAILQ_FOREACH_SAFE(ml, &found_modules, link, nextml) { 715 if (ml->container == file) { 716 TAILQ_REMOVE(&found_modules, ml, link); 717 free(ml, M_LINKER); 718 } 719 } 720 721 /* 722 * Don't try to run SYSUNINITs if we are unloaded due to a 723 * link error. 724 */ 725 if (file->flags & LINKER_FILE_LINKED) { 726 file->flags &= ~LINKER_FILE_LINKED; 727 linker_file_unregister_sysctls(file); 728 linker_file_sysuninit(file); 729 } 730 TAILQ_REMOVE(&linker_files, file, link); 731 732 if (file->deps) { 733 for (i = 0; i < file->ndeps; i++) 734 linker_file_unload(file->deps[i], flags); 735 free(file->deps, M_LINKER); 736 file->deps = NULL; 737 } 738 while ((cp = STAILQ_FIRST(&file->common)) != NULL) { 739 STAILQ_REMOVE_HEAD(&file->common, link); 740 free(cp, M_LINKER); 741 } 742 743 LINKER_UNLOAD(file); 744 745 EVENTHANDLER_INVOKE(kld_unload, file->filename, file->address, 746 file->size); 747 748 if (file->filename) { 749 free(file->filename, M_LINKER); 750 file->filename = NULL; 751 } 752 if (file->pathname) { 753 free(file->pathname, M_LINKER); 754 file->pathname = NULL; 755 } 756 kobj_delete((kobj_t) file, M_LINKER); 757 return (0); 758 } 759 760 int 761 linker_ctf_get(linker_file_t file, linker_ctf_t *lc) 762 { 763 return (LINKER_CTF_GET(file, lc)); 764 } 765 766 static int 767 linker_file_add_dependency(linker_file_t file, linker_file_t dep) 768 { 769 linker_file_t *newdeps; 770 771 sx_assert(&kld_sx, SA_XLOCKED); 772 file->deps = realloc(file->deps, (file->ndeps + 1) * sizeof(*newdeps), 773 M_LINKER, M_WAITOK | M_ZERO); 774 file->deps[file->ndeps] = dep; 775 file->ndeps++; 776 KLD_DPF(FILE, ("linker_file_add_dependency:" 777 " adding %s as dependency for %s\n", 778 dep->filename, file->filename)); 779 return (0); 780 } 781 782 /* 783 * Locate a linker set and its contents. This is a helper function to avoid 784 * linker_if.h exposure elsewhere. Note: firstp and lastp are really void **. 785 * This function is used in this file so we can avoid having lots of (void **) 786 * casts. 787 */ 788 int 789 linker_file_lookup_set(linker_file_t file, const char *name, 790 void *firstp, void *lastp, int *countp) 791 { 792 793 sx_assert(&kld_sx, SA_LOCKED); 794 return (LINKER_LOOKUP_SET(file, name, firstp, lastp, countp)); 795 } 796 797 /* 798 * List all functions in a file. 799 */ 800 int 801 linker_file_function_listall(linker_file_t lf, 802 linker_function_nameval_callback_t callback_func, void *arg) 803 { 804 return (LINKER_EACH_FUNCTION_NAMEVAL(lf, callback_func, arg)); 805 } 806 807 caddr_t 808 linker_file_lookup_symbol(linker_file_t file, const char *name, int deps) 809 { 810 caddr_t sym; 811 int locked; 812 813 locked = sx_xlocked(&kld_sx); 814 if (!locked) 815 sx_xlock(&kld_sx); 816 sym = linker_file_lookup_symbol_internal(file, name, deps); 817 if (!locked) 818 sx_xunlock(&kld_sx); 819 return (sym); 820 } 821 822 static caddr_t 823 linker_file_lookup_symbol_internal(linker_file_t file, const char *name, 824 int deps) 825 { 826 c_linker_sym_t sym; 827 linker_symval_t symval; 828 caddr_t address; 829 size_t common_size = 0; 830 int i; 831 832 sx_assert(&kld_sx, SA_XLOCKED); 833 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", 834 file, name, deps)); 835 836 if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { 837 LINKER_SYMBOL_VALUES(file, sym, &symval); 838 if (symval.value == 0) 839 /* 840 * For commons, first look them up in the 841 * dependencies and only allocate space if not found 842 * there. 843 */ 844 common_size = symval.size; 845 else { 846 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol" 847 ".value=%p\n", symval.value)); 848 return (symval.value); 849 } 850 } 851 if (deps) { 852 for (i = 0; i < file->ndeps; i++) { 853 address = linker_file_lookup_symbol_internal( 854 file->deps[i], name, 0); 855 if (address) { 856 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 857 " deps value=%p\n", address)); 858 return (address); 859 } 860 } 861 } 862 if (common_size > 0) { 863 /* 864 * This is a common symbol which was not found in the 865 * dependencies. We maintain a simple common symbol table in 866 * the file object. 867 */ 868 struct common_symbol *cp; 869 870 STAILQ_FOREACH(cp, &file->common, link) { 871 if (strcmp(cp->name, name) == 0) { 872 KLD_DPF(SYM, ("linker_file_lookup_symbol:" 873 " old common value=%p\n", cp->address)); 874 return (cp->address); 875 } 876 } 877 /* 878 * Round the symbol size up to align. 879 */ 880 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 881 cp = malloc(sizeof(struct common_symbol) 882 + common_size + strlen(name) + 1, M_LINKER, 883 M_WAITOK | M_ZERO); 884 cp->address = (caddr_t)(cp + 1); 885 cp->name = cp->address + common_size; 886 strcpy(cp->name, name); 887 bzero(cp->address, common_size); 888 STAILQ_INSERT_TAIL(&file->common, cp, link); 889 890 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common" 891 " value=%p\n", cp->address)); 892 return (cp->address); 893 } 894 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 895 return (0); 896 } 897 898 /* 899 * Both DDB and stack(9) rely on the kernel linker to provide forward and 900 * backward lookup of symbols. However, DDB and sometimes stack(9) need to 901 * do this in a lockfree manner. We provide a set of internal helper 902 * routines to perform these operations without locks, and then wrappers that 903 * optionally lock. 904 * 905 * linker_debug_lookup() is ifdef DDB as currently it's only used by DDB. 906 */ 907 #ifdef DDB 908 static int 909 linker_debug_lookup(const char *symstr, c_linker_sym_t *sym) 910 { 911 linker_file_t lf; 912 913 TAILQ_FOREACH(lf, &linker_files, link) { 914 if (LINKER_LOOKUP_SYMBOL(lf, symstr, sym) == 0) 915 return (0); 916 } 917 return (ENOENT); 918 } 919 #endif 920 921 static int 922 linker_debug_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 923 { 924 linker_file_t lf; 925 c_linker_sym_t best, es; 926 u_long diff, bestdiff, off; 927 928 best = 0; 929 off = (uintptr_t)value; 930 bestdiff = off; 931 TAILQ_FOREACH(lf, &linker_files, link) { 932 if (LINKER_SEARCH_SYMBOL(lf, value, &es, &diff) != 0) 933 continue; 934 if (es != 0 && diff < bestdiff) { 935 best = es; 936 bestdiff = diff; 937 } 938 if (bestdiff == 0) 939 break; 940 } 941 if (best) { 942 *sym = best; 943 *diffp = bestdiff; 944 return (0); 945 } else { 946 *sym = 0; 947 *diffp = off; 948 return (ENOENT); 949 } 950 } 951 952 static int 953 linker_debug_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 954 { 955 linker_file_t lf; 956 957 TAILQ_FOREACH(lf, &linker_files, link) { 958 if (LINKER_SYMBOL_VALUES(lf, sym, symval) == 0) 959 return (0); 960 } 961 return (ENOENT); 962 } 963 964 static int 965 linker_debug_search_symbol_name(caddr_t value, char *buf, u_int buflen, 966 long *offset) 967 { 968 linker_symval_t symval; 969 c_linker_sym_t sym; 970 int error; 971 972 *offset = 0; 973 error = linker_debug_search_symbol(value, &sym, offset); 974 if (error) 975 return (error); 976 error = linker_debug_symbol_values(sym, &symval); 977 if (error) 978 return (error); 979 strlcpy(buf, symval.name, buflen); 980 return (0); 981 } 982 983 /* 984 * DDB Helpers. DDB has to look across multiple files with their own symbol 985 * tables and string tables. 986 * 987 * Note that we do not obey list locking protocols here. We really don't need 988 * DDB to hang because somebody's got the lock held. We'll take the chance 989 * that the files list is inconsistent instead. 990 */ 991 #ifdef DDB 992 int 993 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 994 { 995 996 return (linker_debug_lookup(symstr, sym)); 997 } 998 #endif 999 1000 int 1001 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 1002 { 1003 1004 return (linker_debug_search_symbol(value, sym, diffp)); 1005 } 1006 1007 int 1008 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 1009 { 1010 1011 return (linker_debug_symbol_values(sym, symval)); 1012 } 1013 1014 int 1015 linker_ddb_search_symbol_name(caddr_t value, char *buf, u_int buflen, 1016 long *offset) 1017 { 1018 1019 return (linker_debug_search_symbol_name(value, buf, buflen, offset)); 1020 } 1021 1022 /* 1023 * stack(9) helper for non-debugging environemnts. Unlike DDB helpers, we do 1024 * obey locking protocols, and offer a significantly less complex interface. 1025 */ 1026 int 1027 linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen, 1028 long *offset, int flags) 1029 { 1030 int error; 1031 1032 KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 && 1033 (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK), 1034 ("%s: bad flags: 0x%x", __func__, flags)); 1035 1036 if (flags & M_NOWAIT) { 1037 if (!sx_try_slock(&kld_sx)) 1038 return (EWOULDBLOCK); 1039 } else 1040 sx_slock(&kld_sx); 1041 1042 error = linker_debug_search_symbol_name(value, buf, buflen, offset); 1043 sx_sunlock(&kld_sx); 1044 return (error); 1045 } 1046 1047 int 1048 linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, 1049 long *offset) 1050 { 1051 1052 return (linker_search_symbol_name_flags(value, buf, buflen, offset, 1053 M_WAITOK)); 1054 } 1055 1056 /* 1057 * Syscalls. 1058 */ 1059 int 1060 kern_kldload(struct thread *td, const char *file, int *fileid) 1061 { 1062 const char *kldname, *modname; 1063 linker_file_t lf; 1064 int error; 1065 1066 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1067 return (error); 1068 1069 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0) 1070 return (error); 1071 1072 /* 1073 * It is possible that kldloaded module will attach a new ifnet, 1074 * so vnet context must be set when this ocurs. 1075 */ 1076 CURVNET_SET(TD_TO_VNET(td)); 1077 1078 /* 1079 * If file does not contain a qualified name or any dot in it 1080 * (kldname.ko, or kldname.ver.ko) treat it as an interface 1081 * name. 1082 */ 1083 if (strchr(file, '/') || strchr(file, '.')) { 1084 kldname = file; 1085 modname = NULL; 1086 } else { 1087 kldname = NULL; 1088 modname = file; 1089 } 1090 1091 sx_xlock(&kld_sx); 1092 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 1093 if (error) { 1094 sx_xunlock(&kld_sx); 1095 goto done; 1096 } 1097 lf->userrefs++; 1098 if (fileid != NULL) 1099 *fileid = lf->id; 1100 sx_xunlock(&kld_sx); 1101 1102 done: 1103 CURVNET_RESTORE(); 1104 return (error); 1105 } 1106 1107 int 1108 sys_kldload(struct thread *td, struct kldload_args *uap) 1109 { 1110 char *pathname = NULL; 1111 int error, fileid; 1112 1113 td->td_retval[0] = -1; 1114 1115 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1116 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 1117 if (error == 0) { 1118 error = kern_kldload(td, pathname, &fileid); 1119 if (error == 0) 1120 td->td_retval[0] = fileid; 1121 } 1122 free(pathname, M_TEMP); 1123 return (error); 1124 } 1125 1126 int 1127 kern_kldunload(struct thread *td, int fileid, int flags) 1128 { 1129 linker_file_t lf; 1130 int error = 0; 1131 1132 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1133 return (error); 1134 1135 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 1136 return (error); 1137 1138 CURVNET_SET(TD_TO_VNET(td)); 1139 sx_xlock(&kld_sx); 1140 lf = linker_find_file_by_id(fileid); 1141 if (lf) { 1142 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 1143 1144 if (lf->userrefs == 0) { 1145 /* 1146 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 1147 */ 1148 printf("kldunload: attempt to unload file that was" 1149 " loaded by the kernel\n"); 1150 error = EBUSY; 1151 } else { 1152 lf->userrefs--; 1153 error = linker_file_unload(lf, flags); 1154 if (error) 1155 lf->userrefs++; 1156 } 1157 } else 1158 error = ENOENT; 1159 sx_xunlock(&kld_sx); 1160 1161 CURVNET_RESTORE(); 1162 return (error); 1163 } 1164 1165 int 1166 sys_kldunload(struct thread *td, struct kldunload_args *uap) 1167 { 1168 1169 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 1170 } 1171 1172 int 1173 sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap) 1174 { 1175 1176 if (uap->flags != LINKER_UNLOAD_NORMAL && 1177 uap->flags != LINKER_UNLOAD_FORCE) 1178 return (EINVAL); 1179 return (kern_kldunload(td, uap->fileid, uap->flags)); 1180 } 1181 1182 int 1183 sys_kldfind(struct thread *td, struct kldfind_args *uap) 1184 { 1185 char *pathname; 1186 const char *filename; 1187 linker_file_t lf; 1188 int error; 1189 1190 #ifdef MAC 1191 error = mac_kld_check_stat(td->td_ucred); 1192 if (error) 1193 return (error); 1194 #endif 1195 1196 td->td_retval[0] = -1; 1197 1198 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1199 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 1200 goto out; 1201 1202 filename = linker_basename(pathname); 1203 sx_xlock(&kld_sx); 1204 lf = linker_find_file_by_name(filename); 1205 if (lf) 1206 td->td_retval[0] = lf->id; 1207 else 1208 error = ENOENT; 1209 sx_xunlock(&kld_sx); 1210 out: 1211 free(pathname, M_TEMP); 1212 return (error); 1213 } 1214 1215 int 1216 sys_kldnext(struct thread *td, struct kldnext_args *uap) 1217 { 1218 linker_file_t lf; 1219 int error = 0; 1220 1221 #ifdef MAC 1222 error = mac_kld_check_stat(td->td_ucred); 1223 if (error) 1224 return (error); 1225 #endif 1226 1227 sx_xlock(&kld_sx); 1228 if (uap->fileid == 0) 1229 lf = TAILQ_FIRST(&linker_files); 1230 else { 1231 lf = linker_find_file_by_id(uap->fileid); 1232 if (lf == NULL) { 1233 error = ENOENT; 1234 goto out; 1235 } 1236 lf = TAILQ_NEXT(lf, link); 1237 } 1238 1239 /* Skip partially loaded files. */ 1240 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) 1241 lf = TAILQ_NEXT(lf, link); 1242 1243 if (lf) 1244 td->td_retval[0] = lf->id; 1245 else 1246 td->td_retval[0] = 0; 1247 out: 1248 sx_xunlock(&kld_sx); 1249 return (error); 1250 } 1251 1252 int 1253 sys_kldstat(struct thread *td, struct kldstat_args *uap) 1254 { 1255 struct kld_file_stat *stat; 1256 int error, version; 1257 1258 /* 1259 * Check the version of the user's structure. 1260 */ 1261 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) 1262 != 0) 1263 return (error); 1264 if (version != sizeof(struct kld_file_stat_1) && 1265 version != sizeof(struct kld_file_stat)) 1266 return (EINVAL); 1267 1268 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO); 1269 error = kern_kldstat(td, uap->fileid, stat); 1270 if (error == 0) 1271 error = copyout(stat, uap->stat, version); 1272 free(stat, M_TEMP); 1273 return (error); 1274 } 1275 1276 int 1277 kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat) 1278 { 1279 linker_file_t lf; 1280 int namelen; 1281 #ifdef MAC 1282 int error; 1283 1284 error = mac_kld_check_stat(td->td_ucred); 1285 if (error) 1286 return (error); 1287 #endif 1288 1289 sx_xlock(&kld_sx); 1290 lf = linker_find_file_by_id(fileid); 1291 if (lf == NULL) { 1292 sx_xunlock(&kld_sx); 1293 return (ENOENT); 1294 } 1295 1296 /* Version 1 fields: */ 1297 namelen = strlen(lf->filename) + 1; 1298 if (namelen > sizeof(stat->name)) 1299 namelen = sizeof(stat->name); 1300 bcopy(lf->filename, &stat->name[0], namelen); 1301 stat->refs = lf->refs; 1302 stat->id = lf->id; 1303 stat->address = lf->address; 1304 stat->size = lf->size; 1305 /* Version 2 fields: */ 1306 namelen = strlen(lf->pathname) + 1; 1307 if (namelen > sizeof(stat->pathname)) 1308 namelen = sizeof(stat->pathname); 1309 bcopy(lf->pathname, &stat->pathname[0], namelen); 1310 sx_xunlock(&kld_sx); 1311 1312 td->td_retval[0] = 0; 1313 return (0); 1314 } 1315 1316 #ifdef DDB 1317 DB_COMMAND(kldstat, db_kldstat) 1318 { 1319 linker_file_t lf; 1320 1321 #define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 1322 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1323 #undef POINTER_WIDTH 1324 TAILQ_FOREACH(lf, &linker_files, link) { 1325 if (db_pager_quit) 1326 return; 1327 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs, 1328 lf->address, lf->size, lf->filename); 1329 } 1330 } 1331 #endif /* DDB */ 1332 1333 int 1334 sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1335 { 1336 linker_file_t lf; 1337 module_t mp; 1338 int error = 0; 1339 1340 #ifdef MAC 1341 error = mac_kld_check_stat(td->td_ucred); 1342 if (error) 1343 return (error); 1344 #endif 1345 1346 sx_xlock(&kld_sx); 1347 lf = linker_find_file_by_id(uap->fileid); 1348 if (lf) { 1349 MOD_SLOCK; 1350 mp = TAILQ_FIRST(&lf->modules); 1351 if (mp != NULL) 1352 td->td_retval[0] = module_getid(mp); 1353 else 1354 td->td_retval[0] = 0; 1355 MOD_SUNLOCK; 1356 } else 1357 error = ENOENT; 1358 sx_xunlock(&kld_sx); 1359 return (error); 1360 } 1361 1362 int 1363 sys_kldsym(struct thread *td, struct kldsym_args *uap) 1364 { 1365 char *symstr = NULL; 1366 c_linker_sym_t sym; 1367 linker_symval_t symval; 1368 linker_file_t lf; 1369 struct kld_sym_lookup lookup; 1370 int error = 0; 1371 1372 #ifdef MAC 1373 error = mac_kld_check_stat(td->td_ucred); 1374 if (error) 1375 return (error); 1376 #endif 1377 1378 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1379 return (error); 1380 if (lookup.version != sizeof(lookup) || 1381 uap->cmd != KLDSYM_LOOKUP) 1382 return (EINVAL); 1383 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1384 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1385 goto out; 1386 sx_xlock(&kld_sx); 1387 if (uap->fileid != 0) { 1388 lf = linker_find_file_by_id(uap->fileid); 1389 if (lf == NULL) 1390 error = ENOENT; 1391 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1392 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1393 lookup.symvalue = (uintptr_t) symval.value; 1394 lookup.symsize = symval.size; 1395 error = copyout(&lookup, uap->data, sizeof(lookup)); 1396 } else 1397 error = ENOENT; 1398 } else { 1399 TAILQ_FOREACH(lf, &linker_files, link) { 1400 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1401 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1402 lookup.symvalue = (uintptr_t)symval.value; 1403 lookup.symsize = symval.size; 1404 error = copyout(&lookup, uap->data, 1405 sizeof(lookup)); 1406 break; 1407 } 1408 } 1409 if (lf == NULL) 1410 error = ENOENT; 1411 } 1412 sx_xunlock(&kld_sx); 1413 out: 1414 free(symstr, M_TEMP); 1415 return (error); 1416 } 1417 1418 /* 1419 * Preloaded module support 1420 */ 1421 1422 static modlist_t 1423 modlist_lookup(const char *name, int ver) 1424 { 1425 modlist_t mod; 1426 1427 TAILQ_FOREACH(mod, &found_modules, link) { 1428 if (strcmp(mod->name, name) == 0 && 1429 (ver == 0 || mod->version == ver)) 1430 return (mod); 1431 } 1432 return (NULL); 1433 } 1434 1435 static modlist_t 1436 modlist_lookup2(const char *name, const struct mod_depend *verinfo) 1437 { 1438 modlist_t mod, bestmod; 1439 int ver; 1440 1441 if (verinfo == NULL) 1442 return (modlist_lookup(name, 0)); 1443 bestmod = NULL; 1444 TAILQ_FOREACH(mod, &found_modules, link) { 1445 if (strcmp(mod->name, name) != 0) 1446 continue; 1447 ver = mod->version; 1448 if (ver == verinfo->md_ver_preferred) 1449 return (mod); 1450 if (ver >= verinfo->md_ver_minimum && 1451 ver <= verinfo->md_ver_maximum && 1452 (bestmod == NULL || ver > bestmod->version)) 1453 bestmod = mod; 1454 } 1455 return (bestmod); 1456 } 1457 1458 static modlist_t 1459 modlist_newmodule(const char *modname, int version, linker_file_t container) 1460 { 1461 modlist_t mod; 1462 1463 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1464 if (mod == NULL) 1465 panic("no memory for module list"); 1466 mod->container = container; 1467 mod->name = modname; 1468 mod->version = version; 1469 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1470 return (mod); 1471 } 1472 1473 static void 1474 linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1475 struct mod_metadata **stop, int preload) 1476 { 1477 struct mod_metadata *mp, **mdp; 1478 const char *modname; 1479 int ver; 1480 1481 for (mdp = start; mdp < stop; mdp++) { 1482 mp = *mdp; 1483 if (mp->md_type != MDT_VERSION) 1484 continue; 1485 modname = mp->md_cval; 1486 ver = ((const struct mod_version *)mp->md_data)->mv_version; 1487 if (modlist_lookup(modname, ver) != NULL) { 1488 printf("module %s already present!\n", modname); 1489 /* XXX what can we do? this is a build error. :-( */ 1490 continue; 1491 } 1492 modlist_newmodule(modname, ver, lf); 1493 } 1494 } 1495 1496 static void 1497 linker_preload(void *arg) 1498 { 1499 caddr_t modptr; 1500 const char *modname, *nmodname; 1501 char *modtype; 1502 linker_file_t lf, nlf; 1503 linker_class_t lc; 1504 int error; 1505 linker_file_list_t loaded_files; 1506 linker_file_list_t depended_files; 1507 struct mod_metadata *mp, *nmp; 1508 struct mod_metadata **start, **stop, **mdp, **nmdp; 1509 const struct mod_depend *verinfo; 1510 int nver; 1511 int resolves; 1512 modlist_t mod; 1513 struct sysinit **si_start, **si_stop; 1514 1515 TAILQ_INIT(&loaded_files); 1516 TAILQ_INIT(&depended_files); 1517 TAILQ_INIT(&found_modules); 1518 error = 0; 1519 1520 modptr = NULL; 1521 sx_xlock(&kld_sx); 1522 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1523 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1524 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1525 if (modname == NULL) { 1526 printf("Preloaded module at %p does not have a" 1527 " name!\n", modptr); 1528 continue; 1529 } 1530 if (modtype == NULL) { 1531 printf("Preloaded module at %p does not have a type!\n", 1532 modptr); 1533 continue; 1534 } 1535 if (bootverbose) 1536 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1537 modptr); 1538 lf = NULL; 1539 TAILQ_FOREACH(lc, &classes, link) { 1540 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1541 if (!error) 1542 break; 1543 lf = NULL; 1544 } 1545 if (lf) 1546 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1547 } 1548 1549 /* 1550 * First get a list of stuff in the kernel. 1551 */ 1552 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1553 &stop, NULL) == 0) 1554 linker_addmodules(linker_kernel_file, start, stop, 1); 1555 1556 /* 1557 * This is a once-off kinky bubble sort to resolve relocation 1558 * dependency requirements. 1559 */ 1560 restart: 1561 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1562 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1563 &stop, NULL); 1564 /* 1565 * First, look to see if we would successfully link with this 1566 * stuff. 1567 */ 1568 resolves = 1; /* unless we know otherwise */ 1569 if (!error) { 1570 for (mdp = start; mdp < stop; mdp++) { 1571 mp = *mdp; 1572 if (mp->md_type != MDT_DEPEND) 1573 continue; 1574 modname = mp->md_cval; 1575 verinfo = mp->md_data; 1576 for (nmdp = start; nmdp < stop; nmdp++) { 1577 nmp = *nmdp; 1578 if (nmp->md_type != MDT_VERSION) 1579 continue; 1580 nmodname = nmp->md_cval; 1581 if (strcmp(modname, nmodname) == 0) 1582 break; 1583 } 1584 if (nmdp < stop) /* it's a self reference */ 1585 continue; 1586 1587 /* 1588 * ok, the module isn't here yet, we 1589 * are not finished 1590 */ 1591 if (modlist_lookup2(modname, verinfo) == NULL) 1592 resolves = 0; 1593 } 1594 } 1595 /* 1596 * OK, if we found our modules, we can link. So, "provide" 1597 * the modules inside and add it to the end of the link order 1598 * list. 1599 */ 1600 if (resolves) { 1601 if (!error) { 1602 for (mdp = start; mdp < stop; mdp++) { 1603 mp = *mdp; 1604 if (mp->md_type != MDT_VERSION) 1605 continue; 1606 modname = mp->md_cval; 1607 nver = ((const struct mod_version *) 1608 mp->md_data)->mv_version; 1609 if (modlist_lookup(modname, 1610 nver) != NULL) { 1611 printf("module %s already" 1612 " present!\n", modname); 1613 TAILQ_REMOVE(&loaded_files, 1614 lf, loaded); 1615 linker_file_unload(lf, 1616 LINKER_UNLOAD_FORCE); 1617 /* we changed tailq next ptr */ 1618 goto restart; 1619 } 1620 modlist_newmodule(modname, nver, lf); 1621 } 1622 } 1623 TAILQ_REMOVE(&loaded_files, lf, loaded); 1624 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1625 /* 1626 * Since we provided modules, we need to restart the 1627 * sort so that the previous files that depend on us 1628 * have a chance. Also, we've busted the tailq next 1629 * pointer with the REMOVE. 1630 */ 1631 goto restart; 1632 } 1633 } 1634 1635 /* 1636 * At this point, we check to see what could not be resolved.. 1637 */ 1638 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1639 TAILQ_REMOVE(&loaded_files, lf, loaded); 1640 printf("KLD file %s is missing dependencies\n", lf->filename); 1641 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1642 } 1643 1644 /* 1645 * We made it. Finish off the linking in the order we determined. 1646 */ 1647 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1648 if (linker_kernel_file) { 1649 linker_kernel_file->refs++; 1650 error = linker_file_add_dependency(lf, 1651 linker_kernel_file); 1652 if (error) 1653 panic("cannot add dependency"); 1654 } 1655 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1656 &stop, NULL); 1657 if (!error) { 1658 for (mdp = start; mdp < stop; mdp++) { 1659 mp = *mdp; 1660 if (mp->md_type != MDT_DEPEND) 1661 continue; 1662 modname = mp->md_cval; 1663 verinfo = mp->md_data; 1664 mod = modlist_lookup2(modname, verinfo); 1665 if (mod == NULL) { 1666 printf("KLD file %s - cannot find " 1667 "dependency \"%s\"\n", 1668 lf->filename, modname); 1669 goto fail; 1670 } 1671 /* Don't count self-dependencies */ 1672 if (lf == mod->container) 1673 continue; 1674 mod->container->refs++; 1675 error = linker_file_add_dependency(lf, 1676 mod->container); 1677 if (error) 1678 panic("cannot add dependency"); 1679 } 1680 } 1681 /* 1682 * Now do relocation etc using the symbol search paths 1683 * established by the dependencies 1684 */ 1685 error = LINKER_LINK_PRELOAD_FINISH(lf); 1686 if (error) { 1687 printf("KLD file %s - could not finalize loading\n", 1688 lf->filename); 1689 goto fail; 1690 } 1691 linker_file_register_modules(lf); 1692 if (!TAILQ_EMPTY(&lf->modules)) 1693 lf->flags |= LINKER_FILE_MODULES; 1694 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1695 &si_stop, NULL) == 0) 1696 sysinit_add(si_start, si_stop); 1697 linker_file_register_sysctls(lf, true); 1698 lf->flags |= LINKER_FILE_LINKED; 1699 continue; 1700 fail: 1701 TAILQ_REMOVE(&depended_files, lf, loaded); 1702 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1703 } 1704 sx_xunlock(&kld_sx); 1705 /* woohoo! we made it! */ 1706 } 1707 1708 SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL); 1709 1710 /* 1711 * Handle preload files that failed to load any modules. 1712 */ 1713 static void 1714 linker_preload_finish(void *arg) 1715 { 1716 linker_file_t lf, nlf; 1717 1718 sx_xlock(&kld_sx); 1719 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) { 1720 /* 1721 * If all of the modules in this file failed to load, unload 1722 * the file and return an error of ENOEXEC. (Parity with 1723 * linker_load_file.) 1724 */ 1725 if ((lf->flags & LINKER_FILE_MODULES) != 0 && 1726 TAILQ_EMPTY(&lf->modules)) { 1727 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1728 continue; 1729 } 1730 1731 lf->flags &= ~LINKER_FILE_MODULES; 1732 lf->userrefs++; /* so we can (try to) kldunload it */ 1733 } 1734 sx_xunlock(&kld_sx); 1735 } 1736 1737 /* 1738 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be 1739 * scheduled at any subsystem and order, so run this as late as possible. init 1740 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that. 1741 */ 1742 SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE, 1743 linker_preload_finish, NULL); 1744 1745 /* 1746 * Search for a not-loaded module by name. 1747 * 1748 * Modules may be found in the following locations: 1749 * 1750 * - preloaded (result is just the module name) - on disk (result is full path 1751 * to module) 1752 * 1753 * If the module name is qualified in any way (contains path, etc.) the we 1754 * simply return a copy of it. 1755 * 1756 * The search path can be manipulated via sysctl. Note that we use the ';' 1757 * character as a separator to be consistent with the bootloader. 1758 */ 1759 1760 static char linker_hintfile[] = "linker.hints"; 1761 static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1762 1763 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path, 1764 sizeof(linker_path), "module load search path"); 1765 1766 TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1767 1768 static char *linker_ext_list[] = { 1769 "", 1770 ".ko", 1771 NULL 1772 }; 1773 1774 /* 1775 * Check if file actually exists either with or without extension listed in 1776 * the linker_ext_list. (probably should be generic for the rest of the 1777 * kernel) 1778 */ 1779 static char * 1780 linker_lookup_file(const char *path, int pathlen, const char *name, 1781 int namelen, struct vattr *vap) 1782 { 1783 struct nameidata nd; 1784 struct thread *td = curthread; /* XXX */ 1785 char *result, **cpp, *sep; 1786 int error, len, extlen, reclen, flags; 1787 enum vtype type; 1788 1789 extlen = 0; 1790 for (cpp = linker_ext_list; *cpp; cpp++) { 1791 len = strlen(*cpp); 1792 if (len > extlen) 1793 extlen = len; 1794 } 1795 extlen++; /* trailing '\0' */ 1796 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1797 1798 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1799 result = malloc(reclen, M_LINKER, M_WAITOK); 1800 for (cpp = linker_ext_list; *cpp; cpp++) { 1801 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1802 namelen, name, *cpp); 1803 /* 1804 * Attempt to open the file, and return the path if 1805 * we succeed and it's a regular file. 1806 */ 1807 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td); 1808 flags = FREAD; 1809 error = vn_open(&nd, &flags, 0, NULL); 1810 if (error == 0) { 1811 NDFREE(&nd, NDF_ONLY_PNBUF); 1812 type = nd.ni_vp->v_type; 1813 if (vap) 1814 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred); 1815 VOP_UNLOCK(nd.ni_vp); 1816 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1817 if (type == VREG) 1818 return (result); 1819 } 1820 } 1821 free(result, M_LINKER); 1822 return (NULL); 1823 } 1824 1825 #define INT_ALIGN(base, ptr) ptr = \ 1826 (base) + roundup2((ptr) - (base), sizeof(int)) 1827 1828 /* 1829 * Lookup KLD which contains requested module in the "linker.hints" file. If 1830 * version specification is available, then try to find the best KLD. 1831 * Otherwise just find the latest one. 1832 */ 1833 static char * 1834 linker_hints_lookup(const char *path, int pathlen, const char *modname, 1835 int modnamelen, const struct mod_depend *verinfo) 1836 { 1837 struct thread *td = curthread; /* XXX */ 1838 struct ucred *cred = td ? td->td_ucred : NULL; 1839 struct nameidata nd; 1840 struct vattr vattr, mattr; 1841 u_char *hints = NULL; 1842 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1843 int error, ival, bestver, *intp, found, flags, clen, blen; 1844 ssize_t reclen; 1845 1846 result = NULL; 1847 bestver = found = 0; 1848 1849 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1850 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1851 strlen(sep) + 1; 1852 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1853 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1854 linker_hintfile); 1855 1856 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td); 1857 flags = FREAD; 1858 error = vn_open(&nd, &flags, 0, NULL); 1859 if (error) 1860 goto bad; 1861 NDFREE(&nd, NDF_ONLY_PNBUF); 1862 if (nd.ni_vp->v_type != VREG) 1863 goto bad; 1864 best = cp = NULL; 1865 error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 1866 if (error) 1867 goto bad; 1868 /* 1869 * XXX: we need to limit this number to some reasonable value 1870 */ 1871 if (vattr.va_size > LINKER_HINTS_MAX) { 1872 printf("hints file too large %ld\n", (long)vattr.va_size); 1873 goto bad; 1874 } 1875 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1876 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1877 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1878 if (error) 1879 goto bad; 1880 VOP_UNLOCK(nd.ni_vp); 1881 vn_close(nd.ni_vp, FREAD, cred, td); 1882 nd.ni_vp = NULL; 1883 if (reclen != 0) { 1884 printf("can't read %zd\n", reclen); 1885 goto bad; 1886 } 1887 intp = (int *)hints; 1888 ival = *intp++; 1889 if (ival != LINKER_HINTS_VERSION) { 1890 printf("hints file version mismatch %d\n", ival); 1891 goto bad; 1892 } 1893 bufend = hints + vattr.va_size; 1894 recptr = (u_char *)intp; 1895 clen = blen = 0; 1896 while (recptr < bufend && !found) { 1897 intp = (int *)recptr; 1898 reclen = *intp++; 1899 ival = *intp++; 1900 cp = (char *)intp; 1901 switch (ival) { 1902 case MDT_VERSION: 1903 clen = *cp++; 1904 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1905 break; 1906 cp += clen; 1907 INT_ALIGN(hints, cp); 1908 ival = *(int *)cp; 1909 cp += sizeof(int); 1910 clen = *cp++; 1911 if (verinfo == NULL || 1912 ival == verinfo->md_ver_preferred) { 1913 found = 1; 1914 break; 1915 } 1916 if (ival >= verinfo->md_ver_minimum && 1917 ival <= verinfo->md_ver_maximum && 1918 ival > bestver) { 1919 bestver = ival; 1920 best = cp; 1921 blen = clen; 1922 } 1923 break; 1924 default: 1925 break; 1926 } 1927 recptr += reclen + sizeof(int); 1928 } 1929 /* 1930 * Finally check if KLD is in the place 1931 */ 1932 if (found) 1933 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1934 else if (best) 1935 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1936 1937 /* 1938 * KLD is newer than hints file. What we should do now? 1939 */ 1940 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1941 printf("warning: KLD '%s' is newer than the linker.hints" 1942 " file\n", result); 1943 bad: 1944 free(pathbuf, M_LINKER); 1945 if (hints) 1946 free(hints, M_TEMP); 1947 if (nd.ni_vp != NULL) { 1948 VOP_UNLOCK(nd.ni_vp); 1949 vn_close(nd.ni_vp, FREAD, cred, td); 1950 } 1951 /* 1952 * If nothing found or hints is absent - fallback to the old 1953 * way by using "kldname[.ko]" as module name. 1954 */ 1955 if (!found && !bestver && result == NULL) 1956 result = linker_lookup_file(path, pathlen, modname, 1957 modnamelen, NULL); 1958 return (result); 1959 } 1960 1961 /* 1962 * Lookup KLD which contains requested module in the all directories. 1963 */ 1964 static char * 1965 linker_search_module(const char *modname, int modnamelen, 1966 const struct mod_depend *verinfo) 1967 { 1968 char *cp, *ep, *result; 1969 1970 /* 1971 * traverse the linker path 1972 */ 1973 for (cp = linker_path; *cp; cp = ep + 1) { 1974 /* find the end of this component */ 1975 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1976 result = linker_hints_lookup(cp, ep - cp, modname, 1977 modnamelen, verinfo); 1978 if (result != NULL) 1979 return (result); 1980 if (*ep == 0) 1981 break; 1982 } 1983 return (NULL); 1984 } 1985 1986 /* 1987 * Search for module in all directories listed in the linker_path. 1988 */ 1989 static char * 1990 linker_search_kld(const char *name) 1991 { 1992 char *cp, *ep, *result; 1993 int len; 1994 1995 /* qualified at all? */ 1996 if (strchr(name, '/')) 1997 return (strdup(name, M_LINKER)); 1998 1999 /* traverse the linker path */ 2000 len = strlen(name); 2001 for (ep = linker_path; *ep; ep++) { 2002 cp = ep; 2003 /* find the end of this component */ 2004 for (; *ep != 0 && *ep != ';'; ep++); 2005 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 2006 if (result != NULL) 2007 return (result); 2008 } 2009 return (NULL); 2010 } 2011 2012 static const char * 2013 linker_basename(const char *path) 2014 { 2015 const char *filename; 2016 2017 filename = strrchr(path, '/'); 2018 if (filename == NULL) 2019 return path; 2020 if (filename[1]) 2021 filename++; 2022 return (filename); 2023 } 2024 2025 #ifdef HWPMC_HOOKS 2026 /* 2027 * Inform hwpmc about the set of kernel modules currently loaded. 2028 */ 2029 void * 2030 linker_hwpmc_list_objects(void) 2031 { 2032 linker_file_t lf; 2033 struct pmckern_map_in *kobase; 2034 int i, nmappings; 2035 2036 nmappings = 0; 2037 sx_slock(&kld_sx); 2038 TAILQ_FOREACH(lf, &linker_files, link) 2039 nmappings++; 2040 2041 /* Allocate nmappings + 1 entries. */ 2042 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in), 2043 M_LINKER, M_WAITOK | M_ZERO); 2044 i = 0; 2045 TAILQ_FOREACH(lf, &linker_files, link) { 2046 2047 /* Save the info for this linker file. */ 2048 kobase[i].pm_file = lf->filename; 2049 kobase[i].pm_address = (uintptr_t)lf->address; 2050 i++; 2051 } 2052 sx_sunlock(&kld_sx); 2053 2054 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?")); 2055 2056 /* The last entry of the malloced area comprises of all zeros. */ 2057 KASSERT(kobase[i].pm_file == NULL, 2058 ("linker_hwpmc_list_objects: last object not NULL")); 2059 2060 return ((void *)kobase); 2061 } 2062 #endif 2063 2064 /* 2065 * Find a file which contains given module and load it, if "parent" is not 2066 * NULL, register a reference to it. 2067 */ 2068 static int 2069 linker_load_module(const char *kldname, const char *modname, 2070 struct linker_file *parent, const struct mod_depend *verinfo, 2071 struct linker_file **lfpp) 2072 { 2073 linker_file_t lfdep; 2074 const char *filename; 2075 char *pathname; 2076 int error; 2077 2078 sx_assert(&kld_sx, SA_XLOCKED); 2079 if (modname == NULL) { 2080 /* 2081 * We have to load KLD 2082 */ 2083 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 2084 " is not NULL")); 2085 /* check if root file system is not mounted */ 2086 if (rootvnode == NULL || curproc->p_fd->fd_rdir == NULL) 2087 return (ENXIO); 2088 pathname = linker_search_kld(kldname); 2089 } else { 2090 if (modlist_lookup2(modname, verinfo) != NULL) 2091 return (EEXIST); 2092 /* check if root file system is not mounted */ 2093 if (rootvnode == NULL || curproc->p_fd->fd_rdir == NULL) 2094 return (ENXIO); 2095 if (kldname != NULL) 2096 pathname = strdup(kldname, M_LINKER); 2097 else 2098 /* 2099 * Need to find a KLD with required module 2100 */ 2101 pathname = linker_search_module(modname, 2102 strlen(modname), verinfo); 2103 } 2104 if (pathname == NULL) 2105 return (ENOENT); 2106 2107 /* 2108 * Can't load more than one file with the same basename XXX: 2109 * Actually it should be possible to have multiple KLDs with 2110 * the same basename but different path because they can 2111 * provide different versions of the same modules. 2112 */ 2113 filename = linker_basename(pathname); 2114 if (linker_find_file_by_name(filename)) 2115 error = EEXIST; 2116 else do { 2117 error = linker_load_file(pathname, &lfdep); 2118 if (error) 2119 break; 2120 if (modname && verinfo && 2121 modlist_lookup2(modname, verinfo) == NULL) { 2122 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 2123 error = ENOENT; 2124 break; 2125 } 2126 if (parent) { 2127 error = linker_file_add_dependency(parent, lfdep); 2128 if (error) 2129 break; 2130 } 2131 if (lfpp) 2132 *lfpp = lfdep; 2133 } while (0); 2134 free(pathname, M_LINKER); 2135 return (error); 2136 } 2137 2138 /* 2139 * This routine is responsible for finding dependencies of userland initiated 2140 * kldload(2)'s of files. 2141 */ 2142 int 2143 linker_load_dependencies(linker_file_t lf) 2144 { 2145 linker_file_t lfdep; 2146 struct mod_metadata **start, **stop, **mdp, **nmdp; 2147 struct mod_metadata *mp, *nmp; 2148 const struct mod_depend *verinfo; 2149 modlist_t mod; 2150 const char *modname, *nmodname; 2151 int ver, error = 0; 2152 2153 /* 2154 * All files are dependent on /kernel. 2155 */ 2156 sx_assert(&kld_sx, SA_XLOCKED); 2157 if (linker_kernel_file) { 2158 linker_kernel_file->refs++; 2159 error = linker_file_add_dependency(lf, linker_kernel_file); 2160 if (error) 2161 return (error); 2162 } 2163 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2164 NULL) != 0) 2165 return (0); 2166 for (mdp = start; mdp < stop; mdp++) { 2167 mp = *mdp; 2168 if (mp->md_type != MDT_VERSION) 2169 continue; 2170 modname = mp->md_cval; 2171 ver = ((const struct mod_version *)mp->md_data)->mv_version; 2172 mod = modlist_lookup(modname, ver); 2173 if (mod != NULL) { 2174 printf("interface %s.%d already present in the KLD" 2175 " '%s'!\n", modname, ver, 2176 mod->container->filename); 2177 return (EEXIST); 2178 } 2179 } 2180 2181 for (mdp = start; mdp < stop; mdp++) { 2182 mp = *mdp; 2183 if (mp->md_type != MDT_DEPEND) 2184 continue; 2185 modname = mp->md_cval; 2186 verinfo = mp->md_data; 2187 nmodname = NULL; 2188 for (nmdp = start; nmdp < stop; nmdp++) { 2189 nmp = *nmdp; 2190 if (nmp->md_type != MDT_VERSION) 2191 continue; 2192 nmodname = nmp->md_cval; 2193 if (strcmp(modname, nmodname) == 0) 2194 break; 2195 } 2196 if (nmdp < stop)/* early exit, it's a self reference */ 2197 continue; 2198 mod = modlist_lookup2(modname, verinfo); 2199 if (mod) { /* woohoo, it's loaded already */ 2200 lfdep = mod->container; 2201 lfdep->refs++; 2202 error = linker_file_add_dependency(lf, lfdep); 2203 if (error) 2204 break; 2205 continue; 2206 } 2207 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2208 if (error) { 2209 printf("KLD %s: depends on %s - not available or" 2210 " version mismatch\n", lf->filename, modname); 2211 break; 2212 } 2213 } 2214 2215 if (error) 2216 return (error); 2217 linker_addmodules(lf, start, stop, 0); 2218 return (error); 2219 } 2220 2221 static int 2222 sysctl_kern_function_list_iterate(const char *name, void *opaque) 2223 { 2224 struct sysctl_req *req; 2225 2226 req = opaque; 2227 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2228 } 2229 2230 /* 2231 * Export a nul-separated, double-nul-terminated list of all function names 2232 * in the kernel. 2233 */ 2234 static int 2235 sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2236 { 2237 linker_file_t lf; 2238 int error; 2239 2240 #ifdef MAC 2241 error = mac_kld_check_stat(req->td->td_ucred); 2242 if (error) 2243 return (error); 2244 #endif 2245 error = sysctl_wire_old_buffer(req, 0); 2246 if (error != 0) 2247 return (error); 2248 sx_xlock(&kld_sx); 2249 TAILQ_FOREACH(lf, &linker_files, link) { 2250 error = LINKER_EACH_FUNCTION_NAME(lf, 2251 sysctl_kern_function_list_iterate, req); 2252 if (error) { 2253 sx_xunlock(&kld_sx); 2254 return (error); 2255 } 2256 } 2257 sx_xunlock(&kld_sx); 2258 return (SYSCTL_OUT(req, "", 1)); 2259 } 2260 2261 SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD, 2262 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 2263