exec_elf32.c revision 1.56
11.56Sjdolecek/*	$NetBSD: exec_elf32.c,v 1.56 2000/11/13 21:32:15 jdolecek Exp $	*/
21.36Schristos
31.36Schristos/*-
41.36Schristos * Copyright (c) 1994 The NetBSD Foundation, Inc.
51.36Schristos * All rights reserved.
61.36Schristos *
71.36Schristos * This code is derived from software contributed to The NetBSD Foundation
81.36Schristos * by Christos Zoulas.
91.36Schristos *
101.36Schristos * Redistribution and use in source and binary forms, with or without
111.36Schristos * modification, are permitted provided that the following conditions
121.36Schristos * are met:
131.36Schristos * 1. Redistributions of source code must retain the above copyright
141.36Schristos *    notice, this list of conditions and the following disclaimer.
151.36Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.36Schristos *    notice, this list of conditions and the following disclaimer in the
171.36Schristos *    documentation and/or other materials provided with the distribution.
181.36Schristos * 3. All advertising materials mentioning features or use of this software
191.36Schristos *    must display the following acknowledgement:
201.37Schristos *	This product includes software developed by the NetBSD
211.37Schristos *	Foundation, Inc. and its contributors.
221.36Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
231.36Schristos *    contributors may be used to endorse or promote products derived
241.36Schristos *    from this software without specific prior written permission.
251.36Schristos *
261.36Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.36Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.36Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.36Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.36Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.36Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.36Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.36Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.36Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.36Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.36Schristos * POSSIBILITY OF SUCH DAMAGE.
371.36Schristos */
381.1Sfvdl
391.1Sfvdl/*
401.9Scgd * Copyright (c) 1996 Christopher G. Demetriou
411.1Sfvdl * All rights reserved.
421.1Sfvdl *
431.1Sfvdl * Redistribution and use in source and binary forms, with or without
441.1Sfvdl * modification, are permitted provided that the following conditions
451.1Sfvdl * are met:
461.1Sfvdl * 1. Redistributions of source code must retain the above copyright
471.1Sfvdl *    notice, this list of conditions and the following disclaimer.
481.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
491.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
501.1Sfvdl *    documentation and/or other materials provided with the distribution.
511.1Sfvdl * 3. The name of the author may not be used to endorse or promote products
521.1Sfvdl *    derived from this software without specific prior written permission
531.1Sfvdl *
541.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
551.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
561.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
571.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
581.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
591.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
601.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
611.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
621.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
631.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
641.1Sfvdl */
651.1Sfvdl
661.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
671.9Scgd#ifndef ELFSIZE
681.9Scgd#define	ELFSIZE		32
691.9Scgd#endif
701.30Sthorpej
711.30Sthorpej#include "opt_compat_linux.h"
721.31Sthorpej#include "opt_compat_ibcs2.h"
731.32Sthorpej#include "opt_compat_svr4.h"
741.42Schristos#include "opt_compat_freebsd.h"
751.47Seeh#include "opt_compat_netbsd32.h"
761.49Ssoren#include "opt_syscall_debug.h"
771.9Scgd
781.1Sfvdl#include <sys/param.h>
791.1Sfvdl#include <sys/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.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.47Seeh#ifdef COMPAT_NETBSD32
911.47Seeh#include <compat/netbsd32/netbsd32_exec.h>
921.47Seeh#endif
931.47Seeh
941.1Sfvdl#ifdef COMPAT_LINUX
951.39Schristos#include <compat/linux/common/linux_exec.h>
961.1Sfvdl#endif
971.1Sfvdl
981.1Sfvdl#ifdef COMPAT_SVR4
991.1Sfvdl#include <compat/svr4/svr4_exec.h>
1001.1Sfvdl#endif
1011.1Sfvdl
1021.27Sscottb#ifdef COMPAT_IBCS2
1031.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
1041.27Sscottb#endif
1051.27Sscottb
1061.42Schristos#ifdef COMPAT_FREEBSD
1071.42Schristos#include <compat/freebsd/freebsd_exec.h>
1081.42Schristos#endif
1091.42Schristos
1101.54Sthorpejint	ELFNAME(check_header)(Elf_Ehdr *, int);
1111.54Sthorpejint	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
1121.54Sthorpej	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
1131.54Sthorpejvoid	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
1141.54Sthorpej	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
1151.54Sthorpej
1161.54Sthorpejint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
1171.54Sthorpej    Elf_Ehdr *);
1181.54Sthorpejstatic int ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
1191.54Sthorpej    Elf_Ehdr *, char *, Elf_Addr *);
1201.40Schristos
1211.8Schristosextern char sigcode[], esigcode[];
1221.8Schristos#ifdef SYSCALL_DEBUG
1231.56Sjdolecekextern const char * const syscallnames[];
1241.17Scgd#endif
1251.8Schristos
1261.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1271.8Schristos	"netbsd",
1281.8Schristos	NULL,
1291.8Schristos	sendsig,
1301.8Schristos	SYS_syscall,
1311.8Schristos	SYS_MAXSYSCALL,
1321.8Schristos	sysent,
1331.8Schristos#ifdef SYSCALL_DEBUG
1341.8Schristos	syscallnames,
1351.17Scgd#else
1361.8Schristos	NULL,
1371.17Scgd#endif
1381.47Seeh	howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)),
1391.17Scgd	ELFNAME(copyargs),
1401.8Schristos	setregs,
1411.8Schristos	sigcode,
1421.8Schristos	esigcode,
1431.17Scgd};
1441.9Scgd
1451.54Sthorpejint (*ELFNAME(probe_funcs)[])(struct proc *, struct exec_package *,
1461.54Sthorpej    Elf_Ehdr *, char *, Elf_Addr *) = {
1471.47Seeh#if defined(COMPAT_NETBSD32) && (ELFSIZE == 32)
1481.47Seeh	    /* This one should go first so it matches instead of netbsd */
1491.47Seeh	ELFNAME2(netbsd32,probe),
1501.47Seeh#endif
1511.40Schristos	ELFNAME2(netbsd,probe),
1521.42Schristos#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32)
1531.42Schristos	ELFNAME2(freebsd,probe),		/* XXX not 64-bit safe */
1541.42Schristos#endif
1551.38Serh#if defined(COMPAT_LINUX)
1561.38Serh	ELFNAME2(linux,probe),
1571.23Shpeyerl#endif
1581.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1591.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1601.9Scgd#endif
1611.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1621.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1631.27Sscottb#endif
1641.8Schristos};
1651.8Schristos
1661.18Scgd/* round up and down to page boundaries. */
1671.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1681.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1691.8Schristos
1701.8Schristos/*
1711.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1721.1Sfvdl * extra information in case of dynamic binding.
1731.1Sfvdl */
1741.1Sfvdlvoid *
1751.54SthorpejELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo,
1761.54Sthorpej    void *stack, void *argp)
1771.1Sfvdl{
1781.1Sfvdl	size_t len;
1791.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1801.1Sfvdl	struct elf_args *ap;
1811.1Sfvdl
1821.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1831.4Sfvdl	if (!stack)
1841.1Sfvdl		return NULL;
1851.1Sfvdl
1861.22Scgd	a = ai;
1871.22Scgd
1881.1Sfvdl	/*
1891.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1901.1Sfvdl	 * linked binaries
1911.1Sfvdl	 */
1921.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1931.1Sfvdl
1941.46Skleink		a->a_type = AT_PHDR;
1951.46Skleink		a->a_v = ap->arg_phaddr;
1961.1Sfvdl		a++;
1971.1Sfvdl
1981.46Skleink		a->a_type = AT_PHENT;
1991.46Skleink		a->a_v = ap->arg_phentsize;
2001.1Sfvdl		a++;
2011.1Sfvdl
2021.46Skleink		a->a_type = AT_PHNUM;
2031.46Skleink		a->a_v = ap->arg_phnum;
2041.1Sfvdl		a++;
2051.1Sfvdl
2061.46Skleink		a->a_type = AT_PAGESZ;
2071.46Skleink		a->a_v = NBPG;
2081.1Sfvdl		a++;
2091.1Sfvdl
2101.46Skleink		a->a_type = AT_BASE;
2111.46Skleink		a->a_v = ap->arg_interp;
2121.1Sfvdl		a++;
2131.1Sfvdl
2141.46Skleink		a->a_type = AT_FLAGS;
2151.46Skleink		a->a_v = 0;
2161.1Sfvdl		a++;
2171.1Sfvdl
2181.46Skleink		a->a_type = AT_ENTRY;
2191.46Skleink		a->a_v = ap->arg_entry;
2201.1Sfvdl		a++;
2211.1Sfvdl
2221.17Scgd		free((char *)ap, M_TEMP);
2231.9Scgd		pack->ep_emul_arg = NULL;
2241.1Sfvdl	}
2251.22Scgd
2261.46Skleink	a->a_type = AT_NULL;
2271.46Skleink	a->a_v = 0;
2281.22Scgd	a++;
2291.22Scgd
2301.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2311.22Scgd	if (copyout(ai, stack, len))
2321.22Scgd		return NULL;
2331.43Skleink	stack = (caddr_t)stack + len;
2341.22Scgd
2351.4Sfvdl	return stack;
2361.1Sfvdl}
2371.1Sfvdl
2381.1Sfvdl/*
2391.1Sfvdl * elf_check_header():
2401.1Sfvdl *
2411.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2421.1Sfvdl */
2431.17Scgdint
2441.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type)
2451.1Sfvdl{
2461.3Sthorpej
2471.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2481.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2491.52Skleink		return ENOEXEC;
2501.1Sfvdl
2511.1Sfvdl	switch (eh->e_machine) {
2521.9Scgd
2531.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2541.1Sfvdl
2551.1Sfvdl	default:
2561.1Sfvdl		return ENOEXEC;
2571.1Sfvdl	}
2581.1Sfvdl
2591.1Sfvdl	if (eh->e_type != type)
2601.1Sfvdl		return ENOEXEC;
2611.1Sfvdl
2621.1Sfvdl	return 0;
2631.1Sfvdl}
2641.1Sfvdl
2651.1Sfvdl/*
2661.1Sfvdl * elf_load_psection():
2671.17Scgd *
2681.1Sfvdl * Load a psection at the appropriate address
2691.1Sfvdl */
2701.17Scgdvoid
2711.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
2721.54Sthorpej    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
2731.1Sfvdl{
2741.8Schristos	u_long uaddr, msize, psize, rm, rf;
2751.1Sfvdl	long diff, offset;
2761.1Sfvdl
2771.1Sfvdl	/*
2781.17Scgd	 * If the user specified an address, then we load there.
2791.17Scgd	 */
2801.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2811.1Sfvdl		if (ph->p_align > 1) {
2821.53Smatt			*addr = ELF_TRUNC(*addr, ph->p_align);
2831.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2841.1Sfvdl		} else
2851.1Sfvdl			uaddr = ph->p_vaddr;
2861.1Sfvdl		diff = ph->p_vaddr - uaddr;
2871.1Sfvdl	} else {
2881.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2891.1Sfvdl		if (ph->p_align > 1)
2901.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2911.1Sfvdl		diff = uaddr - *addr;
2921.1Sfvdl	}
2931.1Sfvdl
2941.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
2951.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
2961.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
2971.1Sfvdl
2981.1Sfvdl	offset = ph->p_offset - diff;
2991.1Sfvdl	*size = ph->p_filesz + diff;
3001.1Sfvdl	msize = ph->p_memsz + diff;
3011.8Schristos	psize = round_page(*size);
3021.1Sfvdl
3031.46Skleink	if ((ph->p_flags & PF_W) != 0) {
3041.8Schristos		/*
3051.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
3061.8Schristos		 * data page if it's not page aligned we map the last page
3071.8Schristos		 * readvn.
3081.8Schristos		 */
3091.8Schristos		psize = trunc_page(*size);
3101.53Smatt	}
3111.53Smatt	if (psize > 0) {
3121.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3131.53Smatt		    offset, *prot, flags);
3141.53Smatt	}
3151.53Smatt	if (psize < *size) {
3161.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
3171.53Smatt		    *addr + psize, vp, offset + psize, *prot,
3181.53Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
3191.53Smatt	}
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.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3291.53Smatt		    0, *prot, flags & VMCMD_RELATIVE);
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.54SthorpejELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off,
3411.54Sthorpej    caddr_t buf, int size)
3421.1Sfvdl{
3431.1Sfvdl	int error;
3441.33Sthorpej	size_t resid;
3451.1Sfvdl
3461.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3471.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3481.1Sfvdl		return error;
3491.1Sfvdl	/*
3501.17Scgd	 * See if we got all of it
3511.17Scgd	 */
3521.1Sfvdl	if (resid != 0)
3531.4Sfvdl		return ENOEXEC;
3541.1Sfvdl	return 0;
3551.1Sfvdl}
3561.1Sfvdl
3571.1Sfvdl/*
3581.1Sfvdl * elf_load_file():
3591.1Sfvdl *
3601.1Sfvdl * Load a file (interpreter/library) pointed to by path
3611.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3621.1Sfvdl * so it might be used externally.
3631.1Sfvdl */
3641.17Scgdint
3651.54SthorpejELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
3661.54Sthorpej    struct exec_vmcmd_set *vcset, u_long *entry, struct elf_args *ap,
3671.54Sthorpej    Elf_Addr *last)
3681.1Sfvdl{
3691.1Sfvdl	int error, i;
3701.1Sfvdl	struct nameidata nd;
3711.14Scgd	struct vnode *vp;
3721.14Scgd	struct vattr attr;
3731.9Scgd	Elf_Ehdr eh;
3741.9Scgd	Elf_Phdr *ph = NULL;
3751.53Smatt	Elf_Phdr *base_ph = NULL;
3761.1Sfvdl	u_long phsize;
3771.1Sfvdl	char *bp = NULL;
3781.9Scgd	Elf_Addr addr = *last;
3791.1Sfvdl
3801.1Sfvdl	bp = path;
3811.1Sfvdl	/*
3821.17Scgd	 * 1. open file
3831.17Scgd	 * 2. read filehdr
3841.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3851.17Scgd	 */
3861.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3871.12Scgd	if ((error = namei(&nd)) != 0)
3881.1Sfvdl		return error;
3891.14Scgd	vp = nd.ni_vp;
3901.14Scgd
3911.26Smycroft	/*
3921.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3931.26Smycroft	 * file, we don't allow it to be used.
3941.26Smycroft	 */
3951.14Scgd	if (vp->v_type != VREG) {
3961.14Scgd		error = EACCES;
3971.14Scgd		goto badunlock;
3981.14Scgd	}
3991.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
4001.26Smycroft		goto badunlock;
4011.14Scgd
4021.17Scgd	/* get attributes */
4031.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
4041.14Scgd		goto badunlock;
4051.14Scgd
4061.14Scgd	/*
4071.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
4081.15Scgd	 * we will be executing code from it, so if the mount point
4091.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
4101.14Scgd	 */
4111.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
4121.14Scgd		error = EACCES;
4131.14Scgd		goto badunlock;
4141.14Scgd	}
4151.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
4161.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
4171.14Scgd
4181.12Scgd#ifdef notyet /* XXX cgd 960926 */
4191.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
4201.12Scgd#endif
4211.28Sfvdl	VOP_UNLOCK(vp, 0);
4221.12Scgd
4231.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
4241.17Scgd	    sizeof(eh))) != 0)
4251.1Sfvdl		goto bad;
4261.1Sfvdl
4271.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
4281.1Sfvdl		goto bad;
4291.1Sfvdl
4301.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4311.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4321.1Sfvdl
4331.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4341.9Scgd	    (caddr_t) ph, phsize)) != 0)
4351.1Sfvdl		goto bad;
4361.1Sfvdl
4371.1Sfvdl	/*
4381.17Scgd	 * Load all the necessary sections
4391.17Scgd	 */
4401.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4411.1Sfvdl		u_long size = 0;
4421.1Sfvdl		int prot = 0;
4431.53Smatt		int flags;
4441.1Sfvdl
4451.1Sfvdl		switch (ph[i].p_type) {
4461.46Skleink		case PT_LOAD:
4471.53Smatt			if (base_ph == NULL) {
4481.53Smatt				addr = *last;
4491.53Smatt				flags = VMCMD_BASE;
4501.53Smatt			} else {
4511.53Smatt				addr = ph[i].p_vaddr - base_ph->p_vaddr;
4521.53Smatt				flags = VMCMD_RELATIVE;
4531.53Smatt			}
4541.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4551.53Smatt			    &size, &prot, flags);
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.53Smatt			if (base_ph == NULL)
4671.53Smatt				base_ph = &ph[i];
4681.1Sfvdl			addr += size;
4691.1Sfvdl			break;
4701.1Sfvdl
4711.46Skleink		case PT_DYNAMIC:
4721.46Skleink		case PT_PHDR:
4731.46Skleink		case PT_NOTE:
4741.1Sfvdl			break;
4751.1Sfvdl
4761.1Sfvdl		default:
4771.1Sfvdl			break;
4781.1Sfvdl		}
4791.1Sfvdl	}
4801.1Sfvdl
4811.17Scgd	free((char *)ph, M_TEMP);
4821.12Scgd	*last = addr;
4831.14Scgd	vrele(vp);
4841.12Scgd	return 0;
4851.12Scgd
4861.14Scgdbadunlock:
4871.28Sfvdl	VOP_UNLOCK(vp, 0);
4881.14Scgd
4891.1Sfvdlbad:
4901.1Sfvdl	if (ph != NULL)
4911.17Scgd		free((char *)ph, M_TEMP);
4921.12Scgd#ifdef notyet /* XXX cgd 960926 */
4931.12Scgd	(maybe) VOP_CLOSE it
4941.12Scgd#endif
4951.14Scgd	vrele(vp);
4961.1Sfvdl	return error;
4971.1Sfvdl}
4981.1Sfvdl
4991.1Sfvdl/*
5001.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
5011.1Sfvdl *
5021.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5031.1Sfvdl *
5041.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5051.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5061.1Sfvdl * text, data, bss, and stack segments.
5071.1Sfvdl */
5081.1Sfvdlint
5091.54SthorpejELFNAME2(exec,makecmds)(struct proc *p, 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.45Sfvdl	/*
5221.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
5231.45Sfvdl	 * but other ELF-based systems allow it as well.
5241.45Sfvdl	 */
5251.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
5261.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
5271.1Sfvdl		return ENOEXEC;
5281.1Sfvdl
5291.1Sfvdl	/*
5301.17Scgd	 * check if vnode is in open for writing, because we want to
5311.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5321.17Scgd	 * reasons
5331.17Scgd	 */
5341.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5351.1Sfvdl#ifdef DIAGNOSTIC
5361.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5371.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5381.1Sfvdl#endif
5391.1Sfvdl		return ETXTBSY;
5401.1Sfvdl	}
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.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5491.8Schristos	    (caddr_t) ph, phsize)) != 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.1Sfvdl	interp[0] = '\0';
5561.1Sfvdl
5571.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5581.1Sfvdl		pp = &ph[i];
5591.46Skleink		if (pp->p_type == PT_INTERP) {
5601.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5611.1Sfvdl				goto bad;
5621.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5631.17Scgd			    pp->p_offset, (caddr_t) interp,
5641.17Scgd			    pp->p_filesz)) != 0)
5651.1Sfvdl				goto bad;
5661.1Sfvdl			break;
5671.1Sfvdl		}
5681.1Sfvdl	}
5691.1Sfvdl
5701.9Scgd	/*
5711.8Schristos	 * Setup things for native emulation.
5721.8Schristos	 */
5731.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5741.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5751.8Schristos
5761.1Sfvdl	/*
5771.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5781.1Sfvdl	 * See which one will accept this executable. This currently only
5791.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5801.38Serh	 * and the Alpha.
5811.1Sfvdl	 *
5821.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5831.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5841.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5851.1Sfvdl	 * set the ep_emul field in the exec package structure.
5861.1Sfvdl	 */
5871.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
5881.17Scgd	if (n != 0) {
5891.1Sfvdl		error = ENOEXEC;
5901.1Sfvdl		for (i = 0; i < n && error; i++)
5911.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
5921.17Scgd			    interp, &pos);
5931.8Schristos#ifdef notyet
5941.8Schristos		/*
5951.8Schristos		 * We should really use a signature in our native binaries
5961.8Schristos		 * and have our own probe function for matching binaries,
5971.8Schristos		 * before trying the emulations. For now, if the emulation
5981.8Schristos		 * probes failed we default to native.
5991.8Schristos		 */
6001.1Sfvdl		if (error)
6011.1Sfvdl			goto bad;
6021.8Schristos#endif
6031.1Sfvdl	}
6041.1Sfvdl
6051.1Sfvdl	/*
6061.17Scgd	 * Load all the necessary sections
6071.17Scgd	 */
6081.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
6091.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
6101.9Scgd		u_long size = 0;
6111.1Sfvdl		int prot = 0;
6121.1Sfvdl
6131.1Sfvdl		pp = &ph[i];
6141.1Sfvdl
6151.1Sfvdl		switch (ph[i].p_type) {
6161.46Skleink		case PT_LOAD:
6171.4Sfvdl			/*
6181.4Sfvdl			 * XXX
6191.4Sfvdl			 * Can handle only 2 sections: text and data
6201.4Sfvdl			 */
6211.4Sfvdl			if (nload++ == 2)
6221.4Sfvdl				goto bad;
6231.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6241.53Smatt			    &ph[i], &addr, &size, &prot, 0);
6251.17Scgd
6261.4Sfvdl			/*
6271.4Sfvdl			 * Decide whether it's text or data by looking
6281.4Sfvdl			 * at the entry point.
6291.4Sfvdl			 */
6301.19Scgd			if (eh->e_entry >= addr &&
6311.19Scgd			    eh->e_entry < (addr + size)) {
6321.4Sfvdl				epp->ep_taddr = addr;
6331.4Sfvdl				epp->ep_tsize = size;
6341.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6351.19Scgd					epp->ep_daddr = addr;
6361.19Scgd					epp->ep_dsize = size;
6371.19Scgd				}
6381.4Sfvdl			} else {
6391.4Sfvdl				epp->ep_daddr = addr;
6401.4Sfvdl				epp->ep_dsize = size;
6411.4Sfvdl			}
6421.1Sfvdl			break;
6431.1Sfvdl
6441.46Skleink		case PT_SHLIB:
6451.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6461.1Sfvdl			error = ENOEXEC;
6471.1Sfvdl			goto bad;
6481.27Sscottb#endif
6491.1Sfvdl
6501.46Skleink		case PT_INTERP:
6511.1Sfvdl			/* Already did this one */
6521.46Skleink		case PT_DYNAMIC:
6531.46Skleink		case PT_NOTE:
6541.1Sfvdl			break;
6551.1Sfvdl
6561.46Skleink		case PT_PHDR:
6571.4Sfvdl			/* Note address of program headers (in text segment) */
6581.4Sfvdl			phdr = pp->p_vaddr;
6591.7Schristos			break;
6601.4Sfvdl
6611.1Sfvdl		default:
6621.1Sfvdl			/*
6631.9Scgd			 * Not fatal; we don't need to understand everything.
6641.1Sfvdl			 */
6651.1Sfvdl			break;
6661.1Sfvdl		}
6671.1Sfvdl	}
6681.5Sfvdl
6691.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6701.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6711.5Sfvdl	/*
6721.5Sfvdl	 * If no position to load the interpreter was set by a probe
6731.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6741.5Sfvdl	 * would (i.e. something safely out of the way).
6751.5Sfvdl	 */
6761.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6771.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6781.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6791.1Sfvdl
6801.1Sfvdl	/*
6811.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6821.17Scgd	 * it's interpreter
6831.17Scgd	 */
6841.1Sfvdl	if (interp[0]) {
6851.1Sfvdl		struct elf_args *ap;
6861.1Sfvdl
6871.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6881.17Scgd		    M_TEMP, M_WAITOK);
6891.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6901.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6911.17Scgd			free((char *)ap, M_TEMP);
6921.1Sfvdl			goto bad;
6931.1Sfvdl		}
6941.1Sfvdl		pos += phsize;
6951.4Sfvdl		ap->arg_phaddr = phdr;
6961.1Sfvdl
6971.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6981.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6991.1Sfvdl		ap->arg_entry = eh->e_entry;
7001.1Sfvdl
7011.1Sfvdl		epp->ep_emul_arg = ap;
7021.1Sfvdl	} else
7031.1Sfvdl		epp->ep_entry = eh->e_entry;
7041.1Sfvdl
7051.8Schristos#ifdef ELF_MAP_PAGE_ZERO
7061.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
7071.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
7081.8Schristos	    VM_PROT_READ);
7091.8Schristos#endif
7101.17Scgd	free((char *)ph, M_TEMP);
7111.48Schs	vn_marktext(epp->ep_vp);
7121.9Scgd	return exec_elf_setup_stack(p, epp);
7131.1Sfvdl
7141.1Sfvdlbad:
7151.17Scgd	free((char *)ph, M_TEMP);
7161.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7171.1Sfvdl	return ENOEXEC;
7181.40Schristos}
7191.40Schristos
7201.47Seehint
7211.54SthorpejELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
7221.54Sthorpej    Elf_Ehdr *eh)
7231.40Schristos{
7241.40Schristos	Elf_Phdr *hph, *ph;
7251.46Skleink	Elf_Nhdr *np = NULL;
7261.40Schristos	size_t phsize;
7271.40Schristos	int error;
7281.40Schristos
7291.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
7301.40Schristos	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
7311.41Schristos	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
7321.41Schristos	    (caddr_t)hph, phsize)) != 0)
7331.40Schristos		goto out1;
7341.40Schristos
7351.40Schristos	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
7361.46Skleink		if (ph->p_type != PT_NOTE ||
7371.46Skleink		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
7381.40Schristos			continue;
7391.40Schristos
7401.46Skleink		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
7411.40Schristos		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
7421.40Schristos		    (caddr_t)np, ph->p_filesz)) != 0)
7431.40Schristos			goto out2;
7441.40Schristos
7451.46Skleink		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
7461.40Schristos			free(np, M_TEMP);
7471.40Schristos			np = NULL;
7481.40Schristos			continue;
7491.40Schristos		}
7501.40Schristos
7511.40Schristos		/* Check the name and description sizes. */
7521.46Skleink		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
7531.46Skleink		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
7541.40Schristos			goto out3;
7551.40Schristos
7561.40Schristos		/* Is the name "NetBSD\0\0"? */
7571.40Schristos		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
7581.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
7591.40Schristos			goto out3;
7601.40Schristos
7611.40Schristos		/* XXX: We could check for the specific emulation here */
7621.40Schristos		/* All checks succeeded. */
7631.40Schristos		error = 0;
7641.40Schristos		goto out2;
7651.40Schristos	}
7661.40Schristos
7671.40Schristosout3:
7681.40Schristos	error = ENOEXEC;
7691.40Schristosout2:
7701.40Schristos	if (np)
7711.40Schristos		free(np, M_TEMP);
7721.40Schristosout1:
7731.40Schristos	free(hph, M_TEMP);
7741.40Schristos	return error;
7751.40Schristos}
7761.40Schristos
7771.40Schristosstatic int
7781.54SthorpejELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
7791.54Sthorpej    Elf_Ehdr *eh, char *itp, Elf_Addr *pos)
7801.40Schristos{
7811.40Schristos	int error;
7821.40Schristos
7831.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
7841.40Schristos		return error;
7851.40Schristos	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
7861.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
7871.40Schristos	return 0;
7881.1Sfvdl}
789