Commit | Line | Data |
---|---|---|
965cadcc GW |
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 REGENTS 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 | * | |
def592a1 | 31 | * $Id: imgact_aout.c,v 1.3 1993/12/30 01:39:29 davidg Exp $ |
965cadcc GW |
32 | */ |
33 | ||
34 | #include "param.h" | |
35 | #include "systm.h" | |
36 | #include "resourcevar.h" | |
37 | #include "exec.h" | |
38 | #include "mman.h" | |
39 | #include "imgact.h" | |
3228baa0 | 40 | #include "kernel.h" |
965cadcc GW |
41 | |
42 | #include "vm/vm.h" | |
43 | ||
44 | int | |
45 | exec_aout_imgact(iparams) | |
46 | struct image_params *iparams; | |
47 | { | |
48 | struct exec *a_out = (struct exec *) iparams->image_header; | |
49 | struct vmspace *vmspace = iparams->proc->p_vmspace; | |
50 | unsigned long vmaddr, virtual_offset, file_offset; | |
51 | unsigned long bss_size; | |
52 | int error, len; | |
53 | ||
54 | /* | |
55 | * Set file/virtual offset based on a.out variant. | |
56 | * We do two cases: host byte order and network byte order | |
57 | * (for NetBSD compatibility) | |
58 | */ | |
59 | switch ((int)(a_out->a_magic & 0xffff)) { | |
60 | case ZMAGIC: | |
61 | virtual_offset = 0; | |
62 | if (a_out->a_text) { | |
63 | file_offset = NBPG; | |
64 | } else { | |
65 | /* Bill's "screwball mode" */ | |
66 | file_offset = 0; | |
67 | } | |
68 | break; | |
69 | case QMAGIC: | |
70 | virtual_offset = NBPG; | |
71 | file_offset = 0; | |
72 | break; | |
73 | default: | |
74 | /* NetBSD compatibility */ | |
75 | switch ((int)(ntohl(a_out->a_magic) & 0xffff)) { | |
76 | case ZMAGIC: | |
77 | case QMAGIC: | |
78 | virtual_offset = NBPG; | |
79 | file_offset = 0; | |
80 | break; | |
81 | default: | |
82 | return (-1); | |
83 | } | |
84 | } | |
85 | ||
86 | bss_size = roundup(a_out->a_bss, NBPG); | |
87 | ||
88 | /* | |
89 | * Check various fields in header for validity/bounds. | |
90 | */ | |
91 | if (/* entry point must lay with text region */ | |
92 | a_out->a_entry < virtual_offset || | |
93 | a_out->a_entry >= virtual_offset + a_out->a_text || | |
94 | ||
95 | /* text and data size must each be page rounded */ | |
96 | a_out->a_text % NBPG || | |
97 | a_out->a_data % NBPG) | |
98 | return (-1); | |
99 | ||
100 | /* text + data can't exceed file size */ | |
101 | if (a_out->a_data + a_out->a_text > iparams->attr->va_size) | |
102 | return (EFAULT); | |
103 | ||
104 | /* | |
105 | * text/data/bss must not exceed limits | |
106 | */ | |
107 | if (/* text can't exceed maximum text size */ | |
108 | a_out->a_text > MAXTSIZ || | |
109 | ||
110 | /* data + bss can't exceed maximum data size */ | |
111 | a_out->a_data + bss_size > MAXDSIZ || | |
112 | ||
113 | /* data + bss can't exceed rlimit */ | |
114 | a_out->a_data + bss_size > | |
115 | iparams->proc->p_rlimit[RLIMIT_DATA].rlim_cur) | |
116 | return (ENOMEM); | |
117 | ||
118 | /* copy in arguments and/or environment from old process */ | |
119 | error = exec_extract_strings(iparams); | |
120 | if (error) | |
121 | return (error); | |
122 | ||
123 | /* | |
124 | * Destroy old process VM and create a new one (with a new stack) | |
125 | */ | |
126 | exec_new_vmspace(iparams); | |
127 | ||
128 | /* | |
129 | * Map text read/execute | |
130 | */ | |
131 | vmaddr = virtual_offset; | |
132 | error = | |
133 | vm_mmap(&vmspace->vm_map, /* map */ | |
134 | &vmaddr, /* address */ | |
135 | a_out->a_text, /* size */ | |
136 | VM_PROT_READ | VM_PROT_EXECUTE, /* protection */ | |
04d0a09d | 137 | VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_WRITE, /* max protection */ |
965cadcc GW |
138 | MAP_FILE | MAP_PRIVATE | MAP_FIXED, /* flags */ |
139 | iparams->vnodep, /* vnode */ | |
140 | file_offset); /* offset */ | |
141 | if (error) | |
142 | return (error); | |
143 | ||
144 | /* | |
145 | * Map data read/write (if text is 0, assume text is in data area | |
146 | * [Bill's screwball mode]) | |
147 | */ | |
148 | vmaddr = virtual_offset + a_out->a_text; | |
149 | error = | |
150 | vm_mmap(&vmspace->vm_map, | |
151 | &vmaddr, | |
152 | a_out->a_data, | |
153 | VM_PROT_READ | VM_PROT_WRITE | (a_out->a_text ? 0 : VM_PROT_EXECUTE), | |
def592a1 | 154 | VM_PROT_ALL, MAP_FILE | MAP_PRIVATE | MAP_FIXED, iparams->vnodep, |
965cadcc GW |
155 | file_offset + a_out->a_text); |
156 | if (error) | |
157 | return (error); | |
158 | ||
159 | /* | |
160 | * Allocate demand-zeroed area for uninitialized data | |
161 | * "bss" = 'block started by symbol' - named after the IBM 7090 | |
162 | * instruction of the same name. | |
163 | */ | |
164 | vmaddr = virtual_offset + a_out->a_text + a_out->a_data; | |
165 | error = vm_allocate(&vmspace->vm_map, &vmaddr, bss_size, FALSE); | |
166 | if (error) | |
167 | return (error); | |
168 | ||
169 | /* Fill in process VM information */ | |
170 | vmspace->vm_tsize = a_out->a_text >> PAGE_SHIFT; | |
171 | vmspace->vm_dsize = (a_out->a_data + bss_size) >> PAGE_SHIFT; | |
172 | vmspace->vm_taddr = (caddr_t) virtual_offset; | |
173 | vmspace->vm_daddr = (caddr_t) virtual_offset + a_out->a_text; | |
174 | ||
175 | /* Fill in image_params */ | |
176 | iparams->interpreted = 0; | |
177 | iparams->entry_addr = a_out->a_entry; | |
178 | ||
179 | return (0); | |
180 | } | |
3228baa0 GW |
181 | |
182 | /* | |
183 | * Tell kern_execve.c about it, with a little help from the linker. | |
184 | * Since `const' objects end up in the text segment, TEXT_SET is the | |
185 | * correct directive to use. | |
186 | */ | |
187 | static const struct execsw aout_execsw = { exec_aout_imgact }; | |
188 | TEXT_SET(execsw_set, aout_execsw); | |
189 |