2020-05-23 20:04:49 -05:00
|
|
|
// only-x86_64
|
2020-06-05 09:52:38 -05:00
|
|
|
// only-linux
|
2022-03-19 10:50:24 -05:00
|
|
|
// needs-asm-support
|
2020-05-23 20:04:49 -05:00
|
|
|
// run-pass
|
|
|
|
|
2022-10-17 16:38:37 -05:00
|
|
|
#![feature(thread_local)]
|
2021-12-09 18:15:33 -06:00
|
|
|
|
|
|
|
use std::arch::asm;
|
2020-05-23 20:04:49 -05:00
|
|
|
|
|
|
|
extern "C" fn f1() -> i32 {
|
|
|
|
111
|
|
|
|
}
|
|
|
|
|
|
|
|
// The compiler will generate a shim to hide the caller location parameter.
|
|
|
|
#[track_caller]
|
|
|
|
fn f2() -> i32 {
|
|
|
|
222
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! call {
|
2020-06-05 09:52:38 -05:00
|
|
|
($func:path) => {
|
2020-05-23 20:04:49 -05:00
|
|
|
unsafe {
|
2020-06-05 09:52:38 -05:00
|
|
|
let result: i32;
|
2020-05-23 20:04:49 -05:00
|
|
|
asm!("call {}", sym $func,
|
|
|
|
out("rax") result,
|
|
|
|
out("rcx") _, out("rdx") _, out("rdi") _, out("rsi") _,
|
|
|
|
out("r8") _, out("r9") _, out("r10") _, out("r11") _,
|
|
|
|
out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
|
|
|
|
out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
|
|
|
|
out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
|
|
|
|
out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _,
|
|
|
|
);
|
2020-06-05 09:52:38 -05:00
|
|
|
result
|
2020-05-23 20:04:49 -05:00
|
|
|
}
|
2020-06-05 09:52:38 -05:00
|
|
|
}
|
2020-05-23 20:04:49 -05:00
|
|
|
}
|
|
|
|
|
2020-06-05 09:52:38 -05:00
|
|
|
macro_rules! static_addr {
|
|
|
|
($s:expr) => {
|
|
|
|
unsafe {
|
|
|
|
let result: *const u32;
|
|
|
|
// LEA performs a RIP-relative address calculation and returns the address
|
|
|
|
asm!("lea {}, [rip + {}]", out(reg) result, sym $s);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
macro_rules! static_tls_addr {
|
|
|
|
($s:expr) => {
|
|
|
|
unsafe {
|
|
|
|
let result: *const u32;
|
|
|
|
asm!(
|
|
|
|
"
|
|
|
|
# Load TLS base address
|
|
|
|
mov {out}, qword ptr fs:[0]
|
|
|
|
# Calculate the address of sym in the TLS block. The @tpoff
|
|
|
|
# relocation gives the offset of the symbol from the start
|
|
|
|
# of the TLS block.
|
|
|
|
lea {out}, [{out} + {sym}@tpoff]
|
|
|
|
",
|
|
|
|
out = out(reg) result,
|
|
|
|
sym = sym $s
|
|
|
|
);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static S1: u32 = 111;
|
|
|
|
#[thread_local]
|
|
|
|
static S2: u32 = 222;
|
|
|
|
|
2020-05-23 20:04:49 -05:00
|
|
|
fn main() {
|
|
|
|
assert_eq!(call!(f1), 111);
|
|
|
|
assert_eq!(call!(f2), 222);
|
2020-06-05 09:52:38 -05:00
|
|
|
assert_eq!(static_addr!(S1), &S1 as *const u32);
|
|
|
|
assert_eq!(static_tls_addr!(S2), &S2 as *const u32);
|
|
|
|
std::thread::spawn(|| {
|
|
|
|
assert_eq!(static_addr!(S1), &S1 as *const u32);
|
|
|
|
assert_eq!(static_tls_addr!(S2), &S2 as *const u32);
|
2021-10-27 12:37:18 -05:00
|
|
|
})
|
|
|
|
.join()
|
|
|
|
.unwrap();
|
2020-05-23 20:04:49 -05:00
|
|
|
}
|