New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 25496 - clang++ -pg links with -lc++ instead of -lc++_p
Summary: clang++ -pg links with -lc++ instead of -lc++_p
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Driver (show other bugs)
Version: 3.7
Hardware: PC FreeBSD
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-11 16:06 PST by yelliott
Modified: 2019-07-07 00:05 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description yelliott 2015-11-11 16:06:51 PST
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
===================================================================
--- contrib/llvm/tools/clang/lib/Driver/ToolChain.cpp   (revision 290623)
+++ contrib/llvm/tools/clang/lib/Driver/ToolChain.cpp   (working copy)
@@ -451,7 +451,11 @@

   switch (Type) {
   case ToolChain::CST_Libcxx:
-    CmdArgs.push_back("-lc++");
+    if (Args.hasArg(options::OPT_pg)) {
+      CmdArgs.push_back("-lc++_p");
+    } else {
+      CmdArgs.push_back("-lc++");
+    }
     break;

   case ToolChain::CST_Libstdcxx:

I don't claim that this is a comprehensive solution -- there seem to be quite a few places in the driver that know about "-lc++" -- but it was enough to solve the problem that I was seeing on FreeBSD.
Comment 1 Dimitry Andric 2016-01-16 18:05:50 PST
The review at http://reviews.llvm.org/D16264 is intended to fix this.
Comment 2 Fangrui Song 2019-07-07 00:05:27 PDT
D16264/rC260851

FreeBSD/OpenBSD may use -lc++_p when -pg is passed.