imgact_aout.c revision 12767
1/*
2 * Copyright (c) 1993, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by David Greenman
16 * 4. The name of the developer may be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	$Id: imgact_aout.c,v 1.19 1995/12/07 12:46:33 davidg Exp $
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/resourcevar.h>
37#include <sys/exec.h>
38#include <sys/mman.h>
39#include <sys/imgact.h>
40#include <sys/imgact_aout.h>
41#include <sys/kernel.h>
42#include <sys/sysent.h>
43
44#include <vm/vm.h>
45#include <vm/vm_param.h>
46#include <vm/vm_prot.h>
47#include <vm/lock.h>
48#include <vm/pmap.h>
49#include <vm/vm_map.h>
50#include <vm/vm_extern.h>
51
52static int	exec_aout_imgact __P((struct image_params *imgp));
53
54static int
55exec_aout_imgact(imgp)
56	struct image_params *imgp;
57{
58	struct exec *a_out = (struct exec *) imgp->image_header;
59	struct vmspace *vmspace = imgp->proc->p_vmspace;
60	unsigned long vmaddr, virtual_offset;
61	unsigned long file_offset;
62	unsigned long bss_size;
63	int error;
64
65#ifdef COMPAT_LINUX
66	/*
67	 * Linux and *BSD binaries look very much alike,
68	 * only the machine id is different:
69	 * 0x64 for Linux, 0x86 for *BSD, 0x00 for BSDI.
70	 */
71	if (((a_out->a_magic >> 16) & 0xff) != 0x86 &&
72	    ((a_out->a_magic >> 16) & 0xff) != 0)
73                return -1;
74#endif /* COMPAT_LINUX */
75
76	/*
77	 * Set file/virtual offset based on a.out variant.
78	 *	We do two cases: host byte order and network byte order
79	 *	(for NetBSD compatibility)
80	 */
81	switch ((int)(a_out->a_magic & 0xffff)) {
82	case ZMAGIC:
83		virtual_offset = 0;
84		if (a_out->a_text) {
85			file_offset = NBPG;
86		} else {
87			/* Bill's "screwball mode" */
88			file_offset = 0;
89		}
90		break;
91	case QMAGIC:
92		virtual_offset = NBPG;
93		file_offset = 0;
94		break;
95	default:
96		/* NetBSD compatibility */
97		switch ((int)(ntohl(a_out->a_magic) & 0xffff)) {
98		case ZMAGIC:
99		case QMAGIC:
100			virtual_offset = NBPG;
101			file_offset = 0;
102			break;
103		default:
104			return (-1);
105		}
106	}
107
108	bss_size = roundup(a_out->a_bss, NBPG);
109
110	/*
111	 * Check various fields in header for validity/bounds.
112	 */
113	if (/* entry point must lay with text region */
114	    a_out->a_entry < virtual_offset ||
115	    a_out->a_entry >= virtual_offset + a_out->a_text ||
116
117	    /* text and data size must each be page rounded */
118	    a_out->a_text % NBPG ||
119	    a_out->a_data % NBPG)
120		return (-1);
121
122	/* text + data can't exceed file size */
123	if (a_out->a_data + a_out->a_text > imgp->attr->va_size)
124		return (EFAULT);
125
126	/*
127	 * text/data/bss must not exceed limits
128	 */
129	if (/* text can't exceed maximum text size */
130	    a_out->a_text > MAXTSIZ ||
131
132	    /* data + bss can't exceed maximum data size */
133	    a_out->a_data + bss_size > MAXDSIZ ||
134
135	    /* data + bss can't exceed rlimit */
136	    a_out->a_data + bss_size >
137		imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur)
138			return (ENOMEM);
139
140	/* copy in arguments and/or environment from old process */
141	error = exec_extract_strings(imgp);
142	if (error)
143		return (error);
144
145	/*
146	 * Destroy old process VM and create a new one (with a new stack)
147	 */
148	exec_new_vmspace(imgp);
149
150	/*
151	 * Map text read/execute
152	 */
153	vmaddr = virtual_offset;
154	error =
155	    vm_mmap(&vmspace->vm_map,			/* map */
156		&vmaddr,				/* address */
157		a_out->a_text,				/* size */
158		VM_PROT_READ | VM_PROT_EXECUTE,		/* protection */
159		VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE,	/* max protection */
160		MAP_PRIVATE | MAP_FIXED,		/* flags */
161		(caddr_t)imgp->vp,			/* vnode */
162		file_offset);				/* offset */
163	if (error)
164		return (error);
165
166	/*
167	 * Map data read/write (if text is 0, assume text is in data area
168	 *	[Bill's screwball mode])
169	 */
170	vmaddr = virtual_offset + a_out->a_text;
171	error =
172	    vm_mmap(&vmspace->vm_map,
173		&vmaddr,
174		a_out->a_data,
175		VM_PROT_READ | VM_PROT_WRITE | (a_out->a_text ? 0 : VM_PROT_EXECUTE),
176		VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED,
177		(caddr_t) imgp->vp,
178		file_offset + a_out->a_text);
179	if (error)
180		return (error);
181
182	if (bss_size != 0) {
183		/*
184		 * Allocate demand-zeroed area for uninitialized data
185		 * "bss" = 'block started by symbol' - named after the IBM 7090
186		 *	instruction of the same name.
187		 */
188		vmaddr = virtual_offset + a_out->a_text + a_out->a_data;
189		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE);
190		if (error)
191			return (error);
192	}
193
194	/* Fill in process VM information */
195	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
196	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
197	vmspace->vm_taddr = (caddr_t) virtual_offset;
198	vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text;
199
200	/* Fill in image_params */
201	imgp->interpreted = 0;
202	imgp->entry_addr = a_out->a_entry;
203
204	imgp->proc->p_sysent = &aout_sysvec;
205
206	/* Indicate that this file should not be modified */
207	imgp->vp->v_flag |= VTEXT;
208
209	return (0);
210}
211
212/*
213 * Tell kern_execve.c about it, with a little help from the linker.
214 * Since `const' objects end up in the text segment, TEXT_SET is the
215 * correct directive to use.
216 */
217static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" };
218TEXT_SET(execsw_set, aout_execsw);
219