1 /* $NetBSD: linux_exec_aout.c,v 1.64 2007/12/08 18:36:07 dsl 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.64 2007/12/08 18:36:07 dsl 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 <sys/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(struct lwp *, struct exec_package *, 73 struct ps_strings *, char **, void *); 74 75 static int exec_linux_aout_prep_zmagic(struct lwp *, 76 struct exec_package *); 77 static int exec_linux_aout_prep_nmagic(struct lwp *, 78 struct exec_package *); 79 static int exec_linux_aout_prep_omagic(struct lwp *, 80 struct exec_package *); 81 static int exec_linux_aout_prep_qmagic(struct lwp *, 82 struct exec_package *); 83 84 int 85 linux_aout_copyargs(struct lwp *l, struct exec_package *pack, 86 struct ps_strings *arginfo, char **stackp, void *argp) 87 { 88 char **cpp = (char **)*stackp; 89 char **stk = (char **)*stackp; 90 char *dp, *sp; 91 size_t len; 92 void *nullp = NULL; 93 int argc = arginfo->ps_nargvstr; 94 int envc = arginfo->ps_nenvstr; 95 int error; 96 97 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) 98 return error; 99 100 /* leave room for envp and argv */ 101 cpp += 2; 102 if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0) 103 return error; 104 105 dp = (char *) (cpp + argc + envc + 2); 106 sp = argp; 107 108 /* XXX don't copy them out, remap them! */ 109 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 110 111 for (; --argc >= 0; sp += len, dp += len) 112 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 113 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 114 return error; 115 116 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 117 return error; 118 119 if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0) 120 return error; 121 122 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 123 124 for (; --envc >= 0; sp += len, dp += len) 125 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 126 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 127 return error; 128 129 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 130 return error; 131 132 *stackp = (char *)cpp; 133 return 0; 134 } 135 136 int 137 exec_linux_aout_makecmds(struct lwp *l, struct exec_package *epp) 138 { 139 struct exec *linux_ep = epp->ep_hdr; 140 int machtype, magic; 141 int error = ENOEXEC; 142 143 magic = LINUX_N_MAGIC(linux_ep); 144 machtype = LINUX_N_MACHTYPE(linux_ep); 145 146 147 if (machtype != LINUX_MID_MACHINE) 148 return (ENOEXEC); 149 150 switch (magic) { 151 case QMAGIC: 152 error = exec_linux_aout_prep_qmagic(l, epp); 153 break; 154 case ZMAGIC: 155 error = exec_linux_aout_prep_zmagic(l, epp); 156 break; 157 case NMAGIC: 158 error = exec_linux_aout_prep_nmagic(l, epp); 159 break; 160 case OMAGIC: 161 error = exec_linux_aout_prep_omagic(l, epp); 162 break; 163 } 164 return error; 165 } 166 167 /* 168 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400 169 * is very likely not page aligned on most architectures, it is treated 170 * as an NMAGIC here. XXX 171 */ 172 173 static int 174 exec_linux_aout_prep_zmagic(struct lwp *l, struct exec_package *epp) 175 { 176 struct exec *execp = epp->ep_hdr; 177 178 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC); 179 epp->ep_tsize = execp->a_text; 180 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC); 181 epp->ep_dsize = execp->a_data + execp->a_bss; 182 epp->ep_entry = execp->a_entry; 183 184 /* set up command for text segment */ 185 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 186 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC), 187 VM_PROT_READ|VM_PROT_EXECUTE); 188 189 /* set up command for data segment */ 190 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 191 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC), 192 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 193 194 /* set up command for bss segment */ 195 if (execp->a_bss) 196 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 197 epp->ep_daddr + execp->a_data, NULLVP, 0, 198 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 199 200 return (*epp->ep_esch->es_setup_stack)(l, epp); 201 } 202 203 /* 204 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package. 205 * Not different from the normal stuff. 206 */ 207 208 static int 209 exec_linux_aout_prep_nmagic(struct lwp *l, struct exec_package *epp) 210 { 211 struct exec *execp = epp->ep_hdr; 212 long bsize, baddr; 213 214 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC); 215 epp->ep_tsize = execp->a_text; 216 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC); 217 epp->ep_dsize = execp->a_data + execp->a_bss; 218 epp->ep_entry = execp->a_entry; 219 220 /* set up command for text segment */ 221 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 222 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC), 223 VM_PROT_READ|VM_PROT_EXECUTE); 224 225 /* set up command for data segment */ 226 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 227 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC), 228 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 229 230 /* set up command for bss segment */ 231 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 232 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 233 if (bsize > 0) 234 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 235 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 236 237 return (*epp->ep_esch->es_setup_stack)(l, epp); 238 } 239 240 /* 241 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package. 242 * Business as usual. 243 */ 244 245 static int 246 exec_linux_aout_prep_omagic(struct lwp *l, struct exec_package *epp) 247 { 248 struct exec *execp = epp->ep_hdr; 249 long dsize, bsize, baddr; 250 251 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC); 252 epp->ep_tsize = execp->a_text; 253 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC); 254 epp->ep_dsize = execp->a_data + execp->a_bss; 255 epp->ep_entry = execp->a_entry; 256 257 /* set up command for text and data segments */ 258 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 259 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 260 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 261 262 /* set up command for bss segment */ 263 baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE); 264 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 265 if (bsize > 0) 266 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 267 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 268 269 /* 270 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 271 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 272 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 273 * respectively to page boundaries. 274 * Compensate `ep_dsize' for the amount of data covered by the last 275 * text page. 276 */ 277 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, 278 PAGE_SIZE); 279 epp->ep_dsize = (dsize > 0) ? dsize : 0; 280 return (*epp->ep_esch->es_setup_stack)(l, epp); 281 } 282 283 static int 284 exec_linux_aout_prep_qmagic(struct lwp *l, struct exec_package *epp) 285 { 286 struct exec *execp = epp->ep_hdr; 287 int error; 288 289 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC); 290 epp->ep_tsize = execp->a_text; 291 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC); 292 epp->ep_dsize = execp->a_data + execp->a_bss; 293 epp->ep_entry = execp->a_entry; 294 295 error = vn_marktext(epp->ep_vp); 296 if (error) 297 return (error); 298 299 /* set up command for text segment */ 300 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 301 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC), 302 VM_PROT_READ|VM_PROT_EXECUTE); 303 304 /* set up command for data segment */ 305 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 306 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC), 307 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 308 309 /* set up command for bss segment */ 310 if (execp->a_bss) 311 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 312 epp->ep_daddr + execp->a_data, NULLVP, 0, 313 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 314 315 return (*epp->ep_esch->es_setup_stack)(l, epp); 316 } 317