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