A LLVM and Clang compiler toolchain built for kernel development

Overview

Cosmic-Clang Toolchain

This is a LLVM and Clang compiler toolchain built for kernel development. Builds are always made from the latest LLVM sources rather than stable releases, so complete stability cannot be guaranteed.

This toolchain targets the AArch32 and AArch64 architectures.

Polly, LLVM's polyhedral loop optimizer, is also included for users who want to experiment with additional optimization. Note that this toolchain is not suitable for anything other than bare-metal development; it has not been built with support for any libc or userspace development in mind.

binutils is also included for convenience. Unlike LLVM, however, the latest stable version of binutils is always used to reduce the opportunity for breakage because this project is primarily focused on cutting-edge Clang, not binutils. This means that users do not need to download separate GCC toolchains to build the Linux kernel.

The build scripts (powered by tc-build) behind this can be found here.

Host compatibility

This toolchain is built on Ubuntu 20.04 focal, which uses glibc 2.37. Compatibility with older distributions cannot be guaranteed. Other libc implementations (such as musl) are not supported.

Building Linux

For an AArch64 cross-compilation setup, you must set the following variables. Some of them can be environment variables, but some must be passed directly to make as a command-line argument. It is recommended to pass all of them as make arguments to avoid confusing errors:

  • CC=clang (must be passed directly to make)
  • CROSS_COMPILE=aarch64-linux-gnu-
  • If your kernel has a 32-bit vDSO: CROSS_COMPILE_ARM32=arm-linux-gnueabi-

Optionally, you can also choose to use as many LLVM tools as possible to reduce reliance on binutils. All of these must be passed directly to make:

  • AS=llvm-as
  • AR=llvm-ar
  • NM=llvm-nm
  • OBJCOPY=llvm-objcopy
  • OBJDUMP=llvm-objdump
  • OBJSIZE=llvm-size
  • STRIP=llvm-strip

Note, however, that additional kernel patches may be required for these LLVM tools to work. It is also possible to replace the binutils linkers (lf.bfd and ld.gold) with lld and use Clang's integrated assembler for inline assembly in C code, but that will require many more kernel patches and it is currently impossible to use the integrated assembler for all assembly code in the kernel.

Android kernels older than 4.14 will require patches for compiling with any Clang toolchain to work; those patches are out of the scope of this project. See android-kernel-clang for more information.

Android kernels 4.19 and newer use the upstream variable CROSS_COMPILE_COMPAT. When building these kernels, replace CROSS_COMPILE_ARM32 in your commands and scripts with CROSS_COMPILE_COMPAT.

Differences from other toolchains

Proton Clang has been designed to be easy-to-use compared to other toolchains, such as AOSP Clang. The differences are as follows:

  • CLANG_TRIPLE does not need to be set because we don't use AOSP binutils
  • LD_LIBRARY_PATH does not need to be set because we set library load paths in the toolchain
  • No separate GCC/binutils toolchains are necessary; all tools are bundled

Common problems

as: unrecognized option '-EL'

Usually, this means that CROSS_COMPILE is not set correctly. Check that variable as well as your PATH to make sure that the required tools are available for Clang to invoke. You can test it manually like tihs:

$ ${CROSS_COMPILE}ld -v
GNU ld (GNU Binutils) 2.37

If you see Command not found or any other error, one of the two variables mentioned above is most likely set incorrectly.

If you continue to encounter this error after verifying CROSS_COMPILE and PATH, you are probably running into a change in Clang 12's handling of cross-compiling. The fix is to either merge linux-stable (which already has the fix included) cherry-pick "Makefile: Fix GCC_TOOLCHAIN_DIR prefix for Clang cross compilation" manually.

If the error still continues to appear and you have an arm64 kernel that includes vdso32, you will also need to cherry-pick "arm64: vdso32: Fix '--prefix=' value for newer versions of clang" to fix the second vdso32 error. Merging linux-stable is also an option if you are on Linux 5.4 or newer.

Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler

This error is actually a result of stack protector checks in the Makefile that cause the real error to be masked. It indicates that one or more unsupported compiler flags have been passed to Clang. Disable CONFIG_CC_STACKPROTECTOR_STRONG in favor of CONFIG_CC_STACKPROTECTOR_NONE temporarily (make sure you don't keep this change permanently for security reasons) and Clang will output the flag that is causing the problem.

In downstream kernels, the cause is usually big.LITTLE CPU optimization flags that have been added to the Makefile unconditionally. The correct solution is to check cc-name for the current compiler and adjust the optimization flags accordingly — big.LITTLE for GCC and little-only for Clang. See "Makefile: Optimize for sm8150's Kryo 485 CPU setup" for an example implementation of this.

scripts/gcc-version.sh: line 25: aarch64-linux-gnu-gcc: command not found

This is caused by unconditional invocations of gcc-version.sh in CAF's camera_v2 driver. The recommended solution is to cherry-pick Google's fix from the Pixel 2 kernel, which simply removes the faulty invocations as they were never useful to begin with.

Note that these errors are harmless and don't necessarily need to be fixed, but nonetheless, ignoring them is not recommended.

undefined reference to 'stpcpy'

This is caused by a libcall optimization added in Clang 14 that optimizes certain basic sprintf calls into stpcpy calls. The correct fix for this is to cherry-pick "lib/string.c: implement stpcpy", which adds a simple implementation of stpcpy to the kernel so that Clang can use it.

You might also like...
NCC is an ANSI/ISO-compliant optimizing C compiler.

The compiler is retargetable by design, but, at present, it only produces binaries for Linux/x86_64. As the compiler ABI differs somewhat from the System V ABI used by Linux, its code cannot be linked against Linux system libraries. It does, however, provide its own (incomplete) standard ANSI/Posix C library.

yadcc - Yet Another Distributed C++ Compiler

Yet Another Distributed C++ Compiler. yadcc是一套腾讯广告自研的分布式编译系统,用于支撑腾讯广告的日常开发及流水线。相对于已有的同类解决方案,我们针对实际的工业生产环境做了性能、可靠性、易用性等方面优化。

C implementation of the Tiny BASIC compiler found in an article by Dr. Austin Henley

Teeny Tiny Basic A C implementation of the Tiny BASIC compiler found in this article and this github repo by Dr. Austin Henley. I did pretty well in A

bcc is an interactive compiler of a language called b.

bcc is an interactive compiler of a language called b.

This is a compiler written from scratch in C

C Compiler This is a compiler written from scratch in C, with fully supporting C18 as a goal. It can currently compile itself, and most simple program

mrcceppc is a reimplementation project for the Metrowerks mwcceppc compiler.

Compiler | mrcceppc mrcceppc is a reimplementation project for the Metrowerks mwcceppc compiler. Compiling Run generate_{version}.bat for which versio

A C header that allow users to compile brainfuck programs within a C compiler.

brainfuck.h A C header that allow users to compile brainfuck programs within a C compiler. You can insert the header into the top of your brainfuck so

Gilbraltar is a version of the OCaml compiler to be able to build a MirageOS for RaspberryPi 4.

Gilbraltar is a version of the OCaml compiler to be able to build a MirageOS for RaspberryPi 4. It's a work in progress repository to provide a dune's toolchain (as ocaml-freestanding) specialized for Raspberry Pi 4.

 NaiveCC: a compiler frontend for a subset of C
NaiveCC: a compiler frontend for a subset of C

NaiveCC: a compiler frontend for a subset of C This is a toy compiler frontend for a subset of the C programming language based on the LR(1) parsing t

Owner
Ǥђ๏ຮ₮⌁Ⲙครtє࿐
Thanks to all open source software projects.
Ǥђ๏ຮ₮⌁Ⲙครtє࿐
JuCC - Jadavpur University Compiler Compiler

JuCC This is the official Jadavpur University Compiler Compiler repository. Key Features Supports a subset of the C language for now. Custom grammar f

Shuvayan Ghosh Dastidar 36 Sep 1, 2022
Compiler Design Project: Simulation of front-end phase of C Compiler involving switch-case construct.

CSPC41 Compiler Design Project Assignment Compiler Design Project: Simulation of front-end phase of C Compiler involving switch-case construct. Using

Adeep Hande 1 Dec 15, 2021
Pre-configured LLVM and ANTLR4 for C++

LLVM + ANTLR4 Starter Project Starter project for ANTLR4 and LLVM C++ project. Prerequisite LLVM 12 Java (for ANTLR4) git Install prerequisite librari

Nathanael Demacon 12 Aug 9, 2022
nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines.

nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines. Tiny? The only types are: int (32-bit signed integer) char (8-

Ajay Tatachar 19 Nov 28, 2022
Smaller C is a simple and small single-pass C compiler

Smaller C is a simple and small single-pass C compiler, currently supporting most of the C language common between C89/ANSI C and C99 (minus some C89 and plus some C99 features).

Alexey Frunze 1.2k Jan 7, 2023
Microvm is a virtual machine and compiler

The aim of this project is to create a stack based language and virtual machine for microcontrollers. A mix of approaches is used. Separate memory is used for program and variable space (Harvard architecture). An interpreter, virtual machine and compiler are available. A demostration of the interpreter in action is presented below.

null 10 Aug 14, 2022
Aheui JIT compiler for PC and web

아희짓 개요 아희짓은 아희 언어를 위한 JIT (Just in Time) 컴파일러입니다. 어셈블러와 유틸 라이브러리외에 외부 라이브러리에 전혀 의존하지 않고 JIT을 바닥부터 구현합니다. 지원 환경 64비트 windows, mac, linux (x86 아키텍쳐) 웹어셈

Sunho Kim 28 Sep 23, 2022
A Small C Compiler

8cc C Compiler Note: 8cc is no longer an active project. The successor is chibicc. 8cc is a compiler for the C programming language. It's intended to

Rui Ueyama 5.8k Jan 8, 2023
GNU Prolog is a native Prolog compiler

GNU Prolog is a native Prolog compiler with constraint solving over finite domains (FD)

Daniel Diaz 64 Dec 13, 2022
Take your first step in writing a compiler.

first-step Take your first step in writing a compiler. Building from Source Before building first-step, please make sure you have installed the follow

PKU Compiler Course 28 Aug 20, 2022