exec_ecoff.c revision 1.34 1 /* $NetBSD: exec_ecoff.c,v 1.34 2024/12/06 16:19:41 riastradh 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.34 2024/12/06 16:19:41 riastradh Exp $");
37
38 #include <sys/param.h>
39 #include <sys/types.h>
40
41 #include <sys/exec.h>
42 #include <sys/exec.h>
43 #include <sys/exec_ecoff.h>
44 #include <sys/module.h>
45 #include <sys/proc.h>
46 #include <sys/resourcevar.h>
47 #include <sys/sdt.h>
48 #include <sys/systm.h>
49 #include <sys/vnode.h>
50
51 MODULE(MODULE_CLASS_EXEC, exec_ecoff, NULL);
52
53 static struct execsw exec_ecoff_execsw = {
54 .es_hdrsz = ECOFF_HDR_SIZE,
55 .es_makecmds = exec_ecoff_makecmds,
56 .u = {
57 .ecoff_probe_func = cpu_exec_ecoff_probe,
58 },
59 .es_emul = &emul_netbsd,
60 .es_prio = EXECSW_PRIO_ANY,
61 .es_arglen = 0,
62 .es_copyargs = copyargs,
63 .es_setregs = cpu_exec_ecoff_setregs,
64 .es_coredump = coredump_netbsd,
65 .es_setup_stack = exec_setup_stack,
66 };
67
68 static int
69 exec_ecoff_modcmd(modcmd_t cmd, void *arg)
70 {
71 switch (cmd) {
72 case MODULE_CMD_INIT:
73 return exec_add(&exec_ecoff_execsw, 1);
74
75 case MODULE_CMD_FINI:
76 return exec_remove(&exec_ecoff_execsw, 1);
77
78 default:
79 return SET_ERROR(ENOTTY);
80 }
81 }
82
83 /*
84 * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
85 *
86 * Given a proc pointer and an exec package pointer, see if the referent
87 * of the epp is in ecoff format. Check 'standard' magic numbers for
88 * this architecture. If that fails, return failure.
89 *
90 * This function is responsible for creating a set of vmcmds which can be
91 * used to build the process's vm space and inserting them into the exec
92 * package.
93 */
94 int
95 exec_ecoff_makecmds(struct lwp *l, struct exec_package *epp)
96 {
97 int error;
98 struct ecoff_exechdr *execp = epp->ep_hdr;
99
100 if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
101 return SET_ERROR(ENOEXEC);
102
103 if (ECOFF_BADMAG(execp))
104 return SET_ERROR(ENOEXEC);
105
106 error = (*epp->ep_esch->u.ecoff_probe_func)(l, epp);
107
108 /*
109 * if there was an error or there are already vmcmds set up,
110 * we return. (the latter can happen if cpu_exec_ecoff_hook()
111 * recursively invokes check_exec() to handle loading of a
112 * dynamically linked binary's shared loader.
113 */
114 if (error || epp->ep_vmcmds.evs_cnt)
115 return (error);
116
117 /*
118 * prepare the exec package to map the executable.
119 */
120 switch (execp->a.magic) {
121 case ECOFF_OMAGIC:
122 error = exec_ecoff_prep_omagic(l, epp, epp->ep_hdr,
123 epp->ep_vp);
124 break;
125 case ECOFF_NMAGIC:
126 error = exec_ecoff_prep_nmagic(l, epp, epp->ep_hdr,
127 epp->ep_vp);
128 break;
129 case ECOFF_ZMAGIC:
130 error = exec_ecoff_prep_zmagic(l, epp, epp->ep_hdr,
131 epp->ep_vp);
132 break;
133 default:
134 return SET_ERROR(ENOEXEC);
135 }
136
137 /* set up the stack */
138 if (!error)
139 error = (*epp->ep_esch->es_setup_stack)(l, epp);
140
141 if (error)
142 kill_vmcmds(&epp->ep_vmcmds);
143
144 return error;
145 }
146
147 /*
148 * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
149 */
150 int
151 exec_ecoff_prep_omagic(struct lwp *l, struct exec_package *epp,
152 struct ecoff_exechdr *execp, struct vnode *vp)
153 {
154 struct ecoff_aouthdr *eap = &execp->a;
155
156 epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
157 epp->ep_tsize = eap->tsize;
158 epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
159 epp->ep_dsize = eap->dsize + eap->bsize;
160 epp->ep_entry = eap->entry;
161
162 /* set up command for text and data segments */
163 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
164 eap->tsize + eap->dsize, epp->ep_taddr, vp,
165 ECOFF_TXTOFF(execp),
166 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
167
168 /* set up command for bss segment */
169 if (eap->bsize > 0)
170 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
171 ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
172 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
173
174 return 0;
175 }
176
177 /*
178 * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
179 * package.
180 */
181 int
182 exec_ecoff_prep_nmagic(struct lwp *l, struct exec_package *epp,
183 struct ecoff_exechdr *execp, struct vnode *vp)
184 {
185 struct ecoff_aouthdr *eap = &execp->a;
186
187 epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
188 epp->ep_tsize = eap->tsize;
189 epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
190 epp->ep_dsize = eap->dsize + eap->bsize;
191 epp->ep_entry = eap->entry;
192
193 /* set up command for text segment */
194 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
195 epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
196 VM_PROT_READ|VM_PROT_EXECUTE);
197
198 /* set up command for data segment */
199 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
200 epp->ep_daddr, vp, ECOFF_DATOFF(execp),
201 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
202
203 /* set up command for bss segment */
204 if (eap->bsize > 0)
205 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
206 ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
207 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
208
209 return 0;
210 }
211
212 /*
213 * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
214 *
215 * First, set the various offsets/lengths in the exec package.
216 *
217 * Then, mark the text image busy (so it can be demand paged) or error
218 * out if this is not possible. Finally, set up vmcmds for the
219 * text, data, bss, and stack segments.
220 */
221 int
222 exec_ecoff_prep_zmagic(struct lwp *l, struct exec_package *epp,
223 struct ecoff_exechdr *execp, struct vnode *vp)
224 {
225 struct ecoff_aouthdr *eap = &execp->a;
226 int error;
227
228 epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
229 epp->ep_tsize = eap->tsize;
230 epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
231 epp->ep_dsize = eap->dsize + eap->bsize;
232 epp->ep_entry = eap->entry;
233
234 error = vn_marktext(vp);
235 if (error)
236 return (error);
237
238 /* set up command for text segment */
239 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
240 epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
241 VM_PROT_READ|VM_PROT_EXECUTE);
242
243 /* set up command for data segment */
244 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
245 epp->ep_daddr, vp, ECOFF_DATOFF(execp),
246 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
247
248 /* set up command for bss segment */
249 if (eap->bsize > 0)
250 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
251 ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
252 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
253
254 return 0;
255 }
256