Patches to enable emulated-tls for older darwin systems in clang.
1) tell clang that TLS is supported on all systems
2) tell clang to pass -femulated-tls by default on systems older than 10.7
3) tell llvm to use cxa_thread_atexit instead of tlv_atexit on systems older than 10.7

requires linking against a std c++ lib that supports TLS and contains cxa_thread_atexit
(libgcc newer than 4.8, or libc++abi with cxa_thread_atexit added).

requires emutls.c objects available at link time to supply supporting routines for emulated-tls.
emutls is available in libgcc, and should be available in libc++abi for supporting emulated-tls

ideally : llvm should call cxa_thread_atexit rather than tlv_atexit based on 
          the -femulated-tls flag rather than based on the system version

these patches were originally formatted against clang-5.0
they can be applied to all clang builds, and will only affect systems 10.4 to 10.6 at present


--- a/tools/clang/lib/Basic/Targets.cpp.orig
+++ b/tools/clang/lib/Basic/Targets.cpp
@@ -264,7 +264,7 @@
     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 from 9 onwards.
       if (Triple.getArch() == llvm::Triple::x86_64 ||
--- a/tools/clang/lib/Driver/ToolChains/Clang.cpp.orig
+++ b/tools/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3209,9 +3209,9 @@
   Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
   Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names);
   // Emulated TLS is enabled by default on Android and OpenBSD, and can be enabled
-  // manually with -femulated-tls.
+  // manually with -femulated-tls. Also default on Darwin < 10.7
   bool EmulatedTLSDefault = Triple.isAndroid() || Triple.isOSOpenBSD() ||
-                            Triple.isWindowsCygwinEnvironment();
+     Triple.isMacOSXVersionLT(10, 7) || Triple.isWindowsCygwinEnvironment();
   if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls,
                    EmulatedTLSDefault))
     CmdArgs.push_back("-femulated-tls");
--- a/tools/clang/lib/CodeGen/ItaniumCXXABI.cpp.orig
+++ b/tools/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -2163,7 +2163,8 @@
   const char *Name = "__cxa_atexit";
   if (TLS) {
     const llvm::Triple &T = CGF.getTarget().getTriple();
-    Name = T.isOSDarwin() ?  "_tlv_atexit" : "__cxa_thread_atexit";
+    // Darwin 10.7+ has _tlv_atexit
+    Name = (T.isOSDarwin() && !T.isMacOSXVersionLT(10, 7)) ?  "_tlv_atexit" : "__cxa_thread_atexit";
   }
 
   // We're assuming that the destructor function is something we can
