linux_exec_aout.c revision 1.32 1 1.32 christos /* $NetBSD: linux_exec_aout.c,v 1.32 1998/10/03 20:17:40 christos Exp $ */
2 1.29 christos
3 1.29 christos /*-
4 1.31 erh * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.29 christos * All rights reserved.
6 1.29 christos *
7 1.29 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.31 erh * by Eric Haszlakiewicz.
9 1.31 erh *
10 1.31 erh * This code is derived from software contributed to The NetBSD Foundation
11 1.29 christos * by Christos Zoulas.
12 1.29 christos *
13 1.29 christos * Redistribution and use in source and binary forms, with or without
14 1.29 christos * modification, are permitted provided that the following conditions
15 1.29 christos * are met:
16 1.29 christos * 1. Redistributions of source code must retain the above copyright
17 1.29 christos * notice, this list of conditions and the following disclaimer.
18 1.29 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.29 christos * notice, this list of conditions and the following disclaimer in the
20 1.29 christos * documentation and/or other materials provided with the distribution.
21 1.29 christos * 3. All advertising materials mentioning features or use of this software
22 1.29 christos * must display the following acknowledgement:
23 1.31 erh * This product includes software developed by the NetBSD
24 1.31 erh * Foundation, Inc. and its contributors.
25 1.29 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
26 1.29 christos * contributors may be used to endorse or promote products derived
27 1.29 christos * from this software without specific prior written permission.
28 1.29 christos *
29 1.29 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 1.29 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 1.29 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 1.29 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 1.29 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 1.29 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 1.29 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 1.29 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 1.29 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 1.29 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 1.29 christos * POSSIBILITY OF SUCH DAMAGE.
40 1.29 christos */
41 1.1 fvdl
42 1.1 fvdl /*
43 1.1 fvdl * Copyright (c) 1995 Frank van der Linden
44 1.1 fvdl * All rights reserved.
45 1.1 fvdl *
46 1.1 fvdl * Redistribution and use in source and binary forms, with or without
47 1.1 fvdl * modification, are permitted provided that the following conditions
48 1.1 fvdl * are met:
49 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
50 1.1 fvdl * notice, this list of conditions and the following disclaimer.
51 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
53 1.1 fvdl * documentation and/or other materials provided with the distribution.
54 1.7 fvdl * 3. The name of the author may not be used to endorse or promote products
55 1.1 fvdl * derived from this software without specific prior written permission
56 1.1 fvdl *
57 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 1.1 fvdl *
68 1.7 fvdl * based on exec_aout.c, sunos_exec.c and svr4_exec.c
69 1.1 fvdl */
70 1.1 fvdl
71 1.1 fvdl #include <sys/param.h>
72 1.1 fvdl #include <sys/systm.h>
73 1.1 fvdl #include <sys/kernel.h>
74 1.1 fvdl #include <sys/proc.h>
75 1.1 fvdl #include <sys/malloc.h>
76 1.1 fvdl #include <sys/namei.h>
77 1.1 fvdl #include <sys/vnode.h>
78 1.13 christos #include <sys/mount.h>
79 1.25 christos #include <sys/exec.h>
80 1.8 fvdl #include <sys/exec_elf.h>
81 1.1 fvdl
82 1.1 fvdl #include <sys/mman.h>
83 1.13 christos #include <sys/syscallargs.h>
84 1.13 christos
85 1.1 fvdl #include <vm/vm.h>
86 1.1 fvdl #include <vm/vm_param.h>
87 1.1 fvdl #include <vm/vm_map.h>
88 1.1 fvdl
89 1.32 christos #include <machine/cpu.h>
90 1.32 christos #include <machine/reg.h>
91 1.32 christos
92 1.32 christos #include <compat/linux/common/linux_types.h>
93 1.32 christos #include <compat/linux/common/linux_signal.h>
94 1.32 christos #include <compat/linux/common/linux_siginfo.h>
95 1.32 christos #include <compat/linux/common/linux_util.h>
96 1.32 christos #include <compat/linux/common/linux_exec.h>
97 1.32 christos #include <compat/linux/common/linux_machdep.h>
98 1.32 christos
99 1.32 christos #include <compat/linux/linux_syscallargs.h>
100 1.4 christos #include <compat/linux/linux_syscall.h>
101 1.31 erh
102 1.1 fvdl
103 1.6 fvdl static void *linux_aout_copyargs __P((struct exec_package *,
104 1.14 christos struct ps_strings *, void *, void *));
105 1.4 christos
106 1.8 fvdl #define LINUX_AOUT_AUX_ARGSIZ 2
107 1.4 christos
108 1.4 christos extern int linux_error[];
109 1.12 mycroft extern char linux_sigcode[], linux_esigcode[];
110 1.4 christos extern struct sysent linux_sysent[];
111 1.4 christos extern char *linux_syscallnames[];
112 1.13 christos
113 1.13 christos int exec_linux_aout_prep_zmagic __P((struct proc *, struct exec_package *));
114 1.13 christos int exec_linux_aout_prep_nmagic __P((struct proc *, struct exec_package *));
115 1.13 christos int exec_linux_aout_prep_omagic __P((struct proc *, struct exec_package *));
116 1.13 christos int exec_linux_aout_prep_qmagic __P((struct proc *, struct exec_package *));
117 1.4 christos
118 1.6 fvdl struct emul emul_linux_aout = {
119 1.4 christos "linux",
120 1.4 christos linux_error,
121 1.4 christos linux_sendsig,
122 1.4 christos LINUX_SYS_syscall,
123 1.4 christos LINUX_SYS_MAXSYSCALL,
124 1.4 christos linux_sysent,
125 1.4 christos linux_syscallnames,
126 1.6 fvdl LINUX_AOUT_AUX_ARGSIZ,
127 1.6 fvdl linux_aout_copyargs,
128 1.26 mycroft linux_setregs,
129 1.6 fvdl linux_sigcode,
130 1.6 fvdl linux_esigcode,
131 1.6 fvdl };
132 1.6 fvdl
133 1.4 christos static void *
134 1.6 fvdl linux_aout_copyargs(pack, arginfo, stack, argp)
135 1.4 christos struct exec_package *pack;
136 1.4 christos struct ps_strings *arginfo;
137 1.4 christos void *stack;
138 1.4 christos void *argp;
139 1.4 christos {
140 1.4 christos char **cpp = stack;
141 1.4 christos char **stk = stack;
142 1.4 christos char *dp, *sp;
143 1.4 christos size_t len;
144 1.4 christos void *nullp = NULL;
145 1.4 christos int argc = arginfo->ps_nargvstr;
146 1.4 christos int envc = arginfo->ps_nenvstr;
147 1.4 christos
148 1.4 christos if (copyout(&argc, cpp++, sizeof(argc)))
149 1.4 christos return NULL;
150 1.4 christos
151 1.4 christos /* leave room for envp and argv */
152 1.4 christos cpp += 2;
153 1.4 christos if (copyout(&cpp, &stk[1], sizeof (cpp)))
154 1.4 christos return NULL;
155 1.4 christos
156 1.4 christos dp = (char *) (cpp + argc + envc + 2);
157 1.4 christos sp = argp;
158 1.4 christos
159 1.4 christos /* XXX don't copy them out, remap them! */
160 1.5 mycroft arginfo->ps_argvstr = cpp; /* remember location of argv for later */
161 1.4 christos
162 1.4 christos for (; --argc >= 0; sp += len, dp += len)
163 1.4 christos if (copyout(&dp, cpp++, sizeof(dp)) ||
164 1.4 christos copyoutstr(sp, dp, ARG_MAX, &len))
165 1.4 christos return NULL;
166 1.4 christos
167 1.4 christos if (copyout(&nullp, cpp++, sizeof(nullp)))
168 1.4 christos return NULL;
169 1.4 christos
170 1.4 christos if (copyout(&cpp, &stk[2], sizeof (cpp)))
171 1.4 christos return NULL;
172 1.4 christos
173 1.5 mycroft arginfo->ps_envstr = cpp; /* remember location of envp for later */
174 1.4 christos
175 1.4 christos for (; --envc >= 0; sp += len, dp += len)
176 1.4 christos if (copyout(&dp, cpp++, sizeof(dp)) ||
177 1.4 christos copyoutstr(sp, dp, ARG_MAX, &len))
178 1.4 christos return NULL;
179 1.4 christos
180 1.4 christos if (copyout(&nullp, cpp++, sizeof(nullp)))
181 1.4 christos return NULL;
182 1.4 christos
183 1.4 christos return cpp;
184 1.4 christos }
185 1.4 christos
186 1.1 fvdl int
187 1.1 fvdl exec_linux_aout_makecmds(p, epp)
188 1.1 fvdl struct proc *p;
189 1.1 fvdl struct exec_package *epp;
190 1.1 fvdl {
191 1.1 fvdl struct exec *linux_ep = epp->ep_hdr;
192 1.1 fvdl int machtype, magic;
193 1.1 fvdl int error = ENOEXEC;
194 1.1 fvdl
195 1.1 fvdl magic = LINUX_N_MAGIC(linux_ep);
196 1.1 fvdl machtype = LINUX_N_MACHTYPE(linux_ep);
197 1.1 fvdl
198 1.1 fvdl
199 1.1 fvdl if (machtype != LINUX_MID_MACHINE)
200 1.1 fvdl return (ENOEXEC);
201 1.1 fvdl
202 1.1 fvdl switch (magic) {
203 1.1 fvdl case QMAGIC:
204 1.1 fvdl error = exec_linux_aout_prep_qmagic(p, epp);
205 1.1 fvdl break;
206 1.1 fvdl case ZMAGIC:
207 1.1 fvdl error = exec_linux_aout_prep_zmagic(p, epp);
208 1.1 fvdl break;
209 1.1 fvdl case NMAGIC:
210 1.1 fvdl error = exec_linux_aout_prep_nmagic(p, epp);
211 1.1 fvdl break;
212 1.1 fvdl case OMAGIC:
213 1.1 fvdl error = exec_linux_aout_prep_omagic(p, epp);
214 1.1 fvdl break;
215 1.1 fvdl }
216 1.4 christos if (error == 0)
217 1.6 fvdl epp->ep_emul = &emul_linux_aout;
218 1.1 fvdl return error;
219 1.1 fvdl }
220 1.1 fvdl
221 1.1 fvdl /*
222 1.1 fvdl * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
223 1.1 fvdl * is very likely not page aligned on most architectures, it is treated
224 1.1 fvdl * as an NMAGIC here. XXX
225 1.1 fvdl */
226 1.1 fvdl
227 1.1 fvdl int
228 1.1 fvdl exec_linux_aout_prep_zmagic(p, epp)
229 1.1 fvdl struct proc *p;
230 1.1 fvdl struct exec_package *epp;
231 1.1 fvdl {
232 1.1 fvdl struct exec *execp = epp->ep_hdr;
233 1.1 fvdl
234 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
235 1.1 fvdl epp->ep_tsize = execp->a_text;
236 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
237 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
238 1.1 fvdl epp->ep_entry = execp->a_entry;
239 1.1 fvdl
240 1.1 fvdl /* set up command for text segment */
241 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
242 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
243 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
244 1.1 fvdl
245 1.1 fvdl /* set up command for data segment */
246 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
247 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
248 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
249 1.1 fvdl
250 1.1 fvdl /* set up command for bss segment */
251 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
252 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
253 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
254 1.1 fvdl
255 1.1 fvdl return exec_aout_setup_stack(p, epp);
256 1.1 fvdl }
257 1.1 fvdl
258 1.1 fvdl /*
259 1.1 fvdl * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
260 1.1 fvdl * Not different from the normal stuff.
261 1.1 fvdl */
262 1.1 fvdl
263 1.1 fvdl int
264 1.1 fvdl exec_linux_aout_prep_nmagic(p, epp)
265 1.1 fvdl struct proc *p;
266 1.1 fvdl struct exec_package *epp;
267 1.1 fvdl {
268 1.1 fvdl struct exec *execp = epp->ep_hdr;
269 1.1 fvdl long bsize, baddr;
270 1.1 fvdl
271 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
272 1.1 fvdl epp->ep_tsize = execp->a_text;
273 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
274 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
275 1.1 fvdl epp->ep_entry = execp->a_entry;
276 1.1 fvdl
277 1.1 fvdl /* set up command for text segment */
278 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
279 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
280 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
281 1.1 fvdl
282 1.1 fvdl /* set up command for data segment */
283 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
284 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
285 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
286 1.1 fvdl
287 1.1 fvdl /* set up command for bss segment */
288 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
289 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
290 1.1 fvdl if (bsize > 0)
291 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
292 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
293 1.1 fvdl
294 1.1 fvdl return exec_aout_setup_stack(p, epp);
295 1.1 fvdl }
296 1.1 fvdl
297 1.1 fvdl /*
298 1.1 fvdl * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
299 1.1 fvdl * Business as usual.
300 1.1 fvdl */
301 1.1 fvdl
302 1.1 fvdl int
303 1.1 fvdl exec_linux_aout_prep_omagic(p, epp)
304 1.1 fvdl struct proc *p;
305 1.1 fvdl struct exec_package *epp;
306 1.1 fvdl {
307 1.1 fvdl struct exec *execp = epp->ep_hdr;
308 1.1 fvdl long dsize, bsize, baddr;
309 1.1 fvdl
310 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
311 1.1 fvdl epp->ep_tsize = execp->a_text;
312 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
313 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
314 1.1 fvdl epp->ep_entry = execp->a_entry;
315 1.1 fvdl
316 1.1 fvdl /* set up command for text and data segments */
317 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
318 1.1 fvdl execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
319 1.1 fvdl LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
320 1.1 fvdl
321 1.1 fvdl /* set up command for bss segment */
322 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
323 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
324 1.1 fvdl if (bsize > 0)
325 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
326 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
327 1.1 fvdl
328 1.1 fvdl /*
329 1.1 fvdl * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
330 1.1 fvdl * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
331 1.1 fvdl * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
332 1.1 fvdl * respectively to page boundaries.
333 1.1 fvdl * Compensate `ep_dsize' for the amount of data covered by the last
334 1.1 fvdl * text page.
335 1.1 fvdl */
336 1.1 fvdl dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
337 1.1 fvdl epp->ep_dsize = (dsize > 0) ? dsize : 0;
338 1.1 fvdl return exec_aout_setup_stack(p, epp);
339 1.1 fvdl }
340 1.1 fvdl
341 1.1 fvdl int
342 1.1 fvdl exec_linux_aout_prep_qmagic(p, epp)
343 1.1 fvdl struct proc *p;
344 1.1 fvdl struct exec_package *epp;
345 1.1 fvdl {
346 1.1 fvdl struct exec *execp = epp->ep_hdr;
347 1.1 fvdl
348 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
349 1.1 fvdl epp->ep_tsize = execp->a_text;
350 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
351 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
352 1.1 fvdl epp->ep_entry = execp->a_entry;
353 1.1 fvdl
354 1.1 fvdl /*
355 1.1 fvdl * check if vnode is in open for writing, because we want to
356 1.1 fvdl * demand-page out of it. if it is, don't do it, for various
357 1.1 fvdl * reasons
358 1.1 fvdl */
359 1.1 fvdl if ((execp->a_text != 0 || execp->a_data != 0) &&
360 1.1 fvdl epp->ep_vp->v_writecount != 0) {
361 1.1 fvdl #ifdef DIAGNOSTIC
362 1.1 fvdl if (epp->ep_vp->v_flag & VTEXT)
363 1.1 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
364 1.1 fvdl #endif
365 1.1 fvdl return ETXTBSY;
366 1.1 fvdl }
367 1.1 fvdl epp->ep_vp->v_flag |= VTEXT;
368 1.1 fvdl
369 1.1 fvdl /* set up command for text segment */
370 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
371 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
372 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
373 1.1 fvdl
374 1.1 fvdl /* set up command for data segment */
375 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
376 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
377 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
378 1.1 fvdl
379 1.1 fvdl /* set up command for bss segment */
380 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
381 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
382 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
383 1.1 fvdl
384 1.1 fvdl return exec_aout_setup_stack(p, epp);
385 1.1 fvdl }
386 1.1 fvdl
387 1.14 christos /*
388 1.6 fvdl * The Linux system call to load shared libraries, a.out version. The
389 1.6 fvdl * a.out shared libs are just files that are mapped onto a fixed
390 1.6 fvdl * address in the process' address space. The address is given in
391 1.1 fvdl * a_entry. Read in the header, set up some VM commands and run them.
392 1.1 fvdl *
393 1.1 fvdl * Yes, both text and data are mapped at once, so we're left with
394 1.1 fvdl * writeable text for the shared libs. The Linux crt0 seemed to break
395 1.1 fvdl * sometimes when data was mapped seperately. It munmapped a uselib()
396 1.1 fvdl * of ld.so by hand, which failed with shared text and data for ld.so
397 1.1 fvdl * Yuck.
398 1.1 fvdl *
399 1.1 fvdl * Because of the problem with ZMAGIC executables (text starts
400 1.6 fvdl * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
401 1.1 fvdl * shared libs are not handled very efficiently :-(
402 1.1 fvdl */
403 1.1 fvdl
404 1.1 fvdl int
405 1.12 mycroft linux_sys_uselib(p, v, retval)
406 1.1 fvdl struct proc *p;
407 1.11 thorpej void *v;
408 1.11 thorpej register_t *retval;
409 1.11 thorpej {
410 1.12 mycroft struct linux_sys_uselib_args /* {
411 1.1 fvdl syscallarg(char *) path;
412 1.11 thorpej } */ *uap = v;
413 1.1 fvdl caddr_t sg;
414 1.1 fvdl long bsize, dsize, tsize, taddr, baddr, daddr;
415 1.1 fvdl struct nameidata ni;
416 1.1 fvdl struct vnode *vp;
417 1.1 fvdl struct exec hdr;
418 1.1 fvdl struct exec_vmcmd_set vcset;
419 1.27 thorpej int i, magic, error;
420 1.27 thorpej size_t rem;
421 1.1 fvdl
422 1.9 christos sg = stackgap_init(p->p_emul);
423 1.9 christos LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
424 1.1 fvdl
425 1.1 fvdl NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
426 1.1 fvdl
427 1.1 fvdl if ((error = namei(&ni)))
428 1.1 fvdl return error;
429 1.1 fvdl
430 1.1 fvdl vp = ni.ni_vp;
431 1.1 fvdl
432 1.1 fvdl if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
433 1.1 fvdl 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
434 1.1 fvdl &rem, p))) {
435 1.1 fvdl vrele(vp);
436 1.1 fvdl return error;
437 1.1 fvdl }
438 1.1 fvdl
439 1.1 fvdl if (rem != 0) {
440 1.1 fvdl vrele(vp);
441 1.1 fvdl return ENOEXEC;
442 1.1 fvdl }
443 1.6 fvdl
444 1.6 fvdl if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
445 1.6 fvdl return ENOEXEC;
446 1.1 fvdl
447 1.1 fvdl magic = LINUX_N_MAGIC(&hdr);
448 1.1 fvdl taddr = hdr.a_entry & (~(NBPG - 1));
449 1.1 fvdl tsize = hdr.a_text;
450 1.1 fvdl daddr = taddr + tsize;
451 1.1 fvdl dsize = hdr.a_data + hdr.a_bss;
452 1.1 fvdl
453 1.1 fvdl if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
454 1.1 fvdl vrele(vp);
455 1.1 fvdl return ETXTBSY;
456 1.1 fvdl }
457 1.1 fvdl vp->v_flag |= VTEXT;
458 1.1 fvdl
459 1.1 fvdl vcset.evs_cnt = 0;
460 1.1 fvdl vcset.evs_used = 0;
461 1.1 fvdl
462 1.1 fvdl NEW_VMCMD(&vcset,
463 1.1 fvdl magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
464 1.1 fvdl hdr.a_text + hdr.a_data, taddr,
465 1.1 fvdl vp, LINUX_N_TXTOFF(hdr, magic),
466 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
467 1.1 fvdl
468 1.1 fvdl baddr = roundup(daddr + hdr.a_data, NBPG);
469 1.1 fvdl bsize = daddr + dsize - baddr;
470 1.1 fvdl if (bsize > 0) {
471 1.1 fvdl NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
472 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
473 1.1 fvdl }
474 1.1 fvdl
475 1.1 fvdl for (i = 0; i < vcset.evs_used && !error; i++) {
476 1.1 fvdl struct exec_vmcmd *vcp;
477 1.1 fvdl
478 1.1 fvdl vcp = &vcset.evs_cmds[i];
479 1.1 fvdl error = (*vcp->ev_proc)(p, vcp);
480 1.1 fvdl }
481 1.1 fvdl
482 1.1 fvdl kill_vmcmds(&vcset);
483 1.1 fvdl
484 1.1 fvdl vrele(vp);
485 1.1 fvdl
486 1.1 fvdl return error;
487 1.1 fvdl }
488