| 413 | |
| 414 | |
| 415 | def get_binary_arch(path: str) -> BinaryArch: |
| 416 | with open(path, 'rb') as f: |
| 417 | sig = f.read(64) |
| 418 | if sig.startswith(b'\x7fELF'): # ELF |
| 419 | bits = {1: 32, 2: 64}[sig[4]] |
| 420 | endian = {1: '<', 2: '>'}[sig[5]] |
| 421 | machine, = struct.unpack_from(endian + 'H', sig, 0x12) |
| 422 | isa = {i.value:i for i in ISA}.get(machine, ISA.Other) |
| 423 | elif sig[:4] in (b'\xcf\xfa\xed\xfe', b'\xce\xfa\xed\xfe'): # Mach-O |
| 424 | s, cpu_type, = struct.unpack_from('<II', sig, 0) |
| 425 | bits = {0xfeedface: 32, 0xfeedfacf: 64}[s] |
| 426 | cpu_type &= 0xff |
| 427 | isa = {0x7: ISA.AMD64, 0xc: ISA.ARM64}[cpu_type] |
| 428 | else: |
| 429 | raise SystemExit(f'Unknown binary format with signature: {sig[:4]!r}') |
| 430 | return BinaryArch(bits=bits, isa=isa) |
| 431 | |
| 432 | |
| 433 | def test_compile( |