exec_elf32.c revision 1.64
11.64Schristos/*	$NetBSD: exec_elf32.c,v 1.64 2001/07/14 02:08:29 christos Exp $	*/
21.36Schristos
31.36Schristos/*-
41.60Smycroft * Copyright (c) 1994, 2000 The NetBSD Foundation, Inc.
51.36Schristos * All rights reserved.
61.36Schristos *
71.36Schristos * This code is derived from software contributed to The NetBSD Foundation
81.36Schristos * by Christos Zoulas.
91.36Schristos *
101.36Schristos * Redistribution and use in source and binary forms, with or without
111.36Schristos * modification, are permitted provided that the following conditions
121.36Schristos * are met:
131.36Schristos * 1. Redistributions of source code must retain the above copyright
141.36Schristos *    notice, this list of conditions and the following disclaimer.
151.36Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.36Schristos *    notice, this list of conditions and the following disclaimer in the
171.36Schristos *    documentation and/or other materials provided with the distribution.
181.36Schristos * 3. All advertising materials mentioning features or use of this software
191.36Schristos *    must display the following acknowledgement:
201.37Schristos *	This product includes software developed by the NetBSD
211.37Schristos *	Foundation, Inc. and its contributors.
221.36Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
231.36Schristos *    contributors may be used to endorse or promote products derived
241.36Schristos *    from this software without specific prior written permission.
251.36Schristos *
261.36Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.36Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.36Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.36Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.36Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.36Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.36Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.36Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.36Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.36Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.36Schristos * POSSIBILITY OF SUCH DAMAGE.
371.36Schristos */
381.1Sfvdl
391.1Sfvdl/*
401.9Scgd * Copyright (c) 1996 Christopher G. Demetriou
411.1Sfvdl * All rights reserved.
421.1Sfvdl *
431.1Sfvdl * Redistribution and use in source and binary forms, with or without
441.1Sfvdl * modification, are permitted provided that the following conditions
451.1Sfvdl * are met:
461.1Sfvdl * 1. Redistributions of source code must retain the above copyright
471.1Sfvdl *    notice, this list of conditions and the following disclaimer.
481.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
491.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
501.1Sfvdl *    documentation and/or other materials provided with the distribution.
511.1Sfvdl * 3. The name of the author may not be used to endorse or promote products
521.1Sfvdl *    derived from this software without specific prior written permission
531.1Sfvdl *
541.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
551.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
561.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
571.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
581.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
591.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
601.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
611.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
621.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
631.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
641.1Sfvdl */
651.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.1Sfvdl#include <sys/param.h>
721.1Sfvdl#include <sys/proc.h>
731.1Sfvdl#include <sys/malloc.h>
741.1Sfvdl#include <sys/namei.h>
751.1Sfvdl#include <sys/vnode.h>
761.1Sfvdl#include <sys/exec.h>
771.1Sfvdl#include <sys/exec_elf.h>
781.8Schristos#include <sys/syscall.h>
791.8Schristos#include <sys/signalvar.h>
801.14Scgd#include <sys/mount.h>
811.14Scgd#include <sys/stat.h>
821.1Sfvdl
831.58Sjdolecek#include <machine/cpu.h>
841.58Sjdolecek#include <machine/reg.h>
851.57Sthorpej
861.58Sjdolecekextern const struct emul emul_netbsd;
871.42Schristos
881.54Sthorpejint	ELFNAME(check_header)(Elf_Ehdr *, int);
891.54Sthorpejint	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
901.54Sthorpej	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
911.54Sthorpejvoid	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
921.54Sthorpej	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
931.54Sthorpej
941.59Smrgint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
951.54Sthorpej    Elf_Ehdr *);
961.58Sjdolecekint ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
971.58Sjdolecek    void *, char *, vaddr_t *);
981.8Schristos
991.18Scgd/* round up and down to page boundaries. */
1001.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1011.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1021.8Schristos
1031.8Schristos/*
1041.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1051.1Sfvdl * extra information in case of dynamic binding.
1061.1Sfvdl */
1071.1Sfvdlvoid *
1081.54SthorpejELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo,
1091.54Sthorpej    void *stack, void *argp)
1101.1Sfvdl{
1111.1Sfvdl	size_t len;
1121.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1131.1Sfvdl	struct elf_args *ap;
1141.1Sfvdl
1151.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1161.4Sfvdl	if (!stack)
1171.1Sfvdl		return NULL;
1181.1Sfvdl
1191.22Scgd	a = ai;
1201.22Scgd
1211.1Sfvdl	/*
1221.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1231.1Sfvdl	 * linked binaries
1241.1Sfvdl	 */
1251.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1261.1Sfvdl
1271.46Skleink		a->a_type = AT_PHDR;
1281.46Skleink		a->a_v = ap->arg_phaddr;
1291.1Sfvdl		a++;
1301.1Sfvdl
1311.46Skleink		a->a_type = AT_PHENT;
1321.46Skleink		a->a_v = ap->arg_phentsize;
1331.1Sfvdl		a++;
1341.1Sfvdl
1351.46Skleink		a->a_type = AT_PHNUM;
1361.46Skleink		a->a_v = ap->arg_phnum;
1371.1Sfvdl		a++;
1381.1Sfvdl
1391.46Skleink		a->a_type = AT_PAGESZ;
1401.57Sthorpej		a->a_v = PAGE_SIZE;
1411.1Sfvdl		a++;
1421.1Sfvdl
1431.46Skleink		a->a_type = AT_BASE;
1441.46Skleink		a->a_v = ap->arg_interp;
1451.1Sfvdl		a++;
1461.1Sfvdl
1471.46Skleink		a->a_type = AT_FLAGS;
1481.46Skleink		a->a_v = 0;
1491.1Sfvdl		a++;
1501.1Sfvdl
1511.46Skleink		a->a_type = AT_ENTRY;
1521.46Skleink		a->a_v = ap->arg_entry;
1531.1Sfvdl		a++;
1541.1Sfvdl
1551.17Scgd		free((char *)ap, M_TEMP);
1561.9Scgd		pack->ep_emul_arg = NULL;
1571.1Sfvdl	}
1581.22Scgd
1591.46Skleink	a->a_type = AT_NULL;
1601.46Skleink	a->a_v = 0;
1611.22Scgd	a++;
1621.22Scgd
1631.34Sperry	len = (a - ai) * sizeof(AuxInfo);
1641.22Scgd	if (copyout(ai, stack, len))
1651.22Scgd		return NULL;
1661.43Skleink	stack = (caddr_t)stack + len;
1671.22Scgd
1681.4Sfvdl	return stack;
1691.1Sfvdl}
1701.1Sfvdl
1711.1Sfvdl/*
1721.1Sfvdl * elf_check_header():
1731.1Sfvdl *
1741.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
1751.1Sfvdl */
1761.17Scgdint
1771.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type)
1781.1Sfvdl{
1791.3Sthorpej
1801.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
1811.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
1821.61Smycroft		return (ENOEXEC);
1831.1Sfvdl
1841.1Sfvdl	switch (eh->e_machine) {
1851.9Scgd
1861.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
1871.1Sfvdl
1881.1Sfvdl	default:
1891.61Smycroft		return (ENOEXEC);
1901.1Sfvdl	}
1911.1Sfvdl
1921.1Sfvdl	if (eh->e_type != type)
1931.61Smycroft		return (ENOEXEC);
1941.61Smycroft
1951.63Sjdolecek	if (eh->e_shnum > 512 ||
1961.61Smycroft	    eh->e_phnum > 128)
1971.61Smycroft		return (ENOEXEC);
1981.1Sfvdl
1991.61Smycroft	return (0);
2001.1Sfvdl}
2011.1Sfvdl
2021.1Sfvdl/*
2031.1Sfvdl * elf_load_psection():
2041.17Scgd *
2051.1Sfvdl * Load a psection at the appropriate address
2061.1Sfvdl */
2071.17Scgdvoid
2081.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
2091.54Sthorpej    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
2101.1Sfvdl{
2111.8Schristos	u_long uaddr, msize, psize, rm, rf;
2121.1Sfvdl	long diff, offset;
2131.1Sfvdl
2141.1Sfvdl	/*
2151.17Scgd	 * If the user specified an address, then we load there.
2161.17Scgd	 */
2171.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2181.1Sfvdl		if (ph->p_align > 1) {
2191.53Smatt			*addr = ELF_TRUNC(*addr, ph->p_align);
2201.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2211.1Sfvdl		} else
2221.1Sfvdl			uaddr = ph->p_vaddr;
2231.1Sfvdl		diff = ph->p_vaddr - uaddr;
2241.1Sfvdl	} else {
2251.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2261.1Sfvdl		if (ph->p_align > 1)
2271.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2281.1Sfvdl		diff = uaddr - *addr;
2291.1Sfvdl	}
2301.1Sfvdl
2311.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
2321.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
2331.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
2341.1Sfvdl
2351.1Sfvdl	offset = ph->p_offset - diff;
2361.1Sfvdl	*size = ph->p_filesz + diff;
2371.1Sfvdl	msize = ph->p_memsz + diff;
2381.8Schristos	psize = round_page(*size);
2391.1Sfvdl
2401.46Skleink	if ((ph->p_flags & PF_W) != 0) {
2411.8Schristos		/*
2421.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
2431.8Schristos		 * data page if it's not page aligned we map the last page
2441.8Schristos		 * readvn.
2451.8Schristos		 */
2461.8Schristos		psize = trunc_page(*size);
2471.53Smatt	}
2481.53Smatt	if (psize > 0) {
2491.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2501.53Smatt		    offset, *prot, flags);
2511.53Smatt	}
2521.53Smatt	if (psize < *size) {
2531.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
2541.53Smatt		    *addr + psize, vp, offset + psize, *prot,
2551.53Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
2561.53Smatt	}
2571.1Sfvdl
2581.1Sfvdl	/*
2591.17Scgd	 * Check if we need to extend the size of the segment
2601.17Scgd	 */
2611.1Sfvdl	rm = round_page(*addr + msize);
2621.1Sfvdl	rf = round_page(*addr + *size);
2631.1Sfvdl
2641.1Sfvdl	if (rm != rf) {
2651.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
2661.53Smatt		    0, *prot, flags & VMCMD_RELATIVE);
2671.1Sfvdl		*size = msize;
2681.1Sfvdl	}
2691.1Sfvdl}
2701.1Sfvdl
2711.1Sfvdl/*
2721.1Sfvdl * elf_load_file():
2731.1Sfvdl *
2741.1Sfvdl * Load a file (interpreter/library) pointed to by path
2751.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
2761.1Sfvdl * so it might be used externally.
2771.1Sfvdl */
2781.17Scgdint
2791.54SthorpejELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
2801.54Sthorpej    struct exec_vmcmd_set *vcset, u_long *entry, struct elf_args *ap,
2811.54Sthorpej    Elf_Addr *last)
2821.1Sfvdl{
2831.1Sfvdl	int error, i;
2841.1Sfvdl	struct nameidata nd;
2851.14Scgd	struct vnode *vp;
2861.14Scgd	struct vattr attr;
2871.9Scgd	Elf_Ehdr eh;
2881.9Scgd	Elf_Phdr *ph = NULL;
2891.53Smatt	Elf_Phdr *base_ph = NULL;
2901.1Sfvdl	u_long phsize;
2911.1Sfvdl	char *bp = NULL;
2921.9Scgd	Elf_Addr addr = *last;
2931.1Sfvdl
2941.1Sfvdl	bp = path;
2951.1Sfvdl	/*
2961.17Scgd	 * 1. open file
2971.17Scgd	 * 2. read filehdr
2981.17Scgd	 * 3. map text, data, and bss out of it using VM_*
2991.17Scgd	 */
3001.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3011.12Scgd	if ((error = namei(&nd)) != 0)
3021.1Sfvdl		return error;
3031.14Scgd	vp = nd.ni_vp;
3041.14Scgd
3051.26Smycroft	/*
3061.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3071.26Smycroft	 * file, we don't allow it to be used.
3081.26Smycroft	 */
3091.14Scgd	if (vp->v_type != VREG) {
3101.14Scgd		error = EACCES;
3111.14Scgd		goto badunlock;
3121.14Scgd	}
3131.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
3141.26Smycroft		goto badunlock;
3151.14Scgd
3161.17Scgd	/* get attributes */
3171.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
3181.14Scgd		goto badunlock;
3191.14Scgd
3201.14Scgd	/*
3211.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
3221.15Scgd	 * we will be executing code from it, so if the mount point
3231.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
3241.14Scgd	 */
3251.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3261.14Scgd		error = EACCES;
3271.14Scgd		goto badunlock;
3281.14Scgd	}
3291.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
3301.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
3311.14Scgd
3321.12Scgd#ifdef notyet /* XXX cgd 960926 */
3331.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
3341.12Scgd#endif
3351.28Sfvdl	VOP_UNLOCK(vp, 0);
3361.12Scgd
3371.64Schristos	if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0)
3381.1Sfvdl		goto bad;
3391.1Sfvdl
3401.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
3411.1Sfvdl		goto bad;
3421.1Sfvdl
3431.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
3441.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
3451.1Sfvdl
3461.64Schristos	if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0)
3471.1Sfvdl		goto bad;
3481.1Sfvdl
3491.1Sfvdl	/*
3501.17Scgd	 * Load all the necessary sections
3511.17Scgd	 */
3521.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
3531.1Sfvdl		u_long size = 0;
3541.1Sfvdl		int prot = 0;
3551.53Smatt		int flags;
3561.1Sfvdl
3571.1Sfvdl		switch (ph[i].p_type) {
3581.46Skleink		case PT_LOAD:
3591.53Smatt			if (base_ph == NULL) {
3601.53Smatt				addr = *last;
3611.53Smatt				flags = VMCMD_BASE;
3621.53Smatt			} else {
3631.53Smatt				addr = ph[i].p_vaddr - base_ph->p_vaddr;
3641.53Smatt				flags = VMCMD_RELATIVE;
3651.53Smatt			}
3661.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
3671.53Smatt			    &size, &prot, flags);
3681.4Sfvdl			/* If entry is within this section it must be text */
3691.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
3701.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
3711.21Sfvdl				/* XXX */
3721.21Sfvdl				*entry = addr + eh.e_entry;
3731.21Sfvdl#ifdef mips
3741.21Sfvdl				*entry -= ph[i].p_vaddr;
3751.21Sfvdl#endif
3761.1Sfvdl				ap->arg_interp = addr;
3771.1Sfvdl			}
3781.53Smatt			if (base_ph == NULL)
3791.53Smatt				base_ph = &ph[i];
3801.1Sfvdl			addr += size;
3811.1Sfvdl			break;
3821.1Sfvdl
3831.46Skleink		case PT_DYNAMIC:
3841.46Skleink		case PT_PHDR:
3851.46Skleink		case PT_NOTE:
3861.1Sfvdl			break;
3871.1Sfvdl
3881.1Sfvdl		default:
3891.1Sfvdl			break;
3901.1Sfvdl		}
3911.1Sfvdl	}
3921.1Sfvdl
3931.17Scgd	free((char *)ph, M_TEMP);
3941.12Scgd	*last = addr;
3951.14Scgd	vrele(vp);
3961.12Scgd	return 0;
3971.12Scgd
3981.14Scgdbadunlock:
3991.28Sfvdl	VOP_UNLOCK(vp, 0);
4001.14Scgd
4011.1Sfvdlbad:
4021.1Sfvdl	if (ph != NULL)
4031.17Scgd		free((char *)ph, M_TEMP);
4041.12Scgd#ifdef notyet /* XXX cgd 960926 */
4051.12Scgd	(maybe) VOP_CLOSE it
4061.12Scgd#endif
4071.14Scgd	vrele(vp);
4081.1Sfvdl	return error;
4091.1Sfvdl}
4101.1Sfvdl
4111.1Sfvdl/*
4121.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4131.1Sfvdl *
4141.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4151.1Sfvdl *
4161.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4171.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4181.1Sfvdl * text, data, bss, and stack segments.
4191.1Sfvdl */
4201.1Sfvdlint
4211.54SthorpejELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
4221.1Sfvdl{
4231.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
4241.9Scgd	Elf_Phdr *ph, *pp;
4251.9Scgd	Elf_Addr phdr = 0, pos = 0;
4261.58Sjdolecek	int error, i, nload;
4271.58Sjdolecek	char *interp = NULL;
4281.9Scgd	u_long phsize;
4291.1Sfvdl
4301.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
4311.1Sfvdl		return ENOEXEC;
4321.1Sfvdl
4331.45Sfvdl	/*
4341.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
4351.45Sfvdl	 * but other ELF-based systems allow it as well.
4361.45Sfvdl	 */
4371.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
4381.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
4391.1Sfvdl		return ENOEXEC;
4401.1Sfvdl
4411.1Sfvdl	/*
4421.17Scgd	 * check if vnode is in open for writing, because we want to
4431.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
4441.17Scgd	 * reasons
4451.17Scgd	 */
4461.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4471.1Sfvdl#ifdef DIAGNOSTIC
4481.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4491.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4501.1Sfvdl#endif
4511.1Sfvdl		return ETXTBSY;
4521.1Sfvdl	}
4531.1Sfvdl	/*
4541.17Scgd	 * Allocate space to hold all the program headers, and read them
4551.17Scgd	 * from the file
4561.17Scgd	 */
4571.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
4581.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4591.1Sfvdl
4601.64Schristos	if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
4611.64Schristos	    0)
4621.1Sfvdl		goto bad;
4631.1Sfvdl
4641.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
4651.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
4661.1Sfvdl
4671.58Sjdolecek	MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
4681.1Sfvdl	interp[0] = '\0';
4691.1Sfvdl
4701.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
4711.1Sfvdl		pp = &ph[i];
4721.46Skleink		if (pp->p_type == PT_INTERP) {
4731.58Sjdolecek			if (pp->p_filesz >= MAXPATHLEN)
4741.1Sfvdl				goto bad;
4751.64Schristos			if ((error = exec_read_from(p, epp->ep_vp,
4761.64Schristos			    pp->p_offset, interp, pp->p_filesz)) != 0)
4771.1Sfvdl				goto bad;
4781.1Sfvdl			break;
4791.1Sfvdl		}
4801.1Sfvdl	}
4811.1Sfvdl
4821.9Scgd	/*
4831.1Sfvdl	 * On the same architecture, we may be emulating different systems.
4841.1Sfvdl	 * See which one will accept this executable. This currently only
4851.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
4861.38Serh	 * and the Alpha.
4871.1Sfvdl	 *
4881.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
4891.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
4901.58Sjdolecek	 * interp[] with a changed path (/emul/xxx/<path>).
4911.1Sfvdl	 */
4921.58Sjdolecek	if (!epp->ep_esch->u.elf_probe_func) {
4931.58Sjdolecek		pos = ELFDEFNNAME(NO_ADDR);
4941.58Sjdolecek	} else {
4951.62Seeh		vaddr_t startp = 0;
4961.62Seeh
4971.58Sjdolecek		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
4981.62Seeh							  &startp);
4991.62Seeh		pos = (Elf_Addr)startp;
5001.1Sfvdl		if (error)
5011.1Sfvdl			goto bad;
5021.1Sfvdl	}
5031.1Sfvdl
5041.1Sfvdl	/*
5051.17Scgd	 * Load all the necessary sections
5061.17Scgd	 */
5071.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5081.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5091.9Scgd		u_long size = 0;
5101.1Sfvdl		int prot = 0;
5111.1Sfvdl
5121.1Sfvdl		pp = &ph[i];
5131.1Sfvdl
5141.1Sfvdl		switch (ph[i].p_type) {
5151.46Skleink		case PT_LOAD:
5161.4Sfvdl			/*
5171.4Sfvdl			 * XXX
5181.4Sfvdl			 * Can handle only 2 sections: text and data
5191.4Sfvdl			 */
5201.4Sfvdl			if (nload++ == 2)
5211.4Sfvdl				goto bad;
5221.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
5231.53Smatt			    &ph[i], &addr, &size, &prot, 0);
5241.17Scgd
5251.4Sfvdl			/*
5261.4Sfvdl			 * Decide whether it's text or data by looking
5271.4Sfvdl			 * at the entry point.
5281.4Sfvdl			 */
5291.19Scgd			if (eh->e_entry >= addr &&
5301.19Scgd			    eh->e_entry < (addr + size)) {
5311.4Sfvdl				epp->ep_taddr = addr;
5321.4Sfvdl				epp->ep_tsize = size;
5331.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
5341.19Scgd					epp->ep_daddr = addr;
5351.19Scgd					epp->ep_dsize = size;
5361.19Scgd				}
5371.4Sfvdl			} else {
5381.4Sfvdl				epp->ep_daddr = addr;
5391.4Sfvdl				epp->ep_dsize = size;
5401.4Sfvdl			}
5411.1Sfvdl			break;
5421.1Sfvdl
5431.46Skleink		case PT_SHLIB:
5441.60Smycroft			/* SCO has these sections. */
5451.46Skleink		case PT_INTERP:
5461.60Smycroft			/* Already did this one. */
5471.46Skleink		case PT_DYNAMIC:
5481.46Skleink		case PT_NOTE:
5491.1Sfvdl			break;
5501.1Sfvdl
5511.46Skleink		case PT_PHDR:
5521.4Sfvdl			/* Note address of program headers (in text segment) */
5531.4Sfvdl			phdr = pp->p_vaddr;
5541.7Schristos			break;
5551.4Sfvdl
5561.1Sfvdl		default:
5571.1Sfvdl			/*
5581.9Scgd			 * Not fatal; we don't need to understand everything.
5591.1Sfvdl			 */
5601.1Sfvdl			break;
5611.1Sfvdl		}
5621.1Sfvdl	}
5631.5Sfvdl
5641.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
5651.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
5661.5Sfvdl	/*
5671.5Sfvdl	 * If no position to load the interpreter was set by a probe
5681.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
5691.5Sfvdl	 * would (i.e. something safely out of the way).
5701.5Sfvdl	 */
5711.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
5721.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
5731.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
5741.1Sfvdl
5751.1Sfvdl	/*
5761.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
5771.17Scgd	 * it's interpreter
5781.17Scgd	 */
5791.1Sfvdl	if (interp[0]) {
5801.1Sfvdl		struct elf_args *ap;
5811.1Sfvdl
5821.58Sjdolecek		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
5831.17Scgd		    M_TEMP, M_WAITOK);
5841.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
5851.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
5861.58Sjdolecek			FREE((char *)ap, M_TEMP);
5871.1Sfvdl			goto bad;
5881.1Sfvdl		}
5891.1Sfvdl		pos += phsize;
5901.4Sfvdl		ap->arg_phaddr = phdr;
5911.1Sfvdl
5921.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
5931.1Sfvdl		ap->arg_phnum = eh->e_phnum;
5941.1Sfvdl		ap->arg_entry = eh->e_entry;
5951.1Sfvdl
5961.1Sfvdl		epp->ep_emul_arg = ap;
5971.1Sfvdl	} else
5981.1Sfvdl		epp->ep_entry = eh->e_entry;
5991.1Sfvdl
6001.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6011.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6021.57Sthorpej	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
6031.57Sthorpej	    epp->ep_vp, 0, VM_PROT_READ);
6041.8Schristos#endif
6051.58Sjdolecek	FREE(interp, M_TEMP);
6061.17Scgd	free((char *)ph, M_TEMP);
6071.48Schs	vn_marktext(epp->ep_vp);
6081.9Scgd	return exec_elf_setup_stack(p, epp);
6091.1Sfvdl
6101.1Sfvdlbad:
6111.58Sjdolecek	if (interp)
6121.58Sjdolecek		FREE(interp, M_TEMP);
6131.17Scgd	free((char *)ph, M_TEMP);
6141.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6151.1Sfvdl	return ENOEXEC;
6161.40Schristos}
6171.40Schristos
6181.59Smrgint
6191.54SthorpejELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
6201.54Sthorpej    Elf_Ehdr *eh)
6211.40Schristos{
6221.61Smycroft	size_t i;
6231.61Smycroft	Elf_Phdr *ph;
6241.40Schristos	size_t phsize;
6251.40Schristos	int error;
6261.40Schristos
6271.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
6281.61Smycroft	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
6291.64Schristos	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
6301.61Smycroft	if (error)
6311.61Smycroft		goto out;
6321.40Schristos
6331.61Smycroft	for (i = 0; i < eh->e_phnum; i++) {
6341.61Smycroft		Elf_Phdr *ephp = &ph[i];
6351.61Smycroft		Elf_Nhdr *np;
6361.40Schristos
6371.61Smycroft		if (ephp->p_type != PT_NOTE ||
6381.61Smycroft		    ephp->p_filesz > 1024 ||
6391.61Smycroft		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
6401.40Schristos			continue;
6411.40Schristos
6421.61Smycroft		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
6431.64Schristos		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
6441.64Schristos		    ephp->p_filesz);
6451.61Smycroft		if (error)
6461.61Smycroft			goto next;
6471.40Schristos
6481.61Smycroft		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
6491.61Smycroft		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
6501.61Smycroft		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
6511.61Smycroft		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
6521.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
6531.61Smycroft			goto next;
6541.40Schristos
6551.40Schristos		error = 0;
6561.61Smycroft		free(np, M_TEMP);
6571.61Smycroft		goto out;
6581.61Smycroft
6591.61Smycroft	next:
6601.61Smycroft		free(np, M_TEMP);
6611.61Smycroft		continue;
6621.40Schristos	}
6631.40Schristos
6641.40Schristos	error = ENOEXEC;
6651.61Smycroftout:
6661.61Smycroft	free(ph, M_TEMP);
6671.61Smycroft	return (error);
6681.40Schristos}
6691.40Schristos
6701.58Sjdolecekint
6711.54SthorpejELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
6721.58Sjdolecek    void *eh, char *itp, vaddr_t *pos)
6731.40Schristos{
6741.40Schristos	int error;
6751.40Schristos
6761.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
6771.40Schristos		return error;
6781.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
6791.40Schristos	return 0;
6801.1Sfvdl}
681