2 * Copyright (c) 1994 Sean Eric Fagan
3 * Copyright (c) 1994 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
29 * $FreeBSD: src/sys/i386/ibcs2/imgact_coff.c,v 1.40 1999/12/15 23:01:47 eivind Exp $
30 * $DragonFly: src/sys/emulation/ibcs2/coff/Attic/imgact_coff.c,v 1.2 2003/06/17 04:28:35 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.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>
51 #include <i386/ibcs2/coff.h>
52 #include <i386/ibcs2/ibcs2_util.h>
54 extern struct sysentvec ibcs2_svr3_sysvec;
56 static int coff_load_file __P((struct proc *p, char *name));
57 static int exec_coff_imgact __P((struct image_params *imgp));
59 static 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));
62 load_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)
66 vm_offset_t map_offset;
69 unsigned char *data_buf = 0;
72 map_offset = trunc_page(offset);
73 map_addr = trunc_page((vm_offset_t)vmaddr);
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!
82 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
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.
88 map_len = round_page(offset + filsz) - trunc_page(map_offset);
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));
95 if ((error = vm_mmap(&vmspace->vm_map,
100 MAP_PRIVATE | MAP_FIXED,
105 if (memsz == filsz) {
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.
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;
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));
125 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
126 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
131 if ((error = vm_mmap(kernel_map,
132 (vm_offset_t *) &data_buf,
138 trunc_page(offset + filsz))) != 0)
141 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
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");
152 coff_load_file(struct proc *p, char *name)
154 struct vmspace *vmspace = p->p_vmspace;
159 struct filehdr *fhdr;
160 struct aouthdr *ahdr;
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;
169 /* XXX use of 'curproc' should be 'p'?*/
170 NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, UIO_SYSSPACE, name, curproc);
180 if (vp->v_writecount) {
185 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
188 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
189 || ((attr.va_mode & 0111) == 0)
190 || (attr.va_type != VREG))
193 if (attr.va_size == 0) {
198 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
201 if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
205 * Lose the lock on the vnode. It's no longer needed, and must not
206 * exist for the pagefault paging to work below.
208 VOP_UNLOCK(vp, 0, p);
210 if ((error = vm_mmap(kernel_map,
211 (vm_offset_t *) &ptr,
220 fhdr = (struct filehdr *)ptr;
222 if (fhdr->f_magic != I386_COFF) {
224 goto dealloc_and_fail;
227 nscns = fhdr->f_nscns;
229 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
231 * XXX -- just fail. I'm so lazy.
234 goto dealloc_and_fail;
237 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
239 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
240 + sizeof(struct aouthdr));
242 for (i = 0; i < nscns; i++) {
243 if (scns[i].s_flags & STYP_NOLOAD)
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;
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;
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;
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;
275 if (vm_map_remove(kernel_map,
277 (vm_offset_t) ptr + PAGE_SIZE))
278 panic(__FUNCTION__ " vm_map_remove failed");
281 VOP_UNLOCK(vp, 0, p);
283 NDFREE(&nd, NDF_ONLY_PNBUF);
289 exec_coff_imgact(imgp)
290 struct image_params *imgp;
292 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
293 const struct aouthdr *ahdr;
294 const struct scnhdr *scns;
296 struct vmspace *vmspace;
299 unsigned long text_offset = 0, text_address = 0, text_size = 0;
300 unsigned long data_offset = 0, data_address = 0, data_size = 0;
301 unsigned long bss_size = 0;
304 if (fhdr->f_magic != I386_COFF ||
305 !(fhdr->f_flags & F_EXEC)) {
307 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
311 nscns = fhdr->f_nscns;
312 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
314 * For now, return an error -- need to be able to
315 * read in all of the section structures.
318 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
322 ahdr = (const struct aouthdr*)
323 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
324 imgp->entry_addr = ahdr->entry;
326 scns = (const struct scnhdr*)
327 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
328 sizeof(struct aouthdr));
330 if ((error = exec_extract_strings(imgp)) != 0) {
331 DPRINTF(("%s(%d): return %d\n", __FILE__, __LINE__, error));
335 exec_new_vmspace(imgp);
336 vmspace = imgp->proc->p_vmspace;
338 for (i = 0; i < nscns; i++) {
340 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
341 "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
342 scns[i].s_vaddr, scns[i].s_scnptr));
343 if (scns[i].s_flags & STYP_NOLOAD) {
345 * A section that is not loaded, for whatever
346 * reason. It takes precedance over other flag
350 } else if (scns[i].s_flags & STYP_TEXT) {
351 text_address = scns[i].s_vaddr;
352 text_size = scns[i].s_size;
353 text_offset = scns[i].s_scnptr;
354 } else if (scns[i].s_flags & STYP_DATA) {
356 data_address = scns[i].s_vaddr;
357 data_size = scns[i].s_size;
358 data_offset = scns[i].s_scnptr;
359 } else if (scns[i].s_flags & STYP_BSS) {
361 bss_size = scns[i].s_size;
362 } else if (scns[i].s_flags & STYP_LIB) {
364 int foff = trunc_page(scns[i].s_scnptr);
365 int off = scns[i].s_scnptr - foff;
366 int len = round_page(scns[i].s_size + PAGE_SIZE);
369 if ((error = vm_mmap(kernel_map,
370 (vm_offset_t *) &buf,
381 int emul_path_len = strlen(ibcs2_emul_path);
383 libbuf = malloc(MAXPATHLEN + emul_path_len,
385 strcpy(libbuf, ibcs2_emul_path);
387 for (j = off; j < scns[i].s_size + off; j++) {
390 libname = buf + j + 4 * *(long*)(buf + j + 4);
391 j += 4* *(long*)(buf + j);
393 DPRINTF(("%s(%d): shared library %s\n",
394 __FILE__, __LINE__, libname));
395 strcpy(&libbuf[emul_path_len], libname);
396 error = coff_load_file(imgp->proc, libbuf);
398 error = coff_load_file(imgp->proc,
403 free(libbuf, M_TEMP);
405 if (vm_map_remove(kernel_map,
407 (vm_offset_t) buf + len))
408 panic("exec_coff_imgact vm_map_remove failed");
417 DPRINTF(("%s(%d): load_coff_section(vmspace, "
418 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
419 __FILE__, __LINE__, text_offset, text_address,
420 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
421 if ((error = load_coff_section(vmspace, imgp->vp,
423 (caddr_t)(void *)(uintptr_t)text_address,
424 text_size, text_size,
425 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
426 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
430 * Map in .data and .bss now
434 DPRINTF(("%s(%d): load_coff_section(vmspace, "
435 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
436 __FILE__, __LINE__, data_offset, data_address,
437 data_size + bss_size, data_size, VM_PROT_ALL));
438 if ((error = load_coff_section(vmspace, imgp->vp,
440 (caddr_t)(void *)(uintptr_t)data_address,
441 data_size + bss_size, data_size,
442 VM_PROT_ALL)) != 0) {
444 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
448 imgp->interpreted = 0;
449 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
451 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
452 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
453 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
454 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
456 hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
459 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
460 __FILE__, __LINE__, hole));
461 DPRINTF(("imgact: error = %d\n", error));
463 error = vm_map_find(&vmspace->vm_map, NULL, 0,
464 (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
465 VM_PROT_ALL, VM_PROT_ALL, 0);
467 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
468 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
469 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
470 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__));
472 /* Indicate that this file should not be modified */
473 imgp->vp->v_flag |= VTEXT;
478 * Tell kern_execve.c about it, with a little help from the linker.
480 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
481 EXEC_SET(coff, coff_execsw);