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