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