file.c revision 1.14 1 1.14 joerg /* $NetBSD: file.c,v 1.14 2011/08/30 19:49:10 joerg 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 *
15 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 cjs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 cjs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 cjs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 cjs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 cjs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 cjs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 cjs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 cjs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 cjs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 cjs */
26 1.1 cjs
27 1.4 lukem #include <sys/cdefs.h>
28 1.4 lukem #ifndef lint
29 1.14 joerg __RCSID("$NetBSD: file.c,v 1.14 2011/08/30 19:49:10 joerg Exp $");
30 1.1 cjs #endif
31 1.1 cjs
32 1.1 cjs #include "os.h"
33 1.4 lukem #include "common.h"
34 1.4 lukem #include "file.h"
35 1.4 lukem #include "mopdef.h"
36 1.8 thorpej #include <stddef.h>
37 1.1 cjs
38 1.1 cjs #ifndef NOAOUT
39 1.8 thorpej # if defined(__NetBSD__) || defined(__OpenBSD__)
40 1.8 thorpej # include <sys/exec_aout.h>
41 1.8 thorpej # endif
42 1.8 thorpej # if defined(__bsdi__)
43 1.8 thorpej # define NOAOUT
44 1.8 thorpej # endif
45 1.8 thorpej # if defined(__FreeBSD__)
46 1.8 thorpej # include <sys/imgact_aout.h>
47 1.8 thorpej # endif
48 1.8 thorpej # if !defined(MID_VAX)
49 1.8 thorpej # define MID_VAX 140
50 1.8 thorpej # endif
51 1.8 thorpej #endif /* NOAOUT */
52 1.8 thorpej
53 1.8 thorpej #ifndef NOELF
54 1.8 thorpej # if defined(__NetBSD__)
55 1.8 thorpej # include <sys/exec_elf.h>
56 1.8 thorpej # else
57 1.8 thorpej # define NOELF
58 1.8 thorpej # endif
59 1.8 thorpej #endif /* NOELF */
60 1.1 cjs
61 1.14 joerg static int getCLBYTES(int);
62 1.14 joerg static int getMID(int, int);
63 1.4 lukem
64 1.9 thorpej const char *
65 1.14 joerg FileTypeName(mopd_imagetype type)
66 1.9 thorpej {
67 1.9 thorpej
68 1.9 thorpej switch (type) {
69 1.9 thorpej case IMAGE_TYPE_MOP:
70 1.9 thorpej return ("MOP");
71 1.9 thorpej
72 1.9 thorpej case IMAGE_TYPE_ELF32:
73 1.9 thorpej return ("Elf32");
74 1.9 thorpej
75 1.9 thorpej case IMAGE_TYPE_AOUT:
76 1.9 thorpej return ("a.out");
77 1.9 thorpej }
78 1.9 thorpej
79 1.9 thorpej abort();
80 1.9 thorpej }
81 1.9 thorpej
82 1.1 cjs void
83 1.14 joerg mopFilePutLX(u_char *buf, int idx, u_int32_t value, int cnt)
84 1.1 cjs {
85 1.1 cjs int i;
86 1.1 cjs for (i = 0; i < cnt; i++) {
87 1.11 lukem buf[idx+i] = value % 256;
88 1.1 cjs value = value / 256;
89 1.1 cjs }
90 1.1 cjs }
91 1.1 cjs
92 1.1 cjs void
93 1.14 joerg mopFilePutBX(u_char *buf, int idx, u_int32_t value, int cnt)
94 1.1 cjs {
95 1.1 cjs int i;
96 1.1 cjs for (i = 0; i < cnt; i++) {
97 1.11 lukem buf[idx+cnt-1-i] = value % 256;
98 1.1 cjs value = value / 256;
99 1.1 cjs }
100 1.1 cjs }
101 1.1 cjs
102 1.4 lukem u_int32_t
103 1.14 joerg mopFileGetLX(u_char *buf, int idx, int cnt)
104 1.1 cjs {
105 1.4 lukem u_int32_t ret = 0;
106 1.1 cjs int i;
107 1.1 cjs
108 1.1 cjs for (i = 0; i < cnt; i++) {
109 1.13 christos int j = idx + cnt - 1 - i;
110 1.13 christos if (j < 0)
111 1.13 christos abort();
112 1.13 christos ret = ret * 256 + buf[j];
113 1.1 cjs }
114 1.1 cjs
115 1.1 cjs return(ret);
116 1.1 cjs }
117 1.1 cjs
118 1.4 lukem u_int32_t
119 1.14 joerg mopFileGetBX(u_char *buf, int idx, int cnt)
120 1.1 cjs {
121 1.4 lukem u_int32_t ret = 0;
122 1.1 cjs int i;
123 1.1 cjs
124 1.1 cjs for (i = 0; i < cnt; i++) {
125 1.13 christos int j = idx + i;
126 1.13 christos if (j < 0)
127 1.13 christos abort();
128 1.13 christos ret = ret * 256 + buf[j];
129 1.1 cjs }
130 1.1 cjs
131 1.1 cjs return(ret);
132 1.1 cjs }
133 1.1 cjs
134 1.1 cjs void
135 1.14 joerg mopFileSwapX(u_char *buf, int idx, int cnt)
136 1.1 cjs {
137 1.1 cjs int i;
138 1.1 cjs u_char c;
139 1.1 cjs
140 1.1 cjs for (i = 0; i < (cnt / 2); i++) {
141 1.11 lukem c = buf[idx+i];
142 1.11 lukem buf[idx+i] = buf[idx+cnt-1-i];
143 1.11 lukem buf[idx+cnt-1-i] = c;
144 1.1 cjs }
145 1.1 cjs
146 1.1 cjs }
147 1.1 cjs
148 1.1 cjs int
149 1.14 joerg CheckMopFile(int fd)
150 1.1 cjs {
151 1.1 cjs u_char header[512];
152 1.1 cjs short image_type;
153 1.1 cjs
154 1.1 cjs if (read(fd, header, 512) != 512)
155 1.1 cjs return(-1);
156 1.1 cjs
157 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
158 1.1 cjs
159 1.1 cjs image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
160 1.1 cjs header[IHD_W_ALIAS]);
161 1.1 cjs
162 1.1 cjs switch(image_type) {
163 1.1 cjs case IHD_C_NATIVE: /* Native mode image (VAX) */
164 1.1 cjs case IHD_C_RSX: /* RSX image produced by TKB */
165 1.1 cjs case IHD_C_BPA: /* BASIC plus analog */
166 1.1 cjs case IHD_C_ALIAS: /* Alias */
167 1.1 cjs case IHD_C_CLI: /* Image is CLI */
168 1.1 cjs case IHD_C_PMAX: /* PMAX system image */
169 1.1 cjs case IHD_C_ALPHA: /* ALPHA system image */
170 1.1 cjs break;
171 1.1 cjs default:
172 1.1 cjs return(-1);
173 1.1 cjs }
174 1.1 cjs
175 1.1 cjs return(0);
176 1.1 cjs }
177 1.1 cjs
178 1.1 cjs int
179 1.14 joerg GetMopFileInfo(struct dllist *dl)
180 1.1 cjs {
181 1.4 lukem u_char header[512];
182 1.4 lukem short image_type;
183 1.4 lukem u_int32_t load_addr, xfr_addr, isd, iha, hbcnt, isize;
184 1.1 cjs
185 1.8 thorpej if (read(dl->ldfd, header, 512) != 512)
186 1.1 cjs return(-1);
187 1.1 cjs
188 1.1 cjs image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
189 1.1 cjs header[IHD_W_ALIAS]);
190 1.1 cjs
191 1.1 cjs switch(image_type) {
192 1.1 cjs case IHD_C_NATIVE: /* Native mode image (VAX) */
193 1.1 cjs isd = (header[IHD_W_SIZE+1]*256 +
194 1.1 cjs header[IHD_W_SIZE]);
195 1.1 cjs iha = (header[IHD_W_ACTIVOFF+1]*256 +
196 1.1 cjs header[IHD_W_ACTIVOFF]);
197 1.1 cjs hbcnt = (header[IHD_B_HDRBLKCNT]);
198 1.1 cjs isize = (header[isd+ISD_W_PAGCNT+1]*256 +
199 1.1 cjs header[isd+ISD_W_PAGCNT]) * 512;
200 1.1 cjs load_addr = ((header[isd+ISD_V_VPN+1]*256 +
201 1.1 cjs header[isd+ISD_V_VPN]) & ISD_M_VPN)
202 1.1 cjs * 512;
203 1.1 cjs xfr_addr = (header[iha+IHA_L_TFRADR1+3]*0x1000000 +
204 1.1 cjs header[iha+IHA_L_TFRADR1+2]*0x10000 +
205 1.1 cjs header[iha+IHA_L_TFRADR1+1]*0x100 +
206 1.1 cjs header[iha+IHA_L_TFRADR1]) & 0x7fffffff;
207 1.1 cjs printf("Native Image (VAX)\n");
208 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
209 1.1 cjs printf("Image Size: %08x\n",isize);
210 1.1 cjs printf("Load Address: %08x\n",load_addr);
211 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
212 1.1 cjs break;
213 1.1 cjs case IHD_C_RSX: /* RSX image produced by TKB */
214 1.1 cjs hbcnt = header[L_BBLK+1]*256 + header[L_BBLK];
215 1.1 cjs isize = (header[L_BLDZ+1]*256 + header[L_BLDZ]) * 64;
216 1.1 cjs load_addr = header[L_BSA+1]*256 + header[L_BSA];
217 1.1 cjs xfr_addr = header[L_BXFR+1]*256 + header[L_BXFR];
218 1.1 cjs printf("RSX Image\n");
219 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
220 1.1 cjs printf("Image Size: %08x\n",isize);
221 1.1 cjs printf("Load Address: %08x\n",load_addr);
222 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
223 1.1 cjs break;
224 1.1 cjs case IHD_C_BPA: /* BASIC plus analog */
225 1.1 cjs printf("BASIC-Plus Image, not supported\n");
226 1.1 cjs return(-1);
227 1.1 cjs break;
228 1.1 cjs case IHD_C_ALIAS: /* Alias */
229 1.1 cjs printf("Alias, not supported\n");
230 1.1 cjs return(-1);
231 1.1 cjs break;
232 1.1 cjs case IHD_C_CLI: /* Image is CLI */
233 1.1 cjs printf("CLI, not supported\n");
234 1.1 cjs return(-1);
235 1.1 cjs break;
236 1.1 cjs case IHD_C_PMAX: /* PMAX system image */
237 1.1 cjs isd = (header[IHD_W_SIZE+1]*256 +
238 1.1 cjs header[IHD_W_SIZE]);
239 1.1 cjs iha = (header[IHD_W_ACTIVOFF+1]*256 +
240 1.1 cjs header[IHD_W_ACTIVOFF]);
241 1.1 cjs hbcnt = (header[IHD_B_HDRBLKCNT]);
242 1.1 cjs isize = (header[isd+ISD_W_PAGCNT+1]*256 +
243 1.1 cjs header[isd+ISD_W_PAGCNT]) * 512;
244 1.1 cjs load_addr = (header[isd+ISD_V_VPN+1]*256 +
245 1.1 cjs header[isd+ISD_V_VPN]) * 512;
246 1.1 cjs xfr_addr = (header[iha+IHA_L_TFRADR1+3]*0x1000000 +
247 1.1 cjs header[iha+IHA_L_TFRADR1+2]*0x10000 +
248 1.1 cjs header[iha+IHA_L_TFRADR1+1]*0x100 +
249 1.1 cjs header[iha+IHA_L_TFRADR1]);
250 1.1 cjs printf("PMAX Image \n");
251 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
252 1.1 cjs printf("Image Size: %08x\n",isize);
253 1.1 cjs printf("Load Address: %08x\n",load_addr);
254 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
255 1.1 cjs break;
256 1.1 cjs case IHD_C_ALPHA: /* ALPHA system image */
257 1.1 cjs isd = (header[EIHD_L_ISDOFF+3]*0x1000000 +
258 1.1 cjs header[EIHD_L_ISDOFF+2]*0x10000 +
259 1.1 cjs header[EIHD_L_ISDOFF+1]*0x100 +
260 1.1 cjs header[EIHD_L_ISDOFF]);
261 1.1 cjs hbcnt = (header[EIHD_L_HDRBLKCNT+3]*0x1000000 +
262 1.1 cjs header[EIHD_L_HDRBLKCNT+2]*0x10000 +
263 1.1 cjs header[EIHD_L_HDRBLKCNT+1]*0x100 +
264 1.1 cjs header[EIHD_L_HDRBLKCNT]);
265 1.1 cjs isize = (header[isd+EISD_L_SECSIZE+3]*0x1000000 +
266 1.1 cjs header[isd+EISD_L_SECSIZE+2]*0x10000 +
267 1.1 cjs header[isd+EISD_L_SECSIZE+1]*0x100 +
268 1.1 cjs header[isd+EISD_L_SECSIZE]);
269 1.1 cjs load_addr = 0;
270 1.1 cjs xfr_addr = 0;
271 1.1 cjs printf("Alpha Image \n");
272 1.1 cjs printf("Header Block Count: %d\n",hbcnt);
273 1.1 cjs printf("Image Size: %08x\n",isize);
274 1.1 cjs printf("Load Address: %08x\n",load_addr);
275 1.1 cjs printf("Transfer Address: %08x\n",xfr_addr);
276 1.1 cjs break;
277 1.1 cjs default:
278 1.1 cjs printf("Unknown Image (%d)\n",image_type);
279 1.1 cjs return(-1);
280 1.1 cjs }
281 1.1 cjs
282 1.8 thorpej dl->image_type = IMAGE_TYPE_MOP;
283 1.8 thorpej dl->loadaddr = load_addr;
284 1.8 thorpej dl->xferaddr = xfr_addr;
285 1.1 cjs
286 1.1 cjs return(0);
287 1.1 cjs }
288 1.1 cjs
289 1.1 cjs #ifndef NOAOUT
290 1.14 joerg static int
291 1.14 joerg getMID(int old_mid, int new_mid)
292 1.1 cjs {
293 1.1 cjs int mid;
294 1.1 cjs
295 1.1 cjs mid = old_mid;
296 1.1 cjs
297 1.1 cjs switch (new_mid) {
298 1.1 cjs case MID_I386:
299 1.1 cjs mid = MID_I386;
300 1.1 cjs break;
301 1.1 cjs #ifdef MID_M68K
302 1.1 cjs case MID_M68K:
303 1.1 cjs mid = MID_M68K;
304 1.1 cjs break;
305 1.1 cjs #endif
306 1.1 cjs #ifdef MID_M68K4K
307 1.1 cjs case MID_M68K4K:
308 1.1 cjs mid = MID_M68K4K;
309 1.1 cjs break;
310 1.1 cjs #endif
311 1.1 cjs #ifdef MID_NS32532
312 1.1 cjs case MID_NS32532:
313 1.1 cjs mid = MID_NS32532;
314 1.1 cjs break;
315 1.1 cjs #endif
316 1.1 cjs case MID_SPARC:
317 1.1 cjs mid = MID_SPARC;
318 1.1 cjs break;
319 1.1 cjs #ifdef MID_PMAX
320 1.1 cjs case MID_PMAX:
321 1.1 cjs mid = MID_PMAX;
322 1.1 cjs break;
323 1.1 cjs #endif
324 1.1 cjs #ifdef MID_VAX
325 1.1 cjs case MID_VAX:
326 1.1 cjs mid = MID_VAX;
327 1.1 cjs break;
328 1.1 cjs #endif
329 1.1 cjs #ifdef MID_ALPHA
330 1.1 cjs case MID_ALPHA:
331 1.1 cjs mid = MID_ALPHA;
332 1.1 cjs break;
333 1.1 cjs #endif
334 1.1 cjs #ifdef MID_MIPS
335 1.1 cjs case MID_MIPS:
336 1.1 cjs mid = MID_MIPS;
337 1.1 cjs break;
338 1.1 cjs #endif
339 1.1 cjs #ifdef MID_ARM6
340 1.1 cjs case MID_ARM6:
341 1.1 cjs mid = MID_ARM6;
342 1.1 cjs break;
343 1.1 cjs #endif
344 1.1 cjs default:
345 1.5 cgd break;
346 1.1 cjs }
347 1.1 cjs
348 1.1 cjs return(mid);
349 1.1 cjs }
350 1.1 cjs
351 1.14 joerg static int
352 1.14 joerg getCLBYTES(int mid)
353 1.1 cjs {
354 1.1 cjs int clbytes;
355 1.1 cjs
356 1.1 cjs switch (mid) {
357 1.1 cjs #ifdef MID_VAX
358 1.1 cjs case MID_VAX:
359 1.1 cjs clbytes = 1024;
360 1.1 cjs break;
361 1.1 cjs #endif
362 1.6 mycroft #ifdef MID_I386
363 1.1 cjs case MID_I386:
364 1.6 mycroft #endif
365 1.1 cjs #ifdef MID_M68K4K
366 1.1 cjs case MID_M68K4K:
367 1.1 cjs #endif
368 1.1 cjs #ifdef MID_NS32532
369 1.1 cjs case MID_NS32532:
370 1.1 cjs #endif
371 1.1 cjs #ifdef MID_PMAX
372 1.1 cjs case MID_PMAX:
373 1.1 cjs #endif
374 1.1 cjs #ifdef MID_MIPS
375 1.1 cjs case MID_MIPS:
376 1.1 cjs #endif
377 1.1 cjs #ifdef MID_ARM6
378 1.1 cjs case MID_ARM6:
379 1.1 cjs #endif
380 1.6 mycroft #if defined(MID_I386) || defined(MID_M68K4K) || defined(MID_NS32532) || \
381 1.6 mycroft defined(MID_PMAX) || defined(MID_MIPS) || defined(MID_ARM6)
382 1.1 cjs clbytes = 4096;
383 1.1 cjs break;
384 1.6 mycroft #endif
385 1.1 cjs #ifdef MID_M68K
386 1.1 cjs case MID_M68K:
387 1.1 cjs #endif
388 1.1 cjs #ifdef MID_ALPHA
389 1.1 cjs case MID_ALPHA:
390 1.1 cjs #endif
391 1.6 mycroft #ifdef MID_SPARC
392 1.6 mycroft case MID_SPARC:
393 1.6 mycroft #endif
394 1.6 mycroft #if defined(MID_M68K) || defined(MID_ALPHA) || defined(MID_SPARC)
395 1.1 cjs clbytes = 8192;
396 1.1 cjs break;
397 1.1 cjs #endif
398 1.1 cjs default:
399 1.1 cjs clbytes = 0;
400 1.1 cjs }
401 1.1 cjs
402 1.1 cjs return(clbytes);
403 1.1 cjs }
404 1.1 cjs #endif
405 1.1 cjs
406 1.1 cjs int
407 1.14 joerg CheckElfFile(int fd)
408 1.8 thorpej {
409 1.8 thorpej #ifdef NOELF
410 1.8 thorpej return(-1);
411 1.8 thorpej #else
412 1.8 thorpej Elf32_Ehdr ehdr;
413 1.8 thorpej
414 1.8 thorpej (void)lseek(fd, (off_t) 0, SEEK_SET);
415 1.8 thorpej
416 1.8 thorpej if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
417 1.8 thorpej return(-1);
418 1.8 thorpej
419 1.8 thorpej if (ehdr.e_ident[0] != ELFMAG0 ||
420 1.8 thorpej ehdr.e_ident[1] != ELFMAG1 ||
421 1.8 thorpej ehdr.e_ident[2] != ELFMAG2 ||
422 1.8 thorpej ehdr.e_ident[3] != ELFMAG3)
423 1.8 thorpej return(-1);
424 1.8 thorpej
425 1.8 thorpej /* Must be Elf32... */
426 1.8 thorpej if (ehdr.e_ident[EI_CLASS] != ELFCLASS32)
427 1.8 thorpej return(-1);
428 1.8 thorpej
429 1.8 thorpej return(0);
430 1.8 thorpej #endif /* NOELF */
431 1.8 thorpej }
432 1.8 thorpej
433 1.8 thorpej int
434 1.14 joerg GetElfFileInfo(struct dllist *dl)
435 1.8 thorpej {
436 1.8 thorpej #ifdef NOELF
437 1.8 thorpej return(-1);
438 1.8 thorpej #else
439 1.8 thorpej Elf32_Ehdr ehdr;
440 1.8 thorpej Elf32_Phdr phdr;
441 1.8 thorpej uint32_t e_machine, e_entry;
442 1.8 thorpej uint32_t e_phoff, e_phentsize, e_phnum;
443 1.8 thorpej int ei_data, i;
444 1.8 thorpej
445 1.8 thorpej (void)lseek(dl->ldfd, (off_t) 0, SEEK_SET);
446 1.8 thorpej
447 1.8 thorpej if (read(dl->ldfd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
448 1.8 thorpej return(-1);
449 1.8 thorpej
450 1.8 thorpej if (ehdr.e_ident[0] != ELFMAG0 ||
451 1.8 thorpej ehdr.e_ident[1] != ELFMAG1 ||
452 1.8 thorpej ehdr.e_ident[2] != ELFMAG2 ||
453 1.8 thorpej ehdr.e_ident[3] != ELFMAG3)
454 1.8 thorpej return(-1);
455 1.8 thorpej
456 1.8 thorpej /* Must be Elf32... */
457 1.8 thorpej if (ehdr.e_ident[EI_CLASS] != ELFCLASS32)
458 1.8 thorpej return(-1);
459 1.8 thorpej
460 1.8 thorpej ei_data = ehdr.e_ident[EI_DATA];
461 1.8 thorpej
462 1.8 thorpej switch (ei_data) {
463 1.8 thorpej case ELFDATA2LSB:
464 1.8 thorpej e_machine = mopFileGetLX((u_char *) &ehdr,
465 1.8 thorpej offsetof(Elf32_Ehdr, e_machine),
466 1.8 thorpej sizeof(ehdr.e_machine));
467 1.8 thorpej e_entry = mopFileGetLX((u_char *) &ehdr,
468 1.8 thorpej offsetof(Elf32_Ehdr, e_entry),
469 1.8 thorpej sizeof(ehdr.e_entry));
470 1.8 thorpej
471 1.8 thorpej e_phoff = mopFileGetLX((u_char *) &ehdr,
472 1.8 thorpej offsetof(Elf32_Ehdr, e_phoff),
473 1.8 thorpej sizeof(ehdr.e_phoff));
474 1.8 thorpej e_phentsize = mopFileGetLX((u_char *) &ehdr,
475 1.8 thorpej offsetof(Elf32_Ehdr, e_phentsize),
476 1.8 thorpej sizeof(ehdr.e_phentsize));
477 1.8 thorpej e_phnum = mopFileGetLX((u_char *) &ehdr,
478 1.8 thorpej offsetof(Elf32_Ehdr, e_phnum),
479 1.8 thorpej sizeof(ehdr.e_phnum));
480 1.8 thorpej break;
481 1.8 thorpej
482 1.8 thorpej case ELFDATA2MSB:
483 1.8 thorpej e_machine = mopFileGetBX((u_char *) &ehdr,
484 1.8 thorpej offsetof(Elf32_Ehdr, e_machine),
485 1.8 thorpej sizeof(ehdr.e_machine));
486 1.8 thorpej e_entry = mopFileGetBX((u_char *) &ehdr,
487 1.8 thorpej offsetof(Elf32_Ehdr, e_entry),
488 1.8 thorpej sizeof(ehdr.e_entry));
489 1.8 thorpej
490 1.8 thorpej e_phoff = mopFileGetBX((u_char *) &ehdr,
491 1.8 thorpej offsetof(Elf32_Ehdr, e_phoff),
492 1.8 thorpej sizeof(ehdr.e_phoff));
493 1.8 thorpej e_phentsize = mopFileGetBX((u_char *) &ehdr,
494 1.8 thorpej offsetof(Elf32_Ehdr, e_phentsize),
495 1.8 thorpej sizeof(ehdr.e_phentsize));
496 1.8 thorpej e_phnum = mopFileGetBX((u_char *) &ehdr,
497 1.8 thorpej offsetof(Elf32_Ehdr, e_phnum),
498 1.8 thorpej sizeof(ehdr.e_phnum));
499 1.8 thorpej break;
500 1.8 thorpej
501 1.8 thorpej default:
502 1.8 thorpej return(-1);
503 1.8 thorpej }
504 1.8 thorpej
505 1.8 thorpej dl->image_type = IMAGE_TYPE_ELF32;
506 1.10 thorpej dl->loadaddr = 0;
507 1.8 thorpej dl->xferaddr = e_entry; /* will relocate itself if necessary */
508 1.8 thorpej
509 1.8 thorpej if (e_phnum > SEC_MAX)
510 1.8 thorpej return(-1);
511 1.8 thorpej dl->e_nsec = e_phnum;
512 1.8 thorpej for (i = 0; i < dl->e_nsec; i++) {
513 1.8 thorpej if (lseek(dl->ldfd, (off_t) e_phoff + (i * e_phentsize),
514 1.8 thorpej SEEK_SET) == (off_t) -1)
515 1.8 thorpej return(-1);
516 1.8 thorpej if (read(dl->ldfd, (char *) &phdr, sizeof(phdr)) !=
517 1.8 thorpej sizeof(phdr))
518 1.8 thorpej return(-1);
519 1.8 thorpej
520 1.8 thorpej switch (ei_data) {
521 1.8 thorpej case ELFDATA2LSB:
522 1.8 thorpej dl->e_sections[i].s_foff =
523 1.8 thorpej mopFileGetLX((u_char *) &phdr,
524 1.8 thorpej offsetof(Elf32_Phdr, p_offset),
525 1.8 thorpej sizeof(phdr.p_offset));
526 1.8 thorpej dl->e_sections[i].s_vaddr =
527 1.8 thorpej mopFileGetLX((u_char *) &phdr,
528 1.8 thorpej offsetof(Elf32_Phdr, p_vaddr),
529 1.8 thorpej sizeof(phdr.p_vaddr));
530 1.8 thorpej dl->e_sections[i].s_fsize =
531 1.8 thorpej mopFileGetLX((u_char *) &phdr,
532 1.8 thorpej offsetof(Elf32_Phdr, p_filesz),
533 1.8 thorpej sizeof(phdr.p_filesz));
534 1.8 thorpej dl->e_sections[i].s_msize =
535 1.8 thorpej mopFileGetLX((u_char *) &phdr,
536 1.8 thorpej offsetof(Elf32_Phdr, p_memsz),
537 1.8 thorpej sizeof(phdr.p_memsz));
538 1.8 thorpej break;
539 1.8 thorpej
540 1.8 thorpej case ELFDATA2MSB:
541 1.8 thorpej dl->e_sections[i].s_foff =
542 1.8 thorpej mopFileGetBX((u_char *) &phdr,
543 1.8 thorpej offsetof(Elf32_Phdr, p_offset),
544 1.8 thorpej sizeof(phdr.p_offset));
545 1.8 thorpej dl->e_sections[i].s_vaddr =
546 1.8 thorpej mopFileGetBX((u_char *) &phdr,
547 1.8 thorpej offsetof(Elf32_Phdr, p_vaddr),
548 1.8 thorpej sizeof(phdr.p_vaddr));
549 1.8 thorpej dl->e_sections[i].s_fsize =
550 1.8 thorpej mopFileGetBX((u_char *) &phdr,
551 1.8 thorpej offsetof(Elf32_Phdr, p_filesz),
552 1.8 thorpej sizeof(phdr.p_filesz));
553 1.8 thorpej dl->e_sections[i].s_msize =
554 1.8 thorpej mopFileGetBX((u_char *) &phdr,
555 1.8 thorpej offsetof(Elf32_Phdr, p_memsz),
556 1.8 thorpej sizeof(phdr.p_memsz));
557 1.8 thorpej break;
558 1.8 thorpej
559 1.8 thorpej default:
560 1.8 thorpej return(-1);
561 1.8 thorpej }
562 1.8 thorpej }
563 1.8 thorpej /*
564 1.8 thorpej * In addition to padding between segments, this also
565 1.8 thorpej * takes care of memsz > filesz.
566 1.8 thorpej */
567 1.8 thorpej for (i = 0; i < dl->e_nsec - 1; i++) {
568 1.8 thorpej dl->e_sections[i].s_pad =
569 1.8 thorpej dl->e_sections[i + 1].s_vaddr -
570 1.8 thorpej (dl->e_sections[i].s_vaddr + dl->e_sections[i].s_fsize);
571 1.8 thorpej }
572 1.8 thorpej dl->e_sections[dl->e_nsec - 1].s_pad =
573 1.8 thorpej dl->e_sections[dl->e_nsec - 1].s_msize -
574 1.8 thorpej dl->e_sections[dl->e_nsec - 1].s_fsize;
575 1.8 thorpej /*
576 1.8 thorpej * Now compute the logical offsets for each section.
577 1.8 thorpej */
578 1.8 thorpej dl->e_sections[0].s_loff = 0;
579 1.8 thorpej for (i = 1; i < dl->e_nsec; i++) {
580 1.8 thorpej dl->e_sections[i].s_loff =
581 1.8 thorpej dl->e_sections[i - 1].s_loff +
582 1.8 thorpej dl->e_sections[i - 1].s_fsize +
583 1.8 thorpej dl->e_sections[i - 1].s_pad;
584 1.8 thorpej }
585 1.8 thorpej
586 1.8 thorpej /* Print info about the image. */
587 1.8 thorpej printf("Elf32 image (");
588 1.8 thorpej switch (e_machine) {
589 1.8 thorpej #ifdef EM_VAX
590 1.8 thorpej case EM_VAX:
591 1.8 thorpej printf("VAX");
592 1.8 thorpej break;
593 1.8 thorpej #endif
594 1.8 thorpej default:
595 1.8 thorpej printf("machine %d", e_machine);
596 1.8 thorpej break;
597 1.8 thorpej }
598 1.8 thorpej printf(")\n");
599 1.8 thorpej printf("Transfer Address: %08x\n", dl->xferaddr);
600 1.8 thorpej printf("Program Sections: %d\n", dl->e_nsec);
601 1.8 thorpej for (i = 0; i < dl->e_nsec; i++) {
602 1.8 thorpej printf(" S%d File Size: %08x\n", i,
603 1.8 thorpej dl->e_sections[i].s_fsize);
604 1.8 thorpej printf(" S%d Pad Size: %08x\n", i,
605 1.8 thorpej dl->e_sections[i].s_pad);
606 1.8 thorpej }
607 1.9 thorpej dl->e_machine = e_machine;
608 1.8 thorpej
609 1.8 thorpej dl->e_curpos = 0;
610 1.8 thorpej dl->e_cursec = 0;
611 1.8 thorpej
612 1.8 thorpej return(0);
613 1.8 thorpej #endif /* NOELF */
614 1.8 thorpej }
615 1.8 thorpej
616 1.8 thorpej int
617 1.14 joerg CheckAOutFile(int fd)
618 1.1 cjs {
619 1.1 cjs #ifdef NOAOUT
620 1.1 cjs return(-1);
621 1.1 cjs #else
622 1.1 cjs struct exec ex, ex_swap;
623 1.1 cjs int mid = -1;
624 1.1 cjs
625 1.1 cjs if (read(fd, (char *)&ex, sizeof(ex)) != sizeof(ex))
626 1.1 cjs return(-1);
627 1.1 cjs
628 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
629 1.1 cjs
630 1.1 cjs if (read(fd, (char *)&ex_swap, sizeof(ex_swap)) != sizeof(ex_swap))
631 1.1 cjs return(-1);
632 1.1 cjs
633 1.1 cjs (void)lseek(fd, (off_t) 0, SEEK_SET);
634 1.1 cjs
635 1.1 cjs mid = getMID(mid, N_GETMID (ex));
636 1.1 cjs
637 1.1 cjs if (mid == -1) {
638 1.1 cjs mid = getMID(mid, N_GETMID (ex_swap));
639 1.1 cjs }
640 1.1 cjs
641 1.1 cjs if (mid != -1) {
642 1.1 cjs return(0);
643 1.1 cjs } else {
644 1.1 cjs return(-1);
645 1.1 cjs }
646 1.7 cgd #endif /* NOAOUT */
647 1.1 cjs }
648 1.1 cjs
649 1.1 cjs int
650 1.14 joerg GetAOutFileInfo(struct dllist *dl)
651 1.1 cjs {
652 1.1 cjs #ifdef NOAOUT
653 1.1 cjs return(-1);
654 1.1 cjs #else
655 1.1 cjs struct exec ex, ex_swap;
656 1.4 lukem u_int32_t mid = -1;
657 1.4 lukem u_int32_t magic, clbytes, clofset;
658 1.1 cjs
659 1.8 thorpej if (read(dl->ldfd, (char *)&ex, sizeof(ex)) != sizeof(ex))
660 1.1 cjs return(-1);
661 1.1 cjs
662 1.8 thorpej (void)lseek(dl->ldfd, (off_t) 0, SEEK_SET);
663 1.1 cjs
664 1.8 thorpej if (read(dl->ldfd, (char *)&ex_swap,
665 1.8 thorpej sizeof(ex_swap)) != sizeof(ex_swap))
666 1.1 cjs return(-1);
667 1.1 cjs
668 1.1 cjs mopFileSwapX((u_char *)&ex_swap, 0, 4);
669 1.1 cjs
670 1.1 cjs mid = getMID(mid, N_GETMID (ex));
671 1.1 cjs
672 1.11 lukem if (mid == (uint32_t)-1) {
673 1.1 cjs mid = getMID(mid, N_GETMID (ex_swap));
674 1.11 lukem if (mid != (uint32_t)-1) {
675 1.1 cjs mopFileSwapX((u_char *)&ex, 0, 4);
676 1.1 cjs }
677 1.1 cjs }
678 1.1 cjs
679 1.11 lukem if (mid == (uint32_t)-1) {
680 1.1 cjs return(-1);
681 1.1 cjs }
682 1.1 cjs
683 1.1 cjs if (N_BADMAG (ex)) {
684 1.1 cjs return(-1);
685 1.1 cjs }
686 1.1 cjs
687 1.1 cjs switch (mid) {
688 1.1 cjs case MID_I386:
689 1.1 cjs #ifdef MID_NS32532
690 1.1 cjs case MID_NS32532:
691 1.1 cjs #endif
692 1.1 cjs #ifdef MID_PMAX
693 1.1 cjs case MID_PMAX:
694 1.1 cjs #endif
695 1.1 cjs #ifdef MID_VAX
696 1.1 cjs case MID_VAX:
697 1.1 cjs #endif
698 1.1 cjs #ifdef MID_ALPHA
699 1.1 cjs case MID_ALPHA:
700 1.1 cjs #endif
701 1.1 cjs #ifdef MID_ARM6
702 1.1 cjs case MID_ARM6:
703 1.1 cjs #endif
704 1.1 cjs ex.a_text = mopFileGetLX((u_char *)&ex_swap, 4, 4);
705 1.1 cjs ex.a_data = mopFileGetLX((u_char *)&ex_swap, 8, 4);
706 1.1 cjs ex.a_bss = mopFileGetLX((u_char *)&ex_swap, 12, 4);
707 1.1 cjs ex.a_syms = mopFileGetLX((u_char *)&ex_swap, 16, 4);
708 1.1 cjs ex.a_entry = mopFileGetLX((u_char *)&ex_swap, 20, 4);
709 1.1 cjs ex.a_trsize= mopFileGetLX((u_char *)&ex_swap, 24, 4);
710 1.1 cjs ex.a_drsize= mopFileGetLX((u_char *)&ex_swap, 28, 4);
711 1.1 cjs break;
712 1.1 cjs #ifdef MID_M68K
713 1.1 cjs case MID_M68K:
714 1.1 cjs #endif
715 1.1 cjs #ifdef MID_M68K4K
716 1.1 cjs case MID_M68K4K:
717 1.1 cjs #endif
718 1.1 cjs case MID_SPARC:
719 1.1 cjs #ifdef MID_MIPS
720 1.1 cjs case MID_MIPS:
721 1.1 cjs #endif
722 1.1 cjs ex.a_text = mopFileGetBX((u_char *)&ex_swap, 4, 4);
723 1.1 cjs ex.a_data = mopFileGetBX((u_char *)&ex_swap, 8, 4);
724 1.1 cjs ex.a_bss = mopFileGetBX((u_char *)&ex_swap, 12, 4);
725 1.1 cjs ex.a_syms = mopFileGetBX((u_char *)&ex_swap, 16, 4);
726 1.1 cjs ex.a_entry = mopFileGetBX((u_char *)&ex_swap, 20, 4);
727 1.1 cjs ex.a_trsize= mopFileGetBX((u_char *)&ex_swap, 24, 4);
728 1.1 cjs ex.a_drsize= mopFileGetBX((u_char *)&ex_swap, 28, 4);
729 1.1 cjs break;
730 1.1 cjs default:
731 1.5 cgd break;
732 1.1 cjs }
733 1.1 cjs
734 1.1 cjs printf("a.out image (");
735 1.1 cjs switch (N_GETMID (ex)) {
736 1.1 cjs case MID_I386:
737 1.1 cjs printf("i386");
738 1.1 cjs break;
739 1.1 cjs #ifdef MID_M68K
740 1.1 cjs case MID_M68K:
741 1.1 cjs printf("m68k");
742 1.1 cjs break;
743 1.1 cjs #endif
744 1.1 cjs #ifdef MID_M68K4K
745 1.1 cjs case MID_M68K4K:
746 1.1 cjs printf("m68k 4k");
747 1.1 cjs break;
748 1.1 cjs #endif
749 1.1 cjs #ifdef MID_NS32532
750 1.1 cjs case MID_NS32532:
751 1.1 cjs printf("pc532");
752 1.1 cjs break;
753 1.1 cjs #endif
754 1.1 cjs case MID_SPARC:
755 1.1 cjs printf("sparc");
756 1.1 cjs break;
757 1.1 cjs #ifdef MID_PMAX
758 1.1 cjs case MID_PMAX:
759 1.1 cjs printf("pmax");
760 1.1 cjs break;
761 1.1 cjs #endif
762 1.1 cjs #ifdef MID_VAX
763 1.1 cjs case MID_VAX:
764 1.1 cjs printf("vax");
765 1.1 cjs break;
766 1.1 cjs #endif
767 1.1 cjs #ifdef MID_ALPHA
768 1.1 cjs case MID_ALPHA:
769 1.1 cjs printf("alpha");
770 1.1 cjs break;
771 1.1 cjs #endif
772 1.1 cjs #ifdef MID_MIPS
773 1.1 cjs case MID_MIPS:
774 1.1 cjs printf("mips");
775 1.1 cjs break;
776 1.1 cjs #endif
777 1.1 cjs #ifdef MID_ARM6
778 1.1 cjs case MID_ARM6:
779 1.1 cjs printf("arm32");
780 1.1 cjs break;
781 1.1 cjs #endif
782 1.1 cjs default:
783 1.5 cgd break;
784 1.1 cjs }
785 1.1 cjs printf(") Magic: ");
786 1.1 cjs switch (N_GETMAGIC (ex)) {
787 1.1 cjs case OMAGIC:
788 1.1 cjs printf("OMAGIC");
789 1.1 cjs break;
790 1.1 cjs case NMAGIC:
791 1.1 cjs printf("NMAGIC");
792 1.1 cjs break;
793 1.1 cjs case ZMAGIC:
794 1.1 cjs printf("ZMAGIC");
795 1.1 cjs break;
796 1.1 cjs case QMAGIC:
797 1.1 cjs printf("QMAGIC");
798 1.1 cjs break;
799 1.1 cjs default:
800 1.4 lukem printf("Unknown %ld", (long) N_GETMAGIC (ex));
801 1.1 cjs }
802 1.1 cjs printf("\n");
803 1.4 lukem printf("Size of text: %08lx\n", (long)ex.a_text);
804 1.4 lukem printf("Size of data: %08lx\n", (long)ex.a_data);
805 1.4 lukem printf("Size of bss: %08lx\n", (long)ex.a_bss);
806 1.4 lukem printf("Size of symbol tab: %08lx\n", (long)ex.a_syms);
807 1.4 lukem printf("Transfer Address: %08lx\n", (long)ex.a_entry);
808 1.4 lukem printf("Size of reloc text: %08lx\n", (long)ex.a_trsize);
809 1.4 lukem printf("Size of reloc data: %08lx\n", (long)ex.a_drsize);
810 1.3 lukem
811 1.1 cjs magic = N_GETMAGIC (ex);
812 1.1 cjs clbytes = getCLBYTES(mid);
813 1.1 cjs clofset = clbytes - 1;
814 1.1 cjs
815 1.8 thorpej dl->image_type = IMAGE_TYPE_AOUT;
816 1.8 thorpej dl->loadaddr = 0;
817 1.8 thorpej dl->xferaddr = ex.a_entry;
818 1.8 thorpej
819 1.8 thorpej dl->a_text = ex.a_text;
820 1.8 thorpej if (magic == ZMAGIC || magic == NMAGIC) {
821 1.8 thorpej dl->a_text_fill = clbytes - (ex.a_text & clofset);
822 1.8 thorpej if (dl->a_text_fill == clbytes)
823 1.8 thorpej dl->a_text_fill = 0;
824 1.8 thorpej } else
825 1.8 thorpej dl->a_text_fill = 0;
826 1.8 thorpej dl->a_data = ex.a_data;
827 1.8 thorpej if (magic == ZMAGIC || magic == NMAGIC) {
828 1.8 thorpej dl->a_data_fill = clbytes - (ex.a_data & clofset);
829 1.8 thorpej if (dl->a_data_fill == clbytes)
830 1.8 thorpej dl->a_data_fill = 0;
831 1.8 thorpej } else
832 1.8 thorpej dl->a_data_fill = 0;
833 1.8 thorpej dl->a_bss = ex.a_bss;
834 1.8 thorpej if (magic == ZMAGIC || magic == NMAGIC) {
835 1.8 thorpej dl->a_bss_fill = clbytes - (ex.a_bss & clofset);
836 1.8 thorpej if (dl->a_bss_fill == clbytes)
837 1.8 thorpej dl->a_bss_fill = 0;
838 1.8 thorpej } else {
839 1.8 thorpej dl->a_bss_fill = clbytes -
840 1.8 thorpej ((ex.a_text+ex.a_data+ex.a_bss) & clofset);
841 1.8 thorpej if (dl->a_bss_fill == clbytes)
842 1.8 thorpej dl->a_bss_fill = 0;
843 1.1 cjs }
844 1.8 thorpej dl->a_mid = mid;
845 1.1 cjs
846 1.1 cjs return(0);
847 1.7 cgd #endif /* NOAOUT */
848 1.1 cjs }
849 1.1 cjs
850 1.1 cjs int
851 1.14 joerg GetFileInfo(struct dllist *dl)
852 1.1 cjs {
853 1.11 lukem int error;
854 1.1 cjs
855 1.11 lukem error = CheckElfFile(dl->ldfd);
856 1.11 lukem if (error == 0) {
857 1.11 lukem error = GetElfFileInfo(dl);
858 1.11 lukem if (error != 0) {
859 1.8 thorpej return(-1);
860 1.8 thorpej }
861 1.8 thorpej return (0);
862 1.8 thorpej }
863 1.1 cjs
864 1.11 lukem error = CheckAOutFile(dl->ldfd);
865 1.11 lukem if (error == 0) {
866 1.11 lukem error = GetAOutFileInfo(dl);
867 1.11 lukem if (error != 0) {
868 1.1 cjs return(-1);
869 1.1 cjs }
870 1.8 thorpej return (0);
871 1.8 thorpej }
872 1.8 thorpej
873 1.11 lukem error = CheckMopFile(dl->ldfd);
874 1.11 lukem if (error == 0) {
875 1.11 lukem error = GetMopFileInfo(dl);
876 1.11 lukem if (error != 0) {
877 1.1 cjs return(-1);
878 1.1 cjs }
879 1.8 thorpej return (0);
880 1.1 cjs }
881 1.1 cjs
882 1.8 thorpej /* Unknown file format. */
883 1.8 thorpej return(-1);
884 1.1 cjs }
885 1.1 cjs
886 1.1 cjs ssize_t
887 1.14 joerg mopFileRead(struct dllist *dlslot, u_char *buf)
888 1.1 cjs {
889 1.1 cjs ssize_t len, outlen;
890 1.8 thorpej int bsz, sec;
891 1.4 lukem int32_t pos, notdone, total;
892 1.8 thorpej uint32_t secoff;
893 1.1 cjs
894 1.8 thorpej switch (dlslot->image_type) {
895 1.8 thorpej case IMAGE_TYPE_MOP:
896 1.1 cjs len = read(dlslot->ldfd,buf,dlslot->dl_bsz);
897 1.8 thorpej break;
898 1.8 thorpej
899 1.8 thorpej case IMAGE_TYPE_ELF32:
900 1.8 thorpej sec = dlslot->e_cursec;
901 1.8 thorpej
902 1.8 thorpej /*
903 1.8 thorpej * We're pretty simplistic here. We do only file-backed
904 1.8 thorpej * or only zero-fill.
905 1.8 thorpej */
906 1.8 thorpej
907 1.8 thorpej /* Determine offset into section. */
908 1.8 thorpej secoff = dlslot->e_curpos - dlslot->e_sections[sec].s_loff;
909 1.8 thorpej
910 1.8 thorpej /*
911 1.8 thorpej * If we're in the file-backed part of the section,
912 1.8 thorpej * transmit some of the file.
913 1.8 thorpej */
914 1.8 thorpej if (secoff < dlslot->e_sections[sec].s_fsize) {
915 1.8 thorpej bsz = dlslot->e_sections[sec].s_fsize - secoff;
916 1.8 thorpej if (bsz > dlslot->dl_bsz)
917 1.8 thorpej bsz = dlslot->dl_bsz;
918 1.8 thorpej if (lseek(dlslot->ldfd,
919 1.8 thorpej dlslot->e_sections[sec].s_foff + secoff,
920 1.8 thorpej SEEK_SET) == (off_t) -1)
921 1.8 thorpej return (-1);
922 1.8 thorpej len = read(dlslot->ldfd, buf, bsz);
923 1.8 thorpej }
924 1.8 thorpej /*
925 1.8 thorpej * Otherwise, if we're in the zero-fill part of the
926 1.8 thorpej * section, transmit some zeros.
927 1.8 thorpej */
928 1.8 thorpej else if (secoff < (dlslot->e_sections[sec].s_fsize +
929 1.8 thorpej dlslot->e_sections[sec].s_pad)) {
930 1.8 thorpej bsz = dlslot->e_sections[sec].s_pad -
931 1.8 thorpej (secoff - dlslot->e_sections[sec].s_fsize);
932 1.8 thorpej if (bsz > dlslot->dl_bsz)
933 1.8 thorpej bsz = dlslot->dl_bsz;
934 1.8 thorpej memset(buf, 0, (len = bsz));
935 1.8 thorpej }
936 1.8 thorpej /*
937 1.8 thorpej * ...and if we haven't hit either of those cases,
938 1.8 thorpej * that's the end of the image.
939 1.8 thorpej */
940 1.8 thorpej else {
941 1.8 thorpej return (0);
942 1.8 thorpej }
943 1.8 thorpej /*
944 1.8 thorpej * Advance the logical image pointer.
945 1.8 thorpej */
946 1.8 thorpej dlslot->e_curpos += bsz;
947 1.8 thorpej if (dlslot->e_curpos >= (dlslot->e_sections[sec].s_loff +
948 1.8 thorpej dlslot->e_sections[sec].s_fsize +
949 1.8 thorpej dlslot->e_sections[sec].s_pad))
950 1.8 thorpej dlslot->e_cursec++;
951 1.8 thorpej break;
952 1.8 thorpej
953 1.8 thorpej case IMAGE_TYPE_AOUT:
954 1.1 cjs bsz = dlslot->dl_bsz;
955 1.1 cjs pos = dlslot->a_lseek;
956 1.1 cjs len = 0;
957 1.1 cjs
958 1.1 cjs total = dlslot->a_text;
959 1.1 cjs
960 1.1 cjs if (pos < total) {
961 1.1 cjs notdone = total - pos;
962 1.1 cjs if (notdone <= bsz) {
963 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],notdone);
964 1.1 cjs } else {
965 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],bsz);
966 1.1 cjs }
967 1.1 cjs len = len + outlen;
968 1.1 cjs pos = pos + outlen;
969 1.1 cjs bsz = bsz - outlen;
970 1.1 cjs }
971 1.1 cjs
972 1.1 cjs total = total + dlslot->a_text_fill;
973 1.1 cjs
974 1.1 cjs if ((bsz > 0) && (pos < total)) {
975 1.1 cjs notdone = total - pos;
976 1.1 cjs if (notdone <= bsz) {
977 1.1 cjs outlen = notdone;
978 1.1 cjs } else {
979 1.1 cjs outlen = bsz;
980 1.1 cjs }
981 1.4 lukem memset(&buf[len], 0, outlen);
982 1.1 cjs len = len + outlen;
983 1.1 cjs pos = pos + outlen;
984 1.1 cjs bsz = bsz - outlen;
985 1.1 cjs }
986 1.1 cjs
987 1.1 cjs total = total + dlslot->a_data;
988 1.1 cjs
989 1.1 cjs if ((bsz > 0) && (pos < total)) {
990 1.1 cjs notdone = total - pos;
991 1.1 cjs if (notdone <= bsz) {
992 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],notdone);
993 1.1 cjs } else {
994 1.1 cjs outlen = read(dlslot->ldfd,&buf[len],bsz);
995 1.1 cjs }
996 1.1 cjs len = len + outlen;
997 1.1 cjs pos = pos + outlen;
998 1.1 cjs bsz = bsz - outlen;
999 1.1 cjs }
1000 1.1 cjs
1001 1.1 cjs total = total + dlslot->a_data_fill;
1002 1.1 cjs
1003 1.1 cjs if ((bsz > 0) && (pos < total)) {
1004 1.1 cjs notdone = total - pos;
1005 1.1 cjs if (notdone <= bsz) {
1006 1.1 cjs outlen = notdone;
1007 1.1 cjs } else {
1008 1.1 cjs outlen = bsz;
1009 1.1 cjs }
1010 1.4 lukem memset(&buf[len], 0, outlen);
1011 1.1 cjs len = len + outlen;
1012 1.1 cjs pos = pos + outlen;
1013 1.1 cjs bsz = bsz - outlen;
1014 1.1 cjs }
1015 1.1 cjs
1016 1.1 cjs total = total + dlslot->a_bss;
1017 1.1 cjs
1018 1.1 cjs if ((bsz > 0) && (pos < total)) {
1019 1.1 cjs notdone = total - pos;
1020 1.1 cjs if (notdone <= bsz) {
1021 1.1 cjs outlen = notdone;
1022 1.1 cjs } else {
1023 1.1 cjs outlen = bsz;
1024 1.1 cjs }
1025 1.4 lukem memset(&buf[len], 0, outlen);
1026 1.1 cjs len = len + outlen;
1027 1.1 cjs pos = pos + outlen;
1028 1.1 cjs bsz = bsz - outlen;
1029 1.1 cjs }
1030 1.1 cjs
1031 1.1 cjs total = total + dlslot->a_bss_fill;
1032 1.1 cjs
1033 1.1 cjs if ((bsz > 0) && (pos < total)) {
1034 1.1 cjs notdone = total - pos;
1035 1.1 cjs if (notdone <= bsz) {
1036 1.1 cjs outlen = notdone;
1037 1.1 cjs } else {
1038 1.1 cjs outlen = bsz;
1039 1.1 cjs }
1040 1.4 lukem memset(&buf[len], 0, outlen);
1041 1.1 cjs len = len + outlen;
1042 1.1 cjs pos = pos + outlen;
1043 1.1 cjs bsz = bsz - outlen;
1044 1.1 cjs }
1045 1.1 cjs
1046 1.1 cjs dlslot->a_lseek = pos;
1047 1.8 thorpej break;
1048 1.10 thorpej
1049 1.10 thorpej default:
1050 1.10 thorpej abort();
1051 1.1 cjs }
1052 1.1 cjs
1053 1.1 cjs return(len);
1054 1.1 cjs }
1055