1
1
// no-system-llvm
2
2
// only-x86_64
3
+ // only-linux
3
4
// run-pass
4
5
5
- #![ feature( asm, track_caller) ]
6
+ #![ feature( asm, track_caller, thread_local ) ]
6
7
7
8
extern "C" fn f1 ( ) -> i32 {
8
9
111
@@ -15,9 +16,9 @@ fn f2() -> i32 {
15
16
}
16
17
17
18
macro_rules! call {
18
- ( $func: path) => { {
19
- let result: i32 ;
19
+ ( $func: path) => {
20
20
unsafe {
21
+ let result: i32 ;
21
22
asm!( "call {}" , sym $func,
22
23
out( "rax" ) result,
23
24
out( "rcx" ) _, out( "rdx" ) _, out( "rdi" ) _, out( "rsi" ) _,
@@ -27,12 +28,53 @@ macro_rules! call {
27
28
out( "xmm8" ) _, out( "xmm9" ) _, out( "xmm10" ) _, out( "xmm11" ) _,
28
29
out( "xmm12" ) _, out( "xmm13" ) _, out( "xmm14" ) _, out( "xmm15" ) _,
29
30
) ;
31
+ result
30
32
}
31
- result
32
- } }
33
+ }
33
34
}
34
35
36
+ macro_rules! static_addr {
37
+ ( $s: expr) => {
38
+ unsafe {
39
+ let result: * const u32 ;
40
+ // LEA performs a RIP-relative address calculation and returns the address
41
+ asm!( "lea {}, [rip + {}]" , out( reg) result, sym $s) ;
42
+ result
43
+ }
44
+ }
45
+ }
46
+ macro_rules! static_tls_addr {
47
+ ( $s: expr) => {
48
+ unsafe {
49
+ let result: * const u32 ;
50
+ asm!(
51
+ "
52
+ # Load TLS base address
53
+ mov {out}, qword ptr fs:[0]
54
+ # Calculate the address of sym in the TLS block. The @tpoff
55
+ # relocation gives the offset of the symbol from the start
56
+ # of the TLS block.
57
+ lea {out}, [{out} + {sym}@tpoff]
58
+ " ,
59
+ out = out( reg) result,
60
+ sym = sym $s
61
+ ) ;
62
+ result
63
+ }
64
+ }
65
+ }
66
+
67
+ static S1 : u32 = 111 ;
68
+ #[ thread_local]
69
+ static S2 : u32 = 222 ;
70
+
35
71
fn main ( ) {
36
72
assert_eq ! ( call!( f1) , 111 ) ;
37
73
assert_eq ! ( call!( f2) , 222 ) ;
74
+ assert_eq ! ( static_addr!( S1 ) , & S1 as * const u32 ) ;
75
+ assert_eq ! ( static_tls_addr!( S2 ) , & S2 as * const u32 ) ;
76
+ std:: thread:: spawn ( || {
77
+ assert_eq ! ( static_addr!( S1 ) , & S1 as * const u32 ) ;
78
+ assert_eq ! ( static_tls_addr!( S2 ) , & S2 as * const u32 ) ;
79
+ } ) ;
38
80
}
0 commit comments