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