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 REGENTS 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.11 1995/02/20 22:23:03 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(iparams) 48 struct image_params *iparams; 49 { 50 struct exec *a_out = (struct exec *) iparams->image_header; 51 struct vmspace *vmspace = iparams->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. 61 */ 62 if (((a_out->a_magic >> 16) & 0xff) != 0x86) 63 return -1; 64 #endif /* COMPAT_LINUX */ 65 66 /* 67 * Set file/virtual offset based on a.out variant. 68 * We do two cases: host byte order and network byte order 69 * (for NetBSD compatibility) 70 */ 71 switch ((int)(a_out->a_magic & 0xffff)) { 72 case ZMAGIC: 73 virtual_offset = 0; 74 if (a_out->a_text) { 75 file_offset = NBPG; 76 } else { 77 /* Bill's "screwball mode" */ 78 file_offset = 0; 79 } 80 break; 81 case QMAGIC: 82 virtual_offset = NBPG; 83 file_offset = 0; 84 break; 85 default: 86 /* NetBSD compatibility */ 87 switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 88 case ZMAGIC: 89 case QMAGIC: 90 virtual_offset = NBPG; 91 file_offset = 0; 92 break; 93 default: 94 return (-1); 95 } 96 } 97 98 bss_size = roundup(a_out->a_bss, NBPG); 99 100 /* 101 * Check various fields in header for validity/bounds. 102 */ 103 if (/* entry point must lay with text region */ 104 a_out->a_entry < virtual_offset || 105 a_out->a_entry >= virtual_offset + a_out->a_text || 106 107 /* text and data size must each be page rounded */ 108 a_out->a_text % NBPG || 109 a_out->a_data % NBPG) 110 return (-1); 111 112 /* text + data can't exceed file size */ 113 if (a_out->a_data + a_out->a_text > iparams->attr->va_size) 114 return (EFAULT); 115 116 /* 117 * text/data/bss must not exceed limits 118 */ 119 if (/* text can't exceed maximum text size */ 120 a_out->a_text > MAXTSIZ || 121 122 /* data + bss can't exceed maximum data size */ 123 a_out->a_data + bss_size > MAXDSIZ || 124 125 /* data + bss can't exceed rlimit */ 126 a_out->a_data + bss_size > 127 iparams->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 128 return (ENOMEM); 129 130 /* copy in arguments and/or environment from old process */ 131 error = exec_extract_strings(iparams); 132 if (error) 133 return (error); 134 135 /* 136 * Destroy old process VM and create a new one (with a new stack) 137 */ 138 exec_new_vmspace(iparams); 139 140 /* 141 * Map text read/execute 142 */ 143 vmaddr = virtual_offset; 144 error = 145 vm_mmap(&vmspace->vm_map, /* map */ 146 &vmaddr, /* address */ 147 a_out->a_text, /* size */ 148 VM_PROT_READ | VM_PROT_EXECUTE, /* protection */ 149 VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, /* max protection */ 150 MAP_PRIVATE | MAP_FIXED, /* flags */ 151 (caddr_t)iparams->vnodep, /* vnode */ 152 file_offset); /* offset */ 153 if (error) 154 return (error); 155 156 /* 157 * Map data read/write (if text is 0, assume text is in data area 158 * [Bill's screwball mode]) 159 */ 160 vmaddr = virtual_offset + a_out->a_text; 161 error = 162 vm_mmap(&vmspace->vm_map, 163 &vmaddr, 164 a_out->a_data, 165 VM_PROT_READ | VM_PROT_WRITE | (a_out->a_text ? 0 : VM_PROT_EXECUTE), 166 VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, 167 (caddr_t) iparams->vnodep, 168 file_offset + a_out->a_text); 169 if (error) 170 return (error); 171 172 if (bss_size != 0) { 173 /* 174 * Allocate demand-zeroed area for uninitialized data 175 * "bss" = 'block started by symbol' - named after the IBM 7090 176 * instruction of the same name. 177 */ 178 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 179 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE); 180 if (error) 181 return (error); 182 } 183 184 /* Fill in process VM information */ 185 vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 186 vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 187 vmspace->vm_taddr = (caddr_t) virtual_offset; 188 vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text; 189 190 /* Fill in image_params */ 191 iparams->interpreted = 0; 192 iparams->entry_addr = a_out->a_entry; 193 194 iparams->proc->p_sysent = &aout_sysvec; 195 return (0); 196 } 197 198 /* 199 * Tell kern_execve.c about it, with a little help from the linker. 200 * Since `const' objects end up in the text segment, TEXT_SET is the 201 * correct directive to use. 202 */ 203 static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 204 TEXT_SET(execsw_set, aout_execsw); 205 206