Cross compiling for Baremetal ARM without using GCC

Dear LLVM developers,

Hello,

I’m trying to find a way of cross-compiling my c code against Baremetal Cortex-M device (so target triple will be arm-none-eabi) only using LLVM/Clang, and not using anything from GNU (ld or libc).

I’m doing this to know which one of LLVM/clang and GCC produces smaller flash image size because saving flash is a big deal in our projects.

  1. When I just follow this - https://clang.llvm.org/docs/CrossCompilation.html using LLVM/Clang Prebuilt binary 5.0.0, I got this error,

daniel@daniel-VirtualBox:~/clang/crosscompile$ make clean all

rm -f main *.o *.map *.a

“clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin”/clang -static --target=arm-none-eabi -mcpu=cortex-m0 -c -o main.o main.c

“clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin”/clang -static --target=arm-none-eabi -mcpu=cortex-m0 -c -o a.o a.c

“clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin”/clang -static --target=arm-none-eabi -mcpu=cortex-m0 -c -o reset.o reset.c

“clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin”/clang -v -static --target=armv6m-none-eabi -o main main.o a.o reset.o

clang version 5.0.0 (tags/RELEASE_500/final)

Target: armv6m-none–eabi

Thread model: single

InstalledDir: clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin

“clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/ld.lld” main.o a.o reset.o -Bstatic -Lclang+llvm-5.0.0-linux-x86_64-ubuntu16.04/lib/clang/5.0.0/lib/baremetal -lc -lm -lclang_rt.builtins-armv6m.a -o main

clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/ld.lld: error: unable to find library -lc

clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/ld.lld: error: unable to find library -lm

clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/ld.lld: error: unable to find library -lclang_rt.builtins-armv6m.a

clang-5.0: error: ld.lld command failed with exit code 1 (use -v to see invocation)

makefile:33: recipe for target ‘main’ failed

make: *** [main] Error 1

  1. To get clang_rt.builtins-armv6m.a or libc, libm, I tried to build LLVM/Clang (compiler_rt) from LLVM/Clang source following this - http://llvm.org/docs/HowToCrossCompileLLVM.html but I got this error,

TARGET_TRIPLE=arm-none-eabi

MYHOSTBIN=${HOME}/clang/source/build_x64/bin

MYGNUARM_ROOT=${HOME}/opt/gcc-arm-none-eabi-6-2017-q2-update

MYCFLAGS=“–specs=nosys.specs -mcpu=cortex-m4 -mthumb -mfloat-abi=softfp -mfpu=fpv4-sp-d16”

*cmake -G “Ninja” *

*-DCMAKE_CROSSCOMPILING=True *

*-DCMAKE_BUILD_TYPE=Release *

*-DCMAKE_INSTALL_PREFIX=${HOME}/clang/toInstall *

*-DLLVM_TABLEGEN=${MYHOSTBIN}/llvm-tblgen *

*-DCLANG_TABLEGEN=${MYHOSTBIN}/clang-tblgen *

*-DLLVM_DEFAULT_TARGET_TRIPLE=arm-none-eabi *

*-DLLVM_TARGET_ARCH=ARM *

*-DLLVM_TARGETS_TO_BUILD=ARM *

*-DLLVM_ENABLE_LTO=Full *

*-DCMAKE_C_FLAGS=“${MYCFLAGS}” *

*-DCMAKE_CXX_FLAGS=“${MYCFLAGS}” *

*-DCMAKE_SYSROOT=${MYGNUARM_ROOT} *

*-DCMAKE_TOOLCHAIN_FILE=…/toolchain.txt *

*-DCMAKE_C_COMPILER=${MYGNUARM_ROOT}/bin/arm-none-eabi-gcc *

*-DCMAKE_CXX_COMPILER=${MYGNUARM_ROOT}/bin/arm-none-eabi-g++ *

*-DCMAKE_ASM_COMPILER=${MYGNUARM_ROOT}/bin/arm-none-eabi-as *

…/llvm

– Looking for __atomic_load_8 in atomic

– Looking for __atomic_load_8 in atomic - not found

CMake Error at cmake/modules/CheckAtomic.cmake:74 (message):

Host compiler appears to require libatomic, but cannot find it.

Call Stack (most recent call first):

cmake/config-ix.cmake:350 (include)

CMakeLists.txt:585 (include)

arm-none-eabi toolchain doesn’t contain libatomic, but I need to build compiler_rt using this tool to get the runtime for ARM, so it’s confusing. Or building compiler_rt using arm-none-eabi toolchain is completely wrong idea in the first place.

  1. Then I found this mail thread http://lists.llvm.org/pipermail/llvm-dev/2017-August/116134.html which seems very close to the thing I’m trying, but the mail thread is unfinished.

So, my question is,

Is there a way of cross-compiling c code against Baremetal Cortex-M device only using LLVM/Clang, and not using anything from GNU (ld or libc)?

If so, do I have to build LLVM/Clang source code to get clang_rt.builtins-armv6m.a or libc, libm for ARM?

If so, the method 2) or 3) above is the right approach? Or are there any other instruction?

Any advice would be a great help for me.

Thank you

Daniel

+clm

Dear LLVM developers,

Hello,

I’m trying to find a way of cross-compiling my c code against Baremetal Cortex-M device (so target triple will be arm-none-eabi) only using LLVM/Clang, and not using anything from GNU (ld or libc).

Exciting!

I’m doing this to know which one of LLVM/clang and GCC produces smaller flash image size because saving flash is a big deal in our projects.

I'd be interested to hear about your findings.

>

snip

arm-none-eabi toolchain doesn’t contain libatomic, but I need to build compiler_rt using this tool to get the runtime for ARM, so it’s confusing. Or building compiler_rt using arm-none-eabi toolchain is completely wrong idea in the first place.

You could use an existing arm-none-eabi toolchain to build the builtins, but it would be better to build it with the just-built clang that you're going to build anyway.

3) Then I found this mail thread http://lists.llvm.org/pipermail/llvm-dev/2017-August/116134.html which seems very close to the thing I’m trying, but the mail thread is unfinished.

So, my question is,

Is there a way of cross-compiling c code against Baremetal Cortex-M device only using LLVM/Clang, and not using anything from GNU (ld or libc)?

Yes. The biggest hurdle at this point is probably going to be weird corner cases in lld's linker script support. Other than that, it is definitely possible to build & use a full llvm (+ newlib) toolchain that supports baremetal arm.

If so, do I have to build LLVM/Clang source code to get clang_rt.builtins-armv6m.a or libc, libm for ARM?

The notes in that thread you found should get you a working C toolchain. Extending that to C++ is where things are "unfinished".

Jon

Hello Daniel,

I don't have full answers but I may be able to move you slightly
forward. I'm assuming that you want an answer that involves only open
source tools.

I've managed to successfully build compiler-rt using Jonathan Roelofs
BaremetalARM.cmake in clang/cmake/caches and an arm-none-eabi gcc
installation as sysroot. One thing that I needed to do for it to work
was to move compiler-rt from llvm/projects/compiler-rt to
llvm/runtimes/compiler-rt. I haven't got any experience at building
newlib or Musl with clang instead of gcc.

I've been working on Arm support in LLD, in theory most of the parts
are there [*] and the linker script support has come a long way
recently. However I've not actively tested on embedded systems yet as
my focus has been on Linux/BSD like targets. At least a few people
have had problems with LLD always allocating the ELF header and
Program Header in the first PT_LOAD Program Header, which is
definitely not what you want for an embedded system. If you do run
into problems please let us know and raise bug reports if possible.

Peter

[*] There aren't any range extension thunks that work for Cortex-M0,
but in the vast majority of cases you won't need these.

Dear Peter,

Thank you very much for the prompt response.
It's good news that you managed to build compiler_rt, but when I followed the same instruction, I met some issues like
"Specified distribution component 'builtins-armv6m-none-eabi' doesn't have a Target".
I have a lot of questions but I think I need to try the instruction once more and get back to you.

Have a good day!

Best Regards,
Daniel

Hi Daniel,

It is able to simulate stm32-p103 board for running simple "Hello World" example https://github.com/xiangzhai/mini-arm-os/blob/llvm-toolchain/00-HelloWorld/Makefile via LLVM toolchain and qemu_stm32 https://github.com/beckus/qemu_stm32

$ make
/data/project/llvm/build/bin/clang -o hello.o -c --target=thumbv7em-unknown-none-eabi -mthumb -march=armv7e-m -mcpu=cortex-m3 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -ffreestanding -O0 -std=c11 -Wall hello.c
/data/project/llvm/build/bin/clang -o startup.o -c --target=thumbv7em-unknown-none-eabi -mthumb -march=armv7e-m -mcpu=cortex-m3 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -ffreestanding -O0 -std=c11 -Wall startup.c
/data/project/llvm/build/bin/ld.lld -o hello.elf --Bstatic --build-id --gc-sections --Map hello.map --script hello.ld hello.o startup.o
/data/project/llvm/build/bin/llvm-readobj -W hello.elf > hello.rd
/data/project/llvm/build/bin/llvm-objdump -D hello.elf > hello.lst
/data/project/llvm/build/bin/llvm-objcopy -O binary hello.elf hello.bin
/data/project/llvm/build/bin/llvm-size hello.elf
text data bss dec hex filename
190 14 0 204 cc hello.elf

$ make qemu
/data/project/qemu_stm32/arm-softmmu/qemu-system-arm -M stm32-p103 -nographic -kernel hello.bin

...
LED Off
Hello World!

But which C library should I choose for big ARMmbed project? Keith merged avr-libc stdio bits within the newlib environment. There were only three functions written in assembly language, two of them were just stubs while the third was a simple ultoa function with a weird interface. With those coded up in C embedded-arm-libc

And I am trying to build ufiasco for Realtek AMEBA RTL8195A board via LLVM toolchain, ARM Compiler 6.9 libc and LLVM's libcxx https://github.com/xiangzhai/ufiasco/tree/llvm-toolchain I will try Keith's newlib fork later :slight_smile: