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