exec_elf32.c revision 1.73
11.73Smycroft/*	$NetBSD: exec_elf32.c,v 1.73 2002/09/12 16:57:44 mycroft 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.69Slukem
661.69Slukem#include <sys/cdefs.h>
671.73Smycroft__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.73 2002/09/12 16:57:44 mycroft Exp $");
681.1Sfvdl
691.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
701.9Scgd#ifndef ELFSIZE
711.9Scgd#define	ELFSIZE		32
721.9Scgd#endif
731.30Sthorpej
741.1Sfvdl#include <sys/param.h>
751.1Sfvdl#include <sys/proc.h>
761.1Sfvdl#include <sys/malloc.h>
771.1Sfvdl#include <sys/namei.h>
781.1Sfvdl#include <sys/vnode.h>
791.1Sfvdl#include <sys/exec.h>
801.1Sfvdl#include <sys/exec_elf.h>
811.8Schristos#include <sys/syscall.h>
821.8Schristos#include <sys/signalvar.h>
831.14Scgd#include <sys/mount.h>
841.14Scgd#include <sys/stat.h>
851.1Sfvdl
861.58Sjdolecek#include <machine/cpu.h>
871.58Sjdolecek#include <machine/reg.h>
881.57Sthorpej
891.58Sjdolecekextern const struct emul emul_netbsd;
901.42Schristos
911.54Sthorpejint	ELFNAME(check_header)(Elf_Ehdr *, int);
921.54Sthorpejint	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
931.54Sthorpej	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
941.54Sthorpejvoid	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
951.54Sthorpej	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
961.54Sthorpej
971.59Smrgint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
981.54Sthorpej    Elf_Ehdr *);
991.58Sjdolecekint ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
1001.58Sjdolecek    void *, char *, vaddr_t *);
1011.8Schristos
1021.18Scgd/* round up and down to page boundaries. */
1031.18Scgd#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
1041.18Scgd#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
1051.8Schristos
1061.8Schristos/*
1071.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
1081.1Sfvdl * extra information in case of dynamic binding.
1091.1Sfvdl */
1101.66Schristosint
1111.72SchristosELFNAME(copyargs)(struct proc *p, struct exec_package *pack,
1121.72Schristos    struct ps_strings *arginfo, char **stackp, void *argp)
1131.1Sfvdl{
1141.1Sfvdl	size_t len;
1151.4Sfvdl	AuxInfo ai[ELF_AUX_ENTRIES], *a;
1161.1Sfvdl	struct elf_args *ap;
1171.66Schristos	int error;
1181.1Sfvdl
1191.72Schristos	if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
1201.66Schristos		return error;
1211.1Sfvdl
1221.22Scgd	a = ai;
1231.22Scgd
1241.1Sfvdl	/*
1251.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1261.1Sfvdl	 * linked binaries
1271.1Sfvdl	 */
1281.17Scgd	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
1291.1Sfvdl
1301.46Skleink		a->a_type = AT_PHDR;
1311.46Skleink		a->a_v = ap->arg_phaddr;
1321.1Sfvdl		a++;
1331.1Sfvdl
1341.46Skleink		a->a_type = AT_PHENT;
1351.46Skleink		a->a_v = ap->arg_phentsize;
1361.1Sfvdl		a++;
1371.1Sfvdl
1381.46Skleink		a->a_type = AT_PHNUM;
1391.46Skleink		a->a_v = ap->arg_phnum;
1401.1Sfvdl		a++;
1411.1Sfvdl
1421.46Skleink		a->a_type = AT_PAGESZ;
1431.57Sthorpej		a->a_v = PAGE_SIZE;
1441.1Sfvdl		a++;
1451.1Sfvdl
1461.46Skleink		a->a_type = AT_BASE;
1471.46Skleink		a->a_v = ap->arg_interp;
1481.1Sfvdl		a++;
1491.1Sfvdl
1501.46Skleink		a->a_type = AT_FLAGS;
1511.46Skleink		a->a_v = 0;
1521.1Sfvdl		a++;
1531.1Sfvdl
1541.46Skleink		a->a_type = AT_ENTRY;
1551.46Skleink		a->a_v = ap->arg_entry;
1561.72Schristos		a++;
1571.72Schristos
1581.72Schristos		a->a_type = AT_EUID;
1591.72Schristos		a->a_v = p->p_ucred->cr_uid;
1601.72Schristos		a++;
1611.72Schristos
1621.72Schristos		a->a_type = AT_RUID;
1631.72Schristos		a->a_v = p->p_cred->p_ruid;
1641.72Schristos		a++;
1651.72Schristos
1661.72Schristos		a->a_type = AT_EGID;
1671.72Schristos		a->a_v = p->p_ucred->cr_gid;
1681.72Schristos		a++;
1691.72Schristos
1701.72Schristos		a->a_type = AT_RGID;
1711.72Schristos		a->a_v = p->p_cred->p_rgid;
1721.1Sfvdl		a++;
1731.1Sfvdl
1741.17Scgd		free((char *)ap, M_TEMP);
1751.9Scgd		pack->ep_emul_arg = NULL;
1761.1Sfvdl	}
1771.22Scgd
1781.46Skleink	a->a_type = AT_NULL;
1791.46Skleink	a->a_v = 0;
1801.22Scgd	a++;
1811.22Scgd
1821.34Sperry	len = (a - ai) * sizeof(AuxInfo);
1831.66Schristos	if ((error = copyout(ai, *stackp, len)) != 0)
1841.66Schristos		return error;
1851.67Schristos	*stackp += len;
1861.22Scgd
1871.66Schristos	return 0;
1881.1Sfvdl}
1891.1Sfvdl
1901.1Sfvdl/*
1911.1Sfvdl * elf_check_header():
1921.1Sfvdl *
1931.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
1941.1Sfvdl */
1951.17Scgdint
1961.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type)
1971.1Sfvdl{
1981.3Sthorpej
1991.46Skleink	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
2001.46Skleink	    eh->e_ident[EI_CLASS] != ELFCLASS)
2011.61Smycroft		return (ENOEXEC);
2021.1Sfvdl
2031.1Sfvdl	switch (eh->e_machine) {
2041.9Scgd
2051.10Scgd	ELFDEFNNAME(MACHDEP_ID_CASES)
2061.1Sfvdl
2071.1Sfvdl	default:
2081.61Smycroft		return (ENOEXEC);
2091.1Sfvdl	}
2101.70Sthorpej
2111.70Sthorpej	if (ELF_EHDR_FLAGS_OK(eh) == 0)
2121.70Sthorpej		return (ENOEXEC);
2131.1Sfvdl
2141.1Sfvdl	if (eh->e_type != type)
2151.61Smycroft		return (ENOEXEC);
2161.61Smycroft
2171.63Sjdolecek	if (eh->e_shnum > 512 ||
2181.61Smycroft	    eh->e_phnum > 128)
2191.61Smycroft		return (ENOEXEC);
2201.1Sfvdl
2211.61Smycroft	return (0);
2221.1Sfvdl}
2231.1Sfvdl
2241.1Sfvdl/*
2251.1Sfvdl * elf_load_psection():
2261.17Scgd *
2271.1Sfvdl * Load a psection at the appropriate address
2281.1Sfvdl */
2291.17Scgdvoid
2301.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
2311.54Sthorpej    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
2321.1Sfvdl{
2331.8Schristos	u_long uaddr, msize, psize, rm, rf;
2341.1Sfvdl	long diff, offset;
2351.1Sfvdl
2361.1Sfvdl	/*
2371.17Scgd	 * If the user specified an address, then we load there.
2381.17Scgd	 */
2391.9Scgd	if (*addr != ELFDEFNNAME(NO_ADDR)) {
2401.1Sfvdl		if (ph->p_align > 1) {
2411.53Smatt			*addr = ELF_TRUNC(*addr, ph->p_align);
2421.18Scgd			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
2431.1Sfvdl		} else
2441.1Sfvdl			uaddr = ph->p_vaddr;
2451.1Sfvdl		diff = ph->p_vaddr - uaddr;
2461.1Sfvdl	} else {
2471.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2481.1Sfvdl		if (ph->p_align > 1)
2491.18Scgd			*addr = ELF_TRUNC(uaddr, ph->p_align);
2501.1Sfvdl		diff = uaddr - *addr;
2511.1Sfvdl	}
2521.1Sfvdl
2531.46Skleink	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
2541.46Skleink	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
2551.46Skleink	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
2561.1Sfvdl
2571.1Sfvdl	offset = ph->p_offset - diff;
2581.1Sfvdl	*size = ph->p_filesz + diff;
2591.1Sfvdl	msize = ph->p_memsz + diff;
2601.1Sfvdl
2611.65Schristos	if (ph->p_align >= PAGE_SIZE) {
2621.65Schristos		if ((ph->p_flags & PF_W) != 0) {
2631.65Schristos			/*
2641.65Schristos			 * Because the pagedvn pager can't handle zero fill
2651.65Schristos			 * of the last data page if it's not page aligned we
2661.65Schristos			 * map the last page readvn.
2671.65Schristos			 */
2681.65Schristos			psize = trunc_page(*size);
2691.65Schristos		} else {
2701.65Schristos			psize = round_page(*size);
2711.65Schristos		}
2721.65Schristos	} else {
2731.65Schristos		psize = *size;
2741.53Smatt	}
2751.65Schristos
2761.53Smatt	if (psize > 0) {
2771.65Schristos		NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
2781.65Schristos		    vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
2791.53Smatt		    offset, *prot, flags);
2801.53Smatt	}
2811.53Smatt	if (psize < *size) {
2821.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
2831.53Smatt		    *addr + psize, vp, offset + psize, *prot,
2841.53Smatt		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
2851.53Smatt	}
2861.1Sfvdl
2871.1Sfvdl	/*
2881.17Scgd	 * Check if we need to extend the size of the segment
2891.17Scgd	 */
2901.1Sfvdl	rm = round_page(*addr + msize);
2911.1Sfvdl	rf = round_page(*addr + *size);
2921.1Sfvdl
2931.1Sfvdl	if (rm != rf) {
2941.53Smatt		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
2951.53Smatt		    0, *prot, flags & VMCMD_RELATIVE);
2961.1Sfvdl		*size = msize;
2971.1Sfvdl	}
2981.1Sfvdl}
2991.1Sfvdl
3001.1Sfvdl/*
3011.1Sfvdl * elf_load_file():
3021.1Sfvdl *
3031.1Sfvdl * Load a file (interpreter/library) pointed to by path
3041.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3051.1Sfvdl * so it might be used externally.
3061.1Sfvdl */
3071.17Scgdint
3081.54SthorpejELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
3091.54Sthorpej    struct exec_vmcmd_set *vcset, u_long *entry, struct elf_args *ap,
3101.54Sthorpej    Elf_Addr *last)
3111.1Sfvdl{
3121.1Sfvdl	int error, i;
3131.1Sfvdl	struct nameidata nd;
3141.14Scgd	struct vnode *vp;
3151.14Scgd	struct vattr attr;
3161.9Scgd	Elf_Ehdr eh;
3171.9Scgd	Elf_Phdr *ph = NULL;
3181.53Smatt	Elf_Phdr *base_ph = NULL;
3191.1Sfvdl	u_long phsize;
3201.1Sfvdl	char *bp = NULL;
3211.9Scgd	Elf_Addr addr = *last;
3221.1Sfvdl
3231.1Sfvdl	bp = path;
3241.1Sfvdl	/*
3251.17Scgd	 * 1. open file
3261.17Scgd	 * 2. read filehdr
3271.17Scgd	 * 3. map text, data, and bss out of it using VM_*
3281.17Scgd	 */
3291.12Scgd	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
3301.12Scgd	if ((error = namei(&nd)) != 0)
3311.1Sfvdl		return error;
3321.14Scgd	vp = nd.ni_vp;
3331.14Scgd
3341.26Smycroft	/*
3351.26Smycroft	 * Similarly, if it's not marked as executable, or it's not a regular
3361.26Smycroft	 * file, we don't allow it to be used.
3371.26Smycroft	 */
3381.14Scgd	if (vp->v_type != VREG) {
3391.14Scgd		error = EACCES;
3401.14Scgd		goto badunlock;
3411.14Scgd	}
3421.26Smycroft	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
3431.26Smycroft		goto badunlock;
3441.14Scgd
3451.17Scgd	/* get attributes */
3461.14Scgd	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
3471.14Scgd		goto badunlock;
3481.14Scgd
3491.14Scgd	/*
3501.14Scgd	 * Check mount point.  Though we're not trying to exec this binary,
3511.15Scgd	 * we will be executing code from it, so if the mount point
3521.15Scgd	 * disallows execution or set-id-ness, we punt or kill the set-id.
3531.14Scgd	 */
3541.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
3551.14Scgd		error = EACCES;
3561.14Scgd		goto badunlock;
3571.14Scgd	}
3581.14Scgd	if (vp->v_mount->mnt_flag & MNT_NOSUID)
3591.24Smycroft		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
3601.14Scgd
3611.12Scgd#ifdef notyet /* XXX cgd 960926 */
3621.12Scgd	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
3631.12Scgd#endif
3641.28Sfvdl	VOP_UNLOCK(vp, 0);
3651.12Scgd
3661.64Schristos	if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0)
3671.1Sfvdl		goto bad;
3681.1Sfvdl
3691.46Skleink	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
3701.1Sfvdl		goto bad;
3711.1Sfvdl
3721.9Scgd	phsize = eh.e_phnum * sizeof(Elf_Phdr);
3731.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
3741.1Sfvdl
3751.64Schristos	if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0)
3761.1Sfvdl		goto bad;
3771.1Sfvdl
3781.1Sfvdl	/*
3791.17Scgd	 * Load all the necessary sections
3801.17Scgd	 */
3811.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
3821.1Sfvdl		u_long size = 0;
3831.1Sfvdl		int prot = 0;
3841.53Smatt		int flags;
3851.1Sfvdl
3861.1Sfvdl		switch (ph[i].p_type) {
3871.46Skleink		case PT_LOAD:
3881.53Smatt			if (base_ph == NULL) {
3891.53Smatt				addr = *last;
3901.53Smatt				flags = VMCMD_BASE;
3911.53Smatt			} else {
3921.53Smatt				addr = ph[i].p_vaddr - base_ph->p_vaddr;
3931.53Smatt				flags = VMCMD_RELATIVE;
3941.53Smatt			}
3951.14Scgd			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
3961.53Smatt			    &size, &prot, flags);
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.73Smycroft				*entry = addr + eh.e_entry - ph[i].p_vaddr;
4011.1Sfvdl				ap->arg_interp = addr;
4021.1Sfvdl			}
4031.53Smatt			if (base_ph == NULL)
4041.53Smatt				base_ph = &ph[i];
4051.1Sfvdl			addr += size;
4061.1Sfvdl			break;
4071.1Sfvdl
4081.46Skleink		case PT_DYNAMIC:
4091.46Skleink		case PT_PHDR:
4101.46Skleink		case PT_NOTE:
4111.1Sfvdl			break;
4121.1Sfvdl
4131.1Sfvdl		default:
4141.1Sfvdl			break;
4151.1Sfvdl		}
4161.1Sfvdl	}
4171.1Sfvdl
4181.17Scgd	free((char *)ph, M_TEMP);
4191.12Scgd	*last = addr;
4201.14Scgd	vrele(vp);
4211.12Scgd	return 0;
4221.12Scgd
4231.14Scgdbadunlock:
4241.28Sfvdl	VOP_UNLOCK(vp, 0);
4251.14Scgd
4261.1Sfvdlbad:
4271.1Sfvdl	if (ph != NULL)
4281.17Scgd		free((char *)ph, M_TEMP);
4291.12Scgd#ifdef notyet /* XXX cgd 960926 */
4301.12Scgd	(maybe) VOP_CLOSE it
4311.12Scgd#endif
4321.14Scgd	vrele(vp);
4331.1Sfvdl	return error;
4341.1Sfvdl}
4351.1Sfvdl
4361.1Sfvdl/*
4371.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4381.1Sfvdl *
4391.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4401.1Sfvdl *
4411.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4421.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4431.1Sfvdl * text, data, bss, and stack segments.
4441.1Sfvdl */
4451.1Sfvdlint
4461.54SthorpejELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
4471.1Sfvdl{
4481.9Scgd	Elf_Ehdr *eh = epp->ep_hdr;
4491.9Scgd	Elf_Phdr *ph, *pp;
4501.9Scgd	Elf_Addr phdr = 0, pos = 0;
4511.58Sjdolecek	int error, i, nload;
4521.58Sjdolecek	char *interp = NULL;
4531.9Scgd	u_long phsize;
4541.1Sfvdl
4551.9Scgd	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
4561.1Sfvdl		return ENOEXEC;
4571.1Sfvdl
4581.45Sfvdl	/*
4591.45Sfvdl	 * XXX allow for executing shared objects. It seems silly
4601.45Sfvdl	 * but other ELF-based systems allow it as well.
4611.45Sfvdl	 */
4621.46Skleink	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
4631.46Skleink	    ELFNAME(check_header)(eh, ET_DYN) != 0)
4641.1Sfvdl		return ENOEXEC;
4651.1Sfvdl
4661.1Sfvdl	/*
4671.17Scgd	 * check if vnode is in open for writing, because we want to
4681.17Scgd	 * demand-page out of it.  if it is, don't do it, for various
4691.17Scgd	 * reasons
4701.17Scgd	 */
4711.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4721.1Sfvdl#ifdef DIAGNOSTIC
4731.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4741.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4751.1Sfvdl#endif
4761.1Sfvdl		return ETXTBSY;
4771.1Sfvdl	}
4781.1Sfvdl	/*
4791.17Scgd	 * Allocate space to hold all the program headers, and read them
4801.17Scgd	 * from the file
4811.17Scgd	 */
4821.9Scgd	phsize = eh->e_phnum * sizeof(Elf_Phdr);
4831.17Scgd	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
4841.1Sfvdl
4851.64Schristos	if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
4861.64Schristos	    0)
4871.1Sfvdl		goto bad;
4881.1Sfvdl
4891.19Scgd	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
4901.19Scgd	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
4911.1Sfvdl
4921.58Sjdolecek	MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
4931.1Sfvdl	interp[0] = '\0';
4941.1Sfvdl
4951.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
4961.1Sfvdl		pp = &ph[i];
4971.46Skleink		if (pp->p_type == PT_INTERP) {
4981.58Sjdolecek			if (pp->p_filesz >= MAXPATHLEN)
4991.1Sfvdl				goto bad;
5001.64Schristos			if ((error = exec_read_from(p, epp->ep_vp,
5011.64Schristos			    pp->p_offset, interp, pp->p_filesz)) != 0)
5021.1Sfvdl				goto bad;
5031.1Sfvdl			break;
5041.1Sfvdl		}
5051.1Sfvdl	}
5061.1Sfvdl
5071.9Scgd	/*
5081.1Sfvdl	 * On the same architecture, we may be emulating different systems.
5091.1Sfvdl	 * See which one will accept this executable. This currently only
5101.38Serh	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
5111.38Serh	 * and the Alpha.
5121.1Sfvdl	 *
5131.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
5141.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
5151.58Sjdolecek	 * interp[] with a changed path (/emul/xxx/<path>).
5161.1Sfvdl	 */
5171.58Sjdolecek	if (!epp->ep_esch->u.elf_probe_func) {
5181.58Sjdolecek		pos = ELFDEFNNAME(NO_ADDR);
5191.58Sjdolecek	} else {
5201.62Seeh		vaddr_t startp = 0;
5211.62Seeh
5221.58Sjdolecek		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
5231.62Seeh							  &startp);
5241.62Seeh		pos = (Elf_Addr)startp;
5251.1Sfvdl		if (error)
5261.1Sfvdl			goto bad;
5271.1Sfvdl	}
5281.1Sfvdl
5291.1Sfvdl	/*
5301.17Scgd	 * Load all the necessary sections
5311.17Scgd	 */
5321.4Sfvdl	for (i = nload = 0; i < eh->e_phnum; i++) {
5331.9Scgd		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
5341.9Scgd		u_long size = 0;
5351.1Sfvdl		int prot = 0;
5361.1Sfvdl
5371.1Sfvdl		pp = &ph[i];
5381.1Sfvdl
5391.1Sfvdl		switch (ph[i].p_type) {
5401.46Skleink		case PT_LOAD:
5411.4Sfvdl			/*
5421.4Sfvdl			 * XXX
5431.4Sfvdl			 * Can handle only 2 sections: text and data
5441.4Sfvdl			 */
5451.4Sfvdl			if (nload++ == 2)
5461.4Sfvdl				goto bad;
5471.9Scgd			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
5481.53Smatt			    &ph[i], &addr, &size, &prot, 0);
5491.17Scgd
5501.4Sfvdl			/*
5511.4Sfvdl			 * Decide whether it's text or data by looking
5521.4Sfvdl			 * at the entry point.
5531.4Sfvdl			 */
5541.19Scgd			if (eh->e_entry >= addr &&
5551.19Scgd			    eh->e_entry < (addr + size)) {
5561.4Sfvdl				epp->ep_taddr = addr;
5571.4Sfvdl				epp->ep_tsize = size;
5581.19Scgd				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
5591.19Scgd					epp->ep_daddr = addr;
5601.19Scgd					epp->ep_dsize = size;
5611.19Scgd				}
5621.4Sfvdl			} else {
5631.4Sfvdl				epp->ep_daddr = addr;
5641.4Sfvdl				epp->ep_dsize = size;
5651.4Sfvdl			}
5661.1Sfvdl			break;
5671.1Sfvdl
5681.46Skleink		case PT_SHLIB:
5691.60Smycroft			/* SCO has these sections. */
5701.46Skleink		case PT_INTERP:
5711.60Smycroft			/* Already did this one. */
5721.46Skleink		case PT_DYNAMIC:
5731.46Skleink		case PT_NOTE:
5741.1Sfvdl			break;
5751.1Sfvdl
5761.46Skleink		case PT_PHDR:
5771.4Sfvdl			/* Note address of program headers (in text segment) */
5781.4Sfvdl			phdr = pp->p_vaddr;
5791.7Schristos			break;
5801.4Sfvdl
5811.1Sfvdl		default:
5821.1Sfvdl			/*
5831.9Scgd			 * Not fatal; we don't need to understand everything.
5841.1Sfvdl			 */
5851.1Sfvdl			break;
5861.1Sfvdl		}
5871.1Sfvdl	}
5881.5Sfvdl
5891.20Sjonathan	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
5901.20Sjonathan#ifndef ELF_INTERP_NON_RELOCATABLE
5911.5Sfvdl	/*
5921.5Sfvdl	 * If no position to load the interpreter was set by a probe
5931.5Sfvdl	 * function, pick the same address that a non-fixed mmap(0, ..)
5941.5Sfvdl	 * would (i.e. something safely out of the way).
5951.5Sfvdl	 */
5961.21Sfvdl	if (pos == ELFDEFNNAME(NO_ADDR))
5971.5Sfvdl		pos = round_page(epp->ep_daddr + MAXDSIZ);
5981.20Sjonathan#endif	/* !ELF_INTERP_NON_RELOCATABLE */
5991.1Sfvdl
6001.1Sfvdl	/*
6011.17Scgd	 * Check if we found a dynamically linked binary and arrange to load
6021.17Scgd	 * it's interpreter
6031.17Scgd	 */
6041.1Sfvdl	if (interp[0]) {
6051.1Sfvdl		struct elf_args *ap;
6061.1Sfvdl
6071.58Sjdolecek		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
6081.17Scgd		    M_TEMP, M_WAITOK);
6091.14Scgd		if ((error = ELFNAME(load_file)(p, epp, interp,
6101.14Scgd		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
6111.58Sjdolecek			FREE((char *)ap, M_TEMP);
6121.1Sfvdl			goto bad;
6131.1Sfvdl		}
6141.1Sfvdl		pos += phsize;
6151.4Sfvdl		ap->arg_phaddr = phdr;
6161.1Sfvdl
6171.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
6181.1Sfvdl		ap->arg_phnum = eh->e_phnum;
6191.1Sfvdl		ap->arg_entry = eh->e_entry;
6201.1Sfvdl
6211.1Sfvdl		epp->ep_emul_arg = ap;
6221.1Sfvdl	} else
6231.1Sfvdl		epp->ep_entry = eh->e_entry;
6241.1Sfvdl
6251.8Schristos#ifdef ELF_MAP_PAGE_ZERO
6261.8Schristos	/* Dell SVR4 maps page zero, yeuch! */
6271.57Sthorpej	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
6281.57Sthorpej	    epp->ep_vp, 0, VM_PROT_READ);
6291.8Schristos#endif
6301.58Sjdolecek	FREE(interp, M_TEMP);
6311.17Scgd	free((char *)ph, M_TEMP);
6321.68Sthorpej	epp->ep_vp->v_flag |= VTEXT;
6331.9Scgd	return exec_elf_setup_stack(p, epp);
6341.1Sfvdl
6351.1Sfvdlbad:
6361.58Sjdolecek	if (interp)
6371.58Sjdolecek		FREE(interp, M_TEMP);
6381.17Scgd	free((char *)ph, M_TEMP);
6391.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
6401.1Sfvdl	return ENOEXEC;
6411.40Schristos}
6421.40Schristos
6431.59Smrgint
6441.54SthorpejELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
6451.54Sthorpej    Elf_Ehdr *eh)
6461.40Schristos{
6471.61Smycroft	size_t i;
6481.61Smycroft	Elf_Phdr *ph;
6491.40Schristos	size_t phsize;
6501.40Schristos	int error;
6511.40Schristos
6521.40Schristos	phsize = eh->e_phnum * sizeof(Elf_Phdr);
6531.61Smycroft	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
6541.64Schristos	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
6551.61Smycroft	if (error)
6561.61Smycroft		goto out;
6571.40Schristos
6581.61Smycroft	for (i = 0; i < eh->e_phnum; i++) {
6591.61Smycroft		Elf_Phdr *ephp = &ph[i];
6601.61Smycroft		Elf_Nhdr *np;
6611.40Schristos
6621.61Smycroft		if (ephp->p_type != PT_NOTE ||
6631.61Smycroft		    ephp->p_filesz > 1024 ||
6641.61Smycroft		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
6651.40Schristos			continue;
6661.40Schristos
6671.61Smycroft		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
6681.64Schristos		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
6691.64Schristos		    ephp->p_filesz);
6701.61Smycroft		if (error)
6711.61Smycroft			goto next;
6721.40Schristos
6731.61Smycroft		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
6741.61Smycroft		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
6751.61Smycroft		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
6761.61Smycroft		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
6771.40Schristos		    ELF_NOTE_NETBSD_NAMESZ))
6781.61Smycroft			goto next;
6791.40Schristos
6801.40Schristos		error = 0;
6811.61Smycroft		free(np, M_TEMP);
6821.61Smycroft		goto out;
6831.61Smycroft
6841.61Smycroft	next:
6851.61Smycroft		free(np, M_TEMP);
6861.61Smycroft		continue;
6871.40Schristos	}
6881.40Schristos
6891.40Schristos	error = ENOEXEC;
6901.61Smycroftout:
6911.61Smycroft	free(ph, M_TEMP);
6921.61Smycroft	return (error);
6931.40Schristos}
6941.40Schristos
6951.58Sjdolecekint
6961.54SthorpejELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
6971.58Sjdolecek    void *eh, char *itp, vaddr_t *pos)
6981.40Schristos{
6991.40Schristos	int error;
7001.40Schristos
7011.40Schristos	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
7021.40Schristos		return error;
7031.41Schristos	*pos = ELFDEFNNAME(NO_ADDR);
7041.40Schristos	return 0;
7051.1Sfvdl}
706