Home | History | Annotate | Line # | Download | only in common
linux_exec_aout.c revision 1.36
      1  1.36     veego /*	$NetBSD: linux_exec_aout.c,v 1.36 1998/10/23 10:54:58 veego Exp $	*/
      2  1.29  christos 
      3  1.29  christos /*-
      4  1.33      fvdl  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5  1.29  christos  * All rights reserved.
      6  1.29  christos  *
      7  1.29  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.33      fvdl  * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
      9  1.29  christos  *
     10  1.29  christos  * Redistribution and use in source and binary forms, with or without
     11  1.29  christos  * modification, are permitted provided that the following conditions
     12  1.29  christos  * are met:
     13  1.29  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.29  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.29  christos  *    documentation and/or other materials provided with the distribution.
     18  1.29  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.29  christos  *    must display the following acknowledgement:
     20  1.31       erh  *	This product includes software developed by the NetBSD
     21  1.31       erh  *	Foundation, Inc. and its contributors.
     22  1.29  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.29  christos  *    contributors may be used to endorse or promote products derived
     24  1.29  christos  *    from this software without specific prior written permission.
     25  1.29  christos  *
     26  1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.29  christos  */
     38   1.1      fvdl 
     39   1.1      fvdl /*
     40   1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     41   1.1      fvdl  */
     42   1.1      fvdl 
     43   1.1      fvdl #include <sys/param.h>
     44   1.1      fvdl #include <sys/systm.h>
     45   1.1      fvdl #include <sys/kernel.h>
     46   1.1      fvdl #include <sys/proc.h>
     47   1.1      fvdl #include <sys/malloc.h>
     48   1.1      fvdl #include <sys/namei.h>
     49   1.1      fvdl #include <sys/vnode.h>
     50  1.13  christos #include <sys/mount.h>
     51  1.25  christos #include <sys/exec.h>
     52   1.8      fvdl #include <sys/exec_elf.h>
     53   1.1      fvdl 
     54   1.1      fvdl #include <sys/mman.h>
     55  1.13  christos #include <sys/syscallargs.h>
     56  1.13  christos 
     57   1.1      fvdl #include <vm/vm.h>
     58   1.1      fvdl #include <vm/vm_param.h>
     59   1.1      fvdl #include <vm/vm_map.h>
     60   1.1      fvdl 
     61  1.32  christos #include <machine/cpu.h>
     62  1.32  christos #include <machine/reg.h>
     63  1.32  christos 
     64  1.32  christos #include <compat/linux/common/linux_types.h>
     65  1.32  christos #include <compat/linux/common/linux_signal.h>
     66  1.32  christos #include <compat/linux/common/linux_util.h>
     67  1.32  christos #include <compat/linux/common/linux_exec.h>
     68  1.32  christos #include <compat/linux/common/linux_machdep.h>
     69  1.36     veego #include <compat/linux/common/linux_errno.h>
     70  1.32  christos 
     71  1.32  christos #include <compat/linux/linux_syscallargs.h>
     72   1.4  christos #include <compat/linux/linux_syscall.h>
     73  1.31       erh 
     74   1.1      fvdl 
     75   1.6      fvdl static void *linux_aout_copyargs __P((struct exec_package *,
     76  1.14  christos     struct ps_strings *, void *, void *));
     77   1.4  christos 
     78   1.8      fvdl #define	LINUX_AOUT_AUX_ARGSIZ	2
     79   1.4  christos 
     80  1.12   mycroft extern char linux_sigcode[], linux_esigcode[];
     81   1.4  christos extern struct sysent linux_sysent[];
     82   1.4  christos extern char *linux_syscallnames[];
     83  1.13  christos 
     84  1.13  christos int exec_linux_aout_prep_zmagic __P((struct proc *, struct exec_package *));
     85  1.13  christos int exec_linux_aout_prep_nmagic __P((struct proc *, struct exec_package *));
     86  1.13  christos int exec_linux_aout_prep_omagic __P((struct proc *, struct exec_package *));
     87  1.13  christos int exec_linux_aout_prep_qmagic __P((struct proc *, struct exec_package *));
     88   1.4  christos 
     89   1.6      fvdl struct emul emul_linux_aout = {
     90   1.4  christos 	"linux",
     91  1.35       erh 	native_to_linux_errno,
     92   1.4  christos 	linux_sendsig,
     93   1.4  christos 	LINUX_SYS_syscall,
     94   1.4  christos 	LINUX_SYS_MAXSYSCALL,
     95   1.4  christos 	linux_sysent,
     96   1.4  christos 	linux_syscallnames,
     97   1.6      fvdl 	LINUX_AOUT_AUX_ARGSIZ,
     98   1.6      fvdl 	linux_aout_copyargs,
     99  1.26   mycroft 	linux_setregs,
    100   1.6      fvdl 	linux_sigcode,
    101   1.6      fvdl 	linux_esigcode,
    102   1.6      fvdl };
    103   1.6      fvdl 
    104   1.4  christos static void *
    105   1.6      fvdl linux_aout_copyargs(pack, arginfo, stack, argp)
    106   1.4  christos 	struct exec_package *pack;
    107   1.4  christos 	struct ps_strings *arginfo;
    108   1.4  christos 	void *stack;
    109   1.4  christos 	void *argp;
    110   1.4  christos {
    111   1.4  christos 	char **cpp = stack;
    112   1.4  christos 	char **stk = stack;
    113   1.4  christos 	char *dp, *sp;
    114   1.4  christos 	size_t len;
    115   1.4  christos 	void *nullp = NULL;
    116   1.4  christos 	int argc = arginfo->ps_nargvstr;
    117   1.4  christos 	int envc = arginfo->ps_nenvstr;
    118   1.4  christos 
    119   1.4  christos 	if (copyout(&argc, cpp++, sizeof(argc)))
    120   1.4  christos 		return NULL;
    121   1.4  christos 
    122   1.4  christos 	/* leave room for envp and argv */
    123   1.4  christos 	cpp += 2;
    124   1.4  christos 	if (copyout(&cpp, &stk[1], sizeof (cpp)))
    125   1.4  christos 		return NULL;
    126   1.4  christos 
    127   1.4  christos 	dp = (char *) (cpp + argc + envc + 2);
    128   1.4  christos 	sp = argp;
    129   1.4  christos 
    130   1.4  christos 	/* XXX don't copy them out, remap them! */
    131   1.5   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    132   1.4  christos 
    133   1.4  christos 	for (; --argc >= 0; sp += len, dp += len)
    134   1.4  christos 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    135   1.4  christos 		    copyoutstr(sp, dp, ARG_MAX, &len))
    136   1.4  christos 			return NULL;
    137   1.4  christos 
    138   1.4  christos 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    139   1.4  christos 		return NULL;
    140   1.4  christos 
    141   1.4  christos 	if (copyout(&cpp, &stk[2], sizeof (cpp)))
    142   1.4  christos 		return NULL;
    143   1.4  christos 
    144   1.5   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    145   1.4  christos 
    146   1.4  christos 	for (; --envc >= 0; sp += len, dp += len)
    147   1.4  christos 		if (copyout(&dp, cpp++, sizeof(dp)) ||
    148   1.4  christos 		    copyoutstr(sp, dp, ARG_MAX, &len))
    149   1.4  christos 			return NULL;
    150   1.4  christos 
    151   1.4  christos 	if (copyout(&nullp, cpp++, sizeof(nullp)))
    152   1.4  christos 		return NULL;
    153   1.4  christos 
    154   1.4  christos 	return cpp;
    155   1.4  christos }
    156   1.4  christos 
    157   1.1      fvdl int
    158   1.1      fvdl exec_linux_aout_makecmds(p, epp)
    159   1.1      fvdl 	struct proc *p;
    160   1.1      fvdl 	struct exec_package *epp;
    161   1.1      fvdl {
    162   1.1      fvdl 	struct exec *linux_ep = epp->ep_hdr;
    163   1.1      fvdl 	int machtype, magic;
    164   1.1      fvdl 	int error = ENOEXEC;
    165   1.1      fvdl 
    166   1.1      fvdl 	magic = LINUX_N_MAGIC(linux_ep);
    167   1.1      fvdl 	machtype = LINUX_N_MACHTYPE(linux_ep);
    168   1.1      fvdl 
    169   1.1      fvdl 
    170   1.1      fvdl 	if (machtype != LINUX_MID_MACHINE)
    171   1.1      fvdl 		return (ENOEXEC);
    172   1.1      fvdl 
    173   1.1      fvdl 	switch (magic) {
    174   1.1      fvdl 	case QMAGIC:
    175   1.1      fvdl 		error = exec_linux_aout_prep_qmagic(p, epp);
    176   1.1      fvdl 		break;
    177   1.1      fvdl 	case ZMAGIC:
    178   1.1      fvdl 		error = exec_linux_aout_prep_zmagic(p, epp);
    179   1.1      fvdl 		break;
    180   1.1      fvdl 	case NMAGIC:
    181   1.1      fvdl 		error = exec_linux_aout_prep_nmagic(p, epp);
    182   1.1      fvdl 		break;
    183   1.1      fvdl 	case OMAGIC:
    184   1.1      fvdl 		error = exec_linux_aout_prep_omagic(p, epp);
    185   1.1      fvdl 		break;
    186   1.1      fvdl 	}
    187   1.4  christos 	if (error == 0)
    188   1.6      fvdl 		epp->ep_emul = &emul_linux_aout;
    189   1.1      fvdl 	return error;
    190   1.1      fvdl }
    191   1.1      fvdl 
    192   1.1      fvdl /*
    193   1.1      fvdl  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    194   1.1      fvdl  * is very likely not page aligned on most architectures, it is treated
    195   1.1      fvdl  * as an NMAGIC here. XXX
    196   1.1      fvdl  */
    197   1.1      fvdl 
    198   1.1      fvdl int
    199   1.1      fvdl exec_linux_aout_prep_zmagic(p, epp)
    200   1.1      fvdl 	struct proc *p;
    201   1.1      fvdl 	struct exec_package *epp;
    202   1.1      fvdl {
    203   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    204   1.1      fvdl 
    205   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    206   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    207   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    208   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    209   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    210   1.1      fvdl 
    211   1.1      fvdl 	/* set up command for text segment */
    212   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    213   1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    214   1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    215   1.1      fvdl 
    216   1.1      fvdl 	/* set up command for data segment */
    217   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    218   1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    219   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    220   1.1      fvdl 
    221   1.1      fvdl 	/* set up command for bss segment */
    222   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    223   1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    224   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    225   1.1      fvdl 
    226   1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    227   1.1      fvdl }
    228   1.1      fvdl 
    229   1.1      fvdl /*
    230   1.1      fvdl  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    231   1.1      fvdl  * Not different from the normal stuff.
    232   1.1      fvdl  */
    233   1.1      fvdl 
    234   1.1      fvdl int
    235   1.1      fvdl exec_linux_aout_prep_nmagic(p, epp)
    236   1.1      fvdl 	struct proc *p;
    237   1.1      fvdl 	struct exec_package *epp;
    238   1.1      fvdl {
    239   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    240   1.1      fvdl 	long bsize, baddr;
    241   1.1      fvdl 
    242   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    243   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    244   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    245   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    246   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    247   1.1      fvdl 
    248   1.1      fvdl 	/* set up command for text segment */
    249   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    250   1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    251   1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    252   1.1      fvdl 
    253   1.1      fvdl 	/* set up command for data segment */
    254   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    255   1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    256   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    257   1.1      fvdl 
    258   1.1      fvdl 	/* set up command for bss segment */
    259   1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    260   1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    261   1.1      fvdl 	if (bsize > 0)
    262   1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    263   1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    264   1.1      fvdl 
    265   1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    266   1.1      fvdl }
    267   1.1      fvdl 
    268   1.1      fvdl /*
    269   1.1      fvdl  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    270   1.1      fvdl  * Business as usual.
    271   1.1      fvdl  */
    272   1.1      fvdl 
    273   1.1      fvdl int
    274   1.1      fvdl exec_linux_aout_prep_omagic(p, epp)
    275   1.1      fvdl 	struct proc *p;
    276   1.1      fvdl 	struct exec_package *epp;
    277   1.1      fvdl {
    278   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    279   1.1      fvdl 	long dsize, bsize, baddr;
    280   1.1      fvdl 
    281   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    282   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    283   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    284   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    285   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    286   1.1      fvdl 
    287   1.1      fvdl 	/* set up command for text and data segments */
    288   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    289   1.1      fvdl 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    290   1.1      fvdl 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    291   1.1      fvdl 
    292   1.1      fvdl 	/* set up command for bss segment */
    293   1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    294   1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    295   1.1      fvdl 	if (bsize > 0)
    296   1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    297   1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    298   1.1      fvdl 
    299   1.1      fvdl 	/*
    300   1.1      fvdl 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    301   1.1      fvdl 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    302   1.1      fvdl 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    303   1.1      fvdl 	 * respectively to page boundaries.
    304   1.1      fvdl 	 * Compensate `ep_dsize' for the amount of data covered by the last
    305   1.1      fvdl 	 * text page.
    306   1.1      fvdl 	 */
    307   1.1      fvdl 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    308   1.1      fvdl 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    309   1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    310   1.1      fvdl }
    311   1.1      fvdl 
    312   1.1      fvdl int
    313   1.1      fvdl exec_linux_aout_prep_qmagic(p, epp)
    314   1.1      fvdl 	struct proc *p;
    315   1.1      fvdl 	struct exec_package *epp;
    316   1.1      fvdl {
    317   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    318   1.1      fvdl 
    319   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    320   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    321   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    322   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    323   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    324   1.1      fvdl 
    325   1.1      fvdl 	/*
    326   1.1      fvdl 	 * check if vnode is in open for writing, because we want to
    327   1.1      fvdl 	 * demand-page out of it.  if it is, don't do it, for various
    328   1.1      fvdl 	 * reasons
    329   1.1      fvdl 	 */
    330   1.1      fvdl 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    331   1.1      fvdl 	    epp->ep_vp->v_writecount != 0) {
    332   1.1      fvdl #ifdef DIAGNOSTIC
    333   1.1      fvdl 		if (epp->ep_vp->v_flag & VTEXT)
    334   1.1      fvdl 			panic("exec: a VTEXT vnode has writecount != 0\n");
    335   1.1      fvdl #endif
    336   1.1      fvdl 		return ETXTBSY;
    337   1.1      fvdl 	}
    338   1.1      fvdl 	epp->ep_vp->v_flag |= VTEXT;
    339   1.1      fvdl 
    340   1.1      fvdl 	/* set up command for text segment */
    341   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    342   1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    343   1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    344   1.1      fvdl 
    345   1.1      fvdl 	/* set up command for data segment */
    346   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    347   1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    348   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    349   1.1      fvdl 
    350   1.1      fvdl 	/* set up command for bss segment */
    351   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    352   1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    353   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    354   1.1      fvdl 
    355   1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    356   1.1      fvdl }
    357   1.1      fvdl 
    358  1.14  christos /*
    359   1.6      fvdl  * The Linux system call to load shared libraries, a.out version. The
    360   1.6      fvdl  * a.out shared libs are just files that are mapped onto a fixed
    361   1.6      fvdl  * address in the process' address space. The address is given in
    362   1.1      fvdl  * a_entry. Read in the header, set up some VM commands and run them.
    363   1.1      fvdl  *
    364   1.1      fvdl  * Yes, both text and data are mapped at once, so we're left with
    365   1.1      fvdl  * writeable text for the shared libs. The Linux crt0 seemed to break
    366   1.1      fvdl  * sometimes when data was mapped seperately. It munmapped a uselib()
    367   1.1      fvdl  * of ld.so by hand, which failed with shared text and data for ld.so
    368   1.1      fvdl  * Yuck.
    369   1.1      fvdl  *
    370   1.1      fvdl  * Because of the problem with ZMAGIC executables (text starts
    371   1.6      fvdl  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
    372   1.1      fvdl  * shared libs are not handled very efficiently :-(
    373   1.1      fvdl  */
    374   1.1      fvdl 
    375   1.1      fvdl int
    376  1.12   mycroft linux_sys_uselib(p, v, retval)
    377   1.1      fvdl 	struct proc *p;
    378  1.11   thorpej 	void *v;
    379  1.11   thorpej 	register_t *retval;
    380  1.11   thorpej {
    381  1.12   mycroft 	struct linux_sys_uselib_args /* {
    382   1.1      fvdl 		syscallarg(char *) path;
    383  1.11   thorpej 	} */ *uap = v;
    384   1.1      fvdl 	caddr_t sg;
    385   1.1      fvdl 	long bsize, dsize, tsize, taddr, baddr, daddr;
    386   1.1      fvdl 	struct nameidata ni;
    387   1.1      fvdl 	struct vnode *vp;
    388   1.1      fvdl 	struct exec hdr;
    389   1.1      fvdl 	struct exec_vmcmd_set vcset;
    390  1.27   thorpej 	int i, magic, error;
    391  1.27   thorpej 	size_t rem;
    392   1.1      fvdl 
    393   1.9  christos 	sg = stackgap_init(p->p_emul);
    394   1.9  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    395   1.1      fvdl 
    396   1.1      fvdl 	NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    397   1.1      fvdl 
    398   1.1      fvdl 	if ((error = namei(&ni)))
    399   1.1      fvdl 		return error;
    400   1.1      fvdl 
    401   1.1      fvdl 	vp = ni.ni_vp;
    402   1.1      fvdl 
    403   1.1      fvdl 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
    404   1.1      fvdl 			     0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
    405   1.1      fvdl 			     &rem, p))) {
    406   1.1      fvdl 		vrele(vp);
    407   1.1      fvdl 		return error;
    408   1.1      fvdl 	}
    409   1.1      fvdl 
    410   1.1      fvdl 	if (rem != 0) {
    411   1.1      fvdl 		vrele(vp);
    412   1.1      fvdl 		return ENOEXEC;
    413   1.1      fvdl 	}
    414   1.6      fvdl 
    415   1.6      fvdl 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
    416   1.6      fvdl 		return ENOEXEC;
    417   1.1      fvdl 
    418   1.1      fvdl 	magic = LINUX_N_MAGIC(&hdr);
    419   1.1      fvdl 	taddr = hdr.a_entry & (~(NBPG - 1));
    420   1.1      fvdl 	tsize = hdr.a_text;
    421   1.1      fvdl 	daddr = taddr + tsize;
    422   1.1      fvdl 	dsize = hdr.a_data + hdr.a_bss;
    423   1.1      fvdl 
    424   1.1      fvdl 	if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
    425   1.1      fvdl 		vrele(vp);
    426   1.1      fvdl                 return ETXTBSY;
    427   1.1      fvdl         }
    428   1.1      fvdl 	vp->v_flag |= VTEXT;
    429   1.1      fvdl 
    430   1.1      fvdl 	vcset.evs_cnt = 0;
    431   1.1      fvdl 	vcset.evs_used = 0;
    432   1.1      fvdl 
    433   1.1      fvdl 	NEW_VMCMD(&vcset,
    434   1.1      fvdl 		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
    435   1.1      fvdl 		  hdr.a_text + hdr.a_data, taddr,
    436   1.1      fvdl 		  vp, LINUX_N_TXTOFF(hdr, magic),
    437   1.1      fvdl 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
    438   1.1      fvdl 
    439   1.1      fvdl 	baddr = roundup(daddr + hdr.a_data, NBPG);
    440   1.1      fvdl 	bsize = daddr + dsize - baddr;
    441   1.1      fvdl         if (bsize > 0) {
    442   1.1      fvdl                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
    443   1.1      fvdl                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    444   1.1      fvdl 	}
    445   1.1      fvdl 
    446   1.1      fvdl 	for (i = 0; i < vcset.evs_used && !error; i++) {
    447   1.1      fvdl 		struct exec_vmcmd *vcp;
    448   1.1      fvdl 
    449   1.1      fvdl 		vcp = &vcset.evs_cmds[i];
    450   1.1      fvdl 		error = (*vcp->ev_proc)(p, vcp);
    451   1.1      fvdl 	}
    452   1.1      fvdl 
    453   1.1      fvdl 	kill_vmcmds(&vcset);
    454   1.1      fvdl 
    455   1.1      fvdl 	vrele(vp);
    456   1.1      fvdl 
    457   1.1      fvdl 	return error;
    458   1.1      fvdl }
    459