linux_exec_aout.c revision 1.7 1 /* $NetBSD: linux_exec_aout.c,v 1.7 1995/06/11 15:15:09 fvdl 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 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/filedesc.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/mount.h>
39 #include <sys/malloc.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/resourcevar.h>
44 #include <sys/wait.h>
45
46 #include <sys/mman.h>
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_kern.h>
51 #include <vm/vm_pager.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_syscallargs.h>
61 #include <compat/linux/linux_util.h>
62 #include <compat/linux/linux_exec.h>
63
64 struct elf_args {
65 u_long arg_entry; /* progran entry point */
66 u_long arg_interp; /* Interpreter load address */
67 u_long arg_phaddr; /* program header address */
68 u_long arg_phentsize; /* Size of program header */
69 u_long arg_phnum; /* Number of program headers */
70 };
71
72 static void *linux_aout_copyargs __P((struct exec_package *,
73 struct ps_strings *, void *, void *));
74 static void *linux_elf_copyargs __P((struct exec_package *, struct ps_strings *,
75 void *, void *));
76 static int linux_elf_check_header __P((Elf32_Ehdr *, int));
77 static void linux_elf_load_psection __P((struct exec_vmcmd_set *,
78 struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
79 static int linux_elf_set_segment __P((struct exec_package *, u_long, u_long,
80 int));
81 static int linux_elf_read_from __P((struct vnode *, u_long, struct proc *,
82 caddr_t, int));
83 static int linux_elf_load_file __P((struct proc *, char *,
84 struct exec_vmcmd_set *, u_long *, struct elf_args *, u_long *));
85
86 #ifdef DEBUG_EXEC_LINUX_ELF
87 #define DPRINTF(x) printf x
88 #else
89 #define DPRINTF(x)
90 #endif
91
92 #define LINUX_ELF_ALIGN(a, b) ((a) & ~((b) - 1))
93 #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
94 #define LINUX_AOUT_AUX_ARGSIZ 2
95
96 extern int linux_error[];
97 extern struct sysent linux_sysent[];
98 extern char *linux_syscallnames[];
99
100 struct emul emul_linux_aout = {
101 "linux",
102 linux_error,
103 linux_sendsig,
104 LINUX_SYS_syscall,
105 LINUX_SYS_MAXSYSCALL,
106 linux_sysent,
107 linux_syscallnames,
108 LINUX_AOUT_AUX_ARGSIZ,
109 linux_aout_copyargs,
110 setregs,
111 linux_sigcode,
112 linux_esigcode,
113 };
114
115 struct emul emul_linux_elf = {
116 "linux",
117 linux_error,
118 linux_sendsig,
119 LINUX_SYS_syscall,
120 LINUX_SYS_MAXSYSCALL,
121 linux_sysent,
122 linux_syscallnames,
123 LINUX_ELF_AUX_ARGSIZ,
124 linux_elf_copyargs,
125 setregs,
126 linux_sigcode,
127 linux_esigcode,
128 };
129
130
131 static void *
132 linux_aout_copyargs(pack, arginfo, stack, argp)
133 struct exec_package *pack;
134 struct ps_strings *arginfo;
135 void *stack;
136 void *argp;
137 {
138 char **cpp = stack;
139 char **stk = stack;
140 char *dp, *sp;
141 size_t len;
142 void *nullp = NULL;
143 int argc = arginfo->ps_nargvstr;
144 int envc = arginfo->ps_nenvstr;
145
146 if (copyout(&argc, cpp++, sizeof(argc)))
147 return NULL;
148
149 /* leave room for envp and argv */
150 cpp += 2;
151 if (copyout(&cpp, &stk[1], sizeof (cpp)))
152 return NULL;
153
154 dp = (char *) (cpp + argc + envc + 2);
155 sp = argp;
156
157 /* XXX don't copy them out, remap them! */
158 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
159
160 for (; --argc >= 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 if (copyout(&cpp, &stk[2], sizeof (cpp)))
169 return NULL;
170
171 arginfo->ps_envstr = cpp; /* remember location of envp for later */
172
173 for (; --envc >= 0; sp += len, dp += len)
174 if (copyout(&dp, cpp++, sizeof(dp)) ||
175 copyoutstr(sp, dp, ARG_MAX, &len))
176 return NULL;
177
178 if (copyout(&nullp, cpp++, sizeof(nullp)))
179 return NULL;
180
181 return cpp;
182 }
183
184 static void *
185 linux_elf_copyargs(pack, arginfo, stack, argp)
186 struct exec_package *pack;
187 struct ps_strings *arginfo;
188 void *stack;
189 void *argp;
190 {
191 char **cpp = stack;
192 char *dp, *sp;
193 size_t len;
194 void *nullp = NULL;
195 int argc = arginfo->ps_nargvstr;
196 int envc = arginfo->ps_nenvstr;
197 AuxInfo *a;
198 struct elf_args *ap;
199
200 if (copyout(&argc, cpp++, sizeof(argc)))
201 return NULL;
202
203 dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
204 sp = argp;
205
206 /* XXX don't copy them out, remap them! */
207 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
208
209 for (; --argc >= 0; sp += len, dp += len)
210 if (copyout(&dp, cpp++, sizeof(dp)) ||
211 copyoutstr(sp, dp, ARG_MAX, &len))
212 return NULL;
213
214 if (copyout(&nullp, cpp++, sizeof(nullp)))
215 return NULL;
216
217 arginfo->ps_envstr = cpp; /* remember location of envp for later */
218
219 for (; --envc >= 0; sp += len, dp += len)
220 if (copyout(&dp, cpp++, sizeof(dp)) ||
221 copyoutstr(sp, dp, ARG_MAX, &len))
222 return NULL;
223
224 if (copyout(&nullp, cpp++, sizeof(nullp)))
225 return NULL;
226
227 /*
228 * Push extra arguments on the stack needed by dynamically
229 * linked binaries
230 */
231 a = (AuxInfo *) cpp;
232 if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
233
234 DPRINTF(("phaddr=0x%x, phsize=%d, phnum=%d, interp=0x%x, ",
235 ap->arg_phaddr, ap->arg_phentsize, ap->arg_phnum,
236 ap->arg_interp));
237 DPRINTF((" entry=0x%x\n", ap->arg_entry));
238
239 a->au_id = AUX_phdr;
240 a->au_v = ap->arg_phaddr;
241 a++;
242
243 a->au_id = AUX_phent;
244 a->au_v = ap->arg_phentsize;
245 a++;
246
247 a->au_id = AUX_phnum;
248 a->au_v = ap->arg_phnum;
249 a++;
250
251 a->au_id = AUX_pagesz;
252 a->au_v = NBPG;
253 a++;
254
255 a->au_id = AUX_base;
256 a->au_v = ap->arg_interp;
257 a++;
258
259 a->au_id = AUX_flags;
260 a->au_v = 0;
261 a++;
262
263 a->au_id = AUX_entry;
264 a->au_v = ap->arg_entry;
265 a++;
266
267 a->au_id = AUX_null;
268 a->au_v = 0;
269 a++;
270
271 free((char *) ap, M_TEMP);
272 }
273 return a;
274 }
275
276 #ifdef DEBUG_EXEC_LINUX_ELF
277 static void
278 print_Ehdr(e)
279 Elf32_Ehdr *e;
280 {
281 printf("e_ident %s, ", e->e_ident);
282 printf("e_type %d, ", e->e_type);
283 printf("e_machine %d, ", e->e_machine);
284 printf("e_version %ld, ", e->e_version);
285 printf("e_entry %lx, ", e->e_entry);
286 printf("e_phoff %lx, ", e->e_phoff);
287 printf("e_shoff %lx, ", e->e_shoff);
288 printf("e_flags %lx, ", e->e_flags);
289 printf("e_ehsize %d, ", e->e_ehsize);
290 printf("e_phentsize %d, ", e->e_phentsize);
291 printf("e_phnum %d, ", e->e_phnum);
292 printf("e_shentsize %d, ", e->e_shentsize);
293 printf("e_shnum %d, ", e->e_shnum);
294 printf("e_shstrndx %d\n", e->e_shstrndx);
295 }
296
297
298 static void
299 print_Phdr(p)
300 Elf32_Phdr *p;
301 {
302 static char *types[] =
303 {
304 "null", "load", "dynamic", "interp",
305 "note", "shlib", "phdr", "entry7"
306 };
307
308 printf("p_type %ld [%s], ", p->p_type, types[p->p_type & 7]);
309 printf("p_offset %lx, ", p->p_offset);
310 printf("p_vaddr %lx, ", p->p_vaddr);
311 printf("p_paddr %lx, ", p->p_paddr);
312 printf("p_filesz %ld, ", p->p_filesz);
313 printf("p_memsz %ld, ", p->p_memsz);
314 printf("p_flags %lx, ", p->p_flags);
315 printf("p_align %ld\n", p->p_align);
316 }
317 #endif
318
319 int
320 exec_linux_aout_makecmds(p, epp)
321 struct proc *p;
322 struct exec_package *epp;
323 {
324 struct exec *linux_ep = epp->ep_hdr;
325 int machtype, magic;
326 int error = ENOEXEC;
327
328 magic = LINUX_N_MAGIC(linux_ep);
329 machtype = LINUX_N_MACHTYPE(linux_ep);
330
331
332 if (machtype != LINUX_MID_MACHINE)
333 return (ENOEXEC);
334
335 switch (magic) {
336 case QMAGIC:
337 error = exec_linux_aout_prep_qmagic(p, epp);
338 break;
339 case ZMAGIC:
340 error = exec_linux_aout_prep_zmagic(p, epp);
341 break;
342 case NMAGIC:
343 error = exec_linux_aout_prep_nmagic(p, epp);
344 break;
345 case OMAGIC:
346 error = exec_linux_aout_prep_omagic(p, epp);
347 break;
348 }
349 if (error == 0)
350 epp->ep_emul = &emul_linux_aout;
351 return error;
352 }
353
354 /*
355 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
356 * is very likely not page aligned on most architectures, it is treated
357 * as an NMAGIC here. XXX
358 */
359
360 int
361 exec_linux_aout_prep_zmagic(p, epp)
362 struct proc *p;
363 struct exec_package *epp;
364 {
365 struct exec *execp = epp->ep_hdr;
366
367 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
368 epp->ep_tsize = execp->a_text;
369 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
370 epp->ep_dsize = execp->a_data + execp->a_bss;
371 epp->ep_entry = execp->a_entry;
372
373 /* set up command for text segment */
374 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
375 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
376 VM_PROT_READ|VM_PROT_EXECUTE);
377
378 /* set up command for data segment */
379 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
380 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
381 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
382
383 /* set up command for bss segment */
384 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
385 epp->ep_daddr + execp->a_data, NULLVP, 0,
386 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
387
388 return exec_aout_setup_stack(p, epp);
389 }
390
391 /*
392 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
393 * Not different from the normal stuff.
394 */
395
396 int
397 exec_linux_aout_prep_nmagic(p, epp)
398 struct proc *p;
399 struct exec_package *epp;
400 {
401 struct exec *execp = epp->ep_hdr;
402 long bsize, baddr;
403
404 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
405 epp->ep_tsize = execp->a_text;
406 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
407 epp->ep_dsize = execp->a_data + execp->a_bss;
408 epp->ep_entry = execp->a_entry;
409
410 /* set up command for text segment */
411 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
412 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
413 VM_PROT_READ|VM_PROT_EXECUTE);
414
415 /* set up command for data segment */
416 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
417 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
418 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
419
420 /* set up command for bss segment */
421 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
422 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
423 if (bsize > 0)
424 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
425 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
426
427 return exec_aout_setup_stack(p, epp);
428 }
429
430 /*
431 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
432 * Business as usual.
433 */
434
435 int
436 exec_linux_aout_prep_omagic(p, epp)
437 struct proc *p;
438 struct exec_package *epp;
439 {
440 struct exec *execp = epp->ep_hdr;
441 long dsize, bsize, baddr;
442
443 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
444 epp->ep_tsize = execp->a_text;
445 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
446 epp->ep_dsize = execp->a_data + execp->a_bss;
447 epp->ep_entry = execp->a_entry;
448
449 /* set up command for text and data segments */
450 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
451 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
452 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
453
454 /* set up command for bss segment */
455 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
456 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
457 if (bsize > 0)
458 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
459 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
460
461 /*
462 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
463 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
464 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
465 * respectively to page boundaries.
466 * Compensate `ep_dsize' for the amount of data covered by the last
467 * text page.
468 */
469 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
470 epp->ep_dsize = (dsize > 0) ? dsize : 0;
471 return exec_aout_setup_stack(p, epp);
472 }
473
474 int
475 exec_linux_aout_prep_qmagic(p, epp)
476 struct proc *p;
477 struct exec_package *epp;
478 {
479 struct exec *execp = epp->ep_hdr;
480
481 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
482 epp->ep_tsize = execp->a_text;
483 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
484 epp->ep_dsize = execp->a_data + execp->a_bss;
485 epp->ep_entry = execp->a_entry;
486
487 /*
488 * check if vnode is in open for writing, because we want to
489 * demand-page out of it. if it is, don't do it, for various
490 * reasons
491 */
492 if ((execp->a_text != 0 || execp->a_data != 0) &&
493 epp->ep_vp->v_writecount != 0) {
494 #ifdef DIAGNOSTIC
495 if (epp->ep_vp->v_flag & VTEXT)
496 panic("exec: a VTEXT vnode has writecount != 0\n");
497 #endif
498 return ETXTBSY;
499 }
500 epp->ep_vp->v_flag |= VTEXT;
501
502 /* set up command for text segment */
503 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
504 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
505 VM_PROT_READ|VM_PROT_EXECUTE);
506
507 /* set up command for data segment */
508 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
509 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
510 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
511
512 /* set up command for bss segment */
513 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
514 epp->ep_daddr + execp->a_data, NULLVP, 0,
515 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
516
517 return exec_aout_setup_stack(p, epp);
518 }
519
520 /*
521 * linux_elf_check_header():
522 *
523 * Check header for validity; return 0 of ok ENOEXEC if error
524 */
525 static int
526 linux_elf_check_header(eh, type)
527 Elf32_Ehdr *eh;
528 int type;
529 {
530 #ifdef sparc
531 /* #$%@#$%@#$%! */
532 # define memcmp bcmp
533 #endif
534 if (memcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0) {
535 DPRINTF(("Not an elf file\n"));
536 return ENOEXEC;
537 }
538
539 switch (eh->e_machine) {
540 #ifdef i386
541 case Elf32_em_386:
542 case Elf32_em_486:
543 #endif
544 #ifdef sparc
545 case Elf32_em_sparc:
546 #endif
547 break;
548
549 default:
550 DPRINTF(("Unsupported elf machine type %d\n", eh->e_machine));
551 return ENOEXEC;
552 }
553
554 if (eh->e_type != type) {
555 DPRINTF(("Not an elf executable\n"));
556 return ENOEXEC;
557 }
558
559 return 0;
560 }
561
562
563 /*
564 * linux_elf_load_psection():
565 *
566 * Load a psection at the appropriate address
567 */
568 static void
569 linux_elf_load_psection(vcset, vp, ph, addr, size, prot)
570 struct exec_vmcmd_set *vcset;
571 struct vnode *vp;
572 Elf32_Phdr *ph;
573 u_long *addr;
574 u_long *size;
575 int *prot;
576 {
577 u_long uaddr;
578 long diff;
579 long offset;
580 u_long msize;
581
582 /*
583 * If the user specified an address, then we load there.
584 */
585 if (*addr != ~0) {
586 uaddr = *addr + ph->p_align;
587 *addr = LINUX_ELF_ALIGN(uaddr, ph->p_align);
588 uaddr = LINUX_ELF_ALIGN(ph->p_vaddr, ph->p_align);
589 diff = ph->p_vaddr - uaddr;
590 } else {
591 uaddr = ph->p_vaddr;
592 *addr = LINUX_ELF_ALIGN(uaddr, ph->p_align);
593 diff = uaddr - *addr;
594 }
595
596 *prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
597 *prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
598 *prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
599
600 offset = ph->p_offset - diff;
601 *size = ph->p_filesz + diff;
602 msize = ph->p_memsz + diff;
603
604 DPRINTF(("Elf Seg@ 0x%x/0x%x sz %d/%d off 0x%x/0x%x[%d] algn 0x%x\n",
605 ph->p_vaddr, *addr, *size, msize, ph->p_offset, offset,
606 diff, ph->p_align));
607
608 NEW_VMCMD(vcset, vmcmd_map_readvn, *size,
609 *addr, vp, offset, *prot);
610
611 /*
612 * Check if we need to extend the size of the segment
613 */
614 {
615 u_long rm = round_page(*addr + msize);
616 u_long rf = round_page(*addr + *size);
617 if (rm != rf) {
618 DPRINTF(("zeropad 0x%x-0x%x\n", rf, rm));
619 NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf,
620 rf, NULLVP, 0, *prot);
621 *size = msize;
622 }
623 }
624 }
625
626
627 /*
628 * linux_elf_set_segment():
629 *
630 * Decide if the segment is text or data, depending on the protection
631 * and set it appropriately
632 */
633 static int
634 linux_elf_set_segment(epp, vaddr, size, prot)
635 struct exec_package *epp;
636 u_long vaddr;
637 u_long size;
638 int prot;
639 {
640 /*
641 * Kludge: Unfortunately the current implementation of
642 * exec package assumes a single text and data segment.
643 * In Elf we can have more, but here we limit ourselves
644 * to two and hope :-(
645 * We also assume that the text is r-x, and data is rwx.
646 */
647 switch (prot) {
648 case (VM_PROT_READ | VM_PROT_EXECUTE):
649 if (epp->ep_tsize != ~0) {
650 DPRINTF(("More than one text segment\n"));
651 return ENOEXEC;
652 }
653 epp->ep_taddr = vaddr;
654 epp->ep_tsize = size;
655 DPRINTF(("Elf Text@ 0x%x, size %d\n", vaddr, size));
656 break;
657
658 case (VM_PROT_READ | VM_PROT_WRITE):
659 case (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE):
660 if (epp->ep_dsize != ~0) {
661 DPRINTF(("More than one data segment\n"));
662 return ENOEXEC;
663 }
664 epp->ep_daddr = vaddr;
665 epp->ep_dsize = size;
666
667 DPRINTF(("Elf Data@ 0x%x, size %d\n", vaddr, size));
668 break;
669
670 default:
671 DPRINTF(("Bad protection 0%o\n", prot));
672 return ENOEXEC;
673 }
674 return 0;
675 }
676
677
678 /*
679 * linux_elf_read_from():
680 *
681 * Read from vnode into buffer at offset.
682 */
683 static int
684 linux_elf_read_from(vp, off, p, buf, size)
685 struct vnode *vp;
686 u_long off;
687 struct proc *p;
688 caddr_t buf;
689 int size;
690 {
691 int error;
692 int resid;
693
694 DPRINTF(("read from 0x%x to 0x%x size %d\n",
695 off, buf, size));
696 if ((error = vn_rdwr(UIO_READ, vp, buf, size,
697 off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
698 &resid, p)) != 0) {
699 DPRINTF(("Bad read error %d\n", error));
700 return error;
701 }
702 /*
703 * See if we got all of it
704 */
705 if (resid != 0) {
706 DPRINTF(("Incomplete read for header ask=%d, rem=%d\n",
707 size, resid));
708 return error;
709 }
710 return 0;
711 }
712
713
714 /*
715 * linux_elf_load_file():
716 *
717 * Load a file (interpreter/library) pointed to by path
718 * [stolen from coff_load_shlib()]. Made slightly more generic than
719 * the svr4 version, for possible later use in linux_uselib().
720 */
721 static int
722 linux_elf_load_file(p, path, vcset, entry, ap, last)
723 struct proc *p;
724 char *path;
725 struct exec_vmcmd_set *vcset;
726 u_long *entry;
727 struct elf_args *ap;
728 u_long *last;
729 {
730 int error, i;
731 struct nameidata nd;
732 Elf32_Ehdr eh;
733 Elf32_Phdr *ph = NULL;
734 u_long phsize;
735 char *bp = NULL;
736 u_long addr = *last;
737
738 DPRINTF(("Loading file %s @ %x\n", path, addr));
739
740 if ((error = linux_emul_find(p, NULL, linux_emul_path, path, &bp, 0)) != 0)
741 bp = NULL;
742 else
743 path = bp;
744 /*
745 * 1. open file
746 * 2. read filehdr
747 * 3. map text, data, and bss out of it using VM_*
748 */
749 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
750 /* first get the vnode */
751 if ((error = namei(&nd)) != 0) {
752 if (bp != NULL)
753 free((char *) bp, M_TEMP);
754 return error;
755 }
756 if ((error = linux_elf_read_from(nd.ni_vp, 0, p, (caddr_t) &eh,
757 sizeof(eh))) != 0)
758 goto bad;
759
760 #ifdef DEBUG_EXEC_LINUX_ELF
761 print_Ehdr(&eh);
762 #endif
763
764 if ((error = linux_elf_check_header(&eh, Elf32_et_dyn)) != 0)
765 goto bad;
766
767 phsize = eh.e_phnum * sizeof(Elf32_Phdr);
768 ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
769
770 if ((error = linux_elf_read_from(nd.ni_vp, eh.e_phoff, p,
771 (caddr_t) ph, phsize)) != 0)
772 goto bad;
773
774 /*
775 * Load all the necessary sections
776 */
777 for (i = 0; i < eh.e_phnum; i++) {
778 u_long size = 0;
779 int prot = 0;
780 #ifdef DEBUG_EXEC_LINUX_ELF
781 print_Phdr(&ph[i]);
782 #endif
783
784 switch (ph[i].p_type) {
785 case Elf32_pt_load:
786 linux_elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
787 &size, &prot);
788 /* Assume that the text segment is r-x only */
789 if ((prot & PROT_WRITE) == 0) {
790 *entry = addr + eh.e_entry;
791 ap->arg_interp = addr;
792 DPRINTF(("Interpreter@ 0x%x\n", addr));
793 }
794 addr += size;
795 break;
796
797 case Elf32_pt_dynamic:
798 case Elf32_pt_phdr:
799 case Elf32_pt_note:
800 break;
801
802 default:
803 DPRINTF(("interp: Unexpected program header type %d\n",
804 ph[i].p_type));
805 break;
806 }
807 }
808
809 bad:
810 if (ph != NULL)
811 free((char *) ph, M_TEMP);
812 if (bp != NULL)
813 free((char *) bp, M_TEMP);
814
815 *last = addr;
816 vrele(nd.ni_vp);
817 return error;
818 }
819
820
821 /*
822 * exec_linux_elf_makecmds(): Prepare an Elf binary's exec package
823 *
824 * First, set of the various offsets/lengths in the exec package.
825 *
826 * Then, mark the text image busy (so it can be demand paged) or error
827 * out if this is not possible. Finally, set up vmcmds for the
828 * text, data, bss, and stack segments.
829 */
830 int
831 exec_linux_elf_makecmds(p, epp)
832 struct proc *p;
833 struct exec_package *epp;
834 {
835 Elf32_Ehdr *eh = epp->ep_hdr;
836 Elf32_Phdr *ph, *pp;
837 int error;
838 int i;
839 char interp[MAXPATHLEN];
840 u_long pos = 0;
841 u_long phsize;
842
843 #ifdef DEBUG_EXEC_LINUX_ELF
844 print_Ehdr(eh);
845 #endif
846 if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
847 return ENOEXEC;
848
849 if (linux_elf_check_header(eh, Elf32_et_exec))
850 return ENOEXEC;
851
852 /*
853 * check if vnode is in open for writing, because we want to
854 * demand-page out of it. if it is, don't do it, for various
855 * reasons
856 */
857 if (epp->ep_vp->v_writecount != 0) {
858 #ifdef DIAGNOSTIC
859 if (epp->ep_vp->v_flag & VTEXT)
860 panic("exec: a VTEXT vnode has writecount != 0\n");
861 #endif
862 return ETXTBSY;
863 }
864 /*
865 * Allocate space to hold all the program headers, and read them
866 * from the file
867 */
868 phsize = eh->e_phnum * sizeof(Elf32_Phdr);
869 ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
870
871 if ((error = linux_elf_read_from(epp->ep_vp, eh->e_phoff, p,
872 (caddr_t) ph, phsize)) != 0)
873 goto bad;
874
875 epp->ep_tsize = ~0;
876 epp->ep_dsize = ~0;
877
878 interp[0] = '\0';
879
880 /*
881 * Load all the necessary sections
882 */
883 for (i = 0; i < eh->e_phnum; i++) {
884 u_long addr = ~0, size = 0;
885 int prot = 0;
886
887 pp = &ph[i];
888 #ifdef DEBUG_EXEC_LINUX_ELF
889 print_Phdr(pp);
890 #endif
891
892 switch (ph[i].p_type) {
893 case Elf32_pt_load:
894 linux_elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
895 &ph[i], &addr, &size, &prot);
896 if ((error = linux_elf_set_segment(epp, addr, size,
897 prot)) != 0)
898 goto bad;
899 break;
900
901 case Elf32_pt_shlib:
902 DPRINTF(("No support for COFF libraries (yet)\n"));
903 error = ENOEXEC;
904 goto bad;
905
906 case Elf32_pt_interp:
907 if (pp->p_filesz >= sizeof(interp)) {
908 DPRINTF(("Interpreter path too long %d\n",
909 pp->p_filesz));
910 goto bad;
911 }
912 if ((error = linux_elf_read_from(epp->ep_vp, pp->p_offset, p,
913 (caddr_t) interp, pp->p_filesz)) != 0)
914 goto bad;
915 break;
916
917 case Elf32_pt_dynamic:
918 case Elf32_pt_phdr:
919 case Elf32_pt_note:
920 break;
921
922 default:
923 /*
924 * Not fatal, we don't need to understand everything
925 * :-)
926 */
927 DPRINTF(("Unsupported program header type %d\n",
928 pp->p_type));
929 break;
930 }
931 }
932
933 /*
934 * Check if we found a dynamically linked binary and arrange to load
935 * it's interpreter
936 */
937 if (interp[0]) {
938 struct elf_args *ap;
939 pos = ~0;
940
941 ap = (struct elf_args *) malloc(sizeof(struct elf_args),
942 M_TEMP, M_WAITOK);
943 if ((error = linux_elf_load_file(p, interp, &epp->ep_vmcmds,
944 &epp->ep_entry, ap, &pos)) != 0) {
945 free((char *) ap, M_TEMP);
946 goto bad;
947 }
948 /* Arrange to load the program headers. */
949 pos = LINUX_ELF_ALIGN(pos + NBPG, NBPG);
950 DPRINTF(("Program header @0x%x\n", pos));
951 ap->arg_phaddr = pos;
952 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, phsize,
953 pos, epp->ep_vp, eh->e_phoff,
954 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
955 pos += phsize;
956
957 ap->arg_phentsize = eh->e_phentsize;
958 ap->arg_phnum = eh->e_phnum;
959 ap->arg_entry = eh->e_entry;
960
961 epp->ep_emul_arg = ap;
962 } else
963 epp->ep_entry = eh->e_entry;
964
965 DPRINTF(("taddr 0x%x tsize 0x%x daddr 0x%x dsize 0x%x\n",
966 epp->ep_taddr, epp->ep_tsize, epp->ep_daddr, epp->ep_dsize));
967
968 free((char *) ph, M_TEMP);
969
970 DPRINTF(("Elf entry@ 0x%x\n", epp->ep_entry));
971 epp->ep_vp->v_flag |= VTEXT;
972
973 epp->ep_emul = &emul_linux_elf;
974
975 return exec_aout_setup_stack(p, epp);
976
977 bad:
978 free((char *) ph, M_TEMP);
979 kill_vmcmds(&epp->ep_vmcmds);
980 return ENOEXEC;
981 }
982 /*
983 * The Linux system call to load shared libraries, a.out version. The
984 * a.out shared libs are just files that are mapped onto a fixed
985 * address in the process' address space. The address is given in
986 * a_entry. Read in the header, set up some VM commands and run them.
987 *
988 * Yes, both text and data are mapped at once, so we're left with
989 * writeable text for the shared libs. The Linux crt0 seemed to break
990 * sometimes when data was mapped seperately. It munmapped a uselib()
991 * of ld.so by hand, which failed with shared text and data for ld.so
992 * Yuck.
993 *
994 * Because of the problem with ZMAGIC executables (text starts
995 * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
996 * shared libs are not handled very efficiently :-(
997 */
998
999 int
1000 linux_uselib(p, uap, retval)
1001 struct proc *p;
1002 struct linux_uselib_args /* {
1003 syscallarg(char *) path;
1004 } */ *uap;
1005 register_t *retval;
1006 {
1007 caddr_t sg;
1008 long bsize, dsize, tsize, taddr, baddr, daddr;
1009 struct nameidata ni;
1010 struct vnode *vp;
1011 struct exec hdr;
1012 struct exec_vmcmd_set vcset;
1013 int rem, i, magic, error;
1014
1015 sg = stackgap_init();
1016 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1017
1018 NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1019
1020 if ((error = namei(&ni)))
1021 return error;
1022
1023 vp = ni.ni_vp;
1024
1025 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
1026 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
1027 &rem, p))) {
1028 vrele(vp);
1029 return error;
1030 }
1031
1032 if (rem != 0) {
1033 vrele(vp);
1034 return ENOEXEC;
1035 }
1036
1037 if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
1038 return ENOEXEC;
1039
1040 magic = LINUX_N_MAGIC(&hdr);
1041 taddr = hdr.a_entry & (~(NBPG - 1));
1042 tsize = hdr.a_text;
1043 daddr = taddr + tsize;
1044 dsize = hdr.a_data + hdr.a_bss;
1045
1046 if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
1047 vrele(vp);
1048 return ETXTBSY;
1049 }
1050 vp->v_flag |= VTEXT;
1051
1052 vcset.evs_cnt = 0;
1053 vcset.evs_used = 0;
1054
1055 NEW_VMCMD(&vcset,
1056 magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
1057 hdr.a_text + hdr.a_data, taddr,
1058 vp, LINUX_N_TXTOFF(hdr, magic),
1059 VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
1060
1061 baddr = roundup(daddr + hdr.a_data, NBPG);
1062 bsize = daddr + dsize - baddr;
1063 if (bsize > 0) {
1064 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
1065 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
1066 }
1067
1068 for (i = 0; i < vcset.evs_used && !error; i++) {
1069 struct exec_vmcmd *vcp;
1070
1071 vcp = &vcset.evs_cmds[i];
1072 error = (*vcp->ev_proc)(p, vcp);
1073 }
1074
1075 kill_vmcmds(&vcset);
1076
1077 vrele(vp);
1078
1079 return error;
1080 }
1081
1082 /*
1083 * Execve(2). Just check the alternate emulation path, and pass it on
1084 * to the NetBSD execve().
1085 */
1086 int
1087 linux_execve(p, uap, retval)
1088 struct proc *p;
1089 struct linux_execve_args /* {
1090 syscallarg(char *) path;
1091 syscallarg(char **) argv;
1092 syscallarg(char **) envp;
1093 } */ *uap;
1094 register_t *retval;
1095 {
1096 caddr_t sg;
1097
1098 sg = stackgap_init();
1099 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1100
1101 return execve(p, uap, retval);
1102 }
1103