Deleted Added
full compact
imgact_coff.c (50477) imgact_coff.c (52635)
1/*-
2 * Copyright (c) 1994 Sean Eric Fagan
3 * Copyright (c) 1994 S�ren Schmidt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
1/*-
2 * Copyright (c) 1994 Sean Eric Fagan
3 * Copyright (c) 1994 S�ren Schmidt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/i386/ibcs2/imgact_coff.c 50477 1999-08-28 01:08:13Z peter $
29 * $FreeBSD: head/sys/i386/ibcs2/imgact_coff.c 52635 1999-10-29 18:09:36Z phk $
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/mman.h>
35#include <sys/imgact.h>
36#include <sys/kernel.h>
37#include <sys/fcntl.h>
38#include <sys/malloc.h>
39#include <sys/mount.h>
40#include <sys/namei.h>
41#include <sys/vnode.h>
42
43#include <vm/vm.h>
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/mman.h>
35#include <sys/imgact.h>
36#include <sys/kernel.h>
37#include <sys/fcntl.h>
38#include <sys/malloc.h>
39#include <sys/mount.h>
40#include <sys/namei.h>
41#include <sys/vnode.h>
42
43#include <vm/vm.h>
44#include <vm/vm_prot.h>
45#include <vm/pmap.h>
46#include <vm/vm_map.h>
47#include <vm/vm_kern.h>
48#include <vm/vm_extern.h>
49#include <vm/vm_zone.h>
50
51#include <i386/ibcs2/coff.h>
52#include <i386/ibcs2/ibcs2_util.h>
53
54extern struct sysentvec ibcs2_svr3_sysvec;
55
56static int coff_load_file __P((struct proc *p, char *name));
57static int exec_coff_imgact __P((struct image_params *imgp));
58
59static int load_coff_section __P((struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot));
60
61static int
62load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
63 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
64{
65 size_t map_len;
66 vm_offset_t map_offset;
67 vm_offset_t map_addr;
68 int error;
69 unsigned char *data_buf = 0;
70 size_t copy_len;
71
72 map_offset = trunc_page(offset);
73 map_addr = trunc_page((vm_offset_t)vmaddr);
74
75 if (memsz > filsz) {
76 /*
77 * We have the stupid situation that
78 * the section is longer than it is on file,
79 * which means it has zero-filled areas, and
80 * we have to work for it. Stupid iBCS!
81 */
82 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
83 } else {
84 /*
85 * The only stuff we care about is on disk, and we
86 * don't care if we map in more than is really there.
87 */
88 map_len = round_page(offset + filsz) - trunc_page(map_offset);
89 }
90
91 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
92 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n",
93 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
94
95 if ((error = vm_mmap(&vmspace->vm_map,
96 &map_addr,
97 map_len,
98 prot,
99 VM_PROT_ALL,
100 MAP_PRIVATE | MAP_FIXED,
101 (caddr_t) vp,
102 map_offset)) != 0)
103 return error;
104
105 if (memsz == filsz) {
106 /* We're done! */
107 return 0;
108 }
109
110 /*
111 * Now we have screwball stuff, to accomodate stupid COFF.
112 * We have to map the remaining bit of the file into the kernel's
113 * memory map, allocate some anonymous memory, copy that last
114 * bit into it, and then we're done. *sigh*
115 * For clean-up reasons, we actally map in the file last.
116 */
117
118 copy_len = (offset + filsz) - trunc_page(offset + filsz);
119 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
120 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
121
122 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len));
123
124 if (map_len != 0) {
125 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
126 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
127 if (error)
128 return error;
129 }
130
131 if ((error = vm_mmap(kernel_map,
132 (vm_offset_t *) &data_buf,
133 PAGE_SIZE,
134 VM_PROT_READ,
135 VM_PROT_READ,
136 0,
137 (caddr_t) vp,
138 trunc_page(offset + filsz))) != 0)
139 return error;
140
141 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
142
143 if (vm_map_remove(kernel_map,
144 (vm_offset_t) data_buf,
145 (vm_offset_t) data_buf + PAGE_SIZE))
146 panic("load_coff_section vm_map_remove failed");
147
148 return error;
149}
150
151static int
152coff_load_file(struct proc *p, char *name)
153{
154 struct vmspace *vmspace = p->p_vmspace;
155 int error;
156 struct nameidata nd;
157 struct vnode *vp;
158 struct vattr attr;
159 struct filehdr *fhdr;
160 struct aouthdr *ahdr;
161 struct scnhdr *scns;
162 char *ptr = 0;
163 int nscns;
164 unsigned long text_offset = 0, text_address = 0, text_size = 0;
165 unsigned long data_offset = 0, data_address = 0, data_size = 0;
166 unsigned long bss_size = 0;
167 int i;
168
169 /* XXX use of 'curproc' should be 'p'?*/
170 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, curproc);
171
172 error = namei(&nd);
173 if (error)
174 return error;
175
176 vp = nd.ni_vp;
177 if (vp == NULL)
178 return ENOEXEC;
179
180 if (vp->v_writecount) {
181 error = ETXTBSY;
182 goto fail;
183 }
184
185 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
186 goto fail;
187
188 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
189 || ((attr.va_mode & 0111) == 0)
190 || (attr.va_type != VREG))
191 goto fail;
192
193 if (attr.va_size == 0) {
194 error = ENOEXEC;
195 goto fail;
196 }
197
198 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
199 goto fail;
200
201 if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
202 goto fail;
203
204 /*
205 * Lose the lock on the vnode. It's no longer needed, and must not
206 * exist for the pagefault paging to work below.
207 */
208 VOP_UNLOCK(vp, 0, p);
209
210 if ((error = vm_mmap(kernel_map,
211 (vm_offset_t *) &ptr,
212 PAGE_SIZE,
213 VM_PROT_READ,
214 VM_PROT_READ,
215 0,
216 (caddr_t) vp,
217 0)) != 0)
218 goto fail;
219
220 fhdr = (struct filehdr *)ptr;
221
222 if (fhdr->f_magic != I386_COFF) {
223 error = ENOEXEC;
224 goto dealloc_and_fail;
225 }
226
227 nscns = fhdr->f_nscns;
228
229 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
230 /*
231 * XXX -- just fail. I'm so lazy.
232 */
233 error = ENOEXEC;
234 goto dealloc_and_fail;
235 }
236
237 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
238
239 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
240 + sizeof(struct aouthdr));
241
242 for (i = 0; i < nscns; i++) {
243 if (scns[i].s_flags & STYP_NOLOAD)
244 continue;
245 else if (scns[i].s_flags & STYP_TEXT) {
246 text_address = scns[i].s_vaddr;
247 text_size = scns[i].s_size;
248 text_offset = scns[i].s_scnptr;
249 }
250 else if (scns[i].s_flags & STYP_DATA) {
251 data_address = scns[i].s_vaddr;
252 data_size = scns[i].s_size;
253 data_offset = scns[i].s_scnptr;
254 } else if (scns[i].s_flags & STYP_BSS) {
255 bss_size = scns[i].s_size;
256 }
257 }
258
259 if ((error = load_coff_section(vmspace, vp, text_offset,
260 (caddr_t)(void *)(uintptr_t)text_address,
261 text_size, text_size,
262 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
263 goto dealloc_and_fail;
264 }
265 if ((error = load_coff_section(vmspace, vp, data_offset,
266 (caddr_t)(void *)(uintptr_t)data_address,
267 data_size + bss_size, data_size,
268 VM_PROT_ALL)) != 0) {
269 goto dealloc_and_fail;
270 }
271
272 error = 0;
273
274 dealloc_and_fail:
275 if (vm_map_remove(kernel_map,
276 (vm_offset_t) ptr,
277 (vm_offset_t) ptr + PAGE_SIZE))
278 panic(__FUNCTION__ " vm_map_remove failed");
279
280 fail:
281 vput(nd.ni_vp);
282 zfree(namei_zone, nd.ni_cnd.cn_pnbuf);
283 return error;
284}
285
286static int
287exec_coff_imgact(imgp)
288 struct image_params *imgp;
289{
290 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
291 const struct aouthdr *ahdr;
292 const struct scnhdr *scns;
293 int i;
294 struct vmspace *vmspace;
295 int nscns;
296 int error;
297 unsigned long text_offset = 0, text_address = 0, text_size = 0;
298 unsigned long data_offset = 0, data_address = 0, data_size = 0;
299 unsigned long bss_size = 0;
300 caddr_t hole;
301
302 if (fhdr->f_magic != I386_COFF ||
303 !(fhdr->f_flags & F_EXEC)) {
304
305 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
306 return -1;
307 }
308
309 nscns = fhdr->f_nscns;
310 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
311 /*
312 * For now, return an error -- need to be able to
313 * read in all of the section structures.
314 */
315
316 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
317 return -1;
318 }
319
320 ahdr = (const struct aouthdr*)
321 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
322 imgp->entry_addr = ahdr->entry;
323
324 scns = (const struct scnhdr*)
325 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
326 sizeof(struct aouthdr));
327
328 if ((error = exec_extract_strings(imgp)) != 0) {
329 DPRINTF(("%s(%d): return %d\n", __FILE__, __LINE__, error));
330 return error;
331 }
332
333 exec_new_vmspace(imgp);
334 vmspace = imgp->proc->p_vmspace;
335
336 for (i = 0; i < nscns; i++) {
337
338 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
339 "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
340 scns[i].s_vaddr, scns[i].s_scnptr));
341 if (scns[i].s_flags & STYP_NOLOAD) {
342 /*
343 * A section that is not loaded, for whatever
344 * reason. It takes precedance over other flag
345 * bits...
346 */
347 continue;
348 } else if (scns[i].s_flags & STYP_TEXT) {
349 text_address = scns[i].s_vaddr;
350 text_size = scns[i].s_size;
351 text_offset = scns[i].s_scnptr;
352 } else if (scns[i].s_flags & STYP_DATA) {
353 /* .data section */
354 data_address = scns[i].s_vaddr;
355 data_size = scns[i].s_size;
356 data_offset = scns[i].s_scnptr;
357 } else if (scns[i].s_flags & STYP_BSS) {
358 /* .bss section */
359 bss_size = scns[i].s_size;
360 } else if (scns[i].s_flags & STYP_LIB) {
361 char *buf = 0;
362 int foff = trunc_page(scns[i].s_scnptr);
363 int off = scns[i].s_scnptr - foff;
364 int len = round_page(scns[i].s_size + PAGE_SIZE);
365 int j;
366
367 if ((error = vm_mmap(kernel_map,
368 (vm_offset_t *) &buf,
369 len,
370 VM_PROT_READ,
371 VM_PROT_READ,
372 0,
373 (caddr_t) imgp->vp,
374 foff)) != 0) {
375 return ENOEXEC;
376 }
377 if(scns[i].s_size) {
378 char *libbuf;
379 int emul_path_len = strlen(ibcs2_emul_path);
380
381 libbuf = malloc(MAXPATHLEN + emul_path_len,
382 M_TEMP, M_WAITOK);
383 strcpy(libbuf, ibcs2_emul_path);
384
385 for (j = off; j < scns[i].s_size + off; j++) {
386 char *libname;
387
388 libname = buf + j + 4 * *(long*)(buf + j + 4);
389 j += 4* *(long*)(buf + j);
390
391 DPRINTF(("%s(%d): shared library %s\n",
392 __FILE__, __LINE__, libname));
393 strcpy(&libbuf[emul_path_len], libname);
394 error = coff_load_file(imgp->proc, libbuf);
395 if (error)
396 error = coff_load_file(imgp->proc,
397 libname);
398 if (error)
399 break;
400 }
401 free(libbuf, M_TEMP);
402 }
403 if (vm_map_remove(kernel_map,
404 (vm_offset_t) buf,
405 (vm_offset_t) buf + len))
406 panic("exec_coff_imgact vm_map_remove failed");
407 if (error)
408 return error;
409 }
410 }
411 /*
412 * Map in .text now
413 */
414
415 DPRINTF(("%s(%d): load_coff_section(vmspace, "
416 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
417 __FILE__, __LINE__, text_offset, text_address,
418 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
419 if ((error = load_coff_section(vmspace, imgp->vp,
420 text_offset,
421 (caddr_t)(void *)(uintptr_t)text_address,
422 text_size, text_size,
423 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
424 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
425 return error;
426 }
427 /*
428 * Map in .data and .bss now
429 */
430
431
432 DPRINTF(("%s(%d): load_coff_section(vmspace, "
433 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
434 __FILE__, __LINE__, data_offset, data_address,
435 data_size + bss_size, data_size, VM_PROT_ALL));
436 if ((error = load_coff_section(vmspace, imgp->vp,
437 data_offset,
438 (caddr_t)(void *)(uintptr_t)data_address,
439 data_size + bss_size, data_size,
440 VM_PROT_ALL)) != 0) {
441
442 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
443 return error;
444 }
445
446 imgp->interpreted = 0;
447 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
448
449 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
450 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
451 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
452 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
453
454 hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
455
456
457 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
458 __FILE__, __LINE__, hole));
459 DPRINTF(("imgact: error = %d\n", error));
460
461 error = vm_map_find(&vmspace->vm_map, NULL, 0,
462 (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
463 VM_PROT_ALL, VM_PROT_ALL, 0);
464
465 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
466 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
467 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
468 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__));
469
470 /* Indicate that this file should not be modified */
471 imgp->vp->v_flag |= VTEXT;
472 return 0;
473}
474
475/*
476 * Tell kern_execve.c about it, with a little help from the linker.
477 */
478static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
479EXEC_SET(coff, coff_execsw);
44#include <vm/pmap.h>
45#include <vm/vm_map.h>
46#include <vm/vm_kern.h>
47#include <vm/vm_extern.h>
48#include <vm/vm_zone.h>
49
50#include <i386/ibcs2/coff.h>
51#include <i386/ibcs2/ibcs2_util.h>
52
53extern struct sysentvec ibcs2_svr3_sysvec;
54
55static int coff_load_file __P((struct proc *p, char *name));
56static int exec_coff_imgact __P((struct image_params *imgp));
57
58static int load_coff_section __P((struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot));
59
60static int
61load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
62 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
63{
64 size_t map_len;
65 vm_offset_t map_offset;
66 vm_offset_t map_addr;
67 int error;
68 unsigned char *data_buf = 0;
69 size_t copy_len;
70
71 map_offset = trunc_page(offset);
72 map_addr = trunc_page((vm_offset_t)vmaddr);
73
74 if (memsz > filsz) {
75 /*
76 * We have the stupid situation that
77 * the section is longer than it is on file,
78 * which means it has zero-filled areas, and
79 * we have to work for it. Stupid iBCS!
80 */
81 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
82 } else {
83 /*
84 * The only stuff we care about is on disk, and we
85 * don't care if we map in more than is really there.
86 */
87 map_len = round_page(offset + filsz) - trunc_page(map_offset);
88 }
89
90 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
91 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, vp, 0x%x)\n",
92 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
93
94 if ((error = vm_mmap(&vmspace->vm_map,
95 &map_addr,
96 map_len,
97 prot,
98 VM_PROT_ALL,
99 MAP_PRIVATE | MAP_FIXED,
100 (caddr_t) vp,
101 map_offset)) != 0)
102 return error;
103
104 if (memsz == filsz) {
105 /* We're done! */
106 return 0;
107 }
108
109 /*
110 * Now we have screwball stuff, to accomodate stupid COFF.
111 * We have to map the remaining bit of the file into the kernel's
112 * memory map, allocate some anonymous memory, copy that last
113 * bit into it, and then we're done. *sigh*
114 * For clean-up reasons, we actally map in the file last.
115 */
116
117 copy_len = (offset + filsz) - trunc_page(offset + filsz);
118 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
119 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
120
121 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len));
122
123 if (map_len != 0) {
124 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
125 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
126 if (error)
127 return error;
128 }
129
130 if ((error = vm_mmap(kernel_map,
131 (vm_offset_t *) &data_buf,
132 PAGE_SIZE,
133 VM_PROT_READ,
134 VM_PROT_READ,
135 0,
136 (caddr_t) vp,
137 trunc_page(offset + filsz))) != 0)
138 return error;
139
140 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
141
142 if (vm_map_remove(kernel_map,
143 (vm_offset_t) data_buf,
144 (vm_offset_t) data_buf + PAGE_SIZE))
145 panic("load_coff_section vm_map_remove failed");
146
147 return error;
148}
149
150static int
151coff_load_file(struct proc *p, char *name)
152{
153 struct vmspace *vmspace = p->p_vmspace;
154 int error;
155 struct nameidata nd;
156 struct vnode *vp;
157 struct vattr attr;
158 struct filehdr *fhdr;
159 struct aouthdr *ahdr;
160 struct scnhdr *scns;
161 char *ptr = 0;
162 int nscns;
163 unsigned long text_offset = 0, text_address = 0, text_size = 0;
164 unsigned long data_offset = 0, data_address = 0, data_size = 0;
165 unsigned long bss_size = 0;
166 int i;
167
168 /* XXX use of 'curproc' should be 'p'?*/
169 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, curproc);
170
171 error = namei(&nd);
172 if (error)
173 return error;
174
175 vp = nd.ni_vp;
176 if (vp == NULL)
177 return ENOEXEC;
178
179 if (vp->v_writecount) {
180 error = ETXTBSY;
181 goto fail;
182 }
183
184 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
185 goto fail;
186
187 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
188 || ((attr.va_mode & 0111) == 0)
189 || (attr.va_type != VREG))
190 goto fail;
191
192 if (attr.va_size == 0) {
193 error = ENOEXEC;
194 goto fail;
195 }
196
197 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
198 goto fail;
199
200 if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
201 goto fail;
202
203 /*
204 * Lose the lock on the vnode. It's no longer needed, and must not
205 * exist for the pagefault paging to work below.
206 */
207 VOP_UNLOCK(vp, 0, p);
208
209 if ((error = vm_mmap(kernel_map,
210 (vm_offset_t *) &ptr,
211 PAGE_SIZE,
212 VM_PROT_READ,
213 VM_PROT_READ,
214 0,
215 (caddr_t) vp,
216 0)) != 0)
217 goto fail;
218
219 fhdr = (struct filehdr *)ptr;
220
221 if (fhdr->f_magic != I386_COFF) {
222 error = ENOEXEC;
223 goto dealloc_and_fail;
224 }
225
226 nscns = fhdr->f_nscns;
227
228 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
229 /*
230 * XXX -- just fail. I'm so lazy.
231 */
232 error = ENOEXEC;
233 goto dealloc_and_fail;
234 }
235
236 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
237
238 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
239 + sizeof(struct aouthdr));
240
241 for (i = 0; i < nscns; i++) {
242 if (scns[i].s_flags & STYP_NOLOAD)
243 continue;
244 else if (scns[i].s_flags & STYP_TEXT) {
245 text_address = scns[i].s_vaddr;
246 text_size = scns[i].s_size;
247 text_offset = scns[i].s_scnptr;
248 }
249 else if (scns[i].s_flags & STYP_DATA) {
250 data_address = scns[i].s_vaddr;
251 data_size = scns[i].s_size;
252 data_offset = scns[i].s_scnptr;
253 } else if (scns[i].s_flags & STYP_BSS) {
254 bss_size = scns[i].s_size;
255 }
256 }
257
258 if ((error = load_coff_section(vmspace, vp, text_offset,
259 (caddr_t)(void *)(uintptr_t)text_address,
260 text_size, text_size,
261 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
262 goto dealloc_and_fail;
263 }
264 if ((error = load_coff_section(vmspace, vp, data_offset,
265 (caddr_t)(void *)(uintptr_t)data_address,
266 data_size + bss_size, data_size,
267 VM_PROT_ALL)) != 0) {
268 goto dealloc_and_fail;
269 }
270
271 error = 0;
272
273 dealloc_and_fail:
274 if (vm_map_remove(kernel_map,
275 (vm_offset_t) ptr,
276 (vm_offset_t) ptr + PAGE_SIZE))
277 panic(__FUNCTION__ " vm_map_remove failed");
278
279 fail:
280 vput(nd.ni_vp);
281 zfree(namei_zone, nd.ni_cnd.cn_pnbuf);
282 return error;
283}
284
285static int
286exec_coff_imgact(imgp)
287 struct image_params *imgp;
288{
289 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
290 const struct aouthdr *ahdr;
291 const struct scnhdr *scns;
292 int i;
293 struct vmspace *vmspace;
294 int nscns;
295 int error;
296 unsigned long text_offset = 0, text_address = 0, text_size = 0;
297 unsigned long data_offset = 0, data_address = 0, data_size = 0;
298 unsigned long bss_size = 0;
299 caddr_t hole;
300
301 if (fhdr->f_magic != I386_COFF ||
302 !(fhdr->f_flags & F_EXEC)) {
303
304 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
305 return -1;
306 }
307
308 nscns = fhdr->f_nscns;
309 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
310 /*
311 * For now, return an error -- need to be able to
312 * read in all of the section structures.
313 */
314
315 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
316 return -1;
317 }
318
319 ahdr = (const struct aouthdr*)
320 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
321 imgp->entry_addr = ahdr->entry;
322
323 scns = (const struct scnhdr*)
324 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
325 sizeof(struct aouthdr));
326
327 if ((error = exec_extract_strings(imgp)) != 0) {
328 DPRINTF(("%s(%d): return %d\n", __FILE__, __LINE__, error));
329 return error;
330 }
331
332 exec_new_vmspace(imgp);
333 vmspace = imgp->proc->p_vmspace;
334
335 for (i = 0; i < nscns; i++) {
336
337 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
338 "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
339 scns[i].s_vaddr, scns[i].s_scnptr));
340 if (scns[i].s_flags & STYP_NOLOAD) {
341 /*
342 * A section that is not loaded, for whatever
343 * reason. It takes precedance over other flag
344 * bits...
345 */
346 continue;
347 } else if (scns[i].s_flags & STYP_TEXT) {
348 text_address = scns[i].s_vaddr;
349 text_size = scns[i].s_size;
350 text_offset = scns[i].s_scnptr;
351 } else if (scns[i].s_flags & STYP_DATA) {
352 /* .data section */
353 data_address = scns[i].s_vaddr;
354 data_size = scns[i].s_size;
355 data_offset = scns[i].s_scnptr;
356 } else if (scns[i].s_flags & STYP_BSS) {
357 /* .bss section */
358 bss_size = scns[i].s_size;
359 } else if (scns[i].s_flags & STYP_LIB) {
360 char *buf = 0;
361 int foff = trunc_page(scns[i].s_scnptr);
362 int off = scns[i].s_scnptr - foff;
363 int len = round_page(scns[i].s_size + PAGE_SIZE);
364 int j;
365
366 if ((error = vm_mmap(kernel_map,
367 (vm_offset_t *) &buf,
368 len,
369 VM_PROT_READ,
370 VM_PROT_READ,
371 0,
372 (caddr_t) imgp->vp,
373 foff)) != 0) {
374 return ENOEXEC;
375 }
376 if(scns[i].s_size) {
377 char *libbuf;
378 int emul_path_len = strlen(ibcs2_emul_path);
379
380 libbuf = malloc(MAXPATHLEN + emul_path_len,
381 M_TEMP, M_WAITOK);
382 strcpy(libbuf, ibcs2_emul_path);
383
384 for (j = off; j < scns[i].s_size + off; j++) {
385 char *libname;
386
387 libname = buf + j + 4 * *(long*)(buf + j + 4);
388 j += 4* *(long*)(buf + j);
389
390 DPRINTF(("%s(%d): shared library %s\n",
391 __FILE__, __LINE__, libname));
392 strcpy(&libbuf[emul_path_len], libname);
393 error = coff_load_file(imgp->proc, libbuf);
394 if (error)
395 error = coff_load_file(imgp->proc,
396 libname);
397 if (error)
398 break;
399 }
400 free(libbuf, M_TEMP);
401 }
402 if (vm_map_remove(kernel_map,
403 (vm_offset_t) buf,
404 (vm_offset_t) buf + len))
405 panic("exec_coff_imgact vm_map_remove failed");
406 if (error)
407 return error;
408 }
409 }
410 /*
411 * Map in .text now
412 */
413
414 DPRINTF(("%s(%d): load_coff_section(vmspace, "
415 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
416 __FILE__, __LINE__, text_offset, text_address,
417 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
418 if ((error = load_coff_section(vmspace, imgp->vp,
419 text_offset,
420 (caddr_t)(void *)(uintptr_t)text_address,
421 text_size, text_size,
422 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
423 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
424 return error;
425 }
426 /*
427 * Map in .data and .bss now
428 */
429
430
431 DPRINTF(("%s(%d): load_coff_section(vmspace, "
432 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
433 __FILE__, __LINE__, data_offset, data_address,
434 data_size + bss_size, data_size, VM_PROT_ALL));
435 if ((error = load_coff_section(vmspace, imgp->vp,
436 data_offset,
437 (caddr_t)(void *)(uintptr_t)data_address,
438 data_size + bss_size, data_size,
439 VM_PROT_ALL)) != 0) {
440
441 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
442 return error;
443 }
444
445 imgp->interpreted = 0;
446 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
447
448 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
449 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
450 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
451 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
452
453 hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
454
455
456 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
457 __FILE__, __LINE__, hole));
458 DPRINTF(("imgact: error = %d\n", error));
459
460 error = vm_map_find(&vmspace->vm_map, NULL, 0,
461 (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
462 VM_PROT_ALL, VM_PROT_ALL, 0);
463
464 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
465 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
466 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
467 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__));
468
469 /* Indicate that this file should not be modified */
470 imgp->vp->v_flag |= VTEXT;
471 return 0;
472}
473
474/*
475 * Tell kern_execve.c about it, with a little help from the linker.
476 */
477static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
478EXEC_SET(coff, coff_execsw);