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