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%08lx, 0x%x, 0x%x, "
95 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, 0x%x)\n",
96 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
98 if ((error = vm_mmap(&vmspace->vm_map,
103 MAP_PRIVATE | MAP_FIXED,
109 if (memsz == filsz) {
115 * Now we have screwball stuff, to accomodate stupid COFF.
116 * We have to map the remaining bit of the file into the kernel's
117 * memory map, allocate some anonymous memory, copy that last
118 * bit into it, and then we're done. *sigh*
119 * For clean-up reasons, we actally map in the file last.
122 copy_len = (offset + filsz) - trunc_page(offset + filsz);
123 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
124 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
126 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));
129 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
130 map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
135 if ((error = vm_mmap(kernel_map,
136 (vm_offset_t *) &data_buf,
143 trunc_page(offset + filsz))) != 0)
146 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
148 if (vm_map_remove(kernel_map,
149 (vm_offset_t) data_buf,
150 (vm_offset_t) data_buf + PAGE_SIZE))
151 panic("load_coff_section vm_map_remove failed");
157 coff_load_file(struct thread *td, char *name)
159 struct proc *p = td->td_proc;
160 struct vmspace *vmspace = p->p_vmspace;
165 struct filehdr *fhdr;
166 struct aouthdr *ahdr;
170 unsigned long text_offset = 0, text_address = 0, text_size = 0;
171 unsigned long data_offset = 0, data_address = 0, data_size = 0;
172 unsigned long bss_size = 0;
175 NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME,
176 UIO_SYSSPACE, name, td);
186 if (vp->v_writecount) {
191 if ((error = VOP_GETATTR(vp, &attr, td->td_ucred)) != 0)
194 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
195 || ((attr.va_mode & 0111) == 0)
196 || (attr.va_type != VREG))
199 if (attr.va_size == 0) {
204 if ((error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td)) != 0)
207 if ((error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL)) != 0)
211 * Lose the lock on the vnode. It's no longer needed, and must not
212 * exist for the pagefault paging to work below.
216 if ((error = vm_mmap(kernel_map,
217 (vm_offset_t *) &ptr,
227 fhdr = (struct filehdr *)ptr;
229 if (fhdr->f_magic != I386_COFF) {
231 goto dealloc_and_fail;
234 nscns = fhdr->f_nscns;
236 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
238 * XXX -- just fail. I'm so lazy.
241 goto dealloc_and_fail;
244 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
246 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
247 + sizeof(struct aouthdr));
249 for (i = 0; i < nscns; i++) {
250 if (scns[i].s_flags & STYP_NOLOAD)
252 else if (scns[i].s_flags & STYP_TEXT) {
253 text_address = scns[i].s_vaddr;
254 text_size = scns[i].s_size;
255 text_offset = scns[i].s_scnptr;
257 else if (scns[i].s_flags & STYP_DATA) {
258 data_address = scns[i].s_vaddr;
259 data_size = scns[i].s_size;
260 data_offset = scns[i].s_scnptr;
261 } else if (scns[i].s_flags & STYP_BSS) {
262 bss_size = scns[i].s_size;
266 if ((error = load_coff_section(vmspace, vp, text_offset,
267 (caddr_t)(void *)(uintptr_t)text_address,
268 text_size, text_size,
269 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
270 goto dealloc_and_fail;
272 if ((error = load_coff_section(vmspace, vp, data_offset,
273 (caddr_t)(void *)(uintptr_t)data_address,
274 data_size + bss_size, data_size,
275 VM_PROT_ALL)) != 0) {
276 goto dealloc_and_fail;
282 if (vm_map_remove(kernel_map,
284 (vm_offset_t) ptr + PAGE_SIZE))
285 panic("%s vm_map_remove failed", __func__);
290 NDFREE(&nd, NDF_ONLY_PNBUF);
296 exec_coff_imgact(imgp)
297 struct image_params *imgp;
299 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
300 const struct aouthdr *ahdr;
301 const struct scnhdr *scns;
303 struct vmspace *vmspace;
306 unsigned long text_offset = 0, text_address = 0, text_size = 0;
307 unsigned long data_offset = 0, data_address = 0, data_size = 0;
308 unsigned long bss_size = 0;
311 if (fhdr->f_magic != I386_COFF ||
312 !(fhdr->f_flags & F_EXEC)) {
314 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
318 nscns = fhdr->f_nscns;
319 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
321 * For now, return an error -- need to be able to
322 * read in all of the section structures.
325 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
329 ahdr = (const struct aouthdr*)
330 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
331 imgp->entry_addr = ahdr->entry;
333 scns = (const struct scnhdr*)
334 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
335 sizeof(struct aouthdr));
337 VOP_UNLOCK(imgp->vp, 0);
339 error = exec_new_vmspace(imgp, &ibcs2_svr3_sysvec);
342 vmspace = imgp->proc->p_vmspace;
344 for (i = 0; i < nscns; i++) {
346 DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
347 "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
348 scns[i].s_vaddr, scns[i].s_scnptr));
349 if (scns[i].s_flags & STYP_NOLOAD) {
351 * A section that is not loaded, for whatever
352 * reason. It takes precedance over other flag
356 } else if (scns[i].s_flags & STYP_TEXT) {
357 text_address = scns[i].s_vaddr;
358 text_size = scns[i].s_size;
359 text_offset = scns[i].s_scnptr;
360 } else if (scns[i].s_flags & STYP_DATA) {
362 data_address = scns[i].s_vaddr;
363 data_size = scns[i].s_size;
364 data_offset = scns[i].s_scnptr;
365 } else if (scns[i].s_flags & STYP_BSS) {
367 bss_size = scns[i].s_size;
368 } else if (scns[i].s_flags & STYP_LIB) {
370 int foff = trunc_page(scns[i].s_scnptr);
371 int off = scns[i].s_scnptr - foff;
372 int len = round_page(scns[i].s_size + PAGE_SIZE);
375 if ((error = vm_mmap(kernel_map,
376 (vm_offset_t *) &buf,
389 int emul_path_len = strlen(ibcs2_emul_path);
391 libbuf = malloc(MAXPATHLEN + emul_path_len,
393 strcpy(libbuf, ibcs2_emul_path);
395 for (j = off; j < scns[i].s_size + off;) {
396 long stroff, nextoff;
399 nextoff = 4 * *(long *)(buf + j);
400 stroff = 4 * *(long *)(buf + j + sizeof(long));
402 libname = buf + j + stroff;
405 DPRINTF(("%s(%d): shared library %s\n",
406 __FILE__, __LINE__, libname));
407 strlcpy(&libbuf[emul_path_len], libname, MAXPATHLEN);
408 error = coff_load_file(
409 FIRST_THREAD_IN_PROC(imgp->proc), libbuf);
411 error = coff_load_file(
412 FIRST_THREAD_IN_PROC(imgp->proc),
417 free(libbuf, M_TEMP);
419 if (vm_map_remove(kernel_map,
421 (vm_offset_t) buf + len))
422 panic("exec_coff_imgact vm_map_remove failed");
431 DPRINTF(("%s(%d): load_coff_section(vmspace, "
432 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
433 __FILE__, __LINE__, text_offset, text_address,
434 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
435 if ((error = load_coff_section(vmspace, imgp->vp,
437 (caddr_t)(void *)(uintptr_t)text_address,
438 text_size, text_size,
439 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
440 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
444 * Map in .data and .bss now
448 DPRINTF(("%s(%d): load_coff_section(vmspace, "
449 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
450 __FILE__, __LINE__, data_offset, data_address,
451 data_size + bss_size, data_size, VM_PROT_ALL));
452 if ((error = load_coff_section(vmspace, imgp->vp,
454 (caddr_t)(void *)(uintptr_t)data_address,
455 data_size + bss_size, data_size,
456 VM_PROT_ALL)) != 0) {
458 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
462 imgp->interpreted = 0;
463 imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
465 vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
466 vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
467 vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
468 vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
470 hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
473 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
474 __FILE__, __LINE__, hole));
475 DPRINTF(("imgact: error = %d\n", error));
477 error = vm_map_find(&vmspace->vm_map, NULL, 0,
478 (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
479 VM_PROT_ALL, VM_PROT_ALL, 0);
481 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
482 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
483 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
484 DPRINTF(("%s(%d): returning successfully!\n", __FILE__, __LINE__));
487 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
493 * Tell kern_execve.c about it, with a little help from the linker.
495 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
496 EXEC_SET(coff, coff_execsw);