1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include <sys/param.h> 30 #include <sys/endian.h> 31 #include <sys/exec.h> 32 #include <sys/linker.h> 33 #include <sys/module.h> 34 #include <sys/stdint.h> 35 #include <string.h> 36 #include <machine/elf.h> 37 #include <stand.h> 38 #define FREEBSD_ELF 39 #include <sys/link_elf.h> 40 41 #include "bootstrap.h" 42 43 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l) 44 45 #if defined(__i386__) && __ELF_WORD_SIZE == 64 46 #undef ELF_TARG_CLASS 47 #undef ELF_TARG_MACH 48 #define ELF_TARG_CLASS ELFCLASS64 49 #define ELF_TARG_MACH EM_X86_64 50 #endif 51 52 typedef struct elf_file { 53 Elf_Phdr *ph; 54 Elf_Ehdr *ehdr; 55 Elf_Sym *symtab; 56 Elf_Hashelt *hashtab; 57 Elf_Hashelt nbuckets; 58 Elf_Hashelt nchains; 59 Elf_Hashelt *buckets; 60 Elf_Hashelt *chains; 61 Elf_Rel *rel; 62 size_t relsz; 63 Elf_Rela *rela; 64 size_t relasz; 65 char *strtab; 66 size_t strsz; 67 int fd; 68 caddr_t firstpage; 69 size_t firstlen; 70 int kernel; 71 uint64_t off; 72 #ifdef LOADER_VERIEXEC_VECTX 73 struct vectx *vctx; 74 #endif 75 } *elf_file_t; 76 77 #ifdef LOADER_VERIEXEC_VECTX 78 #define VECTX_HANDLE(ef) (ef)->vctx 79 #else 80 #define VECTX_HANDLE(ef) (ef)->fd 81 #endif 82 83 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, 84 uint64_t loadaddr); 85 static int __elfN(lookup_symbol)(elf_file_t ef, const char* name, 86 Elf_Sym *sym, unsigned char type); 87 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 88 Elf_Addr p, void *val, size_t len); 89 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef, 90 Elf_Addr p_start, Elf_Addr p_end); 91 static symaddr_fn __elfN(symaddr); 92 static char *fake_modname(const char *name); 93 94 const char *__elfN(kerneltype) = "elf kernel"; 95 const char *__elfN(moduletype) = "elf module"; 96 97 uint64_t __elfN(relocation_offset) = 0; 98 99 #ifdef __powerpc__ 100 extern void elf_wrong_field_size(void); 101 #define CONVERT_FIELD(b, f, e) \ 102 switch (sizeof((b)->f)) { \ 103 case 2: \ 104 (b)->f = e ## 16toh((b)->f); \ 105 break; \ 106 case 4: \ 107 (b)->f = e ## 32toh((b)->f); \ 108 break; \ 109 case 8: \ 110 (b)->f = e ## 64toh((b)->f); \ 111 break; \ 112 default: \ 113 /* Force a link time error. */ \ 114 elf_wrong_field_size(); \ 115 break; \ 116 } 117 118 #define CONVERT_SWITCH(h, d, f) \ 119 switch ((h)->e_ident[EI_DATA]) { \ 120 case ELFDATA2MSB: \ 121 f(d, be); \ 122 break; \ 123 case ELFDATA2LSB: \ 124 f(d, le); \ 125 break; \ 126 default: \ 127 return (EINVAL); \ 128 } 129 130 131 static int elf_header_convert(Elf_Ehdr *ehdr) 132 { 133 /* 134 * Fixup ELF header endianness. 135 * 136 * The Xhdr structure was loaded using block read call to optimize file 137 * accesses. It might happen, that the endianness of the system memory 138 * is different that endianness of the ELF header. Swap fields here to 139 * guarantee that Xhdr always contain valid data regardless of 140 * architecture. 141 */ 142 #define HEADER_FIELDS(b, e) \ 143 CONVERT_FIELD(b, e_type, e); \ 144 CONVERT_FIELD(b, e_machine, e); \ 145 CONVERT_FIELD(b, e_version, e); \ 146 CONVERT_FIELD(b, e_entry, e); \ 147 CONVERT_FIELD(b, e_phoff, e); \ 148 CONVERT_FIELD(b, e_shoff, e); \ 149 CONVERT_FIELD(b, e_flags, e); \ 150 CONVERT_FIELD(b, e_ehsize, e); \ 151 CONVERT_FIELD(b, e_phentsize, e); \ 152 CONVERT_FIELD(b, e_phnum, e); \ 153 CONVERT_FIELD(b, e_shentsize, e); \ 154 CONVERT_FIELD(b, e_shnum, e); \ 155 CONVERT_FIELD(b, e_shstrndx, e) 156 157 CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS); 158 159 #undef HEADER_FIELDS 160 161 return (0); 162 } 163 164 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr) 165 { 166 #define PROGRAM_HEADER_FIELDS(b, e) \ 167 CONVERT_FIELD(b, p_type, e); \ 168 CONVERT_FIELD(b, p_flags, e); \ 169 CONVERT_FIELD(b, p_offset, e); \ 170 CONVERT_FIELD(b, p_vaddr, e); \ 171 CONVERT_FIELD(b, p_paddr, e); \ 172 CONVERT_FIELD(b, p_filesz, e); \ 173 CONVERT_FIELD(b, p_memsz, e); \ 174 CONVERT_FIELD(b, p_align, e) 175 176 CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS); 177 178 #undef PROGRAM_HEADER_FIELDS 179 180 return (0); 181 } 182 183 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr) 184 { 185 #define SECTION_HEADER_FIELDS(b, e) \ 186 CONVERT_FIELD(b, sh_name, e); \ 187 CONVERT_FIELD(b, sh_type, e); \ 188 CONVERT_FIELD(b, sh_link, e); \ 189 CONVERT_FIELD(b, sh_info, e); \ 190 CONVERT_FIELD(b, sh_flags, e); \ 191 CONVERT_FIELD(b, sh_addr, e); \ 192 CONVERT_FIELD(b, sh_offset, e); \ 193 CONVERT_FIELD(b, sh_size, e); \ 194 CONVERT_FIELD(b, sh_addralign, e); \ 195 CONVERT_FIELD(b, sh_entsize, e) 196 197 CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS); 198 199 #undef SECTION_HEADER_FIELDS 200 201 return (0); 202 } 203 #undef CONVERT_SWITCH 204 #undef CONVERT_FIELD 205 #else 206 static int elf_header_convert(Elf_Ehdr *ehdr) 207 { 208 return (0); 209 } 210 211 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr) 212 { 213 return (0); 214 } 215 216 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr) 217 { 218 return (0); 219 } 220 #endif 221 222 #ifdef __amd64__ 223 static bool 224 is_kernphys_relocatable(elf_file_t ef) 225 { 226 Elf_Sym sym; 227 228 return (__elfN(lookup_symbol)(ef, "kernphys", &sym, STT_OBJECT) == 0); 229 } 230 #endif 231 232 #ifdef __i386__ 233 static bool 234 is_tg_kernel_support(struct preloaded_file *fp, elf_file_t ef) 235 { 236 Elf_Sym sym; 237 Elf_Addr p_start, p_end, v, p; 238 char vd_name[16]; 239 int error; 240 241 if (__elfN(lookup_symbol)(ef, "__start_set_vt_drv_set", &sym, STT_NOTYPE) != 0) 242 return (false); 243 p_start = sym.st_value + ef->off; 244 if (__elfN(lookup_symbol)(ef, "__stop_set_vt_drv_set", &sym, STT_NOTYPE) != 0) 245 return (false); 246 p_end = sym.st_value + ef->off; 247 248 /* 249 * Walk through vt_drv_set, each vt driver structure starts with 250 * static 16 chars for driver name. If we have "vbefb", return true. 251 */ 252 for (p = p_start; p < p_end; p += sizeof(Elf_Addr)) { 253 COPYOUT(p, &v, sizeof(v)); 254 255 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v)); 256 if (error == EOPNOTSUPP) 257 v += ef->off; 258 else if (error != 0) 259 return (false); 260 COPYOUT(v, &vd_name, sizeof(vd_name)); 261 if (strncmp(vd_name, "vbefb", sizeof(vd_name)) == 0) 262 return (true); 263 } 264 265 return (false); 266 } 267 #endif 268 269 static int 270 __elfN(load_elf_header)(char *filename, elf_file_t ef) 271 { 272 ssize_t bytes_read; 273 Elf_Ehdr *ehdr; 274 int err; 275 276 /* 277 * Open the image, read and validate the ELF header 278 */ 279 if (filename == NULL) /* can't handle nameless */ 280 return (EFTYPE); 281 if ((ef->fd = open(filename, O_RDONLY)) == -1) 282 return (errno); 283 ef->firstpage = malloc(PAGE_SIZE); 284 if (ef->firstpage == NULL) { 285 close(ef->fd); 286 return (ENOMEM); 287 } 288 preload(ef->fd); 289 #ifdef LOADER_VERIEXEC_VECTX 290 { 291 int verror; 292 293 ef->vctx = vectx_open(ef->fd, filename, 0L, NULL, &verror, __func__); 294 if (verror) { 295 printf("Unverified %s: %s\n", filename, ve_error_get()); 296 close(ef->fd); 297 free(ef->vctx); 298 return (EAUTH); 299 } 300 } 301 #endif 302 bytes_read = VECTX_READ(VECTX_HANDLE(ef), ef->firstpage, PAGE_SIZE); 303 ef->firstlen = (size_t)bytes_read; 304 if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) { 305 err = EFTYPE; /* could be EIO, but may be small file */ 306 goto error; 307 } 308 ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage; 309 310 /* Is it ELF? */ 311 if (!IS_ELF(*ehdr)) { 312 err = EFTYPE; 313 goto error; 314 } 315 316 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ 317 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA || 318 ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ { 319 err = EFTYPE; 320 goto error; 321 } 322 323 err = elf_header_convert(ehdr); 324 if (err) 325 goto error; 326 327 if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) { 328 /* Machine ? */ 329 err = EFTYPE; 330 goto error; 331 } 332 333 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX) 334 if (verify_file(ef->fd, filename, bytes_read, VE_MUST, __func__) < 0) { 335 err = EAUTH; 336 goto error; 337 } 338 #endif 339 return (0); 340 341 error: 342 if (ef->firstpage != NULL) { 343 free(ef->firstpage); 344 ef->firstpage = NULL; 345 } 346 if (ef->fd != -1) { 347 #ifdef LOADER_VERIEXEC_VECTX 348 free(ef->vctx); 349 #endif 350 close(ef->fd); 351 ef->fd = -1; 352 } 353 return (err); 354 } 355 356 /* 357 * Attempt to load the file (file) as an ELF module. It will be stored at 358 * (dest), and a pointer to a module structure describing the loaded object 359 * will be saved in (result). 360 */ 361 int 362 __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result) 363 { 364 return (__elfN(loadfile_raw)(filename, dest, result, 0)); 365 } 366 367 int 368 __elfN(loadfile_raw)(char *filename, uint64_t dest, 369 struct preloaded_file **result, int multiboot) 370 { 371 struct preloaded_file *fp, *kfp; 372 struct elf_file ef; 373 Elf_Ehdr *ehdr; 374 int err; 375 376 fp = NULL; 377 bzero(&ef, sizeof(struct elf_file)); 378 ef.fd = -1; 379 380 err = __elfN(load_elf_header)(filename, &ef); 381 if (err != 0) 382 return (err); 383 384 ehdr = ef.ehdr; 385 386 /* 387 * Check to see what sort of module we are. 388 */ 389 kfp = file_findfile(NULL, __elfN(kerneltype)); 390 #ifdef __powerpc__ 391 /* 392 * Kernels can be ET_DYN, so just assume the first loaded object is the 393 * kernel. This assumption will be checked later. 394 */ 395 if (kfp == NULL) 396 ef.kernel = 1; 397 #endif 398 if (ef.kernel || ehdr->e_type == ET_EXEC) { 399 /* Looks like a kernel */ 400 if (kfp != NULL) { 401 printf("elf" __XSTRING(__ELF_WORD_SIZE) 402 "_loadfile: kernel already loaded\n"); 403 err = EPERM; 404 goto oerr; 405 } 406 /* 407 * Calculate destination address based on kernel entrypoint. 408 * 409 * For ARM, the destination address is independent of any values 410 * in the elf header (an ARM kernel can be loaded at any 2MB 411 * boundary), so we leave dest set to the value calculated by 412 * archsw.arch_loadaddr() and passed in to this function. 413 */ 414 #ifndef __arm__ 415 if (ehdr->e_type == ET_EXEC) 416 dest = (ehdr->e_entry & ~PAGE_MASK); 417 #endif 418 if ((ehdr->e_entry & ~PAGE_MASK) == 0) { 419 printf("elf" __XSTRING(__ELF_WORD_SIZE) 420 "_loadfile: not a kernel (maybe static binary?)\n"); 421 err = EPERM; 422 goto oerr; 423 } 424 ef.kernel = 1; 425 426 } else if (ehdr->e_type == ET_DYN) { 427 /* Looks like a kld module */ 428 if (multiboot != 0) { 429 printf("elf" __XSTRING(__ELF_WORD_SIZE) 430 "_loadfile: can't load module as multiboot\n"); 431 err = EPERM; 432 goto oerr; 433 } 434 if (kfp == NULL) { 435 printf("elf" __XSTRING(__ELF_WORD_SIZE) 436 "_loadfile: can't load module before kernel\n"); 437 err = EPERM; 438 goto oerr; 439 } 440 if (strcmp(__elfN(kerneltype), kfp->f_type)) { 441 printf("elf" __XSTRING(__ELF_WORD_SIZE) 442 "_loadfile: can't load module with kernel type '%s'\n", 443 kfp->f_type); 444 err = EPERM; 445 goto oerr; 446 } 447 /* Looks OK, got ahead */ 448 ef.kernel = 0; 449 450 } else { 451 err = EFTYPE; 452 goto oerr; 453 } 454 455 if (archsw.arch_loadaddr != NULL) 456 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest); 457 else 458 dest = roundup(dest, PAGE_SIZE); 459 460 /* 461 * Ok, we think we should handle this. 462 */ 463 fp = file_alloc(); 464 if (fp == NULL) { 465 printf("elf" __XSTRING(__ELF_WORD_SIZE) 466 "_loadfile: cannot allocate module info\n"); 467 err = EPERM; 468 goto out; 469 } 470 if (ef.kernel == 1 && multiboot == 0) 471 setenv("kernelname", filename, 1); 472 fp->f_name = strdup(filename); 473 if (multiboot == 0) 474 fp->f_type = strdup(ef.kernel ? 475 __elfN(kerneltype) : __elfN(moduletype)); 476 else 477 fp->f_type = strdup("elf multiboot kernel"); 478 479 if (module_verbose >= MODULE_VERBOSE_FULL) { 480 if (ef.kernel) 481 printf("%s entry at 0x%jx\n", filename, 482 (uintmax_t)ehdr->e_entry); 483 } else if (module_verbose > MODULE_VERBOSE_SILENT) 484 printf("%s ", filename); 485 486 fp->f_size = __elfN(loadimage)(fp, &ef, dest); 487 if (fp->f_size == 0 || fp->f_addr == 0) 488 goto ioerr; 489 490 /* save exec header as metadata */ 491 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr); 492 493 /* Load OK, return module pointer */ 494 *result = (struct preloaded_file *)fp; 495 err = 0; 496 #ifdef __amd64__ 497 fp->f_kernphys_relocatable = multiboot || is_kernphys_relocatable(&ef); 498 #endif 499 #ifdef __i386__ 500 fp->f_tg_kernel_support = is_tg_kernel_support(fp, &ef); 501 #endif 502 goto out; 503 504 ioerr: 505 err = EIO; 506 oerr: 507 file_discard(fp); 508 out: 509 if (ef.firstpage) 510 free(ef.firstpage); 511 if (ef.fd != -1) { 512 #ifdef LOADER_VERIEXEC_VECTX 513 if (!err && ef.vctx) { 514 int verror; 515 516 verror = vectx_close(ef.vctx, VE_MUST, __func__); 517 if (verror) { 518 err = EAUTH; 519 file_discard(fp); 520 } 521 } 522 #endif 523 close(ef.fd); 524 } 525 return (err); 526 } 527 528 /* 529 * With the file (fd) open on the image, and (ehdr) containing 530 * the Elf header, load the image at (off) 531 */ 532 static int 533 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off) 534 { 535 int i; 536 u_int j; 537 Elf_Ehdr *ehdr; 538 Elf_Phdr *phdr, *php; 539 Elf_Shdr *shdr; 540 char *shstr; 541 int ret; 542 vm_offset_t firstaddr; 543 vm_offset_t lastaddr; 544 size_t chunk; 545 ssize_t result; 546 Elf_Addr ssym, esym; 547 Elf_Dyn *dp; 548 Elf_Addr adp; 549 Elf_Addr ctors; 550 int ndp; 551 int symstrindex; 552 int symtabindex; 553 Elf_Size size; 554 u_int fpcopy; 555 Elf_Sym sym; 556 Elf_Addr p_start, p_end; 557 558 dp = NULL; 559 shdr = NULL; 560 ret = 0; 561 firstaddr = lastaddr = 0; 562 ehdr = ef->ehdr; 563 #ifdef __powerpc__ 564 if (ef->kernel) { 565 #else 566 if (ehdr->e_type == ET_EXEC) { 567 #endif 568 #if defined(__i386__) || defined(__amd64__) 569 #if __ELF_WORD_SIZE == 64 570 /* x86_64 relocates after locore */ 571 off = - (off & 0xffffffffff000000ull); 572 #else 573 /* i386 relocates after locore */ 574 off = - (off & 0xff000000u); 575 #endif 576 #elif defined(__powerpc__) 577 /* 578 * On the purely virtual memory machines like e500, the kernel 579 * is linked against its final VA range, which is most often 580 * not available at the loader stage, but only after kernel 581 * initializes and completes its VM settings. In such cases we 582 * cannot use p_vaddr field directly to load ELF segments, but 583 * put them at some 'load-time' locations. 584 */ 585 if (off & 0xf0000000u) { 586 off = -(off & 0xf0000000u); 587 /* 588 * XXX the physical load address should not be 589 * hardcoded. Note that the Book-E kernel assumes that 590 * it's loaded at a 16MB boundary for now... 591 */ 592 off += 0x01000000; 593 } 594 ehdr->e_entry += off; 595 if (module_verbose >= MODULE_VERBOSE_FULL) 596 printf("Converted entry 0x%jx\n", 597 (uintmax_t)ehdr->e_entry); 598 599 #elif defined(__arm__) && !defined(EFI) 600 /* 601 * The elf headers in arm kernels specify virtual addresses in 602 * all header fields, even the ones that should be physical 603 * addresses. We assume the entry point is in the first page, 604 * and masking the page offset will leave us with the virtual 605 * address the kernel was linked at. We subtract that from the 606 * load offset, making 'off' into the value which, when added 607 * to a virtual address in an elf header, translates it to a 608 * physical address. We do the va->pa conversion on the entry 609 * point address in the header now, so that later we can launch 610 * the kernel by just jumping to that address. 611 * 612 * When booting from UEFI the copyin and copyout functions 613 * handle adjusting the location relative to the first virtual 614 * address. Because of this there is no need to adjust the 615 * offset or entry point address as these will both be handled 616 * by the efi code. 617 */ 618 off -= ehdr->e_entry & ~PAGE_MASK; 619 ehdr->e_entry += off; 620 if (module_verbose >= MODULE_VERBOSE_FULL) 621 printf("ehdr->e_entry 0x%jx, va<->pa off %llx\n", 622 (uintmax_t)ehdr->e_entry, off); 623 #else 624 off = 0; /* other archs use direct mapped kernels */ 625 #endif 626 } 627 ef->off = off; 628 629 if (ef->kernel) 630 __elfN(relocation_offset) = off; 631 632 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) { 633 printf("elf" __XSTRING(__ELF_WORD_SIZE) 634 "_loadimage: program header not within first page\n"); 635 goto out; 636 } 637 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff); 638 639 for (i = 0; i < ehdr->e_phnum; i++) { 640 if (elf_program_header_convert(ehdr, phdr)) 641 continue; 642 643 /* We want to load PT_LOAD segments only.. */ 644 if (phdr[i].p_type != PT_LOAD) 645 continue; 646 647 if (module_verbose >= MODULE_VERBOSE_FULL) { 648 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx", 649 (long)phdr[i].p_filesz, (long)phdr[i].p_offset, 650 (long)(phdr[i].p_vaddr + off), 651 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 652 } else if (module_verbose > MODULE_VERBOSE_SILENT) { 653 if ((phdr[i].p_flags & PF_W) == 0) { 654 printf("text=0x%lx ", (long)phdr[i].p_filesz); 655 } else { 656 printf("data=0x%lx", (long)phdr[i].p_filesz); 657 if (phdr[i].p_filesz < phdr[i].p_memsz) 658 printf("+0x%lx", (long)(phdr[i].p_memsz - 659 phdr[i].p_filesz)); 660 printf(" "); 661 } 662 } 663 fpcopy = 0; 664 if (ef->firstlen > phdr[i].p_offset) { 665 fpcopy = ef->firstlen - phdr[i].p_offset; 666 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset, 667 phdr[i].p_vaddr + off, fpcopy); 668 } 669 if (phdr[i].p_filesz > fpcopy) { 670 if (kern_pread(VECTX_HANDLE(ef), 671 phdr[i].p_vaddr + off + fpcopy, 672 phdr[i].p_filesz - fpcopy, 673 phdr[i].p_offset + fpcopy) != 0) { 674 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 675 "_loadimage: read failed\n"); 676 goto out; 677 } 678 } 679 /* clear space from oversized segments; eg: bss */ 680 if (phdr[i].p_filesz < phdr[i].p_memsz) { 681 if (module_verbose >= MODULE_VERBOSE_FULL) { 682 printf(" (bss: 0x%lx-0x%lx)", 683 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz), 684 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1)); 685 } 686 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz, 687 phdr[i].p_memsz - phdr[i].p_filesz); 688 } 689 if (module_verbose >= MODULE_VERBOSE_FULL) 690 printf("\n"); 691 692 if (archsw.arch_loadseg != NULL) 693 archsw.arch_loadseg(ehdr, phdr + i, off); 694 695 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off)) 696 firstaddr = phdr[i].p_vaddr + off; 697 if (lastaddr == 0 || lastaddr < 698 (phdr[i].p_vaddr + off + phdr[i].p_memsz)) 699 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz; 700 } 701 lastaddr = roundup(lastaddr, sizeof(long)); 702 703 /* 704 * Get the section headers. We need this for finding the .ctors 705 * section as well as for loading any symbols. Both may be hard 706 * to do if reading from a .gz file as it involves seeking. I 707 * think the rule is going to have to be that you must strip a 708 * file to remove symbols before gzipping it. 709 */ 710 chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize; 711 if (chunk == 0 || ehdr->e_shoff == 0) 712 goto nosyms; 713 shdr = alloc_pread(VECTX_HANDLE(ef), ehdr->e_shoff, chunk); 714 if (shdr == NULL) { 715 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 716 "_loadimage: failed to read section headers"); 717 goto nosyms; 718 } 719 720 for (i = 0; i < ehdr->e_shnum; i++) 721 elf_section_header_convert(ehdr, &shdr[i]); 722 723 file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr); 724 725 /* 726 * Read the section string table and look for the .ctors section. 727 * We need to tell the kernel where it is so that it can call the 728 * ctors. 729 */ 730 chunk = shdr[ehdr->e_shstrndx].sh_size; 731 if (chunk) { 732 shstr = alloc_pread(VECTX_HANDLE(ef), 733 shdr[ehdr->e_shstrndx].sh_offset, chunk); 734 if (shstr) { 735 for (i = 0; i < ehdr->e_shnum; i++) { 736 if (strcmp(shstr + shdr[i].sh_name, 737 ".ctors") != 0) 738 continue; 739 ctors = shdr[i].sh_addr; 740 file_addmetadata(fp, MODINFOMD_CTORS_ADDR, 741 sizeof(ctors), &ctors); 742 size = shdr[i].sh_size; 743 file_addmetadata(fp, MODINFOMD_CTORS_SIZE, 744 sizeof(size), &size); 745 break; 746 } 747 free(shstr); 748 } 749 } 750 751 /* 752 * Now load any symbols. 753 */ 754 symtabindex = -1; 755 symstrindex = -1; 756 for (i = 0; i < ehdr->e_shnum; i++) { 757 if (shdr[i].sh_type != SHT_SYMTAB) 758 continue; 759 for (j = 0; j < ehdr->e_phnum; j++) { 760 if (phdr[j].p_type != PT_LOAD) 761 continue; 762 if (shdr[i].sh_offset >= phdr[j].p_offset && 763 (shdr[i].sh_offset + shdr[i].sh_size <= 764 phdr[j].p_offset + phdr[j].p_filesz)) { 765 shdr[i].sh_offset = 0; 766 shdr[i].sh_size = 0; 767 break; 768 } 769 } 770 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0) 771 continue; /* alread loaded in a PT_LOAD above */ 772 /* Save it for loading below */ 773 symtabindex = i; 774 symstrindex = shdr[i].sh_link; 775 } 776 if (symtabindex < 0 || symstrindex < 0) 777 goto nosyms; 778 779 /* Ok, committed to a load. */ 780 if (module_verbose >= MODULE_VERBOSE_FULL) 781 printf("syms=["); 782 ssym = lastaddr; 783 for (i = symtabindex; i >= 0; i = symstrindex) { 784 char *secname; 785 786 switch(shdr[i].sh_type) { 787 case SHT_SYMTAB: /* Symbol table */ 788 secname = "symtab"; 789 break; 790 case SHT_STRTAB: /* String table */ 791 secname = "strtab"; 792 break; 793 default: 794 secname = "WHOA!!"; 795 break; 796 } 797 size = shdr[i].sh_size; 798 799 archsw.arch_copyin(&size, lastaddr, sizeof(size)); 800 lastaddr += sizeof(size); 801 802 if (module_verbose >= MODULE_VERBOSE_FULL) { 803 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname, 804 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset, 805 (uintmax_t)lastaddr, 806 (uintmax_t)(lastaddr + shdr[i].sh_size)); 807 } else if (module_verbose > MODULE_VERBOSE_SILENT) { 808 if (i == symstrindex) 809 printf("+"); 810 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size); 811 } 812 if (VECTX_LSEEK(VECTX_HANDLE(ef), (off_t)shdr[i].sh_offset, SEEK_SET) == -1) { 813 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 814 "_loadimage: could not seek for symbols - skipped!"); 815 lastaddr = ssym; 816 ssym = 0; 817 goto nosyms; 818 } 819 result = archsw.arch_readin(VECTX_HANDLE(ef), lastaddr, shdr[i].sh_size); 820 if (result < 0 || (size_t)result != shdr[i].sh_size) { 821 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 822 "_loadimage: could not read symbols - skipped! " 823 "(%ju != %ju)", (uintmax_t)result, 824 (uintmax_t)shdr[i].sh_size); 825 lastaddr = ssym; 826 ssym = 0; 827 goto nosyms; 828 } 829 /* Reset offsets relative to ssym */ 830 lastaddr += shdr[i].sh_size; 831 lastaddr = roundup(lastaddr, sizeof(size)); 832 if (i == symtabindex) 833 symtabindex = -1; 834 else if (i == symstrindex) 835 symstrindex = -1; 836 } 837 esym = lastaddr; 838 if (module_verbose >= MODULE_VERBOSE_FULL) 839 printf("]"); 840 841 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym); 842 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym); 843 844 nosyms: 845 if (module_verbose > MODULE_VERBOSE_SILENT) 846 printf("\n"); 847 848 ret = lastaddr - firstaddr; 849 fp->f_addr = firstaddr; 850 851 php = NULL; 852 for (i = 0; i < ehdr->e_phnum; i++) { 853 if (phdr[i].p_type == PT_DYNAMIC) { 854 php = phdr + i; 855 adp = php->p_vaddr; 856 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), 857 &adp); 858 break; 859 } 860 } 861 862 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */ 863 goto out; 864 865 ndp = php->p_filesz / sizeof(Elf_Dyn); 866 if (ndp == 0) 867 goto out; 868 dp = malloc(php->p_filesz); 869 if (dp == NULL) 870 goto out; 871 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz); 872 873 ef->strsz = 0; 874 for (i = 0; i < ndp; i++) { 875 if (dp[i].d_tag == 0) 876 break; 877 switch (dp[i].d_tag) { 878 case DT_HASH: 879 ef->hashtab = 880 (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off); 881 break; 882 case DT_STRTAB: 883 ef->strtab = 884 (char *)(uintptr_t)(dp[i].d_un.d_ptr + off); 885 break; 886 case DT_STRSZ: 887 ef->strsz = dp[i].d_un.d_val; 888 break; 889 case DT_SYMTAB: 890 ef->symtab = 891 (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off); 892 break; 893 case DT_REL: 894 ef->rel = 895 (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off); 896 break; 897 case DT_RELSZ: 898 ef->relsz = dp[i].d_un.d_val; 899 break; 900 case DT_RELA: 901 ef->rela = 902 (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off); 903 break; 904 case DT_RELASZ: 905 ef->relasz = dp[i].d_un.d_val; 906 break; 907 default: 908 break; 909 } 910 } 911 if (ef->hashtab == NULL || ef->symtab == NULL || 912 ef->strtab == NULL || ef->strsz == 0) 913 goto out; 914 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets)); 915 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains)); 916 ef->buckets = ef->hashtab + 2; 917 ef->chains = ef->buckets + ef->nbuckets; 918 919 if (__elfN(lookup_symbol)(ef, "__start_set_modmetadata_set", &sym, 920 STT_NOTYPE) != 0) 921 return 0; 922 p_start = sym.st_value + ef->off; 923 if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set", &sym, 924 STT_NOTYPE) != 0) 925 return 0; 926 p_end = sym.st_value + ef->off; 927 928 if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0) 929 goto out; 930 931 if (ef->kernel) /* kernel must not depend on anything */ 932 goto out; 933 934 out: 935 if (dp) 936 free(dp); 937 if (shdr) 938 free(shdr); 939 return ret; 940 } 941 942 static char invalid_name[] = "bad"; 943 944 char * 945 fake_modname(const char *name) 946 { 947 const char *sp, *ep; 948 char *fp; 949 size_t len; 950 951 sp = strrchr(name, '/'); 952 if (sp) 953 sp++; 954 else 955 sp = name; 956 957 ep = strrchr(sp, '.'); 958 if (ep == NULL) { 959 ep = sp + strlen(sp); 960 } 961 if (ep == sp) { 962 sp = invalid_name; 963 ep = invalid_name + sizeof(invalid_name) - 1; 964 } 965 966 len = ep - sp; 967 fp = malloc(len + 1); 968 if (fp == NULL) 969 return NULL; 970 memcpy(fp, sp, len); 971 fp[len] = '\0'; 972 return fp; 973 } 974 975 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64 976 struct mod_metadata64 { 977 int md_version; /* structure version MDTV_* */ 978 int md_type; /* type of entry MDT_* */ 979 uint64_t md_data; /* specific data */ 980 uint64_t md_cval; /* common string label */ 981 }; 982 #endif 983 #if defined(__amd64__) && __ELF_WORD_SIZE == 32 984 struct mod_metadata32 { 985 int md_version; /* structure version MDTV_* */ 986 int md_type; /* type of entry MDT_* */ 987 uint32_t md_data; /* specific data */ 988 uint32_t md_cval; /* common string label */ 989 }; 990 #endif 991 992 int 993 __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest) 994 { 995 struct elf_file ef; 996 int err, i, j; 997 Elf_Shdr *sh_meta, *shdr = NULL; 998 Elf_Shdr *sh_data[2]; 999 char *shstrtab = NULL; 1000 size_t size; 1001 Elf_Addr p_start, p_end; 1002 1003 bzero(&ef, sizeof(struct elf_file)); 1004 ef.fd = -1; 1005 1006 err = __elfN(load_elf_header)(fp->f_name, &ef); 1007 if (err != 0) 1008 goto out; 1009 1010 if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) { 1011 ef.kernel = 1; 1012 } else if (ef.ehdr->e_type != ET_DYN) { 1013 err = EFTYPE; 1014 goto out; 1015 } 1016 1017 size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize; 1018 shdr = alloc_pread(VECTX_HANDLE(&ef), ef.ehdr->e_shoff, size); 1019 if (shdr == NULL) { 1020 err = ENOMEM; 1021 goto out; 1022 } 1023 1024 /* Load shstrtab. */ 1025 shstrtab = alloc_pread(VECTX_HANDLE(&ef), shdr[ef.ehdr->e_shstrndx].sh_offset, 1026 shdr[ef.ehdr->e_shstrndx].sh_size); 1027 if (shstrtab == NULL) { 1028 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 1029 "load_modmetadata: unable to load shstrtab\n"); 1030 err = EFTYPE; 1031 goto out; 1032 } 1033 1034 /* Find set_modmetadata_set and data sections. */ 1035 sh_data[0] = sh_data[1] = sh_meta = NULL; 1036 for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) { 1037 if (strcmp(&shstrtab[shdr[i].sh_name], 1038 "set_modmetadata_set") == 0) { 1039 sh_meta = &shdr[i]; 1040 } 1041 if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) || 1042 (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) { 1043 sh_data[j++] = &shdr[i]; 1044 } 1045 } 1046 if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) { 1047 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 1048 "load_modmetadata: unable to find set_modmetadata_set or data sections\n"); 1049 err = EFTYPE; 1050 goto out; 1051 } 1052 1053 /* Load set_modmetadata_set into memory */ 1054 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_meta->sh_size, sh_meta->sh_offset); 1055 if (err != 0) { 1056 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 1057 "load_modmetadata: unable to load set_modmetadata_set: %d\n", err); 1058 goto out; 1059 } 1060 p_start = dest; 1061 p_end = dest + sh_meta->sh_size; 1062 dest += sh_meta->sh_size; 1063 1064 /* Load data sections into memory. */ 1065 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[0]->sh_size, 1066 sh_data[0]->sh_offset); 1067 if (err != 0) { 1068 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 1069 "load_modmetadata: unable to load data: %d\n", err); 1070 goto out; 1071 } 1072 1073 /* 1074 * We have to increment the dest, so that the offset is the same into 1075 * both the .rodata and .data sections. 1076 */ 1077 ef.off = -(sh_data[0]->sh_addr - dest); 1078 dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr); 1079 1080 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[1]->sh_size, 1081 sh_data[1]->sh_offset); 1082 if (err != 0) { 1083 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 1084 "load_modmetadata: unable to load data: %d\n", err); 1085 goto out; 1086 } 1087 1088 err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end); 1089 if (err != 0) { 1090 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 1091 "load_modmetadata: unable to parse metadata: %d\n", err); 1092 goto out; 1093 } 1094 1095 out: 1096 if (shstrtab != NULL) 1097 free(shstrtab); 1098 if (shdr != NULL) 1099 free(shdr); 1100 if (ef.firstpage != NULL) 1101 free(ef.firstpage); 1102 if (ef.fd != -1) { 1103 #ifdef LOADER_VERIEXEC_VECTX 1104 if (!err && ef.vctx) { 1105 int verror; 1106 1107 verror = vectx_close(ef.vctx, VE_MUST, __func__); 1108 if (verror) { 1109 err = EAUTH; 1110 file_discard(fp); 1111 } 1112 } 1113 #endif 1114 close(ef.fd); 1115 } 1116 return (err); 1117 } 1118 1119 int 1120 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef, 1121 Elf_Addr p_start, Elf_Addr p_end) 1122 { 1123 struct mod_metadata md; 1124 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64 1125 struct mod_metadata64 md64; 1126 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32 1127 struct mod_metadata32 md32; 1128 #endif 1129 struct mod_depend *mdepend; 1130 struct mod_version mver; 1131 char *s; 1132 int error, modcnt, minfolen; 1133 Elf_Addr v, p; 1134 1135 modcnt = 0; 1136 p = p_start; 1137 while (p < p_end) { 1138 COPYOUT(p, &v, sizeof(v)); 1139 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v)); 1140 if (error == EOPNOTSUPP) 1141 v += ef->off; 1142 else if (error != 0) 1143 return (error); 1144 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64 1145 COPYOUT(v, &md64, sizeof(md64)); 1146 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64)); 1147 if (error == EOPNOTSUPP) { 1148 md64.md_cval += ef->off; 1149 md64.md_data += ef->off; 1150 } else if (error != 0) 1151 return (error); 1152 md.md_version = md64.md_version; 1153 md.md_type = md64.md_type; 1154 md.md_cval = (const char *)(uintptr_t)md64.md_cval; 1155 md.md_data = (void *)(uintptr_t)md64.md_data; 1156 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32 1157 COPYOUT(v, &md32, sizeof(md32)); 1158 error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32)); 1159 if (error == EOPNOTSUPP) { 1160 md32.md_cval += ef->off; 1161 md32.md_data += ef->off; 1162 } else if (error != 0) 1163 return (error); 1164 md.md_version = md32.md_version; 1165 md.md_type = md32.md_type; 1166 md.md_cval = (const char *)(uintptr_t)md32.md_cval; 1167 md.md_data = (void *)(uintptr_t)md32.md_data; 1168 #else 1169 COPYOUT(v, &md, sizeof(md)); 1170 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md)); 1171 if (error == EOPNOTSUPP) { 1172 md.md_cval += ef->off; 1173 md.md_data = (void *)((uintptr_t)md.md_data + 1174 (uintptr_t)ef->off); 1175 } else if (error != 0) 1176 return (error); 1177 #endif 1178 p += sizeof(Elf_Addr); 1179 switch(md.md_type) { 1180 case MDT_DEPEND: 1181 if (ef->kernel) /* kernel must not depend on anything */ 1182 break; 1183 s = strdupout((vm_offset_t)md.md_cval); 1184 minfolen = sizeof(*mdepend) + strlen(s) + 1; 1185 mdepend = malloc(minfolen); 1186 if (mdepend == NULL) 1187 return ENOMEM; 1188 COPYOUT((vm_offset_t)md.md_data, mdepend, 1189 sizeof(*mdepend)); 1190 strcpy((char*)(mdepend + 1), s); 1191 free(s); 1192 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, 1193 mdepend); 1194 free(mdepend); 1195 break; 1196 case MDT_VERSION: 1197 s = strdupout((vm_offset_t)md.md_cval); 1198 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver)); 1199 file_addmodule(fp, s, mver.mv_version, NULL); 1200 free(s); 1201 modcnt++; 1202 break; 1203 } 1204 } 1205 if (modcnt == 0) { 1206 s = fake_modname(fp->f_name); 1207 file_addmodule(fp, s, 1, NULL); 1208 free(s); 1209 } 1210 return 0; 1211 } 1212 1213 static unsigned long 1214 elf_hash(const char *name) 1215 { 1216 const unsigned char *p = (const unsigned char *) name; 1217 unsigned long h = 0; 1218 unsigned long g; 1219 1220 while (*p != '\0') { 1221 h = (h << 4) + *p++; 1222 if ((g = h & 0xf0000000) != 0) 1223 h ^= g >> 24; 1224 h &= ~g; 1225 } 1226 return h; 1227 } 1228 1229 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) 1230 "_lookup_symbol: corrupt symbol table\n"; 1231 int 1232 __elfN(lookup_symbol)(elf_file_t ef, const char* name, Elf_Sym *symp, 1233 unsigned char type) 1234 { 1235 Elf_Hashelt symnum; 1236 Elf_Sym sym; 1237 char *strp; 1238 unsigned long hash; 1239 1240 if (ef->nbuckets == 0) { 1241 printf(__elfN(bad_symtable)); 1242 return ENOENT; 1243 } 1244 1245 hash = elf_hash(name); 1246 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum)); 1247 1248 while (symnum != STN_UNDEF) { 1249 if (symnum >= ef->nchains) { 1250 printf(__elfN(bad_symtable)); 1251 return ENOENT; 1252 } 1253 1254 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym)); 1255 if (sym.st_name == 0) { 1256 printf(__elfN(bad_symtable)); 1257 return ENOENT; 1258 } 1259 1260 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name)); 1261 if (strcmp(name, strp) == 0) { 1262 free(strp); 1263 if (sym.st_shndx != SHN_UNDEF && sym.st_value != 0 && 1264 ELF_ST_TYPE(sym.st_info) == type) { 1265 *symp = sym; 1266 return 0; 1267 } 1268 return ENOENT; 1269 } 1270 free(strp); 1271 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum)); 1272 } 1273 return ENOENT; 1274 } 1275 1276 /* 1277 * Apply any intra-module relocations to the value. p is the load address 1278 * of the value and val/len is the value to be modified. This does NOT modify 1279 * the image in-place, because this is done by kern_linker later on. 1280 * 1281 * Returns EOPNOTSUPP if no relocation method is supplied. 1282 */ 1283 static int 1284 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 1285 Elf_Addr p, void *val, size_t len) 1286 { 1287 size_t n; 1288 Elf_Rela a; 1289 Elf_Rel r; 1290 int error; 1291 1292 /* 1293 * The kernel is already relocated, but we still want to apply 1294 * offset adjustments. 1295 */ 1296 if (ef->kernel) 1297 return (EOPNOTSUPP); 1298 1299 for (n = 0; n < ef->relsz / sizeof(r); n++) { 1300 COPYOUT(ef->rel + n, &r, sizeof(r)); 1301 1302 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL, 1303 ef->off, p, val, len); 1304 if (error != 0) 1305 return (error); 1306 } 1307 for (n = 0; n < ef->relasz / sizeof(a); n++) { 1308 COPYOUT(ef->rela + n, &a, sizeof(a)); 1309 1310 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA, 1311 ef->off, p, val, len); 1312 if (error != 0) 1313 return (error); 1314 } 1315 1316 return (0); 1317 } 1318 1319 static Elf_Addr 1320 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx) 1321 { 1322 1323 /* Symbol lookup by index not required here. */ 1324 return (0); 1325 } 1326