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