1 /* $NetBSD: exec_elf32.c,v 1.48 2000/04/11 04:37:50 chs Exp $ */ 2 3 /*- 4 * Copyright (c) 1994 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Copyright (c) 1996 Christopher G. Demetriou 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 /* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 67 #ifndef ELFSIZE 68 #define ELFSIZE 32 69 #endif 70 71 #include "opt_compat_linux.h" 72 #include "opt_compat_ibcs2.h" 73 #include "opt_compat_svr4.h" 74 #include "opt_compat_freebsd.h" 75 #include "opt_compat_netbsd32.h" 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/kernel.h> 80 #include <sys/proc.h> 81 #include <sys/malloc.h> 82 #include <sys/namei.h> 83 #include <sys/vnode.h> 84 #include <sys/exec.h> 85 #include <sys/exec_elf.h> 86 #include <sys/fcntl.h> 87 #include <sys/syscall.h> 88 #include <sys/signalvar.h> 89 #include <sys/mount.h> 90 #include <sys/stat.h> 91 92 #include <sys/mman.h> 93 #include <vm/vm.h> 94 #include <vm/vm_param.h> 95 #include <vm/vm_map.h> 96 97 #include <machine/cpu.h> 98 #include <machine/reg.h> 99 100 #ifdef COMPAT_NETBSD32 101 #include <compat/netbsd32/netbsd32_exec.h> 102 #endif 103 104 #ifdef COMPAT_LINUX 105 #include <compat/linux/common/linux_exec.h> 106 #endif 107 108 #ifdef COMPAT_SVR4 109 #include <compat/svr4/svr4_exec.h> 110 #endif 111 112 #ifdef COMPAT_IBCS2 113 #include <compat/ibcs2/ibcs2_exec.h> 114 #endif 115 116 #ifdef COMPAT_FREEBSD 117 #include <compat/freebsd/freebsd_exec.h> 118 #endif 119 120 int ELFNAME(check_header) __P((Elf_Ehdr *, int)); 121 int ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *, 122 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *)); 123 void ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *, 124 Elf_Phdr *, Elf_Addr *, u_long *, int *)); 125 126 int ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *, 127 Elf_Ehdr *)); 128 static int ELFNAME2(netbsd,probe) __P((struct proc *, struct exec_package *, 129 Elf_Ehdr *, char *, Elf_Addr *)); 130 131 extern char sigcode[], esigcode[]; 132 #ifdef SYSCALL_DEBUG 133 extern char *syscallnames[]; 134 #endif 135 136 struct emul ELFNAMEEND(emul_netbsd) = { 137 "netbsd", 138 NULL, 139 sendsig, 140 SYS_syscall, 141 SYS_MAXSYSCALL, 142 sysent, 143 #ifdef SYSCALL_DEBUG 144 syscallnames, 145 #else 146 NULL, 147 #endif 148 howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)), 149 ELFNAME(copyargs), 150 setregs, 151 sigcode, 152 esigcode, 153 }; 154 155 int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *, 156 Elf_Ehdr *, char *, Elf_Addr *)) = { 157 #if defined(COMPAT_NETBSD32) && (ELFSIZE == 32) 158 /* This one should go first so it matches instead of netbsd */ 159 ELFNAME2(netbsd32,probe), 160 #endif 161 ELFNAME2(netbsd,probe), 162 #if defined(COMPAT_FREEBSD) && (ELFSIZE == 32) 163 ELFNAME2(freebsd,probe), /* XXX not 64-bit safe */ 164 #endif 165 #if defined(COMPAT_LINUX) 166 ELFNAME2(linux,probe), 167 #endif 168 #if defined(COMPAT_SVR4) && (ELFSIZE == 32) 169 ELFNAME2(svr4,probe), /* XXX not 64-bit safe */ 170 #endif 171 #if defined(COMPAT_IBCS2) && (ELFSIZE == 32) 172 ELFNAME2(ibcs2,probe), /* XXX not 64-bit safe */ 173 #endif 174 }; 175 176 /* round up and down to page boundaries. */ 177 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 178 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 179 180 /* 181 * Copy arguments onto the stack in the normal way, but add some 182 * extra information in case of dynamic binding. 183 */ 184 void * 185 ELFNAME(copyargs)(pack, arginfo, stack, argp) 186 struct exec_package *pack; 187 struct ps_strings *arginfo; 188 void *stack; 189 void *argp; 190 { 191 size_t len; 192 AuxInfo ai[ELF_AUX_ENTRIES], *a; 193 struct elf_args *ap; 194 195 stack = copyargs(pack, arginfo, stack, argp); 196 if (!stack) 197 return NULL; 198 199 a = ai; 200 201 /* 202 * Push extra arguments on the stack needed by dynamically 203 * linked binaries 204 */ 205 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 206 207 a->a_type = AT_PHDR; 208 a->a_v = ap->arg_phaddr; 209 a++; 210 211 a->a_type = AT_PHENT; 212 a->a_v = ap->arg_phentsize; 213 a++; 214 215 a->a_type = AT_PHNUM; 216 a->a_v = ap->arg_phnum; 217 a++; 218 219 a->a_type = AT_PAGESZ; 220 a->a_v = NBPG; 221 a++; 222 223 a->a_type = AT_BASE; 224 a->a_v = ap->arg_interp; 225 a++; 226 227 a->a_type = AT_FLAGS; 228 a->a_v = 0; 229 a++; 230 231 a->a_type = AT_ENTRY; 232 a->a_v = ap->arg_entry; 233 a++; 234 235 free((char *)ap, M_TEMP); 236 pack->ep_emul_arg = NULL; 237 } 238 239 a->a_type = AT_NULL; 240 a->a_v = 0; 241 a++; 242 243 len = (a - ai) * sizeof(AuxInfo); 244 if (copyout(ai, stack, len)) 245 return NULL; 246 stack = (caddr_t)stack + len; 247 248 return stack; 249 } 250 251 /* 252 * elf_check_header(): 253 * 254 * Check header for validity; return 0 of ok ENOEXEC if error 255 */ 256 int 257 ELFNAME(check_header)(eh, type) 258 Elf_Ehdr *eh; 259 int type; 260 { 261 262 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 263 eh->e_ident[EI_CLASS] != ELFCLASS) 264 265 switch (eh->e_machine) { 266 267 ELFDEFNNAME(MACHDEP_ID_CASES) 268 269 default: 270 return ENOEXEC; 271 } 272 273 if (eh->e_type != type) 274 return ENOEXEC; 275 276 return 0; 277 } 278 279 /* 280 * elf_load_psection(): 281 * 282 * Load a psection at the appropriate address 283 */ 284 void 285 ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot) 286 struct exec_vmcmd_set *vcset; 287 struct vnode *vp; 288 Elf_Phdr *ph; 289 Elf_Addr *addr; 290 u_long *size; 291 int *prot; 292 { 293 u_long uaddr, msize, psize, rm, rf; 294 long diff, offset; 295 296 /* 297 * If the user specified an address, then we load there. 298 */ 299 if (*addr != ELFDEFNNAME(NO_ADDR)) { 300 if (ph->p_align > 1) { 301 *addr = ELF_ROUND(*addr, ph->p_align); 302 uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align); 303 } else 304 uaddr = ph->p_vaddr; 305 diff = ph->p_vaddr - uaddr; 306 } else { 307 *addr = uaddr = ph->p_vaddr; 308 if (ph->p_align > 1) 309 *addr = ELF_TRUNC(uaddr, ph->p_align); 310 diff = uaddr - *addr; 311 } 312 313 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 314 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 315 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 316 317 offset = ph->p_offset - diff; 318 *size = ph->p_filesz + diff; 319 msize = ph->p_memsz + diff; 320 psize = round_page(*size); 321 322 if ((ph->p_flags & PF_W) != 0) { 323 /* 324 * Because the pagedvn pager can't handle zero fill of the last 325 * data page if it's not page aligned we map the last page 326 * readvn. 327 */ 328 psize = trunc_page(*size); 329 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 330 offset, *prot); 331 if(psize != *size) 332 NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize, 333 *addr + psize, vp, offset + psize, *prot); 334 } else 335 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 336 offset, *prot); 337 338 /* 339 * Check if we need to extend the size of the segment 340 */ 341 rm = round_page(*addr + msize); 342 rf = round_page(*addr + *size); 343 344 if (rm != rf) { 345 NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 346 0, *prot); 347 *size = msize; 348 } 349 } 350 351 /* 352 * elf_read_from(): 353 * 354 * Read from vnode into buffer at offset. 355 */ 356 int 357 ELFNAME(read_from)(p, vp, off, buf, size) 358 struct vnode *vp; 359 u_long off; 360 struct proc *p; 361 caddr_t buf; 362 int size; 363 { 364 int error; 365 size_t resid; 366 367 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE, 368 0, p->p_ucred, &resid, p)) != 0) 369 return error; 370 /* 371 * See if we got all of it 372 */ 373 if (resid != 0) 374 return ENOEXEC; 375 return 0; 376 } 377 378 /* 379 * elf_load_file(): 380 * 381 * Load a file (interpreter/library) pointed to by path 382 * [stolen from coff_load_shlib()]. Made slightly generic 383 * so it might be used externally. 384 */ 385 int 386 ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last) 387 struct proc *p; 388 struct exec_package *epp; 389 char *path; 390 struct exec_vmcmd_set *vcset; 391 u_long *entry; 392 struct elf_args *ap; 393 Elf_Addr *last; 394 { 395 int error, i; 396 struct nameidata nd; 397 struct vnode *vp; 398 struct vattr attr; 399 Elf_Ehdr eh; 400 Elf_Phdr *ph = NULL; 401 u_long phsize; 402 char *bp = NULL; 403 Elf_Addr addr = *last; 404 405 bp = path; 406 /* 407 * 1. open file 408 * 2. read filehdr 409 * 3. map text, data, and bss out of it using VM_* 410 */ 411 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 412 if ((error = namei(&nd)) != 0) 413 return error; 414 vp = nd.ni_vp; 415 416 /* 417 * Similarly, if it's not marked as executable, or it's not a regular 418 * file, we don't allow it to be used. 419 */ 420 if (vp->v_type != VREG) { 421 error = EACCES; 422 goto badunlock; 423 } 424 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 425 goto badunlock; 426 427 /* get attributes */ 428 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 429 goto badunlock; 430 431 /* 432 * Check mount point. Though we're not trying to exec this binary, 433 * we will be executing code from it, so if the mount point 434 * disallows execution or set-id-ness, we punt or kill the set-id. 435 */ 436 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 437 error = EACCES; 438 goto badunlock; 439 } 440 if (vp->v_mount->mnt_flag & MNT_NOSUID) 441 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 442 443 #ifdef notyet /* XXX cgd 960926 */ 444 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 445 #endif 446 VOP_UNLOCK(vp, 0); 447 448 if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh, 449 sizeof(eh))) != 0) 450 goto bad; 451 452 if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0) 453 goto bad; 454 455 phsize = eh.e_phnum * sizeof(Elf_Phdr); 456 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 457 458 if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff, 459 (caddr_t) ph, phsize)) != 0) 460 goto bad; 461 462 /* 463 * Load all the necessary sections 464 */ 465 for (i = 0; i < eh.e_phnum; i++) { 466 u_long size = 0; 467 int prot = 0; 468 469 switch (ph[i].p_type) { 470 case PT_LOAD: 471 ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 472 &size, &prot); 473 /* If entry is within this section it must be text */ 474 if (eh.e_entry >= ph[i].p_vaddr && 475 eh.e_entry < (ph[i].p_vaddr + size)) { 476 /* XXX */ 477 *entry = addr + eh.e_entry; 478 #ifdef mips 479 *entry -= ph[i].p_vaddr; 480 #endif 481 ap->arg_interp = addr; 482 } 483 addr += size; 484 break; 485 486 case PT_DYNAMIC: 487 case PT_PHDR: 488 case PT_NOTE: 489 break; 490 491 default: 492 break; 493 } 494 } 495 496 free((char *)ph, M_TEMP); 497 *last = addr; 498 vrele(vp); 499 return 0; 500 501 badunlock: 502 VOP_UNLOCK(vp, 0); 503 504 bad: 505 if (ph != NULL) 506 free((char *)ph, M_TEMP); 507 #ifdef notyet /* XXX cgd 960926 */ 508 (maybe) VOP_CLOSE it 509 #endif 510 vrele(vp); 511 return error; 512 } 513 514 /* 515 * exec_elf_makecmds(): Prepare an Elf binary's exec package 516 * 517 * First, set of the various offsets/lengths in the exec package. 518 * 519 * Then, mark the text image busy (so it can be demand paged) or error 520 * out if this is not possible. Finally, set up vmcmds for the 521 * text, data, bss, and stack segments. 522 */ 523 int 524 ELFNAME2(exec,makecmds)(p, epp) 525 struct proc *p; 526 struct exec_package *epp; 527 { 528 Elf_Ehdr *eh = epp->ep_hdr; 529 Elf_Phdr *ph, *pp; 530 Elf_Addr phdr = 0, pos = 0; 531 int error, i, n, nload; 532 char interp[MAXPATHLEN]; 533 u_long phsize; 534 535 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 536 return ENOEXEC; 537 538 /* 539 * XXX allow for executing shared objects. It seems silly 540 * but other ELF-based systems allow it as well. 541 */ 542 if (ELFNAME(check_header)(eh, ET_EXEC) != 0 && 543 ELFNAME(check_header)(eh, ET_DYN) != 0) 544 return ENOEXEC; 545 546 /* 547 * check if vnode is in open for writing, because we want to 548 * demand-page out of it. if it is, don't do it, for various 549 * reasons 550 */ 551 if (epp->ep_vp->v_writecount != 0) { 552 #ifdef DIAGNOSTIC 553 if (epp->ep_vp->v_flag & VTEXT) 554 panic("exec: a VTEXT vnode has writecount != 0\n"); 555 #endif 556 return ETXTBSY; 557 } 558 /* 559 * Allocate space to hold all the program headers, and read them 560 * from the file 561 */ 562 phsize = eh->e_phnum * sizeof(Elf_Phdr); 563 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 564 565 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 566 (caddr_t) ph, phsize)) != 0) 567 goto bad; 568 569 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 570 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 571 572 interp[0] = '\0'; 573 574 for (i = 0; i < eh->e_phnum; i++) { 575 pp = &ph[i]; 576 if (pp->p_type == PT_INTERP) { 577 if (pp->p_filesz >= sizeof(interp)) 578 goto bad; 579 if ((error = ELFNAME(read_from)(p, epp->ep_vp, 580 pp->p_offset, (caddr_t) interp, 581 pp->p_filesz)) != 0) 582 goto bad; 583 break; 584 } 585 } 586 587 /* 588 * Setup things for native emulation. 589 */ 590 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 591 pos = ELFDEFNNAME(NO_ADDR); 592 593 /* 594 * On the same architecture, we may be emulating different systems. 595 * See which one will accept this executable. This currently only 596 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386 597 * and the Alpha. 598 * 599 * Probe functions would normally see if the interpreter (if any) 600 * exists. Emulation packages may possibly replace the interpreter in 601 * interp[] with a changed path (/emul/xxx/<path>), and also 602 * set the ep_emul field in the exec package structure. 603 */ 604 n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]); 605 if (n != 0) { 606 error = ENOEXEC; 607 for (i = 0; i < n && error; i++) 608 error = ELFNAME(probe_funcs)[i](p, epp, eh, 609 interp, &pos); 610 #ifdef notyet 611 /* 612 * We should really use a signature in our native binaries 613 * and have our own probe function for matching binaries, 614 * before trying the emulations. For now, if the emulation 615 * probes failed we default to native. 616 */ 617 if (error) 618 goto bad; 619 #endif 620 } 621 622 /* 623 * Load all the necessary sections 624 */ 625 for (i = nload = 0; i < eh->e_phnum; i++) { 626 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 627 u_long size = 0; 628 int prot = 0; 629 630 pp = &ph[i]; 631 632 switch (ph[i].p_type) { 633 case PT_LOAD: 634 /* 635 * XXX 636 * Can handle only 2 sections: text and data 637 */ 638 if (nload++ == 2) 639 goto bad; 640 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 641 &ph[i], &addr, &size, &prot); 642 643 /* 644 * Decide whether it's text or data by looking 645 * at the entry point. 646 */ 647 if (eh->e_entry >= addr && 648 eh->e_entry < (addr + size)) { 649 epp->ep_taddr = addr; 650 epp->ep_tsize = size; 651 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 652 epp->ep_daddr = addr; 653 epp->ep_dsize = size; 654 } 655 } else { 656 epp->ep_daddr = addr; 657 epp->ep_dsize = size; 658 } 659 break; 660 661 case PT_SHLIB: 662 #ifndef COMPAT_IBCS2 /* SCO has these sections */ 663 error = ENOEXEC; 664 goto bad; 665 #endif 666 667 case PT_INTERP: 668 /* Already did this one */ 669 case PT_DYNAMIC: 670 case PT_NOTE: 671 break; 672 673 case PT_PHDR: 674 /* Note address of program headers (in text segment) */ 675 phdr = pp->p_vaddr; 676 break; 677 678 default: 679 /* 680 * Not fatal; we don't need to understand everything. 681 */ 682 break; 683 } 684 } 685 686 /* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */ 687 #ifndef ELF_INTERP_NON_RELOCATABLE 688 /* 689 * If no position to load the interpreter was set by a probe 690 * function, pick the same address that a non-fixed mmap(0, ..) 691 * would (i.e. something safely out of the way). 692 */ 693 if (pos == ELFDEFNNAME(NO_ADDR)) 694 pos = round_page(epp->ep_daddr + MAXDSIZ); 695 #endif /* !ELF_INTERP_NON_RELOCATABLE */ 696 697 /* 698 * Check if we found a dynamically linked binary and arrange to load 699 * it's interpreter 700 */ 701 if (interp[0]) { 702 struct elf_args *ap; 703 704 ap = (struct elf_args *)malloc(sizeof(struct elf_args), 705 M_TEMP, M_WAITOK); 706 if ((error = ELFNAME(load_file)(p, epp, interp, 707 &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) { 708 free((char *)ap, M_TEMP); 709 goto bad; 710 } 711 pos += phsize; 712 ap->arg_phaddr = phdr; 713 714 ap->arg_phentsize = eh->e_phentsize; 715 ap->arg_phnum = eh->e_phnum; 716 ap->arg_entry = eh->e_entry; 717 718 epp->ep_emul_arg = ap; 719 } else 720 epp->ep_entry = eh->e_entry; 721 722 #ifdef ELF_MAP_PAGE_ZERO 723 /* Dell SVR4 maps page zero, yeuch! */ 724 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0, 725 VM_PROT_READ); 726 #endif 727 free((char *)ph, M_TEMP); 728 vn_marktext(epp->ep_vp); 729 return exec_elf_setup_stack(p, epp); 730 731 bad: 732 free((char *)ph, M_TEMP); 733 kill_vmcmds(&epp->ep_vmcmds); 734 return ENOEXEC; 735 } 736 737 int 738 ELFNAME2(netbsd,signature)(p, epp, eh) 739 struct proc *p; 740 struct exec_package *epp; 741 Elf_Ehdr *eh; 742 { 743 Elf_Phdr *hph, *ph; 744 Elf_Nhdr *np = NULL; 745 size_t phsize; 746 int error; 747 748 phsize = eh->e_phnum * sizeof(Elf_Phdr); 749 hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 750 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 751 (caddr_t)hph, phsize)) != 0) 752 goto out1; 753 754 for (ph = hph; ph < &hph[eh->e_phnum]; ph++) { 755 if (ph->p_type != PT_NOTE || 756 ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 757 continue; 758 759 np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK); 760 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset, 761 (caddr_t)np, ph->p_filesz)) != 0) 762 goto out2; 763 764 if (np->n_type != ELF_NOTE_TYPE_OSVERSION) { 765 free(np, M_TEMP); 766 np = NULL; 767 continue; 768 } 769 770 /* Check the name and description sizes. */ 771 if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 772 np->n_descsz != ELF_NOTE_NETBSD_DESCSZ) 773 goto out3; 774 775 /* Is the name "NetBSD\0\0"? */ 776 if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME, 777 ELF_NOTE_NETBSD_NAMESZ)) 778 goto out3; 779 780 /* XXX: We could check for the specific emulation here */ 781 /* All checks succeeded. */ 782 error = 0; 783 goto out2; 784 } 785 786 out3: 787 error = ENOEXEC; 788 out2: 789 if (np) 790 free(np, M_TEMP); 791 out1: 792 free(hph, M_TEMP); 793 return error; 794 } 795 796 static int 797 ELFNAME2(netbsd,probe)(p, epp, eh, itp, pos) 798 struct proc *p; 799 struct exec_package *epp; 800 Elf_Ehdr *eh; 801 char *itp; 802 Elf_Addr *pos; 803 { 804 int error; 805 806 if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0) 807 return error; 808 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 809 *pos = ELFDEFNNAME(NO_ADDR); 810 return 0; 811 } 812