kern_linker.c revision 43309
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 * $Id: kern_linker.c,v 1.24 1999/01/27 21:49:56 dillon 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/sysproto.h> 36#include <sys/sysent.h> 37#include <sys/proc.h> 38#include <sys/lock.h> 39#include <machine/cpu.h> 40#include <machine/bootinfo.h> 41#include <sys/module.h> 42#include <sys/linker.h> 43#include <sys/unistd.h> 44#include <sys/fcntl.h> 45#include <sys/libkern.h> 46#include <sys/namei.h> 47#include <sys/vnode.h> 48#include <sys/sysctl.h> 49 50#ifdef KLD_DEBUG 51int kld_debug = 0; 52#endif 53 54MALLOC_DEFINE(M_LINKER, "kld", "kernel linker"); 55linker_file_t linker_current_file; 56linker_file_t linker_kernel_file; 57 58static struct lock lock; /* lock for the file list */ 59static linker_class_list_t classes; 60static linker_file_list_t files; 61static int next_file_id = 1; 62 63static void 64linker_init(void* arg) 65{ 66 lockinit(&lock, PVM, "klink", 0, 0); 67 TAILQ_INIT(&classes); 68 TAILQ_INIT(&files); 69} 70 71SYSINIT(linker, SI_SUB_KLD, SI_ORDER_FIRST, linker_init, 0); 72 73int 74linker_add_class(const char* desc, void* priv, 75 struct linker_class_ops* ops) 76{ 77 linker_class_t lc; 78 79 lc = malloc(sizeof(struct linker_class), M_LINKER, M_NOWAIT); 80 if (!lc) 81 return ENOMEM; 82 bzero(lc, sizeof(*lc)); 83 84 lc->desc = desc; 85 lc->priv = priv; 86 lc->ops = ops; 87 TAILQ_INSERT_HEAD(&classes, lc, link); 88 89 return 0; 90} 91 92static void 93linker_file_sysinit(linker_file_t lf) 94{ 95 struct linker_set* sysinits; 96 struct sysinit** sipp; 97 struct sysinit** xipp; 98 struct sysinit* save; 99 moduledata_t *moddata; 100 101 KLD_DPF(FILE, ("linker_file_sysinit: calling SYSINITs for %s\n", 102 lf->filename)); 103 104 sysinits = (struct linker_set*) 105 linker_file_lookup_symbol(lf, "sysinit_set", 0); 106 107 KLD_DPF(FILE, ("linker_file_sysinit: SYSINITs %p\n", sysinits)); 108 if (!sysinits) 109 return; 110 111 /* HACK ALERT! */ 112 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 113 if ((*sipp)->func == module_register_init) { 114 moddata = (*sipp)->udata; 115 moddata->_file = lf; 116 } 117 } 118 119 /* 120 * Perform a bubble sort of the system initialization objects by 121 * their subsystem (primary key) and order (secondary key). 122 * 123 * Since some things care about execution order, this is the 124 * operation which ensures continued function. 125 */ 126 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 127 for (xipp = sipp + 1; *xipp; xipp++) { 128 if ((*sipp)->subsystem <= (*xipp)->subsystem || 129 ((*sipp)->subsystem == (*xipp)->subsystem && 130 (*sipp)->order <= (*xipp)->order)) 131 continue; /* skip*/ 132 save = *sipp; 133 *sipp = *xipp; 134 *xipp = save; 135 } 136 } 137 138 139 /* 140 * Traverse the (now) ordered list of system initialization tasks. 141 * Perform each task, and continue on to the next task. 142 */ 143 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 144 if ((*sipp)->subsystem == SI_SUB_DUMMY) 145 continue; /* skip dummy task(s)*/ 146 147 switch ((*sipp)->type) { 148 case SI_TYPE_DEFAULT: 149 /* no special processing*/ 150 (*((*sipp)->func))((*sipp)->udata); 151 break; 152 153 case SI_TYPE_KTHREAD: 154#if !defined(SMP) 155 /* kernel thread*/ 156 if (fork1(&proc0, RFFDG|RFPROC|RFMEM)) 157 panic("fork kernel thread"); 158 cpu_set_fork_handler(pfind(proc0.p_retval[0]), 159 (*sipp)->func, (*sipp)->udata); 160 break; 161#endif 162 163 case SI_TYPE_KPROCESS: 164 /* kernel thread*/ 165 if (fork1(&proc0, RFFDG|RFPROC)) 166 panic("fork kernel process"); 167 cpu_set_fork_handler(pfind(proc0.p_retval[0]), 168 (*sipp)->func, (*sipp)->udata); 169 break; 170 171 default: 172 panic ("linker_file_sysinit: unrecognized init type"); 173 } 174 } 175} 176 177static void 178linker_file_sysuninit(linker_file_t lf) 179{ 180 struct linker_set* sysuninits; 181 struct sysinit** sipp; 182 struct sysinit** xipp; 183 struct sysinit* save; 184 185 KLD_DPF(FILE, ("linker_file_sysuninit: calling SYSUNINITs for %s\n", 186 lf->filename)); 187 188 sysuninits = (struct linker_set*) 189 linker_file_lookup_symbol(lf, "sysuninit_set", 0); 190 191 KLD_DPF(FILE, ("linker_file_sysuninit: SYSUNINITs %p\n", sysuninits)); 192 if (!sysuninits) 193 return; 194 195 /* 196 * Perform a reverse bubble sort of the system initialization objects 197 * by their subsystem (primary key) and order (secondary key). 198 * 199 * Since some things care about execution order, this is the 200 * operation which ensures continued function. 201 */ 202 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 203 for (xipp = sipp + 1; *xipp; xipp++) { 204 if ((*sipp)->subsystem >= (*xipp)->subsystem || 205 ((*sipp)->subsystem == (*xipp)->subsystem && 206 (*sipp)->order >= (*xipp)->order)) 207 continue; /* skip*/ 208 save = *sipp; 209 *sipp = *xipp; 210 *xipp = save; 211 } 212 } 213 214 215 /* 216 * Traverse the (now) ordered list of system initialization tasks. 217 * Perform each task, and continue on to the next task. 218 */ 219 for (sipp = (struct sysinit **)sysuninits->ls_items; *sipp; sipp++) { 220 if ((*sipp)->subsystem == SI_SUB_DUMMY) 221 continue; /* skip dummy task(s)*/ 222 223 switch ((*sipp)->type) { 224 case SI_TYPE_DEFAULT: 225 /* no special processing*/ 226 (*((*sipp)->func))((*sipp)->udata); 227 break; 228 229 default: 230 panic("linker_file_sysuninit: unrecognized uninit type"); 231 } 232 } 233} 234 235int 236linker_load_file(const char* filename, linker_file_t* result) 237{ 238 linker_class_t lc; 239 linker_file_t lf; 240 int foundfile, error = 0; 241 char *koname = NULL; 242 243 lf = linker_find_file_by_name(filename); 244 if (lf) { 245 KLD_DPF(FILE, ("linker_load_file: file %s is already loaded, incrementing refs\n", filename)); 246 *result = lf; 247 lf->refs++; 248 goto out; 249 } 250 251 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 252 if (koname == NULL) { 253 error = ENOMEM; 254 goto out; 255 } 256 sprintf(koname, "%s.ko", filename); 257 lf = NULL; 258 foundfile = 0; 259 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 260 KLD_DPF(FILE, ("linker_load_file: trying to load %s as %s\n", 261 filename, lc->desc)); 262 263 error = lc->ops->load_file(koname, &lf); /* First with .ko */ 264 if (lf == NULL && error == ENOENT) 265 error = lc->ops->load_file(filename, &lf); /* Then try without */ 266 /* 267 * If we got something other than ENOENT, then it exists but we cannot 268 * load it for some other reason. 269 */ 270 if (error != ENOENT) 271 foundfile = 1; 272 if (lf) { 273 linker_file_sysinit(lf); 274 275 *result = lf; 276 error = 0; 277 goto out; 278 } 279 } 280 /* 281 * Less than ideal, but tells the user whether it failed to load or 282 * the module was not found. 283 */ 284 if (foundfile) 285 error = ENOEXEC; /* Format not recognised (or unloadable) */ 286 else 287 error = ENOENT; /* Nothing found */ 288 289out: 290 if (koname) 291 free(koname, M_LINKER); 292 return error; 293} 294 295linker_file_t 296linker_find_file_by_name(const char* filename) 297{ 298 linker_file_t lf = 0; 299 char *koname; 300 301 koname = malloc(strlen(filename) + 4, M_LINKER, M_WAITOK); 302 if (koname == NULL) 303 goto out; 304 sprintf(koname, "%s.ko", filename); 305 306 lockmgr(&lock, LK_SHARED, 0, curproc); 307 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 308 if (!strcmp(lf->filename, koname)) 309 break; 310 if (!strcmp(lf->filename, filename)) 311 break; 312 } 313 lockmgr(&lock, LK_RELEASE, 0, curproc); 314 315out: 316 if (koname) 317 free(koname, M_LINKER); 318 return lf; 319} 320 321linker_file_t 322linker_find_file_by_id(int fileid) 323{ 324 linker_file_t lf = 0; 325 326 lockmgr(&lock, LK_SHARED, 0, curproc); 327 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) 328 if (lf->id == fileid) 329 break; 330 lockmgr(&lock, LK_RELEASE, 0, curproc); 331 332 return lf; 333} 334 335linker_file_t 336linker_make_file(const char* pathname, void* priv, struct linker_file_ops* ops) 337{ 338 linker_file_t lf = 0; 339 int namelen; 340 const char *filename; 341 342 filename = rindex(pathname, '/'); 343 if (filename && filename[1]) 344 filename++; 345 else 346 filename = pathname; 347 348 KLD_DPF(FILE, ("linker_make_file: new file, filename=%s\n", filename)); 349 lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); 350 namelen = strlen(filename) + 1; 351 lf = malloc(sizeof(struct linker_file) + namelen, M_LINKER, M_WAITOK); 352 if (!lf) 353 goto out; 354 bzero(lf, sizeof(*lf)); 355 356 lf->refs = 1; 357 lf->userrefs = 0; 358 lf->flags = 0; 359 lf->filename = (char*) (lf + 1); 360 strcpy(lf->filename, filename); 361 lf->id = next_file_id++; 362 lf->ndeps = 0; 363 lf->deps = NULL; 364 STAILQ_INIT(&lf->common); 365 TAILQ_INIT(&lf->modules); 366 367 lf->priv = priv; 368 lf->ops = ops; 369 TAILQ_INSERT_TAIL(&files, lf, link); 370 371out: 372 lockmgr(&lock, LK_RELEASE, 0, curproc); 373 return lf; 374} 375 376int 377linker_file_unload(linker_file_t file) 378{ 379 module_t mod, next; 380 struct common_symbol* cp; 381 int error = 0; 382 int i; 383 384 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 385 lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); 386 if (file->refs == 1) { 387 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 388 /* 389 * Inform any modules associated with this file. 390 */ 391 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 392 next = module_getfnext(mod); 393 394 /* 395 * Give the module a chance to veto the unload. 396 */ 397 if ((error = module_unload(mod)) != 0) { 398 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 399 mod)); 400 lockmgr(&lock, LK_RELEASE, 0, curproc); 401 goto out; 402 } 403 404 module_release(mod); 405 } 406 } 407 408 file->refs--; 409 if (file->refs > 0) { 410 lockmgr(&lock, LK_RELEASE, 0, curproc); 411 goto out; 412 } 413 414 /* Don't try to run SYSUNINITs if we are unloaded due to a link error */ 415 if (file->flags & LINKER_FILE_LINKED) 416 linker_file_sysuninit(file); 417 418 TAILQ_REMOVE(&files, file, link); 419 lockmgr(&lock, LK_RELEASE, 0, curproc); 420 421 for (i = 0; i < file->ndeps; i++) 422 linker_file_unload(file->deps[i]); 423 free(file->deps, M_LINKER); 424 425 for (cp = STAILQ_FIRST(&file->common); cp; 426 cp = STAILQ_FIRST(&file->common)) { 427 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 428 free(cp, M_LINKER); 429 } 430 431 file->ops->unload(file); 432 free(file, M_LINKER); 433 434out: 435 return error; 436} 437 438int 439linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 440{ 441 linker_file_t* newdeps; 442 443 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 444 M_LINKER, M_WAITOK); 445 if (newdeps == NULL) 446 return ENOMEM; 447 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*)); 448 449 if (file->deps) { 450 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 451 free(file->deps, M_LINKER); 452 } 453 file->deps = newdeps; 454 file->deps[file->ndeps] = dep; 455 file->ndeps++; 456 457 return 0; 458} 459 460caddr_t 461linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 462{ 463 c_linker_sym_t sym; 464 linker_symval_t symval; 465 linker_file_t lf; 466 caddr_t address; 467 size_t common_size = 0; 468 int i; 469 470 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 471 file, name, deps)); 472 473 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 474 file->ops->symbol_values(file, sym, &symval); 475 if (symval.value == 0) 476 /* 477 * For commons, first look them up in the dependancies and 478 * only allocate space if not found there. 479 */ 480 common_size = symval.size; 481 else { 482 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 483 return symval.value; 484 } 485 } 486 487 if (deps) { 488 for (i = 0; i < file->ndeps; i++) { 489 address = linker_file_lookup_symbol(file->deps[i], name, 0); 490 if (address) { 491 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 492 return address; 493 } 494 } 495 496 /* If we have not found it in the dependencies, search globally */ 497 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 498 /* But skip the current file if it's on the list */ 499 if (lf == file) 500 continue; 501 /* And skip the files we searched above */ 502 for (i = 0; i < file->ndeps; i++) 503 if (lf == file->deps[i]) 504 break; 505 if (i < file->ndeps) 506 continue; 507 address = linker_file_lookup_symbol(lf, name, 0); 508 if (address) { 509 KLD_DPF(SYM, ("linker_file_lookup_symbol: global value=%x\n", address)); 510 return address; 511 } 512 } 513 } 514 515 if (common_size > 0) { 516 /* 517 * This is a common symbol which was not found in the 518 * dependancies. We maintain a simple common symbol table in 519 * the file object. 520 */ 521 struct common_symbol* cp; 522 523 for (cp = STAILQ_FIRST(&file->common); cp; 524 cp = STAILQ_NEXT(cp, link)) 525 if (!strcmp(cp->name, name)) { 526 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 527 return cp->address; 528 } 529 530 /* 531 * Round the symbol size up to align. 532 */ 533 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 534 cp = malloc(sizeof(struct common_symbol) 535 + common_size 536 + strlen(name) + 1, 537 M_LINKER, M_WAITOK); 538 if (!cp) { 539 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 540 return 0; 541 } 542 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1); 543 544 cp->address = (caddr_t) (cp + 1); 545 cp->name = cp->address + common_size; 546 strcpy(cp->name, name); 547 bzero(cp->address, common_size); 548 STAILQ_INSERT_TAIL(&file->common, cp, link); 549 550 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 551 return cp->address; 552 } 553 554 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 555 return 0; 556} 557 558#ifdef DDB 559/* 560 * DDB Helpers. DDB has to look across multiple files with their own 561 * symbol tables and string tables. 562 * 563 * Note that we do not obey list locking protocols here. We really don't 564 * need DDB to hang because somebody's got the lock held. We'll take the 565 * chance that the files list is inconsistant instead. 566 */ 567 568int 569linker_ddb_lookup(const char *symstr, c_linker_sym_t *sym) 570{ 571 linker_file_t lf; 572 573 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 574 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0) 575 return 0; 576 } 577 return ENOENT; 578} 579 580int 581linker_ddb_search_symbol(caddr_t value, c_linker_sym_t *sym, long *diffp) 582{ 583 linker_file_t lf; 584 u_long off = (u_long)value; 585 u_long diff, bestdiff; 586 c_linker_sym_t best; 587 c_linker_sym_t es; 588 589 best = 0; 590 bestdiff = off; 591 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 592 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0) 593 continue; 594 if (es != 0 && diff < bestdiff) { 595 best = es; 596 bestdiff = diff; 597 } 598 if (bestdiff == 0) 599 break; 600 } 601 if (best) { 602 *sym = best; 603 *diffp = bestdiff; 604 return 0; 605 } else { 606 *sym = 0; 607 *diffp = off; 608 return ENOENT; 609 } 610} 611 612int 613linker_ddb_symbol_values(c_linker_sym_t sym, linker_symval_t *symval) 614{ 615 linker_file_t lf; 616 617 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 618 if (lf->ops->symbol_values(lf, sym, symval) == 0) 619 return 0; 620 } 621 return ENOENT; 622} 623 624#endif 625 626/* 627 * Syscalls. 628 */ 629 630int 631kldload(struct proc* p, struct kldload_args* uap) 632{ 633 char* filename = NULL, *modulename; 634 linker_file_t lf; 635 int error = 0; 636 637 p->p_retval[0] = -1; 638 639 if (securelevel > 0) 640 return EPERM; 641 642 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 643 return error; 644 645 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 646 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0) 647 goto out; 648 649 /* Can't load more than one module with the same name */ 650 modulename = rindex(filename, '/'); 651 if (modulename == NULL) 652 modulename = filename; 653 if (linker_find_file_by_name(modulename)) { 654 error = EEXIST; 655 goto out; 656 } 657 658 if ((error = linker_load_file(filename, &lf)) != 0) 659 goto out; 660 661 lf->userrefs++; 662 p->p_retval[0] = lf->id; 663 664out: 665 if (filename) 666 free(filename, M_TEMP); 667 return error; 668} 669 670int 671kldunload(struct proc* p, struct kldunload_args* uap) 672{ 673 linker_file_t lf; 674 int error = 0; 675 676 if (securelevel > 0) 677 return EPERM; 678 679 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 680 return error; 681 682 lf = linker_find_file_by_id(SCARG(uap, fileid)); 683 if (lf) { 684 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 685 if (lf->userrefs == 0) { 686 printf("linkerunload: attempt to unload file that was loaded by the kernel\n"); 687 error = EBUSY; 688 goto out; 689 } 690 lf->userrefs--; 691 error = linker_file_unload(lf); 692 if (error) 693 lf->userrefs++; 694 } else 695 error = ENOENT; 696 697out: 698 return error; 699} 700 701int 702kldfind(struct proc* p, struct kldfind_args* uap) 703{ 704 char* filename = NULL, *modulename; 705 linker_file_t lf; 706 int error = 0; 707 708 p->p_retval[0] = -1; 709 710 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 711 if ((error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) != 0) 712 goto out; 713 714 modulename = rindex(filename, '/'); 715 if (modulename == NULL) 716 modulename = filename; 717 718 lf = linker_find_file_by_name(modulename); 719 if (lf) 720 p->p_retval[0] = lf->id; 721 else 722 error = ENOENT; 723 724out: 725 if (filename) 726 free(filename, M_TEMP); 727 return error; 728} 729 730int 731kldnext(struct proc* p, struct kldnext_args* uap) 732{ 733 linker_file_t lf; 734 int error = 0; 735 736 if (SCARG(uap, fileid) == 0) { 737 if (TAILQ_FIRST(&files)) 738 p->p_retval[0] = TAILQ_FIRST(&files)->id; 739 else 740 p->p_retval[0] = 0; 741 return 0; 742 } 743 744 lf = linker_find_file_by_id(SCARG(uap, fileid)); 745 if (lf) { 746 if (TAILQ_NEXT(lf, link)) 747 p->p_retval[0] = TAILQ_NEXT(lf, link)->id; 748 else 749 p->p_retval[0] = 0; 750 } else 751 error = ENOENT; 752 753 return error; 754} 755 756int 757kldstat(struct proc* p, struct kldstat_args* uap) 758{ 759 linker_file_t lf; 760 int error = 0; 761 int version; 762 struct kld_file_stat* stat; 763 int namelen; 764 765 lf = linker_find_file_by_id(SCARG(uap, fileid)); 766 if (!lf) { 767 error = ENOENT; 768 goto out; 769 } 770 771 stat = SCARG(uap, stat); 772 773 /* 774 * Check the version of the user's structure. 775 */ 776 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0) 777 goto out; 778 if (version != sizeof(struct kld_file_stat)) { 779 error = EINVAL; 780 goto out; 781 } 782 783 namelen = strlen(lf->filename) + 1; 784 if (namelen > MAXPATHLEN) 785 namelen = MAXPATHLEN; 786 if ((error = copyout(lf->filename, &stat->name[0], namelen)) != 0) 787 goto out; 788 if ((error = copyout(&lf->refs, &stat->refs, sizeof(int))) != 0) 789 goto out; 790 if ((error = copyout(&lf->id, &stat->id, sizeof(int))) != 0) 791 goto out; 792 if ((error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) != 0) 793 goto out; 794 if ((error = copyout(&lf->size, &stat->size, sizeof(size_t))) != 0) 795 goto out; 796 797 p->p_retval[0] = 0; 798 799out: 800 return error; 801} 802 803int 804kldfirstmod(struct proc* p, struct kldfirstmod_args* uap) 805{ 806 linker_file_t lf; 807 int error = 0; 808 809 lf = linker_find_file_by_id(SCARG(uap, fileid)); 810 if (lf) { 811 if (TAILQ_FIRST(&lf->modules)) 812 p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 813 else 814 p->p_retval[0] = 0; 815 } else 816 error = ENOENT; 817 818 return error; 819} 820 821int 822kldsym(struct proc *p, struct kldsym_args *uap) 823{ 824 char *symstr = NULL; 825 c_linker_sym_t sym; 826 linker_symval_t symval; 827 linker_file_t lf; 828 struct kld_sym_lookup lookup; 829 int error = 0; 830 831 if ((error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) != 0) 832 goto out; 833 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 834 error = EINVAL; 835 goto out; 836 } 837 838 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 839 if ((error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) != 0) 840 goto out; 841 842 if (SCARG(uap, fileid) != 0) { 843 lf = linker_find_file_by_id(SCARG(uap, fileid)); 844 if (lf == NULL) { 845 error = ENOENT; 846 goto out; 847 } 848 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 849 lf->ops->symbol_values(lf, sym, &symval) == 0) { 850 lookup.symvalue = (u_long)symval.value; 851 lookup.symsize = symval.size; 852 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 853 } else 854 error = ENOENT; 855 } else { 856 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 857 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 858 lf->ops->symbol_values(lf, sym, &symval) == 0) { 859 lookup.symvalue = (u_long)symval.value; 860 lookup.symsize = symval.size; 861 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 862 break; 863 } 864 } 865 if (!lf) 866 error = ENOENT; 867 } 868out: 869 if (symstr) 870 free(symstr, M_TEMP); 871 return error; 872} 873 874/* 875 * Preloaded module support 876 */ 877 878static void 879linker_preload(void* arg) 880{ 881 caddr_t modptr; 882 char *modname; 883 char *modtype; 884 linker_file_t lf; 885 linker_class_t lc; 886 int error; 887 struct linker_set *sysinits; 888 struct sysinit **sipp; 889 moduledata_t *moddata; 890 891 modptr = NULL; 892 while ((modptr = preload_search_next_name(modptr)) != NULL) { 893 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 894 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 895 if (modname == NULL) { 896 printf("Preloaded module at %p does not have a name!\n", modptr); 897 continue; 898 } 899 if (modtype == NULL) { 900 printf("Preloaded module at %p does not have a type!\n", modptr); 901 continue; 902 } 903 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 904 lf = linker_find_file_by_name(modname); 905 if (lf) { 906 lf->userrefs++; 907 continue; 908 } 909 lf = NULL; 910 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 911 error = lc->ops->load_file(modname, &lf); 912 if (error) { 913 lf = NULL; 914 break; 915 } 916 } 917 if (lf) { 918 lf->userrefs++; 919 920 sysinits = (struct linker_set*) 921 linker_file_lookup_symbol(lf, "sysinit_set", 0); 922 if (sysinits) { 923 /* HACK ALERT! 924 * This is to set the sysinit moduledata so that the module 925 * can attach itself to the correct containing file. 926 * The sysinit could be run at *any* time. 927 */ 928 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 929 if ((*sipp)->func == module_register_init) { 930 moddata = (*sipp)->udata; 931 moddata->_file = lf; 932 } 933 } 934 sysinit_add((struct sysinit **)sysinits->ls_items); 935 } 936 } 937 } 938} 939 940SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 941 942/* 943 * Search for a not-loaded module by name. 944 * 945 * Modules may be found in the following locations: 946 * 947 * - preloaded (result is just the module name) 948 * - on disk (result is full path to module) 949 * 950 * If the module name is qualified in any way (contains path, etc.) 951 * the we simply return a copy of it. 952 * 953 * The search path can be manipulated via sysctl. Note that we use the ';' 954 * character as a separator to be consistent with the bootloader. 955 */ 956 957static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/"; 958 959SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 960 sizeof(linker_path), "module load search path"); 961 962static char * 963linker_strdup(const char *str) 964{ 965 char *result; 966 967 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 968 strcpy(result, str); 969 return(result); 970} 971 972char * 973linker_search_path(const char *name) 974{ 975 struct nameidata nd; 976 struct proc *p = curproc; /* XXX */ 977 char *cp, *ep, *result; 978 int error; 979 enum vtype type; 980 981 /* qualified at all? */ 982 if (index(name, '/')) 983 return(linker_strdup(name)); 984 985 /* traverse the linker path */ 986 cp = linker_path; 987 for (;;) { 988 989 /* find the end of this component */ 990 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 991 ; 992 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK); 993 if (result == NULL) /* actually ENOMEM */ 994 return(NULL); 995 996 strncpy(result, cp, ep - cp); 997 strcpy(result + (ep - cp), name); 998 999 /* 1000 * Attempt to open the file, and return the path if we succeed and it's 1001 * a regular file. 1002 */ 1003 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p); 1004 error = vn_open(&nd, FREAD, 0); 1005 if (error == 0) { 1006 type = nd.ni_vp->v_type; 1007 VOP_UNLOCK(nd.ni_vp, 0, p); 1008 vn_close(nd.ni_vp, FREAD, p->p_ucred, p); 1009 if (type == VREG) 1010 return(result); 1011 } 1012 free(result, M_LINKER); 1013 1014 if (*ep == 0) 1015 break; 1016 cp = ep + 1; 1017 } 1018 return(NULL); 1019} 1020