1 /* $NetBSD: exec_ecoff.c,v 1.27 2008/11/19 18:36:06 ad Exp $ */
4 * Copyright (c) 1994 Adam Glass
5 * Copyright (c) 1993, 1994, 1996, 1999 Christopher G. Demetriou
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: exec_ecoff.c,v 1.27 2008/11/19 18:36:06 ad Exp $");
39 #include "opt_coredump.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/vnode.h>
48 #include <sys/resourcevar.h>
49 #include <sys/module.h>
51 #include <sys/exec_ecoff.h>
54 #define DEP "coredump"
59 MODULE(MODULE_CLASS_MISC
, exec_ecoff
, DEP
)
61 static struct execsw exec_ecoff_execsw
= {
64 { .ecoff_probe_func
= cpu_exec_ecoff_probe
},
69 cpu_exec_ecoff_setregs
,
75 exec_ecoff_modcmd(modcmd_t cmd
, void *arg
)
79 return exec_add(&exec_ecoff_execsw
, 1);
82 return exec_remove(&exec_ecoff_execsw
, 1);
90 * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
92 * Given a proc pointer and an exec package pointer, see if the referent
93 * of the epp is in ecoff format. Check 'standard' magic numbers for
94 * this architecture. If that fails, return failure.
96 * This function is responsible for creating a set of vmcmds which can be
97 * used to build the process's vm space and inserting them into the exec
101 exec_ecoff_makecmds(struct lwp
*l
, struct exec_package
*epp
)
104 struct ecoff_exechdr
*execp
= epp
->ep_hdr
;
106 if (epp
->ep_hdrvalid
< ECOFF_HDR_SIZE
)
109 if (ECOFF_BADMAG(execp
))
112 error
= (*epp
->ep_esch
->u
.ecoff_probe_func
)(l
, epp
);
115 * if there was an error or there are already vmcmds set up,
116 * we return. (the latter can happen if cpu_exec_ecoff_hook()
117 * recursively invokes check_exec() to handle loading of a
118 * dynamically linked binary's shared loader.
120 if (error
|| epp
->ep_vmcmds
.evs_cnt
)
124 * prepare the exec package to map the executable.
126 switch (execp
->a
.magic
) {
128 error
= exec_ecoff_prep_omagic(l
, epp
, epp
->ep_hdr
,
132 error
= exec_ecoff_prep_nmagic(l
, epp
, epp
->ep_hdr
,
136 error
= exec_ecoff_prep_zmagic(l
, epp
, epp
->ep_hdr
,
143 /* set up the stack */
145 error
= (*epp
->ep_esch
->es_setup_stack
)(l
, epp
);
148 kill_vmcmds(&epp
->ep_vmcmds
);
154 * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
157 exec_ecoff_prep_omagic(struct lwp
*l
, struct exec_package
*epp
,
158 struct ecoff_exechdr
*execp
, struct vnode
*vp
)
160 struct ecoff_aouthdr
*eap
= &execp
->a
;
162 epp
->ep_taddr
= ECOFF_SEGMENT_ALIGN(execp
, eap
->text_start
);
163 epp
->ep_tsize
= eap
->tsize
;
164 epp
->ep_daddr
= ECOFF_SEGMENT_ALIGN(execp
, eap
->data_start
);
165 epp
->ep_dsize
= eap
->dsize
+ eap
->bsize
;
166 epp
->ep_entry
= eap
->entry
;
168 /* set up command for text and data segments */
169 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_readvn
,
170 eap
->tsize
+ eap
->dsize
, epp
->ep_taddr
, vp
,
172 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
174 /* set up command for bss segment */
176 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_zero
, eap
->bsize
,
177 ECOFF_SEGMENT_ALIGN(execp
, eap
->bss_start
), NULLVP
, 0,
178 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
184 * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
188 exec_ecoff_prep_nmagic(struct lwp
*l
, struct exec_package
*epp
,
189 struct ecoff_exechdr
*execp
, struct vnode
*vp
)
191 struct ecoff_aouthdr
*eap
= &execp
->a
;
193 epp
->ep_taddr
= ECOFF_SEGMENT_ALIGN(execp
, eap
->text_start
);
194 epp
->ep_tsize
= eap
->tsize
;
195 epp
->ep_daddr
= ECOFF_ROUND(eap
->data_start
, ECOFF_LDPGSZ
);
196 epp
->ep_dsize
= eap
->dsize
+ eap
->bsize
;
197 epp
->ep_entry
= eap
->entry
;
199 /* set up command for text segment */
200 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_readvn
, epp
->ep_tsize
,
201 epp
->ep_taddr
, vp
, ECOFF_TXTOFF(execp
),
202 VM_PROT_READ
|VM_PROT_EXECUTE
);
204 /* set up command for data segment */
205 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_readvn
, epp
->ep_dsize
,
206 epp
->ep_daddr
, vp
, ECOFF_DATOFF(execp
),
207 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
209 /* set up command for bss segment */
211 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_zero
, eap
->bsize
,
212 ECOFF_SEGMENT_ALIGN(execp
, eap
->bss_start
), NULLVP
, 0,
213 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
219 * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
221 * First, set the various offsets/lengths in the exec package.
223 * Then, mark the text image busy (so it can be demand paged) or error
224 * out if this is not possible. Finally, set up vmcmds for the
225 * text, data, bss, and stack segments.
228 exec_ecoff_prep_zmagic(struct lwp
*l
, struct exec_package
*epp
,
229 struct ecoff_exechdr
*execp
, struct vnode
*vp
)
231 struct ecoff_aouthdr
*eap
= &execp
->a
;
234 epp
->ep_taddr
= ECOFF_SEGMENT_ALIGN(execp
, eap
->text_start
);
235 epp
->ep_tsize
= eap
->tsize
;
236 epp
->ep_daddr
= ECOFF_SEGMENT_ALIGN(execp
, eap
->data_start
);
237 epp
->ep_dsize
= eap
->dsize
+ eap
->bsize
;
238 epp
->ep_entry
= eap
->entry
;
240 error
= vn_marktext(vp
);
244 /* set up command for text segment */
245 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_pagedvn
, eap
->tsize
,
246 epp
->ep_taddr
, vp
, ECOFF_TXTOFF(execp
),
247 VM_PROT_READ
|VM_PROT_EXECUTE
);
249 /* set up command for data segment */
250 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_pagedvn
, eap
->dsize
,
251 epp
->ep_daddr
, vp
, ECOFF_DATOFF(execp
),
252 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
254 /* set up command for bss segment */
256 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_zero
, eap
->bsize
,
257 ECOFF_SEGMENT_ALIGN(execp
, eap
->bss_start
), NULLVP
, 0,
258 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);