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 * 31185dc761SPeter Wemm * $Id: imgact_aout.c,v 1.23 1996/03/02 19:38:06 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; 60a316d390SJohn Dyson unsigned long vmaddr, virtual_offset; 61a316d390SJohn Dyson unsigned long file_offset; 62cfefd687SGarrett Wollman unsigned long bss_size; 63bb56ec4aSPoul-Henning Kamp int error; 64cfefd687SGarrett Wollman 651e1e0b44SSøren Schmidt /* 661e1e0b44SSøren Schmidt * Linux and *BSD binaries look very much alike, 671e1e0b44SSøren Schmidt * only the machine id is different: 68d3628763SRodney W. Grimes * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI. 69185dc761SPeter Wemm * NetBSD is in network byte order.. ugh. 701e1e0b44SSøren Schmidt */ 71d3628763SRodney W. Grimes if (((a_out->a_magic >> 16) & 0xff) != 0x86 && 72185dc761SPeter Wemm ((a_out->a_magic >> 16) & 0xff) != 0 && 73185dc761SPeter Wemm ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86) 741e1e0b44SSøren Schmidt return -1; 751e1e0b44SSøren Schmidt 76cfefd687SGarrett Wollman /* 77cfefd687SGarrett Wollman * Set file/virtual offset based on a.out variant. 78cfefd687SGarrett Wollman * We do two cases: host byte order and network byte order 79cfefd687SGarrett Wollman * (for NetBSD compatibility) 80cfefd687SGarrett Wollman */ 81cfefd687SGarrett Wollman switch ((int)(a_out->a_magic & 0xffff)) { 82cfefd687SGarrett Wollman case ZMAGIC: 83cfefd687SGarrett Wollman virtual_offset = 0; 84cfefd687SGarrett Wollman if (a_out->a_text) { 85cfefd687SGarrett Wollman file_offset = NBPG; 86cfefd687SGarrett Wollman } else { 87cfefd687SGarrett Wollman /* Bill's "screwball mode" */ 88cfefd687SGarrett Wollman file_offset = 0; 89cfefd687SGarrett Wollman } 90cfefd687SGarrett Wollman break; 91cfefd687SGarrett Wollman case QMAGIC: 92cfefd687SGarrett Wollman virtual_offset = NBPG; 93cfefd687SGarrett Wollman file_offset = 0; 94cfefd687SGarrett Wollman break; 95cfefd687SGarrett Wollman default: 96cfefd687SGarrett Wollman /* NetBSD compatibility */ 97cfefd687SGarrett Wollman switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 98cfefd687SGarrett Wollman case ZMAGIC: 99cfefd687SGarrett Wollman case QMAGIC: 100cfefd687SGarrett Wollman virtual_offset = NBPG; 101cfefd687SGarrett Wollman file_offset = 0; 102cfefd687SGarrett Wollman break; 103cfefd687SGarrett Wollman default: 104cfefd687SGarrett Wollman return (-1); 105cfefd687SGarrett Wollman } 106cfefd687SGarrett Wollman } 107cfefd687SGarrett Wollman 108cfefd687SGarrett Wollman bss_size = roundup(a_out->a_bss, NBPG); 109cfefd687SGarrett Wollman 110cfefd687SGarrett Wollman /* 111cfefd687SGarrett Wollman * Check various fields in header for validity/bounds. 112cfefd687SGarrett Wollman */ 113cfefd687SGarrett Wollman if (/* entry point must lay with text region */ 114cfefd687SGarrett Wollman a_out->a_entry < virtual_offset || 115cfefd687SGarrett Wollman a_out->a_entry >= virtual_offset + a_out->a_text || 116cfefd687SGarrett Wollman 117cfefd687SGarrett Wollman /* text and data size must each be page rounded */ 118cfefd687SGarrett Wollman a_out->a_text % NBPG || 119cfefd687SGarrett Wollman a_out->a_data % NBPG) 120cfefd687SGarrett Wollman return (-1); 121cfefd687SGarrett Wollman 122cfefd687SGarrett Wollman /* text + data can't exceed file size */ 123c52007c2SDavid Greenman if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 124cfefd687SGarrett Wollman return (EFAULT); 125cfefd687SGarrett Wollman 126cfefd687SGarrett Wollman /* 127cfefd687SGarrett Wollman * text/data/bss must not exceed limits 128cfefd687SGarrett Wollman */ 129cfefd687SGarrett Wollman if (/* text can't exceed maximum text size */ 130cfefd687SGarrett Wollman a_out->a_text > MAXTSIZ || 131cfefd687SGarrett Wollman 132cfefd687SGarrett Wollman /* data + bss can't exceed maximum data size */ 133cfefd687SGarrett Wollman a_out->a_data + bss_size > MAXDSIZ || 134cfefd687SGarrett Wollman 135cfefd687SGarrett Wollman /* data + bss can't exceed rlimit */ 136cfefd687SGarrett Wollman a_out->a_data + bss_size > 137c52007c2SDavid Greenman imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 138cfefd687SGarrett Wollman return (ENOMEM); 139cfefd687SGarrett Wollman 140cfefd687SGarrett Wollman /* copy in arguments and/or environment from old process */ 141c52007c2SDavid Greenman error = exec_extract_strings(imgp); 142cfefd687SGarrett Wollman if (error) 143cfefd687SGarrett Wollman return (error); 144cfefd687SGarrett Wollman 145cfefd687SGarrett Wollman /* 146cfefd687SGarrett Wollman * Destroy old process VM and create a new one (with a new stack) 147cfefd687SGarrett Wollman */ 148c52007c2SDavid Greenman exec_new_vmspace(imgp); 149cfefd687SGarrett Wollman 150cfefd687SGarrett Wollman /* 151bd7e5f99SJohn Dyson * Map text/data read/execute 152cfefd687SGarrett Wollman */ 153cfefd687SGarrett Wollman vmaddr = virtual_offset; 154cfefd687SGarrett Wollman error = 155cfefd687SGarrett Wollman vm_mmap(&vmspace->vm_map, /* map */ 156cfefd687SGarrett Wollman &vmaddr, /* address */ 157bd7e5f99SJohn Dyson a_out->a_text + a_out->a_data, /* size */ 158cfefd687SGarrett Wollman VM_PROT_READ | VM_PROT_EXECUTE, /* protection */ 159bd7e5f99SJohn Dyson VM_PROT_ALL, /* max protection */ 16026f9a767SRodney W. Grimes MAP_PRIVATE | MAP_FIXED, /* flags */ 161c52007c2SDavid Greenman (caddr_t)imgp->vp, /* vnode */ 162cfefd687SGarrett Wollman file_offset); /* offset */ 163cfefd687SGarrett Wollman if (error) 164cfefd687SGarrett Wollman return (error); 165cfefd687SGarrett Wollman 166cfefd687SGarrett Wollman /* 167bd7e5f99SJohn Dyson * allow writing of data 168cfefd687SGarrett Wollman */ 169bd7e5f99SJohn Dyson vm_map_protect(&vmspace->vm_map, 170bd7e5f99SJohn Dyson vmaddr + a_out->a_text, 171bd7e5f99SJohn Dyson vmaddr + a_out->a_text + a_out->a_data, 172bd7e5f99SJohn Dyson VM_PROT_ALL, 173bd7e5f99SJohn Dyson FALSE); 174cfefd687SGarrett Wollman 17568940ac1SDavid Greenman if (bss_size != 0) { 176cfefd687SGarrett Wollman /* 177cfefd687SGarrett Wollman * Allocate demand-zeroed area for uninitialized data 178cfefd687SGarrett Wollman * "bss" = 'block started by symbol' - named after the IBM 7090 179cfefd687SGarrett Wollman * instruction of the same name. 180cfefd687SGarrett Wollman */ 181cfefd687SGarrett Wollman vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 182bd7e5f99SJohn Dyson error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 183cfefd687SGarrett Wollman if (error) 184cfefd687SGarrett Wollman return (error); 18568940ac1SDavid Greenman } 186cfefd687SGarrett Wollman 187cfefd687SGarrett Wollman /* Fill in process VM information */ 188cfefd687SGarrett Wollman vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 189cfefd687SGarrett Wollman vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 190cfefd687SGarrett Wollman vmspace->vm_taddr = (caddr_t) virtual_offset; 191cfefd687SGarrett Wollman vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text; 192cfefd687SGarrett Wollman 193cfefd687SGarrett Wollman /* Fill in image_params */ 194c52007c2SDavid Greenman imgp->interpreted = 0; 195c52007c2SDavid Greenman imgp->entry_addr = a_out->a_entry; 196cfefd687SGarrett Wollman 197c52007c2SDavid Greenman imgp->proc->p_sysent = &aout_sysvec; 198c0e5de7dSDavid Greenman 199c0e5de7dSDavid Greenman /* Indicate that this file should not be modified */ 200c52007c2SDavid Greenman imgp->vp->v_flag |= VTEXT; 201c0e5de7dSDavid Greenman 202cfefd687SGarrett Wollman return (0); 203cfefd687SGarrett Wollman } 20492d91f76SGarrett Wollman 20592d91f76SGarrett Wollman /* 20692d91f76SGarrett Wollman * Tell kern_execve.c about it, with a little help from the linker. 20792d91f76SGarrett Wollman * Since `const' objects end up in the text segment, TEXT_SET is the 20892d91f76SGarrett Wollman * correct directive to use. 20992d91f76SGarrett Wollman */ 210f23b4c91SGarrett Wollman static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 21192d91f76SGarrett Wollman TEXT_SET(execsw_set, aout_execsw); 212