Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.25.14.1
      1  1.25.14.1    mellon /*	$NetBSD: linux_exec.c,v 1.25.14.1 1998/01/29 12:02:14 mellon Exp $	*/
      2        1.1      fvdl 
      3        1.1      fvdl /*
      4        1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
      5        1.7      fvdl  * Copyright (c) 1994 Christos Zoulas
      6        1.1      fvdl  * All rights reserved.
      7        1.1      fvdl  *
      8        1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      9        1.1      fvdl  * modification, are permitted provided that the following conditions
     10        1.1      fvdl  * are met:
     11        1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     12        1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     13        1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     15        1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     16        1.7      fvdl  * 3. The name of the author may not be used to endorse or promote products
     17        1.1      fvdl  *    derived from this software without specific prior written permission
     18        1.1      fvdl  *
     19        1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20        1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21        1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22        1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23        1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24        1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25        1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26        1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27        1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28        1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29        1.1      fvdl  *
     30        1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     31        1.1      fvdl  */
     32        1.1      fvdl 
     33       1.18       cgd #define	ELFSIZE		32				/* XXX should die */
     34       1.17       cgd 
     35        1.1      fvdl #include <sys/param.h>
     36        1.1      fvdl #include <sys/systm.h>
     37        1.1      fvdl #include <sys/kernel.h>
     38        1.1      fvdl #include <sys/proc.h>
     39        1.1      fvdl #include <sys/malloc.h>
     40        1.1      fvdl #include <sys/namei.h>
     41        1.1      fvdl #include <sys/vnode.h>
     42       1.13  christos #include <sys/mount.h>
     43       1.25  christos #include <sys/exec.h>
     44        1.8      fvdl #include <sys/exec_elf.h>
     45        1.1      fvdl 
     46        1.1      fvdl #include <sys/mman.h>
     47       1.13  christos #include <sys/syscallargs.h>
     48       1.13  christos 
     49        1.1      fvdl #include <vm/vm.h>
     50        1.1      fvdl #include <vm/vm_param.h>
     51        1.1      fvdl #include <vm/vm_map.h>
     52        1.1      fvdl 
     53        1.1      fvdl #include <machine/cpu.h>
     54        1.1      fvdl #include <machine/reg.h>
     55        1.4  christos #include <machine/linux_machdep.h>
     56        1.1      fvdl 
     57        1.1      fvdl #include <compat/linux/linux_types.h>
     58        1.4  christos #include <compat/linux/linux_syscall.h>
     59       1.10   mycroft #include <compat/linux/linux_signal.h>
     60        1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     61        1.1      fvdl #include <compat/linux/linux_util.h>
     62        1.1      fvdl #include <compat/linux/linux_exec.h>
     63        1.1      fvdl 
     64        1.6      fvdl static void *linux_aout_copyargs __P((struct exec_package *,
     65       1.14  christos     struct ps_strings *, void *, void *));
     66       1.17       cgd static int linux_elf32_signature __P((struct proc *p, struct exec_package *,
     67       1.14  christos     Elf32_Ehdr *));
     68        1.4  christos 
     69        1.8      fvdl #define	LINUX_AOUT_AUX_ARGSIZ	2
     70        1.6      fvdl #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
     71        1.8      fvdl 
     72        1.4  christos 
     73        1.9  christos const char linux_emul_path[] = "/emul/linux";
     74        1.4  christos extern int linux_error[];
     75       1.12   mycroft extern char linux_sigcode[], linux_esigcode[];
     76        1.4  christos extern struct sysent linux_sysent[];
     77        1.4  christos extern char *linux_syscallnames[];
     78       1.13  christos 
     79       1.13  christos int exec_linux_aout_prep_zmagic __P((struct proc *, struct exec_package *));
     80       1.13  christos int exec_linux_aout_prep_nmagic __P((struct proc *, struct exec_package *));
     81       1.13  christos int exec_linux_aout_prep_omagic __P((struct proc *, struct exec_package *));
     82       1.13  christos int exec_linux_aout_prep_qmagic __P((struct proc *, struct exec_package *));
     83        1.4  christos 
     84        1.6      fvdl struct emul emul_linux_aout = {
     85        1.4  christos 	"linux",
     86        1.4  christos 	linux_error,
     87        1.4  christos 	linux_sendsig,
     88        1.4  christos 	LINUX_SYS_syscall,
     89        1.4  christos 	LINUX_SYS_MAXSYSCALL,
     90        1.4  christos 	linux_sysent,
     91        1.4  christos 	linux_syscallnames,
     92        1.6      fvdl 	LINUX_AOUT_AUX_ARGSIZ,
     93        1.6      fvdl 	linux_aout_copyargs,
     94  1.25.14.1    mellon 	linux_setregs,
     95        1.6      fvdl 	linux_sigcode,
     96        1.6      fvdl 	linux_esigcode,
     97        1.6      fvdl };
     98        1.6      fvdl 
     99        1.6      fvdl struct emul emul_linux_elf = {
    100        1.6      fvdl 	"linux",
    101        1.6      fvdl 	linux_error,
    102        1.6      fvdl 	linux_sendsig,
    103        1.6      fvdl 	LINUX_SYS_syscall,
    104        1.6      fvdl 	LINUX_SYS_MAXSYSCALL,
    105        1.6      fvdl 	linux_sysent,
    106        1.6      fvdl 	linux_syscallnames,
    107        1.6      fvdl 	LINUX_ELF_AUX_ARGSIZ,
    108       1.17       cgd 	elf32_copyargs,
    109  1.25.14.1    mellon 	linux_setregs,
    110        1.4  christos 	linux_sigcode,
    111        1.4  christos 	linux_esigcode,
    112        1.4  christos };
    113        1.4  christos 
    114        1.4  christos 
    115        1.4  christos static void *
    116        1.6      fvdl linux_aout_copyargs(pack, arginfo, stack, argp)
    117        1.4  christos 	struct exec_package *pack;
    118        1.4  christos 	struct ps_strings *arginfo;
    119        1.4  christos 	void *stack;
    120        1.4  christos 	void *argp;
    121        1.4  christos {
    122        1.4  christos 	char **cpp = stack;
    123        1.4  christos 	char **stk = stack;
    124        1.4  christos 	char *dp, *sp;
    125        1.4  christos 	size_t len;
    126        1.4  christos 	void *nullp = NULL;
    127        1.4  christos 	int argc = arginfo->ps_nargvstr;
    128        1.4  christos 	int envc = arginfo->ps_nenvstr;
    129        1.4  christos 
    130        1.4  christos 	if (copyout(&argc, cpp++, sizeof(argc)))
    131        1.4  christos 		return NULL;
    132        1.4  christos 
    133        1.4  christos 	/* leave room for envp and argv */
    134        1.4  christos 	cpp += 2;
    135        1.4  christos 	if (copyout(&cpp, &stk[1], sizeof (cpp)))
    136        1.4  christos 		return NULL;
    137        1.4  christos 
    138        1.4  christos 	dp = (char *) (cpp + argc + envc + 2);
    139        1.4  christos 	sp = argp;
    140        1.4  christos 
    141        1.4  christos 	/* XXX don't copy them out, remap them! */
    142        1.5   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    143        1.4  christos 
    144        1.4  christos 	for (; --argc >= 0; sp += len, dp += len)
    145        1.4  christos 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    146        1.4  christos 		    copyoutstr(sp, dp, ARG_MAX, &len))
    147        1.4  christos 			return NULL;
    148        1.4  christos 
    149        1.4  christos 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    150        1.4  christos 		return NULL;
    151        1.4  christos 
    152        1.4  christos 	if (copyout(&cpp, &stk[2], sizeof (cpp)))
    153        1.4  christos 		return NULL;
    154        1.4  christos 
    155        1.5   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    156        1.4  christos 
    157        1.4  christos 	for (; --envc >= 0; sp += len, dp += len)
    158        1.4  christos 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    159        1.4  christos 		    copyoutstr(sp, dp, ARG_MAX, &len))
    160        1.4  christos 			return NULL;
    161        1.4  christos 
    162        1.4  christos 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    163        1.4  christos 		return NULL;
    164        1.4  christos 
    165        1.4  christos 	return cpp;
    166        1.4  christos }
    167        1.4  christos 
    168        1.1      fvdl int
    169        1.1      fvdl exec_linux_aout_makecmds(p, epp)
    170        1.1      fvdl 	struct proc *p;
    171        1.1      fvdl 	struct exec_package *epp;
    172        1.1      fvdl {
    173        1.1      fvdl 	struct exec *linux_ep = epp->ep_hdr;
    174        1.1      fvdl 	int machtype, magic;
    175        1.1      fvdl 	int error = ENOEXEC;
    176        1.1      fvdl 
    177        1.1      fvdl 	magic = LINUX_N_MAGIC(linux_ep);
    178        1.1      fvdl 	machtype = LINUX_N_MACHTYPE(linux_ep);
    179        1.1      fvdl 
    180        1.1      fvdl 
    181        1.1      fvdl 	if (machtype != LINUX_MID_MACHINE)
    182        1.1      fvdl 		return (ENOEXEC);
    183        1.1      fvdl 
    184        1.1      fvdl 	switch (magic) {
    185        1.1      fvdl 	case QMAGIC:
    186        1.1      fvdl 		error = exec_linux_aout_prep_qmagic(p, epp);
    187        1.1      fvdl 		break;
    188        1.1      fvdl 	case ZMAGIC:
    189        1.1      fvdl 		error = exec_linux_aout_prep_zmagic(p, epp);
    190        1.1      fvdl 		break;
    191        1.1      fvdl 	case NMAGIC:
    192        1.1      fvdl 		error = exec_linux_aout_prep_nmagic(p, epp);
    193        1.1      fvdl 		break;
    194        1.1      fvdl 	case OMAGIC:
    195        1.1      fvdl 		error = exec_linux_aout_prep_omagic(p, epp);
    196        1.1      fvdl 		break;
    197        1.1      fvdl 	}
    198        1.4  christos 	if (error == 0)
    199        1.6      fvdl 		epp->ep_emul = &emul_linux_aout;
    200        1.1      fvdl 	return error;
    201        1.1      fvdl }
    202        1.1      fvdl 
    203        1.1      fvdl /*
    204        1.1      fvdl  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    205        1.1      fvdl  * is very likely not page aligned on most architectures, it is treated
    206        1.1      fvdl  * as an NMAGIC here. XXX
    207        1.1      fvdl  */
    208        1.1      fvdl 
    209        1.1      fvdl int
    210        1.1      fvdl exec_linux_aout_prep_zmagic(p, epp)
    211        1.1      fvdl 	struct proc *p;
    212        1.1      fvdl 	struct exec_package *epp;
    213        1.1      fvdl {
    214        1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    215        1.1      fvdl 
    216        1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    217        1.1      fvdl 	epp->ep_tsize = execp->a_text;
    218        1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    219        1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    220        1.1      fvdl 	epp->ep_entry = execp->a_entry;
    221        1.1      fvdl 
    222        1.1      fvdl 	/* set up command for text segment */
    223        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    224        1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    225        1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    226        1.1      fvdl 
    227        1.1      fvdl 	/* set up command for data segment */
    228        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    229        1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    230        1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    231        1.1      fvdl 
    232        1.1      fvdl 	/* set up command for bss segment */
    233        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    234        1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    235        1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    236        1.1      fvdl 
    237        1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    238        1.1      fvdl }
    239        1.1      fvdl 
    240        1.1      fvdl /*
    241        1.1      fvdl  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    242        1.1      fvdl  * Not different from the normal stuff.
    243        1.1      fvdl  */
    244        1.1      fvdl 
    245        1.1      fvdl int
    246        1.1      fvdl exec_linux_aout_prep_nmagic(p, epp)
    247        1.1      fvdl 	struct proc *p;
    248        1.1      fvdl 	struct exec_package *epp;
    249        1.1      fvdl {
    250        1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    251        1.1      fvdl 	long bsize, baddr;
    252        1.1      fvdl 
    253        1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    254        1.1      fvdl 	epp->ep_tsize = execp->a_text;
    255        1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    256        1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    257        1.1      fvdl 	epp->ep_entry = execp->a_entry;
    258        1.1      fvdl 
    259        1.1      fvdl 	/* set up command for text segment */
    260        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    261        1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    262        1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    263        1.1      fvdl 
    264        1.1      fvdl 	/* set up command for data segment */
    265        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    266        1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    267        1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    268        1.1      fvdl 
    269        1.1      fvdl 	/* set up command for bss segment */
    270        1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    271        1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    272        1.1      fvdl 	if (bsize > 0)
    273        1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    274        1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    275        1.1      fvdl 
    276        1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    277        1.1      fvdl }
    278        1.1      fvdl 
    279        1.1      fvdl /*
    280        1.1      fvdl  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    281        1.1      fvdl  * Business as usual.
    282        1.1      fvdl  */
    283        1.1      fvdl 
    284        1.1      fvdl int
    285        1.1      fvdl exec_linux_aout_prep_omagic(p, epp)
    286        1.1      fvdl 	struct proc *p;
    287        1.1      fvdl 	struct exec_package *epp;
    288        1.1      fvdl {
    289        1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    290        1.1      fvdl 	long dsize, bsize, baddr;
    291        1.1      fvdl 
    292        1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    293        1.1      fvdl 	epp->ep_tsize = execp->a_text;
    294        1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    295        1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    296        1.1      fvdl 	epp->ep_entry = execp->a_entry;
    297        1.1      fvdl 
    298        1.1      fvdl 	/* set up command for text and data segments */
    299        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    300        1.1      fvdl 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    301        1.1      fvdl 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    302        1.1      fvdl 
    303        1.1      fvdl 	/* set up command for bss segment */
    304        1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    305        1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    306        1.1      fvdl 	if (bsize > 0)
    307        1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    308        1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    309        1.1      fvdl 
    310        1.1      fvdl 	/*
    311        1.1      fvdl 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    312        1.1      fvdl 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    313        1.1      fvdl 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    314        1.1      fvdl 	 * respectively to page boundaries.
    315        1.1      fvdl 	 * Compensate `ep_dsize' for the amount of data covered by the last
    316        1.1      fvdl 	 * text page.
    317        1.1      fvdl 	 */
    318        1.1      fvdl 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    319        1.1      fvdl 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    320        1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    321        1.1      fvdl }
    322        1.1      fvdl 
    323        1.1      fvdl int
    324        1.1      fvdl exec_linux_aout_prep_qmagic(p, epp)
    325        1.1      fvdl 	struct proc *p;
    326        1.1      fvdl 	struct exec_package *epp;
    327        1.1      fvdl {
    328        1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    329        1.1      fvdl 
    330        1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    331        1.1      fvdl 	epp->ep_tsize = execp->a_text;
    332        1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    333        1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    334        1.1      fvdl 	epp->ep_entry = execp->a_entry;
    335        1.1      fvdl 
    336        1.1      fvdl 	/*
    337        1.1      fvdl 	 * check if vnode is in open for writing, because we want to
    338        1.1      fvdl 	 * demand-page out of it.  if it is, don't do it, for various
    339        1.1      fvdl 	 * reasons
    340        1.1      fvdl 	 */
    341        1.1      fvdl 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    342        1.1      fvdl 	    epp->ep_vp->v_writecount != 0) {
    343        1.1      fvdl #ifdef DIAGNOSTIC
    344        1.1      fvdl 		if (epp->ep_vp->v_flag & VTEXT)
    345        1.1      fvdl 			panic("exec: a VTEXT vnode has writecount != 0\n");
    346        1.1      fvdl #endif
    347        1.1      fvdl 		return ETXTBSY;
    348        1.1      fvdl 	}
    349        1.1      fvdl 	epp->ep_vp->v_flag |= VTEXT;
    350        1.1      fvdl 
    351        1.1      fvdl 	/* set up command for text segment */
    352        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    353        1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    354        1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    355        1.1      fvdl 
    356        1.1      fvdl 	/* set up command for data segment */
    357        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    358        1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    359        1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    360        1.1      fvdl 
    361        1.1      fvdl 	/* set up command for bss segment */
    362        1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    363        1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    364        1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    365        1.1      fvdl 
    366        1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    367        1.1      fvdl }
    368        1.1      fvdl 
    369       1.14  christos /*
    370       1.14  christos  * Take advantage of the fact that all the linux binaries are compiled
    371       1.14  christos  * with gcc, and gcc sticks in the comment field a signature. Note that
    372       1.14  christos  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    373       1.14  christos  * that will not be a problem. We don't bother to read in the string table,
    374       1.14  christos  * but we check all the progbits headers.
    375       1.14  christos  */
    376       1.14  christos static int
    377       1.17       cgd linux_elf32_signature(p, epp, eh)
    378       1.14  christos 	struct proc *p;
    379       1.14  christos 	struct exec_package *epp;
    380       1.14  christos 	Elf32_Ehdr *eh;
    381       1.14  christos {
    382       1.14  christos 	size_t shsize = sizeof(Elf32_Shdr) * eh->e_shnum;
    383       1.14  christos 	size_t i;
    384       1.14  christos 	static const char signature[] = "\0GCC: (GNU) ";
    385       1.14  christos 	char buf[sizeof(signature) - 1];
    386       1.14  christos 	Elf32_Shdr *sh;
    387       1.14  christos 	int error;
    388       1.14  christos 
    389       1.14  christos 	sh = (Elf32_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    390       1.14  christos 
    391       1.17       cgd 	if ((error = elf32_read_from(p, epp->ep_vp, eh->e_shoff,
    392       1.14  christos 	    (caddr_t) sh, shsize)) != 0)
    393       1.14  christos 		goto out;
    394       1.14  christos 
    395       1.14  christos 	for (i = 0; i < eh->e_shnum; i++) {
    396       1.14  christos 		Elf32_Shdr *s = &sh[i];
    397       1.14  christos 
    398       1.14  christos 		/*
    399       1.14  christos 		 * Identify candidates for the comment header;
    400       1.15  christos 		 * Header cannot have a load address, or flags and
    401       1.14  christos 		 * it must be large enough.
    402       1.14  christos 		 */
    403       1.22       jtk 		if (s->sh_type != Elf_sht_progbits ||
    404       1.14  christos 		    s->sh_addr != 0 ||
    405       1.14  christos 		    s->sh_flags != 0 ||
    406       1.14  christos 		    s->sh_size < sizeof(signature) - 1)
    407       1.14  christos 			continue;
    408       1.14  christos 
    409       1.17       cgd 		if ((error = elf32_read_from(p, epp->ep_vp, s->sh_offset,
    410       1.15  christos 		    (caddr_t) buf, sizeof(signature) - 1)) != 0)
    411       1.14  christos 			goto out;
    412       1.14  christos 
    413       1.15  christos 		/*
    414       1.15  christos 		 * error is 0, if the signatures match we are done.
    415       1.15  christos 		 */
    416       1.15  christos 		if (bcmp(buf, signature, sizeof(signature) - 1) == 0)
    417       1.14  christos 			goto out;
    418       1.14  christos 	}
    419       1.14  christos 	error = EFTYPE;
    420       1.14  christos 
    421       1.14  christos out:
    422       1.14  christos 	free(sh, M_TEMP);
    423       1.14  christos 	return error;
    424       1.14  christos }
    425       1.14  christos 
    426        1.8      fvdl int
    427       1.17       cgd linux_elf32_probe(p, epp, eh, itp, pos)
    428        1.8      fvdl 	struct proc *p;
    429        1.8      fvdl 	struct exec_package *epp;
    430       1.14  christos 	Elf32_Ehdr *eh;
    431        1.8      fvdl 	char *itp;
    432       1.17       cgd 	Elf32_Addr *pos;
    433        1.6      fvdl {
    434        1.8      fvdl 	char *bp;
    435        1.8      fvdl 	int error;
    436        1.8      fvdl 	size_t len;
    437       1.14  christos 
    438       1.17       cgd 	if ((error = linux_elf32_signature(p, epp, eh)) != 0)
    439       1.14  christos 		return error;
    440        1.6      fvdl 
    441        1.8      fvdl 	if (itp[0]) {
    442        1.9  christos 		if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
    443        1.8      fvdl 			return error;
    444        1.8      fvdl 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    445        1.8      fvdl 			return error;
    446        1.8      fvdl 		free(bp, M_TEMP);
    447        1.6      fvdl 	}
    448        1.8      fvdl 	epp->ep_emul = &emul_linux_elf;
    449        1.8      fvdl 	*pos = ELF32_NO_ADDR;
    450        1.6      fvdl 	return 0;
    451        1.6      fvdl }
    452        1.6      fvdl 
    453        1.6      fvdl /*
    454        1.6      fvdl  * The Linux system call to load shared libraries, a.out version. The
    455        1.6      fvdl  * a.out shared libs are just files that are mapped onto a fixed
    456        1.6      fvdl  * address in the process' address space. The address is given in
    457        1.1      fvdl  * a_entry. Read in the header, set up some VM commands and run them.
    458        1.1      fvdl  *
    459        1.1      fvdl  * Yes, both text and data are mapped at once, so we're left with
    460        1.1      fvdl  * writeable text for the shared libs. The Linux crt0 seemed to break
    461        1.1      fvdl  * sometimes when data was mapped seperately. It munmapped a uselib()
    462        1.1      fvdl  * of ld.so by hand, which failed with shared text and data for ld.so
    463        1.1      fvdl  * Yuck.
    464        1.1      fvdl  *
    465        1.1      fvdl  * Because of the problem with ZMAGIC executables (text starts
    466        1.6      fvdl  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
    467        1.1      fvdl  * shared libs are not handled very efficiently :-(
    468        1.1      fvdl  */
    469        1.1      fvdl 
    470        1.1      fvdl int
    471       1.12   mycroft linux_sys_uselib(p, v, retval)
    472        1.1      fvdl 	struct proc *p;
    473       1.11   thorpej 	void *v;
    474       1.11   thorpej 	register_t *retval;
    475       1.11   thorpej {
    476       1.12   mycroft 	struct linux_sys_uselib_args /* {
    477        1.1      fvdl 		syscallarg(char *) path;
    478       1.11   thorpej 	} */ *uap = v;
    479        1.1      fvdl 	caddr_t sg;
    480        1.1      fvdl 	long bsize, dsize, tsize, taddr, baddr, daddr;
    481        1.1      fvdl 	struct nameidata ni;
    482        1.1      fvdl 	struct vnode *vp;
    483        1.1      fvdl 	struct exec hdr;
    484        1.1      fvdl 	struct exec_vmcmd_set vcset;
    485        1.1      fvdl 	int rem, i, magic, error;
    486        1.1      fvdl 
    487        1.9  christos 	sg = stackgap_init(p->p_emul);
    488        1.9  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    489        1.1      fvdl 
    490        1.1      fvdl 	NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    491        1.1      fvdl 
    492        1.1      fvdl 	if ((error = namei(&ni)))
    493        1.1      fvdl 		return error;
    494        1.1      fvdl 
    495        1.1      fvdl 	vp = ni.ni_vp;
    496        1.1      fvdl 
    497        1.1      fvdl 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
    498        1.1      fvdl 			     0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
    499        1.1      fvdl 			     &rem, p))) {
    500        1.1      fvdl 		vrele(vp);
    501        1.1      fvdl 		return error;
    502        1.1      fvdl 	}
    503        1.1      fvdl 
    504        1.1      fvdl 	if (rem != 0) {
    505        1.1      fvdl 		vrele(vp);
    506        1.1      fvdl 		return ENOEXEC;
    507        1.1      fvdl 	}
    508        1.6      fvdl 
    509        1.6      fvdl 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
    510        1.6      fvdl 		return ENOEXEC;
    511        1.1      fvdl 
    512        1.1      fvdl 	magic = LINUX_N_MAGIC(&hdr);
    513        1.1      fvdl 	taddr = hdr.a_entry & (~(NBPG - 1));
    514        1.1      fvdl 	tsize = hdr.a_text;
    515        1.1      fvdl 	daddr = taddr + tsize;
    516        1.1      fvdl 	dsize = hdr.a_data + hdr.a_bss;
    517        1.1      fvdl 
    518        1.1      fvdl 	if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
    519        1.1      fvdl 		vrele(vp);
    520        1.1      fvdl                 return ETXTBSY;
    521        1.1      fvdl         }
    522        1.1      fvdl 	vp->v_flag |= VTEXT;
    523        1.1      fvdl 
    524        1.1      fvdl 	vcset.evs_cnt = 0;
    525        1.1      fvdl 	vcset.evs_used = 0;
    526        1.1      fvdl 
    527        1.1      fvdl 	NEW_VMCMD(&vcset,
    528        1.1      fvdl 		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
    529        1.1      fvdl 		  hdr.a_text + hdr.a_data, taddr,
    530        1.1      fvdl 		  vp, LINUX_N_TXTOFF(hdr, magic),
    531        1.1      fvdl 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
    532        1.1      fvdl 
    533        1.1      fvdl 	baddr = roundup(daddr + hdr.a_data, NBPG);
    534        1.1      fvdl 	bsize = daddr + dsize - baddr;
    535        1.1      fvdl         if (bsize > 0) {
    536        1.1      fvdl                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
    537        1.1      fvdl                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    538        1.1      fvdl 	}
    539        1.1      fvdl 
    540        1.1      fvdl 	for (i = 0; i < vcset.evs_used && !error; i++) {
    541        1.1      fvdl 		struct exec_vmcmd *vcp;
    542        1.1      fvdl 
    543        1.1      fvdl 		vcp = &vcset.evs_cmds[i];
    544        1.1      fvdl 		error = (*vcp->ev_proc)(p, vcp);
    545        1.1      fvdl 	}
    546        1.1      fvdl 
    547        1.1      fvdl 	kill_vmcmds(&vcset);
    548        1.1      fvdl 
    549        1.1      fvdl 	vrele(vp);
    550        1.1      fvdl 
    551        1.1      fvdl 	return error;
    552        1.1      fvdl }
    553        1.1      fvdl 
    554        1.1      fvdl /*
    555        1.1      fvdl  * Execve(2). Just check the alternate emulation path, and pass it on
    556        1.1      fvdl  * to the NetBSD execve().
    557        1.1      fvdl  */
    558        1.1      fvdl int
    559       1.12   mycroft linux_sys_execve(p, v, retval)
    560        1.1      fvdl 	struct proc *p;
    561       1.11   thorpej 	void *v;
    562       1.11   thorpej 	register_t *retval;
    563       1.11   thorpej {
    564       1.12   mycroft 	struct linux_sys_execve_args /* {
    565        1.1      fvdl 		syscallarg(char *) path;
    566        1.1      fvdl 		syscallarg(char **) argv;
    567        1.1      fvdl 		syscallarg(char **) envp;
    568       1.11   thorpej 	} */ *uap = v;
    569       1.16   mycroft 	struct sys_execve_args ap;
    570        1.1      fvdl 	caddr_t sg;
    571        1.1      fvdl 
    572        1.9  christos 	sg = stackgap_init(p->p_emul);
    573        1.9  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    574        1.1      fvdl 
    575       1.16   mycroft 	SCARG(&ap, path) = SCARG(uap, path);
    576       1.16   mycroft 	SCARG(&ap, argp) = SCARG(uap, argp);
    577       1.16   mycroft 	SCARG(&ap, envp) = SCARG(uap, envp);
    578       1.16   mycroft 
    579       1.16   mycroft 	return sys_execve(p, &ap, retval);
    580        1.1      fvdl }
    581