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