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