1 /* $NetBSD: subr_kobj.c,v 1.9 2008/01/17 22:32:49 rumble Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the NetBSD 18 * Foundation, Inc. and its contributors. 19 * 4. Neither the name of The NetBSD Foundation nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /*- 37 * Copyright (c) 1998-2000 Doug Rabson 38 * Copyright (c) 2004 Peter Wemm 39 * All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 /* 64 * Kernel loader for ELF objects. 65 * 66 * TODO: adjust kmem_alloc() calls to avoid needless fragmentation. 67 */ 68 69 #include "opt_modular.h" 70 71 #include <sys/cdefs.h> 72 __KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.9 2008/01/17 22:32:49 rumble Exp $"); 73 74 #define ELFSIZE ARCH_ELFSIZE 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/kernel.h> 79 #include <sys/kmem.h> 80 #include <sys/proc.h> 81 #include <sys/namei.h> 82 #include <sys/vnode.h> 83 #include <sys/fcntl.h> 84 #include <sys/kobj.h> 85 #include <sys/ksyms.h> 86 #include <sys/lkm.h> 87 #include <sys/exec.h> 88 #include <sys/exec_elf.h> 89 90 #include <machine/stdarg.h> 91 92 #include <uvm/uvm_extern.h> 93 94 #ifdef MODULAR 95 96 typedef struct { 97 void *addr; 98 Elf_Off size; 99 int flags; 100 int sec; /* Original section */ 101 const char *name; 102 } progent_t; 103 104 typedef struct { 105 Elf_Rel *rel; 106 int nrel; 107 int sec; 108 size_t size; 109 } relent_t; 110 111 typedef struct { 112 Elf_Rela *rela; 113 int nrela; 114 int sec; 115 size_t size; 116 } relaent_t; 117 118 typedef enum kobjtype { 119 KT_UNSET, 120 KT_VNODE, 121 KT_MEMORY 122 } kobjtype_t; 123 124 struct kobj { 125 char ko_name[MAXLKMNAME]; 126 kobjtype_t ko_type; 127 void *ko_source; 128 ssize_t ko_memsize; 129 vaddr_t ko_address; /* Relocation address */ 130 Elf_Shdr *ko_shdr; 131 progent_t *ko_progtab; 132 relaent_t *ko_relatab; 133 relent_t *ko_reltab; 134 Elf_Sym *ko_symtab; /* Symbol table */ 135 char *ko_strtab; /* String table */ 136 char *ko_shstrtab; /* Section name string table */ 137 size_t ko_size; /* Size of text/data/bss */ 138 size_t ko_symcnt; /* Number of symbols */ 139 size_t ko_strtabsz; /* Number of bytes in string table */ 140 size_t ko_shstrtabsz; /* Number of bytes in scn str table */ 141 size_t ko_shdrsz; 142 int ko_nrel; 143 int ko_nrela; 144 int ko_nprogtab; 145 bool ko_ksyms; 146 bool ko_loaded; 147 }; 148 149 static int kobj_relocate(kobj_t); 150 static void kobj_error(const char *, ...); 151 static int kobj_read(kobj_t, void *, size_t, off_t); 152 static void kobj_release_mem(kobj_t); 153 154 extern struct vm_map *lkm_map; 155 static const char *kobj_path = "/modules"; /* XXX ??? */ 156 157 /* 158 * kobj_open_file: 159 * 160 * Open an object located in the file system. 161 */ 162 int 163 kobj_open_file(kobj_t *kop, const char *filename) 164 { 165 struct nameidata nd; 166 kauth_cred_t cred; 167 char *path; 168 int error; 169 kobj_t ko; 170 171 cred = kauth_cred_get(); 172 173 ko = kmem_zalloc(sizeof(*ko), KM_SLEEP); 174 if (ko == NULL) { 175 return ENOMEM; 176 } 177 178 /* XXX where to look? */ 179 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename); 180 error = vn_open(&nd, FREAD, 0); 181 if (error != 0) { 182 if (error != ENOENT) { 183 goto out; 184 } 185 path = PNBUF_GET(); 186 snprintf(path, MAXPATHLEN - 1, "%s/%s", kobj_path, 187 filename); 188 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path); 189 error = vn_open(&nd, FREAD, 0); 190 if (error != 0) { 191 strlcat(path, ".o", MAXPATHLEN); 192 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path); 193 error = vn_open(&nd, FREAD, 0); 194 } 195 PNBUF_PUT(path); 196 if (error != 0) { 197 goto out; 198 } 199 } 200 201 out: 202 if (error != 0) { 203 kmem_free(ko, sizeof(*ko)); 204 } else { 205 ko->ko_type = KT_VNODE; 206 ko->ko_source = nd.ni_vp; 207 *kop = ko; 208 } 209 return error; 210 } 211 212 /* 213 * kobj_open_mem: 214 * 215 * Open a pre-loaded object already resident in memory. If size 216 * is not -1, the complete size of the object is known. 217 */ 218 int 219 kobj_open_mem(kobj_t *kop, void *base, ssize_t size) 220 { 221 kobj_t ko; 222 223 ko = kmem_zalloc(sizeof(*ko), KM_SLEEP); 224 if (ko == NULL) { 225 return ENOMEM; 226 } 227 228 ko->ko_type = KT_MEMORY; 229 ko->ko_source = base; 230 ko->ko_memsize = size; 231 *kop = ko; 232 233 return 0; 234 } 235 236 /* 237 * kobj_close: 238 * 239 * Close an open ELF object. If the object was not successfully 240 * loaded, it will be destroyed. 241 */ 242 void 243 kobj_close(kobj_t ko) 244 { 245 246 KASSERT(ko->ko_source != NULL); 247 248 switch (ko->ko_type) { 249 case KT_VNODE: 250 VOP_UNLOCK(ko->ko_source, 0); 251 vn_close(ko->ko_source, FREAD, kauth_cred_get(), curlwp); 252 break; 253 case KT_MEMORY: 254 /* nothing */ 255 break; 256 default: 257 panic("kobj_close: unknown type"); 258 break; 259 } 260 261 ko->ko_source = NULL; 262 ko->ko_type = KT_UNSET; 263 264 /* Program table and section strings are no longer needed. */ 265 if (ko->ko_progtab != NULL) { 266 kmem_free(ko->ko_progtab, ko->ko_nprogtab * 267 sizeof(*ko->ko_progtab)); 268 ko->ko_progtab = NULL; 269 } 270 if (ko->ko_shstrtab) { 271 kmem_free(ko->ko_shstrtab, ko->ko_shstrtabsz); 272 ko->ko_shstrtab = NULL; 273 } 274 275 /* If the object hasn't been loaded, then destroy it. */ 276 if (!ko->ko_loaded) { 277 kobj_unload(ko); 278 } 279 } 280 281 /* 282 * kobj_load: 283 * 284 * Load an ELF object from the file system and link into the 285 * running kernel image. 286 */ 287 int 288 kobj_load(kobj_t ko) 289 { 290 Elf_Ehdr *hdr; 291 Elf_Shdr *shdr; 292 Elf_Sym *es; 293 vaddr_t mapbase; 294 size_t mapsize; 295 int error; 296 int symtabindex; 297 int symstrindex; 298 int nsym; 299 int pb, rl, ra; 300 int alignmask; 301 int i, j; 302 303 KASSERT(ko->ko_type != KT_UNSET); 304 KASSERT(ko->ko_source != NULL); 305 306 shdr = NULL; 307 mapsize = 0; 308 error = 0; 309 hdr = NULL; 310 311 /* 312 * Read the elf header from the file. 313 */ 314 hdr = kmem_alloc(sizeof(*hdr), KM_SLEEP); 315 if (hdr == NULL) { 316 error = ENOMEM; 317 goto out; 318 } 319 error = kobj_read(ko, hdr, sizeof(*hdr), 0); 320 if (error != 0) 321 goto out; 322 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) { 323 kobj_error("not an ELF object"); 324 error = ENOEXEC; 325 goto out; 326 } 327 328 if (hdr->e_ident[EI_VERSION] != EV_CURRENT || 329 hdr->e_version != EV_CURRENT) { 330 kobj_error("unsupported file version"); 331 error = ENOEXEC; 332 goto out; 333 } 334 if (hdr->e_type != ET_REL) { 335 kobj_error("unsupported file type"); 336 error = ENOEXEC; 337 goto out; 338 } 339 switch (hdr->e_machine) { 340 #if ELFSIZE == 32 341 ELF32_MACHDEP_ID_CASES 342 #else 343 ELF64_MACHDEP_ID_CASES 344 #endif 345 default: 346 kobj_error("unsupported machine"); 347 error = ENOEXEC; 348 goto out; 349 } 350 351 ko->ko_nprogtab = 0; 352 ko->ko_shdr = 0; 353 ko->ko_nrel = 0; 354 ko->ko_nrela = 0; 355 356 /* 357 * Allocate and read in the section header. 358 */ 359 ko->ko_shdrsz = hdr->e_shnum * hdr->e_shentsize; 360 if (ko->ko_shdrsz == 0 || hdr->e_shoff == 0 || 361 hdr->e_shentsize != sizeof(Elf_Shdr)) { 362 error = ENOEXEC; 363 goto out; 364 } 365 shdr = kmem_alloc(ko->ko_shdrsz, KM_SLEEP); 366 if (shdr == NULL) { 367 error = ENOMEM; 368 goto out; 369 } 370 ko->ko_shdr = shdr; 371 error = kobj_read(ko, shdr, ko->ko_shdrsz, hdr->e_shoff); 372 if (error != 0) { 373 goto out; 374 } 375 376 /* 377 * Scan the section header for information and table sizing. 378 */ 379 nsym = 0; 380 symtabindex = -1; 381 symstrindex = -1; 382 for (i = 0; i < hdr->e_shnum; i++) { 383 switch (shdr[i].sh_type) { 384 case SHT_PROGBITS: 385 case SHT_NOBITS: 386 ko->ko_nprogtab++; 387 break; 388 case SHT_SYMTAB: 389 nsym++; 390 symtabindex = i; 391 symstrindex = shdr[i].sh_link; 392 break; 393 case SHT_REL: 394 ko->ko_nrel++; 395 break; 396 case SHT_RELA: 397 ko->ko_nrela++; 398 break; 399 case SHT_STRTAB: 400 break; 401 } 402 } 403 if (ko->ko_nprogtab == 0) { 404 kobj_error("file has no contents"); 405 error = ENOEXEC; 406 goto out; 407 } 408 if (nsym != 1) { 409 /* Only allow one symbol table for now */ 410 kobj_error("file has no valid symbol table"); 411 error = ENOEXEC; 412 goto out; 413 } 414 if (symstrindex < 0 || symstrindex > hdr->e_shnum || 415 shdr[symstrindex].sh_type != SHT_STRTAB) { 416 kobj_error("file has invalid symbol strings"); 417 error = ENOEXEC; 418 goto out; 419 } 420 421 /* 422 * Allocate space for tracking the load chunks. 423 */ 424 if (ko->ko_nprogtab != 0) { 425 ko->ko_progtab = kmem_zalloc(ko->ko_nprogtab * 426 sizeof(*ko->ko_progtab), KM_SLEEP); 427 if (ko->ko_progtab == NULL) { 428 error = ENOMEM; 429 goto out; 430 } 431 } 432 if (ko->ko_nrel != 0) { 433 ko->ko_reltab = kmem_zalloc(ko->ko_nrel * 434 sizeof(*ko->ko_reltab), KM_SLEEP); 435 if (ko->ko_reltab == NULL) { 436 error = ENOMEM; 437 goto out; 438 } 439 } 440 if (ko->ko_nrela != 0) { 441 ko->ko_relatab = kmem_zalloc(ko->ko_nrela * 442 sizeof(*ko->ko_relatab), KM_SLEEP); 443 if (ko->ko_relatab == NULL) { 444 error = ENOMEM; 445 goto out; 446 } 447 } 448 if (symtabindex == -1) { 449 kobj_error("lost symbol table index"); 450 goto out; 451 } 452 453 /* 454 * Allocate space for and load the symbol table. 455 */ 456 ko->ko_symcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym); 457 if (ko->ko_symcnt == 0) { 458 kobj_error("no symbol table"); 459 goto out; 460 } 461 ko->ko_symtab = kmem_alloc(ko->ko_symcnt * sizeof(Elf_Sym), KM_SLEEP); 462 if (ko->ko_symtab == NULL) { 463 error = ENOMEM; 464 goto out; 465 } 466 error = kobj_read(ko, ko->ko_symtab, shdr[symtabindex].sh_size, 467 shdr[symtabindex].sh_offset); 468 if (error != 0) { 469 goto out; 470 } 471 472 /* 473 * Allocate space for and load the symbol strings. 474 */ 475 ko->ko_strtabsz = shdr[symstrindex].sh_size; 476 if (ko->ko_strtabsz == 0) { 477 kobj_error("no symbol strings"); 478 goto out; 479 } 480 ko->ko_strtab = kmem_alloc(ko->ko_strtabsz, KM_SLEEP); 481 if (ko->ko_strtab == NULL) { 482 error = ENOMEM; 483 goto out; 484 } 485 error = kobj_read(ko, ko->ko_strtab, shdr[symstrindex].sh_size, 486 shdr[symstrindex].sh_offset); 487 if (error != 0) { 488 goto out; 489 } 490 491 /* 492 * Do we have a string table for the section names? 493 */ 494 if (hdr->e_shstrndx != 0 && shdr[hdr->e_shstrndx].sh_size != 0 && 495 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) { 496 ko->ko_shstrtabsz = shdr[hdr->e_shstrndx].sh_size; 497 ko->ko_shstrtab = kmem_alloc(shdr[hdr->e_shstrndx].sh_size, 498 KM_SLEEP); 499 if (ko->ko_shstrtab == NULL) { 500 error = ENOMEM; 501 goto out; 502 } 503 error = kobj_read(ko, ko->ko_shstrtab, 504 shdr[hdr->e_shstrndx].sh_size, 505 shdr[hdr->e_shstrndx].sh_offset); 506 if (error != 0) { 507 goto out; 508 } 509 } 510 511 /* 512 * Size up code/data(progbits) and bss(nobits). 513 */ 514 alignmask = 0; 515 for (i = 0; i < hdr->e_shnum; i++) { 516 switch (shdr[i].sh_type) { 517 case SHT_PROGBITS: 518 case SHT_NOBITS: 519 alignmask = shdr[i].sh_addralign - 1; 520 mapsize += alignmask; 521 mapsize &= ~alignmask; 522 mapsize += shdr[i].sh_size; 523 break; 524 } 525 } 526 527 /* 528 * We know how much space we need for the text/data/bss/etc. 529 * This stuff needs to be in a single chunk so that profiling etc 530 * can get the bounds and gdb can associate offsets with modules. 531 */ 532 if (mapsize == 0) { 533 kobj_error("no text/data/bss"); 534 goto out; 535 } 536 mapbase = uvm_km_alloc(lkm_map, round_page(mapsize), 0, 537 UVM_KMF_WIRED | UVM_KMF_EXEC); 538 if (mapbase == 0) { 539 error = ENOMEM; 540 goto out; 541 } 542 ko->ko_address = mapbase; 543 ko->ko_size = mapsize; 544 545 /* 546 * Now load code/data(progbits), zero bss(nobits), allocate space 547 * for and load relocs 548 */ 549 pb = 0; 550 rl = 0; 551 ra = 0; 552 alignmask = 0; 553 for (i = 0; i < hdr->e_shnum; i++) { 554 switch (shdr[i].sh_type) { 555 case SHT_PROGBITS: 556 case SHT_NOBITS: 557 alignmask = shdr[i].sh_addralign - 1; 558 mapbase += alignmask; 559 mapbase &= ~alignmask; 560 ko->ko_progtab[pb].addr = (void *)mapbase; 561 if (shdr[i].sh_type == SHT_PROGBITS) { 562 ko->ko_progtab[pb].name = "<<PROGBITS>>"; 563 error = kobj_read(ko, 564 ko->ko_progtab[pb].addr, shdr[i].sh_size, 565 shdr[i].sh_offset); 566 if (error != 0) { 567 goto out; 568 } 569 } else { 570 ko->ko_progtab[pb].name = "<<NOBITS>>"; 571 memset(ko->ko_progtab[pb].addr, 0, 572 shdr[i].sh_size); 573 } 574 ko->ko_progtab[pb].size = shdr[i].sh_size; 575 ko->ko_progtab[pb].sec = i; 576 if (ko->ko_shstrtab != NULL && shdr[i].sh_name != 0) { 577 ko->ko_progtab[pb].name = 578 ko->ko_shstrtab + shdr[i].sh_name; 579 } 580 581 /* Update all symbol values with the offset. */ 582 for (j = 0; j < ko->ko_symcnt; j++) { 583 es = &ko->ko_symtab[j]; 584 if (es->st_shndx != i) { 585 continue; 586 } 587 es->st_value += 588 (Elf_Addr)ko->ko_progtab[pb].addr; 589 } 590 mapbase += shdr[i].sh_size; 591 pb++; 592 break; 593 case SHT_REL: 594 ko->ko_reltab[rl].size = shdr[i].sh_size; 595 ko->ko_reltab[rl].size -= 596 shdr[i].sh_size % sizeof(Elf_Rel); 597 if (ko->ko_reltab[rl].size != 0) { 598 ko->ko_reltab[rl].rel = 599 kmem_alloc(ko->ko_reltab[rl].size, 600 KM_SLEEP); 601 ko->ko_reltab[rl].nrel = 602 shdr[i].sh_size / sizeof(Elf_Rel); 603 ko->ko_reltab[rl].sec = shdr[i].sh_info; 604 error = kobj_read(ko, 605 ko->ko_reltab[rl].rel, 606 ko->ko_reltab[rl].size, 607 shdr[i].sh_offset); 608 if (error != 0) { 609 goto out; 610 } 611 } 612 rl++; 613 break; 614 case SHT_RELA: 615 ko->ko_relatab[ra].size = shdr[i].sh_size; 616 ko->ko_relatab[ra].size -= 617 shdr[i].sh_size % sizeof(Elf_Rela); 618 if (ko->ko_relatab[ra].size != 0) { 619 ko->ko_relatab[ra].rela = 620 kmem_alloc(ko->ko_relatab[ra].size, 621 KM_SLEEP); 622 ko->ko_relatab[ra].nrela = 623 shdr[i].sh_size / sizeof(Elf_Rela); 624 ko->ko_relatab[ra].sec = shdr[i].sh_info; 625 error = kobj_read(ko, 626 ko->ko_relatab[ra].rela, 627 shdr[i].sh_size, 628 shdr[i].sh_offset); 629 if (error != 0) { 630 goto out; 631 } 632 } 633 ra++; 634 break; 635 } 636 } 637 if (pb != ko->ko_nprogtab) { 638 panic("lost progbits"); 639 } 640 if (rl != ko->ko_nrel) { 641 panic("lost rel"); 642 } 643 if (ra != ko->ko_nrela) { 644 panic("lost rela"); 645 } 646 if (mapbase != ko->ko_address + mapsize) { 647 panic("mapbase 0x%lx != address %lx + mapsize 0x%lx (0x%lx)\n", 648 (long)mapbase, (long)ko->ko_address, (long)mapsize, 649 (long)ko->ko_address + mapsize); 650 } 651 652 /* 653 * Perform relocations. Done before registering with ksyms, 654 * which will pack our symbol table. 655 */ 656 error = kobj_relocate(ko); 657 if (error != 0) { 658 goto out; 659 } 660 661 /* 662 * Notify MD code that a module has been loaded. 663 */ 664 error = kobj_machdep(ko, (void *)ko->ko_address, ko->ko_size, true); 665 if (error != 0) { 666 kobj_error("machine dependent init failed"); 667 goto out; 668 } 669 ko->ko_loaded = true; 670 out: 671 kobj_release_mem(ko); 672 if (hdr != NULL) { 673 kmem_free(hdr, sizeof(*hdr)); 674 } 675 676 return error; 677 } 678 679 /* 680 * kobj_unload: 681 * 682 * Unload an object previously loaded by kobj_load(). 683 */ 684 void 685 kobj_unload(kobj_t ko) 686 { 687 int error; 688 689 KASSERT(ko->ko_progtab == NULL); 690 KASSERT(ko->ko_shstrtab == NULL); 691 692 if (ko->ko_address != 0) { 693 uvm_km_free(lkm_map, ko->ko_address, round_page(ko->ko_size), 694 UVM_KMF_WIRED); 695 } 696 if (ko->ko_ksyms == true) { 697 ksyms_delsymtab(ko->ko_name); 698 } 699 if (ko->ko_symtab != NULL) { 700 kmem_free(ko->ko_symtab, ko->ko_symcnt * sizeof(Elf_Sym)); 701 } 702 if (ko->ko_strtab != NULL) { 703 kmem_free(ko->ko_strtab, ko->ko_strtabsz); 704 } 705 706 /* 707 * Notify MD code that a module has been unloaded. 708 */ 709 if (ko->ko_loaded) { 710 error = kobj_machdep(ko, (void *)ko->ko_address, ko->ko_size, 711 false); 712 if (error != 0) { 713 kobj_error("machine dependent deinit failed"); 714 } 715 } 716 717 kmem_free(ko, sizeof(*ko)); 718 } 719 720 /* 721 * kobj_stat: 722 * 723 * Return size and load address of an object. 724 */ 725 void 726 kobj_stat(kobj_t ko, vaddr_t *address, size_t *size) 727 { 728 729 if (address != NULL) { 730 *address = ko->ko_address; 731 } 732 if (size != NULL) { 733 *size = ko->ko_size; 734 } 735 } 736 737 /* 738 * kobj_set_name: 739 * 740 * Set an object's name. Used only for symbol table lookups. 741 * May only be called after the module is loaded. 742 */ 743 int 744 kobj_set_name(kobj_t ko, const char *name) 745 { 746 int error; 747 748 KASSERT(ko->ko_loaded); 749 750 strlcpy(ko->ko_name, name, sizeof(ko->ko_name)); 751 752 /* 753 * Now that we know the name, register the symbol table. 754 */ 755 error = ksyms_addsymtab(ko->ko_name, ko->ko_symtab, ko->ko_symcnt * 756 sizeof(Elf_Sym), ko->ko_strtab, ko->ko_strtabsz); 757 if (error != 0) { 758 kobj_error("unable to register module symbol table"); 759 } else { 760 ko->ko_ksyms = true; 761 } 762 763 return error; 764 } 765 766 /* 767 * kobj_find_section: 768 * 769 * Given a section name, search the loaded object and return 770 * virtual address if present and loaded. 771 */ 772 int 773 kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size) 774 { 775 int i; 776 777 KASSERT(ko->ko_progtab != NULL); 778 779 for (i = 0; i < ko->ko_nprogtab; i++) { 780 if (strcmp(ko->ko_progtab[i].name, name) == 0) { 781 if (addr != NULL) { 782 *addr = ko->ko_progtab[i].addr; 783 } 784 if (size != NULL) { 785 *size = ko->ko_progtab[i].size; 786 } 787 return 0; 788 } 789 } 790 791 return ENOENT; 792 } 793 794 /* 795 * kobj_release_mem: 796 * 797 * Release object data not needed after loading. 798 */ 799 static void 800 kobj_release_mem(kobj_t ko) 801 { 802 int i; 803 804 for (i = 0; i < ko->ko_nrel; i++) { 805 if (ko->ko_reltab[i].rel) { 806 kmem_free(ko->ko_reltab[i].rel, 807 ko->ko_reltab[i].size); 808 } 809 } 810 for (i = 0; i < ko->ko_nrela; i++) { 811 if (ko->ko_relatab[i].rela) { 812 kmem_free(ko->ko_relatab[i].rela, 813 ko->ko_relatab[i].size); 814 } 815 } 816 if (ko->ko_reltab != NULL) { 817 kmem_free(ko->ko_reltab, ko->ko_nrel * 818 sizeof(*ko->ko_reltab)); 819 ko->ko_reltab = NULL; 820 ko->ko_nrel = 0; 821 } 822 if (ko->ko_relatab != NULL) { 823 kmem_free(ko->ko_relatab, ko->ko_nrela * 824 sizeof(*ko->ko_relatab)); 825 ko->ko_relatab = NULL; 826 ko->ko_nrela = 0; 827 } 828 if (ko->ko_shdr != NULL) { 829 kmem_free(ko->ko_shdr, ko->ko_shdrsz); 830 ko->ko_shdr = NULL; 831 } 832 } 833 834 /* 835 * kobj_sym_lookup: 836 * 837 * Symbol lookup function to be used when the symbol index 838 * is known (ie during relocation). 839 */ 840 uintptr_t 841 kobj_sym_lookup(kobj_t ko, uintptr_t symidx) 842 { 843 const Elf_Sym *sym; 844 const char *symbol; 845 int error; 846 u_long addr; 847 848 /* Don't even try to lookup the symbol if the index is bogus. */ 849 if (symidx >= ko->ko_symcnt) 850 return 0; 851 852 sym = ko->ko_symtab + symidx; 853 854 /* Quick answer if there is a definition included. */ 855 if (sym->st_shndx != SHN_UNDEF) { 856 return sym->st_value; 857 } 858 859 /* If we get here, then it is undefined and needs a lookup. */ 860 switch (ELF_ST_BIND(sym->st_info)) { 861 case STB_LOCAL: 862 /* Local, but undefined? huh? */ 863 kobj_error("local symbol undefined"); 864 return 0; 865 866 case STB_GLOBAL: 867 /* Relative to Data or Function name */ 868 symbol = ko->ko_strtab + sym->st_name; 869 870 /* Force a lookup failure if the symbol name is bogus. */ 871 if (*symbol == 0) { 872 kobj_error("bad symbol name"); 873 return 0; 874 } 875 876 error = ksyms_getval(NULL, symbol, &addr, KSYMS_ANY); 877 if (error != 0) { 878 kobj_error("symbol %s undefined", symbol); 879 return (uintptr_t)0; 880 } 881 return (uintptr_t)addr; 882 883 case STB_WEAK: 884 kobj_error("weak symbols not supported\n"); 885 return 0; 886 887 default: 888 return 0; 889 } 890 } 891 892 /* 893 * kobj_findbase: 894 * 895 * Return base address of the given section. 896 */ 897 static uintptr_t 898 kobj_findbase(kobj_t ko, int sec) 899 { 900 int i; 901 902 for (i = 0; i < ko->ko_nprogtab; i++) { 903 if (sec == ko->ko_progtab[i].sec) { 904 return (uintptr_t)ko->ko_progtab[i].addr; 905 } 906 } 907 return 0; 908 } 909 910 /* 911 * kobj_relocate: 912 * 913 * Resolve all relocations for the loaded object. 914 */ 915 static int 916 kobj_relocate(kobj_t ko) 917 { 918 const Elf_Rel *rellim; 919 const Elf_Rel *rel; 920 const Elf_Rela *relalim; 921 const Elf_Rela *rela; 922 const Elf_Sym *sym; 923 uintptr_t base; 924 int i, error; 925 uintptr_t symidx; 926 927 /* 928 * Perform relocations without addend if there are any. 929 */ 930 for (i = 0; i < ko->ko_nrel; i++) { 931 rel = ko->ko_reltab[i].rel; 932 if (rel == NULL) { 933 continue; 934 } 935 rellim = rel + ko->ko_reltab[i].nrel; 936 base = kobj_findbase(ko, ko->ko_reltab[i].sec); 937 if (base == 0) { 938 panic("lost base for e_reltab"); 939 } 940 for (; rel < rellim; rel++) { 941 symidx = ELF_R_SYM(rel->r_info); 942 if (symidx >= ko->ko_symcnt) { 943 continue; 944 } 945 sym = ko->ko_symtab + symidx; 946 error = kobj_reloc(ko, base, rel, false, 947 ELF_ST_BIND(sym->st_info) == STB_LOCAL); 948 if (error != 0) { 949 return ENOENT; 950 } 951 } 952 } 953 954 /* 955 * Perform relocations with addend if there are any. 956 */ 957 for (i = 0; i < ko->ko_nrela; i++) { 958 rela = ko->ko_relatab[i].rela; 959 if (rela == NULL) { 960 continue; 961 } 962 relalim = rela + ko->ko_relatab[i].nrela; 963 base = kobj_findbase(ko, ko->ko_relatab[i].sec); 964 if (base == 0) { 965 panic("lost base for e_relatab"); 966 } 967 for (; rela < relalim; rela++) { 968 symidx = ELF_R_SYM(rela->r_info); 969 if (symidx >= ko->ko_symcnt) { 970 continue; 971 } 972 sym = ko->ko_symtab + symidx; 973 error = kobj_reloc(ko, base, rela, true, 974 ELF_ST_BIND(sym->st_info) == STB_LOCAL); 975 if (error != 0) { 976 return ENOENT; 977 } 978 } 979 } 980 981 return 0; 982 } 983 984 /* 985 * kobj_error: 986 * 987 * Utility function: log an error. 988 */ 989 static void 990 kobj_error(const char *fmt, ...) 991 { 992 va_list ap; 993 994 va_start(ap, fmt); 995 printf("WARNING: linker error: "); 996 vprintf(fmt, ap); 997 printf("\n"); 998 va_end(ap); 999 } 1000 1001 /* 1002 * kobj_read: 1003 * 1004 * Utility function: read from the object. 1005 */ 1006 static int 1007 kobj_read(kobj_t ko, void *base, size_t size, off_t off) 1008 { 1009 size_t resid; 1010 int error; 1011 1012 KASSERT(ko->ko_source != NULL); 1013 1014 switch (ko->ko_type) { 1015 case KT_VNODE: 1016 error = vn_rdwr(UIO_READ, ko->ko_source, base, size, off, 1017 UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, 1018 curlwp); 1019 if (error == 0 && resid != 0) { 1020 error = EINVAL; 1021 } 1022 break; 1023 case KT_MEMORY: 1024 if (ko->ko_memsize != -1 && off + size > ko->ko_memsize) { 1025 kobj_error("kobj_read: preloaded object short"); 1026 error = EINVAL; 1027 } else { 1028 memcpy(base, (uint8_t *)ko->ko_source + off, size); 1029 error = 0; 1030 } 1031 break; 1032 default: 1033 panic("kobj_read: invalid type"); 1034 } 1035 1036 return error; 1037 } 1038 1039 #else /* MODULAR */ 1040 1041 int 1042 kobj_open_file(kobj_t *kop, const char *name) 1043 { 1044 1045 return ENOSYS; 1046 } 1047 1048 int 1049 kobj_open_mem(kobj_t *kop, void *base, ssize_t size) 1050 { 1051 1052 return ENOSYS; 1053 } 1054 1055 void 1056 kobj_close(kobj_t ko) 1057 { 1058 1059 panic("not modular"); 1060 } 1061 1062 int 1063 kobj_load(kobj_t ko) 1064 { 1065 1066 panic("not modular"); 1067 } 1068 1069 void 1070 kobj_unload(kobj_t ko) 1071 { 1072 1073 panic("not modular"); 1074 } 1075 1076 void 1077 kobj_stat(kobj_t ko, vaddr_t *base, size_t *size) 1078 { 1079 1080 panic("not modular"); 1081 } 1082 1083 int 1084 kobj_set_name(kobj_t ko, const char *name) 1085 { 1086 1087 panic("not modular"); 1088 } 1089 1090 int 1091 kobj_find_section(kobj_t ko, const char *name, void **addr, size_t *size) 1092 { 1093 1094 panic("not modular"); 1095 } 1096 1097 #endif /* MODULAR */ 1098