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