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