load_elf.c revision 134441
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: head/sys/boot/common/load_elf.c 134441 2004-08-28 14:57:34Z iedowse $"); 30 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 size_t chunk; 245 ssize_t result; 246 Elf_Addr ssym, esym; 247 Elf_Dyn *dp; 248 Elf_Addr adp; 249 int ndp; 250 int symstrindex; 251 int symtabindex; 252 Elf_Size size; 253 u_int fpcopy; 254 255 dp = NULL; 256 shdr = NULL; 257 ret = 0; 258 firstaddr = lastaddr = 0; 259 ehdr = ef->ehdr; 260 if (ef->kernel) { 261#ifdef __i386__ 262#if __ELF_WORD_SIZE == 64 263 off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */ 264#else 265 off = - (off & 0xff000000u); /* i386 relocates after locore */ 266#endif 267#else 268 off = 0; /* alpha is direct mapped for kernels */ 269#endif 270 } 271 ef->off = off; 272 273 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) { 274 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n"); 275 goto out; 276 } 277 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff); 278 279 for (i = 0; i < ehdr->e_phnum; i++) { 280 /* We want to load PT_LOAD segments only.. */ 281 if (phdr[i].p_type != PT_LOAD) 282 continue; 283 284#ifdef ELF_VERBOSE 285 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx", 286 (long)phdr[i].p_filesz, (long)phdr[i].p_offset, 287 (long)(phdr[i].p_vaddr + off), 288 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 289#else 290 if ((phdr[i].p_flags & PF_W) == 0) { 291 printf("text=0x%lx ", (long)phdr[i].p_filesz); 292 } else { 293 printf("data=0x%lx", (long)phdr[i].p_filesz); 294 if (phdr[i].p_filesz < phdr[i].p_memsz) 295 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz)); 296 printf(" "); 297 } 298#endif 299 fpcopy = 0; 300 if (ef->firstlen > phdr[i].p_offset) { 301 fpcopy = ef->firstlen - phdr[i].p_offset; 302 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset, 303 phdr[i].p_vaddr + off, fpcopy); 304 } 305 if (phdr[i].p_filesz > fpcopy) { 306 if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy, 307 phdr[i].p_filesz - fpcopy, phdr[i].p_offset + fpcopy) != 0) { 308 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 309 "_loadimage: read failed\n"); 310 goto out; 311 } 312 } 313 /* clear space from oversized segments; eg: bss */ 314 if (phdr[i].p_filesz < phdr[i].p_memsz) { 315#ifdef ELF_VERBOSE 316 printf(" (bss: 0x%lx-0x%lx)", 317 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz), 318 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1)); 319#endif 320 321 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz, 322 phdr[i].p_memsz - phdr[i].p_filesz); 323 } 324#ifdef ELF_VERBOSE 325 printf("\n"); 326#endif 327 328 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off)) 329 firstaddr = phdr[i].p_vaddr + off; 330 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz)) 331 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz; 332 } 333 lastaddr = roundup(lastaddr, sizeof(long)); 334 335 /* 336 * Now grab the symbol tables. This isn't easy if we're reading a 337 * .gz file. I think the rule is going to have to be that you must 338 * strip a file to remove symbols before gzipping it so that we do not 339 * try to lseek() on it. 340 */ 341 chunk = ehdr->e_shnum * ehdr->e_shentsize; 342 if (chunk == 0 || ehdr->e_shoff == 0) 343 goto nosyms; 344 shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk); 345 if (shdr == NULL) { 346 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 347 "_loadimage: failed to read section headers"); 348 goto nosyms; 349 } 350 symtabindex = -1; 351 symstrindex = -1; 352 for (i = 0; i < ehdr->e_shnum; i++) { 353 if (shdr[i].sh_type != SHT_SYMTAB) 354 continue; 355 for (j = 0; j < ehdr->e_phnum; j++) { 356 if (phdr[j].p_type != PT_LOAD) 357 continue; 358 if (shdr[i].sh_offset >= phdr[j].p_offset && 359 (shdr[i].sh_offset + shdr[i].sh_size <= 360 phdr[j].p_offset + phdr[j].p_filesz)) { 361 shdr[i].sh_offset = 0; 362 shdr[i].sh_size = 0; 363 break; 364 } 365 } 366 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0) 367 continue; /* alread loaded in a PT_LOAD above */ 368 /* Save it for loading below */ 369 symtabindex = i; 370 symstrindex = shdr[i].sh_link; 371 } 372 if (symtabindex < 0 || symstrindex < 0) 373 goto nosyms; 374 375 /* Ok, committed to a load. */ 376#ifndef ELF_VERBOSE 377 printf("syms=["); 378#endif 379 ssym = lastaddr; 380 for (i = symtabindex; i >= 0; i = symstrindex) { 381#ifdef ELF_VERBOSE 382 char *secname; 383 384 switch(shdr[i].sh_type) { 385 case SHT_SYMTAB: /* Symbol table */ 386 secname = "symtab"; 387 break; 388 case SHT_STRTAB: /* String table */ 389 secname = "strtab"; 390 break; 391 default: 392 secname = "WHOA!!"; 393 break; 394 } 395#endif 396 397 size = shdr[i].sh_size; 398 archsw.arch_copyin(&size, lastaddr, sizeof(size)); 399 lastaddr += sizeof(size); 400 401#ifdef ELF_VERBOSE 402 printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname, 403 shdr[i].sh_size, shdr[i].sh_offset, 404 lastaddr, lastaddr + shdr[i].sh_size); 405#else 406 if (i == symstrindex) 407 printf("+"); 408 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size); 409#endif 410 411 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) { 412 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!"); 413 lastaddr = ssym; 414 ssym = 0; 415 goto nosyms; 416 } 417 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size); 418 if (result < 0 || (size_t)result != shdr[i].sh_size) { 419 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped!"); 420 lastaddr = ssym; 421 ssym = 0; 422 goto nosyms; 423 } 424 /* Reset offsets relative to ssym */ 425 lastaddr += shdr[i].sh_size; 426 lastaddr = roundup(lastaddr, sizeof(size)); 427 if (i == symtabindex) 428 symtabindex = -1; 429 else if (i == symstrindex) 430 symstrindex = -1; 431 } 432 esym = lastaddr; 433#ifndef ELF_VERBOSE 434 printf("]"); 435#endif 436 437 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym); 438 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym); 439 440nosyms: 441 printf("\n"); 442 443 ret = lastaddr - firstaddr; 444 fp->f_addr = firstaddr; 445 446 php = NULL; 447 for (i = 0; i < ehdr->e_phnum; i++) { 448 if (phdr[i].p_type == PT_DYNAMIC) { 449 php = phdr + i; 450 adp = php->p_vaddr; 451 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp); 452 break; 453 } 454 } 455 456 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */ 457 goto out; 458 459 ndp = php->p_filesz / sizeof(Elf_Dyn); 460 if (ndp == 0) 461 goto out; 462 dp = malloc(php->p_filesz); 463 if (dp == NULL) 464 goto out; 465 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz); 466 467 ef->strsz = 0; 468 for (i = 0; i < ndp; i++) { 469 if (dp[i].d_tag == 0) 470 break; 471 switch (dp[i].d_tag) { 472 case DT_HASH: 473 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off); 474 break; 475 case DT_STRTAB: 476 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off); 477 break; 478 case DT_STRSZ: 479 ef->strsz = dp[i].d_un.d_val; 480 break; 481 case DT_SYMTAB: 482 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off); 483 break; 484 case DT_RELA: 485 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off); 486 break; 487 case DT_RELASZ: 488 ef->relasz = dp[i].d_un.d_val; 489 break; 490 default: 491 break; 492 } 493 } 494 if (ef->hashtab == NULL || ef->symtab == NULL || 495 ef->strtab == NULL || ef->strsz == 0) 496 goto out; 497 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets)); 498 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains)); 499 ef->buckets = ef->hashtab + 2; 500 ef->chains = ef->buckets + ef->nbuckets; 501 if (__elfN(parse_modmetadata)(fp, ef) == 0) 502 goto out; 503 504 if (ef->kernel) /* kernel must not depend on anything */ 505 goto out; 506 507out: 508 if (dp) 509 free(dp); 510 if (shdr) 511 free(shdr); 512 return ret; 513} 514 515static char invalid_name[] = "bad"; 516 517char * 518fake_modname(const char *name) 519{ 520 const char *sp, *ep; 521 char *fp; 522 size_t len; 523 524 sp = strrchr(name, '/'); 525 if (sp) 526 sp++; 527 else 528 sp = name; 529 ep = strrchr(name, '.'); 530 if (ep) { 531 if (ep == name) { 532 sp = invalid_name; 533 ep = invalid_name + sizeof(invalid_name) - 1; 534 } 535 } else 536 ep = name + strlen(name); 537 len = ep - sp; 538 fp = malloc(len + 1); 539 if (fp == NULL) 540 return NULL; 541 memcpy(fp, sp, len); 542 fp[len] = '\0'; 543 return fp; 544} 545 546#if defined(__i386__) && __ELF_WORD_SIZE == 64 547struct mod_metadata64 { 548 int md_version; /* structure version MDTV_* */ 549 int md_type; /* type of entry MDT_* */ 550 u_int64_t md_data; /* specific data */ 551 u_int64_t md_cval; /* common string label */ 552}; 553#endif 554 555int 556__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef) 557{ 558 struct mod_metadata md; 559#if defined(__i386__) && __ELF_WORD_SIZE == 64 560 struct mod_metadata64 md64; 561#endif 562 struct mod_depend *mdepend; 563 struct mod_version mver; 564 Elf_Sym sym; 565 char *s; 566 int modcnt, minfolen; 567 Elf_Addr v, p, p_stop; 568 569 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0) 570 return ENOENT; 571 p = sym.st_value + ef->off; 572 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0) 573 return ENOENT; 574 p_stop = sym.st_value + ef->off; 575 576 modcnt = 0; 577 while (p < p_stop) { 578 COPYOUT(p, &v, sizeof(v)); 579#ifdef __sparc64__ 580 __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v)); 581#else 582 v += ef->off; 583#endif 584#if defined(__i386__) && __ELF_WORD_SIZE == 64 585 COPYOUT(v, &md64, sizeof(md64)); 586 md.md_version = md64.md_version; 587 md.md_type = md64.md_type; 588 md.md_cval = (const char *)(uintptr_t)(md64.md_cval + ef->off); 589 md.md_data = (void *)(uintptr_t)(md64.md_data + ef->off); 590#else 591 COPYOUT(v, &md, sizeof(md)); 592#ifdef __sparc64__ 593 __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md)); 594#else 595 md.md_cval += ef->off; 596 md.md_data += ef->off; 597#endif 598#endif 599 p += sizeof(Elf_Addr); 600 switch(md.md_type) { 601 case MDT_DEPEND: 602 if (ef->kernel) /* kernel must not depend on anything */ 603 break; 604 s = strdupout((vm_offset_t)md.md_cval); 605 minfolen = sizeof(*mdepend) + strlen(s) + 1; 606 mdepend = malloc(minfolen); 607 if (mdepend == NULL) 608 return ENOMEM; 609 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend)); 610 strcpy((char*)(mdepend + 1), s); 611 free(s); 612 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend); 613 free(mdepend); 614 break; 615 case MDT_VERSION: 616 s = strdupout((vm_offset_t)md.md_cval); 617 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver)); 618 file_addmodule(fp, s, mver.mv_version, NULL); 619 free(s); 620 modcnt++; 621 break; 622 } 623 } 624 if (modcnt == 0) { 625 s = fake_modname(fp->f_name); 626 file_addmodule(fp, s, 1, NULL); 627 free(s); 628 } 629 return 0; 630} 631 632static unsigned long 633elf_hash(const char *name) 634{ 635 const unsigned char *p = (const unsigned char *) name; 636 unsigned long h = 0; 637 unsigned long g; 638 639 while (*p != '\0') { 640 h = (h << 4) + *p++; 641 if ((g = h & 0xf0000000) != 0) 642 h ^= g >> 24; 643 h &= ~g; 644 } 645 return h; 646} 647 648static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n"; 649int 650__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name, 651 Elf_Sym *symp) 652{ 653 Elf_Hashelt symnum; 654 Elf_Sym sym; 655 char *strp; 656 unsigned long hash; 657 658 hash = elf_hash(name); 659 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum)); 660 661 while (symnum != STN_UNDEF) { 662 if (symnum >= ef->nchains) { 663 printf(__elfN(bad_symtable)); 664 return ENOENT; 665 } 666 667 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym)); 668 if (sym.st_name == 0) { 669 printf(__elfN(bad_symtable)); 670 return ENOENT; 671 } 672 673 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name)); 674 if (strcmp(name, strp) == 0) { 675 free(strp); 676 if (sym.st_shndx != SHN_UNDEF || 677 (sym.st_value != 0 && 678 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) { 679 *symp = sym; 680 return 0; 681 } 682 return ENOENT; 683 } 684 free(strp); 685 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum)); 686 } 687 return ENOENT; 688} 689 690#ifdef __sparc__ 691/* 692 * Apply any intra-module relocations to the value. *p is the load address 693 * of the value and val/len is the value to be modified. This does NOT modify 694 * the image in-place, because this is done by kern_linker later on. 695 */ 696static void 697__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 698 void *p, void *val, size_t len) 699{ 700 Elf_Addr off = (Elf_Addr)p - ef->off, word; 701 size_t n; 702 Elf_Rela r; 703 704 for (n = 0; n < ef->relasz / sizeof(r); n++) { 705 COPYOUT(ef->rela + n, &r, sizeof(r)); 706 707 if (r.r_offset >= off && r.r_offset < off + len && 708 ELF_R_TYPE(r.r_info) == R_SPARC_RELATIVE) { 709 word = ef->off + r.r_addend; 710 bcopy(&word, (char *)val + (r.r_offset - off), 711 sizeof(word)); 712 } 713 } 714} 715#endif 716