exec_elf32.c revision 1.49
11.49Ssoren/*	$NetBSD: exec_elf32.c,v 1.49 2000/06/06 18:26:34 soren Exp $	*/
21.36Schristos
31.36Schristos/*-
41.36Schristos * Copyright (c) 1994 The NetBSD Foundation, Inc.
51.36Schristos * All rights reserved.
61.36Schristos *
71.36Schristos * This code is derived from software contributed to The NetBSD Foundation
81.36Schristos * by Christos Zoulas.
91.36Schristos *
101.36Schristos * Redistribution and use in source and binary forms, with or without
111.36Schristos * modification, are permitted provided that the following conditions
121.36Schristos * are met:
131.36Schristos * 1. Redistributions of source code must retain the above copyright
141.36Schristos *    notice, this list of conditions and the following disclaimer.
151.36Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.36Schristos *    notice, this list of conditions and the following disclaimer in the
171.36Schristos *    documentation and/or other materials provided with the distribution.
181.36Schristos * 3. All advertising materials mentioning features or use of this software
191.36Schristos *    must display the following acknowledgement:
201.37Schristos *	This product includes software developed by the NetBSD
211.37Schristos *	Foundation, Inc. and its contributors.
221.36Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
231.36Schristos *    contributors may be used to endorse or promote products derived
241.36Schristos *    from this software without specific prior written permission.
251.36Schristos *
261.36Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.36Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.36Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.36Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.36Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.36Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.36Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.36Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.36Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.36Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.36Schristos * POSSIBILITY OF SUCH DAMAGE.
371.36Schristos */
381.1Sfvdl
391.1Sfvdl/*
401.9Scgd * Copyright (c) 1996 Christopher G. Demetriou
411.1Sfvdl * All rights reserved.
421.1Sfvdl *
431.1Sfvdl * Redistribution and use in source and binary forms, with or without
441.1Sfvdl * modification, are permitted provided that the following conditions
451.1Sfvdl * are met:
461.1Sfvdl * 1. Redistributions of source code must retain the above copyright
471.1Sfvdl *    notice, this list of conditions and the following disclaimer.
481.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
491.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
501.1Sfvdl *    documentation and/or other materials provided with the distribution.
511.1Sfvdl * 3. The name of the author may not be used to endorse or promote products
521.1Sfvdl *    derived from this software without specific prior written permission
531.1Sfvdl *
541.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
551.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
561.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
571.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
581.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
591.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
601.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
611.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
621.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
631.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
641.1Sfvdl */
651.1Sfvdl
661.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
671.9Scgd#ifndef ELFSIZE
681.9Scgd#define	ELFSIZE		32
691.9Scgd#endif
701.30Sthorpej
711.30Sthorpej#include "opt_compat_linux.h"
721.31Sthorpej#include "opt_compat_ibcs2.h"
731.32Sthorpej#include "opt_compat_svr4.h"
741.42Schristos#include "opt_compat_freebsd.h"
751.47Seeh#include "opt_compat_netbsd32.h"
761.49Ssoren#include "opt_syscall_debug.h"
771.9Scgd
781.1Sfvdl#include <sys/param.h>
791.1Sfvdl#include <sys/systm.h>
801.1Sfvdl#include <sys/kernel.h>
811.1Sfvdl#include <sys/proc.h>
821.1Sfvdl#include <sys/malloc.h>
831.1Sfvdl#include <sys/namei.h>
841.1Sfvdl#include <sys/vnode.h>
851.1Sfvdl#include <sys/exec.h>
861.1Sfvdl#include <sys/exec_elf.h>
871.9Scgd#include <sys/fcntl.h>
881.8Schristos#include <sys/syscall.h>
891.8Schristos#include <sys/signalvar.h>
901.14Scgd#include <sys/mount.h>
911.14Scgd#include <sys/stat.h>
921.1Sfvdl
931.1Sfvdl#include <sys/mman.h>
941.1Sfvdl#include <vm/vm.h>
951.1Sfvdl#include <vm/vm_param.h>
961.1Sfvdl#include <vm/vm_map.h>
971.1Sfvdl
981.1Sfvdl#include <machine/cpu.h>
991.1Sfvdl#include <machine/reg.h>
1001.1Sfvdl
1011.47Seeh#ifdef COMPAT_NETBSD32
1021.47Seeh#include <compat/netbsd32/netbsd32_exec.h>
1031.47Seeh#endif
1041.47Seeh
1051.1Sfvdl#ifdef COMPAT_LINUX
1061.39Schristos#include <compat/linux/common/linux_exec.h>
1071.1Sfvdl#endif
1081.1Sfvdl
1091.1Sfvdl#ifdef COMPAT_SVR4
1101.1Sfvdl#include <compat/svr4/svr4_exec.h>
1111.1Sfvdl#endif
1121.1Sfvdl
1131.27Sscottb#ifdef COMPAT_IBCS2
1141.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
1151.27Sscottb#endif
1161.27Sscottb
1171.42Schristos#ifdef COMPAT_FREEBSD
1181.42Schristos#include <compat/freebsd/freebsd_exec.h>
1191.42Schristos#endif
1201.42Schristos
1211.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
1221.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
1231.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
1241.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
1251.17Scgd	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
1261.1Sfvdl
1271.47Seehint ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *,
1281.40Schristos    Elf_Ehdr *));
1291.40Schristosstatic int ELFNAME2(netbsd,probe) __P((struct proc *, struct exec_package *,
1301.40Schristos    Elf_Ehdr *, char *, Elf_Addr *));
1311.40Schristos
1321.8Schristosextern char sigcode[], esigcode[];
1331.8Schristos#ifdef SYSCALL_DEBUG
1341.8Schristosextern char *syscallnames[];
1351.17Scgd#endif
1361.8Schristos
1371.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1381.8Schristos	"netbsd",
1391.8Schristos	NULL,
1401.8Schristos	sendsig,
1411.8Schristos	SYS_syscall,
1421.8Schristos	SYS_MAXSYSCALL,
1431.8Schristos	sysent,
1441.8Schristos#ifdef SYSCALL_DEBUG
1451.8Schristos	syscallnames,
1461.17Scgd#else
1471.8Schristos	NULL,
1481.17Scgd#endif
1491.47Seeh	howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)),
1501.17Scgd	ELFNAME(copyargs),
1511.8Schristos	setregs,
1521.8Schristos	sigcode,
1531.8Schristos	esigcode,
1541.17Scgd};
1551.9Scgd
1561.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1571.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1581.47Seeh#if defined(COMPAT_NETBSD32) && (ELFSIZE == 32)
1591.47Seeh	    /* This one should go first so it matches instead of netbsd */
1601.47Seeh	ELFNAME2(netbsd32,probe),
1611.47Seeh#endif
1621.40Schristos	ELFNAME2(netbsd,probe),
1631.42Schristos#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32)
1641.42Schristos	ELFNAME2(freebsd,probe),		/* XXX not 64-bit safe */
1651.42Schristos#endif
1661.38Serh#if defined(COMPAT_LINUX)
1671.38Serh	ELFNAME2(linux,probe),
1681.23Shpeyerl#endif
1691.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1701.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1711.9Scgd#endif
1721.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1731.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1741.27Sscottb#endif
1751.8Schristos};
1761.8Schristos
1771.18Scgd/* round up and down to page boundaries. */
1781.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1791.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1801.8Schristos
1811.8Schristos/*
1821.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1831.1Sfvdl * extra information in case of dynamic binding.
1841.1Sfvdl */
1851.1Sfvdlvoid *
1861.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1871.1Sfvdl	struct exec_package *pack;
1881.1Sfvdl	struct ps_strings *arginfo;
1891.1Sfvdl	void *stack;
1901.1Sfvdl	void *argp;
1911.1Sfvdl{
1921.1Sfvdl	size_t len;
1931.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1941.1Sfvdl	struct elf_args *ap;
1951.1Sfvdl
1961.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1971.4Sfvdl	if (!stack)
1981.1Sfvdl		return NULL;
1991.1Sfvdl
2001.22Scgd	a = ai;
2011.22Scgd
2021.1Sfvdl	/*
2031.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
2041.1Sfvdl	 * linked binaries
2051.1Sfvdl	 */
2061.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
2071.1Sfvdl
2081.46Skleink		a->a_type = AT_PHDR;
2091.46Skleink		a->a_v = ap->arg_phaddr;
2101.1Sfvdl		a++;
2111.1Sfvdl
2121.46Skleink		a->a_type = AT_PHENT;
2131.46Skleink		a->a_v = ap->arg_phentsize;
2141.1Sfvdl		a++;
2151.1Sfvdl
2161.46Skleink		a->a_type = AT_PHNUM;
2171.46Skleink		a->a_v = ap->arg_phnum;
2181.1Sfvdl		a++;
2191.1Sfvdl
2201.46Skleink		a->a_type = AT_PAGESZ;
2211.46Skleink		a->a_v = NBPG;
2221.1Sfvdl		a++;
2231.1Sfvdl
2241.46Skleink		a->a_type = AT_BASE;
2251.46Skleink		a->a_v = ap->arg_interp;
2261.1Sfvdl		a++;
2271.1Sfvdl
2281.46Skleink		a->a_type = AT_FLAGS;
2291.46Skleink		a->a_v = 0;
2301.1Sfvdl		a++;
2311.1Sfvdl
2321.46Skleink		a->a_type = AT_ENTRY;
2331.46Skleink		a->a_v = ap->arg_entry;
2341.1Sfvdl		a++;
2351.1Sfvdl
2361.17Scgd		free((char *)ap, M_TEMP);
2371.9Scgd		pack->ep_emul_arg = NULL;
2381.1Sfvdl	}
2391.22Scgd
2401.46Skleink	a->a_type = AT_NULL;
2411.46Skleink	a->a_v = 0;
2421.22Scgd	a++;
2431.22Scgd
2441.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2451.22Scgd	if (copyout(ai, stack, len))
2461.22Scgd		return NULL;
2471.43Skleink	stack = (caddr_t)stack + len;
2481.22Scgd
2491.4Sfvdl	return stack;
2501.1Sfvdl}
2511.1Sfvdl
2521.1Sfvdl/*
2531.1Sfvdl * elf_check_header():
2541.1Sfvdl *
2551.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2561.1Sfvdl */
2571.17Scgdint
2581.9ScgdELFNAME(check_header)(eh, type)
2591.9Scgd	Elf_Ehdr *eh;
2601.1Sfvdl	int type;
2611.1Sfvdl{
2621.3Sthorpej
2631.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2641.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2651.1Sfvdl
2661.1Sfvdl	switch (eh->e_machine) {
2671.9Scgd
2681.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2691.1Sfvdl
2701.1Sfvdl	default:
2711.1Sfvdl		return ENOEXEC;
2721.1Sfvdl	}
2731.1Sfvdl
2741.1Sfvdl	if (eh->e_type != type)
2751.1Sfvdl		return ENOEXEC;
2761.1Sfvdl
2771.1Sfvdl	return 0;
2781.1Sfvdl}
2791.1Sfvdl
2801.1Sfvdl/*
2811.1Sfvdl * elf_load_psection():
2821.17Scgd *
2831.1Sfvdl * Load a psection at the appropriate address
2841.1Sfvdl */
2851.17Scgdvoid
2861.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2871.1Sfvdl	struct exec_vmcmd_set *vcset;
2881.1Sfvdl	struct vnode *vp;
2891.9Scgd	Elf_Phdr *ph;
2901.9Scgd	Elf_Addr *addr;
2911.1Sfvdl	u_long *size;
2921.1Sfvdl	int *prot;
2931.1Sfvdl{
2941.8Schristos	u_long uaddr, msize, psize, rm, rf;
2951.1Sfvdl	long diff, offset;
2961.1Sfvdl
2971.1Sfvdl	/*
2981.17Scgd	 * If the user specified an address, then we load there.
2991.17Scgd	 */
3001.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
3011.1Sfvdl		if (ph->p_align > 1) {
3021.18Scgd			*addr = ELF_ROUND(*addr, ph->p_align);
3031.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
3041.1Sfvdl		} else
3051.1Sfvdl			uaddr = ph->p_vaddr;
3061.1Sfvdl		diff = ph->p_vaddr - uaddr;
3071.1Sfvdl	} else {
3081.1Sfvdl		*addr = uaddr = ph->p_vaddr;
3091.1Sfvdl		if (ph->p_align > 1)
3101.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
3111.1Sfvdl		diff = uaddr - *addr;
3121.1Sfvdl	}
3131.1Sfvdl
3141.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
3151.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
3161.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
3171.1Sfvdl
3181.1Sfvdl	offset = ph->p_offset - diff;
3191.1Sfvdl	*size = ph->p_filesz + diff;
3201.1Sfvdl	msize = ph->p_memsz + diff;
3211.8Schristos	psize = round_page(*size);
3221.1Sfvdl
3231.46Skleink	if ((ph->p_flags & PF_W) != 0) {
3241.8Schristos		/*
3251.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
3261.8Schristos		 * data page if it's not page aligned we map the last page
3271.8Schristos		 * readvn.
3281.8Schristos		 */
3291.8Schristos		psize = trunc_page(*size);
3301.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3311.8Schristos		    offset, *prot);
3321.8Schristos		if(psize != *size)
3331.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
3341.8Schristos			    *addr + psize, vp, offset + psize, *prot);
3351.9Scgd	} else
3361.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3371.8Schristos		    offset, *prot);
3381.1Sfvdl
3391.1Sfvdl	/*
3401.17Scgd	 * Check if we need to extend the size of the segment
3411.17Scgd	 */
3421.1Sfvdl	rm = round_page(*addr + msize);
3431.1Sfvdl	rf = round_page(*addr + *size);
3441.1Sfvdl
3451.1Sfvdl	if (rm != rf) {
3461.17Scgd		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3471.17Scgd		    0, *prot);
3481.1Sfvdl		*size = msize;
3491.1Sfvdl	}
3501.1Sfvdl}
3511.1Sfvdl
3521.1Sfvdl/*
3531.1Sfvdl * elf_read_from():
3541.1Sfvdl *
3551.1Sfvdl *	Read from vnode into buffer at offset.
3561.1Sfvdl */
3571.7Schristosint
3581.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3591.1Sfvdl	struct vnode *vp;
3601.1Sfvdl	u_long off;
3611.1Sfvdl	struct proc *p;
3621.1Sfvdl	caddr_t buf;
3631.1Sfvdl	int size;
3641.1Sfvdl{
3651.1Sfvdl	int error;
3661.33Sthorpej	size_t resid;
3671.1Sfvdl
3681.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3691.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3701.1Sfvdl		return error;
3711.1Sfvdl	/*
3721.17Scgd	 * See if we got all of it
3731.17Scgd	 */
3741.1Sfvdl	if (resid != 0)
3751.4Sfvdl		return ENOEXEC;
3761.1Sfvdl	return 0;
3771.1Sfvdl}
3781.1Sfvdl
3791.1Sfvdl/*
3801.1Sfvdl * elf_load_file():
3811.1Sfvdl *
3821.1Sfvdl * Load a file (interpreter/library) pointed to by path
3831.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3841.1Sfvdl * so it might be used externally.
3851.1Sfvdl */
3861.17Scgdint
3871.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3881.1Sfvdl	struct proc *p;
3891.14Scgd	struct exec_package *epp;
3901.1Sfvdl	char *path;
3911.1Sfvdl	struct exec_vmcmd_set *vcset;
3921.1Sfvdl	u_long *entry;
3931.1Sfvdl	struct elf_args	*ap;
3941.9Scgd	Elf_Addr *last;
3951.1Sfvdl{
3961.1Sfvdl	int error, i;
3971.1Sfvdl	struct nameidata nd;
3981.14Scgd	struct vnode *vp;
3991.14Scgd	struct vattr attr;
4001.9Scgd	Elf_Ehdr eh;
4011.9Scgd	Elf_Phdr *ph = NULL;
4021.1Sfvdl	u_long phsize;
4031.1Sfvdl	char *bp = NULL;
4041.9Scgd	Elf_Addr addr = *last;
4051.1Sfvdl
4061.1Sfvdl	bp = path;
4071.1Sfvdl	/*
4081.17Scgd	 * 1. open file
4091.17Scgd	 * 2. read filehdr
4101.17Scgd	 * 3. map text, data, and bss out of it using VM_*
4111.17Scgd	 */
4121.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
4131.12Scgd	if ((error = namei(&nd)) != 0)
4141.1Sfvdl		return error;
4151.14Scgd	vp = nd.ni_vp;
4161.14Scgd
4171.26Smycroft	/*
4181.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
4191.26Smycroft	 * file, we don't allow it to be used.
4201.26Smycroft	 */
4211.14Scgd	if (vp->v_type != VREG) {
4221.14Scgd		error = EACCES;
4231.14Scgd		goto badunlock;
4241.14Scgd	}
4251.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
4261.26Smycroft		goto badunlock;
4271.14Scgd
4281.17Scgd	/* get attributes */
4291.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
4301.14Scgd		goto badunlock;
4311.14Scgd
4321.14Scgd	/*
4331.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
4341.15Scgd	 * we will be executing code from it, so if the mount point
4351.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
4361.14Scgd	 */
4371.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
4381.14Scgd		error = EACCES;
4391.14Scgd		goto badunlock;
4401.14Scgd	}
4411.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
4421.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
4431.14Scgd
4441.12Scgd#ifdef notyet /* XXX cgd 960926 */
4451.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
4461.12Scgd#endif
4471.28Sfvdl	VOP_UNLOCK(vp, 0);
4481.12Scgd
4491.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
4501.17Scgd	    sizeof(eh))) != 0)
4511.1Sfvdl		goto bad;
4521.1Sfvdl
4531.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
4541.1Sfvdl		goto bad;
4551.1Sfvdl
4561.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4571.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4581.1Sfvdl
4591.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4601.9Scgd	    (caddr_t) ph, phsize)) != 0)
4611.1Sfvdl		goto bad;
4621.1Sfvdl
4631.1Sfvdl	/*
4641.17Scgd	 * Load all the necessary sections
4651.17Scgd	 */
4661.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4671.1Sfvdl		u_long size = 0;
4681.1Sfvdl		int prot = 0;
4691.1Sfvdl
4701.1Sfvdl		switch (ph[i].p_type) {
4711.46Skleink		case PT_LOAD:
4721.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4731.17Scgd			    &size, &prot);
4741.4Sfvdl			/* If entry is within this section it must be text */
4751.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4761.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4771.21Sfvdl				/* XXX */
4781.21Sfvdl				*entry = addr + eh.e_entry;
4791.21Sfvdl#ifdef mips
4801.21Sfvdl				*entry -= ph[i].p_vaddr;
4811.21Sfvdl#endif
4821.1Sfvdl				ap->arg_interp = addr;
4831.1Sfvdl			}
4841.1Sfvdl			addr += size;
4851.1Sfvdl			break;
4861.1Sfvdl
4871.46Skleink		case PT_DYNAMIC:
4881.46Skleink		case PT_PHDR:
4891.46Skleink		case PT_NOTE:
4901.1Sfvdl			break;
4911.1Sfvdl
4921.1Sfvdl		default:
4931.1Sfvdl			break;
4941.1Sfvdl		}
4951.1Sfvdl	}
4961.1Sfvdl
4971.17Scgd	free((char *)ph, M_TEMP);
4981.12Scgd	*last = addr;
4991.14Scgd	vrele(vp);
5001.12Scgd	return 0;
5011.12Scgd
5021.14Scgdbadunlock:
5031.28Sfvdl	VOP_UNLOCK(vp, 0);
5041.14Scgd
5051.1Sfvdlbad:
5061.1Sfvdl	if (ph != NULL)
5071.17Scgd		free((char *)ph, M_TEMP);
5081.12Scgd#ifdef notyet /* XXX cgd 960926 */
5091.12Scgd	(maybe) VOP_CLOSE it
5101.12Scgd#endif
5111.14Scgd	vrele(vp);
5121.1Sfvdl	return error;
5131.1Sfvdl}
5141.1Sfvdl
5151.1Sfvdl/*
5161.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
5171.1Sfvdl *
5181.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5191.1Sfvdl *
5201.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5211.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5221.1Sfvdl * text, data, bss, and stack segments.
5231.1Sfvdl */
5241.1Sfvdlint
5251.9ScgdELFNAME2(exec,makecmds)(p, epp)
5261.1Sfvdl	struct proc *p;
5271.1Sfvdl	struct exec_package *epp;
5281.1Sfvdl{
5291.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
5301.9Scgd	Elf_Phdr *ph, *pp;
5311.9Scgd	Elf_Addr phdr = 0, pos = 0;
5321.4Sfvdl	int error, i, n, nload;
5331.1Sfvdl	char interp[MAXPATHLEN];
5341.9Scgd	u_long phsize;
5351.1Sfvdl
5361.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
5371.1Sfvdl		return ENOEXEC;
5381.1Sfvdl
5391.45Sfvdl	/*
5401.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
5411.45Sfvdl	 * but other ELF-based systems allow it as well.
5421.45Sfvdl	 */
5431.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
5441.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
5451.1Sfvdl		return ENOEXEC;
5461.1Sfvdl
5471.1Sfvdl	/*
5481.17Scgd	 * check if vnode is in open for writing, because we want to
5491.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5501.17Scgd	 * reasons
5511.17Scgd	 */
5521.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5531.1Sfvdl#ifdef DIAGNOSTIC
5541.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5551.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5561.1Sfvdl#endif
5571.1Sfvdl		return ETXTBSY;
5581.1Sfvdl	}
5591.1Sfvdl	/*
5601.17Scgd	 * Allocate space to hold all the program headers, and read them
5611.17Scgd	 * from the file
5621.17Scgd	 */
5631.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5641.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5651.1Sfvdl
5661.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5671.8Schristos	    (caddr_t) ph, phsize)) != 0)
5681.1Sfvdl		goto bad;
5691.1Sfvdl
5701.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5711.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5721.1Sfvdl
5731.1Sfvdl	interp[0] = '\0';
5741.1Sfvdl
5751.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5761.1Sfvdl		pp = &ph[i];
5771.46Skleink		if (pp->p_type == PT_INTERP) {
5781.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5791.1Sfvdl				goto bad;
5801.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5811.17Scgd			    pp->p_offset, (caddr_t) interp,
5821.17Scgd			    pp->p_filesz)) != 0)
5831.1Sfvdl				goto bad;
5841.1Sfvdl			break;
5851.1Sfvdl		}
5861.1Sfvdl	}
5871.1Sfvdl
5881.9Scgd	/*
5891.8Schristos	 * Setup things for native emulation.
5901.8Schristos	 */
5911.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5921.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5931.8Schristos
5941.1Sfvdl	/*
5951.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5961.1Sfvdl	 * See which one will accept this executable. This currently only
5971.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5981.38Serh	 * and the Alpha.
5991.1Sfvdl	 *
6001.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
6011.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
6021.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
6031.1Sfvdl	 * set the ep_emul field in the exec package structure.
6041.1Sfvdl	 */
6051.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
6061.17Scgd	if (n != 0) {
6071.1Sfvdl		error = ENOEXEC;
6081.1Sfvdl		for (i = 0; i < n && error; i++)
6091.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
6101.17Scgd			    interp, &pos);
6111.8Schristos#ifdef notyet
6121.8Schristos		/*
6131.8Schristos		 * We should really use a signature in our native binaries
6141.8Schristos		 * and have our own probe function for matching binaries,
6151.8Schristos		 * before trying the emulations. For now, if the emulation
6161.8Schristos		 * probes failed we default to native.
6171.8Schristos		 */
6181.1Sfvdl		if (error)
6191.1Sfvdl			goto bad;
6201.8Schristos#endif
6211.1Sfvdl	}
6221.1Sfvdl
6231.1Sfvdl	/*
6241.17Scgd	 * Load all the necessary sections
6251.17Scgd	 */
6261.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
6271.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
6281.9Scgd		u_long size = 0;
6291.1Sfvdl		int prot = 0;
6301.1Sfvdl
6311.1Sfvdl		pp = &ph[i];
6321.1Sfvdl
6331.1Sfvdl		switch (ph[i].p_type) {
6341.46Skleink		case PT_LOAD:
6351.4Sfvdl			/*
6361.4Sfvdl			 * XXX
6371.4Sfvdl			 * Can handle only 2 sections: text and data
6381.4Sfvdl			 */
6391.4Sfvdl			if (nload++ == 2)
6401.4Sfvdl				goto bad;
6411.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6421.17Scgd			    &ph[i], &addr, &size, &prot);
6431.17Scgd
6441.4Sfvdl			/*
6451.4Sfvdl			 * Decide whether it's text or data by looking
6461.4Sfvdl			 * at the entry point.
6471.4Sfvdl			 */
6481.19Scgd			if (eh->e_entry >= addr &&
6491.19Scgd			    eh->e_entry < (addr + size)) {
6501.4Sfvdl				epp->ep_taddr = addr;
6511.4Sfvdl				epp->ep_tsize = size;
6521.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6531.19Scgd					epp->ep_daddr = addr;
6541.19Scgd					epp->ep_dsize = size;
6551.19Scgd				}
6561.4Sfvdl			} else {
6571.4Sfvdl				epp->ep_daddr = addr;
6581.4Sfvdl				epp->ep_dsize = size;
6591.4Sfvdl			}
6601.1Sfvdl			break;
6611.1Sfvdl
6621.46Skleink		case PT_SHLIB:
6631.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6641.1Sfvdl			error = ENOEXEC;
6651.1Sfvdl			goto bad;
6661.27Sscottb#endif
6671.1Sfvdl
6681.46Skleink		case PT_INTERP:
6691.1Sfvdl			/* Already did this one */
6701.46Skleink		case PT_DYNAMIC:
6711.46Skleink		case PT_NOTE:
6721.1Sfvdl			break;
6731.1Sfvdl
6741.46Skleink		case PT_PHDR:
6751.4Sfvdl			/* Note address of program headers (in text segment) */
6761.4Sfvdl			phdr = pp->p_vaddr;
6771.7Schristos			break;
6781.4Sfvdl
6791.1Sfvdl		default:
6801.1Sfvdl			/*
6811.9Scgd			 * Not fatal; we don't need to understand everything.
6821.1Sfvdl			 */
6831.1Sfvdl			break;
6841.1Sfvdl		}
6851.1Sfvdl	}
6861.5Sfvdl
6871.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6881.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6891.5Sfvdl	/*
6901.5Sfvdl	 * If no position to load the interpreter was set by a probe
6911.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6921.5Sfvdl	 * would (i.e. something safely out of the way).
6931.5Sfvdl	 */
6941.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6951.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6961.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6971.1Sfvdl
6981.1Sfvdl	/*
6991.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
7001.17Scgd	 * it's interpreter
7011.17Scgd	 */
7021.1Sfvdl	if (interp[0]) {
7031.1Sfvdl		struct elf_args *ap;
7041.1Sfvdl
7051.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
7061.17Scgd		    M_TEMP, M_WAITOK);
7071.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
7081.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
7091.17Scgd			free((char *)ap, M_TEMP);
7101.1Sfvdl			goto bad;
7111.1Sfvdl		}
7121.1Sfvdl		pos += phsize;
7131.4Sfvdl		ap->arg_phaddr = phdr;
7141.1Sfvdl
7151.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
7161.1Sfvdl		ap->arg_phnum = eh->e_phnum;
7171.1Sfvdl		ap->arg_entry = eh->e_entry;
7181.1Sfvdl
7191.1Sfvdl		epp->ep_emul_arg = ap;
7201.1Sfvdl	} else
7211.1Sfvdl		epp->ep_entry = eh->e_entry;
7221.1Sfvdl
7231.8Schristos#ifdef ELF_MAP_PAGE_ZERO
7241.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
7251.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
7261.8Schristos	    VM_PROT_READ);
7271.8Schristos#endif
7281.17Scgd	free((char *)ph, M_TEMP);
7291.48Schs	vn_marktext(epp->ep_vp);
7301.9Scgd	return exec_elf_setup_stack(p, epp);
7311.1Sfvdl
7321.1Sfvdlbad:
7331.17Scgd	free((char *)ph, M_TEMP);
7341.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7351.1Sfvdl	return ENOEXEC;
7361.40Schristos}
7371.40Schristos
7381.47Seehint
7391.40SchristosELFNAME2(netbsd,signature)(p, epp, eh)
7401.40Schristos	struct proc *p;
7411.40Schristos	struct exec_package *epp;
7421.40Schristos	Elf_Ehdr *eh;
7431.40Schristos{
7441.40Schristos	Elf_Phdr *hph, *ph;
7451.46Skleink	Elf_Nhdr *np = NULL;
7461.40Schristos	size_t phsize;
7471.40Schristos	int error;
7481.40Schristos
7491.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
7501.40Schristos	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
7511.41Schristos	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
7521.41Schristos	    (caddr_t)hph, phsize)) != 0)
7531.40Schristos		goto out1;
7541.40Schristos
7551.40Schristos	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
7561.46Skleink		if (ph->p_type != PT_NOTE ||
7571.46Skleink		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
7581.40Schristos			continue;
7591.40Schristos
7601.46Skleink		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
7611.40Schristos		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
7621.40Schristos		    (caddr_t)np, ph->p_filesz)) != 0)
7631.40Schristos			goto out2;
7641.40Schristos
7651.46Skleink		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
7661.40Schristos			free(np, M_TEMP);
7671.40Schristos			np = NULL;
7681.40Schristos			continue;
7691.40Schristos		}
7701.40Schristos
7711.40Schristos		/* Check the name and description sizes. */
7721.46Skleink		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
7731.46Skleink		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
7741.40Schristos			goto out3;
7751.40Schristos
7761.40Schristos		/* Is the name "NetBSD\0\0"? */
7771.40Schristos		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
7781.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
7791.40Schristos			goto out3;
7801.40Schristos
7811.40Schristos		/* XXX: We could check for the specific emulation here */
7821.40Schristos		/* All checks succeeded. */
7831.40Schristos		error = 0;
7841.40Schristos		goto out2;
7851.40Schristos	}
7861.40Schristos
7871.40Schristosout3:
7881.40Schristos	error = ENOEXEC;
7891.40Schristosout2:
7901.40Schristos	if (np)
7911.40Schristos		free(np, M_TEMP);
7921.40Schristosout1:
7931.40Schristos	free(hph, M_TEMP);
7941.40Schristos	return error;
7951.40Schristos}
7961.40Schristos
7971.40Schristosstatic int
7981.40SchristosELFNAME2(netbsd,probe)(p, epp, eh, itp, pos)
7991.40Schristos	struct proc *p;
8001.40Schristos	struct exec_package *epp;
8011.40Schristos	Elf_Ehdr *eh;
8021.40Schristos	char *itp;
8031.40Schristos	Elf_Addr *pos;
8041.40Schristos{
8051.40Schristos	int error;
8061.40Schristos
8071.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
8081.40Schristos		return error;
8091.40Schristos	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
8101.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
8111.40Schristos	return 0;
8121.1Sfvdl}
813