summaryrefslogtreecommitdiffstats
path: root/sys/compat/pecoff/imgact_pecoff.c
blob: 9d08513a9f27be2a2ee76151e2108b75c5471be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* $NetBSD$	 */
/* $FreeBSD$       */

/*
 * Copyright (c) 2000 Masaru OKI
 * Copyright (c) 1994, 1995, 1998 Scott Bartram
 * Copyright (c) 1994 Adam Glass
 * Copyright (c) 1993, 1994 Christopher G. Demetriou
 *
 * originally from NetBSD kern/exec_ecoff.c
 *
 * Copyright (c) 2000 Takanori Watanabe
 * Copyright (c) 2000 KUROSAWA Takahiro
 * Copyright (c) 1995-1996 Sen Schmidt
 * Copyright (c) 1996 Peter Wemm
 * All rights reserved.
 *
 * originally from FreeBSD kern/imgact_elf.c
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Masaru OKI.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include <sys/param.h>
#include <sys/systm.h>
#include <sys/imgact.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mman.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/sysent.h>
#include <sys/vnode.h>

#include <machine/reg.h>

#include <vm/vm.h>
#include <vm/vm_kern.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_object.h>
#include <vm/vm_extern.h>
#include <vm/vm_zone.h>

#include <sys/user.h>
#include <sys/exec.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <machine/cpu.h>
#include <sys/syscall.h>
#include <sys/sysent.h>
#include <machine/md_var.h>
#include <machine/pecoff_machdep.h>
#include <compat/pecoff/imgact_pecoff.h>

#include "opt_pecoff.h"

#define PECOFF_PE_SIGNATURE "PE\0\0"
static int      pecoff_fixup(register_t **, struct image_params *);
static int 
pecoff_coredump(register struct proc *, register struct vnode *,
		off_t);
#ifndef PECOFF_DEBUG
#define DPRINTF(a)
#else
#define DPRINTF(a) printf a
#endif
static struct sysentvec pecoff_sysvec = {
	SYS_MAXSYSCALL,
	sysent,
	0,
	0,
	0,
	0,
	0,
	0,
	pecoff_fixup,
	sendsig,
	sigcode,
	&szsigcode,
	0,
	"FreeBSD PECoff",
	pecoff_coredump,
	NULL,
	MINSIGSTKSZ
	
};

static const char signature[] = PECOFF_PE_SIGNATURE;

static int 
exec_pecoff_coff_prep_omagic(struct image_params *,
			     struct coff_filehdr *,
			     struct coff_aouthdr *, int peoffs);
static int 
exec_pecoff_coff_prep_nmagic(struct image_params *,
			     struct coff_filehdr *,
			     struct coff_aouthdr *, int peoffs);
static int 
exec_pecoff_coff_prep_zmagic(struct image_params *,
			     struct coff_filehdr *,
			     struct coff_aouthdr *, int peoffs);

static int 
exec_pecoff_coff_makecmds(struct image_params *,
			  struct coff_filehdr *, int);

static int      pecoff_signature(struct proc *, struct vnode *, const struct pecoff_dos_filehdr *);
static int      pecoff_read_from(struct proc *, struct vnode *, int, caddr_t, int);
static int 
pecoff_load_section(struct proc * p,
		    struct vmspace * vmspace, struct vnode * vp,
	     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
		    vm_prot_t prot);

static int 
pecoff_fixup(register_t ** stack_base, struct image_params * imgp)
{
	int             len = sizeof(struct pecoff_args);
	struct pecoff_imghdr *ap;
	register_t     *pos;

	pos = *stack_base + (imgp->argc + imgp->envc + 2);
	ap = (struct pecoff_imghdr *) imgp->auxargs;
	if (copyout(ap, pos, len)) {
		return NULL;
	}
	free(ap, M_TEMP);
	imgp->auxargs = NULL;
	(*stack_base)--;
	suword(*stack_base, (long) imgp->argc);
	return 0;
}


static int 
pecoff_coredump(register struct proc * p, register struct vnode * vp,
		off_t limit)
{
	register struct ucred *cred = p->p_ucred;
	register struct vmspace *vm = p->p_vmspace;
	int             error;
#ifdef PECOFF_DEBUG
	struct vm_map  *map;
	struct vm_map_entry *ent;
	struct reg      regs;

#endif
	if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >= limit)
		return (EFAULT);
	fill_kinfo_proc(p, &p->p_addr->u_kproc);

#if PECOFF_DEBUG
	fill_regs(p, &regs);
	printf("EIP%x\n", regs.r_eip);
	printf("EAX%x EBX%x ECX%x EDI%x\n",
	       regs.r_eax, regs.r_ebx, regs.r_ecx, regs.r_edi);
	map = &vm->vm_map;
	ent = &map->header;
	printf("%p %p %p\n", ent, ent->prev, ent->next);
#endif
	error = cpu_coredump(p, vp, cred);
	if (error == 0)
		error = vn_rdwr_inchunks(UIO_WRITE, vp, vm->vm_daddr,
				(int) ctob(vm->vm_dsize), (off_t) ctob(UPAGES), UIO_USERSPACE,
			    IO_UNIT, cred, (int *) NULL, p);
	if (error == 0)
		error = vn_rdwr_inchunks(UIO_WRITE, vp,
			(caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
				round_page(ctob(vm->vm_ssize)),
		   (off_t) ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
			    IO_UNIT, cred, (int *) NULL, p);
	return (error);

}

static int 
pecoff_load_section(struct proc * p, struct vmspace * vmspace, struct vnode * vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
{
	size_t          map_len;
	vm_offset_t     map_addr;
	int             error, rv;
	size_t          copy_len;
	size_t          copy_map_len;
	size_t          copy_start;
	vm_object_t     object;
	vm_offset_t     copy_map_offset;
	vm_offset_t     file_addr;
	vm_offset_t     data_buf = 0;

	object = vp->v_object;
	error = 0;

	map_addr = trunc_page((vm_offset_t) vmaddr);
	file_addr = trunc_page(offset);
	DPRINTF(("SECARG:%x %p %x %x\n", offset, vmaddr, memsz, filsz));
	if (file_addr != offset) {
		/*
		 * The section is not on page  boundary. We can't use
		 * vm_map_insert(). Use copyin instead.
		 */
		map_len = round_page(memsz);
		copy_len = filsz;
		copy_map_offset = file_addr;
		copy_map_len = round_page(offset + filsz) - file_addr;
		copy_start = offset - file_addr;

		DPRINTF(("offset=%x vmaddr=%lx filsz=%x memsz=%x\n",
			 offset, (long)vmaddr, filsz, memsz));
		DPRINTF(("map_len=%x copy_len=%x copy_map_offset=%x"
			 " copy_map_len=%x copy_start=%x\n",
			 map_len, copy_len, copy_map_offset,
			 copy_map_len, copy_start));
	} else {

		map_len = trunc_page(filsz);

		if (map_len != 0) {
			vm_object_reference(object);
			vm_map_lock(&vmspace->vm_map);
			rv = vm_map_insert(&vmspace->vm_map,
					   object,
					   file_addr,	/* file offset */
					   map_addr,	/* virtual start */
					   map_addr + map_len,	/* virtual end */
					   prot,
					   VM_PROT_ALL,
					   MAP_COPY_ON_WRITE | MAP_PREFAULT);

			vm_map_unlock(&vmspace->vm_map);
			if (rv != KERN_SUCCESS) {
				vm_object_deallocate(object);
				return EINVAL;
			}
			/* we can stop now if we've covered it all */
			if (memsz == filsz)
				return 0;

		}
		copy_map_offset = trunc_page(offset + filsz);
		copy_map_len = PAGE_SIZE;
		copy_start = 0;
		copy_len = (offset + filsz) - trunc_page(offset + filsz);
		map_addr = trunc_page((vm_offset_t) vmaddr + filsz);
		map_len = round_page((vm_offset_t) vmaddr + memsz) - map_addr;

	}

	if (map_len != 0) {
		vm_map_lock(&vmspace->vm_map);
		rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
				   map_addr, map_addr + map_len,
				   VM_PROT_ALL, VM_PROT_ALL, 0);
		vm_map_unlock(&vmspace->vm_map);
		DPRINTF(("EMP-rv:%d,%x %x\n", rv, map_addr, map_addr + map_len));
		if (rv != KERN_SUCCESS) {
			return EINVAL;
		}
	}
	DPRINTF(("COPYARG %x %x\n", map_addr, copy_len));
	if (copy_len != 0) {
		vm_object_reference(object);
		rv = vm_map_find(exec_map,
				 object,
				 copy_map_offset,
				 &data_buf,
				 copy_map_len,
				 TRUE,
				 VM_PROT_READ,
				 VM_PROT_ALL,
				 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
		if (rv != KERN_SUCCESS) {
			vm_object_deallocate(object);
			return EINVAL;
		}
		/* send the page fragment to user space */

		error = copyout((caddr_t) data_buf + copy_start,
				(caddr_t) map_addr, copy_len);
		vm_map_remove(exec_map, data_buf, data_buf + copy_map_len);
		DPRINTF(("%d\n", error));
		if (error)
			return (error);
	}
	/*
	 * set it to the specified protection
	 */
	vm_map_protect(&vmspace->vm_map, map_addr,
		       map_addr + map_len, prot,
		       FALSE);
	return error;

}
static int 
pecoff_load_file(struct proc * p, const char *file, u_long * addr, u_long * entry, u_long * ldexport)
{

	struct nameidata nd;
	struct pecoff_dos_filehdr dh;
	struct coff_filehdr *fp = 0;
	struct coff_aouthdr *ap;
	struct pecoff_opthdr *wp;
	struct coff_scnhdr *sh = 0;
	struct vmspace *vmspace = p->p_vmspace;
	struct vattr    attr;
	struct image_params image_params, *imgp;
	int             peofs;
	int             error, i, scnsiz;

	imgp = &image_params;
	/*
	 * Initialize part of the common data
	 */
	imgp->proc = p;
	imgp->uap = NULL;
	imgp->attr = &attr;
	imgp->firstpage = NULL;

	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, p);

	if ((error = namei(&nd)) != 0) {
		nd.ni_vp = NULL;
		goto fail;
	}
	NDFREE(&nd, NDF_ONLY_PNBUF);
	imgp->vp = nd.ni_vp;

	/*
	 * Check permissions, modes, uid, etc on the file, and "open" it.
	 */
	error = exec_check_permissions(imgp);
	if (error) {
		VOP_UNLOCK(nd.ni_vp, 0, p);
		goto fail;
	}
	VOP_UNLOCK(nd.ni_vp, 0, p);
	if (error)
		goto fail;
	if ((error = pecoff_read_from(p, imgp->vp, 0, (caddr_t) & dh, sizeof(dh))) != 0)
		goto fail;
	if ((error = pecoff_signature(p, imgp->vp, &dh) != 0))
		goto fail;
	fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
	peofs = dh.d_peofs + sizeof(signature) - 1;
	if ((error = pecoff_read_from(p, imgp->vp, peofs, (caddr_t) fp, PECOFF_HDR_SIZE) != 0))
		goto fail;
	if (COFF_BADMAG(fp)) {
		error = ENOEXEC;
		goto fail;
	}
	ap = (void *) ((char *) fp + sizeof(struct coff_filehdr));
	wp = (void *) ((char *) ap + sizeof(struct coff_aouthdr));
	/* read section header */
	scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
	sh = malloc(scnsiz, M_TEMP, M_WAITOK);
	if ((error = pecoff_read_from(p, imgp->vp, peofs + PECOFF_HDR_SIZE,
				      (caddr_t) sh, scnsiz)) != 0)
		goto fail;

	/*
	 * Read Section infomation and map sections.
	 */

	for (i = 0; i < fp->f_nscns; i++) {
		int             prot = 0;

		if (sh[i].s_flags & COFF_STYP_DISCARD)
			continue;
		/* XXX ? */
		if ((sh[i].s_flags & COFF_STYP_TEXT) &&
		    (sh[i].s_flags & COFF_STYP_EXEC) == 0)
			continue;
		if ((sh[i].s_flags & (COFF_STYP_TEXT | COFF_STYP_DATA | COFF_STYP_BSS)) == 0)
			continue;

		prot |= (sh[i].s_flags & COFF_STYP_READ) ? VM_PROT_READ : 0;
		prot |= (sh[i].s_flags & COFF_STYP_WRITE) ? VM_PROT_WRITE : 0;
		prot |= (sh[i].s_flags & COFF_STYP_EXEC) ? VM_PROT_EXECUTE : 0;

		sh[i].s_vaddr += wp->w_base;	/* RVA --> VA */
		if ((error = pecoff_load_section(p, vmspace, imgp->vp, sh[i].s_scnptr
						 ,(caddr_t) sh[i].s_vaddr,
						 sh[i].s_paddr, sh[i].s_size
						 ,prot)) != 0)
			goto fail;

	}
	*entry = wp->w_base + ap->a_entry;
	*addr = wp->w_base;
	*ldexport = wp->w_imghdr[0].i_vaddr + wp->w_base;
fail:
	if (fp)
		free(fp, M_TEMP);
	if (sh)
		free(sh, M_TEMP);
	if (nd.ni_vp)
		vrele(nd.ni_vp);

	return error;
}
static int
exec_pecoff_coff_prep_omagic(struct image_params * imgp,
			     struct coff_filehdr * fp,
			     struct coff_aouthdr * ap, int peofs)
{
	return ENOEXEC;
}
static int
exec_pecoff_coff_prep_nmagic(struct image_params * imgp,
			     struct coff_filehdr * fp,
			     struct coff_aouthdr * ap, int peofs)
{
	return ENOEXEC;
}
static int
exec_pecoff_coff_prep_zmagic(struct image_params * imgp,
			     struct coff_filehdr * fp,
			     struct coff_aouthdr * ap, int peofs)
{
	int             scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
	int             error = ENOEXEC, i;
	int             prot;
	u_long          text_size = 0, data_size = 0, dsize;
	u_long          text_addr = 0, data_addr = VM_MAXUSER_ADDRESS;
	u_long          ldexport, ldbase;
	struct pecoff_opthdr *wp;
	struct coff_scnhdr *sh;
	struct vmspace *vmspace;
	struct pecoff_args *argp = NULL;

	sh = malloc(scnsiz, M_TEMP, M_WAITOK);

	wp = (void *) ((char *) ap + sizeof(struct coff_aouthdr));
	error = pecoff_read_from(imgp->proc, imgp->vp, peofs + PECOFF_HDR_SIZE,
				 (caddr_t) sh, scnsiz);
	if ((error = exec_extract_strings(imgp)) != 0)
		goto fail;
	exec_new_vmspace(imgp);
	vmspace = imgp->proc->p_vmspace;
	for (i = 0; i < fp->f_nscns; i++) {
		prot = VM_PROT_WRITE;	/* XXX for relocation? */
		prot |= (sh[i].s_flags & COFF_STYP_READ) ? VM_PROT_READ : 0;
		prot |= (sh[i].s_flags & COFF_STYP_WRITE) ? VM_PROT_WRITE : 0;
		prot |= (sh[i].s_flags & COFF_STYP_EXEC) ? VM_PROT_EXECUTE : 0;
		sh[i].s_vaddr += wp->w_base;
		if (sh[i].s_flags & COFF_STYP_DISCARD)
			continue;
		if ((sh[i].s_flags & COFF_STYP_TEXT) != 0) {

			error = pecoff_load_section(imgp->proc, vmspace,
						    imgp->vp, sh[i].s_scnptr
			,(caddr_t) sh[i].s_vaddr, sh[i].s_paddr, sh[i].s_size
						    ,prot);
			DPRINTF(("ERROR%d\n", error));
			if (error)
				goto fail;
			text_addr = trunc_page(sh[i].s_vaddr);
			text_size = trunc_page(sh[i].s_size + sh[i].s_vaddr - text_addr);

		}
		if ((sh[i].s_flags & (COFF_STYP_DATA|COFF_STYP_BSS)) != 0) {
			if (pecoff_load_section(imgp->proc,
					   vmspace, imgp->vp, sh[i].s_scnptr
						,(caddr_t) sh[i].s_vaddr, sh[i].s_paddr, sh[i].s_size,
						prot) != 0)
				goto fail;
			data_addr = min(trunc_page(sh[i].s_vaddr), data_addr);
			dsize = round_page(sh[i].s_vaddr + sh[i].s_paddr)
				- data_addr;
			data_size = max(dsize, data_size);

		}
	}
	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
	vmspace->vm_taddr = (caddr_t) (uintptr_t) text_addr;
	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
	vmspace->vm_daddr = (caddr_t) (uintptr_t) data_addr;
	argp = malloc(sizeof(struct pecoff_args), M_TEMP, M_WAITOK);
	if (argp == NULL) {
		error = ENOMEM;
		goto fail;
	}
	argp->a_base = wp->w_base;
	argp->a_entry = wp->w_base + ap->a_entry;
	argp->a_end = data_addr + data_size;
	argp->a_subsystem = wp->w_subvers;
	error = pecoff_load_file(imgp->proc, "/usr/libexec/ld.so.dll", &ldbase, &imgp->entry_addr, &ldexport);
	if (error)
		goto fail;

	argp->a_ldbase = ldbase;
	argp->a_ldexport = ldexport;
	memcpy(argp->a_imghdr, wp->w_imghdr, sizeof(struct pecoff_imghdr) * 16);
	for (i = 0; i < 16; i++) {
		argp->a_imghdr[i].i_vaddr += wp->w_base;
	}
	imgp->proc->p_sysent = &pecoff_sysvec;
	if (error)
		goto fail;
	imgp->auxargs = argp;
	imgp->auxarg_size = sizeof(struct pecoff_args);
	imgp->interpreted = 0;

	imgp->vp->v_flag |= VTEXT;
	if (sh != NULL)
		free(sh, M_TEMP);
	return 0;
fail:
	error = (error) ? error : ENOEXEC;
	if (sh != NULL)
		free(sh, M_TEMP);
	if (argp != NULL)
		free(argp, M_TEMP);

	return error;
}

int
exec_pecoff_coff_makecmds(struct image_params * imgp,
			  struct coff_filehdr * fp, int peofs)
{
	struct coff_aouthdr *ap;
	int             error;

	if (COFF_BADMAG(fp)) {
		return ENOEXEC;
	}
	ap = (void *) ((char *) fp + sizeof(struct coff_filehdr));
	switch (ap->a_magic) {
	case COFF_OMAGIC:
		error = exec_pecoff_coff_prep_omagic(imgp, fp, ap, peofs);
		break;
	case COFF_NMAGIC:
		error = exec_pecoff_coff_prep_nmagic(imgp, fp, ap, peofs);
		break;
	case COFF_ZMAGIC:
		error = exec_pecoff_coff_prep_zmagic(imgp, fp, ap, peofs);
		break;
	default:
		return ENOEXEC;
	}

	return error;
}

static int
pecoff_signature(p, vp, dp)
	struct proc    *p;
	struct vnode   *vp;
	const struct pecoff_dos_filehdr *dp;
{
	int             error;
	char            buf[512];
	char           *pesig;
	if (DOS_BADMAG(dp)) {
		return ENOEXEC;
	}
	error = pecoff_read_from(p, vp, dp->d_peofs, buf, sizeof(buf));
	if (error) {
		return error;
	}
	pesig = buf;
	if (memcmp(pesig, signature, sizeof(signature) - 1) == 0) {
		return 0;
	}
	return EFTYPE;
}
int
pecoff_read_from(p, vp, pos, buf, siz)
	struct proc    *p;
	struct vnode   *vp;
	int             pos;
	caddr_t         buf;
	int             siz;
{
	int             error;
	size_t          resid;

	error = vn_rdwr(UIO_READ, vp, buf, siz, pos,
			UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
			&resid, p);
	if (error)
		return error;

	if (resid != 0) {
		return ENOEXEC;
	}
	return 0;
}

static int 
imgact_pecoff(struct image_params * imgp)
{
	const struct pecoff_dos_filehdr *dp = (const struct pecoff_dos_filehdr *)
	imgp->image_header;
	struct coff_filehdr *fp;
	int             error, peofs;
	error = pecoff_signature(imgp->proc, imgp->vp, dp);
	if (error) {
		return -1;
	}
	peofs = dp->d_peofs + sizeof(signature) - 1;
	fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
	error = pecoff_read_from(imgp->proc, imgp->vp, peofs, (caddr_t) fp,
				 PECOFF_HDR_SIZE);
	if (error) {
		free(fp, M_TEMP);
		return error;
	}
	error = exec_pecoff_coff_makecmds(imgp, fp, peofs);
	free(fp, M_TEMP);
	return error;
}

static struct execsw pecoff_execsw = {imgact_pecoff, "FreeBSD PEcoff"};
EXEC_SET(pecoff, pecoff_execsw);
OpenPOWER on IntegriCloud