2 * Copyright (c) 2000 Masaru OKI
3 * Copyright (c) 1994, 1995, 1998 Scott Bartram
4 * Copyright (c) 1994 Adam Glass
5 * Copyright (c) 1993, 1994 Christopher G. Demetriou
7 * originally from NetBSD kern/exec_ecoff.c
9 * Copyright (c) 2000 Takanori Watanabe
10 * Copyright (c) 2000 KUROSAWA Takahiro
11 * Copyright (c) 1995-1996 S
\ fen Schmidt
12 * Copyright (c) 1996 Peter Wemm
13 * All rights reserved.
15 * originally from FreeBSD kern/imgact_elf.c
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by Masaru OKI.
28 * 4. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/imgact.h>
50 #include <sys/malloc.h>
52 #include <sys/mutex.h>
53 #include <sys/namei.h>
55 #include <sys/signalvar.h>
56 #include <sys/sysent.h>
57 #include <sys/vnode.h>
59 #include <machine/reg.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_param.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
70 #include <sys/kernel.h>
71 #include <sys/module.h>
72 #include <machine/cpu.h>
73 #include <sys/syscall.h>
74 #include <sys/sysent.h>
75 #include <machine/md_var.h>
76 #include <machine/pecoff_machdep.h>
77 #include <compat/pecoff/imgact_pecoff.h>
79 #include "opt_pecoff.h"
81 #define PECOFF_PE_SIGNATURE "PE\0\0"
82 static int pecoff_fixup(register_t **, struct image_params *);
86 #define DPRINTF(a) printf a
88 static struct sysentvec pecoff_sysvec = {
112 exec_copyout_strings,
118 static const char signature[] = PECOFF_PE_SIGNATURE;
121 exec_pecoff_coff_prep_omagic(struct image_params *,
122 struct coff_filehdr *,
123 struct coff_aouthdr *, int peoffs);
125 exec_pecoff_coff_prep_nmagic(struct image_params *,
126 struct coff_filehdr *,
127 struct coff_aouthdr *, int peoffs);
129 exec_pecoff_coff_prep_zmagic(struct image_params *,
130 struct coff_filehdr *,
131 struct coff_aouthdr *, int peoffs);
134 exec_pecoff_coff_makecmds(struct image_params *,
135 struct coff_filehdr *, int);
137 static int pecoff_signature(struct thread *, struct vnode *, const struct pecoff_dos_filehdr *);
138 static int pecoff_read_from(struct thread *, struct vnode *, int, caddr_t, int);
140 pecoff_load_section(struct thread * td,
141 struct vmspace * vmspace, struct vnode * vp,
142 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
146 pecoff_fixup(register_t ** stack_base, struct image_params * imgp)
148 int len = sizeof(struct pecoff_args);
149 struct pecoff_imghdr *ap;
152 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
153 ap = (struct pecoff_imghdr *) imgp->auxargs;
154 if (copyout(ap, pos, len)) {
158 imgp->auxargs = NULL;
160 suword(*stack_base, (long) imgp->args->argc);
165 pecoff_load_section(struct thread * td, struct vmspace * vmspace, struct vnode * vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
168 vm_offset_t map_addr;
174 vm_offset_t copy_map_offset;
175 vm_offset_t file_addr;
176 vm_offset_t data_buf = 0;
178 object = vp->v_object;
181 map_addr = trunc_page((vm_offset_t) vmaddr);
182 file_addr = trunc_page(offset);
183 DPRINTF(("SECARG:%x %p %x %x\n", offset, vmaddr, memsz, filsz));
184 if (file_addr != offset) {
186 * The section is not on page boundary. We can't use
187 * vm_map_insert(). Use copyin instead.
189 map_len = round_page(memsz);
191 copy_map_offset = file_addr;
192 copy_map_len = round_page(offset + filsz) - file_addr;
193 copy_start = offset - file_addr;
195 DPRINTF(("offset=%x vmaddr=%lx filsz=%x memsz=%x\n",
196 offset, (long)vmaddr, filsz, memsz));
197 DPRINTF(("map_len=%x copy_len=%x copy_map_offset=%x"
198 " copy_map_len=%x copy_start=%x\n",
199 map_len, copy_len, copy_map_offset,
200 copy_map_len, copy_start));
203 map_len = trunc_page(filsz);
206 vm_object_reference(object);
207 vm_map_lock(&vmspace->vm_map);
208 rv = vm_map_insert(&vmspace->vm_map,
210 file_addr, /* file offset */
211 map_addr, /* virtual start */
212 map_addr + map_len, /* virtual end */
215 MAP_COPY_ON_WRITE | MAP_PREFAULT);
217 vm_map_unlock(&vmspace->vm_map);
218 if (rv != KERN_SUCCESS) {
219 vm_object_deallocate(object);
222 /* we can stop now if we've covered it all */
227 copy_map_offset = trunc_page(offset + filsz);
228 copy_map_len = PAGE_SIZE;
230 copy_len = (offset + filsz) - trunc_page(offset + filsz);
231 map_addr = trunc_page((vm_offset_t) vmaddr + filsz);
232 map_len = round_page((vm_offset_t) vmaddr + memsz) - map_addr;
237 vm_map_lock(&vmspace->vm_map);
238 rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
239 map_addr, map_addr + map_len,
240 VM_PROT_ALL, VM_PROT_ALL, 0);
241 vm_map_unlock(&vmspace->vm_map);
242 DPRINTF(("EMP-rv:%d,%x %x\n", rv, map_addr, map_addr + map_len));
243 if (rv != KERN_SUCCESS) {
247 DPRINTF(("COPYARG %x %x\n", map_addr, copy_len));
249 vm_object_reference(object);
250 rv = vm_map_find(exec_map,
258 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
259 if (rv != KERN_SUCCESS) {
260 vm_object_deallocate(object);
263 /* send the page fragment to user space */
265 error = copyout((caddr_t) data_buf + copy_start,
266 (caddr_t) map_addr, copy_len);
267 vm_map_remove(exec_map, data_buf, data_buf + copy_map_len);
268 DPRINTF(("%d\n", error));
273 * set it to the specified protection
275 vm_map_protect(&vmspace->vm_map, map_addr,
276 map_addr + map_len, prot,
282 pecoff_load_file(struct thread * td, const char *file, u_long * addr, u_long * entry, u_long * ldexport)
286 struct pecoff_dos_filehdr dh;
287 struct coff_filehdr *fp = 0;
288 struct coff_aouthdr *ap;
289 struct pecoff_opthdr *wp;
290 struct coff_scnhdr *sh = 0;
291 struct vmspace *vmspace = td->td_proc->p_vmspace;
293 struct image_params image_params, *imgp;
295 int error, i, scnsiz;
297 imgp = &image_params;
299 * Initialize part of the common data
301 imgp->proc = td->td_proc;
302 imgp->execlabel = NULL;
304 imgp->firstpage = NULL;
306 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, td);
308 if ((error = namei(&nd)) != 0) {
312 NDFREE(&nd, NDF_ONLY_PNBUF);
316 * Check permissions, modes, uid, etc on the file, and "open" it.
318 error = exec_check_permissions(imgp);
319 VOP_UNLOCK(nd.ni_vp, 0, td);
322 if ((error = pecoff_read_from(td, imgp->vp, 0, (caddr_t) & dh, sizeof(dh))) != 0)
324 if ((error = pecoff_signature(td, imgp->vp, &dh) != 0))
326 fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
327 peofs = dh.d_peofs + sizeof(signature) - 1;
328 if ((error = pecoff_read_from(td, imgp->vp, peofs, (caddr_t) fp, PECOFF_HDR_SIZE) != 0))
330 if (COFF_BADMAG(fp)) {
334 ap = (void *) ((char *) fp + sizeof(struct coff_filehdr));
335 wp = (void *) ((char *) ap + sizeof(struct coff_aouthdr));
336 /* read section header */
337 scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
338 sh = malloc(scnsiz, M_TEMP, M_WAITOK);
339 if ((error = pecoff_read_from(td, imgp->vp, peofs + PECOFF_HDR_SIZE,
340 (caddr_t) sh, scnsiz)) != 0)
344 * Read Section infomation and map sections.
347 for (i = 0; i < fp->f_nscns; i++) {
350 if (sh[i].s_flags & COFF_STYP_DISCARD)
353 if ((sh[i].s_flags & COFF_STYP_TEXT) &&
354 (sh[i].s_flags & COFF_STYP_EXEC) == 0)
356 if ((sh[i].s_flags & (COFF_STYP_TEXT | COFF_STYP_DATA | COFF_STYP_BSS)) == 0)
359 prot |= (sh[i].s_flags & COFF_STYP_READ) ? VM_PROT_READ : 0;
360 prot |= (sh[i].s_flags & COFF_STYP_WRITE) ? VM_PROT_WRITE : 0;
361 prot |= (sh[i].s_flags & COFF_STYP_EXEC) ? VM_PROT_EXECUTE : 0;
363 sh[i].s_vaddr += wp->w_base; /* RVA --> VA */
364 if ((error = pecoff_load_section(td, vmspace, imgp->vp, sh[i].s_scnptr
365 ,(caddr_t) sh[i].s_vaddr,
366 sh[i].s_paddr, sh[i].s_size
371 *entry = wp->w_base + ap->a_entry;
373 *ldexport = wp->w_imghdr[0].i_vaddr + wp->w_base;
385 exec_pecoff_coff_prep_omagic(struct image_params * imgp,
386 struct coff_filehdr * fp,
387 struct coff_aouthdr * ap, int peofs)
392 exec_pecoff_coff_prep_nmagic(struct image_params * imgp,
393 struct coff_filehdr * fp,
394 struct coff_aouthdr * ap, int peofs)
399 exec_pecoff_coff_prep_zmagic(struct image_params * imgp,
400 struct coff_filehdr * fp,
401 struct coff_aouthdr * ap, int peofs)
403 int scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
404 int error = ENOEXEC, i;
406 u_long text_size = 0, data_size = 0, dsize;
407 u_long text_addr = 0, data_addr = VM_MAXUSER_ADDRESS;
408 u_long ldexport = 0, ldbase = 0;
409 struct pecoff_opthdr *wp;
410 struct coff_scnhdr *sh;
411 struct vmspace *vmspace;
412 struct pecoff_args *argp = NULL;
414 sh = malloc(scnsiz, M_TEMP, M_WAITOK);
416 wp = (void *) ((char *) ap + sizeof(struct coff_aouthdr));
417 error = pecoff_read_from(FIRST_THREAD_IN_PROC(imgp->proc), imgp->vp,
418 peofs + PECOFF_HDR_SIZE, (caddr_t) sh, scnsiz);
421 error = exec_new_vmspace(imgp, &pecoff_sysvec);
424 vmspace = imgp->proc->p_vmspace;
425 for (i = 0; i < fp->f_nscns; i++) {
426 prot = VM_PROT_WRITE; /* XXX for relocation? */
427 prot |= (sh[i].s_flags & COFF_STYP_READ) ? VM_PROT_READ : 0;
428 prot |= (sh[i].s_flags & COFF_STYP_WRITE) ? VM_PROT_WRITE : 0;
429 prot |= (sh[i].s_flags & COFF_STYP_EXEC) ? VM_PROT_EXECUTE : 0;
430 sh[i].s_vaddr += wp->w_base;
431 if (sh[i].s_flags & COFF_STYP_DISCARD)
433 if ((sh[i].s_flags & COFF_STYP_TEXT) != 0) {
435 error = pecoff_load_section(
436 FIRST_THREAD_IN_PROC(imgp->proc),
437 vmspace, imgp->vp, sh[i].s_scnptr,
438 (caddr_t) sh[i].s_vaddr, sh[i].s_paddr,
440 DPRINTF(("ERROR%d\n", error));
443 text_addr = trunc_page(sh[i].s_vaddr);
444 text_size = trunc_page(sh[i].s_size + sh[i].s_vaddr - text_addr);
447 if ((sh[i].s_flags & (COFF_STYP_DATA|COFF_STYP_BSS)) != 0) {
448 if (pecoff_load_section(
449 FIRST_THREAD_IN_PROC(imgp->proc), vmspace,
450 imgp->vp, sh[i].s_scnptr, (caddr_t) sh[i].s_vaddr,
451 sh[i].s_paddr, sh[i].s_size, prot) != 0)
453 data_addr = min(trunc_page(sh[i].s_vaddr), data_addr);
454 dsize = round_page(sh[i].s_vaddr + sh[i].s_paddr)
456 data_size = max(dsize, data_size);
460 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
461 vmspace->vm_taddr = (caddr_t) (uintptr_t) text_addr;
462 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
463 vmspace->vm_daddr = (caddr_t) (uintptr_t) data_addr;
464 argp = malloc(sizeof(struct pecoff_args), M_TEMP, M_WAITOK);
469 argp->a_base = wp->w_base;
470 argp->a_entry = wp->w_base + ap->a_entry;
471 argp->a_end = data_addr + data_size;
472 argp->a_subsystem = wp->w_subvers;
473 error = pecoff_load_file(FIRST_THREAD_IN_PROC(imgp->proc),
474 "/usr/libexec/ld.so.dll", &ldbase, &imgp->entry_addr, &ldexport);
478 argp->a_ldbase = ldbase;
479 argp->a_ldexport = ldexport;
480 memcpy(argp->a_imghdr, wp->w_imghdr, sizeof(struct pecoff_imghdr) * 16);
481 for (i = 0; i < 16; i++) {
482 argp->a_imghdr[i].i_vaddr += wp->w_base;
484 imgp->proc->p_sysent = &pecoff_sysvec;
485 imgp->auxargs = argp;
486 imgp->auxarg_size = sizeof(struct pecoff_args);
487 imgp->interpreted = 0;
493 error = (error) ? error : ENOEXEC;
503 exec_pecoff_coff_makecmds(struct image_params * imgp,
504 struct coff_filehdr * fp, int peofs)
506 struct coff_aouthdr *ap;
509 if (COFF_BADMAG(fp)) {
512 ap = (void *) ((char *) fp + sizeof(struct coff_filehdr));
513 switch (ap->a_magic) {
515 error = exec_pecoff_coff_prep_omagic(imgp, fp, ap, peofs);
518 error = exec_pecoff_coff_prep_nmagic(imgp, fp, ap, peofs);
521 error = exec_pecoff_coff_prep_zmagic(imgp, fp, ap, peofs);
531 pecoff_signature(td, vp, dp)
534 const struct pecoff_dos_filehdr *dp;
539 if (DOS_BADMAG(dp)) {
542 error = pecoff_read_from(td, vp, dp->d_peofs, buf, sizeof(buf));
547 if (memcmp(pesig, signature, sizeof(signature) - 1) == 0) {
553 pecoff_read_from(td, vp, pos, buf, siz)
563 error = vn_rdwr(UIO_READ, vp, buf, siz, pos,
564 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
576 imgact_pecoff(struct image_params * imgp)
578 const struct pecoff_dos_filehdr *dp = (const struct pecoff_dos_filehdr *)
580 struct coff_filehdr *fp;
582 struct thread *td = curthread;
584 error = pecoff_signature(FIRST_THREAD_IN_PROC(imgp->proc),
589 VOP_UNLOCK(imgp->vp, 0, td);
591 peofs = dp->d_peofs + sizeof(signature) - 1;
592 fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
593 error = pecoff_read_from(FIRST_THREAD_IN_PROC(imgp->proc),
594 imgp->vp, peofs, (caddr_t) fp, PECOFF_HDR_SIZE);
598 error = exec_pecoff_coff_makecmds(imgp, fp, peofs);
601 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
605 static struct execsw pecoff_execsw = {imgact_pecoff, "FreeBSD PEcoff"};
606 EXEC_SET(pecoff, pecoff_execsw);