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.33 1997/02/22 09:38:55 peter Exp $ 27 */ 28 29 #include "opt_rlimit.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/resourcevar.h> 34 #include <sys/exec.h> 35 #include <sys/mman.h> 36 #include <sys/imgact.h> 37 #include <sys/imgact_aout.h> 38 #include <sys/kernel.h> 39 #include <sys/proc.h> 40 #include <sys/sysent.h> 41 #include <sys/vnode.h> 42 43 #include <vm/vm.h> 44 #include <vm/vm_param.h> 45 #include <vm/vm_prot.h> 46 #include <sys/lock.h> 47 #include <vm/pmap.h> 48 #include <vm/vm_map.h> 49 #include <vm/vm_extern.h> 50 51 static int exec_aout_imgact __P((struct image_params *imgp)); 52 53 static int 54 exec_aout_imgact(imgp) 55 struct image_params *imgp; 56 { 57 const struct exec *a_out = (const struct exec *) imgp->image_header; 58 struct vmspace *vmspace; 59 vm_offset_t vmaddr; 60 unsigned long virtual_offset; 61 unsigned long file_offset; 62 unsigned long bss_size; 63 int error; 64 65 /* 66 * Linux and *BSD binaries look very much alike, 67 * only the machine id is different: 68 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI. 69 * NetBSD is in network byte order.. ugh. 70 */ 71 if (((a_out->a_magic >> 16) & 0xff) != 0x86 && 72 ((a_out->a_magic >> 16) & 0xff) != 0 && 73 ((((int)ntohl(a_out->a_magic)) >> 16) & 0xff) != 0x86) 74 return -1; 75 76 /* 77 * Set file/virtual offset based on a.out variant. 78 * We do two cases: host byte order and network byte order 79 * (for NetBSD compatibility) 80 */ 81 switch ((int)(a_out->a_magic & 0xffff)) { 82 case ZMAGIC: 83 virtual_offset = 0; 84 if (a_out->a_text) { 85 file_offset = PAGE_SIZE; 86 } else { 87 /* Bill's "screwball mode" */ 88 file_offset = 0; 89 } 90 break; 91 case QMAGIC: 92 virtual_offset = PAGE_SIZE; 93 file_offset = 0; 94 break; 95 default: 96 /* NetBSD compatibility */ 97 switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { 98 case ZMAGIC: 99 case QMAGIC: 100 virtual_offset = PAGE_SIZE; 101 file_offset = 0; 102 break; 103 default: 104 return (-1); 105 } 106 } 107 108 bss_size = roundup(a_out->a_bss, PAGE_SIZE); 109 110 /* 111 * Check various fields in header for validity/bounds. 112 */ 113 if (/* entry point must lay with text region */ 114 a_out->a_entry < virtual_offset || 115 a_out->a_entry >= virtual_offset + a_out->a_text || 116 117 /* text and data size must each be page rounded */ 118 a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) 119 return (-1); 120 121 /* text + data can't exceed file size */ 122 if (a_out->a_data + a_out->a_text > imgp->attr->va_size) 123 return (EFAULT); 124 125 /* 126 * text/data/bss must not exceed limits 127 */ 128 if (/* text can't exceed maximum text size */ 129 a_out->a_text > MAXTSIZ || 130 131 /* data + bss can't exceed maximum data size */ 132 a_out->a_data + bss_size > MAXDSIZ || 133 134 /* data + bss can't exceed rlimit */ 135 a_out->a_data + bss_size > 136 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur) 137 return (ENOMEM); 138 139 /* copy in arguments and/or environment from old process */ 140 error = exec_extract_strings(imgp); 141 if (error) 142 return (error); 143 144 /* 145 * Destroy old process VM and create a new one (with a new stack) 146 */ 147 exec_new_vmspace(imgp); 148 149 /* 150 * The vm space can be changed by exec_new_vmspace 151 */ 152 vmspace = imgp->proc->p_vmspace; 153 154 /* 155 * Map text/data read/execute 156 */ 157 vmaddr = virtual_offset; 158 error = 159 vm_mmap(&vmspace->vm_map, /* map */ 160 &vmaddr, /* address */ 161 a_out->a_text + a_out->a_data, /* size */ 162 VM_PROT_READ | VM_PROT_EXECUTE, /* protection */ 163 VM_PROT_ALL, /* max protection */ 164 MAP_PRIVATE | MAP_FIXED, /* flags */ 165 (caddr_t)imgp->vp, /* vnode */ 166 file_offset); /* offset */ 167 if (error) 168 return (error); 169 170 /* 171 * allow writing of data 172 */ 173 vm_map_protect(&vmspace->vm_map, 174 vmaddr + a_out->a_text, 175 vmaddr + a_out->a_text + a_out->a_data, 176 VM_PROT_ALL, 177 FALSE); 178 179 if (bss_size != 0) { 180 /* 181 * Allocate demand-zeroed area for uninitialized data 182 * "bss" = 'block started by symbol' - named after the IBM 7090 183 * instruction of the same name. 184 */ 185 vmaddr = virtual_offset + a_out->a_text + a_out->a_data; 186 error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 187 if (error) 188 return (error); 189 } 190 191 /* Fill in process VM information */ 192 vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; 193 vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; 194 vmspace->vm_taddr = (caddr_t) virtual_offset; 195 vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text; 196 197 /* Fill in image_params */ 198 imgp->interpreted = 0; 199 imgp->entry_addr = a_out->a_entry; 200 201 imgp->proc->p_sysent = &aout_sysvec; 202 203 /* Indicate that this file should not be modified */ 204 imgp->vp->v_flag |= VTEXT; 205 206 return (0); 207 } 208 209 /* 210 * Tell kern_execve.c about it, with a little help from the linker. 211 * Since `const' objects end up in the text segment, TEXT_SET is the 212 * correct directive to use. 213 */ 214 static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" }; 215 TEXT_SET(execsw_set, aout_execsw); 216