2021-06-24 14:01:49 +00:00
|
|
|
// only-aarch64
|
2020-06-05 14:52:38 +00:00
|
|
|
// only-linux
|
2022-03-19 15:50:24 +00:00
|
|
|
// needs-asm-support
|
2020-05-24 01:04:49 +00:00
|
|
|
// run-pass
|
|
|
|
|
2022-10-17 21:38:37 +00:00
|
|
|
#![feature(thread_local)]
|
2021-12-10 00:15:33 +00:00
|
|
|
|
|
|
|
use std::arch::asm;
|
2020-05-24 01:04:49 +00: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 14:52:38 +00:00
|
|
|
($func:path) => {
|
2020-05-24 01:04:49 +00:00
|
|
|
unsafe {
|
2020-06-05 14:52:38 +00:00
|
|
|
let result: i32;
|
2021-06-24 14:01:49 +00:00
|
|
|
asm!("bl {}", sym $func,
|
|
|
|
out("w0") result,
|
|
|
|
out("x20") _, out("x21") _, out("x22") _,
|
|
|
|
out("x23") _, out("x24") _, out("x25") _,
|
|
|
|
out("x26") _, out("x27") _, out("x28") _,
|
2020-05-24 01:04:49 +00:00
|
|
|
);
|
2020-06-05 14:52:38 +00:00
|
|
|
result
|
2020-05-24 01:04:49 +00:00
|
|
|
}
|
2020-06-05 14:52:38 +00:00
|
|
|
}
|
2020-05-24 01:04:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 14:52:38 +00:00
|
|
|
macro_rules! static_addr {
|
|
|
|
($s:expr) => {
|
|
|
|
unsafe {
|
|
|
|
let result: *const u32;
|
2021-06-24 14:01:49 +00:00
|
|
|
asm!(
|
|
|
|
// ADRP gives the address of a 4KB page from a PC-relative address
|
|
|
|
"adrp {out}, {sym}",
|
|
|
|
// We then add the remaining lower 12 bits
|
|
|
|
"add {out}, {out}, #:lo12:{sym}",
|
|
|
|
out = out(reg) result,
|
|
|
|
sym = sym $s);
|
2020-06-05 14:52:38 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
macro_rules! static_tls_addr {
|
|
|
|
($s:expr) => {
|
|
|
|
unsafe {
|
|
|
|
let result: *const u32;
|
|
|
|
asm!(
|
2021-06-24 14:01:49 +00:00
|
|
|
// Load the thread pointer register
|
|
|
|
"mrs {out}, TPIDR_EL0",
|
|
|
|
// Add the top 12 bits of the symbol's offset
|
|
|
|
"add {out}, {out}, :tprel_hi12:{sym}",
|
|
|
|
// And the bottom 12 bits
|
2021-10-13 09:44:02 +00:00
|
|
|
"add {out}, {out}, :tprel_lo12_nc:{sym}",
|
2020-06-05 14:52:38 +00:00
|
|
|
out = out(reg) result,
|
|
|
|
sym = sym $s
|
|
|
|
);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static S1: u32 = 111;
|
|
|
|
#[thread_local]
|
|
|
|
static S2: u32 = 222;
|
|
|
|
|
2020-05-24 01:04:49 +00:00
|
|
|
fn main() {
|
|
|
|
assert_eq!(call!(f1), 111);
|
|
|
|
assert_eq!(call!(f2), 222);
|
2020-06-05 14:52:38 +00: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 17:37:18 +00:00
|
|
|
})
|
|
|
|
.join()
|
|
|
|
.unwrap();
|
2020-05-24 01:04:49 +00:00
|
|
|
}
|