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