Home | History | Annotate | Line # | Download | only in common
linux_exec_aout.c revision 1.62.2.1
      1 /*	$NetBSD: linux_exec_aout.c,v 1.62.2.1 2007/12/08 18:18:47 mjf 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/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.62.2.1 2007/12/08 18:18:47 mjf Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/proc.h>
     50 #include <sys/malloc.h>
     51 #include <sys/namei.h>
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/exec.h>
     55 #include <sys/exec_elf.h>
     56 
     57 #include <sys/mman.h>
     58 #include <sys/syscallargs.h>
     59 
     60 #include <sys/cpu.h>
     61 #include <machine/reg.h>
     62 
     63 #include <compat/linux/common/linux_types.h>
     64 #include <compat/linux/common/linux_signal.h>
     65 #include <compat/linux/common/linux_util.h>
     66 #include <compat/linux/common/linux_exec.h>
     67 #include <compat/linux/common/linux_machdep.h>
     68 
     69 #include <compat/linux/linux_syscallargs.h>
     70 #include <compat/linux/linux_syscall.h>
     71 
     72 int linux_aout_copyargs(struct lwp *, struct exec_package *,
     73     struct ps_strings *, char **, void *);
     74 
     75 static int exec_linux_aout_prep_zmagic(struct lwp *,
     76     struct exec_package *);
     77 static int exec_linux_aout_prep_nmagic(struct lwp *,
     78     struct exec_package *);
     79 static int exec_linux_aout_prep_omagic(struct lwp *,
     80     struct exec_package *);
     81 static int exec_linux_aout_prep_qmagic(struct lwp *,
     82     struct exec_package *);
     83 
     84 int
     85 linux_aout_copyargs(struct lwp *l, struct exec_package *pack,
     86     struct ps_strings *arginfo, char **stackp, void *argp)
     87 {
     88 	char **cpp = (char **)*stackp;
     89 	char **stk = (char **)*stackp;
     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 	int error;
     96 
     97 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
     98 		return error;
     99 
    100 	/* leave room for envp and argv */
    101 	cpp += 2;
    102 	if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0)
    103 		return error;
    104 
    105 	dp = (char *) (cpp + argc + envc + 2);
    106 	sp = argp;
    107 
    108 	/* XXX don't copy them out, remap them! */
    109 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    110 
    111 	for (; --argc >= 0; sp += len, dp += len)
    112 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    113 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    114 			return error;
    115 
    116 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    117 		return error;
    118 
    119 	if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0)
    120 		return error;
    121 
    122 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    123 
    124 	for (; --envc >= 0; sp += len, dp += len)
    125 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    126 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    127 			return error;
    128 
    129 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    130 		return error;
    131 
    132 	*stackp = (char *)cpp;
    133 	return 0;
    134 }
    135 
    136 int
    137 exec_linux_aout_makecmds(l, epp)
    138 	struct lwp *l;
    139 	struct exec_package *epp;
    140 {
    141 	struct exec *linux_ep = epp->ep_hdr;
    142 	int machtype, magic;
    143 	int error = ENOEXEC;
    144 
    145 	magic = LINUX_N_MAGIC(linux_ep);
    146 	machtype = LINUX_N_MACHTYPE(linux_ep);
    147 
    148 
    149 	if (machtype != LINUX_MID_MACHINE)
    150 		return (ENOEXEC);
    151 
    152 	switch (magic) {
    153 	case QMAGIC:
    154 		error = exec_linux_aout_prep_qmagic(l, epp);
    155 		break;
    156 	case ZMAGIC:
    157 		error = exec_linux_aout_prep_zmagic(l, epp);
    158 		break;
    159 	case NMAGIC:
    160 		error = exec_linux_aout_prep_nmagic(l, epp);
    161 		break;
    162 	case OMAGIC:
    163 		error = exec_linux_aout_prep_omagic(l, epp);
    164 		break;
    165 	}
    166 	return error;
    167 }
    168 
    169 /*
    170  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    171  * is very likely not page aligned on most architectures, it is treated
    172  * as an NMAGIC here. XXX
    173  */
    174 
    175 static int
    176 exec_linux_aout_prep_zmagic(l, epp)
    177 	struct lwp *l;
    178 	struct exec_package *epp;
    179 {
    180 	struct exec *execp = epp->ep_hdr;
    181 
    182 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    183 	epp->ep_tsize = execp->a_text;
    184 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    185 	epp->ep_dsize = execp->a_data + execp->a_bss;
    186 	epp->ep_entry = execp->a_entry;
    187 
    188 	/* set up command for text segment */
    189 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    190 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    191 	    VM_PROT_READ|VM_PROT_EXECUTE);
    192 
    193 	/* set up command for data segment */
    194 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    195 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    196 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    197 
    198 	/* set up command for bss segment */
    199 	if (execp->a_bss)
    200 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    201 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    202 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    203 
    204 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    205 }
    206 
    207 /*
    208  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    209  * Not different from the normal stuff.
    210  */
    211 
    212 static int
    213 exec_linux_aout_prep_nmagic(l, epp)
    214 	struct lwp *l;
    215 	struct exec_package *epp;
    216 {
    217 	struct exec *execp = epp->ep_hdr;
    218 	long bsize, baddr;
    219 
    220 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    221 	epp->ep_tsize = execp->a_text;
    222 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    223 	epp->ep_dsize = execp->a_data + execp->a_bss;
    224 	epp->ep_entry = execp->a_entry;
    225 
    226 	/* set up command for text segment */
    227 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    228 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    229 	    VM_PROT_READ|VM_PROT_EXECUTE);
    230 
    231 	/* set up command for data segment */
    232 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    233 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    234 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    235 
    236 	/* set up command for bss segment */
    237 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    238 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    239 	if (bsize > 0)
    240 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    241 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    242 
    243 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    244 }
    245 
    246 /*
    247  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    248  * Business as usual.
    249  */
    250 
    251 static int
    252 exec_linux_aout_prep_omagic(l, epp)
    253 	struct lwp *l;
    254 	struct exec_package *epp;
    255 {
    256 	struct exec *execp = epp->ep_hdr;
    257 	long dsize, bsize, baddr;
    258 
    259 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    260 	epp->ep_tsize = execp->a_text;
    261 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    262 	epp->ep_dsize = execp->a_data + execp->a_bss;
    263 	epp->ep_entry = execp->a_entry;
    264 
    265 	/* set up command for text and data segments */
    266 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    267 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    268 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    269 
    270 	/* set up command for bss segment */
    271 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    272 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    273 	if (bsize > 0)
    274 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    275 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    276 
    277 	/*
    278 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    279 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    280 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    281 	 * respectively to page boundaries.
    282 	 * Compensate `ep_dsize' for the amount of data covered by the last
    283 	 * text page.
    284 	 */
    285 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    286 							PAGE_SIZE);
    287 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    288 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    289 }
    290 
    291 static int
    292 exec_linux_aout_prep_qmagic(l, epp)
    293 	struct lwp *l;
    294 	struct exec_package *epp;
    295 {
    296 	struct exec *execp = epp->ep_hdr;
    297 	int error;
    298 
    299 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    300 	epp->ep_tsize = execp->a_text;
    301 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    302 	epp->ep_dsize = execp->a_data + execp->a_bss;
    303 	epp->ep_entry = execp->a_entry;
    304 
    305 	error = vn_marktext(epp->ep_vp);
    306 	if (error)
    307 		return (error);
    308 
    309 	/* set up command for text segment */
    310 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    311 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    312 	    VM_PROT_READ|VM_PROT_EXECUTE);
    313 
    314 	/* set up command for data segment */
    315 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    316 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    317 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    318 
    319 	/* set up command for bss segment */
    320 	if (execp->a_bss)
    321 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    322 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    323 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    324 
    325 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    326 }
    327