exec_elf32.c revision 1.32
11.32Sthorpej/*	$NetBSD: exec_elf32.c,v 1.32 1998/06/26 00:07:45 thorpej 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.30Sthorpej
361.30Sthorpej#include "opt_compat_linux.h"
371.31Sthorpej#include "opt_compat_ibcs2.h"
381.32Sthorpej#include "opt_compat_svr4.h"
391.9Scgd
401.1Sfvdl#include <sys/param.h>
411.1Sfvdl#include <sys/systm.h>
421.1Sfvdl#include <sys/kernel.h>
431.1Sfvdl#include <sys/proc.h>
441.1Sfvdl#include <sys/malloc.h>
451.1Sfvdl#include <sys/namei.h>
461.1Sfvdl#include <sys/vnode.h>
471.1Sfvdl#include <sys/exec.h>
481.1Sfvdl#include <sys/exec_elf.h>
491.9Scgd#include <sys/fcntl.h>
501.8Schristos#include <sys/syscall.h>
511.8Schristos#include <sys/signalvar.h>
521.14Scgd#include <sys/mount.h>
531.14Scgd#include <sys/stat.h>
541.1Sfvdl
551.1Sfvdl#include <sys/mman.h>
561.1Sfvdl#include <vm/vm.h>
571.1Sfvdl#include <vm/vm_param.h>
581.1Sfvdl#include <vm/vm_map.h>
591.1Sfvdl
601.1Sfvdl#include <machine/cpu.h>
611.1Sfvdl#include <machine/reg.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.27Sscottb#ifdef COMPAT_IBCS2
721.27Sscottb#include <compat/ibcs2/ibcs2_exec.h>
731.27Sscottb#endif
741.27Sscottb
751.9Scgd#define	CONCAT(x,y)	__CONCAT(x,y)
761.9Scgd#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
771.9Scgd#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
781.9Scgd#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
791.9Scgd#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
801.9Scgd
811.17Scgdint	ELFNAME(check_header) __P((Elf_Ehdr *, int));
821.17Scgdint	ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
831.17Scgd	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
841.17Scgdvoid	ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
851.17Scgd	    Elf_Phdr *, Elf_Addr *, u_long *, int *));
861.1Sfvdl
871.8Schristosextern char sigcode[], esigcode[];
881.8Schristos#ifdef SYSCALL_DEBUG
891.8Schristosextern char *syscallnames[];
901.17Scgd#endif
911.8Schristos
921.9Scgdstruct emul ELFNAMEEND(emul_netbsd) = {
931.8Schristos	"netbsd",
941.8Schristos	NULL,
951.8Schristos	sendsig,
961.8Schristos	SYS_syscall,
971.8Schristos	SYS_MAXSYSCALL,
981.8Schristos	sysent,
991.8Schristos#ifdef SYSCALL_DEBUG
1001.8Schristos	syscallnames,
1011.17Scgd#else
1021.8Schristos	NULL,
1031.17Scgd#endif
1041.9Scgd	ELF_AUX_ENTRIES * sizeof(AuxInfo),
1051.17Scgd	ELFNAME(copyargs),
1061.8Schristos	setregs,
1071.8Schristos	sigcode,
1081.8Schristos	esigcode,
1091.17Scgd};
1101.9Scgd
1111.9Scgdint (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
1121.9Scgd    Elf_Ehdr *, char *, Elf_Addr *)) = {
1131.23Shpeyerl#if defined(COMPAT_LINUX) && (ELFSIZE == 32)
1141.23Shpeyerl	ELFNAME2(linux,probe),			/* XXX not 64-bit safe */
1151.23Shpeyerl#endif
1161.9Scgd#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
1171.9Scgd	ELFNAME2(svr4,probe),			/* XXX not 64-bit safe */
1181.9Scgd#endif
1191.27Sscottb#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
1201.27Sscottb	ELFNAME2(ibcs2,probe),			/* XXX not 64-bit safe */
1211.27Sscottb#endif
1221.8Schristos};
1231.8Schristos
1241.18Scgd/* round up and down to page boundaries. */
1251.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1261.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1271.8Schristos
1281.8Schristos/*
1291.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1301.1Sfvdl * extra information in case of dynamic binding.
1311.1Sfvdl */
1321.1Sfvdlvoid *
1331.9ScgdELFNAME(copyargs)(pack, arginfo, stack, argp)
1341.1Sfvdl	struct exec_package *pack;
1351.1Sfvdl	struct ps_strings *arginfo;
1361.1Sfvdl	void *stack;
1371.1Sfvdl	void *argp;
1381.1Sfvdl{
1391.1Sfvdl	size_t len;
1401.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1411.1Sfvdl	struct elf_args *ap;
1421.1Sfvdl
1431.4Sfvdl	stack = copyargs(pack, arginfo, stack, argp);
1441.4Sfvdl	if (!stack)
1451.1Sfvdl		return NULL;
1461.1Sfvdl
1471.22Scgd	a = ai;
1481.22Scgd
1491.1Sfvdl	/*
1501.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1511.1Sfvdl	 * linked binaries
1521.1Sfvdl	 */
1531.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1541.1Sfvdl
1551.1Sfvdl		a->au_id = AUX_phdr;
1561.1Sfvdl		a->au_v = ap->arg_phaddr;
1571.1Sfvdl		a++;
1581.1Sfvdl
1591.1Sfvdl		a->au_id = AUX_phent;
1601.1Sfvdl		a->au_v = ap->arg_phentsize;
1611.1Sfvdl		a++;
1621.1Sfvdl
1631.1Sfvdl		a->au_id = AUX_phnum;
1641.1Sfvdl		a->au_v = ap->arg_phnum;
1651.1Sfvdl		a++;
1661.1Sfvdl
1671.1Sfvdl		a->au_id = AUX_pagesz;
1681.1Sfvdl		a->au_v = NBPG;
1691.1Sfvdl		a++;
1701.1Sfvdl
1711.1Sfvdl		a->au_id = AUX_base;
1721.1Sfvdl		a->au_v = ap->arg_interp;
1731.1Sfvdl		a++;
1741.1Sfvdl
1751.1Sfvdl		a->au_id = AUX_flags;
1761.1Sfvdl		a->au_v = 0;
1771.1Sfvdl		a++;
1781.1Sfvdl
1791.1Sfvdl		a->au_id = AUX_entry;
1801.1Sfvdl		a->au_v = ap->arg_entry;
1811.1Sfvdl		a++;
1821.1Sfvdl
1831.17Scgd		free((char *)ap, M_TEMP);
1841.9Scgd		pack->ep_emul_arg = NULL;
1851.1Sfvdl	}
1861.22Scgd
1871.22Scgd	a->au_id = AUX_null;
1881.22Scgd	a->au_v = 0;
1891.22Scgd	a++;
1901.22Scgd
1911.22Scgd	len = (a - ai) * sizeof (AuxInfo);
1921.22Scgd	if (copyout(ai, stack, len))
1931.22Scgd		return NULL;
1941.29Skleink	(caddr_t)stack += len;
1951.22Scgd
1961.4Sfvdl	return stack;
1971.1Sfvdl}
1981.1Sfvdl
1991.1Sfvdl/*
2001.1Sfvdl * elf_check_header():
2011.1Sfvdl *
2021.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
2031.1Sfvdl */
2041.17Scgdint
2051.9ScgdELFNAME(check_header)(eh, type)
2061.9Scgd	Elf_Ehdr *eh;
2071.1Sfvdl	int type;
2081.1Sfvdl{
2091.3Sthorpej
2101.9Scgd	if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
2111.1Sfvdl		return ENOEXEC;
2121.1Sfvdl
2131.1Sfvdl	switch (eh->e_machine) {
2141.9Scgd
2151.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2161.1Sfvdl
2171.1Sfvdl	default:
2181.1Sfvdl		return ENOEXEC;
2191.1Sfvdl	}
2201.1Sfvdl
2211.1Sfvdl	if (eh->e_type != type)
2221.1Sfvdl		return ENOEXEC;
2231.1Sfvdl
2241.1Sfvdl	return 0;
2251.1Sfvdl}
2261.1Sfvdl
2271.1Sfvdl/*
2281.1Sfvdl * elf_load_psection():
2291.17Scgd *
2301.1Sfvdl * Load a psection at the appropriate address
2311.1Sfvdl */
2321.17Scgdvoid
2331.9ScgdELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
2341.1Sfvdl	struct exec_vmcmd_set *vcset;
2351.1Sfvdl	struct vnode *vp;
2361.9Scgd	Elf_Phdr *ph;
2371.9Scgd	Elf_Addr *addr;
2381.1Sfvdl	u_long *size;
2391.1Sfvdl	int *prot;
2401.1Sfvdl{
2411.8Schristos	u_long uaddr, msize, psize, rm, rf;
2421.1Sfvdl	long diff, offset;
2431.1Sfvdl
2441.1Sfvdl	/*
2451.17Scgd	 * If the user specified an address, then we load there.
2461.17Scgd	 */
2471.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2481.1Sfvdl		if (ph->p_align > 1) {
2491.18Scgd			*addr = ELF_ROUND(*addr, ph->p_align);
2501.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2511.1Sfvdl		} else
2521.1Sfvdl			uaddr = ph->p_vaddr;
2531.1Sfvdl		diff = ph->p_vaddr - uaddr;
2541.1Sfvdl	} else {
2551.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2561.1Sfvdl		if (ph->p_align > 1)
2571.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2581.1Sfvdl		diff = uaddr - *addr;
2591.1Sfvdl	}
2601.1Sfvdl
2611.9Scgd	*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
2621.9Scgd	*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
2631.9Scgd	*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
2641.1Sfvdl
2651.1Sfvdl	offset = ph->p_offset - diff;
2661.1Sfvdl	*size = ph->p_filesz + diff;
2671.1Sfvdl	msize = ph->p_memsz + diff;
2681.8Schristos	psize = round_page(*size);
2691.1Sfvdl
2701.9Scgd	if ((ph->p_flags & Elf_pf_w) != 0) {
2711.8Schristos		/*
2721.8Schristos		 * Because the pagedvn pager can't handle zero fill of the last
2731.8Schristos		 * data page if it's not page aligned we map the last page
2741.8Schristos		 * readvn.
2751.8Schristos		 */
2761.8Schristos		psize = trunc_page(*size);
2771.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2781.8Schristos		    offset, *prot);
2791.8Schristos		if(psize != *size)
2801.8Schristos			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
2811.8Schristos			    *addr + psize, vp, offset + psize, *prot);
2821.9Scgd	} else
2831.8Schristos		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
2841.8Schristos		    offset, *prot);
2851.1Sfvdl
2861.1Sfvdl	/*
2871.17Scgd	 * Check if we need to extend the size of the segment
2881.17Scgd	 */
2891.1Sfvdl	rm = round_page(*addr + msize);
2901.1Sfvdl	rf = round_page(*addr + *size);
2911.1Sfvdl
2921.1Sfvdl	if (rm != rf) {
2931.17Scgd		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
2941.17Scgd		    0, *prot);
2951.1Sfvdl		*size = msize;
2961.1Sfvdl	}
2971.1Sfvdl}
2981.1Sfvdl
2991.1Sfvdl/*
3001.1Sfvdl * elf_read_from():
3011.1Sfvdl *
3021.1Sfvdl *	Read from vnode into buffer at offset.
3031.1Sfvdl */
3041.7Schristosint
3051.9ScgdELFNAME(read_from)(p, vp, off, buf, size)
3061.1Sfvdl	struct vnode *vp;
3071.1Sfvdl	u_long off;
3081.1Sfvdl	struct proc *p;
3091.1Sfvdl	caddr_t buf;
3101.1Sfvdl	int size;
3111.1Sfvdl{
3121.1Sfvdl	int error;
3131.1Sfvdl	int resid;
3141.1Sfvdl
3151.17Scgd	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
3161.17Scgd	    0, p->p_ucred, &resid, p)) != 0)
3171.1Sfvdl		return error;
3181.1Sfvdl	/*
3191.17Scgd	 * See if we got all of it
3201.17Scgd	 */
3211.1Sfvdl	if (resid != 0)
3221.4Sfvdl		return ENOEXEC;
3231.1Sfvdl	return 0;
3241.1Sfvdl}
3251.1Sfvdl
3261.1Sfvdl/*
3271.1Sfvdl * elf_load_file():
3281.1Sfvdl *
3291.1Sfvdl * Load a file (interpreter/library) pointed to by path
3301.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3311.1Sfvdl * so it might be used externally.
3321.1Sfvdl */
3331.17Scgdint
3341.14ScgdELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
3351.1Sfvdl	struct proc *p;
3361.14Scgd	struct exec_package *epp;
3371.1Sfvdl	char *path;
3381.1Sfvdl	struct exec_vmcmd_set *vcset;
3391.1Sfvdl	u_long *entry;
3401.1Sfvdl	struct elf_args	*ap;
3411.9Scgd	Elf_Addr *last;
3421.1Sfvdl{
3431.1Sfvdl	int error, i;
3441.1Sfvdl	struct nameidata nd;
3451.14Scgd	struct vnode *vp;
3461.14Scgd	struct vattr attr;
3471.9Scgd	Elf_Ehdr eh;
3481.9Scgd	Elf_Phdr *ph = NULL;
3491.1Sfvdl	u_long phsize;
3501.1Sfvdl	char *bp = NULL;
3511.9Scgd	Elf_Addr addr = *last;
3521.1Sfvdl
3531.1Sfvdl	bp = path;
3541.1Sfvdl	/*
3551.17Scgd	 * 1. open file
3561.17Scgd	 * 2. read filehdr
3571.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3581.17Scgd	 */
3591.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3601.12Scgd	if ((error = namei(&nd)) != 0)
3611.1Sfvdl		return error;
3621.14Scgd	vp = nd.ni_vp;
3631.14Scgd
3641.26Smycroft	/*
3651.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3661.26Smycroft	 * file, we don't allow it to be used.
3671.26Smycroft	 */
3681.14Scgd	if (vp->v_type != VREG) {
3691.14Scgd		error = EACCES;
3701.14Scgd		goto badunlock;
3711.14Scgd	}
3721.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
3731.26Smycroft		goto badunlock;
3741.14Scgd
3751.17Scgd	/* get attributes */
3761.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
3771.14Scgd		goto badunlock;
3781.14Scgd
3791.14Scgd	/*
3801.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
3811.15Scgd	 * we will be executing code from it, so if the mount point
3821.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
3831.14Scgd	 */
3841.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3851.14Scgd		error = EACCES;
3861.14Scgd		goto badunlock;
3871.14Scgd	}
3881.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
3891.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
3901.14Scgd
3911.12Scgd#ifdef notyet /* XXX cgd 960926 */
3921.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
3931.12Scgd#endif
3941.28Sfvdl	VOP_UNLOCK(vp, 0);
3951.12Scgd
3961.14Scgd	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
3971.17Scgd	    sizeof(eh))) != 0)
3981.1Sfvdl		goto bad;
3991.1Sfvdl
4001.9Scgd	if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
4011.1Sfvdl		goto bad;
4021.1Sfvdl
4031.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
4041.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4051.1Sfvdl
4061.14Scgd	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
4071.9Scgd	    (caddr_t) ph, phsize)) != 0)
4081.1Sfvdl		goto bad;
4091.1Sfvdl
4101.1Sfvdl	/*
4111.17Scgd	 * Load all the necessary sections
4121.17Scgd	 */
4131.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
4141.1Sfvdl		u_long size = 0;
4151.1Sfvdl		int prot = 0;
4161.1Sfvdl
4171.1Sfvdl		switch (ph[i].p_type) {
4181.9Scgd		case Elf_pt_load:
4191.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
4201.17Scgd			    &size, &prot);
4211.4Sfvdl			/* If entry is within this section it must be text */
4221.4Sfvdl			if (eh.e_entry >= ph[i].p_vaddr &&
4231.4Sfvdl			    eh.e_entry < (ph[i].p_vaddr + size)) {
4241.21Sfvdl				/* XXX */
4251.21Sfvdl				*entry = addr + eh.e_entry;
4261.21Sfvdl#ifdef mips
4271.21Sfvdl				*entry -= ph[i].p_vaddr;
4281.21Sfvdl#endif
4291.1Sfvdl				ap->arg_interp = addr;
4301.1Sfvdl			}
4311.1Sfvdl			addr += size;
4321.1Sfvdl			break;
4331.1Sfvdl
4341.9Scgd		case Elf_pt_dynamic:
4351.9Scgd		case Elf_pt_phdr:
4361.9Scgd		case Elf_pt_note:
4371.1Sfvdl			break;
4381.1Sfvdl
4391.1Sfvdl		default:
4401.1Sfvdl			break;
4411.1Sfvdl		}
4421.1Sfvdl	}
4431.1Sfvdl
4441.17Scgd	free((char *)ph, M_TEMP);
4451.12Scgd	*last = addr;
4461.14Scgd	vrele(vp);
4471.12Scgd	return 0;
4481.12Scgd
4491.14Scgdbadunlock:
4501.28Sfvdl	VOP_UNLOCK(vp, 0);
4511.14Scgd
4521.1Sfvdlbad:
4531.1Sfvdl	if (ph != NULL)
4541.17Scgd		free((char *)ph, M_TEMP);
4551.12Scgd#ifdef notyet /* XXX cgd 960926 */
4561.12Scgd	(maybe) VOP_CLOSE it
4571.12Scgd#endif
4581.14Scgd	vrele(vp);
4591.1Sfvdl	return error;
4601.1Sfvdl}
4611.1Sfvdl
4621.1Sfvdl/*
4631.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4641.1Sfvdl *
4651.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4661.1Sfvdl *
4671.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4681.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4691.1Sfvdl * text, data, bss, and stack segments.
4701.1Sfvdl */
4711.1Sfvdlint
4721.9ScgdELFNAME2(exec,makecmds)(p, epp)
4731.1Sfvdl	struct proc *p;
4741.1Sfvdl	struct exec_package *epp;
4751.1Sfvdl{
4761.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
4771.9Scgd	Elf_Phdr *ph, *pp;
4781.9Scgd	Elf_Addr phdr = 0, pos = 0;
4791.4Sfvdl	int error, i, n, nload;
4801.1Sfvdl	char interp[MAXPATHLEN];
4811.9Scgd	u_long phsize;
4821.1Sfvdl
4831.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
4841.1Sfvdl		return ENOEXEC;
4851.1Sfvdl
4861.9Scgd	if (ELFNAME(check_header)(eh, Elf_et_exec))
4871.1Sfvdl		return ENOEXEC;
4881.1Sfvdl
4891.1Sfvdl	/*
4901.17Scgd	 * check if vnode is in open for writing, because we want to
4911.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
4921.17Scgd	 * reasons
4931.17Scgd	 */
4941.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4951.1Sfvdl#ifdef DIAGNOSTIC
4961.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4971.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4981.1Sfvdl#endif
4991.1Sfvdl		return ETXTBSY;
5001.1Sfvdl	}
5011.1Sfvdl	/*
5021.17Scgd	 * Allocate space to hold all the program headers, and read them
5031.17Scgd	 * from the file
5041.17Scgd	 */
5051.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
5061.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
5071.1Sfvdl
5081.9Scgd	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
5091.8Schristos	    (caddr_t) ph, phsize)) != 0)
5101.1Sfvdl		goto bad;
5111.1Sfvdl
5121.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
5131.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
5141.1Sfvdl
5151.1Sfvdl	interp[0] = '\0';
5161.1Sfvdl
5171.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5181.1Sfvdl		pp = &ph[i];
5191.9Scgd		if (pp->p_type == Elf_pt_interp) {
5201.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
5211.1Sfvdl				goto bad;
5221.17Scgd			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
5231.17Scgd			    pp->p_offset, (caddr_t) interp,
5241.17Scgd			    pp->p_filesz)) != 0)
5251.1Sfvdl				goto bad;
5261.1Sfvdl			break;
5271.1Sfvdl		}
5281.1Sfvdl	}
5291.1Sfvdl
5301.9Scgd	/*
5311.8Schristos	 * Setup things for native emulation.
5321.8Schristos	 */
5331.9Scgd	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
5341.9Scgd	pos = ELFDEFNNAME(NO_ADDR);
5351.8Schristos
5361.1Sfvdl	/*
5371.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5381.1Sfvdl	 * See which one will accept this executable. This currently only
5391.27Sscottb	 * applies to Linux, SVR4, and IBCS2 on the i386.
5401.1Sfvdl	 *
5411.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5421.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5431.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
5441.1Sfvdl	 * set the ep_emul field in the exec package structure.
5451.1Sfvdl	 */
5461.17Scgd	n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0];
5471.17Scgd	if (n != 0) {
5481.1Sfvdl		error = ENOEXEC;
5491.1Sfvdl		for (i = 0; i < n && error; i++)
5501.17Scgd			error = ELFNAME(probe_funcs)[i](p, epp, eh,
5511.17Scgd			    interp, &pos);
5521.1Sfvdl
5531.8Schristos#ifdef notyet
5541.8Schristos		/*
5551.8Schristos		 * We should really use a signature in our native binaries
5561.8Schristos		 * and have our own probe function for matching binaries,
5571.8Schristos		 * before trying the emulations. For now, if the emulation
5581.8Schristos		 * probes failed we default to native.
5591.8Schristos		 */
5601.1Sfvdl		if (error)
5611.1Sfvdl			goto bad;
5621.8Schristos#endif
5631.1Sfvdl	}
5641.1Sfvdl
5651.1Sfvdl	/*
5661.17Scgd	 * Load all the necessary sections
5671.17Scgd	 */
5681.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5691.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5701.9Scgd		u_long size = 0;
5711.1Sfvdl		int prot = 0;
5721.1Sfvdl
5731.1Sfvdl		pp = &ph[i];
5741.1Sfvdl
5751.1Sfvdl		switch (ph[i].p_type) {
5761.9Scgd		case Elf_pt_load:
5771.4Sfvdl			/*
5781.4Sfvdl			 * XXX
5791.4Sfvdl			 * Can handle only 2 sections: text and data
5801.4Sfvdl			 */
5811.4Sfvdl			if (nload++ == 2)
5821.4Sfvdl				goto bad;
5831.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
5841.17Scgd			    &ph[i], &addr, &size, &prot);
5851.17Scgd
5861.4Sfvdl			/*
5871.4Sfvdl			 * Decide whether it's text or data by looking
5881.4Sfvdl			 * at the entry point.
5891.4Sfvdl			 */
5901.19Scgd			if (eh->e_entry >= addr &&
5911.19Scgd			    eh->e_entry < (addr + size)) {
5921.4Sfvdl				epp->ep_taddr = addr;
5931.4Sfvdl				epp->ep_tsize = size;
5941.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
5951.19Scgd					epp->ep_daddr = addr;
5961.19Scgd					epp->ep_dsize = size;
5971.19Scgd				}
5981.4Sfvdl			} else {
5991.4Sfvdl				epp->ep_daddr = addr;
6001.4Sfvdl				epp->ep_dsize = size;
6011.4Sfvdl			}
6021.1Sfvdl			break;
6031.1Sfvdl
6041.9Scgd		case Elf_pt_shlib:
6051.27Sscottb#ifndef COMPAT_IBCS2			/* SCO has these sections */
6061.1Sfvdl			error = ENOEXEC;
6071.1Sfvdl			goto bad;
6081.27Sscottb#endif
6091.1Sfvdl
6101.9Scgd		case Elf_pt_interp:
6111.1Sfvdl			/* Already did this one */
6121.9Scgd		case Elf_pt_dynamic:
6131.9Scgd		case Elf_pt_note:
6141.1Sfvdl			break;
6151.1Sfvdl
6161.9Scgd		case Elf_pt_phdr:
6171.4Sfvdl			/* Note address of program headers (in text segment) */
6181.4Sfvdl			phdr = pp->p_vaddr;
6191.7Schristos			break;
6201.4Sfvdl
6211.1Sfvdl		default:
6221.1Sfvdl			/*
6231.9Scgd			 * Not fatal; we don't need to understand everything.
6241.1Sfvdl			 */
6251.1Sfvdl			break;
6261.1Sfvdl		}
6271.1Sfvdl	}
6281.5Sfvdl
6291.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
6301.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
6311.5Sfvdl	/*
6321.5Sfvdl	 * If no position to load the interpreter was set by a probe
6331.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
6341.5Sfvdl	 * would (i.e. something safely out of the way).
6351.5Sfvdl	 */
6361.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
6371.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
6381.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
6391.1Sfvdl
6401.1Sfvdl	/*
6411.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6421.17Scgd	 * it's interpreter
6431.17Scgd	 */
6441.1Sfvdl	if (interp[0]) {
6451.1Sfvdl		struct elf_args *ap;
6461.1Sfvdl
6471.17Scgd		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
6481.17Scgd		    M_TEMP, M_WAITOK);
6491.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6501.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6511.17Scgd			free((char *)ap, M_TEMP);
6521.1Sfvdl			goto bad;
6531.1Sfvdl		}
6541.1Sfvdl		pos += phsize;
6551.4Sfvdl		ap->arg_phaddr = phdr;
6561.1Sfvdl
6571.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6581.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6591.1Sfvdl		ap->arg_entry = eh->e_entry;
6601.1Sfvdl
6611.1Sfvdl		epp->ep_emul_arg = ap;
6621.1Sfvdl	} else
6631.1Sfvdl		epp->ep_entry = eh->e_entry;
6641.1Sfvdl
6651.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6661.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6671.8Schristos	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
6681.8Schristos	    VM_PROT_READ);
6691.8Schristos#endif
6701.17Scgd	free((char *)ph, M_TEMP);
6711.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
6721.9Scgd	return exec_elf_setup_stack(p, epp);
6731.1Sfvdl
6741.1Sfvdlbad:
6751.17Scgd	free((char *)ph, M_TEMP);
6761.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6771.1Sfvdl	return ENOEXEC;
6781.1Sfvdl}
679