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