linux_exec_aout.c revision 1.20 1 /* $NetBSD: linux_exec_aout.c,v 1.20 1996/10/03 02:16:18 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1995 Frank van der Linden
5 * Copyright (c) 1994 Christos Zoulas
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * based on exec_aout.c, sunos_exec.c and svr4_exec.c
31 */
32
33 #define ELFSIZE 32 /* XXX should die */
34 #ifndef EXEC_ELF32 /* XXX should die */
35 #define EXEC_ELF32 /* XXX should die */
36 #endif /* XXX should die */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/malloc.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/exec_elf.h>
47
48 #include <sys/mman.h>
49 #include <sys/syscallargs.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_map.h>
54
55 #include <machine/cpu.h>
56 #include <machine/reg.h>
57 #include <machine/exec.h>
58 #include <machine/linux_machdep.h>
59
60 #include <compat/linux/linux_types.h>
61 #include <compat/linux/linux_syscall.h>
62 #include <compat/linux/linux_signal.h>
63 #include <compat/linux/linux_syscallargs.h>
64 #include <compat/linux/linux_util.h>
65 #include <compat/linux/linux_exec.h>
66
67 static void *linux_aout_copyargs __P((struct exec_package *,
68 struct ps_strings *, void *, void *));
69 static int linux_elf32_signature __P((struct proc *p, struct exec_package *,
70 Elf32_Ehdr *));
71
72 #define LINUX_AOUT_AUX_ARGSIZ 2
73 #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
74
75
76 const char linux_emul_path[] = "/emul/linux";
77 extern int linux_error[];
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 linux_error,
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 setregs,
98 linux_sigcode,
99 linux_esigcode,
100 };
101
102 struct emul emul_linux_elf = {
103 "linux",
104 linux_error,
105 linux_sendsig,
106 LINUX_SYS_syscall,
107 LINUX_SYS_MAXSYSCALL,
108 linux_sysent,
109 linux_syscallnames,
110 LINUX_ELF_AUX_ARGSIZ,
111 elf32_copyargs,
112 setregs,
113 linux_sigcode,
114 linux_esigcode,
115 };
116
117
118 static void *
119 linux_aout_copyargs(pack, arginfo, stack, argp)
120 struct exec_package *pack;
121 struct ps_strings *arginfo;
122 void *stack;
123 void *argp;
124 {
125 char **cpp = stack;
126 char **stk = stack;
127 char *dp, *sp;
128 size_t len;
129 void *nullp = NULL;
130 int argc = arginfo->ps_nargvstr;
131 int envc = arginfo->ps_nenvstr;
132
133 if (copyout(&argc, cpp++, sizeof(argc)))
134 return NULL;
135
136 /* leave room for envp and argv */
137 cpp += 2;
138 if (copyout(&cpp, &stk[1], sizeof (cpp)))
139 return NULL;
140
141 dp = (char *) (cpp + argc + envc + 2);
142 sp = argp;
143
144 /* XXX don't copy them out, remap them! */
145 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
146
147 for (; --argc >= 0; sp += len, dp += len)
148 if (copyout(&dp, cpp++, sizeof(dp)) ||
149 copyoutstr(sp, dp, ARG_MAX, &len))
150 return NULL;
151
152 if (copyout(&nullp, cpp++, sizeof(nullp)))
153 return NULL;
154
155 if (copyout(&cpp, &stk[2], sizeof (cpp)))
156 return NULL;
157
158 arginfo->ps_envstr = cpp; /* remember location of envp for later */
159
160 for (; --envc >= 0; sp += len, dp += len)
161 if (copyout(&dp, cpp++, sizeof(dp)) ||
162 copyoutstr(sp, dp, ARG_MAX, &len))
163 return NULL;
164
165 if (copyout(&nullp, cpp++, sizeof(nullp)))
166 return NULL;
167
168 return cpp;
169 }
170
171 int
172 exec_linux_aout_makecmds(p, epp)
173 struct proc *p;
174 struct exec_package *epp;
175 {
176 struct exec *linux_ep = epp->ep_hdr;
177 int machtype, magic;
178 int error = ENOEXEC;
179
180 magic = LINUX_N_MAGIC(linux_ep);
181 machtype = LINUX_N_MACHTYPE(linux_ep);
182
183
184 if (machtype != LINUX_MID_MACHINE)
185 return (ENOEXEC);
186
187 switch (magic) {
188 case QMAGIC:
189 error = exec_linux_aout_prep_qmagic(p, epp);
190 break;
191 case ZMAGIC:
192 error = exec_linux_aout_prep_zmagic(p, epp);
193 break;
194 case NMAGIC:
195 error = exec_linux_aout_prep_nmagic(p, epp);
196 break;
197 case OMAGIC:
198 error = exec_linux_aout_prep_omagic(p, epp);
199 break;
200 }
201 if (error == 0)
202 epp->ep_emul = &emul_linux_aout;
203 return error;
204 }
205
206 /*
207 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
208 * is very likely not page aligned on most architectures, it is treated
209 * as an NMAGIC here. XXX
210 */
211
212 int
213 exec_linux_aout_prep_zmagic(p, epp)
214 struct proc *p;
215 struct exec_package *epp;
216 {
217 struct exec *execp = epp->ep_hdr;
218
219 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
220 epp->ep_tsize = execp->a_text;
221 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
222 epp->ep_dsize = execp->a_data + execp->a_bss;
223 epp->ep_entry = execp->a_entry;
224
225 /* set up command for text segment */
226 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
227 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
228 VM_PROT_READ|VM_PROT_EXECUTE);
229
230 /* set up command for data segment */
231 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
232 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
233 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
234
235 /* set up command for bss segment */
236 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
237 epp->ep_daddr + execp->a_data, NULLVP, 0,
238 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
239
240 return exec_aout_setup_stack(p, epp);
241 }
242
243 /*
244 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
245 * Not different from the normal stuff.
246 */
247
248 int
249 exec_linux_aout_prep_nmagic(p, epp)
250 struct proc *p;
251 struct exec_package *epp;
252 {
253 struct exec *execp = epp->ep_hdr;
254 long bsize, baddr;
255
256 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
257 epp->ep_tsize = execp->a_text;
258 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
259 epp->ep_dsize = execp->a_data + execp->a_bss;
260 epp->ep_entry = execp->a_entry;
261
262 /* set up command for text segment */
263 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
264 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
265 VM_PROT_READ|VM_PROT_EXECUTE);
266
267 /* set up command for data segment */
268 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
269 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
270 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
271
272 /* set up command for bss segment */
273 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
274 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
275 if (bsize > 0)
276 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
277 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
278
279 return exec_aout_setup_stack(p, epp);
280 }
281
282 /*
283 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
284 * Business as usual.
285 */
286
287 int
288 exec_linux_aout_prep_omagic(p, epp)
289 struct proc *p;
290 struct exec_package *epp;
291 {
292 struct exec *execp = epp->ep_hdr;
293 long dsize, bsize, baddr;
294
295 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
296 epp->ep_tsize = execp->a_text;
297 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
298 epp->ep_dsize = execp->a_data + execp->a_bss;
299 epp->ep_entry = execp->a_entry;
300
301 /* set up command for text and data segments */
302 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
303 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
304 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
305
306 /* set up command for bss segment */
307 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
308 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
309 if (bsize > 0)
310 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
311 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
312
313 /*
314 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
315 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
316 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
317 * respectively to page boundaries.
318 * Compensate `ep_dsize' for the amount of data covered by the last
319 * text page.
320 */
321 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
322 epp->ep_dsize = (dsize > 0) ? dsize : 0;
323 return exec_aout_setup_stack(p, epp);
324 }
325
326 int
327 exec_linux_aout_prep_qmagic(p, epp)
328 struct proc *p;
329 struct exec_package *epp;
330 {
331 struct exec *execp = epp->ep_hdr;
332
333 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
334 epp->ep_tsize = execp->a_text;
335 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
336 epp->ep_dsize = execp->a_data + execp->a_bss;
337 epp->ep_entry = execp->a_entry;
338
339 /*
340 * check if vnode is in open for writing, because we want to
341 * demand-page out of it. if it is, don't do it, for various
342 * reasons
343 */
344 if ((execp->a_text != 0 || execp->a_data != 0) &&
345 epp->ep_vp->v_writecount != 0) {
346 #ifdef DIAGNOSTIC
347 if (epp->ep_vp->v_flag & VTEXT)
348 panic("exec: a VTEXT vnode has writecount != 0\n");
349 #endif
350 return ETXTBSY;
351 }
352 epp->ep_vp->v_flag |= VTEXT;
353
354 /* set up command for text segment */
355 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
356 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
357 VM_PROT_READ|VM_PROT_EXECUTE);
358
359 /* set up command for data segment */
360 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
361 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
362 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
363
364 /* set up command for bss segment */
365 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
366 epp->ep_daddr + execp->a_data, NULLVP, 0,
367 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
368
369 return exec_aout_setup_stack(p, epp);
370 }
371
372 /*
373 * Take advantage of the fact that all the linux binaries are compiled
374 * with gcc, and gcc sticks in the comment field a signature. Note that
375 * on SVR4 binaries, the gcc signature will follow the OS name signature,
376 * that will not be a problem. We don't bother to read in the string table,
377 * but we check all the progbits headers.
378 */
379 static int
380 linux_elf32_signature(p, epp, eh)
381 struct proc *p;
382 struct exec_package *epp;
383 Elf32_Ehdr *eh;
384 {
385 size_t shsize = sizeof(Elf32_Shdr) * eh->e_shnum;
386 size_t i;
387 static const char signature[] = "\0GCC: (GNU) ";
388 char buf[sizeof(signature) - 1];
389 Elf32_Shdr *sh;
390 int error;
391
392 sh = (Elf32_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
393
394 if ((error = elf32_read_from(p, epp->ep_vp, eh->e_shoff,
395 (caddr_t) sh, shsize)) != 0)
396 goto out;
397
398 for (i = 0; i < eh->e_shnum; i++) {
399 Elf32_Shdr *s = &sh[i];
400
401 /*
402 * Identify candidates for the comment header;
403 * Header cannot have a load address, or flags and
404 * it must be large enough.
405 */
406 if (s->sh_type != Elf32_sht_progbits ||
407 s->sh_addr != 0 ||
408 s->sh_flags != 0 ||
409 s->sh_size < sizeof(signature) - 1)
410 continue;
411
412 if ((error = elf32_read_from(p, epp->ep_vp, s->sh_offset,
413 (caddr_t) buf, sizeof(signature) - 1)) != 0)
414 goto out;
415
416 /*
417 * error is 0, if the signatures match we are done.
418 */
419 if (bcmp(buf, signature, sizeof(signature) - 1) == 0)
420 goto out;
421 }
422 error = EFTYPE;
423
424 out:
425 free(sh, M_TEMP);
426 return error;
427 }
428
429 int
430 linux_elf32_probe(p, epp, eh, itp, pos)
431 struct proc *p;
432 struct exec_package *epp;
433 Elf32_Ehdr *eh;
434 char *itp;
435 Elf32_Addr *pos;
436 {
437 char *bp;
438 int error;
439 size_t len;
440
441 if ((error = linux_elf32_signature(p, epp, eh)) != 0)
442 return error;
443
444 if (itp[0]) {
445 if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
446 return error;
447 if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
448 return error;
449 free(bp, M_TEMP);
450 }
451 epp->ep_emul = &emul_linux_elf;
452 *pos = ELF32_NO_ADDR;
453 return 0;
454 }
455
456 /*
457 * The Linux system call to load shared libraries, a.out version. The
458 * a.out shared libs are just files that are mapped onto a fixed
459 * address in the process' address space. The address is given in
460 * a_entry. Read in the header, set up some VM commands and run them.
461 *
462 * Yes, both text and data are mapped at once, so we're left with
463 * writeable text for the shared libs. The Linux crt0 seemed to break
464 * sometimes when data was mapped seperately. It munmapped a uselib()
465 * of ld.so by hand, which failed with shared text and data for ld.so
466 * Yuck.
467 *
468 * Because of the problem with ZMAGIC executables (text starts
469 * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
470 * shared libs are not handled very efficiently :-(
471 */
472
473 int
474 linux_sys_uselib(p, v, retval)
475 struct proc *p;
476 void *v;
477 register_t *retval;
478 {
479 struct linux_sys_uselib_args /* {
480 syscallarg(char *) path;
481 } */ *uap = v;
482 caddr_t sg;
483 long bsize, dsize, tsize, taddr, baddr, daddr;
484 struct nameidata ni;
485 struct vnode *vp;
486 struct exec hdr;
487 struct exec_vmcmd_set vcset;
488 int rem, i, magic, error;
489
490 sg = stackgap_init(p->p_emul);
491 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
492
493 NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
494
495 if ((error = namei(&ni)))
496 return error;
497
498 vp = ni.ni_vp;
499
500 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
501 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
502 &rem, p))) {
503 vrele(vp);
504 return error;
505 }
506
507 if (rem != 0) {
508 vrele(vp);
509 return ENOEXEC;
510 }
511
512 if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
513 return ENOEXEC;
514
515 magic = LINUX_N_MAGIC(&hdr);
516 taddr = hdr.a_entry & (~(NBPG - 1));
517 tsize = hdr.a_text;
518 daddr = taddr + tsize;
519 dsize = hdr.a_data + hdr.a_bss;
520
521 if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
522 vrele(vp);
523 return ETXTBSY;
524 }
525 vp->v_flag |= VTEXT;
526
527 vcset.evs_cnt = 0;
528 vcset.evs_used = 0;
529
530 NEW_VMCMD(&vcset,
531 magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
532 hdr.a_text + hdr.a_data, taddr,
533 vp, LINUX_N_TXTOFF(hdr, magic),
534 VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
535
536 baddr = roundup(daddr + hdr.a_data, NBPG);
537 bsize = daddr + dsize - baddr;
538 if (bsize > 0) {
539 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
540 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
541 }
542
543 for (i = 0; i < vcset.evs_used && !error; i++) {
544 struct exec_vmcmd *vcp;
545
546 vcp = &vcset.evs_cmds[i];
547 error = (*vcp->ev_proc)(p, vcp);
548 }
549
550 kill_vmcmds(&vcset);
551
552 vrele(vp);
553
554 return error;
555 }
556
557 /*
558 * Execve(2). Just check the alternate emulation path, and pass it on
559 * to the NetBSD execve().
560 */
561 int
562 linux_sys_execve(p, v, retval)
563 struct proc *p;
564 void *v;
565 register_t *retval;
566 {
567 struct linux_sys_execve_args /* {
568 syscallarg(char *) path;
569 syscallarg(char **) argv;
570 syscallarg(char **) envp;
571 } */ *uap = v;
572 struct sys_execve_args ap;
573 caddr_t sg;
574
575 sg = stackgap_init(p->p_emul);
576 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
577
578 SCARG(&ap, path) = SCARG(uap, path);
579 SCARG(&ap, argp) = SCARG(uap, argp);
580 SCARG(&ap, envp) = SCARG(uap, envp);
581
582 return sys_execve(p, &ap, retval);
583 }
584