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