exec_elf32.c revision 1.46
11.46Skleink/*	$NetBSD: exec_elf32.c,v 1.46 1999/10/25 13:55:07 kleink 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.9Scgd
761.1Sfvdl#include <sys/param.h>
771.1Sfvdl#include <sys/systm.h>
781.1Sfvdl#include <sys/kernel.h>
791.1Sfvdl#include <sys/proc.h>
801.1Sfvdl#include <sys/malloc.h>
811.1Sfvdl#include <sys/namei.h>
821.1Sfvdl#include <sys/vnode.h>
831.1Sfvdl#include <sys/exec.h>
841.1Sfvdl#include <sys/exec_elf.h>
851.9Scgd#include <sys/fcntl.h>
861.8Schristos#include <sys/syscall.h>
871.8Schristos#include <sys/signalvar.h>
881.14Scgd#include <sys/mount.h>
891.14Scgd#include <sys/stat.h>
901.1Sfvdl
911.1Sfvdl#include <sys/mman.h>
921.1Sfvdl#include <vm/vm.h>
931.1Sfvdl#include <vm/vm_param.h>
941.1Sfvdl#include <vm/vm_map.h>
951.1Sfvdl
961.1Sfvdl#include <machine/cpu.h>
971.1Sfvdl#include <machine/reg.h>
981.1Sfvdl
991.1Sfvdl#ifdef COMPAT_LINUX
1001.39Schristos#include <compat/linux/common/linux_exec.h>
1011.1Sfvdl#endif
1021.1Sfvdl
1031.1Sfvdl#ifdef COMPAT_SVR4
1041.1Sfvdl#include <compat/svr4/svr4_exec.h>
1051.1Sfvdl#endif
1061.1Sfvdl
1071.27Sscottb#ifdef COMPAT_IBCS2
1081.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
1091.27Sscottb#endif
1101.27Sscottb
1111.42Schristos#ifdef COMPAT_FREEBSD
1121.42Schristos#include <compat/freebsd/freebsd_exec.h>
1131.42Schristos#endif
1141.42Schristos
1151.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
1161.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
1171.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
1181.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
1191.17Scgd	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
1201.1Sfvdl
1211.40Schristosstatic int ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *,
1221.40Schristos    Elf_Ehdr *));
1231.40Schristosstatic int ELFNAME2(netbsd,probe) __P((struct proc *, struct exec_package *,
1241.40Schristos    Elf_Ehdr *, char *, Elf_Addr *));
1251.40Schristos
1261.8Schristosextern char sigcode[], esigcode[];
1271.8Schristos#ifdef SYSCALL_DEBUG
1281.8Schristosextern char *syscallnames[];
1291.17Scgd#endif
1301.8Schristos
1311.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1321.8Schristos	"netbsd",
1331.8Schristos	NULL,
1341.8Schristos	sendsig,
1351.8Schristos	SYS_syscall,
1361.8Schristos	SYS_MAXSYSCALL,
1371.8Schristos	sysent,
1381.8Schristos#ifdef SYSCALL_DEBUG
1391.8Schristos	syscallnames,
1401.17Scgd#else
1411.8Schristos	NULL,
1421.17Scgd#endif
1431.44Scgd	howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (char *)),
1441.17Scgd	ELFNAME(copyargs),
1451.8Schristos	setregs,
1461.8Schristos	sigcode,
1471.8Schristos	esigcode,
1481.17Scgd};
1491.9Scgd
1501.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1511.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1521.40Schristos	ELFNAME2(netbsd,probe),
1531.42Schristos#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32)
1541.42Schristos	ELFNAME2(freebsd,probe),		/* XXX not 64-bit safe */
1551.42Schristos#endif
1561.38Serh#if defined(COMPAT_LINUX)
1571.38Serh	ELFNAME2(linux,probe),
1581.23Shpeyerl#endif
1591.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1601.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1611.9Scgd#endif
1621.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1631.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1641.27Sscottb#endif
1651.8Schristos};
1661.8Schristos
1671.18Scgd/* round up and down to page boundaries. */
1681.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1691.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1701.8Schristos
1711.8Schristos/*
1721.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1731.1Sfvdl * extra information in case of dynamic binding.
1741.1Sfvdl */
1751.1Sfvdlvoid *
1761.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1771.1Sfvdl	struct exec_package *pack;
1781.1Sfvdl	struct ps_strings *arginfo;
1791.1Sfvdl	void *stack;
1801.1Sfvdl	void *argp;
1811.1Sfvdl{
1821.1Sfvdl	size_t len;
1831.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1841.1Sfvdl	struct elf_args *ap;
1851.1Sfvdl
1861.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1871.4Sfvdl	if (!stack)
1881.1Sfvdl		return NULL;
1891.1Sfvdl
1901.22Scgd	a = ai;
1911.22Scgd
1921.1Sfvdl	/*
1931.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1941.1Sfvdl	 * linked binaries
1951.1Sfvdl	 */
1961.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1971.1Sfvdl
1981.46Skleink		a->a_type = AT_PHDR;
1991.46Skleink		a->a_v = ap->arg_phaddr;
2001.1Sfvdl		a++;
2011.1Sfvdl
2021.46Skleink		a->a_type = AT_PHENT;
2031.46Skleink		a->a_v = ap->arg_phentsize;
2041.1Sfvdl		a++;
2051.1Sfvdl
2061.46Skleink		a->a_type = AT_PHNUM;
2071.46Skleink		a->a_v = ap->arg_phnum;
2081.1Sfvdl		a++;
2091.1Sfvdl
2101.46Skleink		a->a_type = AT_PAGESZ;
2111.46Skleink		a->a_v = NBPG;
2121.1Sfvdl		a++;
2131.1Sfvdl
2141.46Skleink		a->a_type = AT_BASE;
2151.46Skleink		a->a_v = ap->arg_interp;
2161.1Sfvdl		a++;
2171.1Sfvdl
2181.46Skleink		a->a_type = AT_FLAGS;
2191.46Skleink		a->a_v = 0;
2201.1Sfvdl		a++;
2211.1Sfvdl
2221.46Skleink		a->a_type = AT_ENTRY;
2231.46Skleink		a->a_v = ap->arg_entry;
2241.1Sfvdl		a++;
2251.1Sfvdl
2261.17Scgd		free((char *)ap, M_TEMP);
2271.9Scgd		pack->ep_emul_arg = NULL;
2281.1Sfvdl	}
2291.22Scgd
2301.46Skleink	a->a_type = AT_NULL;
2311.46Skleink	a->a_v = 0;
2321.22Scgd	a++;
2331.22Scgd
2341.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2351.22Scgd	if (copyout(ai, stack, len))
2361.22Scgd		return NULL;
2371.43Skleink	stack = (caddr_t)stack + len;
2381.22Scgd
2391.4Sfvdl	return stack;
2401.1Sfvdl}
2411.1Sfvdl
2421.1Sfvdl/*
2431.1Sfvdl * elf_check_header():
2441.1Sfvdl *
2451.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2461.1Sfvdl */
2471.17Scgdint
2481.9ScgdELFNAME(check_header)(eh, type)
2491.9Scgd	Elf_Ehdr *eh;
2501.1Sfvdl	int type;
2511.1Sfvdl{
2521.3Sthorpej
2531.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2541.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2551.1Sfvdl
2561.1Sfvdl	switch (eh->e_machine) {
2571.9Scgd
2581.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2591.1Sfvdl
2601.1Sfvdl	default:
2611.1Sfvdl		return ENOEXEC;
2621.1Sfvdl	}
2631.1Sfvdl
2641.1Sfvdl	if (eh->e_type != type)
2651.1Sfvdl		return ENOEXEC;
2661.1Sfvdl
2671.1Sfvdl	return 0;
2681.1Sfvdl}
2691.1Sfvdl
2701.1Sfvdl/*
2711.1Sfvdl * elf_load_psection():
2721.17Scgd *
2731.1Sfvdl * Load a psection at the appropriate address
2741.1Sfvdl */
2751.17Scgdvoid
2761.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2771.1Sfvdl	struct exec_vmcmd_set *vcset;
2781.1Sfvdl	struct vnode *vp;
2791.9Scgd	Elf_Phdr *ph;
2801.9Scgd	Elf_Addr *addr;
2811.1Sfvdl	u_long *size;
2821.1Sfvdl	int *prot;
2831.1Sfvdl{
2841.8Schristos	u_long uaddr, msize, psize, rm, rf;
2851.1Sfvdl	long diff, offset;
2861.1Sfvdl
2871.1Sfvdl	/*
2881.17Scgd	 * If the user specified an address, then we load there.
2891.17Scgd	 */
2901.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2911.1Sfvdl		if (ph->p_align > 1) {
2921.18Scgd			*addr = ELF_ROUND(*addr, ph->p_align);
2931.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2941.1Sfvdl		} else
2951.1Sfvdl			uaddr = ph->p_vaddr;
2961.1Sfvdl		diff = ph->p_vaddr - uaddr;
2971.1Sfvdl	} else {
2981.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2991.1Sfvdl		if (ph->p_align > 1)
3001.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
3011.1Sfvdl		diff = uaddr - *addr;
3021.1Sfvdl	}
3031.1Sfvdl
3041.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
3051.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
3061.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
3071.1Sfvdl
3081.1Sfvdl	offset = ph->p_offset - diff;
3091.1Sfvdl	*size = ph->p_filesz + diff;
3101.1Sfvdl	msize = ph->p_memsz + diff;
3111.8Schristos	psize = round_page(*size);
3121.1Sfvdl
3131.46Skleink	if ((ph->p_flags & PF_W) != 0) {
3141.8Schristos		/*
3151.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
3161.8Schristos		 * data page if it's not page aligned we map the last page
3171.8Schristos		 * readvn.
3181.8Schristos		 */
3191.8Schristos		psize = trunc_page(*size);
3201.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3211.8Schristos		    offset, *prot);
3221.8Schristos		if(psize != *size)
3231.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
3241.8Schristos			    *addr + psize, vp, offset + psize, *prot);
3251.9Scgd	} else
3261.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3271.8Schristos		    offset, *prot);
3281.1Sfvdl
3291.1Sfvdl	/*
3301.17Scgd	 * Check if we need to extend the size of the segment
3311.17Scgd	 */
3321.1Sfvdl	rm = round_page(*addr + msize);
3331.1Sfvdl	rf = round_page(*addr + *size);
3341.1Sfvdl
3351.1Sfvdl	if (rm != rf) {
3361.17Scgd		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3371.17Scgd		    0, *prot);
3381.1Sfvdl		*size = msize;
3391.1Sfvdl	}
3401.1Sfvdl}
3411.1Sfvdl
3421.1Sfvdl/*
3431.1Sfvdl * elf_read_from():
3441.1Sfvdl *
3451.1Sfvdl *	Read from vnode into buffer at offset.
3461.1Sfvdl */
3471.7Schristosint
3481.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3491.1Sfvdl	struct vnode *vp;
3501.1Sfvdl	u_long off;
3511.1Sfvdl	struct proc *p;
3521.1Sfvdl	caddr_t buf;
3531.1Sfvdl	int size;
3541.1Sfvdl{
3551.1Sfvdl	int error;
3561.33Sthorpej	size_t resid;
3571.1Sfvdl
3581.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3591.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3601.1Sfvdl		return error;
3611.1Sfvdl	/*
3621.17Scgd	 * See if we got all of it
3631.17Scgd	 */
3641.1Sfvdl	if (resid != 0)
3651.4Sfvdl		return ENOEXEC;
3661.1Sfvdl	return 0;
3671.1Sfvdl}
3681.1Sfvdl
3691.1Sfvdl/*
3701.1Sfvdl * elf_load_file():
3711.1Sfvdl *
3721.1Sfvdl * Load a file (interpreter/library) pointed to by path
3731.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3741.1Sfvdl * so it might be used externally.
3751.1Sfvdl */
3761.17Scgdint
3771.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3781.1Sfvdl	struct proc *p;
3791.14Scgd	struct exec_package *epp;
3801.1Sfvdl	char *path;
3811.1Sfvdl	struct exec_vmcmd_set *vcset;
3821.1Sfvdl	u_long *entry;
3831.1Sfvdl	struct elf_args	*ap;
3841.9Scgd	Elf_Addr *last;
3851.1Sfvdl{
3861.1Sfvdl	int error, i;
3871.1Sfvdl	struct nameidata nd;
3881.14Scgd	struct vnode *vp;
3891.14Scgd	struct vattr attr;
3901.9Scgd	Elf_Ehdr eh;
3911.9Scgd	Elf_Phdr *ph = NULL;
3921.1Sfvdl	u_long phsize;
3931.1Sfvdl	char *bp = NULL;
3941.9Scgd	Elf_Addr addr = *last;
3951.1Sfvdl
3961.1Sfvdl	bp = path;
3971.1Sfvdl	/*
3981.17Scgd	 * 1. open file
3991.17Scgd	 * 2. read filehdr
4001.17Scgd	 * 3. map text, data, and bss out of it using VM_*
4011.17Scgd	 */
4021.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
4031.12Scgd	if ((error = namei(&nd)) != 0)
4041.1Sfvdl		return error;
4051.14Scgd	vp = nd.ni_vp;
4061.14Scgd
4071.26Smycroft	/*
4081.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
4091.26Smycroft	 * file, we don't allow it to be used.
4101.26Smycroft	 */
4111.14Scgd	if (vp->v_type != VREG) {
4121.14Scgd		error = EACCES;
4131.14Scgd		goto badunlock;
4141.14Scgd	}
4151.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
4161.26Smycroft		goto badunlock;
4171.14Scgd
4181.17Scgd	/* get attributes */
4191.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
4201.14Scgd		goto badunlock;
4211.14Scgd
4221.14Scgd	/*
4231.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
4241.15Scgd	 * we will be executing code from it, so if the mount point
4251.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
4261.14Scgd	 */
4271.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
4281.14Scgd		error = EACCES;
4291.14Scgd		goto badunlock;
4301.14Scgd	}
4311.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
4321.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
4331.14Scgd
4341.12Scgd#ifdef notyet /* XXX cgd 960926 */
4351.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
4361.12Scgd#endif
4371.28Sfvdl	VOP_UNLOCK(vp, 0);
4381.12Scgd
4391.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
4401.17Scgd	    sizeof(eh))) != 0)
4411.1Sfvdl		goto bad;
4421.1Sfvdl
4431.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
4441.1Sfvdl		goto bad;
4451.1Sfvdl
4461.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4471.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4481.1Sfvdl
4491.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4501.9Scgd	    (caddr_t) ph, phsize)) != 0)
4511.1Sfvdl		goto bad;
4521.1Sfvdl
4531.1Sfvdl	/*
4541.17Scgd	 * Load all the necessary sections
4551.17Scgd	 */
4561.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4571.1Sfvdl		u_long size = 0;
4581.1Sfvdl		int prot = 0;
4591.1Sfvdl
4601.1Sfvdl		switch (ph[i].p_type) {
4611.46Skleink		case PT_LOAD:
4621.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4631.17Scgd			    &size, &prot);
4641.4Sfvdl			/* If entry is within this section it must be text */
4651.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4661.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4671.21Sfvdl				/* XXX */
4681.21Sfvdl				*entry = addr + eh.e_entry;
4691.21Sfvdl#ifdef mips
4701.21Sfvdl				*entry -= ph[i].p_vaddr;
4711.21Sfvdl#endif
4721.1Sfvdl				ap->arg_interp = addr;
4731.1Sfvdl			}
4741.1Sfvdl			addr += size;
4751.1Sfvdl			break;
4761.1Sfvdl
4771.46Skleink		case PT_DYNAMIC:
4781.46Skleink		case PT_PHDR:
4791.46Skleink		case PT_NOTE:
4801.1Sfvdl			break;
4811.1Sfvdl
4821.1Sfvdl		default:
4831.1Sfvdl			break;
4841.1Sfvdl		}
4851.1Sfvdl	}
4861.1Sfvdl
4871.17Scgd	free((char *)ph, M_TEMP);
4881.12Scgd	*last = addr;
4891.14Scgd	vrele(vp);
4901.12Scgd	return 0;
4911.12Scgd
4921.14Scgdbadunlock:
4931.28Sfvdl	VOP_UNLOCK(vp, 0);
4941.14Scgd
4951.1Sfvdlbad:
4961.1Sfvdl	if (ph != NULL)
4971.17Scgd		free((char *)ph, M_TEMP);
4981.12Scgd#ifdef notyet /* XXX cgd 960926 */
4991.12Scgd	(maybe) VOP_CLOSE it
5001.12Scgd#endif
5011.14Scgd	vrele(vp);
5021.1Sfvdl	return error;
5031.1Sfvdl}
5041.1Sfvdl
5051.1Sfvdl/*
5061.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
5071.1Sfvdl *
5081.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5091.1Sfvdl *
5101.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5111.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5121.1Sfvdl * text, data, bss, and stack segments.
5131.1Sfvdl */
5141.1Sfvdlint
5151.9ScgdELFNAME2(exec,makecmds)(p, epp)
5161.1Sfvdl	struct proc *p;
5171.1Sfvdl	struct exec_package *epp;
5181.1Sfvdl{
5191.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
5201.9Scgd	Elf_Phdr *ph, *pp;
5211.9Scgd	Elf_Addr phdr = 0, pos = 0;
5221.4Sfvdl	int error, i, n, nload;
5231.1Sfvdl	char interp[MAXPATHLEN];
5241.9Scgd	u_long phsize;
5251.1Sfvdl
5261.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
5271.1Sfvdl		return ENOEXEC;
5281.1Sfvdl
5291.45Sfvdl	/*
5301.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
5311.45Sfvdl	 * but other ELF-based systems allow it as well.
5321.45Sfvdl	 */
5331.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
5341.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
5351.1Sfvdl		return ENOEXEC;
5361.1Sfvdl
5371.1Sfvdl	/*
5381.17Scgd	 * check if vnode is in open for writing, because we want to
5391.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5401.17Scgd	 * reasons
5411.17Scgd	 */
5421.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5431.1Sfvdl#ifdef DIAGNOSTIC
5441.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5451.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5461.1Sfvdl#endif
5471.1Sfvdl		return ETXTBSY;
5481.1Sfvdl	}
5491.1Sfvdl	/*
5501.17Scgd	 * Allocate space to hold all the program headers, and read them
5511.17Scgd	 * from the file
5521.17Scgd	 */
5531.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5541.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5551.1Sfvdl
5561.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5571.8Schristos	    (caddr_t) ph, phsize)) != 0)
5581.1Sfvdl		goto bad;
5591.1Sfvdl
5601.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5611.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5621.1Sfvdl
5631.1Sfvdl	interp[0] = '\0';
5641.1Sfvdl
5651.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5661.1Sfvdl		pp = &ph[i];
5671.46Skleink		if (pp->p_type == PT_INTERP) {
5681.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5691.1Sfvdl				goto bad;
5701.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5711.17Scgd			    pp->p_offset, (caddr_t) interp,
5721.17Scgd			    pp->p_filesz)) != 0)
5731.1Sfvdl				goto bad;
5741.1Sfvdl			break;
5751.1Sfvdl		}
5761.1Sfvdl	}
5771.1Sfvdl
5781.9Scgd	/*
5791.8Schristos	 * Setup things for native emulation.
5801.8Schristos	 */
5811.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5821.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5831.8Schristos
5841.1Sfvdl	/*
5851.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5861.1Sfvdl	 * See which one will accept this executable. This currently only
5871.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5881.38Serh	 * and the Alpha.
5891.1Sfvdl	 *
5901.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5911.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5921.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5931.1Sfvdl	 * set the ep_emul field in the exec package structure.
5941.1Sfvdl	 */
5951.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
5961.17Scgd	if (n != 0) {
5971.1Sfvdl		error = ENOEXEC;
5981.1Sfvdl		for (i = 0; i < n && error; i++)
5991.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
6001.17Scgd			    interp, &pos);
6011.8Schristos#ifdef notyet
6021.8Schristos		/*
6031.8Schristos		 * We should really use a signature in our native binaries
6041.8Schristos		 * and have our own probe function for matching binaries,
6051.8Schristos		 * before trying the emulations. For now, if the emulation
6061.8Schristos		 * probes failed we default to native.
6071.8Schristos		 */
6081.1Sfvdl		if (error)
6091.1Sfvdl			goto bad;
6101.8Schristos#endif
6111.1Sfvdl	}
6121.1Sfvdl
6131.1Sfvdl	/*
6141.17Scgd	 * Load all the necessary sections
6151.17Scgd	 */
6161.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
6171.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
6181.9Scgd		u_long size = 0;
6191.1Sfvdl		int prot = 0;
6201.1Sfvdl
6211.1Sfvdl		pp = &ph[i];
6221.1Sfvdl
6231.1Sfvdl		switch (ph[i].p_type) {
6241.46Skleink		case PT_LOAD:
6251.4Sfvdl			/*
6261.4Sfvdl			 * XXX
6271.4Sfvdl			 * Can handle only 2 sections: text and data
6281.4Sfvdl			 */
6291.4Sfvdl			if (nload++ == 2)
6301.4Sfvdl				goto bad;
6311.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6321.17Scgd			    &ph[i], &addr, &size, &prot);
6331.17Scgd
6341.4Sfvdl			/*
6351.4Sfvdl			 * Decide whether it's text or data by looking
6361.4Sfvdl			 * at the entry point.
6371.4Sfvdl			 */
6381.19Scgd			if (eh->e_entry >= addr &&
6391.19Scgd			    eh->e_entry < (addr + size)) {
6401.4Sfvdl				epp->ep_taddr = addr;
6411.4Sfvdl				epp->ep_tsize = size;
6421.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6431.19Scgd					epp->ep_daddr = addr;
6441.19Scgd					epp->ep_dsize = size;
6451.19Scgd				}
6461.4Sfvdl			} else {
6471.4Sfvdl				epp->ep_daddr = addr;
6481.4Sfvdl				epp->ep_dsize = size;
6491.4Sfvdl			}
6501.1Sfvdl			break;
6511.1Sfvdl
6521.46Skleink		case PT_SHLIB:
6531.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6541.1Sfvdl			error = ENOEXEC;
6551.1Sfvdl			goto bad;
6561.27Sscottb#endif
6571.1Sfvdl
6581.46Skleink		case PT_INTERP:
6591.1Sfvdl			/* Already did this one */
6601.46Skleink		case PT_DYNAMIC:
6611.46Skleink		case PT_NOTE:
6621.1Sfvdl			break;
6631.1Sfvdl
6641.46Skleink		case PT_PHDR:
6651.4Sfvdl			/* Note address of program headers (in text segment) */
6661.4Sfvdl			phdr = pp->p_vaddr;
6671.7Schristos			break;
6681.4Sfvdl
6691.1Sfvdl		default:
6701.1Sfvdl			/*
6711.9Scgd			 * Not fatal; we don't need to understand everything.
6721.1Sfvdl			 */
6731.1Sfvdl			break;
6741.1Sfvdl		}
6751.1Sfvdl	}
6761.5Sfvdl
6771.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6781.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6791.5Sfvdl	/*
6801.5Sfvdl	 * If no position to load the interpreter was set by a probe
6811.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6821.5Sfvdl	 * would (i.e. something safely out of the way).
6831.5Sfvdl	 */
6841.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6851.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6861.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6871.1Sfvdl
6881.1Sfvdl	/*
6891.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6901.17Scgd	 * it's interpreter
6911.17Scgd	 */
6921.1Sfvdl	if (interp[0]) {
6931.1Sfvdl		struct elf_args *ap;
6941.1Sfvdl
6951.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6961.17Scgd		    M_TEMP, M_WAITOK);
6971.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6981.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6991.17Scgd			free((char *)ap, M_TEMP);
7001.1Sfvdl			goto bad;
7011.1Sfvdl		}
7021.1Sfvdl		pos += phsize;
7031.4Sfvdl		ap->arg_phaddr = phdr;
7041.1Sfvdl
7051.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
7061.1Sfvdl		ap->arg_phnum = eh->e_phnum;
7071.1Sfvdl		ap->arg_entry = eh->e_entry;
7081.1Sfvdl
7091.1Sfvdl		epp->ep_emul_arg = ap;
7101.1Sfvdl	} else
7111.1Sfvdl		epp->ep_entry = eh->e_entry;
7121.1Sfvdl
7131.8Schristos#ifdef ELF_MAP_PAGE_ZERO
7141.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
7151.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
7161.8Schristos	    VM_PROT_READ);
7171.8Schristos#endif
7181.17Scgd	free((char *)ph, M_TEMP);
7191.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
7201.9Scgd	return exec_elf_setup_stack(p, epp);
7211.1Sfvdl
7221.1Sfvdlbad:
7231.17Scgd	free((char *)ph, M_TEMP);
7241.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7251.1Sfvdl	return ENOEXEC;
7261.40Schristos}
7271.40Schristos
7281.40Schristosstatic int
7291.40SchristosELFNAME2(netbsd,signature)(p, epp, eh)
7301.40Schristos	struct proc *p;
7311.40Schristos	struct exec_package *epp;
7321.40Schristos	Elf_Ehdr *eh;
7331.40Schristos{
7341.40Schristos	Elf_Phdr *hph, *ph;
7351.46Skleink	Elf_Nhdr *np = NULL;
7361.40Schristos	size_t phsize;
7371.40Schristos	int error;
7381.40Schristos
7391.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
7401.40Schristos	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
7411.41Schristos	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
7421.41Schristos	    (caddr_t)hph, phsize)) != 0)
7431.40Schristos		goto out1;
7441.40Schristos
7451.40Schristos	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
7461.46Skleink		if (ph->p_type != PT_NOTE ||
7471.46Skleink		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
7481.40Schristos			continue;
7491.40Schristos
7501.46Skleink		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
7511.40Schristos		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
7521.40Schristos		    (caddr_t)np, ph->p_filesz)) != 0)
7531.40Schristos			goto out2;
7541.40Schristos
7551.46Skleink		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
7561.40Schristos			free(np, M_TEMP);
7571.40Schristos			np = NULL;
7581.40Schristos			continue;
7591.40Schristos		}
7601.40Schristos
7611.40Schristos		/* Check the name and description sizes. */
7621.46Skleink		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
7631.46Skleink		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
7641.40Schristos			goto out3;
7651.40Schristos
7661.40Schristos		/* Is the name "NetBSD\0\0"? */
7671.40Schristos		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
7681.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
7691.40Schristos			goto out3;
7701.40Schristos
7711.40Schristos		/* XXX: We could check for the specific emulation here */
7721.40Schristos		/* All checks succeeded. */
7731.40Schristos		error = 0;
7741.40Schristos		goto out2;
7751.40Schristos	}
7761.40Schristos
7771.40Schristosout3:
7781.40Schristos	error = ENOEXEC;
7791.40Schristosout2:
7801.40Schristos	if (np)
7811.40Schristos		free(np, M_TEMP);
7821.40Schristosout1:
7831.40Schristos	free(hph, M_TEMP);
7841.40Schristos	return error;
7851.40Schristos}
7861.40Schristos
7871.40Schristosstatic int
7881.40SchristosELFNAME2(netbsd,probe)(p, epp, eh, itp, pos)
7891.40Schristos	struct proc *p;
7901.40Schristos	struct exec_package *epp;
7911.40Schristos	Elf_Ehdr *eh;
7921.40Schristos	char *itp;
7931.40Schristos	Elf_Addr *pos;
7941.40Schristos{
7951.40Schristos	int error;
7961.40Schristos
7971.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
7981.40Schristos		return error;
7991.40Schristos
8001.40Schristos	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
8011.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
8021.40Schristos	return 0;
8031.1Sfvdl}
804