kern_linker.c revision 42837
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.19 1999/01/17 17:58:52 peter 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->filename = (char*) (lf + 1); 359 strcpy(lf->filename, filename); 360 lf->id = next_file_id++; 361 lf->ndeps = 0; 362 lf->deps = NULL; 363 STAILQ_INIT(&lf->common); 364 TAILQ_INIT(&lf->modules); 365 366 lf->priv = priv; 367 lf->ops = ops; 368 TAILQ_INSERT_TAIL(&files, lf, link); 369 370out: 371 lockmgr(&lock, LK_RELEASE, 0, curproc); 372 return lf; 373} 374 375int 376linker_file_unload(linker_file_t file) 377{ 378 module_t mod, next; 379 struct common_symbol* cp; 380 int error = 0; 381 int i; 382 383 KLD_DPF(FILE, ("linker_file_unload: lf->refs=%d\n", file->refs)); 384 lockmgr(&lock, LK_EXCLUSIVE|LK_RETRY, 0, curproc); 385 if (file->refs == 1) { 386 KLD_DPF(FILE, ("linker_file_unload: file is unloading, informing modules\n")); 387 /* 388 * Inform any modules associated with this file. 389 */ 390 for (mod = TAILQ_FIRST(&file->modules); mod; mod = next) { 391 next = module_getfnext(mod); 392 393 /* 394 * Give the module a chance to veto the unload. 395 */ 396 if (error = module_unload(mod)) { 397 KLD_DPF(FILE, ("linker_file_unload: module %x vetoes unload\n", 398 mod)); 399 lockmgr(&lock, LK_RELEASE, 0, curproc); 400 goto out; 401 } 402 403 module_release(mod); 404 } 405 } 406 407 file->refs--; 408 if (file->refs > 0) { 409 lockmgr(&lock, LK_RELEASE, 0, curproc); 410 goto out; 411 } 412 413 linker_file_sysuninit(file); 414 415 TAILQ_REMOVE(&files, file, link); 416 lockmgr(&lock, LK_RELEASE, 0, curproc); 417 418 for (i = 0; i < file->ndeps; i++) 419 linker_file_unload(file->deps[i]); 420 free(file->deps, M_LINKER); 421 422 for (cp = STAILQ_FIRST(&file->common); cp; 423 cp = STAILQ_FIRST(&file->common)) { 424 STAILQ_REMOVE(&file->common, cp, common_symbol, link); 425 free(cp, M_LINKER); 426 } 427 428 file->ops->unload(file); 429 free(file, M_LINKER); 430 431out: 432 return error; 433} 434 435int 436linker_file_add_dependancy(linker_file_t file, linker_file_t dep) 437{ 438 linker_file_t* newdeps; 439 440 newdeps = malloc((file->ndeps + 1) * sizeof(linker_file_t*), 441 M_LINKER, M_WAITOK); 442 if (newdeps == NULL) 443 return ENOMEM; 444 bzero(newdeps, (file->ndeps + 1) * sizeof(linker_file_t*)); 445 446 if (file->deps) { 447 bcopy(file->deps, newdeps, file->ndeps * sizeof(linker_file_t*)); 448 free(file->deps, M_LINKER); 449 } 450 file->deps = newdeps; 451 file->deps[file->ndeps] = dep; 452 file->ndeps++; 453 454 return 0; 455} 456 457caddr_t 458linker_file_lookup_symbol(linker_file_t file, const char* name, int deps) 459{ 460 linker_sym_t sym; 461 linker_symval_t symval; 462 caddr_t address; 463 size_t common_size = 0; 464 int i; 465 466 KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%x, name=%s, deps=%d\n", 467 file, name, deps)); 468 469 if (file->ops->lookup_symbol(file, name, &sym) == 0) { 470 file->ops->symbol_values(file, sym, &symval); 471 if (symval.value == 0) 472 /* 473 * For commons, first look them up in the dependancies and 474 * only allocate space if not found there. 475 */ 476 common_size = symval.size; 477 else { 478 KLD_DPF(SYM, ("linker_file_lookup_symbol: symbol.value=%x\n", symval.value)); 479 return symval.value; 480 } 481 } 482 483 if (deps) 484 for (i = 0; i < file->ndeps; i++) { 485 address = linker_file_lookup_symbol(file->deps[i], name, 0); 486 if (address) { 487 KLD_DPF(SYM, ("linker_file_lookup_symbol: deps value=%x\n", address)); 488 return address; 489 } 490 } 491 492 if (common_size > 0) { 493 /* 494 * This is a common symbol which was not found in the 495 * dependancies. We maintain a simple common symbol table in 496 * the file object. 497 */ 498 struct common_symbol* cp; 499 500 for (cp = STAILQ_FIRST(&file->common); cp; 501 cp = STAILQ_NEXT(cp, link)) 502 if (!strcmp(cp->name, name)) { 503 KLD_DPF(SYM, ("linker_file_lookup_symbol: old common value=%x\n", cp->address)); 504 return cp->address; 505 } 506 507 /* 508 * Round the symbol size up to align. 509 */ 510 common_size = (common_size + sizeof(int) - 1) & -sizeof(int); 511 cp = malloc(sizeof(struct common_symbol) 512 + common_size 513 + strlen(name) + 1, 514 M_LINKER, M_WAITOK); 515 if (!cp) { 516 KLD_DPF(SYM, ("linker_file_lookup_symbol: nomem\n")); 517 return 0; 518 } 519 bzero(cp, sizeof(struct common_symbol) + common_size + strlen(name)+ 1); 520 521 cp->address = (caddr_t) (cp + 1); 522 cp->name = cp->address + common_size; 523 strcpy(cp->name, name); 524 bzero(cp->address, common_size); 525 STAILQ_INSERT_TAIL(&file->common, cp, link); 526 527 KLD_DPF(SYM, ("linker_file_lookup_symbol: new common value=%x\n", cp->address)); 528 return cp->address; 529 } 530 531 KLD_DPF(SYM, ("linker_file_lookup_symbol: fail\n")); 532 return 0; 533} 534 535#ifdef DDB 536/* 537 * DDB Helpers. DDB has to look across multiple files with their own 538 * symbol tables and string tables. 539 * 540 * Note that we do not obey list locking protocols here. We really don't 541 * need DDB to hang because somebody's got the lock held. We'll take the 542 * chance that the files list is inconsistant instead. 543 */ 544 545int 546linker_ddb_lookup(char *symstr, linker_sym_t *sym) 547{ 548 linker_file_t lf; 549 550 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 551 if (lf->ops->lookup_symbol(lf, symstr, sym) == 0) 552 return 0; 553 } 554 return ENOENT; 555} 556 557int 558linker_ddb_search_symbol(caddr_t value, linker_sym_t *sym, long *diffp) 559{ 560 linker_file_t lf; 561 u_long off = (u_long)value; 562 u_long diff, bestdiff; 563 linker_sym_t best; 564 linker_sym_t es; 565 566 best = 0; 567 bestdiff = off; 568 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 569 if (lf->ops->search_symbol(lf, value, &es, &diff) != 0) 570 continue; 571 if (es != 0 && diff < bestdiff) { 572 best = es; 573 bestdiff = diff; 574 } 575 if (bestdiff == 0) 576 break; 577 } 578 if (best) { 579 *sym = best; 580 *diffp = bestdiff; 581 return 0; 582 } else { 583 *sym = 0; 584 *diffp = off; 585 return ENOENT; 586 } 587} 588 589int 590linker_ddb_symbol_values(linker_sym_t sym, linker_symval_t *symval) 591{ 592 linker_file_t lf; 593 594 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 595 if (lf->ops->symbol_values(lf, sym, symval) == 0) 596 return 0; 597 } 598 return ENOENT; 599} 600 601#endif 602 603/* 604 * Syscalls. 605 */ 606 607int 608kldload(struct proc* p, struct kldload_args* uap) 609{ 610 char* filename = NULL, *modulename; 611 linker_file_t lf; 612 int error = 0; 613 614 p->p_retval[0] = -1; 615 616 if (securelevel > 0) 617 return EPERM; 618 619 if (error = suser(p->p_ucred, &p->p_acflag)) 620 return error; 621 622 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 623 if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) 624 goto out; 625 626 /* Can't load more than one module with the same name */ 627 modulename = rindex(filename, '/'); 628 if (modulename == NULL) 629 modulename = filename; 630 if (linker_find_file_by_name(modulename)) { 631 error = EEXIST; 632 goto out; 633 } 634 635 if (error = linker_load_file(filename, &lf)) 636 goto out; 637 638 lf->userrefs++; 639 p->p_retval[0] = lf->id; 640 641out: 642 if (filename) 643 free(filename, M_TEMP); 644 return error; 645} 646 647int 648kldunload(struct proc* p, struct kldunload_args* uap) 649{ 650 linker_file_t lf; 651 int error = 0; 652 653 if (securelevel > 0) 654 return EPERM; 655 656 if (error = suser(p->p_ucred, &p->p_acflag)) 657 return error; 658 659 lf = linker_find_file_by_id(SCARG(uap, fileid)); 660 if (lf) { 661 KLD_DPF(FILE, ("kldunload: lf->userrefs=%d\n", lf->userrefs)); 662 if (lf->userrefs == 0) { 663 printf("linkerunload: attempt to unload file which was not loaded by user\n"); 664 error = EBUSY; 665 goto out; 666 } 667 error = linker_file_unload(lf); 668 if (error) 669 goto out; 670 lf->userrefs--; 671 } else 672 error = ENOENT; 673 674out: 675 return error; 676} 677 678int 679kldfind(struct proc* p, struct kldfind_args* uap) 680{ 681 char* filename = NULL, *modulename; 682 linker_file_t lf; 683 int error = 0; 684 685 p->p_retval[0] = -1; 686 687 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 688 if (error = copyinstr(SCARG(uap, file), filename, MAXPATHLEN, NULL)) 689 goto out; 690 691 modulename = rindex(filename, '/'); 692 if (modulename == NULL) 693 modulename = filename; 694 695 lf = linker_find_file_by_name(modulename); 696 if (lf) 697 p->p_retval[0] = lf->id; 698 else 699 error = ENOENT; 700 701out: 702 if (filename) 703 free(filename, M_TEMP); 704 return error; 705} 706 707int 708kldnext(struct proc* p, struct kldnext_args* uap) 709{ 710 linker_file_t lf; 711 int error = 0; 712 713 if (SCARG(uap, fileid) == 0) { 714 if (TAILQ_FIRST(&files)) 715 p->p_retval[0] = TAILQ_FIRST(&files)->id; 716 else 717 p->p_retval[0] = 0; 718 return 0; 719 } 720 721 lf = linker_find_file_by_id(SCARG(uap, fileid)); 722 if (lf) { 723 if (TAILQ_NEXT(lf, link)) 724 p->p_retval[0] = TAILQ_NEXT(lf, link)->id; 725 else 726 p->p_retval[0] = 0; 727 } else 728 error = ENOENT; 729 730 return error; 731} 732 733int 734kldstat(struct proc* p, struct kldstat_args* uap) 735{ 736 linker_file_t lf; 737 int error = 0; 738 int version; 739 struct kld_file_stat* stat; 740 int namelen; 741 742 lf = linker_find_file_by_id(SCARG(uap, fileid)); 743 if (!lf) { 744 error = ENOENT; 745 goto out; 746 } 747 748 stat = SCARG(uap, stat); 749 750 /* 751 * Check the version of the user's structure. 752 */ 753 if (error = copyin(&stat->version, &version, sizeof(version))) 754 goto out; 755 if (version != sizeof(struct kld_file_stat)) { 756 error = EINVAL; 757 goto out; 758 } 759 760 namelen = strlen(lf->filename) + 1; 761 if (namelen > MAXPATHLEN) 762 namelen = MAXPATHLEN; 763 if (error = copyout(lf->filename, &stat->name[0], namelen)) 764 goto out; 765 if (error = copyout(&lf->refs, &stat->refs, sizeof(int))) 766 goto out; 767 if (error = copyout(&lf->id, &stat->id, sizeof(int))) 768 goto out; 769 if (error = copyout(&lf->address, &stat->address, sizeof(caddr_t))) 770 goto out; 771 if (error = copyout(&lf->size, &stat->size, sizeof(size_t))) 772 goto out; 773 774 p->p_retval[0] = 0; 775 776out: 777 return error; 778} 779 780int 781kldfirstmod(struct proc* p, struct kldfirstmod_args* uap) 782{ 783 linker_file_t lf; 784 int error = 0; 785 786 lf = linker_find_file_by_id(SCARG(uap, fileid)); 787 if (lf) { 788 if (TAILQ_FIRST(&lf->modules)) 789 p->p_retval[0] = module_getid(TAILQ_FIRST(&lf->modules)); 790 else 791 p->p_retval[0] = 0; 792 } else 793 error = ENOENT; 794 795 return error; 796} 797 798int 799kldsym(struct proc *p, struct kldsym_args *uap) 800{ 801 char *symstr = NULL; 802 linker_sym_t sym; 803 linker_symval_t symval; 804 linker_file_t lf; 805 struct kld_sym_lookup lookup; 806 int error = 0; 807 808 if (error = copyin(SCARG(uap, data), &lookup, sizeof(lookup))) 809 goto out; 810 if (lookup.version != sizeof(lookup) || SCARG(uap, cmd) != KLDSYM_LOOKUP) { 811 error = EINVAL; 812 goto out; 813 } 814 815 symstr = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 816 if (error = copyinstr(lookup.symname, symstr, MAXPATHLEN, NULL)) 817 goto out; 818 819 if (SCARG(uap, fileid) != 0) { 820 lf = linker_find_file_by_id(SCARG(uap, fileid)); 821 if (lf == NULL) { 822 error = ENOENT; 823 goto out; 824 } 825 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 826 lf->ops->symbol_values(lf, sym, &symval) == 0) { 827 lookup.symvalue = (u_long)symval.value; 828 lookup.symsize = symval.size; 829 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 830 } else 831 error = ENOENT; 832 } else { 833 for (lf = TAILQ_FIRST(&files); lf; lf = TAILQ_NEXT(lf, link)) { 834 if (lf->ops->lookup_symbol(lf, symstr, &sym) == 0 && 835 lf->ops->symbol_values(lf, sym, &symval) == 0) { 836 lookup.symvalue = (u_long)symval.value; 837 lookup.symsize = symval.size; 838 error = copyout(&lookup, SCARG(uap, data), sizeof(lookup)); 839 break; 840 } 841 } 842 if (!lf) 843 error = ENOENT; 844 } 845out: 846 if (symstr) 847 free(symstr, M_TEMP); 848 return error; 849} 850 851/* 852 * Preloaded module support 853 */ 854 855static void 856linker_preload(void* arg) 857{ 858 caddr_t modptr; 859 char *modname; 860 char *modtype; 861 linker_file_t lf; 862 linker_class_t lc; 863 int error; 864 struct linker_set *sysinits; 865 struct sysinit **sipp; 866 moduledata_t *moddata; 867 868 modptr = NULL; 869 while ((modptr = preload_search_next_name(modptr)) != NULL) { 870 modname = (char *)preload_search_info(modptr, MODINFO_NAME); 871 modtype = (char *)preload_search_info(modptr, MODINFO_TYPE); 872 if (modname == NULL) { 873 printf("Preloaded module at %p does not have a name!\n", modptr); 874 continue; 875 } 876 if (modtype == NULL) { 877 printf("Preloaded module at %p does not have a type!\n", modptr); 878 continue; 879 } 880 printf("Preloaded %s \"%s\" at %p.\n", modtype, modname, modptr); 881 lf = linker_find_file_by_name(modname); 882 if (lf) { 883 lf->userrefs++; 884 continue; 885 } 886 lf = NULL; 887 for (lc = TAILQ_FIRST(&classes); lc; lc = TAILQ_NEXT(lc, link)) { 888 error = lc->ops->load_file(modname, &lf); 889 if (error) { 890 lf = NULL; 891 break; 892 } 893 } 894 if (lf) { 895 lf->userrefs++; 896 897 sysinits = (struct linker_set*) 898 linker_file_lookup_symbol(lf, "sysinit_set", 0); 899 if (sysinits) { 900 /* HACK ALERT! 901 * This is to set the sysinit moduledata so that the module 902 * can attach itself to the correct containing file. 903 * The sysinit could be run at *any* time. 904 */ 905 for (sipp = (struct sysinit **)sysinits->ls_items; *sipp; sipp++) { 906 if ((*sipp)->func == module_register_init) { 907 moddata = (*sipp)->udata; 908 moddata->_file = lf; 909 } 910 } 911 sysinit_add((struct sysinit **)sysinits->ls_items); 912 } 913 } 914 } 915} 916 917SYSINIT(preload, SI_SUB_KLD, SI_ORDER_MIDDLE, linker_preload, 0); 918 919/* 920 * Search for a not-loaded module by name. 921 * 922 * Modules may be found in the following locations: 923 * 924 * - preloaded (result is just the module name) 925 * - on disk (result is full path to module) 926 * 927 * If the module name is qualified in any way (contains path, etc.) 928 * the we simply return a copy of it. 929 * 930 * The search path can be manipulated via sysctl. Note that we use the ';' 931 * character as a separator to be consistent with the bootloader. 932 */ 933 934static char linker_path[MAXPATHLEN + 1] = "/;/boot/;/modules/"; 935 936SYSCTL_STRING(_kern, OID_AUTO, module_path, CTLFLAG_RW, linker_path, 937 sizeof(linker_path), "module load search path"); 938 939static char * 940linker_strdup(const char *str) 941{ 942 char *result; 943 944 if ((result = malloc((strlen(str) + 1), M_LINKER, M_WAITOK)) != NULL) 945 strcpy(result, str); 946 return(result); 947} 948 949char * 950linker_search_path(const char *name) 951{ 952 struct nameidata nd; 953 struct proc *p = curproc; /* XXX */ 954 char *cp, *ep, *result; 955 int error; 956 enum vtype type; 957 958 /* qualified at all? */ 959 if (index(name, '/')) 960 return(linker_strdup(name)); 961 962 /* traverse the linker path */ 963 cp = linker_path; 964 for (;;) { 965 966 /* find the end of this component */ 967 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++) 968 ; 969 result = malloc((strlen(name) + (ep - cp) + 1), M_LINKER, M_WAITOK); 970 if (result == NULL) /* actually ENOMEM */ 971 return(NULL); 972 973 strncpy(result, cp, ep - cp); 974 strcpy(result + (ep - cp), name); 975 976 /* 977 * Attempt to open the file, and return the path if we succeed and it's 978 * a regular file. 979 */ 980 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, result, p); 981 error = vn_open(&nd, FREAD, 0); 982 if (error == 0) { 983 type = nd.ni_vp->v_type; 984 VOP_UNLOCK(nd.ni_vp, 0, p); 985 vn_close(nd.ni_vp, FREAD, p->p_ucred, p); 986 if (type == VREG) 987 return(result); 988 } 989 free(result, M_LINKER); 990 991 if (*ep == 0) 992 break; 993 cp = ep + 1; 994 } 995 return(NULL); 996} 997