file.c revision 1.4 1 1.4 lukem /* $NetBSD: file.c,v 1.4 1997/10/16 23:24:35 lukem Exp $ */
2 1.2 thorpej
3 1.1 cjs /*
4 1.1 cjs * Copyright (c) 1995-96 Mats O Jansson. All rights reserved.
5 1.1 cjs *
6 1.1 cjs * Redistribution and use in source and binary forms, with or without
7 1.1 cjs * modification, are permitted provided that the following conditions
8 1.1 cjs * are met:
9 1.1 cjs * 1. Redistributions of source code must retain the above copyright
10 1.1 cjs * notice, this list of conditions and the following disclaimer.
11 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cjs * notice, this list of conditions and the following disclaimer in the
13 1.1 cjs * documentation and/or other materials provided with the distribution.
14 1.1 cjs * 3. All advertising materials mentioning features or use of this software
15 1.1 cjs * must display the following acknowledgement:
16 1.1 cjs * This product includes software developed by Mats O Jansson.
17 1.1 cjs * 4. The name of the author may not be used to endorse or promote products
18 1.1 cjs * derived from this software without specific prior written permission.
19 1.1 cjs *
20 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 cjs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 cjs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 cjs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 cjs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 cjs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 cjs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 cjs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 cjs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 cjs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 cjs */
31 1.1 cjs
32 1.4 lukem #include <sys/cdefs.h>
33 1.4 lukem #ifndef lint
34 1.4 lukem __RCSID("$NetBSD: file.c,v 1.4 1997/10/16 23:24:35 lukem Exp $");
35 1.1 cjs #endif
36 1.1 cjs
37 1.1 cjs #include "os.h"
38 1.4 lukem #include "common.h"
39 1.4 lukem #include "file.h"
40 1.4 lukem #include "mopdef.h"
41 1.1 cjs
42 1.1 cjs #ifndef NOAOUT
43 1.1 cjs #if defined(__NetBSD__) || defined(__OpenBSD__)
44 1.1 cjs #include <sys/exec_aout.h>
45 1.1 cjs #endif
46 1.1 cjs #if defined(__bsdi__)
47 1.1 cjs #define NOAOUT
48 1.1 cjs #endif
49 1.1 cjs #if defined(__FreeBSD__)
50 1.1 cjs #include <sys/imgact_aout.h>
51 1.1 cjs #endif
52 1.1 cjs #if !defined(MID_VAX)
53 1.1 cjs #define MID_VAX 140
54 1.1 cjs #endif
55 1.1 cjs #endif
56 1.1 cjs
57 1.4 lukem int getCLBYTES __P((int));
58 1.4 lukem int getMID __P((int, int));
59 1.4 lukem
60 1.1 cjs void
61 1.1 cjs mopFilePutLX(buf, index, value, cnt)
62 1.4 lukem u_char *buf;
63 1.4 lukem int index, cnt;
64 1.4 lukem u_int32_t value;
65 1.1 cjs {
66 1.1 cjs int i;
67 1.1 cjs for (i = 0; i < cnt; i++) {
68 1.1 cjs buf[index+i] = value % 256;
69 1.1 cjs value = value / 256;
70 1.1 cjs }
71 1.1 cjs }
72 1.1 cjs
73 1.1 cjs void
74 1.1 cjs mopFilePutBX(buf, index, value, cnt)
75 1.4 lukem u_char *buf;
76 1.4 lukem int index, cnt;
77 1.4 lukem u_int32_t value;
78 1.1 cjs {
79 1.1 cjs int i;
80 1.1 cjs for (i = 0; i < cnt; i++) {
81 1.1 cjs buf[index+cnt-1-i] = value % 256;
82 1.1 cjs value = value / 256;
83 1.1 cjs }
84 1.1 cjs }
85 1.1 cjs
86 1.4 lukem u_int32_t
87 1.1 cjs mopFileGetLX(buf, index, cnt)
88 1.1 cjs u_char *buf;
89 1.1 cjs int index, cnt;
90 1.1 cjs {
91 1.4 lukem u_int32_t ret = 0;
92 1.1 cjs int i;
93 1.1 cjs
94 1.1 cjs for (i = 0; i < cnt; i++) {
95 1.1 cjs ret = ret*256 + buf[index+cnt-1-i];
96 1.1 cjs }
97 1.1 cjs
98 1.1 cjs return(ret);
99 1.1 cjs }
100 1.1 cjs
101 1.4 lukem u_int32_t
102 1.1 cjs mopFileGetBX(buf, index, cnt)
103 1.1 cjs u_char *buf;
104 1.1 cjs int index, cnt;
105 1.1 cjs {
106 1.4 lukem u_int32_t ret = 0;
107 1.1 cjs int i;
108 1.1 cjs
109 1.1 cjs for (i = 0; i < cnt; i++) {
110 1.1 cjs ret = ret*256 + buf[index+i];
111 1.1 cjs }
112 1.1 cjs
113 1.1 cjs return(ret);
114 1.1 cjs }
115 1.1 cjs
116 1.1 cjs void
117 1.1 cjs mopFileSwapX(buf, index, cnt)
118 1.1 cjs u_char *buf;
119 1.1 cjs int index, cnt;
120 1.1 cjs {
121 1.1 cjs int i;
122 1.1 cjs u_char c;
123 1.1 cjs
124 1.1 cjs for (i = 0; i < (cnt / 2); i++) {
125 1.1 cjs c = buf[index+i];
126 1.1 cjs buf[index+i] = buf[index+cnt-1-i];
127 1.1 cjs buf[index+cnt-1-i] = c;
128 1.1 cjs }
129 1.1 cjs
130 1.1 cjs }
131 1.1 cjs
132 1.1 cjs int
133 1.1 cjs CheckMopFile(fd)
134 1.1 cjs int fd;
135 1.1 cjs {
136 1.1 cjs u_char header[512];
137 1.1 cjs short image_type;
138 1.1 cjs
139 1.1 cjs if (read(fd, header, 512) != 512)
140 1.1 cjs return(-1);
141 1.1 cjs
142 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
143 1.1 cjs
144 1.1 cjs image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
145 1.1 cjs header[IHD_W_ALIAS]);
146 1.1 cjs
147 1.1 cjs switch(image_type) {
148 1.1 cjs case IHD_C_NATIVE: /* Native mode image (VAX) */
149 1.1 cjs case IHD_C_RSX: /* RSX image produced by TKB */
150 1.1 cjs case IHD_C_BPA: /* BASIC plus analog */
151 1.1 cjs case IHD_C_ALIAS: /* Alias */
152 1.1 cjs case IHD_C_CLI: /* Image is CLI */
153 1.1 cjs case IHD_C_PMAX: /* PMAX system image */
154 1.1 cjs case IHD_C_ALPHA: /* ALPHA system image */
155 1.1 cjs break;
156 1.1 cjs default:
157 1.1 cjs return(-1);
158 1.1 cjs }
159 1.1 cjs
160 1.1 cjs return(0);
161 1.1 cjs }
162 1.1 cjs
163 1.1 cjs int
164 1.1 cjs GetMopFileInfo(fd, load, xfr)
165 1.4 lukem int fd;
166 1.4 lukem u_int32_t *load, *xfr;
167 1.1 cjs {
168 1.4 lukem u_char header[512];
169 1.4 lukem short image_type;
170 1.4 lukem u_int32_t load_addr, xfr_addr, isd, iha, hbcnt, isize;
171 1.1 cjs
172 1.1 cjs if (read(fd, header, 512) != 512)
173 1.1 cjs return(-1);
174 1.1 cjs
175 1.1 cjs image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
176 1.1 cjs header[IHD_W_ALIAS]);
177 1.1 cjs
178 1.1 cjs switch(image_type) {
179 1.1 cjs case IHD_C_NATIVE: /* Native mode image (VAX) */
180 1.1 cjs isd = (header[IHD_W_SIZE+1]*256 +
181 1.1 cjs header[IHD_W_SIZE]);
182 1.1 cjs iha = (header[IHD_W_ACTIVOFF+1]*256 +
183 1.1 cjs header[IHD_W_ACTIVOFF]);
184 1.1 cjs hbcnt = (header[IHD_B_HDRBLKCNT]);
185 1.1 cjs isize = (header[isd+ISD_W_PAGCNT+1]*256 +
186 1.1 cjs header[isd+ISD_W_PAGCNT]) * 512;
187 1.1 cjs load_addr = ((header[isd+ISD_V_VPN+1]*256 +
188 1.1 cjs header[isd+ISD_V_VPN]) & ISD_M_VPN)
189 1.1 cjs * 512;
190 1.1 cjs xfr_addr = (header[iha+IHA_L_TFRADR1+3]*0x1000000 +
191 1.1 cjs header[iha+IHA_L_TFRADR1+2]*0x10000 +
192 1.1 cjs header[iha+IHA_L_TFRADR1+1]*0x100 +
193 1.1 cjs header[iha+IHA_L_TFRADR1]) & 0x7fffffff;
194 1.1 cjs printf("Native Image (VAX)\n");
195 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
196 1.1 cjs printf("Image Size: %08x\n",isize);
197 1.1 cjs printf("Load Address: %08x\n",load_addr);
198 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
199 1.1 cjs break;
200 1.1 cjs case IHD_C_RSX: /* RSX image produced by TKB */
201 1.1 cjs hbcnt = header[L_BBLK+1]*256 + header[L_BBLK];
202 1.1 cjs isize = (header[L_BLDZ+1]*256 + header[L_BLDZ]) * 64;
203 1.1 cjs load_addr = header[L_BSA+1]*256 + header[L_BSA];
204 1.1 cjs xfr_addr = header[L_BXFR+1]*256 + header[L_BXFR];
205 1.1 cjs printf("RSX Image\n");
206 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
207 1.1 cjs printf("Image Size: %08x\n",isize);
208 1.1 cjs printf("Load Address: %08x\n",load_addr);
209 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
210 1.1 cjs break;
211 1.1 cjs case IHD_C_BPA: /* BASIC plus analog */
212 1.1 cjs printf("BASIC-Plus Image, not supported\n");
213 1.1 cjs return(-1);
214 1.1 cjs break;
215 1.1 cjs case IHD_C_ALIAS: /* Alias */
216 1.1 cjs printf("Alias, not supported\n");
217 1.1 cjs return(-1);
218 1.1 cjs break;
219 1.1 cjs case IHD_C_CLI: /* Image is CLI */
220 1.1 cjs printf("CLI, not supported\n");
221 1.1 cjs return(-1);
222 1.1 cjs break;
223 1.1 cjs case IHD_C_PMAX: /* PMAX system image */
224 1.1 cjs isd = (header[IHD_W_SIZE+1]*256 +
225 1.1 cjs header[IHD_W_SIZE]);
226 1.1 cjs iha = (header[IHD_W_ACTIVOFF+1]*256 +
227 1.1 cjs header[IHD_W_ACTIVOFF]);
228 1.1 cjs hbcnt = (header[IHD_B_HDRBLKCNT]);
229 1.1 cjs isize = (header[isd+ISD_W_PAGCNT+1]*256 +
230 1.1 cjs header[isd+ISD_W_PAGCNT]) * 512;
231 1.1 cjs load_addr = (header[isd+ISD_V_VPN+1]*256 +
232 1.1 cjs header[isd+ISD_V_VPN]) * 512;
233 1.1 cjs xfr_addr = (header[iha+IHA_L_TFRADR1+3]*0x1000000 +
234 1.1 cjs header[iha+IHA_L_TFRADR1+2]*0x10000 +
235 1.1 cjs header[iha+IHA_L_TFRADR1+1]*0x100 +
236 1.1 cjs header[iha+IHA_L_TFRADR1]);
237 1.1 cjs printf("PMAX Image \n");
238 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
239 1.1 cjs printf("Image Size: %08x\n",isize);
240 1.1 cjs printf("Load Address: %08x\n",load_addr);
241 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
242 1.1 cjs break;
243 1.1 cjs case IHD_C_ALPHA: /* ALPHA system image */
244 1.1 cjs isd = (header[EIHD_L_ISDOFF+3]*0x1000000 +
245 1.1 cjs header[EIHD_L_ISDOFF+2]*0x10000 +
246 1.1 cjs header[EIHD_L_ISDOFF+1]*0x100 +
247 1.1 cjs header[EIHD_L_ISDOFF]);
248 1.1 cjs hbcnt = (header[EIHD_L_HDRBLKCNT+3]*0x1000000 +
249 1.1 cjs header[EIHD_L_HDRBLKCNT+2]*0x10000 +
250 1.1 cjs header[EIHD_L_HDRBLKCNT+1]*0x100 +
251 1.1 cjs header[EIHD_L_HDRBLKCNT]);
252 1.1 cjs isize = (header[isd+EISD_L_SECSIZE+3]*0x1000000 +
253 1.1 cjs header[isd+EISD_L_SECSIZE+2]*0x10000 +
254 1.1 cjs header[isd+EISD_L_SECSIZE+1]*0x100 +
255 1.1 cjs header[isd+EISD_L_SECSIZE]);
256 1.1 cjs load_addr = 0;
257 1.1 cjs xfr_addr = 0;
258 1.1 cjs printf("Alpha Image \n");
259 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
260 1.1 cjs printf("Image Size: %08x\n",isize);
261 1.1 cjs printf("Load Address: %08x\n",load_addr);
262 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
263 1.1 cjs break;
264 1.1 cjs default:
265 1.1 cjs printf("Unknown Image (%d)\n",image_type);
266 1.1 cjs return(-1);
267 1.1 cjs }
268 1.1 cjs
269 1.1 cjs if (load != NULL) {
270 1.1 cjs *load = load_addr;
271 1.1 cjs }
272 1.1 cjs
273 1.1 cjs if (xfr != NULL) {
274 1.1 cjs *xfr = xfr_addr;
275 1.1 cjs }
276 1.1 cjs
277 1.1 cjs return(0);
278 1.1 cjs }
279 1.1 cjs
280 1.1 cjs #ifndef NOAOUT
281 1.1 cjs int
282 1.1 cjs getMID(old_mid,new_mid)
283 1.1 cjs int old_mid, new_mid;
284 1.1 cjs {
285 1.1 cjs int mid;
286 1.1 cjs
287 1.1 cjs mid = old_mid;
288 1.1 cjs
289 1.1 cjs switch (new_mid) {
290 1.1 cjs case MID_I386:
291 1.1 cjs mid = MID_I386;
292 1.1 cjs break;
293 1.1 cjs #ifdef MID_M68K
294 1.1 cjs case MID_M68K:
295 1.1 cjs mid = MID_M68K;
296 1.1 cjs break;
297 1.1 cjs #endif
298 1.1 cjs #ifdef MID_M68K4K
299 1.1 cjs case MID_M68K4K:
300 1.1 cjs mid = MID_M68K4K;
301 1.1 cjs break;
302 1.1 cjs #endif
303 1.1 cjs #ifdef MID_NS32532
304 1.1 cjs case MID_NS32532:
305 1.1 cjs mid = MID_NS32532;
306 1.1 cjs break;
307 1.1 cjs #endif
308 1.1 cjs case MID_SPARC:
309 1.1 cjs mid = MID_SPARC;
310 1.1 cjs break;
311 1.1 cjs #ifdef MID_PMAX
312 1.1 cjs case MID_PMAX:
313 1.1 cjs mid = MID_PMAX;
314 1.1 cjs break;
315 1.1 cjs #endif
316 1.1 cjs #ifdef MID_VAX
317 1.1 cjs case MID_VAX:
318 1.1 cjs mid = MID_VAX;
319 1.1 cjs break;
320 1.1 cjs #endif
321 1.1 cjs #ifdef MID_ALPHA
322 1.1 cjs case MID_ALPHA:
323 1.1 cjs mid = MID_ALPHA;
324 1.1 cjs break;
325 1.1 cjs #endif
326 1.1 cjs #ifdef MID_MIPS
327 1.1 cjs case MID_MIPS:
328 1.1 cjs mid = MID_MIPS;
329 1.1 cjs break;
330 1.1 cjs #endif
331 1.1 cjs #ifdef MID_ARM6
332 1.1 cjs case MID_ARM6:
333 1.1 cjs mid = MID_ARM6;
334 1.1 cjs break;
335 1.1 cjs #endif
336 1.1 cjs default:
337 1.1 cjs }
338 1.1 cjs
339 1.1 cjs return(mid);
340 1.1 cjs }
341 1.1 cjs
342 1.1 cjs int
343 1.1 cjs getCLBYTES(mid)
344 1.1 cjs int mid;
345 1.1 cjs {
346 1.1 cjs int clbytes;
347 1.1 cjs
348 1.1 cjs switch (mid) {
349 1.1 cjs #ifdef MID_VAX
350 1.1 cjs case MID_VAX:
351 1.1 cjs clbytes = 1024;
352 1.1 cjs break;
353 1.1 cjs #endif
354 1.1 cjs case MID_I386:
355 1.1 cjs #ifdef MID_M68K4K
356 1.1 cjs case MID_M68K4K:
357 1.1 cjs #endif
358 1.1 cjs #ifdef MID_NS32532
359 1.1 cjs case MID_NS32532:
360 1.1 cjs #endif
361 1.1 cjs case MID_SPARC: /* It might be 8192 */
362 1.1 cjs #ifdef MID_PMAX
363 1.1 cjs case MID_PMAX:
364 1.1 cjs #endif
365 1.1 cjs #ifdef MID_MIPS
366 1.1 cjs case MID_MIPS:
367 1.1 cjs #endif
368 1.1 cjs #ifdef MID_ARM6
369 1.1 cjs case MID_ARM6:
370 1.1 cjs #endif
371 1.1 cjs clbytes = 4096;
372 1.1 cjs break;
373 1.1 cjs #ifdef MID_M68K
374 1.1 cjs case MID_M68K:
375 1.1 cjs #endif
376 1.1 cjs #ifdef MID_ALPHA
377 1.1 cjs case MID_ALPHA:
378 1.1 cjs #endif
379 1.1 cjs #if defined(MID_M68K) || defined(MID_ALPHA)
380 1.1 cjs clbytes = 8192;
381 1.1 cjs break;
382 1.1 cjs #endif
383 1.1 cjs default:
384 1.1 cjs clbytes = 0;
385 1.1 cjs }
386 1.1 cjs
387 1.1 cjs return(clbytes);
388 1.1 cjs }
389 1.1 cjs #endif
390 1.1 cjs
391 1.1 cjs int
392 1.1 cjs CheckAOutFile(fd)
393 1.1 cjs int fd;
394 1.1 cjs {
395 1.1 cjs #ifdef NOAOUT
396 1.1 cjs return(-1);
397 1.1 cjs #else
398 1.1 cjs struct exec ex, ex_swap;
399 1.1 cjs int mid = -1;
400 1.1 cjs
401 1.1 cjs if (read(fd, (char *)&ex, sizeof(ex)) != sizeof(ex))
402 1.1 cjs return(-1);
403 1.1 cjs
404 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
405 1.1 cjs
406 1.1 cjs if (read(fd, (char *)&ex_swap, sizeof(ex_swap)) != sizeof(ex_swap))
407 1.1 cjs return(-1);
408 1.1 cjs
409 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
410 1.1 cjs
411 1.1 cjs mid = getMID(mid, N_GETMID (ex));
412 1.1 cjs
413 1.1 cjs if (mid == -1) {
414 1.1 cjs mid = getMID(mid, N_GETMID (ex_swap));
415 1.1 cjs }
416 1.1 cjs
417 1.1 cjs if (mid != -1) {
418 1.1 cjs return(0);
419 1.1 cjs } else {
420 1.1 cjs return(-1);
421 1.1 cjs }
422 1.1 cjs #endif NOAOUT
423 1.1 cjs }
424 1.1 cjs
425 1.1 cjs int
426 1.1 cjs GetAOutFileInfo(fd, load, xfr, a_text, a_text_fill,
427 1.1 cjs a_data, a_data_fill, a_bss, a_bss_fill, aout)
428 1.4 lukem int fd;
429 1.4 lukem u_int32_t *load, *xfr, *a_text, *a_text_fill;
430 1.4 lukem u_int32_t *a_data, *a_data_fill, *a_bss, *a_bss_fill;
431 1.4 lukem int *aout;
432 1.1 cjs {
433 1.1 cjs #ifdef NOAOUT
434 1.1 cjs return(-1);
435 1.1 cjs #else
436 1.1 cjs struct exec ex, ex_swap;
437 1.4 lukem u_int32_t mid = -1;
438 1.4 lukem u_int32_t magic, clbytes, clofset;
439 1.1 cjs
440 1.1 cjs if (read(fd, (char *)&ex, sizeof(ex)) != sizeof(ex))
441 1.1 cjs return(-1);
442 1.1 cjs
443 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
444 1.1 cjs
445 1.1 cjs if (read(fd, (char *)&ex_swap, sizeof(ex_swap)) != sizeof(ex_swap))
446 1.1 cjs return(-1);
447 1.1 cjs
448 1.1 cjs mopFileSwapX((u_char *)&ex_swap, 0, 4);
449 1.1 cjs
450 1.1 cjs mid = getMID(mid, N_GETMID (ex));
451 1.1 cjs
452 1.1 cjs if (mid == -1) {
453 1.1 cjs mid = getMID(mid, N_GETMID (ex_swap));
454 1.1 cjs if (mid != -1) {
455 1.1 cjs mopFileSwapX((u_char *)&ex, 0, 4);
456 1.1 cjs }
457 1.1 cjs }
458 1.1 cjs
459 1.1 cjs if (mid == -1) {
460 1.1 cjs return(-1);
461 1.1 cjs }
462 1.1 cjs
463 1.1 cjs if (N_BADMAG (ex)) {
464 1.1 cjs return(-1);
465 1.1 cjs }
466 1.1 cjs
467 1.1 cjs switch (mid) {
468 1.1 cjs case MID_I386:
469 1.1 cjs #ifdef MID_NS32532
470 1.1 cjs case MID_NS32532:
471 1.1 cjs #endif
472 1.1 cjs #ifdef MID_PMAX
473 1.1 cjs case MID_PMAX:
474 1.1 cjs #endif
475 1.1 cjs #ifdef MID_VAX
476 1.1 cjs case MID_VAX:
477 1.1 cjs #endif
478 1.1 cjs #ifdef MID_ALPHA
479 1.1 cjs case MID_ALPHA:
480 1.1 cjs #endif
481 1.1 cjs #ifdef MID_ARM6
482 1.1 cjs case MID_ARM6:
483 1.1 cjs #endif
484 1.1 cjs ex.a_text = mopFileGetLX((u_char *)&ex_swap, 4, 4);
485 1.1 cjs ex.a_data = mopFileGetLX((u_char *)&ex_swap, 8, 4);
486 1.1 cjs ex.a_bss = mopFileGetLX((u_char *)&ex_swap, 12, 4);
487 1.1 cjs ex.a_syms = mopFileGetLX((u_char *)&ex_swap, 16, 4);
488 1.1 cjs ex.a_entry = mopFileGetLX((u_char *)&ex_swap, 20, 4);
489 1.1 cjs ex.a_trsize= mopFileGetLX((u_char *)&ex_swap, 24, 4);
490 1.1 cjs ex.a_drsize= mopFileGetLX((u_char *)&ex_swap, 28, 4);
491 1.1 cjs break;
492 1.1 cjs #ifdef MID_M68K
493 1.1 cjs case MID_M68K:
494 1.1 cjs #endif
495 1.1 cjs #ifdef MID_M68K4K
496 1.1 cjs case MID_M68K4K:
497 1.1 cjs #endif
498 1.1 cjs case MID_SPARC:
499 1.1 cjs #ifdef MID_MIPS
500 1.1 cjs case MID_MIPS:
501 1.1 cjs #endif
502 1.1 cjs ex.a_text = mopFileGetBX((u_char *)&ex_swap, 4, 4);
503 1.1 cjs ex.a_data = mopFileGetBX((u_char *)&ex_swap, 8, 4);
504 1.1 cjs ex.a_bss = mopFileGetBX((u_char *)&ex_swap, 12, 4);
505 1.1 cjs ex.a_syms = mopFileGetBX((u_char *)&ex_swap, 16, 4);
506 1.1 cjs ex.a_entry = mopFileGetBX((u_char *)&ex_swap, 20, 4);
507 1.1 cjs ex.a_trsize= mopFileGetBX((u_char *)&ex_swap, 24, 4);
508 1.1 cjs ex.a_drsize= mopFileGetBX((u_char *)&ex_swap, 28, 4);
509 1.1 cjs break;
510 1.1 cjs default:
511 1.1 cjs }
512 1.1 cjs
513 1.1 cjs printf("a.out image (");
514 1.1 cjs switch (N_GETMID (ex)) {
515 1.1 cjs case MID_I386:
516 1.1 cjs printf("i386");
517 1.1 cjs break;
518 1.1 cjs #ifdef MID_M68K
519 1.1 cjs case MID_M68K:
520 1.1 cjs printf("m68k");
521 1.1 cjs break;
522 1.1 cjs #endif
523 1.1 cjs #ifdef MID_M68K4K
524 1.1 cjs case MID_M68K4K:
525 1.1 cjs printf("m68k 4k");
526 1.1 cjs break;
527 1.1 cjs #endif
528 1.1 cjs #ifdef MID_NS32532
529 1.1 cjs case MID_NS32532:
530 1.1 cjs printf("pc532");
531 1.1 cjs break;
532 1.1 cjs #endif
533 1.1 cjs case MID_SPARC:
534 1.1 cjs printf("sparc");
535 1.1 cjs break;
536 1.1 cjs #ifdef MID_PMAX
537 1.1 cjs case MID_PMAX:
538 1.1 cjs printf("pmax");
539 1.1 cjs break;
540 1.1 cjs #endif
541 1.1 cjs #ifdef MID_VAX
542 1.1 cjs case MID_VAX:
543 1.1 cjs printf("vax");
544 1.1 cjs break;
545 1.1 cjs #endif
546 1.1 cjs #ifdef MID_ALPHA
547 1.1 cjs case MID_ALPHA:
548 1.1 cjs printf("alpha");
549 1.1 cjs break;
550 1.1 cjs #endif
551 1.1 cjs #ifdef MID_MIPS
552 1.1 cjs case MID_MIPS:
553 1.1 cjs printf("mips");
554 1.1 cjs break;
555 1.1 cjs #endif
556 1.1 cjs #ifdef MID_ARM6
557 1.1 cjs case MID_ARM6:
558 1.1 cjs printf("arm32");
559 1.1 cjs break;
560 1.1 cjs #endif
561 1.1 cjs default:
562 1.1 cjs }
563 1.1 cjs printf(") Magic: ");
564 1.1 cjs switch (N_GETMAGIC (ex)) {
565 1.1 cjs case OMAGIC:
566 1.1 cjs printf("OMAGIC");
567 1.1 cjs break;
568 1.1 cjs case NMAGIC:
569 1.1 cjs printf("NMAGIC");
570 1.1 cjs break;
571 1.1 cjs case ZMAGIC:
572 1.1 cjs printf("ZMAGIC");
573 1.1 cjs break;
574 1.1 cjs case QMAGIC:
575 1.1 cjs printf("QMAGIC");
576 1.1 cjs break;
577 1.1 cjs default:
578 1.4 lukem printf("Unknown %ld", (long) N_GETMAGIC (ex));
579 1.1 cjs }
580 1.1 cjs printf("\n");
581 1.4 lukem printf("Size of text: %08lx\n", (long)ex.a_text);
582 1.4 lukem printf("Size of data: %08lx\n", (long)ex.a_data);
583 1.4 lukem printf("Size of bss: %08lx\n", (long)ex.a_bss);
584 1.4 lukem printf("Size of symbol tab: %08lx\n", (long)ex.a_syms);
585 1.4 lukem printf("Transfer Address: %08lx\n", (long)ex.a_entry);
586 1.4 lukem printf("Size of reloc text: %08lx\n", (long)ex.a_trsize);
587 1.4 lukem printf("Size of reloc data: %08lx\n", (long)ex.a_drsize);
588 1.3 lukem
589 1.1 cjs magic = N_GETMAGIC (ex);
590 1.1 cjs clbytes = getCLBYTES(mid);
591 1.1 cjs clofset = clbytes - 1;
592 1.1 cjs
593 1.1 cjs if (load != NULL) {
594 1.1 cjs *load = 0;
595 1.1 cjs }
596 1.1 cjs
597 1.1 cjs if (xfr != NULL) {
598 1.1 cjs *xfr = ex.a_entry;
599 1.1 cjs }
600 1.1 cjs
601 1.1 cjs if (a_text != NULL) {
602 1.1 cjs *a_text = ex.a_text;
603 1.1 cjs }
604 1.1 cjs
605 1.1 cjs if (a_text_fill != NULL) {
606 1.1 cjs if (magic == ZMAGIC || magic == NMAGIC) {
607 1.1 cjs *a_text_fill = clbytes - (ex.a_text & clofset);
608 1.1 cjs if (*a_text_fill == clbytes) {
609 1.1 cjs *a_text_fill = 0;
610 1.1 cjs }
611 1.1 cjs } else {
612 1.1 cjs *a_text_fill = 0;
613 1.1 cjs }
614 1.1 cjs }
615 1.1 cjs
616 1.1 cjs if (a_data != NULL) {
617 1.1 cjs *a_data = ex.a_data;
618 1.1 cjs }
619 1.1 cjs
620 1.1 cjs if (a_data_fill != NULL) {
621 1.1 cjs if (magic == ZMAGIC || magic == NMAGIC) {
622 1.1 cjs *a_data_fill = clbytes - (ex.a_data & clofset);
623 1.1 cjs if (*a_data_fill == clbytes) {
624 1.1 cjs *a_data_fill = 0;
625 1.1 cjs }
626 1.1 cjs } else {
627 1.1 cjs *a_data_fill = 0;
628 1.1 cjs }
629 1.1 cjs }
630 1.1 cjs
631 1.1 cjs if (a_bss != NULL) {
632 1.1 cjs *a_bss = ex.a_bss;
633 1.1 cjs }
634 1.1 cjs
635 1.1 cjs if (a_bss_fill != NULL) {
636 1.1 cjs if (magic == ZMAGIC || magic == NMAGIC) {
637 1.1 cjs *a_bss_fill = clbytes - (ex.a_bss & clofset);
638 1.1 cjs if (*a_bss_fill == clbytes) {
639 1.1 cjs *a_bss_fill = 0;
640 1.1 cjs }
641 1.1 cjs } else {
642 1.1 cjs *a_bss_fill = clbytes -
643 1.1 cjs ((ex.a_text+ex.a_data+ex.a_bss) & clofset);
644 1.1 cjs if (*a_text_fill == clbytes) {
645 1.1 cjs *a_text_fill = 0;
646 1.1 cjs }
647 1.1 cjs }
648 1.1 cjs }
649 1.1 cjs
650 1.1 cjs if (aout != NULL) {
651 1.1 cjs *aout = mid;
652 1.1 cjs }
653 1.1 cjs
654 1.1 cjs return(0);
655 1.1 cjs #endif NOAOUT
656 1.1 cjs }
657 1.1 cjs
658 1.1 cjs int
659 1.1 cjs GetFileInfo(fd, load, xfr, aout,
660 1.1 cjs a_text, a_text_fill, a_data, a_data_fill, a_bss, a_bss_fill)
661 1.1 cjs int fd, *aout;
662 1.4 lukem u_int32_t *load, *xfr, *a_text, *a_text_fill;
663 1.4 lukem u_int32_t *a_data, *a_data_fill, *a_bss, *a_bss_fill;
664 1.1 cjs {
665 1.1 cjs int err;
666 1.1 cjs
667 1.1 cjs err = CheckAOutFile(fd);
668 1.1 cjs
669 1.1 cjs if (err == 0) {
670 1.1 cjs err = GetAOutFileInfo(fd, load, xfr,
671 1.1 cjs a_text, a_text_fill,
672 1.1 cjs a_data, a_data_fill,
673 1.1 cjs a_bss, a_bss_fill,
674 1.1 cjs aout);
675 1.1 cjs if (err != 0) {
676 1.1 cjs return(-1);
677 1.1 cjs }
678 1.1 cjs } else {
679 1.1 cjs err = CheckMopFile(fd);
680 1.1 cjs
681 1.1 cjs if (err == 0) {
682 1.1 cjs err = GetMopFileInfo(fd, load, xfr);
683 1.1 cjs if (err != 0) {
684 1.1 cjs return(-1);
685 1.1 cjs }
686 1.1 cjs *aout = -1;
687 1.1 cjs } else {
688 1.1 cjs return(-1);
689 1.1 cjs }
690 1.1 cjs }
691 1.1 cjs
692 1.1 cjs return(0);
693 1.1 cjs }
694 1.1 cjs
695 1.1 cjs ssize_t
696 1.1 cjs mopFileRead(dlslot, buf)
697 1.1 cjs struct dllist *dlslot;
698 1.1 cjs u_char *buf;
699 1.1 cjs {
700 1.1 cjs ssize_t len, outlen;
701 1.1 cjs int bsz;
702 1.4 lukem int32_t pos, notdone, total;
703 1.1 cjs
704 1.1 cjs if (dlslot->aout == -1) {
705 1.1 cjs len = read(dlslot->ldfd,buf,dlslot->dl_bsz);
706 1.1 cjs } else {
707 1.1 cjs bsz = dlslot->dl_bsz;
708 1.1 cjs pos = dlslot->a_lseek;
709 1.1 cjs len = 0;
710 1.1 cjs
711 1.1 cjs total = dlslot->a_text;
712 1.1 cjs
713 1.1 cjs if (pos < total) {
714 1.1 cjs notdone = total - pos;
715 1.1 cjs if (notdone <= bsz) {
716 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],notdone);
717 1.1 cjs } else {
718 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],bsz);
719 1.1 cjs }
720 1.1 cjs len = len + outlen;
721 1.1 cjs pos = pos + outlen;
722 1.1 cjs bsz = bsz - outlen;
723 1.1 cjs }
724 1.1 cjs
725 1.1 cjs total = total + dlslot->a_text_fill;
726 1.1 cjs
727 1.1 cjs if ((bsz > 0) && (pos < total)) {
728 1.1 cjs notdone = total - pos;
729 1.1 cjs if (notdone <= bsz) {
730 1.1 cjs outlen = notdone;
731 1.1 cjs } else {
732 1.1 cjs outlen = bsz;
733 1.1 cjs }
734 1.4 lukem memset(&buf[len], 0, outlen);
735 1.1 cjs len = len + outlen;
736 1.1 cjs pos = pos + outlen;
737 1.1 cjs bsz = bsz - outlen;
738 1.1 cjs }
739 1.1 cjs
740 1.1 cjs total = total + dlslot->a_data;
741 1.1 cjs
742 1.1 cjs if ((bsz > 0) && (pos < total)) {
743 1.1 cjs notdone = total - pos;
744 1.1 cjs if (notdone <= bsz) {
745 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],notdone);
746 1.1 cjs } else {
747 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],bsz);
748 1.1 cjs }
749 1.1 cjs len = len + outlen;
750 1.1 cjs pos = pos + outlen;
751 1.1 cjs bsz = bsz - outlen;
752 1.1 cjs }
753 1.1 cjs
754 1.1 cjs total = total + dlslot->a_data_fill;
755 1.1 cjs
756 1.1 cjs if ((bsz > 0) && (pos < total)) {
757 1.1 cjs notdone = total - pos;
758 1.1 cjs if (notdone <= bsz) {
759 1.1 cjs outlen = notdone;
760 1.1 cjs } else {
761 1.1 cjs outlen = bsz;
762 1.1 cjs }
763 1.4 lukem memset(&buf[len], 0, outlen);
764 1.1 cjs len = len + outlen;
765 1.1 cjs pos = pos + outlen;
766 1.1 cjs bsz = bsz - outlen;
767 1.1 cjs }
768 1.1 cjs
769 1.1 cjs total = total + dlslot->a_bss;
770 1.1 cjs
771 1.1 cjs if ((bsz > 0) && (pos < total)) {
772 1.1 cjs notdone = total - pos;
773 1.1 cjs if (notdone <= bsz) {
774 1.1 cjs outlen = notdone;
775 1.1 cjs } else {
776 1.1 cjs outlen = bsz;
777 1.1 cjs }
778 1.4 lukem memset(&buf[len], 0, outlen);
779 1.1 cjs len = len + outlen;
780 1.1 cjs pos = pos + outlen;
781 1.1 cjs bsz = bsz - outlen;
782 1.1 cjs }
783 1.1 cjs
784 1.1 cjs total = total + dlslot->a_bss_fill;
785 1.1 cjs
786 1.1 cjs if ((bsz > 0) && (pos < total)) {
787 1.1 cjs notdone = total - pos;
788 1.1 cjs if (notdone <= bsz) {
789 1.1 cjs outlen = notdone;
790 1.1 cjs } else {
791 1.1 cjs outlen = bsz;
792 1.1 cjs }
793 1.4 lukem memset(&buf[len], 0, outlen);
794 1.1 cjs len = len + outlen;
795 1.1 cjs pos = pos + outlen;
796 1.1 cjs bsz = bsz - outlen;
797 1.1 cjs }
798 1.1 cjs
799 1.1 cjs dlslot->a_lseek = pos;
800 1.1 cjs
801 1.1 cjs }
802 1.1 cjs
803 1.1 cjs return(len);
804 1.1 cjs }
805