From 63501ee748110f8bd9c54859992406b7c1089f83 Mon Sep 17 00:00:00 2001
Date: Sun, 16 May 2021 12:30:32 -0700
Subject: [PATCH 19/24] 10.6 and less: use emulated TLS before 10.7

enable TLS on 10.4 and up

ItaniumCXXABI.cpp: call correct c++ TLS exit function

on 10.7+, we use the system TLS and _tlv_atexit
on < 10.7, we use emulated TLS and __cxa_thread_atexit
---
 clang/lib/Basic/Targets/OSTargets.h | 2 +-
 clang/lib/CodeGen/ItaniumCXXABI.cpp | 2 +-
 llvm/include/llvm/ADT/Triple.h      | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index 67fa1a537fea..2f2ce1ccafde 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -92,7 +92,7 @@ public:
     this->TLSSupported = false;
 
     if (Triple.isMacOSX())
-      this->TLSSupported = !Triple.isMacOSXVersionLT(10, 7);
+      this->TLSSupported = !Triple.isMacOSXVersionLT(10, 4);
     else if (Triple.isiOS()) {
       // 64-bit iOS supported it from 8 onwards, 32-bit device from 9 onwards,
       // 32-bit simulator from 10 onwards.
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 50fb30a95cbb..1df40230e8f1 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -2487,7 +2487,7 @@ static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
   const char *Name = "__cxa_atexit";
   if (TLS) {
     const llvm::Triple &T = CGF.getTarget().getTriple();
-    Name = T.isOSDarwin() ?  "_tlv_atexit" : "__cxa_thread_atexit";
+    Name = (T.isOSDarwin() && !T.isMacOSXVersionLT(10, 7)) ?  "_tlv_atexit" : "__cxa_thread_atexit";
   }
 
   // We're assuming that the destructor function is something we can
diff --git a/llvm/include/llvm/ADT/Triple.h b/llvm/include/llvm/ADT/Triple.h
index eed315c929ad..649b35f94307 100644
--- a/llvm/include/llvm/TargetParser/Triple.h	2022-12-20 15:29:16
+++ b/llvm/include/llvm/TargetParser/Triple.h	2022-12-30 13:17:19
@@ -1009,7 +1009,7 @@
   /// Note: Android API level 29 (10) introduced ELF TLS.
   bool hasDefaultEmulatedTLS() const {
     return (isAndroid() && isAndroidVersionLT(29)) || isOSOpenBSD() ||
-           isWindowsCygwinEnvironment() || isOHOSFamily();
+           isWindowsCygwinEnvironment() || isOHOSFamily() || isMacOSXVersionLT(10, 7);
   }
 
   /// Tests whether the target uses -data-sections as default.

