exec_elf32.c revision 1.39
11.39Schristos/*	$NetBSD: exec_elf32.c,v 1.39 1998/10/03 20:39:32 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.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.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.39Schristos#include <compat/linux/common/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.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
1111.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
1121.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
1131.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
1141.17Scgd	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
1151.1Sfvdl
1161.8Schristosextern char sigcode[], esigcode[];
1171.8Schristos#ifdef SYSCALL_DEBUG
1181.8Schristosextern char *syscallnames[];
1191.17Scgd#endif
1201.8Schristos
1211.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1221.8Schristos	"netbsd",
1231.8Schristos	NULL,
1241.8Schristos	sendsig,
1251.8Schristos	SYS_syscall,
1261.8Schristos	SYS_MAXSYSCALL,
1271.8Schristos	sysent,
1281.8Schristos#ifdef SYSCALL_DEBUG
1291.8Schristos	syscallnames,
1301.17Scgd#else
1311.8Schristos	NULL,
1321.17Scgd#endif
1331.9Scgd	ELF_AUX_ENTRIES * sizeof(AuxInfo),
1341.17Scgd	ELFNAME(copyargs),
1351.8Schristos	setregs,
1361.8Schristos	sigcode,
1371.8Schristos	esigcode,
1381.17Scgd};
1391.9Scgd
1401.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1411.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1421.38Serh#if defined(COMPAT_LINUX)
1431.38Serh	ELFNAME2(linux,probe),
1441.23Shpeyerl#endif
1451.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1461.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1471.9Scgd#endif
1481.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1491.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1501.27Sscottb#endif
1511.8Schristos};
1521.8Schristos
1531.18Scgd/* round up and down to page boundaries. */
1541.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1551.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1561.8Schristos
1571.8Schristos/*
1581.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1591.1Sfvdl * extra information in case of dynamic binding.
1601.1Sfvdl */
1611.1Sfvdlvoid *
1621.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1631.1Sfvdl	struct exec_package *pack;
1641.1Sfvdl	struct ps_strings *arginfo;
1651.1Sfvdl	void *stack;
1661.1Sfvdl	void *argp;
1671.1Sfvdl{
1681.1Sfvdl	size_t len;
1691.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1701.1Sfvdl	struct elf_args *ap;
1711.1Sfvdl
1721.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1731.4Sfvdl	if (!stack)
1741.1Sfvdl		return NULL;
1751.1Sfvdl
1761.22Scgd	a = ai;
1771.22Scgd
1781.1Sfvdl	/*
1791.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1801.1Sfvdl	 * linked binaries
1811.1Sfvdl	 */
1821.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1831.1Sfvdl
1841.1Sfvdl		a->au_id = AUX_phdr;
1851.1Sfvdl		a->au_v = ap->arg_phaddr;
1861.1Sfvdl		a++;
1871.1Sfvdl
1881.1Sfvdl		a->au_id = AUX_phent;
1891.1Sfvdl		a->au_v = ap->arg_phentsize;
1901.1Sfvdl		a++;
1911.1Sfvdl
1921.1Sfvdl		a->au_id = AUX_phnum;
1931.1Sfvdl		a->au_v = ap->arg_phnum;
1941.1Sfvdl		a++;
1951.1Sfvdl
1961.1Sfvdl		a->au_id = AUX_pagesz;
1971.1Sfvdl		a->au_v = NBPG;
1981.1Sfvdl		a++;
1991.1Sfvdl
2001.1Sfvdl		a->au_id = AUX_base;
2011.1Sfvdl		a->au_v = ap->arg_interp;
2021.1Sfvdl		a++;
2031.1Sfvdl
2041.1Sfvdl		a->au_id = AUX_flags;
2051.1Sfvdl		a->au_v = 0;
2061.1Sfvdl		a++;
2071.1Sfvdl
2081.1Sfvdl		a->au_id = AUX_entry;
2091.1Sfvdl		a->au_v = ap->arg_entry;
2101.1Sfvdl		a++;
2111.1Sfvdl
2121.17Scgd		free((char *)ap, M_TEMP);
2131.9Scgd		pack->ep_emul_arg = NULL;
2141.1Sfvdl	}
2151.22Scgd
2161.22Scgd	a->au_id = AUX_null;
2171.22Scgd	a->au_v = 0;
2181.22Scgd	a++;
2191.22Scgd
2201.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2211.22Scgd	if (copyout(ai, stack, len))
2221.22Scgd		return NULL;
2231.29Skleink	(caddr_t)stack += len;
2241.22Scgd
2251.4Sfvdl	return stack;
2261.1Sfvdl}
2271.1Sfvdl
2281.1Sfvdl/*
2291.1Sfvdl * elf_check_header():
2301.1Sfvdl *
2311.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2321.1Sfvdl */
2331.17Scgdint
2341.9ScgdELFNAME(check_header)(eh, type)
2351.9Scgd	Elf_Ehdr *eh;
2361.1Sfvdl	int type;
2371.1Sfvdl{
2381.3Sthorpej
2391.35Sperry	if (memcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
2401.1Sfvdl		return ENOEXEC;
2411.1Sfvdl
2421.1Sfvdl	switch (eh->e_machine) {
2431.9Scgd
2441.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2451.1Sfvdl
2461.1Sfvdl	default:
2471.1Sfvdl		return ENOEXEC;
2481.1Sfvdl	}
2491.1Sfvdl
2501.1Sfvdl	if (eh->e_type != type)
2511.1Sfvdl		return ENOEXEC;
2521.1Sfvdl
2531.1Sfvdl	return 0;
2541.1Sfvdl}
2551.1Sfvdl
2561.1Sfvdl/*
2571.1Sfvdl * elf_load_psection():
2581.17Scgd *
2591.1Sfvdl * Load a psection at the appropriate address
2601.1Sfvdl */
2611.17Scgdvoid
2621.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2631.1Sfvdl	struct exec_vmcmd_set *vcset;
2641.1Sfvdl	struct vnode *vp;
2651.9Scgd	Elf_Phdr *ph;
2661.9Scgd	Elf_Addr *addr;
2671.1Sfvdl	u_long *size;
2681.1Sfvdl	int *prot;
2691.1Sfvdl{
2701.8Schristos	u_long uaddr, msize, psize, rm, rf;
2711.1Sfvdl	long diff, offset;
2721.1Sfvdl
2731.1Sfvdl	/*
2741.17Scgd	 * If the user specified an address, then we load there.
2751.17Scgd	 */
2761.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2771.1Sfvdl		if (ph->p_align > 1) {
2781.18Scgd			*addr = ELF_ROUND(*addr, ph->p_align);
2791.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2801.1Sfvdl		} else
2811.1Sfvdl			uaddr = ph->p_vaddr;
2821.1Sfvdl		diff = ph->p_vaddr - uaddr;
2831.1Sfvdl	} else {
2841.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2851.1Sfvdl		if (ph->p_align > 1)
2861.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2871.1Sfvdl		diff = uaddr - *addr;
2881.1Sfvdl	}
2891.1Sfvdl
2901.9Scgd	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
2911.9Scgd	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
2921.9Scgd	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
2931.1Sfvdl
2941.1Sfvdl	offset = ph->p_offset - diff;
2951.1Sfvdl	*size = ph->p_filesz + diff;
2961.1Sfvdl	msize = ph->p_memsz + diff;
2971.8Schristos	psize = round_page(*size);
2981.1Sfvdl
2991.9Scgd	if ((ph->p_flags & Elf_pf_w) != 0) {
3001.8Schristos		/*
3011.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
3021.8Schristos		 * data page if it's not page aligned we map the last page
3031.8Schristos		 * readvn.
3041.8Schristos		 */
3051.8Schristos		psize = trunc_page(*size);
3061.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3071.8Schristos		    offset, *prot);
3081.8Schristos		if(psize != *size)
3091.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
3101.8Schristos			    *addr + psize, vp, offset + psize, *prot);
3111.9Scgd	} else
3121.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3131.8Schristos		    offset, *prot);
3141.1Sfvdl
3151.1Sfvdl	/*
3161.17Scgd	 * Check if we need to extend the size of the segment
3171.17Scgd	 */
3181.1Sfvdl	rm = round_page(*addr + msize);
3191.1Sfvdl	rf = round_page(*addr + *size);
3201.1Sfvdl
3211.1Sfvdl	if (rm != rf) {
3221.17Scgd		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3231.17Scgd		    0, *prot);
3241.1Sfvdl		*size = msize;
3251.1Sfvdl	}
3261.1Sfvdl}
3271.1Sfvdl
3281.1Sfvdl/*
3291.1Sfvdl * elf_read_from():
3301.1Sfvdl *
3311.1Sfvdl *	Read from vnode into buffer at offset.
3321.1Sfvdl */
3331.7Schristosint
3341.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3351.1Sfvdl	struct vnode *vp;
3361.1Sfvdl	u_long off;
3371.1Sfvdl	struct proc *p;
3381.1Sfvdl	caddr_t buf;
3391.1Sfvdl	int size;
3401.1Sfvdl{
3411.1Sfvdl	int error;
3421.33Sthorpej	size_t resid;
3431.1Sfvdl
3441.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3451.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3461.1Sfvdl		return error;
3471.1Sfvdl	/*
3481.17Scgd	 * See if we got all of it
3491.17Scgd	 */
3501.1Sfvdl	if (resid != 0)
3511.4Sfvdl		return ENOEXEC;
3521.1Sfvdl	return 0;
3531.1Sfvdl}
3541.1Sfvdl
3551.1Sfvdl/*
3561.1Sfvdl * elf_load_file():
3571.1Sfvdl *
3581.1Sfvdl * Load a file (interpreter/library) pointed to by path
3591.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3601.1Sfvdl * so it might be used externally.
3611.1Sfvdl */
3621.17Scgdint
3631.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3641.1Sfvdl	struct proc *p;
3651.14Scgd	struct exec_package *epp;
3661.1Sfvdl	char *path;
3671.1Sfvdl	struct exec_vmcmd_set *vcset;
3681.1Sfvdl	u_long *entry;
3691.1Sfvdl	struct elf_args	*ap;
3701.9Scgd	Elf_Addr *last;
3711.1Sfvdl{
3721.1Sfvdl	int error, i;
3731.1Sfvdl	struct nameidata nd;
3741.14Scgd	struct vnode *vp;
3751.14Scgd	struct vattr attr;
3761.9Scgd	Elf_Ehdr eh;
3771.9Scgd	Elf_Phdr *ph = NULL;
3781.1Sfvdl	u_long phsize;
3791.1Sfvdl	char *bp = NULL;
3801.9Scgd	Elf_Addr addr = *last;
3811.1Sfvdl
3821.1Sfvdl	bp = path;
3831.1Sfvdl	/*
3841.17Scgd	 * 1. open file
3851.17Scgd	 * 2. read filehdr
3861.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3871.17Scgd	 */
3881.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3891.12Scgd	if ((error = namei(&nd)) != 0)
3901.1Sfvdl		return error;
3911.14Scgd	vp = nd.ni_vp;
3921.14Scgd
3931.26Smycroft	/*
3941.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3951.26Smycroft	 * file, we don't allow it to be used.
3961.26Smycroft	 */
3971.14Scgd	if (vp->v_type != VREG) {
3981.14Scgd		error = EACCES;
3991.14Scgd		goto badunlock;
4001.14Scgd	}
4011.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
4021.26Smycroft		goto badunlock;
4031.14Scgd
4041.17Scgd	/* get attributes */
4051.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
4061.14Scgd		goto badunlock;
4071.14Scgd
4081.14Scgd	/*
4091.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
4101.15Scgd	 * we will be executing code from it, so if the mount point
4111.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
4121.14Scgd	 */
4131.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
4141.14Scgd		error = EACCES;
4151.14Scgd		goto badunlock;
4161.14Scgd	}
4171.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
4181.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
4191.14Scgd
4201.12Scgd#ifdef notyet /* XXX cgd 960926 */
4211.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
4221.12Scgd#endif
4231.28Sfvdl	VOP_UNLOCK(vp, 0);
4241.12Scgd
4251.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
4261.17Scgd	    sizeof(eh))) != 0)
4271.1Sfvdl		goto bad;
4281.1Sfvdl
4291.9Scgd	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
4301.1Sfvdl		goto bad;
4311.1Sfvdl
4321.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4331.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4341.1Sfvdl
4351.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4361.9Scgd	    (caddr_t) ph, phsize)) != 0)
4371.1Sfvdl		goto bad;
4381.1Sfvdl
4391.1Sfvdl	/*
4401.17Scgd	 * Load all the necessary sections
4411.17Scgd	 */
4421.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4431.1Sfvdl		u_long size = 0;
4441.1Sfvdl		int prot = 0;
4451.1Sfvdl
4461.1Sfvdl		switch (ph[i].p_type) {
4471.9Scgd		case Elf_pt_load:
4481.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4491.17Scgd			    &size, &prot);
4501.4Sfvdl			/* If entry is within this section it must be text */
4511.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4521.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4531.21Sfvdl				/* XXX */
4541.21Sfvdl				*entry = addr + eh.e_entry;
4551.21Sfvdl#ifdef mips
4561.21Sfvdl				*entry -= ph[i].p_vaddr;
4571.21Sfvdl#endif
4581.1Sfvdl				ap->arg_interp = addr;
4591.1Sfvdl			}
4601.1Sfvdl			addr += size;
4611.1Sfvdl			break;
4621.1Sfvdl
4631.9Scgd		case Elf_pt_dynamic:
4641.9Scgd		case Elf_pt_phdr:
4651.9Scgd		case Elf_pt_note:
4661.1Sfvdl			break;
4671.1Sfvdl
4681.1Sfvdl		default:
4691.1Sfvdl			break;
4701.1Sfvdl		}
4711.1Sfvdl	}
4721.1Sfvdl
4731.17Scgd	free((char *)ph, M_TEMP);
4741.12Scgd	*last = addr;
4751.14Scgd	vrele(vp);
4761.12Scgd	return 0;
4771.12Scgd
4781.14Scgdbadunlock:
4791.28Sfvdl	VOP_UNLOCK(vp, 0);
4801.14Scgd
4811.1Sfvdlbad:
4821.1Sfvdl	if (ph != NULL)
4831.17Scgd		free((char *)ph, M_TEMP);
4841.12Scgd#ifdef notyet /* XXX cgd 960926 */
4851.12Scgd	(maybe) VOP_CLOSE it
4861.12Scgd#endif
4871.14Scgd	vrele(vp);
4881.1Sfvdl	return error;
4891.1Sfvdl}
4901.1Sfvdl
4911.1Sfvdl/*
4921.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4931.1Sfvdl *
4941.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4951.1Sfvdl *
4961.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4971.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4981.1Sfvdl * text, data, bss, and stack segments.
4991.1Sfvdl */
5001.1Sfvdlint
5011.9ScgdELFNAME2(exec,makecmds)(p, epp)
5021.1Sfvdl	struct proc *p;
5031.1Sfvdl	struct exec_package *epp;
5041.1Sfvdl{
5051.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
5061.9Scgd	Elf_Phdr *ph, *pp;
5071.9Scgd	Elf_Addr phdr = 0, pos = 0;
5081.4Sfvdl	int error, i, n, nload;
5091.1Sfvdl	char interp[MAXPATHLEN];
5101.9Scgd	u_long phsize;
5111.1Sfvdl
5121.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
5131.1Sfvdl		return ENOEXEC;
5141.1Sfvdl
5151.9Scgd	if (ELFNAME(check_header)(eh, Elf_et_exec))
5161.1Sfvdl		return ENOEXEC;
5171.1Sfvdl
5181.1Sfvdl	/*
5191.17Scgd	 * check if vnode is in open for writing, because we want to
5201.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5211.17Scgd	 * reasons
5221.17Scgd	 */
5231.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5241.1Sfvdl#ifdef DIAGNOSTIC
5251.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5261.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5271.1Sfvdl#endif
5281.1Sfvdl		return ETXTBSY;
5291.1Sfvdl	}
5301.1Sfvdl	/*
5311.17Scgd	 * Allocate space to hold all the program headers, and read them
5321.17Scgd	 * from the file
5331.17Scgd	 */
5341.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5351.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5361.1Sfvdl
5371.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5381.8Schristos	    (caddr_t) ph, phsize)) != 0)
5391.1Sfvdl		goto bad;
5401.1Sfvdl
5411.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5421.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5431.1Sfvdl
5441.1Sfvdl	interp[0] = '\0';
5451.1Sfvdl
5461.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5471.1Sfvdl		pp = &ph[i];
5481.9Scgd		if (pp->p_type == Elf_pt_interp) {
5491.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5501.1Sfvdl				goto bad;
5511.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5521.17Scgd			    pp->p_offset, (caddr_t) interp,
5531.17Scgd			    pp->p_filesz)) != 0)
5541.1Sfvdl				goto bad;
5551.1Sfvdl			break;
5561.1Sfvdl		}
5571.1Sfvdl	}
5581.1Sfvdl
5591.9Scgd	/*
5601.8Schristos	 * Setup things for native emulation.
5611.8Schristos	 */
5621.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5631.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5641.8Schristos
5651.1Sfvdl	/*
5661.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5671.1Sfvdl	 * See which one will accept this executable. This currently only
5681.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5691.38Serh	 * and the Alpha.
5701.1Sfvdl	 *
5711.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5721.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5731.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5741.1Sfvdl	 * set the ep_emul field in the exec package structure.
5751.1Sfvdl	 */
5761.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
5771.17Scgd	if (n != 0) {
5781.1Sfvdl		error = ENOEXEC;
5791.1Sfvdl		for (i = 0; i < n && error; i++)
5801.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
5811.17Scgd			    interp, &pos);
5821.8Schristos#ifdef notyet
5831.8Schristos		/*
5841.8Schristos		 * We should really use a signature in our native binaries
5851.8Schristos		 * and have our own probe function for matching binaries,
5861.8Schristos		 * before trying the emulations. For now, if the emulation
5871.8Schristos		 * probes failed we default to native.
5881.8Schristos		 */
5891.1Sfvdl		if (error)
5901.1Sfvdl			goto bad;
5911.8Schristos#endif
5921.1Sfvdl	}
5931.1Sfvdl
5941.1Sfvdl	/*
5951.17Scgd	 * Load all the necessary sections
5961.17Scgd	 */
5971.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5981.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5991.9Scgd		u_long size = 0;
6001.1Sfvdl		int prot = 0;
6011.1Sfvdl
6021.1Sfvdl		pp = &ph[i];
6031.1Sfvdl
6041.1Sfvdl		switch (ph[i].p_type) {
6051.9Scgd		case Elf_pt_load:
6061.4Sfvdl			/*
6071.4Sfvdl			 * XXX
6081.4Sfvdl			 * Can handle only 2 sections: text and data
6091.4Sfvdl			 */
6101.4Sfvdl			if (nload++ == 2)
6111.4Sfvdl				goto bad;
6121.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6131.17Scgd			    &ph[i], &addr, &size, &prot);
6141.17Scgd
6151.4Sfvdl			/*
6161.4Sfvdl			 * Decide whether it's text or data by looking
6171.4Sfvdl			 * at the entry point.
6181.4Sfvdl			 */
6191.19Scgd			if (eh->e_entry >= addr &&
6201.19Scgd			    eh->e_entry < (addr + size)) {
6211.4Sfvdl				epp->ep_taddr = addr;
6221.4Sfvdl				epp->ep_tsize = size;
6231.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6241.19Scgd					epp->ep_daddr = addr;
6251.19Scgd					epp->ep_dsize = size;
6261.19Scgd				}
6271.4Sfvdl			} else {
6281.4Sfvdl				epp->ep_daddr = addr;
6291.4Sfvdl				epp->ep_dsize = size;
6301.4Sfvdl			}
6311.1Sfvdl			break;
6321.1Sfvdl
6331.9Scgd		case Elf_pt_shlib:
6341.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6351.1Sfvdl			error = ENOEXEC;
6361.1Sfvdl			goto bad;
6371.27Sscottb#endif
6381.1Sfvdl
6391.9Scgd		case Elf_pt_interp:
6401.1Sfvdl			/* Already did this one */
6411.9Scgd		case Elf_pt_dynamic:
6421.9Scgd		case Elf_pt_note:
6431.1Sfvdl			break;
6441.1Sfvdl
6451.9Scgd		case Elf_pt_phdr:
6461.4Sfvdl			/* Note address of program headers (in text segment) */
6471.4Sfvdl			phdr = pp->p_vaddr;
6481.7Schristos			break;
6491.4Sfvdl
6501.1Sfvdl		default:
6511.1Sfvdl			/*
6521.9Scgd			 * Not fatal; we don't need to understand everything.
6531.1Sfvdl			 */
6541.1Sfvdl			break;
6551.1Sfvdl		}
6561.1Sfvdl	}
6571.5Sfvdl
6581.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6591.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6601.5Sfvdl	/*
6611.5Sfvdl	 * If no position to load the interpreter was set by a probe
6621.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6631.5Sfvdl	 * would (i.e. something safely out of the way).
6641.5Sfvdl	 */
6651.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6661.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6671.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6681.1Sfvdl
6691.1Sfvdl	/*
6701.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6711.17Scgd	 * it's interpreter
6721.17Scgd	 */
6731.1Sfvdl	if (interp[0]) {
6741.1Sfvdl		struct elf_args *ap;
6751.1Sfvdl
6761.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6771.17Scgd		    M_TEMP, M_WAITOK);
6781.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6791.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6801.17Scgd			free((char *)ap, M_TEMP);
6811.1Sfvdl			goto bad;
6821.1Sfvdl		}
6831.1Sfvdl		pos += phsize;
6841.4Sfvdl		ap->arg_phaddr = phdr;
6851.1Sfvdl
6861.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6871.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6881.1Sfvdl		ap->arg_entry = eh->e_entry;
6891.1Sfvdl
6901.1Sfvdl		epp->ep_emul_arg = ap;
6911.1Sfvdl	} else
6921.1Sfvdl		epp->ep_entry = eh->e_entry;
6931.1Sfvdl
6941.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6951.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6961.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
6971.8Schristos	    VM_PROT_READ);
6981.8Schristos#endif
6991.17Scgd	free((char *)ph, M_TEMP);
7001.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
7011.9Scgd	return exec_elf_setup_stack(p, epp);
7021.1Sfvdl
7031.1Sfvdlbad:
7041.17Scgd	free((char *)ph, M_TEMP);
7051.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7061.1Sfvdl	return ENOEXEC;
7071.1Sfvdl}
708