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