[llvm-dev] Cross compiling for Baremetal ARM without using GCC

Daniel Kang via llvm-dev llvm-dev at lists.llvm.org
Tue Oct 31 16:55:14 PDT 2017


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 at 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

2) 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.

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)?
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



This message and any attachments may contain confidential information from Cypress or its subsidiaries. If it has been received in error, please advise the sender and immediately delete this message.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20171031/424056b0/attachment-0001.html>


More information about the llvm-dev mailing list