30#include <sys/param.h> 31#include <sys/exec.h> 32#include <sys/linker.h> 33#include <sys/module.h> 34#include <string.h> 35#include <machine/elf.h> 36#include <stand.h> 37#define FREEBSD_ELF 38#include <link.h> 39 40#include "bootstrap.h" 41 42#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l) 43 44#if defined(__i386__) && __ELF_WORD_SIZE == 64 45#undef ELF_TARG_CLASS 46#undef ELF_TARG_MACH 47#define ELF_TARG_CLASS ELFCLASS64 48#define ELF_TARG_MACH EM_X86_64 49#endif 50 51typedef struct elf_file { 52 Elf_Phdr *ph; 53 Elf_Ehdr *ehdr; 54 Elf_Sym *symtab; 55 Elf_Hashelt *hashtab; 56 Elf_Hashelt nbuckets; 57 Elf_Hashelt nchains; 58 Elf_Hashelt *buckets; 59 Elf_Hashelt *chains; 60 Elf_Rela *rela; 61 size_t relasz; 62 char *strtab; 63 size_t strsz; 64 int fd; 65 caddr_t firstpage; 66 size_t firstlen; 67 int kernel; 68 u_int64_t off; 69} *elf_file_t; 70 71static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr); 72static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym); 73#ifdef __sparc__ 74static void __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 75 void *p, void *val, size_t len); 76#endif 77static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef); 78static char *fake_modname(const char *name); 79 80const char *__elfN(kerneltype) = "elf kernel"; 81const char *__elfN(moduletype) = "elf module"; 82 83/* 84 * Attempt to load the file (file) as an ELF module. It will be stored at 85 * (dest), and a pointer to a module structure describing the loaded object 86 * will be saved in (result). 87 */ 88int 89__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result) 90{ 91 struct preloaded_file *fp, *kfp; 92 struct elf_file ef; 93 Elf_Ehdr *ehdr; 94 int err; 95 u_int pad; 96 ssize_t bytes_read; 97 98 fp = NULL; 99 bzero(&ef, sizeof(struct elf_file)); 100 101 /* 102 * Open the image, read and validate the ELF header 103 */ 104 if (filename == NULL) /* can't handle nameless */ 105 return(EFTYPE); 106 if ((ef.fd = open(filename, O_RDONLY)) == -1) 107 return(errno); 108 ef.firstpage = malloc(PAGE_SIZE); 109 if (ef.firstpage == NULL) { 110 close(ef.fd); 111 return(ENOMEM); 112 } 113 bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE); 114 ef.firstlen = (size_t)bytes_read; 115 if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) { 116 err = EFTYPE; /* could be EIO, but may be small file */ 117 goto oerr; 118 } 119 ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage; 120 121 /* Is it ELF? */ 122 if (!IS_ELF(*ehdr)) { 123 err = EFTYPE; 124 goto oerr; 125 } 126 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ 127 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA || 128 ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */ 129 ehdr->e_version != EV_CURRENT || 130 ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */ 131 err = EFTYPE; 132 goto oerr; 133 } 134 135 136 /* 137 * Check to see what sort of module we are. 138 */ 139 kfp = file_findfile(NULL, NULL); 140 if (ehdr->e_type == ET_DYN) { 141 /* Looks like a kld module */ 142 if (kfp == NULL) { 143 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n"); 144 err = EPERM; 145 goto oerr; 146 } 147 if (strcmp(__elfN(kerneltype), kfp->f_type)) { 148 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type); 149 err = EPERM; 150 goto oerr; 151 } 152 /* Looks OK, got ahead */ 153 ef.kernel = 0; 154 155 /* Page-align the load address */ 156 pad = (u_int)dest & PAGE_MASK; 157 if (pad != 0) { 158 pad = PAGE_SIZE - pad; 159 dest += pad; 160 } 161 } else if (ehdr->e_type == ET_EXEC) { 162 /* Looks like a kernel */ 163 if (kfp != NULL) { 164 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n"); 165 err = EPERM; 166 goto oerr; 167 } 168 /* 169 * Calculate destination address based on kernel entrypoint 170 */ 171 dest = ehdr->e_entry; 172 if (dest == 0) { 173 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n"); 174 err = EPERM; 175 goto oerr; 176 } 177 ef.kernel = 1; 178 179 } else { 180 err = EFTYPE; 181 goto oerr; 182 } 183 184 /* 185 * Ok, we think we should handle this. 186 */ 187 fp = file_alloc(); 188 if (fp == NULL) { 189 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n"); 190 err = EPERM; 191 goto out; 192 } 193 if (ef.kernel) 194 setenv("kernelname", filename, 1); 195 fp->f_name = strdup(filename); 196 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype)); 197 198#ifdef ELF_VERBOSE 199 if (ef.kernel) 200 printf("%s entry at 0x%jx\n", filename, (uintmax_t)dest); 201#else 202 printf("%s ", filename); 203#endif 204 205 fp->f_size = __elfN(loadimage)(fp, &ef, dest); 206 if (fp->f_size == 0 || fp->f_addr == 0) 207 goto ioerr; 208 209 /* save exec header as metadata */ 210 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr); 211 212 /* Load OK, return module pointer */ 213 *result = (struct preloaded_file *)fp; 214 err = 0; 215 goto out; 216 217 ioerr: 218 err = EIO; 219 oerr: 220 file_discard(fp); 221 out: 222 if (ef.firstpage) 223 free(ef.firstpage); 224 close(ef.fd); 225 return(err); 226} 227 228/* 229 * With the file (fd) open on the image, and (ehdr) containing 230 * the Elf header, load the image at (off) 231 */ 232static int 233__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off) 234{ 235 int i; 236 u_int j; 237 Elf_Ehdr *ehdr; 238 Elf_Phdr *phdr, *php; 239 Elf_Shdr *shdr; 240 int ret; 241 vm_offset_t firstaddr; 242 vm_offset_t lastaddr; 243 void *buf; 244 size_t resid, chunk; 245 ssize_t result; 246 vm_offset_t dest; 247 Elf_Addr ssym, esym; 248 Elf_Dyn *dp; 249 Elf_Addr adp; 250 int ndp; 251 int symstrindex; 252 int symtabindex; 253 Elf_Size size; 254 u_int fpcopy; 255 256 dp = NULL; 257 shdr = NULL; 258 ret = 0; 259 firstaddr = lastaddr = 0; 260 ehdr = ef->ehdr; 261 if (ef->kernel) { 262#ifdef __i386__ 263#if __ELF_WORD_SIZE == 64 264 off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */ 265#else 266 off = - (off & 0xff000000u); /* i386 relocates after locore */ 267#endif 268#else 269 off = 0; /* alpha is direct mapped for kernels */ 270#endif 271 } 272 ef->off = off; 273 274 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) { 275 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n"); 276 goto out; 277 } 278 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff); 279 280 for (i = 0; i < ehdr->e_phnum; i++) { 281 /* We want to load PT_LOAD segments only.. */ 282 if (phdr[i].p_type != PT_LOAD) 283 continue; 284 285#ifdef ELF_VERBOSE 286 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx", 287 (long)phdr[i].p_filesz, (long)phdr[i].p_offset, 288 (long)(phdr[i].p_vaddr + off), 289 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 290#else 291 if ((phdr[i].p_flags & PF_W) == 0) { 292 printf("text=0x%lx ", (long)phdr[i].p_filesz); 293 } else { 294 printf("data=0x%lx", (long)phdr[i].p_filesz); 295 if (phdr[i].p_filesz < phdr[i].p_memsz) 296 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz)); 297 printf(" "); 298 } 299#endif 300 fpcopy = 0; 301 if (ef->firstlen > phdr[i].p_offset) { 302 fpcopy = ef->firstlen - phdr[i].p_offset; 303 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset, 304 phdr[i].p_vaddr + off, fpcopy); 305 } 306 if (phdr[i].p_filesz > fpcopy) { 307 if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy), 308 SEEK_SET) == -1) { 309 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadexec: cannot seek\n"); 310 goto out; 311 } 312 if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy, 313 phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) { 314 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadexec: archsw.readin failed\n"); 315 goto out; 316 } 317 } 318 /* clear space from oversized segments; eg: bss */ 319 if (phdr[i].p_filesz < phdr[i].p_memsz) { 320#ifdef ELF_VERBOSE 321 printf(" (bss: 0x%lx-0x%lx)", 322 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz), 323 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 324#endif 325 326 /* no archsw.arch_bzero */ 327 buf = malloc(PAGE_SIZE); 328 if (buf == NULL) { 329 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: malloc() failed\n"); 330 goto out; 331 } 332 bzero(buf, PAGE_SIZE); 333 resid = phdr[i].p_memsz - phdr[i].p_filesz; 334 dest = phdr[i].p_vaddr + off + phdr[i].p_filesz; 335 while (resid > 0) { 336 chunk = min(PAGE_SIZE, resid); 337 archsw.arch_copyin(buf, dest, chunk); 338 resid -= chunk; 339 dest += chunk; 340 } 341 free(buf); 342 } 343#ifdef ELF_VERBOSE 344 printf("\n"); 345#endif 346 347 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off)) 348 firstaddr = phdr[i].p_vaddr + off; 349 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz)) 350 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz; 351 } 352 lastaddr = roundup(lastaddr, sizeof(long)); 353 354 /* 355 * Now grab the symbol tables. This isn't easy if we're reading a 356 * .gz file. I think the rule is going to have to be that you must 357 * strip a file to remove symbols before gzipping it so that we do not 358 * try to lseek() on it. 359 */ 360 chunk = ehdr->e_shnum * ehdr->e_shentsize; 361 if (chunk == 0 || ehdr->e_shoff == 0) 362 goto nosyms; 363 shdr = malloc(chunk); 364 if (shdr == NULL) 365 goto nosyms; 366 if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) { 367 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: cannot lseek() to section headers"); 368 goto nosyms; 369 } 370 result = read(ef->fd, shdr, chunk); 371 if (result < 0 || (size_t)result != chunk) { 372 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: read section headers failed"); 373 goto nosyms; 374 } 375 symtabindex = -1; 376 symstrindex = -1; 377 for (i = 0; i < ehdr->e_shnum; i++) { 378 if (shdr[i].sh_type != SHT_SYMTAB) 379 continue; 380 for (j = 0; j < ehdr->e_phnum; j++) { 381 if (phdr[j].p_type != PT_LOAD) 382 continue; 383 if (shdr[i].sh_offset >= phdr[j].p_offset && 384 (shdr[i].sh_offset + shdr[i].sh_size <= 385 phdr[j].p_offset + phdr[j].p_filesz)) { 386 shdr[i].sh_offset = 0; 387 shdr[i].sh_size = 0; 388 break; 389 } 390 } 391 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0) 392 continue; /* alread loaded in a PT_LOAD above */ 393 /* Save it for loading below */ 394 symtabindex = i; 395 symstrindex = shdr[i].sh_link; 396 } 397 if (symtabindex < 0 || symstrindex < 0) 398 goto nosyms; 399 400 /* Ok, committed to a load. */ 401#ifndef ELF_VERBOSE 402 printf("syms=["); 403#endif 404 ssym = lastaddr; 405 for (i = symtabindex; i >= 0; i = symstrindex) { 406#ifdef ELF_VERBOSE 407 char *secname; 408 409 switch(shdr[i].sh_type) { 410 case SHT_SYMTAB: /* Symbol table */ 411 secname = "symtab"; 412 break; 413 case SHT_STRTAB: /* String table */ 414 secname = "strtab"; 415 break; 416 default: 417 secname = "WHOA!!"; 418 break; 419 } 420#endif 421 422 size = shdr[i].sh_size; 423 archsw.arch_copyin(&size, lastaddr, sizeof(size)); 424 lastaddr += sizeof(size); 425 426#ifdef ELF_VERBOSE 427 printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname, 428 shdr[i].sh_size, shdr[i].sh_offset, 429 lastaddr, lastaddr + shdr[i].sh_size); 430#else 431 if (i == symstrindex) 432 printf("+"); 433 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size); 434#endif 435 436 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) { 437 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!"); 438 lastaddr = ssym; 439 ssym = 0; 440 goto nosyms; 441 } 442 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size); 443 if (result < 0 || (size_t)result != shdr[i].sh_size) { 444 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped!"); 445 lastaddr = ssym; 446 ssym = 0; 447 goto nosyms; 448 } 449 /* Reset offsets relative to ssym */ 450 lastaddr += shdr[i].sh_size; 451 lastaddr = roundup(lastaddr, sizeof(size)); 452 if (i == symtabindex) 453 symtabindex = -1; 454 else if (i == symstrindex) 455 symstrindex = -1; 456 } 457 esym = lastaddr; 458#ifndef ELF_VERBOSE 459 printf("]"); 460#endif 461 462 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym); 463 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym); 464 465nosyms: 466 printf("\n"); 467 468 ret = lastaddr - firstaddr; 469 fp->f_addr = firstaddr; 470 471 php = NULL; 472 for (i = 0; i < ehdr->e_phnum; i++) { 473 if (phdr[i].p_type == PT_DYNAMIC) { 474 php = phdr + i; 475 adp = php->p_vaddr; 476 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp); 477 break; 478 } 479 } 480 481 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */ 482 goto out; 483 484 ndp = php->p_filesz / sizeof(Elf_Dyn); 485 if (ndp == 0) 486 goto out; 487 dp = malloc(php->p_filesz); 488 if (dp == NULL) 489 goto out; 490 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz); 491 492 ef->strsz = 0; 493 for (i = 0; i < ndp; i++) { 494 if (dp[i].d_tag == NULL) 495 break; 496 switch (dp[i].d_tag) { 497 case DT_HASH: 498 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off); 499 break; 500 case DT_STRTAB: 501 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off); 502 break; 503 case DT_STRSZ: 504 ef->strsz = dp[i].d_un.d_val; 505 break; 506 case DT_SYMTAB: 507 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off); 508 break; 509 case DT_RELA: 510 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off); 511 break; 512 case DT_RELASZ: 513 ef->relasz = dp[i].d_un.d_val; 514 break; 515 default: 516 break; 517 } 518 } 519 if (ef->hashtab == NULL || ef->symtab == NULL || 520 ef->strtab == NULL || ef->strsz == 0) 521 goto out; 522 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets)); 523 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains)); 524 ef->buckets = ef->hashtab + 2; 525 ef->chains = ef->buckets + ef->nbuckets; 526 if (__elfN(parse_modmetadata)(fp, ef) == 0) 527 goto out; 528 529 if (ef->kernel) /* kernel must not depend on anything */ 530 goto out; 531 532out: 533 if (dp) 534 free(dp); 535 if (shdr) 536 free(shdr); 537 return ret; 538} 539 540static char invalid_name[] = "bad"; 541 542char * 543fake_modname(const char *name) 544{ 545 const char *sp, *ep; 546 char *fp; 547 size_t len; 548 549 sp = strrchr(name, '/'); 550 if (sp) 551 sp++; 552 else 553 sp = name; 554 ep = strrchr(name, '.'); 555 if (ep) { 556 if (ep == name) { 557 sp = invalid_name; 558 ep = invalid_name + sizeof(invalid_name) - 1; 559 } 560 } else 561 ep = name + strlen(name); 562 len = ep - sp; 563 fp = malloc(len + 1); 564 if (fp == NULL) 565 return NULL; 566 memcpy(fp, sp, len); 567 fp[len] = '\0'; 568 return fp; 569} 570 571#if defined(__i386__) && __ELF_WORD_SIZE == 64 572struct mod_metadata64 { 573 int md_version; /* structure version MDTV_* */ 574 int md_type; /* type of entry MDT_* */ 575 u_int64_t md_data; /* specific data */ 576 u_int64_t md_cval; /* common string label */ 577}; 578#endif 579 580int 581__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef) 582{ 583 struct mod_metadata md; 584#if defined(__i386__) && __ELF_WORD_SIZE == 64 585 struct mod_metadata64 md64; 586#endif 587 struct mod_depend *mdepend; 588 struct mod_version mver; 589 Elf_Sym sym; 590 char *s; 591 int modcnt, minfolen; 592 Elf_Addr v, p, p_stop; 593 594 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0) 595 return ENOENT; 596 p = sym.st_value + ef->off; 597 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0) 598 return ENOENT; 599 p_stop = sym.st_value + ef->off; 600 601 modcnt = 0; 602 while (p < p_stop) { 603 COPYOUT(p, &v, sizeof(v)); 604#ifdef __sparc64__ 605 __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v)); 606#else 607 v += ef->off; 608#endif 609#if defined(__i386__) && __ELF_WORD_SIZE == 64 610 COPYOUT(v, &md64, sizeof(md64)); 611 md.md_version = md64.md_version; 612 md.md_type = md64.md_type; 613 md.md_cval = (const char *)(uintptr_t)(md64.md_cval + ef->off); 614 md.md_data = (void *)(uintptr_t)(md64.md_data + ef->off); 615#else 616 COPYOUT(v, &md, sizeof(md)); 617#ifdef __sparc64__ 618 __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md)); 619#else 620 md.md_cval += ef->off; 621 md.md_data += ef->off; 622#endif 623#endif 624 p += sizeof(Elf_Addr); 625 switch(md.md_type) { 626 case MDT_DEPEND: 627 if (ef->kernel) /* kernel must not depend on anything */ 628 break; 629 s = strdupout((vm_offset_t)md.md_cval); 630 minfolen = sizeof(*mdepend) + strlen(s) + 1; 631 mdepend = malloc(minfolen); 632 if (mdepend == NULL) 633 return ENOMEM; 634 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend)); 635 strcpy((char*)(mdepend + 1), s); 636 free(s); 637 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend); 638 free(mdepend); 639 break; 640 case MDT_VERSION: 641 s = strdupout((vm_offset_t)md.md_cval); 642 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver)); 643 file_addmodule(fp, s, mver.mv_version, NULL); 644 free(s); 645 modcnt++; 646 break; 647 } 648 } 649 if (modcnt == 0) { 650 s = fake_modname(fp->f_name); 651 file_addmodule(fp, s, 1, NULL); 652 free(s); 653 } 654 return 0; 655} 656 657static unsigned long 658elf_hash(const char *name) 659{ 660 const unsigned char *p = (const unsigned char *) name; 661 unsigned long h = 0; 662 unsigned long g; 663 664 while (*p != '\0') { 665 h = (h << 4) + *p++; 666 if ((g = h & 0xf0000000) != 0) 667 h ^= g >> 24; 668 h &= ~g; 669 } 670 return h; 671} 672 673static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n"; 674int 675__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name, 676 Elf_Sym *symp) 677{ 678 Elf_Hashelt symnum; 679 Elf_Sym sym; 680 char *strp; 681 unsigned long hash; 682 683 hash = elf_hash(name); 684 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum)); 685 686 while (symnum != STN_UNDEF) { 687 if (symnum >= ef->nchains) { 688 printf(__elfN(bad_symtable)); 689 return ENOENT; 690 } 691 692 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym)); 693 if (sym.st_name == 0) { 694 printf(__elfN(bad_symtable)); 695 return ENOENT; 696 } 697 698 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name)); 699 if (strcmp(name, strp) == 0) { 700 free(strp); 701 if (sym.st_shndx != SHN_UNDEF || 702 (sym.st_value != 0 && 703 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) { 704 *symp = sym; 705 return 0; 706 } 707 return ENOENT; 708 } 709 free(strp); 710 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum)); 711 } 712 return ENOENT; 713} 714 715#ifdef __sparc__ 716/* 717 * Apply any intra-module relocations to the value. *p is the load address 718 * of the value and val/len is the value to be modified. This does NOT modify 719 * the image in-place, because this is done by kern_linker later on. 720 */ 721static void 722__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 723 void *p, void *val, size_t len) 724{ 725 Elf_Addr off = (Elf_Addr)p - ef->off, word; 726 size_t n; 727 Elf_Rela r; 728 729 for (n = 0; n < ef->relasz / sizeof(r); n++) { 730 COPYOUT(ef->rela + n, &r, sizeof(r)); 731 732 if (r.r_offset >= off && r.r_offset < off + len && 733 ELF_R_TYPE(r.r_info) == R_SPARC_RELATIVE) { 734 word = ef->off + r.r_addend; 735 bcopy(&word, (char *)val + (r.r_offset - off), 736 sizeof(word)); 737 } 738 } 739} 740#endif
| 31#include <sys/param.h> 32#include <sys/exec.h> 33#include <sys/linker.h> 34#include <sys/module.h> 35#include <string.h> 36#include <machine/elf.h> 37#include <stand.h> 38#define FREEBSD_ELF 39#include <link.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 52typedef 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_Rela *rela; 62 size_t relasz; 63 char *strtab; 64 size_t strsz; 65 int fd; 66 caddr_t firstpage; 67 size_t firstlen; 68 int kernel; 69 u_int64_t off; 70} *elf_file_t; 71 72static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr); 73static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym); 74#ifdef __sparc__ 75static void __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 76 void *p, void *val, size_t len); 77#endif 78static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef); 79static char *fake_modname(const char *name); 80 81const char *__elfN(kerneltype) = "elf kernel"; 82const char *__elfN(moduletype) = "elf module"; 83 84/* 85 * Attempt to load the file (file) as an ELF module. It will be stored at 86 * (dest), and a pointer to a module structure describing the loaded object 87 * will be saved in (result). 88 */ 89int 90__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result) 91{ 92 struct preloaded_file *fp, *kfp; 93 struct elf_file ef; 94 Elf_Ehdr *ehdr; 95 int err; 96 u_int pad; 97 ssize_t bytes_read; 98 99 fp = NULL; 100 bzero(&ef, sizeof(struct elf_file)); 101 102 /* 103 * Open the image, read and validate the ELF header 104 */ 105 if (filename == NULL) /* can't handle nameless */ 106 return(EFTYPE); 107 if ((ef.fd = open(filename, O_RDONLY)) == -1) 108 return(errno); 109 ef.firstpage = malloc(PAGE_SIZE); 110 if (ef.firstpage == NULL) { 111 close(ef.fd); 112 return(ENOMEM); 113 } 114 bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE); 115 ef.firstlen = (size_t)bytes_read; 116 if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) { 117 err = EFTYPE; /* could be EIO, but may be small file */ 118 goto oerr; 119 } 120 ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage; 121 122 /* Is it ELF? */ 123 if (!IS_ELF(*ehdr)) { 124 err = EFTYPE; 125 goto oerr; 126 } 127 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ 128 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA || 129 ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */ 130 ehdr->e_version != EV_CURRENT || 131 ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */ 132 err = EFTYPE; 133 goto oerr; 134 } 135 136 137 /* 138 * Check to see what sort of module we are. 139 */ 140 kfp = file_findfile(NULL, NULL); 141 if (ehdr->e_type == ET_DYN) { 142 /* Looks like a kld module */ 143 if (kfp == NULL) { 144 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n"); 145 err = EPERM; 146 goto oerr; 147 } 148 if (strcmp(__elfN(kerneltype), kfp->f_type)) { 149 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type); 150 err = EPERM; 151 goto oerr; 152 } 153 /* Looks OK, got ahead */ 154 ef.kernel = 0; 155 156 /* Page-align the load address */ 157 pad = (u_int)dest & PAGE_MASK; 158 if (pad != 0) { 159 pad = PAGE_SIZE - pad; 160 dest += pad; 161 } 162 } else if (ehdr->e_type == ET_EXEC) { 163 /* Looks like a kernel */ 164 if (kfp != NULL) { 165 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n"); 166 err = EPERM; 167 goto oerr; 168 } 169 /* 170 * Calculate destination address based on kernel entrypoint 171 */ 172 dest = ehdr->e_entry; 173 if (dest == 0) { 174 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n"); 175 err = EPERM; 176 goto oerr; 177 } 178 ef.kernel = 1; 179 180 } else { 181 err = EFTYPE; 182 goto oerr; 183 } 184 185 /* 186 * Ok, we think we should handle this. 187 */ 188 fp = file_alloc(); 189 if (fp == NULL) { 190 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n"); 191 err = EPERM; 192 goto out; 193 } 194 if (ef.kernel) 195 setenv("kernelname", filename, 1); 196 fp->f_name = strdup(filename); 197 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype)); 198 199#ifdef ELF_VERBOSE 200 if (ef.kernel) 201 printf("%s entry at 0x%jx\n", filename, (uintmax_t)dest); 202#else 203 printf("%s ", filename); 204#endif 205 206 fp->f_size = __elfN(loadimage)(fp, &ef, dest); 207 if (fp->f_size == 0 || fp->f_addr == 0) 208 goto ioerr; 209 210 /* save exec header as metadata */ 211 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr); 212 213 /* Load OK, return module pointer */ 214 *result = (struct preloaded_file *)fp; 215 err = 0; 216 goto out; 217 218 ioerr: 219 err = EIO; 220 oerr: 221 file_discard(fp); 222 out: 223 if (ef.firstpage) 224 free(ef.firstpage); 225 close(ef.fd); 226 return(err); 227} 228 229/* 230 * With the file (fd) open on the image, and (ehdr) containing 231 * the Elf header, load the image at (off) 232 */ 233static int 234__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off) 235{ 236 int i; 237 u_int j; 238 Elf_Ehdr *ehdr; 239 Elf_Phdr *phdr, *php; 240 Elf_Shdr *shdr; 241 int ret; 242 vm_offset_t firstaddr; 243 vm_offset_t lastaddr; 244 void *buf; 245 size_t resid, chunk; 246 ssize_t result; 247 vm_offset_t dest; 248 Elf_Addr ssym, esym; 249 Elf_Dyn *dp; 250 Elf_Addr adp; 251 int ndp; 252 int symstrindex; 253 int symtabindex; 254 Elf_Size size; 255 u_int fpcopy; 256 257 dp = NULL; 258 shdr = NULL; 259 ret = 0; 260 firstaddr = lastaddr = 0; 261 ehdr = ef->ehdr; 262 if (ef->kernel) { 263#ifdef __i386__ 264#if __ELF_WORD_SIZE == 64 265 off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */ 266#else 267 off = - (off & 0xff000000u); /* i386 relocates after locore */ 268#endif 269#else 270 off = 0; /* alpha is direct mapped for kernels */ 271#endif 272 } 273 ef->off = off; 274 275 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) { 276 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n"); 277 goto out; 278 } 279 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff); 280 281 for (i = 0; i < ehdr->e_phnum; i++) { 282 /* We want to load PT_LOAD segments only.. */ 283 if (phdr[i].p_type != PT_LOAD) 284 continue; 285 286#ifdef ELF_VERBOSE 287 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx", 288 (long)phdr[i].p_filesz, (long)phdr[i].p_offset, 289 (long)(phdr[i].p_vaddr + off), 290 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 291#else 292 if ((phdr[i].p_flags & PF_W) == 0) { 293 printf("text=0x%lx ", (long)phdr[i].p_filesz); 294 } else { 295 printf("data=0x%lx", (long)phdr[i].p_filesz); 296 if (phdr[i].p_filesz < phdr[i].p_memsz) 297 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz)); 298 printf(" "); 299 } 300#endif 301 fpcopy = 0; 302 if (ef->firstlen > phdr[i].p_offset) { 303 fpcopy = ef->firstlen - phdr[i].p_offset; 304 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset, 305 phdr[i].p_vaddr + off, fpcopy); 306 } 307 if (phdr[i].p_filesz > fpcopy) { 308 if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy), 309 SEEK_SET) == -1) { 310 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadexec: cannot seek\n"); 311 goto out; 312 } 313 if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy, 314 phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) { 315 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadexec: archsw.readin failed\n"); 316 goto out; 317 } 318 } 319 /* clear space from oversized segments; eg: bss */ 320 if (phdr[i].p_filesz < phdr[i].p_memsz) { 321#ifdef ELF_VERBOSE 322 printf(" (bss: 0x%lx-0x%lx)", 323 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz), 324 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 325#endif 326 327 /* no archsw.arch_bzero */ 328 buf = malloc(PAGE_SIZE); 329 if (buf == NULL) { 330 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: malloc() failed\n"); 331 goto out; 332 } 333 bzero(buf, PAGE_SIZE); 334 resid = phdr[i].p_memsz - phdr[i].p_filesz; 335 dest = phdr[i].p_vaddr + off + phdr[i].p_filesz; 336 while (resid > 0) { 337 chunk = min(PAGE_SIZE, resid); 338 archsw.arch_copyin(buf, dest, chunk); 339 resid -= chunk; 340 dest += chunk; 341 } 342 free(buf); 343 } 344#ifdef ELF_VERBOSE 345 printf("\n"); 346#endif 347 348 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off)) 349 firstaddr = phdr[i].p_vaddr + off; 350 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz)) 351 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz; 352 } 353 lastaddr = roundup(lastaddr, sizeof(long)); 354 355 /* 356 * Now grab the symbol tables. This isn't easy if we're reading a 357 * .gz file. I think the rule is going to have to be that you must 358 * strip a file to remove symbols before gzipping it so that we do not 359 * try to lseek() on it. 360 */ 361 chunk = ehdr->e_shnum * ehdr->e_shentsize; 362 if (chunk == 0 || ehdr->e_shoff == 0) 363 goto nosyms; 364 shdr = malloc(chunk); 365 if (shdr == NULL) 366 goto nosyms; 367 if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) { 368 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: cannot lseek() to section headers"); 369 goto nosyms; 370 } 371 result = read(ef->fd, shdr, chunk); 372 if (result < 0 || (size_t)result != chunk) { 373 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: read section headers failed"); 374 goto nosyms; 375 } 376 symtabindex = -1; 377 symstrindex = -1; 378 for (i = 0; i < ehdr->e_shnum; i++) { 379 if (shdr[i].sh_type != SHT_SYMTAB) 380 continue; 381 for (j = 0; j < ehdr->e_phnum; j++) { 382 if (phdr[j].p_type != PT_LOAD) 383 continue; 384 if (shdr[i].sh_offset >= phdr[j].p_offset && 385 (shdr[i].sh_offset + shdr[i].sh_size <= 386 phdr[j].p_offset + phdr[j].p_filesz)) { 387 shdr[i].sh_offset = 0; 388 shdr[i].sh_size = 0; 389 break; 390 } 391 } 392 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0) 393 continue; /* alread loaded in a PT_LOAD above */ 394 /* Save it for loading below */ 395 symtabindex = i; 396 symstrindex = shdr[i].sh_link; 397 } 398 if (symtabindex < 0 || symstrindex < 0) 399 goto nosyms; 400 401 /* Ok, committed to a load. */ 402#ifndef ELF_VERBOSE 403 printf("syms=["); 404#endif 405 ssym = lastaddr; 406 for (i = symtabindex; i >= 0; i = symstrindex) { 407#ifdef ELF_VERBOSE 408 char *secname; 409 410 switch(shdr[i].sh_type) { 411 case SHT_SYMTAB: /* Symbol table */ 412 secname = "symtab"; 413 break; 414 case SHT_STRTAB: /* String table */ 415 secname = "strtab"; 416 break; 417 default: 418 secname = "WHOA!!"; 419 break; 420 } 421#endif 422 423 size = shdr[i].sh_size; 424 archsw.arch_copyin(&size, lastaddr, sizeof(size)); 425 lastaddr += sizeof(size); 426 427#ifdef ELF_VERBOSE 428 printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname, 429 shdr[i].sh_size, shdr[i].sh_offset, 430 lastaddr, lastaddr + shdr[i].sh_size); 431#else 432 if (i == symstrindex) 433 printf("+"); 434 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size); 435#endif 436 437 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) { 438 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!"); 439 lastaddr = ssym; 440 ssym = 0; 441 goto nosyms; 442 } 443 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size); 444 if (result < 0 || (size_t)result != shdr[i].sh_size) { 445 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped!"); 446 lastaddr = ssym; 447 ssym = 0; 448 goto nosyms; 449 } 450 /* Reset offsets relative to ssym */ 451 lastaddr += shdr[i].sh_size; 452 lastaddr = roundup(lastaddr, sizeof(size)); 453 if (i == symtabindex) 454 symtabindex = -1; 455 else if (i == symstrindex) 456 symstrindex = -1; 457 } 458 esym = lastaddr; 459#ifndef ELF_VERBOSE 460 printf("]"); 461#endif 462 463 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym); 464 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym); 465 466nosyms: 467 printf("\n"); 468 469 ret = lastaddr - firstaddr; 470 fp->f_addr = firstaddr; 471 472 php = NULL; 473 for (i = 0; i < ehdr->e_phnum; i++) { 474 if (phdr[i].p_type == PT_DYNAMIC) { 475 php = phdr + i; 476 adp = php->p_vaddr; 477 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp); 478 break; 479 } 480 } 481 482 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */ 483 goto out; 484 485 ndp = php->p_filesz / sizeof(Elf_Dyn); 486 if (ndp == 0) 487 goto out; 488 dp = malloc(php->p_filesz); 489 if (dp == NULL) 490 goto out; 491 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz); 492 493 ef->strsz = 0; 494 for (i = 0; i < ndp; i++) { 495 if (dp[i].d_tag == NULL) 496 break; 497 switch (dp[i].d_tag) { 498 case DT_HASH: 499 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off); 500 break; 501 case DT_STRTAB: 502 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off); 503 break; 504 case DT_STRSZ: 505 ef->strsz = dp[i].d_un.d_val; 506 break; 507 case DT_SYMTAB: 508 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off); 509 break; 510 case DT_RELA: 511 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off); 512 break; 513 case DT_RELASZ: 514 ef->relasz = dp[i].d_un.d_val; 515 break; 516 default: 517 break; 518 } 519 } 520 if (ef->hashtab == NULL || ef->symtab == NULL || 521 ef->strtab == NULL || ef->strsz == 0) 522 goto out; 523 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets)); 524 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains)); 525 ef->buckets = ef->hashtab + 2; 526 ef->chains = ef->buckets + ef->nbuckets; 527 if (__elfN(parse_modmetadata)(fp, ef) == 0) 528 goto out; 529 530 if (ef->kernel) /* kernel must not depend on anything */ 531 goto out; 532 533out: 534 if (dp) 535 free(dp); 536 if (shdr) 537 free(shdr); 538 return ret; 539} 540 541static char invalid_name[] = "bad"; 542 543char * 544fake_modname(const char *name) 545{ 546 const char *sp, *ep; 547 char *fp; 548 size_t len; 549 550 sp = strrchr(name, '/'); 551 if (sp) 552 sp++; 553 else 554 sp = name; 555 ep = strrchr(name, '.'); 556 if (ep) { 557 if (ep == name) { 558 sp = invalid_name; 559 ep = invalid_name + sizeof(invalid_name) - 1; 560 } 561 } else 562 ep = name + strlen(name); 563 len = ep - sp; 564 fp = malloc(len + 1); 565 if (fp == NULL) 566 return NULL; 567 memcpy(fp, sp, len); 568 fp[len] = '\0'; 569 return fp; 570} 571 572#if defined(__i386__) && __ELF_WORD_SIZE == 64 573struct mod_metadata64 { 574 int md_version; /* structure version MDTV_* */ 575 int md_type; /* type of entry MDT_* */ 576 u_int64_t md_data; /* specific data */ 577 u_int64_t md_cval; /* common string label */ 578}; 579#endif 580 581int 582__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef) 583{ 584 struct mod_metadata md; 585#if defined(__i386__) && __ELF_WORD_SIZE == 64 586 struct mod_metadata64 md64; 587#endif 588 struct mod_depend *mdepend; 589 struct mod_version mver; 590 Elf_Sym sym; 591 char *s; 592 int modcnt, minfolen; 593 Elf_Addr v, p, p_stop; 594 595 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0) 596 return ENOENT; 597 p = sym.st_value + ef->off; 598 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0) 599 return ENOENT; 600 p_stop = sym.st_value + ef->off; 601 602 modcnt = 0; 603 while (p < p_stop) { 604 COPYOUT(p, &v, sizeof(v)); 605#ifdef __sparc64__ 606 __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v)); 607#else 608 v += ef->off; 609#endif 610#if defined(__i386__) && __ELF_WORD_SIZE == 64 611 COPYOUT(v, &md64, sizeof(md64)); 612 md.md_version = md64.md_version; 613 md.md_type = md64.md_type; 614 md.md_cval = (const char *)(uintptr_t)(md64.md_cval + ef->off); 615 md.md_data = (void *)(uintptr_t)(md64.md_data + ef->off); 616#else 617 COPYOUT(v, &md, sizeof(md)); 618#ifdef __sparc64__ 619 __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md)); 620#else 621 md.md_cval += ef->off; 622 md.md_data += ef->off; 623#endif 624#endif 625 p += sizeof(Elf_Addr); 626 switch(md.md_type) { 627 case MDT_DEPEND: 628 if (ef->kernel) /* kernel must not depend on anything */ 629 break; 630 s = strdupout((vm_offset_t)md.md_cval); 631 minfolen = sizeof(*mdepend) + strlen(s) + 1; 632 mdepend = malloc(minfolen); 633 if (mdepend == NULL) 634 return ENOMEM; 635 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend)); 636 strcpy((char*)(mdepend + 1), s); 637 free(s); 638 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend); 639 free(mdepend); 640 break; 641 case MDT_VERSION: 642 s = strdupout((vm_offset_t)md.md_cval); 643 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver)); 644 file_addmodule(fp, s, mver.mv_version, NULL); 645 free(s); 646 modcnt++; 647 break; 648 } 649 } 650 if (modcnt == 0) { 651 s = fake_modname(fp->f_name); 652 file_addmodule(fp, s, 1, NULL); 653 free(s); 654 } 655 return 0; 656} 657 658static unsigned long 659elf_hash(const char *name) 660{ 661 const unsigned char *p = (const unsigned char *) name; 662 unsigned long h = 0; 663 unsigned long g; 664 665 while (*p != '\0') { 666 h = (h << 4) + *p++; 667 if ((g = h & 0xf0000000) != 0) 668 h ^= g >> 24; 669 h &= ~g; 670 } 671 return h; 672} 673 674static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n"; 675int 676__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name, 677 Elf_Sym *symp) 678{ 679 Elf_Hashelt symnum; 680 Elf_Sym sym; 681 char *strp; 682 unsigned long hash; 683 684 hash = elf_hash(name); 685 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum)); 686 687 while (symnum != STN_UNDEF) { 688 if (symnum >= ef->nchains) { 689 printf(__elfN(bad_symtable)); 690 return ENOENT; 691 } 692 693 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym)); 694 if (sym.st_name == 0) { 695 printf(__elfN(bad_symtable)); 696 return ENOENT; 697 } 698 699 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name)); 700 if (strcmp(name, strp) == 0) { 701 free(strp); 702 if (sym.st_shndx != SHN_UNDEF || 703 (sym.st_value != 0 && 704 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) { 705 *symp = sym; 706 return 0; 707 } 708 return ENOENT; 709 } 710 free(strp); 711 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum)); 712 } 713 return ENOENT; 714} 715 716#ifdef __sparc__ 717/* 718 * Apply any intra-module relocations to the value. *p is the load address 719 * of the value and val/len is the value to be modified. This does NOT modify 720 * the image in-place, because this is done by kern_linker later on. 721 */ 722static void 723__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 724 void *p, void *val, size_t len) 725{ 726 Elf_Addr off = (Elf_Addr)p - ef->off, word; 727 size_t n; 728 Elf_Rela r; 729 730 for (n = 0; n < ef->relasz / sizeof(r); n++) { 731 COPYOUT(ef->rela + n, &r, sizeof(r)); 732 733 if (r.r_offset >= off && r.r_offset < off + len && 734 ELF_R_TYPE(r.r_info) == R_SPARC_RELATIVE) { 735 word = ef->off + r.r_addend; 736 bcopy(&word, (char *)val + (r.r_offset - off), 737 sizeof(word)); 738 } 739 } 740} 741#endif
|