[PATCH] D110128: [Driver] Correctly handle static C++ standard library
Petr Hosek via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 20 23:31:48 PDT 2021
phosek created this revision.
phosek added reviewers: MaskRay, mcgrathr.
phosek requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D110128
Files:
clang/lib/Driver/ToolChains/Fuchsia.cpp
clang/lib/Driver/ToolChains/Gnu.cpp
clang/test/Driver/fuchsia.cpp
clang/test/Driver/linux-ld.c
Index: clang/test/Driver/linux-ld.c
===================================================================
--- clang/test/Driver/linux-ld.c
+++ clang/test/Driver/linux-ld.c
@@ -496,6 +496,26 @@
// 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 -no-canonical-prefixes -x c++ %s -### -o %t.o 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: "--push-state"
+// CHECK-BASIC-LIBCXX-SHARED: "--as-needed"
+// CHECK-BASIC-LIBCXX-SHARED: "-lc++"
+// CHECK-BASIC-LIBCXX-SHARED: "--pop-state"
+// CHECK-BASIC-LIBCXX-SHARED: "-lm"
+// RUN: %clangxx -no-canonical-prefixes -x c++ %s -### -o %t.o 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: "--as-needed"
+// CHECK-BASIC-LIBCXX-STATIC: "-Bstatic"
+// CHECK-BASIC-LIBCXX-STATIC: "-lc++"
+// CHECK-BASIC-LIBCXX-STATIC: "--pop-state"
+// CHECK-BASIC-LIBCXX-STATIC: "-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 \
@@ -542,7 +562,7 @@
// CHECK-BASIC-LIBCXX-C-LINK: "-internal-isystem" "[[SYSROOT]]/usr/local/include"
// CHECK-BASIC-LIBCXX-C-LINK: "--sysroot=[[SYSROOT]]"
// CHECK-BASIC-LIBCXX-C-LINK: "-L[[SYSROOT]]/usr/bin/../lib"
-//
+
// Check multi arch support on Ubuntu 12.04 LTS.
// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
// RUN: --target=arm-unknown-linux-gnueabihf -rtlib=platform \
Index: clang/test/Driver/fuchsia.cpp
===================================================================
--- clang/test/Driver/fuchsia.cpp
+++ 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"
@@ -58,9 +58,8 @@
// 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 \
Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -577,11 +577,12 @@
if (ToolChain.ShouldLinkCXXStdlib(Args)) {
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");
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
- if (OnlyLibstdcxxStatic)
- CmdArgs.push_back("-Bdynamic");
+ CmdArgs.push_back("--pop-state");
}
CmdArgs.push_back("-lm");
}
Index: clang/lib/Driver/ToolChains/Fuchsia.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Fuchsia.cpp
+++ clang/lib/Driver/ToolChains/Fuchsia.cpp
@@ -142,10 +142,8 @@
if (OnlyLibstdcxxStatic)
CmdArgs.push_back("-Bstatic");
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
- if (OnlyLibstdcxxStatic)
- CmdArgs.push_back("-Bdynamic");
- CmdArgs.push_back("-lm");
CmdArgs.push_back("--pop-state");
+ CmdArgs.push_back("-lm");
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110128.373786.patch
Type: text/x-patch
Size: 4229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210921/f5df18da/attachment.bin>
More information about the cfe-commits
mailing list