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 74int kld_debug = 0; 75SYSCTL_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. */ 80const int kld_off_address = offsetof(struct linker_file, address); 81const int kld_off_filename = offsetof(struct linker_file, filename); 82const int kld_off_pathname = offsetof(struct linker_file, pathname); 83const 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 */ 89static const char *linker_basename(const char *path); 90 91/* 92 * Find a currently loaded file given its filename. 93 */ 94static linker_file_t linker_find_file_by_name(const char* _filename); 95 96/* 97 * Find a currently loaded file given its file id. 98 */ 99static linker_file_t linker_find_file_by_id(int _fileid); 100 101/* Metadata from the static kernel */ 102SET_DECLARE(modmetadata_set, struct mod_metadata); 103 104MALLOC_DEFINE(M_LINKER, "linker", "kernel linker"); 105 106linker_file_t linker_kernel_file; 107 108static 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 */ 114static int loadcnt; 115 116static linker_class_list_t classes; 117static linker_file_list_t linker_files; 118static int next_file_id = 1; 119static 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); \ 126retry: \ 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 137/* XXX wrong name; we're looking at version provision tags here, not modules */ 138typedef TAILQ_HEAD(, modlist) modlisthead_t; 139struct modlist { 140 TAILQ_ENTRY(modlist) link; /* chain together all modules */ 141 linker_file_t container; 142 const char *name; 143 int version; 144}; 145typedef struct modlist *modlist_t; 146static modlisthead_t found_modules; 147 148static int linker_file_add_dependency(linker_file_t file, 149 linker_file_t dep); 150static caddr_t linker_file_lookup_symbol_internal(linker_file_t file, 151 const char* name, int deps); 152static int linker_load_module(const char *kldname, 153 const char *modname, struct linker_file *parent, 154 const struct mod_depend *verinfo, struct linker_file **lfpp); 155static modlist_t modlist_lookup2(const char *name, const struct mod_depend *verinfo); 156 157static void 158linker_init(void *arg) 159{ 160 161 sx_init(&kld_sx, "kernel linker"); 162 TAILQ_INIT(&classes); 163 TAILQ_INIT(&linker_files); 164} 165 166SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, NULL); 167 168static void 169linker_stop_class_add(void *arg) 170{ 171 172 linker_no_more_classes = 1; 173} 174 175SYSINIT(linker_class, SI_SUB_KLD, SI_ORDER_ANY, linker_stop_class_add, NULL); 176 177int 178linker_add_class(linker_class_t lc) 179{ 180 181 /* 182 * We disallow any class registration past SI_ORDER_ANY 183 * of SI_SUB_KLD. We bump the reference count to keep the 184 * ops from being freed. 185 */ 186 if (linker_no_more_classes == 1) 187 return (EPERM); 188 kobj_class_compile((kobj_class_t) lc); 189 ((kobj_class_t)lc)->refs++; /* XXX: kobj_mtx */ 190 TAILQ_INSERT_TAIL(&classes, lc, link); 191 return (0); 192} 193 194static void 195linker_file_sysinit(linker_file_t lf) 196{ 197 struct sysinit **start, **stop, **sipp, **xipp, *save; 198 199 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 200 lf->filename)); 201 202 sx_assert(&kld_sx, SA_XLOCKED); 203 204 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 205 return; 206 /* 207 * Perform a bubble sort of the system initialization objects by 208 * their subsystem (primary key) and order (secondary key). 209 * 210 * Since some things care about execution order, this is the operation 211 * which ensures continued function. 212 */ 213 for (sipp = start; sipp < stop; sipp++) { 214 for (xipp = sipp + 1; xipp < stop; xipp++) { 215 if ((*sipp)->subsystem < (*xipp)->subsystem || 216 ((*sipp)->subsystem == (*xipp)->subsystem && 217 (*sipp)->order <= (*xipp)->order)) 218 continue; /* skip */ 219 save = *sipp; 220 *sipp = *xipp; 221 *xipp = save; 222 } 223 } 224 225 /* 226 * Traverse the (now) ordered list of system initialization tasks. 227 * Perform each task, and continue on to the next task. 228 */ 229 sx_xunlock(&kld_sx); 230 mtx_lock(&Giant); 231 for (sipp = start; sipp < stop; sipp++) { 232 if ((*sipp)->subsystem == SI_SUB_DUMMY) 233 continue; /* skip dummy task(s) */ 234 235 /* Call function */ 236 (*((*sipp)->func)) ((*sipp)->udata); 237 } 238 mtx_unlock(&Giant); 239 sx_xlock(&kld_sx); 240} 241 242static void 243linker_file_sysuninit(linker_file_t lf) 244{ 245 struct sysinit **start, **stop, **sipp, **xipp, *save; 246 247 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 248 lf->filename)); 249 250 sx_assert(&kld_sx, SA_XLOCKED); 251 252 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, 253 NULL) != 0) 254 return; 255 256 /* 257 * Perform a reverse bubble sort of the system initialization objects 258 * by their subsystem (primary key) and order (secondary key). 259 * 260 * Since some things care about execution order, this is the operation 261 * which ensures continued function. 262 */ 263 for (sipp = start; sipp < stop; sipp++) { 264 for (xipp = sipp + 1; xipp < stop; xipp++) { 265 if ((*sipp)->subsystem > (*xipp)->subsystem || 266 ((*sipp)->subsystem == (*xipp)->subsystem && 267 (*sipp)->order >= (*xipp)->order)) 268 continue; /* skip */ 269 save = *sipp; 270 *sipp = *xipp; 271 *xipp = save; 272 } 273 } 274 275 /* 276 * Traverse the (now) ordered list of system initialization tasks. 277 * Perform each task, and continue on to the next task. 278 */ 279 sx_xunlock(&kld_sx); 280 mtx_lock(&Giant); 281 for (sipp = start; sipp < stop; sipp++) { 282 if ((*sipp)->subsystem == SI_SUB_DUMMY) 283 continue; /* skip dummy task(s) */ 284 285 /* Call function */ 286 (*((*sipp)->func)) ((*sipp)->udata); 287 } 288 mtx_unlock(&Giant); 289 sx_xlock(&kld_sx); 290} 291 292static void 293linker_file_register_sysctls(linker_file_t lf, bool enable) 294{ 295 struct sysctl_oid **start, **stop, **oidp; 296 297 KLD_DPF(FILE, 298 ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 299 lf->filename)); 300 301 sx_assert(&kld_sx, SA_XLOCKED); 302 303 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 304 return; 305 306 sx_xunlock(&kld_sx); 307 sysctl_wlock(); 308 for (oidp = start; oidp < stop; oidp++) { 309 if (enable) 310 sysctl_register_oid(*oidp); 311 else 312 sysctl_register_disabled_oid(*oidp); 313 } 314 sysctl_wunlock(); 315 sx_xlock(&kld_sx); 316} 317 318static void 319linker_file_enable_sysctls(linker_file_t lf) 320{ 321 struct sysctl_oid **start, **stop, **oidp; 322 323 KLD_DPF(FILE, 324 ("linker_file_enable_sysctls: enable SYSCTLs for %s\n", 325 lf->filename)); 326 327 sx_assert(&kld_sx, SA_XLOCKED); 328 329 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 330 return; 331 332 sx_xunlock(&kld_sx); 333 sysctl_wlock(); 334 for (oidp = start; oidp < stop; oidp++) 335 sysctl_enable_oid(*oidp); 336 sysctl_wunlock(); 337 sx_xlock(&kld_sx); 338} 339 340static void 341linker_file_unregister_sysctls(linker_file_t lf) 342{ 343 struct sysctl_oid **start, **stop, **oidp; 344 345 KLD_DPF(FILE, ("linker_file_unregister_sysctls: unregistering SYSCTLs" 346 " for %s\n", lf->filename)); 347 348 sx_assert(&kld_sx, SA_XLOCKED); 349 350 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 351 return; 352 353 sx_xunlock(&kld_sx); 354 sysctl_wlock(); 355 for (oidp = start; oidp < stop; oidp++) 356 sysctl_unregister_oid(*oidp); 357 sysctl_wunlock(); 358 sx_xlock(&kld_sx); 359} 360 361static int 362linker_file_register_modules(linker_file_t lf) 363{ 364 struct mod_metadata **start, **stop, **mdp; 365 const moduledata_t *moddata; 366 int first_error, error; 367 368 KLD_DPF(FILE, ("linker_file_register_modules: registering modules" 369 " in %s\n", lf->filename)); 370 371 sx_assert(&kld_sx, SA_XLOCKED); 372 373 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &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 403static void 404linker_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 412SYSINIT(linker_kernel, SI_SUB_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 413 NULL); 414 415static int 416linker_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 508int 509linker_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 528int 529linker_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 553static linker_file_t 554linker_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 573static linker_file_t 574linker_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 585int 586linker_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 601linker_file_t 602linker_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 636int 637linker_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 760int 761linker_ctf_get(linker_file_t file, linker_ctf_t *lc) 762{ 763 return (LINKER_CTF_GET(file, lc)); 764} 765 766static int 767linker_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 */ 788int 789linker_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 */ 800int 801linker_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 807caddr_t 808linker_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 822static caddr_t 823linker_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 908static int 909linker_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 921static int 922linker_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 952static int 953linker_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 964static int 965linker_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 992int 993linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 994{ 995 996 return (linker_debug_lookup(symstr, sym)); 997} 998#endif 999 1000int 1001linker_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 1007int 1008linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 1009{ 1010 1011 return (linker_debug_symbol_values(sym, symval)); 1012} 1013 1014int 1015linker_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 */ 1026int 1027linker_search_symbol_name(caddr_t value, char *buf, u_int buflen, 1028 long *offset) 1029{ 1030 int error; 1031 1032 sx_slock(&kld_sx); 1033 error = linker_debug_search_symbol_name(value, buf, buflen, offset); 1034 sx_sunlock(&kld_sx); 1035 return (error); 1036} 1037 1038/* 1039 * Syscalls. 1040 */ 1041int 1042kern_kldload(struct thread *td, const char *file, int *fileid) 1043{ 1044 const char *kldname, *modname; 1045 linker_file_t lf; 1046 int error; 1047 1048 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1049 return (error); 1050 1051 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0) 1052 return (error); 1053 1054 /* 1055 * It is possible that kldloaded module will attach a new ifnet, 1056 * so vnet context must be set when this ocurs. 1057 */ 1058 CURVNET_SET(TD_TO_VNET(td)); 1059 1060 /* 1061 * If file does not contain a qualified name or any dot in it 1062 * (kldname.ko, or kldname.ver.ko) treat it as an interface 1063 * name. 1064 */ 1065 if (strchr(file, '/') || strchr(file, '.')) { 1066 kldname = file; 1067 modname = NULL; 1068 } else { 1069 kldname = NULL; 1070 modname = file; 1071 } 1072 1073 sx_xlock(&kld_sx); 1074 error = linker_load_module(kldname, modname, NULL, NULL, &lf); 1075 if (error) { 1076 sx_xunlock(&kld_sx); 1077 goto done; 1078 } 1079 lf->userrefs++; 1080 if (fileid != NULL) 1081 *fileid = lf->id; 1082 sx_xunlock(&kld_sx); 1083 1084done: 1085 CURVNET_RESTORE(); 1086 return (error); 1087} 1088 1089int 1090sys_kldload(struct thread *td, struct kldload_args *uap) 1091{ 1092 char *pathname = NULL; 1093 int error, fileid; 1094 1095 td->td_retval[0] = -1; 1096 1097 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1098 error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL); 1099 if (error == 0) { 1100 error = kern_kldload(td, pathname, &fileid); 1101 if (error == 0) 1102 td->td_retval[0] = fileid; 1103 } 1104 free(pathname, M_TEMP); 1105 return (error); 1106} 1107 1108int 1109kern_kldunload(struct thread *td, int fileid, int flags) 1110{ 1111 linker_file_t lf; 1112 int error = 0; 1113 1114 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 1115 return (error); 1116 1117 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 1118 return (error); 1119 1120 CURVNET_SET(TD_TO_VNET(td)); 1121 sx_xlock(&kld_sx); 1122 lf = linker_find_file_by_id(fileid); 1123 if (lf) { 1124 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 1125 1126 if (lf->userrefs == 0) { 1127 /* 1128 * XXX: maybe LINKER_UNLOAD_FORCE should override ? 1129 */ 1130 printf("kldunload: attempt to unload file that was" 1131 " loaded by the kernel\n"); 1132 error = EBUSY; 1133 } else { 1134 lf->userrefs--; 1135 error = linker_file_unload(lf, flags); 1136 if (error) 1137 lf->userrefs++; 1138 } 1139 } else 1140 error = ENOENT; 1141 sx_xunlock(&kld_sx); 1142 1143 CURVNET_RESTORE(); 1144 return (error); 1145} 1146 1147int 1148sys_kldunload(struct thread *td, struct kldunload_args *uap) 1149{ 1150 1151 return (kern_kldunload(td, uap->fileid, LINKER_UNLOAD_NORMAL)); 1152} 1153 1154int 1155sys_kldunloadf(struct thread *td, struct kldunloadf_args *uap) 1156{ 1157 1158 if (uap->flags != LINKER_UNLOAD_NORMAL && 1159 uap->flags != LINKER_UNLOAD_FORCE) 1160 return (EINVAL); 1161 return (kern_kldunload(td, uap->fileid, uap->flags)); 1162} 1163 1164int 1165sys_kldfind(struct thread *td, struct kldfind_args *uap) 1166{ 1167 char *pathname; 1168 const char *filename; 1169 linker_file_t lf; 1170 int error; 1171 1172#ifdef MAC 1173 error = mac_kld_check_stat(td->td_ucred); 1174 if (error) 1175 return (error); 1176#endif 1177 1178 td->td_retval[0] = -1; 1179 1180 pathname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1181 if ((error = copyinstr(uap->file, pathname, MAXPATHLEN, NULL)) != 0) 1182 goto out; 1183 1184 filename = linker_basename(pathname); 1185 sx_xlock(&kld_sx); 1186 lf = linker_find_file_by_name(filename); 1187 if (lf) 1188 td->td_retval[0] = lf->id; 1189 else 1190 error = ENOENT; 1191 sx_xunlock(&kld_sx); 1192out: 1193 free(pathname, M_TEMP); 1194 return (error); 1195} 1196 1197int 1198sys_kldnext(struct thread *td, struct kldnext_args *uap) 1199{ 1200 linker_file_t lf; 1201 int error = 0; 1202 1203#ifdef MAC 1204 error = mac_kld_check_stat(td->td_ucred); 1205 if (error) 1206 return (error); 1207#endif 1208 1209 sx_xlock(&kld_sx); 1210 if (uap->fileid == 0) 1211 lf = TAILQ_FIRST(&linker_files); 1212 else { 1213 lf = linker_find_file_by_id(uap->fileid); 1214 if (lf == NULL) { 1215 error = ENOENT; 1216 goto out; 1217 } 1218 lf = TAILQ_NEXT(lf, link); 1219 } 1220 1221 /* Skip partially loaded files. */ 1222 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) 1223 lf = TAILQ_NEXT(lf, link); 1224 1225 if (lf) 1226 td->td_retval[0] = lf->id; 1227 else 1228 td->td_retval[0] = 0; 1229out: 1230 sx_xunlock(&kld_sx); 1231 return (error); 1232} 1233 1234int 1235sys_kldstat(struct thread *td, struct kldstat_args *uap) 1236{ 1237 struct kld_file_stat *stat; 1238 int error, version; 1239 1240 /* 1241 * Check the version of the user's structure. 1242 */ 1243 if ((error = copyin(&uap->stat->version, &version, sizeof(version))) 1244 != 0) 1245 return (error); 1246 if (version != sizeof(struct kld_file_stat_1) && 1247 version != sizeof(struct kld_file_stat)) 1248 return (EINVAL); 1249 1250 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO); 1251 error = kern_kldstat(td, uap->fileid, stat); 1252 if (error == 0) 1253 error = copyout(stat, uap->stat, version); 1254 free(stat, M_TEMP); 1255 return (error); 1256} 1257 1258int 1259kern_kldstat(struct thread *td, int fileid, struct kld_file_stat *stat) 1260{ 1261 linker_file_t lf; 1262 int namelen; 1263#ifdef MAC 1264 int error; 1265 1266 error = mac_kld_check_stat(td->td_ucred); 1267 if (error) 1268 return (error); 1269#endif 1270 1271 sx_xlock(&kld_sx); 1272 lf = linker_find_file_by_id(fileid); 1273 if (lf == NULL) { 1274 sx_xunlock(&kld_sx); 1275 return (ENOENT); 1276 } 1277 1278 /* Version 1 fields: */ 1279 namelen = strlen(lf->filename) + 1; 1280 if (namelen > sizeof(stat->name)) 1281 namelen = sizeof(stat->name); 1282 bcopy(lf->filename, &stat->name[0], namelen); 1283 stat->refs = lf->refs; 1284 stat->id = lf->id; 1285 stat->address = lf->address; 1286 stat->size = lf->size; 1287 /* Version 2 fields: */ 1288 namelen = strlen(lf->pathname) + 1; 1289 if (namelen > sizeof(stat->pathname)) 1290 namelen = sizeof(stat->pathname); 1291 bcopy(lf->pathname, &stat->pathname[0], namelen); 1292 sx_xunlock(&kld_sx); 1293 1294 td->td_retval[0] = 0; 1295 return (0); 1296} 1297 1298#ifdef DDB 1299DB_COMMAND(kldstat, db_kldstat) 1300{ 1301 linker_file_t lf; 1302 1303#define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2)) 1304 db_printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' '); 1305#undef POINTER_WIDTH 1306 TAILQ_FOREACH(lf, &linker_files, link) { 1307 if (db_pager_quit) 1308 return; 1309 db_printf("%2d %4d %p %-8zx %s\n", lf->id, lf->refs, 1310 lf->address, lf->size, lf->filename); 1311 } 1312} 1313#endif /* DDB */ 1314 1315int 1316sys_kldfirstmod(struct thread *td, struct kldfirstmod_args *uap) 1317{ 1318 linker_file_t lf; 1319 module_t mp; 1320 int error = 0; 1321 1322#ifdef MAC 1323 error = mac_kld_check_stat(td->td_ucred); 1324 if (error) 1325 return (error); 1326#endif 1327 1328 sx_xlock(&kld_sx); 1329 lf = linker_find_file_by_id(uap->fileid); 1330 if (lf) { 1331 MOD_SLOCK; 1332 mp = TAILQ_FIRST(&lf->modules); 1333 if (mp != NULL) 1334 td->td_retval[0] = module_getid(mp); 1335 else 1336 td->td_retval[0] = 0; 1337 MOD_SUNLOCK; 1338 } else 1339 error = ENOENT; 1340 sx_xunlock(&kld_sx); 1341 return (error); 1342} 1343 1344int 1345sys_kldsym(struct thread *td, struct kldsym_args *uap) 1346{ 1347 char *symstr = NULL; 1348 c_linker_sym_t sym; 1349 linker_symval_t symval; 1350 linker_file_t lf; 1351 struct kld_sym_lookup lookup; 1352 int error = 0; 1353 1354#ifdef MAC 1355 error = mac_kld_check_stat(td->td_ucred); 1356 if (error) 1357 return (error); 1358#endif 1359 1360 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 1361 return (error); 1362 if (lookup.version != sizeof(lookup) || 1363 uap->cmd != KLDSYM_LOOKUP) 1364 return (EINVAL); 1365 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1366 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 1367 goto out; 1368 sx_xlock(&kld_sx); 1369 if (uap->fileid != 0) { 1370 lf = linker_find_file_by_id(uap->fileid); 1371 if (lf == NULL) 1372 error = ENOENT; 1373 else if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1374 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1375 lookup.symvalue = (uintptr_t) symval.value; 1376 lookup.symsize = symval.size; 1377 error = copyout(&lookup, uap->data, sizeof(lookup)); 1378 } else 1379 error = ENOENT; 1380 } else { 1381 TAILQ_FOREACH(lf, &linker_files, link) { 1382 if (LINKER_LOOKUP_SYMBOL(lf, symstr, &sym) == 0 && 1383 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0) { 1384 lookup.symvalue = (uintptr_t)symval.value; 1385 lookup.symsize = symval.size; 1386 error = copyout(&lookup, uap->data, 1387 sizeof(lookup)); 1388 break; 1389 } 1390 } 1391 if (lf == NULL) 1392 error = ENOENT; 1393 } 1394 sx_xunlock(&kld_sx); 1395out: 1396 free(symstr, M_TEMP); 1397 return (error); 1398} 1399 1400/* 1401 * Preloaded module support 1402 */ 1403 1404static modlist_t 1405modlist_lookup(const char *name, int ver) 1406{ 1407 modlist_t mod; 1408 1409 TAILQ_FOREACH(mod, &found_modules, link) { 1410 if (strcmp(mod->name, name) == 0 && 1411 (ver == 0 || mod->version == ver)) 1412 return (mod); 1413 } 1414 return (NULL); 1415} 1416 1417static modlist_t 1418modlist_lookup2(const char *name, const struct mod_depend *verinfo) 1419{ 1420 modlist_t mod, bestmod; 1421 int ver; 1422 1423 if (verinfo == NULL) 1424 return (modlist_lookup(name, 0)); 1425 bestmod = NULL; 1426 TAILQ_FOREACH(mod, &found_modules, link) { 1427 if (strcmp(mod->name, name) != 0) 1428 continue; 1429 ver = mod->version; 1430 if (ver == verinfo->md_ver_preferred) 1431 return (mod); 1432 if (ver >= verinfo->md_ver_minimum && 1433 ver <= verinfo->md_ver_maximum && 1434 (bestmod == NULL || ver > bestmod->version)) 1435 bestmod = mod; 1436 } 1437 return (bestmod); 1438} 1439 1440static modlist_t 1441modlist_newmodule(const char *modname, int version, linker_file_t container) 1442{ 1443 modlist_t mod; 1444 1445 mod = malloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1446 if (mod == NULL) 1447 panic("no memory for module list"); 1448 mod->container = container; 1449 mod->name = modname; 1450 mod->version = version; 1451 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1452 return (mod); 1453} 1454 1455static void 1456linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1457 struct mod_metadata **stop, int preload) 1458{ 1459 struct mod_metadata *mp, **mdp; 1460 const char *modname; 1461 int ver; 1462 1463 for (mdp = start; mdp < stop; mdp++) { 1464 mp = *mdp; 1465 if (mp->md_type != MDT_VERSION) 1466 continue; 1467 modname = mp->md_cval; 1468 ver = ((const struct mod_version *)mp->md_data)->mv_version; 1469 if (modlist_lookup(modname, ver) != NULL) { 1470 printf("module %s already present!\n", modname); 1471 /* XXX what can we do? this is a build error. :-( */ 1472 continue; 1473 } 1474 modlist_newmodule(modname, ver, lf); 1475 } 1476} 1477 1478static void 1479linker_preload(void *arg) 1480{ 1481 caddr_t modptr; 1482 const char *modname, *nmodname; 1483 char *modtype; 1484 linker_file_t lf, nlf; 1485 linker_class_t lc; 1486 int error; 1487 linker_file_list_t loaded_files; 1488 linker_file_list_t depended_files; 1489 struct mod_metadata *mp, *nmp; 1490 struct mod_metadata **start, **stop, **mdp, **nmdp; 1491 const struct mod_depend *verinfo; 1492 int nver; 1493 int resolves; 1494 modlist_t mod; 1495 struct sysinit **si_start, **si_stop; 1496 1497 TAILQ_INIT(&loaded_files); 1498 TAILQ_INIT(&depended_files); 1499 TAILQ_INIT(&found_modules); 1500 error = 0; 1501 1502 modptr = NULL; 1503 sx_xlock(&kld_sx); 1504 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1505 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1506 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1507 if (modname == NULL) { 1508 printf("Preloaded module at %p does not have a" 1509 " name!\n", modptr); 1510 continue; 1511 } 1512 if (modtype == NULL) { 1513 printf("Preloaded module at %p does not have a type!\n", 1514 modptr); 1515 continue; 1516 } 1517 if (bootverbose) 1518 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, 1519 modptr); 1520 lf = NULL; 1521 TAILQ_FOREACH(lc, &classes, link) { 1522 error = LINKER_LINK_PRELOAD(lc, modname, &lf); 1523 if (!error) 1524 break; 1525 lf = NULL; 1526 } 1527 if (lf) 1528 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1529 } 1530 1531 /* 1532 * First get a list of stuff in the kernel. 1533 */ 1534 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1535 &stop, NULL) == 0) 1536 linker_addmodules(linker_kernel_file, start, stop, 1); 1537 1538 /* 1539 * This is a once-off kinky bubble sort to resolve relocation 1540 * dependency requirements. 1541 */ 1542restart: 1543 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1544 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1545 &stop, NULL); 1546 /* 1547 * First, look to see if we would successfully link with this 1548 * stuff. 1549 */ 1550 resolves = 1; /* unless we know otherwise */ 1551 if (!error) { 1552 for (mdp = start; mdp < stop; mdp++) { 1553 mp = *mdp; 1554 if (mp->md_type != MDT_DEPEND) 1555 continue; 1556 modname = mp->md_cval; 1557 verinfo = mp->md_data; 1558 for (nmdp = start; nmdp < stop; nmdp++) { 1559 nmp = *nmdp; 1560 if (nmp->md_type != MDT_VERSION) 1561 continue; 1562 nmodname = nmp->md_cval; 1563 if (strcmp(modname, nmodname) == 0) 1564 break; 1565 } 1566 if (nmdp < stop) /* it's a self reference */ 1567 continue; 1568 1569 /* 1570 * ok, the module isn't here yet, we 1571 * are not finished 1572 */ 1573 if (modlist_lookup2(modname, verinfo) == NULL) 1574 resolves = 0; 1575 } 1576 } 1577 /* 1578 * OK, if we found our modules, we can link. So, "provide" 1579 * the modules inside and add it to the end of the link order 1580 * list. 1581 */ 1582 if (resolves) { 1583 if (!error) { 1584 for (mdp = start; mdp < stop; mdp++) { 1585 mp = *mdp; 1586 if (mp->md_type != MDT_VERSION) 1587 continue; 1588 modname = mp->md_cval; 1589 nver = ((const struct mod_version *) 1590 mp->md_data)->mv_version; 1591 if (modlist_lookup(modname, 1592 nver) != NULL) { 1593 printf("module %s already" 1594 " present!\n", modname); 1595 TAILQ_REMOVE(&loaded_files, 1596 lf, loaded); 1597 linker_file_unload(lf, 1598 LINKER_UNLOAD_FORCE); 1599 /* we changed tailq next ptr */ 1600 goto restart; 1601 } 1602 modlist_newmodule(modname, nver, lf); 1603 } 1604 } 1605 TAILQ_REMOVE(&loaded_files, lf, loaded); 1606 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1607 /* 1608 * Since we provided modules, we need to restart the 1609 * sort so that the previous files that depend on us 1610 * have a chance. Also, we've busted the tailq next 1611 * pointer with the REMOVE. 1612 */ 1613 goto restart; 1614 } 1615 } 1616 1617 /* 1618 * At this point, we check to see what could not be resolved.. 1619 */ 1620 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1621 TAILQ_REMOVE(&loaded_files, lf, loaded); 1622 printf("KLD file %s is missing dependencies\n", lf->filename); 1623 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1624 } 1625 1626 /* 1627 * We made it. Finish off the linking in the order we determined. 1628 */ 1629 TAILQ_FOREACH_SAFE(lf, &depended_files, loaded, nlf) { 1630 if (linker_kernel_file) { 1631 linker_kernel_file->refs++; 1632 error = linker_file_add_dependency(lf, 1633 linker_kernel_file); 1634 if (error) 1635 panic("cannot add dependency"); 1636 } 1637 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, 1638 &stop, NULL); 1639 if (!error) { 1640 for (mdp = start; mdp < stop; mdp++) { 1641 mp = *mdp; 1642 if (mp->md_type != MDT_DEPEND) 1643 continue; 1644 modname = mp->md_cval; 1645 verinfo = mp->md_data; 1646 mod = modlist_lookup2(modname, verinfo); 1647 if (mod == NULL) { 1648 printf("KLD file %s - cannot find " 1649 "dependency \"%s\"\n", 1650 lf->filename, modname); 1651 goto fail; 1652 } 1653 /* Don't count self-dependencies */ 1654 if (lf == mod->container) 1655 continue; 1656 mod->container->refs++; 1657 error = linker_file_add_dependency(lf, 1658 mod->container); 1659 if (error) 1660 panic("cannot add dependency"); 1661 } 1662 } 1663 /* 1664 * Now do relocation etc using the symbol search paths 1665 * established by the dependencies 1666 */ 1667 error = LINKER_LINK_PRELOAD_FINISH(lf); 1668 if (error) { 1669 printf("KLD file %s - could not finalize loading\n", 1670 lf->filename); 1671 goto fail; 1672 } 1673 linker_file_register_modules(lf); 1674 if (!TAILQ_EMPTY(&lf->modules)) 1675 lf->flags |= LINKER_FILE_MODULES; 1676 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, 1677 &si_stop, NULL) == 0) 1678 sysinit_add(si_start, si_stop); 1679 linker_file_register_sysctls(lf, true); 1680 lf->flags |= LINKER_FILE_LINKED; 1681 continue; 1682fail: 1683 TAILQ_REMOVE(&depended_files, lf, loaded); 1684 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1685 } 1686 sx_xunlock(&kld_sx); 1687 /* woohoo! we made it! */ 1688} 1689 1690SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, NULL); 1691 1692/* 1693 * Handle preload files that failed to load any modules. 1694 */ 1695static void 1696linker_preload_finish(void *arg) 1697{ 1698 linker_file_t lf, nlf; 1699 1700 sx_xlock(&kld_sx); 1701 TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) { 1702 /* 1703 * If all of the modules in this file failed to load, unload 1704 * the file and return an error of ENOEXEC. (Parity with 1705 * linker_load_file.) 1706 */ 1707 if ((lf->flags & LINKER_FILE_MODULES) != 0 && 1708 TAILQ_EMPTY(&lf->modules)) { 1709 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 1710 continue; 1711 } 1712 1713 lf->flags &= ~LINKER_FILE_MODULES; 1714 lf->userrefs++; /* so we can (try to) kldunload it */ 1715 } 1716 sx_xunlock(&kld_sx); 1717} 1718 1719/* 1720 * Attempt to run after all DECLARE_MODULE SYSINITs. Unfortunately they can be 1721 * scheduled at any subsystem and order, so run this as late as possible. init 1722 * becomes runnable in SI_SUB_KTHREAD_INIT, so go slightly before that. 1723 */ 1724SYSINIT(preload_finish, SI_SUB_KTHREAD_INIT - 100, SI_ORDER_MIDDLE, 1725 linker_preload_finish, NULL); 1726 1727/* 1728 * Search for a not-loaded module by name. 1729 * 1730 * Modules may be found in the following locations: 1731 * 1732 * - preloaded (result is just the module name) - on disk (result is full path 1733 * to module) 1734 * 1735 * If the module name is qualified in any way (contains path, etc.) the we 1736 * simply return a copy of it. 1737 * 1738 * The search path can be manipulated via sysctl. Note that we use the ';' 1739 * character as a separator to be consistent with the bootloader. 1740 */ 1741 1742static char linker_hintfile[] = "linker.hints"; 1743static char linker_path[MAXPATHLEN] = "/boot/kernel;/boot/modules"; 1744 1745SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RWTUN, linker_path, 1746 sizeof(linker_path), "module load search path"); 1747 1748TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1749 1750static char *linker_ext_list[] = { 1751 "", 1752 ".ko", 1753 NULL 1754}; 1755 1756/* 1757 * Check if file actually exists either with or without extension listed in 1758 * the linker_ext_list. (probably should be generic for the rest of the 1759 * kernel) 1760 */ 1761static char * 1762linker_lookup_file(const char *path, int pathlen, const char *name, 1763 int namelen, struct vattr *vap) 1764{ 1765 struct nameidata nd; 1766 struct thread *td = curthread; /* XXX */ 1767 char *result, **cpp, *sep; 1768 int error, len, extlen, reclen, flags; 1769 enum vtype type; 1770 1771 extlen = 0; 1772 for (cpp = linker_ext_list; *cpp; cpp++) { 1773 len = strlen(*cpp); 1774 if (len > extlen) 1775 extlen = len; 1776 } 1777 extlen++; /* trailing '\0' */ 1778 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1779 1780 reclen = pathlen + strlen(sep) + namelen + extlen + 1; 1781 result = malloc(reclen, M_LINKER, M_WAITOK); 1782 for (cpp = linker_ext_list; *cpp; cpp++) { 1783 snprintf(result, reclen, "%.*s%s%.*s%s", pathlen, path, sep, 1784 namelen, name, *cpp); 1785 /* 1786 * Attempt to open the file, and return the path if 1787 * we succeed and it's a regular file. 1788 */ 1789 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, td); 1790 flags = FREAD; 1791 error = vn_open(&nd, &flags, 0, NULL); 1792 if (error == 0) { 1793 NDFREE(&nd, NDF_ONLY_PNBUF); 1794 type = nd.ni_vp->v_type; 1795 if (vap) 1796 VOP_GETATTR(nd.ni_vp, vap, td->td_ucred); 1797 VOP_UNLOCK(nd.ni_vp, 0); 1798 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 1799 if (type == VREG) 1800 return (result); 1801 } 1802 } 1803 free(result, M_LINKER); 1804 return (NULL); 1805} 1806 1807#define INT_ALIGN(base, ptr) ptr = \ 1808 (base) + roundup2((ptr) - (base), sizeof(int)) 1809 1810/* 1811 * Lookup KLD which contains requested module in the "linker.hints" file. If 1812 * version specification is available, then try to find the best KLD. 1813 * Otherwise just find the latest one. 1814 */ 1815static char * 1816linker_hints_lookup(const char *path, int pathlen, const char *modname, 1817 int modnamelen, const struct mod_depend *verinfo) 1818{ 1819 struct thread *td = curthread; /* XXX */ 1820 struct ucred *cred = td ? td->td_ucred : NULL; 1821 struct nameidata nd; 1822 struct vattr vattr, mattr; 1823 u_char *hints = NULL; 1824 u_char *cp, *recptr, *bufend, *result, *best, *pathbuf, *sep; 1825 int error, ival, bestver, *intp, found, flags, clen, blen; 1826 ssize_t reclen; 1827 1828 result = NULL; 1829 bestver = found = 0; 1830 1831 sep = (path[pathlen - 1] != '/') ? "/" : ""; 1832 reclen = imax(modnamelen, strlen(linker_hintfile)) + pathlen + 1833 strlen(sep) + 1; 1834 pathbuf = malloc(reclen, M_LINKER, M_WAITOK); 1835 snprintf(pathbuf, reclen, "%.*s%s%s", pathlen, path, sep, 1836 linker_hintfile); 1837 1838 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, pathbuf, td); 1839 flags = FREAD; 1840 error = vn_open(&nd, &flags, 0, NULL); 1841 if (error) 1842 goto bad; 1843 NDFREE(&nd, NDF_ONLY_PNBUF); 1844 if (nd.ni_vp->v_type != VREG) 1845 goto bad; 1846 best = cp = NULL; 1847 error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 1848 if (error) 1849 goto bad; 1850 /* 1851 * XXX: we need to limit this number to some reasonable value 1852 */ 1853 if (vattr.va_size > LINKER_HINTS_MAX) { 1854 printf("hints file too large %ld\n", (long)vattr.va_size); 1855 goto bad; 1856 } 1857 hints = malloc(vattr.va_size, M_TEMP, M_WAITOK); 1858 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)hints, vattr.va_size, 0, 1859 UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &reclen, td); 1860 if (error) 1861 goto bad; 1862 VOP_UNLOCK(nd.ni_vp, 0); 1863 vn_close(nd.ni_vp, FREAD, cred, td); 1864 nd.ni_vp = NULL; 1865 if (reclen != 0) { 1866 printf("can't read %zd\n", reclen); 1867 goto bad; 1868 } 1869 intp = (int *)hints; 1870 ival = *intp++; 1871 if (ival != LINKER_HINTS_VERSION) { 1872 printf("hints file version mismatch %d\n", ival); 1873 goto bad; 1874 } 1875 bufend = hints + vattr.va_size; 1876 recptr = (u_char *)intp; 1877 clen = blen = 0; 1878 while (recptr < bufend && !found) { 1879 intp = (int *)recptr; 1880 reclen = *intp++; 1881 ival = *intp++; 1882 cp = (char *)intp; 1883 switch (ival) { 1884 case MDT_VERSION: 1885 clen = *cp++; 1886 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1887 break; 1888 cp += clen; 1889 INT_ALIGN(hints, cp); 1890 ival = *(int *)cp; 1891 cp += sizeof(int); 1892 clen = *cp++; 1893 if (verinfo == NULL || 1894 ival == verinfo->md_ver_preferred) { 1895 found = 1; 1896 break; 1897 } 1898 if (ival >= verinfo->md_ver_minimum && 1899 ival <= verinfo->md_ver_maximum && 1900 ival > bestver) { 1901 bestver = ival; 1902 best = cp; 1903 blen = clen; 1904 } 1905 break; 1906 default: 1907 break; 1908 } 1909 recptr += reclen + sizeof(int); 1910 } 1911 /* 1912 * Finally check if KLD is in the place 1913 */ 1914 if (found) 1915 result = linker_lookup_file(path, pathlen, cp, clen, &mattr); 1916 else if (best) 1917 result = linker_lookup_file(path, pathlen, best, blen, &mattr); 1918 1919 /* 1920 * KLD is newer than hints file. What we should do now? 1921 */ 1922 if (result && timespeccmp(&mattr.va_mtime, &vattr.va_mtime, >)) 1923 printf("warning: KLD '%s' is newer than the linker.hints" 1924 " file\n", result); 1925bad: 1926 free(pathbuf, M_LINKER); 1927 if (hints) 1928 free(hints, M_TEMP); 1929 if (nd.ni_vp != NULL) { 1930 VOP_UNLOCK(nd.ni_vp, 0); 1931 vn_close(nd.ni_vp, FREAD, cred, td); 1932 } 1933 /* 1934 * If nothing found or hints is absent - fallback to the old 1935 * way by using "kldname[.ko]" as module name. 1936 */ 1937 if (!found && !bestver && result == NULL) 1938 result = linker_lookup_file(path, pathlen, modname, 1939 modnamelen, NULL); 1940 return (result); 1941} 1942 1943/* 1944 * Lookup KLD which contains requested module in the all directories. 1945 */ 1946static char * 1947linker_search_module(const char *modname, int modnamelen, 1948 const struct mod_depend *verinfo) 1949{ 1950 char *cp, *ep, *result; 1951 1952 /* 1953 * traverse the linker path 1954 */ 1955 for (cp = linker_path; *cp; cp = ep + 1) { 1956 /* find the end of this component */ 1957 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); 1958 result = linker_hints_lookup(cp, ep - cp, modname, 1959 modnamelen, verinfo); 1960 if (result != NULL) 1961 return (result); 1962 if (*ep == 0) 1963 break; 1964 } 1965 return (NULL); 1966} 1967 1968/* 1969 * Search for module in all directories listed in the linker_path. 1970 */ 1971static char * 1972linker_search_kld(const char *name) 1973{ 1974 char *cp, *ep, *result; 1975 int len; 1976 1977 /* qualified at all? */ 1978 if (strchr(name, '/')) 1979 return (strdup(name, M_LINKER)); 1980 1981 /* traverse the linker path */ 1982 len = strlen(name); 1983 for (ep = linker_path; *ep; ep++) { 1984 cp = ep; 1985 /* find the end of this component */ 1986 for (; *ep != 0 && *ep != ';'; ep++); 1987 result = linker_lookup_file(cp, ep - cp, name, len, NULL); 1988 if (result != NULL) 1989 return (result); 1990 } 1991 return (NULL); 1992} 1993 1994static const char * 1995linker_basename(const char *path) 1996{ 1997 const char *filename; 1998 1999 filename = strrchr(path, '/'); 2000 if (filename == NULL) 2001 return path; 2002 if (filename[1]) 2003 filename++; 2004 return (filename); 2005} 2006 2007#ifdef HWPMC_HOOKS 2008/* 2009 * Inform hwpmc about the set of kernel modules currently loaded. 2010 */ 2011void * 2012linker_hwpmc_list_objects(void) 2013{ 2014 linker_file_t lf; 2015 struct pmckern_map_in *kobase; 2016 int i, nmappings; 2017 2018 nmappings = 0; 2019 sx_slock(&kld_sx); 2020 TAILQ_FOREACH(lf, &linker_files, link) 2021 nmappings++; 2022 2023 /* Allocate nmappings + 1 entries. */ 2024 kobase = malloc((nmappings + 1) * sizeof(struct pmckern_map_in), 2025 M_LINKER, M_WAITOK | M_ZERO); 2026 i = 0; 2027 TAILQ_FOREACH(lf, &linker_files, link) { 2028 2029 /* Save the info for this linker file. */ 2030 kobase[i].pm_file = lf->filename; 2031 kobase[i].pm_address = (uintptr_t)lf->address; 2032 i++; 2033 } 2034 sx_sunlock(&kld_sx); 2035 2036 KASSERT(i > 0, ("linker_hpwmc_list_objects: no kernel objects?")); 2037 2038 /* The last entry of the malloced area comprises of all zeros. */ 2039 KASSERT(kobase[i].pm_file == NULL, 2040 ("linker_hwpmc_list_objects: last object not NULL")); 2041 2042 return ((void *)kobase); 2043} 2044#endif 2045 2046/* 2047 * Find a file which contains given module and load it, if "parent" is not 2048 * NULL, register a reference to it. 2049 */ 2050static int 2051linker_load_module(const char *kldname, const char *modname, 2052 struct linker_file *parent, const struct mod_depend *verinfo, 2053 struct linker_file **lfpp) 2054{ 2055 linker_file_t lfdep; 2056 const char *filename; 2057 char *pathname; 2058 int error; 2059 2060 sx_assert(&kld_sx, SA_XLOCKED); 2061 if (modname == NULL) { 2062 /* 2063 * We have to load KLD 2064 */ 2065 KASSERT(verinfo == NULL, ("linker_load_module: verinfo" 2066 " is not NULL")); 2067 /* check if root file system is not mounted */ 2068 if (rootvnode == NULL || curproc->p_fd->fd_rdir == NULL) 2069 return (ENXIO); 2070 pathname = linker_search_kld(kldname); 2071 } else { 2072 if (modlist_lookup2(modname, verinfo) != NULL) 2073 return (EEXIST); 2074 /* check if root file system is not mounted */ 2075 if (rootvnode == NULL || curproc->p_fd->fd_rdir == NULL) 2076 return (ENXIO); 2077 if (kldname != NULL) 2078 pathname = strdup(kldname, M_LINKER); 2079 else 2080 /* 2081 * Need to find a KLD with required module 2082 */ 2083 pathname = linker_search_module(modname, 2084 strlen(modname), verinfo); 2085 } 2086 if (pathname == NULL) 2087 return (ENOENT); 2088 2089 /* 2090 * Can't load more than one file with the same basename XXX: 2091 * Actually it should be possible to have multiple KLDs with 2092 * the same basename but different path because they can 2093 * provide different versions of the same modules. 2094 */ 2095 filename = linker_basename(pathname); 2096 if (linker_find_file_by_name(filename)) 2097 error = EEXIST; 2098 else do { 2099 error = linker_load_file(pathname, &lfdep); 2100 if (error) 2101 break; 2102 if (modname && verinfo && 2103 modlist_lookup2(modname, verinfo) == NULL) { 2104 linker_file_unload(lfdep, LINKER_UNLOAD_FORCE); 2105 error = ENOENT; 2106 break; 2107 } 2108 if (parent) { 2109 error = linker_file_add_dependency(parent, lfdep); 2110 if (error) 2111 break; 2112 } 2113 if (lfpp) 2114 *lfpp = lfdep; 2115 } while (0); 2116 free(pathname, M_LINKER); 2117 return (error); 2118} 2119 2120/* 2121 * This routine is responsible for finding dependencies of userland initiated 2122 * kldload(2)'s of files. 2123 */ 2124int 2125linker_load_dependencies(linker_file_t lf) 2126{ 2127 linker_file_t lfdep; 2128 struct mod_metadata **start, **stop, **mdp, **nmdp; 2129 struct mod_metadata *mp, *nmp; 2130 const struct mod_depend *verinfo; 2131 modlist_t mod; 2132 const char *modname, *nmodname; 2133 int ver, error = 0; 2134 2135 /* 2136 * All files are dependent on /kernel. 2137 */ 2138 sx_assert(&kld_sx, SA_XLOCKED); 2139 if (linker_kernel_file) { 2140 linker_kernel_file->refs++; 2141 error = linker_file_add_dependency(lf, linker_kernel_file); 2142 if (error) 2143 return (error); 2144 } 2145 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, 2146 NULL) != 0) 2147 return (0); 2148 for (mdp = start; mdp < stop; mdp++) { 2149 mp = *mdp; 2150 if (mp->md_type != MDT_VERSION) 2151 continue; 2152 modname = mp->md_cval; 2153 ver = ((const struct mod_version *)mp->md_data)->mv_version; 2154 mod = modlist_lookup(modname, ver); 2155 if (mod != NULL) { 2156 printf("interface %s.%d already present in the KLD" 2157 " '%s'!\n", modname, ver, 2158 mod->container->filename); 2159 return (EEXIST); 2160 } 2161 } 2162 2163 for (mdp = start; mdp < stop; mdp++) { 2164 mp = *mdp; 2165 if (mp->md_type != MDT_DEPEND) 2166 continue; 2167 modname = mp->md_cval; 2168 verinfo = mp->md_data; 2169 nmodname = NULL; 2170 for (nmdp = start; nmdp < stop; nmdp++) { 2171 nmp = *nmdp; 2172 if (nmp->md_type != MDT_VERSION) 2173 continue; 2174 nmodname = nmp->md_cval; 2175 if (strcmp(modname, nmodname) == 0) 2176 break; 2177 } 2178 if (nmdp < stop)/* early exit, it's a self reference */ 2179 continue; 2180 mod = modlist_lookup2(modname, verinfo); 2181 if (mod) { /* woohoo, it's loaded already */ 2182 lfdep = mod->container; 2183 lfdep->refs++; 2184 error = linker_file_add_dependency(lf, lfdep); 2185 if (error) 2186 break; 2187 continue; 2188 } 2189 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 2190 if (error) { 2191 printf("KLD %s: depends on %s - not available or" 2192 " version mismatch\n", lf->filename, modname); 2193 break; 2194 } 2195 } 2196 2197 if (error) 2198 return (error); 2199 linker_addmodules(lf, start, stop, 0); 2200 return (error); 2201} 2202 2203static int 2204sysctl_kern_function_list_iterate(const char *name, void *opaque) 2205{ 2206 struct sysctl_req *req; 2207 2208 req = opaque; 2209 return (SYSCTL_OUT(req, name, strlen(name) + 1)); 2210} 2211 2212/* 2213 * Export a nul-separated, double-nul-terminated list of all function names 2214 * in the kernel. 2215 */ 2216static int 2217sysctl_kern_function_list(SYSCTL_HANDLER_ARGS) 2218{ 2219 linker_file_t lf; 2220 int error; 2221 2222#ifdef MAC 2223 error = mac_kld_check_stat(req->td->td_ucred); 2224 if (error) 2225 return (error); 2226#endif 2227 error = sysctl_wire_old_buffer(req, 0); 2228 if (error != 0) 2229 return (error); 2230 sx_xlock(&kld_sx); 2231 TAILQ_FOREACH(lf, &linker_files, link) { 2232 error = LINKER_EACH_FUNCTION_NAME(lf, 2233 sysctl_kern_function_list_iterate, req); 2234 if (error) { 2235 sx_xunlock(&kld_sx); 2236 return (error); 2237 } 2238 } 2239 sx_xunlock(&kld_sx); 2240 return (SYSCTL_OUT(req, "", 1)); 2241} 2242 2243SYSCTL_PROC(_kern, OID_AUTO, function_list, CTLTYPE_OPAQUE | CTLFLAG_RD, 2244 NULL, 0, sysctl_kern_function_list, "", "kernel function list"); 2245