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