2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994 Sean Eric Fagan
5 * Copyright (c) 1994 Søren Schmidt
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/fcntl.h>
39 #include <sys/imgact.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_extern.h>
54 #include <i386/ibcs2/coff.h>
55 #include <i386/ibcs2/ibcs2_util.h>
57 MODULE_DEPEND(coff, ibcs2, 1, 1, 1);
59 extern struct sysentvec ibcs2_svr3_sysvec;
61 static int coff_load_file(struct thread *td, char *name);
62 static int exec_coff_imgact(struct image_params *imgp);
64 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);
67 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
68 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
71 vm_offset_t map_offset;
74 unsigned char *data_buf = NULL;
77 map_offset = trunc_page(offset);
78 map_addr = trunc_page((vm_offset_t)vmaddr);
82 * We have the stupid situation that
83 * the section is longer than it is on file,
84 * which means it has zero-filled areas, and
85 * we have to work for it. Stupid iBCS!
87 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
90 * The only stuff we care about is on disk, and we
91 * don't care if we map in more than is really there.
93 map_len = round_page(offset + filsz) - trunc_page(map_offset);
96 DPRINTF(("%s(%d): vm_mmap(&vmspace->vm_map, &0x%08jx, 0x%x, 0x%x, "
97 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, 0x%x)\n",
98 __FILE__, __LINE__, (uintmax_t)map_addr, map_len, prot,
101 if ((error = vm_mmap(&vmspace->vm_map,
106 MAP_PRIVATE | MAP_FIXED,
112 if (memsz == filsz) {
118 * Now we have screwball stuff, to accomodate stupid COFF.
119 * We have to map the remaining bit of the file into the kernel's
120 * memory map, allocate some anonymous memory, copy that last
121 * bit into it, and then we're done. *sigh*
122 * For clean-up reasons, we actally map in the file last.
125 copy_len = (offset + filsz) - trunc_page(offset + filsz);
126 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
127 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
129 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));
132 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
133 map_len, 0, VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0);
135 return (vm_mmap_to_errno(error));
138 if ((error = vm_mmap(exec_map,
139 (vm_offset_t *) &data_buf,
146 trunc_page(offset + filsz))) != 0)
149 error = copyout(data_buf, (caddr_t) map_addr, copy_len);
151 kmap_free_wakeup(exec_map, (vm_offset_t)data_buf, PAGE_SIZE);
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 error = VOP_GET_WRITECOUNT(vp, &writecount);
189 if (writecount != 0) {
194 if ((error = VOP_GETATTR(vp, &attr, td->td_ucred)) != 0)
197 if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
198 || ((attr.va_mode & 0111) == 0)
199 || (attr.va_type != VREG))
202 if (attr.va_size == 0) {
207 if ((error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td)) != 0)
210 if ((error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL)) != 0)
214 * Lose the lock on the vnode. It's no longer needed, and must not
215 * exist for the pagefault paging to work below.
219 if ((error = vm_mmap(exec_map,
220 (vm_offset_t *) &ptr,
230 fhdr = (struct filehdr *)ptr;
232 if (fhdr->f_magic != I386_COFF) {
234 goto dealloc_and_fail;
237 nscns = fhdr->f_nscns;
239 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
241 * XXX -- just fail. I'm so lazy.
244 goto dealloc_and_fail;
247 ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
249 scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
250 + sizeof(struct aouthdr));
252 for (i = 0; i < nscns; i++) {
253 if (scns[i].s_flags & STYP_NOLOAD)
255 else if (scns[i].s_flags & STYP_TEXT) {
256 text_address = scns[i].s_vaddr;
257 text_size = scns[i].s_size;
258 text_offset = scns[i].s_scnptr;
260 else if (scns[i].s_flags & STYP_DATA) {
261 data_address = scns[i].s_vaddr;
262 data_size = scns[i].s_size;
263 data_offset = scns[i].s_scnptr;
264 } else if (scns[i].s_flags & STYP_BSS) {
265 bss_size = scns[i].s_size;
269 if ((error = load_coff_section(vmspace, vp, text_offset,
270 (caddr_t)(void *)(uintptr_t)text_address,
271 text_size, text_size,
272 VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
273 goto dealloc_and_fail;
275 if ((error = load_coff_section(vmspace, vp, data_offset,
276 (caddr_t)(void *)(uintptr_t)data_address,
277 data_size + bss_size, data_size,
278 VM_PROT_ALL)) != 0) {
279 goto dealloc_and_fail;
285 kmap_free_wakeup(exec_map, (vm_offset_t)ptr, PAGE_SIZE);
289 NDFREE(&nd, NDF_ONLY_PNBUF);
295 exec_coff_imgact(imgp)
296 struct image_params *imgp;
298 const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
299 const struct aouthdr *ahdr;
300 const struct scnhdr *scns;
302 struct vmspace *vmspace;
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;
310 if (fhdr->f_magic != I386_COFF ||
311 !(fhdr->f_flags & F_EXEC)) {
313 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
317 nscns = fhdr->f_nscns;
318 if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
320 * For now, return an error -- need to be able to
321 * read in all of the section structures.
324 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
328 ahdr = (const struct aouthdr*)
329 ((const char*)(imgp->image_header) + sizeof(struct filehdr));
330 imgp->entry_addr = ahdr->entry;
332 scns = (const struct scnhdr*)
333 ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
334 sizeof(struct aouthdr));
336 VOP_UNLOCK(imgp->vp, 0);
338 error = exec_new_vmspace(imgp, &ibcs2_svr3_sysvec);
341 vmspace = imgp->proc->p_vmspace;
343 for (i = 0; i < nscns; i++) {
345 DPRINTF(("i = %d, s_name = %s, s_vaddr = %08lx, "
346 "s_scnptr = %ld s_size = %lx\n", i, scns[i].s_name,
347 scns[i].s_vaddr, scns[i].s_scnptr, scns[i].s_size));
348 if (scns[i].s_flags & STYP_NOLOAD) {
350 * A section that is not loaded, for whatever
351 * reason. It takes precedance over other flag
355 } else if (scns[i].s_flags & STYP_TEXT) {
356 text_address = scns[i].s_vaddr;
357 text_size = scns[i].s_size;
358 text_offset = scns[i].s_scnptr;
359 } else if (scns[i].s_flags & STYP_DATA) {
361 data_address = scns[i].s_vaddr;
362 data_size = scns[i].s_size;
363 data_offset = scns[i].s_scnptr;
364 } else if (scns[i].s_flags & STYP_BSS) {
366 bss_size = scns[i].s_size;
367 } else if (scns[i].s_flags & STYP_LIB) {
369 int foff = trunc_page(scns[i].s_scnptr);
370 int off = scns[i].s_scnptr - foff;
371 int len = round_page(scns[i].s_size + PAGE_SIZE);
374 if ((error = vm_mmap(exec_map,
375 (vm_offset_t *) &buf,
388 int emul_path_len = strlen(ibcs2_emul_path);
390 libbuf = malloc(MAXPATHLEN + emul_path_len,
392 strcpy(libbuf, ibcs2_emul_path);
394 for (j = off; j < scns[i].s_size + off;) {
395 long stroff, nextoff;
398 nextoff = 4 * *(long *)(buf + j);
399 stroff = 4 * *(long *)(buf + j + sizeof(long));
401 libname = buf + j + stroff;
404 DPRINTF(("%s(%d): shared library %s\n",
405 __FILE__, __LINE__, libname));
406 strlcpy(&libbuf[emul_path_len], libname, MAXPATHLEN);
407 error = coff_load_file(
408 FIRST_THREAD_IN_PROC(imgp->proc), libbuf);
410 error = coff_load_file(
411 FIRST_THREAD_IN_PROC(imgp->proc),
415 "error %d loading coff shared library %s\n",
420 free(libbuf, M_TEMP);
422 kmap_free_wakeup(exec_map, (vm_offset_t)buf, len);
431 DPRINTF(("%s(%d): load_coff_section(vmspace, "
432 "imgp->vp, %08lx, %08lx, 0x%lx, 0x%lx, 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%lx, 0x%lx, 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 = trunc_page((vm_offset_t)vmspace->vm_daddr +
471 ctob(vmspace->vm_dsize));
473 DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%jx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
474 __FILE__, __LINE__, (uintmax_t)hole));
475 DPRINTF(("imgact: error = %d\n", error));
477 vm_map_find(&vmspace->vm_map, NULL, 0,
478 (vm_offset_t *)&hole, PAGE_SIZE, 0, VMFS_NO_SPACE,
479 VM_PROT_ALL, VM_PROT_ALL, 0);
480 DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%p end = 0x%p\n",
481 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
482 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
483 DPRINTF(("%s(%d): returning %d!\n", __FILE__, __LINE__, error));
486 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
492 * Tell kern_execve.c about it, with a little help from the linker.
494 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
495 EXEC_SET(coff, coff_execsw);