1 /* $NetBSD: exec_aout.c,v 1.33 2005/12/11 12:24:29 christos Exp $ */
4 * Copyright (c) 1993, 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.33 2005/12/11 12:24:29 christos Exp $");
37 #include "opt_coredump.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
46 #include <sys/exec_aout.h>
47 #include <sys/resourcevar.h>
48 #include <sys/module.h>
50 #include <uvm/uvm_extern.h>
53 #define DEP "coredump"
58 MODULE(MODULE_CLASS_MISC
, exec_aout
, DEP
);
60 static struct execsw exec_aout_execsw
[] = {
61 { sizeof(struct exec
),
74 exec_aout_modcmd(modcmd_t cmd
, void *arg
)
79 return exec_add(exec_aout_execsw
,
80 __arraycount(exec_aout_execsw
));
83 return exec_remove(exec_aout_execsw
,
84 __arraycount(exec_aout_execsw
));
92 * exec_aout_makecmds(): Check if it's an a.out-format executable.
94 * Given a lwp pointer and an exec package pointer, see if the referent
95 * of the epp is in a.out format. First check 'standard' magic numbers for
96 * this architecture. If that fails, try a CPU-dependent hook.
98 * This function, in the former case, or the hook, in the latter, is
99 * responsible for creating a set of vmcmds which can be used to build
100 * the process's vm space and inserting them into the exec package.
104 exec_aout_makecmds(struct lwp
*l
, struct exec_package
*epp
)
106 u_long midmag
, magic
;
109 struct exec
*execp
= epp
->ep_hdr
;
111 if (epp
->ep_hdrvalid
< sizeof(struct exec
))
114 midmag
= ntohl(execp
->a_midmag
);
115 mid
= (midmag
>> 16) & 0x3ff;
116 magic
= midmag
& 0xffff;
118 midmag
= mid
<< 16 | magic
;
121 case (MID_MACHINE
<< 16) | ZMAGIC
:
122 error
= exec_aout_prep_zmagic(l
, epp
);
124 case (MID_MACHINE
<< 16) | NMAGIC
:
125 error
= exec_aout_prep_nmagic(l
, epp
);
127 case (MID_MACHINE
<< 16) | OMAGIC
:
128 error
= exec_aout_prep_omagic(l
, epp
);
131 error
= cpu_exec_aout_makecmds(l
, epp
);
135 kill_vmcmds(&epp
->ep_vmcmds
);
141 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
143 * First, set of the various offsets/lengths in the exec package.
145 * Then, mark the text image busy (so it can be demand paged) or error
146 * out if this is not possible. Finally, set up vmcmds for the
147 * text, data, bss, and stack segments.
151 exec_aout_prep_zmagic(struct lwp
*l
, struct exec_package
*epp
)
153 struct exec
*execp
= epp
->ep_hdr
;
156 epp
->ep_taddr
= AOUT_LDPGSZ
;
157 epp
->ep_tsize
= execp
->a_text
;
158 epp
->ep_daddr
= epp
->ep_taddr
+ execp
->a_text
;
159 epp
->ep_dsize
= execp
->a_data
+ execp
->a_bss
;
160 epp
->ep_entry
= execp
->a_entry
;
162 error
= vn_marktext(epp
->ep_vp
);
166 /* set up command for text segment */
167 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_pagedvn
, round_page(execp
->a_text
),
168 epp
->ep_taddr
, epp
->ep_vp
, 0, VM_PROT_READ
|VM_PROT_EXECUTE
);
170 /* set up command for data segment */
171 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_pagedvn
, round_page(execp
->a_data
),
172 epp
->ep_daddr
, epp
->ep_vp
, execp
->a_text
,
173 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
175 /* set up command for bss segment */
176 if (execp
->a_bss
> 0)
177 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_zero
, execp
->a_bss
,
178 epp
->ep_daddr
+ execp
->a_data
, NULLVP
, 0,
179 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
181 return (*epp
->ep_esch
->es_setup_stack
)(l
, epp
);
185 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
189 exec_aout_prep_nmagic(struct lwp
*l
, struct exec_package
*epp
)
191 struct exec
*execp
= epp
->ep_hdr
;
194 epp
->ep_taddr
= AOUT_LDPGSZ
;
195 epp
->ep_tsize
= execp
->a_text
;
196 epp
->ep_daddr
= roundup(epp
->ep_taddr
+ execp
->a_text
, AOUT_LDPGSZ
);
197 epp
->ep_dsize
= execp
->a_data
+ execp
->a_bss
;
198 epp
->ep_entry
= execp
->a_entry
;
200 /* set up command for text segment */
201 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_readvn
, execp
->a_text
,
202 epp
->ep_taddr
, epp
->ep_vp
, sizeof(struct exec
),
203 VM_PROT_READ
|VM_PROT_EXECUTE
);
205 /* set up command for data segment */
206 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_readvn
, execp
->a_data
,
207 epp
->ep_daddr
, epp
->ep_vp
, execp
->a_text
+ sizeof(struct exec
),
208 VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
210 /* set up command for bss segment */
211 baddr
= round_page(epp
->ep_daddr
+ execp
->a_data
);
212 bsize
= epp
->ep_daddr
+ epp
->ep_dsize
- baddr
;
214 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_zero
, bsize
, baddr
,
215 NULLVP
, 0, VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
217 return (*epp
->ep_esch
->es_setup_stack
)(l
, epp
);
221 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
225 exec_aout_prep_omagic(struct lwp
*l
, struct exec_package
*epp
)
227 struct exec
*execp
= epp
->ep_hdr
;
228 long dsize
, bsize
, baddr
;
230 epp
->ep_taddr
= AOUT_LDPGSZ
;
231 epp
->ep_tsize
= execp
->a_text
;
232 epp
->ep_daddr
= epp
->ep_taddr
+ execp
->a_text
;
233 epp
->ep_dsize
= execp
->a_data
+ execp
->a_bss
;
234 epp
->ep_entry
= execp
->a_entry
;
236 /* set up command for text and data segments */
237 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_readvn
,
238 execp
->a_text
+ execp
->a_data
, epp
->ep_taddr
, epp
->ep_vp
,
239 sizeof(struct exec
), VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
241 /* set up command for bss segment */
242 baddr
= round_page(epp
->ep_daddr
+ execp
->a_data
);
243 bsize
= epp
->ep_daddr
+ epp
->ep_dsize
- baddr
;
245 NEW_VMCMD(&epp
->ep_vmcmds
, vmcmd_map_zero
, bsize
, baddr
,
246 NULLVP
, 0, VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
);
249 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
250 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
251 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
252 * respectively to page boundaries.
253 * Compensate `ep_dsize' for the amount of data covered by the last
256 dsize
= epp
->ep_dsize
+ execp
->a_text
- round_page(execp
->a_text
);
257 epp
->ep_dsize
= (dsize
> 0) ? dsize
: 0;
258 return (*epp
->ep_esch
->es_setup_stack
)(l
, epp
);