1cfefd687SGarrett Wollman /* 2cfefd687SGarrett Wollman * Copyright (c) 1993, David Greenman 3cfefd687SGarrett Wollman * All rights reserved. 4cfefd687SGarrett Wollman * 5cfefd687SGarrett Wollman * Redistribution and use in source and binary forms, with or without 6cfefd687SGarrett Wollman * modification, are permitted provided that the following conditions 7cfefd687SGarrett Wollman * are met: 8cfefd687SGarrett Wollman * 1. Redistributions of source code must retain the above copyright 9cfefd687SGarrett Wollman * notice, this list of conditions and the following disclaimer. 10cfefd687SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright 11cfefd687SGarrett Wollman * notice, this list of conditions and the following disclaimer in the 12cfefd687SGarrett Wollman * documentation and/or other materials provided with the distribution. 13cfefd687SGarrett Wollman * 3. All advertising materials mentioning features or use of this software 14cfefd687SGarrett Wollman * must display the following acknowledgement: 15cfefd687SGarrett Wollman * This product includes software developed by David Greenman 16cfefd687SGarrett Wollman * 4. The name of the developer may be used to endorse or promote products 17cfefd687SGarrett Wollman * derived from this software without specific prior written permission. 18cfefd687SGarrett Wollman * 19cfefd687SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20cfefd687SGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21cfefd687SGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221984b014SDavid Greenman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23cfefd687SGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24cfefd687SGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25cfefd687SGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26cfefd687SGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27cfefd687SGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28cfefd687SGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29cfefd687SGarrett Wollman * SUCH DAMAGE. 30cfefd687SGarrett Wollman * 31ede8dc43SBruce Evans * $Id: imgact_aout.c,v 1.24 1996/03/03 20:06:53 peter Exp $ 32cfefd687SGarrett Wollman */ 33cfefd687SGarrett Wollman 3426f9a767SRodney W. Grimes #include <sys/param.h> 3526f9a767SRodney W. Grimes #include <sys/systm.h> 3626f9a767SRodney W. Grimes #include <sys/resourcevar.h> 3726f9a767SRodney W. Grimes #include <sys/exec.h> 3826f9a767SRodney W. Grimes #include <sys/mman.h> 3926f9a767SRodney W. Grimes #include <sys/imgact.h> 40bc6d7444SDavid Greenman #include <sys/imgact_aout.h> 4126f9a767SRodney W. Grimes #include <sys/kernel.h> 42f3f0ca60SSøren Schmidt #include <sys/sysent.h> 43cfefd687SGarrett Wollman 4426f9a767SRodney W. Grimes #include <vm/vm.h> 45efeaf95aSDavid Greenman #include <vm/vm_param.h> 46efeaf95aSDavid Greenman #include <vm/vm_prot.h> 47efeaf95aSDavid Greenman #include <vm/lock.h> 48efeaf95aSDavid Greenman #include <vm/pmap.h> 49efeaf95aSDavid Greenman #include <vm/vm_map.h> 50efeaf95aSDavid Greenman #include <vm/vm_extern.h> 51cfefd687SGarrett Wollman 527ee050b7SBruce Evans static int exec_aout_imgact __P((struct image_params *imgp)); 537ee050b7SBruce Evans 547ee050b7SBruce Evans static int 55c52007c2SDavid Greenman exec_aout_imgact(imgp) 56c52007c2SDavid Greenman struct image_params *imgp; 57cfefd687SGarrett Wollman { 58c52007c2SDavid Greenman struct exec *a_out = (struct exec *) imgp->image_header; 59c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 60ede8dc43SBruce Evans vm_offset_t vmaddr; 61ede8dc43SBruce Evans unsigned long virtual_offset; 62a316d390SJohn Dyson unsigned long file_offset; 63cfefd687SGarrett Wollman unsigned long bss_size; 64bb56ec4aSPoul-Henning Kamp int error; 65cfefd687SGarrett Wollman 661e1e0b44SSøren Schmidt /* 671e1e0b44SSøren Schmidt * Linux and *BSD binaries look very much alike, 681e1e0b44SSøren Schmidt * only the machine id is different: 69d3628763SRodney W. Grimes * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI. 70185dc761SPeter Wemm * NetBSD is in network byte order.. ugh. 711e1e0b44SSøren Schmidt */ 72d3628763SRodney W. Grimes if (((a_out->a_magic >> 16) & 0xff) != 0x86 && 73185dc761SPeter Wemm ((a_out->a_magic >> 16) & 0xff) != 0 && 74185dc761SPeter Wemm ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86) 751e1e0b44SSøren Schmidt return -1; 761e1e0b44SSøren Schmidt 77cfefd687SGarrett Wollman /* 78cfefd687SGarrett Wollman * Set file/virtual offset based on a.out variant. 79cfefd687SGarrett Wollman * We do two cases: host byte order and network byte order 80cfefd687SGarrett Wollman * (for NetBSD compatibility) 81cfefd687SGarrett Wollman */ 82cfefd687SGarrett Wollman switch ((int)(a_out->a_magic & 0xffff)) { 83cfefd687SGarrett Wollman case ZMAGIC: 84cfefd687SGarrett Wollman virtual_offset = 0; 85cfefd687SGarrett Wollman if (a_out->a_text) { 86cfefd687SGarrett Wollman file_offset = NBPG; 87cfefd687SGarrett Wollman } else { 88cfefd687SGarrett Wollman /* Bill's "screwball mode" */ 89cfefd687SGarrett Wollman file_offset = 0; 90cfefd687SGarrett Wollman } 91cfefd687SGarrett Wollman break; 92cfefd687SGarrett Wollman case QMAGIC: 93cfefd687SGarrett Wollman virtual_offset = NBPG; 94cfefd687SGarrett Wollman file_offset = 0; 95cfefd687SGarrett Wollman break; 96cfefd687SGarrett Wollman default: 97cfefd687SGarrett Wollman /* NetBSD compatibility */ 98cfefd687SGarrett Wollman switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 99cfefd687SGarrett Wollman case ZMAGIC: 100cfefd687SGarrett Wollman case QMAGIC: 101cfefd687SGarrett Wollman virtual_offset = NBPG; 102cfefd687SGarrett Wollman file_offset = 0; 103cfefd687SGarrett Wollman break; 104cfefd687SGarrett Wollman default: 105cfefd687SGarrett Wollman return (-1); 106cfefd687SGarrett Wollman } 107cfefd687SGarrett Wollman } 108cfefd687SGarrett Wollman 109cfefd687SGarrett Wollman bss_size = roundup(a_out->a_bss, NBPG); 110cfefd687SGarrett Wollman 111cfefd687SGarrett Wollman /* 112cfefd687SGarrett Wollman * Check various fields in header for validity/bounds. 113cfefd687SGarrett Wollman */ 114cfefd687SGarrett Wollman if (/* entry point must lay with text region */ 115cfefd687SGarrett Wollman a_out->a_entry < virtual_offset || 116cfefd687SGarrett Wollman a_out->a_entry >= virtual_offset + a_out->a_text || 117cfefd687SGarrett Wollman 118cfefd687SGarrett Wollman /* text and data size must each be page rounded */ 119cfefd687SGarrett Wollman a_out->a_text % NBPG || 120cfefd687SGarrett Wollman a_out->a_data % NBPG) 121cfefd687SGarrett Wollman return (-1); 122cfefd687SGarrett Wollman 123cfefd687SGarrett Wollman /* text + data can't exceed file size */ 124c52007c2SDavid Greenman if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 125cfefd687SGarrett Wollman return (EFAULT); 126cfefd687SGarrett Wollman 127cfefd687SGarrett Wollman /* 128cfefd687SGarrett Wollman * text/data/bss must not exceed limits 129cfefd687SGarrett Wollman */ 130cfefd687SGarrett Wollman if (/* text can't exceed maximum text size */ 131cfefd687SGarrett Wollman a_out->a_text > MAXTSIZ || 132cfefd687SGarrett Wollman 133cfefd687SGarrett Wollman /* data + bss can't exceed maximum data size */ 134cfefd687SGarrett Wollman a_out->a_data + bss_size > MAXDSIZ || 135cfefd687SGarrett Wollman 136cfefd687SGarrett Wollman /* data + bss can't exceed rlimit */ 137cfefd687SGarrett Wollman a_out->a_data + bss_size > 138c52007c2SDavid Greenman imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 139cfefd687SGarrett Wollman return (ENOMEM); 140cfefd687SGarrett Wollman 141cfefd687SGarrett Wollman /* copy in arguments and/or environment from old process */ 142c52007c2SDavid Greenman error = exec_extract_strings(imgp); 143cfefd687SGarrett Wollman if (error) 144cfefd687SGarrett Wollman return (error); 145cfefd687SGarrett Wollman 146cfefd687SGarrett Wollman /* 147cfefd687SGarrett Wollman * Destroy old process VM and create a new one (with a new stack) 148cfefd687SGarrett Wollman */ 149c52007c2SDavid Greenman exec_new_vmspace(imgp); 150cfefd687SGarrett Wollman 151cfefd687SGarrett Wollman /* 152bd7e5f99SJohn Dyson * Map text/data read/execute 153cfefd687SGarrett Wollman */ 154cfefd687SGarrett Wollman vmaddr = virtual_offset; 155cfefd687SGarrett Wollman error = 156cfefd687SGarrett Wollman vm_mmap(&vmspace->vm_map, /* map */ 157cfefd687SGarrett Wollman &vmaddr, /* address */ 158bd7e5f99SJohn Dyson a_out->a_text + a_out->a_data, /* size */ 159cfefd687SGarrett Wollman VM_PROT_READ | VM_PROT_EXECUTE, /* protection */ 160bd7e5f99SJohn Dyson VM_PROT_ALL, /* max protection */ 16126f9a767SRodney W. Grimes MAP_PRIVATE | MAP_FIXED, /* flags */ 162c52007c2SDavid Greenman (caddr_t)imgp->vp, /* vnode */ 163cfefd687SGarrett Wollman file_offset); /* offset */ 164cfefd687SGarrett Wollman if (error) 165cfefd687SGarrett Wollman return (error); 166cfefd687SGarrett Wollman 167cfefd687SGarrett Wollman /* 168bd7e5f99SJohn Dyson * allow writing of data 169cfefd687SGarrett Wollman */ 170bd7e5f99SJohn Dyson vm_map_protect(&vmspace->vm_map, 171bd7e5f99SJohn Dyson vmaddr + a_out->a_text, 172bd7e5f99SJohn Dyson vmaddr + a_out->a_text + a_out->a_data, 173bd7e5f99SJohn Dyson VM_PROT_ALL, 174bd7e5f99SJohn Dyson FALSE); 175cfefd687SGarrett Wollman 17668940ac1SDavid Greenman if (bss_size != 0) { 177cfefd687SGarrett Wollman /* 178cfefd687SGarrett Wollman * Allocate demand-zeroed area for uninitialized data 179cfefd687SGarrett Wollman * "bss" = 'block started by symbol' - named after the IBM 7090 180cfefd687SGarrett Wollman * instruction of the same name. 181cfefd687SGarrett Wollman */ 182cfefd687SGarrett Wollman vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 183bd7e5f99SJohn Dyson error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 184cfefd687SGarrett Wollman if (error) 185cfefd687SGarrett Wollman return (error); 18668940ac1SDavid Greenman } 187cfefd687SGarrett Wollman 188cfefd687SGarrett Wollman /* Fill in process VM information */ 189cfefd687SGarrett Wollman vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 190cfefd687SGarrett Wollman vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 191cfefd687SGarrett Wollman vmspace->vm_taddr = (caddr_t) virtual_offset; 192cfefd687SGarrett Wollman vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text; 193cfefd687SGarrett Wollman 194cfefd687SGarrett Wollman /* Fill in image_params */ 195c52007c2SDavid Greenman imgp->interpreted = 0; 196c52007c2SDavid Greenman imgp->entry_addr = a_out->a_entry; 197cfefd687SGarrett Wollman 198c52007c2SDavid Greenman imgp->proc->p_sysent = &aout_sysvec; 199c0e5de7dSDavid Greenman 200c0e5de7dSDavid Greenman /* Indicate that this file should not be modified */ 201c52007c2SDavid Greenman imgp->vp->v_flag |= VTEXT; 202c0e5de7dSDavid Greenman 203cfefd687SGarrett Wollman return (0); 204cfefd687SGarrett Wollman } 20592d91f76SGarrett Wollman 20692d91f76SGarrett Wollman /* 20792d91f76SGarrett Wollman * Tell kern_execve.c about it, with a little help from the linker. 20892d91f76SGarrett Wollman * Since `const' objects end up in the text segment, TEXT_SET is the 20992d91f76SGarrett Wollman * correct directive to use. 21092d91f76SGarrett Wollman */ 211f23b4c91SGarrett Wollman static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 21292d91f76SGarrett Wollman TEXT_SET(execsw_set, aout_execsw); 213