exec_elf32.c revision 1.57
11.57Sthorpej/*	$NetBSD: exec_elf32.c,v 1.57 2000/11/14 22:14:53 thorpej 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.57Sthorpej#include <uvm/uvm_extern.h>
911.57Sthorpej
921.47Seeh#ifdef COMPAT_NETBSD32
931.47Seeh#include <compat/netbsd32/netbsd32_exec.h>
941.47Seeh#endif
951.47Seeh
961.1Sfvdl#ifdef COMPAT_LINUX
971.39Schristos#include <compat/linux/common/linux_exec.h>
981.1Sfvdl#endif
991.1Sfvdl
1001.1Sfvdl#ifdef COMPAT_SVR4
1011.1Sfvdl#include <compat/svr4/svr4_exec.h>
1021.1Sfvdl#endif
1031.1Sfvdl
1041.27Sscottb#ifdef COMPAT_IBCS2
1051.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
1061.27Sscottb#endif
1071.27Sscottb
1081.42Schristos#ifdef COMPAT_FREEBSD
1091.42Schristos#include <compat/freebsd/freebsd_exec.h>
1101.42Schristos#endif
1111.42Schristos
1121.54Sthorpejint	ELFNAME(check_header)(Elf_Ehdr *, int);
1131.54Sthorpejint	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
1141.54Sthorpej	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
1151.54Sthorpejvoid	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
1161.54Sthorpej	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
1171.54Sthorpej
1181.54Sthorpejint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
1191.54Sthorpej    Elf_Ehdr *);
1201.54Sthorpejstatic int ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
1211.54Sthorpej    Elf_Ehdr *, char *, Elf_Addr *);
1221.40Schristos
1231.8Schristosextern char sigcode[], esigcode[];
1241.8Schristos#ifdef SYSCALL_DEBUG
1251.56Sjdolecekextern const char * const syscallnames[];
1261.17Scgd#endif
1271.8Schristos
1281.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1291.8Schristos	"netbsd",
1301.8Schristos	NULL,
1311.8Schristos	sendsig,
1321.8Schristos	SYS_syscall,
1331.8Schristos	SYS_MAXSYSCALL,
1341.8Schristos	sysent,
1351.8Schristos#ifdef SYSCALL_DEBUG
1361.8Schristos	syscallnames,
1371.17Scgd#else
1381.8Schristos	NULL,
1391.17Scgd#endif
1401.47Seeh	howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)),
1411.17Scgd	ELFNAME(copyargs),
1421.8Schristos	setregs,
1431.8Schristos	sigcode,
1441.8Schristos	esigcode,
1451.17Scgd};
1461.9Scgd
1471.54Sthorpejint (*ELFNAME(probe_funcs)[])(struct proc *, struct exec_package *,
1481.54Sthorpej    Elf_Ehdr *, char *, Elf_Addr *) = {
1491.47Seeh#if defined(COMPAT_NETBSD32) && (ELFSIZE == 32)
1501.47Seeh	    /* This one should go first so it matches instead of netbsd */
1511.47Seeh	ELFNAME2(netbsd32,probe),
1521.47Seeh#endif
1531.40Schristos	ELFNAME2(netbsd,probe),
1541.42Schristos#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32)
1551.42Schristos	ELFNAME2(freebsd,probe),		/* XXX not 64-bit safe */
1561.42Schristos#endif
1571.38Serh#if defined(COMPAT_LINUX)
1581.38Serh	ELFNAME2(linux,probe),
1591.23Shpeyerl#endif
1601.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1611.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1621.9Scgd#endif
1631.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1641.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1651.27Sscottb#endif
1661.8Schristos};
1671.8Schristos
1681.18Scgd/* round up and down to page boundaries. */
1691.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1701.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1711.8Schristos
1721.8Schristos/*
1731.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1741.1Sfvdl * extra information in case of dynamic binding.
1751.1Sfvdl */
1761.1Sfvdlvoid *
1771.54SthorpejELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo,
1781.54Sthorpej    void *stack, void *argp)
1791.1Sfvdl{
1801.1Sfvdl	size_t len;
1811.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1821.1Sfvdl	struct elf_args *ap;
1831.1Sfvdl
1841.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1851.4Sfvdl	if (!stack)
1861.1Sfvdl		return NULL;
1871.1Sfvdl
1881.22Scgd	a = ai;
1891.22Scgd
1901.1Sfvdl	/*
1911.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1921.1Sfvdl	 * linked binaries
1931.1Sfvdl	 */
1941.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1951.1Sfvdl
1961.46Skleink		a->a_type = AT_PHDR;
1971.46Skleink		a->a_v = ap->arg_phaddr;
1981.1Sfvdl		a++;
1991.1Sfvdl
2001.46Skleink		a->a_type = AT_PHENT;
2011.46Skleink		a->a_v = ap->arg_phentsize;
2021.1Sfvdl		a++;
2031.1Sfvdl
2041.46Skleink		a->a_type = AT_PHNUM;
2051.46Skleink		a->a_v = ap->arg_phnum;
2061.1Sfvdl		a++;
2071.1Sfvdl
2081.46Skleink		a->a_type = AT_PAGESZ;
2091.57Sthorpej		a->a_v = PAGE_SIZE;
2101.1Sfvdl		a++;
2111.1Sfvdl
2121.46Skleink		a->a_type = AT_BASE;
2131.46Skleink		a->a_v = ap->arg_interp;
2141.1Sfvdl		a++;
2151.1Sfvdl
2161.46Skleink		a->a_type = AT_FLAGS;
2171.46Skleink		a->a_v = 0;
2181.1Sfvdl		a++;
2191.1Sfvdl
2201.46Skleink		a->a_type = AT_ENTRY;
2211.46Skleink		a->a_v = ap->arg_entry;
2221.1Sfvdl		a++;
2231.1Sfvdl
2241.17Scgd		free((char *)ap, M_TEMP);
2251.9Scgd		pack->ep_emul_arg = NULL;
2261.1Sfvdl	}
2271.22Scgd
2281.46Skleink	a->a_type = AT_NULL;
2291.46Skleink	a->a_v = 0;
2301.22Scgd	a++;
2311.22Scgd
2321.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2331.22Scgd	if (copyout(ai, stack, len))
2341.22Scgd		return NULL;
2351.43Skleink	stack = (caddr_t)stack + len;
2361.22Scgd
2371.4Sfvdl	return stack;
2381.1Sfvdl}
2391.1Sfvdl
2401.1Sfvdl/*
2411.1Sfvdl * elf_check_header():
2421.1Sfvdl *
2431.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2441.1Sfvdl */
2451.17Scgdint
2461.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type)
2471.1Sfvdl{
2481.3Sthorpej
2491.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2501.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2511.52Skleink		return ENOEXEC;
2521.1Sfvdl
2531.1Sfvdl	switch (eh->e_machine) {
2541.9Scgd
2551.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2561.1Sfvdl
2571.1Sfvdl	default:
2581.1Sfvdl		return ENOEXEC;
2591.1Sfvdl	}
2601.1Sfvdl
2611.1Sfvdl	if (eh->e_type != type)
2621.1Sfvdl		return ENOEXEC;
2631.1Sfvdl
2641.1Sfvdl	return 0;
2651.1Sfvdl}
2661.1Sfvdl
2671.1Sfvdl/*
2681.1Sfvdl * elf_load_psection():
2691.17Scgd *
2701.1Sfvdl * Load a psection at the appropriate address
2711.1Sfvdl */
2721.17Scgdvoid
2731.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
2741.54Sthorpej    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
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.53Smatt			*addr = ELF_TRUNC(*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.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
2971.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
2981.46Skleink	*prot |= (ph->p_flags & 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.46Skleink	if ((ph->p_flags & 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.53Smatt	}
3131.53Smatt	if (psize > 0) {
3141.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3151.53Smatt		    offset, *prot, flags);
3161.53Smatt	}
3171.53Smatt	if (psize < *size) {
3181.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
3191.53Smatt		    *addr + psize, vp, offset + psize, *prot,
3201.53Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
3211.53Smatt	}
3221.1Sfvdl
3231.1Sfvdl	/*
3241.17Scgd	 * Check if we need to extend the size of the segment
3251.17Scgd	 */
3261.1Sfvdl	rm = round_page(*addr + msize);
3271.1Sfvdl	rf = round_page(*addr + *size);
3281.1Sfvdl
3291.1Sfvdl	if (rm != rf) {
3301.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3311.53Smatt		    0, *prot, flags & VMCMD_RELATIVE);
3321.1Sfvdl		*size = msize;
3331.1Sfvdl	}
3341.1Sfvdl}
3351.1Sfvdl
3361.1Sfvdl/*
3371.1Sfvdl * elf_read_from():
3381.1Sfvdl *
3391.1Sfvdl *	Read from vnode into buffer at offset.
3401.1Sfvdl */
3411.7Schristosint
3421.54SthorpejELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off,
3431.54Sthorpej    caddr_t buf, int size)
3441.1Sfvdl{
3451.1Sfvdl	int error;
3461.33Sthorpej	size_t resid;
3471.1Sfvdl
3481.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3491.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3501.1Sfvdl		return error;
3511.1Sfvdl	/*
3521.17Scgd	 * See if we got all of it
3531.17Scgd	 */
3541.1Sfvdl	if (resid != 0)
3551.4Sfvdl		return ENOEXEC;
3561.1Sfvdl	return 0;
3571.1Sfvdl}
3581.1Sfvdl
3591.1Sfvdl/*
3601.1Sfvdl * elf_load_file():
3611.1Sfvdl *
3621.1Sfvdl * Load a file (interpreter/library) pointed to by path
3631.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3641.1Sfvdl * so it might be used externally.
3651.1Sfvdl */
3661.17Scgdint
3671.54SthorpejELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
3681.54Sthorpej    struct exec_vmcmd_set *vcset, u_long *entry, struct elf_args *ap,
3691.54Sthorpej    Elf_Addr *last)
3701.1Sfvdl{
3711.1Sfvdl	int error, i;
3721.1Sfvdl	struct nameidata nd;
3731.14Scgd	struct vnode *vp;
3741.14Scgd	struct vattr attr;
3751.9Scgd	Elf_Ehdr eh;
3761.9Scgd	Elf_Phdr *ph = NULL;
3771.53Smatt	Elf_Phdr *base_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.46Skleink	if ((error = ELFNAME(check_header)(&eh, 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.53Smatt		int flags;
4461.1Sfvdl
4471.1Sfvdl		switch (ph[i].p_type) {
4481.46Skleink		case PT_LOAD:
4491.53Smatt			if (base_ph == NULL) {
4501.53Smatt				addr = *last;
4511.53Smatt				flags = VMCMD_BASE;
4521.53Smatt			} else {
4531.53Smatt				addr = ph[i].p_vaddr - base_ph->p_vaddr;
4541.53Smatt				flags = VMCMD_RELATIVE;
4551.53Smatt			}
4561.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4571.53Smatt			    &size, &prot, flags);
4581.4Sfvdl			/* If entry is within this section it must be text */
4591.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4601.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4611.21Sfvdl				/* XXX */
4621.21Sfvdl				*entry = addr + eh.e_entry;
4631.21Sfvdl#ifdef mips
4641.21Sfvdl				*entry -= ph[i].p_vaddr;
4651.21Sfvdl#endif
4661.1Sfvdl				ap->arg_interp = addr;
4671.1Sfvdl			}
4681.53Smatt			if (base_ph == NULL)
4691.53Smatt				base_ph = &ph[i];
4701.1Sfvdl			addr += size;
4711.1Sfvdl			break;
4721.1Sfvdl
4731.46Skleink		case PT_DYNAMIC:
4741.46Skleink		case PT_PHDR:
4751.46Skleink		case PT_NOTE:
4761.1Sfvdl			break;
4771.1Sfvdl
4781.1Sfvdl		default:
4791.1Sfvdl			break;
4801.1Sfvdl		}
4811.1Sfvdl	}
4821.1Sfvdl
4831.17Scgd	free((char *)ph, M_TEMP);
4841.12Scgd	*last = addr;
4851.14Scgd	vrele(vp);
4861.12Scgd	return 0;
4871.12Scgd
4881.14Scgdbadunlock:
4891.28Sfvdl	VOP_UNLOCK(vp, 0);
4901.14Scgd
4911.1Sfvdlbad:
4921.1Sfvdl	if (ph != NULL)
4931.17Scgd		free((char *)ph, M_TEMP);
4941.12Scgd#ifdef notyet /* XXX cgd 960926 */
4951.12Scgd	(maybe) VOP_CLOSE it
4961.12Scgd#endif
4971.14Scgd	vrele(vp);
4981.1Sfvdl	return error;
4991.1Sfvdl}
5001.1Sfvdl
5011.1Sfvdl/*
5021.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
5031.1Sfvdl *
5041.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5051.1Sfvdl *
5061.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5071.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5081.1Sfvdl * text, data, bss, and stack segments.
5091.1Sfvdl */
5101.1Sfvdlint
5111.54SthorpejELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
5121.1Sfvdl{
5131.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
5141.9Scgd	Elf_Phdr *ph, *pp;
5151.9Scgd	Elf_Addr phdr = 0, pos = 0;
5161.4Sfvdl	int error, i, n, nload;
5171.1Sfvdl	char interp[MAXPATHLEN];
5181.9Scgd	u_long phsize;
5191.1Sfvdl
5201.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
5211.1Sfvdl		return ENOEXEC;
5221.1Sfvdl
5231.45Sfvdl	/*
5241.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
5251.45Sfvdl	 * but other ELF-based systems allow it as well.
5261.45Sfvdl	 */
5271.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
5281.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
5291.1Sfvdl		return ENOEXEC;
5301.1Sfvdl
5311.1Sfvdl	/*
5321.17Scgd	 * check if vnode is in open for writing, because we want to
5331.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5341.17Scgd	 * reasons
5351.17Scgd	 */
5361.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5371.1Sfvdl#ifdef DIAGNOSTIC
5381.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5391.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5401.1Sfvdl#endif
5411.1Sfvdl		return ETXTBSY;
5421.1Sfvdl	}
5431.1Sfvdl	/*
5441.17Scgd	 * Allocate space to hold all the program headers, and read them
5451.17Scgd	 * from the file
5461.17Scgd	 */
5471.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5481.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5491.1Sfvdl
5501.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5511.8Schristos	    (caddr_t) ph, phsize)) != 0)
5521.1Sfvdl		goto bad;
5531.1Sfvdl
5541.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5551.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5561.1Sfvdl
5571.1Sfvdl	interp[0] = '\0';
5581.1Sfvdl
5591.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5601.1Sfvdl		pp = &ph[i];
5611.46Skleink		if (pp->p_type == PT_INTERP) {
5621.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5631.1Sfvdl				goto bad;
5641.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5651.17Scgd			    pp->p_offset, (caddr_t) interp,
5661.17Scgd			    pp->p_filesz)) != 0)
5671.1Sfvdl				goto bad;
5681.1Sfvdl			break;
5691.1Sfvdl		}
5701.1Sfvdl	}
5711.1Sfvdl
5721.9Scgd	/*
5731.8Schristos	 * Setup things for native emulation.
5741.8Schristos	 */
5751.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5761.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5771.8Schristos
5781.1Sfvdl	/*
5791.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5801.1Sfvdl	 * See which one will accept this executable. This currently only
5811.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5821.38Serh	 * and the Alpha.
5831.1Sfvdl	 *
5841.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5851.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5861.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5871.1Sfvdl	 * set the ep_emul field in the exec package structure.
5881.1Sfvdl	 */
5891.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
5901.17Scgd	if (n != 0) {
5911.1Sfvdl		error = ENOEXEC;
5921.1Sfvdl		for (i = 0; i < n && error; i++)
5931.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
5941.17Scgd			    interp, &pos);
5951.8Schristos#ifdef notyet
5961.8Schristos		/*
5971.8Schristos		 * We should really use a signature in our native binaries
5981.8Schristos		 * and have our own probe function for matching binaries,
5991.8Schristos		 * before trying the emulations. For now, if the emulation
6001.8Schristos		 * probes failed we default to native.
6011.8Schristos		 */
6021.1Sfvdl		if (error)
6031.1Sfvdl			goto bad;
6041.8Schristos#endif
6051.1Sfvdl	}
6061.1Sfvdl
6071.1Sfvdl	/*
6081.17Scgd	 * Load all the necessary sections
6091.17Scgd	 */
6101.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
6111.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
6121.9Scgd		u_long size = 0;
6131.1Sfvdl		int prot = 0;
6141.1Sfvdl
6151.1Sfvdl		pp = &ph[i];
6161.1Sfvdl
6171.1Sfvdl		switch (ph[i].p_type) {
6181.46Skleink		case PT_LOAD:
6191.4Sfvdl			/*
6201.4Sfvdl			 * XXX
6211.4Sfvdl			 * Can handle only 2 sections: text and data
6221.4Sfvdl			 */
6231.4Sfvdl			if (nload++ == 2)
6241.4Sfvdl				goto bad;
6251.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6261.53Smatt			    &ph[i], &addr, &size, &prot, 0);
6271.17Scgd
6281.4Sfvdl			/*
6291.4Sfvdl			 * Decide whether it's text or data by looking
6301.4Sfvdl			 * at the entry point.
6311.4Sfvdl			 */
6321.19Scgd			if (eh->e_entry >= addr &&
6331.19Scgd			    eh->e_entry < (addr + size)) {
6341.4Sfvdl				epp->ep_taddr = addr;
6351.4Sfvdl				epp->ep_tsize = size;
6361.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6371.19Scgd					epp->ep_daddr = addr;
6381.19Scgd					epp->ep_dsize = size;
6391.19Scgd				}
6401.4Sfvdl			} else {
6411.4Sfvdl				epp->ep_daddr = addr;
6421.4Sfvdl				epp->ep_dsize = size;
6431.4Sfvdl			}
6441.1Sfvdl			break;
6451.1Sfvdl
6461.46Skleink		case PT_SHLIB:
6471.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6481.1Sfvdl			error = ENOEXEC;
6491.1Sfvdl			goto bad;
6501.27Sscottb#endif
6511.1Sfvdl
6521.46Skleink		case PT_INTERP:
6531.1Sfvdl			/* Already did this one */
6541.46Skleink		case PT_DYNAMIC:
6551.46Skleink		case PT_NOTE:
6561.1Sfvdl			break;
6571.1Sfvdl
6581.46Skleink		case PT_PHDR:
6591.4Sfvdl			/* Note address of program headers (in text segment) */
6601.4Sfvdl			phdr = pp->p_vaddr;
6611.7Schristos			break;
6621.4Sfvdl
6631.1Sfvdl		default:
6641.1Sfvdl			/*
6651.9Scgd			 * Not fatal; we don't need to understand everything.
6661.1Sfvdl			 */
6671.1Sfvdl			break;
6681.1Sfvdl		}
6691.1Sfvdl	}
6701.5Sfvdl
6711.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6721.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6731.5Sfvdl	/*
6741.5Sfvdl	 * If no position to load the interpreter was set by a probe
6751.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6761.5Sfvdl	 * would (i.e. something safely out of the way).
6771.5Sfvdl	 */
6781.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6791.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6801.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6811.1Sfvdl
6821.1Sfvdl	/*
6831.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6841.17Scgd	 * it's interpreter
6851.17Scgd	 */
6861.1Sfvdl	if (interp[0]) {
6871.1Sfvdl		struct elf_args *ap;
6881.1Sfvdl
6891.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6901.17Scgd		    M_TEMP, M_WAITOK);
6911.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6921.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6931.17Scgd			free((char *)ap, M_TEMP);
6941.1Sfvdl			goto bad;
6951.1Sfvdl		}
6961.1Sfvdl		pos += phsize;
6971.4Sfvdl		ap->arg_phaddr = phdr;
6981.1Sfvdl
6991.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
7001.1Sfvdl		ap->arg_phnum = eh->e_phnum;
7011.1Sfvdl		ap->arg_entry = eh->e_entry;
7021.1Sfvdl
7031.1Sfvdl		epp->ep_emul_arg = ap;
7041.1Sfvdl	} else
7051.1Sfvdl		epp->ep_entry = eh->e_entry;
7061.1Sfvdl
7071.8Schristos#ifdef ELF_MAP_PAGE_ZERO
7081.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
7091.57Sthorpej	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
7101.57Sthorpej	    epp->ep_vp, 0, VM_PROT_READ);
7111.8Schristos#endif
7121.17Scgd	free((char *)ph, M_TEMP);
7131.48Schs	vn_marktext(epp->ep_vp);
7141.9Scgd	return exec_elf_setup_stack(p, epp);
7151.1Sfvdl
7161.1Sfvdlbad:
7171.17Scgd	free((char *)ph, M_TEMP);
7181.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7191.1Sfvdl	return ENOEXEC;
7201.40Schristos}
7211.40Schristos
7221.47Seehint
7231.54SthorpejELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
7241.54Sthorpej    Elf_Ehdr *eh)
7251.40Schristos{
7261.40Schristos	Elf_Phdr *hph, *ph;
7271.46Skleink	Elf_Nhdr *np = NULL;
7281.40Schristos	size_t phsize;
7291.40Schristos	int error;
7301.40Schristos
7311.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
7321.40Schristos	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
7331.41Schristos	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
7341.41Schristos	    (caddr_t)hph, phsize)) != 0)
7351.40Schristos		goto out1;
7361.40Schristos
7371.40Schristos	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
7381.46Skleink		if (ph->p_type != PT_NOTE ||
7391.46Skleink		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
7401.40Schristos			continue;
7411.40Schristos
7421.46Skleink		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
7431.40Schristos		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
7441.40Schristos		    (caddr_t)np, ph->p_filesz)) != 0)
7451.40Schristos			goto out2;
7461.40Schristos
7471.46Skleink		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
7481.40Schristos			free(np, M_TEMP);
7491.40Schristos			np = NULL;
7501.40Schristos			continue;
7511.40Schristos		}
7521.40Schristos
7531.40Schristos		/* Check the name and description sizes. */
7541.46Skleink		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
7551.46Skleink		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
7561.40Schristos			goto out3;
7571.40Schristos
7581.40Schristos		/* Is the name "NetBSD\0\0"? */
7591.40Schristos		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
7601.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
7611.40Schristos			goto out3;
7621.40Schristos
7631.40Schristos		/* XXX: We could check for the specific emulation here */
7641.40Schristos		/* All checks succeeded. */
7651.40Schristos		error = 0;
7661.40Schristos		goto out2;
7671.40Schristos	}
7681.40Schristos
7691.40Schristosout3:
7701.40Schristos	error = ENOEXEC;
7711.40Schristosout2:
7721.40Schristos	if (np)
7731.40Schristos		free(np, M_TEMP);
7741.40Schristosout1:
7751.40Schristos	free(hph, M_TEMP);
7761.40Schristos	return error;
7771.40Schristos}
7781.40Schristos
7791.40Schristosstatic int
7801.54SthorpejELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
7811.54Sthorpej    Elf_Ehdr *eh, char *itp, Elf_Addr *pos)
7821.40Schristos{
7831.40Schristos	int error;
7841.40Schristos
7851.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
7861.40Schristos		return error;
7871.40Schristos	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
7881.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
7891.40Schristos	return 0;
7901.1Sfvdl}
791