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