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