Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary to arm

How I can run x86 binaries (for example .exe file) on arm?As I see on Wikipedia,I need to convert binary data for the emulated platform into binary data suitable for execution on the targeted platform.but question is:How I can do it?I need to open file in hex editor and change?Or something else?

like image 652
nmzik Avatar asked Apr 02 '13 05:04

nmzik


3 Answers

To successfully do this, you'd have to do two things.. one relatively easy, one very hard. Neither of which you want to do by hand in a hex editor.

  1. Convert the machine code from x86 to ARM. This is the easy one, because you should be able to map each x86 opcode to one or more ARM opcodes. There are different ways to do this, some more efficient than others, but it can be done with a pretty straightforward mapping.

  2. Remap function calls (and other jumps). This one is hard, because monkeying with the opcodes is going to change all the offsets for the jump and return points. If you have dynamically linked libraries (.so), and we assume that all the libraries are available at exactly the same version in both places (a sketchy assumption at best), you'd have to remap the loads.

It's essentially a machine->machine compiler and linker.

So, can you do it? Sure.

Is it easy? No.

There may be a commercial tool out there, but I'm not aware of it.

like image 135
PaulProgrammer Avatar answered Nov 07 '22 22:11

PaulProgrammer


You can not do this with a binary;note1 here binary means an object with no symbol information like an elf file. Even with an elf file, this is difficult to impossible. The issue is determining code from data. If you resolve this issue, then you can make de-compilers and other tools.

Even if you haven an elf file, a compiler will insert constants used in the code in the text segment. You have to look at many op-codes and do a reverse basic block to figure out where a function starts and ends.

A better mechanism is to emulate the x86 on the ARM. Here, you can use JIT technology to do the translation as encountered, but you approximately double code space. Also, the code will execute horribly. The ARM has 16 registers and the x86 is register starved (usually it has hidden registers). A compilers big job is to allocate these registers. QEMU is one technology that does this. I am unsure if it goes in the x86 to ARM direction; and it will have a tough job as noted.

Note1: The x86 has an asymmetric op-code sizing. In order to recognize a function prologue and epilogue, you would have to scan an image multiple times. To do this, I think the problem would be something like O(n!) where n is the bytes of the image, and then you might have trouble with in-line assembler and library routines coded in assembler. It maybe possible, but it is extremely hard.

like image 45
2 revs Avatar answered Nov 07 '22 20:11

2 revs


To run an ARM executable on an X86 machine all you need is qemu-user.

Example: you have busybox compiled for AARCH64 architecture (ARM64) and you want to run it on an X86_64 linux system:

Assuming a static compile darm binary:

$ qemu-aarch64-static ./busybox

What I am curioous is if there is a way to embed both in a single program.

like image 20
Zibri Avatar answered Nov 07 '22 21:11

Zibri