Home | History | Annotate | Line # | Download | only in kern
exec_ecoff.c revision 1.33
      1 /*	$NetBSD: exec_ecoff.c,v 1.33 2024/12/06 16:18:41 riastradh 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.33 2024/12/06 16:18:41 riastradh Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 
     41 #include <sys/exec.h>
     42 #include <sys/exec.h>
     43 #include <sys/exec_ecoff.h>
     44 #include <sys/module.h>
     45 #include <sys/proc.h>
     46 #include <sys/resourcevar.h>
     47 #include <sys/systm.h>
     48 #include <sys/vnode.h>
     49 
     50 MODULE(MODULE_CLASS_EXEC, exec_ecoff, NULL);
     51 
     52 static struct execsw exec_ecoff_execsw = {
     53 	.es_hdrsz = ECOFF_HDR_SIZE,
     54 	.es_makecmds = exec_ecoff_makecmds,
     55 	.u = {
     56 		.ecoff_probe_func = cpu_exec_ecoff_probe,
     57 	},
     58 	.es_emul = &emul_netbsd,
     59 	.es_prio = EXECSW_PRIO_ANY,
     60 	.es_arglen = 0,
     61 	.es_copyargs = copyargs,
     62 	.es_setregs = cpu_exec_ecoff_setregs,
     63 	.es_coredump = coredump_netbsd,
     64 	.es_setup_stack = exec_setup_stack,
     65 };
     66 
     67 static int
     68 exec_ecoff_modcmd(modcmd_t cmd, void *arg)
     69 {
     70 	switch (cmd) {
     71 	case MODULE_CMD_INIT:
     72 		return exec_add(&exec_ecoff_execsw, 1);
     73 
     74 	case MODULE_CMD_FINI:
     75 		return exec_remove(&exec_ecoff_execsw, 1);
     76 
     77 	default:
     78 		return ENOTTY;
     79         }
     80 }
     81 
     82 /*
     83  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
     84  *
     85  * Given a proc pointer and an exec package pointer, see if the referent
     86  * of the epp is in ecoff format.  Check 'standard' magic numbers for
     87  * this architecture.  If that fails, return failure.
     88  *
     89  * This function is  responsible for creating a set of vmcmds which can be
     90  * used to build the process's vm space and inserting them into the exec
     91  * package.
     92  */
     93 int
     94 exec_ecoff_makecmds(struct lwp *l, struct exec_package *epp)
     95 {
     96 	int error;
     97 	struct ecoff_exechdr *execp = epp->ep_hdr;
     98 
     99 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
    100 		return ENOEXEC;
    101 
    102 	if (ECOFF_BADMAG(execp))
    103 		return ENOEXEC;
    104 
    105 	error = (*epp->ep_esch->u.ecoff_probe_func)(l, epp);
    106 
    107 	/*
    108 	 * if there was an error or there are already vmcmds set up,
    109 	 * we return.  (the latter can happen if cpu_exec_ecoff_hook()
    110 	 * recursively invokes check_exec() to handle loading of a
    111 	 * dynamically linked binary's shared loader.
    112 	 */
    113 	if (error || epp->ep_vmcmds.evs_cnt)
    114 		return (error);
    115 
    116 	/*
    117 	 * prepare the exec package to map the executable.
    118 	 */
    119 	switch (execp->a.magic) {
    120 	case ECOFF_OMAGIC:
    121 		error = exec_ecoff_prep_omagic(l, epp, epp->ep_hdr,
    122 		   epp->ep_vp);
    123 		break;
    124 	case ECOFF_NMAGIC:
    125 		error = exec_ecoff_prep_nmagic(l, epp, epp->ep_hdr,
    126 		   epp->ep_vp);
    127 		break;
    128 	case ECOFF_ZMAGIC:
    129 		error = exec_ecoff_prep_zmagic(l, epp, epp->ep_hdr,
    130 		   epp->ep_vp);
    131 		break;
    132 	default:
    133 		return ENOEXEC;
    134 	}
    135 
    136 	/* set up the stack */
    137 	if (!error)
    138 		error = (*epp->ep_esch->es_setup_stack)(l, epp);
    139 
    140 	if (error)
    141 		kill_vmcmds(&epp->ep_vmcmds);
    142 
    143 	return error;
    144 }
    145 
    146 /*
    147  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
    148  */
    149 int
    150 exec_ecoff_prep_omagic(struct lwp *l, struct exec_package *epp,
    151     struct ecoff_exechdr *execp, struct vnode *vp)
    152 {
    153 	struct ecoff_aouthdr *eap = &execp->a;
    154 
    155 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    156 	epp->ep_tsize = eap->tsize;
    157 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    158 	epp->ep_dsize = eap->dsize + eap->bsize;
    159 	epp->ep_entry = eap->entry;
    160 
    161 	/* set up command for text and data segments */
    162 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    163 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
    164 	    ECOFF_TXTOFF(execp),
    165 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    166 
    167 	/* set up command for bss segment */
    168 	if (eap->bsize > 0)
    169 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    170 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    171 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    172 
    173 	return 0;
    174 }
    175 
    176 /*
    177  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
    178  *                           package.
    179  */
    180 int
    181 exec_ecoff_prep_nmagic(struct lwp *l, struct exec_package *epp,
    182     struct ecoff_exechdr *execp, struct vnode *vp)
    183 {
    184 	struct ecoff_aouthdr *eap = &execp->a;
    185 
    186 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    187 	epp->ep_tsize = eap->tsize;
    188 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
    189 	epp->ep_dsize = eap->dsize + eap->bsize;
    190 	epp->ep_entry = eap->entry;
    191 
    192 	/* set up command for text segment */
    193 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
    194 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    195 	    VM_PROT_READ|VM_PROT_EXECUTE);
    196 
    197 	/* set up command for data segment */
    198 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
    199 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    200 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    201 
    202 	/* set up command for bss segment */
    203 	if (eap->bsize > 0)
    204 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    205 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    206 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    207 
    208 	return 0;
    209 }
    210 
    211 /*
    212  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
    213  *
    214  * First, set the various offsets/lengths in the exec package.
    215  *
    216  * Then, mark the text image busy (so it can be demand paged) or error
    217  * out if this is not possible.  Finally, set up vmcmds for the
    218  * text, data, bss, and stack segments.
    219  */
    220 int
    221 exec_ecoff_prep_zmagic(struct lwp *l, struct exec_package *epp,
    222     struct ecoff_exechdr *execp, struct vnode *vp)
    223 {
    224 	struct ecoff_aouthdr *eap = &execp->a;
    225 	int error;
    226 
    227 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    228 	epp->ep_tsize = eap->tsize;
    229 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    230 	epp->ep_dsize = eap->dsize + eap->bsize;
    231 	epp->ep_entry = eap->entry;
    232 
    233 	error = vn_marktext(vp);
    234 	if (error)
    235 		return (error);
    236 
    237 	/* set up command for text segment */
    238 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
    239 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    240 	    VM_PROT_READ|VM_PROT_EXECUTE);
    241 
    242 	/* set up command for data segment */
    243 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
    244 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    245 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    246 
    247 	/* set up command for bss segment */
    248 	if (eap->bsize > 0)
    249 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    250 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    251 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    252 
    253 	return 0;
    254 }
    255