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