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