exec_elf32.c revision 1.22
11.22Scgd/*	$NetBSD: exec_elf32.c,v 1.22 1996/12/17 22:04:20 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.1Sfvdl#include <sys/param.h>
371.1Sfvdl#include <sys/systm.h>
381.1Sfvdl#include <sys/kernel.h>
391.1Sfvdl#include <sys/proc.h>
401.1Sfvdl#include <sys/malloc.h>
411.1Sfvdl#include <sys/namei.h>
421.1Sfvdl#include <sys/vnode.h>
431.1Sfvdl#include <sys/exec.h>
441.1Sfvdl#include <sys/exec_elf.h>
451.9Scgd#include <sys/fcntl.h>
461.8Schristos#include <sys/syscall.h>
471.8Schristos#include <sys/signalvar.h>
481.14Scgd#include <sys/mount.h>
491.14Scgd#include <sys/stat.h>
501.1Sfvdl
511.1Sfvdl#include <sys/mman.h>
521.1Sfvdl#include <vm/vm.h>
531.1Sfvdl#include <vm/vm_param.h>
541.1Sfvdl#include <vm/vm_map.h>
551.1Sfvdl
561.1Sfvdl#include <machine/cpu.h>
571.1Sfvdl#include <machine/reg.h>
581.1Sfvdl
591.1Sfvdl#ifdef COMPAT_LINUX
601.1Sfvdl#include <compat/linux/linux_exec.h>
611.1Sfvdl#endif
621.1Sfvdl
631.1Sfvdl#ifdef COMPAT_SVR4
641.1Sfvdl#include <compat/svr4/svr4_exec.h>
651.1Sfvdl#endif
661.1Sfvdl
671.9Scgd#define	CONCAT(x,y)	__CONCAT(x,y)
681.9Scgd#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
691.9Scgd#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
701.9Scgd#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
711.9Scgd#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
721.9Scgd
731.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
741.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
751.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
761.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
771.17Scgd	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
781.1Sfvdl
791.8Schristosextern char sigcode[], esigcode[];
801.8Schristos#ifdef SYSCALL_DEBUG
811.8Schristosextern char *syscallnames[];
821.17Scgd#endif
831.8Schristos
841.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
851.8Schristos	"netbsd",
861.8Schristos	NULL,
871.8Schristos	sendsig,
881.8Schristos	SYS_syscall,
891.8Schristos	SYS_MAXSYSCALL,
901.8Schristos	sysent,
911.8Schristos#ifdef SYSCALL_DEBUG
921.8Schristos	syscallnames,
931.17Scgd#else
941.8Schristos	NULL,
951.17Scgd#endif
961.9Scgd	ELF_AUX_ENTRIES * sizeof(AuxInfo),
971.17Scgd	ELFNAME(copyargs),
981.8Schristos	setregs,
991.8Schristos	sigcode,
1001.8Schristos	esigcode,
1011.17Scgd};
1021.9Scgd
1031.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1041.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1051.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1061.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1071.9Scgd#endif
1081.9Scgd#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
1091.9Scgd	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
1101.9Scgd#endif
1111.8Schristos};
1121.8Schristos
1131.18Scgd/* round up and down to page boundaries. */
1141.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1151.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1161.8Schristos
1171.8Schristos/*
1181.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1191.1Sfvdl * extra information in case of dynamic binding.
1201.1Sfvdl */
1211.1Sfvdlvoid *
1221.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1231.1Sfvdl	struct exec_package *pack;
1241.1Sfvdl	struct ps_strings *arginfo;
1251.1Sfvdl	void *stack;
1261.1Sfvdl	void *argp;
1271.1Sfvdl{
1281.1Sfvdl	size_t len;
1291.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1301.1Sfvdl	struct elf_args *ap;
1311.1Sfvdl
1321.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1331.4Sfvdl	if (!stack)
1341.1Sfvdl		return NULL;
1351.1Sfvdl
1361.22Scgd	a = ai;
1371.22Scgd
1381.1Sfvdl	/*
1391.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1401.1Sfvdl	 * linked binaries
1411.1Sfvdl	 */
1421.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1431.1Sfvdl
1441.1Sfvdl		a->au_id = AUX_phdr;
1451.1Sfvdl		a->au_v = ap->arg_phaddr;
1461.1Sfvdl		a++;
1471.1Sfvdl
1481.1Sfvdl		a->au_id = AUX_phent;
1491.1Sfvdl		a->au_v = ap->arg_phentsize;
1501.1Sfvdl		a++;
1511.1Sfvdl
1521.1Sfvdl		a->au_id = AUX_phnum;
1531.1Sfvdl		a->au_v = ap->arg_phnum;
1541.1Sfvdl		a++;
1551.1Sfvdl
1561.1Sfvdl		a->au_id = AUX_pagesz;
1571.1Sfvdl		a->au_v = NBPG;
1581.1Sfvdl		a++;
1591.1Sfvdl
1601.1Sfvdl		a->au_id = AUX_base;
1611.1Sfvdl		a->au_v = ap->arg_interp;
1621.1Sfvdl		a++;
1631.1Sfvdl
1641.1Sfvdl		a->au_id = AUX_flags;
1651.1Sfvdl		a->au_v = 0;
1661.1Sfvdl		a++;
1671.1Sfvdl
1681.1Sfvdl		a->au_id = AUX_entry;
1691.1Sfvdl		a->au_v = ap->arg_entry;
1701.1Sfvdl		a++;
1711.1Sfvdl
1721.17Scgd		free((char *)ap, M_TEMP);
1731.9Scgd		pack->ep_emul_arg = NULL;
1741.1Sfvdl	}
1751.22Scgd
1761.22Scgd	a->au_id = AUX_null;
1771.22Scgd	a->au_v = 0;
1781.22Scgd	a++;
1791.22Scgd
1801.22Scgd	len = (a - ai) * sizeof (AuxInfo);
1811.22Scgd	if (copyout(ai, stack, len))
1821.22Scgd		return NULL;
1831.22Scgd	stack += len;
1841.22Scgd
1851.4Sfvdl	return stack;
1861.1Sfvdl}
1871.1Sfvdl
1881.1Sfvdl/*
1891.1Sfvdl * elf_check_header():
1901.1Sfvdl *
1911.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
1921.1Sfvdl */
1931.17Scgdint
1941.9ScgdELFNAME(check_header)(eh, type)
1951.9Scgd	Elf_Ehdr *eh;
1961.1Sfvdl	int type;
1971.1Sfvdl{
1981.3Sthorpej
1991.9Scgd	if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
2001.1Sfvdl		return ENOEXEC;
2011.1Sfvdl
2021.1Sfvdl	switch (eh->e_machine) {
2031.9Scgd
2041.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2051.1Sfvdl
2061.1Sfvdl	default:
2071.1Sfvdl		return ENOEXEC;
2081.1Sfvdl	}
2091.1Sfvdl
2101.1Sfvdl	if (eh->e_type != type)
2111.1Sfvdl		return ENOEXEC;
2121.1Sfvdl
2131.1Sfvdl	return 0;
2141.1Sfvdl}
2151.1Sfvdl
2161.1Sfvdl/*
2171.1Sfvdl * elf_load_psection():
2181.17Scgd *
2191.1Sfvdl * Load a psection at the appropriate address
2201.1Sfvdl */
2211.17Scgdvoid
2221.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2231.1Sfvdl	struct exec_vmcmd_set *vcset;
2241.1Sfvdl	struct vnode *vp;
2251.9Scgd	Elf_Phdr *ph;
2261.9Scgd	Elf_Addr *addr;
2271.1Sfvdl	u_long *size;
2281.1Sfvdl	int *prot;
2291.1Sfvdl{
2301.8Schristos	u_long uaddr, msize, psize, rm, rf;
2311.1Sfvdl	long diff, offset;
2321.1Sfvdl
2331.1Sfvdl	/*
2341.17Scgd	 * If the user specified an address, then we load there.
2351.17Scgd	 */
2361.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2371.1Sfvdl		if (ph->p_align > 1) {
2381.18Scgd			*addr = ELF_ROUND(*addr, ph->p_align);
2391.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2401.1Sfvdl		} else
2411.1Sfvdl			uaddr = ph->p_vaddr;
2421.1Sfvdl		diff = ph->p_vaddr - uaddr;
2431.1Sfvdl	} else {
2441.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2451.1Sfvdl		if (ph->p_align > 1)
2461.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2471.1Sfvdl		diff = uaddr - *addr;
2481.1Sfvdl	}
2491.1Sfvdl
2501.9Scgd	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
2511.9Scgd	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
2521.9Scgd	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
2531.1Sfvdl
2541.1Sfvdl	offset = ph->p_offset - diff;
2551.1Sfvdl	*size = ph->p_filesz + diff;
2561.1Sfvdl	msize = ph->p_memsz + diff;
2571.8Schristos	psize = round_page(*size);
2581.1Sfvdl
2591.9Scgd	if ((ph->p_flags & Elf_pf_w) != 0) {
2601.8Schristos		/*
2611.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
2621.8Schristos		 * data page if it's not page aligned we map the last page
2631.8Schristos		 * readvn.
2641.8Schristos		 */
2651.8Schristos		psize = trunc_page(*size);
2661.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2671.8Schristos		    offset, *prot);
2681.8Schristos		if(psize != *size)
2691.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
2701.8Schristos			    *addr + psize, vp, offset + psize, *prot);
2711.9Scgd	} else
2721.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2731.8Schristos		    offset, *prot);
2741.1Sfvdl
2751.1Sfvdl	/*
2761.17Scgd	 * Check if we need to extend the size of the segment
2771.17Scgd	 */
2781.1Sfvdl	rm = round_page(*addr + msize);
2791.1Sfvdl	rf = round_page(*addr + *size);
2801.1Sfvdl
2811.1Sfvdl	if (rm != rf) {
2821.17Scgd		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
2831.17Scgd		    0, *prot);
2841.1Sfvdl		*size = msize;
2851.1Sfvdl	}
2861.1Sfvdl}
2871.1Sfvdl
2881.1Sfvdl/*
2891.1Sfvdl * elf_read_from():
2901.1Sfvdl *
2911.1Sfvdl *	Read from vnode into buffer at offset.
2921.1Sfvdl */
2931.7Schristosint
2941.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
2951.1Sfvdl	struct vnode *vp;
2961.1Sfvdl	u_long off;
2971.1Sfvdl	struct proc *p;
2981.1Sfvdl	caddr_t buf;
2991.1Sfvdl	int size;
3001.1Sfvdl{
3011.1Sfvdl	int error;
3021.1Sfvdl	int resid;
3031.1Sfvdl
3041.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3051.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3061.1Sfvdl		return error;
3071.1Sfvdl	/*
3081.17Scgd	 * See if we got all of it
3091.17Scgd	 */
3101.1Sfvdl	if (resid != 0)
3111.4Sfvdl		return ENOEXEC;
3121.1Sfvdl	return 0;
3131.1Sfvdl}
3141.1Sfvdl
3151.1Sfvdl/*
3161.1Sfvdl * elf_load_file():
3171.1Sfvdl *
3181.1Sfvdl * Load a file (interpreter/library) pointed to by path
3191.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3201.1Sfvdl * so it might be used externally.
3211.1Sfvdl */
3221.17Scgdint
3231.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3241.1Sfvdl	struct proc *p;
3251.14Scgd	struct exec_package *epp;
3261.1Sfvdl	char *path;
3271.1Sfvdl	struct exec_vmcmd_set *vcset;
3281.1Sfvdl	u_long *entry;
3291.1Sfvdl	struct elf_args	*ap;
3301.9Scgd	Elf_Addr *last;
3311.1Sfvdl{
3321.1Sfvdl	int error, i;
3331.1Sfvdl	struct nameidata nd;
3341.14Scgd	struct vnode *vp;
3351.14Scgd	struct vattr attr;
3361.9Scgd	Elf_Ehdr eh;
3371.9Scgd	Elf_Phdr *ph = NULL;
3381.1Sfvdl	u_long phsize;
3391.1Sfvdl	char *bp = NULL;
3401.9Scgd	Elf_Addr addr = *last;
3411.1Sfvdl
3421.1Sfvdl	bp = path;
3431.1Sfvdl	/*
3441.17Scgd	 * 1. open file
3451.17Scgd	 * 2. read filehdr
3461.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3471.17Scgd	 */
3481.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3491.12Scgd	if ((error = namei(&nd)) != 0)
3501.1Sfvdl		return error;
3511.14Scgd	vp = nd.ni_vp;
3521.14Scgd
3531.14Scgd	/* check for regular file */
3541.14Scgd	if (vp->v_type != VREG) {
3551.14Scgd		error = EACCES;
3561.14Scgd		goto badunlock;
3571.14Scgd	}
3581.14Scgd
3591.17Scgd	/* get attributes */
3601.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
3611.14Scgd		goto badunlock;
3621.14Scgd
3631.14Scgd	/*
3641.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
3651.15Scgd	 * we will be executing code from it, so if the mount point
3661.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
3671.14Scgd	 */
3681.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3691.14Scgd		error = EACCES;
3701.14Scgd		goto badunlock;
3711.14Scgd	}
3721.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
3731.14Scgd		epp->ep_vap->va_mode &= ~(VSUID | VSGID);
3741.14Scgd
3751.14Scgd	/*
3761.14Scgd	 * Similarly, if it's not marked as executable, we don't allow
3771.14Scgd	 * it to be used.  For root we have to see if any exec bit on.
3781.14Scgd	 */
3791.14Scgd	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
3801.14Scgd		goto badunlock;
3811.14Scgd	if ((attr.va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
3821.17Scgd		error = EACCES;
3831.14Scgd		goto badunlock;
3841.14Scgd	}
3851.14Scgd
3861.12Scgd#ifdef notyet /* XXX cgd 960926 */
3871.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
3881.12Scgd#endif
3891.14Scgd	VOP_UNLOCK(vp);
3901.12Scgd
3911.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
3921.17Scgd	    sizeof(eh))) != 0)
3931.1Sfvdl		goto bad;
3941.1Sfvdl
3951.9Scgd	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
3961.1Sfvdl		goto bad;
3971.1Sfvdl
3981.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
3991.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4001.1Sfvdl
4011.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4021.9Scgd	    (caddr_t) ph, phsize)) != 0)
4031.1Sfvdl		goto bad;
4041.1Sfvdl
4051.1Sfvdl	/*
4061.17Scgd	 * Load all the necessary sections
4071.17Scgd	 */
4081.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4091.1Sfvdl		u_long size = 0;
4101.1Sfvdl		int prot = 0;
4111.1Sfvdl
4121.1Sfvdl		switch (ph[i].p_type) {
4131.9Scgd		case Elf_pt_load:
4141.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4151.17Scgd			    &size, &prot);
4161.4Sfvdl			/* If entry is within this section it must be text */
4171.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4181.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4191.21Sfvdl				/* XXX */
4201.21Sfvdl				*entry = addr + eh.e_entry;
4211.21Sfvdl#ifdef mips
4221.21Sfvdl				*entry -= ph[i].p_vaddr;
4231.21Sfvdl#endif
4241.1Sfvdl				ap->arg_interp = addr;
4251.1Sfvdl			}
4261.1Sfvdl			addr += size;
4271.1Sfvdl			break;
4281.1Sfvdl
4291.9Scgd		case Elf_pt_dynamic:
4301.9Scgd		case Elf_pt_phdr:
4311.9Scgd		case Elf_pt_note:
4321.1Sfvdl			break;
4331.1Sfvdl
4341.1Sfvdl		default:
4351.1Sfvdl			break;
4361.1Sfvdl		}
4371.1Sfvdl	}
4381.1Sfvdl
4391.17Scgd	free((char *)ph, M_TEMP);
4401.12Scgd	*last = addr;
4411.14Scgd	vrele(vp);
4421.12Scgd	return 0;
4431.12Scgd
4441.14Scgdbadunlock:
4451.14Scgd	VOP_UNLOCK(vp);
4461.14Scgd
4471.1Sfvdlbad:
4481.1Sfvdl	if (ph != NULL)
4491.17Scgd		free((char *)ph, M_TEMP);
4501.12Scgd#ifdef notyet /* XXX cgd 960926 */
4511.12Scgd	(maybe) VOP_CLOSE it
4521.12Scgd#endif
4531.14Scgd	vrele(vp);
4541.1Sfvdl	return error;
4551.1Sfvdl}
4561.1Sfvdl
4571.1Sfvdl/*
4581.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4591.1Sfvdl *
4601.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4611.1Sfvdl *
4621.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4631.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4641.1Sfvdl * text, data, bss, and stack segments.
4651.1Sfvdl */
4661.1Sfvdlint
4671.9ScgdELFNAME2(exec,makecmds)(p, epp)
4681.1Sfvdl	struct proc *p;
4691.1Sfvdl	struct exec_package *epp;
4701.1Sfvdl{
4711.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
4721.9Scgd	Elf_Phdr *ph, *pp;
4731.9Scgd	Elf_Addr phdr = 0, pos = 0;
4741.4Sfvdl	int error, i, n, nload;
4751.1Sfvdl	char interp[MAXPATHLEN];
4761.9Scgd	u_long phsize;
4771.1Sfvdl
4781.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
4791.1Sfvdl		return ENOEXEC;
4801.1Sfvdl
4811.9Scgd	if (ELFNAME(check_header)(eh, Elf_et_exec))
4821.1Sfvdl		return ENOEXEC;
4831.1Sfvdl
4841.1Sfvdl	/*
4851.17Scgd	 * check if vnode is in open for writing, because we want to
4861.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
4871.17Scgd	 * reasons
4881.17Scgd	 */
4891.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4901.1Sfvdl#ifdef DIAGNOSTIC
4911.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4921.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4931.1Sfvdl#endif
4941.1Sfvdl		return ETXTBSY;
4951.1Sfvdl	}
4961.1Sfvdl	/*
4971.17Scgd	 * Allocate space to hold all the program headers, and read them
4981.17Scgd	 * from the file
4991.17Scgd	 */
5001.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5011.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5021.1Sfvdl
5031.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5041.8Schristos	    (caddr_t) ph, phsize)) != 0)
5051.1Sfvdl		goto bad;
5061.1Sfvdl
5071.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5081.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5091.1Sfvdl
5101.1Sfvdl	interp[0] = '\0';
5111.1Sfvdl
5121.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5131.1Sfvdl		pp = &ph[i];
5141.9Scgd		if (pp->p_type == Elf_pt_interp) {
5151.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5161.1Sfvdl				goto bad;
5171.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5181.17Scgd			    pp->p_offset, (caddr_t) interp,
5191.17Scgd			    pp->p_filesz)) != 0)
5201.1Sfvdl				goto bad;
5211.1Sfvdl			break;
5221.1Sfvdl		}
5231.1Sfvdl	}
5241.1Sfvdl
5251.9Scgd	/*
5261.8Schristos	 * Setup things for native emulation.
5271.8Schristos	 */
5281.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5291.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5301.8Schristos
5311.1Sfvdl	/*
5321.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5331.1Sfvdl	 * See which one will accept this executable. This currently only
5341.1Sfvdl	 * applies to Linux and SVR4 on the i386.
5351.1Sfvdl	 *
5361.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5371.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5381.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5391.1Sfvdl	 * set the ep_emul field in the exec package structure.
5401.1Sfvdl	 */
5411.17Scgd	n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0];
5421.17Scgd	if (n != 0) {
5431.1Sfvdl		error = ENOEXEC;
5441.1Sfvdl		for (i = 0; i < n && error; i++)
5451.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
5461.17Scgd			    interp, &pos);
5471.1Sfvdl
5481.8Schristos#ifdef notyet
5491.8Schristos		/*
5501.8Schristos		 * We should really use a signature in our native binaries
5511.8Schristos		 * and have our own probe function for matching binaries,
5521.8Schristos		 * before trying the emulations. For now, if the emulation
5531.8Schristos		 * probes failed we default to native.
5541.8Schristos		 */
5551.1Sfvdl		if (error)
5561.1Sfvdl			goto bad;
5571.8Schristos#endif
5581.1Sfvdl	}
5591.1Sfvdl
5601.1Sfvdl	/*
5611.17Scgd	 * Load all the necessary sections
5621.17Scgd	 */
5631.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5641.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5651.9Scgd		u_long size = 0;
5661.1Sfvdl		int prot = 0;
5671.1Sfvdl
5681.1Sfvdl		pp = &ph[i];
5691.1Sfvdl
5701.1Sfvdl		switch (ph[i].p_type) {
5711.9Scgd		case Elf_pt_load:
5721.4Sfvdl			/*
5731.4Sfvdl			 * XXX
5741.4Sfvdl			 * Can handle only 2 sections: text and data
5751.4Sfvdl			 */
5761.4Sfvdl			if (nload++ == 2)
5771.4Sfvdl				goto bad;
5781.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
5791.17Scgd			    &ph[i], &addr, &size, &prot);
5801.17Scgd
5811.4Sfvdl			/*
5821.4Sfvdl			 * Decide whether it's text or data by looking
5831.4Sfvdl			 * at the entry point.
5841.4Sfvdl			 */
5851.19Scgd			if (eh->e_entry >= addr &&
5861.19Scgd			    eh->e_entry < (addr + size)) {
5871.4Sfvdl				epp->ep_taddr = addr;
5881.4Sfvdl				epp->ep_tsize = size;
5891.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
5901.19Scgd					epp->ep_daddr = addr;
5911.19Scgd					epp->ep_dsize = size;
5921.19Scgd				}
5931.4Sfvdl			} else {
5941.4Sfvdl				epp->ep_daddr = addr;
5951.4Sfvdl				epp->ep_dsize = size;
5961.4Sfvdl			}
5971.1Sfvdl			break;
5981.1Sfvdl
5991.9Scgd		case Elf_pt_shlib:
6001.1Sfvdl			error = ENOEXEC;
6011.1Sfvdl			goto bad;
6021.1Sfvdl
6031.9Scgd		case Elf_pt_interp:
6041.1Sfvdl			/* Already did this one */
6051.9Scgd		case Elf_pt_dynamic:
6061.9Scgd		case Elf_pt_note:
6071.1Sfvdl			break;
6081.1Sfvdl
6091.9Scgd		case Elf_pt_phdr:
6101.4Sfvdl			/* Note address of program headers (in text segment) */
6111.4Sfvdl			phdr = pp->p_vaddr;
6121.7Schristos			break;
6131.4Sfvdl
6141.1Sfvdl		default:
6151.1Sfvdl			/*
6161.9Scgd			 * Not fatal; we don't need to understand everything.
6171.1Sfvdl			 */
6181.1Sfvdl			break;
6191.1Sfvdl		}
6201.1Sfvdl	}
6211.5Sfvdl
6221.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6231.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6241.5Sfvdl	/*
6251.5Sfvdl	 * If no position to load the interpreter was set by a probe
6261.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6271.5Sfvdl	 * would (i.e. something safely out of the way).
6281.5Sfvdl	 */
6291.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6301.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6311.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6321.1Sfvdl
6331.1Sfvdl	/*
6341.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6351.17Scgd	 * it's interpreter
6361.17Scgd	 */
6371.1Sfvdl	if (interp[0]) {
6381.1Sfvdl		struct elf_args *ap;
6391.1Sfvdl
6401.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6411.17Scgd		    M_TEMP, M_WAITOK);
6421.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6431.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6441.17Scgd			free((char *)ap, M_TEMP);
6451.1Sfvdl			goto bad;
6461.1Sfvdl		}
6471.1Sfvdl		pos += phsize;
6481.4Sfvdl		ap->arg_phaddr = phdr;
6491.1Sfvdl
6501.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6511.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6521.1Sfvdl		ap->arg_entry = eh->e_entry;
6531.1Sfvdl
6541.1Sfvdl		epp->ep_emul_arg = ap;
6551.1Sfvdl	} else
6561.1Sfvdl		epp->ep_entry = eh->e_entry;
6571.1Sfvdl
6581.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6591.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6601.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
6611.8Schristos	    VM_PROT_READ);
6621.8Schristos#endif
6631.17Scgd	free((char *)ph, M_TEMP);
6641.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
6651.9Scgd	return exec_elf_setup_stack(p, epp);
6661.1Sfvdl
6671.1Sfvdlbad:
6681.17Scgd	free((char *)ph, M_TEMP);
6691.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6701.1Sfvdl	return ENOEXEC;
6711.1Sfvdl}
672