Giter Site home page Giter Site logo

stm32_without_gnu's Introduction

What you need to compile an armv7-m (stm32) project

Toolchain

My toolchain was compiled from the mainline source code, with clang, clang-tools-extra, lld and lldb enabled.

The key build command can be found as build_clang.sh

What you need to tweak

Standard peripheral libraries

Several naked functions are defined in core_cm3.c. They decleared variables inside the function body which works fine on gcc but is in fact a violation to the C standard. Clang will not build with those naked functions. What you need is patching naked functions with respect to armv7-m ABI. Fortunately the variables they decleared may be omitted.

Also, neither llvm-as nor the buildin one inside clang deals with all GNU AS syntaxs flawlessly. You need a C version of startup file, whose inline asm contains only syntaxs supported by clang/llvm.

Compile flags

gcc->clang

Clang supports multiple targets by default, thus you need to specify the actual target, that is, add --target=arm-none-eabi to CFLAGS

In practice, clang will refer to LDFLAGS, which causes a contradiction.

A project compiled with gcc would happily linked with builtin linker provided by gcc. In clang's case, the builtin one is broken, which cannot identify most of our arguments.

The workaround is use another FLAGS for our final link. LDFLAGS is set to -fuse-ld=$(PREFIX)ld.lld and we may define another LLDFLAGS for ld.lld to link: -T$(LDSCRIPT) Note that -nostartfiles is omitted in LLDFLAG, which is not supported as an argument but is a default behaviour. Detailed description can be found in references.

objdump->llvm-objdump

The cross GCC toolchain would identify the thumb instructions correctly, without any explict argument, while for llvm, an argument --arch-name=thumb is needed, for llvm-objdump would recognize our ELF as ARM, rather than thumb. As a result, mostly wrong instructions.

What's missing

This project does not depend on any GNU libraries including C libs, thus no compiler-rt is needed. For a project that depends on libc, this guide is quite a start point rather than the end point.

References

  1. https://rust-embedded.github.io/blog/2018-08-2x-psa-cortex-m-breakage/
  2. https://github.com/piratkin/simple/blob/master/build.sh
  3. https://clang.llvm.org/docs/CrossCompilation.html
  4. https://llvm.org/docs/HowToCrossCompileLLVM.html

stm32_without_gnu's People

Contributors

chenguokai avatar

Stargazers

Frank Pian avatar

Watchers

 avatar

Forkers

mttbx

stm32_without_gnu's Issues

是否可以链接.s文件?

大佬,我看你把链接的.s文件屏蔽了,这个不需要么?

我在研究clang编译方向,有可能加个wechat么?
十分感谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.