xref: /freebsd/sys/kern/imgact_aout.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
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.21 1995/12/15 02:57:40 peter 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 
52 static int	exec_aout_imgact __P((struct image_params *imgp));
53 
54 static int
55 exec_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 #if defined(COMPAT_LINUX) || defined(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 || defined(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/data read/execute
152 	 */
153 	vmaddr = virtual_offset;
154 	error =
155 	    vm_mmap(&vmspace->vm_map,			/* map */
156 		&vmaddr,				/* address */
157 		a_out->a_text + a_out->a_data,		/* size */
158 		VM_PROT_READ | VM_PROT_EXECUTE,		/* protection */
159 		VM_PROT_ALL,				/* 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 	 * allow writing of data
168 	 */
169 	vm_map_protect(&vmspace->vm_map,
170 		vmaddr + a_out->a_text,
171 		vmaddr + a_out->a_text + a_out->a_data,
172 		VM_PROT_ALL,
173 		FALSE);
174 
175 	if (bss_size != 0) {
176 		/*
177 		 * Allocate demand-zeroed area for uninitialized data
178 		 * "bss" = 'block started by symbol' - named after the IBM 7090
179 		 *	instruction of the same name.
180 		 */
181 		vmaddr = virtual_offset + a_out->a_text + a_out->a_data;
182 		error = vm_map_find(&vmspace->vm_map, NULL, 0, &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
183 		if (error)
184 			return (error);
185 	}
186 
187 	/* Fill in process VM information */
188 	vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT;
189 	vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT;
190 	vmspace->vm_taddr = (caddr_t) virtual_offset;
191 	vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text;
192 
193 	/* Fill in image_params */
194 	imgp->interpreted = 0;
195 	imgp->entry_addr = a_out->a_entry;
196 
197 	imgp->proc->p_sysent = &aout_sysvec;
198 
199 	/* Indicate that this file should not be modified */
200 	imgp->vp->v_flag |= VTEXT;
201 
202 	return (0);
203 }
204 
205 /*
206  * Tell kern_execve.c about it, with a little help from the linker.
207  * Since `const' objects end up in the text segment, TEXT_SET is the
208  * correct directive to use.
209  */
210 static const struct execsw aout_execsw = { exec_aout_imgact, "a.out" };
211 TEXT_SET(execsw_set, aout_execsw);
212