[cfe-commits] r169310 - in /cfe/trunk: lib/Driver/Tools.cpp test/Driver/asan-ld.c

Matt Beaumont-Gay matthewbg at google.com
Tue Dec 4 13:18:26 PST 2012


Author: matthewbg
Date: Tue Dec  4 15:18:26 2012
New Revision: 169310

URL: http://llvm.org/viewvc/llvm-project?rev=169310&view=rev
Log:
Currently, with -fsanitize=address, the driver appends libclang_rt.asan.a to
the link command. This all works fine when the driver is also responsible for
adding -lstdc++ to the link command. But, if -lstdc++ (or libstdc++.a, etc) is
passed explicitly to the driver, the ASan runtime will appear in the link
command after the standard library, leading to multiple-definition errors for
the global 'operator new' and 'operator delete'. Fix this in a painfully
simple way, by inserting libclang_rt.asan.a at the start of the link command
instead of the end.

If we need to do something more clever, we can walk the link command looking
for something that resembles libstdc++ and insert libclang_rt.asan.a as late
as possible, but the simple solution works for now.

Modified:
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/test/Driver/asan-ld.c

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=169310&r1=169309&r2=169310&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Tue Dec  4 15:18:26 2012
@@ -1512,7 +1512,7 @@
     llvm::sys::path::append(LibAsan, "lib", "linux",
         (Twine("libclang_rt.asan-") +
             TC.getArchName() + "-android.so"));
-    CmdArgs.push_back(Args.MakeArgString(LibAsan));
+    CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
   } else {
     if (!Args.hasArg(options::OPT_shared)) {
       // LibAsan is "libclang_rt.asan-<ArchName>.a" in the Linux library
@@ -1521,7 +1521,11 @@
       llvm::sys::path::append(LibAsan, "lib", "linux",
                               (Twine("libclang_rt.asan-") +
                                TC.getArchName() + ".a"));
-      CmdArgs.push_back(Args.MakeArgString(LibAsan));
+      // The ASan runtime needs to come before -lstdc++ (or -lc++, libstdc++.a,
+      // etc.) so that the linker picks ASan's versions of the global 'operator
+      // new' and 'operator delete' symbols. We take the extreme (but simple)
+      // strategy of inserting it at the front of the link command.
+      CmdArgs.insert(CmdArgs.begin(), Args.MakeArgString(LibAsan));
       CmdArgs.push_back("-lpthread");
       CmdArgs.push_back("-ldl");
       CmdArgs.push_back("-export-dynamic");

Modified: cfe/trunk/test/Driver/asan-ld.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/asan-ld.c?rev=169310&r1=169309&r2=169310&view=diff
==============================================================================
--- cfe/trunk/test/Driver/asan-ld.c (original)
+++ cfe/trunk/test/Driver/asan-ld.c Tue Dec  4 15:18:26 2012
@@ -24,7 +24,17 @@
 // CHECK-LINUX-CXX: "-ldl"
 // CHECK-LINUX-CXX: "-export-dynamic"
 // CHECK-LINUX-CXX: stdc++
+
+// RUN: %clang -no-canonical-prefixes %s -### -o /dev/null -fsanitize=address \
+// RUN:     -target i386-unknown-linux --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:     -lstdc++ -static 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-LINUX-CXX-STATIC %s
 //
+// CHECK-LINUX-CXX-STATIC: "{{.*}}ld{{(.exe)?}}"
+// CHECK-LINUX-CXX-STATIC-NOT: stdc++
+// CHECK-LINUX-CXX-STATIC: libclang_rt.asan-i386.a"
+// CHECK-LINUX-CXX-STATIC: stdc++
+
 // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
 // RUN:     -target arm-linux-androideabi -fsanitize=address \
 // RUN:     --sysroot=%S/Inputs/basic_android_tree/sysroot \





More information about the cfe-commits mailing list