exec_elf32.c revision 1.1
11.1Sfvdl/*	$NetBSD: exec_elf32.c,v 1.1 1995/06/22 21:29:53 fvdl Exp $	*/
21.1Sfvdl
31.1Sfvdl/*
41.1Sfvdl * Copyright (c) 1994 Christos Zoulas
51.1Sfvdl * All rights reserved.
61.1Sfvdl *
71.1Sfvdl * Redistribution and use in source and binary forms, with or without
81.1Sfvdl * modification, are permitted provided that the following conditions
91.1Sfvdl * are met:
101.1Sfvdl * 1. Redistributions of source code must retain the above copyright
111.1Sfvdl *    notice, this list of conditions and the following disclaimer.
121.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
131.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
141.1Sfvdl *    documentation and/or other materials provided with the distribution.
151.1Sfvdl * 3. The name of the author may not be used to endorse or promote products
161.1Sfvdl *    derived from this software without specific prior written permission
171.1Sfvdl *
181.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281.1Sfvdl *
291.1Sfvdl */
301.1Sfvdl
311.1Sfvdl#include <sys/param.h>
321.1Sfvdl#include <sys/systm.h>
331.1Sfvdl#include <sys/kernel.h>
341.1Sfvdl#include <sys/proc.h>
351.1Sfvdl#include <sys/malloc.h>
361.1Sfvdl#include <sys/namei.h>
371.1Sfvdl#include <sys/vnode.h>
381.1Sfvdl#include <sys/exec.h>
391.1Sfvdl#include <sys/exec_elf.h>
401.1Sfvdl
411.1Sfvdl#include <sys/mman.h>
421.1Sfvdl#include <vm/vm.h>
431.1Sfvdl#include <vm/vm_param.h>
441.1Sfvdl#include <vm/vm_map.h>
451.1Sfvdl
461.1Sfvdl#include <machine/cpu.h>
471.1Sfvdl#include <machine/reg.h>
481.1Sfvdl#include <machine/exec.h>
491.1Sfvdl
501.1Sfvdl#ifdef COMPAT_LINUX
511.1Sfvdl#include <compat/linux/linux_exec.h>
521.1Sfvdl#endif
531.1Sfvdl
541.1Sfvdl#ifdef COMPAT_SVR4
551.1Sfvdl#include <compat/svr4/svr4_exec.h>
561.1Sfvdl#endif
571.1Sfvdl
581.1Sfvdlint (*elf_probe_funcs[])() = {
591.1Sfvdl#ifdef COMPAT_SVR4
601.1Sfvdl	svr4_elf_probe,
611.1Sfvdl#endif
621.1Sfvdl#ifdef COMPAT_LINUX
631.1Sfvdl	linux_elf_probe
641.1Sfvdl#endif
651.1Sfvdl};
661.1Sfvdl
671.1Sfvdlstatic int elf_set_segment __P((struct exec_package *, u_long, u_long,
681.1Sfvdl	int));
691.1Sfvdlstatic int elf_read_from __P((struct proc *, struct vnode *, u_long,
701.1Sfvdl	caddr_t, int));
711.1Sfvdlstatic void elf_load_psection __P((struct exec_vmcmd_set *,
721.1Sfvdl	struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
731.1Sfvdl
741.1Sfvdl#define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
751.1Sfvdl#define ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
761.1Sfvdl
771.1Sfvdl/*
781.1Sfvdl * Copy arguments onto the stack in the normal way, but add some
791.1Sfvdl * extra information in case of dynamic binding.
801.1Sfvdl */
811.1Sfvdlvoid *
821.1Sfvdlelf_copyargs(pack, arginfo, stack, argp)
831.1Sfvdl	struct exec_package *pack;
841.1Sfvdl	struct ps_strings *arginfo;
851.1Sfvdl	void *stack;
861.1Sfvdl	void *argp;
871.1Sfvdl{
881.1Sfvdl	char **cpp = stack;
891.1Sfvdl	char *dp, *sp;
901.1Sfvdl	size_t len;
911.1Sfvdl	void *nullp = NULL;
921.1Sfvdl	int argc = arginfo->ps_nargvstr;
931.1Sfvdl	int envc = arginfo->ps_nenvstr;
941.1Sfvdl	AuxInfo *a;
951.1Sfvdl	struct elf_args *ap;
961.1Sfvdl
971.1Sfvdl	if (copyout(&argc, cpp++, sizeof(argc)))
981.1Sfvdl		return NULL;
991.1Sfvdl
1001.1Sfvdl	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
1011.1Sfvdl	sp = argp;
1021.1Sfvdl
1031.1Sfvdl	/* XXX don't copy them out, remap them! */
1041.1Sfvdl	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
1051.1Sfvdl
1061.1Sfvdl	for (; --argc >= 0; sp += len, dp += len)
1071.1Sfvdl		if (copyout(&dp, cpp++, sizeof(dp)) ||
1081.1Sfvdl		    copyoutstr(sp, dp, ARG_MAX, &len))
1091.1Sfvdl			return NULL;
1101.1Sfvdl
1111.1Sfvdl	if (copyout(&nullp, cpp++, sizeof(nullp)))
1121.1Sfvdl		return NULL;
1131.1Sfvdl
1141.1Sfvdl	arginfo->ps_envstr = cpp; /* remember location of envp for later */
1151.1Sfvdl
1161.1Sfvdl	for (; --envc >= 0; sp += len, dp += len)
1171.1Sfvdl		if (copyout(&dp, cpp++, sizeof(dp)) ||
1181.1Sfvdl		    copyoutstr(sp, dp, ARG_MAX, &len))
1191.1Sfvdl			return NULL;
1201.1Sfvdl
1211.1Sfvdl	if (copyout(&nullp, cpp++, sizeof(nullp)))
1221.1Sfvdl		return NULL;
1231.1Sfvdl
1241.1Sfvdl	/*
1251.1Sfvdl	 * Push extra arguments on the stack needed by dynamically
1261.1Sfvdl	 * linked binaries
1271.1Sfvdl	 */
1281.1Sfvdl	a = (AuxInfo *) cpp;
1291.1Sfvdl	if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
1301.1Sfvdl
1311.1Sfvdl		a->au_id = AUX_phdr;
1321.1Sfvdl		a->au_v = ap->arg_phaddr;
1331.1Sfvdl		a++;
1341.1Sfvdl
1351.1Sfvdl		a->au_id = AUX_phent;
1361.1Sfvdl		a->au_v = ap->arg_phentsize;
1371.1Sfvdl		a++;
1381.1Sfvdl
1391.1Sfvdl		a->au_id = AUX_phnum;
1401.1Sfvdl		a->au_v = ap->arg_phnum;
1411.1Sfvdl		a++;
1421.1Sfvdl
1431.1Sfvdl		a->au_id = AUX_pagesz;
1441.1Sfvdl		a->au_v = NBPG;
1451.1Sfvdl		a++;
1461.1Sfvdl
1471.1Sfvdl		a->au_id = AUX_base;
1481.1Sfvdl		a->au_v = ap->arg_interp;
1491.1Sfvdl		a++;
1501.1Sfvdl
1511.1Sfvdl		a->au_id = AUX_flags;
1521.1Sfvdl		a->au_v = 0;
1531.1Sfvdl		a++;
1541.1Sfvdl
1551.1Sfvdl		a->au_id = AUX_entry;
1561.1Sfvdl		a->au_v = ap->arg_entry;
1571.1Sfvdl		a++;
1581.1Sfvdl
1591.1Sfvdl		a->au_id = AUX_null;
1601.1Sfvdl		a->au_v = 0;
1611.1Sfvdl		a++;
1621.1Sfvdl
1631.1Sfvdl		free((char *) ap, M_TEMP);
1641.1Sfvdl	}
1651.1Sfvdl	return a;
1661.1Sfvdl}
1671.1Sfvdl
1681.1Sfvdl/*
1691.1Sfvdl * elf_check_header():
1701.1Sfvdl *
1711.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error
1721.1Sfvdl *
1731.1Sfvdl * XXX machine type needs to be moved to <machine/param.h> so
1741.1Sfvdl * just one comparison can be done. Unfortunately, there is both
1751.1Sfvdl * em_486 and em_386, so this would not work on the i386.
1761.1Sfvdl */
1771.1Sfvdlint
1781.1Sfvdlelf_check_header(eh, type)
1791.1Sfvdl	Elf32_Ehdr *eh;
1801.1Sfvdl	int type;
1811.1Sfvdl{
1821.1Sfvdl#ifdef sparc
1831.1Sfvdl  /* #$%@#$%@#$%! */
1841.1Sfvdl# define memcmp bcmp
1851.1Sfvdl#endif
1861.1Sfvdl	if (memcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0)
1871.1Sfvdl		return ENOEXEC;
1881.1Sfvdl
1891.1Sfvdl	switch (eh->e_machine) {
1901.1Sfvdl	/* XXX */
1911.1Sfvdl#ifdef i386
1921.1Sfvdl	case Elf32_em_386:
1931.1Sfvdl	case Elf32_em_486:
1941.1Sfvdl#endif
1951.1Sfvdl#ifdef sparc
1961.1Sfvdl	case Elf32_em_sparc:
1971.1Sfvdl#endif
1981.1Sfvdl		break;
1991.1Sfvdl
2001.1Sfvdl	default:
2011.1Sfvdl		return ENOEXEC;
2021.1Sfvdl	}
2031.1Sfvdl
2041.1Sfvdl	if (eh->e_type != type)
2051.1Sfvdl		return ENOEXEC;
2061.1Sfvdl
2071.1Sfvdl	return 0;
2081.1Sfvdl}
2091.1Sfvdl
2101.1Sfvdl/*
2111.1Sfvdl * elf_load_psection():
2121.1Sfvdl *
2131.1Sfvdl * Load a psection at the appropriate address
2141.1Sfvdl */
2151.1Sfvdlstatic void
2161.1Sfvdlelf_load_psection(vcset, vp, ph, addr, size, prot)
2171.1Sfvdl	struct exec_vmcmd_set *vcset;
2181.1Sfvdl	struct vnode *vp;
2191.1Sfvdl	Elf32_Phdr *ph;
2201.1Sfvdl	u_long *addr;
2211.1Sfvdl	u_long *size;
2221.1Sfvdl	int *prot;
2231.1Sfvdl{
2241.1Sfvdl	u_long uaddr, msize, rm, rf;
2251.1Sfvdl	long diff, offset;
2261.1Sfvdl
2271.1Sfvdl	/*
2281.1Sfvdl         * If the user specified an address, then we load there.
2291.1Sfvdl         */
2301.1Sfvdl	if (*addr != ELF32_NO_ADDR) {
2311.1Sfvdl		if (ph->p_align > 1) {
2321.1Sfvdl			*addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
2331.1Sfvdl			uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
2341.1Sfvdl		} else
2351.1Sfvdl			uaddr = ph->p_vaddr;
2361.1Sfvdl		diff = ph->p_vaddr - uaddr;
2371.1Sfvdl	} else {
2381.1Sfvdl		*addr = uaddr = ph->p_vaddr;
2391.1Sfvdl		if (ph->p_align > 1)
2401.1Sfvdl			*addr = ELF_ALIGN(uaddr, ph->p_align);
2411.1Sfvdl		diff = uaddr - *addr;
2421.1Sfvdl	}
2431.1Sfvdl
2441.1Sfvdl	*prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
2451.1Sfvdl	*prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
2461.1Sfvdl	*prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
2471.1Sfvdl
2481.1Sfvdl	offset = ph->p_offset - diff;
2491.1Sfvdl	*size = ph->p_filesz + diff;
2501.1Sfvdl	msize = ph->p_memsz + diff;
2511.1Sfvdl
2521.1Sfvdl	NEW_VMCMD(vcset, vmcmd_map_readvn, *size, *addr, vp, offset, *prot);
2531.1Sfvdl
2541.1Sfvdl	/*
2551.1Sfvdl         * Check if we need to extend the size of the segment
2561.1Sfvdl         */
2571.1Sfvdl	rm = round_page(*addr + msize);
2581.1Sfvdl	rf = round_page(*addr + *size);
2591.1Sfvdl
2601.1Sfvdl	if (rm != rf) {
2611.1Sfvdl		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
2621.1Sfvdl		*size = msize;
2631.1Sfvdl	}
2641.1Sfvdl}
2651.1Sfvdl
2661.1Sfvdl/*
2671.1Sfvdl * elf_set_segment():
2681.1Sfvdl *
2691.1Sfvdl * Decide if the segment is text or data, depending on the protection
2701.1Sfvdl * and set it appropriately
2711.1Sfvdl */
2721.1Sfvdlstatic int
2731.1Sfvdlelf_set_segment(epp, vaddr, size, prot)
2741.1Sfvdl	struct exec_package *epp;
2751.1Sfvdl	u_long vaddr;
2761.1Sfvdl	u_long size;
2771.1Sfvdl	int prot;
2781.1Sfvdl{
2791.1Sfvdl	/*
2801.1Sfvdl         * Kludge: Unfortunately the current implementation of
2811.1Sfvdl         * exec package assumes a single text and data segment.
2821.1Sfvdl         * In Elf we can have more, but here we limit ourselves
2831.1Sfvdl         * to two and hope :-(
2841.1Sfvdl         * We also assume that the text is r-x, and data is rwx or rw-.
2851.1Sfvdl         */
2861.1Sfvdl	switch (prot) {
2871.1Sfvdl	case (VM_PROT_READ | VM_PROT_EXECUTE):
2881.1Sfvdl		if (epp->ep_tsize != ELF32_NO_ADDR)
2891.1Sfvdl			return ENOEXEC;
2901.1Sfvdl		epp->ep_taddr = vaddr;
2911.1Sfvdl		epp->ep_tsize = size;
2921.1Sfvdl		break;
2931.1Sfvdl
2941.1Sfvdl	case (VM_PROT_READ | VM_PROT_WRITE):
2951.1Sfvdl	case (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE):
2961.1Sfvdl		if (epp->ep_dsize != ELF32_NO_ADDR)
2971.1Sfvdl			return ENOEXEC;
2981.1Sfvdl		epp->ep_daddr = vaddr;
2991.1Sfvdl		epp->ep_dsize = size;
3001.1Sfvdl		break;
3011.1Sfvdl
3021.1Sfvdl	default:
3031.1Sfvdl		return ENOEXEC;
3041.1Sfvdl	}
3051.1Sfvdl	return 0;
3061.1Sfvdl}
3071.1Sfvdl
3081.1Sfvdl/*
3091.1Sfvdl * elf_read_from():
3101.1Sfvdl *
3111.1Sfvdl *	Read from vnode into buffer at offset.
3121.1Sfvdl */
3131.1Sfvdlstatic int
3141.1Sfvdlelf_read_from(p, vp, off, buf, size)
3151.1Sfvdl	struct vnode *vp;
3161.1Sfvdl	u_long off;
3171.1Sfvdl	struct proc *p;
3181.1Sfvdl	caddr_t buf;
3191.1Sfvdl	int size;
3201.1Sfvdl{
3211.1Sfvdl	int error;
3221.1Sfvdl	int resid;
3231.1Sfvdl
3241.1Sfvdl	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
3251.1Sfvdl			     off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
3261.1Sfvdl			     &resid, p)) != 0)
3271.1Sfvdl		return error;
3281.1Sfvdl	/*
3291.1Sfvdl         * See if we got all of it
3301.1Sfvdl         */
3311.1Sfvdl	if (resid != 0)
3321.1Sfvdl		return error;
3331.1Sfvdl	return 0;
3341.1Sfvdl}
3351.1Sfvdl
3361.1Sfvdl/*
3371.1Sfvdl * elf_load_file():
3381.1Sfvdl *
3391.1Sfvdl * Load a file (interpreter/library) pointed to by path
3401.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic
3411.1Sfvdl * so it might be used externally.
3421.1Sfvdl */
3431.1Sfvdlint
3441.1Sfvdlelf_load_file(p, path, vcset, entry, ap, last)
3451.1Sfvdl	struct proc *p;
3461.1Sfvdl	char *path;
3471.1Sfvdl	struct exec_vmcmd_set *vcset;
3481.1Sfvdl	u_long *entry;
3491.1Sfvdl	struct elf_args	*ap;
3501.1Sfvdl	u_long *last;
3511.1Sfvdl{
3521.1Sfvdl	int error, i;
3531.1Sfvdl	struct nameidata nd;
3541.1Sfvdl	Elf32_Ehdr eh;
3551.1Sfvdl	Elf32_Phdr *ph = NULL;
3561.1Sfvdl	u_long phsize;
3571.1Sfvdl	char *bp = NULL;
3581.1Sfvdl	u_long addr = *last;
3591.1Sfvdl
3601.1Sfvdl	bp = path;
3611.1Sfvdl	/*
3621.1Sfvdl         * 1. open file
3631.1Sfvdl         * 2. read filehdr
3641.1Sfvdl         * 3. map text, data, and bss out of it using VM_*
3651.1Sfvdl         */
3661.1Sfvdl	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
3671.1Sfvdl	if ((error = namei(&nd)) != 0) {
3681.1Sfvdl		return error;
3691.1Sfvdl	}
3701.1Sfvdl	if ((error = elf_read_from(p, nd.ni_vp, 0, (caddr_t) &eh,
3711.1Sfvdl				    sizeof(eh))) != 0)
3721.1Sfvdl		goto bad;
3731.1Sfvdl
3741.1Sfvdl	if ((error = elf_check_header(&eh, Elf32_et_dyn)) != 0)
3751.1Sfvdl		goto bad;
3761.1Sfvdl
3771.1Sfvdl	phsize = eh.e_phnum * sizeof(Elf32_Phdr);
3781.1Sfvdl	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
3791.1Sfvdl
3801.1Sfvdl	if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff,
3811.1Sfvdl				    (caddr_t) ph, phsize)) != 0)
3821.1Sfvdl		goto bad;
3831.1Sfvdl
3841.1Sfvdl	/*
3851.1Sfvdl         * Load all the necessary sections
3861.1Sfvdl         */
3871.1Sfvdl	for (i = 0; i < eh.e_phnum; i++) {
3881.1Sfvdl		u_long size = 0;
3891.1Sfvdl		int prot = 0;
3901.1Sfvdl
3911.1Sfvdl		switch (ph[i].p_type) {
3921.1Sfvdl		case Elf32_pt_load:
3931.1Sfvdl			elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
3941.1Sfvdl						&size, &prot);
3951.1Sfvdl			/* Assume that the text segment is r-x only */
3961.1Sfvdl			if ((prot & PROT_WRITE) == 0) {
3971.1Sfvdl				*entry = addr + eh.e_entry;
3981.1Sfvdl				ap->arg_interp = addr;
3991.1Sfvdl			}
4001.1Sfvdl			addr += size;
4011.1Sfvdl			break;
4021.1Sfvdl
4031.1Sfvdl		case Elf32_pt_dynamic:
4041.1Sfvdl		case Elf32_pt_phdr:
4051.1Sfvdl		case Elf32_pt_note:
4061.1Sfvdl			break;
4071.1Sfvdl
4081.1Sfvdl		default:
4091.1Sfvdl			break;
4101.1Sfvdl		}
4111.1Sfvdl	}
4121.1Sfvdl
4131.1Sfvdlbad:
4141.1Sfvdl	if (ph != NULL)
4151.1Sfvdl		free((char *) ph, M_TEMP);
4161.1Sfvdl
4171.1Sfvdl	*last = addr;
4181.1Sfvdl	vrele(nd.ni_vp);
4191.1Sfvdl	return error;
4201.1Sfvdl}
4211.1Sfvdl
4221.1Sfvdl/*
4231.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
4241.1Sfvdl *
4251.1Sfvdl * First, set of the various offsets/lengths in the exec package.
4261.1Sfvdl *
4271.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error
4281.1Sfvdl * out if this is not possible.  Finally, set up vmcmds for the
4291.1Sfvdl * text, data, bss, and stack segments.
4301.1Sfvdl *
4311.1Sfvdl * XXX no demand paging (yet?)
4321.1Sfvdl */
4331.1Sfvdlint
4341.1Sfvdlexec_elf_makecmds(p, epp)
4351.1Sfvdl	struct proc *p;
4361.1Sfvdl	struct exec_package *epp;
4371.1Sfvdl{
4381.1Sfvdl	Elf32_Ehdr *eh = epp->ep_hdr;
4391.1Sfvdl	Elf32_Phdr *ph, *pp;
4401.1Sfvdl	int error, i, n;
4411.1Sfvdl	char interp[MAXPATHLEN];
4421.1Sfvdl	u_long pos = 0, phsize;
4431.1Sfvdl
4441.1Sfvdl	if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
4451.1Sfvdl		return ENOEXEC;
4461.1Sfvdl
4471.1Sfvdl	if (elf_check_header(eh, Elf32_et_exec))
4481.1Sfvdl		return ENOEXEC;
4491.1Sfvdl
4501.1Sfvdl	/*
4511.1Sfvdl         * check if vnode is in open for writing, because we want to
4521.1Sfvdl         * demand-page out of it.  if it is, don't do it, for various
4531.1Sfvdl         * reasons
4541.1Sfvdl         */
4551.1Sfvdl	if (epp->ep_vp->v_writecount != 0) {
4561.1Sfvdl#ifdef DIAGNOSTIC
4571.1Sfvdl		if (epp->ep_vp->v_flag & VTEXT)
4581.1Sfvdl			panic("exec: a VTEXT vnode has writecount != 0\n");
4591.1Sfvdl#endif
4601.1Sfvdl		return ETXTBSY;
4611.1Sfvdl	}
4621.1Sfvdl	/*
4631.1Sfvdl         * Allocate space to hold all the program headers, and read them
4641.1Sfvdl         * from the file
4651.1Sfvdl         */
4661.1Sfvdl	phsize = eh->e_phnum * sizeof(Elf32_Phdr);
4671.1Sfvdl	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
4681.1Sfvdl
4691.1Sfvdl	if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff,
4701.1Sfvdl				    (caddr_t) ph, phsize)) != 0)
4711.1Sfvdl		goto bad;
4721.1Sfvdl
4731.1Sfvdl	epp->ep_tsize = ELF32_NO_ADDR;
4741.1Sfvdl	epp->ep_dsize = ELF32_NO_ADDR;
4751.1Sfvdl
4761.1Sfvdl	interp[0] = '\0';
4771.1Sfvdl
4781.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
4791.1Sfvdl		pp = &ph[i];
4801.1Sfvdl		if (pp->p_type == Elf32_pt_interp) {
4811.1Sfvdl			if (pp->p_filesz >= sizeof(interp))
4821.1Sfvdl				goto bad;
4831.1Sfvdl			if ((error = elf_read_from(p, epp->ep_vp, pp->p_offset,
4841.1Sfvdl				      (caddr_t) interp, pp->p_filesz)) != 0)
4851.1Sfvdl				goto bad;
4861.1Sfvdl			break;
4871.1Sfvdl		}
4881.1Sfvdl	}
4891.1Sfvdl
4901.1Sfvdl	/*
4911.1Sfvdl	 * On the same architecture, we may be emulating different systems.
4921.1Sfvdl	 * See which one will accept this executable. This currently only
4931.1Sfvdl	 * applies to Linux and SVR4 on the i386.
4941.1Sfvdl	 *
4951.1Sfvdl	 * Probe functions would normally see if the interpreter (if any)
4961.1Sfvdl	 * exists. Emulation packages may possibly replace the interpreter in
4971.1Sfvdl	 * interp[] with a changed path (/emul/xxx/<path>), and also
4981.1Sfvdl	 * set the ep_emul field in the exec package structure.
4991.1Sfvdl	 */
5001.1Sfvdl	if ((n = sizeof elf_probe_funcs / sizeof elf_probe_funcs[0])) {
5011.1Sfvdl		error = ENOEXEC;
5021.1Sfvdl		for (i = 0; i < n && error; i++)
5031.1Sfvdl			error = elf_probe_funcs[i](p, epp, interp, &pos);
5041.1Sfvdl
5051.1Sfvdl		if (error)
5061.1Sfvdl			goto bad;
5071.1Sfvdl	}
5081.1Sfvdl
5091.1Sfvdl	/*
5101.1Sfvdl         * Load all the necessary sections
5111.1Sfvdl         */
5121.1Sfvdl	for (i = 0; i < eh->e_phnum; i++) {
5131.1Sfvdl		u_long  addr = ELF32_NO_ADDR, size = 0;
5141.1Sfvdl		int prot = 0;
5151.1Sfvdl
5161.1Sfvdl		pp = &ph[i];
5171.1Sfvdl
5181.1Sfvdl		switch (ph[i].p_type) {
5191.1Sfvdl		case Elf32_pt_load:
5201.1Sfvdl			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
5211.1Sfvdl				&ph[i], &addr, &size, &prot);
5221.1Sfvdl			if ((error = elf_set_segment(epp, addr, size,
5231.1Sfvdl						      prot)) != 0)
5241.1Sfvdl				goto bad;
5251.1Sfvdl			break;
5261.1Sfvdl
5271.1Sfvdl		case Elf32_pt_shlib:
5281.1Sfvdl			error = ENOEXEC;
5291.1Sfvdl			goto bad;
5301.1Sfvdl
5311.1Sfvdl		case Elf32_pt_interp:
5321.1Sfvdl			/* Already did this one */
5331.1Sfvdl		case Elf32_pt_dynamic:
5341.1Sfvdl		case Elf32_pt_phdr:
5351.1Sfvdl		case Elf32_pt_note:
5361.1Sfvdl			break;
5371.1Sfvdl
5381.1Sfvdl		default:
5391.1Sfvdl			/*
5401.1Sfvdl			 * Not fatal, we don't need to understand everything
5411.1Sfvdl			 * :-)
5421.1Sfvdl			 */
5431.1Sfvdl			break;
5441.1Sfvdl		}
5451.1Sfvdl	}
5461.1Sfvdl
5471.1Sfvdl	/*
5481.1Sfvdl         * Check if we found a dynamically linked binary and arrange to load
5491.1Sfvdl         * it's interpreter
5501.1Sfvdl         */
5511.1Sfvdl	if (interp[0]) {
5521.1Sfvdl		struct elf_args *ap;
5531.1Sfvdl
5541.1Sfvdl		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
5551.1Sfvdl						 M_TEMP, M_WAITOK);
5561.1Sfvdl		if ((error = elf_load_file(p, interp, &epp->ep_vmcmds,
5571.1Sfvdl				&epp->ep_entry, ap, &pos)) != 0) {
5581.1Sfvdl			free((char *) ap, M_TEMP);
5591.1Sfvdl			goto bad;
5601.1Sfvdl		}
5611.1Sfvdl		/* Arrange to load the program headers. */
5621.1Sfvdl		pos = ELF_ALIGN(pos + NBPG, NBPG);
5631.1Sfvdl		ap->arg_phaddr = pos;
5641.1Sfvdl		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, phsize,
5651.1Sfvdl			  pos, epp->ep_vp, eh->e_phoff,
5661.1Sfvdl			  VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
5671.1Sfvdl		pos += phsize;
5681.1Sfvdl
5691.1Sfvdl		ap->arg_phentsize = eh->e_phentsize;
5701.1Sfvdl		ap->arg_phnum = eh->e_phnum;
5711.1Sfvdl		ap->arg_entry = eh->e_entry;
5721.1Sfvdl
5731.1Sfvdl		epp->ep_emul_arg = ap;
5741.1Sfvdl	} else
5751.1Sfvdl		epp->ep_entry = eh->e_entry;
5761.1Sfvdl
5771.1Sfvdl	free((char *) ph, M_TEMP);
5781.1Sfvdl	epp->ep_vp->v_flag |= VTEXT;
5791.1Sfvdl	return exec_aout_setup_stack(p, epp);
5801.1Sfvdl
5811.1Sfvdlbad:
5821.1Sfvdl	free((char *) ph, M_TEMP);
5831.1Sfvdl	kill_vmcmds(&epp->ep_vmcmds);
5841.1Sfvdl	return ENOEXEC;
5851.1Sfvdl}
586