linux_exec_aout.c revision 1.35 1 /* $NetBSD: linux_exec_aout.c,v 1.35 1998/10/23 03:53:18 erh 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 <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/vm_map.h>
60
61 #include <machine/cpu.h>
62 #include <machine/reg.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_util.h>
67 #include <compat/linux/common/linux_exec.h>
68 #include <compat/linux/common/linux_machdep.h>
69
70 #include <compat/linux/linux_syscallargs.h>
71 #include <compat/linux/linux_syscall.h>
72
73
74 static void *linux_aout_copyargs __P((struct exec_package *,
75 struct ps_strings *, void *, void *));
76
77 #define LINUX_AOUT_AUX_ARGSIZ 2
78
79 extern char linux_sigcode[], linux_esigcode[];
80 extern struct sysent linux_sysent[];
81 extern char *linux_syscallnames[];
82
83 int exec_linux_aout_prep_zmagic __P((struct proc *, struct exec_package *));
84 int exec_linux_aout_prep_nmagic __P((struct proc *, struct exec_package *));
85 int exec_linux_aout_prep_omagic __P((struct proc *, struct exec_package *));
86 int exec_linux_aout_prep_qmagic __P((struct proc *, struct exec_package *));
87
88 struct emul emul_linux_aout = {
89 "linux",
90 native_to_linux_errno,
91 linux_sendsig,
92 LINUX_SYS_syscall,
93 LINUX_SYS_MAXSYSCALL,
94 linux_sysent,
95 linux_syscallnames,
96 LINUX_AOUT_AUX_ARGSIZ,
97 linux_aout_copyargs,
98 linux_setregs,
99 linux_sigcode,
100 linux_esigcode,
101 };
102
103 static void *
104 linux_aout_copyargs(pack, arginfo, stack, argp)
105 struct exec_package *pack;
106 struct ps_strings *arginfo;
107 void *stack;
108 void *argp;
109 {
110 char **cpp = stack;
111 char **stk = stack;
112 char *dp, *sp;
113 size_t len;
114 void *nullp = NULL;
115 int argc = arginfo->ps_nargvstr;
116 int envc = arginfo->ps_nenvstr;
117
118 if (copyout(&argc, cpp++, sizeof(argc)))
119 return NULL;
120
121 /* leave room for envp and argv */
122 cpp += 2;
123 if (copyout(&cpp, &stk[1], sizeof (cpp)))
124 return NULL;
125
126 dp = (char *) (cpp + argc + envc + 2);
127 sp = argp;
128
129 /* XXX don't copy them out, remap them! */
130 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
131
132 for (; --argc >= 0; sp += len, dp += len)
133 if (copyout(&dp, cpp++, sizeof(dp)) ||
134 copyoutstr(sp, dp, ARG_MAX, &len))
135 return NULL;
136
137 if (copyout(&nullp, cpp++, sizeof(nullp)))
138 return NULL;
139
140 if (copyout(&cpp, &stk[2], sizeof (cpp)))
141 return NULL;
142
143 arginfo->ps_envstr = cpp; /* remember location of envp for later */
144
145 for (; --envc >= 0; sp += len, dp += len)
146 if (copyout(&dp, cpp++, sizeof(dp)) ||
147 copyoutstr(sp, dp, ARG_MAX, &len))
148 return NULL;
149
150 if (copyout(&nullp, cpp++, sizeof(nullp)))
151 return NULL;
152
153 return cpp;
154 }
155
156 int
157 exec_linux_aout_makecmds(p, epp)
158 struct proc *p;
159 struct exec_package *epp;
160 {
161 struct exec *linux_ep = epp->ep_hdr;
162 int machtype, magic;
163 int error = ENOEXEC;
164
165 magic = LINUX_N_MAGIC(linux_ep);
166 machtype = LINUX_N_MACHTYPE(linux_ep);
167
168
169 if (machtype != LINUX_MID_MACHINE)
170 return (ENOEXEC);
171
172 switch (magic) {
173 case QMAGIC:
174 error = exec_linux_aout_prep_qmagic(p, epp);
175 break;
176 case ZMAGIC:
177 error = exec_linux_aout_prep_zmagic(p, epp);
178 break;
179 case NMAGIC:
180 error = exec_linux_aout_prep_nmagic(p, epp);
181 break;
182 case OMAGIC:
183 error = exec_linux_aout_prep_omagic(p, epp);
184 break;
185 }
186 if (error == 0)
187 epp->ep_emul = &emul_linux_aout;
188 return error;
189 }
190
191 /*
192 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
193 * is very likely not page aligned on most architectures, it is treated
194 * as an NMAGIC here. XXX
195 */
196
197 int
198 exec_linux_aout_prep_zmagic(p, epp)
199 struct proc *p;
200 struct exec_package *epp;
201 {
202 struct exec *execp = epp->ep_hdr;
203
204 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
205 epp->ep_tsize = execp->a_text;
206 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
207 epp->ep_dsize = execp->a_data + execp->a_bss;
208 epp->ep_entry = execp->a_entry;
209
210 /* set up command for text segment */
211 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
212 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
213 VM_PROT_READ|VM_PROT_EXECUTE);
214
215 /* set up command for data segment */
216 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
217 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
218 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
219
220 /* set up command for bss segment */
221 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
222 epp->ep_daddr + execp->a_data, NULLVP, 0,
223 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
224
225 return exec_aout_setup_stack(p, epp);
226 }
227
228 /*
229 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
230 * Not different from the normal stuff.
231 */
232
233 int
234 exec_linux_aout_prep_nmagic(p, epp)
235 struct proc *p;
236 struct exec_package *epp;
237 {
238 struct exec *execp = epp->ep_hdr;
239 long bsize, baddr;
240
241 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
242 epp->ep_tsize = execp->a_text;
243 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
244 epp->ep_dsize = execp->a_data + execp->a_bss;
245 epp->ep_entry = execp->a_entry;
246
247 /* set up command for text segment */
248 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
249 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
250 VM_PROT_READ|VM_PROT_EXECUTE);
251
252 /* set up command for data segment */
253 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
254 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
255 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
256
257 /* set up command for bss segment */
258 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
259 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
260 if (bsize > 0)
261 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
262 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
263
264 return exec_aout_setup_stack(p, epp);
265 }
266
267 /*
268 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
269 * Business as usual.
270 */
271
272 int
273 exec_linux_aout_prep_omagic(p, epp)
274 struct proc *p;
275 struct exec_package *epp;
276 {
277 struct exec *execp = epp->ep_hdr;
278 long dsize, bsize, baddr;
279
280 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
281 epp->ep_tsize = execp->a_text;
282 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
283 epp->ep_dsize = execp->a_data + execp->a_bss;
284 epp->ep_entry = execp->a_entry;
285
286 /* set up command for text and data segments */
287 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
288 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
289 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
290
291 /* set up command for bss segment */
292 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
293 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
294 if (bsize > 0)
295 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
296 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
297
298 /*
299 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
300 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
301 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
302 * respectively to page boundaries.
303 * Compensate `ep_dsize' for the amount of data covered by the last
304 * text page.
305 */
306 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
307 epp->ep_dsize = (dsize > 0) ? dsize : 0;
308 return exec_aout_setup_stack(p, epp);
309 }
310
311 int
312 exec_linux_aout_prep_qmagic(p, epp)
313 struct proc *p;
314 struct exec_package *epp;
315 {
316 struct exec *execp = epp->ep_hdr;
317
318 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
319 epp->ep_tsize = execp->a_text;
320 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
321 epp->ep_dsize = execp->a_data + execp->a_bss;
322 epp->ep_entry = execp->a_entry;
323
324 /*
325 * check if vnode is in open for writing, because we want to
326 * demand-page out of it. if it is, don't do it, for various
327 * reasons
328 */
329 if ((execp->a_text != 0 || execp->a_data != 0) &&
330 epp->ep_vp->v_writecount != 0) {
331 #ifdef DIAGNOSTIC
332 if (epp->ep_vp->v_flag & VTEXT)
333 panic("exec: a VTEXT vnode has writecount != 0\n");
334 #endif
335 return ETXTBSY;
336 }
337 epp->ep_vp->v_flag |= VTEXT;
338
339 /* set up command for text segment */
340 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
341 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
342 VM_PROT_READ|VM_PROT_EXECUTE);
343
344 /* set up command for data segment */
345 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
346 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
347 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
348
349 /* set up command for bss segment */
350 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
351 epp->ep_daddr + execp->a_data, NULLVP, 0,
352 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
353
354 return exec_aout_setup_stack(p, epp);
355 }
356
357 /*
358 * The Linux system call to load shared libraries, a.out version. The
359 * a.out shared libs are just files that are mapped onto a fixed
360 * address in the process' address space. The address is given in
361 * a_entry. Read in the header, set up some VM commands and run them.
362 *
363 * Yes, both text and data are mapped at once, so we're left with
364 * writeable text for the shared libs. The Linux crt0 seemed to break
365 * sometimes when data was mapped seperately. It munmapped a uselib()
366 * of ld.so by hand, which failed with shared text and data for ld.so
367 * Yuck.
368 *
369 * Because of the problem with ZMAGIC executables (text starts
370 * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
371 * shared libs are not handled very efficiently :-(
372 */
373
374 int
375 linux_sys_uselib(p, v, retval)
376 struct proc *p;
377 void *v;
378 register_t *retval;
379 {
380 struct linux_sys_uselib_args /* {
381 syscallarg(char *) path;
382 } */ *uap = v;
383 caddr_t sg;
384 long bsize, dsize, tsize, taddr, baddr, daddr;
385 struct nameidata ni;
386 struct vnode *vp;
387 struct exec hdr;
388 struct exec_vmcmd_set vcset;
389 int i, magic, error;
390 size_t rem;
391
392 sg = stackgap_init(p->p_emul);
393 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
394
395 NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
396
397 if ((error = namei(&ni)))
398 return error;
399
400 vp = ni.ni_vp;
401
402 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
403 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
404 &rem, p))) {
405 vrele(vp);
406 return error;
407 }
408
409 if (rem != 0) {
410 vrele(vp);
411 return ENOEXEC;
412 }
413
414 if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
415 return ENOEXEC;
416
417 magic = LINUX_N_MAGIC(&hdr);
418 taddr = hdr.a_entry & (~(NBPG - 1));
419 tsize = hdr.a_text;
420 daddr = taddr + tsize;
421 dsize = hdr.a_data + hdr.a_bss;
422
423 if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
424 vrele(vp);
425 return ETXTBSY;
426 }
427 vp->v_flag |= VTEXT;
428
429 vcset.evs_cnt = 0;
430 vcset.evs_used = 0;
431
432 NEW_VMCMD(&vcset,
433 magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
434 hdr.a_text + hdr.a_data, taddr,
435 vp, LINUX_N_TXTOFF(hdr, magic),
436 VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
437
438 baddr = roundup(daddr + hdr.a_data, NBPG);
439 bsize = daddr + dsize - baddr;
440 if (bsize > 0) {
441 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
442 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
443 }
444
445 for (i = 0; i < vcset.evs_used && !error; i++) {
446 struct exec_vmcmd *vcp;
447
448 vcp = &vcset.evs_cmds[i];
449 error = (*vcp->ev_proc)(p, vcp);
450 }
451
452 kill_vmcmds(&vcset);
453
454 vrele(vp);
455
456 return error;
457 }
458