FreeBSD/Linux Kernel Cross Reference
sys/kern/exec_aout.c
1 /* $NetBSD: exec_aout.c,v 1.33 2005/12/11 12:24:29 christos Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
20 *
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.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.33 2005/12/11 12:24:29 christos Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/malloc.h>
40 #include <sys/vnode.h>
41 #include <sys/exec.h>
42 #include <sys/exec_aout.h>
43 #include <sys/resourcevar.h>
44
45 #include <uvm/uvm_extern.h>
46
47 /*
48 * exec_aout_makecmds(): Check if it's an a.out-format executable.
49 *
50 * Given a lwp pointer and an exec package pointer, see if the referent
51 * of the epp is in a.out format. First check 'standard' magic numbers for
52 * this architecture. If that fails, try a CPU-dependent hook.
53 *
54 * This function, in the former case, or the hook, in the latter, is
55 * responsible for creating a set of vmcmds which can be used to build
56 * the process's vm space and inserting them into the exec package.
57 */
58
59 int
60 exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
61 {
62 u_long midmag, magic;
63 u_short mid;
64 int error;
65 struct exec *execp = epp->ep_hdr;
66
67 if (epp->ep_hdrvalid < sizeof(struct exec))
68 return ENOEXEC;
69
70 midmag = ntohl(execp->a_midmag);
71 mid = (midmag >> 16) & 0x3ff;
72 magic = midmag & 0xffff;
73
74 midmag = mid << 16 | magic;
75
76 switch (midmag) {
77 case (MID_MACHINE << 16) | ZMAGIC:
78 error = exec_aout_prep_zmagic(l, epp);
79 break;
80 case (MID_MACHINE << 16) | NMAGIC:
81 error = exec_aout_prep_nmagic(l, epp);
82 break;
83 case (MID_MACHINE << 16) | OMAGIC:
84 error = exec_aout_prep_omagic(l, epp);
85 break;
86 default:
87 error = cpu_exec_aout_makecmds(l, epp);
88 }
89
90 if (error)
91 kill_vmcmds(&epp->ep_vmcmds);
92
93 return error;
94 }
95
96 /*
97 * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
98 *
99 * First, set of the various offsets/lengths in the exec package.
100 *
101 * Then, mark the text image busy (so it can be demand paged) or error
102 * out if this is not possible. Finally, set up vmcmds for the
103 * text, data, bss, and stack segments.
104 */
105
106 int
107 exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
108 {
109 struct exec *execp = epp->ep_hdr;
110 int error;
111
112 epp->ep_taddr = AOUT_LDPGSZ;
113 epp->ep_tsize = execp->a_text;
114 epp->ep_daddr = epp->ep_taddr + execp->a_text;
115 epp->ep_dsize = execp->a_data + execp->a_bss;
116 epp->ep_entry = execp->a_entry;
117
118 error = vn_marktext(epp->ep_vp);
119 if (error)
120 return (error);
121
122 /* set up command for text segment */
123 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
124 epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
125
126 /* set up command for data segment */
127 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
128 epp->ep_daddr, epp->ep_vp, execp->a_text,
129 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
130
131 /* set up command for bss segment */
132 if (execp->a_bss > 0)
133 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
134 epp->ep_daddr + execp->a_data, NULLVP, 0,
135 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
136
137 return (*epp->ep_esch->es_setup_stack)(l, epp);
138 }
139
140 /*
141 * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
142 */
143
144 int
145 exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
146 {
147 struct exec *execp = epp->ep_hdr;
148 long bsize, baddr;
149
150 epp->ep_taddr = AOUT_LDPGSZ;
151 epp->ep_tsize = execp->a_text;
152 epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
153 epp->ep_dsize = execp->a_data + execp->a_bss;
154 epp->ep_entry = execp->a_entry;
155
156 /* set up command for text segment */
157 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
158 epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
159 VM_PROT_READ|VM_PROT_EXECUTE);
160
161 /* set up command for data segment */
162 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
163 epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
164 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
165
166 /* set up command for bss segment */
167 baddr = round_page(epp->ep_daddr + execp->a_data);
168 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
169 if (bsize > 0)
170 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
171 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
172
173 return (*epp->ep_esch->es_setup_stack)(l, epp);
174 }
175
176 /*
177 * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
178 */
179
180 int
181 exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
182 {
183 struct exec *execp = epp->ep_hdr;
184 long dsize, bsize, baddr;
185
186 epp->ep_taddr = AOUT_LDPGSZ;
187 epp->ep_tsize = execp->a_text;
188 epp->ep_daddr = epp->ep_taddr + execp->a_text;
189 epp->ep_dsize = execp->a_data + execp->a_bss;
190 epp->ep_entry = execp->a_entry;
191
192 /* set up command for text and data segments */
193 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
194 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
195 sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
196
197 /* set up command for bss segment */
198 baddr = round_page(epp->ep_daddr + execp->a_data);
199 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
200 if (bsize > 0)
201 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
202 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
203
204 /*
205 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
206 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
207 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
208 * respectively to page boundaries.
209 * Compensate `ep_dsize' for the amount of data covered by the last
210 * text page.
211 */
212 dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
213 epp->ep_dsize = (dsize > 0) ? dsize : 0;
214 return (*epp->ep_esch->es_setup_stack)(l, epp);
215 }
Cache object: 03e7df2ba092f34f4f21347e477bcec3
|