- Building Linux with Clang/LLVMВ¶
- AboutВ¶
- ClangВ¶
- Cross CompilingВ¶
- LLVM UtilitiesВ¶
- Omitting CROSS_COMPILEВ¶
- Supported ArchitecturesВ¶
- Building Linux with Clang/LLVMВ¶
- AboutВ¶
- ClangВ¶
- Cross CompilingВ¶
- LLVM UtilitiesВ¶
- Omitting CROSS_COMPILEВ¶
- Supported ArchitecturesВ¶
- Building Linux with Clang/LLVMВ¶
- AboutВ¶
- ClangВ¶
- Cross CompilingВ¶
- LLVM UtilitiesВ¶
- Supported ArchitecturesВ¶
- Building Linux with Clang/LLVMВ¶
- AboutВ¶
- ClangВ¶
- Cross CompilingВ¶
- LLVM UtilitiesВ¶
- Supported ArchitecturesВ¶
- Building the Linux kernel
Building Linux with Clang/LLVMВ¶
This document covers how to build the Linux kernel with Clang and LLVM utilities.
AboutВ¶
The Linux kernel has always traditionally been compiled with GNU toolchains such as GCC and binutils. Ongoing work has allowed for Clang and LLVM utilities to be used as viable substitutes. Distributions such as Android, ChromeOS, and OpenMandriva use Clang built kernels. LLVM is a collection of toolchain components implemented in terms of C++ objects. Clang is a front-end to LLVM that supports C and the GNU C extensions required by the kernel, and is pronounced “klang,” not “see-lang.”
ClangВ¶
The compiler used can be swapped out via CC= command line argument to make . CC= should be set when selecting a config and during a build.
Cross CompilingВ¶
A single Clang compiler binary will typically contain all supported backends, which can help simplify cross compiling.
CROSS_COMPILE is not used to prefix the Clang compiler binary, instead CROSS_COMPILE is used to set a command line flag: —target=
LLVM UtilitiesВ¶
LLVM has substitutes for GNU binutils utilities. Kbuild supports LLVM=1 to enable them.
They can be enabled individually. The full list of the parameters:
The integrated assembler is enabled by default. You can pass LLVM_IAS=0 to disable it.
Omitting CROSS_COMPILEВ¶
As explained above, CROSS_COMPILE is used to set —target=
If CROSS_COMPILE is not specified, the —target=
That means if you use only LLVM tools, CROSS_COMPILE becomes unnecessary.
For example, to cross-compile the arm64 kernel:
If LLVM_IAS=0 is specified, CROSS_COMPILE is also used to derive —prefix=
to search for the GNU assembler and linker.
Supported ArchitecturesВ¶
LLVM does not target all of the architectures that Linux supports and just because a target is supported in LLVM does not mean that the kernel will build or work without any issues. Below is a general summary of architectures that currently work with CC=clang or LLVM=1 . Level of support corresponds to “S” values in the MAINTAINERS files. If an architecture is not present, it either means that LLVM does not target it or there are known issues. Using the latest stable version of LLVM or even the development tree will generally yield the best results. An architecture’s defconfig is generally expected to work well, certain configurations may have problems that have not been uncovered yet. Bug reports are always welcome at the issue tracker below!
Источник
Building Linux with Clang/LLVMВ¶
This document covers how to build the Linux kernel with Clang and LLVM utilities.
AboutВ¶
The Linux kernel has always traditionally been compiled with GNU toolchains such as GCC and binutils. Ongoing work has allowed for Clang and LLVM utilities to be used as viable substitutes. Distributions such as Android, ChromeOS, and OpenMandriva use Clang built kernels. LLVM is a collection of toolchain components implemented in terms of C++ objects. Clang is a front-end to LLVM that supports C and the GNU C extensions required by the kernel, and is pronounced “klang,” not “see-lang.”
ClangВ¶
The compiler used can be swapped out via CC= command line argument to make . CC= should be set when selecting a config and during a build.
Cross CompilingВ¶
A single Clang compiler binary will typically contain all supported backends, which can help simplify cross compiling.
CROSS_COMPILE is not used to prefix the Clang compiler binary, instead CROSS_COMPILE is used to set a command line flag: —target=
LLVM UtilitiesВ¶
LLVM has substitutes for GNU binutils utilities. Kbuild supports LLVM=1 to enable them.
They can be enabled individually. The full list of the parameters:
The integrated assembler is enabled by default. You can pass LLVM_IAS=0 to disable it.
Omitting CROSS_COMPILEВ¶
As explained above, CROSS_COMPILE is used to set —target=
If CROSS_COMPILE is not specified, the —target=
That means if you use only LLVM tools, CROSS_COMPILE becomes unnecessary.
For example, to cross-compile the arm64 kernel:
If LLVM_IAS=0 is specified, CROSS_COMPILE is also used to derive —prefix=
to search for the GNU assembler and linker.
Supported ArchitecturesВ¶
LLVM does not target all of the architectures that Linux supports and just because a target is supported in LLVM does not mean that the kernel will build or work without any issues. Below is a general summary of architectures that currently work with CC=clang or LLVM=1 . Level of support corresponds to “S” values in the MAINTAINERS files. If an architecture is not present, it either means that LLVM does not target it or there are known issues. Using the latest stable version of LLVM or even the development tree will generally yield the best results. An architecture’s defconfig is generally expected to work well, certain configurations may have problems that have not been uncovered yet. Bug reports are always welcome at the issue tracker below!
Источник
Building Linux with Clang/LLVMВ¶
This document covers how to build the Linux kernel with Clang and LLVM utilities.
AboutВ¶
The Linux kernel has always traditionally been compiled with GNU toolchains such as GCC and binutils. Ongoing work has allowed for Clang and LLVM utilities to be used as viable substitutes. Distributions such as Android, ChromeOS, and OpenMandriva use Clang built kernels. LLVM is a collection of toolchain components implemented in terms of C++ objects. Clang is a front-end to LLVM that supports C and the GNU C extensions required by the kernel, and is pronounced “klang,” not “see-lang.”
ClangВ¶
The compiler used can be swapped out via CC= command line argument to make . CC= should be set when selecting a config and during a build.
Cross CompilingВ¶
A single Clang compiler binary will typically contain all supported backends, which can help simplify cross compiling.
CROSS_COMPILE is not used to prefix the Clang compiler binary, instead CROSS_COMPILE is used to set a command line flag: —target=
LLVM UtilitiesВ¶
LLVM has substitutes for GNU binutils utilities. Kbuild supports LLVM=1 to enable them.
They can be enabled individually. The full list of the parameters:
Currently, the integrated assembler is disabled by default. You can pass LLVM_IAS=1 to enable it.
Supported ArchitecturesВ¶
LLVM does not target all of the architectures that Linux supports and just because a target is supported in LLVM does not mean that the kernel will build or work without any issues. Below is a general summary of architectures that currently work with CC=clang or LLVM=1 . Level of support corresponds to “S” values in the MAINTAINERS files. If an architecture is not present, it either means that LLVM does not target it or there are known issues. Using the latest stable version of LLVM or even the development tree will generally yield the best results. An architecture’s defconfig is generally expected to work well, certain configurations may have problems that have not been uncovered yet. Bug reports are always welcome at the issue tracker below!
Источник
Building Linux with Clang/LLVMВ¶
This document covers how to build the Linux kernel with Clang and LLVM utilities.
AboutВ¶
The Linux kernel has always traditionally been compiled with GNU toolchains such as GCC and binutils. Ongoing work has allowed for Clang and LLVM utilities to be used as viable substitutes. Distributions such as Android, ChromeOS, and OpenMandriva use Clang built kernels. LLVM is a collection of toolchain components implemented in terms of C++ objects. Clang is a front-end to LLVM that supports C and the GNU C extensions required by the kernel, and is pronounced “klang,” not “see-lang.”
ClangВ¶
The compiler used can be swapped out via CC= command line argument to make . CC= should be set when selecting a config and during a build.
Cross CompilingВ¶
A single Clang compiler binary will typically contain all supported backends, which can help simplify cross compiling.
CROSS_COMPILE is not used to prefix the Clang compiler binary, instead CROSS_COMPILE is used to set a command line flag: —target=
LLVM UtilitiesВ¶
LLVM has substitutes for GNU binutils utilities. Kbuild supports LLVM=1 to enable them.
They can be enabled individually. The full list of the parameters:
Currently, the integrated assembler is disabled by default. You can pass LLVM_IAS=1 to enable it.
Supported ArchitecturesВ¶
LLVM does not target all of the architectures that Linux supports and just because a target is supported in LLVM does not mean that the kernel will build or work without any issues. Below is a general summary of architectures that currently work with CC=clang or LLVM=1 . Level of support corresponds to “S” values in the MAINTAINERS files. If an architecture is not present, it either means that LLVM does not target it or there are known issues. Using the latest stable version of LLVM or even the development tree will generally yield the best results. An architecture’s defconfig is generally expected to work well, certain configurations may have problems that have not been uncovered yet. Bug reports are always welcome at the issue tracker below!
Источник
Building the Linux kernel
The Linux kernel can be built with clang/LLVM for X64 and ARM64. We can use the Checked C version of clang/LLVM instead, allowing us to try out using Checked C in the kernel. With some patches the Linux kernel can be built using clang/LLVM for ARM32, but it is not booting with those patches.
For X64 and ARM64, a stable recent version of the Linux kernel can be used. For example, Linux 4.19 RC3 can be used. For ARM32, a fork of 4.19 RC1 with patches applied is available at ttps://github.com/Prabhuk/linux_kernel_clang_build
Add installation of prerequisites
Instructions to build the kernel for X86_64
Please follow the instructions to build Checked C Clang.
Boot the built image under QEMU:
Instructions to build ARM
- Linux Kernel code (referred as kernel code or kernel going forward), is typically compiled using gcc.
- Certain gcc optimizations used by kernel code are available only in gcc and these optimizations are applied through use of compiler flags.
- There is no guaranteed binary interoperability for binary code that is generated by different compilers.
- However, the libraries that confirm to the platform ABI can interoperate though the libraries themselves are incompatible C libraries.
- https://softwareengineering.stackexchange.com/questions/235706/are-c-object-files-created-with-different-compilers-binary-compatible
- https://kevinaboos.wordpress.com/2013/10/03/static-analysis-of-linux-kernel-drivers-using-clang/
- LLVMLinux patches repo: https://github.com/Fuzion24/LLVM-Linux-Kernel
- Google’s efforts: https://lkml.org/lkml/2017/8/22/912
- https://github.com/nathanchance/android-kernel-clang
- https://lwn.net/Articles/734071/
- ARM32: http://lists.llvm.org/pipermail/llvm-dev/2018-March/121810.html
- ChromiumOS kernel: https://chromium.googlesource.com/chromiumos/third_party/kernel/+/v4.9
- https://github.com/dnschneid/crouton/wiki/Build-chrome-os-kernel-and-kernel-modules
- Android kernel: https://android.googlesource.com/kernel/common/+log/f0907aa15ed9f9c7541bb244ed3f52c376ced19c
- https://clang.llvm.org/docs/CrossCompilation.html
Building for ARM 64 and x86 architectures:
Источник