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