1 1.43 riastrad /* $NetBSD: exec_aout.c,v 1.43 2024/12/06 16:19:41 riastradh Exp $ */ 2 1.10 cgd 3 1.1 cgd /* 4 1.8 cgd * Copyright (c) 1993, 1994 Christopher G. Demetriou 5 1.1 cgd * All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.1 cgd * 3. All advertising materials mentioning features or use of this software 16 1.1 cgd * must display the following acknowledgement: 17 1.1 cgd * This product includes software developed by Christopher G. Demetriou. 18 1.1 cgd * 4. The name of the author may not be used to endorse or promote products 19 1.9 jtc * derived from this software without specific prior written permission 20 1.1 cgd * 21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 1.1 cgd */ 32 1.22 lukem 33 1.22 lukem #include <sys/cdefs.h> 34 1.43 riastrad __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.43 2024/12/06 16:19:41 riastradh Exp $"); 35 1.1 cgd 36 1.2 deraadt #include <sys/param.h> 37 1.42 riastrad #include <sys/types.h> 38 1.42 riastrad 39 1.2 deraadt #include <sys/exec.h> 40 1.26 thorpej #include <sys/exec_aout.h> 41 1.42 riastrad #include <sys/module.h> 42 1.42 riastrad #include <sys/proc.h> 43 1.2 deraadt #include <sys/resourcevar.h> 44 1.43 riastrad #include <sys/sdt.h> 45 1.42 riastrad #include <sys/systm.h> 46 1.42 riastrad #include <sys/vnode.h> 47 1.1 cgd 48 1.20 thorpej #include <uvm/uvm_extern.h> 49 1.20 thorpej 50 1.41 pgoyette MODULE(MODULE_CLASS_EXEC, exec_aout, NULL); 51 1.34 ad 52 1.39 christos static struct execsw exec_aout_execsw = { 53 1.39 christos .es_hdrsz = sizeof(struct exec), 54 1.39 christos .es_makecmds = exec_aout_makecmds, 55 1.39 christos .u = { 56 1.39 christos .elf_probe_func = NULL, 57 1.39 christos }, 58 1.40 matt .es_emul = &emul_netbsd, 59 1.39 christos .es_prio = EXECSW_PRIO_ANY, 60 1.39 christos .es_arglen = 0, 61 1.39 christos .es_copyargs = copyargs, 62 1.39 christos .es_setregs = NULL, 63 1.39 christos .es_coredump = coredump_netbsd, 64 1.39 christos .es_setup_stack = exec_setup_stack, 65 1.34 ad }; 66 1.34 ad 67 1.34 ad static int 68 1.34 ad exec_aout_modcmd(modcmd_t cmd, void *arg) 69 1.34 ad { 70 1.34 ad 71 1.34 ad switch (cmd) { 72 1.34 ad case MODULE_CMD_INIT: 73 1.39 christos return exec_add(&exec_aout_execsw, 1); 74 1.34 ad 75 1.34 ad case MODULE_CMD_FINI: 76 1.39 christos return exec_remove(&exec_aout_execsw, 1); 77 1.34 ad 78 1.34 ad default: 79 1.43 riastrad return SET_ERROR(ENOTTY); 80 1.34 ad } 81 1.34 ad } 82 1.34 ad 83 1.1 cgd /* 84 1.1 cgd * exec_aout_makecmds(): Check if it's an a.out-format executable. 85 1.1 cgd * 86 1.33 christos * Given a lwp pointer and an exec package pointer, see if the referent 87 1.1 cgd * of the epp is in a.out format. First check 'standard' magic numbers for 88 1.31 wiz * this architecture. If that fails, try a CPU-dependent hook. 89 1.1 cgd * 90 1.1 cgd * This function, in the former case, or the hook, in the latter, is 91 1.1 cgd * responsible for creating a set of vmcmds which can be used to build 92 1.1 cgd * the process's vm space and inserting them into the exec package. 93 1.1 cgd */ 94 1.1 cgd 95 1.1 cgd int 96 1.33 christos exec_aout_makecmds(struct lwp *l, struct exec_package *epp) 97 1.1 cgd { 98 1.1 cgd u_long midmag, magic; 99 1.1 cgd u_short mid; 100 1.1 cgd int error; 101 1.8 cgd struct exec *execp = epp->ep_hdr; 102 1.1 cgd 103 1.8 cgd if (epp->ep_hdrvalid < sizeof(struct exec)) 104 1.43 riastrad return SET_ERROR(ENOEXEC); 105 1.8 cgd 106 1.8 cgd midmag = ntohl(execp->a_midmag); 107 1.1 cgd mid = (midmag >> 16) & 0x3ff; 108 1.1 cgd magic = midmag & 0xffff; 109 1.1 cgd 110 1.1 cgd midmag = mid << 16 | magic; 111 1.1 cgd 112 1.1 cgd switch (midmag) { 113 1.1 cgd case (MID_MACHINE << 16) | ZMAGIC: 114 1.33 christos error = exec_aout_prep_zmagic(l, epp); 115 1.1 cgd break; 116 1.2 deraadt case (MID_MACHINE << 16) | NMAGIC: 117 1.33 christos error = exec_aout_prep_nmagic(l, epp); 118 1.2 deraadt break; 119 1.1 cgd case (MID_MACHINE << 16) | OMAGIC: 120 1.33 christos error = exec_aout_prep_omagic(l, epp); 121 1.2 deraadt break; 122 1.1 cgd default: 123 1.33 christos error = cpu_exec_aout_makecmds(l, epp); 124 1.1 cgd } 125 1.1 cgd 126 1.3 cgd if (error) 127 1.6 cgd kill_vmcmds(&epp->ep_vmcmds); 128 1.38 christos else 129 1.38 christos epp->ep_flags &= ~EXEC_TOPDOWN_VM; 130 1.1 cgd 131 1.1 cgd return error; 132 1.1 cgd } 133 1.1 cgd 134 1.1 cgd /* 135 1.1 cgd * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package 136 1.1 cgd * 137 1.1 cgd * First, set of the various offsets/lengths in the exec package. 138 1.1 cgd * 139 1.1 cgd * Then, mark the text image busy (so it can be demand paged) or error 140 1.1 cgd * out if this is not possible. Finally, set up vmcmds for the 141 1.1 cgd * text, data, bss, and stack segments. 142 1.1 cgd */ 143 1.1 cgd 144 1.1 cgd int 145 1.33 christos exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp) 146 1.1 cgd { 147 1.8 cgd struct exec *execp = epp->ep_hdr; 148 1.25 chs int error; 149 1.1 cgd 150 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ; 151 1.1 cgd epp->ep_tsize = execp->a_text; 152 1.1 cgd epp->ep_daddr = epp->ep_taddr + execp->a_text; 153 1.1 cgd epp->ep_dsize = execp->a_data + execp->a_bss; 154 1.1 cgd epp->ep_entry = execp->a_entry; 155 1.1 cgd 156 1.25 chs error = vn_marktext(epp->ep_vp); 157 1.25 chs if (error) 158 1.25 chs return (error); 159 1.1 cgd 160 1.1 cgd /* set up command for text segment */ 161 1.17 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text), 162 1.3 cgd epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE); 163 1.1 cgd 164 1.1 cgd /* set up command for data segment */ 165 1.17 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data), 166 1.3 cgd epp->ep_daddr, epp->ep_vp, execp->a_text, 167 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 168 1.1 cgd 169 1.1 cgd /* set up command for bss segment */ 170 1.23 chs if (execp->a_bss > 0) 171 1.23 chs NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 172 1.23 chs epp->ep_daddr + execp->a_data, NULLVP, 0, 173 1.23 chs VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 174 1.2 deraadt 175 1.33 christos return (*epp->ep_esch->es_setup_stack)(l, epp); 176 1.2 deraadt } 177 1.2 deraadt 178 1.2 deraadt /* 179 1.2 deraadt * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package 180 1.2 deraadt */ 181 1.2 deraadt 182 1.2 deraadt int 183 1.33 christos exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp) 184 1.2 deraadt { 185 1.8 cgd struct exec *execp = epp->ep_hdr; 186 1.2 deraadt long bsize, baddr; 187 1.2 deraadt 188 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ; 189 1.2 deraadt epp->ep_tsize = execp->a_text; 190 1.27 thorpej epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ); 191 1.2 deraadt epp->ep_dsize = execp->a_data + execp->a_bss; 192 1.2 deraadt epp->ep_entry = execp->a_entry; 193 1.2 deraadt 194 1.2 deraadt /* set up command for text segment */ 195 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 196 1.3 cgd epp->ep_taddr, epp->ep_vp, sizeof(struct exec), 197 1.3 cgd VM_PROT_READ|VM_PROT_EXECUTE); 198 1.2 deraadt 199 1.2 deraadt /* set up command for data segment */ 200 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 201 1.3 cgd epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec), 202 1.3 cgd VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 203 1.2 deraadt 204 1.2 deraadt /* set up command for bss segment */ 205 1.20 thorpej baddr = round_page(epp->ep_daddr + execp->a_data); 206 1.2 deraadt bsize = epp->ep_daddr + epp->ep_dsize - baddr; 207 1.3 cgd if (bsize > 0) 208 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 209 1.5 mycroft NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 210 1.2 deraadt 211 1.33 christos return (*epp->ep_esch->es_setup_stack)(l, epp); 212 1.2 deraadt } 213 1.2 deraadt 214 1.2 deraadt /* 215 1.2 deraadt * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package 216 1.2 deraadt */ 217 1.2 deraadt 218 1.2 deraadt int 219 1.33 christos exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp) 220 1.2 deraadt { 221 1.8 cgd struct exec *execp = epp->ep_hdr; 222 1.13 pk long dsize, bsize, baddr; 223 1.2 deraadt 224 1.27 thorpej epp->ep_taddr = AOUT_LDPGSZ; 225 1.2 deraadt epp->ep_tsize = execp->a_text; 226 1.2 deraadt epp->ep_daddr = epp->ep_taddr + execp->a_text; 227 1.2 deraadt epp->ep_dsize = execp->a_data + execp->a_bss; 228 1.2 deraadt epp->ep_entry = execp->a_entry; 229 1.2 deraadt 230 1.2 deraadt /* set up command for text and data segments */ 231 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 232 1.3 cgd execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 233 1.3 cgd sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 234 1.2 deraadt 235 1.2 deraadt /* set up command for bss segment */ 236 1.20 thorpej baddr = round_page(epp->ep_daddr + execp->a_data); 237 1.2 deraadt bsize = epp->ep_daddr + epp->ep_dsize - baddr; 238 1.3 cgd if (bsize > 0) 239 1.3 cgd NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 240 1.5 mycroft NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 241 1.2 deraadt 242 1.13 pk /* 243 1.13 pk * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 244 1.13 pk * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 245 1.13 pk * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 246 1.13 pk * respectively to page boundaries. 247 1.13 pk * Compensate `ep_dsize' for the amount of data covered by the last 248 1.32 perry * text page. 249 1.13 pk */ 250 1.20 thorpej dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text); 251 1.13 pk epp->ep_dsize = (dsize > 0) ? dsize : 0; 252 1.33 christos return (*epp->ep_esch->es_setup_stack)(l, epp); 253 1.1 cgd } 254