首页 > 技术点滴 > 让arm的程序在x86的cpu上运行

让arm的程序在x86的cpu上运行

2014年8月20日 baoz 阅读评论

如果希望简单的研究下arm里的东西又没有arm的环境,可以尝试用下面2个方案走起。

方案1、

svn checkout http://arm-x86.googlecode.com/svn/trunk/ arm-x86-read-only

方案2、

http://tuxthink.blogspot.com/2012/04/executing-arm-executable-in-x86-using.html

原文本地备份一个

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 $ arm-elf-gcc hello.c -o hello$ file hello
hello: ELF 32-bit LSB executable, ARM, version 1, statically linked, not stripped$ ./hello
bash:./hello: cannot execute binary file$ qemu-arm hello
Hello world

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.

分类: 技术点滴 标签: ,
  1. libmaru
    2014年8月21日20:18 | #1

    搭配binfmt-support使用更佳