Home | History | Annotate | Line # | Download | only in common
linux_exec_aout.c revision 1.57.4.1
      1  1.57.4.1      yamt /*	$NetBSD: linux_exec_aout.c,v 1.57.4.1 2006/06/21 14:59:12 yamt Exp $	*/
      2      1.29  christos 
      3      1.29  christos /*-
      4      1.33      fvdl  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5      1.29  christos  * All rights reserved.
      6      1.29  christos  *
      7      1.29  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.33      fvdl  * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
      9      1.29  christos  *
     10      1.29  christos  * Redistribution and use in source and binary forms, with or without
     11      1.29  christos  * modification, are permitted provided that the following conditions
     12      1.29  christos  * are met:
     13      1.29  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.29  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.29  christos  *    documentation and/or other materials provided with the distribution.
     18      1.29  christos  * 3. All advertising materials mentioning features or use of this software
     19      1.29  christos  *    must display the following acknowledgement:
     20      1.31       erh  *	This product includes software developed by the NetBSD
     21      1.31       erh  *	Foundation, Inc. and its contributors.
     22      1.29  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.29  christos  *    contributors may be used to endorse or promote products derived
     24      1.29  christos  *    from this software without specific prior written permission.
     25      1.29  christos  *
     26      1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     37      1.29  christos  */
     38       1.1      fvdl 
     39       1.1      fvdl /*
     40       1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     41       1.1      fvdl  */
     42      1.47     lukem 
     43      1.47     lukem #include <sys/cdefs.h>
     44  1.57.4.1      yamt __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.57.4.1 2006/06/21 14:59:12 yamt Exp $");
     45       1.1      fvdl 
     46       1.1      fvdl #include <sys/param.h>
     47       1.1      fvdl #include <sys/systm.h>
     48       1.1      fvdl #include <sys/kernel.h>
     49       1.1      fvdl #include <sys/proc.h>
     50       1.1      fvdl #include <sys/malloc.h>
     51       1.1      fvdl #include <sys/namei.h>
     52       1.1      fvdl #include <sys/vnode.h>
     53      1.13  christos #include <sys/mount.h>
     54      1.25  christos #include <sys/exec.h>
     55       1.8      fvdl #include <sys/exec_elf.h>
     56       1.1      fvdl 
     57       1.1      fvdl #include <sys/mman.h>
     58      1.51   thorpej #include <sys/sa.h>
     59      1.13  christos #include <sys/syscallargs.h>
     60       1.1      fvdl 
     61      1.32  christos #include <machine/cpu.h>
     62      1.32  christos #include <machine/reg.h>
     63      1.32  christos 
     64      1.32  christos #include <compat/linux/common/linux_types.h>
     65      1.32  christos #include <compat/linux/common/linux_signal.h>
     66      1.32  christos #include <compat/linux/common/linux_util.h>
     67      1.32  christos #include <compat/linux/common/linux_exec.h>
     68      1.32  christos #include <compat/linux/common/linux_machdep.h>
     69      1.32  christos 
     70      1.32  christos #include <compat/linux/linux_syscallargs.h>
     71       1.4  christos #include <compat/linux/linux_syscall.h>
     72      1.31       erh 
     73  1.57.4.1      yamt int linux_aout_copyargs __P((struct lwp *, struct exec_package *,
     74      1.48  christos     struct ps_strings *, char **, void *));
     75       1.4  christos 
     76  1.57.4.1      yamt static int exec_linux_aout_prep_zmagic __P((struct lwp *,
     77      1.45  christos     struct exec_package *));
     78  1.57.4.1      yamt static int exec_linux_aout_prep_nmagic __P((struct lwp *,
     79      1.45  christos     struct exec_package *));
     80  1.57.4.1      yamt static int exec_linux_aout_prep_omagic __P((struct lwp *,
     81      1.45  christos     struct exec_package *));
     82  1.57.4.1      yamt static int exec_linux_aout_prep_qmagic __P((struct lwp *,
     83      1.45  christos     struct exec_package *));
     84       1.6      fvdl 
     85      1.45  christos int
     86  1.57.4.1      yamt linux_aout_copyargs(l, pack, arginfo, stackp, argp)
     87  1.57.4.1      yamt 	struct lwp *l;
     88       1.4  christos 	struct exec_package *pack;
     89       1.4  christos 	struct ps_strings *arginfo;
     90      1.45  christos 	char **stackp;
     91       1.4  christos 	void *argp;
     92       1.4  christos {
     93      1.45  christos 	char **cpp = (char **)*stackp;
     94      1.45  christos 	char **stk = (char **)*stackp;
     95       1.4  christos 	char *dp, *sp;
     96       1.4  christos 	size_t len;
     97       1.4  christos 	void *nullp = NULL;
     98       1.4  christos 	int argc = arginfo->ps_nargvstr;
     99       1.4  christos 	int envc = arginfo->ps_nenvstr;
    100      1.45  christos 	int error;
    101       1.4  christos 
    102      1.45  christos 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
    103      1.45  christos 		return error;
    104       1.4  christos 
    105       1.4  christos 	/* leave room for envp and argv */
    106       1.4  christos 	cpp += 2;
    107      1.45  christos 	if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0)
    108      1.45  christos 		return error;
    109       1.4  christos 
    110       1.4  christos 	dp = (char *) (cpp + argc + envc + 2);
    111       1.4  christos 	sp = argp;
    112       1.4  christos 
    113       1.4  christos 	/* XXX don't copy them out, remap them! */
    114       1.5   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    115       1.4  christos 
    116       1.4  christos 	for (; --argc >= 0; sp += len, dp += len)
    117      1.45  christos 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    118      1.45  christos 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    119      1.45  christos 			return error;
    120       1.4  christos 
    121      1.45  christos 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    122      1.45  christos 		return error;
    123       1.4  christos 
    124      1.45  christos 	if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0)
    125      1.45  christos 		return error;
    126       1.4  christos 
    127       1.5   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    128       1.4  christos 
    129       1.4  christos 	for (; --envc >= 0; sp += len, dp += len)
    130      1.45  christos 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    131      1.45  christos 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    132      1.45  christos 			return error;
    133       1.4  christos 
    134      1.45  christos 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    135      1.45  christos 		return error;
    136       1.4  christos 
    137      1.45  christos 	*stackp = (char *)cpp;
    138      1.45  christos 	return 0;
    139       1.4  christos }
    140       1.4  christos 
    141       1.1      fvdl int
    142  1.57.4.1      yamt exec_linux_aout_makecmds(l, epp)
    143  1.57.4.1      yamt 	struct lwp *l;
    144       1.1      fvdl 	struct exec_package *epp;
    145       1.1      fvdl {
    146       1.1      fvdl 	struct exec *linux_ep = epp->ep_hdr;
    147       1.1      fvdl 	int machtype, magic;
    148       1.1      fvdl 	int error = ENOEXEC;
    149       1.1      fvdl 
    150       1.1      fvdl 	magic = LINUX_N_MAGIC(linux_ep);
    151       1.1      fvdl 	machtype = LINUX_N_MACHTYPE(linux_ep);
    152       1.1      fvdl 
    153       1.1      fvdl 
    154       1.1      fvdl 	if (machtype != LINUX_MID_MACHINE)
    155       1.1      fvdl 		return (ENOEXEC);
    156       1.1      fvdl 
    157       1.1      fvdl 	switch (magic) {
    158       1.1      fvdl 	case QMAGIC:
    159  1.57.4.1      yamt 		error = exec_linux_aout_prep_qmagic(l, epp);
    160       1.1      fvdl 		break;
    161       1.1      fvdl 	case ZMAGIC:
    162  1.57.4.1      yamt 		error = exec_linux_aout_prep_zmagic(l, epp);
    163       1.1      fvdl 		break;
    164       1.1      fvdl 	case NMAGIC:
    165  1.57.4.1      yamt 		error = exec_linux_aout_prep_nmagic(l, epp);
    166       1.1      fvdl 		break;
    167       1.1      fvdl 	case OMAGIC:
    168  1.57.4.1      yamt 		error = exec_linux_aout_prep_omagic(l, epp);
    169       1.1      fvdl 		break;
    170       1.1      fvdl 	}
    171       1.1      fvdl 	return error;
    172       1.1      fvdl }
    173       1.1      fvdl 
    174       1.1      fvdl /*
    175       1.1      fvdl  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    176       1.1      fvdl  * is very likely not page aligned on most architectures, it is treated
    177       1.1      fvdl  * as an NMAGIC here. XXX
    178       1.1      fvdl  */
    179       1.1      fvdl 
    180      1.42  jdolecek static int
    181  1.57.4.1      yamt exec_linux_aout_prep_zmagic(l, epp)
    182  1.57.4.1      yamt 	struct lwp *l;
    183       1.1      fvdl 	struct exec_package *epp;
    184       1.1      fvdl {
    185       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    186       1.1      fvdl 
    187       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    188       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    189       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    190       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    191       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    192       1.1      fvdl 
    193       1.1      fvdl 	/* set up command for text segment */
    194       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    195       1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    196       1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    197       1.1      fvdl 
    198       1.1      fvdl 	/* set up command for data segment */
    199       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    200       1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    201       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    202       1.1      fvdl 
    203       1.1      fvdl 	/* set up command for bss segment */
    204      1.56  christos 	if (execp->a_bss)
    205      1.56  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    206      1.56  christos 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    207      1.56  christos 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    208       1.1      fvdl 
    209  1.57.4.1      yamt 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    210       1.1      fvdl }
    211       1.1      fvdl 
    212       1.1      fvdl /*
    213       1.1      fvdl  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    214       1.1      fvdl  * Not different from the normal stuff.
    215       1.1      fvdl  */
    216       1.1      fvdl 
    217      1.42  jdolecek static int
    218  1.57.4.1      yamt exec_linux_aout_prep_nmagic(l, epp)
    219  1.57.4.1      yamt 	struct lwp *l;
    220       1.1      fvdl 	struct exec_package *epp;
    221       1.1      fvdl {
    222       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    223       1.1      fvdl 	long bsize, baddr;
    224       1.1      fvdl 
    225       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    226       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    227       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    228       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    229       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    230       1.1      fvdl 
    231       1.1      fvdl 	/* set up command for text segment */
    232       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    233       1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    234       1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    235       1.1      fvdl 
    236       1.1      fvdl 	/* set up command for data segment */
    237       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    238       1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    239       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    240       1.1      fvdl 
    241       1.1      fvdl 	/* set up command for bss segment */
    242      1.52   thorpej 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    243       1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    244       1.1      fvdl 	if (bsize > 0)
    245       1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    246       1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    247       1.1      fvdl 
    248  1.57.4.1      yamt 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    249       1.1      fvdl }
    250       1.1      fvdl 
    251       1.1      fvdl /*
    252       1.1      fvdl  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    253       1.1      fvdl  * Business as usual.
    254       1.1      fvdl  */
    255       1.1      fvdl 
    256      1.42  jdolecek static int
    257  1.57.4.1      yamt exec_linux_aout_prep_omagic(l, epp)
    258  1.57.4.1      yamt 	struct lwp *l;
    259       1.1      fvdl 	struct exec_package *epp;
    260       1.1      fvdl {
    261       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    262       1.1      fvdl 	long dsize, bsize, baddr;
    263       1.1      fvdl 
    264       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    265       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    266       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    267       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    268       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    269       1.1      fvdl 
    270       1.1      fvdl 	/* set up command for text and data segments */
    271       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    272       1.1      fvdl 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    273       1.1      fvdl 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    274       1.1      fvdl 
    275       1.1      fvdl 	/* set up command for bss segment */
    276      1.52   thorpej 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    277       1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    278       1.1      fvdl 	if (bsize > 0)
    279       1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    280       1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    281       1.1      fvdl 
    282       1.1      fvdl 	/*
    283       1.1      fvdl 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    284       1.1      fvdl 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    285       1.1      fvdl 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    286       1.1      fvdl 	 * respectively to page boundaries.
    287       1.1      fvdl 	 * Compensate `ep_dsize' for the amount of data covered by the last
    288      1.57     perry 	 * text page.
    289       1.1      fvdl 	 */
    290      1.52   thorpej 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    291      1.52   thorpej 							PAGE_SIZE);
    292       1.1      fvdl 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    293  1.57.4.1      yamt 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    294       1.1      fvdl }
    295       1.1      fvdl 
    296      1.42  jdolecek static int
    297  1.57.4.1      yamt exec_linux_aout_prep_qmagic(l, epp)
    298  1.57.4.1      yamt 	struct lwp *l;
    299       1.1      fvdl 	struct exec_package *epp;
    300       1.1      fvdl {
    301       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    302      1.50       chs 	int error;
    303       1.1      fvdl 
    304       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    305       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    306       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    307       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    308       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    309       1.1      fvdl 
    310      1.50       chs 	error = vn_marktext(epp->ep_vp);
    311      1.50       chs 	if (error)
    312      1.50       chs 		return (error);
    313       1.1      fvdl 
    314       1.1      fvdl 	/* set up command for text segment */
    315       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    316       1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    317       1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    318       1.1      fvdl 
    319       1.1      fvdl 	/* set up command for data segment */
    320       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    321       1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    322       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    323       1.1      fvdl 
    324       1.1      fvdl 	/* set up command for bss segment */
    325      1.56  christos 	if (execp->a_bss)
    326      1.56  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    327      1.56  christos 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    328      1.56  christos 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    329       1.1      fvdl 
    330  1.57.4.1      yamt 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    331       1.1      fvdl }
    332