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