[clang] 03142c5 - [Driver] Correctly handle static C++ standard library
Petr Hosek via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 24 00:40:38 PDT 2021
Author: Petr Hosek
Date: 2021-09-24T00:40:16-07:00
New Revision: 03142c5f67788bcc1573f76732d0fccd75c6b965
URL: https://github.com/llvm/llvm-project/commit/03142c5f67788bcc1573f76732d0fccd75c6b965
DIFF: https://github.com/llvm/llvm-project/commit/03142c5f67788bcc1573f76732d0fccd75c6b965.diff
LOG: [Driver] Correctly handle static C++ standard library
When statically linking C++ standard library, we shouldn't add -Bdynamic
after including the library on the link line because that might override
user settings like -static and -static-pie. Rather, we should surround
the library with --push-state/--pop-state to make sure that -Bstatic
only applies to C++ standard library and nothing else. This has been
supported since GNU ld 2.25 (2014) so backwards compatibility should
no longer be a concern.
Differential Revision: https://reviews.llvm.org/D110128
Added:
Modified:
clang/lib/Driver/ToolChains/Fuchsia.cpp
clang/lib/Driver/ToolChains/Gnu.cpp
clang/test/Driver/fuchsia.cpp
clang/test/Driver/linux-ld.c
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/Fuchsia.cpp b/clang/lib/Driver/ToolChains/Fuchsia.cpp
index 144443db5a073..fe865229f5bbe 100644
--- a/clang/lib/Driver/ToolChains/Fuchsia.cpp
+++ b/clang/lib/Driver/ToolChains/Fuchsia.cpp
@@ -138,14 +138,13 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
!Args.hasArg(options::OPT_static);
CmdArgs.push_back("--push-state");
- CmdArgs.push_back("--as-needed");
if (OnlyLibstdcxxStatic)
CmdArgs.push_back("-Bstatic");
+ else
+ CmdArgs.push_back("--as-needed");
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
- if (OnlyLibstdcxxStatic)
- CmdArgs.push_back("-Bdynamic");
- CmdArgs.push_back("-lm");
CmdArgs.push_back("--pop-state");
+ CmdArgs.push_back("-lm");
}
}
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 9aca45312bb0a..82262e58a446a 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -577,11 +577,13 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (ToolChain.ShouldLinkCXXStdlib(Args)) {
bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
!Args.hasArg(options::OPT_static);
- if (OnlyLibstdcxxStatic)
+ if (OnlyLibstdcxxStatic) {
+ CmdArgs.push_back("--push-state");
CmdArgs.push_back("-Bstatic");
+ }
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
if (OnlyLibstdcxxStatic)
- CmdArgs.push_back("-Bdynamic");
+ CmdArgs.push_back("--pop-state");
}
CmdArgs.push_back("-lm");
}
diff --git a/clang/test/Driver/fuchsia.cpp b/clang/test/Driver/fuchsia.cpp
index bd4c7ebeb5601..2165f7e4f15e6 100644
--- a/clang/test/Driver/fuchsia.cpp
+++ b/clang/test/Driver/fuchsia.cpp
@@ -37,8 +37,8 @@
// CHECK: "--push-state"
// CHECK: "--as-needed"
// CHECK: "-lc++"
-// CHECK: "-lm"
// CHECK: "--pop-state"
+// CHECK: "-lm"
// CHECK-X86_64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-fuchsia{{/|\\\\}}libclang_rt.builtins.a"
// CHECK-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}aarch64-unknown-fuchsia{{/|\\\\}}libclang_rt.builtins.a"
// CHECK-RISCV64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}riscv64-unknown-fuchsia{{/|\\\\}}libclang_rt.builtins.a"
@@ -55,12 +55,10 @@
// RUN: -fuse-ld=lld 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK-STATIC
// CHECK-STATIC: "--push-state"
-// CHECK-STATIC: "--as-needed"
// CHECK-STATIC: "-Bstatic"
// CHECK-STATIC: "-lc++"
-// CHECK-STATIC: "-Bdynamic"
-// CHECK-STATIC: "-lm"
// CHECK-STATIC: "--pop-state"
+// CHECK-STATIC: "-lm"
// CHECK-STATIC: "-lc"
// RUN: %clangxx %s -### --target=x86_64-unknown-fuchsia -nostdlib++ -fuse-ld=lld 2>&1 \
diff --git a/clang/test/Driver/linux-ld.c b/clang/test/Driver/linux-ld.c
index cc505588331bb..87be241726710 100644
--- a/clang/test/Driver/linux-ld.c
+++ b/clang/test/Driver/linux-ld.c
@@ -496,6 +496,22 @@
// CHECK-GCC-VERSION1: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
// CHECK-GCC-VERSION1: "{{.*}}/Inputs/basic_linux_tree/usr/lib/gcc/i386-unknown-linux/10.2.0{{/|\\\\}}crtbegin.o"
+// RUN: %clangxx -x c++ %s -### 2>&1 \
+// RUN: --target=x86_64-unknown-linux-gnu \
+// RUN: -stdlib=libc++ \
+// RUN: | FileCheck --check-prefix=CHECK-BASIC-LIBCXX-SHARED %s
+// CHECK-BASIC-LIBCXX-SHARED: "-lc++"
+// CHECK-BASIC-LIBCXX-SHARED-SAME: {{^}} "-lm"
+// RUN: %clangxx -x c++ %s -### 2>&1 \
+// RUN: --target=x86_64-unknown-linux-gnu \
+// RUN: -stdlib=libc++ -static-libstdc++ \
+// RUN: | FileCheck --check-prefix=CHECK-BASIC-LIBCXX-STATIC %s
+// CHECK-BASIC-LIBCXX-STATIC: "--push-state"
+// CHECK-BASIC-LIBCXX-STATIC-SAME: {{^}} "-Bstatic"
+// CHECK-BASIC-LIBCXX-STATIC-SAME: {{^}} "-lc++"
+// CHECK-BASIC-LIBCXX-STATIC-SAME: {{^}} "--pop-state"
+// CHECK-BASIC-LIBCXX-STATIC-SAME: {{^}} "-lm"
+
// Test a simulated installation of libc++ on Linux, both through sysroot and
// the installation path of Clang.
// RUN: %clangxx -no-canonical-prefixes -x c++ %s -### -o %t.o 2>&1 \
More information about the cfe-commits
mailing list