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