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.105 2005/08/11 16:11:33 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 64 |
#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] = { |
131 |
#define AOUT_FLAG_DECOSF1 1 |
#define AOUT_FLAG_DECOSF1 1 |
132 |
#define AOUT_FLAG_FROM_BEGINNING 2 |
#define AOUT_FLAG_FROM_BEGINNING 2 |
133 |
#define AOUT_FLAG_VADDR_ZERO_HACK 4 |
#define AOUT_FLAG_VADDR_ZERO_HACK 4 |
134 |
|
#define AOUT_FLAG_NO_SIZES 8 |
135 |
/* |
/* |
136 |
* file_load_aout(): |
* file_load_aout(): |
137 |
* |
* |
167 |
if (flags & AOUT_FLAG_DECOSF1) { |
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; |
178 |
textsize = ftello(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)) { |
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 |
int len2 = 0; |
int len2 = 0; |
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; |
585 |
|
|
586 |
|
if (encoding == ELFDATA2LSB) |
587 |
|
*byte_orderp = EMUL_LITTLE_ENDIAN; |
588 |
|
else |
589 |
|
*byte_orderp = EMUL_BIG_ENDIAN; |
590 |
|
|
591 |
|
n_executables_loaded ++; |
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
|
/* |
596 |
* file_load_ecoff(): |
* file_load_ecoff(): |
597 |
* |
* |
598 |
* Loads an ecoff binary image into the emulated memory. The entry point |
* Loads an ecoff binary image into the emulated memory. The entry point |
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); */ |
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]; |
1366 |
ok = 1; |
ok = 1; |
1367 |
} |
} |
1368 |
break; |
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_SH: |
1420 |
|
switch (emachine) { |
1421 |
|
case EM_SH: |
1422 |
|
ok = 1; |
1423 |
|
} |
1424 |
|
break; |
1425 |
case ARCH_SPARC: |
case ARCH_SPARC: |
1426 |
switch (emachine) { |
switch (emachine) { |
1427 |
case EM_SPARC: |
case EM_SPARC: |
1442 |
break; |
break; |
1443 |
} |
} |
1444 |
break; |
break; |
|
case ARCH_ARM: |
|
|
switch (emachine) { |
|
|
case EM_ARM: |
|
|
ok = 1; |
|
|
} |
|
|
break; |
|
|
case ARCH_IA64: |
|
|
switch (emachine) { |
|
|
case EM_IA_64: |
|
|
ok = 1; |
|
|
} |
|
|
break; |
|
|
case ARCH_M68K: |
|
|
switch (emachine) { |
|
|
case EM_68K: |
|
|
ok = 1; |
|
|
} |
|
|
break; |
|
1445 |
default: |
default: |
1446 |
fatal("file.c: INTERNAL ERROR: Unimplemented arch!\n"); |
fatal("file.c: INTERNAL ERROR: Unimplemented arch!\n"); |
1447 |
} |
} |
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); |
1569 |
|
|
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 |
fatal("WARNING! vaddr (0x%llx) and paddr " |
if (elf64) |
1574 |
"(0x%llx) differ; using vaddr\n", |
fatal("NOTE: vaddr (0x%llx) and " |
1575 |
(long long)p_vaddr, (long long)p_paddr); |
"paddr (0x%llx) differ; using " |
1576 |
|
"vaddr\n", (long long)p_vaddr, |
1577 |
|
(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) { |
1585 |
fprintf(stderr, "%s: memsz < filesz. TODO: how" |
fprintf(stderr, "%s: memsz < filesz. TODO: how" |
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 |
} |
} |
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 |
unsigned char buf2[2]; |
unsigned char buf2[2]; |
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); |
1990 |
entrypointp, arch, byte_orderp); |
entrypointp, arch, byte_orderp); |
1991 |
goto ret; |
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) { |
if (buf[0]==0x00 && buf[1]==0x86 && buf[2]==0x01 && buf[3]==0x0b) { |
2000 |
/* i386 a.out (old OpenBSD and NetBSD etc) */ |
/* i386 a.out (old OpenBSD and NetBSD etc) */ |
2001 |
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
file_load_aout(machine, mem, filename, AOUT_FLAG_FROM_BEGINNING, |
2002 |
entrypointp, arch, byte_orderp); |
entrypointp, arch, byte_orderp); |
2003 |
goto ret; |
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 |
/* DEC OSF1 on MIPS: */ |
/* DEC OSF1 on MIPS: */ |
2013 |
file_load_aout(machine, mem, filename, AOUT_FLAG_DECOSF1, |
file_load_aout(machine, mem, filename, AOUT_FLAG_DECOSF1, |
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 |
2110 |
|
|
2111 |
ret: |
ret: |
2112 |
debug_indentation(-iadd); |
debug_indentation(-iadd); |
2113 |
|
quiet_mode = old_quiet_mode; |
2114 |
} |
} |
2115 |
|
|