Skip to content

Commit 35cd6d8

Browse files
committed
emulated TLS before 10.7
1 parent b2cdf9e commit 35cd6d8

File tree

9 files changed

+53
-3
lines changed

9 files changed

+53
-3
lines changed

library/std/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ hermit-abi = { version = "0.1.17", features = ['rustc-dep-of-std'] }
4949
wasi = { version = "0.9.0", features = ['rustc-dep-of-std'], default-features = false }
5050

5151
[features]
52+
macos_before_10_7 = []
5253
backtrace = [
5354
"gimli-symbolize",
5455
'addr2line/rustc-dep-of-std',

library/std/build.rs

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

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

library/std/src/sys/unix/thread_local_dtor.rs

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
6161
#[thread_local]
6262
static REGISTERED: Cell<bool> = Cell::new(false);
6363
if !REGISTERED.get() {
64+
#[cfg(feature = "macos_before_10_7")]
65+
__cxa_thread_atexit(run_dtors, ptr::null_mut());
66+
#[cfg(not(feature = "macos_before_10_7"))]
6467
_tlv_atexit(run_dtors, ptr::null_mut());
6568
REGISTERED.set(true);
6669
}
@@ -75,6 +78,9 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
7578
}
7679

7780
extern "C" {
81+
#[cfg(feature = "macos_before_10_7")]
82+
fn __cxa_thread_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
83+
#[cfg(not(feature = "macos_before_10_7"))]
7884
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
7985
}
8086

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
@@ -2487,7 +2487,7 @@ static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
24872487
const char *Name = "__cxa_atexit";
24882488
if (TLS) {
24892489
const llvm::Triple &T = CGF.getTarget().getTriple();
2490-
Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
2490+
Name = (T.isOSDarwin() && !T.isMacOSXVersionLT(10, 7)) ? "_tlv_atexit" : "__cxa_thread_atexit";
24912491
}
24922492

24932493
// 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
@@ -234,6 +234,7 @@ if(APPLE)
234234
atomic_flag_test_and_set_explicit.c
235235
atomic_signal_fence.c
236236
atomic_thread_fence.c
237+
emutls.c
237238
)
238239
endif()
239240

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ class Triple {
809809

810810
/// Tests whether the target uses emulated TLS as default.
811811
bool hasDefaultEmulatedTLS() const {
812-
return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment();
812+
return isAndroid() || isOSOpenBSD() || isWindowsCygwinEnvironment() || isMacOSXVersionLT(10, 7);
813813
}
814814

815815
/// Tests whether the target uses -data-sections as default.

src/tools/legacy-bootstrap/helper.rs

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

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)