Daniel Kang via llvm-dev
2017-Oct-31 23:55 UTC
[llvm-dev] 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 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>
Jonathan Roelofs via llvm-dev
2017-Nov-01 11:48 UTC
[llvm-dev] Cross compiling for Baremetal ARM without using GCC
+clm On 10/31/17 5:55 PM, Daniel Kang via llvm-dev wrote:> 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> > 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. > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-- Jon Roelofs jonathan at codesourcery.com CodeSourcery / Mentor Embedded / Siemens
Peter Smith via llvm-dev
2017-Nov-01 12:02 UTC
[llvm-dev] Cross compiling for Baremetal ARM without using GCC
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. On 31 October 2017 at 23:55, Daniel Kang via llvm-dev <llvm-dev at lists.llvm.org> wrote:> 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. > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >
Daniel Kang via llvm-dev
2017-Nov-01 19:49 UTC
[llvm-dev] Cross compiling for Baremetal ARM without using GCC
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> -----Original Message----- > From: Peter Smith [mailto:peter.smith at linaro.org] > Sent: Wednesday, November 01, 2017 5:02 AM > To: Daniel Kang <Daniel.Kang at cypress.com> > Cc: llvm-dev at lists.llvm.org; Nick Forrette <nick.forrette at cypress.com>; Mike > Noel <mike.noel at cypress.com> > Subject: Re: [llvm-dev] Cross compiling for Baremetal ARM without using GCC > > 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. > > On 31 October 2017 at 23:55, Daniel Kang via llvm-dev <llvm- > dev at lists.llvm.org> wrote: > > 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/bareme > > -Lclang+tal > > -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 > > clang+find > > library -lc > > > > clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/ld.lld: error: unable to > > clang+find > > library -lm > > > > clang+llvm-5.0.0-linux-x86_64-ubuntu16.04/bin/ld.lld: error: unable to > > clang+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. > > > > > > _______________________________________________ > > LLVM Developers mailing list > > llvm-dev at lists.llvm.org > > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >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.