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