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