2003-09-07 02:55:06 +04:00
|
|
|
/* $NetBSD: pecoff_exec.c,v 1.24 2003/09/06 22:55:06 erh Exp $ */
|
2000-06-10 02:38:56 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Masaru OKI
|
|
|
|
* Copyright (c) 1994, 1995, 1998 Scott Bartram
|
|
|
|
* Copyright (c) 1994 Adam Glass
|
|
|
|
* Copyright (c) 1993, 1994 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2000-06-15 19:37:05 +04:00
|
|
|
* from compat/ibcs2/ibcs2_exec.c
|
2000-06-10 02:38:56 +04:00
|
|
|
* originally from kern/exec_ecoff.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:
|
2000-06-15 19:37:05 +04:00
|
|
|
* This product includes software developed by Scott Bartram.
|
2000-06-10 02:38:56 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 05:07:52 +03:00
|
|
|
#include <sys/cdefs.h>
|
2003-09-07 02:55:06 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: pecoff_exec.c,v 1.24 2003/09/06 22:55:06 erh Exp $");
|
2001-11-13 05:07:52 +03:00
|
|
|
|
2000-06-10 02:38:56 +04:00
|
|
|
/*#define DEBUG_PECOFF*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <sys/exec_coff.h>
|
|
|
|
#include <machine/coff_machdep.h>
|
|
|
|
|
|
|
|
#include <compat/pecoff/pecoff_exec.h>
|
|
|
|
#include <compat/pecoff/pecoff_util.h>
|
2002-03-18 10:11:06 +03:00
|
|
|
#include <compat/pecoff/pecoff_syscall.h>
|
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
int pecoff_signature (struct proc *p, struct vnode *vp,
|
2002-03-18 10:11:06 +03:00
|
|
|
struct pecoff_dos_filehdr *dp);
|
2003-06-30 02:28:00 +04:00
|
|
|
int pecoff_load_file (struct proc *p, struct exec_package *epp,
|
2002-03-18 10:11:06 +03:00
|
|
|
const char *path, struct exec_vmcmd_set *vcset,
|
|
|
|
u_long *entry, struct pecoff_args *argp);
|
|
|
|
void pecoff_load_section (struct exec_vmcmd_set *vcset, struct vnode *vp,
|
|
|
|
struct coff_scnhdr *sh, long *addr,
|
|
|
|
u_long *size, int *prot);
|
2003-06-30 02:28:00 +04:00
|
|
|
int exec_pecoff_makecmds (struct proc *p, struct exec_package *epp);
|
|
|
|
int exec_pecoff_coff_makecmds (struct proc *p, struct exec_package *epp,
|
2002-03-18 10:11:06 +03:00
|
|
|
struct coff_filehdr *fp, int peofs);
|
|
|
|
int exec_pecoff_prep_omagic (struct proc *p, struct exec_package *epp,
|
|
|
|
struct coff_filehdr *fp,
|
|
|
|
struct coff_aouthdr *ap, int peofs);
|
|
|
|
int exec_pecoff_prep_nmagic (struct proc *p, struct exec_package *epp,
|
|
|
|
struct coff_filehdr *fp,
|
|
|
|
struct coff_aouthdr *ap, int peofs);
|
2003-06-30 02:28:00 +04:00
|
|
|
int exec_pecoff_prep_zmagic (struct proc *p, struct exec_package *epp,
|
2002-03-18 10:11:06 +03:00
|
|
|
struct coff_filehdr *fp,
|
|
|
|
struct coff_aouthdr *ap, int peofs);
|
|
|
|
|
|
|
|
|
2001-07-30 01:28:45 +04:00
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
pecoff_copyargs(p, pack, arginfo, stackp, argp)
|
|
|
|
struct proc *p;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct exec_package *pack;
|
|
|
|
struct ps_strings *arginfo;
|
2001-07-30 01:28:45 +04:00
|
|
|
char **stackp;
|
2000-06-10 02:38:56 +04:00
|
|
|
void *argp;
|
|
|
|
{
|
|
|
|
int len = sizeof(struct pecoff_args);
|
2001-12-03 09:11:33 +03:00
|
|
|
struct pecoff_args *ap;
|
2001-07-30 01:28:45 +04:00
|
|
|
int error;
|
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
|
2001-07-30 01:28:45 +04:00
|
|
|
return error;
|
2000-06-10 02:38:56 +04:00
|
|
|
|
2001-12-03 09:11:33 +03:00
|
|
|
ap = (struct pecoff_args *)pack->ep_emul_arg;
|
2001-07-30 01:28:45 +04:00
|
|
|
if ((error = copyout(ap, *stackp, len)) != 0)
|
|
|
|
return error;
|
|
|
|
|
2000-06-10 02:38:56 +04:00
|
|
|
#if 0 /* kern_exec.c? */
|
2002-03-18 10:11:06 +03:00
|
|
|
free(ap, M_TEMP);
|
2000-06-10 02:38:56 +04:00
|
|
|
pack->ep_emul_arg = 0;
|
|
|
|
#endif
|
|
|
|
|
2001-07-30 01:28:45 +04:00
|
|
|
*stackp += len;
|
|
|
|
return error;
|
2000-06-10 02:38:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PECOFF_SIGNATURE "PE\0\0"
|
|
|
|
static const char signature[] = PECOFF_SIGNATURE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check PE signature.
|
|
|
|
*/
|
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
pecoff_signature(p, vp, dp)
|
|
|
|
struct proc *p;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct vnode *vp;
|
|
|
|
struct pecoff_dos_filehdr *dp;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
char buf[sizeof(signature) - 1];
|
|
|
|
|
|
|
|
if (DOS_BADMAG(dp)) {
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, vp, dp->d_peofs, buf, sizeof(buf));
|
2000-06-10 02:38:56 +04:00
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (memcmp(buf, signature, sizeof(signature) - 1) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return EFTYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load(mmap) file. for dynamic linker (ld.so.dll)
|
|
|
|
*/
|
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
pecoff_load_file(p, epp, path, vcset, entry, argp)
|
|
|
|
struct proc *p;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct exec_package *epp;
|
2002-03-18 10:11:06 +03:00
|
|
|
const char *path;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct exec_vmcmd_set *vcset;
|
|
|
|
u_long *entry;
|
|
|
|
struct pecoff_args *argp;
|
|
|
|
{
|
|
|
|
int error, peofs, scnsiz, i;
|
|
|
|
struct nameidata nd;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct vattr attr;
|
|
|
|
struct pecoff_dos_filehdr dh;
|
|
|
|
struct coff_filehdr *fp = 0;
|
|
|
|
struct coff_aouthdr *ap;
|
|
|
|
struct pecoff_opthdr *wp;
|
|
|
|
struct coff_scnhdr *sh = 0;
|
2002-03-18 10:11:06 +03:00
|
|
|
const char *bp;
|
2000-06-10 02:38:56 +04:00
|
|
|
|
2002-11-29 22:35:25 +03:00
|
|
|
/*
|
|
|
|
* Following has ~same effect as emul_find_interp(), but the code
|
|
|
|
* needs to do some more checks while having the vnode open.
|
|
|
|
* emul_find_interp() wouldn't really simplify handling here.
|
|
|
|
*/
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
|
2002-03-24 08:55:49 +03:00
|
|
|
path, &bp, 0))) {
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(path) + 1;
|
|
|
|
if (len > MAXPATHLEN)
|
|
|
|
return error;
|
|
|
|
ptr = malloc(len, M_TEMP, M_WAITOK);
|
|
|
|
copystr(path, ptr, len, 0);
|
|
|
|
bp = ptr;
|
|
|
|
}
|
2002-11-29 22:35:25 +03:00
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, bp, p);
|
2002-03-18 10:11:06 +03:00
|
|
|
if ((error = namei(&nd)) != 0) {
|
|
|
|
free((void *)bp, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
2000-06-10 02:38:56 +04:00
|
|
|
vp = nd.ni_vp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Similarly, if it's not marked as executable, or it's not a regular
|
|
|
|
* file, we don't allow it to be used.
|
|
|
|
*/
|
|
|
|
if (vp->v_type != VREG) {
|
|
|
|
error = EACCES;
|
|
|
|
goto badunlock;
|
|
|
|
}
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
goto badunlock;
|
|
|
|
|
|
|
|
/* get attributes */
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
goto badunlock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check mount point. Though we're not trying to exec this binary,
|
|
|
|
* we will be executing code from it, so if the mount point
|
|
|
|
* disallows execution or set-id-ness, we punt or kill the set-id.
|
|
|
|
*/
|
|
|
|
if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
|
|
|
|
error = EACCES;
|
|
|
|
goto badunlock;
|
|
|
|
}
|
|
|
|
if (vp->v_mount->mnt_flag & MNT_NOSUID)
|
|
|
|
epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
|
|
|
|
|
2003-09-07 02:55:06 +04:00
|
|
|
if ((error = vn_marktext(vp)))
|
|
|
|
goto badunlock;
|
|
|
|
|
2000-06-10 02:38:56 +04:00
|
|
|
VOP_UNLOCK(vp, 0);
|
|
|
|
/*
|
|
|
|
* Read header.
|
|
|
|
*/
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, vp, 0, &dh, sizeof(dh));
|
2000-06-10 02:38:56 +04:00
|
|
|
if (error != 0)
|
|
|
|
goto bad;
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = pecoff_signature(p, vp, &dh)) != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
goto bad;
|
|
|
|
fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
|
|
|
|
peofs = dh.d_peofs + sizeof(signature) - 1;
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, vp, peofs, fp, PECOFF_HDR_SIZE);
|
2003-01-08 20:43:51 +03:00
|
|
|
if (error != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
goto bad;
|
|
|
|
if (COFF_BADMAG(fp)) {
|
|
|
|
error = ENOEXEC;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
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);
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = exec_read_from(p, vp, peofs + PECOFF_HDR_SIZE, sh,
|
2001-07-14 06:05:05 +04:00
|
|
|
scnsiz)) != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read section header, and mmap.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < fp->f_nscns; i++) {
|
|
|
|
int prot = 0;
|
|
|
|
long addr, size;
|
|
|
|
|
|
|
|
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;
|
2001-11-27 20:59:49 +03:00
|
|
|
if ((sh[i].s_flags & (COFF_STYP_TEXT|COFF_STYP_DATA|
|
|
|
|
COFF_STYP_BSS|COFF_STYP_READ)) == 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
continue;
|
|
|
|
sh[i].s_vaddr += wp->w_base; /* RVA --> VA */
|
|
|
|
pecoff_load_section(vcset, vp, &sh[i], &addr, &size, &prot);
|
|
|
|
}
|
|
|
|
*entry = wp->w_base + ap->a_entry;
|
|
|
|
argp->a_ldbase = wp->w_base;
|
|
|
|
argp->a_ldexport = wp->w_imghdr[0].i_vaddr + wp->w_base;
|
|
|
|
|
2002-03-18 10:11:06 +03:00
|
|
|
free(fp, M_TEMP);
|
|
|
|
free(sh, M_TEMP);
|
|
|
|
free((void *)bp, M_TEMP);
|
2000-06-10 02:38:56 +04:00
|
|
|
vrele(vp);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
badunlock:
|
|
|
|
VOP_UNLOCK(vp, 0);
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if (fp != 0)
|
2002-03-18 10:11:06 +03:00
|
|
|
free(fp, M_TEMP);
|
2000-06-10 02:38:56 +04:00
|
|
|
if (sh != 0)
|
2002-03-18 10:11:06 +03:00
|
|
|
free(sh, M_TEMP);
|
|
|
|
free((void *)bp, M_TEMP);
|
2000-06-10 02:38:56 +04:00
|
|
|
vrele(vp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mmap one section.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pecoff_load_section(vcset, vp, sh, addr, size, prot)
|
|
|
|
struct exec_vmcmd_set *vcset;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct coff_scnhdr *sh;
|
|
|
|
long *addr;
|
|
|
|
u_long *size;
|
|
|
|
int *prot;
|
|
|
|
{
|
|
|
|
u_long diff, offset;
|
|
|
|
|
|
|
|
*addr = COFF_ALIGN(sh->s_vaddr);
|
|
|
|
diff = (sh->s_vaddr - *addr);
|
|
|
|
offset = sh->s_scnptr - diff;
|
2001-01-15 20:05:32 +03:00
|
|
|
*size = COFF_ROUND(sh->s_size + diff, COFF_LDPGSZ);
|
2000-06-10 02:38:56 +04:00
|
|
|
|
|
|
|
*prot |= (sh->s_flags & COFF_STYP_EXEC) ? VM_PROT_EXECUTE : 0;
|
|
|
|
*prot |= (sh->s_flags & COFF_STYP_READ) ? VM_PROT_READ : 0;
|
|
|
|
*prot |= (sh->s_flags & COFF_STYP_WRITE) ? VM_PROT_WRITE : 0;
|
|
|
|
|
2001-01-15 20:05:32 +03:00
|
|
|
if (diff == 0 && offset == COFF_ALIGN(offset))
|
|
|
|
NEW_VMCMD(vcset, vmcmd_map_pagedvn, *size, *addr, vp,
|
|
|
|
offset, *prot);
|
|
|
|
else
|
|
|
|
NEW_VMCMD(vcset, vmcmd_map_readvn, sh->s_size, sh->s_vaddr, vp,
|
|
|
|
sh->s_scnptr, *prot);
|
|
|
|
|
|
|
|
if (*size < sh->s_paddr) {
|
2000-06-10 02:38:56 +04:00
|
|
|
u_long baddr, bsize;
|
|
|
|
|
|
|
|
baddr = *addr + COFF_ROUND(*size, COFF_LDPGSZ);
|
|
|
|
bsize = sh->s_paddr - COFF_ROUND(*size, COFF_LDPGSZ);
|
|
|
|
DPRINTF(("additional zero space (addr %lx size %ld)\n",
|
|
|
|
baddr, bsize));
|
|
|
|
NEW_VMCMD(vcset, vmcmd_map_zero, bsize, baddr,
|
|
|
|
NULLVP, 0, *prot);
|
|
|
|
*size = COFF_ROUND(sh->s_paddr, COFF_LDPGSZ);
|
|
|
|
}
|
|
|
|
DPRINTF(("section %s loaded. (addr %lx size %ld prot %d)\n",
|
|
|
|
sh->s_name, sh->s_vaddr, sh->s_size, *prot));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
exec_pecoff_makecmds(p, epp)
|
|
|
|
struct proc *p;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct exec_package *epp;
|
|
|
|
{
|
|
|
|
int error, peofs;
|
|
|
|
struct pecoff_dos_filehdr *dp = epp->ep_hdr;
|
|
|
|
struct coff_filehdr *fp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mmap EXE file (PE format)
|
|
|
|
* 1. read header (DOS,PE)
|
|
|
|
* 2. mmap code section (READ|EXEC)
|
|
|
|
* 3. mmap other section, such as data (READ|WRITE|EXEC)
|
|
|
|
*/
|
|
|
|
if (epp->ep_hdrvalid < PECOFF_DOS_HDR_SIZE) {
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = pecoff_signature(p, epp->ep_vp, dp)) != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
return error;
|
2003-09-07 02:55:06 +04:00
|
|
|
|
|
|
|
if ((error = vn_marktext(epp->ep_vp)) != 0)
|
|
|
|
return error;
|
|
|
|
|
2000-06-10 02:38:56 +04:00
|
|
|
peofs = dp->d_peofs + sizeof(signature) - 1;
|
|
|
|
fp = malloc(PECOFF_HDR_SIZE, M_TEMP, M_WAITOK);
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, epp->ep_vp, peofs, fp, PECOFF_HDR_SIZE);
|
2000-06-10 02:38:56 +04:00
|
|
|
if (error) {
|
|
|
|
free(fp, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_pecoff_coff_makecmds(p, epp, fp, peofs);
|
2000-06-10 02:38:56 +04:00
|
|
|
|
2000-11-21 03:37:49 +03:00
|
|
|
if (error != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
kill_vmcmds(&epp->ep_vmcmds);
|
|
|
|
|
|
|
|
free(fp, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
exec_pecoff_coff_makecmds(p, epp, fp, peofs)
|
|
|
|
struct proc *p;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct exec_package *epp;
|
|
|
|
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_prep_omagic(p, epp, fp, ap, peofs);
|
|
|
|
break;
|
|
|
|
case COFF_NMAGIC:
|
|
|
|
error = exec_pecoff_prep_nmagic(p, epp, fp, ap, peofs);
|
|
|
|
break;
|
|
|
|
case COFF_ZMAGIC:
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_pecoff_prep_zmagic(p, epp, fp, ap, peofs);
|
2000-06-10 02:38:56 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
exec_pecoff_prep_omagic(p, epp, fp, ap, peofs)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
struct coff_filehdr *fp;
|
|
|
|
struct coff_aouthdr *ap;
|
|
|
|
int peofs;
|
|
|
|
{
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
exec_pecoff_prep_nmagic(p, epp, fp, ap, peofs)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
struct coff_filehdr *fp;
|
|
|
|
struct coff_aouthdr *ap;
|
|
|
|
int peofs;
|
|
|
|
{
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
exec_pecoff_prep_zmagic(p, epp, fp, ap, peofs)
|
|
|
|
struct proc *p;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct exec_package *epp;
|
|
|
|
struct coff_filehdr *fp;
|
|
|
|
struct coff_aouthdr *ap;
|
|
|
|
int peofs;
|
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
struct pecoff_opthdr *wp;
|
2003-01-08 20:43:51 +03:00
|
|
|
long tsize, daddr, dsize, baddr, bsize;
|
2000-06-10 02:38:56 +04:00
|
|
|
struct coff_scnhdr *sh;
|
|
|
|
struct pecoff_args *argp;
|
|
|
|
int scnsiz = sizeof(struct coff_scnhdr) * fp->f_nscns;
|
|
|
|
|
|
|
|
wp = (void *)((char *)ap + sizeof(struct coff_aouthdr));
|
|
|
|
|
2003-01-08 20:43:51 +03:00
|
|
|
epp->ep_tsize = ap->a_tsize;
|
2000-06-10 02:38:56 +04:00
|
|
|
epp->ep_daddr = VM_MAXUSER_ADDRESS;
|
|
|
|
epp->ep_dsize = 0;
|
|
|
|
/* read section header */
|
|
|
|
sh = malloc(scnsiz, M_TEMP, M_WAITOK);
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, epp->ep_vp, peofs + PECOFF_HDR_SIZE, sh,
|
2001-07-14 06:05:05 +04:00
|
|
|
scnsiz);
|
2000-06-10 02:38:56 +04:00
|
|
|
if (error) {
|
|
|
|
free(sh, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* map section
|
|
|
|
*/
|
|
|
|
for (i = 0; i < fp->f_nscns; i++) {
|
|
|
|
int prot = /*0*/VM_PROT_WRITE;
|
2001-11-27 20:59:49 +03:00
|
|
|
long s_flags = sh[i].s_flags;
|
2000-06-10 02:38:56 +04:00
|
|
|
|
2001-11-27 20:59:49 +03:00
|
|
|
if ((s_flags & COFF_STYP_DISCARD) != 0)
|
2000-06-10 02:38:56 +04:00
|
|
|
continue;
|
|
|
|
sh[i].s_vaddr += wp->w_base; /* RVA --> VA */
|
|
|
|
|
2001-11-27 20:59:49 +03:00
|
|
|
if ((s_flags & COFF_STYP_TEXT) != 0) {
|
2000-06-10 02:38:56 +04:00
|
|
|
/* set up command for text segment */
|
|
|
|
/* DPRINTF(("COFF text addr %lx size %ld offset %ld\n",
|
|
|
|
sh[i].s_vaddr, sh[i].s_size, sh[i].s_scnptr));
|
|
|
|
*/ pecoff_load_section(&epp->ep_vmcmds, epp->ep_vp,
|
|
|
|
&sh[i], &epp->ep_taddr,
|
2003-01-08 20:43:51 +03:00
|
|
|
&tsize, &prot);
|
2001-11-27 20:59:49 +03:00
|
|
|
} else if ((s_flags & COFF_STYP_BSS) != 0) {
|
2000-06-10 02:38:56 +04:00
|
|
|
/* set up command for bss segment */
|
2001-01-15 20:05:32 +03:00
|
|
|
baddr = sh[i].s_vaddr;
|
|
|
|
bsize = sh[i].s_paddr;
|
2000-06-10 02:38:56 +04:00
|
|
|
NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
|
|
|
|
bsize, baddr, NULLVP, 0,
|
|
|
|
VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
|
2001-01-15 20:05:32 +03:00
|
|
|
epp->ep_daddr = min(epp->ep_daddr, baddr);
|
|
|
|
bsize = baddr + bsize - epp->ep_daddr;
|
|
|
|
epp->ep_dsize = max(epp->ep_dsize, bsize);
|
2001-11-27 20:59:49 +03:00
|
|
|
} else if ((s_flags & (COFF_STYP_DATA|COFF_STYP_READ)) != 0) {
|
|
|
|
/* set up command for data segment */
|
|
|
|
/* DPRINTF(("COFF data addr %lx size %ld offset %ld\n",
|
|
|
|
sh[i].s_vaddr, sh[i].s_size, sh[i].s_scnptr));*/
|
|
|
|
pecoff_load_section(&epp->ep_vmcmds, epp->ep_vp,
|
|
|
|
&sh[i], &daddr, &dsize, &prot);
|
|
|
|
epp->ep_daddr = min(epp->ep_daddr, daddr);
|
|
|
|
dsize = daddr + dsize - epp->ep_daddr;
|
|
|
|
epp->ep_dsize = max(epp->ep_dsize, dsize);
|
2000-06-10 02:38:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* set up ep_emul_arg */
|
|
|
|
argp = malloc(sizeof(struct pecoff_args), M_TEMP, M_WAITOK);
|
|
|
|
epp->ep_emul_arg = argp;
|
|
|
|
argp->a_base = wp->w_base;
|
|
|
|
argp->a_entry = wp->w_base + ap->a_entry;
|
|
|
|
argp->a_end = epp->ep_daddr + epp->ep_dsize;
|
|
|
|
argp->a_subsystem = wp->w_subvers;
|
|
|
|
memcpy(argp->a_imghdr, wp->w_imghdr, sizeof(wp->w_imghdr));
|
|
|
|
/* imghdr RVA --> VA */
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
argp->a_imghdr[i].i_vaddr += wp->w_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load dynamic linker
|
|
|
|
*/
|
2003-06-30 02:28:00 +04:00
|
|
|
error = pecoff_load_file(p, epp, "/usr/libexec/ld.so.dll",
|
2000-06-10 02:38:56 +04:00
|
|
|
&epp->ep_vmcmds, &epp->ep_entry, argp);
|
|
|
|
if (error) {
|
|
|
|
free(sh, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
DPRINTF(("text addr: %lx size: %ld data addr: %lx size: %ld entry: %lx\n",
|
|
|
|
epp->ep_taddr, epp->ep_tsize,
|
|
|
|
epp->ep_daddr, epp->ep_dsize,
|
|
|
|
epp->ep_entry));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(sh, M_TEMP);
|
2003-08-08 22:57:01 +04:00
|
|
|
return (*epp->ep_esch->es_setup_stack)(p, epp);
|
2000-06-10 02:38:56 +04:00
|
|
|
}
|