Reputation: 75
I tried to use LLVM to compile C++ source code to arm binaries. Here's an example:
//hello.cpp
#include <iostream>
int main() {
std::cout << "Hello World!\n" << std::endl;
return 0;
}
I use the following commands:
clang++ -emit-llvm -c hello.cpp -o hello.bc
llc -march=arm hello.bc -o hello.s // I do need this step for running some backend optimizations
arm-none-linux-gnueabi-g++ -static hello.s -o hello.exe
There was no warnings or errors so far. Then I tried to run the "hello.exe" with qemu-arm and gem5 simulator, both crashed with segmentation fault:
output from qemu-arm:
qemu-arm hello.exe
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault (core dumped)
output form gem5:
~/gem5/build/ARM/gem5.opt ~/gem5/configs/example/se.py -c ./hello.exe
**** REAL SIMULATION ****
info: Entering event queue @ 0. Starting simulation...
panic: Page table fault when accessing virtual address 0xfffffff4
@ tick 2035000
[invoke:build/ARM/sim/faults.cc, line 70]
Memory Usage: 646432 KBytes
Program aborted at cycle 2035000
Aborted (core dumped)
I was using clang version 3.7.0svn. Am I missing anything? In fact the compiling process was used to several c programs and they all worked correctly. But not for c++ programs.
Upvotes: 3
Views: 1894
Reputation: 75
Thanks for everyone's help. I just figured out how it would work:
clang++ -target arm-none-linux-gnueabi --sysroot=/usr/local/arm-2009q3 -static hello.cpp -emit-llvm -c -I /usr/local/arm-2009q3/arm-none-linux-gnueabi/libc/usr/include/
llc hello.bc
arm-none-linux-gnueabi-g++ -static hello.s -o hello.exe
The arm binary runs properly on both qemu-arm and gem5.
I thought the LLVM IR should be totally independent from the target machine but in fact it is not (weird...)
I'm still not understand why c programs didn't have this problem.
Upvotes: 2
Reputation: 9324
This is not going to work in this way. In fact, LLVM IR is not target neutral, therefore compiling x86 IR to ARM won't work: ABIs are different, etc.
You need to use clang's -target option (or build clang as a cross-compiler specifying the default target).
Upvotes: 1