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