I have never used a compiler other than gcc before to cross-compile c code for ARM. This is the first time I am straying outside gcc. All these days I have always heard all kinds of promising reviews on LLVM. So Let us try it out now. If you are using ubuntu, you can install llvm using apt,
apt-get install llvm clang
If you are using any other distribution, follow these instructions to compile llvm from source.
Its time to get our hands dirty. Here is the simple code we are going to compile,
The compiling process is like this,
1. compile c code into llvm assembly using clang
2. convert llvm assembly into ARM assembly using llc
3. convert ARM assembly code into binary/ELF using binutils
clang -emit-llvm hello.c -c -o hello.bc
-emit-llvm option instructs clang to generate llvm assembly. The generated llvm assembly,
hello.bc can be executed using
Let us convert this llvm assembly into ARM assembly,
llc -march=arm hello.bc -o hello.s
Now, we can binutils(as + ld) to assemble and link these generated ARM assembly files.
arm-elf-gcc hello.s -o hello.elf
You can use gdb to simulate the generated elf.
Here is the single command which compiles, assembles and links the given c code,
clang -march=armv7-a -ccc-host-triple arm-elf -ccc-gcc-name arm-elf-gcc hello.c