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