Pages

Executing arm executable in x86 using qemu

QEMU is an emulator capable of emulating a number of processors, enabling the code written for one architecture to run on another. It works in two modes

1. Use mode : In which only applications written for one architecture can be executed on another. For e.g. an application written for ARM can be executed on an x86 machine.

2. System mode: In which a complete Operating system compiled for an architecture can be run on another. For e.g. a kernel compiled for ARM can be booted on an X86 machine.

In this post we will see how to work with the user mode of the QEMU by executing an ARM executable on an x86 machine (any normal desktop or laptop are x86 systmes).
Before starting to work with QEMU we will have to install it, which can be done from the package manager.
In Debian based systems :



Or we can download the source from qemu and compile it from source. While compiling make sure that the target "ARM" and "x86" both are selected.

To confirm if the installation has happened properly, open a terminal and execute the command



To generate an arm executable we will also need the cross compiler for arm, which can be downloaded from "gnuarm" or compiled using the script in this post script to download and compile gnuarm

Now let us write a simple program in c, to print hello world:

hello.c



compile this using the arm cross compiler



To verify the file type of executable generated you can use the command file



Now try running the "hello" executable directly on the x86 system.



The error indicates that the executable is not supported by the architecture of the system.

Now let us try running the same using qemu



qemu-arm is able to execute the ARM executable successfully on x86 machine.
This enables us to write and test code of any architecture on usual x86 systems and then port only the final code on to the real system.


5 comments:

  1. Hi,while executing arm exe getting the result

    /lib/ld-linux-armhf.so.3: No such file or directory

    How to solve this..

    ReplyDelete
  2. I'm pretty sure it's because you're using an x86 PC without arm binaries for simple things, like gui or sound

    ReplyDelete
  3. I am also getting same issue.
    {/lib/ld-linux-armhf.so.3: No such file or directory}
    any suggestion

    ReplyDelete
    Replies
    1. arm-linux-gnueabi-gcc-9 hello.c -o hello -static

      https://stackoverflow.com/questions/47787562/u-boot-lib-ld-linux-armhf-so-3-no-such-file-or-directory

      Delete
  4. Hello
    I try thé same for ppc target but qemu complains not to find bios. I download openbios-ppc (i am on ubuntu on wsl) and then once buis is launched I do not know to launch my helloworld app.
    Qemu also complains not having SDL enabled. Any ideas ?

    ReplyDelete