Skip to content

Commit f1b5414

Browse files
committed
emulated TLS before 10.7
1 parent 4ed6a1c commit f1b5414

File tree

9 files changed

+53
-3
lines changed

9 files changed

+53
-3
lines changed

src/libstd/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ cc = "1.0"
5858

5959
[features]
6060
default = ["std_detect_file_io", "std_detect_dlsym_getauxval"]
61+
macos_before_10_7 = []
6162

6263
backtrace = [
6364
"backtrace_rs/dbghelp", # backtrace/symbolize on MSVC

src/libstd/build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use std::env;
22

3+
#[path = "../tools/legacy-bootstrap/helper.rs"]
4+
mod legacy_bootstrap_helper;
5+
36
fn main() {
7+
legacy_bootstrap_helper::insert_cargo_features();
8+
49
let target = env::var("TARGET").expect("TARGET was not set");
510
if target.contains("linux") {
611
if target.contains("android") {

src/libstd/sys/unix/fast_thread_local.rs

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
5050
#[thread_local]
5151
static REGISTERED: Cell<bool> = Cell::new(false);
5252
if !REGISTERED.get() {
53+
#[cfg(feature = "macos_before_10_7")]
54+
__cxa_thread_atexit(run_dtors, ptr::null_mut());
55+
#[cfg(not(feature = "macos_before_10_7"))]
5356
_tlv_atexit(run_dtors, ptr::null_mut());
5457
REGISTERED.set(true);
5558
}
@@ -64,6 +67,10 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) {
6467
}
6568

6669
extern {
70+
#[cfg(feature = "macos_before_10_7")]
71+
fn __cxa_thread_atexit(dtor: unsafe extern fn(*mut u8),
72+
arg: *mut u8);
73+
#[cfg(not(feature = "macos_before_10_7"))]
6774
fn _tlv_atexit(dtor: unsafe extern fn(*mut u8),
6875
arg: *mut u8);
6976
}

src/llvm-project/clang/lib/Basic/Targets/OSTargets.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
9292
this->TLSSupported = false;
9393

9494
if (Triple.isMacOSX())
95-
this->TLSSupported = !Triple.isMacOSXVersionLT(10, 7);
95+
this->TLSSupported = !Triple.isMacOSXVersionLT(10, 4);
9696
else if (Triple.isiOS()) {
9797
// 64-bit iOS supported it from 8 onwards, 32-bit device from 9 onwards,
9898
// 32-bit simulator from 10 onwards.

src/llvm-project/clang/lib/CodeGen/ItaniumCXXABI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,7 @@ static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
22872287
const char *Name = "__cxa_atexit";
22882288
if (TLS) {
22892289
const llvm::Triple &T = CGF.getTarget().getTriple();
2290-
Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
2290+
Name = (T.isOSDarwin() && !T.isMacOSXVersionLT(10, 7)) ? "_tlv_atexit" : "__cxa_thread_atexit";
22912291
}
22922292

22932293
// We're assuming that the destructor function is something we can

src/llvm-project/compiler-rt/lib/builtins/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ if(APPLE)
206206
atomic_flag_test_and_set_explicit.c
207207
atomic_signal_fence.c
208208
atomic_thread_fence.c
209+
emutls.c
209210
)
210211
endif()
211212

src/llvm-project/llvm/include/llvm/ADT/Triple.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ class Triple {
737737

738738
/// Tests whether the target uses emulated TLS as default.
739739
bool hasDefaultEmulatedTLS() const {
740-
return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment();
740+
return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment() || isMacOSXVersionLT(10, 7);
741741
}
742742

743743
/// @}

src/tools/legacy-bootstrap/helper.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[cfg(target_os = "macos")]
2+
mod macos;
3+
4+
pub fn insert_cargo_features() {
5+
#[cfg(target_os = "macos")]
6+
macos_feature_version();
7+
}
8+
9+
#[cfg(target_os = "macos")]
10+
fn macos_feature_version() {
11+
let version = macos::version();
12+
if version < 11 {
13+
println!("cargo:rustc-cfg=feature=\"macos_before_10_7\"");
14+
}
15+
}

src/tools/legacy-bootstrap/macos.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::str;
2+
use std::process;
3+
4+
pub fn version() -> u32 {
5+
match process::Command::new("sw_vers").arg("-buildVersion").output() {
6+
Ok(out) =>
7+
match str::from_utf8(&out.stdout) {
8+
Ok(version_str) =>
9+
match version_str.get(0..2) {
10+
Some(str) =>
11+
str.parse::<u32>().unwrap(),
12+
_ =>
13+
panic!("Unexpected build version string {}", version_str),
14+
},
15+
Err(e) =>
16+
panic!("Can't parse output: {}", e),
17+
},
18+
Err(e) =>
19+
panic!("Can't run sw_vers: {}", e),
20+
}
21+
}

0 commit comments

Comments
 (0)