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 without 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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_extern.h>
52 #include <i386/ibcs2/coff.h>
53 #include <i386/ibcs2/ibcs2_util.h>
55 MODULE_DEPEND(coff, ibcs2, 1, 1, 1);
57 extern struct sysentvec ibcs2_svr3_sysvec;
59 static int coff_load_file(struct thread *td, char *name);
60 static int exec_coff_imgact(struct image_params *imgp);
62 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);
65 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
66 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
69 vm_offset_t map_offset;
72 unsigned char *data_buf = 0;
75 map_offset = trunc_page(offset);
76 map_addr = trunc_page((vm_offset_t)vmaddr);
80 * We have the stupid situation that
81 * the section is longer than it is on file,
82 * which means it has zero-filled areas, and
83 * we have to work for it. Stupid iBCS!
85 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
88 * The only stuff we care about is on disk, and we
89 * don't care if we map in more than is really there.
91 map_len = round_page(offset + filsz) - trunc_page(map_offset);
94 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08jx, 0x%x, 0x%x, "
95 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, 0x%x)\n",
96 __FILE__, __LINE__, (uintmax_t)map_addr, map_len, prot,
99 if ((error = vm_mmap(&vmspace->vm_map,
104 MAP_PRIVATE | MAP_FIXED,
110 if (memsz == filsz) {
116 * Now we have screwball stuff, to accomodate stupid COFF.
117 * We have to map the remaining bit of the file into the kernel's
118 * memory map, allocate some anonymous memory, copy that last
119 * bit into it, and then we're done. *sigh*
120 * For clean-up reasons, we actally map in the file last.
123 copy_len = (offset + filsz) - trunc_page(offset + filsz);
124 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
125 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
127 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08jx,0x%x, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, (uintmax_t)map_addr, map_len));
130 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
131 map_len, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
133 return (vm_mmap_to_errno(error));
136 if ((error = vm_mmap(exec_map,
137 (vm_offset_t *) &data_buf,
144 trunc_page(offset + filsz))) != 0)
147 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
149 if (vm_map_remove(exec_map,
150 (vm_offset_t) data_buf,
151 (vm_offset_t) data_buf + PAGE_SIZE))
152 panic("load_coff_section vm_map_remove failed");
158 coff_load_file(struct thread *td, char *name)
160 struct proc *p = td->td_proc;
161 struct vmspace *vmspace = p->p_vmspace;
166 struct filehdr *fhdr;
167 struct aouthdr *ahdr;
171 unsigned long text_offset = 0, text_address = 0, text_size = 0;
172 unsigned long data_offset = 0, data_address = 0, data_size = 0;
173 unsigned long bss_size = 0;
176 NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME,
177 UIO_SYSSPACE, name, td);
187 if (vp->v_writecount) {
192 if ((error = VOP_GETATTR(vp, &attr, td->td_ucred)) != 0)
195 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
196 || ((attr.va_mode & 0111) == 0)
197 || (attr.va_type != VREG))
200 if (attr.va_size == 0) {
205 if ((error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td)) != 0)
208 if ((error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL)) != 0)
212 * Lose the lock on the vnode. It's no longer needed, and must not
213 * exist for the pagefault paging to work below.
217 if ((error = vm_mmap(exec_map,
218 (vm_offset_t *) &ptr,
228 fhdr = (struct filehdr *)ptr;
230 if (fhdr->f_magic != I386_COFF) {
232 goto dealloc_and_fail;
235 nscns = fhdr->f_nscns;
237 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
239 * XXX -- just fail. I'm so lazy.
242 goto dealloc_and_fail;
245 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
247 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
248 + sizeof(struct aouthdr));
250 for (i = 0; i < nscns; i++) {
251 if (scns[i].s_flags & STYP_NOLOAD)
253 else if (scns[i].s_flags & STYP_TEXT) {
254 text_address = scns[i].s_vaddr;
255 text_size = scns[i].s_size;
256 text_offset = scns[i].s_scnptr;
258 else if (scns[i].s_flags & STYP_DATA) {
259 data_address = scns[i].s_vaddr;
260 data_size = scns[i].s_size;
261 data_offset = scns[i].s_scnptr;
262 } else if (scns[i].s_flags & STYP_BSS) {
263 bss_size = scns[i].s_size;
267 if ((error = load_coff_section(vmspace, vp, text_offset,
268 (caddr_t)(void *)(uintptr_t)text_address,
269 text_size, text_size,
270 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
271 goto dealloc_and_fail;
273 if ((error = load_coff_section(vmspace, vp, data_offset,
274 (caddr_t)(void *)(uintptr_t)data_address,
275 data_size + bss_size, data_size,
276 VM_PROT_ALL)) != 0) {
277 goto dealloc_and_fail;
283 if (vm_map_remove(exec_map,
285 (vm_offset_t) ptr + PAGE_SIZE))
286 panic("%s vm_map_remove failed", __func__);
291 NDFREE(&nd, NDF_ONLY_PNBUF);
297 exec_coff_imgact(imgp)
298 struct image_params *imgp;
300 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
301 const struct aouthdr *ahdr;
302 const struct scnhdr *scns;
304 struct vmspace *vmspace;
307 unsigned long text_offset = 0, text_address = 0, text_size = 0;
308 unsigned long data_offset = 0, data_address = 0, data_size = 0;
309 unsigned long bss_size = 0;
312 if (fhdr->f_magic != I386_COFF ||
313 !(fhdr->f_flags & F_EXEC)) {
315 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
319 nscns = fhdr->f_nscns;
320 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
322 * For now, return an error -- need to be able to
323 * read in all of the section structures.
326 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
330 ahdr = (const struct aouthdr*)
331 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
332 imgp->entry_addr = ahdr->entry;
334 scns = (const struct scnhdr*)
335 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
336 sizeof(struct aouthdr));
338 VOP_UNLOCK(imgp->vp, 0);
340 error = exec_new_vmspace(imgp, &ibcs2_svr3_sysvec);
343 vmspace = imgp->proc->p_vmspace;
345 for (i = 0; i < nscns; i++) {
347 DPRINTF(("i = %d, s_name = %s, s_vaddr = %08lx, "
348 "s_scnptr = %ld s_size = %lx\n", i, scns[i].s_name,
349 scns[i].s_vaddr, scns[i].s_scnptr, scns[i].s_size));
350 if (scns[i].s_flags & STYP_NOLOAD) {
352 * A section that is not loaded, for whatever
353 * reason. It takes precedance over other flag
357 } else if (scns[i].s_flags & STYP_TEXT) {
358 text_address = scns[i].s_vaddr;
359 text_size = scns[i].s_size;
360 text_offset = scns[i].s_scnptr;
361 } else if (scns[i].s_flags & STYP_DATA) {
363 data_address = scns[i].s_vaddr;
364 data_size = scns[i].s_size;
365 data_offset = scns[i].s_scnptr;
366 } else if (scns[i].s_flags & STYP_BSS) {
368 bss_size = scns[i].s_size;
369 } else if (scns[i].s_flags & STYP_LIB) {
371 int foff = trunc_page(scns[i].s_scnptr);
372 int off = scns[i].s_scnptr - foff;
373 int len = round_page(scns[i].s_size + PAGE_SIZE);
376 if ((error = vm_mmap(exec_map,
377 (vm_offset_t *) &buf,
390 int emul_path_len = strlen(ibcs2_emul_path);
392 libbuf = malloc(MAXPATHLEN + emul_path_len,
394 strcpy(libbuf, ibcs2_emul_path);
396 for (j = off; j < scns[i].s_size + off;) {
397 long stroff, nextoff;
400 nextoff = 4 * *(long *)(buf + j);
401 stroff = 4 * *(long *)(buf + j + sizeof(long));
403 libname = buf + j + stroff;
406 DPRINTF(("%s(%d): shared library %s\n",
407 __FILE__, __LINE__, libname));
408 strlcpy(&libbuf[emul_path_len], libname, MAXPATHLEN);
409 error = coff_load_file(
410 FIRST_THREAD_IN_PROC(imgp->proc), libbuf);
412 error = coff_load_file(
413 FIRST_THREAD_IN_PROC(imgp->proc),
417 "error %d loading coff shared library %s\n",
422 free(libbuf, M_TEMP);
424 if (vm_map_remove(exec_map,
426 (vm_offset_t) buf + len))
427 panic("exec_coff_imgact vm_map_remove failed");
436 DPRINTF(("%s(%d): load_coff_section(vmspace, "
437 "imgp->vp, %08lx, %08lx, 0x%lx, 0x%lx, 0x%x)\n",
438 __FILE__, __LINE__, text_offset, text_address,
439 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
440 if ((error = load_coff_section(vmspace, imgp->vp,
442 (caddr_t)(void *)(uintptr_t)text_address,
443 text_size, text_size,
444 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
445 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
449 * Map in .data and .bss now
453 DPRINTF(("%s(%d): load_coff_section(vmspace, "
454 "imgp->vp, 0x%08lx, 0x%08lx, 0x%lx, 0x%lx, 0x%x)\n",
455 __FILE__, __LINE__, data_offset, data_address,
456 data_size + bss_size, data_size, VM_PROT_ALL));
457 if ((error = load_coff_section(vmspace, imgp->vp,
459 (caddr_t)(void *)(uintptr_t)data_address,
460 data_size + bss_size, data_size,
461 VM_PROT_ALL)) != 0) {
463 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
467 imgp->interpreted = 0;
468 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
470 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
471 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
472 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
473 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
475 hole = trunc_page((vm_offset_t)vmspace->vm_daddr +
476 ctob(vmspace->vm_dsize));
478 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%jx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
479 __FILE__, __LINE__, (uintmax_t)hole));
480 DPRINTF(("imgact: error = %d\n", error));
482 vm_map_find(&vmspace->vm_map, NULL, 0,
483 (vm_offset_t *)&hole, PAGE_SIZE, VMFS_NO_SPACE,
484 VM_PROT_ALL, VM_PROT_ALL, 0);
485 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%p end = 0x%p\n",
486 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
487 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
488 DPRINTF(("%s(%d): returning %d!\n", __FILE__, __LINE__, error));
491 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
497 * Tell kern_execve.c about it, with a little help from the linker.
499 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
500 EXEC_SET(coff, coff_execsw);