exec_elf32.c revision 1.53
11.53Smatt/*	$NetBSD: exec_elf32.c,v 1.53 2000/07/13 02:35:25 matt 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/systm.h>
801.1Sfvdl#include <sys/kernel.h>
811.1Sfvdl#include <sys/proc.h>
821.1Sfvdl#include <sys/malloc.h>
831.1Sfvdl#include <sys/namei.h>
841.1Sfvdl#include <sys/vnode.h>
851.1Sfvdl#include <sys/exec.h>
861.1Sfvdl#include <sys/exec_elf.h>
871.9Scgd#include <sys/fcntl.h>
881.8Schristos#include <sys/syscall.h>
891.8Schristos#include <sys/signalvar.h>
901.14Scgd#include <sys/mount.h>
911.14Scgd#include <sys/stat.h>
921.1Sfvdl
931.1Sfvdl#include <sys/mman.h>
941.1Sfvdl
951.1Sfvdl#include <machine/cpu.h>
961.1Sfvdl#include <machine/reg.h>
971.1Sfvdl
981.47Seeh#ifdef COMPAT_NETBSD32
991.47Seeh#include <compat/netbsd32/netbsd32_exec.h>
1001.47Seeh#endif
1011.47Seeh
1021.1Sfvdl#ifdef COMPAT_LINUX
1031.39Schristos#include <compat/linux/common/linux_exec.h>
1041.1Sfvdl#endif
1051.1Sfvdl
1061.1Sfvdl#ifdef COMPAT_SVR4
1071.1Sfvdl#include <compat/svr4/svr4_exec.h>
1081.1Sfvdl#endif
1091.1Sfvdl
1101.27Sscottb#ifdef COMPAT_IBCS2
1111.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
1121.27Sscottb#endif
1131.27Sscottb
1141.42Schristos#ifdef COMPAT_FREEBSD
1151.42Schristos#include <compat/freebsd/freebsd_exec.h>
1161.42Schristos#endif
1171.42Schristos
1181.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
1191.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
1201.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
1211.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
1221.53Smatt	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int));
1231.1Sfvdl
1241.47Seehint ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *,
1251.40Schristos    Elf_Ehdr *));
1261.40Schristosstatic int ELFNAME2(netbsd,probe) __P((struct proc *, struct exec_package *,
1271.40Schristos    Elf_Ehdr *, char *, Elf_Addr *));
1281.40Schristos
1291.8Schristosextern char sigcode[], esigcode[];
1301.8Schristos#ifdef SYSCALL_DEBUG
1311.8Schristosextern char *syscallnames[];
1321.17Scgd#endif
1331.8Schristos
1341.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
1351.8Schristos	"netbsd",
1361.8Schristos	NULL,
1371.8Schristos	sendsig,
1381.8Schristos	SYS_syscall,
1391.8Schristos	SYS_MAXSYSCALL,
1401.8Schristos	sysent,
1411.8Schristos#ifdef SYSCALL_DEBUG
1421.8Schristos	syscallnames,
1431.17Scgd#else
1441.8Schristos	NULL,
1451.17Scgd#endif
1461.47Seeh	howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)),
1471.17Scgd	ELFNAME(copyargs),
1481.8Schristos	setregs,
1491.8Schristos	sigcode,
1501.8Schristos	esigcode,
1511.17Scgd};
1521.9Scgd
1531.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1541.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1551.47Seeh#if defined(COMPAT_NETBSD32) && (ELFSIZE == 32)
1561.47Seeh	    /* This one should go first so it matches instead of netbsd */
1571.47Seeh	ELFNAME2(netbsd32,probe),
1581.47Seeh#endif
1591.40Schristos	ELFNAME2(netbsd,probe),
1601.42Schristos#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32)
1611.42Schristos	ELFNAME2(freebsd,probe),		/* XXX not 64-bit safe */
1621.42Schristos#endif
1631.38Serh#if defined(COMPAT_LINUX)
1641.38Serh	ELFNAME2(linux,probe),
1651.23Shpeyerl#endif
1661.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1671.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1681.9Scgd#endif
1691.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1701.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1711.27Sscottb#endif
1721.8Schristos};
1731.8Schristos
1741.18Scgd/* round up and down to page boundaries. */
1751.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1761.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1771.8Schristos
1781.8Schristos/*
1791.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1801.1Sfvdl * extra information in case of dynamic binding.
1811.1Sfvdl */
1821.1Sfvdlvoid *
1831.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1841.1Sfvdl	struct exec_package *pack;
1851.1Sfvdl	struct ps_strings *arginfo;
1861.1Sfvdl	void *stack;
1871.1Sfvdl	void *argp;
1881.1Sfvdl{
1891.1Sfvdl	size_t len;
1901.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1911.1Sfvdl	struct elf_args *ap;
1921.1Sfvdl
1931.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1941.4Sfvdl	if (!stack)
1951.1Sfvdl		return NULL;
1961.1Sfvdl
1971.22Scgd	a = ai;
1981.22Scgd
1991.1Sfvdl	/*
2001.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
2011.1Sfvdl	 * linked binaries
2021.1Sfvdl	 */
2031.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
2041.1Sfvdl
2051.46Skleink		a->a_type = AT_PHDR;
2061.46Skleink		a->a_v = ap->arg_phaddr;
2071.1Sfvdl		a++;
2081.1Sfvdl
2091.46Skleink		a->a_type = AT_PHENT;
2101.46Skleink		a->a_v = ap->arg_phentsize;
2111.1Sfvdl		a++;
2121.1Sfvdl
2131.46Skleink		a->a_type = AT_PHNUM;
2141.46Skleink		a->a_v = ap->arg_phnum;
2151.1Sfvdl		a++;
2161.1Sfvdl
2171.46Skleink		a->a_type = AT_PAGESZ;
2181.46Skleink		a->a_v = NBPG;
2191.1Sfvdl		a++;
2201.1Sfvdl
2211.46Skleink		a->a_type = AT_BASE;
2221.46Skleink		a->a_v = ap->arg_interp;
2231.1Sfvdl		a++;
2241.1Sfvdl
2251.46Skleink		a->a_type = AT_FLAGS;
2261.46Skleink		a->a_v = 0;
2271.1Sfvdl		a++;
2281.1Sfvdl
2291.46Skleink		a->a_type = AT_ENTRY;
2301.46Skleink		a->a_v = ap->arg_entry;
2311.1Sfvdl		a++;
2321.1Sfvdl
2331.17Scgd		free((char *)ap, M_TEMP);
2341.9Scgd		pack->ep_emul_arg = NULL;
2351.1Sfvdl	}
2361.22Scgd
2371.46Skleink	a->a_type = AT_NULL;
2381.46Skleink	a->a_v = 0;
2391.22Scgd	a++;
2401.22Scgd
2411.34Sperry	len = (a - ai) * sizeof(AuxInfo);
2421.22Scgd	if (copyout(ai, stack, len))
2431.22Scgd		return NULL;
2441.43Skleink	stack = (caddr_t)stack + len;
2451.22Scgd
2461.4Sfvdl	return stack;
2471.1Sfvdl}
2481.1Sfvdl
2491.1Sfvdl/*
2501.1Sfvdl * elf_check_header():
2511.1Sfvdl *
2521.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2531.1Sfvdl */
2541.17Scgdint
2551.9ScgdELFNAME(check_header)(eh, type)
2561.9Scgd	Elf_Ehdr *eh;
2571.1Sfvdl	int type;
2581.1Sfvdl{
2591.3Sthorpej
2601.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2611.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2621.52Skleink		return ENOEXEC;
2631.1Sfvdl
2641.1Sfvdl	switch (eh->e_machine) {
2651.9Scgd
2661.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2671.1Sfvdl
2681.1Sfvdl	default:
2691.1Sfvdl		return ENOEXEC;
2701.1Sfvdl	}
2711.1Sfvdl
2721.1Sfvdl	if (eh->e_type != type)
2731.1Sfvdl		return ENOEXEC;
2741.1Sfvdl
2751.1Sfvdl	return 0;
2761.1Sfvdl}
2771.1Sfvdl
2781.1Sfvdl/*
2791.1Sfvdl * elf_load_psection():
2801.17Scgd *
2811.1Sfvdl * Load a psection at the appropriate address
2821.1Sfvdl */
2831.17Scgdvoid
2841.53SmattELFNAME(load_psection)(vcset, vp, ph, addr, size, prot, flags)
2851.1Sfvdl	struct exec_vmcmd_set *vcset;
2861.1Sfvdl	struct vnode *vp;
2871.53Smatt	const Elf_Phdr *ph;
2881.9Scgd	Elf_Addr *addr;
2891.1Sfvdl	u_long *size;
2901.1Sfvdl	int *prot;
2911.53Smatt	int flags;
2921.1Sfvdl{
2931.8Schristos	u_long uaddr, msize, psize, rm, rf;
2941.1Sfvdl	long diff, offset;
2951.1Sfvdl
2961.1Sfvdl	/*
2971.17Scgd	 * If the user specified an address, then we load there.
2981.17Scgd	 */
2991.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
3001.1Sfvdl		if (ph->p_align > 1) {
3011.53Smatt			*addr = ELF_TRUNC(*addr, ph->p_align);
3021.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
3031.1Sfvdl		} else
3041.1Sfvdl			uaddr = ph->p_vaddr;
3051.1Sfvdl		diff = ph->p_vaddr - uaddr;
3061.1Sfvdl	} else {
3071.1Sfvdl		*addr = uaddr = ph->p_vaddr;
3081.1Sfvdl		if (ph->p_align > 1)
3091.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
3101.1Sfvdl		diff = uaddr - *addr;
3111.1Sfvdl	}
3121.1Sfvdl
3131.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
3141.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
3151.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
3161.1Sfvdl
3171.1Sfvdl	offset = ph->p_offset - diff;
3181.1Sfvdl	*size = ph->p_filesz + diff;
3191.1Sfvdl	msize = ph->p_memsz + diff;
3201.8Schristos	psize = round_page(*size);
3211.1Sfvdl
3221.46Skleink	if ((ph->p_flags & PF_W) != 0) {
3231.8Schristos		/*
3241.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
3251.8Schristos		 * data page if it's not page aligned we map the last page
3261.8Schristos		 * readvn.
3271.8Schristos		 */
3281.8Schristos		psize = trunc_page(*size);
3291.53Smatt	}
3301.53Smatt	if (psize > 0) {
3311.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
3321.53Smatt		    offset, *prot, flags);
3331.53Smatt	}
3341.53Smatt	if (psize < *size) {
3351.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
3361.53Smatt		    *addr + psize, vp, offset + psize, *prot,
3371.53Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
3381.53Smatt	}
3391.1Sfvdl
3401.1Sfvdl	/*
3411.17Scgd	 * Check if we need to extend the size of the segment
3421.17Scgd	 */
3431.1Sfvdl	rm = round_page(*addr + msize);
3441.1Sfvdl	rf = round_page(*addr + *size);
3451.1Sfvdl
3461.1Sfvdl	if (rm != rf) {
3471.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
3481.53Smatt		    0, *prot, flags & VMCMD_RELATIVE);
3491.1Sfvdl		*size = msize;
3501.1Sfvdl	}
3511.1Sfvdl}
3521.1Sfvdl
3531.1Sfvdl/*
3541.1Sfvdl * elf_read_from():
3551.1Sfvdl *
3561.1Sfvdl *	Read from vnode into buffer at offset.
3571.1Sfvdl */
3581.7Schristosint
3591.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3601.1Sfvdl	struct vnode *vp;
3611.1Sfvdl	u_long off;
3621.1Sfvdl	struct proc *p;
3631.1Sfvdl	caddr_t buf;
3641.1Sfvdl	int size;
3651.1Sfvdl{
3661.1Sfvdl	int error;
3671.33Sthorpej	size_t resid;
3681.1Sfvdl
3691.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3701.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3711.1Sfvdl		return error;
3721.1Sfvdl	/*
3731.17Scgd	 * See if we got all of it
3741.17Scgd	 */
3751.1Sfvdl	if (resid != 0)
3761.4Sfvdl		return ENOEXEC;
3771.1Sfvdl	return 0;
3781.1Sfvdl}
3791.1Sfvdl
3801.1Sfvdl/*
3811.1Sfvdl * elf_load_file():
3821.1Sfvdl *
3831.1Sfvdl * Load a file (interpreter/library) pointed to by path
3841.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3851.1Sfvdl * so it might be used externally.
3861.1Sfvdl */
3871.17Scgdint
3881.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3891.1Sfvdl	struct proc *p;
3901.14Scgd	struct exec_package *epp;
3911.1Sfvdl	char *path;
3921.1Sfvdl	struct exec_vmcmd_set *vcset;
3931.1Sfvdl	u_long *entry;
3941.1Sfvdl	struct elf_args	*ap;
3951.9Scgd	Elf_Addr *last;
3961.1Sfvdl{
3971.1Sfvdl	int error, i;
3981.1Sfvdl	struct nameidata nd;
3991.14Scgd	struct vnode *vp;
4001.14Scgd	struct vattr attr;
4011.9Scgd	Elf_Ehdr eh;
4021.9Scgd	Elf_Phdr *ph = NULL;
4031.53Smatt	Elf_Phdr *base_ph = NULL;
4041.1Sfvdl	u_long phsize;
4051.1Sfvdl	char *bp = NULL;
4061.9Scgd	Elf_Addr addr = *last;
4071.1Sfvdl
4081.1Sfvdl	bp = path;
4091.1Sfvdl	/*
4101.17Scgd	 * 1. open file
4111.17Scgd	 * 2. read filehdr
4121.17Scgd	 * 3. map text, data, and bss out of it using VM_*
4131.17Scgd	 */
4141.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
4151.12Scgd	if ((error = namei(&nd)) != 0)
4161.1Sfvdl		return error;
4171.14Scgd	vp = nd.ni_vp;
4181.14Scgd
4191.26Smycroft	/*
4201.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
4211.26Smycroft	 * file, we don't allow it to be used.
4221.26Smycroft	 */
4231.14Scgd	if (vp->v_type != VREG) {
4241.14Scgd		error = EACCES;
4251.14Scgd		goto badunlock;
4261.14Scgd	}
4271.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
4281.26Smycroft		goto badunlock;
4291.14Scgd
4301.17Scgd	/* get attributes */
4311.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
4321.14Scgd		goto badunlock;
4331.14Scgd
4341.14Scgd	/*
4351.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
4361.15Scgd	 * we will be executing code from it, so if the mount point
4371.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
4381.14Scgd	 */
4391.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
4401.14Scgd		error = EACCES;
4411.14Scgd		goto badunlock;
4421.14Scgd	}
4431.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
4441.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
4451.14Scgd
4461.12Scgd#ifdef notyet /* XXX cgd 960926 */
4471.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
4481.12Scgd#endif
4491.28Sfvdl	VOP_UNLOCK(vp, 0);
4501.12Scgd
4511.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
4521.17Scgd	    sizeof(eh))) != 0)
4531.1Sfvdl		goto bad;
4541.1Sfvdl
4551.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
4561.1Sfvdl		goto bad;
4571.1Sfvdl
4581.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4591.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4601.1Sfvdl
4611.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4621.9Scgd	    (caddr_t) ph, phsize)) != 0)
4631.1Sfvdl		goto bad;
4641.1Sfvdl
4651.1Sfvdl	/*
4661.17Scgd	 * Load all the necessary sections
4671.17Scgd	 */
4681.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4691.1Sfvdl		u_long size = 0;
4701.1Sfvdl		int prot = 0;
4711.53Smatt		int flags;
4721.1Sfvdl
4731.1Sfvdl		switch (ph[i].p_type) {
4741.46Skleink		case PT_LOAD:
4751.53Smatt			if (base_ph == NULL) {
4761.53Smatt				addr = *last;
4771.53Smatt				flags = VMCMD_BASE;
4781.53Smatt			} else {
4791.53Smatt				addr = ph[i].p_vaddr - base_ph->p_vaddr;
4801.53Smatt				flags = VMCMD_RELATIVE;
4811.53Smatt			}
4821.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4831.53Smatt			    &size, &prot, flags);
4841.4Sfvdl			/* If entry is within this section it must be text */
4851.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4861.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4871.21Sfvdl				/* XXX */
4881.21Sfvdl				*entry = addr + eh.e_entry;
4891.21Sfvdl#ifdef mips
4901.21Sfvdl				*entry -= ph[i].p_vaddr;
4911.21Sfvdl#endif
4921.1Sfvdl				ap->arg_interp = addr;
4931.1Sfvdl			}
4941.53Smatt			if (base_ph == NULL)
4951.53Smatt				base_ph = &ph[i];
4961.1Sfvdl			addr += size;
4971.1Sfvdl			break;
4981.1Sfvdl
4991.46Skleink		case PT_DYNAMIC:
5001.46Skleink		case PT_PHDR:
5011.46Skleink		case PT_NOTE:
5021.1Sfvdl			break;
5031.1Sfvdl
5041.1Sfvdl		default:
5051.1Sfvdl			break;
5061.1Sfvdl		}
5071.1Sfvdl	}
5081.1Sfvdl
5091.17Scgd	free((char *)ph, M_TEMP);
5101.12Scgd	*last = addr;
5111.14Scgd	vrele(vp);
5121.12Scgd	return 0;
5131.12Scgd
5141.14Scgdbadunlock:
5151.28Sfvdl	VOP_UNLOCK(vp, 0);
5161.14Scgd
5171.1Sfvdlbad:
5181.1Sfvdl	if (ph != NULL)
5191.17Scgd		free((char *)ph, M_TEMP);
5201.12Scgd#ifdef notyet /* XXX cgd 960926 */
5211.12Scgd	(maybe) VOP_CLOSE it
5221.12Scgd#endif
5231.14Scgd	vrele(vp);
5241.1Sfvdl	return error;
5251.1Sfvdl}
5261.1Sfvdl
5271.1Sfvdl/*
5281.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
5291.1Sfvdl *
5301.1Sfvdl * First, set of the various offsets/lengths in the exec package.
5311.1Sfvdl *
5321.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
5331.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
5341.1Sfvdl * text, data, bss, and stack segments.
5351.1Sfvdl */
5361.1Sfvdlint
5371.9ScgdELFNAME2(exec,makecmds)(p, epp)
5381.1Sfvdl	struct proc *p;
5391.1Sfvdl	struct exec_package *epp;
5401.1Sfvdl{
5411.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
5421.9Scgd	Elf_Phdr *ph, *pp;
5431.9Scgd	Elf_Addr phdr = 0, pos = 0;
5441.4Sfvdl	int error, i, n, nload;
5451.1Sfvdl	char interp[MAXPATHLEN];
5461.9Scgd	u_long phsize;
5471.1Sfvdl
5481.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
5491.1Sfvdl		return ENOEXEC;
5501.1Sfvdl
5511.45Sfvdl	/*
5521.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
5531.45Sfvdl	 * but other ELF-based systems allow it as well.
5541.45Sfvdl	 */
5551.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
5561.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
5571.1Sfvdl		return ENOEXEC;
5581.1Sfvdl
5591.1Sfvdl	/*
5601.17Scgd	 * check if vnode is in open for writing, because we want to
5611.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
5621.17Scgd	 * reasons
5631.17Scgd	 */
5641.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
5651.1Sfvdl#ifdef DIAGNOSTIC
5661.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
5671.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
5681.1Sfvdl#endif
5691.1Sfvdl		return ETXTBSY;
5701.1Sfvdl	}
5711.1Sfvdl	/*
5721.17Scgd	 * Allocate space to hold all the program headers, and read them
5731.17Scgd	 * from the file
5741.17Scgd	 */
5751.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5761.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5771.1Sfvdl
5781.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5791.8Schristos	    (caddr_t) ph, phsize)) != 0)
5801.1Sfvdl		goto bad;
5811.1Sfvdl
5821.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5831.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5841.1Sfvdl
5851.1Sfvdl	interp[0] = '\0';
5861.1Sfvdl
5871.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5881.1Sfvdl		pp = &ph[i];
5891.46Skleink		if (pp->p_type == PT_INTERP) {
5901.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5911.1Sfvdl				goto bad;
5921.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5931.17Scgd			    pp->p_offset, (caddr_t) interp,
5941.17Scgd			    pp->p_filesz)) != 0)
5951.1Sfvdl				goto bad;
5961.1Sfvdl			break;
5971.1Sfvdl		}
5981.1Sfvdl	}
5991.1Sfvdl
6001.9Scgd	/*
6011.8Schristos	 * Setup things for native emulation.
6021.8Schristos	 */
6031.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
6041.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
6051.8Schristos
6061.1Sfvdl	/*
6071.1Sfvdl	 * On the same architecture, we may be emulating different systems.
6081.1Sfvdl	 * See which one will accept this executable. This currently only
6091.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
6101.38Serh	 * and the Alpha.
6111.1Sfvdl	 *
6121.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
6131.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
6141.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
6151.1Sfvdl	 * set the ep_emul field in the exec package structure.
6161.1Sfvdl	 */
6171.34Sperry	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
6181.17Scgd	if (n != 0) {
6191.1Sfvdl		error = ENOEXEC;
6201.1Sfvdl		for (i = 0; i < n && error; i++)
6211.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
6221.17Scgd			    interp, &pos);
6231.8Schristos#ifdef notyet
6241.8Schristos		/*
6251.8Schristos		 * We should really use a signature in our native binaries
6261.8Schristos		 * and have our own probe function for matching binaries,
6271.8Schristos		 * before trying the emulations. For now, if the emulation
6281.8Schristos		 * probes failed we default to native.
6291.8Schristos		 */
6301.1Sfvdl		if (error)
6311.1Sfvdl			goto bad;
6321.8Schristos#endif
6331.1Sfvdl	}
6341.1Sfvdl
6351.1Sfvdl	/*
6361.17Scgd	 * Load all the necessary sections
6371.17Scgd	 */
6381.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
6391.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
6401.9Scgd		u_long size = 0;
6411.1Sfvdl		int prot = 0;
6421.1Sfvdl
6431.1Sfvdl		pp = &ph[i];
6441.1Sfvdl
6451.1Sfvdl		switch (ph[i].p_type) {
6461.46Skleink		case PT_LOAD:
6471.4Sfvdl			/*
6481.4Sfvdl			 * XXX
6491.4Sfvdl			 * Can handle only 2 sections: text and data
6501.4Sfvdl			 */
6511.4Sfvdl			if (nload++ == 2)
6521.4Sfvdl				goto bad;
6531.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
6541.53Smatt			    &ph[i], &addr, &size, &prot, 0);
6551.17Scgd
6561.4Sfvdl			/*
6571.4Sfvdl			 * Decide whether it's text or data by looking
6581.4Sfvdl			 * at the entry point.
6591.4Sfvdl			 */
6601.19Scgd			if (eh->e_entry >= addr &&
6611.19Scgd			    eh->e_entry < (addr + size)) {
6621.4Sfvdl				epp->ep_taddr = addr;
6631.4Sfvdl				epp->ep_tsize = size;
6641.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
6651.19Scgd					epp->ep_daddr = addr;
6661.19Scgd					epp->ep_dsize = size;
6671.19Scgd				}
6681.4Sfvdl			} else {
6691.4Sfvdl				epp->ep_daddr = addr;
6701.4Sfvdl				epp->ep_dsize = size;
6711.4Sfvdl			}
6721.1Sfvdl			break;
6731.1Sfvdl
6741.46Skleink		case PT_SHLIB:
6751.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6761.1Sfvdl			error = ENOEXEC;
6771.1Sfvdl			goto bad;
6781.27Sscottb#endif
6791.1Sfvdl
6801.46Skleink		case PT_INTERP:
6811.1Sfvdl			/* Already did this one */
6821.46Skleink		case PT_DYNAMIC:
6831.46Skleink		case PT_NOTE:
6841.1Sfvdl			break;
6851.1Sfvdl
6861.46Skleink		case PT_PHDR:
6871.4Sfvdl			/* Note address of program headers (in text segment) */
6881.4Sfvdl			phdr = pp->p_vaddr;
6891.7Schristos			break;
6901.4Sfvdl
6911.1Sfvdl		default:
6921.1Sfvdl			/*
6931.9Scgd			 * Not fatal; we don't need to understand everything.
6941.1Sfvdl			 */
6951.1Sfvdl			break;
6961.1Sfvdl		}
6971.1Sfvdl	}
6981.5Sfvdl
6991.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
7001.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
7011.5Sfvdl	/*
7021.5Sfvdl	 * If no position to load the interpreter was set by a probe
7031.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
7041.5Sfvdl	 * would (i.e. something safely out of the way).
7051.5Sfvdl	 */
7061.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
7071.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
7081.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
7091.1Sfvdl
7101.1Sfvdl	/*
7111.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
7121.17Scgd	 * it's interpreter
7131.17Scgd	 */
7141.1Sfvdl	if (interp[0]) {
7151.1Sfvdl		struct elf_args *ap;
7161.1Sfvdl
7171.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
7181.17Scgd		    M_TEMP, M_WAITOK);
7191.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
7201.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
7211.17Scgd			free((char *)ap, M_TEMP);
7221.1Sfvdl			goto bad;
7231.1Sfvdl		}
7241.1Sfvdl		pos += phsize;
7251.4Sfvdl		ap->arg_phaddr = phdr;
7261.1Sfvdl
7271.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
7281.1Sfvdl		ap->arg_phnum = eh->e_phnum;
7291.1Sfvdl		ap->arg_entry = eh->e_entry;
7301.1Sfvdl
7311.1Sfvdl		epp->ep_emul_arg = ap;
7321.1Sfvdl	} else
7331.1Sfvdl		epp->ep_entry = eh->e_entry;
7341.1Sfvdl
7351.8Schristos#ifdef ELF_MAP_PAGE_ZERO
7361.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
7371.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
7381.8Schristos	    VM_PROT_READ);
7391.8Schristos#endif
7401.17Scgd	free((char *)ph, M_TEMP);
7411.48Schs	vn_marktext(epp->ep_vp);
7421.9Scgd	return exec_elf_setup_stack(p, epp);
7431.1Sfvdl
7441.1Sfvdlbad:
7451.17Scgd	free((char *)ph, M_TEMP);
7461.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
7471.1Sfvdl	return ENOEXEC;
7481.40Schristos}
7491.40Schristos
7501.47Seehint
7511.40SchristosELFNAME2(netbsd,signature)(p, epp, eh)
7521.40Schristos	struct proc *p;
7531.40Schristos	struct exec_package *epp;
7541.40Schristos	Elf_Ehdr *eh;
7551.40Schristos{
7561.40Schristos	Elf_Phdr *hph, *ph;
7571.46Skleink	Elf_Nhdr *np = NULL;
7581.40Schristos	size_t phsize;
7591.40Schristos	int error;
7601.40Schristos
7611.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
7621.40Schristos	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
7631.41Schristos	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
7641.41Schristos	    (caddr_t)hph, phsize)) != 0)
7651.40Schristos		goto out1;
7661.40Schristos
7671.40Schristos	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
7681.46Skleink		if (ph->p_type != PT_NOTE ||
7691.46Skleink		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
7701.40Schristos			continue;
7711.40Schristos
7721.46Skleink		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
7731.40Schristos		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
7741.40Schristos		    (caddr_t)np, ph->p_filesz)) != 0)
7751.40Schristos			goto out2;
7761.40Schristos
7771.46Skleink		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
7781.40Schristos			free(np, M_TEMP);
7791.40Schristos			np = NULL;
7801.40Schristos			continue;
7811.40Schristos		}
7821.40Schristos
7831.40Schristos		/* Check the name and description sizes. */
7841.46Skleink		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
7851.46Skleink		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
7861.40Schristos			goto out3;
7871.40Schristos
7881.40Schristos		/* Is the name "NetBSD\0\0"? */
7891.40Schristos		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
7901.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
7911.40Schristos			goto out3;
7921.40Schristos
7931.40Schristos		/* XXX: We could check for the specific emulation here */
7941.40Schristos		/* All checks succeeded. */
7951.40Schristos		error = 0;
7961.40Schristos		goto out2;
7971.40Schristos	}
7981.40Schristos
7991.40Schristosout3:
8001.40Schristos	error = ENOEXEC;
8011.40Schristosout2:
8021.40Schristos	if (np)
8031.40Schristos		free(np, M_TEMP);
8041.40Schristosout1:
8051.40Schristos	free(hph, M_TEMP);
8061.40Schristos	return error;
8071.40Schristos}
8081.40Schristos
8091.40Schristosstatic int
8101.40SchristosELFNAME2(netbsd,probe)(p, epp, eh, itp, pos)
8111.40Schristos	struct proc *p;
8121.40Schristos	struct exec_package *epp;
8131.40Schristos	Elf_Ehdr *eh;
8141.40Schristos	char *itp;
8151.40Schristos	Elf_Addr *pos;
8161.40Schristos{
8171.40Schristos	int error;
8181.40Schristos
8191.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
8201.40Schristos		return error;
8211.40Schristos	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
8221.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
8231.40Schristos	return 0;
8241.1Sfvdl}
825