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