1 /* $NetBSD: linux_exec_aout.c,v 1.50 2002/10/05 22:34:04 chs Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * based on exec_aout.c, sunos_exec.c and svr4_exec.c 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.50 2002/10/05 22:34:04 chs Exp $"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/proc.h> 50 #include <sys/malloc.h> 51 #include <sys/namei.h> 52 #include <sys/vnode.h> 53 #include <sys/mount.h> 54 #include <sys/exec.h> 55 #include <sys/exec_elf.h> 56 57 #include <sys/mman.h> 58 #include <sys/syscallargs.h> 59 60 #include <machine/cpu.h> 61 #include <machine/reg.h> 62 63 #include <compat/linux/common/linux_types.h> 64 #include <compat/linux/common/linux_signal.h> 65 #include <compat/linux/common/linux_util.h> 66 #include <compat/linux/common/linux_exec.h> 67 #include <compat/linux/common/linux_machdep.h> 68 69 #include <compat/linux/linux_syscallargs.h> 70 #include <compat/linux/linux_syscall.h> 71 72 int linux_aout_copyargs __P((struct proc *, struct exec_package *, 73 struct ps_strings *, char **, void *)); 74 75 static int exec_linux_aout_prep_zmagic __P((struct proc *, 76 struct exec_package *)); 77 static int exec_linux_aout_prep_nmagic __P((struct proc *, 78 struct exec_package *)); 79 static int exec_linux_aout_prep_omagic __P((struct proc *, 80 struct exec_package *)); 81 static int exec_linux_aout_prep_qmagic __P((struct proc *, 82 struct exec_package *)); 83 84 int 85 linux_aout_copyargs(p, pack, arginfo, stackp, argp) 86 struct proc *p; 87 struct exec_package *pack; 88 struct ps_strings *arginfo; 89 char **stackp; 90 void *argp; 91 { 92 char **cpp = (char **)*stackp; 93 char **stk = (char **)*stackp; 94 char *dp, *sp; 95 size_t len; 96 void *nullp = NULL; 97 int argc = arginfo->ps_nargvstr; 98 int envc = arginfo->ps_nenvstr; 99 int error; 100 101 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) 102 return error; 103 104 /* leave room for envp and argv */ 105 cpp += 2; 106 if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0) 107 return error; 108 109 dp = (char *) (cpp + argc + envc + 2); 110 sp = argp; 111 112 /* XXX don't copy them out, remap them! */ 113 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 114 115 for (; --argc >= 0; sp += len, dp += len) 116 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 117 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 118 return error; 119 120 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 121 return error; 122 123 if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0) 124 return error; 125 126 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 127 128 for (; --envc >= 0; sp += len, dp += len) 129 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 130 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 131 return error; 132 133 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 134 return error; 135 136 *stackp = (char *)cpp; 137 return 0; 138 } 139 140 int 141 exec_linux_aout_makecmds(p, epp) 142 struct proc *p; 143 struct exec_package *epp; 144 { 145 struct exec *linux_ep = epp->ep_hdr; 146 int machtype, magic; 147 int error = ENOEXEC; 148 149 magic = LINUX_N_MAGIC(linux_ep); 150 machtype = LINUX_N_MACHTYPE(linux_ep); 151 152 153 if (machtype != LINUX_MID_MACHINE) 154 return (ENOEXEC); 155 156 switch (magic) { 157 case QMAGIC: 158 error = exec_linux_aout_prep_qmagic(p, epp); 159 break; 160 case ZMAGIC: 161 error = exec_linux_aout_prep_zmagic(p, epp); 162 break; 163 case NMAGIC: 164 error = exec_linux_aout_prep_nmagic(p, epp); 165 break; 166 case OMAGIC: 167 error = exec_linux_aout_prep_omagic(p, epp); 168 break; 169 } 170 return error; 171 } 172 173 /* 174 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400 175 * is very likely not page aligned on most architectures, it is treated 176 * as an NMAGIC here. XXX 177 */ 178 179 static int 180 exec_linux_aout_prep_zmagic(p, epp) 181 struct proc *p; 182 struct exec_package *epp; 183 { 184 struct exec *execp = epp->ep_hdr; 185 186 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC); 187 epp->ep_tsize = execp->a_text; 188 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC); 189 epp->ep_dsize = execp->a_data + execp->a_bss; 190 epp->ep_entry = execp->a_entry; 191 192 /* set up command for text segment */ 193 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 194 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC), 195 VM_PROT_READ|VM_PROT_EXECUTE); 196 197 /* set up command for data segment */ 198 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 199 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC), 200 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 201 202 /* set up command for bss segment */ 203 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 204 epp->ep_daddr + execp->a_data, NULLVP, 0, 205 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 206 207 return exec_aout_setup_stack(p, epp); 208 } 209 210 /* 211 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package. 212 * Not different from the normal stuff. 213 */ 214 215 static int 216 exec_linux_aout_prep_nmagic(p, epp) 217 struct proc *p; 218 struct exec_package *epp; 219 { 220 struct exec *execp = epp->ep_hdr; 221 long bsize, baddr; 222 223 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC); 224 epp->ep_tsize = execp->a_text; 225 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC); 226 epp->ep_dsize = execp->a_data + execp->a_bss; 227 epp->ep_entry = execp->a_entry; 228 229 /* set up command for text segment */ 230 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 231 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC), 232 VM_PROT_READ|VM_PROT_EXECUTE); 233 234 /* set up command for data segment */ 235 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 236 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC), 237 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 238 239 /* set up command for bss segment */ 240 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG); 241 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 242 if (bsize > 0) 243 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 244 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 245 246 return exec_aout_setup_stack(p, epp); 247 } 248 249 /* 250 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package. 251 * Business as usual. 252 */ 253 254 static int 255 exec_linux_aout_prep_omagic(p, epp) 256 struct proc *p; 257 struct exec_package *epp; 258 { 259 struct exec *execp = epp->ep_hdr; 260 long dsize, bsize, baddr; 261 262 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC); 263 epp->ep_tsize = execp->a_text; 264 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC); 265 epp->ep_dsize = execp->a_data + execp->a_bss; 266 epp->ep_entry = execp->a_entry; 267 268 /* set up command for text and data segments */ 269 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 270 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 271 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 272 273 /* set up command for bss segment */ 274 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG); 275 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 276 if (bsize > 0) 277 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 278 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 279 280 /* 281 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 282 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 283 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 284 * respectively to page boundaries. 285 * Compensate `ep_dsize' for the amount of data covered by the last 286 * text page. 287 */ 288 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG); 289 epp->ep_dsize = (dsize > 0) ? dsize : 0; 290 return exec_aout_setup_stack(p, epp); 291 } 292 293 static int 294 exec_linux_aout_prep_qmagic(p, epp) 295 struct proc *p; 296 struct exec_package *epp; 297 { 298 struct exec *execp = epp->ep_hdr; 299 int error; 300 301 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC); 302 epp->ep_tsize = execp->a_text; 303 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC); 304 epp->ep_dsize = execp->a_data + execp->a_bss; 305 epp->ep_entry = execp->a_entry; 306 307 error = vn_marktext(epp->ep_vp); 308 if (error) 309 return (error); 310 311 /* set up command for text segment */ 312 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 313 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC), 314 VM_PROT_READ|VM_PROT_EXECUTE); 315 316 /* set up command for data segment */ 317 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 318 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC), 319 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 320 321 /* set up command for bss segment */ 322 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 323 epp->ep_daddr + execp->a_data, NULLVP, 0, 324 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 325 326 return exec_aout_setup_stack(p, epp); 327 } 328