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