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