1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2006 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: file.c,v 1.85 2005/04/06 21:28:36 debug Exp $ |
* $Id: file.c,v 1.127 2006/01/22 23:20:33 debug Exp $ |
29 |
* |
* |
30 |
* This file contains functions which load executable images into (emulated) |
* This file contains functions which load executable images into (emulated) |
31 |
* memory. File formats recognized so far: |
* memory. File formats recognized so far are: |
32 |
* |
* |
|
* ELF 32-bit and 64-bit ELFs |
|
33 |
* a.out old format used by OpenBSD 2.x pmax kernels |
* a.out old format used by OpenBSD 2.x pmax kernels |
34 |
|
* Mach-O MacOS X format, etc. |
35 |
* ecoff old format used by Ultrix, Windows NT, etc |
* ecoff old format used by Ultrix, Windows NT, etc |
36 |
* srec Motorola SREC format |
* srec Motorola SREC format |
37 |
* raw raw binaries, "address:[skiplen:[entrypoint:]]filename" |
* raw raw binaries, "address:[skiplen:[entrypoint:]]filename" |
38 |
|
* ELF 32-bit and 64-bit ELFs |
39 |
* |
* |
40 |
* If a file is not of one of the above mentioned formats, it is assumed |
* If a file is not of one of the above mentioned formats, it is assumed |
41 |
* to be symbol data generated by 'nm' or 'nm -S'. |
* to be symbol data generated by 'nm' or 'nm -S'. |
56 |
#include "symbol.h" |
#include "symbol.h" |
57 |
|
|
58 |
|
|
59 |
|
extern int quiet_mode; |
60 |
|
extern int verbose; |
61 |
|
|
62 |
|
|
63 |
/* ELF machine types as strings: (same as exec_elf.h) */ |
/* ELF machine types as strings: (same as exec_elf.h) */ |
64 |
#define N_ELF_MACHINE_TYPES 54 |
#define N_ELF_MACHINE_TYPES 64 |
65 |
static char *elf_machine_type[N_ELF_MACHINE_TYPES] = { |
static char *elf_machine_type[N_ELF_MACHINE_TYPES] = { |
66 |
"NONE", "M32", "SPARC", "386", /* 0..3 */ |
"NONE", "M32", "SPARC", "386", /* 0..3 */ |
67 |
"68K", "88K", "486", "860", /* 4..7 */ |
"68K", "88K", "486", "860", /* 4..7 */ |
76 |
"ARM", "ALPHA", "SH", "SPARCV9", /* 40..43 */ |
"ARM", "ALPHA", "SH", "SPARCV9", /* 40..43 */ |
77 |
"TRICORE", "ARC", "H8_300", "H8_300H", /* 44..47 */ |
"TRICORE", "ARC", "H8_300", "H8_300H", /* 44..47 */ |
78 |
"H8S", "H8_500", "IA_64", "MIPS_X", /* 48..51 */ |
"H8S", "H8_500", "IA_64", "MIPS_X", /* 48..51 */ |
79 |
"COLDFIRE", "68HC12" /* 52..53 */ |
"COLDFIRE", "68HC12", "unknown54", "unknown55", /* 52..55 */ |
80 |
|
"unknown56", "unknown57", "unknown58", "unknown59", /* 56..59 */ |
81 |
|
"unknown60", "unknown61", "AMD64", "unknown63" /* 60..63 */ |
82 |
}; |
}; |
83 |
|
|
84 |
|
|
128 |
} |
} |
129 |
|
|
130 |
|
|
131 |
|
#define AOUT_FLAG_DECOSF1 1 |
132 |
|
#define AOUT_FLAG_FROM_BEGINNING 2 |
133 |
|
#define AOUT_FLAG_VADDR_ZERO_HACK 4 |
134 |
|
#define AOUT_FLAG_NO_SIZES 8 |
135 |
/* |
/* |
136 |
* file_load_aout(): |
* file_load_aout(): |
137 |
* |
* |
142 |
* formats, where text/data are aligned differently. |
* formats, where text/data are aligned differently. |
143 |
*/ |
*/ |
144 |
static void file_load_aout(struct machine *m, struct memory *mem, |
static void file_load_aout(struct machine *m, struct memory *mem, |
145 |
char *filename, int osf1_hack, |
char *filename, int flags, |
146 |
uint64_t *entrypointp, int arch, int *byte_orderp) |
uint64_t *entrypointp, int arch, int *byte_orderp) |
147 |
{ |
{ |
148 |
struct exec aout_header; |
struct exec aout_header; |
152 |
uint32_t entry, datasize, textsize; |
uint32_t entry, datasize, textsize; |
153 |
int32_t symbsize = 0; |
int32_t symbsize = 0; |
154 |
uint32_t vaddr, total_len; |
uint32_t vaddr, total_len; |
155 |
unsigned char buf[4096]; |
unsigned char buf[65536]; |
156 |
unsigned char *syms; |
unsigned char *syms; |
157 |
|
|
158 |
|
if (m->cpus[0]->byte_order == EMUL_BIG_ENDIAN) |
159 |
|
encoding = ELFDATA2MSB; |
160 |
|
|
161 |
f = fopen(filename, "r"); |
f = fopen(filename, "r"); |
162 |
if (f == NULL) { |
if (f == NULL) { |
163 |
perror(filename); |
perror(filename); |
164 |
exit(1); |
exit(1); |
165 |
} |
} |
166 |
|
|
167 |
if (osf1_hack) { |
if (flags & AOUT_FLAG_DECOSF1) { |
168 |
fread(&buf, 1, 32, f); |
fread(&buf, 1, 32, f); |
169 |
vaddr = buf[16] + (buf[17] << 8) + |
vaddr = buf[16] + (buf[17] << 8) + |
170 |
(buf[18] << 16) + (buf[19] << 24); |
(buf[18] << 16) + ((uint64_t)buf[19] << 24); |
171 |
entry = buf[20] + (buf[21] << 8) + |
entry = buf[20] + (buf[21] << 8) + |
172 |
(buf[22] << 16) + (buf[23] << 24); |
(buf[22] << 16) + ((uint64_t)buf[23] << 24); |
173 |
debug("OSF1 a.out, load address 0x%08lx, " |
debug("OSF1 a.out, load address 0x%08lx, " |
174 |
"entry point 0x%08x\n", (long)vaddr, (long)entry); |
"entry point 0x%08x\n", (long)vaddr, (long)entry); |
175 |
symbsize = 0; |
symbsize = 0; |
176 |
fseek(f, 0, SEEK_END); |
fseek(f, 0, SEEK_END); |
177 |
/* This is of course wrong, but should work anyway: */ |
/* This is of course wrong, but should work anyway: */ |
178 |
textsize = ftell(f) - 512; |
textsize = ftello(f) - 512; |
179 |
datasize = 0; |
datasize = 0; |
180 |
fseek(f, 512, SEEK_SET); |
fseek(f, 512, SEEK_SET); |
181 |
|
} else if (flags & AOUT_FLAG_NO_SIZES) { |
182 |
|
fseek(f, 0, SEEK_END); |
183 |
|
textsize = ftello(f) - 32; |
184 |
|
datasize = 0; |
185 |
|
vaddr = entry = 0; |
186 |
|
fseek(f, 32, SEEK_SET); |
187 |
} else { |
} else { |
188 |
len = fread(&aout_header, 1, sizeof(aout_header), f); |
len = fread(&aout_header, 1, sizeof(aout_header), f); |
189 |
if (len != sizeof(aout_header)) { |
if (len != sizeof(aout_header)) { |
193 |
} |
} |
194 |
|
|
195 |
unencode(entry, &aout_header.a_entry, uint32_t); |
unencode(entry, &aout_header.a_entry, uint32_t); |
|
vaddr = entry; |
|
196 |
debug("a.out, entry point 0x%08lx\n", (long)entry); |
debug("a.out, entry point 0x%08lx\n", (long)entry); |
197 |
|
vaddr = entry; |
198 |
|
|
199 |
|
if (flags & AOUT_FLAG_VADDR_ZERO_HACK) |
200 |
|
vaddr = 0; |
201 |
|
|
202 |
unencode(textsize, &aout_header.a_text, uint32_t); |
unencode(textsize, &aout_header.a_text, uint32_t); |
203 |
unencode(datasize, &aout_header.a_data, uint32_t); |
unencode(datasize, &aout_header.a_data, uint32_t); |
206 |
unencode(symbsize, &aout_header.a_syms, uint32_t); |
unencode(symbsize, &aout_header.a_syms, uint32_t); |
207 |
} |
} |
208 |
|
|
209 |
|
if (flags & AOUT_FLAG_FROM_BEGINNING) { |
210 |
|
fseek(f, 0, SEEK_SET); |
211 |
|
vaddr &= ~0xfff; |
212 |
|
} |
213 |
|
|
214 |
/* Load text and data: */ |
/* Load text and data: */ |
215 |
total_len = textsize + datasize; |
total_len = textsize + datasize; |
216 |
while (total_len != 0) { |
while (total_len != 0) { |
218 |
len = fread(buf, 1, len, f); |
len = fread(buf, 1, len, f); |
219 |
|
|
220 |
/* printf("fread len=%i vaddr=%x buf[0..]=%02x %02x %02x\n", |
/* printf("fread len=%i vaddr=%x buf[0..]=%02x %02x %02x\n", |
221 |
len, (int)vaddr, buf[0], buf[1], buf[2]); */ |
(int)len, (int)vaddr, buf[0], buf[1], buf[2]); */ |
222 |
|
|
223 |
if (len > 0) |
if (len > 0) { |
224 |
m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr, |
int len2 = 0; |
225 |
&buf[0], len, MEM_WRITE, NO_EXCEPTIONS); |
uint64_t vaddr1 = vaddr & |
226 |
else { |
((1 << BITS_PER_MEMBLOCK) - 1); |
227 |
if (osf1_hack) |
uint64_t vaddr2 = (vaddr + |
228 |
|
len) & ((1 << BITS_PER_MEMBLOCK) - 1); |
229 |
|
if (vaddr2 < vaddr1) { |
230 |
|
len2 = len - vaddr2; |
231 |
|
m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr, |
232 |
|
&buf[0], len2, MEM_WRITE, NO_EXCEPTIONS); |
233 |
|
} |
234 |
|
m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr + len2, |
235 |
|
&buf[len2], len-len2, MEM_WRITE, NO_EXCEPTIONS); |
236 |
|
} else { |
237 |
|
if (flags & AOUT_FLAG_DECOSF1) |
238 |
break; |
break; |
239 |
else { |
else { |
240 |
fprintf(stderr, "could not read from %s\n", |
fprintf(stderr, "could not read from %s\n", |
255 |
char *string_symbols; |
char *string_symbols; |
256 |
off_t oldpos; |
off_t oldpos; |
257 |
|
|
258 |
debug("symbols: %i bytes @ 0x%x\n", symbsize, (int)ftell(f)); |
debug("symbols: %i bytes @ 0x%x\n", symbsize, (int)ftello(f)); |
259 |
syms = malloc(symbsize); |
syms = malloc(symbsize); |
260 |
if (syms == NULL) { |
if (syms == NULL) { |
261 |
fprintf(stderr, "out of memory\n"); |
fprintf(stderr, "out of memory\n"); |
268 |
exit(1); |
exit(1); |
269 |
} |
} |
270 |
|
|
271 |
oldpos = ftell(f); |
oldpos = ftello(f); |
272 |
fseek(f, 0, SEEK_END); |
fseek(f, 0, SEEK_END); |
273 |
strings_len = ftell(f) - oldpos; |
strings_len = ftello(f) - oldpos; |
274 |
fseek(f, oldpos, SEEK_SET); |
fseek(f, oldpos, SEEK_SET); |
275 |
debug("strings: %i bytes @ 0x%x\n", strings_len, (int)ftell(f)); |
debug("strings: %i bytes @ 0x%x\n", strings_len,(int)ftello(f)); |
276 |
string_symbols = malloc(strings_len); |
string_symbols = malloc(strings_len); |
277 |
if (string_symbols == NULL) { |
if (string_symbols == NULL) { |
278 |
fprintf(stderr, "out of memory\n"); |
fprintf(stderr, "out of memory\n"); |
294 |
|
|
295 |
if (type != 0 && addr != 0) |
if (type != 0 && addr != 0) |
296 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
297 |
addr, 0, string_symbols + str_index, 0); |
addr, 0, string_symbols + str_index, 0, -1); |
298 |
i++; |
i++; |
299 |
} |
} |
300 |
|
|
304 |
|
|
305 |
fclose(f); |
fclose(f); |
306 |
|
|
307 |
|
*entrypointp = (int32_t)entry; |
308 |
|
|
309 |
|
if (encoding == ELFDATA2LSB) |
310 |
|
*byte_orderp = EMUL_LITTLE_ENDIAN; |
311 |
|
else |
312 |
|
*byte_orderp = EMUL_BIG_ENDIAN; |
313 |
|
|
314 |
|
n_executables_loaded ++; |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
/* |
319 |
|
* file_load_macho(): |
320 |
|
* |
321 |
|
* Loads a Mach-O binary image into the emulated memory. The entry point |
322 |
|
* is stored in the specified CPU's registers. |
323 |
|
* |
324 |
|
* TODO: |
325 |
|
* |
326 |
|
* o) Almost everything. |
327 |
|
* |
328 |
|
* o) I haven't had time to look into whether Apple's open source |
329 |
|
* license is BSD-compatible or not. Perhaps it would be possible |
330 |
|
* to use a header file containing symbolic names, and not use |
331 |
|
* hardcoded values. |
332 |
|
*/ |
333 |
|
static void file_load_macho(struct machine *m, struct memory *mem, |
334 |
|
char *filename, uint64_t *entrypointp, int arch, int *byte_orderp, |
335 |
|
int is_64bit, int is_reversed) |
336 |
|
{ |
337 |
|
FILE *f; |
338 |
|
uint64_t entry = 0; |
339 |
|
int entry_set = 0; |
340 |
|
int encoding = ELFDATA2MSB; |
341 |
|
unsigned char buf[65536]; |
342 |
|
char *symbols, *strings; |
343 |
|
uint32_t cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags; |
344 |
|
uint64_t vmaddr, vmsize, fileoff, filesize; |
345 |
|
int cmd_type, cmd_len, i, flavor; |
346 |
|
int32_t symoff, nsyms, stroff, strsize; |
347 |
|
size_t len, pos; |
348 |
|
|
349 |
|
if (m->cpus[0]->byte_order == EMUL_BIG_ENDIAN) |
350 |
|
encoding = ELFDATA2MSB; |
351 |
|
|
352 |
|
f = fopen(filename, "r"); |
353 |
|
if (f == NULL) { |
354 |
|
perror(filename); |
355 |
|
exit(1); |
356 |
|
} |
357 |
|
|
358 |
|
if (is_64bit) { |
359 |
|
fatal("TODO: 64-bit Mach-O. Not supported yet.\n"); |
360 |
|
exit(1); |
361 |
|
} |
362 |
|
if (is_reversed) { |
363 |
|
fatal("TODO: Reversed-endianness. Not supported yet.\n"); |
364 |
|
exit(1); |
365 |
|
} |
366 |
|
|
367 |
|
len = fread(buf, 1, sizeof(buf), f); |
368 |
|
if (len < 100) { |
369 |
|
fatal("Bad Mach-O file?\n"); |
370 |
|
exit(1); |
371 |
|
} |
372 |
|
|
373 |
|
unencode(cputype, &buf[4], uint32_t); |
374 |
|
unencode(cpusubtype, &buf[8], uint32_t); |
375 |
|
unencode(filetype, &buf[12], uint32_t); |
376 |
|
unencode(ncmds, &buf[16], uint32_t); |
377 |
|
unencode(sizeofcmds, &buf[20], uint32_t); |
378 |
|
unencode(flags, &buf[24], uint32_t); |
379 |
|
|
380 |
|
/* debug("cputype=0x%x cpusubtype=0x%x filetype=0x%x\n", |
381 |
|
cputype, cpusubtype, filetype); |
382 |
|
debug("ncmds=%i sizeofcmds=0x%08x flags=0x%08x\n", |
383 |
|
ncmds, sizeofcmds, flags); */ |
384 |
|
|
385 |
|
/* |
386 |
|
* Compare to "normal" values. |
387 |
|
* NOTE/TODO: These were for a Darwin (Macintosh PPC) kernel. |
388 |
|
*/ |
389 |
|
if (cputype != 0x12) { |
390 |
|
fatal("Error: Unimplemented cputype 0x%x\n", cputype); |
391 |
|
exit(1); |
392 |
|
} |
393 |
|
if (cpusubtype != 0) { |
394 |
|
fatal("Error: Unimplemented cpusubtype 0x%x\n", cpusubtype); |
395 |
|
exit(1); |
396 |
|
} |
397 |
|
/* Filetype 2 means an executable image. */ |
398 |
|
if (filetype != 2) { |
399 |
|
fatal("Error: Unimplemented filetype 0x%x\n", filetype); |
400 |
|
exit(1); |
401 |
|
} |
402 |
|
if (!(flags & 1)) { |
403 |
|
fatal("Error: File has 'undefined references'. Cannot" |
404 |
|
" be executed.\n", flags); |
405 |
|
exit(1); |
406 |
|
} |
407 |
|
|
408 |
|
/* I've only encountered flags == 1 so far. */ |
409 |
|
if (flags != 1) { |
410 |
|
fatal("Error: Unimplemented flags 0x%x\n", flags); |
411 |
|
exit(1); |
412 |
|
} |
413 |
|
|
414 |
|
/* |
415 |
|
* Read all load commands: |
416 |
|
*/ |
417 |
|
pos = is_64bit? 32 : 28; |
418 |
|
cmd_type = 0; |
419 |
|
do { |
420 |
|
/* Read command type and length: */ |
421 |
|
unencode(cmd_type, &buf[pos], uint32_t); |
422 |
|
unencode(cmd_len, &buf[pos+4], uint32_t); |
423 |
|
|
424 |
|
#if 0 |
425 |
|
debug("cmd %i, len=%i\n", cmd_type, cmd_len); |
426 |
|
for (i=8; i<cmd_len; i++) { |
427 |
|
unsigned char ch = buf[pos+i]; |
428 |
|
if (ch >= ' ' && ch < 127) |
429 |
|
debug("%c", ch); |
430 |
|
else |
431 |
|
debug("."); |
432 |
|
} |
433 |
|
#endif |
434 |
|
switch (cmd_type) { |
435 |
|
case 1: /* LC_SEGMENT */ |
436 |
|
debug("seg "); |
437 |
|
for (i=0; i<16; i++) { |
438 |
|
if (buf[pos + 8 + i] == 0) |
439 |
|
break; |
440 |
|
debug("%c", buf[pos + 8 + i]); |
441 |
|
} |
442 |
|
unencode(vmaddr, &buf[pos+8+16+0], uint32_t); |
443 |
|
unencode(vmsize, &buf[pos+8+16+4], uint32_t); |
444 |
|
unencode(fileoff, &buf[pos+8+16+8], uint32_t); |
445 |
|
unencode(filesize, &buf[pos+8+16+12], uint32_t); |
446 |
|
debug(": vmaddr=0x%x size=0x%x fileoff=0x%x", |
447 |
|
(int)vmaddr, (int)vmsize, (int)fileoff); |
448 |
|
|
449 |
|
if (filesize == 0) { |
450 |
|
debug("\n"); |
451 |
|
break; |
452 |
|
} |
453 |
|
|
454 |
|
fseek(f, fileoff, SEEK_SET); |
455 |
|
|
456 |
|
/* Load data from the file: */ |
457 |
|
while (filesize != 0) { |
458 |
|
unsigned char buf[32768]; |
459 |
|
ssize_t len = filesize > sizeof(buf) ? |
460 |
|
sizeof(buf) : filesize; |
461 |
|
len = fread(buf, 1, len, f); |
462 |
|
|
463 |
|
/* printf("fread len=%i vmaddr=%x buf[0..]=" |
464 |
|
"%02x %02x %02x\n", (int)len, (int)vmaddr, |
465 |
|
buf[0], buf[1], buf[2]); */ |
466 |
|
|
467 |
|
if (len > 0) { |
468 |
|
int len2 = 0; |
469 |
|
uint64_t vaddr1 = vmaddr & |
470 |
|
((1 << BITS_PER_MEMBLOCK) - 1); |
471 |
|
uint64_t vaddr2 = (vmaddr + |
472 |
|
len) & ((1 << BITS_PER_MEMBLOCK)-1); |
473 |
|
if (vaddr2 < vaddr1) { |
474 |
|
len2 = len - vaddr2; |
475 |
|
m->cpus[0]->memory_rw(m->cpus[ |
476 |
|
0], mem, vmaddr, &buf[0], |
477 |
|
len2, MEM_WRITE, |
478 |
|
NO_EXCEPTIONS); |
479 |
|
} |
480 |
|
m->cpus[0]->memory_rw(m->cpus[0], mem, |
481 |
|
vmaddr + len2, &buf[len2], len-len2, |
482 |
|
MEM_WRITE, NO_EXCEPTIONS); |
483 |
|
} else { |
484 |
|
fprintf(stderr, "error reading\n"); |
485 |
|
exit(1); |
486 |
|
} |
487 |
|
|
488 |
|
vmaddr += len; |
489 |
|
filesize -= len; |
490 |
|
} |
491 |
|
|
492 |
|
debug("\n"); |
493 |
|
break; |
494 |
|
|
495 |
|
case 2: /* LC_SYMTAB */ |
496 |
|
unencode(symoff, &buf[pos+8], uint32_t); |
497 |
|
unencode(nsyms, &buf[pos+12], uint32_t); |
498 |
|
unencode(stroff, &buf[pos+16], uint32_t); |
499 |
|
unencode(strsize, &buf[pos+20], uint32_t); |
500 |
|
debug("symtable: %i symbols @ 0x%x (strings at " |
501 |
|
"0x%x)\n", nsyms, symoff, stroff); |
502 |
|
|
503 |
|
symbols = malloc(12 * nsyms); |
504 |
|
if (symbols == NULL) { |
505 |
|
fprintf(stderr, "out of memory\n"); |
506 |
|
exit(1); |
507 |
|
} |
508 |
|
fseek(f, symoff, SEEK_SET); |
509 |
|
fread(symbols, 1, 12 * nsyms, f); |
510 |
|
|
511 |
|
strings = malloc(strsize); |
512 |
|
if (strings == NULL) { |
513 |
|
fprintf(stderr, "out of memory\n"); |
514 |
|
exit(1); |
515 |
|
} |
516 |
|
fseek(f, stroff, SEEK_SET); |
517 |
|
fread(strings, 1, strsize, f); |
518 |
|
|
519 |
|
for (i=0; i<nsyms; i++) { |
520 |
|
int n_strx, n_type, n_sect, n_desc; |
521 |
|
uint32_t n_value; |
522 |
|
unencode(n_strx, &symbols[i*12+0], int32_t); |
523 |
|
unencode(n_type, &symbols[i*12+4], uint8_t); |
524 |
|
unencode(n_sect, &symbols[i*12+5], uint8_t); |
525 |
|
unencode(n_desc, &symbols[i*12+6], int16_t); |
526 |
|
unencode(n_value, &symbols[i*12+8], uint32_t); |
527 |
|
/* debug("%i: strx=%i type=%i sect=%i desc=%i" |
528 |
|
" value=0x%x\n", i, n_strx, n_type, |
529 |
|
n_sect, n_desc, n_value); */ |
530 |
|
add_symbol_name(&m->symbol_context, |
531 |
|
n_value, 0, strings + n_strx, 0, -1); |
532 |
|
} |
533 |
|
|
534 |
|
free(symbols); |
535 |
|
free(strings); |
536 |
|
break; |
537 |
|
|
538 |
|
case 5: debug("unix thread context: "); |
539 |
|
/* See http://cvs.sf.net/viewcvs.py/hte/ |
540 |
|
HT%20Editor/machostruc.h or similar for details |
541 |
|
on the thread struct. */ |
542 |
|
unencode(flavor, &buf[pos+8], uint32_t); |
543 |
|
if (flavor != 1) { |
544 |
|
fatal("unimplemented flavor %i\n", flavor); |
545 |
|
exit(1); |
546 |
|
} |
547 |
|
|
548 |
|
if (arch != ARCH_PPC) { |
549 |
|
fatal("non-PPC arch? TODO\n"); |
550 |
|
exit(1); |
551 |
|
} |
552 |
|
|
553 |
|
unencode(entry, &buf[pos+16], uint32_t); |
554 |
|
entry_set = 1; |
555 |
|
debug("pc=0x%x\n", (int)entry); |
556 |
|
|
557 |
|
for (i=1; i<40; i++) { |
558 |
|
uint32_t x; |
559 |
|
unencode(x, &buf[pos+16+i*4], uint32_t); |
560 |
|
if (x != 0) { |
561 |
|
fatal("Entry nr %i in the Mach-O" |
562 |
|
" thread struct is non-zero" |
563 |
|
" (0x%x). This is not supported" |
564 |
|
" yet. TODO\n", i, x); |
565 |
|
exit(1); |
566 |
|
} |
567 |
|
} |
568 |
|
break; |
569 |
|
|
570 |
|
default:fatal("WARNING! Unimplemented load command %i!\n", |
571 |
|
cmd_type); |
572 |
|
} |
573 |
|
|
574 |
|
pos += cmd_len; |
575 |
|
} while (pos < sizeofcmds && cmd_type != 0); |
576 |
|
|
577 |
|
fclose(f); |
578 |
|
|
579 |
|
if (!entry_set) { |
580 |
|
fatal("No entry point? Aborting.\n"); |
581 |
|
exit(1); |
582 |
|
} |
583 |
|
|
584 |
*entrypointp = entry; |
*entrypointp = entry; |
585 |
|
|
586 |
if (encoding == ELFDATA2LSB) |
if (encoding == ELFDATA2LSB) |
780 |
if (s_scnptr != 0 && s_size != 0 && |
if (s_scnptr != 0 && s_size != 0 && |
781 |
s_vaddr != 0 && !(s_flags & 0x02)) { |
s_vaddr != 0 && !(s_flags & 0x02)) { |
782 |
/* Remember the current file offset: */ |
/* Remember the current file offset: */ |
783 |
oldpos = ftell(f); |
oldpos = ftello(f); |
784 |
|
|
785 |
/* Load the section into emulated memory: */ |
/* Load the section into emulated memory: */ |
786 |
fseek(f, s_scnptr, SEEK_SET); |
fseek(f, s_scnptr, SEEK_SET); |
869 |
debug("%c", sym->name[i]); */ |
debug("%c", sym->name[i]); */ |
870 |
v = sym->value[0] + (sym->value[1] << 8) |
v = sym->value[0] + (sym->value[1] << 8) |
871 |
+ (sym->value[2] << 16) |
+ (sym->value[2] << 16) |
872 |
+ (sym->value[3] << 24); |
+ ((uint64_t)sym->value[3] << 24); |
873 |
altname = sym->name[4] + (sym->name[5] << 8) |
altname = sym->name[4] + (sym->name[5] << 8) |
874 |
+ (sym->name[6] << 16) |
+ (sym->name[6] << 16) |
875 |
+ (sym->name[3] << 24); |
+ ((uint64_t)sym->name[3] << 24); |
876 |
t = (sym->type[1] << 8) + sym->type[0]; |
t = (sym->type[1] << 8) + sym->type[0]; |
877 |
/* TODO: big endian COFF? */ |
/* TODO: big endian COFF? */ |
878 |
/* debug("' value=0x%x type=0x%04x", v, t); */ |
/* debug("' value=0x%x type=0x%04x", v, t); */ |
881 |
memcpy(name, sym->name, 8); |
memcpy(name, sym->name, 8); |
882 |
name[8] = '\0'; |
name[8] = '\0'; |
883 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
884 |
v, 0, name, 0); |
v, 0, name, 0, -1); |
885 |
n_real_symbols ++; |
n_real_symbols ++; |
886 |
} else if (t == 0x20 && !sym->name[0]) { |
} else if (t == 0x20 && !sym->name[0]) { |
887 |
off_t ofs; |
off_t ofs; |
893 |
/* debug(" [altname=0x%x '%s']", |
/* debug(" [altname=0x%x '%s']", |
894 |
altname, name); */ |
altname, name); */ |
895 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
896 |
v, 0, name, 0); |
v, 0, name, 0, -1); |
897 |
n_real_symbols ++; |
n_real_symbols ++; |
898 |
} |
} |
899 |
|
|
964 |
|
|
965 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
966 |
extsyms[sym_nr].es_value, 0, |
extsyms[sym_nr].es_value, 0, |
967 |
symbol_data + extsyms[sym_nr].es_strindex, 0); |
symbol_data + extsyms[sym_nr].es_strindex, 0, -1); |
968 |
} |
} |
969 |
|
|
970 |
free(extsyms); |
free(extsyms); |
1103 |
bytes[2]; |
bytes[2]; |
1104 |
break; |
break; |
1105 |
case 3: data_start = 4; |
case 3: data_start = 4; |
1106 |
vaddr = (bytes[0] << 24) + (bytes[1] << 16) + |
vaddr = ((uint64_t)bytes[0] << 24) + |
1107 |
(bytes[2] << 8) + bytes[3]; |
(bytes[1] << 16) + (bytes[2]<<8) + bytes[3]; |
1108 |
} |
} |
1109 |
m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr, |
m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr, |
1110 |
&bytes[data_start], count - 1 - data_start, |
&bytes[data_start], count - 1 - data_start, |
1116 |
case 9: |
case 9: |
1117 |
/* switch again, to get the entry point: */ |
/* switch again, to get the entry point: */ |
1118 |
switch (buf[1]) { |
switch (buf[1]) { |
1119 |
case 7: entry = (bytes[0] << 24) + (bytes[1] << 16) + |
case 7: entry = ((uint64_t)bytes[0] << 24) + |
1120 |
(bytes[2] << 8) + bytes[3]; |
(bytes[1] << 16) + (bytes[2]<<8) + bytes[3]; |
1121 |
break; |
break; |
1122 |
case 8: entry = (bytes[0] << 16) + (bytes[1] << 8) + |
case 8: entry = (bytes[0] << 16) + (bytes[1] << 8) + |
1123 |
bytes[2]; |
bytes[2]; |
1206 |
} |
} |
1207 |
|
|
1208 |
debug("RAW: 0x%llx bytes @ 0x%08llx", |
debug("RAW: 0x%llx bytes @ 0x%08llx", |
1209 |
(long long) (ftell(f) - skip), (long long)loadaddr); |
(long long) (ftello(f) - skip), (long long)loadaddr); |
1210 |
if (skip != 0) |
if (skip != 0) |
1211 |
debug(" (0x%llx bytes of header skipped)", (long long)skip); |
debug(" (0x%llx bytes of header skipped)", (long long)skip); |
1212 |
debug("\n"); |
debug("\n"); |
1312 |
unencode(eshoff, &hdr64.e_shoff, Elf64_Off); |
unencode(eshoff, &hdr64.e_shoff, Elf64_Off); |
1313 |
if (ephentsize != sizeof(Elf64_Phdr)) { |
if (ephentsize != sizeof(Elf64_Phdr)) { |
1314 |
fprintf(stderr, "%s: incorrect phentsize? %i, should " |
fprintf(stderr, "%s: incorrect phentsize? %i, should " |
1315 |
"be %i\n", filename, (int)ephentsize, |
"be %i\nPerhaps this is a dynamically linked " |
1316 |
(int)sizeof(Elf64_Phdr)); |
"binary (which isn't supported yet).\n", filename, |
1317 |
|
(int)ephentsize, (int)sizeof(Elf64_Phdr)); |
1318 |
exit(1); |
exit(1); |
1319 |
} |
} |
1320 |
if (eshentsize != sizeof(Elf64_Shdr)) { |
if (eshentsize != sizeof(Elf64_Shdr)) { |
1321 |
fprintf(stderr, "%s: incorrect phentsize? %i, should " |
fprintf(stderr, "%s: incorrect shentsize? %i, should " |
1322 |
"be %i\n", filename, (int)ephentsize, |
"be %i\nPerhaps this is a dynamically linked " |
1323 |
(int)sizeof(Elf64_Shdr)); |
"binary (which isn't supported yet).\n", filename, |
1324 |
|
(int)eshentsize, (int)sizeof(Elf64_Shdr)); |
1325 |
exit(1); |
exit(1); |
1326 |
} |
} |
1327 |
} else { |
} else { |
1337 |
unencode(eshoff, &hdr32.e_shoff, Elf32_Off); |
unencode(eshoff, &hdr32.e_shoff, Elf32_Off); |
1338 |
if (ephentsize != sizeof(Elf32_Phdr)) { |
if (ephentsize != sizeof(Elf32_Phdr)) { |
1339 |
fprintf(stderr, "%s: incorrect phentsize? %i, should " |
fprintf(stderr, "%s: incorrect phentsize? %i, should " |
1340 |
"be %i\n", filename, (int)ephentsize, |
"be %i\nPerhaps this is a dynamically linked " |
1341 |
(int)sizeof(Elf32_Phdr)); |
"binary (which isn't supported yet).\n", filename, |
1342 |
|
(int)ephentsize, (int)sizeof(Elf32_Phdr)); |
1343 |
exit(1); |
exit(1); |
1344 |
} |
} |
1345 |
if (eshentsize != sizeof(Elf32_Shdr)) { |
if (eshentsize != sizeof(Elf32_Shdr)) { |
1346 |
fprintf(stderr, "%s: incorrect phentsize? %i, should " |
fprintf(stderr, "%s: incorrect shentsize? %i, should " |
1347 |
"be %i\n", filename, (int)ephentsize, |
"be %i\nPerhaps this is a dynamically linked " |
1348 |
(int)sizeof(Elf32_Shdr)); |
"binary (which isn't supported yet).\n", filename, |
1349 |
|
(int)eshentsize, (int)sizeof(Elf32_Shdr)); |
1350 |
exit(1); |
exit(1); |
1351 |
} |
} |
1352 |
} |
} |
1359 |
|
|
1360 |
ok = 0; |
ok = 0; |
1361 |
switch (arch) { |
switch (arch) { |
1362 |
|
case ARCH_ALPHA: |
1363 |
|
switch (emachine) { |
1364 |
|
case EM_ALPHA: |
1365 |
|
case -28634: |
1366 |
|
ok = 1; |
1367 |
|
} |
1368 |
|
break; |
1369 |
|
case ARCH_ARM: |
1370 |
|
switch (emachine) { |
1371 |
|
case EM_ARM: |
1372 |
|
ok = 1; |
1373 |
|
} |
1374 |
|
break; |
1375 |
|
case ARCH_AVR: |
1376 |
|
switch (emachine) { |
1377 |
|
case EM_AVR: |
1378 |
|
ok = 1; |
1379 |
|
} |
1380 |
|
break; |
1381 |
|
case ARCH_HPPA: |
1382 |
|
switch (emachine) { |
1383 |
|
case EM_PARISC: |
1384 |
|
ok = 1; |
1385 |
|
} |
1386 |
|
break; |
1387 |
|
case ARCH_I960: |
1388 |
|
switch (emachine) { |
1389 |
|
case EM_960: |
1390 |
|
ok = 1; |
1391 |
|
} |
1392 |
|
break; |
1393 |
|
case ARCH_IA64: |
1394 |
|
switch (emachine) { |
1395 |
|
case EM_IA_64: |
1396 |
|
ok = 1; |
1397 |
|
} |
1398 |
|
break; |
1399 |
|
case ARCH_M68K: |
1400 |
|
switch (emachine) { |
1401 |
|
case EM_68K: |
1402 |
|
ok = 1; |
1403 |
|
} |
1404 |
|
break; |
1405 |
case ARCH_MIPS: |
case ARCH_MIPS: |
1406 |
switch (emachine) { |
switch (emachine) { |
1407 |
case EM_MIPS: |
case EM_MIPS: |
1416 |
ok = 1; |
ok = 1; |
1417 |
} |
} |
1418 |
break; |
break; |
1419 |
case ARCH_SPARC: |
case ARCH_SH: |
1420 |
switch (emachine) { |
switch (emachine) { |
1421 |
case EM_SPARC: |
case EM_SH: |
|
case EM_SPARCV9: |
|
1422 |
ok = 1; |
ok = 1; |
1423 |
} |
} |
1424 |
break; |
break; |
1425 |
case ARCH_HPPA: |
case ARCH_SPARC: |
1426 |
switch (emachine) { |
switch (emachine) { |
1427 |
case EM_PARISC: |
case EM_SPARC: |
1428 |
|
case EM_SPARCV9: |
1429 |
ok = 1; |
ok = 1; |
1430 |
} |
} |
1431 |
break; |
break; |
1432 |
case ARCH_ALPHA: |
case ARCH_X86: |
1433 |
switch (emachine) { |
switch (emachine) { |
1434 |
case EM_ALPHA: |
case EM_386: |
1435 |
case -28634: |
case EM_486: |
1436 |
|
*tocp = 1; |
1437 |
|
ok = 1; |
1438 |
|
break; |
1439 |
|
case EM_AMD64: |
1440 |
|
*tocp = 2; |
1441 |
ok = 1; |
ok = 1; |
1442 |
|
break; |
1443 |
} |
} |
1444 |
break; |
break; |
1445 |
default: |
default: |
1473 |
* TODO: Find out what e_flag actually contains. |
* TODO: Find out what e_flag actually contains. |
1474 |
* TODO 2: This only sets mips16 for cpu 0. Yuck. Fix this! |
* TODO 2: This only sets mips16 for cpu 0. Yuck. Fix this! |
1475 |
*/ |
*/ |
1476 |
|
if (arch == ARCH_MIPS && ((eflags >> 24) & 0xff) == 0x24) { |
|
if (((eflags >> 24) & 0xff) == 0x24) { |
|
1477 |
debug("MIPS16 encoding (e_flags = 0x%08x)\n", eflags); |
debug("MIPS16 encoding (e_flags = 0x%08x)\n", eflags); |
1478 |
#ifdef ENABLE_MIPS16 |
#ifdef ENABLE_MIPS16 |
1479 |
m->cpus[0]->cd.mips.mips16 = 1; |
m->cpus[0]->cd.mips.mips16 = 1; |
1482 |
"(or use the --mips16 configure option)\n"); |
"(or use the --mips16 configure option)\n"); |
1483 |
exit(1); |
exit(1); |
1484 |
#endif |
#endif |
1485 |
} else if (eentry & 0x3) { |
} else if (arch == ARCH_MIPS && (eentry & 0x3)) { |
1486 |
debug("MIPS16 encoding (eentry not 32-bit aligned)\n"); |
debug("MIPS16 encoding (eentry not 32-bit aligned)\n"); |
1487 |
#ifdef ENABLE_MIPS16 |
#ifdef ENABLE_MIPS16 |
1488 |
m->cpus[0]->cd.mips.mips16 = 1; |
m->cpus[0]->cd.mips.mips16 = 1; |
1493 |
#endif |
#endif |
1494 |
} |
} |
1495 |
|
|
1496 |
|
/* |
1497 |
|
* SH64: 32-bit instruction encoding? TODO |
1498 |
|
*/ |
1499 |
|
if (arch == ARCH_SH && (eentry & 1)) { |
1500 |
|
debug("SH64: 32-bit instruction encoding\n"); |
1501 |
|
m->cpus[0]->cd.sh.compact = 0; |
1502 |
|
m->cpus[0]->cd.sh.bits = 64; |
1503 |
|
} |
1504 |
|
|
1505 |
/* Read the program headers: */ |
/* Read the program headers: */ |
1506 |
|
|
1507 |
for (i=0; i<ephnum; i++) { |
for (i=0; i<ephnum; i++) { |
1538 |
unencode(p_align, &phdr32.p_align, Elf32_Word); |
unencode(p_align, &phdr32.p_align, Elf32_Word); |
1539 |
} |
} |
1540 |
|
|
1541 |
|
/* |
1542 |
|
* Hack for loading Linux/PPC kernels, linked to high |
1543 |
|
* addresses, at low addresses. |
1544 |
|
*/ |
1545 |
|
if (arch == ARCH_PPC) { |
1546 |
|
if (elf64) { |
1547 |
|
p_vaddr &= 0x0fffffffffffffffULL; |
1548 |
|
p_paddr &= 0x0fffffffffffffffULL; |
1549 |
|
} else { |
1550 |
|
p_vaddr &= 0x0fffffff; |
1551 |
|
p_paddr &= 0x0fffffff; |
1552 |
|
} |
1553 |
|
} |
1554 |
|
|
1555 |
if (p_memsz != 0 && (p_type == PT_LOAD || |
if (p_memsz != 0 && (p_type == PT_LOAD || |
1556 |
(p_type & PF_MASKPROC) == PT_MIPS_REGINFO)) { |
(p_type & PF_MASKPROC) == PT_MIPS_REGINFO)) { |
1557 |
debug("chunk %i (", i); |
debug("chunk %i (", i); |
1570 |
debug(" len=0x%llx\n", (long long)p_memsz); |
debug(" len=0x%llx\n", (long long)p_memsz); |
1571 |
|
|
1572 |
if (p_vaddr != p_paddr) { |
if (p_vaddr != p_paddr) { |
1573 |
fprintf(stderr, "%s: vaddr != paddr. TODO: " |
if (elf64) |
1574 |
"how to handle this? vaddr=%016llx paddr" |
fatal("NOTE: vaddr (0x%llx) and " |
1575 |
"=%016llx\n", filename, (long long)p_vaddr, |
"paddr (0x%llx) differ; using " |
1576 |
(long long)p_paddr); |
"vaddr\n", (long long)p_vaddr, |
1577 |
exit(1); |
(long long)p_paddr); |
1578 |
|
else |
1579 |
|
fatal("NOTE: vaddr (0x%08x) and " |
1580 |
|
"paddr (0x%08x) differ; using vaddr" |
1581 |
|
"\n", (int)p_vaddr, (int)p_paddr); |
1582 |
} |
} |
1583 |
|
|
1584 |
if (p_memsz < p_filesz) { |
if (p_memsz < p_filesz) { |
1642 |
} |
} |
1643 |
} |
} |
1644 |
|
|
1645 |
/* Read the section headers to find the address of the _gp symbol: */ |
/* |
1646 |
|
* Read the section headers to find the address of the _gp |
1647 |
|
* symbol (for MIPS): |
1648 |
|
*/ |
1649 |
|
|
1650 |
for (i=0; i<eshnum; i++) { |
for (i=0; i<eshnum; i++) { |
1651 |
int sh_name, sh_type, sh_flags, sh_link, sh_info, sh_entsize; |
int sh_name, sh_type, sh_flags, sh_link, sh_info, sh_entsize; |
1799 |
} else { |
} else { |
1800 |
sym32 = symbols_sym32[i]; |
sym32 = symbols_sym32[i]; |
1801 |
unencode(st_name, &sym32.st_name, Elf32_Word); |
unencode(st_name, &sym32.st_name, Elf32_Word); |
1802 |
unencode(st_info, &sym64.st_info, Elf_Byte); |
unencode(st_info, &sym32.st_info, Elf_Byte); |
1803 |
unencode(addr, &sym32.st_value, Elf32_Word); |
unencode(addr, &sym32.st_value, Elf32_Word); |
1804 |
unencode(size, &sym32.st_size, Elf32_Word); |
unencode(size, &sym32.st_size, Elf32_Word); |
1805 |
} |
} |
1806 |
|
|
1807 |
|
/* debug("symbol info=0x%02x addr=0x%016llx" |
1808 |
|
" (%i) '%s'\n", st_info, (long long)addr, |
1809 |
|
st_name, symbol_strings + st_name); */ |
1810 |
|
|
1811 |
if (size == 0) |
if (size == 0) |
1812 |
size ++; |
size ++; |
1813 |
|
|
1814 |
if (addr != 0) { |
if (addr != 0) /* && ((st_info >> 4) & 0xf) |
1815 |
|
>= STB_GLOBAL) */ { |
1816 |
/* debug("symbol info=0x%02x addr=0x%016llx" |
/* debug("symbol info=0x%02x addr=0x%016llx" |
1817 |
" '%s'\n", st_info, (long long)addr, |
" '%s'\n", st_info, (long long)addr, |
1818 |
symbol_strings + st_name); */ |
symbol_strings + st_name); */ |
1819 |
add_symbol_name(&m->symbol_context, |
add_symbol_name(&m->symbol_context, |
1820 |
addr, size, symbol_strings + st_name, 0); |
addr, size, symbol_strings + st_name, |
1821 |
|
0, -1); |
1822 |
} |
} |
1823 |
|
|
1824 |
if (strcmp(symbol_strings + st_name, "_gp") == 0) { |
if (strcmp(symbol_strings + st_name, "_gp") == 0) { |
1914 |
char *filename, uint64_t *entrypointp, |
char *filename, uint64_t *entrypointp, |
1915 |
int arch, uint64_t *gpp, int *byte_orderp, uint64_t *tocp) |
int arch, uint64_t *gpp, int *byte_orderp, uint64_t *tocp) |
1916 |
{ |
{ |
1917 |
int iadd = 4; |
int iadd = DEBUG_INDENTATION, old_quiet_mode; |
1918 |
FILE *f; |
FILE *f; |
1919 |
unsigned char buf[12]; |
unsigned char buf[12]; |
1920 |
int len, i; |
unsigned char buf2[2]; |
1921 |
|
size_t len, len2, i; |
1922 |
off_t size; |
off_t size; |
1923 |
|
|
1924 |
if (byte_orderp == NULL) { |
if (byte_orderp == NULL) { |
1940 |
if (filename[0] == '@') |
if (filename[0] == '@') |
1941 |
return; |
return; |
1942 |
|
|
1943 |
debug("loading %s:\n", filename); |
debug("loading %s%s\n", filename, verbose >= 2? ":" : ""); |
1944 |
debug_indentation(iadd); |
debug_indentation(iadd); |
1945 |
|
|
1946 |
|
old_quiet_mode = quiet_mode; |
1947 |
|
if (verbose < 2) |
1948 |
|
quiet_mode = 1; |
1949 |
|
|
1950 |
f = fopen(filename, "r"); |
f = fopen(filename, "r"); |
1951 |
if (f == NULL) { |
if (f == NULL) { |
1952 |
file_load_raw(machine, mem, filename, entrypointp); |
file_load_raw(machine, mem, filename, entrypointp); |
1954 |
} |
} |
1955 |
|
|
1956 |
fseek(f, 0, SEEK_END); |
fseek(f, 0, SEEK_END); |
1957 |
size = ftell(f); |
size = ftello(f); |
1958 |
fseek(f, 0, SEEK_SET); |
fseek(f, 0, SEEK_SET); |
1959 |
|
|
1960 |
memset(buf, 0, sizeof(buf)); |
memset(buf, 0, sizeof(buf)); |
1961 |
len = fread(buf, 1, sizeof(buf), f); |
len = fread(buf, 1, sizeof(buf), f); |
1962 |
|
fseek(f, 510, SEEK_SET); |
1963 |
|
len2 = fread(buf2, 1, sizeof(buf2), f); |
1964 |
fclose(f); |
fclose(f); |
1965 |
|
|
1966 |
if (len < (signed int)sizeof(buf)) { |
if (len < (signed int)sizeof(buf)) { |
1976 |
goto ret; |
goto ret; |
1977 |
} |
} |
1978 |
|
|
1979 |
/* Is it an a.out? (Special case for DEC OSF1 kernels.) */ |
/* Is it an a.out? */ |
1980 |
if (buf[0]==0x00 && buf[1]==0x8b && buf[2]==0x01 && buf[3]==0x07) { |
if (buf[0]==0x00 && buf[1]==0x8b && buf[2]==0x01 && buf[3]==0x07) { |
1981 |
|
/* MIPS a.out */ |
1982 |
file_load_aout(machine, mem, filename, 0, |
file_load_aout(machine, mem, filename, 0, |
1983 |
entrypointp, arch, byte_orderp); |
entrypointp, arch, byte_orderp); |
1984 |
goto ret; |
goto ret; |
1985 |
} |
} |
1986 |
|
if (buf[0]==0x00 && buf[1]==0x87 && buf[2]==0x01 && buf[3]==0x08) { |
1987 |
|
/* M68K a.out */ |
1988 |
|
file_load_aout(machine, mem, filename, |
1989 |
|
AOUT_FLAG_VADDR_ZERO_HACK /* for OpenBSD/mac68k */, |
1990 |
|
entrypointp, arch, byte_orderp); |
1991 |
|
goto ret; |
1992 |
|
} |
1993 |
|
if (buf[0]==0x00 && buf[1]==0x8f && buf[2]==0x01 && buf[3]==0x0b) { |
1994 |
|
/* ARM a.out */ |
1995 |
|
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
1996 |
|
entrypointp, arch, byte_orderp); |
1997 |
|
goto ret; |
1998 |
|
} |
1999 |
|
if (buf[0]==0x00 && buf[1]==0x86 && buf[2]==0x01 && buf[3]==0x0b) { |
2000 |
|
/* i386 a.out (old OpenBSD and NetBSD etc) */ |
2001 |
|
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
2002 |
|
entrypointp, arch, byte_orderp); |
2003 |
|
goto ret; |
2004 |
|
} |
2005 |
|
if (buf[0]==0x01 && buf[1]==0x03 && buf[2]==0x01 && buf[3]==0x07) { |
2006 |
|
/* SPARC a.out (old 32-bit NetBSD etc) */ |
2007 |
|
file_load_aout(machine, mem, filename, AOUT_FLAG_NO_SIZES, |
2008 |
|
entrypointp, arch, byte_orderp); |
2009 |
|
goto ret; |
2010 |
|
} |
2011 |
if (buf[0]==0x00 && buf[2]==0x00 && buf[8]==0x7a && buf[9]==0x75) { |
if (buf[0]==0x00 && buf[2]==0x00 && buf[8]==0x7a && buf[9]==0x75) { |
2012 |
file_load_aout(machine, mem, filename, 1, |
/* DEC OSF1 on MIPS: */ |
2013 |
|
file_load_aout(machine, mem, filename, AOUT_FLAG_DECOSF1, |
2014 |
entrypointp, arch, byte_orderp); |
entrypointp, arch, byte_orderp); |
2015 |
goto ret; |
goto ret; |
2016 |
} |
} |
2017 |
|
|
2018 |
/* |
/* |
2019 |
|
* Is it a Mach-O file? |
2020 |
|
*/ |
2021 |
|
if (buf[0] == 0xfe && buf[1] == 0xed && buf[2] == 0xfa && |
2022 |
|
(buf[3] == 0xce || buf[3] == 0xcf)) { |
2023 |
|
file_load_macho(machine, mem, filename, entrypointp, |
2024 |
|
arch, byte_orderp, buf[3] == 0xcf, 0); |
2025 |
|
goto ret; |
2026 |
|
} |
2027 |
|
if ((buf[0] == 0xce || buf[0] == 0xcf) && buf[1] == 0xfa && |
2028 |
|
buf[2] == 0xed && buf[3] == 0xfe) { |
2029 |
|
file_load_macho(machine, mem, filename, entrypointp, |
2030 |
|
arch, byte_orderp, buf[0] == 0xcf, 1); |
2031 |
|
goto ret; |
2032 |
|
} |
2033 |
|
|
2034 |
|
/* |
2035 |
* Is it an ecoff? |
* Is it an ecoff? |
2036 |
* |
* |
2037 |
* TODO: What's the deal with the magic value's byte order? Sometimes |
* TODO: What's the deal with the magic value's byte order? Sometimes |
2071 |
fprintf(stderr, "\nThis file is very large (%lli bytes)\n", |
fprintf(stderr, "\nThis file is very large (%lli bytes)\n", |
2072 |
(long long)size); |
(long long)size); |
2073 |
fprintf(stderr, "Are you sure it is a kernel and not a disk " |
fprintf(stderr, "Are you sure it is a kernel and not a disk " |
2074 |
"image?\n"); |
"image? (Use the -d option.)\n"); |
2075 |
exit(1); |
exit(1); |
2076 |
} |
} |
2077 |
|
|
2078 |
|
if (size == 1474560) |
2079 |
|
fprintf(stderr, "Hm... this file is the size of a 1.44 MB " |
2080 |
|
"floppy image. Maybe you forgot the\n-d switch?\n"); |
2081 |
|
|
2082 |
/* |
/* |
2083 |
* Last resort: symbol definitions from nm (or nm -S): |
* Last resort: symbol definitions from nm (or nm -S): |
2084 |
* |
* |
2094 |
"unknown.\n\n ", filename); |
"unknown.\n\n ", filename); |
2095 |
for (i=0; i<(signed)sizeof(buf); i++) |
for (i=0; i<(signed)sizeof(buf); i++) |
2096 |
fprintf(stderr, " %02x", buf[i]); |
fprintf(stderr, " %02x", buf[i]); |
2097 |
|
|
2098 |
|
if (len2 == 2 && buf2[0] == 0x55 && buf2[1] == 0xaa) |
2099 |
|
fprintf(stderr, "\n\nIt has a PC-style " |
2100 |
|
"bootsector marker."); |
2101 |
|
|
2102 |
fprintf(stderr, "\n\nPossible explanations:\n\n" |
fprintf(stderr, "\n\nPossible explanations:\n\n" |
2103 |
" o) If this is a disk image, you forgot '-d' " |
" o) If this is a disk image, you forgot '-d' " |
2104 |
"on the command line.\n" |
"on the command line.\n" |
2110 |
|
|
2111 |
ret: |
ret: |
2112 |
debug_indentation(-iadd); |
debug_indentation(-iadd); |
2113 |
|
quiet_mode = old_quiet_mode; |
2114 |
} |
} |
2115 |
|
|