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