Home | History | Annotate | Line # | Download | only in kern
exec_ecoff.c revision 1.12
      1  1.12    mrg /*	$NetBSD: exec_ecoff.c,v 1.12 2000/06/27 17:41:08 mrg 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.1  glass 
     35   1.1  glass #include <sys/param.h>
     36   1.1  glass #include <sys/systm.h>
     37   1.1  glass #include <sys/proc.h>
     38   1.1  glass #include <sys/malloc.h>
     39   1.1  glass #include <sys/vnode.h>
     40   1.1  glass #include <sys/exec.h>
     41   1.1  glass #include <sys/resourcevar.h>
     42   1.1  glass 
     43   1.1  glass #include <sys/exec_ecoff.h>
     44   1.1  glass 
     45   1.1  glass /*
     46   1.1  glass  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
     47   1.1  glass  *
     48   1.1  glass  * Given a proc pointer and an exec package pointer, see if the referent
     49   1.1  glass  * of the epp is in ecoff format.  Check 'standard' magic numbers for
     50   1.1  glass  * this architecture.  If that fails, return failure.
     51   1.1  glass  *
     52   1.1  glass  * This function is  responsible for creating a set of vmcmds which can be
     53   1.1  glass  * used to build the process's vm space and inserting them into the exec
     54   1.1  glass  * package.
     55   1.1  glass  */
     56   1.1  glass int
     57   1.1  glass exec_ecoff_makecmds(p, epp)
     58   1.1  glass 	struct proc *p;
     59   1.1  glass 	struct exec_package *epp;
     60   1.1  glass {
     61   1.1  glass 	int error;
     62   1.7    cgd 	struct ecoff_exechdr *execp = epp->ep_hdr;
     63   1.1  glass 
     64   1.1  glass 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
     65   1.1  glass 		return ENOEXEC;
     66   1.1  glass 
     67   1.7    cgd 	if (ECOFF_BADMAG(execp))
     68   1.1  glass 		return ENOEXEC;
     69  1.10    cgd 
     70  1.10    cgd 	error = cpu_exec_ecoff_hook(p, epp);
     71  1.10    cgd 
     72  1.10    cgd 	/*
     73  1.10    cgd 	 * if there was an error or there are already vmcmds set up,
     74  1.10    cgd 	 * we return.  (the latter can happen if cpu_exec_ecoff_hook()
     75  1.10    cgd 	 * recursively invokes check_exec() to handle loading of a
     76  1.10    cgd 	 * dynamically linked binary's shared loader.
     77  1.10    cgd 	 */
     78  1.10    cgd 	if (error || epp->ep_vmcmds.evs_cnt)
     79  1.10    cgd 		return (error);
     80  1.10    cgd 
     81  1.10    cgd 	/*
     82  1.10    cgd 	 * prepare the exec package to map the executable.
     83  1.10    cgd 	 */
     84   1.7    cgd 	switch (execp->a.magic) {
     85   1.2  glass 	case ECOFF_OMAGIC:
     86  1.10    cgd 		error = exec_ecoff_prep_omagic(p, epp, epp->ep_hdr,
     87  1.10    cgd 		   epp->ep_vp);
     88   1.2  glass 		break;
     89   1.2  glass 	case ECOFF_NMAGIC:
     90  1.10    cgd 		error = exec_ecoff_prep_nmagic(p, epp, epp->ep_hdr,
     91  1.10    cgd 		   epp->ep_vp);
     92   1.2  glass 		break;
     93   1.1  glass 	case ECOFF_ZMAGIC:
     94  1.10    cgd 		error = exec_ecoff_prep_zmagic(p, epp, epp->ep_hdr,
     95  1.10    cgd 		   epp->ep_vp);
     96   1.1  glass 		break;
     97   1.1  glass 	default:
     98   1.1  glass 		return ENOEXEC;
     99   1.1  glass 	}
    100   1.1  glass 
    101  1.10    cgd 	/* set up the stack */
    102  1.10    cgd 	if (!error)
    103  1.10    cgd 		error = exec_ecoff_setup_stack(p, epp);
    104   1.1  glass 
    105   1.1  glass 	if (error)
    106   1.1  glass 		kill_vmcmds(&epp->ep_vmcmds);
    107   1.1  glass 
    108   1.1  glass 	return error;
    109   1.1  glass }
    110   1.1  glass 
    111   1.1  glass /*
    112   1.2  glass  * exec_ecoff_setup_stack(): Set up the stack segment for an ecoff
    113   1.2  glass  * executable.
    114   1.1  glass  *
    115   1.2  glass  * Note that the ep_ssize parameter must be set to be the current stack
    116   1.2  glass  * limit; this is adjusted in the body of execve() to yield the
    117   1.2  glass  * appropriate stack segment usage once the argument length is
    118   1.2  glass  * calculated.
    119   1.1  glass  *
    120   1.2  glass  * This function returns an int for uniformity with other (future) formats'
    121   1.2  glass  * stack setup functions.  They might have errors to return.
    122   1.1  glass  */
    123   1.2  glass int
    124   1.2  glass exec_ecoff_setup_stack(p, epp)
    125   1.1  glass 	struct proc *p;
    126   1.1  glass 	struct exec_package *epp;
    127   1.1  glass {
    128   1.2  glass 
    129   1.2  glass 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    130   1.2  glass 	epp->ep_minsaddr = USRSTACK;
    131   1.2  glass 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    132   1.1  glass 
    133   1.1  glass 	/*
    134   1.2  glass 	 * set up commands for stack.  note that this takes *two*, one to
    135   1.2  glass 	 * map the part of the stack which we can access, and one to map
    136   1.2  glass 	 * the part which we can't.
    137   1.2  glass 	 *
    138   1.2  glass 	 * arguably, it could be made into one, but that would require the
    139   1.2  glass 	 * addition of another mapping proc, which is unnecessary
    140   1.2  glass 	 *
    141   1.9    cgd 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
    142   1.2  glass 	 * <stack> ep_minsaddr
    143   1.1  glass 	 */
    144   1.2  glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    145   1.7    cgd 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    146   1.7    cgd 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
    147   1.2  glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    148   1.7    cgd 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    149   1.7    cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    150   1.1  glass 
    151   1.2  glass 	return 0;
    152   1.2  glass }
    153   1.1  glass 
    154   1.1  glass /*
    155   1.1  glass  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
    156   1.1  glass  */
    157   1.2  glass int
    158  1.10    cgd exec_ecoff_prep_omagic(p, epp, execp, vp)
    159   1.1  glass 	struct proc *p;
    160   1.1  glass 	struct exec_package *epp;
    161  1.10    cgd 	struct ecoff_exechdr *execp;
    162  1.10    cgd 	struct vnode *vp;
    163   1.1  glass {
    164   1.7    cgd 	struct ecoff_aouthdr *eap = &execp->a;
    165   1.7    cgd 
    166   1.7    cgd 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    167   1.7    cgd 	epp->ep_tsize = eap->tsize;
    168   1.7    cgd 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    169   1.7    cgd 	epp->ep_dsize = eap->dsize + eap->bsize;
    170   1.7    cgd 	epp->ep_entry = eap->entry;
    171   1.1  glass 
    172   1.1  glass 	/* set up command for text and data segments */
    173   1.1  glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    174  1.10    cgd 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
    175   1.7    cgd 	    ECOFF_TXTOFF(execp),
    176   1.7    cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    177   1.1  glass 
    178   1.1  glass 	/* set up command for bss segment */
    179   1.7    cgd 	if (eap->bsize > 0)
    180   1.7    cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    181   1.7    cgd 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    182   1.7    cgd 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    183   1.1  glass 
    184  1.10    cgd 	return 0;
    185   1.1  glass }
    186   1.1  glass 
    187   1.1  glass /*
    188   1.2  glass  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
    189   1.2  glass  *                           package.
    190   1.2  glass  */
    191   1.2  glass int
    192  1.10    cgd exec_ecoff_prep_nmagic(p, epp, execp, vp)
    193   1.2  glass 	struct proc *p;
    194   1.2  glass 	struct exec_package *epp;
    195  1.10    cgd 	struct ecoff_exechdr *execp;
    196  1.10    cgd 	struct vnode *vp;
    197   1.2  glass {
    198   1.7    cgd 	struct ecoff_aouthdr *eap = &execp->a;
    199   1.7    cgd 
    200   1.7    cgd 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    201   1.7    cgd 	epp->ep_tsize = eap->tsize;
    202   1.7    cgd 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
    203   1.7    cgd 	epp->ep_dsize = eap->dsize + eap->bsize;
    204   1.7    cgd 	epp->ep_entry = eap->entry;
    205   1.2  glass 
    206   1.2  glass 	/* set up command for text segment */
    207   1.2  glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
    208  1.10    cgd 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    209   1.7    cgd 	    VM_PROT_READ|VM_PROT_EXECUTE);
    210   1.2  glass 
    211   1.2  glass 	/* set up command for data segment */
    212   1.2  glass 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
    213  1.10    cgd 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    214   1.7    cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    215   1.2  glass 
    216   1.2  glass 	/* set up command for bss segment */
    217   1.7    cgd 	if (eap->bsize > 0)
    218   1.7    cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    219   1.7    cgd 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    220   1.7    cgd 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    221   1.2  glass 
    222  1.10    cgd 	return 0;
    223   1.2  glass }
    224   1.2  glass 
    225   1.2  glass /*
    226   1.2  glass  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
    227   1.1  glass  *
    228   1.2  glass  * First, set the various offsets/lengths in the exec package.
    229   1.1  glass  *
    230   1.2  glass  * Then, mark the text image busy (so it can be demand paged) or error
    231   1.2  glass  * out if this is not possible.  Finally, set up vmcmds for the
    232   1.2  glass  * text, data, bss, and stack segments.
    233   1.1  glass  */
    234   1.1  glass int
    235  1.10    cgd exec_ecoff_prep_zmagic(p, epp, execp, vp)
    236   1.1  glass 	struct proc *p;
    237   1.1  glass 	struct exec_package *epp;
    238  1.10    cgd 	struct ecoff_exechdr *execp;
    239  1.10    cgd 	struct vnode *vp;
    240   1.1  glass {
    241   1.7    cgd 	struct ecoff_aouthdr *eap = &execp->a;
    242   1.7    cgd 
    243   1.7    cgd 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    244   1.7    cgd 	epp->ep_tsize = eap->tsize;
    245   1.7    cgd 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    246   1.7    cgd 	epp->ep_dsize = eap->dsize + eap->bsize;
    247   1.7    cgd 	epp->ep_entry = eap->entry;
    248   1.1  glass 
    249   1.1  glass 	/*
    250   1.2  glass 	 * check if vnode is in open for writing, because we want to
    251   1.2  glass 	 * demand-page out of it.  if it is, don't do it, for various
    252   1.2  glass 	 * reasons
    253   1.1  glass 	 */
    254   1.7    cgd 	if ((eap->tsize != 0 || eap->dsize != 0) &&
    255  1.10    cgd 	    vp->v_writecount != 0) {
    256   1.2  glass #ifdef DIAGNOSTIC
    257  1.10    cgd 		if (vp->v_flag & VTEXT)
    258   1.2  glass 			panic("exec: a VTEXT vnode has writecount != 0\n");
    259   1.2  glass #endif
    260   1.2  glass 		return ETXTBSY;
    261   1.2  glass 	}
    262  1.11    chs 	vn_marktext(vp);
    263   1.2  glass 
    264   1.2  glass 	/* set up command for text segment */
    265   1.7    cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
    266  1.10    cgd 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    267   1.7    cgd 	    VM_PROT_READ|VM_PROT_EXECUTE);
    268   1.2  glass 
    269   1.2  glass 	/* set up command for data segment */
    270   1.7    cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
    271  1.10    cgd 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    272   1.7    cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    273   1.2  glass 
    274   1.2  glass 	/* set up command for bss segment */
    275   1.7    cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    276   1.7    cgd 	    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    277   1.7    cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    278   1.1  glass 
    279  1.10    cgd 	return 0;
    280   1.1  glass }
    281