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