Building FreeBSD with clang/llvm

This is an effort to get FreeBSD to build with clang/llvm, clang is a compiler built on the Low Level Virtual Machine compiler infrastructure. Both clang and llvm are released under a BSD like license. This is work in progress, play at your own risk

Status

The amd64 and i386 kernels boot multiuser. All of userland compiles except for libc, libobjc (cc1obj: not found) and C++ code. There are some pending patches to FreeBSD.

Build status - kernel

amd64

i386

arm

powerpc

GENERIC

OK

OK

Broken (inline asm)

Broken (llc dies with abort trap)

LINT

OK

ce(4)

Broken (inline asm)

Broken (llc dies with abort trap)

Build status - userland

Problem areas

sys/boot

freebsd bug - patch pending commit, still we dont fit into required space

libc

http://llvm.org/bugs/show_bug.cgi?id=879

gcc

http://llvm.org/bugs/show_bug.cgi?id=5089

csu

freebsd csu is broken, we are working on a replacement

Tracking bug is 3696.

Quickstart

CLANGBSD IS WORK-IN-PROGRESS, THINGS MIGHT NOT WORK

Checkout the ClangBSD branch:

# svn co svn://svn.freebsd.org/base/projects/clangbsd src

Setup /etc/src.conf like this:

WITH_CLANG=
WITH_CLANG_IS_CC=
NO_WERROR=
WERROR=
# Don't forget this when using Jails!
NO_FSCHG=

Building/installing world (into a chroot).

# make buildworld
# make installworld DESTDIR=/usr/obj/clang
# chroot /usr/obj/clang /bin/echo Hello clang world!
Hello clang world!
# chroot /usr/obj/clang /bin/tcsh

Building/installing kernel.

# make kernel KERNCONF=GENERIC INSTKERNNAME=clang

Boot it.

OK set module_path=/boot/clang
OK boot clang

To update the ClangBSD tree perform the following steps.

# cd src/
# svn up

Using the static analyzer

On the kernel (This requires lots of space in the output directory, 406M when using "scan-build make MODULES_OVERRIDE=").

# cd /sys/{amd64,i386}/conf/
# config GENERIC
# cd ../compile/GENERIC/
# make depend
# scan-build make MODULES_OVERRIDE=
# {firefox,epiphany,konqueror,lynx,less} /tmp/scan-build-YYYY-MM-DD-N/index.html

On a userland program.

# cd /usr/src/usr.bin/make/
# make cleandir && make obj && make depend
# scan-build make
# {firefox,epiphany,konqueror,lynx,less} /tmp/scan-build-YYYY-MM-DD-N/index.html

Using the analyzer on vanilla sources, no ClangBSD required

Scanning the FreeBSD sources using the static analyzer requires you to:

# svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
# cd llvm/tools && svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
# cd .. && ./configure --enable-optimized && gmake
# cp -p tools/clang/utils/ccc-analyzer tools/clang/utils/scan-build tools/clang/utils/scanview.css tools/clang/utils/sorttable.js Release/bin

Now you have all the necessary tools in llvm/Release/bin. They need to be next to each other, as we will be using the full-path to scan-build later and it must find clang-cc, which it searches in it's own directory and $PATH. As $PATH will be cleansed during buildworld, only the first option remains.

Running the analyzer is now as easy as

# cd /usr/src && /path/to/llvm/Release/bin/scan-build -k make buildworld

Weekly scans of buildworld and buildkernel can be studied at https://www.spoerlein.net/scan-build. The insane amount of false positives due to missing intra-procedural analysis (IPA) in ccc-analyzer makes them not very useful though. One run usually produces 3 GB of HTML output.

Link Time Optimizations

LLVM supports LTO. It can be used by these pseudo steps

0) build gold and libLLVMgold.so (see http://llvm.org/docs/LinkTimeOptimization.html)
1) CFLAGS+=-emit-llvm
2) setenv LD "/tmp/ld-new --plugin /tmp/libLLVMgold.so"
3) setenv CC clang
4) build the kernel

This currently does not work because of assertion being hit in llvm. Once this is fixed I dont know if the kernel works. There are suggestions
for using export maps. Investigate that.

This does not link because of missing symbols. Some variation of SYSTEM_LD from conf/kern.pre.mk should be used though. This is work in progress.

Automatic test generation with KLEE

KLEE is a symbolic virtual machine built on top of the LLVM compiler infrastructure. More about KLEE here and paper here.

Building KLEE

# svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
# svn co http://llvm.org/svn/llvm-project/cfe/trunk llvm/tools/clang
# svn co http://llvm.org/svn/llvm-project/klee/trunk klee
# cd llvm/
# ./configure --prefix=/usr/local --enable-optimized --enable-targets=host-only --enable-bindings=none
# gmake
# gmake install
# cd ../klee/
# ./configure --with-llvm=$PWD/../llvm
# gmake LLVMGCC=/usr/local/bin/clang
# cp Release/bin/* /usr/local/bin/
# cp Release/lib/* /usr/local/lib/

Using KLEE

# cd /usr/src/usr.bin/touch/
# clang -c -g -emit-llvm -o touch.bc touch.c
# klee touch.bc
KLEE: output directory = "klee-out-0"
...

More to come later...

Known issues

Contacts

Resources

BuildingFreeBSDWithClang (last edited 2009-10-19 20:38:46 by RomanDivacky)