exec_elf32.c revision 1.59
11.59Smrg/*	$NetBSD: exec_elf32.c,v 1.59 2000/11/28 13:10:41 mrg 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.58Sjdolecek#include "opt_compat_ibcs2.h"	/* XXX quirk for PT_SHLIB section */
721.9Scgd
731.1Sfvdl#include <sys/param.h>
741.1Sfvdl#include <sys/proc.h>
751.1Sfvdl#include <sys/malloc.h>
761.1Sfvdl#include <sys/namei.h>
771.1Sfvdl#include <sys/vnode.h>
781.1Sfvdl#include <sys/exec.h>
791.1Sfvdl#include <sys/exec_elf.h>
801.8Schristos#include <sys/syscall.h>
811.8Schristos#include <sys/signalvar.h>
821.14Scgd#include <sys/mount.h>
831.14Scgd#include <sys/stat.h>
841.1Sfvdl
851.58Sjdolecek#include <machine/cpu.h>
861.58Sjdolecek#include <machine/reg.h>
871.57Sthorpej
881.58Sjdolecekextern const struct emul emul_netbsd;
891.42Schristos
901.54Sthorpejint	ELFNAME(check_header)(Elf_Ehdr *, int);
911.54Sthorpejint	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
921.54Sthorpej	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
931.54Sthorpejvoid	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
941.54Sthorpej	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
951.54Sthorpej
961.59Smrgint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
971.54Sthorpej    Elf_Ehdr *);
981.58Sjdolecekint ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
991.58Sjdolecek    void *, char *, vaddr_t *);
1001.8Schristos
1011.18Scgd/* round up and down to page boundaries. */
1021.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1031.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1041.8Schristos
1051.8Schristos/*
1061.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1071.1Sfvdl * extra information in case of dynamic binding.
1081.1Sfvdl */
1091.1Sfvdlvoid *
1101.54SthorpejELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo,
1111.54Sthorpej    void *stack, void *argp)
1121.1Sfvdl{
1131.1Sfvdl	size_t len;
1141.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1151.1Sfvdl	struct elf_args *ap;
1161.1Sfvdl
1171.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1181.4Sfvdl	if (!stack)
1191.1Sfvdl		return NULL;
1201.1Sfvdl
1211.22Scgd	a = ai;
1221.22Scgd
1231.1Sfvdl	/*
1241.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1251.1Sfvdl	 * linked binaries
1261.1Sfvdl	 */
1271.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1281.1Sfvdl
1291.46Skleink		a->a_type = AT_PHDR;
1301.46Skleink		a->a_v = ap->arg_phaddr;
1311.1Sfvdl		a++;
1321.1Sfvdl
1331.46Skleink		a->a_type = AT_PHENT;
1341.46Skleink		a->a_v = ap->arg_phentsize;
1351.1Sfvdl		a++;
1361.1Sfvdl
1371.46Skleink		a->a_type = AT_PHNUM;
1381.46Skleink		a->a_v = ap->arg_phnum;
1391.1Sfvdl		a++;
1401.1Sfvdl
1411.46Skleink		a->a_type = AT_PAGESZ;
1421.57Sthorpej		a->a_v = PAGE_SIZE;
1431.1Sfvdl		a++;
1441.1Sfvdl
1451.46Skleink		a->a_type = AT_BASE;
1461.46Skleink		a->a_v = ap->arg_interp;
1471.1Sfvdl		a++;
1481.1Sfvdl
1491.46Skleink		a->a_type = AT_FLAGS;
1501.46Skleink		a->a_v = 0;
1511.1Sfvdl		a++;
1521.1Sfvdl
1531.46Skleink		a->a_type = AT_ENTRY;
1541.46Skleink		a->a_v = ap->arg_entry;
1551.1Sfvdl		a++;
1561.1Sfvdl
1571.17Scgd		free((char *)ap, M_TEMP);
1581.9Scgd		pack->ep_emul_arg = NULL;
1591.1Sfvdl	}
1601.22Scgd
1611.46Skleink	a->a_type = AT_NULL;
1621.46Skleink	a->a_v = 0;
1631.22Scgd	a++;
1641.22Scgd
1651.34Sperry	len = (a - ai) * sizeof(AuxInfo);
1661.22Scgd	if (copyout(ai, stack, len))
1671.22Scgd		return NULL;
1681.43Skleink	stack = (caddr_t)stack + len;
1691.22Scgd
1701.4Sfvdl	return stack;
1711.1Sfvdl}
1721.1Sfvdl
1731.1Sfvdl/*
1741.1Sfvdl * elf_check_header():
1751.1Sfvdl *
1761.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
1771.1Sfvdl */
1781.17Scgdint
1791.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type)
1801.1Sfvdl{
1811.3Sthorpej
1821.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
1831.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
1841.52Skleink		return ENOEXEC;
1851.1Sfvdl
1861.1Sfvdl	switch (eh->e_machine) {
1871.9Scgd
1881.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
1891.1Sfvdl
1901.1Sfvdl	default:
1911.1Sfvdl		return ENOEXEC;
1921.1Sfvdl	}
1931.1Sfvdl
1941.1Sfvdl	if (eh->e_type != type)
1951.1Sfvdl		return ENOEXEC;
1961.1Sfvdl
1971.1Sfvdl	return 0;
1981.1Sfvdl}
1991.1Sfvdl
2001.1Sfvdl/*
2011.1Sfvdl * elf_load_psection():
2021.17Scgd *
2031.1Sfvdl * Load a psection at the appropriate address
2041.1Sfvdl */
2051.17Scgdvoid
2061.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
2071.54Sthorpej    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
2081.1Sfvdl{
2091.8Schristos	u_long uaddr, msize, psize, rm, rf;
2101.1Sfvdl	long diff, offset;
2111.1Sfvdl
2121.1Sfvdl	/*
2131.17Scgd	 * If the user specified an address, then we load there.
2141.17Scgd	 */
2151.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2161.1Sfvdl		if (ph->p_align > 1) {
2171.53Smatt			*addr = ELF_TRUNC(*addr, ph->p_align);
2181.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2191.1Sfvdl		} else
2201.1Sfvdl			uaddr = ph->p_vaddr;
2211.1Sfvdl		diff = ph->p_vaddr - uaddr;
2221.1Sfvdl	} else {
2231.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2241.1Sfvdl		if (ph->p_align > 1)
2251.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2261.1Sfvdl		diff = uaddr - *addr;
2271.1Sfvdl	}
2281.1Sfvdl
2291.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
2301.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
2311.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
2321.1Sfvdl
2331.1Sfvdl	offset = ph->p_offset - diff;
2341.1Sfvdl	*size = ph->p_filesz + diff;
2351.1Sfvdl	msize = ph->p_memsz + diff;
2361.8Schristos	psize = round_page(*size);
2371.1Sfvdl
2381.46Skleink	if ((ph->p_flags & PF_W) != 0) {
2391.8Schristos		/*
2401.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
2411.8Schristos		 * data page if it's not page aligned we map the last page
2421.8Schristos		 * readvn.
2431.8Schristos		 */
2441.8Schristos		psize = trunc_page(*size);
2451.53Smatt	}
2461.53Smatt	if (psize > 0) {
2471.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2481.53Smatt		    offset, *prot, flags);
2491.53Smatt	}
2501.53Smatt	if (psize < *size) {
2511.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
2521.53Smatt		    *addr + psize, vp, offset + psize, *prot,
2531.53Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
2541.53Smatt	}
2551.1Sfvdl
2561.1Sfvdl	/*
2571.17Scgd	 * Check if we need to extend the size of the segment
2581.17Scgd	 */
2591.1Sfvdl	rm = round_page(*addr + msize);
2601.1Sfvdl	rf = round_page(*addr + *size);
2611.1Sfvdl
2621.1Sfvdl	if (rm != rf) {
2631.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
2641.53Smatt		    0, *prot, flags & VMCMD_RELATIVE);
2651.1Sfvdl		*size = msize;
2661.1Sfvdl	}
2671.1Sfvdl}
2681.1Sfvdl
2691.1Sfvdl/*
2701.1Sfvdl * elf_read_from():
2711.1Sfvdl *
2721.1Sfvdl *	Read from vnode into buffer at offset.
2731.1Sfvdl */
2741.7Schristosint
2751.54SthorpejELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off,
2761.54Sthorpej    caddr_t buf, int size)
2771.1Sfvdl{
2781.1Sfvdl	int error;
2791.33Sthorpej	size_t resid;
2801.1Sfvdl
2811.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
2821.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
2831.1Sfvdl		return error;
2841.1Sfvdl	/*
2851.17Scgd	 * See if we got all of it
2861.17Scgd	 */
2871.1Sfvdl	if (resid != 0)
2881.4Sfvdl		return ENOEXEC;
2891.1Sfvdl	return 0;
2901.1Sfvdl}
2911.1Sfvdl
2921.1Sfvdl/*
2931.1Sfvdl * elf_load_file():
2941.1Sfvdl *
2951.1Sfvdl * Load a file (interpreter/library) pointed to by path
2961.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
2971.1Sfvdl * so it might be used externally.
2981.1Sfvdl */
2991.17Scgdint
3001.54SthorpejELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
3011.54Sthorpej    struct exec_vmcmd_set *vcset, u_long *entry, struct elf_args *ap,
3021.54Sthorpej    Elf_Addr *last)
3031.1Sfvdl{
3041.1Sfvdl	int error, i;
3051.1Sfvdl	struct nameidata nd;
3061.14Scgd	struct vnode *vp;
3071.14Scgd	struct vattr attr;
3081.9Scgd	Elf_Ehdr eh;
3091.9Scgd	Elf_Phdr *ph = NULL;
3101.53Smatt	Elf_Phdr *base_ph = NULL;
3111.1Sfvdl	u_long phsize;
3121.1Sfvdl	char *bp = NULL;
3131.9Scgd	Elf_Addr addr = *last;
3141.1Sfvdl
3151.1Sfvdl	bp = path;
3161.1Sfvdl	/*
3171.17Scgd	 * 1. open file
3181.17Scgd	 * 2. read filehdr
3191.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3201.17Scgd	 */
3211.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3221.12Scgd	if ((error = namei(&nd)) != 0)
3231.1Sfvdl		return error;
3241.14Scgd	vp = nd.ni_vp;
3251.14Scgd
3261.26Smycroft	/*
3271.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3281.26Smycroft	 * file, we don't allow it to be used.
3291.26Smycroft	 */
3301.14Scgd	if (vp->v_type != VREG) {
3311.14Scgd		error = EACCES;
3321.14Scgd		goto badunlock;
3331.14Scgd	}
3341.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
3351.26Smycroft		goto badunlock;
3361.14Scgd
3371.17Scgd	/* get attributes */
3381.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
3391.14Scgd		goto badunlock;
3401.14Scgd
3411.14Scgd	/*
3421.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
3431.15Scgd	 * we will be executing code from it, so if the mount point
3441.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
3451.14Scgd	 */
3461.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3471.14Scgd		error = EACCES;
3481.14Scgd		goto badunlock;
3491.14Scgd	}
3501.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
3511.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
3521.14Scgd
3531.12Scgd#ifdef notyet /* XXX cgd 960926 */
3541.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
3551.12Scgd#endif
3561.28Sfvdl	VOP_UNLOCK(vp, 0);
3571.12Scgd
3581.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
3591.17Scgd	    sizeof(eh))) != 0)
3601.1Sfvdl		goto bad;
3611.1Sfvdl
3621.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
3631.1Sfvdl		goto bad;
3641.1Sfvdl
3651.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
3661.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
3671.1Sfvdl
3681.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
3691.9Scgd	    (caddr_t) ph, phsize)) != 0)
3701.1Sfvdl		goto bad;
3711.1Sfvdl
3721.1Sfvdl	/*
3731.17Scgd	 * Load all the necessary sections
3741.17Scgd	 */
3751.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
3761.1Sfvdl		u_long size = 0;
3771.1Sfvdl		int prot = 0;
3781.53Smatt		int flags;
3791.1Sfvdl
3801.1Sfvdl		switch (ph[i].p_type) {
3811.46Skleink		case PT_LOAD:
3821.53Smatt			if (base_ph == NULL) {
3831.53Smatt				addr = *last;
3841.53Smatt				flags = VMCMD_BASE;
3851.53Smatt			} else {
3861.53Smatt				addr = ph[i].p_vaddr - base_ph->p_vaddr;
3871.53Smatt				flags = VMCMD_RELATIVE;
3881.53Smatt			}
3891.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
3901.53Smatt			    &size, &prot, flags);
3911.4Sfvdl			/* If entry is within this section it must be text */
3921.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
3931.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
3941.21Sfvdl				/* XXX */
3951.21Sfvdl				*entry = addr + eh.e_entry;
3961.21Sfvdl#ifdef mips
3971.21Sfvdl				*entry -= ph[i].p_vaddr;
3981.21Sfvdl#endif
3991.1Sfvdl				ap->arg_interp = addr;
4001.1Sfvdl			}
4011.53Smatt			if (base_ph == NULL)
4021.53Smatt				base_ph = &ph[i];
4031.1Sfvdl			addr += size;
4041.1Sfvdl			break;
4051.1Sfvdl
4061.46Skleink		case PT_DYNAMIC:
4071.46Skleink		case PT_PHDR:
4081.46Skleink		case PT_NOTE:
4091.1Sfvdl			break;
4101.1Sfvdl
4111.1Sfvdl		default:
4121.1Sfvdl			break;
4131.1Sfvdl		}
4141.1Sfvdl	}
4151.1Sfvdl
4161.17Scgd	free((char *)ph, M_TEMP);
4171.12Scgd	*last = addr;
4181.14Scgd	vrele(vp);
4191.12Scgd	return 0;
4201.12Scgd
4211.14Scgdbadunlock:
4221.28Sfvdl	VOP_UNLOCK(vp, 0);
4231.14Scgd
4241.1Sfvdlbad:
4251.1Sfvdl	if (ph != NULL)
4261.17Scgd		free((char *)ph, M_TEMP);
4271.12Scgd#ifdef notyet /* XXX cgd 960926 */
4281.12Scgd	(maybe) VOP_CLOSE it
4291.12Scgd#endif
4301.14Scgd	vrele(vp);
4311.1Sfvdl	return error;
4321.1Sfvdl}
4331.1Sfvdl
4341.1Sfvdl/*
4351.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4361.1Sfvdl *
4371.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4381.1Sfvdl *
4391.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4401.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4411.1Sfvdl * text, data, bss, and stack segments.
4421.1Sfvdl */
4431.1Sfvdlint
4441.54SthorpejELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
4451.1Sfvdl{
4461.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
4471.9Scgd	Elf_Phdr *ph, *pp;
4481.9Scgd	Elf_Addr phdr = 0, pos = 0;
4491.58Sjdolecek	int error, i, nload;
4501.58Sjdolecek	char *interp = NULL;
4511.9Scgd	u_long phsize;
4521.1Sfvdl
4531.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
4541.1Sfvdl		return ENOEXEC;
4551.1Sfvdl
4561.45Sfvdl	/*
4571.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
4581.45Sfvdl	 * but other ELF-based systems allow it as well.
4591.45Sfvdl	 */
4601.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
4611.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
4621.1Sfvdl		return ENOEXEC;
4631.1Sfvdl
4641.1Sfvdl	/*
4651.17Scgd	 * check if vnode is in open for writing, because we want to
4661.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
4671.17Scgd	 * reasons
4681.17Scgd	 */
4691.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4701.1Sfvdl#ifdef DIAGNOSTIC
4711.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4721.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4731.1Sfvdl#endif
4741.1Sfvdl		return ETXTBSY;
4751.1Sfvdl	}
4761.1Sfvdl	/*
4771.17Scgd	 * Allocate space to hold all the program headers, and read them
4781.17Scgd	 * from the file
4791.17Scgd	 */
4801.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
4811.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4821.1Sfvdl
4831.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
4841.8Schristos	    (caddr_t) ph, phsize)) != 0)
4851.1Sfvdl		goto bad;
4861.1Sfvdl
4871.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
4881.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
4891.1Sfvdl
4901.58Sjdolecek	MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
4911.1Sfvdl	interp[0] = '\0';
4921.1Sfvdl
4931.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
4941.1Sfvdl		pp = &ph[i];
4951.46Skleink		if (pp->p_type == PT_INTERP) {
4961.58Sjdolecek			if (pp->p_filesz >= MAXPATHLEN)
4971.1Sfvdl				goto bad;
4981.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
4991.17Scgd			    pp->p_offset, (caddr_t) interp,
5001.17Scgd			    pp->p_filesz)) != 0)
5011.1Sfvdl				goto bad;
5021.1Sfvdl			break;
5031.1Sfvdl		}
5041.1Sfvdl	}
5051.1Sfvdl
5061.9Scgd	/*
5071.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5081.1Sfvdl	 * See which one will accept this executable. This currently only
5091.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5101.38Serh	 * and the Alpha.
5111.1Sfvdl	 *
5121.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5131.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5141.58Sjdolecek	 * interp[] with a changed path (/emul/xxx/<path>).
5151.1Sfvdl	 */
5161.58Sjdolecek	if (!epp->ep_esch->u.elf_probe_func) {
5171.58Sjdolecek		p->p_emul = epp->ep_esch->es_emul;
5181.58Sjdolecek		pos = ELFDEFNNAME(NO_ADDR);
5191.58Sjdolecek	} else {
5201.58Sjdolecek		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
5211.58Sjdolecek				(vaddr_t *)&pos);
5221.1Sfvdl		if (error)
5231.1Sfvdl			goto bad;
5241.1Sfvdl	}
5251.1Sfvdl
5261.1Sfvdl	/*
5271.17Scgd	 * Load all the necessary sections
5281.17Scgd	 */
5291.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5301.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5311.9Scgd		u_long size = 0;
5321.1Sfvdl		int prot = 0;
5331.1Sfvdl
5341.1Sfvdl		pp = &ph[i];
5351.1Sfvdl
5361.1Sfvdl		switch (ph[i].p_type) {
5371.46Skleink		case PT_LOAD:
5381.4Sfvdl			/*
5391.4Sfvdl			 * XXX
5401.4Sfvdl			 * Can handle only 2 sections: text and data
5411.4Sfvdl			 */
5421.4Sfvdl			if (nload++ == 2)
5431.4Sfvdl				goto bad;
5441.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
5451.53Smatt			    &ph[i], &addr, &size, &prot, 0);
5461.17Scgd
5471.4Sfvdl			/*
5481.4Sfvdl			 * Decide whether it's text or data by looking
5491.4Sfvdl			 * at the entry point.
5501.4Sfvdl			 */
5511.19Scgd			if (eh->e_entry >= addr &&
5521.19Scgd			    eh->e_entry < (addr + size)) {
5531.4Sfvdl				epp->ep_taddr = addr;
5541.4Sfvdl				epp->ep_tsize = size;
5551.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
5561.19Scgd					epp->ep_daddr = addr;
5571.19Scgd					epp->ep_dsize = size;
5581.19Scgd				}
5591.4Sfvdl			} else {
5601.4Sfvdl				epp->ep_daddr = addr;
5611.4Sfvdl				epp->ep_dsize = size;
5621.4Sfvdl			}
5631.1Sfvdl			break;
5641.1Sfvdl
5651.46Skleink		case PT_SHLIB:
5661.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
5671.1Sfvdl			error = ENOEXEC;
5681.1Sfvdl			goto bad;
5691.27Sscottb#endif
5701.1Sfvdl
5711.46Skleink		case PT_INTERP:
5721.1Sfvdl			/* Already did this one */
5731.46Skleink		case PT_DYNAMIC:
5741.46Skleink		case PT_NOTE:
5751.1Sfvdl			break;
5761.1Sfvdl
5771.46Skleink		case PT_PHDR:
5781.4Sfvdl			/* Note address of program headers (in text segment) */
5791.4Sfvdl			phdr = pp->p_vaddr;
5801.7Schristos			break;
5811.4Sfvdl
5821.1Sfvdl		default:
5831.1Sfvdl			/*
5841.9Scgd			 * Not fatal; we don't need to understand everything.
5851.1Sfvdl			 */
5861.1Sfvdl			break;
5871.1Sfvdl		}
5881.1Sfvdl	}
5891.5Sfvdl
5901.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
5911.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
5921.5Sfvdl	/*
5931.5Sfvdl	 * If no position to load the interpreter was set by a probe
5941.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
5951.5Sfvdl	 * would (i.e. something safely out of the way).
5961.5Sfvdl	 */
5971.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
5981.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
5991.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6001.1Sfvdl
6011.1Sfvdl	/*
6021.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6031.17Scgd	 * it's interpreter
6041.17Scgd	 */
6051.1Sfvdl	if (interp[0]) {
6061.1Sfvdl		struct elf_args *ap;
6071.1Sfvdl
6081.58Sjdolecek		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
6091.17Scgd		    M_TEMP, M_WAITOK);
6101.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6111.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6121.58Sjdolecek			FREE((char *)ap, M_TEMP);
6131.1Sfvdl			goto bad;
6141.1Sfvdl		}
6151.1Sfvdl		pos += phsize;
6161.4Sfvdl		ap->arg_phaddr = phdr;
6171.1Sfvdl
6181.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6191.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6201.1Sfvdl		ap->arg_entry = eh->e_entry;
6211.1Sfvdl
6221.1Sfvdl		epp->ep_emul_arg = ap;
6231.1Sfvdl	} else
6241.1Sfvdl		epp->ep_entry = eh->e_entry;
6251.1Sfvdl
6261.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6271.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6281.57Sthorpej	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
6291.57Sthorpej	    epp->ep_vp, 0, VM_PROT_READ);
6301.8Schristos#endif
6311.58Sjdolecek	FREE(interp, M_TEMP);
6321.17Scgd	free((char *)ph, M_TEMP);
6331.48Schs	vn_marktext(epp->ep_vp);
6341.9Scgd	return exec_elf_setup_stack(p, epp);
6351.1Sfvdl
6361.1Sfvdlbad:
6371.58Sjdolecek	if (interp)
6381.58Sjdolecek		FREE(interp, M_TEMP);
6391.17Scgd	free((char *)ph, M_TEMP);
6401.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6411.1Sfvdl	return ENOEXEC;
6421.40Schristos}
6431.40Schristos
6441.59Smrgint
6451.54SthorpejELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
6461.54Sthorpej    Elf_Ehdr *eh)
6471.40Schristos{
6481.40Schristos	Elf_Phdr *hph, *ph;
6491.46Skleink	Elf_Nhdr *np = NULL;
6501.40Schristos	size_t phsize;
6511.40Schristos	int error;
6521.40Schristos
6531.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
6541.40Schristos	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
6551.41Schristos	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
6561.41Schristos	    (caddr_t)hph, phsize)) != 0)
6571.40Schristos		goto out1;
6581.40Schristos
6591.40Schristos	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
6601.46Skleink		if (ph->p_type != PT_NOTE ||
6611.46Skleink		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
6621.40Schristos			continue;
6631.40Schristos
6641.46Skleink		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
6651.40Schristos		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
6661.40Schristos		    (caddr_t)np, ph->p_filesz)) != 0)
6671.40Schristos			goto out2;
6681.40Schristos
6691.46Skleink		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
6701.40Schristos			free(np, M_TEMP);
6711.40Schristos			np = NULL;
6721.40Schristos			continue;
6731.40Schristos		}
6741.40Schristos
6751.40Schristos		/* Check the name and description sizes. */
6761.46Skleink		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
6771.46Skleink		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
6781.40Schristos			goto out3;
6791.40Schristos
6801.40Schristos		/* Is the name "NetBSD\0\0"? */
6811.40Schristos		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
6821.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
6831.40Schristos			goto out3;
6841.40Schristos
6851.40Schristos		/* XXX: We could check for the specific emulation here */
6861.40Schristos		/* All checks succeeded. */
6871.40Schristos		error = 0;
6881.40Schristos		goto out2;
6891.40Schristos	}
6901.40Schristos
6911.40Schristosout3:
6921.40Schristos	error = ENOEXEC;
6931.40Schristosout2:
6941.40Schristos	if (np)
6951.40Schristos		free(np, M_TEMP);
6961.40Schristosout1:
6971.40Schristos	free(hph, M_TEMP);
6981.40Schristos	return error;
6991.40Schristos}
7001.40Schristos
7011.58Sjdolecekint
7021.54SthorpejELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
7031.58Sjdolecek    void *eh, char *itp, vaddr_t *pos)
7041.40Schristos{
7051.40Schristos	int error;
7061.40Schristos
7071.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
7081.40Schristos		return error;
7091.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
7101.40Schristos	return 0;
7111.1Sfvdl}
712