Skip to content

Commit 0c7669b

Browse files
ilovepitstellar
authored andcommitted
[clang][driver] Pass -femulated-tls through to the linker in LTO mode
Currently the driver does not propagate the `-f[no-]emulated-tls` flags to the linker under LTO. This can be surprising when the platform defaults differ from the flags being passed. A related discussion can be found in https://reviews.llvm.org/D143619. While the focus there was RISC-V support, the root cause was that setting `-femualted-tls` and `-flto` when compiling with Clang resulted in missing symbols because the platform defaults for Android differed from the flags being passed to Clang. This patch changes the Clang driver's behavior to pass the emulated-tls flags through to the linker when compiling with LTO/ThinLTO. Reviewed By: phosek, vit9696 Differential Revision: https://reviews.llvm.org/D147834 (cherry picked from commit a78816a)
1 parent 753307d commit 0c7669b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,13 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
757757
D.Diag(clang::diag::warn_drv_fjmc_for_elf_only);
758758
}
759759

760+
if (Arg *A = Args.getLastArg(options::OPT_femulated_tls,
761+
options::OPT_fno_emulated_tls)) {
762+
bool Enable = A->getOption().getID() == options::OPT_femulated_tls;
763+
CmdArgs.push_back(Args.MakeArgString(
764+
Twine(PluginOptPrefix) + "-emulated-tls=" + (Enable ? "1" : "0")));
765+
}
766+
760767
if (Args.hasFlag(options::OPT_fstack_size_section,
761768
options::OPT_fno_stack_size_section, false))
762769
CmdArgs.push_back(

clang/test/Driver/emulated-tls.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
// RUN: %clang -### -target i686-pc-openbsd %s -femulated-tls -fno-emulated-tls 2>&1 \
3030
// RUN: | FileCheck -check-prefix=NOEMU %s
3131

32+
// Test that when lto is used any -emualted-tls flags are passed to the linker
33+
// LINUX and Android have different defaults for EmulatedTLS
34+
// RUN: %clang -### -flto --target=riscv64-linux -fno-emulated-tls %s 2>&1 \
35+
// RUN: | FileCheck %s --check-prefix=LTO_NOEMUTLS
36+
// RUN: %clang -### -flto --target=riscv64-linux-android10000 -femulated-tls %s 2>&1 \
37+
// RUN: | FileCheck %s --check-prefix=LTO_EMUTLS
38+
// RUN: %clang -### -flto --target=riscv64-linux -femulated-tls %s 2>&1 \
39+
// RUN: | FileCheck %s --check-prefix=LTO_EMUTLS
40+
// RUN: %clang -### -flto --target=riscv64-linux-android10000 -fno-emulated-tls %s 2>&1 \
41+
// RUN: | FileCheck %s --check-prefix=LTO_NOEMUTLS
3242

3343
// Default without -f[no-]emulated-tls, will be decided by the target triple.
3444
// DEFAULT-NOT: "-cc1" {{.*}}"-femulated-tls"
@@ -40,3 +50,10 @@
4050

4151
// NOEMU: "-cc1" {{.*}}"-fno-emulated-tls"
4252
// NOEMU-NOT: "-cc1" {{.*}}"-femulated-tls"
53+
54+
// LTO related checks
55+
// LTO_NOEMUTLS: plugin-opt=-emulated-tls=0
56+
// LTO_NOEMUTLS-NOT: plugin-opt=-emulated-tls=1
57+
58+
// LTO_EMUTLS: plugin-opt=-emulated-tls=1
59+
// LTO_EMUTLS-NOT: plugin-opt=-emulated-tls=0

0 commit comments

Comments
 (0)