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