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