Home | History | Annotate | Line # | Download | only in kern
exec_ecoff.c revision 1.32
      1  1.32  pgoyette /*	$NetBSD: exec_ecoff.c,v 1.32 2019/11/20 19:37:53 pgoyette Exp $	*/
      2   1.3       cgd 
      3   1.1     glass /*
      4   1.1     glass  * Copyright (c) 1994 Adam Glass
      5  1.10       cgd  * Copyright (c) 1993, 1994, 1996, 1999 Christopher G. Demetriou
      6   1.1     glass  * All rights reserved.
      7   1.1     glass  *
      8   1.1     glass  * Redistribution and use in source and binary forms, with or without
      9   1.1     glass  * modification, are permitted provided that the following conditions
     10   1.1     glass  * are met:
     11   1.1     glass  * 1. Redistributions of source code must retain the above copyright
     12   1.1     glass  *    notice, this list of conditions and the following disclaimer.
     13   1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     15   1.1     glass  *    documentation and/or other materials provided with the distribution.
     16   1.1     glass  * 3. All advertising materials mentioning features or use of this software
     17   1.1     glass  *    must display the following acknowledgement:
     18  1.10       cgd  *      This product includes software developed by Christopher G. Demetriou
     19  1.10       cgd  *      for the NetBSD Project.
     20   1.1     glass  * 4. The name of the author may not be used to endorse or promote products
     21   1.1     glass  *    derived from this software without specific prior written permission
     22   1.1     glass  *
     23   1.1     glass  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24   1.1     glass  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.1     glass  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.1     glass  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27   1.1     glass  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28   1.1     glass  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29   1.1     glass  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30   1.1     glass  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31   1.1     glass  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32   1.1     glass  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.1     glass  */
     34  1.16     lukem 
     35  1.16     lukem #include <sys/cdefs.h>
     36  1.32  pgoyette __KERNEL_RCSID(0, "$NetBSD: exec_ecoff.c,v 1.32 2019/11/20 19:37:53 pgoyette Exp $");
     37   1.1     glass 
     38   1.1     glass #include <sys/param.h>
     39   1.1     glass #include <sys/systm.h>
     40   1.1     glass #include <sys/proc.h>
     41   1.1     glass #include <sys/vnode.h>
     42   1.1     glass #include <sys/exec.h>
     43   1.1     glass #include <sys/resourcevar.h>
     44  1.27        ad #include <sys/module.h>
     45  1.27        ad #include <sys/exec.h>
     46  1.27        ad #include <sys/exec_ecoff.h>
     47  1.27        ad 
     48  1.32  pgoyette MODULE(MODULE_CLASS_EXEC, exec_ecoff, NULL);
     49  1.27        ad 
     50  1.28    cegger static struct execsw exec_ecoff_execsw = {
     51  1.31  christos 	.es_hdrsz = ECOFF_HDR_SIZE,
     52  1.31  christos 	.es_makecmds = exec_ecoff_makecmds,
     53  1.31  christos 	.u = {
     54  1.31  christos 		.ecoff_probe_func = cpu_exec_ecoff_probe,
     55  1.31  christos 	},
     56  1.31  christos 	.es_emul = &emul_netbsd,
     57  1.31  christos 	.es_prio = EXECSW_PRIO_ANY,
     58  1.31  christos 	.es_arglen = 0,
     59  1.31  christos 	.es_copyargs = copyargs,
     60  1.31  christos 	.es_setregs = cpu_exec_ecoff_setregs,
     61  1.31  christos 	.es_coredump = coredump_netbsd,
     62  1.31  christos 	.es_setup_stack = exec_setup_stack,
     63  1.27        ad };
     64  1.27        ad 
     65  1.27        ad static int
     66  1.27        ad exec_ecoff_modcmd(modcmd_t cmd, void *arg)
     67  1.27        ad {
     68  1.27        ad 	switch (cmd) {
     69  1.27        ad 	case MODULE_CMD_INIT:
     70  1.27        ad 		return exec_add(&exec_ecoff_execsw, 1);
     71  1.27        ad 
     72  1.27        ad 	case MODULE_CMD_FINI:
     73  1.27        ad 		return exec_remove(&exec_ecoff_execsw, 1);
     74   1.1     glass 
     75  1.27        ad 	default:
     76  1.27        ad 		return ENOTTY;
     77  1.27        ad         }
     78  1.27        ad }
     79   1.1     glass 
     80   1.1     glass /*
     81   1.1     glass  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
     82   1.1     glass  *
     83   1.1     glass  * Given a proc pointer and an exec package pointer, see if the referent
     84   1.1     glass  * of the epp is in ecoff format.  Check 'standard' magic numbers for
     85   1.1     glass  * this architecture.  If that fails, return failure.
     86   1.1     glass  *
     87   1.1     glass  * This function is  responsible for creating a set of vmcmds which can be
     88   1.1     glass  * used to build the process's vm space and inserting them into the exec
     89   1.1     glass  * package.
     90   1.1     glass  */
     91   1.1     glass int
     92  1.26  christos exec_ecoff_makecmds(struct lwp *l, struct exec_package *epp)
     93   1.1     glass {
     94   1.1     glass 	int error;
     95   1.7       cgd 	struct ecoff_exechdr *execp = epp->ep_hdr;
     96   1.1     glass 
     97   1.1     glass 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
     98   1.1     glass 		return ENOEXEC;
     99   1.1     glass 
    100   1.7       cgd 	if (ECOFF_BADMAG(execp))
    101   1.1     glass 		return ENOEXEC;
    102  1.10       cgd 
    103  1.26  christos 	error = (*epp->ep_esch->u.ecoff_probe_func)(l, epp);
    104  1.10       cgd 
    105  1.10       cgd 	/*
    106  1.10       cgd 	 * if there was an error or there are already vmcmds set up,
    107  1.10       cgd 	 * we return.  (the latter can happen if cpu_exec_ecoff_hook()
    108  1.10       cgd 	 * recursively invokes check_exec() to handle loading of a
    109  1.10       cgd 	 * dynamically linked binary's shared loader.
    110  1.10       cgd 	 */
    111  1.10       cgd 	if (error || epp->ep_vmcmds.evs_cnt)
    112  1.10       cgd 		return (error);
    113  1.10       cgd 
    114  1.10       cgd 	/*
    115  1.10       cgd 	 * prepare the exec package to map the executable.
    116  1.10       cgd 	 */
    117   1.7       cgd 	switch (execp->a.magic) {
    118   1.2     glass 	case ECOFF_OMAGIC:
    119  1.26  christos 		error = exec_ecoff_prep_omagic(l, epp, epp->ep_hdr,
    120  1.10       cgd 		   epp->ep_vp);
    121   1.2     glass 		break;
    122   1.2     glass 	case ECOFF_NMAGIC:
    123  1.26  christos 		error = exec_ecoff_prep_nmagic(l, epp, epp->ep_hdr,
    124  1.10       cgd 		   epp->ep_vp);
    125   1.2     glass 		break;
    126   1.1     glass 	case ECOFF_ZMAGIC:
    127  1.26  christos 		error = exec_ecoff_prep_zmagic(l, epp, epp->ep_hdr,
    128  1.10       cgd 		   epp->ep_vp);
    129   1.1     glass 		break;
    130   1.1     glass 	default:
    131   1.1     glass 		return ENOEXEC;
    132   1.1     glass 	}
    133   1.1     glass 
    134  1.10       cgd 	/* set up the stack */
    135  1.10       cgd 	if (!error)
    136  1.26  christos 		error = (*epp->ep_esch->es_setup_stack)(l, epp);
    137   1.1     glass 
    138   1.1     glass 	if (error)
    139   1.1     glass 		kill_vmcmds(&epp->ep_vmcmds);
    140   1.1     glass 
    141   1.1     glass 	return error;
    142   1.2     glass }
    143   1.1     glass 
    144   1.1     glass /*
    145   1.1     glass  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
    146   1.1     glass  */
    147   1.2     glass int
    148  1.26  christos exec_ecoff_prep_omagic(struct lwp *l, struct exec_package *epp,
    149  1.13   thorpej     struct ecoff_exechdr *execp, struct vnode *vp)
    150   1.1     glass {
    151   1.7       cgd 	struct ecoff_aouthdr *eap = &execp->a;
    152   1.7       cgd 
    153   1.7       cgd 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    154   1.7       cgd 	epp->ep_tsize = eap->tsize;
    155   1.7       cgd 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    156   1.7       cgd 	epp->ep_dsize = eap->dsize + eap->bsize;
    157   1.7       cgd 	epp->ep_entry = eap->entry;
    158   1.1     glass 
    159   1.1     glass 	/* set up command for text and data segments */
    160   1.1     glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    161  1.10       cgd 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
    162   1.7       cgd 	    ECOFF_TXTOFF(execp),
    163   1.7       cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    164   1.1     glass 
    165   1.1     glass 	/* set up command for bss segment */
    166   1.7       cgd 	if (eap->bsize > 0)
    167   1.7       cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    168   1.7       cgd 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    169   1.7       cgd 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    170  1.25     perry 
    171  1.10       cgd 	return 0;
    172   1.1     glass }
    173   1.1     glass 
    174   1.1     glass /*
    175   1.2     glass  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
    176   1.2     glass  *                           package.
    177   1.2     glass  */
    178   1.2     glass int
    179  1.26  christos exec_ecoff_prep_nmagic(struct lwp *l, struct exec_package *epp,
    180  1.13   thorpej     struct ecoff_exechdr *execp, struct vnode *vp)
    181   1.2     glass {
    182   1.7       cgd 	struct ecoff_aouthdr *eap = &execp->a;
    183   1.7       cgd 
    184   1.7       cgd 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    185   1.7       cgd 	epp->ep_tsize = eap->tsize;
    186   1.7       cgd 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
    187   1.7       cgd 	epp->ep_dsize = eap->dsize + eap->bsize;
    188   1.7       cgd 	epp->ep_entry = eap->entry;
    189   1.2     glass 
    190   1.2     glass 	/* set up command for text segment */
    191   1.2     glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
    192  1.10       cgd 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    193   1.7       cgd 	    VM_PROT_READ|VM_PROT_EXECUTE);
    194   1.2     glass 
    195   1.2     glass 	/* set up command for data segment */
    196   1.2     glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
    197  1.10       cgd 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    198   1.7       cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    199   1.2     glass 
    200   1.2     glass 	/* set up command for bss segment */
    201   1.7       cgd 	if (eap->bsize > 0)
    202   1.7       cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    203   1.7       cgd 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    204   1.7       cgd 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    205   1.2     glass 
    206  1.10       cgd 	return 0;
    207   1.2     glass }
    208   1.2     glass 
    209   1.2     glass /*
    210   1.2     glass  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
    211   1.1     glass  *
    212   1.2     glass  * First, set the various offsets/lengths in the exec package.
    213   1.1     glass  *
    214   1.2     glass  * Then, mark the text image busy (so it can be demand paged) or error
    215   1.2     glass  * out if this is not possible.  Finally, set up vmcmds for the
    216   1.2     glass  * text, data, bss, and stack segments.
    217   1.1     glass  */
    218   1.1     glass int
    219  1.26  christos exec_ecoff_prep_zmagic(struct lwp *l, struct exec_package *epp,
    220  1.13   thorpej     struct ecoff_exechdr *execp, struct vnode *vp)
    221   1.1     glass {
    222   1.7       cgd 	struct ecoff_aouthdr *eap = &execp->a;
    223  1.18       chs 	int error;
    224   1.7       cgd 
    225   1.7       cgd 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    226   1.7       cgd 	epp->ep_tsize = eap->tsize;
    227   1.7       cgd 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    228   1.7       cgd 	epp->ep_dsize = eap->dsize + eap->bsize;
    229   1.7       cgd 	epp->ep_entry = eap->entry;
    230   1.1     glass 
    231  1.19       chs 	error = vn_marktext(vp);
    232  1.18       chs 	if (error)
    233  1.18       chs 		return (error);
    234   1.2     glass 
    235   1.2     glass 	/* set up command for text segment */
    236   1.7       cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
    237  1.10       cgd 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    238   1.7       cgd 	    VM_PROT_READ|VM_PROT_EXECUTE);
    239   1.2     glass 
    240   1.2     glass 	/* set up command for data segment */
    241   1.7       cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
    242  1.10       cgd 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    243   1.7       cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    244   1.2     glass 
    245   1.2     glass 	/* set up command for bss segment */
    246  1.24  christos 	if (eap->bsize > 0)
    247  1.24  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    248  1.24  christos 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    249  1.24  christos 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    250   1.1     glass 
    251  1.10       cgd 	return 0;
    252   1.1     glass }
    253