This post shows how to build an LLVM bitcode (namely .bc image ) for Linux;
This repository presents how to build an LLVM bitcode. Specifically, here, we focus on Linux.
- LLVM/Clang version 7.0.1
- Python 3+
2. Download Linux Source Code
Here, we demo how to download the v4.12-r5 linux source code with git
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git cd linux-stable git reset --hard v4.12-rc5
3. Install LLVM, Python
The next step is to install LLVM, python;
- Install LLVM:
- Install python: Install python 3 on a Mac or an Utunbu, please download and install from the official site.
3. Use Whole Program LLVM to build bc
- Install WLLVM
bash pip install wllvm
Setup the system environment WLLVM includes four python executables:
wllvmfor compiling C code and
wllvm++for compiling C++, an auxiliary tool
extract-bcfor extracting the bitcode from a build product (object file, executable, library or archive), and a sanity checker,
wllvm-sanity-checkerfor detecting configuration oversights.
Three environment variables must be set to use these wrappers:
LLVM_COMPILERshould be set to either
LLVM_GCC_PREFIXshould be set to the prefix for the version of gcc that should be used with dragonegg. This can be empty if there is no prefix. This variable is not used if
$LLVM_COMPILER == clang.
LLVM_DRAGONEGG_PLUGINshould be the full path to the dragonegg plugin. This variable is not used if
$LLVM_COMPILER == clang.
Once the environment is set up, just use
wllvm++as your C and C++ compilers, respectively.
In addition to the above environment variables the following can be optionally used:
LLVM_CC_NAMEcan be set if your clang compiler is not called
clangbut something like
LLVM_CXX_NAMEcan be used to describe what the C++ compiler is called. Note that in these sorts of cases, the environment variable
LLVM_COMPILERshould still be set to
clang-3.7etc. We also pay attention to the environment variables
LLVM_AR_NAMEin an analagous way, since they too get adorned with suffixes in various Linux distributions.
LLVM_COMPILER_PATHcan be set to the absolute path to the folder that contains the compiler and other LLVM tools such as
llvm-linkto be used. This prevents searching for the compiler in your PATH environment variable. This can be useful if you have different versions of clang on your system and you want to easily switch compilers without tinkering with your PATH variable. Example
WLLVM_CONFIGURE_ONLYcan be set to anything. If it is set,
wllvm++behave like a normal C or C++ compiler. They do not produce bitcode. Setting
WLLVM_CONFIGURE_ONLYmay prevent configuration errors caused by the unexpected production of hidden bitcode files. It is sometimes required when configuring a build.
Note that using LLVM to compile Linux directly will fail. Therefore, patches should be applied first.
Please download patches from clang-kernel-build.
patch -p1 -i <path-to-clang-kerenl-build>/clang-flags.patch patch -p1 -i <path-to-clang-kerenl-build>/clang-uaccess.patch # boot-workaround-PR18415.patch has been copied here from LLVMLinux buildbot, # which is currently down. patch -p1 -i <path-to-clang-kerenl-build>/boot-workaround-PR18415.patch patch -p1 -i <path-to-clang-kerenl-build>/llist.patch
- Build the Linux
- cd linux-stable
- make CC=wllvm defconfig
- make CC=wllvm -j64 2>&1 | tee build.log
With the above step, an exectuable
vmlinux is generated
- Extract the bitcode