1 /* $OpenBSD: exec_elf.c,v 1.66 2008/07/18 16:58:06 kurt Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Per Fogelstrom 5 * All rights reserved. 6 * 7 * Copyright (c) 1994 Christos Zoulas 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/proc.h> 38 #include <sys/malloc.h> 39 #include <sys/pool.h> 40 #include <sys/mount.h> 41 #include <sys/namei.h> 42 #include <sys/vnode.h> 43 #include <sys/exec.h> 44 #include <sys/exec_elf.h> 45 #include <sys/exec_olf.h> 46 #include <sys/file.h> 47 #include <sys/syscall.h> 48 #include <sys/signalvar.h> 49 #include <sys/stat.h> 50 51 #include <sys/mman.h> 52 #include <uvm/uvm_extern.h> 53 54 #include <machine/cpu.h> 55 #include <machine/reg.h> 56 #include <machine/exec.h> 57 58 #ifdef COMPAT_LINUX 59 #include <compat/linux/linux_exec.h> 60 #endif 61 62 #ifdef COMPAT_SVR4 63 #include <compat/svr4/svr4_exec.h> 64 #endif 65 66 #ifdef COMPAT_FREEBSD 67 #include <compat/freebsd/freebsd_exec.h> 68 #endif 69 70 struct ELFNAME(probe_entry) { 71 int (*func)(struct proc *, struct exec_package *, char *, 72 u_long *, u_int8_t *); 73 } ELFNAME(probes)[] = { 74 /* XXX - bogus, shouldn't be size independent.. */ 75 #ifdef COMPAT_FREEBSD 76 { freebsd_elf_probe }, 77 #endif 78 #ifdef COMPAT_LINUX 79 { linux_elf_probe }, 80 #endif 81 #ifdef COMPAT_SVR4 82 { svr4_elf_probe }, 83 #endif 84 { NULL } 85 }; 86 87 int ELFNAME(load_file)(struct proc *, char *, struct exec_package *, 88 struct elf_args *, Elf_Addr *); 89 int ELFNAME(check_header)(Elf_Ehdr *); 90 int ELFNAME(read_from)(struct proc *, struct vnode *, u_long, caddr_t, int); 91 void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *, 92 Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int); 93 94 extern char sigcode[], esigcode[]; 95 #ifdef SYSCALL_DEBUG 96 extern char *syscallnames[]; 97 #endif 98 99 /* round up and down to page boundaries. */ 100 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 101 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 102 103 /* 104 * We limit the number of program headers to 32, this should 105 * be a reasonable limit for ELF, the most we have seen so far is 12 106 */ 107 #define ELF_MAX_VALID_PHDR 32 108 109 /* 110 * This is the basic elf emul. elf_probe_funcs may change to other emuls. 111 */ 112 struct emul ELFNAMEEND(emul) = { 113 "native", 114 NULL, 115 sendsig, 116 SYS_syscall, 117 SYS_MAXSYSCALL, 118 sysent, 119 #ifdef SYSCALL_DEBUG 120 syscallnames, 121 #else 122 NULL, 123 #endif 124 sizeof (AuxInfo) * ELF_AUX_ENTRIES, 125 ELFNAME(copyargs), 126 setregs, 127 ELFNAME2(exec,fixup), 128 sigcode, 129 esigcode, 130 EMUL_ENABLED | EMUL_NATIVE, 131 }; 132 133 /* 134 * Copy arguments onto the stack in the normal way, but add some 135 * space for extra information in case of dynamic binding. 136 */ 137 void * 138 ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo, 139 void *stack, void *argp) 140 { 141 stack = copyargs(pack, arginfo, stack, argp); 142 if (!stack) 143 return (NULL); 144 145 /* 146 * Push space for extra arguments on the stack needed by 147 * dynamically linked binaries. 148 */ 149 if (pack->ep_interp != NULL) { 150 pack->ep_emul_argp = stack; 151 stack = (char *)stack + ELF_AUX_ENTRIES * sizeof (AuxInfo); 152 } 153 return (stack); 154 } 155 156 /* 157 * Check header for validity; return 0 for ok, ENOEXEC if error 158 */ 159 int 160 ELFNAME(check_header)(Elf_Ehdr *ehdr) 161 { 162 /* 163 * We need to check magic, class size, endianess, and version before 164 * we look at the rest of the Elf_Ehdr structure. These few elements 165 * are represented in a machine independant fashion. 166 */ 167 if (!IS_ELF(*ehdr) || 168 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 169 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA || 170 ehdr->e_ident[EI_VERSION] != ELF_TARG_VER) 171 return (ENOEXEC); 172 173 /* Now check the machine dependant header */ 174 if (ehdr->e_machine != ELF_TARG_MACH || 175 ehdr->e_version != ELF_TARG_VER) 176 return (ENOEXEC); 177 178 /* Don't allow an insane amount of sections. */ 179 if (ehdr->e_phnum > ELF_MAX_VALID_PHDR) 180 return (ENOEXEC); 181 182 return (0); 183 } 184 185 /* 186 * Load a psection at the appropriate address 187 */ 188 void 189 ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, 190 Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags) 191 { 192 u_long uaddr, msize, lsize, psize, rm, rf; 193 long diff, offset, bdiff; 194 Elf_Addr base; 195 196 /* 197 * If the user specified an address, then we load there. 198 */ 199 if (*addr != ELFDEFNNAME(NO_ADDR)) { 200 if (ph->p_align > 1) { 201 *addr = ELF_TRUNC(*addr, ph->p_align); 202 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 203 /* page align vaddr */ 204 base = *addr + trunc_page(ph->p_vaddr) 205 - ELF_TRUNC(ph->p_vaddr, ph->p_align); 206 207 bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr); 208 209 } else 210 diff = 0; 211 } else { 212 *addr = uaddr = ph->p_vaddr; 213 if (ph->p_align > 1) 214 *addr = ELF_TRUNC(uaddr, ph->p_align); 215 base = trunc_page(uaddr); 216 bdiff = uaddr - base; 217 diff = uaddr - *addr; 218 } 219 220 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 221 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 222 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 223 224 msize = ph->p_memsz + diff; 225 offset = ph->p_offset - bdiff; 226 lsize = ph->p_filesz + bdiff; 227 psize = round_page(lsize); 228 229 /* 230 * Because the pagedvn pager can't handle zero fill of the last 231 * data page if it's not page aligned we map the last page readvn. 232 */ 233 if (ph->p_flags & PF_W) { 234 psize = trunc_page(lsize); 235 if (psize > 0) 236 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, 237 offset, *prot, flags); 238 if (psize != lsize) { 239 NEW_VMCMD2(vcset, vmcmd_map_readvn, lsize - psize, 240 base + psize, vp, offset + psize, *prot, flags); 241 } 242 } else { 243 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, offset, 244 *prot, flags); 245 } 246 247 /* 248 * Check if we need to extend the size of the segment 249 */ 250 rm = round_page(*addr + ph->p_memsz + diff); 251 rf = round_page(*addr + ph->p_filesz + diff); 252 253 if (rm != rf) { 254 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, 255 *prot, flags); 256 } 257 *size = msize; 258 } 259 260 /* 261 * Read from vnode into buffer at offset. 262 */ 263 int 264 ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, caddr_t buf, 265 int size) 266 { 267 int error; 268 size_t resid; 269 270 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE, 271 0, p->p_ucred, &resid, p)) != 0) 272 return error; 273 /* 274 * See if we got all of it 275 */ 276 if (resid != 0) 277 return (ENOEXEC); 278 return (0); 279 } 280 281 /* 282 * Load a file (interpreter/library) pointed to by path [stolen from 283 * coff_load_shlib()]. Made slightly generic so it might be used externally. 284 */ 285 int 286 ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp, 287 struct elf_args *ap, Elf_Addr *last) 288 { 289 int error, i; 290 struct nameidata nd; 291 Elf_Ehdr eh; 292 Elf_Phdr *ph = NULL; 293 u_long phsize; 294 Elf_Addr addr; 295 struct vnode *vp; 296 Elf_Phdr *base_ph = NULL; 297 struct interp_ld_sec { 298 Elf_Addr vaddr; 299 u_long memsz; 300 } loadmap[ELF_MAX_VALID_PHDR]; 301 int nload, idx = 0; 302 Elf_Addr pos = *last; 303 int file_align; 304 305 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 306 if ((error = namei(&nd)) != 0) { 307 return (error); 308 } 309 vp = nd.ni_vp; 310 if (vp->v_type != VREG) { 311 error = EACCES; 312 goto bad; 313 } 314 if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0) 315 goto bad; 316 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 317 error = EACCES; 318 goto bad; 319 } 320 if ((error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) != 0) 321 goto bad1; 322 if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, 323 (caddr_t)&eh, sizeof(eh))) != 0) 324 goto bad1; 325 326 if (ELFNAME(check_header)(&eh) || eh.e_type != ET_DYN) { 327 error = ENOEXEC; 328 goto bad1; 329 } 330 331 phsize = eh.e_phnum * sizeof(Elf_Phdr); 332 ph = malloc(phsize, M_TEMP, M_WAITOK); 333 334 if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, (caddr_t)ph, 335 phsize)) != 0) 336 goto bad1; 337 338 for (i = 0; i < eh.e_phnum; i++) { 339 if (ph[i].p_type == PT_LOAD) { 340 loadmap[idx].vaddr = trunc_page(ph[i].p_vaddr); 341 loadmap[idx].memsz = round_page (ph[i].p_vaddr + 342 ph[i].p_memsz - loadmap[idx].vaddr); 343 file_align = ph[i].p_align; 344 idx++; 345 } 346 } 347 nload = idx; 348 349 /* 350 * If no position to load the interpreter was set by a probe 351 * function, pick the same address that a non-fixed mmap(0, ..) 352 * would (i.e. something safely out of the way). 353 */ 354 if (pos == ELFDEFNNAME(NO_ADDR)) { 355 pos = uvm_map_hint(p, VM_PROT_EXECUTE); 356 } 357 358 pos = ELF_ROUND(pos, file_align); 359 *last = epp->ep_interp_pos = pos; 360 for (i = 0; i < nload;/**/) { 361 vaddr_t addr; 362 struct uvm_object *uobj; 363 off_t uoff; 364 size_t size; 365 366 #ifdef this_needs_fixing 367 if (i == 0) { 368 uobj = &vp->v_uvm.u_obj; 369 /* need to fix uoff */ 370 } else { 371 #endif 372 uobj = NULL; 373 uoff = 0; 374 #ifdef this_needs_fixing 375 } 376 #endif 377 378 addr = trunc_page(pos + loadmap[i].vaddr); 379 size = round_page(addr + loadmap[i].memsz) - addr; 380 381 /* CRAP - map_findspace does not avoid daddr+MAXDSIZ */ 382 if ((addr + size > (vaddr_t)p->p_vmspace->vm_daddr) && 383 (addr < (vaddr_t)p->p_vmspace->vm_daddr + MAXDSIZ)) 384 addr = round_page((vaddr_t)p->p_vmspace->vm_daddr + 385 MAXDSIZ); 386 387 if (uvm_map_findspace(&p->p_vmspace->vm_map, addr, size, 388 &addr, uobj, uoff, 0, UVM_FLAG_FIXED) == NULL) { 389 if (uvm_map_findspace(&p->p_vmspace->vm_map, addr, size, 390 &addr, uobj, uoff, 0, 0) == NULL) { 391 error = ENOMEM; /* XXX */ 392 goto bad1; 393 } 394 } 395 if (addr != pos + loadmap[i].vaddr) { 396 /* base changed. */ 397 pos = addr - trunc_page(loadmap[i].vaddr); 398 pos = ELF_ROUND(pos,file_align); 399 epp->ep_interp_pos = *last = pos; 400 i = 0; 401 continue; 402 } 403 404 i++; 405 } 406 407 /* 408 * Load all the necessary sections 409 */ 410 for (i = 0; i < eh.e_phnum; i++) { 411 Elf_Addr size = 0; 412 int prot = 0; 413 int flags; 414 415 switch (ph[i].p_type) { 416 case PT_LOAD: 417 if (base_ph == NULL) { 418 flags = VMCMD_BASE; 419 addr = *last; 420 base_ph = &ph[i]; 421 } else { 422 flags = VMCMD_RELATIVE; 423 addr = ph[i].p_vaddr - base_ph->p_vaddr; 424 } 425 ELFNAME(load_psection)(&epp->ep_vmcmds, nd.ni_vp, 426 &ph[i], &addr, &size, &prot, flags); 427 /* If entry is within this section it must be text */ 428 if (eh.e_entry >= ph[i].p_vaddr && 429 eh.e_entry < (ph[i].p_vaddr + size)) { 430 epp->ep_entry = addr + eh.e_entry - 431 ELF_TRUNC(ph[i].p_vaddr,ph[i].p_align); 432 ap->arg_interp = addr; 433 } 434 addr += size; 435 break; 436 437 case PT_DYNAMIC: 438 case PT_PHDR: 439 case PT_NOTE: 440 break; 441 442 default: 443 break; 444 } 445 } 446 447 vn_marktext(nd.ni_vp); 448 449 bad1: 450 VOP_CLOSE(nd.ni_vp, FREAD, p->p_ucred, p); 451 bad: 452 if (ph != NULL) 453 free(ph, M_TEMP); 454 455 *last = addr; 456 vput(nd.ni_vp); 457 return (error); 458 } 459 460 /* 461 * Prepare an Elf binary's exec package 462 * 463 * First, set of the various offsets/lengths in the exec package. 464 * 465 * Then, mark the text image busy (so it can be demand paged) or error out if 466 * this is not possible. Finally, set up vmcmds for the text, data, bss, and 467 * stack segments. 468 */ 469 int 470 ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) 471 { 472 Elf_Ehdr *eh = epp->ep_hdr; 473 Elf_Phdr *ph, *pp, *base_ph = NULL; 474 Elf_Addr phdr = 0, exe_base = 0; 475 int error, i; 476 char *interp = NULL; 477 u_long pos = 0, phsize; 478 u_int8_t os = OOS_NULL; 479 480 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 481 return (ENOEXEC); 482 483 if (ELFNAME(check_header)(eh) || 484 (eh->e_type != ET_EXEC && eh->e_type != ET_DYN)) 485 return (ENOEXEC); 486 487 /* 488 * check if vnode is in open for writing, because we want to demand- 489 * page out of it. if it is, don't do it, for various reasons. 490 */ 491 if (epp->ep_vp->v_writecount != 0) { 492 #ifdef DIAGNOSTIC 493 if (epp->ep_vp->v_flag & VTEXT) 494 panic("exec: a VTEXT vnode has writecount != 0"); 495 #endif 496 return (ETXTBSY); 497 } 498 /* 499 * Allocate space to hold all the program headers, and read them 500 * from the file 501 */ 502 phsize = eh->e_phnum * sizeof(Elf_Phdr); 503 ph = malloc(phsize, M_TEMP, M_WAITOK); 504 505 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph, 506 phsize)) != 0) 507 goto bad; 508 509 epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 510 epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 511 512 for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) { 513 if (pp->p_type == PT_INTERP && !interp) { 514 if (pp->p_filesz >= MAXPATHLEN) 515 goto bad; 516 interp = pool_get(&namei_pool, PR_WAITOK); 517 if ((error = ELFNAME(read_from)(p, epp->ep_vp, 518 pp->p_offset, interp, pp->p_filesz)) != 0) { 519 goto bad; 520 } 521 } else if (pp->p_type == PT_LOAD) { 522 if (base_ph == NULL) 523 base_ph = pp; 524 } 525 } 526 527 if (eh->e_type == ET_DYN) { 528 /* need an interpreter and load sections for PIE */ 529 if (interp == NULL || base_ph == NULL) 530 goto bad; 531 /* randomize exe_base for PIE */ 532 exe_base = uvm_map_pie(base_ph->p_align); 533 } 534 535 /* 536 * OK, we want a slightly different twist of the 537 * standard emulation package for "real" elf. 538 */ 539 epp->ep_emul = &ELFNAMEEND(emul); 540 pos = ELFDEFNNAME(NO_ADDR); 541 542 /* 543 * On the same architecture, we may be emulating different systems. 544 * See which one will accept this executable. 545 * 546 * Probe functions would normally see if the interpreter (if any) 547 * exists. Emulation packages may possibly replace the interpreter in 548 * *interp with a changed path (/emul/xxx/<path>), and also 549 * set the ep_emul field in the exec package structure. 550 */ 551 error = ENOEXEC; 552 p->p_os = OOS_OPENBSD; 553 #ifdef NATIVE_EXEC_ELF 554 if (ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "OpenBSD", 8, 4) == 0) { 555 goto native; 556 } 557 #endif 558 for (i = 0; ELFNAME(probes)[i].func != NULL && error; i++) { 559 error = (*ELFNAME(probes)[i].func)(p, epp, interp, &pos, &os); 560 } 561 if (!error) 562 p->p_os = os; 563 #ifndef NATIVE_EXEC_ELF 564 else 565 goto bad; 566 #else 567 native: 568 #endif /* NATIVE_EXEC_ELF */ 569 570 /* 571 * Load all the necessary sections 572 */ 573 for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) { 574 Elf_Addr addr, size = 0; 575 int prot = 0; 576 int flags = 0; 577 578 switch (pp->p_type) { 579 case PT_LOAD: 580 if (exe_base != 0) { 581 if (pp == base_ph) { 582 flags = VMCMD_BASE; 583 addr = exe_base; 584 } else { 585 flags = VMCMD_RELATIVE; 586 addr = pp->p_vaddr - base_ph->p_vaddr; 587 } 588 } else 589 addr = ELFDEFNNAME(NO_ADDR); 590 591 /* 592 * Calculates size of text and data segments 593 * by starting at first and going to end of last. 594 * 'rwx' sections are treated as data. 595 * this is correct for BSS_PLT, but may not be 596 * for DATA_PLT, is fine for TEXT_PLT. 597 */ 598 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 599 pp, &addr, &size, &prot, flags); 600 601 /* 602 * Update exe_base in case allignment was off. 603 * For PIE, addr is relative to exe_base so 604 * adjust it (non PIE exe_base is 0 so no change). 605 */ 606 if (flags == VMCMD_BASE) 607 exe_base = addr; 608 else 609 addr += exe_base; 610 611 /* 612 * Decide whether it's text or data by looking 613 * at the protection of the section 614 */ 615 if (prot & VM_PROT_WRITE) { 616 /* data section */ 617 if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) { 618 epp->ep_daddr = addr; 619 epp->ep_dsize = size; 620 } else { 621 if (addr < epp->ep_daddr) { 622 epp->ep_dsize = 623 epp->ep_dsize + 624 epp->ep_daddr - 625 addr; 626 epp->ep_daddr = addr; 627 } else 628 epp->ep_dsize = addr+size - 629 epp->ep_daddr; 630 } 631 } else if (prot & VM_PROT_EXECUTE) { 632 /* text section */ 633 if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) { 634 epp->ep_taddr = addr; 635 epp->ep_tsize = size; 636 } else { 637 if (addr < epp->ep_taddr) { 638 epp->ep_tsize = 639 epp->ep_tsize + 640 epp->ep_taddr - 641 addr; 642 epp->ep_taddr = addr; 643 } else 644 epp->ep_tsize = addr+size - 645 epp->ep_taddr; 646 } 647 } 648 break; 649 650 case PT_SHLIB: 651 error = ENOEXEC; 652 goto bad; 653 654 case PT_INTERP: 655 /* Already did this one */ 656 case PT_DYNAMIC: 657 case PT_NOTE: 658 break; 659 660 case PT_PHDR: 661 /* Note address of program headers (in text segment) */ 662 phdr = pp->p_vaddr; 663 break; 664 665 default: 666 /* 667 * Not fatal, we don't need to understand everything 668 * :-) 669 */ 670 break; 671 } 672 } 673 674 phdr += exe_base; 675 676 /* 677 * Strangely some linux programs may have all load sections marked 678 * writeable, in this case, textsize is not -1, but rather 0; 679 */ 680 if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) 681 epp->ep_tsize = 0; 682 /* 683 * Another possibility is that it has all load sections marked 684 * read-only. Fake a zero-sized data segment right after the 685 * text segment. 686 */ 687 if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) { 688 epp->ep_daddr = round_page(epp->ep_taddr + epp->ep_tsize); 689 epp->ep_dsize = 0; 690 } 691 692 epp->ep_interp = interp; 693 epp->ep_entry = eh->e_entry + exe_base; 694 695 /* 696 * Check if we found a dynamically linked binary and arrange to load 697 * its interpreter when the exec file is released. 698 */ 699 if (interp) { 700 struct elf_args *ap; 701 702 ap = malloc(sizeof(struct elf_args), M_TEMP, M_WAITOK); 703 704 ap->arg_phaddr = phdr; 705 ap->arg_phentsize = eh->e_phentsize; 706 ap->arg_phnum = eh->e_phnum; 707 ap->arg_entry = eh->e_entry + exe_base; 708 ap->arg_os = os; 709 710 epp->ep_emul_arg = ap; 711 epp->ep_interp_pos = pos; 712 } 713 714 #if defined(COMPAT_SVR4) && defined(i386) && 0 /* nothing sets OOS_DELL... */ 715 #ifndef ELF_MAP_PAGE_ZERO 716 /* Dell SVR4 maps page zero, yeuch! */ 717 if (p->p_os == OOS_DELL) 718 #endif 719 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 720 epp->ep_vp, 0, VM_PROT_READ); 721 #endif 722 723 free(ph, M_TEMP); 724 vn_marktext(epp->ep_vp); 725 return (exec_setup_stack(p, epp)); 726 727 bad: 728 if (interp) 729 pool_put(&namei_pool, interp); 730 free(ph, M_TEMP); 731 kill_vmcmds(&epp->ep_vmcmds); 732 return (ENOEXEC); 733 } 734 735 /* 736 * Phase II of load. It is now safe to load the interpreter. Info collected 737 * when loading the program is available for setup of the interpreter. 738 */ 739 int 740 ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp) 741 { 742 char *interp; 743 int error; 744 struct elf_args *ap; 745 AuxInfo ai[ELF_AUX_ENTRIES], *a; 746 Elf_Addr pos = epp->ep_interp_pos; 747 748 if (epp->ep_interp == NULL) { 749 return (0); 750 } 751 752 interp = epp->ep_interp; 753 ap = epp->ep_emul_arg; 754 755 if ((error = ELFNAME(load_file)(p, interp, epp, ap, &pos)) != 0) { 756 free(ap, M_TEMP); 757 pool_put(&namei_pool, interp); 758 kill_vmcmds(&epp->ep_vmcmds); 759 return (error); 760 } 761 /* 762 * We have to do this ourselves... 763 */ 764 error = exec_process_vmcmds(p, epp); 765 766 /* 767 * Push extra arguments on the stack needed by dynamically 768 * linked binaries 769 */ 770 if (error == 0) { 771 a = ai; 772 773 a->au_id = AUX_phdr; 774 a->au_v = ap->arg_phaddr; 775 a++; 776 777 a->au_id = AUX_phent; 778 a->au_v = ap->arg_phentsize; 779 a++; 780 781 a->au_id = AUX_phnum; 782 a->au_v = ap->arg_phnum; 783 a++; 784 785 a->au_id = AUX_pagesz; 786 a->au_v = PAGE_SIZE; 787 a++; 788 789 a->au_id = AUX_base; 790 a->au_v = ap->arg_interp; 791 a++; 792 793 a->au_id = AUX_flags; 794 a->au_v = 0; 795 a++; 796 797 a->au_id = AUX_entry; 798 a->au_v = ap->arg_entry; 799 a++; 800 801 a->au_id = AUX_null; 802 a->au_v = 0; 803 a++; 804 805 error = copyout(ai, epp->ep_emul_argp, sizeof ai); 806 } 807 free(ap, M_TEMP); 808 pool_put(&namei_pool, interp); 809 return (error); 810 } 811 812 /* 813 * Older ELF binaries use EI_ABIVERSION (formerly EI_BRAND) to brand 814 * executables. Newer ELF binaries use EI_OSABI instead. 815 */ 816 char * 817 ELFNAME(check_brand)(Elf_Ehdr *eh) 818 { 819 if (eh->e_ident[EI_ABIVERSION] == '\0') 820 return (NULL); 821 return (&eh->e_ident[EI_ABIVERSION]); 822 } 823 824 int 825 ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, 826 char *os_name, size_t name_size, size_t desc_size) 827 { 828 Elf_Phdr *hph, *ph; 829 Elf_Note *np = NULL; 830 size_t phsize; 831 int error; 832 833 phsize = eh->e_phnum * sizeof(Elf_Phdr); 834 hph = malloc(phsize, M_TEMP, M_WAITOK); 835 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 836 (caddr_t)hph, phsize)) != 0) 837 goto out1; 838 839 for (ph = hph; ph < &hph[eh->e_phnum]; ph++) { 840 if (ph->p_type != PT_NOTE || 841 ph->p_filesz > 1024 || 842 ph->p_filesz < sizeof(Elf_Note) + name_size) 843 continue; 844 845 np = malloc(ph->p_filesz, M_TEMP, M_WAITOK); 846 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset, 847 (caddr_t)np, ph->p_filesz)) != 0) 848 goto out2; 849 850 #if 0 851 if (np->type != ELF_NOTE_TYPE_OSVERSION) { 852 free(np, M_TEMP); 853 np = NULL; 854 continue; 855 } 856 #endif 857 858 /* Check the name and description sizes. */ 859 if (np->namesz != name_size || 860 np->descsz != desc_size) 861 goto out3; 862 863 if (bcmp((np + 1), os_name, name_size)) 864 goto out3; 865 866 /* XXX: We could check for the specific emulation here */ 867 /* All checks succeeded. */ 868 error = 0; 869 goto out2; 870 } 871 872 out3: 873 error = ENOEXEC; 874 out2: 875 if (np) 876 free(np, M_TEMP); 877 out1: 878 free(hph, M_TEMP); 879 return error; 880 } 881