exec_elf32.c revision 1.36
11.36Schristos/*	$NetBSD: exec_elf32.c,v 1.36 1998/09/05 14:35:17 christos 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.36Schristos *        This product includes software developed by the NetBSD
211.36Schristos *        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.9Scgd
751.1Sfvdl#include <sys/param.h>
761.1Sfvdl#include <sys/systm.h>
771.1Sfvdl#include <sys/kernel.h>
781.1Sfvdl#include <sys/proc.h>
791.1Sfvdl#include <sys/malloc.h>
801.1Sfvdl#include <sys/namei.h>
811.1Sfvdl#include <sys/vnode.h>
821.1Sfvdl#include <sys/exec.h>
831.1Sfvdl#include <sys/exec_elf.h>
841.9Scgd#include <sys/fcntl.h>
851.8Schristos#include <sys/syscall.h>
861.8Schristos#include <sys/signalvar.h>
871.14Scgd#include <sys/mount.h>
881.14Scgd#include <sys/stat.h>
891.1Sfvdl
901.1Sfvdl#include <sys/mman.h>
911.1Sfvdl#include <vm/vm.h>
921.1Sfvdl#include <vm/vm_param.h>
931.1Sfvdl#include <vm/vm_map.h>
941.1Sfvdl
951.1Sfvdl#include <machine/cpu.h>
961.1Sfvdl#include <machine/reg.h>
971.1Sfvdl
981.1Sfvdl#ifdef COMPAT_LINUX
991.1Sfvdl#include <compat/linux/linux_exec.h>
1001.1Sfvdl#endif
1011.1Sfvdl
1021.1Sfvdl#ifdef COMPAT_SVR4
1031.1Sfvdl#include <compat/svr4/svr4_exec.h>
1041.1Sfvdl#endif
1051.1Sfvdl
1061.27Sscottb#ifdef COMPAT_IBCS2
1071.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
1081.27Sscottb#endif
1091.27Sscottb
1101.9Scgd#define	CONCAT(x,y)	__CONCAT(x,y)
1111.9Scgd#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
1121.9Scgd#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
1131.9Scgd#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
1141.9Scgd#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
1151.9Scgd
1161.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
1171.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
1181.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
1191.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
1201.17Scgd	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
1211.1Sfvdl
1221.8Schristosextern char sigcode[], esigcode[];
1231.8Schristos#ifdef SYSCALL_DEBUG
1241.8Schristosextern char *syscallnames[];
1251.17Scgd#endif
1261.8Schristos
1271.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1281.8Schristos	"netbsd",
1291.8Schristos	NULL,
1301.8Schristos	sendsig,
1311.8Schristos	SYS_syscall,
1321.8Schristos	SYS_MAXSYSCALL,
1331.8Schristos	sysent,
1341.8Schristos#ifdef SYSCALL_DEBUG
1351.8Schristos	syscallnames,
1361.17Scgd#else
1371.8Schristos	NULL,
1381.17Scgd#endif
1391.9Scgd	ELF_AUX_ENTRIES * sizeof(AuxInfo),
1401.17Scgd	ELFNAME(copyargs),
1411.8Schristos	setregs,
1421.8Schristos	sigcode,
1431.8Schristos	esigcode,
1441.17Scgd};
1451.9Scgd
1461.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1471.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1481.23Shpeyerl#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
1491.23Shpeyerl	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
1501.23Shpeyerl#endif
1511.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1521.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1531.9Scgd#endif
1541.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1551.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1561.27Sscottb#endif
1571.8Schristos};
1581.8Schristos
1591.18Scgd/* round up and down to page boundaries. */
1601.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1611.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1621.8Schristos
1631.8Schristos/*
1641.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1651.1Sfvdl * extra information in case of dynamic binding.
1661.1Sfvdl */
1671.1Sfvdlvoid *
1681.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1691.1Sfvdl	struct exec_package *pack;
1701.1Sfvdl	struct ps_strings *arginfo;
1711.1Sfvdl	void *stack;
1721.1Sfvdl	void *argp;
1731.1Sfvdl{
1741.1Sfvdl	size_t len;
1751.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1761.1Sfvdl	struct elf_args *ap;
1771.1Sfvdl
1781.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1791.4Sfvdl	if (!stack)
1801.1Sfvdl		return NULL;
1811.1Sfvdl
1821.22Scgd	a = ai;
1831.22Scgd
1841.1Sfvdl	/*
1851.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1861.1Sfvdl	 * linked binaries
1871.1Sfvdl	 */
1881.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1891.1Sfvdl
1901.1Sfvdl		a->au_id = AUX_phdr;
1911.1Sfvdl		a->au_v = ap->arg_phaddr;
1921.1Sfvdl		a++;
1931.1Sfvdl
1941.1Sfvdl		a->au_id = AUX_phent;
1951.1Sfvdl		a->au_v = ap->arg_phentsize;
1961.1Sfvdl		a++;
1971.1Sfvdl
1981.1Sfvdl		a->au_id = AUX_phnum;
1991.1Sfvdl		a->au_v = ap->arg_phnum;
2001.1Sfvdl		a++;
2011.1Sfvdl
2021.1Sfvdl		a->au_id = AUX_pagesz;
2031.1Sfvdl		a->au_v = NBPG;
2041.1Sfvdl		a++;
2051.1Sfvdl
2061.1Sfvdl		a->au_id = AUX_base;
2071.1Sfvdl		a->au_v = ap->arg_interp;
2081.1Sfvdl		a++;
2091.1Sfvdl
2101.1Sfvdl		a->au_id = AUX_flags;
2111.1Sfvdl		a->au_v = 0;
2121.1Sfvdl		a++;
2131.1Sfvdl
2141.1Sfvdl		a->au_id = AUX_entry;
2151.1Sfvdl		a->au_v = ap->arg_entry;
2161.1Sfvdl		a++;
2171.1Sfvdl
2181.17Scgd		free((char *)ap, M_TEMP);
2191.9Scgd		pack->ep_emul_arg = NULL;
2201.1Sfvdl	}
2211.22Scgd
2221.22Scgd	a->au_id = AUX_null;
2231.22Scgd	a->au_v = 0;
2241.22Scgd	a++;
2251.22Scgd
2261.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2271.22Scgd	if (copyout(ai, stack, len))
2281.22Scgd		return NULL;
2291.29Skleink	(caddr_t)stack += len;
2301.22Scgd
2311.4Sfvdl	return stack;
2321.1Sfvdl}
2331.1Sfvdl
2341.1Sfvdl/*
2351.1Sfvdl * elf_check_header():
2361.1Sfvdl *
2371.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2381.1Sfvdl */
2391.17Scgdint
2401.9ScgdELFNAME(check_header)(eh, type)
2411.9Scgd	Elf_Ehdr *eh;
2421.1Sfvdl	int type;
2431.1Sfvdl{
2441.3Sthorpej
2451.35Sperry	if (memcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
2461.1Sfvdl		return ENOEXEC;
2471.1Sfvdl
2481.1Sfvdl	switch (eh->e_machine) {
2491.9Scgd
2501.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2511.1Sfvdl
2521.1Sfvdl	default:
2531.1Sfvdl		return ENOEXEC;
2541.1Sfvdl	}
2551.1Sfvdl
2561.1Sfvdl	if (eh->e_type != type)
2571.1Sfvdl		return ENOEXEC;
2581.1Sfvdl
2591.1Sfvdl	return 0;
2601.1Sfvdl}
2611.1Sfvdl
2621.1Sfvdl/*
2631.1Sfvdl * elf_load_psection():
2641.17Scgd *
2651.1Sfvdl * Load a psection at the appropriate address
2661.1Sfvdl */
2671.17Scgdvoid
2681.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2691.1Sfvdl	struct exec_vmcmd_set *vcset;
2701.1Sfvdl	struct vnode *vp;
2711.9Scgd	Elf_Phdr *ph;
2721.9Scgd	Elf_Addr *addr;
2731.1Sfvdl	u_long *size;
2741.1Sfvdl	int *prot;
2751.1Sfvdl{
2761.8Schristos	u_long uaddr, msize, psize, rm, rf;
2771.1Sfvdl	long diff, offset;
2781.1Sfvdl
2791.1Sfvdl	/*
2801.17Scgd	 * If the user specified an address, then we load there.
2811.17Scgd	 */
2821.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2831.1Sfvdl		if (ph->p_align > 1) {
2841.18Scgd			*addr = ELF_ROUND(*addr, ph->p_align);
2851.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2861.1Sfvdl		} else
2871.1Sfvdl			uaddr = ph->p_vaddr;
2881.1Sfvdl		diff = ph->p_vaddr - uaddr;
2891.1Sfvdl	} else {
2901.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2911.1Sfvdl		if (ph->p_align > 1)
2921.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2931.1Sfvdl		diff = uaddr - *addr;
2941.1Sfvdl	}
2951.1Sfvdl
2961.9Scgd	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
2971.9Scgd	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
2981.9Scgd	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
2991.1Sfvdl
3001.1Sfvdl	offset = ph->p_offset - diff;
3011.1Sfvdl	*size = ph->p_filesz + diff;
3021.1Sfvdl	msize = ph->p_memsz + diff;
3031.8Schristos	psize = round_page(*size);
3041.1Sfvdl
3051.9Scgd	if ((ph->p_flags & Elf_pf_w) != 0) {
3061.8Schristos		/*
3071.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
3081.8Schristos		 * data page if it's not page aligned we map the last page
3091.8Schristos		 * readvn.
3101.8Schristos		 */
3111.8Schristos		psize = trunc_page(*size);
3121.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3131.8Schristos		    offset, *prot);
3141.8Schristos		if(psize != *size)
3151.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
3161.8Schristos			    *addr + psize, vp, offset + psize, *prot);
3171.9Scgd	} else
3181.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3191.8Schristos		    offset, *prot);
3201.1Sfvdl
3211.1Sfvdl	/*
3221.17Scgd	 * Check if we need to extend the size of the segment
3231.17Scgd	 */
3241.1Sfvdl	rm = round_page(*addr + msize);
3251.1Sfvdl	rf = round_page(*addr + *size);
3261.1Sfvdl
3271.1Sfvdl	if (rm != rf) {
3281.17Scgd		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3291.17Scgd		    0, *prot);
3301.1Sfvdl		*size = msize;
3311.1Sfvdl	}
3321.1Sfvdl}
3331.1Sfvdl
3341.1Sfvdl/*
3351.1Sfvdl * elf_read_from():
3361.1Sfvdl *
3371.1Sfvdl *	Read from vnode into buffer at offset.
3381.1Sfvdl */
3391.7Schristosint
3401.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3411.1Sfvdl	struct vnode *vp;
3421.1Sfvdl	u_long off;
3431.1Sfvdl	struct proc *p;
3441.1Sfvdl	caddr_t buf;
3451.1Sfvdl	int size;
3461.1Sfvdl{
3471.1Sfvdl	int error;
3481.33Sthorpej	size_t resid;
3491.1Sfvdl
3501.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3511.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3521.1Sfvdl		return error;
3531.1Sfvdl	/*
3541.17Scgd	 * See if we got all of it
3551.17Scgd	 */
3561.1Sfvdl	if (resid != 0)
3571.4Sfvdl		return ENOEXEC;
3581.1Sfvdl	return 0;
3591.1Sfvdl}
3601.1Sfvdl
3611.1Sfvdl/*
3621.1Sfvdl * elf_load_file():
3631.1Sfvdl *
3641.1Sfvdl * Load a file (interpreter/library) pointed to by path
3651.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3661.1Sfvdl * so it might be used externally.
3671.1Sfvdl */
3681.17Scgdint
3691.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3701.1Sfvdl	struct proc *p;
3711.14Scgd	struct exec_package *epp;
3721.1Sfvdl	char *path;
3731.1Sfvdl	struct exec_vmcmd_set *vcset;
3741.1Sfvdl	u_long *entry;
3751.1Sfvdl	struct elf_args	*ap;
3761.9Scgd	Elf_Addr *last;
3771.1Sfvdl{
3781.1Sfvdl	int error, i;
3791.1Sfvdl	struct nameidata nd;
3801.14Scgd	struct vnode *vp;
3811.14Scgd	struct vattr attr;
3821.9Scgd	Elf_Ehdr eh;
3831.9Scgd	Elf_Phdr *ph = NULL;
3841.1Sfvdl	u_long phsize;
3851.1Sfvdl	char *bp = NULL;
3861.9Scgd	Elf_Addr addr = *last;
3871.1Sfvdl
3881.1Sfvdl	bp = path;
3891.1Sfvdl	/*
3901.17Scgd	 * 1. open file
3911.17Scgd	 * 2. read filehdr
3921.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3931.17Scgd	 */
3941.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3951.12Scgd	if ((error = namei(&nd)) != 0)
3961.1Sfvdl		return error;
3971.14Scgd	vp = nd.ni_vp;
3981.14Scgd
3991.26Smycroft	/*
4001.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
4011.26Smycroft	 * file, we don't allow it to be used.
4021.26Smycroft	 */
4031.14Scgd	if (vp->v_type != VREG) {
4041.14Scgd		error = EACCES;
4051.14Scgd		goto badunlock;
4061.14Scgd	}
4071.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
4081.26Smycroft		goto badunlock;
4091.14Scgd
4101.17Scgd	/* get attributes */
4111.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
4121.14Scgd		goto badunlock;
4131.14Scgd
4141.14Scgd	/*
4151.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
4161.15Scgd	 * we will be executing code from it, so if the mount point
4171.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
4181.14Scgd	 */
4191.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
4201.14Scgd		error = EACCES;
4211.14Scgd		goto badunlock;
4221.14Scgd	}
4231.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
4241.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
4251.14Scgd
4261.12Scgd#ifdef notyet /* XXX cgd 960926 */
4271.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
4281.12Scgd#endif
4291.28Sfvdl	VOP_UNLOCK(vp, 0);
4301.12Scgd
4311.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
4321.17Scgd	    sizeof(eh))) != 0)
4331.1Sfvdl		goto bad;
4341.1Sfvdl
4351.9Scgd	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
4361.1Sfvdl		goto bad;
4371.1Sfvdl
4381.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4391.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4401.1Sfvdl
4411.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4421.9Scgd	    (caddr_t) ph, phsize)) != 0)
4431.1Sfvdl		goto bad;
4441.1Sfvdl
4451.1Sfvdl	/*
4461.17Scgd	 * Load all the necessary sections
4471.17Scgd	 */
4481.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4491.1Sfvdl		u_long size = 0;
4501.1Sfvdl		int prot = 0;
4511.1Sfvdl
4521.1Sfvdl		switch (ph[i].p_type) {
4531.9Scgd		case Elf_pt_load:
4541.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4551.17Scgd			    &size, &prot);
4561.4Sfvdl			/* If entry is within this section it must be text */
4571.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4581.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4591.21Sfvdl				/* XXX */
4601.21Sfvdl				*entry = addr + eh.e_entry;
4611.21Sfvdl#ifdef mips
4621.21Sfvdl				*entry -= ph[i].p_vaddr;
4631.21Sfvdl#endif
4641.1Sfvdl				ap->arg_interp = addr;
4651.1Sfvdl			}
4661.1Sfvdl			addr += size;
4671.1Sfvdl			break;
4681.1Sfvdl
4691.9Scgd		case Elf_pt_dynamic:
4701.9Scgd		case Elf_pt_phdr:
4711.9Scgd		case Elf_pt_note:
4721.1Sfvdl			break;
4731.1Sfvdl
4741.1Sfvdl		default:
4751.1Sfvdl			break;
4761.1Sfvdl		}
4771.1Sfvdl	}
4781.1Sfvdl
4791.17Scgd	free((char *)ph, M_TEMP);
4801.12Scgd	*last = addr;
4811.14Scgd	vrele(vp);
4821.12Scgd	return 0;
4831.12Scgd
4841.14Scgdbadunlock:
4851.28Sfvdl	VOP_UNLOCK(vp, 0);
4861.14Scgd
4871.1Sfvdlbad:
4881.1Sfvdl	if (ph != NULL)
4891.17Scgd		free((char *)ph, M_TEMP);
4901.12Scgd#ifdef notyet /* XXX cgd 960926 */
4911.12Scgd	(maybe) VOP_CLOSE it
4921.12Scgd#endif
4931.14Scgd	vrele(vp);
4941.1Sfvdl	return error;
4951.1Sfvdl}
4961.1Sfvdl
4971.1Sfvdl/*
4981.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4991.1Sfvdl *
5001.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5011.1Sfvdl *
5021.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5031.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5041.1Sfvdl * text, data, bss, and stack segments.
5051.1Sfvdl */
5061.1Sfvdlint
5071.9ScgdELFNAME2(exec,makecmds)(p, epp)
5081.1Sfvdl	struct proc *p;
5091.1Sfvdl	struct exec_package *epp;
5101.1Sfvdl{
5111.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
5121.9Scgd	Elf_Phdr *ph, *pp;
5131.9Scgd	Elf_Addr phdr = 0, pos = 0;
5141.4Sfvdl	int error, i, n, nload;
5151.1Sfvdl	char interp[MAXPATHLEN];
5161.9Scgd	u_long phsize;
5171.1Sfvdl
5181.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
5191.1Sfvdl		return ENOEXEC;
5201.1Sfvdl
5211.9Scgd	if (ELFNAME(check_header)(eh, Elf_et_exec))
5221.1Sfvdl		return ENOEXEC;
5231.1Sfvdl
5241.1Sfvdl	/*
5251.17Scgd	 * check if vnode is in open for writing, because we want to
5261.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5271.17Scgd	 * reasons
5281.17Scgd	 */
5291.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5301.1Sfvdl#ifdef DIAGNOSTIC
5311.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5321.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5331.1Sfvdl#endif
5341.1Sfvdl		return ETXTBSY;
5351.1Sfvdl	}
5361.1Sfvdl	/*
5371.17Scgd	 * Allocate space to hold all the program headers, and read them
5381.17Scgd	 * from the file
5391.17Scgd	 */
5401.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5411.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5421.1Sfvdl
5431.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5441.8Schristos	    (caddr_t) ph, phsize)) != 0)
5451.1Sfvdl		goto bad;
5461.1Sfvdl
5471.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5481.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5491.1Sfvdl
5501.1Sfvdl	interp[0] = '\0';
5511.1Sfvdl
5521.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5531.1Sfvdl		pp = &ph[i];
5541.9Scgd		if (pp->p_type == Elf_pt_interp) {
5551.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5561.1Sfvdl				goto bad;
5571.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5581.17Scgd			    pp->p_offset, (caddr_t) interp,
5591.17Scgd			    pp->p_filesz)) != 0)
5601.1Sfvdl				goto bad;
5611.1Sfvdl			break;
5621.1Sfvdl		}
5631.1Sfvdl	}
5641.1Sfvdl
5651.9Scgd	/*
5661.8Schristos	 * Setup things for native emulation.
5671.8Schristos	 */
5681.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5691.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5701.8Schristos
5711.1Sfvdl	/*
5721.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5731.1Sfvdl	 * See which one will accept this executable. This currently only
5741.27Sscottb	 * applies to Linux, SVR4, and IBCS2 on the i386.
5751.1Sfvdl	 *
5761.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5771.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5781.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5791.1Sfvdl	 * set the ep_emul field in the exec package structure.
5801.1Sfvdl	 */
5811.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
5821.17Scgd	if (n != 0) {
5831.1Sfvdl		error = ENOEXEC;
5841.1Sfvdl		for (i = 0; i < n && error; i++)
5851.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
5861.17Scgd			    interp, &pos);
5871.1Sfvdl
5881.8Schristos#ifdef notyet
5891.8Schristos		/*
5901.8Schristos		 * We should really use a signature in our native binaries
5911.8Schristos		 * and have our own probe function for matching binaries,
5921.8Schristos		 * before trying the emulations. For now, if the emulation
5931.8Schristos		 * probes failed we default to native.
5941.8Schristos		 */
5951.1Sfvdl		if (error)
5961.1Sfvdl			goto bad;
5971.8Schristos#endif
5981.1Sfvdl	}
5991.1Sfvdl
6001.1Sfvdl	/*
6011.17Scgd	 * Load all the necessary sections
6021.17Scgd	 */
6031.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
6041.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
6051.9Scgd		u_long size = 0;
6061.1Sfvdl		int prot = 0;
6071.1Sfvdl
6081.1Sfvdl		pp = &ph[i];
6091.1Sfvdl
6101.1Sfvdl		switch (ph[i].p_type) {
6111.9Scgd		case Elf_pt_load:
6121.4Sfvdl			/*
6131.4Sfvdl			 * XXX
6141.4Sfvdl			 * Can handle only 2 sections: text and data
6151.4Sfvdl			 */
6161.4Sfvdl			if (nload++ == 2)
6171.4Sfvdl				goto bad;
6181.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6191.17Scgd			    &ph[i], &addr, &size, &prot);
6201.17Scgd
6211.4Sfvdl			/*
6221.4Sfvdl			 * Decide whether it's text or data by looking
6231.4Sfvdl			 * at the entry point.
6241.4Sfvdl			 */
6251.19Scgd			if (eh->e_entry >= addr &&
6261.19Scgd			    eh->e_entry < (addr + size)) {
6271.4Sfvdl				epp->ep_taddr = addr;
6281.4Sfvdl				epp->ep_tsize = size;
6291.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6301.19Scgd					epp->ep_daddr = addr;
6311.19Scgd					epp->ep_dsize = size;
6321.19Scgd				}
6331.4Sfvdl			} else {
6341.4Sfvdl				epp->ep_daddr = addr;
6351.4Sfvdl				epp->ep_dsize = size;
6361.4Sfvdl			}
6371.1Sfvdl			break;
6381.1Sfvdl
6391.9Scgd		case Elf_pt_shlib:
6401.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6411.1Sfvdl			error = ENOEXEC;
6421.1Sfvdl			goto bad;
6431.27Sscottb#endif
6441.1Sfvdl
6451.9Scgd		case Elf_pt_interp:
6461.1Sfvdl			/* Already did this one */
6471.9Scgd		case Elf_pt_dynamic:
6481.9Scgd		case Elf_pt_note:
6491.1Sfvdl			break;
6501.1Sfvdl
6511.9Scgd		case Elf_pt_phdr:
6521.4Sfvdl			/* Note address of program headers (in text segment) */
6531.4Sfvdl			phdr = pp->p_vaddr;
6541.7Schristos			break;
6551.4Sfvdl
6561.1Sfvdl		default:
6571.1Sfvdl			/*
6581.9Scgd			 * Not fatal; we don't need to understand everything.
6591.1Sfvdl			 */
6601.1Sfvdl			break;
6611.1Sfvdl		}
6621.1Sfvdl	}
6631.5Sfvdl
6641.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6651.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6661.5Sfvdl	/*
6671.5Sfvdl	 * If no position to load the interpreter was set by a probe
6681.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6691.5Sfvdl	 * would (i.e. something safely out of the way).
6701.5Sfvdl	 */
6711.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6721.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6731.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6741.1Sfvdl
6751.1Sfvdl	/*
6761.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6771.17Scgd	 * it's interpreter
6781.17Scgd	 */
6791.1Sfvdl	if (interp[0]) {
6801.1Sfvdl		struct elf_args *ap;
6811.1Sfvdl
6821.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6831.17Scgd		    M_TEMP, M_WAITOK);
6841.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6851.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6861.17Scgd			free((char *)ap, M_TEMP);
6871.1Sfvdl			goto bad;
6881.1Sfvdl		}
6891.1Sfvdl		pos += phsize;
6901.4Sfvdl		ap->arg_phaddr = phdr;
6911.1Sfvdl
6921.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6931.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6941.1Sfvdl		ap->arg_entry = eh->e_entry;
6951.1Sfvdl
6961.1Sfvdl		epp->ep_emul_arg = ap;
6971.1Sfvdl	} else
6981.1Sfvdl		epp->ep_entry = eh->e_entry;
6991.1Sfvdl
7001.8Schristos#ifdef ELF_MAP_PAGE_ZERO
7011.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
7021.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
7031.8Schristos	    VM_PROT_READ);
7041.8Schristos#endif
7051.17Scgd	free((char *)ph, M_TEMP);
7061.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
7071.9Scgd	return exec_elf_setup_stack(p, epp);
7081.1Sfvdl
7091.1Sfvdlbad:
7101.17Scgd	free((char *)ph, M_TEMP);
7111.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7121.1Sfvdl	return ENOEXEC;
7131.1Sfvdl}
714