loadfile_aout.c revision 1.2.2.2 1 1.2.2.2 thorpej /* $NetBSD: loadfile_aout.c,v 1.2.2.2 2001/11/12 21:19:05 thorpej Exp $ */
2 1.2.2.2 thorpej
3 1.2.2.2 thorpej /*-
4 1.2.2.2 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.2.2.2 thorpej * All rights reserved.
6 1.2.2.2 thorpej *
7 1.2.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.2.2.2 thorpej * NASA Ames Research Center and by Christos Zoulas.
10 1.2.2.2 thorpej *
11 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
13 1.2.2.2 thorpej * are met:
14 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.2.2.2 thorpej * must display the following acknowledgement:
21 1.2.2.2 thorpej * This product includes software developed by the NetBSD
22 1.2.2.2 thorpej * Foundation, Inc. and its contributors.
23 1.2.2.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.2.2.2 thorpej * contributors may be used to endorse or promote products derived
25 1.2.2.2 thorpej * from this software without specific prior written permission.
26 1.2.2.2 thorpej *
27 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.2.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.2.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.2.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.2.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.2.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.2.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.2.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.2.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.2.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.2.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.2.2.2 thorpej */
39 1.2.2.2 thorpej
40 1.2.2.2 thorpej /*
41 1.2.2.2 thorpej * Copyright (c) 1992, 1993
42 1.2.2.2 thorpej * The Regents of the University of California. All rights reserved.
43 1.2.2.2 thorpej *
44 1.2.2.2 thorpej * This code is derived from software contributed to Berkeley by
45 1.2.2.2 thorpej * Ralph Campbell.
46 1.2.2.2 thorpej *
47 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
48 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
49 1.2.2.2 thorpej * are met:
50 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
51 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
52 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
53 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
54 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
55 1.2.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
56 1.2.2.2 thorpej * must display the following acknowledgement:
57 1.2.2.2 thorpej * This product includes software developed by the University of
58 1.2.2.2 thorpej * California, Berkeley and its contributors.
59 1.2.2.2 thorpej * 4. Neither the name of the University nor the names of its contributors
60 1.2.2.2 thorpej * may be used to endorse or promote products derived from this software
61 1.2.2.2 thorpej * without specific prior written permission.
62 1.2.2.2 thorpej *
63 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.2.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.2.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.2.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.2.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.2.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.2.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.2.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.2.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.2.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.2.2.2 thorpej * SUCH DAMAGE.
74 1.2.2.2 thorpej *
75 1.2.2.2 thorpej * @(#)boot.c 8.1 (Berkeley) 6/10/93
76 1.2.2.2 thorpej */
77 1.2.2.2 thorpej
78 1.2.2.2 thorpej #ifdef _STANDALONE
79 1.2.2.2 thorpej #include <lib/libsa/stand.h>
80 1.2.2.2 thorpej #include <lib/libkern/libkern.h>
81 1.2.2.2 thorpej #else
82 1.2.2.2 thorpej #include <stdio.h>
83 1.2.2.2 thorpej #include <string.h>
84 1.2.2.2 thorpej #include <errno.h>
85 1.2.2.2 thorpej #include <stdlib.h>
86 1.2.2.2 thorpej #include <unistd.h>
87 1.2.2.2 thorpej #include <fcntl.h>
88 1.2.2.2 thorpej #include <err.h>
89 1.2.2.2 thorpej #endif
90 1.2.2.2 thorpej
91 1.2.2.2 thorpej #include <sys/param.h>
92 1.2.2.2 thorpej #include <sys/exec.h>
93 1.2.2.2 thorpej
94 1.2.2.2 thorpej #include "loadfile.h"
95 1.2.2.2 thorpej
96 1.2.2.2 thorpej #ifdef BOOT_AOUT
97 1.2.2.2 thorpej
98 1.2.2.2 thorpej int
99 1.2.2.2 thorpej loadfile_aout(fd, x, marks, flags)
100 1.2.2.2 thorpej int fd;
101 1.2.2.2 thorpej struct exec *x;
102 1.2.2.2 thorpej u_long *marks;
103 1.2.2.2 thorpej int flags;
104 1.2.2.2 thorpej {
105 1.2.2.2 thorpej u_long entry = x->a_entry;
106 1.2.2.2 thorpej paddr_t aoutp = 0;
107 1.2.2.2 thorpej paddr_t minp, maxp;
108 1.2.2.2 thorpej int cc;
109 1.2.2.2 thorpej paddr_t offset = marks[MARK_START];
110 1.2.2.2 thorpej u_long magic = N_GETMAGIC(*x);
111 1.2.2.2 thorpej int sub;
112 1.2.2.2 thorpej
113 1.2.2.2 thorpej /* In OMAGIC and NMAGIC, exec header isn't part of text segment */
114 1.2.2.2 thorpej if (magic == OMAGIC || magic == NMAGIC)
115 1.2.2.2 thorpej sub = 0;
116 1.2.2.2 thorpej else
117 1.2.2.2 thorpej sub = sizeof(*x);
118 1.2.2.2 thorpej
119 1.2.2.2 thorpej minp = maxp = ALIGNENTRY(entry);
120 1.2.2.2 thorpej
121 1.2.2.2 thorpej if (lseek(fd, sizeof(*x), SEEK_SET) == -1) {
122 1.2.2.2 thorpej WARN(("lseek text"));
123 1.2.2.2 thorpej return 1;
124 1.2.2.2 thorpej }
125 1.2.2.2 thorpej
126 1.2.2.2 thorpej /*
127 1.2.2.2 thorpej * Leave a copy of the exec header before the text.
128 1.2.2.2 thorpej * The kernel may use this to verify that the
129 1.2.2.2 thorpej * symbols were loaded by this boot program.
130 1.2.2.2 thorpej */
131 1.2.2.2 thorpej if (magic == OMAGIC || magic == NMAGIC) {
132 1.2.2.2 thorpej if (flags & LOAD_HDR && maxp >= sizeof(*x))
133 1.2.2.2 thorpej BCOPY(x, maxp - sizeof(*x), sizeof(*x));
134 1.2.2.2 thorpej }
135 1.2.2.2 thorpej else {
136 1.2.2.2 thorpej if (flags & LOAD_HDR)
137 1.2.2.2 thorpej BCOPY(x, maxp, sizeof(*x));
138 1.2.2.2 thorpej if (flags & (LOAD_HDR|COUNT_HDR))
139 1.2.2.2 thorpej maxp += sizeof(*x);
140 1.2.2.2 thorpej }
141 1.2.2.2 thorpej
142 1.2.2.2 thorpej /*
143 1.2.2.2 thorpej * Read in the text segment.
144 1.2.2.2 thorpej */
145 1.2.2.2 thorpej if (flags & LOAD_TEXT) {
146 1.2.2.2 thorpej PROGRESS(("%ld", x->a_text));
147 1.2.2.2 thorpej
148 1.2.2.2 thorpej if (READ(fd, maxp, x->a_text - sub) != x->a_text - sub) {
149 1.2.2.2 thorpej WARN(("read text"));
150 1.2.2.2 thorpej return 1;
151 1.2.2.2 thorpej }
152 1.2.2.2 thorpej } else {
153 1.2.2.2 thorpej if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
154 1.2.2.2 thorpej WARN(("seek text"));
155 1.2.2.2 thorpej return 1;
156 1.2.2.2 thorpej }
157 1.2.2.2 thorpej }
158 1.2.2.2 thorpej if (flags & (LOAD_TEXT|COUNT_TEXT))
159 1.2.2.2 thorpej maxp += x->a_text - sub;
160 1.2.2.2 thorpej
161 1.2.2.2 thorpej /*
162 1.2.2.2 thorpej * Provide alignment if required
163 1.2.2.2 thorpej */
164 1.2.2.2 thorpej if (magic == ZMAGIC || magic == NMAGIC) {
165 1.2.2.2 thorpej int size = -(unsigned int)maxp & (__LDPGSZ - 1);
166 1.2.2.2 thorpej
167 1.2.2.2 thorpej if (flags & LOAD_TEXTA) {
168 1.2.2.2 thorpej PROGRESS(("/%d", size));
169 1.2.2.2 thorpej BZERO(maxp, size);
170 1.2.2.2 thorpej }
171 1.2.2.2 thorpej
172 1.2.2.2 thorpej if (flags & (LOAD_TEXTA|COUNT_TEXTA))
173 1.2.2.2 thorpej maxp += size;
174 1.2.2.2 thorpej }
175 1.2.2.2 thorpej
176 1.2.2.2 thorpej /*
177 1.2.2.2 thorpej * Read in the data segment.
178 1.2.2.2 thorpej */
179 1.2.2.2 thorpej if (flags & LOAD_DATA) {
180 1.2.2.2 thorpej PROGRESS(("+%ld", x->a_data));
181 1.2.2.2 thorpej
182 1.2.2.2 thorpej if (READ(fd, maxp, x->a_data) != x->a_data) {
183 1.2.2.2 thorpej WARN(("read data"));
184 1.2.2.2 thorpej return 1;
185 1.2.2.2 thorpej }
186 1.2.2.2 thorpej }
187 1.2.2.2 thorpej else {
188 1.2.2.2 thorpej if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
189 1.2.2.2 thorpej WARN(("seek data"));
190 1.2.2.2 thorpej return 1;
191 1.2.2.2 thorpej }
192 1.2.2.2 thorpej }
193 1.2.2.2 thorpej if (flags & (LOAD_DATA|COUNT_DATA))
194 1.2.2.2 thorpej maxp += x->a_data;
195 1.2.2.2 thorpej
196 1.2.2.2 thorpej /*
197 1.2.2.2 thorpej * Zero out the BSS section.
198 1.2.2.2 thorpej * (Kernel doesn't care, but do it anyway.)
199 1.2.2.2 thorpej */
200 1.2.2.2 thorpej if (flags & LOAD_BSS) {
201 1.2.2.2 thorpej PROGRESS(("+%ld", x->a_bss));
202 1.2.2.2 thorpej
203 1.2.2.2 thorpej BZERO(maxp, x->a_bss);
204 1.2.2.2 thorpej }
205 1.2.2.2 thorpej
206 1.2.2.2 thorpej if (flags & (LOAD_BSS|COUNT_BSS))
207 1.2.2.2 thorpej maxp += x->a_bss;
208 1.2.2.2 thorpej
209 1.2.2.2 thorpej /*
210 1.2.2.2 thorpej * Read in the symbol table and strings.
211 1.2.2.2 thorpej * (Always set the symtab size word.)
212 1.2.2.2 thorpej */
213 1.2.2.2 thorpej if (flags & LOAD_SYM)
214 1.2.2.2 thorpej BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
215 1.2.2.2 thorpej
216 1.2.2.2 thorpej if (flags & (LOAD_SYM|COUNT_SYM)) {
217 1.2.2.2 thorpej maxp += sizeof(x->a_syms);
218 1.2.2.2 thorpej aoutp = maxp;
219 1.2.2.2 thorpej }
220 1.2.2.2 thorpej
221 1.2.2.2 thorpej if (x->a_syms > 0) {
222 1.2.2.2 thorpej /* Symbol table and string table length word. */
223 1.2.2.2 thorpej
224 1.2.2.2 thorpej if (flags & LOAD_SYM) {
225 1.2.2.2 thorpej PROGRESS(("+[%ld", x->a_syms));
226 1.2.2.2 thorpej
227 1.2.2.2 thorpej if (READ(fd, maxp, x->a_syms) != x->a_syms) {
228 1.2.2.2 thorpej WARN(("read symbols"));
229 1.2.2.2 thorpej return 1;
230 1.2.2.2 thorpej }
231 1.2.2.2 thorpej } else {
232 1.2.2.2 thorpej if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
233 1.2.2.2 thorpej WARN(("seek symbols"));
234 1.2.2.2 thorpej return 1;
235 1.2.2.2 thorpej }
236 1.2.2.2 thorpej }
237 1.2.2.2 thorpej if (flags & (LOAD_SYM|COUNT_SYM))
238 1.2.2.2 thorpej maxp += x->a_syms;
239 1.2.2.2 thorpej
240 1.2.2.2 thorpej if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) {
241 1.2.2.2 thorpej WARN(("read string table"));
242 1.2.2.2 thorpej return 1;
243 1.2.2.2 thorpej }
244 1.2.2.2 thorpej
245 1.2.2.2 thorpej if (flags & LOAD_SYM) {
246 1.2.2.2 thorpej BCOPY(&cc, maxp, sizeof(cc));
247 1.2.2.2 thorpej
248 1.2.2.2 thorpej /* String table. Length word includes itself. */
249 1.2.2.2 thorpej
250 1.2.2.2 thorpej PROGRESS(("+%d]", cc));
251 1.2.2.2 thorpej }
252 1.2.2.2 thorpej if (flags & (LOAD_SYM|COUNT_SYM))
253 1.2.2.2 thorpej maxp += sizeof(cc);
254 1.2.2.2 thorpej
255 1.2.2.2 thorpej cc -= sizeof(int);
256 1.2.2.2 thorpej if (cc <= 0) {
257 1.2.2.2 thorpej WARN(("symbol table too short"));
258 1.2.2.2 thorpej return 1;
259 1.2.2.2 thorpej }
260 1.2.2.2 thorpej
261 1.2.2.2 thorpej if (flags & LOAD_SYM) {
262 1.2.2.2 thorpej if (READ(fd, maxp, cc) != cc) {
263 1.2.2.2 thorpej WARN(("read strings"));
264 1.2.2.2 thorpej return 1;
265 1.2.2.2 thorpej }
266 1.2.2.2 thorpej } else {
267 1.2.2.2 thorpej if (lseek(fd, cc, SEEK_CUR) == -1) {
268 1.2.2.2 thorpej WARN(("seek strings"));
269 1.2.2.2 thorpej return 1;
270 1.2.2.2 thorpej }
271 1.2.2.2 thorpej }
272 1.2.2.2 thorpej if (flags & (LOAD_SYM|COUNT_SYM))
273 1.2.2.2 thorpej maxp += cc;
274 1.2.2.2 thorpej }
275 1.2.2.2 thorpej
276 1.2.2.2 thorpej marks[MARK_START] = LOADADDR(minp);
277 1.2.2.2 thorpej marks[MARK_ENTRY] = LOADADDR(entry);
278 1.2.2.2 thorpej marks[MARK_NSYM] = x->a_syms;
279 1.2.2.2 thorpej marks[MARK_SYM] = LOADADDR(aoutp);
280 1.2.2.2 thorpej marks[MARK_END] = LOADADDR(maxp);
281 1.2.2.2 thorpej return 0;
282 1.2.2.2 thorpej }
283 1.2.2.2 thorpej
284 1.2.2.2 thorpej #endif /* BOOT_AOUT */
285