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