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