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