When I pass --coverage to clang (which I build from trunk together with llvm, projects/compiler-rt and clang/tools/extra), I get a link error. It seems that /opt/llvm/r181582/lib/clang/3.4/lib/linux/libclang_rt.profile-x86_64.a does not get included. It is not a general problem with it not finding the compiler-rt installation though, as -fsantize=undefined workes fine. The following transcript shows a hello world program, the link failure that is printed when only --coverage is specified, that linking succeeds if libclang_rt.profile-x86_64.a is manually specified, and that -fsantitize=undefined manages to find libclang_rt.ubsan-x86_64.a on its own. $ cat hello.c #include <stdio.h> int main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) { printf("Hello world\n"); return 0; } $ $ $ /opt/llvm/r181582/bin/clang -v --coverage hello.c clang version 3.4 (trunk 181582) Target: x86_64-unknown-linux-gnu Thread model: posix "/opt/llvm/r181582/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name hello.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.23.2 -v -femit-coverage-notes -femit-coverage-data -resource-dir /opt/llvm/r181582/bin/../lib/clang/3.4 -internal-isystem /usr/local/include -internal-isystem /opt/llvm/r181582/bin/../lib/clang/3.4/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/magnus/src/clang/coverage -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/hello-m48ncx.o -x c hello.c clang -cc1 version 3.4 based upon LLVM 3.4svn default target x86_64-unknown-linux-gnu ignoring nonexistent directory "/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /opt/llvm/r181582/bin/../lib/clang/3.4/include /usr/include/x86_64-linux-gnu /usr/include End of search list. "/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../.. -L/lib -L/usr/lib /tmp/hello-m48ncx.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crtn.o /opt/llvm/r181582/bin/../lib/libprofile_rt.a /tmp/hello-m48ncx.o: In function `__llvm_gcov_writeout': hello.c:(.text+0x65): undefined reference to `llvm_gcda_start_file' hello.c:(.text+0x7b): undefined reference to `llvm_gcda_emit_function' hello.c:(.text+0x8d): undefined reference to `llvm_gcda_emit_arcs' hello.c:(.text+0x92): undefined reference to `llvm_gcda_end_file' /tmp/hello-m48ncx.o: In function `__llvm_gcov_init': hello.c:(.text+0xd5): undefined reference to `llvm_gcov_init' clang: error: linker command failed with exit code 1 (use -v to see invocation) $ $ $ /opt/llvm/r181582/bin/clang --coverage -v hello.c /opt/llvm/r181582/lib/clang/3.4/lib/linux/libclang_rt.profile-x86_64.a clang version 3.4 (trunk 181582) Target: x86_64-unknown-linux-gnu Thread model: posix "/opt/llvm/r181582/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name hello.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.23.2 -v -femit-coverage-notes -femit-coverage-data -resource-dir /opt/llvm/r181582/bin/../lib/clang/3.4 -internal-isystem /usr/local/include -internal-isystem /opt/llvm/r181582/bin/../lib/clang/3.4/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/magnus/src/clang/coverage -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/hello-JKAJNW.o -x c hello.c clang -cc1 version 3.4 based upon LLVM 3.4svn default target x86_64-unknown-linux-gnu ignoring nonexistent directory "/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /opt/llvm/r181582/bin/../lib/clang/3.4/include /usr/include/x86_64-linux-gnu /usr/include End of search list. "/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../.. -L/lib -L/usr/lib /tmp/hello-JKAJNW.o /opt/llvm/r181582/lib/clang/3.4/lib/linux/libclang_rt.profile-x86_64.a -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crtn.o /opt/llvm/r181582/bin/../lib/libprofile_rt.a $ $ $ ./a.out Hello world $ $ $ /opt/llvm/r181582/bin/clang -v -fsanitize=undefined hello.c clang version 3.4 (trunk 181582) Target: x86_64-unknown-linux-gnu Thread model: posix "/opt/llvm/r181582/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name hello.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.23.2 -v -resource-dir /opt/llvm/r181582/bin/../lib/clang/3.4 -internal-isystem /usr/local/include -internal-isystem /opt/llvm/r181582/bin/../lib/clang/3.4/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/magnus/src/clang/coverage -ferror-limit 19 -fmessage-length 80 -fsanitize=alignment,bool,bounds,enum,float-cast-overflow,float-divide-by-zero,integer-divide-by-zero,null,object-size,return,shift,signed-integer-overflow,unreachable,vla-bound,vptr -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/hello-rF6z0N.o -x c hello.c clang -cc1 version 3.4 based upon LLVM 3.4svn default target x86_64-unknown-linux-gnu ignoring nonexistent directory "/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /opt/llvm/r181582/bin/../lib/clang/3.4/include /usr/include/x86_64-linux-gnu /usr/include End of search list. "/usr/bin/ld" -whole-archive /opt/llvm/r181582/bin/../lib/clang/3.4/lib/linux/libclang_rt.san-x86_64.a -no-whole-archive -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.7 -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.7/../../.. -L/lib -L/usr/lib /tmp/hello-rF6z0N.o -lpthread -ldl -whole-archive /opt/llvm/r181582/bin/../lib/clang/3.4/lib/linux/libclang_rt.ubsan-x86_64.a -no-whole-archive -lpthread -ldl -export-dynamic -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crtn.o $ $ $ ./a.out Hello world $
The addProfileRT() function in clang's lib/Driver/Tools.cpp needs to be updated to use libclang_rt, since that is where we've moved the runtime support for profiling. This doesn't affect Darwin, because the DarwinClang::AddLinkRuntimeLibArgs code already handles this.
This is a regression, btw. clang --coverage used to work on 3.2.
Seems to have been fixed somewhere between r184680 (fails) and r184817 (works).
Indeed it's fixed by r184666 and also r184708. http://llvm.org/viewvc/llvm-project?view=revision&revision=184666 http://llvm.org/viewvc/llvm-project?view=revision&revision=184708
Works in 3.4.