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 without 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mman.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/vnode.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_extern.h>
50
51 #include <i386/ibcs2/coff.h>
52 #include <i386/ibcs2/ibcs2_util.h>
53
54 MODULE_DEPEND(coff, ibcs2, 1, 1, 1);
55
56 extern struct sysentvec ibcs2_svr3_sysvec;
57
58 static int coff_load_file(struct thread *td, char *name);
59 static int exec_coff_imgact(struct image_params *imgp);
60
61 static int load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot);
62
63 static int
64 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
65 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
66 {
67 size_t map_len;
68 vm_offset_t map_offset;
69 vm_offset_t map_addr;
70 int error;
71 unsigned char *data_buf = 0;
72 size_t copy_len;
73
74 map_offset = trunc_page(offset);
75 map_addr = trunc_page((vm_offset_t)vmaddr);
76
77 if (memsz > filsz) {
78 /*
79 * We have the stupid situation that
80 * the section is longer than it is on file,
81 * which means it has zero-filled areas, and
82 * we have to work for it. Stupid iBCS!
83 */
84 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
85 } else {
86 /*
87 * The only stuff we care about is on disk, and we
88 * don't care if we map in more than is really there.
89 */
90 map_len = round_page(offset + filsz) - trunc_page(map_offset);
91 }
92
93 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
94 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, 0x%x)\n",
95 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
96
97 if ((error = vm_mmap(&vmspace->vm_map,
98 &map_addr,
99 map_len,
100 prot,
101 VM_PROT_ALL,
102 MAP_PRIVATE | MAP_FIXED,
103 OBJT_VNODE,
104 vp,
105 map_offset)) != 0)
106 return error;
107
108 if (memsz == filsz) {
109 /* We're done! */
110 return 0;
111 }
112
113 /*
114 * Now we have screwball stuff, to accomodate stupid COFF.
115 * We have to map the remaining bit of the file into the kernel's
116 * memory map, allocate some anonymous memory, copy that last
117 * bit into it, and then we're done. *sigh*
118 * For clean-up reasons, we actally map in the file last.
119 */
120
121 copy_len = (offset + filsz) - trunc_page(offset + filsz);
122 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
123 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
124
125 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));
126
127 if (map_len != 0) {
128 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
129 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
130 if (error)
131 return error;
132 }
133
134 if ((error = vm_mmap(kernel_map,
135 (vm_offset_t *) &data_buf,
136 PAGE_SIZE,
137 VM_PROT_READ,
138 VM_PROT_READ,
139 0,
140 OBJT_VNODE,
141 vp,
142 trunc_page(offset + filsz))) != 0)
143 return error;
144
145 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
146
147 if (vm_map_remove(kernel_map,
148 (vm_offset_t) data_buf,
149 (vm_offset_t) data_buf + PAGE_SIZE))
150 panic("load_coff_section vm_map_remove failed");
151
152 return error;
153 }
154
155 static int
156 coff_load_file(struct thread *td, char *name)
157 {
158 struct proc *p = td->td_proc;
159 struct vmspace *vmspace = p->p_vmspace;
160 int error;
161 struct nameidata nd;
162 struct vnode *vp;
163 struct vattr attr;
164 struct filehdr *fhdr;
165 struct aouthdr *ahdr;
166 struct scnhdr *scns;
167 char *ptr = 0;
168 int nscns;
169 unsigned long text_offset = 0, text_address = 0, text_size = 0;
170 unsigned long data_offset = 0, data_address = 0, data_size = 0;
171 unsigned long bss_size = 0;
172 int i;
173
174 NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME,
175 UIO_SYSSPACE, name, td);
176
177 error = namei(&nd);
178 if (error)
179 return error;
180
181 vp = nd.ni_vp;
182 if (vp == NULL)
183 return ENOEXEC;
184
185 if (vp->v_writecount) {
186 error = ETXTBSY;
187 goto fail;
188 }
189
190 if ((error = VOP_GETATTR(vp, &attr, td->td_ucred, td)) != 0)
191 goto fail;
192
193 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
194 || ((attr.va_mode & 0111) == 0)
195 || (attr.va_type != VREG))
196 goto fail;
197
198 if (attr.va_size == 0) {
199 error = ENOEXEC;
200 goto fail;
201 }
202
203 if ((error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td)) != 0)
204 goto fail;
205
206 if ((error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1)) != 0)
207 goto fail;
208
209 /*
210 * Lose the lock on the vnode. It's no longer needed, and must not
211 * exist for the pagefault paging to work below.
212 */
213 VOP_UNLOCK(vp, 0, td);
214
215 if ((error = vm_mmap(kernel_map,
216 (vm_offset_t *) &ptr,
217 PAGE_SIZE,
218 VM_PROT_READ,
219 VM_PROT_READ,
220 0,
221 OBJT_VNODE,
222 vp,
223 0)) != 0)
224 goto unlocked_fail;
225
226 fhdr = (struct filehdr *)ptr;
227
228 if (fhdr->f_magic != I386_COFF) {
229 error = ENOEXEC;
230 goto dealloc_and_fail;
231 }
232
233 nscns = fhdr->f_nscns;
234
235 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
236 /*
237 * XXX -- just fail. I'm so lazy.
238 */
239 error = ENOEXEC;
240 goto dealloc_and_fail;
241 }
242
243 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
244
245 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
246 + sizeof(struct aouthdr));
247
248 for (i = 0; i < nscns; i++) {
249 if (scns[i].s_flags & STYP_NOLOAD)
250 continue;
251 else if (scns[i].s_flags & STYP_TEXT) {
252 text_address = scns[i].s_vaddr;
253 text_size = scns[i].s_size;
254 text_offset = scns[i].s_scnptr;
255 }
256 else if (scns[i].s_flags & STYP_DATA) {
257 data_address = scns[i].s_vaddr;
258 data_size = scns[i].s_size;
259 data_offset = scns[i].s_scnptr;
260 } else if (scns[i].s_flags & STYP_BSS) {
261 bss_size = scns[i].s_size;
262 }
263 }
264
265 if ((error = load_coff_section(vmspace, vp, text_offset,
266 (caddr_t)(void *)(uintptr_t)text_address,
267 text_size, text_size,
268 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
269 goto dealloc_and_fail;
270 }
271 if ((error = load_coff_section(vmspace, vp, data_offset,
272 (caddr_t)(void *)(uintptr_t)data_address,
273 data_size + bss_size, data_size,
274 VM_PROT_ALL)) != 0) {
275 goto dealloc_and_fail;
276 }
277
278 error = 0;
279
280 dealloc_and_fail:
281 if (vm_map_remove(kernel_map,
282 (vm_offset_t) ptr,
283 (vm_offset_t) ptr + PAGE_SIZE))
284 panic("%s vm_map_remove failed", __func__);
285
286 fail:
287 VOP_UNLOCK(vp, 0, td);
288 unlocked_fail:
289 NDFREE(&nd, NDF_ONLY_PNBUF);
290 vrele(nd.ni_vp);
291 return error;
292 }
293
294 static int
295 exec_coff_imgact(imgp)
296 struct image_params *imgp;
297 {
298 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
299 const struct aouthdr *ahdr;
300 const struct scnhdr *scns;
301 int i;
302 struct vmspace *vmspace;
303 int nscns;
304 int error;
305 unsigned long text_offset = 0, text_address = 0, text_size = 0;
306 unsigned long data_offset = 0, data_address = 0, data_size = 0;
307 unsigned long bss_size = 0;
308 caddr_t hole;
309 struct thread *td = curthread;
310
311 if (fhdr->f_magic != I386_COFF ||
312 !(fhdr->f_flags & F_EXEC)) {
313
314 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
315 return -1;
316 }
317
318 nscns = fhdr->f_nscns;
319 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
320 /*
321 * For now, return an error -- need to be able to
322 * read in all of the section structures.
323 */
324
325 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
326 return -1;
327 }
328
329 ahdr = (const struct aouthdr*)
330 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
331 imgp->entry_addr = ahdr->entry;
332
333 scns = (const struct scnhdr*)
334 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
335 sizeof(struct aouthdr));
336
337 VOP_UNLOCK(imgp->vp, 0, td);
338
339 exec_new_vmspace(imgp, &ibcs2_svr3_sysvec);
340 vmspace = imgp->proc->p_vmspace;
341
342 for (i = 0; i < nscns; i++) {
343
344 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
345 "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
346 scns[i].s_vaddr, scns[i].s_scnptr));
347 if (scns[i].s_flags & STYP_NOLOAD) {
348 /*
349 * A section that is not loaded, for whatever
350 * reason. It takes precedance over other flag
351 * bits...
352 */
353 continue;
354 } else if (scns[i].s_flags & STYP_TEXT) {
355 text_address = scns[i].s_vaddr;
356 text_size = scns[i].s_size;
357 text_offset = scns[i].s_scnptr;
358 } else if (scns[i].s_flags & STYP_DATA) {
359 /* .data section */
360 data_address = scns[i].s_vaddr;
361 data_size = scns[i].s_size;
362 data_offset = scns[i].s_scnptr;
363 } else if (scns[i].s_flags & STYP_BSS) {
364 /* .bss section */
365 bss_size = scns[i].s_size;
366 } else if (scns[i].s_flags & STYP_LIB) {
367 char *buf = 0;
368 int foff = trunc_page(scns[i].s_scnptr);
369 int off = scns[i].s_scnptr - foff;
370 int len = round_page(scns[i].s_size + PAGE_SIZE);
371 int j;
372
373 if ((error = vm_mmap(kernel_map,
374 (vm_offset_t *) &buf,
375 len,
376 VM_PROT_READ,
377 VM_PROT_READ,
378 0,
379 OBJT_VNODE,
380 imgp->vp,
381 foff)) != 0) {
382 error = ENOEXEC;
383 goto fail;
384 }
385 if(scns[i].s_size) {
386 char *libbuf;
387 int emul_path_len = strlen(ibcs2_emul_path);
388
389 libbuf = malloc(MAXPATHLEN + emul_path_len,
390 M_TEMP, M_WAITOK);
391 strcpy(libbuf, ibcs2_emul_path);
392
393 for (j = off; j < scns[i].s_size + off;) {
394 long stroff, nextoff;
395 char *libname;
396
397 nextoff = 4 * *(long *)(buf + j);
398 stroff = 4 * *(long *)(buf + j + sizeof(long));
399
400 libname = buf + j + stroff;
401 j += nextoff;
402
403 DPRINTF(("%s(%d): shared library %s\n",
404 __FILE__, __LINE__, libname));
405 strlcpy(&libbuf[emul_path_len], libname, MAXPATHLEN);
406 /* XXXKSE only 1:1 in coff */ error = coff_load_file(
407 FIRST_THREAD_IN_PROC(imgp->proc), libbuf);
408 if (error)
409 error = coff_load_file(
410 FIRST_THREAD_IN_PROC(imgp->proc),
411 libname);
412 if (error)
413 break;
414 }
415 free(libbuf, M_TEMP);
416 }
417 if (vm_map_remove(kernel_map,
418 (vm_offset_t) buf,
419 (vm_offset_t) buf + len))
420 panic("exec_coff_imgact vm_map_remove failed");
421 if (error)
422 goto fail;
423 }
424 }
425 /*
426 * Map in .text now
427 */
428
429 DPRINTF(("%s(%d): load_coff_section(vmspace, "
430 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
431 __FILE__, __LINE__, text_offset, text_address,
432 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
433 if ((error = load_coff_section(vmspace, imgp->vp,
434 text_offset,
435 (caddr_t)(void *)(uintptr_t)text_address,
436 text_size, text_size,
437 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
438 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
439 goto fail;
440 }
441 /*
442 * Map in .data and .bss now
443 */
444
445
446 DPRINTF(("%s(%d): load_coff_section(vmspace, "
447 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
448 __FILE__, __LINE__, data_offset, data_address,
449 data_size + bss_size, data_size, VM_PROT_ALL));
450 if ((error = load_coff_section(vmspace, imgp->vp,
451 data_offset,
452 (caddr_t)(void *)(uintptr_t)data_address,
453 data_size + bss_size, data_size,
454 VM_PROT_ALL)) != 0) {
455
456 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
457 goto fail;
458 }
459
460 imgp->interpreted = 0;
461 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
462
463 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
464 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
465 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
466 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
467
468 hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
469
470
471 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
472 __FILE__, __LINE__, hole));
473 DPRINTF(("imgact: error = %d\n", error));
474
475 error = vm_map_find(&vmspace->vm_map, NULL, 0,
476 (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
477 VM_PROT_ALL, VM_PROT_ALL, 0);
478
479 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
480 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
481 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
482 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__));
483
484 fail:
485 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
486
487 return error;
488 }
489
490 /*
491 * Tell kern_execve.c about it, with a little help from the linker.
492 */
493 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
494 EXEC_SET(coff, coff_execsw);
Cache object: d6f73b75fb1d2d146510bbd9c068ccf2
|