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