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.40 2008/01/05 14:02:38 swildner 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/lock.h> 40 #include <sys/module.h> 41 #include <sys/linker.h> 42 #include <sys/fcntl.h> 43 #include <sys/libkern.h> 44 #include <sys/nlookup.h> 45 #include <sys/vnode.h> 46 #include <sys/sysctl.h> 47 48 #include <vm/vm_zone.h> 49 50 #ifdef _KERNEL_VIRTUAL 51 #include <dlfcn.h> 52 #endif 53 54 #ifdef KLD_DEBUG 55 int kld_debug = 0; 56 #endif 57 58 /* Metadata from the static kernel */ 59 SET_DECLARE(modmetadata_set, struct mod_metadata); 60 MALLOC_DEFINE(M_LINKER, "kld", "kernel linker"); 61 62 linker_file_t linker_current_file; 63 linker_file_t linker_kernel_file; 64 65 static struct lock lock; /* lock for the file list */ 66 static linker_class_list_t classes; 67 static linker_file_list_t linker_files; 68 static int next_file_id = 1; 69 70 static void 71 linker_init(void* arg) 72 { 73 lockinit(&lock, "klink", 0, 0); 74 TAILQ_INIT(&classes); 75 TAILQ_INIT(&linker_files); 76 } 77 78 SYSINIT(linker, SI_BOOT2_KLD, SI_ORDER_FIRST, linker_init, 0); 79 80 int 81 linker_add_class(const char* desc, void* priv, 82 struct linker_class_ops* ops) 83 { 84 linker_class_t lc; 85 86 lc = kmalloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT | M_ZERO); 87 if (!lc) 88 return ENOMEM; 89 90 lc->desc = desc; 91 lc->priv = priv; 92 lc->ops = ops; 93 TAILQ_INSERT_HEAD(&classes, lc, link); 94 95 return 0; 96 } 97 98 static int 99 linker_file_sysinit(linker_file_t lf) 100 { 101 struct sysinit** start, ** stop; 102 struct sysinit** sipp; 103 struct sysinit** xipp; 104 struct sysinit* save; 105 const moduledata_t *moddata; 106 int error; 107 108 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 109 lf->filename)); 110 111 if (linker_file_lookup_set(lf, "sysinit_set", &start, &stop, NULL) != 0) 112 return 0; /* XXX is this correct ? No sysinit ? */ 113 114 /* HACK ALERT! */ 115 for (sipp = start; sipp < stop; sipp++) { 116 if ((*sipp)->func == module_register_init) { 117 moddata = (*sipp)->udata; 118 error = module_register(moddata, lf); 119 if (error) { 120 kprintf("linker_file_sysinit \"%s\" failed to register! %d\n", 121 lf->filename, error); 122 return error; 123 } 124 } 125 } 126 127 /* 128 * Perform a bubble sort of the system initialization objects by 129 * their subsystem (primary key) and order (secondary key). 130 * 131 * Since some things care about execution order, this is the 132 * operation which ensures continued function. 133 */ 134 for (sipp = start; sipp < stop; sipp++) { 135 for (xipp = sipp + 1; xipp < stop; xipp++) { 136 if ((*sipp)->subsystem < (*xipp)->subsystem || 137 ((*sipp)->subsystem == (*xipp)->subsystem && 138 (*sipp)->order <= (*xipp)->order)) 139 continue; /* skip*/ 140 save = *sipp; 141 *sipp = *xipp; 142 *xipp = save; 143 } 144 } 145 146 147 /* 148 * Traverse the (now) ordered list of system initialization tasks. 149 * Perform each task, and continue on to the next task. 150 */ 151 for (sipp = start; sipp < stop; sipp++) { 152 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY) 153 continue; /* skip dummy task(s)*/ 154 155 /* Call function */ 156 (*((*sipp)->func))((*sipp)->udata); 157 } 158 return 0; /* no errors */ 159 } 160 161 static void 162 linker_file_sysuninit(linker_file_t lf) 163 { 164 struct sysinit** start, ** stop; 165 struct sysinit** sipp; 166 struct sysinit** xipp; 167 struct sysinit* save; 168 169 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 170 lf->filename)); 171 172 if (linker_file_lookup_set(lf, "sysuninit_set", &start, &stop, NULL) != 0) 173 return; 174 175 /* 176 * Perform a reverse bubble sort of the system initialization objects 177 * by their subsystem (primary key) and order (secondary key). 178 * 179 * Since some things care about execution order, this is the 180 * operation which ensures continued function. 181 */ 182 for (sipp = start; sipp < stop; sipp++) { 183 for (xipp = sipp + 1; xipp < stop; xipp++) { 184 if ((*sipp)->subsystem > (*xipp)->subsystem || 185 ((*sipp)->subsystem == (*xipp)->subsystem && 186 (*sipp)->order >= (*xipp)->order)) 187 continue; /* skip*/ 188 save = *sipp; 189 *sipp = *xipp; 190 *xipp = save; 191 } 192 } 193 194 195 /* 196 * Traverse the (now) ordered list of system initialization tasks. 197 * Perform each task, and continue on to the next task. 198 */ 199 for (sipp = start; sipp < stop; sipp++) { 200 if ((*sipp)->subsystem == SI_SPECIAL_DUMMY) 201 continue; /* skip dummy task(s)*/ 202 203 /* Call function */ 204 (*((*sipp)->func))((*sipp)->udata); 205 } 206 } 207 208 static void 209 linker_file_register_sysctls(linker_file_t lf) 210 { 211 struct sysctl_oid **start, **stop, **oidp; 212 213 KLD_DPF(FILE, ("linker_file_register_sysctls: registering SYSCTLs for %s\n", 214 lf->filename)); 215 216 if (linker_file_lookup_set(lf, "sysctl_set", &start, &stop, NULL) != 0) 217 return; 218 for (oidp = start; oidp < stop; oidp++) 219 sysctl_register_oid(*oidp); 220 } 221 222 static void 223 linker_file_unregister_sysctls(linker_file_t lf) 224 { 225 struct sysctl_oid **start, **stop, **oidp; 226 227 KLD_DPF(FILE, ("linker_file_unregister_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_unregister_oid(*oidp); 234 } 235 236 int 237 linker_load_file(const char* filename, linker_file_t* result) 238 { 239 linker_class_t lc; 240 linker_file_t lf; 241 int foundfile, error = 0; 242 char *koname = NULL; 243 244 /* Refuse to load modules if securelevel raised */ 245 if (securelevel > 0 || kernel_mem_readonly) 246 return EPERM; 247 248 lf = linker_find_file_by_name(filename); 249 if (lf) { 250 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 251 *result = lf; 252 lf->refs++; 253 goto out; 254 } 255 if (find_mod_metadata(filename)) { 256 if (linker_kernel_file) 257 ++linker_kernel_file->refs; 258 *result = linker_kernel_file; 259 goto out; 260 } 261 262 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 263 if (koname == NULL) { 264 error = ENOMEM; 265 goto out; 266 } 267 ksprintf(koname, "%s.ko", filename); 268 lf = NULL; 269 foundfile = 0; 270 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 271 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 272 filename, lc->desc)); 273 274 error = lc->ops->load_file(koname, &lf); /* First with .ko */ 275 if (lf == NULL && error == ENOENT) 276 error = lc->ops->load_file(filename, &lf); /* Then try without */ 277 /* 278 * If we got something other than ENOENT, then it exists but we cannot 279 * load it for some other reason. 280 */ 281 if (error != ENOENT) 282 foundfile = 1; 283 if (lf) { 284 linker_file_register_sysctls(lf); 285 error = linker_file_sysinit(lf); 286 287 *result = lf; 288 goto out; 289 } 290 } 291 /* 292 * Less than ideal, but tells the user whether it failed to load or 293 * the module was not found. 294 */ 295 if (foundfile) { 296 /* 297 * Format not recognized or otherwise unloadable. 298 * When loading a module that is statically built into 299 * the kernel EEXIST percolates back up as the return 300 * value. Preserve this so that apps can recognize this 301 * special case. 302 */ 303 if (error != EEXIST) 304 error = ENOEXEC; 305 } else { 306 error = ENOENT; /* Nothing found */ 307 } 308 309 out: 310 if (koname) 311 kfree(koname, M_LINKER); 312 return error; 313 } 314 315 linker_file_t 316 linker_find_file_by_name(const char* filename) 317 { 318 linker_file_t lf = 0; 319 char *koname; 320 int i; 321 322 for (i = strlen(filename); i > 0 && filename[i-1] != '/'; --i) 323 ; 324 filename += i; 325 326 koname = kmalloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 327 if (koname == NULL) 328 goto out; 329 ksprintf(koname, "%s.ko", filename); 330 331 lockmgr(&lock, LK_SHARED); 332 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 333 if (!strcmp(lf->filename, koname)) 334 break; 335 if (!strcmp(lf->filename, filename)) 336 break; 337 } 338 lockmgr(&lock, LK_RELEASE); 339 340 out: 341 if (koname) 342 kfree(koname, M_LINKER); 343 return lf; 344 } 345 346 linker_file_t 347 linker_find_file_by_id(int fileid) 348 { 349 linker_file_t lf = 0; 350 351 lockmgr(&lock, LK_SHARED); 352 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) 353 if (lf->id == fileid) 354 break; 355 lockmgr(&lock, LK_RELEASE); 356 357 return lf; 358 } 359 360 linker_file_t 361 linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) 362 { 363 linker_file_t lf = 0; 364 int namelen; 365 const char *filename; 366 367 filename = rindex(pathname, '/'); 368 if (filename && filename[1]) 369 filename++; 370 else 371 filename = pathname; 372 373 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 374 lockmgr(&lock, LK_EXCLUSIVE); 375 namelen = strlen(filename) + 1; 376 lf = kmalloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK); 377 if (!lf) 378 goto out; 379 bzero(lf, sizeof(*lf)); 380 381 lf->refs = 1; 382 lf->userrefs = 0; 383 lf->flags = 0; 384 lf->filename = (char*) (lf + 1); 385 strcpy(lf->filename, filename); 386 lf->id = next_file_id++; 387 lf->ndeps = 0; 388 lf->deps = NULL; 389 STAILQ_INIT(&lf->common); 390 TAILQ_INIT(&lf->modules); 391 392 lf->priv = priv; 393 lf->ops = ops; 394 TAILQ_INSERT_TAIL(&linker_files, lf, link); 395 396 out: 397 lockmgr(&lock, LK_RELEASE); 398 return lf; 399 } 400 401 int 402 linker_file_unload(linker_file_t file) 403 { 404 module_t mod, next; 405 struct common_symbol* cp; 406 int error = 0; 407 int i; 408 409 /* Refuse to unload modules if securelevel raised */ 410 if (securelevel > 0 || kernel_mem_readonly) 411 return EPERM; 412 413 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 414 lockmgr(&lock, LK_EXCLUSIVE); 415 if (file->refs == 1) { 416 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 417 /* 418 * Temporarily bump file->refs to prevent recursive unloading 419 */ 420 ++file->refs; 421 422 /* 423 * Inform any modules associated with this file. 424 */ 425 mod = TAILQ_FIRST(&file->modules); 426 while (mod) { 427 /* 428 * Give the module a chance to veto the unload. Note that the 429 * act of unloading the module may cause other modules in the 430 * same file list to be unloaded recursively. 431 */ 432 if ((error = module_unload(mod)) != 0) { 433 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 434 mod)); 435 lockmgr(&lock, LK_RELEASE); 436 file->refs--; 437 goto out; 438 } 439 440 /* 441 * Recursive relationships may prevent the release from 442 * immediately removing the module, or may remove other 443 * modules in the list. 444 */ 445 next = module_getfnext(mod); 446 module_release(mod); 447 mod = next; 448 } 449 450 /* 451 * Since we intend to destroy the file structure, we expect all 452 * modules to have been removed by now. 453 */ 454 for (mod = TAILQ_FIRST(&file->modules); 455 mod; 456 mod = module_getfnext(mod) 457 ) { 458 kprintf("linker_file_unload: module %p still has refs!\n", mod); 459 } 460 --file->refs; 461 } 462 463 file->refs--; 464 if (file->refs > 0) { 465 lockmgr(&lock, LK_RELEASE); 466 goto out; 467 } 468 469 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 470 if (file->flags & LINKER_FILE_LINKED) { 471 linker_file_sysuninit(file); 472 linker_file_unregister_sysctls(file); 473 } 474 475 TAILQ_REMOVE(&linker_files, file, link); 476 lockmgr(&lock, LK_RELEASE); 477 478 for (i = 0; i < file->ndeps; i++) 479 linker_file_unload(file->deps[i]); 480 kfree(file->deps, M_LINKER); 481 482 for (cp = STAILQ_FIRST(&file->common); cp; 483 cp = STAILQ_FIRST(&file->common)) { 484 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 485 kfree(cp, M_LINKER); 486 } 487 488 file->ops->unload(file); 489 kfree(file, M_LINKER); 490 491 out: 492 return error; 493 } 494 495 int 496 linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 497 { 498 linker_file_t* newdeps; 499 500 newdeps = kmalloc((file->ndeps + 1) * sizeof(linker_file_t*), 501 M_LINKER, M_WAITOK | M_ZERO); 502 if (newdeps == NULL) 503 return ENOMEM; 504 505 if (file->deps) { 506 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 507 kfree(file->deps, M_LINKER); 508 } 509 file->deps = newdeps; 510 file->deps[file->ndeps] = dep; 511 file->ndeps++; 512 513 return 0; 514 } 515 516 /* 517 * Locate a linker set and its contents. 518 * This is a helper function to avoid linker_if.h exposure elsewhere. 519 * Note: firstp and lastp are really void *** 520 */ 521 int 522 linker_file_lookup_set(linker_file_t file, const char *name, 523 void *firstp, void *lastp, int *countp) 524 { 525 return file->ops->lookup_set(file, name, firstp, lastp, countp); 526 } 527 528 int 529 linker_file_lookup_symbol(linker_file_t file, const char* name, int deps, caddr_t *raddr) 530 { 531 c_linker_sym_t sym; 532 linker_symval_t symval; 533 linker_file_t lf; 534 size_t common_size = 0; 535 int i; 536 537 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 538 file, name, deps)); 539 540 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 541 file->ops->symbol_values(file, sym, &symval); 542 543 /* 544 * XXX Assume a common symbol if its value is 0 and it has a non-zero 545 * size, otherwise it could be an absolute symbol with a value of 0. 546 */ 547 if (symval.value == 0 && symval.size != 0) { 548 /* 549 * For commons, first look them up in the dependancies and 550 * only allocate space if not found there. 551 */ 552 common_size = symval.size; 553 } else { 554 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 555 *raddr = symval.value; 556 return 0; 557 } 558 } 559 if (deps) { 560 for (i = 0; i < file->ndeps; i++) { 561 if (linker_file_lookup_symbol(file->deps[i], name, 0, raddr) == 0) { 562 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", *raddr)); 563 return 0; 564 } 565 } 566 567 /* If we have not found it in the dependencies, search globally */ 568 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 569 /* But skip the current file if it's on the list */ 570 if (lf == file) 571 continue; 572 /* And skip the files we searched above */ 573 for (i = 0; i < file->ndeps; i++) 574 if (lf == file->deps[i]) 575 break; 576 if (i < file->ndeps) 577 continue; 578 if (linker_file_lookup_symbol(lf, name, 0, raddr) == 0) { 579 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", *raddr)); 580 return 0; 581 } 582 } 583 } 584 585 if (common_size > 0) { 586 /* 587 * This is a common symbol which was not found in the 588 * dependancies. We maintain a simple common symbol table in 589 * the file object. 590 */ 591 struct common_symbol* cp; 592 593 for (cp = STAILQ_FIRST(&file->common); cp; 594 cp = STAILQ_NEXT(cp, link)) 595 if (!strcmp(cp->name, name)) { 596 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 597 *raddr = cp->address; 598 return 0; 599 } 600 601 /* 602 * Round the symbol size up to align. 603 */ 604 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 605 cp = kmalloc(sizeof(struct common_symbol) 606 + common_size 607 + strlen(name) + 1, 608 M_LINKER, M_WAITOK | M_ZERO); 609 if (!cp) { 610 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 611 return ENOMEM; 612 } 613 614 cp->address = (caddr_t) (cp + 1); 615 cp->name = cp->address + common_size; 616 strcpy(cp->name, name); 617 bzero(cp->address, common_size); 618 STAILQ_INSERT_TAIL(&file->common, cp, link); 619 620 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 621 *raddr = cp->address; 622 return 0; 623 } 624 625 #ifdef _KERNEL_VIRTUAL 626 *raddr = dlsym(RTLD_NEXT, name); 627 if (*raddr != NULL) { 628 KLD_DPF(SYM, ("linker_file_lookup_symbol: found dlsym=%x\n", *raddr)); 629 return 0; 630 } 631 #endif 632 633 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 634 return ENOENT; 635 } 636 637 #ifdef DDB 638 /* 639 * DDB Helpers. DDB has to look across multiple files with their own 640 * symbol tables and string tables. 641 * 642 * Note that we do not obey list locking protocols here. We really don't 643 * need DDB to hang because somebody's got the lock held. We'll take the 644 * chance that the files list is inconsistant instead. 645 */ 646 647 int 648 linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 649 { 650 linker_file_t lf; 651 652 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 653 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0) 654 return 0; 655 } 656 return ENOENT; 657 } 658 659 int 660 linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 661 { 662 linker_file_t lf; 663 u_long off = (uintptr_t)value; 664 u_long diff, bestdiff; 665 c_linker_sym_t best; 666 c_linker_sym_t es; 667 668 best = 0; 669 bestdiff = off; 670 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 671 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0) 672 continue; 673 if (es != 0 && diff < bestdiff) { 674 best = es; 675 bestdiff = diff; 676 } 677 if (bestdiff == 0) 678 break; 679 } 680 if (best) { 681 *sym = best; 682 *diffp = bestdiff; 683 return 0; 684 } else { 685 *sym = 0; 686 *diffp = off; 687 return ENOENT; 688 } 689 } 690 691 int 692 linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 693 { 694 linker_file_t lf; 695 696 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 697 if (lf->ops->symbol_values(lf, sym, symval) == 0) 698 return 0; 699 } 700 return ENOENT; 701 } 702 703 #endif 704 705 /* 706 * Syscalls. 707 */ 708 709 int 710 sys_kldload(struct kldload_args *uap) 711 { 712 struct thread *td = curthread; 713 char* filename = NULL, *modulename; 714 linker_file_t lf; 715 int error = 0; 716 717 uap->sysmsg_result = -1; 718 719 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */ 720 return EPERM; 721 722 if ((error = suser(td)) != 0) 723 return error; 724 725 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 726 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0) 727 goto out; 728 729 /* Can't load more than one module with the same name */ 730 modulename = rindex(filename, '/'); 731 if (modulename == NULL) 732 modulename = filename; 733 else 734 modulename++; 735 if (linker_find_file_by_name(modulename)) { 736 error = EEXIST; 737 goto out; 738 } 739 740 if ((error = linker_load_file(filename, &lf)) != 0) 741 goto out; 742 743 lf->userrefs++; 744 uap->sysmsg_result = lf->id; 745 746 out: 747 if (filename) 748 kfree(filename, M_TEMP); 749 return error; 750 } 751 752 int 753 sys_kldunload(struct kldunload_args *uap) 754 { 755 struct thread *td = curthread; 756 linker_file_t lf; 757 int error = 0; 758 759 if (securelevel > 0 || kernel_mem_readonly) /* redundant, but that's OK */ 760 return EPERM; 761 762 if ((error = suser(td)) != 0) 763 return error; 764 765 lf = linker_find_file_by_id(uap->fileid); 766 if (lf) { 767 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 768 if (lf->userrefs == 0) { 769 kprintf("linkerunload: attempt to unload file that was loaded by the kernel\n"); 770 error = EBUSY; 771 goto out; 772 } 773 lf->userrefs--; 774 error = linker_file_unload(lf); 775 if (error) 776 lf->userrefs++; 777 } else 778 error = ENOENT; 779 780 out: 781 return error; 782 } 783 784 int 785 sys_kldfind(struct kldfind_args *uap) 786 { 787 char *filename = NULL, *modulename; 788 linker_file_t lf; 789 int error = 0; 790 791 uap->sysmsg_result = -1; 792 793 filename = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 794 if ((error = copyinstr(uap->file, filename, MAXPATHLEN, NULL)) != 0) 795 goto out; 796 797 modulename = rindex(filename, '/'); 798 if (modulename == NULL) 799 modulename = filename; 800 801 lf = linker_find_file_by_name(modulename); 802 if (lf) 803 uap->sysmsg_result = lf->id; 804 else 805 error = ENOENT; 806 807 out: 808 if (filename) 809 kfree(filename, M_TEMP); 810 return error; 811 } 812 813 int 814 sys_kldnext(struct kldnext_args *uap) 815 { 816 linker_file_t lf; 817 int error = 0; 818 819 if (uap->fileid == 0) { 820 if (TAILQ_FIRST(&linker_files)) 821 uap->sysmsg_result = TAILQ_FIRST(&linker_files)->id; 822 else 823 uap->sysmsg_result = 0; 824 return 0; 825 } 826 827 lf = linker_find_file_by_id(uap->fileid); 828 if (lf) { 829 if (TAILQ_NEXT(lf, link)) 830 uap->sysmsg_result = TAILQ_NEXT(lf, link)->id; 831 else 832 uap->sysmsg_result = 0; 833 } else 834 error = ENOENT; 835 836 return error; 837 } 838 839 int 840 sys_kldstat(struct kldstat_args *uap) 841 { 842 linker_file_t lf; 843 int error = 0; 844 int version; 845 struct kld_file_stat* stat; 846 int namelen; 847 848 lf = linker_find_file_by_id(uap->fileid); 849 if (!lf) { 850 error = ENOENT; 851 goto out; 852 } 853 854 stat = uap->stat; 855 856 /* 857 * Check the version of the user's structure. 858 */ 859 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 860 goto out; 861 if (version != sizeof(struct kld_file_stat)) { 862 error = EINVAL; 863 goto out; 864 } 865 866 namelen = strlen(lf->filename) + 1; 867 if (namelen > MAXPATHLEN) 868 namelen = MAXPATHLEN; 869 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 870 goto out; 871 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 872 goto out; 873 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 874 goto out; 875 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 876 goto out; 877 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 878 goto out; 879 880 uap->sysmsg_result = 0; 881 882 out: 883 return error; 884 } 885 886 int 887 sys_kldfirstmod(struct kldfirstmod_args *uap) 888 { 889 linker_file_t lf; 890 int error = 0; 891 892 lf = linker_find_file_by_id(uap->fileid); 893 if (lf) { 894 if (TAILQ_FIRST(&lf->modules)) 895 uap->sysmsg_result = module_getid(TAILQ_FIRST(&lf->modules)); 896 else 897 uap->sysmsg_result = 0; 898 } else 899 error = ENOENT; 900 901 return error; 902 } 903 904 int 905 sys_kldsym(struct kldsym_args *uap) 906 { 907 char *symstr = NULL; 908 c_linker_sym_t sym; 909 linker_symval_t symval; 910 linker_file_t lf; 911 struct kld_sym_lookup lookup; 912 int error = 0; 913 914 if ((error = copyin(uap->data, &lookup, sizeof(lookup))) != 0) 915 goto out; 916 if (lookup.version != sizeof(lookup) || uap->cmd != KLDSYM_LOOKUP) { 917 error = EINVAL; 918 goto out; 919 } 920 921 symstr = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); 922 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 923 goto out; 924 925 if (uap->fileid != 0) { 926 lf = linker_find_file_by_id(uap->fileid); 927 if (lf == NULL) { 928 error = ENOENT; 929 goto out; 930 } 931 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 932 lf->ops->symbol_values(lf, sym, &symval) == 0) { 933 lookup.symvalue = (uintptr_t)symval.value; 934 lookup.symsize = symval.size; 935 error = copyout(&lookup, uap->data, sizeof(lookup)); 936 } else 937 error = ENOENT; 938 } else { 939 for (lf = TAILQ_FIRST(&linker_files); lf; lf = TAILQ_NEXT(lf, link)) { 940 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 941 lf->ops->symbol_values(lf, sym, &symval) == 0) { 942 lookup.symvalue = (uintptr_t)symval.value; 943 lookup.symsize = symval.size; 944 error = copyout(&lookup, uap->data, sizeof(lookup)); 945 break; 946 } 947 } 948 if (!lf) 949 error = ENOENT; 950 } 951 out: 952 if (symstr) 953 kfree(symstr, M_TEMP); 954 return error; 955 } 956 957 /* 958 * Look for module metadata in the static kernel 959 */ 960 struct mod_metadata * 961 find_mod_metadata(const char *modname) 962 { 963 int len; 964 struct mod_metadata **mdp; 965 struct mod_metadata *mdt; 966 967 /* 968 * Strip path prefixes and any dot extension. MDT_MODULE names 969 * are just the module name without a path or ".ko". 970 */ 971 for (len = strlen(modname) - 1; len >= 0; --len) { 972 if (modname[len] == '/') 973 break; 974 } 975 modname += len + 1; 976 for (len = 0; modname[len] && modname[len] != '.'; ++len) 977 ; 978 979 /* 980 * Look for the module declaration 981 */ 982 SET_FOREACH(mdp, modmetadata_set) { 983 mdt = *mdp; 984 if (mdt->md_type != MDT_MODULE) 985 continue; 986 if (strlen(mdt->md_cval) == len && 987 strncmp(mdt->md_cval, modname, len) == 0) { 988 return(mdt); 989 } 990 } 991 return(NULL); 992 } 993 994 /* 995 * Preloaded module support 996 */ 997 static void 998 linker_preload(void* arg) 999 { 1000 caddr_t modptr; 1001 char *modname; 1002 char *modtype; 1003 linker_file_t lf; 1004 linker_class_t lc; 1005 int error; 1006 struct sysinit **sipp; 1007 const moduledata_t *moddata; 1008 struct sysinit **si_start, **si_stop; 1009 1010 modptr = NULL; 1011 while ((modptr = preload_search_next_name(modptr)) != NULL) { 1012 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 1013 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 1014 if (modname == NULL) { 1015 kprintf("Preloaded module at %p does not have a name!\n", modptr); 1016 continue; 1017 } 1018 if (modtype == NULL) { 1019 kprintf("Preloaded module at %p does not have a type!\n", modptr); 1020 continue; 1021 } 1022 1023 /* 1024 * This is a hack at the moment, but what's in FreeBSD-5 is even 1025 * worse so I'd rather the hack. 1026 */ 1027 kprintf("Preloaded %s \"%s\" at %p", modtype, modname, modptr); 1028 if (find_mod_metadata(modname)) { 1029 kprintf(" (ignored, already in static kernel)\n"); 1030 continue; 1031 } 1032 kprintf(".\n"); 1033 1034 lf = linker_find_file_by_name(modname); 1035 if (lf) { 1036 lf->refs++; 1037 lf->userrefs++; 1038 continue; 1039 } 1040 lf = NULL; 1041 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 1042 error = lc->ops->load_file(modname, &lf); 1043 if (error) { 1044 lf = NULL; 1045 break; 1046 } 1047 } 1048 if (lf) { 1049 lf->userrefs++; 1050 1051 if (linker_file_lookup_set(lf, "sysinit_set", &si_start, &si_stop, NULL) == 0) { 1052 /* HACK ALERT! 1053 * This is to set the sysinit moduledata so that the module 1054 * can attach itself to the correct containing file. 1055 * The sysinit could be run at *any* time. 1056 */ 1057 for (sipp = si_start; sipp < si_stop; sipp++) { 1058 if ((*sipp)->func == module_register_init) { 1059 moddata = (*sipp)->udata; 1060 error = module_register(moddata, lf); 1061 if (error) 1062 kprintf("Preloaded %s \"%s\" failed to register: %d\n", 1063 modtype, modname, error); 1064 } 1065 } 1066 sysinit_add(si_start, si_stop); 1067 } 1068 linker_file_register_sysctls(lf); 1069 } 1070 } 1071 } 1072 1073 SYSINIT(preload, SI_BOOT2_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 1074 1075 /* 1076 * Search for a not-loaded module by name. 1077 * 1078 * Modules may be found in the following locations: 1079 * 1080 * - preloaded (result is just the module name) 1081 * - on disk (result is full path to module) 1082 * 1083 * If the module name is qualified in any way (contains path, etc.) 1084 * the we simply return a copy of it. 1085 * 1086 * The search path can be manipulated via sysctl. Note that we use the ';' 1087 * character as a separator to be consistent with the bootloader. 1088 */ 1089 1090 static char linker_path[MAXPATHLEN] = "/;/boot;/modules"; 1091 1092 SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 1093 sizeof(linker_path), "module load search path"); 1094 TUNABLE_STR("module_path", linker_path, sizeof(linker_path)); 1095 1096 static char * 1097 linker_strdup(const char *str) 1098 { 1099 char *result; 1100 1101 if ((result = kmalloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 1102 strcpy(result, str); 1103 return(result); 1104 } 1105 1106 char * 1107 linker_search_path(const char *name) 1108 { 1109 struct nlookupdata nd; 1110 char *cp, *ep, *result; 1111 size_t name_len, prefix_len; 1112 int sep; 1113 int error; 1114 enum vtype type; 1115 1116 /* qualified at all? */ 1117 if (index(name, '/')) 1118 return(linker_strdup(name)); 1119 1120 /* traverse the linker path */ 1121 cp = linker_path; 1122 name_len = strlen(name); 1123 for (;;) { 1124 1125 /* find the end of this component */ 1126 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 1127 ; 1128 prefix_len = ep - cp; 1129 /* if this component doesn't end with a slash, add one */ 1130 if (ep == cp || *(ep - 1) != '/') 1131 sep = 1; 1132 else 1133 sep = 0; 1134 1135 /* 1136 * +2 : possible separator, plus terminator. 1137 */ 1138 result = kmalloc(prefix_len + name_len + 2, M_LINKER, M_WAITOK); 1139 1140 strncpy(result, cp, prefix_len); 1141 if (sep) 1142 result[prefix_len++] = '/'; 1143 strcpy(result + prefix_len, name); 1144 1145 /* 1146 * Attempt to open the file, and return the path if we succeed and it's 1147 * a regular file. 1148 */ 1149 error = nlookup_init(&nd, result, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP); 1150 if (error == 0) 1151 error = vn_open(&nd, NULL, FREAD, 0); 1152 if (error == 0) { 1153 type = nd.nl_open_vp->v_type; 1154 if (type == VREG) { 1155 nlookup_done(&nd); 1156 return (result); 1157 } 1158 } 1159 nlookup_done(&nd); 1160 kfree(result, M_LINKER); 1161 1162 if (*ep == 0) 1163 break; 1164 cp = ep + 1; 1165 } 1166 return(NULL); 1167 } 1168