exec_elf32.c revision 1.84
11.84Smatt/*	$NetBSD: exec_elf32.c,v 1.84 2003/02/27 23:10:52 matt Exp $	*/
21.36Schristos
31.36Schristos/*-
41.60Smycroft * Copyright (c) 1994, 2000 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.69Slukem
661.69Slukem#include <sys/cdefs.h>
671.84Smatt__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.84 2003/02/27 23:10:52 matt Exp $");
681.1Sfvdl
691.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
701.9Scgd#ifndef ELFSIZE
711.9Scgd#define	ELFSIZE		32
721.9Scgd#endif
731.30Sthorpej
741.1Sfvdl#include <sys/param.h>
751.1Sfvdl#include <sys/proc.h>
761.1Sfvdl#include <sys/malloc.h>
771.1Sfvdl#include <sys/namei.h>
781.1Sfvdl#include <sys/vnode.h>
791.1Sfvdl#include <sys/exec.h>
801.1Sfvdl#include <sys/exec_elf.h>
811.8Schristos#include <sys/syscall.h>
821.8Schristos#include <sys/signalvar.h>
831.14Scgd#include <sys/mount.h>
841.14Scgd#include <sys/stat.h>
851.1Sfvdl
861.58Sjdolecek#include <machine/cpu.h>
871.58Sjdolecek#include <machine/reg.h>
881.57Sthorpej
891.58Sjdolecekextern const struct emul emul_netbsd;
901.42Schristos
911.54Sthorpejint	ELFNAME(check_header)(Elf_Ehdr *, int);
921.54Sthorpejint	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
931.54Sthorpej	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
941.54Sthorpejvoid	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
951.54Sthorpej	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
961.54Sthorpej
971.59Smrgint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
981.54Sthorpej    Elf_Ehdr *);
991.58Sjdolecekint ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
1001.58Sjdolecek    void *, char *, vaddr_t *);
1011.8Schristos
1021.18Scgd/* round up and down to page boundaries. */
1031.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1041.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1051.8Schristos
1061.8Schristos/*
1071.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1081.1Sfvdl * extra information in case of dynamic binding.
1091.1Sfvdl */
1101.66Schristosint
1111.72SchristosELFNAME(copyargs)(struct proc *p, struct exec_package *pack,
1121.72Schristos    struct ps_strings *arginfo, char **stackp, void *argp)
1131.1Sfvdl{
1141.1Sfvdl	size_t len;
1151.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1161.1Sfvdl	struct elf_args *ap;
1171.66Schristos	int error;
1181.1Sfvdl
1191.72Schristos	if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
1201.66Schristos		return error;
1211.1Sfvdl
1221.22Scgd	a = ai;
1231.22Scgd
1241.1Sfvdl	/*
1251.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1261.1Sfvdl	 * linked binaries
1271.1Sfvdl	 */
1281.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1291.77Sjdolecek		struct vattr *vap = pack->ep_vap;
1301.1Sfvdl
1311.46Skleink		a->a_type = AT_PHDR;
1321.46Skleink		a->a_v = ap->arg_phaddr;
1331.1Sfvdl		a++;
1341.1Sfvdl
1351.46Skleink		a->a_type = AT_PHENT;
1361.46Skleink		a->a_v = ap->arg_phentsize;
1371.1Sfvdl		a++;
1381.1Sfvdl
1391.46Skleink		a->a_type = AT_PHNUM;
1401.46Skleink		a->a_v = ap->arg_phnum;
1411.1Sfvdl		a++;
1421.1Sfvdl
1431.46Skleink		a->a_type = AT_PAGESZ;
1441.57Sthorpej		a->a_v = PAGE_SIZE;
1451.1Sfvdl		a++;
1461.1Sfvdl
1471.46Skleink		a->a_type = AT_BASE;
1481.46Skleink		a->a_v = ap->arg_interp;
1491.1Sfvdl		a++;
1501.1Sfvdl
1511.46Skleink		a->a_type = AT_FLAGS;
1521.46Skleink		a->a_v = 0;
1531.1Sfvdl		a++;
1541.1Sfvdl
1551.46Skleink		a->a_type = AT_ENTRY;
1561.46Skleink		a->a_v = ap->arg_entry;
1571.72Schristos		a++;
1581.72Schristos
1591.72Schristos		a->a_type = AT_EUID;
1601.77Sjdolecek		if (vap->va_mode & S_ISUID)
1611.77Sjdolecek			a->a_v = vap->va_uid;
1621.77Sjdolecek		else
1631.77Sjdolecek			a->a_v = p->p_ucred->cr_uid;
1641.72Schristos		a++;
1651.72Schristos
1661.72Schristos		a->a_type = AT_RUID;
1671.72Schristos		a->a_v = p->p_cred->p_ruid;
1681.72Schristos		a++;
1691.72Schristos
1701.72Schristos		a->a_type = AT_EGID;
1711.77Sjdolecek		if (vap->va_mode & S_ISGID)
1721.77Sjdolecek			a->a_v = vap->va_gid;
1731.77Sjdolecek		else
1741.77Sjdolecek			a->a_v = p->p_ucred->cr_gid;
1751.72Schristos		a++;
1761.72Schristos
1771.72Schristos		a->a_type = AT_RGID;
1781.72Schristos		a->a_v = p->p_cred->p_rgid;
1791.1Sfvdl		a++;
1801.1Sfvdl
1811.76Schs		free(ap, M_TEMP);
1821.9Scgd		pack->ep_emul_arg = NULL;
1831.1Sfvdl	}
1841.22Scgd
1851.46Skleink	a->a_type = AT_NULL;
1861.46Skleink	a->a_v = 0;
1871.22Scgd	a++;
1881.22Scgd
1891.34Sperry	len = (a - ai) * sizeof(AuxInfo);
1901.66Schristos	if ((error = copyout(ai, *stackp, len)) != 0)
1911.66Schristos		return error;
1921.67Schristos	*stackp += len;
1931.22Scgd
1941.66Schristos	return 0;
1951.1Sfvdl}
1961.1Sfvdl
1971.1Sfvdl/*
1981.1Sfvdl * elf_check_header():
1991.1Sfvdl *
2001.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2011.1Sfvdl */
2021.17Scgdint
2031.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type)
2041.1Sfvdl{
2051.3Sthorpej
2061.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2071.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2081.61Smycroft		return (ENOEXEC);
2091.1Sfvdl
2101.1Sfvdl	switch (eh->e_machine) {
2111.9Scgd
2121.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2131.1Sfvdl
2141.1Sfvdl	default:
2151.61Smycroft		return (ENOEXEC);
2161.1Sfvdl	}
2171.70Sthorpej
2181.70Sthorpej	if (ELF_EHDR_FLAGS_OK(eh) == 0)
2191.70Sthorpej		return (ENOEXEC);
2201.1Sfvdl
2211.1Sfvdl	if (eh->e_type != type)
2221.61Smycroft		return (ENOEXEC);
2231.61Smycroft
2241.63Sjdolecek	if (eh->e_shnum > 512 ||
2251.61Smycroft	    eh->e_phnum > 128)
2261.61Smycroft		return (ENOEXEC);
2271.1Sfvdl
2281.61Smycroft	return (0);
2291.1Sfvdl}
2301.1Sfvdl
2311.1Sfvdl/*
2321.1Sfvdl * elf_load_psection():
2331.17Scgd *
2341.1Sfvdl * Load a psection at the appropriate address
2351.1Sfvdl */
2361.17Scgdvoid
2371.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
2381.54Sthorpej    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
2391.1Sfvdl{
2401.8Schristos	u_long uaddr, msize, psize, rm, rf;
2411.1Sfvdl	long diff, offset;
2421.1Sfvdl
2431.1Sfvdl	/*
2441.17Scgd	 * If the user specified an address, then we load there.
2451.17Scgd	 */
2461.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2471.1Sfvdl		if (ph->p_align > 1) {
2481.53Smatt			*addr = ELF_TRUNC(*addr, ph->p_align);
2491.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2501.1Sfvdl		} else
2511.1Sfvdl			uaddr = ph->p_vaddr;
2521.1Sfvdl		diff = ph->p_vaddr - uaddr;
2531.1Sfvdl	} else {
2541.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2551.1Sfvdl		if (ph->p_align > 1)
2561.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2571.1Sfvdl		diff = uaddr - *addr;
2581.1Sfvdl	}
2591.1Sfvdl
2601.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
2611.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
2621.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
2631.1Sfvdl
2641.1Sfvdl	offset = ph->p_offset - diff;
2651.1Sfvdl	*size = ph->p_filesz + diff;
2661.1Sfvdl	msize = ph->p_memsz + diff;
2671.1Sfvdl
2681.65Schristos	if (ph->p_align >= PAGE_SIZE) {
2691.65Schristos		if ((ph->p_flags & PF_W) != 0) {
2701.65Schristos			/*
2711.65Schristos			 * Because the pagedvn pager can't handle zero fill
2721.65Schristos			 * of the last data page if it's not page aligned we
2731.65Schristos			 * map the last page readvn.
2741.65Schristos			 */
2751.65Schristos			psize = trunc_page(*size);
2761.65Schristos		} else {
2771.65Schristos			psize = round_page(*size);
2781.65Schristos		}
2791.65Schristos	} else {
2801.65Schristos		psize = *size;
2811.53Smatt	}
2821.65Schristos
2831.53Smatt	if (psize > 0) {
2841.65Schristos		NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
2851.65Schristos		    vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
2861.53Smatt		    offset, *prot, flags);
2871.53Smatt	}
2881.53Smatt	if (psize < *size) {
2891.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
2901.53Smatt		    *addr + psize, vp, offset + psize, *prot,
2911.83Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
2921.53Smatt	}
2931.1Sfvdl
2941.1Sfvdl	/*
2951.17Scgd	 * Check if we need to extend the size of the segment
2961.17Scgd	 */
2971.1Sfvdl	rm = round_page(*addr + msize);
2981.1Sfvdl	rf = round_page(*addr + *size);
2991.1Sfvdl
3001.1Sfvdl	if (rm != rf) {
3011.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3021.83Smatt		    0, *prot, flags & VMCMD_RELATIVE);
3031.1Sfvdl		*size = msize;
3041.1Sfvdl	}
3051.1Sfvdl}
3061.1Sfvdl
3071.1Sfvdl/*
3081.1Sfvdl * elf_load_file():
3091.1Sfvdl *
3101.1Sfvdl * Load a file (interpreter/library) pointed to by path
3111.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3121.1Sfvdl * so it might be used externally.
3131.1Sfvdl */
3141.17Scgdint
3151.54SthorpejELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
3161.79Satatat    struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap,
3171.54Sthorpej    Elf_Addr *last)
3181.1Sfvdl{
3191.83Smatt	int error, i;
3201.1Sfvdl	struct nameidata nd;
3211.14Scgd	struct vnode *vp;
3221.14Scgd	struct vattr attr;
3231.9Scgd	Elf_Ehdr eh;
3241.9Scgd	Elf_Phdr *ph = NULL;
3251.83Smatt	const Elf_Phdr *ph0;
3261.83Smatt	const Elf_Phdr *base_ph;
3271.83Smatt	const Elf_Phdr *last_ph;
3281.1Sfvdl	u_long phsize;
3291.9Scgd	Elf_Addr addr = *last;
3301.1Sfvdl
3311.1Sfvdl	/*
3321.17Scgd	 * 1. open file
3331.17Scgd	 * 2. read filehdr
3341.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3351.17Scgd	 */
3361.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3371.12Scgd	if ((error = namei(&nd)) != 0)
3381.1Sfvdl		return error;
3391.14Scgd	vp = nd.ni_vp;
3401.14Scgd
3411.26Smycroft	/*
3421.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3431.26Smycroft	 * file, we don't allow it to be used.
3441.26Smycroft	 */
3451.14Scgd	if (vp->v_type != VREG) {
3461.14Scgd		error = EACCES;
3471.14Scgd		goto badunlock;
3481.14Scgd	}
3491.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
3501.26Smycroft		goto badunlock;
3511.14Scgd
3521.17Scgd	/* get attributes */
3531.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
3541.14Scgd		goto badunlock;
3551.14Scgd
3561.14Scgd	/*
3571.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
3581.15Scgd	 * we will be executing code from it, so if the mount point
3591.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
3601.14Scgd	 */
3611.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3621.14Scgd		error = EACCES;
3631.14Scgd		goto badunlock;
3641.14Scgd	}
3651.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
3661.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
3671.14Scgd
3681.12Scgd#ifdef notyet /* XXX cgd 960926 */
3691.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
3701.12Scgd#endif
3711.76Schs
3721.76Schs	error = vn_marktext(vp);
3731.76Schs	if (error)
3741.76Schs		goto badunlock;
3751.76Schs
3761.28Sfvdl	VOP_UNLOCK(vp, 0);
3771.12Scgd
3781.64Schristos	if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0)
3791.1Sfvdl		goto bad;
3801.1Sfvdl
3811.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
3821.1Sfvdl		goto bad;
3831.1Sfvdl
3841.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
3851.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
3861.1Sfvdl
3871.64Schristos	if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0)
3881.1Sfvdl		goto bad;
3891.1Sfvdl
3901.83Smatt	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
3911.83Smatt#ifndef ELF_INTERP_NON_RELOCATABLE
3921.1Sfvdl	/*
3931.83Smatt	 * If no position to load the interpreter was set by a probe
3941.83Smatt	 * function, pick the same address that a non-fixed mmap(0, ..)
3951.83Smatt	 * would (i.e. something safely out of the way).
3961.83Smatt	 */
3971.83Smatt	if (*last == ELFDEFNNAME(NO_ADDR)) {
3981.83Smatt		u_long limit = 0;
3991.83Smatt		/*
4001.83Smatt		 * Find the start and ending addresses of the psections to
4011.83Smatt		 * be loaded.  This will give us the size.
4021.83Smatt		 */
4031.83Smatt		for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum;
4041.83Smatt		     i++, ph0++) {
4051.83Smatt			if (ph0->p_type == PT_LOAD) {
4061.83Smatt				u_long psize = ph0->p_vaddr + ph0->p_memsz;
4071.83Smatt				if (base_ph == NULL)
4081.83Smatt					base_ph = ph0;
4091.83Smatt				if (psize > limit)
4101.83Smatt					limit = psize;
4111.83Smatt			}
4121.83Smatt		}
4131.83Smatt
4141.83Smatt		/*
4151.83Smatt		 * Now compute the size and load address.
4161.83Smatt		 */
4171.84Smatt		addr = VM_DEFAULT_ADDRESS(epp->ep_daddr + MAXDSIZ,
4181.83Smatt		    round_page(limit) - trunc_page(base_ph->p_vaddr));
4191.83Smatt	} else
4201.83Smatt#endif	/* !ELF_INTERP_NON_RELOCATABLE */
4211.83Smatt		addr = *last;
4221.83Smatt
4231.83Smatt	/*
4241.83Smatt	 * Load all the necessary sections
4251.83Smatt	 */
4261.83Smatt	for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
4271.83Smatt	     i < eh.e_phnum; i++, ph0++) {
4281.83Smatt		switch (ph0->p_type) {
4291.83Smatt		case PT_LOAD: {
4301.83Smatt			u_long size;
4311.83Smatt			int prot = 0;
4321.83Smatt			int flags;
4331.1Sfvdl
4341.53Smatt			if (base_ph == NULL) {
4351.82Smatt				/*
4361.82Smatt				 * First encountered psection is always the
4371.82Smatt				 * base psection.
4381.82Smatt				 */
4391.83Smatt				base_ph = ph0;
4401.53Smatt				flags = VMCMD_BASE;
4411.53Smatt			} else {
4421.83Smatt				u_long limit = round_page(last_ph->p_vaddr
4431.83Smatt				    + last_ph->p_memsz);
4441.83Smatt
4451.82Smatt				/*
4461.83Smatt				 * If there is a gap in between the psections,
4471.83Smatt				 * map it as inaccessible so nothing else
4481.83Smatt				 * mmap'ed will be placed there.
4491.82Smatt				 */
4501.83Smatt				if (limit != trunc_page(ph0->p_vaddr)) {
4511.83Smatt					NEW_VMCMD2(vcset, vmcmd_map_zero,
4521.83Smatt					    trunc_page(ph0->p_vaddr) - limit,
4531.83Smatt					    limit - base_ph->p_vaddr,
4541.83Smatt					    NULLVP, 0, 0, VMCMD_RELATIVE);
4551.83Smatt				}
4561.83Smatt
4571.83Smatt				addr = ph0->p_vaddr - base_ph->p_vaddr;
4581.53Smatt				flags = VMCMD_RELATIVE;
4591.53Smatt			}
4601.83Smatt			last_ph = ph0;
4611.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4621.83Smatt			    &size, &prot, flags);
4631.82Smatt			/*
4641.82Smatt			 * If entry is within this psection then this
4651.82Smatt			 * must contain the .text section.  *entryoff is
4661.83Smatt			 * relative to the base psection.
4671.82Smatt			 */
4681.83Smatt			if (eh.e_entry >= ph0->p_vaddr &&
4691.83Smatt			    eh.e_entry < (ph0->p_vaddr + size)) {
4701.83Smatt				*entryoff = eh.e_entry - base_ph->p_vaddr;
4711.1Sfvdl			}
4721.84Smatt			addr += size;
4731.1Sfvdl			break;
4741.83Smatt		}
4751.1Sfvdl
4761.46Skleink		case PT_DYNAMIC:
4771.46Skleink		case PT_PHDR:
4781.46Skleink		case PT_NOTE:
4791.1Sfvdl			break;
4801.1Sfvdl
4811.1Sfvdl		default:
4821.1Sfvdl			break;
4831.1Sfvdl		}
4841.1Sfvdl	}
4851.1Sfvdl
4861.76Schs	free(ph, M_TEMP);
4871.82Smatt	/*
4881.82Smatt	 * This value is ignored if TOPDOWN.
4891.82Smatt	 */
4901.12Scgd	*last = addr;
4911.14Scgd	vrele(vp);
4921.12Scgd	return 0;
4931.12Scgd
4941.14Scgdbadunlock:
4951.28Sfvdl	VOP_UNLOCK(vp, 0);
4961.14Scgd
4971.1Sfvdlbad:
4981.1Sfvdl	if (ph != NULL)
4991.76Schs		free(ph, M_TEMP);
5001.12Scgd#ifdef notyet /* XXX cgd 960926 */
5011.12Scgd	(maybe) VOP_CLOSE it
5021.12Scgd#endif
5031.14Scgd	vrele(vp);
5041.1Sfvdl	return error;
5051.1Sfvdl}
5061.1Sfvdl
5071.1Sfvdl/*
5081.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
5091.1Sfvdl *
5101.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5111.1Sfvdl *
5121.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5131.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5141.1Sfvdl * text, data, bss, and stack segments.
5151.1Sfvdl */
5161.1Sfvdlint
5171.54SthorpejELFNAME2(exec,makecmds)(struct proc *p, 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.58Sjdolecek	int error, i, nload;
5231.58Sjdolecek	char *interp = NULL;
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.76Schs	error = vn_marktext(epp->ep_vp);
5381.76Schs	if (error)
5391.76Schs		return (error);
5401.76Schs
5411.1Sfvdl	/*
5421.17Scgd	 * Allocate space to hold all the program headers, and read them
5431.17Scgd	 * from the file
5441.17Scgd	 */
5451.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5461.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5471.1Sfvdl
5481.64Schristos	if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
5491.64Schristos	    0)
5501.1Sfvdl		goto bad;
5511.1Sfvdl
5521.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5531.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5541.1Sfvdl
5551.58Sjdolecek	MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
5561.1Sfvdl	interp[0] = '\0';
5571.1Sfvdl
5581.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5591.1Sfvdl		pp = &ph[i];
5601.46Skleink		if (pp->p_type == PT_INTERP) {
5611.58Sjdolecek			if (pp->p_filesz >= MAXPATHLEN)
5621.1Sfvdl				goto bad;
5631.64Schristos			if ((error = exec_read_from(p, epp->ep_vp,
5641.64Schristos			    pp->p_offset, interp, pp->p_filesz)) != 0)
5651.1Sfvdl				goto bad;
5661.1Sfvdl			break;
5671.1Sfvdl		}
5681.1Sfvdl	}
5691.1Sfvdl
5701.9Scgd	/*
5711.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5721.1Sfvdl	 * See which one will accept this executable. This currently only
5731.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5741.38Serh	 * and the Alpha.
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.58Sjdolecek	 * interp[] with a changed path (/emul/xxx/<path>).
5791.1Sfvdl	 */
5801.58Sjdolecek	if (!epp->ep_esch->u.elf_probe_func) {
5811.58Sjdolecek		pos = ELFDEFNNAME(NO_ADDR);
5821.58Sjdolecek	} else {
5831.62Seeh		vaddr_t startp = 0;
5841.62Seeh
5851.58Sjdolecek		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
5861.62Seeh							  &startp);
5871.62Seeh		pos = (Elf_Addr)startp;
5881.1Sfvdl		if (error)
5891.1Sfvdl			goto bad;
5901.1Sfvdl	}
5911.1Sfvdl
5921.1Sfvdl	/*
5931.17Scgd	 * Load all the necessary sections
5941.17Scgd	 */
5951.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5961.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5971.9Scgd		u_long size = 0;
5981.1Sfvdl		int prot = 0;
5991.1Sfvdl
6001.1Sfvdl		pp = &ph[i];
6011.1Sfvdl
6021.1Sfvdl		switch (ph[i].p_type) {
6031.46Skleink		case PT_LOAD:
6041.4Sfvdl			/*
6051.4Sfvdl			 * XXX
6061.4Sfvdl			 * Can handle only 2 sections: text and data
6071.4Sfvdl			 */
6081.4Sfvdl			if (nload++ == 2)
6091.4Sfvdl				goto bad;
6101.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6111.79Satatat			    &ph[i], &addr, &size, &prot, VMCMD_FIXED);
6121.17Scgd
6131.4Sfvdl			/*
6141.4Sfvdl			 * Decide whether it's text or data by looking
6151.4Sfvdl			 * at the entry point.
6161.4Sfvdl			 */
6171.19Scgd			if (eh->e_entry >= addr &&
6181.19Scgd			    eh->e_entry < (addr + size)) {
6191.4Sfvdl				epp->ep_taddr = addr;
6201.4Sfvdl				epp->ep_tsize = size;
6211.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6221.19Scgd					epp->ep_daddr = addr;
6231.19Scgd					epp->ep_dsize = size;
6241.19Scgd				}
6251.4Sfvdl			} else {
6261.4Sfvdl				epp->ep_daddr = addr;
6271.4Sfvdl				epp->ep_dsize = size;
6281.4Sfvdl			}
6291.1Sfvdl			break;
6301.1Sfvdl
6311.46Skleink		case PT_SHLIB:
6321.60Smycroft			/* SCO has these sections. */
6331.46Skleink		case PT_INTERP:
6341.60Smycroft			/* Already did this one. */
6351.46Skleink		case PT_DYNAMIC:
6361.46Skleink		case PT_NOTE:
6371.1Sfvdl			break;
6381.1Sfvdl
6391.46Skleink		case PT_PHDR:
6401.4Sfvdl			/* Note address of program headers (in text segment) */
6411.4Sfvdl			phdr = pp->p_vaddr;
6421.7Schristos			break;
6431.4Sfvdl
6441.1Sfvdl		default:
6451.1Sfvdl			/*
6461.9Scgd			 * Not fatal; we don't need to understand everything.
6471.1Sfvdl			 */
6481.1Sfvdl			break;
6491.1Sfvdl		}
6501.1Sfvdl	}
6511.1Sfvdl
6521.1Sfvdl	/*
6531.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6541.79Satatat	 * its interpreter
6551.17Scgd	 */
6561.1Sfvdl	if (interp[0]) {
6571.1Sfvdl		struct elf_args *ap;
6581.79Satatat		int i = epp->ep_vmcmds.evs_used;
6591.79Satatat		u_long interp_offset;
6601.1Sfvdl
6611.58Sjdolecek		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
6621.17Scgd		    M_TEMP, M_WAITOK);
6631.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6641.79Satatat		    &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
6651.76Schs			FREE(ap, M_TEMP);
6661.1Sfvdl			goto bad;
6671.1Sfvdl		}
6681.79Satatat		ap->arg_interp = epp->ep_vmcmds.evs_cmds[i].ev_addr;
6691.79Satatat		epp->ep_entry = ap->arg_interp + interp_offset;
6701.4Sfvdl		ap->arg_phaddr = phdr;
6711.1Sfvdl
6721.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6731.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6741.1Sfvdl		ap->arg_entry = eh->e_entry;
6751.1Sfvdl
6761.1Sfvdl		epp->ep_emul_arg = ap;
6771.1Sfvdl	} else
6781.1Sfvdl		epp->ep_entry = eh->e_entry;
6791.1Sfvdl
6801.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6811.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6821.57Sthorpej	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
6831.57Sthorpej	    epp->ep_vp, 0, VM_PROT_READ);
6841.8Schristos#endif
6851.58Sjdolecek	FREE(interp, M_TEMP);
6861.76Schs	free(ph, M_TEMP);
6871.9Scgd	return exec_elf_setup_stack(p, epp);
6881.1Sfvdl
6891.1Sfvdlbad:
6901.58Sjdolecek	if (interp)
6911.58Sjdolecek		FREE(interp, M_TEMP);
6921.76Schs	free(ph, M_TEMP);
6931.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6941.1Sfvdl	return ENOEXEC;
6951.40Schristos}
6961.40Schristos
6971.59Smrgint
6981.54SthorpejELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
6991.54Sthorpej    Elf_Ehdr *eh)
7001.40Schristos{
7011.61Smycroft	size_t i;
7021.61Smycroft	Elf_Phdr *ph;
7031.40Schristos	size_t phsize;
7041.40Schristos	int error;
7051.40Schristos
7061.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
7071.61Smycroft	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
7081.64Schristos	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
7091.61Smycroft	if (error)
7101.61Smycroft		goto out;
7111.40Schristos
7121.61Smycroft	for (i = 0; i < eh->e_phnum; i++) {
7131.61Smycroft		Elf_Phdr *ephp = &ph[i];
7141.61Smycroft		Elf_Nhdr *np;
7151.40Schristos
7161.61Smycroft		if (ephp->p_type != PT_NOTE ||
7171.61Smycroft		    ephp->p_filesz > 1024 ||
7181.61Smycroft		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
7191.40Schristos			continue;
7201.40Schristos
7211.61Smycroft		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
7221.64Schristos		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
7231.64Schristos		    ephp->p_filesz);
7241.61Smycroft		if (error)
7251.61Smycroft			goto next;
7261.40Schristos
7271.61Smycroft		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
7281.61Smycroft		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
7291.61Smycroft		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
7301.61Smycroft		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
7311.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
7321.61Smycroft			goto next;
7331.40Schristos
7341.40Schristos		error = 0;
7351.61Smycroft		free(np, M_TEMP);
7361.61Smycroft		goto out;
7371.61Smycroft
7381.61Smycroft	next:
7391.61Smycroft		free(np, M_TEMP);
7401.61Smycroft		continue;
7411.40Schristos	}
7421.40Schristos
7431.40Schristos	error = ENOEXEC;
7441.61Smycroftout:
7451.61Smycroft	free(ph, M_TEMP);
7461.61Smycroft	return (error);
7471.40Schristos}
7481.40Schristos
7491.58Sjdolecekint
7501.54SthorpejELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
7511.58Sjdolecek    void *eh, char *itp, vaddr_t *pos)
7521.40Schristos{
7531.40Schristos	int error;
7541.40Schristos
7551.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
7561.40Schristos		return error;
7571.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
7581.40Schristos	return 0;
7591.1Sfvdl}
760