1 /*- 2 * Copyright (c) 1997 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/kern/kern_linker.c,v 1.41.2.3 2001/11/21 17:50:35 luigi Exp $ 27 * $DragonFly: src/sys/kern/kern_linker.c,v 1.44 2008/09/01 19:39:44 dillon Exp $ 28 */ 29 30 #include "opt_ddb.h" 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/sysproto.h> 37 #include <sys/sysent.h> 38 #include <sys/proc.h> 39 #include <sys/priv.h> 40 #include <sys/lock.h> 41 #include <sys/module.h> 42 #include <sys/queue.h> 43 #include <sys/linker.h> 44 #include <sys/fcntl.h> 45 #include <sys/libkern.h> 46 #include <sys/nlookup.h> 47 #include <sys/vnode.h> 48 #include <sys/sysctl.h> 49 50 #include <vm/vm_zone.h> 51 52 #ifdef _KERNEL_VIRTUAL 53 #include <dlfcn.h> 54 #endif 55 56 #ifdef KLD_DEBUG 57 int kld_debug = 1; 58 #endif 59 60 /* Metadata from the static kernel */ 61 SET_DECLARE(modmetadata_set, struct mod_metadata); 62 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker"); 63 64 linker_file_t linker_current_file; 65 linker_file_t linker_kernel_file; 66 67 static struct lock lock; /* lock for the file list */ 68 static linker_class_list_t classes; 69 static linker_file_list_t linker_files; 70 static int next_file_id = 1; 71 72 /* XXX wrong name; we're looking at version provision tags here, not modules */ 73 typedef TAILQ_HEAD(, modlist) modlisthead_t; 74 struct modlist { 75 TAILQ_ENTRY(modlist) link; /* chain together all modules */ 76 linker_file_t container; 77 const char *name; 78 int version; 79 }; 80 typedef struct modlist *modlist_t; 81 static modlisthead_t found_modules; 82 83 84 static int linker_load_module(const char *kldname, const char *modname, 85 struct linker_file *parent, struct mod_depend *verinfo, 86 struct linker_file **lfpp); 87 88 static char * 89 linker_strdup(const char *str) 90 { 91 char *result; 92 93 result = kmalloc(strlen(str) + 1, M_LINKER, M_WAITOK); 94 strcpy(result, str); 95 return(result); 96 } 97 98 static void 99 linker_init(void* arg) 100 { 101 lockinit(&lock, "klink", 0, 0); 102 TAILQ_INIT(&classes); 103 TAILQ_INIT(&linker_files); 104 } 105 106 SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0); 107 108 int 109 linker_add_class(const char* desc, void* priv, 110 struct linker_class_ops* ops) 111 { 112 linker_class_t lc; 113 114 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO); 115 if (!lc) 116 return ENOMEM; 117 118 lc->desc = desc; 119 lc->priv = priv; 120 lc->ops = ops; 121 TAILQ_INSERT_HEAD(&classes, lc, link); 122 123 return 0; 124 } 125 126 static void 127 linker_file_sysinit(linker_file_t lf) 128 { 129 struct sysinit** start, ** stop; 130 struct sysinit** sipp; 131 struct sysinit** xipp; 132 struct sysinit* save; 133 134 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 135 lf->filename)); 136 137 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 138 return; 139 140 /* 141 * Perform a bubble sort of the system initialization objects by 142 * their subsystem (primary key) and order (secondary key). 143 * 144 * Since some things care about execution order, this is the 145 * operation which ensures continued function. 146 */ 147 for (sipp = start; sipp < stop; sipp++) { 148 for (xipp = sipp + 1; xipp < stop; xipp++) { 149 if ((*sipp)->subsystem < (*xipp)->subsystem || 150 ((*sipp)->subsystem == (*xipp)->subsystem && 151 (*sipp)->order <= (*xipp)->order)) 152 continue; /* skip*/ 153 save = *sipp; 154 *sipp = *xipp; 155 *xipp = save; 156 } 157 } 158 159 160 /* 161 * Traverse the (now) ordered list of system initialization tasks. 162 * Perform each task, and continue on to the next task. 163 */ 164 for (sipp = start; sipp < stop; sipp++) { 165 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY) 166 continue; /* skip dummy task(s)*/ 167 168 /* Call function */ 169 (*((*sipp)->func))((*sipp)->udata); 170 } 171 } 172 173 static void 174 linker_file_sysuninit(linker_file_t lf) 175 { 176 struct sysinit** start, ** stop; 177 struct sysinit** sipp; 178 struct sysinit** xipp; 179 struct sysinit* save; 180 181 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 182 lf->filename)); 183 184 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0) 185 return; 186 187 /* 188 * Perform a reverse bubble sort of the system initialization objects 189 * by their subsystem (primary key) and order (secondary key). 190 * 191 * Since some things care about execution order, this is the 192 * operation which ensures continued function. 193 */ 194 for (sipp = start; sipp < stop; sipp++) { 195 for (xipp = sipp + 1; xipp < stop; xipp++) { 196 if ((*sipp)->subsystem > (*xipp)->subsystem || 197 ((*sipp)->subsystem == (*xipp)->subsystem && 198 (*sipp)->order >= (*xipp)->order)) 199 continue; /* skip*/ 200 save = *sipp; 201 *sipp = *xipp; 202 *xipp = save; 203 } 204 } 205 206 207 /* 208 * Traverse the (now) ordered list of system initialization tasks. 209 * Perform each task, and continue on to the next task. 210 */ 211 for (sipp = start; sipp < stop; sipp++) { 212 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY) 213 continue; /* skip dummy task(s)*/ 214 215 /* Call function */ 216 (*((*sipp)->func))((*sipp)->udata); 217 } 218 } 219 220 static void 221 linker_file_register_sysctls(linker_file_t lf) 222 { 223 struct sysctl_oid **start, **stop, **oidp; 224 225 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 226 lf->filename)); 227 228 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 229 return; 230 for (oidp = start; oidp < stop; oidp++) 231 sysctl_register_oid(*oidp); 232 } 233 234 static void 235 linker_file_unregister_sysctls(linker_file_t lf) 236 { 237 struct sysctl_oid **start, **stop, **oidp; 238 239 KLD_DPF(FILE, ("linker_file_unregister_sysctls: registering SYSCTLs for %s\n", 240 lf->filename)); 241 242 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 243 return; 244 for (oidp = start; oidp < stop; oidp++) 245 sysctl_unregister_oid(*oidp); 246 } 247 248 static int 249 linker_file_register_modules(linker_file_t lf) 250 { 251 struct mod_metadata **start, **stop, **mdp; 252 const moduledata_t *moddata; 253 int first_error, error; 254 255 KLD_DPF(FILE, ("linker_file_register_modules: registering modules in %s\n", 256 lf->filename)); 257 258 if (linker_file_lookup_set(lf, "modmetadata_set", &start, &stop, NULL) != 0) { 259 /* 260 * This fallback should be unnecessary, but if we get booted 261 * from boot2 instead of loader and we are missing our 262 * metadata then we have to try the best we can. 263 */ 264 if (lf == linker_kernel_file) { 265 start = SET_BEGIN(modmetadata_set); 266 stop = SET_LIMIT(modmetadata_set); 267 } else 268 return (0); 269 } 270 first_error = 0; 271 for (mdp = start; mdp < stop; mdp++) { 272 if ((*mdp)->md_type != MDT_MODULE) 273 continue; 274 moddata = (*mdp)->md_data; 275 KLD_DPF(FILE, ("Registering module %s in %s\n", moddata->name, lf->filename)); 276 error = module_register(moddata, lf); 277 if (error) { 278 kprintf("Module %s failed to register: %d\n", moddata->name, error); 279 if (first_error == 0) 280 first_error = error; 281 } 282 } 283 return (first_error); 284 } 285 286 static void 287 linker_init_kernel_modules(void) 288 { 289 290 linker_file_register_modules(linker_kernel_file); 291 } 292 293 SYSINIT(linker_kernel, SI_BOOT2_KLD, SI_ORDER_ANY, linker_init_kernel_modules, 0); 294 295 int 296 linker_load_file(const char *filename, linker_file_t *result) 297 { 298 linker_class_t lc; 299 linker_file_t lf; 300 int foundfile, error = 0; 301 302 /* Refuse to load modules if securelevel raised */ 303 if (securelevel > 0 || kernel_mem_readonly) 304 return EPERM; 305 306 lf = linker_find_file_by_name(filename); 307 if (lf) { 308 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 309 *result = lf; 310 lf->refs++; 311 goto out; 312 } 313 314 lf = NULL; 315 foundfile = 0; 316 TAILQ_FOREACH(lc, &classes, link) { 317 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 318 filename, lc->desc)); 319 320 error = lc->ops->load_file(filename, &lf); 321 /* 322 * If we got something other than ENOENT, then it exists but we cannot 323 * load it for some other reason. 324 */ 325 if (error != ENOENT) 326 foundfile = 1; 327 if (lf) { 328 error = linker_file_register_modules(lf); 329 if (error == EEXIST) { 330 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */); 331 return (error); 332 } 333 linker_file_register_sysctls(lf); 334 linker_file_sysinit(lf); 335 lf->flags |= LINKER_FILE_LINKED; 336 *result = lf; 337 return (0); 338 } 339 } 340 /* 341 * Less than ideal, but tells the user whether it failed to load or 342 * the module was not found. 343 */ 344 if (foundfile) { 345 /* 346 * If the file type has not been recognized by the last try 347 * printout a message before to fail. 348 */ 349 if (error == ENOSYS) 350 kprintf("linker_load_file: Unsupported file type\n"); 351 352 /* 353 * Format not recognized or otherwise unloadable. 354 * When loading a module that is statically built into 355 * the kernel EEXIST percolates back up as the return 356 * value. Preserve this so that apps can recognize this 357 * special case. 358 */ 359 if (error != EEXIST) 360 error = ENOEXEC; 361 } else { 362 error = ENOENT; /* Nothing found */ 363 } 364 365 out: 366 return error; 367 } 368 369 linker_file_t 370 linker_find_file_by_name(const char* filename) 371 { 372 linker_file_t lf = 0; 373 char *koname; 374 int i; 375 376 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i) 377 ; 378 filename += i; 379 380 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 381 ksprintf(koname, "%s.ko", filename); 382 383 lockmgr(&lock, LK_SHARED); 384 TAILQ_FOREACH(lf, &linker_files, link) { 385 if (!strcmp(lf->filename, koname)) 386 break; 387 if (!strcmp(lf->filename, filename)) 388 break; 389 } 390 lockmgr(&lock, LK_RELEASE); 391 392 if (koname) 393 kfree(koname, M_LINKER); 394 return lf; 395 } 396 397 linker_file_t 398 linker_find_file_by_id(int fileid) 399 { 400 linker_file_t lf = 0; 401 402 lockmgr(&lock, LK_SHARED); 403 TAILQ_FOREACH(lf, &linker_files, link) 404 if (lf->id == fileid) 405 break; 406 lockmgr(&lock, LK_RELEASE); 407 408 return lf; 409 } 410 411 linker_file_t 412 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) 413 { 414 linker_file_t lf = 0; 415 const char *filename; 416 417 filename = rindex(pathname, '/'); 418 if (filename && filename[1]) 419 filename++; 420 else 421 filename = pathname; 422 423 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 424 lockmgr(&lock, LK_EXCLUSIVE); 425 lf = kmalloc(sizeof(struct linker_file), M_LINKER, M_WAITOK); 426 bzero(lf, sizeof(*lf)); 427 428 lf->refs = 1; 429 lf->userrefs = 0; 430 lf->flags = 0; 431 lf->filename = linker_strdup(filename); 432 lf->id = next_file_id++; 433 lf->ndeps = 0; 434 lf->deps = NULL; 435 STAILQ_INIT(&lf->common); 436 TAILQ_INIT(&lf->modules); 437 438 lf->priv = priv; 439 lf->ops = ops; 440 TAILQ_INSERT_TAIL(&linker_files, lf, link); 441 442 lockmgr(&lock, LK_RELEASE); 443 return lf; 444 } 445 446 int 447 linker_file_unload(linker_file_t file) 448 { 449 module_t mod, next;; 450 modlist_t ml, nextml; 451 struct common_symbol* cp; 452 int error = 0; 453 int i; 454 455 /* Refuse to unload modules if securelevel raised */ 456 if (securelevel > 0 || kernel_mem_readonly) 457 return EPERM; 458 459 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 460 461 lockmgr(&lock, LK_EXCLUSIVE); 462 463 /* Easy case of just dropping a reference. */ 464 if (file->refs > 1) { 465 file->refs--; 466 lockmgr(&lock, LK_RELEASE); 467 return (0); 468 } 469 470 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 471 472 /* 473 * Inform any modules associated with this file. 474 */ 475 mod = TAILQ_FIRST(&file->modules); 476 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 477 next = module_getfnext(mod); 478 479 /* 480 * Give the module a chance to veto the unload. Note that the 481 * act of unloading the module may cause other modules in the 482 * same file list to be unloaded recursively. 483 */ 484 if ((error = module_unload(mod)) != 0) { 485 KLD_DPF(FILE, ("linker_file_unload: module %p vetoes unload\n", 486 mod)); 487 lockmgr(&lock, LK_RELEASE); 488 file->refs--; 489 goto out; 490 } 491 module_release(mod); 492 } 493 494 TAILQ_FOREACH_MUTABLE(ml, &found_modules, link, nextml) { 495 if (ml->container == file) { 496 TAILQ_REMOVE(&found_modules, ml, link); 497 kfree(ml, M_LINKER); 498 } 499 } 500 501 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 502 if (file->flags & LINKER_FILE_LINKED) { 503 file->flags &= ~LINKER_FILE_LINKED; 504 lockmgr(&lock, LK_RELEASE); 505 linker_file_sysuninit(file); 506 linker_file_unregister_sysctls(file); 507 lockmgr(&lock, LK_EXCLUSIVE); 508 } 509 510 TAILQ_REMOVE(&linker_files, file, link); 511 512 if (file->deps) { 513 lockmgr(&lock, LK_RELEASE); 514 for (i = 0; i < file->ndeps; i++) 515 linker_file_unload(file->deps[i]); 516 lockmgr(&lock, LK_EXCLUSIVE); 517 kfree(file->deps, M_LINKER); 518 file->deps = NULL; 519 } 520 521 while ((cp = STAILQ_FIRST(&file->common)) != NULL) { 522 STAILQ_REMOVE_HEAD(&file->common, link); 523 kfree(cp, M_LINKER); 524 } 525 526 file->ops->unload(file); 527 528 if (file->filename) { 529 kfree(file->filename, M_LINKER); 530 file->filename = NULL; 531 } 532 533 kfree(file, M_LINKER); 534 535 lockmgr(&lock, LK_RELEASE); 536 537 out: 538 return error; 539 } 540 541 void 542 linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 543 { 544 linker_file_t* newdeps; 545 546 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*), 547 M_LINKER, M_WAITOK | M_ZERO); 548 549 if (file->deps) { 550 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 551 kfree(file->deps, M_LINKER); 552 } 553 file->deps = newdeps; 554 file->deps[file->ndeps] = dep; 555 file->ndeps++; 556 } 557 558 /* 559 * Locate a linker set and its contents. 560 * This is a helper function to avoid linker_if.h exposure elsewhere. 561 * Note: firstp and lastp are really void *** 562 */ 563 int 564 linker_file_lookup_set(linker_file_t file, const char *name, 565 void *firstp, void *lastp, int *countp) 566 { 567 return file->ops->lookup_set(file, name, firstp, lastp, countp); 568 } 569 570 int 571 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr) 572 { 573 c_linker_sym_t sym; 574 linker_symval_t symval; 575 linker_file_t lf; 576 size_t common_size = 0; 577 int i; 578 579 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", 580 file, name, deps)); 581 582 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 583 file->ops->symbol_values(file, sym, &symval); 584 585 /* 586 * XXX Assume a common symbol if its value is 0 and it has a non-zero 587 * size, otherwise it could be an absolute symbol with a value of 0. 588 */ 589 if (symval.value == 0 && symval.size != 0) { 590 /* 591 * For commons, first look them up in the dependancies and 592 * only allocate space if not found there. 593 */ 594 common_size = symval.size; 595 } else { 596 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%p\n", symval.value)); 597 *raddr = symval.value; 598 return 0; 599 } 600 } 601 if (deps) { 602 for (i = 0; i < file->ndeps; i++) { 603 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) { 604 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%p\n", *raddr)); 605 return 0; 606 } 607 } 608 609 /* If we have not found it in the dependencies, search globally */ 610 TAILQ_FOREACH(lf, &linker_files, link) { 611 /* But skip the current file if it's on the list */ 612 if (lf == file) 613 continue; 614 /* And skip the files we searched above */ 615 for (i = 0; i < file->ndeps; i++) 616 if (lf == file->deps[i]) 617 break; 618 if (i < file->ndeps) 619 continue; 620 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) { 621 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%p\n", *raddr)); 622 return 0; 623 } 624 } 625 } 626 627 if (common_size > 0) { 628 /* 629 * This is a common symbol which was not found in the 630 * dependancies. We maintain a simple common symbol table in 631 * the file object. 632 */ 633 struct common_symbol* cp; 634 635 STAILQ_FOREACH(cp, &file->common, link) 636 if (!strcmp(cp->name, name)) { 637 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%p\n", cp->address)); 638 *raddr = cp->address; 639 return 0; 640 } 641 642 /* 643 * Round the symbol size up to align. 644 */ 645 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 646 cp = kmalloc(sizeof(struct common_symbol) 647 + common_size 648 + strlen(name) + 1, 649 M_LINKER, M_WAITOK | M_ZERO); 650 651 cp->address = (caddr_t) (cp + 1); 652 cp->name = cp->address + common_size; 653 strcpy(cp->name, name); 654 bzero(cp->address, common_size); 655 STAILQ_INSERT_TAIL(&file->common, cp, link); 656 657 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%p\n", cp->address)); 658 *raddr = cp->address; 659 return 0; 660 } 661 662 #ifdef _KERNEL_VIRTUAL 663 *raddr = dlsym(RTLD_NEXT, name); 664 if (*raddr != NULL) { 665 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr)); 666 return 0; 667 } 668 #endif 669 670 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 671 return ENOENT; 672 } 673 674 #ifdef DDB 675 /* 676 * DDB Helpers. DDB has to look across multiple files with their own 677 * symbol tables and string tables. 678 * 679 * Note that we do not obey list locking protocols here. We really don't 680 * need DDB to hang because somebody's got the lock held. We'll take the 681 * chance that the files list is inconsistant instead. 682 */ 683 684 int 685 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 686 { 687 linker_file_t lf; 688 689 TAILQ_FOREACH(lf, &linker_files, link) { 690 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0) 691 return 0; 692 } 693 return ENOENT; 694 } 695 696 int 697 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 698 { 699 linker_file_t lf; 700 u_long off = (uintptr_t)value; 701 u_long diff, bestdiff; 702 c_linker_sym_t best; 703 c_linker_sym_t es; 704 705 best = 0; 706 bestdiff = off; 707 TAILQ_FOREACH(lf, &linker_files, link) { 708 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0) 709 continue; 710 if (es != 0 && diff < bestdiff) { 711 best = es; 712 bestdiff = diff; 713 } 714 if (bestdiff == 0) 715 break; 716 } 717 if (best) { 718 *sym = best; 719 *diffp = bestdiff; 720 return 0; 721 } else { 722 *sym = 0; 723 *diffp = off; 724 return ENOENT; 725 } 726 } 727 728 int 729 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 730 { 731 linker_file_t lf; 732 733 TAILQ_FOREACH(lf, &linker_files, link) { 734 if (lf->ops->symbol_values(lf, sym, symval) == 0) 735 return 0; 736 } 737 return ENOENT; 738 } 739 740 #endif 741 742 /* 743 * Syscalls. 744 */ 745 746 int 747 sys_kldload(struct kldload_args *uap) 748 { 749 struct thread *td = curthread; 750 char *file; 751 char *kldname, *modname; 752 linker_file_t lf; 753 int error = 0; 754 755 uap->sysmsg_result = -1; 756 757 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */ 758 return EPERM; 759 760 if ((error = priv_check(td, PRIV_KLD_LOAD)) != 0) 761 return error; 762 763 file = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 764 if ((error = copyinstr(uap->file, file, MAXPATHLEN, NULL)) != 0) 765 goto out; 766 767 /* 768 * If file does not contain a qualified name or any dot in it 769 * (kldname.ko, or kldname.ver.ko) treat it as an interface 770 * name. 771 */ 772 if (index(file, '/') || index(file, '.')) { 773 kldname = file; 774 modname = NULL; 775 } else { 776 kldname = NULL; 777 modname = file; 778 } 779 780 if ((error = linker_load_module(kldname, modname, NULL, NULL, &lf)) != 0) 781 goto out; 782 783 lf->userrefs++; 784 uap->sysmsg_result = lf->id; 785 786 out: 787 if (file) 788 kfree(file, M_TEMP); 789 return error; 790 } 791 792 int 793 sys_kldunload(struct kldunload_args *uap) 794 { 795 struct thread *td = curthread; 796 linker_file_t lf; 797 int error = 0; 798 799 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */ 800 return EPERM; 801 802 if ((error = priv_check(td, PRIV_KLD_UNLOAD)) != 0) 803 return error; 804 805 lf = linker_find_file_by_id(uap->fileid); 806 if (lf) { 807 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 808 if (lf->userrefs == 0) { 809 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n"); 810 error = EBUSY; 811 goto out; 812 } 813 lf->userrefs--; 814 error = linker_file_unload(lf); 815 if (error) 816 lf->userrefs++; 817 } else 818 error = ENOENT; 819 820 out: 821 return error; 822 } 823 824 int 825 sys_kldfind(struct kldfind_args *uap) 826 { 827 char *filename = NULL, *modulename; 828 linker_file_t lf; 829 int error = 0; 830 831 uap->sysmsg_result = -1; 832 833 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 834 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0) 835 goto out; 836 837 modulename = rindex(filename, '/'); 838 if (modulename == NULL) 839 modulename = filename; 840 841 lf = linker_find_file_by_name(modulename); 842 if (lf) 843 uap->sysmsg_result = lf->id; 844 else 845 error = ENOENT; 846 847 out: 848 if (filename) 849 kfree(filename, M_TEMP); 850 return error; 851 } 852 853 int 854 sys_kldnext(struct kldnext_args *uap) 855 { 856 linker_file_t lf; 857 int error = 0; 858 859 if (uap->fileid == 0) 860 lf = TAILQ_FIRST(&linker_files); 861 else { 862 lf = linker_find_file_by_id(uap->fileid); 863 if (lf == NULL) { 864 error = ENOENT; 865 goto out; 866 } 867 lf = TAILQ_NEXT(lf, link); 868 } 869 870 /* Skip partially loaded files. */ 871 while (lf != NULL && !(lf->flags & LINKER_FILE_LINKED)) { 872 lf = TAILQ_NEXT(lf, link); 873 } 874 875 if (lf) 876 uap->sysmsg_result = lf->id; 877 else 878 uap->sysmsg_result = 0; 879 880 out: 881 return error; 882 } 883 884 int 885 sys_kldstat(struct kldstat_args *uap) 886 { 887 linker_file_t lf; 888 int error = 0; 889 int version; 890 struct kld_file_stat* stat; 891 int namelen; 892 893 lf = linker_find_file_by_id(uap->fileid); 894 if (!lf) { 895 error = ENOENT; 896 goto out; 897 } 898 899 stat = uap->stat; 900 901 /* 902 * Check the version of the user's structure. 903 */ 904 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 905 goto out; 906 if (version != sizeof(struct kld_file_stat)) { 907 error = EINVAL; 908 goto out; 909 } 910 911 namelen = strlen(lf->filename) + 1; 912 if (namelen > MAXPATHLEN) 913 namelen = MAXPATHLEN; 914 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 915 goto out; 916 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 917 goto out; 918 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 919 goto out; 920 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 921 goto out; 922 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 923 goto out; 924 925 uap->sysmsg_result = 0; 926 927 out: 928 return error; 929 } 930 931 int 932 sys_kldfirstmod(struct kldfirstmod_args *uap) 933 { 934 linker_file_t lf; 935 int error = 0; 936 937 lf = linker_find_file_by_id(uap->fileid); 938 if (lf) { 939 if (TAILQ_FIRST(&lf->modules)) 940 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules)); 941 else 942 uap->sysmsg_result = 0; 943 } else 944 error = ENOENT; 945 946 return error; 947 } 948 949 int 950 sys_kldsym(struct kldsym_args *uap) 951 { 952 char *symstr = NULL; 953 c_linker_sym_t sym; 954 linker_symval_t symval; 955 linker_file_t lf; 956 struct kld_sym_lookup lookup; 957 int error = 0; 958 959 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 960 goto out; 961 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) { 962 error = EINVAL; 963 goto out; 964 } 965 966 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 967 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 968 goto out; 969 970 if (uap->fileid != 0) { 971 lf = linker_find_file_by_id(uap->fileid); 972 if (lf == NULL) { 973 error = ENOENT; 974 goto out; 975 } 976 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 977 lf->ops->symbol_values(lf, sym, &symval) == 0) { 978 lookup.symvalue = (uintptr_t)symval.value; 979 lookup.symsize = symval.size; 980 error = copyout(&lookup, uap->data, sizeof(lookup)); 981 } else 982 error = ENOENT; 983 } else { 984 TAILQ_FOREACH(lf, &linker_files, link) { 985 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 986 lf->ops->symbol_values(lf, sym, &symval) == 0) { 987 lookup.symvalue = (uintptr_t)symval.value; 988 lookup.symsize = symval.size; 989 error = copyout(&lookup, uap->data, sizeof(lookup)); 990 break; 991 } 992 } 993 if (!lf) 994 error = ENOENT; 995 } 996 out: 997 if (symstr) 998 kfree(symstr, M_TEMP); 999 return error; 1000 } 1001 1002 /* 1003 * Preloaded module support 1004 */ 1005 1006 static modlist_t 1007 modlist_lookup(const char *name, int ver) 1008 { 1009 modlist_t mod; 1010 1011 TAILQ_FOREACH(mod, &found_modules, link) { 1012 if (strcmp(mod->name, name) == 0 && (ver == 0 || mod->version == ver)) 1013 return (mod); 1014 } 1015 return (NULL); 1016 } 1017 1018 static modlist_t 1019 modlist_lookup2(const char *name, struct mod_depend *verinfo) 1020 { 1021 modlist_t mod, bestmod; 1022 int ver; 1023 1024 if (verinfo == NULL) 1025 return (modlist_lookup(name, 0)); 1026 bestmod = NULL; 1027 TAILQ_FOREACH(mod, &found_modules, link) { 1028 if (strcmp(mod->name, name) != 0) 1029 continue; 1030 ver = mod->version; 1031 if (ver == verinfo->md_ver_preferred) 1032 return (mod); 1033 if (ver >= verinfo->md_ver_minimum && 1034 ver <= verinfo->md_ver_maximum && 1035 (bestmod == NULL || ver > bestmod->version)) 1036 bestmod = mod; 1037 } 1038 return (bestmod); 1039 } 1040 1041 static modlist_t 1042 modlist_newmodule(const char *modname, int version, linker_file_t container) 1043 { 1044 modlist_t mod; 1045 1046 mod = kmalloc(sizeof(struct modlist), M_LINKER, M_NOWAIT | M_ZERO); 1047 if (mod == NULL) 1048 panic("no memory for module list"); 1049 mod->container = container; 1050 mod->name = modname; 1051 mod->version = version; 1052 TAILQ_INSERT_TAIL(&found_modules, mod, link); 1053 return (mod); 1054 } 1055 1056 static void 1057 linker_addmodules(linker_file_t lf, struct mod_metadata **start, 1058 struct mod_metadata **stop, int preload) 1059 { 1060 struct mod_metadata *mp, **mdp; 1061 const char *modname; 1062 int ver; 1063 1064 for (mdp = start; mdp < stop; mdp++) { 1065 mp = *mdp; 1066 if (mp->md_type != MDT_VERSION) 1067 continue; 1068 modname = mp->md_cval; 1069 ver = ((struct mod_version *)mp->md_data)->mv_version; 1070 if (modlist_lookup(modname, ver) != NULL) { 1071 kprintf("module %s already present!\n", modname); 1072 /* XXX what can we do? this is a build error. :-( */ 1073 continue; 1074 } 1075 modlist_newmodule(modname, ver, lf); 1076 } 1077 } 1078 1079 static void 1080 linker_preload(void* arg) 1081 { 1082 caddr_t modptr; 1083 const char *modname, *nmodname; 1084 char *modtype; 1085 linker_file_t lf, nlf; 1086 linker_class_t lc; 1087 int error; 1088 linker_file_list_t loaded_files; 1089 linker_file_list_t depended_files; 1090 struct mod_metadata *mp, *nmp; 1091 struct mod_metadata **start, **stop, **mdp, **nmdp; 1092 struct mod_depend *verinfo; 1093 int nver; 1094 int resolves; 1095 modlist_t mod; 1096 struct sysinit **si_start, **si_stop; 1097 1098 TAILQ_INIT(&loaded_files); 1099 TAILQ_INIT(&depended_files); 1100 TAILQ_INIT(&found_modules); 1101 1102 modptr = NULL; 1103 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1104 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1105 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1106 if (modname == NULL) { 1107 kprintf("Preloaded module at %p does not have a name!\n", modptr); 1108 continue; 1109 } 1110 if (modtype == NULL) { 1111 kprintf("Preloaded module at %p does not have a type!\n", modptr); 1112 continue; 1113 } 1114 1115 if (bootverbose) 1116 kprintf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 1117 lf = NULL; 1118 TAILQ_FOREACH(lc, &classes, link) { 1119 error = lc->ops->preload_file(modname, &lf); 1120 if (!error) 1121 break; 1122 lf = NULL; 1123 } 1124 if (lf) 1125 TAILQ_INSERT_TAIL(&loaded_files, lf, loaded); 1126 } 1127 1128 /* 1129 * First get a list of stuff in the kernel. 1130 */ 1131 if (linker_file_lookup_set(linker_kernel_file, MDT_SETNAME, &start, 1132 &stop, NULL) == 0) 1133 linker_addmodules(linker_kernel_file, start, stop, 1); 1134 1135 /* 1136 * This is a once-off kinky bubble sort to resolve relocation 1137 * dependency requirements. 1138 */ 1139 restart: 1140 TAILQ_FOREACH(lf, &loaded_files, loaded) { 1141 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL); 1142 /* 1143 * First, look to see if we would successfully link with this 1144 * stuff. 1145 */ 1146 resolves = 1; /* unless we know otherwise */ 1147 if (!error) { 1148 for (mdp = start; mdp < stop; mdp++) { 1149 mp = *mdp; 1150 if (mp->md_type != MDT_DEPEND) 1151 continue; 1152 modname = mp->md_cval; 1153 verinfo = mp->md_data; 1154 for (nmdp = start; nmdp < stop; nmdp++) { 1155 nmp = *nmdp; 1156 if (nmp->md_type != MDT_VERSION) 1157 continue; 1158 nmodname = nmp->md_cval; 1159 if (strcmp(modname, nmodname) == 0) 1160 break; 1161 } 1162 if (nmdp < stop)/* it's a self reference */ 1163 continue; 1164 1165 /* 1166 * ok, the module isn't here yet, we 1167 * are not finished 1168 */ 1169 if (modlist_lookup2(modname, verinfo) == NULL) 1170 resolves = 0; 1171 } 1172 } 1173 /* 1174 * OK, if we found our modules, we can link. So, "provide" 1175 * the modules inside and add it to the end of the link order 1176 * list. 1177 */ 1178 if (resolves) { 1179 if (!error) { 1180 for (mdp = start; mdp < stop; mdp++) { 1181 mp = *mdp; 1182 if (mp->md_type != MDT_VERSION) 1183 continue; 1184 modname = mp->md_cval; 1185 nver = ((struct mod_version *)mp->md_data)->mv_version; 1186 if (modlist_lookup(modname, nver) != NULL) { 1187 kprintf("module %s already present!\n", modname); 1188 TAILQ_REMOVE(&loaded_files, lf, loaded); 1189 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ ); 1190 /* we changed tailq next ptr */ 1191 goto restart; 1192 } 1193 modlist_newmodule(modname, nver, lf); 1194 } 1195 } 1196 TAILQ_REMOVE(&loaded_files, lf, loaded); 1197 TAILQ_INSERT_TAIL(&depended_files, lf, loaded); 1198 /* 1199 * Since we provided modules, we need to restart the 1200 * sort so that the previous files that depend on us 1201 * have a chance. Also, we've busted the tailq next 1202 * pointer with the REMOVE. 1203 */ 1204 goto restart; 1205 } 1206 } 1207 1208 /* 1209 * At this point, we check to see what could not be resolved.. 1210 */ 1211 while ((lf = TAILQ_FIRST(&loaded_files)) != NULL) { 1212 TAILQ_REMOVE(&loaded_files, lf, loaded); 1213 kprintf("KLD file %s is missing dependencies\n", lf->filename); 1214 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ ); 1215 } 1216 1217 /* 1218 * We made it. Finish off the linking in the order we determined. 1219 */ 1220 TAILQ_FOREACH_MUTABLE(lf, &depended_files, loaded, nlf) { 1221 if (linker_kernel_file) { 1222 linker_kernel_file->refs++; 1223 linker_file_add_dependancy(lf, linker_kernel_file); 1224 } 1225 lf->userrefs++; 1226 1227 error = linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, NULL); 1228 if (!error) { 1229 for (mdp = start; mdp < stop; mdp++) { 1230 mp = *mdp; 1231 if (mp->md_type != MDT_DEPEND) 1232 continue; 1233 modname = mp->md_cval; 1234 verinfo = mp->md_data; 1235 mod = modlist_lookup2(modname, verinfo); 1236 /* Don't count self-dependencies */ 1237 if (lf == mod->container) 1238 continue; 1239 mod->container->refs++; 1240 linker_file_add_dependancy(lf, mod->container); 1241 } 1242 } 1243 /* 1244 * Now do relocation etc using the symbol search paths 1245 * established by the dependencies 1246 */ 1247 error = lf->ops->preload_finish(lf); 1248 if (error) { 1249 TAILQ_REMOVE(&depended_files, lf, loaded); 1250 kprintf("KLD file %s - could not finalize loading\n", 1251 lf->filename); 1252 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */); 1253 continue; 1254 } 1255 linker_file_register_modules(lf); 1256 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) 1257 sysinit_add(si_start, si_stop); 1258 linker_file_register_sysctls(lf); 1259 lf->flags |= LINKER_FILE_LINKED; 1260 } 1261 /* woohoo! we made it! */ 1262 } 1263 1264 SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1265 1266 /* 1267 * Search for a not-loaded module by name. 1268 * 1269 * Modules may be found in the following locations: 1270 * 1271 * - preloaded (result is just the module name) 1272 * - on disk (result is full path to module) 1273 * 1274 * If the module name is qualified in any way (contains path, etc.) 1275 * the we simply return a copy of it. 1276 * 1277 * The search path can be manipulated via sysctl. Note that we use the ';' 1278 * character as a separator to be consistent with the bootloader. 1279 */ 1280 1281 static char linker_path[MAXPATHLEN] = "/boot;/boot/modules;/;/modules"; 1282 1283 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1284 sizeof(linker_path), "module load search path"); 1285 TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1286 1287 char * 1288 linker_search_path(const char *name) 1289 { 1290 struct nlookupdata nd; 1291 char *cp, *ep, *result; 1292 size_t name_len, prefix_len; 1293 size_t result_len; 1294 int sep; 1295 int error; 1296 enum vtype type; 1297 const char *exts[] = { "", ".ko", NULL }; 1298 const char **ext; 1299 1300 /* qualified at all? */ 1301 if (index(name, '/')) 1302 return(linker_strdup(name)); 1303 1304 /* traverse the linker path */ 1305 cp = linker_path; 1306 name_len = strlen(name); 1307 for (;;) { 1308 1309 /* find the end of this component */ 1310 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1311 ; 1312 prefix_len = ep - cp; 1313 /* if this component doesn't end with a slash, add one */ 1314 if (ep == cp || *(ep - 1) != '/') 1315 sep = 1; 1316 else 1317 sep = 0; 1318 1319 /* 1320 * +2+3 : possible separator, plus terminator + possible extension. 1321 */ 1322 result = kmalloc(prefix_len + name_len + 2+3, M_LINKER, M_WAITOK); 1323 1324 strncpy(result, cp, prefix_len); 1325 if (sep) 1326 result[prefix_len++] = '/'; 1327 strcpy(result + prefix_len, name); 1328 1329 result_len = strlen(result); 1330 for (ext = exts; *ext != NULL; ext++) { 1331 strcpy(result + result_len, *ext); 1332 1333 /* 1334 * Attempt to open the file, and return the path if we succeed and it's 1335 * a regular file. 1336 */ 1337 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP); 1338 if (error == 0) 1339 error = vn_open(&nd, NULL, FREAD, 0); 1340 if (error == 0) { 1341 type = nd.nl_open_vp->v_type; 1342 if (type == VREG) { 1343 nlookup_done(&nd); 1344 return (result); 1345 } 1346 } 1347 nlookup_done(&nd); 1348 } 1349 1350 kfree(result, M_LINKER); 1351 1352 if (*ep == 0) 1353 break; 1354 cp = ep + 1; 1355 } 1356 return(NULL); 1357 } 1358 1359 /* 1360 * Find a file which contains given module and load it, if "parent" is not 1361 * NULL, register a reference to it. 1362 */ 1363 static int 1364 linker_load_module(const char *kldname, const char *modname, 1365 struct linker_file *parent, struct mod_depend *verinfo, 1366 struct linker_file **lfpp) 1367 { 1368 linker_file_t lfdep; 1369 const char *filename; 1370 char *pathname; 1371 int error; 1372 1373 if (modname == NULL) { 1374 /* 1375 * We have to load KLD 1376 */ 1377 KASSERT(verinfo == NULL, ("linker_load_module: verinfo is not NULL")); 1378 pathname = linker_search_path(kldname); 1379 } else { 1380 if (modlist_lookup2(modname, verinfo) != NULL) 1381 return (EEXIST); 1382 if (kldname != NULL) 1383 pathname = linker_strdup(kldname); 1384 else if (rootvnode == NULL) 1385 pathname = NULL; 1386 else 1387 pathname = linker_search_path(modname); 1388 #if 0 1389 /* 1390 * Need to find a KLD with required module 1391 */ 1392 pathname = linker_search_module(modname, 1393 strlen(modname), verinfo); 1394 #endif 1395 } 1396 if (pathname == NULL) 1397 return (ENOENT); 1398 1399 /* 1400 * Can't load more than one file with the same basename XXX: 1401 * Actually it should be possible to have multiple KLDs with 1402 * the same basename but different path because they can 1403 * provide different versions of the same modules. 1404 */ 1405 filename = rindex(pathname, '/'); 1406 if (filename == NULL) 1407 filename = filename; 1408 else 1409 filename++; 1410 if (linker_find_file_by_name(filename)) 1411 error = EEXIST; 1412 else 1413 do { 1414 error = linker_load_file(pathname, &lfdep); 1415 if (error) 1416 break; 1417 if (modname && verinfo && modlist_lookup2(modname, verinfo) == NULL) { 1418 linker_file_unload(lfdep /* , LINKER_UNLOAD_FORCE */ ); 1419 error = ENOENT; 1420 break; 1421 } 1422 if (parent) { 1423 linker_file_add_dependancy(parent, lfdep); 1424 } 1425 if (lfpp) 1426 *lfpp = lfdep; 1427 } while (0); 1428 kfree(pathname, M_LINKER); 1429 return (error); 1430 } 1431 1432 /* 1433 * This routine is responsible for finding dependencies of userland initiated 1434 * kldload(2)'s of files. 1435 */ 1436 int 1437 linker_load_dependencies(linker_file_t lf) 1438 { 1439 linker_file_t lfdep; 1440 struct mod_metadata **start, **stop, **mdp, **nmdp; 1441 struct mod_metadata *mp, *nmp; 1442 struct mod_depend *verinfo; 1443 modlist_t mod; 1444 const char *modname, *nmodname; 1445 int ver, error = 0, count; 1446 1447 /* 1448 * All files are dependant on /kernel. 1449 */ 1450 if (linker_kernel_file) { 1451 linker_kernel_file->refs++; 1452 linker_file_add_dependancy(lf, linker_kernel_file); 1453 } 1454 if (linker_file_lookup_set(lf, MDT_SETNAME, &start, &stop, &count) != 0) 1455 return (0); 1456 for (mdp = start; mdp < stop; mdp++) { 1457 mp = *mdp; 1458 if (mp->md_type != MDT_VERSION) 1459 continue; 1460 modname = mp->md_cval; 1461 ver = ((struct mod_version *)mp->md_data)->mv_version; 1462 mod = modlist_lookup(modname, ver); 1463 if (mod != NULL) { 1464 kprintf("interface %s.%d already present in the KLD '%s'!\n", 1465 modname, ver, mod->container->filename); 1466 return (EEXIST); 1467 } 1468 } 1469 1470 for (mdp = start; mdp < stop; mdp++) { 1471 mp = *mdp; 1472 if (mp->md_type != MDT_DEPEND) 1473 continue; 1474 modname = mp->md_cval; 1475 verinfo = mp->md_data; 1476 nmodname = NULL; 1477 for (nmdp = start; nmdp < stop; nmdp++) { 1478 nmp = *nmdp; 1479 if (nmp->md_type != MDT_VERSION) 1480 continue; 1481 nmodname = nmp->md_cval; 1482 if (strcmp(modname, nmodname) == 0) 1483 break; 1484 } 1485 if (nmdp < stop) /* early exit, it's a self reference */ 1486 continue; 1487 mod = modlist_lookup2(modname, verinfo); 1488 if (mod) { /* woohoo, it's loaded already */ 1489 lfdep = mod->container; 1490 lfdep->refs++; 1491 linker_file_add_dependancy(lf, lfdep); 1492 continue; 1493 } 1494 error = linker_load_module(NULL, modname, lf, verinfo, NULL); 1495 if (error) { 1496 kprintf("KLD %s: depends on %s - not available or version mismatch\n", 1497 lf->filename, modname); 1498 break; 1499 } 1500 } 1501 1502 if (error) 1503 return (error); 1504 linker_addmodules(lf, start, stop, 0); 1505 return (error); 1506 } 1507