Bug 25496 - clang++ -pg links with -lc++ instead of -lc++_p

I’m seeing a link failure when using “clang++ -pg” to link a program.

I first saw the problem on FreeBSD 10.1:

$ clang -v
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
Target: x86_64-unknown-freebsd10.1

I was able to reproduce the problem on FreeBSD 11.0-CURRENT:

$ clang -v
FreeBSD clang version 3.7.0 (tags/RELEASE_370/final 246257) 20150906
Target: x86_64-unknown-freebsd11.0

Here is a small test script to reproduce the problem:

#!/bin/sh -x
clang++ -pg -o foo -x c++ - -pthread <<!EOF!
#include <pthread.h>
static pthread_mutex_t mutex;
int main(int argc, char **argv) { pthread_mutex_lock(&mutex); return 0; }
!EOF!

The failure looks like this:

  • clang++ -pg -o foo -x c++ - -pthread
    /usr/bin/ld: //lib/libgcc_s.so.1: invalid DSO for symbol `_Unwind_ForcedUnwind@@GCC_3.0’ definition
    //lib/libgcc_s.so.1: could not read symbols: Bad value
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)

On success, the program compiles and links without any warnings.

If you add -v to the clang++ command line, you can see that it’s linking with -lc++, whereas all the other “system” type libraries that it’s linking with have “_p” tacked onto their names. When I adjusted the driver to use -lc++_p in this case, the link proceeded without errors. The diff (in the FreeBSD HEAD tree) looked like this:

Index: contrib/llvm/tools/clang/lib/Driver/ToolChain.cpp