exec_ecoff.c revision 1.22.2.6 1 /* $NetBSD: exec_ecoff.c,v 1.22.2.6 2005/02/04 11:47:42 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1994 Adam Glass
5 * Copyright (c) 1993, 1994, 1996, 1999 Christopher G. Demetriou
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christopher G. Demetriou
19 * for the NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: exec_ecoff.c,v 1.22.2.6 2005/02/04 11:47:42 skrll Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/malloc.h>
42 #include <sys/vnode.h>
43 #include <sys/exec.h>
44 #include <sys/resourcevar.h>
45
46 #include <sys/exec_ecoff.h>
47
48 /*
49 * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
50 *
51 * Given a proc pointer and an exec package pointer, see if the referent
52 * of the epp is in ecoff format. Check 'standard' magic numbers for
53 * this architecture. If that fails, return failure.
54 *
55 * This function is responsible for creating a set of vmcmds which can be
56 * used to build the process's vm space and inserting them into the exec
57 * package.
58 */
59 int
60 exec_ecoff_makecmds(struct lwp *l, struct exec_package *epp)
61 {
62 int error;
63 struct ecoff_exechdr *execp = epp->ep_hdr;
64 struct proc *p = l->l_proc;
65
66 if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
67 return ENOEXEC;
68
69 if (ECOFF_BADMAG(execp))
70 return ENOEXEC;
71
72 error = (*epp->ep_esch->u.ecoff_probe_func)(l, epp);
73
74 /*
75 * if there was an error or there are already vmcmds set up,
76 * we return. (the latter can happen if cpu_exec_ecoff_hook()
77 * recursively invokes check_exec() to handle loading of a
78 * dynamically linked binary's shared loader.
79 */
80 if (error || epp->ep_vmcmds.evs_cnt)
81 return (error);
82
83 /*
84 * prepare the exec package to map the executable.
85 */
86 switch (execp->a.magic) {
87 case ECOFF_OMAGIC:
88 error = exec_ecoff_prep_omagic(p, epp, epp->ep_hdr,
89 epp->ep_vp);
90 break;
91 case ECOFF_NMAGIC:
92 error = exec_ecoff_prep_nmagic(p, epp, epp->ep_hdr,
93 epp->ep_vp);
94 break;
95 case ECOFF_ZMAGIC:
96 error = exec_ecoff_prep_zmagic(p, epp, epp->ep_hdr,
97 epp->ep_vp);
98 break;
99 default:
100 return ENOEXEC;
101 }
102
103 /* set up the stack */
104 if (!error)
105 error = (*epp->ep_esch->es_setup_stack)(p, epp);
106
107 if (error)
108 kill_vmcmds(&epp->ep_vmcmds);
109
110 return error;
111 }
112
113 /*
114 * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
115 */
116 int
117 exec_ecoff_prep_omagic(struct proc *p, struct exec_package *epp,
118 struct ecoff_exechdr *execp, struct vnode *vp)
119 {
120 struct ecoff_aouthdr *eap = &execp->a;
121
122 epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
123 epp->ep_tsize = eap->tsize;
124 epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
125 epp->ep_dsize = eap->dsize + eap->bsize;
126 epp->ep_entry = eap->entry;
127
128 /* set up command for text and data segments */
129 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
130 eap->tsize + eap->dsize, epp->ep_taddr, vp,
131 ECOFF_TXTOFF(execp),
132 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
133
134 /* set up command for bss segment */
135 if (eap->bsize > 0)
136 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
137 ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
138 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
139
140 return 0;
141 }
142
143 /*
144 * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
145 * package.
146 */
147 int
148 exec_ecoff_prep_nmagic(struct proc *p, struct exec_package *epp,
149 struct ecoff_exechdr *execp, struct vnode *vp)
150 {
151 struct ecoff_aouthdr *eap = &execp->a;
152
153 epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
154 epp->ep_tsize = eap->tsize;
155 epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
156 epp->ep_dsize = eap->dsize + eap->bsize;
157 epp->ep_entry = eap->entry;
158
159 /* set up command for text segment */
160 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
161 epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
162 VM_PROT_READ|VM_PROT_EXECUTE);
163
164 /* set up command for data segment */
165 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
166 epp->ep_daddr, vp, ECOFF_DATOFF(execp),
167 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
168
169 /* set up command for bss segment */
170 if (eap->bsize > 0)
171 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
172 ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
173 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
174
175 return 0;
176 }
177
178 /*
179 * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
180 *
181 * First, set the various offsets/lengths in the exec package.
182 *
183 * Then, mark the text image busy (so it can be demand paged) or error
184 * out if this is not possible. Finally, set up vmcmds for the
185 * text, data, bss, and stack segments.
186 */
187 int
188 exec_ecoff_prep_zmagic(struct proc *p, struct exec_package *epp,
189 struct ecoff_exechdr *execp, struct vnode *vp)
190 {
191 struct ecoff_aouthdr *eap = &execp->a;
192 int error;
193
194 epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
195 epp->ep_tsize = eap->tsize;
196 epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
197 epp->ep_dsize = eap->dsize + eap->bsize;
198 epp->ep_entry = eap->entry;
199
200 error = vn_marktext(vp);
201 if (error)
202 return (error);
203
204 /* set up command for text segment */
205 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
206 epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
207 VM_PROT_READ|VM_PROT_EXECUTE);
208
209 /* set up command for data segment */
210 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
211 epp->ep_daddr, vp, ECOFF_DATOFF(execp),
212 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
213
214 /* set up command for bss segment */
215 if (eap->bsize > 0)
216 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
217 ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
218 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
219
220 return 0;
221 }
222