Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.6
      1  1.6      fvdl /*	$NetBSD: linux_exec_elf32.c,v 1.6 1995/06/11 14:56:47 fvdl Exp $	*/
      2  1.1      fvdl 
      3  1.1      fvdl /*
      4  1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
      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. All advertising materials mentioning features or use of this software
     16  1.1      fvdl  *    must display the following acknowledgement:
     17  1.1      fvdl  *      This product includes software developed for the NetBSD Project
     18  1.1      fvdl  *      by Frank van der Linden
     19  1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     20  1.1      fvdl  *    derived from this software without specific prior written permission
     21  1.1      fvdl  *
     22  1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1      fvdl  *
     33  1.1      fvdl  * based on kern/exec_aout.c and compat/sunos/sunos_exec.c
     34  1.1      fvdl  */
     35  1.1      fvdl 
     36  1.1      fvdl #include <sys/param.h>
     37  1.1      fvdl #include <sys/systm.h>
     38  1.1      fvdl #include <sys/filedesc.h>
     39  1.1      fvdl #include <sys/kernel.h>
     40  1.1      fvdl #include <sys/proc.h>
     41  1.1      fvdl #include <sys/mount.h>
     42  1.1      fvdl #include <sys/malloc.h>
     43  1.1      fvdl #include <sys/namei.h>
     44  1.1      fvdl #include <sys/vnode.h>
     45  1.1      fvdl #include <sys/file.h>
     46  1.1      fvdl #include <sys/resourcevar.h>
     47  1.1      fvdl #include <sys/wait.h>
     48  1.1      fvdl 
     49  1.1      fvdl #include <sys/mman.h>
     50  1.1      fvdl #include <vm/vm.h>
     51  1.1      fvdl #include <vm/vm_param.h>
     52  1.1      fvdl #include <vm/vm_map.h>
     53  1.1      fvdl #include <vm/vm_kern.h>
     54  1.1      fvdl #include <vm/vm_pager.h>
     55  1.1      fvdl 
     56  1.1      fvdl #include <machine/cpu.h>
     57  1.1      fvdl #include <machine/reg.h>
     58  1.1      fvdl #include <machine/exec.h>
     59  1.4  christos #include <machine/linux_machdep.h>
     60  1.1      fvdl 
     61  1.1      fvdl #include <compat/linux/linux_types.h>
     62  1.4  christos #include <compat/linux/linux_syscall.h>
     63  1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     64  1.1      fvdl #include <compat/linux/linux_util.h>
     65  1.1      fvdl #include <compat/linux/linux_exec.h>
     66  1.1      fvdl 
     67  1.6      fvdl struct elf_args {
     68  1.6      fvdl 	u_long  arg_entry;	/* progran entry point */
     69  1.6      fvdl 	u_long  arg_interp;	/* Interpreter load address */
     70  1.6      fvdl 	u_long  arg_phaddr;	/* program header address */
     71  1.6      fvdl 	u_long  arg_phentsize;	/* Size of program header */
     72  1.6      fvdl 	u_long  arg_phnum;	/* Number of program headers */
     73  1.6      fvdl };
     74  1.6      fvdl 
     75  1.6      fvdl static void *linux_aout_copyargs __P((struct exec_package *,
     76  1.6      fvdl 	struct ps_strings *, void *, void *));
     77  1.6      fvdl static void *linux_elf_copyargs __P((struct exec_package *, struct ps_strings *,
     78  1.6      fvdl 	void *, void *));
     79  1.6      fvdl static int linux_elf_check_header __P((Elf32_Ehdr *, int));
     80  1.6      fvdl static void linux_elf_load_psection __P((struct exec_vmcmd_set *,
     81  1.6      fvdl 	struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
     82  1.6      fvdl static int linux_elf_set_segment __P((struct exec_package *, u_long, u_long,
     83  1.6      fvdl 	int));
     84  1.6      fvdl static int linux_elf_read_from __P((struct vnode *, u_long, struct proc *,
     85  1.6      fvdl 	caddr_t, int));
     86  1.6      fvdl static int linux_elf_load_file __P((struct proc *, char *,
     87  1.6      fvdl 	struct exec_vmcmd_set *, u_long *, struct elf_args *, u_long *));
     88  1.6      fvdl 
     89  1.6      fvdl #ifdef DEBUG_EXEC_LINUX_ELF
     90  1.6      fvdl #define DPRINTF(x) printf x
     91  1.6      fvdl #else
     92  1.6      fvdl #define DPRINTF(x)
     93  1.6      fvdl #endif
     94  1.4  christos 
     95  1.6      fvdl #define LINUX_ELF_ALIGN(a, b) ((a) & ~((b) - 1))
     96  1.6      fvdl #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
     97  1.6      fvdl #define	LINUX_AOUT_AUX_ARGSIZ	2
     98  1.4  christos 
     99  1.4  christos extern int linux_error[];
    100  1.4  christos extern struct sysent linux_sysent[];
    101  1.4  christos extern char *linux_syscallnames[];
    102  1.4  christos 
    103  1.6      fvdl struct emul emul_linux_aout = {
    104  1.4  christos 	"linux",
    105  1.4  christos 	linux_error,
    106  1.4  christos 	linux_sendsig,
    107  1.4  christos 	LINUX_SYS_syscall,
    108  1.4  christos 	LINUX_SYS_MAXSYSCALL,
    109  1.4  christos 	linux_sysent,
    110  1.4  christos 	linux_syscallnames,
    111  1.6      fvdl 	LINUX_AOUT_AUX_ARGSIZ,
    112  1.6      fvdl 	linux_aout_copyargs,
    113  1.6      fvdl 	setregs,
    114  1.6      fvdl 	linux_sigcode,
    115  1.6      fvdl 	linux_esigcode,
    116  1.6      fvdl };
    117  1.6      fvdl 
    118  1.6      fvdl struct emul emul_linux_elf = {
    119  1.6      fvdl 	"linux",
    120  1.6      fvdl 	linux_error,
    121  1.6      fvdl 	linux_sendsig,
    122  1.6      fvdl 	LINUX_SYS_syscall,
    123  1.6      fvdl 	LINUX_SYS_MAXSYSCALL,
    124  1.6      fvdl 	linux_sysent,
    125  1.6      fvdl 	linux_syscallnames,
    126  1.6      fvdl 	LINUX_ELF_AUX_ARGSIZ,
    127  1.6      fvdl 	linux_elf_copyargs,
    128  1.4  christos 	setregs,
    129  1.4  christos 	linux_sigcode,
    130  1.4  christos 	linux_esigcode,
    131  1.4  christos };
    132  1.4  christos 
    133  1.4  christos 
    134  1.4  christos static void *
    135  1.6      fvdl linux_aout_copyargs(pack, arginfo, stack, argp)
    136  1.4  christos 	struct exec_package *pack;
    137  1.4  christos 	struct ps_strings *arginfo;
    138  1.4  christos 	void *stack;
    139  1.4  christos 	void *argp;
    140  1.4  christos {
    141  1.4  christos 	char **cpp = stack;
    142  1.4  christos 	char **stk = stack;
    143  1.4  christos 	char *dp, *sp;
    144  1.4  christos 	size_t len;
    145  1.4  christos 	void *nullp = NULL;
    146  1.4  christos 	int argc = arginfo->ps_nargvstr;
    147  1.4  christos 	int envc = arginfo->ps_nenvstr;
    148  1.4  christos 
    149  1.4  christos 	if (copyout(&argc, cpp++, sizeof(argc)))
    150  1.4  christos 		return NULL;
    151  1.4  christos 
    152  1.4  christos 	/* leave room for envp and argv */
    153  1.4  christos 	cpp += 2;
    154  1.4  christos 	if (copyout(&cpp, &stk[1], sizeof (cpp)))
    155  1.4  christos 		return NULL;
    156  1.4  christos 
    157  1.4  christos 	dp = (char *) (cpp + argc + envc + 2);
    158  1.4  christos 	sp = argp;
    159  1.4  christos 
    160  1.4  christos 	/* XXX don't copy them out, remap them! */
    161  1.5   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    162  1.4  christos 
    163  1.4  christos 	for (; --argc >= 0; sp += len, dp += len)
    164  1.4  christos 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    165  1.4  christos 		    copyoutstr(sp, dp, ARG_MAX, &len))
    166  1.4  christos 			return NULL;
    167  1.4  christos 
    168  1.4  christos 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    169  1.4  christos 		return NULL;
    170  1.4  christos 
    171  1.4  christos 	if (copyout(&cpp, &stk[2], sizeof (cpp)))
    172  1.4  christos 		return NULL;
    173  1.4  christos 
    174  1.5   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    175  1.4  christos 
    176  1.4  christos 	for (; --envc >= 0; sp += len, dp += len)
    177  1.4  christos 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    178  1.4  christos 		    copyoutstr(sp, dp, ARG_MAX, &len))
    179  1.4  christos 			return NULL;
    180  1.4  christos 
    181  1.4  christos 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    182  1.4  christos 		return NULL;
    183  1.4  christos 
    184  1.4  christos 	return cpp;
    185  1.4  christos }
    186  1.4  christos 
    187  1.6      fvdl static void *
    188  1.6      fvdl linux_elf_copyargs(pack, arginfo, stack, argp)
    189  1.6      fvdl 	struct exec_package *pack;
    190  1.6      fvdl 	struct ps_strings *arginfo;
    191  1.6      fvdl 	void *stack;
    192  1.6      fvdl 	void *argp;
    193  1.6      fvdl {
    194  1.6      fvdl 	char **cpp = stack;
    195  1.6      fvdl 	char *dp, *sp;
    196  1.6      fvdl 	size_t len;
    197  1.6      fvdl 	void *nullp = NULL;
    198  1.6      fvdl 	int argc = arginfo->ps_nargvstr;
    199  1.6      fvdl 	int envc = arginfo->ps_nenvstr;
    200  1.6      fvdl 	AuxInfo *a;
    201  1.6      fvdl 	struct elf_args *ap;
    202  1.6      fvdl 
    203  1.6      fvdl 	if (copyout(&argc, cpp++, sizeof(argc)))
    204  1.6      fvdl 		return NULL;
    205  1.6      fvdl 
    206  1.6      fvdl 	dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
    207  1.6      fvdl 	sp = argp;
    208  1.6      fvdl 
    209  1.6      fvdl 	/* XXX don't copy them out, remap them! */
    210  1.6      fvdl 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    211  1.6      fvdl 
    212  1.6      fvdl 	for (; --argc >= 0; sp += len, dp += len)
    213  1.6      fvdl 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    214  1.6      fvdl 		    copyoutstr(sp, dp, ARG_MAX, &len))
    215  1.6      fvdl 			return NULL;
    216  1.6      fvdl 
    217  1.6      fvdl 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    218  1.6      fvdl 		return NULL;
    219  1.6      fvdl 
    220  1.6      fvdl 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    221  1.6      fvdl 
    222  1.6      fvdl 	for (; --envc >= 0; sp += len, dp += len)
    223  1.6      fvdl 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    224  1.6      fvdl 		    copyoutstr(sp, dp, ARG_MAX, &len))
    225  1.6      fvdl 			return NULL;
    226  1.6      fvdl 
    227  1.6      fvdl 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    228  1.6      fvdl 		return NULL;
    229  1.6      fvdl 
    230  1.6      fvdl 	/*
    231  1.6      fvdl 	 * Push extra arguments on the stack needed by dynamically
    232  1.6      fvdl 	 * linked binaries
    233  1.6      fvdl 	 */
    234  1.6      fvdl 	a = (AuxInfo *) cpp;
    235  1.6      fvdl 	if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
    236  1.6      fvdl 
    237  1.6      fvdl 		DPRINTF(("phaddr=0x%x, phsize=%d, phnum=%d, interp=0x%x, ",
    238  1.6      fvdl 			 ap->arg_phaddr, ap->arg_phentsize, ap->arg_phnum,
    239  1.6      fvdl 			 ap->arg_interp));
    240  1.6      fvdl 		DPRINTF((" entry=0x%x\n", ap->arg_entry));
    241  1.6      fvdl 
    242  1.6      fvdl 		a->au_id = AUX_phdr;
    243  1.6      fvdl 		a->au_v = ap->arg_phaddr;
    244  1.6      fvdl 		a++;
    245  1.6      fvdl 
    246  1.6      fvdl 		a->au_id = AUX_phent;
    247  1.6      fvdl 		a->au_v = ap->arg_phentsize;
    248  1.6      fvdl 		a++;
    249  1.6      fvdl 
    250  1.6      fvdl 		a->au_id = AUX_phnum;
    251  1.6      fvdl 		a->au_v = ap->arg_phnum;
    252  1.6      fvdl 		a++;
    253  1.6      fvdl 
    254  1.6      fvdl 		a->au_id = AUX_pagesz;
    255  1.6      fvdl 		a->au_v = NBPG;
    256  1.6      fvdl 		a++;
    257  1.6      fvdl 
    258  1.6      fvdl 		a->au_id = AUX_base;
    259  1.6      fvdl 		a->au_v = ap->arg_interp;
    260  1.6      fvdl 		a++;
    261  1.6      fvdl 
    262  1.6      fvdl 		a->au_id = AUX_flags;
    263  1.6      fvdl 		a->au_v = 0;
    264  1.6      fvdl 		a++;
    265  1.6      fvdl 
    266  1.6      fvdl 		a->au_id = AUX_entry;
    267  1.6      fvdl 		a->au_v = ap->arg_entry;
    268  1.6      fvdl 		a++;
    269  1.6      fvdl 
    270  1.6      fvdl 		a->au_id = AUX_null;
    271  1.6      fvdl 		a->au_v = 0;
    272  1.6      fvdl 		a++;
    273  1.6      fvdl 
    274  1.6      fvdl 		free((char *) ap, M_TEMP);
    275  1.6      fvdl 	}
    276  1.6      fvdl 	return a;
    277  1.6      fvdl }
    278  1.6      fvdl 
    279  1.6      fvdl #ifdef DEBUG_EXEC_LINUX_ELF
    280  1.6      fvdl static void
    281  1.6      fvdl print_Ehdr(e)
    282  1.6      fvdl 	Elf32_Ehdr     *e;
    283  1.6      fvdl {
    284  1.6      fvdl 	printf("e_ident %s, ", e->e_ident);
    285  1.6      fvdl 	printf("e_type %d, ", e->e_type);
    286  1.6      fvdl 	printf("e_machine %d, ", e->e_machine);
    287  1.6      fvdl 	printf("e_version %ld, ", e->e_version);
    288  1.6      fvdl 	printf("e_entry %lx, ", e->e_entry);
    289  1.6      fvdl 	printf("e_phoff %lx, ", e->e_phoff);
    290  1.6      fvdl 	printf("e_shoff %lx, ", e->e_shoff);
    291  1.6      fvdl 	printf("e_flags %lx, ", e->e_flags);
    292  1.6      fvdl 	printf("e_ehsize %d, ", e->e_ehsize);
    293  1.6      fvdl 	printf("e_phentsize %d, ", e->e_phentsize);
    294  1.6      fvdl 	printf("e_phnum %d, ", e->e_phnum);
    295  1.6      fvdl 	printf("e_shentsize %d, ", e->e_shentsize);
    296  1.6      fvdl 	printf("e_shnum %d, ", e->e_shnum);
    297  1.6      fvdl 	printf("e_shstrndx %d\n", e->e_shstrndx);
    298  1.6      fvdl }
    299  1.6      fvdl 
    300  1.6      fvdl 
    301  1.6      fvdl static void
    302  1.6      fvdl print_Phdr(p)
    303  1.6      fvdl 	Elf32_Phdr     *p;
    304  1.6      fvdl {
    305  1.6      fvdl 	static char    *types[] =
    306  1.6      fvdl 	{
    307  1.6      fvdl 		"null", "load", "dynamic", "interp",
    308  1.6      fvdl 		"note", "shlib", "phdr", "entry7"
    309  1.6      fvdl 	};
    310  1.6      fvdl 
    311  1.6      fvdl 	printf("p_type %ld [%s], ", p->p_type, types[p->p_type & 7]);
    312  1.6      fvdl 	printf("p_offset %lx, ", p->p_offset);
    313  1.6      fvdl 	printf("p_vaddr %lx, ", p->p_vaddr);
    314  1.6      fvdl 	printf("p_paddr %lx, ", p->p_paddr);
    315  1.6      fvdl 	printf("p_filesz %ld, ", p->p_filesz);
    316  1.6      fvdl 	printf("p_memsz %ld, ", p->p_memsz);
    317  1.6      fvdl 	printf("p_flags %lx, ", p->p_flags);
    318  1.6      fvdl 	printf("p_align %ld\n", p->p_align);
    319  1.6      fvdl }
    320  1.6      fvdl #endif
    321  1.4  christos 
    322  1.1      fvdl int
    323  1.1      fvdl exec_linux_aout_makecmds(p, epp)
    324  1.1      fvdl 	struct proc *p;
    325  1.1      fvdl 	struct exec_package *epp;
    326  1.1      fvdl {
    327  1.1      fvdl 	struct exec *linux_ep = epp->ep_hdr;
    328  1.1      fvdl 	int machtype, magic;
    329  1.1      fvdl 	int error = ENOEXEC;
    330  1.1      fvdl 
    331  1.1      fvdl 	magic = LINUX_N_MAGIC(linux_ep);
    332  1.1      fvdl 	machtype = LINUX_N_MACHTYPE(linux_ep);
    333  1.1      fvdl 
    334  1.1      fvdl 
    335  1.1      fvdl 	if (machtype != LINUX_MID_MACHINE)
    336  1.1      fvdl 		return (ENOEXEC);
    337  1.1      fvdl 
    338  1.1      fvdl 	switch (magic) {
    339  1.1      fvdl 	case QMAGIC:
    340  1.1      fvdl 		error = exec_linux_aout_prep_qmagic(p, epp);
    341  1.1      fvdl 		break;
    342  1.1      fvdl 	case ZMAGIC:
    343  1.1      fvdl 		error = exec_linux_aout_prep_zmagic(p, epp);
    344  1.1      fvdl 		break;
    345  1.1      fvdl 	case NMAGIC:
    346  1.1      fvdl 		error = exec_linux_aout_prep_nmagic(p, epp);
    347  1.1      fvdl 		break;
    348  1.1      fvdl 	case OMAGIC:
    349  1.1      fvdl 		error = exec_linux_aout_prep_omagic(p, epp);
    350  1.1      fvdl 		break;
    351  1.1      fvdl 	}
    352  1.4  christos 	if (error == 0)
    353  1.6      fvdl 		epp->ep_emul = &emul_linux_aout;
    354  1.1      fvdl 	return error;
    355  1.1      fvdl }
    356  1.1      fvdl 
    357  1.1      fvdl /*
    358  1.1      fvdl  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    359  1.1      fvdl  * is very likely not page aligned on most architectures, it is treated
    360  1.1      fvdl  * as an NMAGIC here. XXX
    361  1.1      fvdl  */
    362  1.1      fvdl 
    363  1.1      fvdl int
    364  1.1      fvdl exec_linux_aout_prep_zmagic(p, epp)
    365  1.1      fvdl 	struct proc *p;
    366  1.1      fvdl 	struct exec_package *epp;
    367  1.1      fvdl {
    368  1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    369  1.1      fvdl 
    370  1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    371  1.1      fvdl 	epp->ep_tsize = execp->a_text;
    372  1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    373  1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    374  1.1      fvdl 	epp->ep_entry = execp->a_entry;
    375  1.1      fvdl 
    376  1.1      fvdl 	/* set up command for text segment */
    377  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    378  1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    379  1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    380  1.1      fvdl 
    381  1.1      fvdl 	/* set up command for data segment */
    382  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    383  1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    384  1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    385  1.1      fvdl 
    386  1.1      fvdl 	/* set up command for bss segment */
    387  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    388  1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    389  1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    390  1.1      fvdl 
    391  1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    392  1.1      fvdl }
    393  1.1      fvdl 
    394  1.1      fvdl /*
    395  1.1      fvdl  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    396  1.1      fvdl  * Not different from the normal stuff.
    397  1.1      fvdl  */
    398  1.1      fvdl 
    399  1.1      fvdl int
    400  1.1      fvdl exec_linux_aout_prep_nmagic(p, epp)
    401  1.1      fvdl 	struct proc *p;
    402  1.1      fvdl 	struct exec_package *epp;
    403  1.1      fvdl {
    404  1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    405  1.1      fvdl 	long bsize, baddr;
    406  1.1      fvdl 
    407  1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    408  1.1      fvdl 	epp->ep_tsize = execp->a_text;
    409  1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    410  1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    411  1.1      fvdl 	epp->ep_entry = execp->a_entry;
    412  1.1      fvdl 
    413  1.1      fvdl 	/* set up command for text segment */
    414  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    415  1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    416  1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    417  1.1      fvdl 
    418  1.1      fvdl 	/* set up command for data segment */
    419  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    420  1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    421  1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    422  1.1      fvdl 
    423  1.1      fvdl 	/* set up command for bss segment */
    424  1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    425  1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    426  1.1      fvdl 	if (bsize > 0)
    427  1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    428  1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    429  1.1      fvdl 
    430  1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    431  1.1      fvdl }
    432  1.1      fvdl 
    433  1.1      fvdl /*
    434  1.1      fvdl  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    435  1.1      fvdl  * Business as usual.
    436  1.1      fvdl  */
    437  1.1      fvdl 
    438  1.1      fvdl int
    439  1.1      fvdl exec_linux_aout_prep_omagic(p, epp)
    440  1.1      fvdl 	struct proc *p;
    441  1.1      fvdl 	struct exec_package *epp;
    442  1.1      fvdl {
    443  1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    444  1.1      fvdl 	long dsize, bsize, baddr;
    445  1.1      fvdl 
    446  1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    447  1.1      fvdl 	epp->ep_tsize = execp->a_text;
    448  1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    449  1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    450  1.1      fvdl 	epp->ep_entry = execp->a_entry;
    451  1.1      fvdl 
    452  1.1      fvdl 	/* set up command for text and data segments */
    453  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    454  1.1      fvdl 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    455  1.1      fvdl 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    456  1.1      fvdl 
    457  1.1      fvdl 	/* set up command for bss segment */
    458  1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    459  1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    460  1.1      fvdl 	if (bsize > 0)
    461  1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    462  1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    463  1.1      fvdl 
    464  1.1      fvdl 	/*
    465  1.1      fvdl 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    466  1.1      fvdl 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    467  1.1      fvdl 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    468  1.1      fvdl 	 * respectively to page boundaries.
    469  1.1      fvdl 	 * Compensate `ep_dsize' for the amount of data covered by the last
    470  1.1      fvdl 	 * text page.
    471  1.1      fvdl 	 */
    472  1.1      fvdl 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    473  1.1      fvdl 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    474  1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    475  1.1      fvdl }
    476  1.1      fvdl 
    477  1.1      fvdl int
    478  1.1      fvdl exec_linux_aout_prep_qmagic(p, epp)
    479  1.1      fvdl 	struct proc *p;
    480  1.1      fvdl 	struct exec_package *epp;
    481  1.1      fvdl {
    482  1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    483  1.1      fvdl 
    484  1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    485  1.1      fvdl 	epp->ep_tsize = execp->a_text;
    486  1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    487  1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    488  1.1      fvdl 	epp->ep_entry = execp->a_entry;
    489  1.1      fvdl 
    490  1.1      fvdl 	/*
    491  1.1      fvdl 	 * check if vnode is in open for writing, because we want to
    492  1.1      fvdl 	 * demand-page out of it.  if it is, don't do it, for various
    493  1.1      fvdl 	 * reasons
    494  1.1      fvdl 	 */
    495  1.1      fvdl 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    496  1.1      fvdl 	    epp->ep_vp->v_writecount != 0) {
    497  1.1      fvdl #ifdef DIAGNOSTIC
    498  1.1      fvdl 		if (epp->ep_vp->v_flag & VTEXT)
    499  1.1      fvdl 			panic("exec: a VTEXT vnode has writecount != 0\n");
    500  1.1      fvdl #endif
    501  1.1      fvdl 		return ETXTBSY;
    502  1.1      fvdl 	}
    503  1.1      fvdl 	epp->ep_vp->v_flag |= VTEXT;
    504  1.1      fvdl 
    505  1.1      fvdl 	/* set up command for text segment */
    506  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    507  1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    508  1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    509  1.1      fvdl 
    510  1.1      fvdl 	/* set up command for data segment */
    511  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    512  1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    513  1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    514  1.1      fvdl 
    515  1.1      fvdl 	/* set up command for bss segment */
    516  1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    517  1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    518  1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    519  1.1      fvdl 
    520  1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    521  1.1      fvdl }
    522  1.1      fvdl 
    523  1.1      fvdl /*
    524  1.6      fvdl  * linux_elf_check_header():
    525  1.6      fvdl  *
    526  1.6      fvdl  * Check header for validity; return 0 of ok ENOEXEC if error
    527  1.6      fvdl  */
    528  1.6      fvdl static int
    529  1.6      fvdl linux_elf_check_header(eh, type)
    530  1.6      fvdl 	Elf32_Ehdr     *eh;
    531  1.6      fvdl 	int             type;
    532  1.6      fvdl {
    533  1.6      fvdl #ifdef sparc
    534  1.6      fvdl   /* #$%@#$%@#$%! */
    535  1.6      fvdl # define memcmp bcmp
    536  1.6      fvdl #endif
    537  1.6      fvdl 	if (memcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0) {
    538  1.6      fvdl 		DPRINTF(("Not an elf file\n"));
    539  1.6      fvdl 		return ENOEXEC;
    540  1.6      fvdl 	}
    541  1.6      fvdl 
    542  1.6      fvdl 	switch (eh->e_machine) {
    543  1.6      fvdl #ifdef i386
    544  1.6      fvdl 	case Elf32_em_386:
    545  1.6      fvdl 	case Elf32_em_486:
    546  1.6      fvdl #endif
    547  1.6      fvdl #ifdef sparc
    548  1.6      fvdl 	case Elf32_em_sparc:
    549  1.6      fvdl #endif
    550  1.6      fvdl 		break;
    551  1.6      fvdl 
    552  1.6      fvdl 	default:
    553  1.6      fvdl 		DPRINTF(("Unsupported elf machine type %d\n", eh->e_machine));
    554  1.6      fvdl 		return ENOEXEC;
    555  1.6      fvdl 	}
    556  1.6      fvdl 
    557  1.6      fvdl 	if (eh->e_type != type) {
    558  1.6      fvdl 		DPRINTF(("Not an elf executable\n"));
    559  1.6      fvdl 		return ENOEXEC;
    560  1.6      fvdl 	}
    561  1.6      fvdl 
    562  1.6      fvdl 	return 0;
    563  1.6      fvdl }
    564  1.6      fvdl 
    565  1.6      fvdl 
    566  1.6      fvdl /*
    567  1.6      fvdl  * linux_elf_load_psection():
    568  1.6      fvdl  *
    569  1.6      fvdl  * Load a psection at the appropriate address
    570  1.6      fvdl  */
    571  1.6      fvdl static void
    572  1.6      fvdl linux_elf_load_psection(vcset, vp, ph, addr, size, prot)
    573  1.6      fvdl 	struct exec_vmcmd_set   *vcset;
    574  1.6      fvdl 	struct vnode		*vp;
    575  1.6      fvdl 	Elf32_Phdr		*ph;
    576  1.6      fvdl 	u_long			*addr;
    577  1.6      fvdl 	u_long			*size;
    578  1.6      fvdl 	int			*prot;
    579  1.6      fvdl {
    580  1.6      fvdl 	u_long	uaddr;
    581  1.6      fvdl 	long	diff;
    582  1.6      fvdl 	long	offset;
    583  1.6      fvdl 	u_long	msize;
    584  1.6      fvdl 
    585  1.6      fvdl 	/*
    586  1.6      fvdl          * If the user specified an address, then we load there.
    587  1.6      fvdl          */
    588  1.6      fvdl 	if (*addr != ~0) {
    589  1.6      fvdl 		uaddr = *addr + ph->p_align;
    590  1.6      fvdl 		*addr = LINUX_ELF_ALIGN(uaddr, ph->p_align);
    591  1.6      fvdl 		uaddr = LINUX_ELF_ALIGN(ph->p_vaddr, ph->p_align);
    592  1.6      fvdl 		diff = ph->p_vaddr - uaddr;
    593  1.6      fvdl 	} else {
    594  1.6      fvdl 		uaddr = ph->p_vaddr;
    595  1.6      fvdl 		*addr = LINUX_ELF_ALIGN(uaddr, ph->p_align);
    596  1.6      fvdl 		diff = uaddr - *addr;
    597  1.6      fvdl 	}
    598  1.6      fvdl 
    599  1.6      fvdl 	*prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
    600  1.6      fvdl 	*prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
    601  1.6      fvdl 	*prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
    602  1.6      fvdl 
    603  1.6      fvdl 	offset = ph->p_offset - diff;
    604  1.6      fvdl 	*size = ph->p_filesz + diff;
    605  1.6      fvdl 	msize = ph->p_memsz + diff;
    606  1.6      fvdl 
    607  1.6      fvdl 	DPRINTF(("Elf Seg@ 0x%x/0x%x sz %d/%d off 0x%x/0x%x[%d] algn 0x%x\n",
    608  1.6      fvdl 		 ph->p_vaddr, *addr, *size, msize, ph->p_offset, offset,
    609  1.6      fvdl 		 diff, ph->p_align));
    610  1.6      fvdl 
    611  1.6      fvdl 	NEW_VMCMD(vcset, vmcmd_map_readvn, *size,
    612  1.6      fvdl 		  *addr, vp, offset, *prot);
    613  1.6      fvdl 
    614  1.6      fvdl 	/*
    615  1.6      fvdl          * Check if we need to extend the size of the segment
    616  1.6      fvdl          */
    617  1.6      fvdl 	{
    618  1.6      fvdl 		u_long	rm = round_page(*addr + msize);
    619  1.6      fvdl 		u_long	rf = round_page(*addr + *size);
    620  1.6      fvdl 		if (rm != rf) {
    621  1.6      fvdl 			DPRINTF(("zeropad 0x%x-0x%x\n", rf, rm));
    622  1.6      fvdl 			NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf,
    623  1.6      fvdl 				  rf, NULLVP, 0, *prot);
    624  1.6      fvdl 			*size = msize;
    625  1.6      fvdl 		}
    626  1.6      fvdl 	}
    627  1.6      fvdl }
    628  1.6      fvdl 
    629  1.6      fvdl 
    630  1.6      fvdl /*
    631  1.6      fvdl  * linux_elf_set_segment():
    632  1.6      fvdl  *
    633  1.6      fvdl  * Decide if the segment is text or data, depending on the protection
    634  1.6      fvdl  * and set it appropriately
    635  1.6      fvdl  */
    636  1.6      fvdl static int
    637  1.6      fvdl linux_elf_set_segment(epp, vaddr, size, prot)
    638  1.6      fvdl 	struct exec_package	*epp;
    639  1.6      fvdl 	u_long			 vaddr;
    640  1.6      fvdl 	u_long			 size;
    641  1.6      fvdl 	int			 prot;
    642  1.6      fvdl {
    643  1.6      fvdl 	/*
    644  1.6      fvdl          * Kludge: Unfortunately the current implementation of
    645  1.6      fvdl          * exec package assumes a single text and data segment.
    646  1.6      fvdl          * In Elf we can have more, but here we limit ourselves
    647  1.6      fvdl          * to two and hope :-(
    648  1.6      fvdl          * We also assume that the text is r-x, and data is rwx.
    649  1.6      fvdl          */
    650  1.6      fvdl 	switch (prot) {
    651  1.6      fvdl 	case (VM_PROT_READ | VM_PROT_EXECUTE):
    652  1.6      fvdl 		if (epp->ep_tsize != ~0) {
    653  1.6      fvdl 			DPRINTF(("More than one text segment\n"));
    654  1.6      fvdl 			return ENOEXEC;
    655  1.6      fvdl 		}
    656  1.6      fvdl 		epp->ep_taddr = vaddr;
    657  1.6      fvdl 		epp->ep_tsize = size;
    658  1.6      fvdl 		DPRINTF(("Elf Text@ 0x%x, size %d\n", vaddr, size));
    659  1.6      fvdl 		break;
    660  1.6      fvdl 
    661  1.6      fvdl 	case (VM_PROT_READ | VM_PROT_WRITE):
    662  1.6      fvdl 	case (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE):
    663  1.6      fvdl 		if (epp->ep_dsize != ~0) {
    664  1.6      fvdl 			DPRINTF(("More than one data segment\n"));
    665  1.6      fvdl 			return ENOEXEC;
    666  1.6      fvdl 		}
    667  1.6      fvdl 		epp->ep_daddr = vaddr;
    668  1.6      fvdl 		epp->ep_dsize = size;
    669  1.6      fvdl 
    670  1.6      fvdl 		DPRINTF(("Elf Data@ 0x%x, size %d\n", vaddr, size));
    671  1.6      fvdl 		break;
    672  1.6      fvdl 
    673  1.6      fvdl 	default:
    674  1.6      fvdl 		DPRINTF(("Bad protection 0%o\n", prot));
    675  1.6      fvdl 		return ENOEXEC;
    676  1.6      fvdl 	}
    677  1.6      fvdl 	return 0;
    678  1.6      fvdl }
    679  1.6      fvdl 
    680  1.6      fvdl 
    681  1.6      fvdl /*
    682  1.6      fvdl  * linux_elf_read_from():
    683  1.6      fvdl  *
    684  1.6      fvdl  *	Read from vnode into buffer at offset.
    685  1.6      fvdl  */
    686  1.6      fvdl static int
    687  1.6      fvdl linux_elf_read_from(vp, off, p, buf, size)
    688  1.6      fvdl 	struct vnode	*vp;
    689  1.6      fvdl 	u_long		 off;
    690  1.6      fvdl 	struct proc	*p;
    691  1.6      fvdl 	caddr_t		 buf;
    692  1.6      fvdl 	int		 size;
    693  1.6      fvdl {
    694  1.6      fvdl 	int	error;
    695  1.6      fvdl 	int	resid;
    696  1.6      fvdl 
    697  1.6      fvdl 	DPRINTF(("read from 0x%x to 0x%x size %d\n",
    698  1.6      fvdl 		 off, buf, size));
    699  1.6      fvdl 	if ((error = vn_rdwr(UIO_READ, vp, buf, size,
    700  1.6      fvdl 			     off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
    701  1.6      fvdl 			     &resid, p)) != 0) {
    702  1.6      fvdl 		DPRINTF(("Bad read error %d\n", error));
    703  1.6      fvdl 		return error;
    704  1.6      fvdl 	}
    705  1.6      fvdl 	/*
    706  1.6      fvdl          * See if we got all of it
    707  1.6      fvdl          */
    708  1.6      fvdl 	if (resid != 0) {
    709  1.6      fvdl 		DPRINTF(("Incomplete read for header ask=%d, rem=%d\n",
    710  1.6      fvdl 			 size, resid));
    711  1.6      fvdl 		return error;
    712  1.6      fvdl 	}
    713  1.6      fvdl 	return 0;
    714  1.6      fvdl }
    715  1.6      fvdl 
    716  1.6      fvdl 
    717  1.6      fvdl /*
    718  1.6      fvdl  * linux_elf_load_file():
    719  1.6      fvdl  *
    720  1.6      fvdl  * Load a file (interpreter/library) pointed to by path
    721  1.6      fvdl  * [stolen from coff_load_shlib()]. Made slightly more generic than
    722  1.6      fvdl  * the svr4 version, for possible later use in linux_uselib().
    723  1.6      fvdl  */
    724  1.6      fvdl static int
    725  1.6      fvdl linux_elf_load_file(p, path, vcset, entry, ap, last)
    726  1.6      fvdl 	struct proc		*p;
    727  1.6      fvdl 	char			*path;
    728  1.6      fvdl 	struct exec_vmcmd_set   *vcset;
    729  1.6      fvdl 	u_long			*entry;
    730  1.6      fvdl 	struct elf_args		*ap;
    731  1.6      fvdl 	u_long			*last;
    732  1.6      fvdl {
    733  1.6      fvdl 	int			 error, i;
    734  1.6      fvdl 	struct nameidata	 nd;
    735  1.6      fvdl 	Elf32_Ehdr		 eh;
    736  1.6      fvdl 	Elf32_Phdr		*ph = NULL;
    737  1.6      fvdl 	u_long			 phsize;
    738  1.6      fvdl 	char			*bp = NULL;
    739  1.6      fvdl 	u_long			 addr = *last;
    740  1.6      fvdl 
    741  1.6      fvdl 	DPRINTF(("Loading file %s @ %x\n", path, addr));
    742  1.6      fvdl 
    743  1.6      fvdl 	if ((error = linux_emul_find(p, NULL, linux_emul_path, path, &bp, 0)) != 0)
    744  1.6      fvdl 		bp = NULL;
    745  1.6      fvdl 	else
    746  1.6      fvdl 		path = bp;
    747  1.6      fvdl 	/*
    748  1.6      fvdl          * 1. open file
    749  1.6      fvdl          * 2. read filehdr
    750  1.6      fvdl          * 3. map text, data, and bss out of it using VM_*
    751  1.6      fvdl          */
    752  1.6      fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
    753  1.6      fvdl 	/* first get the vnode */
    754  1.6      fvdl 	if ((error = namei(&nd)) != 0) {
    755  1.6      fvdl 		if (bp != NULL)
    756  1.6      fvdl 			free((char *) bp, M_TEMP);
    757  1.6      fvdl 		return error;
    758  1.6      fvdl 	}
    759  1.6      fvdl 	if ((error = linux_elf_read_from(nd.ni_vp, 0, p, (caddr_t) &eh,
    760  1.6      fvdl 				    sizeof(eh))) != 0)
    761  1.6      fvdl 		goto bad;
    762  1.6      fvdl 
    763  1.6      fvdl #ifdef DEBUG_EXEC_LINUX_ELF
    764  1.6      fvdl 	print_Ehdr(&eh);
    765  1.6      fvdl #endif
    766  1.6      fvdl 
    767  1.6      fvdl 	if ((error = linux_elf_check_header(&eh, Elf32_et_dyn)) != 0)
    768  1.6      fvdl 		goto bad;
    769  1.6      fvdl 
    770  1.6      fvdl 	phsize = eh.e_phnum * sizeof(Elf32_Phdr);
    771  1.6      fvdl 	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
    772  1.6      fvdl 
    773  1.6      fvdl 	if ((error = linux_elf_read_from(nd.ni_vp, eh.e_phoff, p,
    774  1.6      fvdl 				    (caddr_t) ph, phsize)) != 0)
    775  1.6      fvdl 		goto bad;
    776  1.6      fvdl 
    777  1.6      fvdl 	/*
    778  1.6      fvdl          * Load all the necessary sections
    779  1.6      fvdl          */
    780  1.6      fvdl 	for (i = 0; i < eh.e_phnum; i++) {
    781  1.6      fvdl 		u_long	size = 0;
    782  1.6      fvdl 		int	prot = 0;
    783  1.6      fvdl #ifdef DEBUG_EXEC_LINUX_ELF
    784  1.6      fvdl 		print_Phdr(&ph[i]);
    785  1.6      fvdl #endif
    786  1.6      fvdl 
    787  1.6      fvdl 		switch (ph[i].p_type) {
    788  1.6      fvdl 		case Elf32_pt_load:
    789  1.6      fvdl 			linux_elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
    790  1.6      fvdl 						&size, &prot);
    791  1.6      fvdl 			/* Assume that the text segment is r-x only */
    792  1.6      fvdl 			if ((prot & PROT_WRITE) == 0) {
    793  1.6      fvdl 				*entry = addr + eh.e_entry;
    794  1.6      fvdl 				ap->arg_interp = addr;
    795  1.6      fvdl 				DPRINTF(("Interpreter@ 0x%x\n", addr));
    796  1.6      fvdl 			}
    797  1.6      fvdl 			addr += size;
    798  1.6      fvdl 			break;
    799  1.6      fvdl 
    800  1.6      fvdl 		case Elf32_pt_dynamic:
    801  1.6      fvdl 		case Elf32_pt_phdr:
    802  1.6      fvdl 		case Elf32_pt_note:
    803  1.6      fvdl 			break;
    804  1.6      fvdl 
    805  1.6      fvdl 		default:
    806  1.6      fvdl 			DPRINTF(("interp: Unexpected program header type %d\n",
    807  1.6      fvdl 				 ph[i].p_type));
    808  1.6      fvdl 			break;
    809  1.6      fvdl 		}
    810  1.6      fvdl 	}
    811  1.6      fvdl 
    812  1.6      fvdl bad:
    813  1.6      fvdl 	if (ph != NULL)
    814  1.6      fvdl 		free((char *) ph, M_TEMP);
    815  1.6      fvdl 	if (bp != NULL)
    816  1.6      fvdl 		free((char *) bp, M_TEMP);
    817  1.6      fvdl 
    818  1.6      fvdl 	*last = addr;
    819  1.6      fvdl 	vrele(nd.ni_vp);
    820  1.6      fvdl 	return error;
    821  1.6      fvdl }
    822  1.6      fvdl 
    823  1.6      fvdl 
    824  1.6      fvdl /*
    825  1.6      fvdl  * exec_linux_elf_makecmds(): Prepare an Elf binary's exec package
    826  1.6      fvdl  *
    827  1.6      fvdl  * First, set of the various offsets/lengths in the exec package.
    828  1.6      fvdl  *
    829  1.6      fvdl  * Then, mark the text image busy (so it can be demand paged) or error
    830  1.6      fvdl  * out if this is not possible.  Finally, set up vmcmds for the
    831  1.6      fvdl  * text, data, bss, and stack segments.
    832  1.6      fvdl  */
    833  1.6      fvdl int
    834  1.6      fvdl exec_linux_elf_makecmds(p, epp)
    835  1.6      fvdl 	struct proc		*p;
    836  1.6      fvdl 	struct exec_package	*epp;
    837  1.6      fvdl {
    838  1.6      fvdl 	Elf32_Ehdr     *eh = epp->ep_hdr;
    839  1.6      fvdl 	Elf32_Phdr     *ph, *pp;
    840  1.6      fvdl 	int             error;
    841  1.6      fvdl 	int             i;
    842  1.6      fvdl 	char            interp[MAXPATHLEN];
    843  1.6      fvdl 	u_long          pos = 0;
    844  1.6      fvdl 	u_long          phsize;
    845  1.6      fvdl 
    846  1.6      fvdl #ifdef DEBUG_EXEC_LINUX_ELF
    847  1.6      fvdl 	print_Ehdr(eh);
    848  1.6      fvdl #endif
    849  1.6      fvdl 	if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
    850  1.6      fvdl 		return ENOEXEC;
    851  1.6      fvdl 
    852  1.6      fvdl 	if (linux_elf_check_header(eh, Elf32_et_exec))
    853  1.6      fvdl 		return ENOEXEC;
    854  1.6      fvdl 
    855  1.6      fvdl 	/*
    856  1.6      fvdl          * check if vnode is in open for writing, because we want to
    857  1.6      fvdl          * demand-page out of it.  if it is, don't do it, for various
    858  1.6      fvdl          * reasons
    859  1.6      fvdl          */
    860  1.6      fvdl 	if (epp->ep_vp->v_writecount != 0) {
    861  1.6      fvdl #ifdef DIAGNOSTIC
    862  1.6      fvdl 		if (epp->ep_vp->v_flag & VTEXT)
    863  1.6      fvdl 			panic("exec: a VTEXT vnode has writecount != 0\n");
    864  1.6      fvdl #endif
    865  1.6      fvdl 		return ETXTBSY;
    866  1.6      fvdl 	}
    867  1.6      fvdl 	/*
    868  1.6      fvdl          * Allocate space to hold all the program headers, and read them
    869  1.6      fvdl          * from the file
    870  1.6      fvdl          */
    871  1.6      fvdl 	phsize = eh->e_phnum * sizeof(Elf32_Phdr);
    872  1.6      fvdl 	ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
    873  1.6      fvdl 
    874  1.6      fvdl 	if ((error = linux_elf_read_from(epp->ep_vp, eh->e_phoff, p,
    875  1.6      fvdl 				    (caddr_t) ph, phsize)) != 0)
    876  1.6      fvdl 		goto bad;
    877  1.6      fvdl 
    878  1.6      fvdl 	epp->ep_tsize = ~0;
    879  1.6      fvdl 	epp->ep_dsize = ~0;
    880  1.6      fvdl 
    881  1.6      fvdl 	interp[0] = '\0';
    882  1.6      fvdl 
    883  1.6      fvdl 	/*
    884  1.6      fvdl          * Load all the necessary sections
    885  1.6      fvdl          */
    886  1.6      fvdl 	for (i = 0; i < eh->e_phnum; i++) {
    887  1.6      fvdl 		u_long          addr = ~0, size = 0;
    888  1.6      fvdl 		int             prot = 0;
    889  1.6      fvdl 
    890  1.6      fvdl 		pp = &ph[i];
    891  1.6      fvdl #ifdef DEBUG_EXEC_LINUX_ELF
    892  1.6      fvdl 		print_Phdr(pp);
    893  1.6      fvdl #endif
    894  1.6      fvdl 
    895  1.6      fvdl 		switch (ph[i].p_type) {
    896  1.6      fvdl 		case Elf32_pt_load:
    897  1.6      fvdl 			linux_elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
    898  1.6      fvdl 				&ph[i], &addr, &size, &prot);
    899  1.6      fvdl 			if ((error = linux_elf_set_segment(epp, addr, size,
    900  1.6      fvdl 						      prot)) != 0)
    901  1.6      fvdl 				goto bad;
    902  1.6      fvdl 			break;
    903  1.6      fvdl 
    904  1.6      fvdl 		case Elf32_pt_shlib:
    905  1.6      fvdl 			DPRINTF(("No support for COFF libraries (yet)\n"));
    906  1.6      fvdl 			error = ENOEXEC;
    907  1.6      fvdl 			goto bad;
    908  1.6      fvdl 
    909  1.6      fvdl 		case Elf32_pt_interp:
    910  1.6      fvdl 			if (pp->p_filesz >= sizeof(interp)) {
    911  1.6      fvdl 				DPRINTF(("Interpreter path too long %d\n",
    912  1.6      fvdl 					 pp->p_filesz));
    913  1.6      fvdl 				goto bad;
    914  1.6      fvdl 			}
    915  1.6      fvdl 			if ((error = linux_elf_read_from(epp->ep_vp, pp->p_offset, p,
    916  1.6      fvdl 				      (caddr_t) interp, pp->p_filesz)) != 0)
    917  1.6      fvdl 				goto bad;
    918  1.6      fvdl 			break;
    919  1.6      fvdl 
    920  1.6      fvdl 		case Elf32_pt_dynamic:
    921  1.6      fvdl 		case Elf32_pt_phdr:
    922  1.6      fvdl 		case Elf32_pt_note:
    923  1.6      fvdl 			break;
    924  1.6      fvdl 
    925  1.6      fvdl 		default:
    926  1.6      fvdl 			/*
    927  1.6      fvdl 			 * Not fatal, we don't need to understand everything
    928  1.6      fvdl 			 * :-)
    929  1.6      fvdl 			 */
    930  1.6      fvdl 			DPRINTF(("Unsupported program header type %d\n",
    931  1.6      fvdl 				 pp->p_type));
    932  1.6      fvdl 			break;
    933  1.6      fvdl 		}
    934  1.6      fvdl 	}
    935  1.6      fvdl 
    936  1.6      fvdl 	/*
    937  1.6      fvdl          * Check if we found a dynamically linked binary and arrange to load
    938  1.6      fvdl          * it's interpreter
    939  1.6      fvdl          */
    940  1.6      fvdl 	if (interp[0]) {
    941  1.6      fvdl 		struct elf_args *ap;
    942  1.6      fvdl 		pos = ~0;
    943  1.6      fvdl 
    944  1.6      fvdl 		ap = (struct elf_args *) malloc(sizeof(struct elf_args),
    945  1.6      fvdl 						 M_TEMP, M_WAITOK);
    946  1.6      fvdl 		if ((error = linux_elf_load_file(p, interp, &epp->ep_vmcmds,
    947  1.6      fvdl 				&epp->ep_entry, ap, &pos)) != 0) {
    948  1.6      fvdl 			free((char *) ap, M_TEMP);
    949  1.6      fvdl 			goto bad;
    950  1.6      fvdl 		}
    951  1.6      fvdl 		/* Arrange to load the program headers. */
    952  1.6      fvdl 		pos = LINUX_ELF_ALIGN(pos + NBPG, NBPG);
    953  1.6      fvdl 		DPRINTF(("Program header @0x%x\n", pos));
    954  1.6      fvdl 		ap->arg_phaddr = pos;
    955  1.6      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, phsize,
    956  1.6      fvdl 			  pos, epp->ep_vp, eh->e_phoff,
    957  1.6      fvdl 			  VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    958  1.6      fvdl 		pos += phsize;
    959  1.6      fvdl 
    960  1.6      fvdl 		ap->arg_phentsize = eh->e_phentsize;
    961  1.6      fvdl 		ap->arg_phnum = eh->e_phnum;
    962  1.6      fvdl 		ap->arg_entry = eh->e_entry;
    963  1.6      fvdl 
    964  1.6      fvdl 		epp->ep_emul_arg = ap;
    965  1.6      fvdl 	} else
    966  1.6      fvdl 		epp->ep_entry = eh->e_entry;
    967  1.6      fvdl 
    968  1.6      fvdl 	DPRINTF(("taddr 0x%x tsize 0x%x daddr 0x%x dsize 0x%x\n",
    969  1.6      fvdl 	       epp->ep_taddr, epp->ep_tsize, epp->ep_daddr, epp->ep_dsize));
    970  1.6      fvdl 
    971  1.6      fvdl 	free((char *) ph, M_TEMP);
    972  1.6      fvdl 
    973  1.6      fvdl 	DPRINTF(("Elf entry@ 0x%x\n", epp->ep_entry));
    974  1.6      fvdl 	epp->ep_vp->v_flag |= VTEXT;
    975  1.6      fvdl 
    976  1.6      fvdl 	epp->ep_emul = &emul_linux_elf;
    977  1.6      fvdl 
    978  1.6      fvdl 	return exec_aout_setup_stack(p, epp);
    979  1.6      fvdl 
    980  1.6      fvdl bad:
    981  1.6      fvdl 	free((char *) ph, M_TEMP);
    982  1.6      fvdl 	kill_vmcmds(&epp->ep_vmcmds);
    983  1.6      fvdl 	return ENOEXEC;
    984  1.6      fvdl }
    985  1.6      fvdl /*
    986  1.6      fvdl  * The Linux system call to load shared libraries, a.out version. The
    987  1.6      fvdl  * a.out shared libs are just files that are mapped onto a fixed
    988  1.6      fvdl  * address in the process' address space. The address is given in
    989  1.1      fvdl  * a_entry. Read in the header, set up some VM commands and run them.
    990  1.1      fvdl  *
    991  1.1      fvdl  * Yes, both text and data are mapped at once, so we're left with
    992  1.1      fvdl  * writeable text for the shared libs. The Linux crt0 seemed to break
    993  1.1      fvdl  * sometimes when data was mapped seperately. It munmapped a uselib()
    994  1.1      fvdl  * of ld.so by hand, which failed with shared text and data for ld.so
    995  1.1      fvdl  * Yuck.
    996  1.1      fvdl  *
    997  1.1      fvdl  * Because of the problem with ZMAGIC executables (text starts
    998  1.6      fvdl  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
    999  1.1      fvdl  * shared libs are not handled very efficiently :-(
   1000  1.1      fvdl  */
   1001  1.1      fvdl 
   1002  1.1      fvdl int
   1003  1.1      fvdl linux_uselib(p, uap, retval)
   1004  1.1      fvdl 	struct proc *p;
   1005  1.1      fvdl 	struct linux_uselib_args /* {
   1006  1.1      fvdl 		syscallarg(char *) path;
   1007  1.1      fvdl 	} */ *uap;
   1008  1.1      fvdl 	register_t *retval;
   1009  1.1      fvdl {
   1010  1.1      fvdl 	caddr_t sg;
   1011  1.1      fvdl 	long bsize, dsize, tsize, taddr, baddr, daddr;
   1012  1.1      fvdl 	struct nameidata ni;
   1013  1.1      fvdl 	struct vnode *vp;
   1014  1.1      fvdl 	struct exec hdr;
   1015  1.1      fvdl 	struct exec_vmcmd_set vcset;
   1016  1.1      fvdl 	int rem, i, magic, error;
   1017  1.1      fvdl 
   1018  1.1      fvdl 	sg = stackgap_init();
   1019  1.2      fvdl 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
   1020  1.1      fvdl 
   1021  1.1      fvdl 	NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1022  1.1      fvdl 
   1023  1.1      fvdl 	if ((error = namei(&ni)))
   1024  1.1      fvdl 		return error;
   1025  1.1      fvdl 
   1026  1.1      fvdl 	vp = ni.ni_vp;
   1027  1.1      fvdl 
   1028  1.1      fvdl 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
   1029  1.1      fvdl 			     0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
   1030  1.1      fvdl 			     &rem, p))) {
   1031  1.1      fvdl 		vrele(vp);
   1032  1.1      fvdl 		return error;
   1033  1.1      fvdl 	}
   1034  1.1      fvdl 
   1035  1.1      fvdl 	if (rem != 0) {
   1036  1.1      fvdl 		vrele(vp);
   1037  1.1      fvdl 		return ENOEXEC;
   1038  1.1      fvdl 	}
   1039  1.6      fvdl 
   1040  1.6      fvdl 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
   1041  1.6      fvdl 		return ENOEXEC;
   1042  1.1      fvdl 
   1043  1.1      fvdl 	magic = LINUX_N_MAGIC(&hdr);
   1044  1.1      fvdl 	taddr = hdr.a_entry & (~(NBPG - 1));
   1045  1.1      fvdl 	tsize = hdr.a_text;
   1046  1.1      fvdl 	daddr = taddr + tsize;
   1047  1.1      fvdl 	dsize = hdr.a_data + hdr.a_bss;
   1048  1.1      fvdl 
   1049  1.1      fvdl 	if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
   1050  1.1      fvdl 		vrele(vp);
   1051  1.1      fvdl                 return ETXTBSY;
   1052  1.1      fvdl         }
   1053  1.1      fvdl 	vp->v_flag |= VTEXT;
   1054  1.1      fvdl 
   1055  1.1      fvdl 	vcset.evs_cnt = 0;
   1056  1.1      fvdl 	vcset.evs_used = 0;
   1057  1.1      fvdl 
   1058  1.1      fvdl 	NEW_VMCMD(&vcset,
   1059  1.1      fvdl 		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
   1060  1.1      fvdl 		  hdr.a_text + hdr.a_data, taddr,
   1061  1.1      fvdl 		  vp, LINUX_N_TXTOFF(hdr, magic),
   1062  1.1      fvdl 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
   1063  1.1      fvdl 
   1064  1.1      fvdl 	baddr = roundup(daddr + hdr.a_data, NBPG);
   1065  1.1      fvdl 	bsize = daddr + dsize - baddr;
   1066  1.1      fvdl         if (bsize > 0) {
   1067  1.1      fvdl                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
   1068  1.1      fvdl                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
   1069  1.1      fvdl 	}
   1070  1.1      fvdl 
   1071  1.1      fvdl 	for (i = 0; i < vcset.evs_used && !error; i++) {
   1072  1.1      fvdl 		struct exec_vmcmd *vcp;
   1073  1.1      fvdl 
   1074  1.1      fvdl 		vcp = &vcset.evs_cmds[i];
   1075  1.1      fvdl 		error = (*vcp->ev_proc)(p, vcp);
   1076  1.1      fvdl 	}
   1077  1.1      fvdl 
   1078  1.1      fvdl 	kill_vmcmds(&vcset);
   1079  1.1      fvdl 
   1080  1.1      fvdl 	vrele(vp);
   1081  1.1      fvdl 
   1082  1.1      fvdl 	return error;
   1083  1.1      fvdl }
   1084  1.1      fvdl 
   1085  1.1      fvdl /*
   1086  1.1      fvdl  * Execve(2). Just check the alternate emulation path, and pass it on
   1087  1.1      fvdl  * to the NetBSD execve().
   1088  1.1      fvdl  */
   1089  1.1      fvdl int
   1090  1.1      fvdl linux_execve(p, uap, retval)
   1091  1.1      fvdl 	struct proc *p;
   1092  1.1      fvdl 	struct linux_execve_args /* {
   1093  1.1      fvdl 		syscallarg(char *) path;
   1094  1.1      fvdl 		syscallarg(char **) argv;
   1095  1.1      fvdl 		syscallarg(char **) envp;
   1096  1.1      fvdl 	} */ *uap;
   1097  1.1      fvdl 	register_t *retval;
   1098  1.1      fvdl {
   1099  1.1      fvdl 	caddr_t sg;
   1100  1.1      fvdl 
   1101  1.1      fvdl 	sg = stackgap_init();
   1102  1.2      fvdl 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
   1103  1.1      fvdl 
   1104  1.1      fvdl 	return execve(p, uap, retval);
   1105  1.1      fvdl }
   1106