Home | History | Annotate | Line # | Download | only in common
linux_exec_aout.c revision 1.65.2.1
      1 /*	$NetBSD: linux_exec_aout.c,v 1.65.2.1 2008/05/10 23:48:55 wrstuden 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.65.2.1 2008/05/10 23:48:55 wrstuden Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/malloc.h>
     44 #include <sys/namei.h>
     45 #include <sys/vnode.h>
     46 #include <sys/mount.h>
     47 #include <sys/exec.h>
     48 #include <sys/exec_elf.h>
     49 
     50 #include <sys/mman.h>
     51 #include <sys/sa.h>
     52 #include <sys/syscallargs.h>
     53 
     54 #include <sys/cpu.h>
     55 #include <machine/reg.h>
     56 
     57 #include <compat/linux/common/linux_types.h>
     58 #include <compat/linux/common/linux_signal.h>
     59 #include <compat/linux/common/linux_util.h>
     60 #include <compat/linux/common/linux_exec.h>
     61 #include <compat/linux/common/linux_machdep.h>
     62 
     63 #include <compat/linux/linux_syscallargs.h>
     64 #include <compat/linux/linux_syscall.h>
     65 
     66 int linux_aout_copyargs(struct lwp *, struct exec_package *,
     67     struct ps_strings *, char **, void *);
     68 
     69 static int exec_linux_aout_prep_zmagic(struct lwp *,
     70     struct exec_package *);
     71 static int exec_linux_aout_prep_nmagic(struct lwp *,
     72     struct exec_package *);
     73 static int exec_linux_aout_prep_omagic(struct lwp *,
     74     struct exec_package *);
     75 static int exec_linux_aout_prep_qmagic(struct lwp *,
     76     struct exec_package *);
     77 
     78 int
     79 linux_aout_copyargs(struct lwp *l, struct exec_package *pack,
     80     struct ps_strings *arginfo, char **stackp, void *argp)
     81 {
     82 	char **cpp = (char **)*stackp;
     83 	char **stk = (char **)*stackp;
     84 	char *dp, *sp;
     85 	size_t len;
     86 	void *nullp = NULL;
     87 	int argc = arginfo->ps_nargvstr;
     88 	int envc = arginfo->ps_nenvstr;
     89 	int error;
     90 
     91 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
     92 		return error;
     93 
     94 	/* leave room for envp and argv */
     95 	cpp += 2;
     96 	if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0)
     97 		return error;
     98 
     99 	dp = (char *) (cpp + argc + envc + 2);
    100 	sp = argp;
    101 
    102 	/* XXX don't copy them out, remap them! */
    103 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    104 
    105 	for (; --argc >= 0; sp += len, dp += len)
    106 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    107 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    108 			return error;
    109 
    110 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    111 		return error;
    112 
    113 	if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0)
    114 		return error;
    115 
    116 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    117 
    118 	for (; --envc >= 0; sp += len, dp += len)
    119 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    120 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    121 			return error;
    122 
    123 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    124 		return error;
    125 
    126 	*stackp = (char *)cpp;
    127 	return 0;
    128 }
    129 
    130 int
    131 exec_linux_aout_makecmds(struct lwp *l, struct exec_package *epp)
    132 {
    133 	struct exec *linux_ep = epp->ep_hdr;
    134 	int machtype, magic;
    135 	int error = ENOEXEC;
    136 
    137 	magic = LINUX_N_MAGIC(linux_ep);
    138 	machtype = LINUX_N_MACHTYPE(linux_ep);
    139 
    140 
    141 	if (machtype != LINUX_MID_MACHINE)
    142 		return (ENOEXEC);
    143 
    144 	switch (magic) {
    145 	case QMAGIC:
    146 		error = exec_linux_aout_prep_qmagic(l, epp);
    147 		break;
    148 	case ZMAGIC:
    149 		error = exec_linux_aout_prep_zmagic(l, epp);
    150 		break;
    151 	case NMAGIC:
    152 		error = exec_linux_aout_prep_nmagic(l, epp);
    153 		break;
    154 	case OMAGIC:
    155 		error = exec_linux_aout_prep_omagic(l, epp);
    156 		break;
    157 	}
    158 	return error;
    159 }
    160 
    161 /*
    162  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    163  * is very likely not page aligned on most architectures, it is treated
    164  * as an NMAGIC here. XXX
    165  */
    166 
    167 static int
    168 exec_linux_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
    169 {
    170 	struct exec *execp = epp->ep_hdr;
    171 
    172 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    173 	epp->ep_tsize = execp->a_text;
    174 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    175 	epp->ep_dsize = execp->a_data + execp->a_bss;
    176 	epp->ep_entry = execp->a_entry;
    177 
    178 	/* set up command for text segment */
    179 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    180 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    181 	    VM_PROT_READ|VM_PROT_EXECUTE);
    182 
    183 	/* set up command for data segment */
    184 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    185 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    186 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    187 
    188 	/* set up command for bss segment */
    189 	if (execp->a_bss)
    190 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    191 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    192 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    193 
    194 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    195 }
    196 
    197 /*
    198  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    199  * Not different from the normal stuff.
    200  */
    201 
    202 static int
    203 exec_linux_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
    204 {
    205 	struct exec *execp = epp->ep_hdr;
    206 	long bsize, baddr;
    207 
    208 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    209 	epp->ep_tsize = execp->a_text;
    210 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    211 	epp->ep_dsize = execp->a_data + execp->a_bss;
    212 	epp->ep_entry = execp->a_entry;
    213 
    214 	/* set up command for text segment */
    215 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    216 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    217 	    VM_PROT_READ|VM_PROT_EXECUTE);
    218 
    219 	/* set up command for data segment */
    220 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    221 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    222 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    223 
    224 	/* set up command for bss segment */
    225 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    226 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    227 	if (bsize > 0)
    228 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    229 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    230 
    231 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    232 }
    233 
    234 /*
    235  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    236  * Business as usual.
    237  */
    238 
    239 static int
    240 exec_linux_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
    241 {
    242 	struct exec *execp = epp->ep_hdr;
    243 	long dsize, bsize, baddr;
    244 
    245 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    246 	epp->ep_tsize = execp->a_text;
    247 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    248 	epp->ep_dsize = execp->a_data + execp->a_bss;
    249 	epp->ep_entry = execp->a_entry;
    250 
    251 	/* set up command for text and data segments */
    252 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    253 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    254 	    LINUX_N_TXTOFF(*execp, OMAGIC), 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, PAGE_SIZE);
    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 	/*
    264 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    265 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    266 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    267 	 * respectively to page boundaries.
    268 	 * Compensate `ep_dsize' for the amount of data covered by the last
    269 	 * text page.
    270 	 */
    271 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    272 							PAGE_SIZE);
    273 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    274 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    275 }
    276 
    277 static int
    278 exec_linux_aout_prep_qmagic(struct lwp *l, struct exec_package *epp)
    279 {
    280 	struct exec *execp = epp->ep_hdr;
    281 	int error;
    282 
    283 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    284 	epp->ep_tsize = execp->a_text;
    285 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    286 	epp->ep_dsize = execp->a_data + execp->a_bss;
    287 	epp->ep_entry = execp->a_entry;
    288 
    289 	error = vn_marktext(epp->ep_vp);
    290 	if (error)
    291 		return (error);
    292 
    293 	/* set up command for text segment */
    294 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    295 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    296 	    VM_PROT_READ|VM_PROT_EXECUTE);
    297 
    298 	/* set up command for data segment */
    299 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    300 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    301 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    302 
    303 	/* set up command for bss segment */
    304 	if (execp->a_bss)
    305 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    306 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    307 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    308 
    309 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    310 }
    311