exec_elf32.c revision 1.9
11.9Scgd/*	$NetBSD: exec_elf32.c,v 1.9 1996/09/26 20:51:05 cgd Exp $	*/
21.1Sfvdl
31.1Sfvdl/*
41.9Scgd * Copyright (c) 1996 Christopher G. Demetriou
51.1Sfvdl * Copyright (c) 1994 Christos Zoulas
61.1Sfvdl * All rights reserved.
71.1Sfvdl *
81.1Sfvdl * Redistribution and use in source and binary forms, with or without
91.1Sfvdl * modification, are permitted provided that the following conditions
101.1Sfvdl * are met:
111.1Sfvdl * 1. Redistributions of source code must retain the above copyright
121.1Sfvdl *    notice, this list of conditions and the following disclaimer.
131.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
141.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
151.1Sfvdl *    documentation and/or other materials provided with the distribution.
161.1Sfvdl * 3. The name of the author may not be used to endorse or promote products
171.1Sfvdl *    derived from this software without specific prior written permission
181.1Sfvdl *
191.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
201.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
211.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
221.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
231.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
241.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
281.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291.1Sfvdl */
301.1Sfvdl
311.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
321.9Scgd#ifndef ELFSIZE
331.9Scgd#define	ELFSIZE		32
341.9Scgd#endif
351.9Scgd
361.9Scgd#if defined(COMPAT_SVR4) || defined(COMPAT_LINUX)	/* XXX should die */
371.9Scgd#undef EXEC_ELF32					/* XXX should die */
381.9Scgd#define EXEC_ELF32					/* XXX should die */
391.9Scgd#endif							/* XXX should die */
401.9Scgd
411.1Sfvdl#include <sys/param.h>
421.1Sfvdl#include <sys/systm.h>
431.1Sfvdl#include <sys/kernel.h>
441.1Sfvdl#include <sys/proc.h>
451.1Sfvdl#include <sys/malloc.h>
461.1Sfvdl#include <sys/namei.h>
471.1Sfvdl#include <sys/vnode.h>
481.1Sfvdl#include <sys/exec.h>
491.1Sfvdl#include <sys/exec_elf.h>
501.9Scgd#include <sys/fcntl.h>
511.8Schristos#include <sys/syscall.h>
521.8Schristos#include <sys/signalvar.h>
531.1Sfvdl
541.1Sfvdl#include <sys/mman.h>
551.1Sfvdl#include <vm/vm.h>
561.1Sfvdl#include <vm/vm_param.h>
571.1Sfvdl#include <vm/vm_map.h>
581.1Sfvdl
591.1Sfvdl#include <machine/cpu.h>
601.1Sfvdl#include <machine/reg.h>
611.1Sfvdl#include <machine/exec.h>
621.1Sfvdl
631.1Sfvdl#ifdef COMPAT_LINUX
641.1Sfvdl#include <compat/linux/linux_exec.h>
651.1Sfvdl#endif
661.1Sfvdl
671.1Sfvdl#ifdef COMPAT_SVR4
681.1Sfvdl#include <compat/svr4/svr4_exec.h>
691.1Sfvdl#endif
701.1Sfvdl
711.9Scgd#define	CONCAT(x,y)	__CONCAT(x,y)
721.9Scgd#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
731.9Scgd#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
741.9Scgd#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
751.9Scgd#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
761.9Scgd
771.9Scgdint ELFNAME(check_header) __P((Elf_Ehdr *, int));
781.9Scgdint ELFNAME(load_file) __P((struct proc *, char *, struct exec_vmcmd_set *,
791.9Scgd			    u_long *, struct elf_args *, Elf_Addr *));
801.9Scgdstatic void ELFNAME(load_psection) __P((struct exec_vmcmd_set *,
811.9Scgd	struct vnode *, Elf_Phdr *, Elf_Addr *, u_long *, int *));
821.1Sfvdl
831.8Schristosextern char sigcode[], esigcode[];
841.8Schristos#ifdef SYSCALL_DEBUG
851.8Schristosextern char *syscallnames[];
861.9Scgd#endif
871.8Schristos
881.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
891.8Schristos	"netbsd",
901.8Schristos	NULL,
911.8Schristos	sendsig,
921.8Schristos	SYS_syscall,
931.8Schristos	SYS_MAXSYSCALL,
941.8Schristos	sysent,
951.8Schristos#ifdef SYSCALL_DEBUG
961.8Schristos	syscallnames,
971.9Scgd#else
981.8Schristos	NULL,
991.9Scgd#endif
1001.9Scgd	ELF_AUX_ENTRIES * sizeof(AuxInfo),
1011.9Scgd	ELFNAME(copyargs),
1021.8Schristos	setregs,
1031.8Schristos	sigcode,
1041.8Schristos	esigcode,
1051.9Scgd};
1061.9Scgd
1071.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1081.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1091.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1101.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1111.9Scgd#endif
1121.9Scgd#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
1131.9Scgd	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
1141.9Scgd#endif
1151.8Schristos};
1161.8Schristos
1171.9Scgd#define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
1181.8Schristos
1191.8Schristos/*
1201.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1211.1Sfvdl * extra information in case of dynamic binding.
1221.1Sfvdl */
1231.1Sfvdlvoid *
1241.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1251.1Sfvdl	struct exec_package *pack;
1261.1Sfvdl	struct ps_strings *arginfo;
1271.1Sfvdl	void *stack;
1281.1Sfvdl	void *argp;
1291.1Sfvdl{
1301.1Sfvdl	size_t len;
1311.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1321.1Sfvdl	struct elf_args *ap;
1331.1Sfvdl
1341.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1351.4Sfvdl	if (!stack)
1361.1Sfvdl		return NULL;
1371.1Sfvdl
1381.1Sfvdl	/*
1391.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1401.1Sfvdl	 * linked binaries
1411.1Sfvdl	 */
1421.1Sfvdl	if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
1431.4Sfvdl		a = ai;
1441.1Sfvdl
1451.1Sfvdl		a->au_id = AUX_phdr;
1461.1Sfvdl		a->au_v = ap->arg_phaddr;
1471.1Sfvdl		a++;
1481.1Sfvdl
1491.1Sfvdl		a->au_id = AUX_phent;
1501.1Sfvdl		a->au_v = ap->arg_phentsize;
1511.1Sfvdl		a++;
1521.1Sfvdl
1531.1Sfvdl		a->au_id = AUX_phnum;
1541.1Sfvdl		a->au_v = ap->arg_phnum;
1551.1Sfvdl		a++;
1561.1Sfvdl
1571.1Sfvdl		a->au_id = AUX_pagesz;
1581.1Sfvdl		a->au_v = NBPG;
1591.1Sfvdl		a++;
1601.1Sfvdl
1611.1Sfvdl		a->au_id = AUX_base;
1621.1Sfvdl		a->au_v = ap->arg_interp;
1631.1Sfvdl		a++;
1641.1Sfvdl
1651.1Sfvdl		a->au_id = AUX_flags;
1661.1Sfvdl		a->au_v = 0;
1671.1Sfvdl		a++;
1681.1Sfvdl
1691.1Sfvdl		a->au_id = AUX_entry;
1701.1Sfvdl		a->au_v = ap->arg_entry;
1711.1Sfvdl		a++;
1721.1Sfvdl
1731.1Sfvdl		a->au_id = AUX_null;
1741.1Sfvdl		a->au_v = 0;
1751.1Sfvdl		a++;
1761.1Sfvdl
1771.1Sfvdl		free((char *) ap, M_TEMP);
1781.9Scgd		pack->ep_emul_arg = NULL;
1791.4Sfvdl		len = ELF_AUX_ENTRIES * sizeof (AuxInfo);
1801.4Sfvdl		if (copyout(ai, stack, len))
1811.4Sfvdl			return NULL;
1821.4Sfvdl		stack += len;
1831.1Sfvdl	}
1841.4Sfvdl	return stack;
1851.1Sfvdl}
1861.1Sfvdl
1871.1Sfvdl/*
1881.1Sfvdl * elf_check_header():
1891.1Sfvdl *
1901.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
1911.1Sfvdl *
1921.1Sfvdl * XXX machine type needs to be moved to <machine/param.h> so
1931.1Sfvdl * just one comparison can be done. Unfortunately, there is both
1941.1Sfvdl * em_486 and em_386, so this would not work on the i386.
1951.1Sfvdl */
1961.1Sfvdlint
1971.9ScgdELFNAME(check_header)(eh, type)
1981.9Scgd	Elf_Ehdr *eh;
1991.1Sfvdl	int type;
2001.1Sfvdl{
2011.3Sthorpej
2021.9Scgd	if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
2031.1Sfvdl		return ENOEXEC;
2041.1Sfvdl
2051.1Sfvdl	switch (eh->e_machine) {
2061.9Scgd
2071.9Scgd#if 0 /* XXX notyet */
2081.9Scgd	ELFDEFNNAME(MACHDEP_ID_CASES);
2091.9Scgd#else /* XXX */
2101.1Sfvdl#ifdef i386
2111.9Scgd	case Elf_em_386:
2121.9Scgd	case Elf_em_486:
2131.9Scgd		break;
2141.1Sfvdl#endif
2151.1Sfvdl#ifdef sparc
2161.9Scgd	case Elf_em_sparc:
2171.9Scgd		break;
2181.1Sfvdl#endif
2191.8Schristos#ifdef mips
2201.9Scgd	case Elf_em_mips:
2211.9Scgd		break;
2221.8Schristos#endif
2231.9Scgd#ifdef alpha
2241.9Scgd	case Elf_em_alpha:
2251.1Sfvdl		break;
2261.9Scgd#endif
2271.9Scgd#endif /* XXX */
2281.1Sfvdl
2291.1Sfvdl	default:
2301.1Sfvdl		return ENOEXEC;
2311.1Sfvdl	}
2321.1Sfvdl
2331.1Sfvdl	if (eh->e_type != type)
2341.1Sfvdl		return ENOEXEC;
2351.1Sfvdl
2361.1Sfvdl	return 0;
2371.1Sfvdl}
2381.1Sfvdl
2391.1Sfvdl/*
2401.1Sfvdl * elf_load_psection():
2411.1Sfvdl *
2421.1Sfvdl * Load a psection at the appropriate address
2431.1Sfvdl */
2441.1Sfvdlstatic void
2451.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2461.1Sfvdl	struct exec_vmcmd_set *vcset;
2471.1Sfvdl	struct vnode *vp;
2481.9Scgd	Elf_Phdr *ph;
2491.9Scgd	Elf_Addr *addr;
2501.1Sfvdl	u_long *size;
2511.1Sfvdl	int *prot;
2521.1Sfvdl{
2531.8Schristos	u_long uaddr, msize, psize, rm, rf;
2541.1Sfvdl	long diff, offset;
2551.1Sfvdl
2561.1Sfvdl	/*
2571.1Sfvdl         * If the user specified an address, then we load there.
2581.1Sfvdl         */
2591.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2601.1Sfvdl		if (ph->p_align > 1) {
2611.1Sfvdl			*addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
2621.1Sfvdl			uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
2631.1Sfvdl		} else
2641.1Sfvdl			uaddr = ph->p_vaddr;
2651.1Sfvdl		diff = ph->p_vaddr - uaddr;
2661.1Sfvdl	} else {
2671.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2681.1Sfvdl		if (ph->p_align > 1)
2691.1Sfvdl			*addr = ELF_ALIGN(uaddr, ph->p_align);
2701.1Sfvdl		diff = uaddr - *addr;
2711.1Sfvdl	}
2721.1Sfvdl
2731.9Scgd	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
2741.9Scgd	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
2751.9Scgd	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
2761.1Sfvdl
2771.1Sfvdl	offset = ph->p_offset - diff;
2781.1Sfvdl	*size = ph->p_filesz + diff;
2791.1Sfvdl	msize = ph->p_memsz + diff;
2801.8Schristos	psize = round_page(*size);
2811.1Sfvdl
2821.9Scgd	if ((ph->p_flags & Elf_pf_w) != 0) {
2831.8Schristos		/*
2841.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
2851.8Schristos		 * data page if it's not page aligned we map the last page
2861.8Schristos		 * readvn.
2871.8Schristos		 */
2881.8Schristos		psize = trunc_page(*size);
2891.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2901.8Schristos		    offset, *prot);
2911.8Schristos		if(psize != *size)
2921.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
2931.8Schristos			    *addr + psize, vp, offset + psize, *prot);
2941.9Scgd	} else
2951.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2961.8Schristos		    offset, *prot);
2971.1Sfvdl
2981.1Sfvdl	/*
2991.1Sfvdl         * Check if we need to extend the size of the segment
3001.1Sfvdl         */
3011.1Sfvdl	rm = round_page(*addr + msize);
3021.1Sfvdl	rf = round_page(*addr + *size);
3031.1Sfvdl
3041.1Sfvdl	if (rm != rf) {
3051.1Sfvdl		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
3061.1Sfvdl		*size = msize;
3071.1Sfvdl	}
3081.1Sfvdl}
3091.1Sfvdl
3101.1Sfvdl/*
3111.1Sfvdl * elf_read_from():
3121.1Sfvdl *
3131.1Sfvdl *	Read from vnode into buffer at offset.
3141.1Sfvdl */
3151.7Schristosint
3161.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3171.1Sfvdl	struct vnode *vp;
3181.1Sfvdl	u_long off;
3191.1Sfvdl	struct proc *p;
3201.1Sfvdl	caddr_t buf;
3211.1Sfvdl	int size;
3221.1Sfvdl{
3231.1Sfvdl	int error;
3241.1Sfvdl	int resid;
3251.1Sfvdl
3261.1Sfvdl	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
3271.1Sfvdl			     off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
3281.1Sfvdl			     &resid, p)) != 0)
3291.1Sfvdl		return error;
3301.1Sfvdl	/*
3311.1Sfvdl         * See if we got all of it
3321.1Sfvdl         */
3331.1Sfvdl	if (resid != 0)
3341.4Sfvdl		return ENOEXEC;
3351.1Sfvdl	return 0;
3361.1Sfvdl}
3371.1Sfvdl
3381.1Sfvdl/*
3391.1Sfvdl * elf_load_file():
3401.1Sfvdl *
3411.1Sfvdl * Load a file (interpreter/library) pointed to by path
3421.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3431.1Sfvdl * so it might be used externally.
3441.1Sfvdl */
3451.1Sfvdlint
3461.9ScgdELFNAME(load_file)(p, path, vcset, entry, ap, last)
3471.1Sfvdl	struct proc *p;
3481.1Sfvdl	char *path;
3491.1Sfvdl	struct exec_vmcmd_set *vcset;
3501.1Sfvdl	u_long *entry;
3511.1Sfvdl	struct elf_args	*ap;
3521.9Scgd	Elf_Addr *last;
3531.1Sfvdl{
3541.1Sfvdl	int error, i;
3551.1Sfvdl	struct nameidata nd;
3561.9Scgd	Elf_Ehdr eh;
3571.9Scgd	Elf_Phdr *ph = NULL;
3581.1Sfvdl	u_long phsize;
3591.1Sfvdl	char *bp = NULL;
3601.9Scgd	Elf_Addr addr = *last;
3611.1Sfvdl
3621.1Sfvdl	bp = path;
3631.1Sfvdl	/*
3641.1Sfvdl         * 1. open file
3651.1Sfvdl         * 2. read filehdr
3661.1Sfvdl         * 3. map text, data, and bss out of it using VM_*
3671.1Sfvdl         */
3681.1Sfvdl	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
3691.1Sfvdl	if ((error = namei(&nd)) != 0) {
3701.1Sfvdl		return error;
3711.1Sfvdl	}
3721.9Scgd	if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, (caddr_t) &eh,
3731.1Sfvdl				    sizeof(eh))) != 0)
3741.1Sfvdl		goto bad;
3751.1Sfvdl
3761.9Scgd	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
3771.1Sfvdl		goto bad;
3781.1Sfvdl
3791.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
3801.9Scgd	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
3811.1Sfvdl
3821.9Scgd	if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff,
3831.9Scgd	    (caddr_t) ph, phsize)) != 0)
3841.1Sfvdl		goto bad;
3851.1Sfvdl
3861.1Sfvdl	/*
3871.1Sfvdl         * Load all the necessary sections
3881.1Sfvdl         */
3891.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
3901.1Sfvdl		u_long size = 0;
3911.1Sfvdl		int prot = 0;
3921.1Sfvdl
3931.1Sfvdl		switch (ph[i].p_type) {
3941.9Scgd		case Elf_pt_load:
3951.9Scgd			ELFNAME(load_psection)(vcset, nd.ni_vp, &ph[i], &addr,
3961.1Sfvdl						&size, &prot);
3971.4Sfvdl			/* If entry is within this section it must be text */
3981.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
3991.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4001.1Sfvdl				*entry = addr + eh.e_entry;
4011.1Sfvdl				ap->arg_interp = addr;
4021.1Sfvdl			}
4031.1Sfvdl			addr += size;
4041.1Sfvdl			break;
4051.1Sfvdl
4061.9Scgd		case Elf_pt_dynamic:
4071.9Scgd		case Elf_pt_phdr:
4081.9Scgd		case Elf_pt_note:
4091.1Sfvdl			break;
4101.1Sfvdl
4111.1Sfvdl		default:
4121.1Sfvdl			break;
4131.1Sfvdl		}
4141.1Sfvdl	}
4151.1Sfvdl
4161.1Sfvdlbad:
4171.1Sfvdl	if (ph != NULL)
4181.1Sfvdl		free((char *) ph, M_TEMP);
4191.1Sfvdl
4201.1Sfvdl	*last = addr;
4211.1Sfvdl	vrele(nd.ni_vp);
4221.1Sfvdl	return error;
4231.1Sfvdl}
4241.1Sfvdl
4251.1Sfvdl/*
4261.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4271.1Sfvdl *
4281.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4291.1Sfvdl *
4301.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4311.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4321.1Sfvdl * text, data, bss, and stack segments.
4331.1Sfvdl */
4341.1Sfvdlint
4351.9ScgdELFNAME2(exec,makecmds)(p, epp)
4361.1Sfvdl	struct proc *p;
4371.1Sfvdl	struct exec_package *epp;
4381.1Sfvdl{
4391.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
4401.9Scgd	Elf_Phdr *ph, *pp;
4411.9Scgd	Elf_Addr phdr = 0, pos = 0;
4421.4Sfvdl	int error, i, n, nload;
4431.1Sfvdl	char interp[MAXPATHLEN];
4441.9Scgd	u_long phsize;
4451.1Sfvdl
4461.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
4471.1Sfvdl		return ENOEXEC;
4481.1Sfvdl
4491.9Scgd	if (ELFNAME(check_header)(eh, Elf_et_exec))
4501.1Sfvdl		return ENOEXEC;
4511.1Sfvdl
4521.1Sfvdl	/*
4531.1Sfvdl         * check if vnode is in open for writing, because we want to
4541.1Sfvdl         * demand-page out of it.  if it is, don't do it, for various
4551.1Sfvdl         * reasons
4561.1Sfvdl         */
4571.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4581.1Sfvdl#ifdef DIAGNOSTIC
4591.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4601.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4611.1Sfvdl#endif
4621.1Sfvdl		return ETXTBSY;
4631.1Sfvdl	}
4641.1Sfvdl	/*
4651.1Sfvdl         * Allocate space to hold all the program headers, and read them
4661.1Sfvdl         * from the file
4671.1Sfvdl         */
4681.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
4691.9Scgd	ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
4701.1Sfvdl
4711.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
4721.8Schristos	    (caddr_t) ph, phsize)) != 0)
4731.1Sfvdl		goto bad;
4741.1Sfvdl
4751.9Scgd	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
4761.9Scgd	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
4771.1Sfvdl
4781.1Sfvdl	interp[0] = '\0';
4791.1Sfvdl
4801.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
4811.1Sfvdl		pp = &ph[i];
4821.9Scgd		if (pp->p_type == Elf_pt_interp) {
4831.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
4841.1Sfvdl				goto bad;
4851.9Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp, pp->p_offset,
4861.1Sfvdl				      (caddr_t) interp, pp->p_filesz)) != 0)
4871.1Sfvdl				goto bad;
4881.1Sfvdl			break;
4891.1Sfvdl		}
4901.1Sfvdl	}
4911.1Sfvdl
4921.9Scgd	/*
4931.8Schristos	 * Setup things for native emulation.
4941.8Schristos	 */
4951.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
4961.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
4971.8Schristos
4981.1Sfvdl	/*
4991.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5001.1Sfvdl	 * See which one will accept this executable. This currently only
5011.1Sfvdl	 * applies to Linux and SVR4 on the i386.
5021.1Sfvdl	 *
5031.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5041.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5051.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5061.1Sfvdl	 * set the ep_emul field in the exec package structure.
5071.1Sfvdl	 */
5081.9Scgd	if ((n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0])) {
5091.1Sfvdl		error = ENOEXEC;
5101.1Sfvdl		for (i = 0; i < n && error; i++)
5111.9Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh, interp, &pos);
5121.1Sfvdl
5131.8Schristos#ifdef notyet
5141.8Schristos		/*
5151.8Schristos		 * We should really use a signature in our native binaries
5161.8Schristos		 * and have our own probe function for matching binaries,
5171.8Schristos		 * before trying the emulations. For now, if the emulation
5181.8Schristos		 * probes failed we default to native.
5191.8Schristos		 */
5201.1Sfvdl		if (error)
5211.1Sfvdl			goto bad;
5221.8Schristos#endif
5231.1Sfvdl	}
5241.1Sfvdl
5251.1Sfvdl	/*
5261.1Sfvdl         * Load all the necessary sections
5271.1Sfvdl         */
5281.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5291.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5301.9Scgd		u_long size = 0;
5311.1Sfvdl		int prot = 0;
5321.1Sfvdl
5331.1Sfvdl		pp = &ph[i];
5341.1Sfvdl
5351.1Sfvdl		switch (ph[i].p_type) {
5361.9Scgd		case Elf_pt_load:
5371.4Sfvdl			/*
5381.4Sfvdl			 * XXX
5391.4Sfvdl			 * Can handle only 2 sections: text and data
5401.4Sfvdl			 */
5411.4Sfvdl			if (nload++ == 2)
5421.4Sfvdl				goto bad;
5431.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
5441.1Sfvdl				&ph[i], &addr, &size, &prot);
5451.4Sfvdl			/*
5461.4Sfvdl			 * Decide whether it's text or data by looking
5471.4Sfvdl			 * at the entry point.
5481.4Sfvdl			 */
5491.4Sfvdl			if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
5501.4Sfvdl				epp->ep_taddr = addr;
5511.4Sfvdl				epp->ep_tsize = size;
5521.4Sfvdl			} else {
5531.4Sfvdl				epp->ep_daddr = addr;
5541.4Sfvdl				epp->ep_dsize = size;
5551.4Sfvdl			}
5561.1Sfvdl			break;
5571.1Sfvdl
5581.9Scgd		case Elf_pt_shlib:
5591.1Sfvdl			error = ENOEXEC;
5601.1Sfvdl			goto bad;
5611.1Sfvdl
5621.9Scgd		case Elf_pt_interp:
5631.1Sfvdl			/* Already did this one */
5641.9Scgd		case Elf_pt_dynamic:
5651.9Scgd		case Elf_pt_note:
5661.1Sfvdl			break;
5671.1Sfvdl
5681.9Scgd		case Elf_pt_phdr:
5691.4Sfvdl			/* Note address of program headers (in text segment) */
5701.4Sfvdl			phdr = pp->p_vaddr;
5711.7Schristos			break;
5721.4Sfvdl
5731.1Sfvdl		default:
5741.1Sfvdl			/*
5751.9Scgd			 * Not fatal; we don't need to understand everything.
5761.1Sfvdl			 */
5771.1Sfvdl			break;
5781.1Sfvdl		}
5791.1Sfvdl	}
5801.5Sfvdl
5811.5Sfvdl	/*
5821.5Sfvdl	 * If no position to load the interpreter was set by a probe
5831.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
5841.5Sfvdl	 * would (i.e. something safely out of the way).
5851.5Sfvdl	 */
5861.9Scgd	if (pos == ELFDEFNNAME(NO_ADDR) &&
5871.9Scgd	    epp->ep_emul == &ELFNAMEEND(emul_netbsd))
5881.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
5891.1Sfvdl
5901.1Sfvdl	/*
5911.1Sfvdl         * Check if we found a dynamically linked binary and arrange to load
5921.1Sfvdl         * it's interpreter
5931.1Sfvdl         */
5941.1Sfvdl	if (interp[0]) {
5951.1Sfvdl		struct elf_args *ap;
5961.1Sfvdl
5971.1Sfvdl		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
5981.1Sfvdl						 M_TEMP, M_WAITOK);
5991.9Scgd		if ((error = ELFNAME(load_file)(p, interp, &epp->ep_vmcmds,
6001.1Sfvdl				&epp->ep_entry, ap, &pos)) != 0) {
6011.1Sfvdl			free((char *) ap, M_TEMP);
6021.1Sfvdl			goto bad;
6031.1Sfvdl		}
6041.1Sfvdl		pos += phsize;
6051.4Sfvdl		ap->arg_phaddr = phdr;
6061.1Sfvdl
6071.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6081.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6091.1Sfvdl		ap->arg_entry = eh->e_entry;
6101.1Sfvdl
6111.1Sfvdl		epp->ep_emul_arg = ap;
6121.1Sfvdl	} else
6131.1Sfvdl		epp->ep_entry = eh->e_entry;
6141.1Sfvdl
6151.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6161.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6171.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
6181.8Schristos	    VM_PROT_READ);
6191.8Schristos#endif
6201.1Sfvdl	free((char *) ph, M_TEMP);
6211.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
6221.9Scgd	return exec_elf_setup_stack(p, epp);
6231.1Sfvdl
6241.1Sfvdlbad:
6251.1Sfvdl	free((char *) ph, M_TEMP);
6261.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6271.1Sfvdl	return ENOEXEC;
6281.1Sfvdl}
629