Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / ibcs2 / ibcs2_exec_xout.c
blob4dbaeb266b075dfec27aaf9de08da47504ae2171
1 /* $NetBSD: ibcs2_exec_xout.c,v 1.17 2007/12/04 18:40:10 dsl Exp $ */
3 /*
4 * Copyright (c) 1994, 1995, 1998 Scott Bartram
5 * Copyright (c) 1994 Adam Glass
6 * Copyright (c) 1993, 1994 Christopher G. Demetriou
7 * All rights reserved.
9 * originally from kern/exec_ecoff.c
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Scott Bartram.
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ibcs2_exec_xout.c,v 1.17 2007/12/04 18:40:10 dsl Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/exec.h>
49 #include <sys/resourcevar.h>
51 #include <sys/mman.h>
52 #include <sys/syscallargs.h>
54 #include <sys/cpu.h>
55 #include <machine/reg.h>
56 #include <machine/ibcs2_machdep.h>
58 #include <compat/ibcs2/ibcs2_types.h>
59 #include <compat/ibcs2/ibcs2_exec.h>
60 #include <compat/ibcs2/ibcs2_errno.h>
61 #include <compat/ibcs2/ibcs2_util.h>
62 #include <compat/ibcs2/ibcs2_syscall.h>
64 int exec_ibcs2_xout_prep_nmagic(struct lwp *, struct exec_package *,
65 struct xexec *, struct xext *);
66 int exec_ibcs2_xout_prep_zmagic(struct lwp *, struct exec_package *,
67 struct xexec *, struct xext *);
69 int
70 exec_ibcs2_xout_makecmds(struct lwp *l, struct exec_package *epp)
72 int error;
73 struct xexec *xp = epp->ep_hdr;
74 struct xext *xep;
76 if (epp->ep_hdrvalid < XOUT_HDR_SIZE)
77 return ENOEXEC;
79 if ((xp->x_magic != XOUT_MAGIC) || (xp->x_cpu != XC_386))
80 return ENOEXEC;
81 if ((xp->x_renv & (XE_ABS | XE_VMOD)) || !(xp->x_renv & XE_EXEC))
82 return ENOEXEC;
84 xep = (void *)((char *)epp->ep_hdr + sizeof(struct xexec));
85 #ifdef notyet
86 if (xp->x_renv & XE_PURE)
87 error = exec_ibcs2_xout_prep_zmagic(l, epp, xp, xep);
88 else
89 #endif
90 error = exec_ibcs2_xout_prep_nmagic(l, epp, xp, xep);
92 if (error)
93 kill_vmcmds(&epp->ep_vmcmds);
95 return error;
99 * exec_ibcs2_xout_prep_nmagic(): Prepare a pure x.out binary's exec package
104 exec_ibcs2_xout_prep_nmagic(struct lwp *l, struct exec_package *epp, struct xexec *xp, struct xext *xep)
106 int error;
107 size_t nseg, i;
108 long baddr, bsize;
109 struct xseg *xs;
110 size_t resid;
111 size_t segsize = (size_t)xep->xe_segsize;
113 if (segsize > 16 * sizeof(*xs))
114 return ENOEXEC;
116 /* read in segment table */
117 xs = (struct xseg *)malloc(segsize, M_TEMP, M_WAITOK);
118 error = vn_rdwr(UIO_READ, epp->ep_vp, (void *)xs,
119 segsize, xep->xe_segpos,
120 UIO_SYSSPACE, IO_NODELOCKED, l->l_cred,
121 &resid, NULL);
122 if (error) {
123 DPRINTF(("segment table read error %d\n", error));
124 free(xs, M_TEMP);
125 return ENOEXEC;
128 for (nseg = segsize / sizeof(*xs), i = 0; i < nseg; i++) {
129 switch (xs[i].xs_type) {
130 case XS_TTEXT: /* text segment */
132 DPRINTF(("text addr %lx psize %ld vsize %ld off %ld\n",
133 xs[i].xs_rbase, xs[i].xs_psize,
134 xs[i].xs_vsize, xs[i].xs_filpos));
136 epp->ep_taddr = xs[i].xs_rbase; /* XXX - align ??? */
137 epp->ep_tsize = xs[i].xs_vsize;
139 DPRINTF(("VMCMD: addr %lx size %ld offset %ld\n",
140 epp->ep_taddr, epp->ep_tsize,
141 xs[i].xs_filpos));
142 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
143 epp->ep_tsize, epp->ep_taddr,
144 epp->ep_vp, xs[i].xs_filpos,
145 VM_PROT_READ|VM_PROT_EXECUTE);
146 break;
148 case XS_TDATA: /* data segment */
150 DPRINTF(("data addr %lx psize %ld vsize %ld off %ld\n",
151 xs[i].xs_rbase, xs[i].xs_psize,
152 xs[i].xs_vsize, xs[i].xs_filpos));
154 epp->ep_daddr = xs[i].xs_rbase; /* XXX - align ??? */
155 epp->ep_dsize = xs[i].xs_vsize;
157 DPRINTF(("VMCMD: addr %lx size %ld offset %ld\n",
158 epp->ep_daddr, xs[i].xs_psize,
159 xs[i].xs_filpos));
160 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
161 xs[i].xs_psize, epp->ep_daddr,
162 epp->ep_vp, xs[i].xs_filpos,
163 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
165 /* set up command for bss segment */
166 baddr = round_page(epp->ep_daddr + xs[i].xs_psize);
167 bsize = epp->ep_daddr + epp->ep_dsize - baddr;
168 if (bsize > 0) {
169 DPRINTF(("VMCMD: bss addr %lx size %ld off %d\n",
170 baddr, bsize, 0));
171 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
172 bsize, baddr, NULLVP, 0,
173 VM_PROT_READ|VM_PROT_WRITE|
174 VM_PROT_EXECUTE);
176 break;
178 default:
179 break;
183 /* set up entry point */
184 epp->ep_entry = xp->x_entry;
186 DPRINTF(("text addr: %lx size: %ld data addr: %lx size: %ld entry: %lx\n",
187 epp->ep_taddr, epp->ep_tsize,
188 epp->ep_daddr, epp->ep_dsize,
189 epp->ep_entry));
191 free(xs, M_TEMP);
192 return (*epp->ep_esch->es_setup_stack)(l, epp);