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