Home | History | Annotate | Line # | Download | only in kern
exec_elf.c revision 1.1
      1  1.1  fvdl /*	$NetBSD: exec_elf.c,v 1.1 1995/06/22 21:29:53 fvdl Exp $	*/
      2  1.1  fvdl 
      3  1.1  fvdl /*
      4  1.1  fvdl  * Copyright (c) 1994 Christos Zoulas
      5  1.1  fvdl  * All rights reserved.
      6  1.1  fvdl  *
      7  1.1  fvdl  * Redistribution and use in source and binary forms, with or without
      8  1.1  fvdl  * modification, are permitted provided that the following conditions
      9  1.1  fvdl  * are met:
     10  1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     11  1.1  fvdl  *    notice, this list of conditions and the following disclaimer.
     12  1.1  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  fvdl  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  fvdl  *    documentation and/or other materials provided with the distribution.
     15  1.1  fvdl  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  fvdl  *    derived from this software without specific prior written permission
     17  1.1  fvdl  *
     18  1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  fvdl  *
     29  1.1  fvdl  */
     30  1.1  fvdl 
     31  1.1  fvdl #include <sys/param.h>
     32  1.1  fvdl #include <sys/systm.h>
     33  1.1  fvdl #include <sys/kernel.h>
     34  1.1  fvdl #include <sys/proc.h>
     35  1.1  fvdl #include <sys/malloc.h>
     36  1.1  fvdl #include <sys/namei.h>
     37  1.1  fvdl #include <sys/vnode.h>
     38  1.1  fvdl #include <sys/exec.h>
     39  1.1  fvdl #include <sys/exec_elf.h>
     40  1.1  fvdl 
     41  1.1  fvdl #include <sys/mman.h>
     42  1.1  fvdl #include <vm/vm.h>
     43  1.1  fvdl #include <vm/vm_param.h>
     44  1.1  fvdl #include <vm/vm_map.h>
     45  1.1  fvdl 
     46  1.1  fvdl #include <machine/cpu.h>
     47  1.1  fvdl #include <machine/reg.h>
     48  1.1  fvdl #include <machine/exec.h>
     49  1.1  fvdl 
     50  1.1  fvdl #ifdef COMPAT_LINUX
     51  1.1  fvdl #include <compat/linux/linux_exec.h>
     52  1.1  fvdl #endif
     53  1.1  fvdl 
     54  1.1  fvdl #ifdef COMPAT_SVR4
     55  1.1  fvdl #include <compat/svr4/svr4_exec.h>
     56  1.1  fvdl #endif
     57  1.1  fvdl 
     58  1.1  fvdl int (*elf_probe_funcs[])() = {
     59  1.1  fvdl #ifdef COMPAT_SVR4
     60  1.1  fvdl 	svr4_elf_probe,
     61  1.1  fvdl #endif
     62  1.1  fvdl #ifdef COMPAT_LINUX
     63  1.1  fvdl 	linux_elf_probe
     64  1.1  fvdl #endif
     65  1.1  fvdl };
     66  1.1  fvdl 
     67  1.1  fvdl static int elf_set_segment __P((struct exec_package *, u_long, u_long,
     68  1.1  fvdl 	int));
     69  1.1  fvdl static int elf_read_from __P((struct proc *, struct vnode *, u_long,
     70  1.1  fvdl 	caddr_t, int));
     71  1.1  fvdl static void elf_load_psection __P((struct exec_vmcmd_set *,
     72  1.1  fvdl 	struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
     73  1.1  fvdl 
     74  1.1  fvdl #define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
     75  1.1  fvdl #define ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
     76  1.1  fvdl 
     77  1.1  fvdl /*
     78  1.1  fvdl  * Copy arguments onto the stack in the normal way, but add some
     79  1.1  fvdl  * extra information in case of dynamic binding.
     80  1.1  fvdl  */
     81  1.1  fvdl void *
     82  1.1  fvdl elf_copyargs(pack, arginfo, stack, argp)
     83  1.1  fvdl 	struct exec_package *pack;
     84  1.1  fvdl 	struct ps_strings *arginfo;
     85  1.1  fvdl 	void *stack;
     86  1.1  fvdl 	void *argp;
     87  1.1  fvdl {
     88  1.1  fvdl 	char **cpp = stack;
     89  1.1  fvdl 	char *dp, *sp;
     90  1.1  fvdl 	size_t len;
     91  1.1  fvdl 	void *nullp = NULL;
     92  1.1  fvdl 	int argc = arginfo->ps_nargvstr;
     93  1.1  fvdl 	int envc = arginfo->ps_nenvstr;
     94  1.1  fvdl 	AuxInfo *a;
     95  1.1  fvdl 	struct elf_args *ap;
     96  1.1  fvdl 
     97  1.1  fvdl 	if (copyout(&argc, cpp++, sizeof(argc)))
     98  1.1  fvdl 		return NULL;
     99  1.1  fvdl 
    100  1.1  fvdl 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
    101  1.1  fvdl 	sp = argp;
    102  1.1  fvdl 
    103  1.1  fvdl 	/* XXX don't copy them out, remap them! */
    104  1.1  fvdl 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    105  1.1  fvdl 
    106  1.1  fvdl 	for (; --argc >= 0; sp += len, dp += len)
    107  1.1  fvdl 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    108  1.1  fvdl 		    copyoutstr(sp, dp, ARG_MAX, &len))
    109  1.1  fvdl 			return NULL;
    110  1.1  fvdl 
    111  1.1  fvdl 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    112  1.1  fvdl 		return NULL;
    113  1.1  fvdl 
    114  1.1  fvdl 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    115  1.1  fvdl 
    116  1.1  fvdl 	for (; --envc >= 0; sp += len, dp += len)
    117  1.1  fvdl 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    118  1.1  fvdl 		    copyoutstr(sp, dp, ARG_MAX, &len))
    119  1.1  fvdl 			return NULL;
    120  1.1  fvdl 
    121  1.1  fvdl 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    122  1.1  fvdl 		return NULL;
    123  1.1  fvdl 
    124  1.1  fvdl 	/*
    125  1.1  fvdl 	 * Push extra arguments on the stack needed by dynamically
    126  1.1  fvdl 	 * linked binaries
    127  1.1  fvdl 	 */
    128  1.1  fvdl 	a = (AuxInfo *) cpp;
    129  1.1  fvdl 	if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
    130  1.1  fvdl 
    131  1.1  fvdl 		a->au_id = AUX_phdr;
    132  1.1  fvdl 		a->au_v = ap->arg_phaddr;
    133  1.1  fvdl 		a++;
    134  1.1  fvdl 
    135  1.1  fvdl 		a->au_id = AUX_phent;
    136  1.1  fvdl 		a->au_v = ap->arg_phentsize;
    137  1.1  fvdl 		a++;
    138  1.1  fvdl 
    139  1.1  fvdl 		a->au_id = AUX_phnum;
    140  1.1  fvdl 		a->au_v = ap->arg_phnum;
    141  1.1  fvdl 		a++;
    142  1.1  fvdl 
    143  1.1  fvdl 		a->au_id = AUX_pagesz;
    144  1.1  fvdl 		a->au_v = NBPG;
    145  1.1  fvdl 		a++;
    146  1.1  fvdl 
    147  1.1  fvdl 		a->au_id = AUX_base;
    148  1.1  fvdl 		a->au_v = ap->arg_interp;
    149  1.1  fvdl 		a++;
    150  1.1  fvdl 
    151  1.1  fvdl 		a->au_id = AUX_flags;
    152  1.1  fvdl 		a->au_v = 0;
    153  1.1  fvdl 		a++;
    154  1.1  fvdl 
    155  1.1  fvdl 		a->au_id = AUX_entry;
    156  1.1  fvdl 		a->au_v = ap->arg_entry;
    157  1.1  fvdl 		a++;
    158  1.1  fvdl 
    159  1.1  fvdl 		a->au_id = AUX_null;
    160  1.1  fvdl 		a->au_v = 0;
    161  1.1  fvdl 		a++;
    162  1.1  fvdl 
    163  1.1  fvdl 		free((char *) ap, M_TEMP);
    164  1.1  fvdl 	}
    165  1.1  fvdl 	return a;
    166  1.1  fvdl }
    167  1.1  fvdl 
    168  1.1  fvdl /*
    169  1.1  fvdl  * elf_check_header():
    170  1.1  fvdl  *
    171  1.1  fvdl  * Check header for validity; return 0 of ok ENOEXEC if error
    172  1.1  fvdl  *
    173  1.1  fvdl  * XXX machine type needs to be moved to <machine/param.h> so
    174  1.1  fvdl  * just one comparison can be done. Unfortunately, there is both
    175  1.1  fvdl  * em_486 and em_386, so this would not work on the i386.
    176  1.1  fvdl  */
    177  1.1  fvdl int
    178  1.1  fvdl elf_check_header(eh, type)
    179  1.1  fvdl 	Elf32_Ehdr *eh;
    180  1.1  fvdl 	int type;
    181  1.1  fvdl {
    182  1.1  fvdl #ifdef sparc
    183  1.1  fvdl   /* #$%@#$%@#$%! */
    184  1.1  fvdl # define memcmp bcmp
    185  1.1  fvdl #endif
    186  1.1  fvdl 	if (memcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0)
    187  1.1  fvdl 		return ENOEXEC;
    188  1.1  fvdl 
    189  1.1  fvdl 	switch (eh->e_machine) {
    190  1.1  fvdl 	/* XXX */
    191  1.1  fvdl #ifdef i386
    192  1.1  fvdl 	case Elf32_em_386:
    193  1.1  fvdl 	case Elf32_em_486:
    194  1.1  fvdl #endif
    195  1.1  fvdl #ifdef sparc
    196  1.1  fvdl 	case Elf32_em_sparc:
    197  1.1  fvdl #endif
    198  1.1  fvdl 		break;
    199  1.1  fvdl 
    200  1.1  fvdl 	default:
    201  1.1  fvdl 		return ENOEXEC;
    202  1.1  fvdl 	}
    203  1.1  fvdl 
    204  1.1  fvdl 	if (eh->e_type != type)
    205  1.1  fvdl 		return ENOEXEC;
    206  1.1  fvdl 
    207  1.1  fvdl 	return 0;
    208  1.1  fvdl }
    209  1.1  fvdl 
    210  1.1  fvdl /*
    211  1.1  fvdl  * elf_load_psection():
    212  1.1  fvdl  *
    213  1.1  fvdl  * Load a psection at the appropriate address
    214  1.1  fvdl  */
    215  1.1  fvdl static void
    216  1.1  fvdl elf_load_psection(vcset, vp, ph, addr, size, prot)
    217  1.1  fvdl 	struct exec_vmcmd_set *vcset;
    218  1.1  fvdl 	struct vnode *vp;
    219  1.1  fvdl 	Elf32_Phdr *ph;
    220  1.1  fvdl 	u_long *addr;
    221  1.1  fvdl 	u_long *size;
    222  1.1  fvdl 	int *prot;
    223  1.1  fvdl {
    224  1.1  fvdl 	u_long uaddr, msize, rm, rf;
    225  1.1  fvdl 	long diff, offset;
    226  1.1  fvdl 
    227  1.1  fvdl 	/*
    228  1.1  fvdl          * If the user specified an address, then we load there.
    229  1.1  fvdl          */
    230  1.1  fvdl 	if (*addr != ELF32_NO_ADDR) {
    231  1.1  fvdl 		if (ph->p_align > 1) {
    232  1.1  fvdl 			*addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
    233  1.1  fvdl 			uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
    234  1.1  fvdl 		} else
    235  1.1  fvdl 			uaddr = ph->p_vaddr;
    236  1.1  fvdl 		diff = ph->p_vaddr - uaddr;
    237  1.1  fvdl 	} else {
    238  1.1  fvdl 		*addr = uaddr = ph->p_vaddr;
    239  1.1  fvdl 		if (ph->p_align > 1)
    240  1.1  fvdl 			*addr = ELF_ALIGN(uaddr, ph->p_align);
    241  1.1  fvdl 		diff = uaddr - *addr;
    242  1.1  fvdl 	}
    243  1.1  fvdl 
    244  1.1  fvdl 	*prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
    245  1.1  fvdl 	*prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
    246  1.1  fvdl 	*prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
    247  1.1  fvdl 
    248  1.1  fvdl 	offset = ph->p_offset - diff;
    249  1.1  fvdl 	*size = ph->p_filesz + diff;
    250  1.1  fvdl 	msize = ph->p_memsz + diff;
    251  1.1  fvdl 
    252  1.1  fvdl 	NEW_VMCMD(vcset, vmcmd_map_readvn, *size, *addr, vp, offset, *prot);
    253  1.1  fvdl 
    254  1.1  fvdl 	/*
    255  1.1  fvdl          * Check if we need to extend the size of the segment
    256  1.1  fvdl          */
    257  1.1  fvdl 	rm = round_page(*addr + msize);
    258  1.1  fvdl 	rf = round_page(*addr + *size);
    259  1.1  fvdl 
    260  1.1  fvdl 	if (rm != rf) {
    261  1.1  fvdl 		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
    262  1.1  fvdl 		*size = msize;
    263  1.1  fvdl 	}
    264  1.1  fvdl }
    265  1.1  fvdl 
    266  1.1  fvdl /*
    267  1.1  fvdl  * elf_set_segment():
    268  1.1  fvdl  *
    269  1.1  fvdl  * Decide if the segment is text or data, depending on the protection
    270  1.1  fvdl  * and set it appropriately
    271  1.1  fvdl  */
    272  1.1  fvdl static int
    273  1.1  fvdl elf_set_segment(epp, vaddr, size, prot)
    274  1.1  fvdl 	struct exec_package *epp;
    275  1.1  fvdl 	u_long vaddr;
    276  1.1  fvdl 	u_long size;
    277  1.1  fvdl 	int prot;
    278  1.1  fvdl {
    279  1.1  fvdl 	/*
    280  1.1  fvdl          * Kludge: Unfortunately the current implementation of
    281  1.1  fvdl          * exec package assumes a single text and data segment.
    282  1.1  fvdl          * In Elf we can have more, but here we limit ourselves
    283  1.1  fvdl          * to two and hope :-(
    284  1.1  fvdl          * We also assume that the text is r-x, and data is rwx or rw-.
    285  1.1  fvdl          */
    286  1.1  fvdl 	switch (prot) {
    287  1.1  fvdl 	case (VM_PROT_READ | VM_PROT_EXECUTE):
    288  1.1  fvdl 		if (epp->ep_tsize != ELF32_NO_ADDR)
    289  1.1  fvdl 			return ENOEXEC;
    290  1.1  fvdl 		epp->ep_taddr = vaddr;
    291  1.1  fvdl 		epp->ep_tsize = size;
    292  1.1  fvdl 		break;
    293  1.1  fvdl 
    294  1.1  fvdl 	case (VM_PROT_READ | VM_PROT_WRITE):
    295  1.1  fvdl 	case (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE):
    296  1.1  fvdl 		if (epp->ep_dsize != ELF32_NO_ADDR)
    297  1.1  fvdl 			return ENOEXEC;
    298  1.1  fvdl 		epp->ep_daddr = vaddr;
    299  1.1  fvdl 		epp->ep_dsize = size;
    300  1.1  fvdl 		break;
    301  1.1  fvdl 
    302  1.1  fvdl 	default:
    303  1.1  fvdl 		return ENOEXEC;
    304  1.1  fvdl 	}
    305  1.1  fvdl 	return 0;
    306  1.1  fvdl }
    307  1.1  fvdl 
    308  1.1  fvdl /*
    309  1.1  fvdl  * elf_read_from():
    310  1.1  fvdl  *
    311  1.1  fvdl  *	Read from vnode into buffer at offset.
    312  1.1  fvdl  */
    313  1.1  fvdl static int
    314  1.1  fvdl elf_read_from(p, vp, off, buf, size)
    315  1.1  fvdl 	struct vnode *vp;
    316  1.1  fvdl 	u_long off;
    317  1.1  fvdl 	struct proc *p;
    318  1.1  fvdl 	caddr_t buf;
    319  1.1  fvdl 	int size;
    320  1.1  fvdl {
    321  1.1  fvdl 	int error;
    322  1.1  fvdl 	int resid;
    323  1.1  fvdl 
    324  1.1  fvdl 	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
    325  1.1  fvdl 			     off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
    326  1.1  fvdl 			     &resid, p)) != 0)
    327  1.1  fvdl 		return error;
    328  1.1  fvdl 	/*
    329  1.1  fvdl          * See if we got all of it
    330  1.1  fvdl          */
    331  1.1  fvdl 	if (resid != 0)
    332  1.1  fvdl 		return error;
    333  1.1  fvdl 	return 0;
    334  1.1  fvdl }
    335  1.1  fvdl 
    336  1.1  fvdl /*
    337  1.1  fvdl  * elf_load_file():
    338  1.1  fvdl  *
    339  1.1  fvdl  * Load a file (interpreter/library) pointed to by path
    340  1.1  fvdl  * [stolen from coff_load_shlib()]. Made slightly generic
    341  1.1  fvdl  * so it might be used externally.
    342  1.1  fvdl  */
    343  1.1  fvdl int
    344  1.1  fvdl elf_load_file(p, path, vcset, entry, ap, last)
    345  1.1  fvdl 	struct proc *p;
    346  1.1  fvdl 	char *path;
    347  1.1  fvdl 	struct exec_vmcmd_set *vcset;
    348  1.1  fvdl 	u_long *entry;
    349  1.1  fvdl 	struct elf_args	*ap;
    350  1.1  fvdl 	u_long *last;
    351  1.1  fvdl {
    352  1.1  fvdl 	int error, i;
    353  1.1  fvdl 	struct nameidata nd;
    354  1.1  fvdl 	Elf32_Ehdr eh;
    355  1.1  fvdl 	Elf32_Phdr *ph = NULL;
    356  1.1  fvdl 	u_long phsize;
    357  1.1  fvdl 	char *bp = NULL;
    358  1.1  fvdl 	u_long addr = *last;
    359  1.1  fvdl 
    360  1.1  fvdl 	bp = path;
    361  1.1  fvdl 	/*
    362  1.1  fvdl          * 1. open file
    363  1.1  fvdl          * 2. read filehdr
    364  1.1  fvdl          * 3. map text, data, and bss out of it using VM_*
    365  1.1  fvdl          */
    366  1.1  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
    367  1.1  fvdl 	if ((error = namei(&nd)) != 0) {
    368  1.1  fvdl 		return error;
    369  1.1  fvdl 	}
    370  1.1  fvdl 	if ((error = elf_read_from(p, nd.ni_vp, 0, (caddr_t) &eh,
    371  1.1  fvdl 				    sizeof(eh))) != 0)
    372  1.1  fvdl 		goto bad;
    373  1.1  fvdl 
    374  1.1  fvdl 	if ((error = elf_check_header(&eh, Elf32_et_dyn)) != 0)
    375  1.1  fvdl 		goto bad;
    376  1.1  fvdl 
    377  1.1  fvdl 	phsize = eh.e_phnum * sizeof(Elf32_Phdr);
    378  1.1  fvdl 	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
    379  1.1  fvdl 
    380  1.1  fvdl 	if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff,
    381  1.1  fvdl 				    (caddr_t) ph, phsize)) != 0)
    382  1.1  fvdl 		goto bad;
    383  1.1  fvdl 
    384  1.1  fvdl 	/*
    385  1.1  fvdl          * Load all the necessary sections
    386  1.1  fvdl          */
    387  1.1  fvdl 	for (i = 0; i < eh.e_phnum; i++) {
    388  1.1  fvdl 		u_long size = 0;
    389  1.1  fvdl 		int prot = 0;
    390  1.1  fvdl 
    391  1.1  fvdl 		switch (ph[i].p_type) {
    392  1.1  fvdl 		case Elf32_pt_load:
    393  1.1  fvdl 			elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
    394  1.1  fvdl 						&size, &prot);
    395  1.1  fvdl 			/* Assume that the text segment is r-x only */
    396  1.1  fvdl 			if ((prot & PROT_WRITE) == 0) {
    397  1.1  fvdl 				*entry = addr + eh.e_entry;
    398  1.1  fvdl 				ap->arg_interp = addr;
    399  1.1  fvdl 			}
    400  1.1  fvdl 			addr += size;
    401  1.1  fvdl 			break;
    402  1.1  fvdl 
    403  1.1  fvdl 		case Elf32_pt_dynamic:
    404  1.1  fvdl 		case Elf32_pt_phdr:
    405  1.1  fvdl 		case Elf32_pt_note:
    406  1.1  fvdl 			break;
    407  1.1  fvdl 
    408  1.1  fvdl 		default:
    409  1.1  fvdl 			break;
    410  1.1  fvdl 		}
    411  1.1  fvdl 	}
    412  1.1  fvdl 
    413  1.1  fvdl bad:
    414  1.1  fvdl 	if (ph != NULL)
    415  1.1  fvdl 		free((char *) ph, M_TEMP);
    416  1.1  fvdl 
    417  1.1  fvdl 	*last = addr;
    418  1.1  fvdl 	vrele(nd.ni_vp);
    419  1.1  fvdl 	return error;
    420  1.1  fvdl }
    421  1.1  fvdl 
    422  1.1  fvdl /*
    423  1.1  fvdl  * exec_elf_makecmds(): Prepare an Elf binary's exec package
    424  1.1  fvdl  *
    425  1.1  fvdl  * First, set of the various offsets/lengths in the exec package.
    426  1.1  fvdl  *
    427  1.1  fvdl  * Then, mark the text image busy (so it can be demand paged) or error
    428  1.1  fvdl  * out if this is not possible.  Finally, set up vmcmds for the
    429  1.1  fvdl  * text, data, bss, and stack segments.
    430  1.1  fvdl  *
    431  1.1  fvdl  * XXX no demand paging (yet?)
    432  1.1  fvdl  */
    433  1.1  fvdl int
    434  1.1  fvdl exec_elf_makecmds(p, epp)
    435  1.1  fvdl 	struct proc *p;
    436  1.1  fvdl 	struct exec_package *epp;
    437  1.1  fvdl {
    438  1.1  fvdl 	Elf32_Ehdr *eh = epp->ep_hdr;
    439  1.1  fvdl 	Elf32_Phdr *ph, *pp;
    440  1.1  fvdl 	int error, i, n;
    441  1.1  fvdl 	char interp[MAXPATHLEN];
    442  1.1  fvdl 	u_long pos = 0, phsize;
    443  1.1  fvdl 
    444  1.1  fvdl 	if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
    445  1.1  fvdl 		return ENOEXEC;
    446  1.1  fvdl 
    447  1.1  fvdl 	if (elf_check_header(eh, Elf32_et_exec))
    448  1.1  fvdl 		return ENOEXEC;
    449  1.1  fvdl 
    450  1.1  fvdl 	/*
    451  1.1  fvdl          * check if vnode is in open for writing, because we want to
    452  1.1  fvdl          * demand-page out of it.  if it is, don't do it, for various
    453  1.1  fvdl          * reasons
    454  1.1  fvdl          */
    455  1.1  fvdl 	if (epp->ep_vp->v_writecount != 0) {
    456  1.1  fvdl #ifdef DIAGNOSTIC
    457  1.1  fvdl 		if (epp->ep_vp->v_flag & VTEXT)
    458  1.1  fvdl 			panic("exec: a VTEXT vnode has writecount != 0\n");
    459  1.1  fvdl #endif
    460  1.1  fvdl 		return ETXTBSY;
    461  1.1  fvdl 	}
    462  1.1  fvdl 	/*
    463  1.1  fvdl          * Allocate space to hold all the program headers, and read them
    464  1.1  fvdl          * from the file
    465  1.1  fvdl          */
    466  1.1  fvdl 	phsize = eh->e_phnum * sizeof(Elf32_Phdr);
    467  1.1  fvdl 	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
    468  1.1  fvdl 
    469  1.1  fvdl 	if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff,
    470  1.1  fvdl 				    (caddr_t) ph, phsize)) != 0)
    471  1.1  fvdl 		goto bad;
    472  1.1  fvdl 
    473  1.1  fvdl 	epp->ep_tsize = ELF32_NO_ADDR;
    474  1.1  fvdl 	epp->ep_dsize = ELF32_NO_ADDR;
    475  1.1  fvdl 
    476  1.1  fvdl 	interp[0] = '\0';
    477  1.1  fvdl 
    478  1.1  fvdl 	for (i = 0; i < eh->e_phnum; i++) {
    479  1.1  fvdl 		pp = &ph[i];
    480  1.1  fvdl 		if (pp->p_type == Elf32_pt_interp) {
    481  1.1  fvdl 			if (pp->p_filesz >= sizeof(interp))
    482  1.1  fvdl 				goto bad;
    483  1.1  fvdl 			if ((error = elf_read_from(p, epp->ep_vp, pp->p_offset,
    484  1.1  fvdl 				      (caddr_t) interp, pp->p_filesz)) != 0)
    485  1.1  fvdl 				goto bad;
    486  1.1  fvdl 			break;
    487  1.1  fvdl 		}
    488  1.1  fvdl 	}
    489  1.1  fvdl 
    490  1.1  fvdl 	/*
    491  1.1  fvdl 	 * On the same architecture, we may be emulating different systems.
    492  1.1  fvdl 	 * See which one will accept this executable. This currently only
    493  1.1  fvdl 	 * applies to Linux and SVR4 on the i386.
    494  1.1  fvdl 	 *
    495  1.1  fvdl 	 * Probe functions would normally see if the interpreter (if any)
    496  1.1  fvdl 	 * exists. Emulation packages may possibly replace the interpreter in
    497  1.1  fvdl 	 * interp[] with a changed path (/emul/xxx/<path>), and also
    498  1.1  fvdl 	 * set the ep_emul field in the exec package structure.
    499  1.1  fvdl 	 */
    500  1.1  fvdl 	if ((n = sizeof elf_probe_funcs / sizeof elf_probe_funcs[0])) {
    501  1.1  fvdl 		error = ENOEXEC;
    502  1.1  fvdl 		for (i = 0; i < n && error; i++)
    503  1.1  fvdl 			error = elf_probe_funcs[i](p, epp, interp, &pos);
    504  1.1  fvdl 
    505  1.1  fvdl 		if (error)
    506  1.1  fvdl 			goto bad;
    507  1.1  fvdl 	}
    508  1.1  fvdl 
    509  1.1  fvdl 	/*
    510  1.1  fvdl          * Load all the necessary sections
    511  1.1  fvdl          */
    512  1.1  fvdl 	for (i = 0; i < eh->e_phnum; i++) {
    513  1.1  fvdl 		u_long  addr = ELF32_NO_ADDR, size = 0;
    514  1.1  fvdl 		int prot = 0;
    515  1.1  fvdl 
    516  1.1  fvdl 		pp = &ph[i];
    517  1.1  fvdl 
    518  1.1  fvdl 		switch (ph[i].p_type) {
    519  1.1  fvdl 		case Elf32_pt_load:
    520  1.1  fvdl 			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
    521  1.1  fvdl 				&ph[i], &addr, &size, &prot);
    522  1.1  fvdl 			if ((error = elf_set_segment(epp, addr, size,
    523  1.1  fvdl 						      prot)) != 0)
    524  1.1  fvdl 				goto bad;
    525  1.1  fvdl 			break;
    526  1.1  fvdl 
    527  1.1  fvdl 		case Elf32_pt_shlib:
    528  1.1  fvdl 			error = ENOEXEC;
    529  1.1  fvdl 			goto bad;
    530  1.1  fvdl 
    531  1.1  fvdl 		case Elf32_pt_interp:
    532  1.1  fvdl 			/* Already did this one */
    533  1.1  fvdl 		case Elf32_pt_dynamic:
    534  1.1  fvdl 		case Elf32_pt_phdr:
    535  1.1  fvdl 		case Elf32_pt_note:
    536  1.1  fvdl 			break;
    537  1.1  fvdl 
    538  1.1  fvdl 		default:
    539  1.1  fvdl 			/*
    540  1.1  fvdl 			 * Not fatal, we don't need to understand everything
    541  1.1  fvdl 			 * :-)
    542  1.1  fvdl 			 */
    543  1.1  fvdl 			break;
    544  1.1  fvdl 		}
    545  1.1  fvdl 	}
    546  1.1  fvdl 
    547  1.1  fvdl 	/*
    548  1.1  fvdl          * Check if we found a dynamically linked binary and arrange to load
    549  1.1  fvdl          * it's interpreter
    550  1.1  fvdl          */
    551  1.1  fvdl 	if (interp[0]) {
    552  1.1  fvdl 		struct elf_args *ap;
    553  1.1  fvdl 
    554  1.1  fvdl 		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
    555  1.1  fvdl 						 M_TEMP, M_WAITOK);
    556  1.1  fvdl 		if ((error = elf_load_file(p, interp, &epp->ep_vmcmds,
    557  1.1  fvdl 				&epp->ep_entry, ap, &pos)) != 0) {
    558  1.1  fvdl 			free((char *) ap, M_TEMP);
    559  1.1  fvdl 			goto bad;
    560  1.1  fvdl 		}
    561  1.1  fvdl 		/* Arrange to load the program headers. */
    562  1.1  fvdl 		pos = ELF_ALIGN(pos + NBPG, NBPG);
    563  1.1  fvdl 		ap->arg_phaddr = pos;
    564  1.1  fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, phsize,
    565  1.1  fvdl 			  pos, epp->ep_vp, eh->e_phoff,
    566  1.1  fvdl 			  VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    567  1.1  fvdl 		pos += phsize;
    568  1.1  fvdl 
    569  1.1  fvdl 		ap->arg_phentsize = eh->e_phentsize;
    570  1.1  fvdl 		ap->arg_phnum = eh->e_phnum;
    571  1.1  fvdl 		ap->arg_entry = eh->e_entry;
    572  1.1  fvdl 
    573  1.1  fvdl 		epp->ep_emul_arg = ap;
    574  1.1  fvdl 	} else
    575  1.1  fvdl 		epp->ep_entry = eh->e_entry;
    576  1.1  fvdl 
    577  1.1  fvdl 	free((char *) ph, M_TEMP);
    578  1.1  fvdl 	epp->ep_vp->v_flag |= VTEXT;
    579  1.1  fvdl 	return exec_aout_setup_stack(p, epp);
    580  1.1  fvdl 
    581  1.1  fvdl bad:
    582  1.1  fvdl 	free((char *) ph, M_TEMP);
    583  1.1  fvdl 	kill_vmcmds(&epp->ep_vmcmds);
    584  1.1  fvdl 	return ENOEXEC;
    585  1.1  fvdl }
    586