xref: /freebsd/sys/kern/imgact_aout.c (revision 3fc21fdd5f8a4eeb71f43cc6841f59ef1debaf9a)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31677b542eSDavid E. O'Brien 
3226f9a767SRodney W. Grimes #include <sys/param.h>
3326f9a767SRodney W. Grimes #include <sys/exec.h>
3426f9a767SRodney W. Grimes #include <sys/imgact.h>
35bc6d7444SDavid Greenman #include <sys/imgact_aout.h>
3626f9a767SRodney W. Grimes #include <sys/kernel.h>
377332c129SKonstantin Belousov #include <sys/limits.h>
38fb919e4dSMark Murray #include <sys/lock.h>
39e5d6cd0cSBruce Evans #include <sys/malloc.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41a794e791SBruce Evans #include <sys/proc.h>
421ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
43fb919e4dSMark Murray #include <sys/resourcevar.h>
4422d4b0fbSJohn Polstra #include <sys/signalvar.h>
4522d4b0fbSJohn Polstra #include <sys/syscall.h>
46e5d6cd0cSBruce Evans #include <sys/sysent.h>
47e5d6cd0cSBruce Evans #include <sys/systm.h>
48a794e791SBruce Evans #include <sys/vnode.h>
49fb919e4dSMark Murray 
50710ded3aSPeter Wemm #include <machine/frame.h>
51e5d6cd0cSBruce Evans #include <machine/md_var.h>
52cfefd687SGarrett Wollman 
5326f9a767SRodney W. Grimes #include <vm/vm.h>
54efeaf95aSDavid Greenman #include <vm/pmap.h>
55efeaf95aSDavid Greenman #include <vm/vm_map.h>
561616db3cSJohn Dyson #include <vm/vm_object.h>
57e5d6cd0cSBruce Evans #include <vm/vm_param.h>
58cfefd687SGarrett Wollman 
597332c129SKonstantin Belousov #ifdef __amd64__
607332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_signal.h>
617332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
627332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_proto.h>
637332c129SKonstantin Belousov #include <compat/freebsd32/freebsd32_syscall.h>
647332c129SKonstantin Belousov #include <compat/ia32/ia32_signal.h>
657332c129SKonstantin Belousov #endif
667332c129SKonstantin Belousov 
674d77a549SAlfred Perlstein static int	exec_aout_imgact(struct image_params *imgp);
6831174518SJohn Baldwin static int	aout_fixup(uintptr_t *stack_base, struct image_params *imgp);
697ee050b7SBruce Evans 
70d86c1f0dSKonstantin Belousov #define	AOUT32_USRSTACK		0xbfc00000
71d86c1f0dSKonstantin Belousov 
727332c129SKonstantin Belousov #if defined(__i386__)
73d86c1f0dSKonstantin Belousov 
74d86c1f0dSKonstantin Belousov #define	AOUT32_PS_STRINGS	(AOUT32_USRSTACK - sizeof(struct ps_strings))
75d86c1f0dSKonstantin Belousov 
7622d4b0fbSJohn Polstra struct sysentvec aout_sysvec = {
77a8d403e1SKonstantin Belousov 	.sv_size	= SYS_MAXSYSCALL,
78a8d403e1SKonstantin Belousov 	.sv_table	= sysent,
79a8d403e1SKonstantin Belousov 	.sv_transtrap	= NULL,
80a8d403e1SKonstantin Belousov 	.sv_fixup	= aout_fixup,
81a8d403e1SKonstantin Belousov 	.sv_sendsig	= sendsig,
82a8d403e1SKonstantin Belousov 	.sv_sigcode	= sigcode,
83a8d403e1SKonstantin Belousov 	.sv_szsigcode	= &szsigcode,
84a8d403e1SKonstantin Belousov 	.sv_name	= "FreeBSD a.out",
85a8d403e1SKonstantin Belousov 	.sv_coredump	= NULL,
86a8d403e1SKonstantin Belousov 	.sv_imgact_try	= NULL,
87a8d403e1SKonstantin Belousov 	.sv_minsigstksz	= MINSIGSTKSZ,
88a8d403e1SKonstantin Belousov 	.sv_minuser	= VM_MIN_ADDRESS,
89d86c1f0dSKonstantin Belousov 	.sv_maxuser	= AOUT32_USRSTACK,
90d86c1f0dSKonstantin Belousov 	.sv_usrstack	= AOUT32_USRSTACK,
91d86c1f0dSKonstantin Belousov 	.sv_psstrings	= AOUT32_PS_STRINGS,
92*3fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
93a8d403e1SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
94a8d403e1SKonstantin Belousov 	.sv_copyout_strings	= exec_copyout_strings,
95a8d403e1SKonstantin Belousov 	.sv_setregs	= exec_setregs,
96a8d403e1SKonstantin Belousov 	.sv_fixlimit	= NULL,
97b4cf0e62SKonstantin Belousov 	.sv_maxssiz	= NULL,
987332c129SKonstantin Belousov 	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32,
99afe1a688SKonstantin Belousov 	.sv_set_syscall_retval = cpu_set_syscall_retval,
100afe1a688SKonstantin Belousov 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
101afe1a688SKonstantin Belousov 	.sv_syscallnames = syscallnames,
102e5d81ef1SDmitry Chagin 	.sv_schedtail	= NULL,
10391d1786fSDmitry Chagin 	.sv_thread_detach = NULL,
104038c7205SDmitry Chagin 	.sv_trap	= NULL,
10528a66fc3SKonstantin Belousov 	.sv_onexec_old = exec_onexec_old,
10628a66fc3SKonstantin Belousov 	.sv_onexit =  exit_onexit,
107de8374dfSDmitry Chagin 	.sv_set_fork_retval = x86_set_fork_retval,
10822d4b0fbSJohn Polstra };
10922d4b0fbSJohn Polstra 
1107332c129SKonstantin Belousov #elif defined(__amd64__)
1117332c129SKonstantin Belousov 
11298c8b625SKonstantin Belousov #include "vdso_ia32_offsets.h"
11398c8b625SKonstantin Belousov 
11498c8b625SKonstantin Belousov extern const char _binary_elf_vdso32_so_1_start[];
11598c8b625SKonstantin Belousov extern const char _binary_elf_vdso32_so_1_end[];
11698c8b625SKonstantin Belousov extern char _binary_elf_vdso32_so_1_size;
11798c8b625SKonstantin Belousov 
1187332c129SKonstantin Belousov #define	AOUT32_PS_STRINGS \
1197332c129SKonstantin Belousov     (AOUT32_USRSTACK - sizeof(struct freebsd32_ps_strings))
120481af8b9SKonstantin Belousov #define	AOUT32_MINUSER		FREEBSD32_MINUSER
1217332c129SKonstantin Belousov 
1227332c129SKonstantin Belousov extern const char *freebsd32_syscallnames[];
1237332c129SKonstantin Belousov extern u_long ia32_maxssiz;
1247332c129SKonstantin Belousov 
12598c8b625SKonstantin Belousov static int aout_szsigcode;
12698c8b625SKonstantin Belousov 
1277332c129SKonstantin Belousov struct sysentvec aout_sysvec = {
1287332c129SKonstantin Belousov 	.sv_size	= FREEBSD32_SYS_MAXSYSCALL,
1297332c129SKonstantin Belousov 	.sv_table	= freebsd32_sysent,
1307332c129SKonstantin Belousov 	.sv_transtrap	= NULL,
1317332c129SKonstantin Belousov 	.sv_fixup	= aout_fixup,
1327332c129SKonstantin Belousov 	.sv_sendsig	= ia32_sendsig,
13398c8b625SKonstantin Belousov 	.sv_sigcode	= _binary_elf_vdso32_so_1_start,
13498c8b625SKonstantin Belousov 	.sv_szsigcode	= &aout_szsigcode,
1357332c129SKonstantin Belousov 	.sv_name	= "FreeBSD a.out",
1367332c129SKonstantin Belousov 	.sv_coredump	= NULL,
1377332c129SKonstantin Belousov 	.sv_imgact_try	= NULL,
1387332c129SKonstantin Belousov 	.sv_minsigstksz	= MINSIGSTKSZ,
139481af8b9SKonstantin Belousov 	.sv_minuser	= AOUT32_MINUSER,
1407332c129SKonstantin Belousov 	.sv_maxuser	= AOUT32_USRSTACK,
1417332c129SKonstantin Belousov 	.sv_usrstack	= AOUT32_USRSTACK,
1427332c129SKonstantin Belousov 	.sv_psstrings	= AOUT32_PS_STRINGS,
143*3fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct freebsd32_ps_strings),
1447332c129SKonstantin Belousov 	.sv_stackprot	= VM_PROT_ALL,
1457332c129SKonstantin Belousov 	.sv_copyout_strings	= freebsd32_copyout_strings,
1467332c129SKonstantin Belousov 	.sv_setregs	= ia32_setregs,
1477332c129SKonstantin Belousov 	.sv_fixlimit	= ia32_fixlimit,
1487332c129SKonstantin Belousov 	.sv_maxssiz	= &ia32_maxssiz,
1497332c129SKonstantin Belousov 	.sv_flags	= SV_ABI_FREEBSD | SV_AOUT | SV_IA32 | SV_ILP32,
1507332c129SKonstantin Belousov 	.sv_set_syscall_retval = ia32_set_syscall_retval,
1517332c129SKonstantin Belousov 	.sv_fetch_syscall_args = ia32_fetch_syscall_args,
1527332c129SKonstantin Belousov 	.sv_syscallnames = freebsd32_syscallnames,
15328a66fc3SKonstantin Belousov 	.sv_onexec_old	= exec_onexec_old,
15428a66fc3SKonstantin Belousov 	.sv_onexit	= exit_onexit,
155de8374dfSDmitry Chagin 	.sv_set_fork_retval = x86_set_fork_retval,
1567332c129SKonstantin Belousov };
15798c8b625SKonstantin Belousov 
15898c8b625SKonstantin Belousov static void
15998c8b625SKonstantin Belousov aout_sysent(void *arg __unused)
16098c8b625SKonstantin Belousov {
16198c8b625SKonstantin Belousov 	aout_szsigcode = (int)(uintptr_t)&_binary_elf_vdso32_so_1_size;
16298c8b625SKonstantin Belousov }
16398c8b625SKonstantin Belousov SYSINIT(aout_sysent, SI_SUB_EXEC, SI_ORDER_ANY, aout_sysent, NULL);
1647332c129SKonstantin Belousov #else
1659da5257eSKonstantin Belousov #error "Only ia32 arch is supported"
1667332c129SKonstantin Belousov #endif
1677332c129SKonstantin Belousov 
168b9e91a85SBruce Evans static int
16931174518SJohn Baldwin aout_fixup(uintptr_t *stack_base, struct image_params *imgp)
170f36ba452SJake Burkholder {
171f36ba452SJake Burkholder 
17231174518SJohn Baldwin 	*stack_base -= sizeof(uint32_t);
17331174518SJohn Baldwin 	if (suword32((void *)*stack_base, imgp->args->argc) != 0)
17431174518SJohn Baldwin 		return (EFAULT);
17531174518SJohn Baldwin 	return (0);
176f36ba452SJake Burkholder }
177f36ba452SJake Burkholder 
178f36ba452SJake Burkholder static int
1797332c129SKonstantin Belousov exec_aout_imgact(struct image_params *imgp)
180cfefd687SGarrett Wollman {
181290e05ddSKonstantin Belousov 	const struct exec *a_out;
1825856e12eSJohn Dyson 	struct vmspace *vmspace;
1832f33b2c0SAlan Cox 	vm_map_t map;
1841616db3cSJohn Dyson 	vm_object_t object;
1851616db3cSJohn Dyson 	vm_offset_t text_end, data_end;
186ede8dc43SBruce Evans 	unsigned long virtual_offset;
187a316d390SJohn Dyson 	unsigned long file_offset;
188cfefd687SGarrett Wollman 	unsigned long bss_size;
189bb56ec4aSPoul-Henning Kamp 	int error;
190cfefd687SGarrett Wollman 
191290e05ddSKonstantin Belousov 	a_out = (const struct exec *)imgp->image_header;
192290e05ddSKonstantin Belousov 
1931e1e0b44SSøren Schmidt 	/*
1941e1e0b44SSøren Schmidt 	 * Linux and *BSD binaries look very much alike,
1951e1e0b44SSøren Schmidt 	 * only the machine id is different:
196d3628763SRodney W. Grimes 	 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
197185dc761SPeter Wemm 	 * NetBSD is in network byte order.. ugh.
1981e1e0b44SSøren Schmidt 	 */
199f4dc9a40SWarner Losh 	if (((a_out->a_midmag >> 16) & 0xff) != 0x86 &&
200f4dc9a40SWarner Losh 	    ((a_out->a_midmag >> 16) & 0xff) != 0 &&
201f4dc9a40SWarner Losh 	    ((((int)ntohl(a_out->a_midmag)) >> 16) & 0xff) != 0x86)
202290e05ddSKonstantin Belousov                 return (-1);
2031e1e0b44SSøren Schmidt 
204cfefd687SGarrett Wollman 	/*
205cfefd687SGarrett Wollman 	 * Set file/virtual offset based on a.out variant.
206cfefd687SGarrett Wollman 	 *	We do two cases: host byte order and network byte order
207cfefd687SGarrett Wollman 	 *	(for NetBSD compatibility)
208cfefd687SGarrett Wollman 	 */
209f4dc9a40SWarner Losh 	switch ((int)(a_out->a_midmag & 0xffff)) {
210cfefd687SGarrett Wollman 	case ZMAGIC:
211cfefd687SGarrett Wollman 		virtual_offset = 0;
212cfefd687SGarrett Wollman 		if (a_out->a_text) {
213f8845af0SPoul-Henning Kamp 			file_offset = PAGE_SIZE;
214cfefd687SGarrett Wollman 		} else {
215cfefd687SGarrett Wollman 			/* Bill's "screwball mode" */
216cfefd687SGarrett Wollman 			file_offset = 0;
217cfefd687SGarrett Wollman 		}
218cfefd687SGarrett Wollman 		break;
219cfefd687SGarrett Wollman 	case QMAGIC:
220f8845af0SPoul-Henning Kamp 		virtual_offset = PAGE_SIZE;
221cfefd687SGarrett Wollman 		file_offset = 0;
2224fe88fe6SJohn Polstra 		/* Pass PS_STRINGS for BSD/OS binaries only. */
2234fe88fe6SJohn Polstra 		if (N_GETMID(*a_out) == MID_ZERO)
224397df744SBrooks Davis 			imgp->ps_strings = (void *)aout_sysvec.sv_psstrings;
225cfefd687SGarrett Wollman 		break;
226cfefd687SGarrett Wollman 	default:
227cfefd687SGarrett Wollman 		/* NetBSD compatibility */
228f4dc9a40SWarner Losh 		switch ((int)(ntohl(a_out->a_midmag) & 0xffff)) {
229cfefd687SGarrett Wollman 		case ZMAGIC:
230cfefd687SGarrett Wollman 		case QMAGIC:
231f8845af0SPoul-Henning Kamp 			virtual_offset = PAGE_SIZE;
232cfefd687SGarrett Wollman 			file_offset = 0;
233cfefd687SGarrett Wollman 			break;
234cfefd687SGarrett Wollman 		default:
235cfefd687SGarrett Wollman 			return (-1);
236cfefd687SGarrett Wollman 		}
237cfefd687SGarrett Wollman 	}
238cfefd687SGarrett Wollman 
239f8845af0SPoul-Henning Kamp 	bss_size = roundup(a_out->a_bss, PAGE_SIZE);
240cfefd687SGarrett Wollman 
241cfefd687SGarrett Wollman 	/*
242cfefd687SGarrett Wollman 	 * Check various fields in header for validity/bounds.
243cfefd687SGarrett Wollman 	 */
244cfefd687SGarrett Wollman 	if (/* entry point must lay with text region */
245cfefd687SGarrett Wollman 	    a_out->a_entry < virtual_offset ||
246cfefd687SGarrett Wollman 	    a_out->a_entry >= virtual_offset + a_out->a_text ||
247cfefd687SGarrett Wollman 
248cfefd687SGarrett Wollman 	    /* text and data size must each be page rounded */
2497332c129SKonstantin Belousov 	    a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK
2507332c129SKonstantin Belousov 
2517332c129SKonstantin Belousov #ifdef __amd64__
2527332c129SKonstantin Belousov 	    ||
2537332c129SKonstantin Belousov 	    /* overflows */
2547332c129SKonstantin Belousov 	    virtual_offset + a_out->a_text + a_out->a_data + bss_size > UINT_MAX
2557332c129SKonstantin Belousov #endif
2567332c129SKonstantin Belousov 	    )
257cfefd687SGarrett Wollman 		return (-1);
258cfefd687SGarrett Wollman 
259cfefd687SGarrett Wollman 	/* text + data can't exceed file size */
260c52007c2SDavid Greenman 	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
261cfefd687SGarrett Wollman 		return (EFAULT);
262cfefd687SGarrett Wollman 
263cfefd687SGarrett Wollman 	/*
264cfefd687SGarrett Wollman 	 * text/data/bss must not exceed limits
265cfefd687SGarrett Wollman 	 */
26691d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
267cfefd687SGarrett Wollman 	if (/* text can't exceed maximum text size */
268cbc89bfbSPaul Saab 	    a_out->a_text > maxtsiz ||
269cfefd687SGarrett Wollman 
270cfefd687SGarrett Wollman 	    /* data + bss can't exceed rlimit */
271f6f6d240SMateusz Guzik 	    a_out->a_data + bss_size > lim_cur_proc(imgp->proc, RLIMIT_DATA) ||
2721ba5ad42SEdward Tomasz Napierala 	    racct_set(imgp->proc, RACCT_DATA, a_out->a_data + bss_size) != 0) {
27391d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
274cfefd687SGarrett Wollman 		return (ENOMEM);
27591d5354aSJohn Baldwin 	}
27691d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
277cfefd687SGarrett Wollman 
278cfefd687SGarrett Wollman 	/*
27960bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
28060bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
28160bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
28260bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
28360bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
28460bb3943SAlan Cox 	 * v_usecount may become zero.
28560bb3943SAlan Cox 	 */
286b249ce48SMateusz Guzik 	VOP_UNLOCK(imgp->vp);
28760bb3943SAlan Cox 
28860bb3943SAlan Cox 	/*
289cfefd687SGarrett Wollman 	 * Destroy old process VM and create a new one (with a new stack)
290cfefd687SGarrett Wollman 	 */
29189b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, &aout_sysvec);
292cfefd687SGarrett Wollman 
29378022527SKonstantin Belousov 	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
29489b57fcfSKonstantin Belousov 	if (error)
29589b57fcfSKonstantin Belousov 		return (error);
29660bb3943SAlan Cox 
297cfefd687SGarrett Wollman 	/*
2985856e12eSJohn Dyson 	 * The vm space can be changed by exec_new_vmspace
2995856e12eSJohn Dyson 	 */
3005856e12eSJohn Dyson 	vmspace = imgp->proc->p_vmspace;
3015856e12eSJohn Dyson 
3020b2ed1aeSJeff Roberson 	object = imgp->object;
3032f33b2c0SAlan Cox 	map = &vmspace->vm_map;
3042f33b2c0SAlan Cox 	vm_map_lock(map);
3051616db3cSJohn Dyson 	vm_object_reference(object);
3061616db3cSJohn Dyson 
3071616db3cSJohn Dyson 	text_end = virtual_offset + a_out->a_text;
3080a91231dSAlan Cox 	error = vm_map_insert(map, object,
3091616db3cSJohn Dyson 		file_offset,
3101616db3cSJohn Dyson 		virtual_offset, text_end,
3111616db3cSJohn Dyson 		VM_PROT_READ | VM_PROT_EXECUTE, VM_PROT_ALL,
31278022527SKonstantin Belousov 		MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
3132f33b2c0SAlan Cox 	if (error) {
3142f33b2c0SAlan Cox 		vm_map_unlock(map);
31541634e2eSAlan Cox 		vm_object_deallocate(object);
316cfefd687SGarrett Wollman 		return (error);
3172f33b2c0SAlan Cox 	}
31878022527SKonstantin Belousov 	VOP_SET_TEXT_CHECKED(imgp->vp);
3191616db3cSJohn Dyson 	data_end = text_end + a_out->a_data;
3201616db3cSJohn Dyson 	if (a_out->a_data) {
3211616db3cSJohn Dyson 		vm_object_reference(object);
3220a91231dSAlan Cox 		error = vm_map_insert(map, object,
3231616db3cSJohn Dyson 			file_offset + a_out->a_text,
3241616db3cSJohn Dyson 			text_end, data_end,
3251616db3cSJohn Dyson 			VM_PROT_ALL, VM_PROT_ALL,
32678022527SKonstantin Belousov 			MAP_COPY_ON_WRITE | MAP_PREFAULT | MAP_VN_EXEC);
3272f33b2c0SAlan Cox 		if (error) {
3282f33b2c0SAlan Cox 			vm_map_unlock(map);
32941634e2eSAlan Cox 			vm_object_deallocate(object);
3301616db3cSJohn Dyson 			return (error);
3311616db3cSJohn Dyson 		}
33278022527SKonstantin Belousov 		VOP_SET_TEXT_CHECKED(imgp->vp);
3332f33b2c0SAlan Cox 	}
334cfefd687SGarrett Wollman 
3351616db3cSJohn Dyson 	if (bss_size) {
3360a91231dSAlan Cox 		error = vm_map_insert(map, NULL, 0,
3371616db3cSJohn Dyson 			data_end, data_end + bss_size,
3381616db3cSJohn Dyson 			VM_PROT_ALL, VM_PROT_ALL, 0);
3392f33b2c0SAlan Cox 		if (error) {
3402f33b2c0SAlan Cox 			vm_map_unlock(map);
341cfefd687SGarrett Wollman 			return (error);
34268940ac1SDavid Greenman 		}
3432f33b2c0SAlan Cox 	}
3442f33b2c0SAlan Cox 	vm_map_unlock(map);
345cfefd687SGarrett Wollman 
346cfefd687SGarrett Wollman 	/* Fill in process VM information */
347cfefd687SGarrett Wollman 	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
348cfefd687SGarrett Wollman 	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
3497cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t) (uintptr_t) virtual_offset;
3507cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t) (uintptr_t)
3517cd99438SBruce Evans 			    (virtual_offset + a_out->a_text);
352cfefd687SGarrett Wollman 
353cfefd687SGarrett Wollman 	/* Fill in image_params */
354c52007c2SDavid Greenman 	imgp->interpreted = 0;
355c52007c2SDavid Greenman 	imgp->entry_addr = a_out->a_entry;
356cfefd687SGarrett Wollman 
357c52007c2SDavid Greenman 	imgp->proc->p_sysent = &aout_sysvec;
358c0e5de7dSDavid Greenman 
359cfefd687SGarrett Wollman 	return (0);
360cfefd687SGarrett Wollman }
36192d91f76SGarrett Wollman 
36292d91f76SGarrett Wollman /*
36392d91f76SGarrett Wollman  * Tell kern_execve.c about it, with a little help from the linker.
36492d91f76SGarrett Wollman  */
365b7feabf9SEd Maste static struct execsw aout_execsw = {
366b7feabf9SEd Maste 	.ex_imgact = exec_aout_imgact,
367b7feabf9SEd Maste 	.ex_name = "a.out"
368b7feabf9SEd Maste };
369aa855a59SPeter Wemm EXEC_SET(aout, aout_execsw);
370