xref: /freebsd/sys/kern/imgact_aout.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
19454b2d8SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4cfefd687SGarrett Wollman  * Copyright (c) 1993, David Greenman
5cfefd687SGarrett Wollman  * All rights reserved.
6cfefd687SGarrett Wollman  *
7cfefd687SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
8cfefd687SGarrett Wollman  * modification, are permitted provided that the following conditions
9cfefd687SGarrett Wollman  * are met:
10cfefd687SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
11cfefd687SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
12cfefd687SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
13cfefd687SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
14cfefd687SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
15cfefd687SGarrett Wollman  *
16cfefd687SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17cfefd687SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18cfefd687SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191984b014SDavid Greenman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20cfefd687SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21cfefd687SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22cfefd687SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23cfefd687SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24cfefd687SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25cfefd687SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26cfefd687SGarrett Wollman  * SUCH DAMAGE.
27cfefd687SGarrett Wollman  */
28cfefd687SGarrett Wollman 
2926f9a767SRodney W. Grimes #include <sys/param.h>
3026f9a767SRodney W. Grimes #include <sys/exec.h>
3126f9a767SRodney W. Grimes #include <sys/imgact.h>
32bc6d7444SDavid Greenman #include <sys/imgact_aout.h>
3326f9a767SRodney W. Grimes #include <sys/kernel.h>
347332c129SKonstantin Belousov #include <sys/limits.h>
35fb919e4dSMark Murray #include <sys/lock.h>
36e5d6cd0cSBruce Evans #include <sys/malloc.h>
37fb919e4dSMark Murray #include <sys/mutex.h>
38a794e791SBruce Evans #include <sys/proc.h>
391ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
40fb919e4dSMark Murray #include <sys/resourcevar.h>
4122d4b0fbSJohn Polstra #include <sys/signalvar.h>
4222d4b0fbSJohn Polstra #include <sys/syscall.h>
43e5d6cd0cSBruce Evans #include <sys/sysent.h>
44e5d6cd0cSBruce Evans #include <sys/systm.h>
45a794e791SBruce Evans #include <sys/vnode.h>
46fb919e4dSMark Murray 
47710ded3aSPeter Wemm #include <machine/frame.h>
48e5d6cd0cSBruce Evans #include <machine/md_var.h>
49cfefd687SGarrett Wollman 
5026f9a767SRodney W. Grimes #include <vm/vm.h>
51efeaf95aSDavid Greenman #include <vm/pmap.h>
52efeaf95aSDavid Greenman #include <vm/vm_map.h>
531616db3cSJohn Dyson #include <vm/vm_object.h>
54e5d6cd0cSBruce Evans #include <vm/vm_param.h>
55cfefd687SGarrett Wollman 
567332c129SKonstantin Belousov #ifdef __amd64__
577332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_signal.h>
587332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
597332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h>
607332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h>
617332c129SKonstantin Belousov #include <compat/ia32/ia32_signal.h>
627332c129SKonstantin Belousov #endif
637332c129SKonstantin Belousov 
644d77a549SAlfred Perlstein static int	exec_aout_imgact(struct image_params *imgp);
6531174518SJohn Baldwin static int	aout_fixup(uintptr_t *stack_base, struct image_params *imgp);
667ee050b7SBruce Evans 
67d86c1f0dSKonstantin Belousov #define	AOUT32_USRSTACK		0xbfc00000
68d86c1f0dSKonstantin Belousov 
697332c129SKonstantin Belousov #if defined(__i386__)
70d86c1f0dSKonstantin Belousov 
71d86c1f0dSKonstantin Belousov #define	AOUT32_PS_STRINGS	(AOUT32_USRSTACK - sizeof(struct ps_strings))
72d86c1f0dSKonstantin Belousov 
7322d4b0fbSJohn Polstra struct sysentvec aout_sysvec = {
74a8d403e1SKonstantin Belousov 	.sv_size	= SYS_MAXSYSCALL,
75a8d403e1SKonstantin Belousov 	.sv_table	= sysent,
76a8d403e1SKonstantin Belousov 	.sv_fixup	= aout_fixup,
77a8d403e1SKonstantin Belousov 	.sv_sendsig	= sendsig,
78a8d403e1SKonstantin Belousov 	.sv_sigcode	= sigcode,
79a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &szsigcode,
80a8d403e1SKonstantin Belousov 	.sv_name	= "FreeBSD a.out",
81a8d403e1SKonstantin Belousov 	.sv_coredump	= NULL,
82a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= MINSIGSTKSZ,
83a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
84d86c1f0dSKonstantin Belousov 	.sv_maxuser	= AOUT32_USRSTACK,
85d86c1f0dSKonstantin Belousov 	.sv_usrstack	= AOUT32_USRSTACK,
86d86c1f0dSKonstantin Belousov 	.sv_psstrings	= AOUT32_PS_STRINGS,
873fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
88a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
89a8d403e1SKonstantin Belousov 	.sv_copyout_strings	= exec_copyout_strings,
90a8d403e1SKonstantin Belousov 	.sv_setregs	= exec_setregs,
91a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
92b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
93*b82b4ae7SKonstantin Belousov 	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32 |
94*b82b4ae7SKonstantin Belousov 			    SV_SIGSYS,
95afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = cpu_set_syscall_retval,
96afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
97afe1a688SKonstantin Belousov 	.sv_syscallnames = syscallnames,
98e5d81ef1SDmitry Chagin 	.sv_schedtail	= NULL,
9991d1786fSDmitry Chagin 	.sv_thread_detach = NULL,
100038c7205SDmitry Chagin 	.sv_trap	= NULL,
10128a66fc3SKonstantin Belousov 	.sv_onexec_old = exec_onexec_old,
10228a66fc3SKonstantin Belousov 	.sv_onexit =  exit_onexit,
103de8374dfSDmitry Chagin 	.sv_set_fork_retval = x86_set_fork_retval,
10422d4b0fbSJohn Polstra };
10522d4b0fbSJohn Polstra 
1067332c129SKonstantin Belousov #elif defined(__amd64__)
1077332c129SKonstantin Belousov 
10898c8b625SKonstantin Belousov #include "vdso_ia32_offsets.h"
10998c8b625SKonstantin Belousov 
11098c8b625SKonstantin Belousov extern const char _binary_elf_vdso32_so_1_start[];
11198c8b625SKonstantin Belousov extern const char _binary_elf_vdso32_so_1_end[];
11298c8b625SKonstantin Belousov extern char _binary_elf_vdso32_so_1_size;
11398c8b625SKonstantin Belousov 
1147332c129SKonstantin Belousov #define	AOUT32_PS_STRINGS \
1157332c129SKonstantin Belousov     (AOUT32_USRSTACK - sizeof(struct freebsd32_ps_strings))
116481af8b9SKonstantin Belousov #define	AOUT32_MINUSER		FREEBSD32_MINUSER
1177332c129SKonstantin Belousov 
1187332c129SKonstantin Belousov extern const char *freebsd32_syscallnames[];
1197332c129SKonstantin Belousov extern u_long ia32_maxssiz;
1207332c129SKonstantin Belousov 
12198c8b625SKonstantin Belousov static int aout_szsigcode;
12298c8b625SKonstantin Belousov 
1237332c129SKonstantin Belousov struct sysentvec aout_sysvec = {
1247332c129SKonstantin Belousov 	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
1257332c129SKonstantin Belousov 	.sv_table	= freebsd32_sysent,
1267332c129SKonstantin Belousov 	.sv_fixup	= aout_fixup,
1277332c129SKonstantin Belousov 	.sv_sendsig	= ia32_sendsig,
12898c8b625SKonstantin Belousov 	.sv_sigcode	= _binary_elf_vdso32_so_1_start,
12998c8b625SKonstantin Belousov 	.sv_szsigcode	= &aout_szsigcode,
1307332c129SKonstantin Belousov 	.sv_name	= "FreeBSD a.out",
1317332c129SKonstantin Belousov 	.sv_coredump	= NULL,
1327332c129SKonstantin Belousov 	.sv_minsigstksz	= MINSIGSTKSZ,
133481af8b9SKonstantin Belousov 	.sv_minuser	= AOUT32_MINUSER,
1347332c129SKonstantin Belousov 	.sv_maxuser	= AOUT32_USRSTACK,
1357332c129SKonstantin Belousov 	.sv_usrstack	= AOUT32_USRSTACK,
1367332c129SKonstantin Belousov 	.sv_psstrings	= AOUT32_PS_STRINGS,
1373fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct freebsd32_ps_strings),
1387332c129SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
1397332c129SKonstantin Belousov 	.sv_copyout_strings	= freebsd32_copyout_strings,
1407332c129SKonstantin Belousov 	.sv_setregs	= ia32_setregs,
1417332c129SKonstantin Belousov 	.sv_fixlimit	= ia32_fixlimit,
1427332c129SKonstantin Belousov 	.sv_maxssiz	= &ia32_maxssiz,
143*b82b4ae7SKonstantin Belousov 	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32 |
144*b82b4ae7SKonstantin Belousov 			    SV_SIGSYS,
1457332c129SKonstantin Belousov 	.sv_set_syscall_retval = ia32_set_syscall_retval,
1467332c129SKonstantin Belousov 	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
1477332c129SKonstantin Belousov 	.sv_syscallnames = freebsd32_syscallnames,
14828a66fc3SKonstantin Belousov 	.sv_onexec_old	= exec_onexec_old,
14928a66fc3SKonstantin Belousov 	.sv_onexit	= exit_onexit,
150de8374dfSDmitry Chagin 	.sv_set_fork_retval = x86_set_fork_retval,
1517332c129SKonstantin Belousov };
15298c8b625SKonstantin Belousov 
15398c8b625SKonstantin Belousov static void
aout_sysent(void * arg __unused)15498c8b625SKonstantin Belousov aout_sysent(void *arg __unused)
15598c8b625SKonstantin Belousov {
15698c8b625SKonstantin Belousov 	aout_szsigcode = (int)(uintptr_t)&_binary_elf_vdso32_so_1_size;
15798c8b625SKonstantin Belousov }
15898c8b625SKonstantin Belousov SYSINIT(aout_sysent, SI_SUB_EXEC, SI_ORDER_ANY, aout_sysent, NULL);
1597332c129SKonstantin Belousov #else
1609da5257eSKonstantin Belousov #error "Only ia32 arch is supported"
1617332c129SKonstantin Belousov #endif
1627332c129SKonstantin Belousov 
163b9e91a85SBruce Evans static int
aout_fixup(uintptr_t * stack_base,struct image_params * imgp)16431174518SJohn Baldwin aout_fixup(uintptr_t *stack_base, struct image_params *imgp)
165f36ba452SJake Burkholder {
166f36ba452SJake Burkholder 
16731174518SJohn Baldwin 	*stack_base -= sizeof(uint32_t);
16831174518SJohn Baldwin 	if (suword32((void *)*stack_base, imgp->args->argc) != 0)
16931174518SJohn Baldwin 		return (EFAULT);
17031174518SJohn Baldwin 	return (0);
171f36ba452SJake Burkholder }
172f36ba452SJake Burkholder 
173f36ba452SJake Burkholder static int
exec_aout_imgact(struct image_params * imgp)1747332c129SKonstantin Belousov exec_aout_imgact(struct image_params *imgp)
175cfefd687SGarrett Wollman {
176290e05ddSKonstantin Belousov 	const struct exec *a_out;
1775856e12eSJohn Dyson 	struct vmspace *vmspace;
1782f33b2c0SAlan Cox 	vm_map_t map;
1791616db3cSJohn Dyson 	vm_object_t object;
1801616db3cSJohn Dyson 	vm_offset_t text_end, data_end;
181ede8dc43SBruce Evans 	unsigned long virtual_offset;
182a316d390SJohn Dyson 	unsigned long file_offset;
183cfefd687SGarrett Wollman 	unsigned long bss_size;
184bb56ec4aSPoul-Henning Kamp 	int error;
185cfefd687SGarrett Wollman 
186290e05ddSKonstantin Belousov 	a_out = (const struct exec *)imgp->image_header;
187290e05ddSKonstantin Belousov 
1881e1e0b44SSøren Schmidt 	/*
1891e1e0b44SSøren Schmidt 	 * Linux and *BSD binaries look very much alike,
1901e1e0b44SSøren Schmidt 	 * only the machine id is different:
191d3628763SRodney W. Grimes 	 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
192185dc761SPeter Wemm 	 * NetBSD is in network byte order.. ugh.
1931e1e0b44SSøren Schmidt 	 */
194f4dc9a40SWarner Losh 	if (((a_out->a_midmag >> 16) & 0xff) != 0x86 &&
195f4dc9a40SWarner Losh 	    ((a_out->a_midmag >> 16) & 0xff) != 0 &&
196f4dc9a40SWarner Losh 	    ((((int)ntohl(a_out->a_midmag)) >> 16) & 0xff) != 0x86)
197290e05ddSKonstantin Belousov                 return (-1);
1981e1e0b44SSøren Schmidt 
199cfefd687SGarrett Wollman 	/*
200cfefd687SGarrett Wollman 	 * Set file/virtual offset based on a.out variant.
201cfefd687SGarrett Wollman 	 *	We do two cases: host byte order and network byte order
202cfefd687SGarrett Wollman 	 *	(for NetBSD compatibility)
203cfefd687SGarrett Wollman 	 */
204f4dc9a40SWarner Losh 	switch ((int)(a_out->a_midmag & 0xffff)) {
205cfefd687SGarrett Wollman 	case ZMAGIC:
206cfefd687SGarrett Wollman 		virtual_offset = 0;
207cfefd687SGarrett Wollman 		if (a_out->a_text) {
208f8845af0SPoul-Henning Kamp 			file_offset = PAGE_SIZE;
209cfefd687SGarrett Wollman 		} else {
210cfefd687SGarrett Wollman 			/* Bill's "screwball mode" */
211cfefd687SGarrett Wollman 			file_offset = 0;
212cfefd687SGarrett Wollman 		}
213cfefd687SGarrett Wollman 		break;
214cfefd687SGarrett Wollman 	case QMAGIC:
215f8845af0SPoul-Henning Kamp 		virtual_offset = PAGE_SIZE;
216cfefd687SGarrett Wollman 		file_offset = 0;
2174fe88fe6SJohn Polstra 		/* Pass PS_STRINGS for BSD/OS binaries only. */
2184fe88fe6SJohn Polstra 		if (N_GETMID(*a_out) == MID_ZERO)
219397df744SBrooks Davis 			imgp->ps_strings = (void *)aout_sysvec.sv_psstrings;
220cfefd687SGarrett Wollman 		break;
221cfefd687SGarrett Wollman 	default:
222cfefd687SGarrett Wollman 		/* NetBSD compatibility */
223f4dc9a40SWarner Losh 		switch ((int)(ntohl(a_out->a_midmag) & 0xffff)) {
224cfefd687SGarrett Wollman 		case ZMAGIC:
225cfefd687SGarrett Wollman 		case QMAGIC:
226f8845af0SPoul-Henning Kamp 			virtual_offset = PAGE_SIZE;
227cfefd687SGarrett Wollman 			file_offset = 0;
228cfefd687SGarrett Wollman 			break;
229cfefd687SGarrett Wollman 		default:
230cfefd687SGarrett Wollman 			return (-1);
231cfefd687SGarrett Wollman 		}
232cfefd687SGarrett Wollman 	}
233cfefd687SGarrett Wollman 
234f8845af0SPoul-Henning Kamp 	bss_size = roundup(a_out->a_bss, PAGE_SIZE);
235cfefd687SGarrett Wollman 
236cfefd687SGarrett Wollman 	/*
237cfefd687SGarrett Wollman 	 * Check various fields in header for validity/bounds.
238cfefd687SGarrett Wollman 	 */
239cfefd687SGarrett Wollman 	if (/* entry point must lay with text region */
240cfefd687SGarrett Wollman 	    a_out->a_entry < virtual_offset ||
241cfefd687SGarrett Wollman 	    a_out->a_entry >= virtual_offset + a_out->a_text ||
242cfefd687SGarrett Wollman 
243cfefd687SGarrett Wollman 	    /* text and data size must each be page rounded */
2447332c129SKonstantin Belousov 	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK
2457332c129SKonstantin Belousov 
2467332c129SKonstantin Belousov #ifdef __amd64__
2477332c129SKonstantin Belousov 	    ||
2487332c129SKonstantin Belousov 	    /* overflows */
2497332c129SKonstantin Belousov 	    virtual_offset + a_out->a_text + a_out->a_data + bss_size > UINT_MAX
2507332c129SKonstantin Belousov #endif
2517332c129SKonstantin Belousov 	    )
252cfefd687SGarrett Wollman 		return (-1);
253cfefd687SGarrett Wollman 
254cfefd687SGarrett Wollman 	/* text + data can't exceed file size */
255c52007c2SDavid Greenman 	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
256cfefd687SGarrett Wollman 		return (EFAULT);
257cfefd687SGarrett Wollman 
258cfefd687SGarrett Wollman 	/*
259cfefd687SGarrett Wollman 	 * text/data/bss must not exceed limits
260cfefd687SGarrett Wollman 	 */
26191d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
262cfefd687SGarrett Wollman 	if (/* text can't exceed maximum text size */
263cbc89bfbSPaul Saab 	    a_out->a_text > maxtsiz ||
264cfefd687SGarrett Wollman 
265cfefd687SGarrett Wollman 	    /* data + bss can't exceed rlimit */
266f6f6d240SMateusz Guzik 	    a_out->a_data + bss_size > lim_cur_proc(imgp->proc, RLIMIT_DATA) ||
2671ba5ad42SEdward Tomasz Napierala 	    racct_set(imgp->proc, RACCT_DATA, a_out->a_data + bss_size) != 0) {
26891d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
269cfefd687SGarrett Wollman 		return (ENOMEM);
27091d5354aSJohn Baldwin 	}
27191d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
272cfefd687SGarrett Wollman 
273cfefd687SGarrett Wollman 	/*
27460bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
27560bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
27660bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
27760bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
27860bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
27960bb3943SAlan Cox 	 * v_usecount may become zero.
28060bb3943SAlan Cox 	 */
281b249ce48SMateusz Guzik 	VOP_UNLOCK(imgp->vp);
28260bb3943SAlan Cox 
28360bb3943SAlan Cox 	/*
284cfefd687SGarrett Wollman 	 * Destroy old process VM and create a new one (with a new stack)
285cfefd687SGarrett Wollman 	 */
28689b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, &aout_sysvec);
287cfefd687SGarrett Wollman 
28878022527SKonstantin Belousov 	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
28989b57fcfSKonstantin Belousov 	if (error)
29089b57fcfSKonstantin Belousov 		return (error);
29160bb3943SAlan Cox 
292cfefd687SGarrett Wollman 	/*
2935856e12eSJohn Dyson 	 * The vm space can be changed by exec_new_vmspace
2945856e12eSJohn Dyson 	 */
2955856e12eSJohn Dyson 	vmspace = imgp->proc->p_vmspace;
2965856e12eSJohn Dyson 
2970b2ed1aeSJeff Roberson 	object = imgp->object;
2982f33b2c0SAlan Cox 	map = &vmspace->vm_map;
2992f33b2c0SAlan Cox 	vm_map_lock(map);
3001616db3cSJohn Dyson 	vm_object_reference(object);
3011616db3cSJohn Dyson 
3021616db3cSJohn Dyson 	text_end = virtual_offset + a_out->a_text;
3030a91231dSAlan Cox 	error = vm_map_insert(map, object,
3041616db3cSJohn Dyson 		file_offset,
3051616db3cSJohn Dyson 		virtual_offset, text_end,
3061616db3cSJohn Dyson 		VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
30778022527SKonstantin Belousov 		MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
3082f33b2c0SAlan Cox 	if (error) {
3092f33b2c0SAlan Cox 		vm_map_unlock(map);
31041634e2eSAlan Cox 		vm_object_deallocate(object);
311cfefd687SGarrett Wollman 		return (error);
3122f33b2c0SAlan Cox 	}
31378022527SKonstantin Belousov 	VOP_SET_TEXT_CHECKED(imgp->vp);
3141616db3cSJohn Dyson 	data_end = text_end + a_out->a_data;
3151616db3cSJohn Dyson 	if (a_out->a_data) {
3161616db3cSJohn Dyson 		vm_object_reference(object);
3170a91231dSAlan Cox 		error = vm_map_insert(map, object,
3181616db3cSJohn Dyson 			file_offset + a_out->a_text,
3191616db3cSJohn Dyson 			text_end, data_end,
3201616db3cSJohn Dyson 			VM_PROT_ALL, VM_PROT_ALL,
32178022527SKonstantin Belousov 			MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
3222f33b2c0SAlan Cox 		if (error) {
3232f33b2c0SAlan Cox 			vm_map_unlock(map);
32441634e2eSAlan Cox 			vm_object_deallocate(object);
3251616db3cSJohn Dyson 			return (error);
3261616db3cSJohn Dyson 		}
32778022527SKonstantin Belousov 		VOP_SET_TEXT_CHECKED(imgp->vp);
3282f33b2c0SAlan Cox 	}
329cfefd687SGarrett Wollman 
3301616db3cSJohn Dyson 	if (bss_size) {
3310a91231dSAlan Cox 		error = vm_map_insert(map, NULL, 0,
3321616db3cSJohn Dyson 			data_end, data_end + bss_size,
3331616db3cSJohn Dyson 			VM_PROT_ALL, VM_PROT_ALL, 0);
3342f33b2c0SAlan Cox 		if (error) {
3352f33b2c0SAlan Cox 			vm_map_unlock(map);
336cfefd687SGarrett Wollman 			return (error);
33768940ac1SDavid Greenman 		}
3382f33b2c0SAlan Cox 	}
3392f33b2c0SAlan Cox 	vm_map_unlock(map);
340cfefd687SGarrett Wollman 
341cfefd687SGarrett Wollman 	/* Fill in process VM information */
342cfefd687SGarrett Wollman 	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
343cfefd687SGarrett Wollman 	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
3447cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
3457cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t) (uintptr_t)
3467cd99438SBruce Evans 			    (virtual_offset + a_out->a_text);
347cfefd687SGarrett Wollman 
3481811c1e9SMark Johnston 	error = exec_map_stack(imgp);
3491811c1e9SMark Johnston 	if (error != 0)
3501811c1e9SMark Johnston 		return (error);
3511811c1e9SMark Johnston 
352cfefd687SGarrett Wollman 	/* Fill in image_params */
353c52007c2SDavid Greenman 	imgp->interpreted = 0;
354c52007c2SDavid Greenman 	imgp->entry_addr = a_out->a_entry;
355cfefd687SGarrett Wollman 
356c52007c2SDavid Greenman 	imgp->proc->p_sysent = &aout_sysvec;
357c0e5de7dSDavid Greenman 
358cfefd687SGarrett Wollman 	return (0);
359cfefd687SGarrett Wollman }
36092d91f76SGarrett Wollman 
36192d91f76SGarrett Wollman /*
36292d91f76SGarrett Wollman  * Tell kern_execve.c about it, with a little help from the linker.
36392d91f76SGarrett Wollman  */
364b7feabf9SEd Maste static struct execsw aout_execsw = {
365b7feabf9SEd Maste 	.ex_imgact = exec_aout_imgact,
366b7feabf9SEd Maste 	.ex_name = "a.out"
367b7feabf9SEd Maste };
368aa855a59SPeter Wemm EXEC_SET(aout, aout_execsw);
369