1
1
const std = @import ("std" );
2
2
const builtin = @import ("builtin" );
3
+ const windows = std .os .windows ;
3
4
4
5
export var _tls_index : u32 = std .os .windows .TLS_OUT_OF_INDEXES ;
5
- export var _tls_start : u8 linksection (".tls" ) = 0 ;
6
- export var _tls_end : u8 linksection (".tls$ZZZ" ) = 0 ;
7
- export var __xl_a : std . os . windows .PIMAGE_TLS_CALLBACK linksection (".CRT$XLA" ) = null ;
8
- export var __xl_z : std . os . windows .PIMAGE_TLS_CALLBACK linksection (".CRT$XLZ" ) = null ;
6
+ export var _tls_start : ? * anyopaque linksection (".tls" ) = null ;
7
+ export var _tls_end : ? * anyopaque linksection (".tls$ZZZ" ) = null ;
8
+ export var __xl_a : windows .PIMAGE_TLS_CALLBACK linksection (".CRT$XLA" ) = null ;
9
+ export var __xl_z : windows .PIMAGE_TLS_CALLBACK linksection (".CRT$XLZ" ) = null ;
9
10
10
11
comptime {
11
- if (builtin .target . cpu .arch == .x86 and builtin .zig_backend != .stage2_c ) {
12
+ if (builtin .cpu .arch == .x86 and builtin . abi == .msvc and builtin .zig_backend != .stage2_c ) {
12
13
// The __tls_array is the offset of the ThreadLocalStoragePointer field
13
14
// in the TEB block whose base address held in the %fs segment.
14
15
asm (
@@ -19,8 +20,6 @@ comptime {
19
20
}
20
21
21
22
// TODO this is how I would like it to be expressed
22
- // TODO also note, ReactOS has a +1 on StartAddressOfRawData and AddressOfCallBacks. Investigate
23
- // why they do that.
24
23
//export const _tls_used linksection(".rdata$T") = std.os.windows.IMAGE_TLS_DIRECTORY {
25
24
// .StartAddressOfRawData = @intFromPtr(&_tls_start),
26
25
// .EndAddressOfRawData = @intFromPtr(&_tls_end),
@@ -31,18 +30,21 @@ comptime {
31
30
//};
32
31
// This is the workaround because we can't do @intFromPtr at comptime like that.
33
32
pub const IMAGE_TLS_DIRECTORY = extern struct {
34
- StartAddressOfRawData : * anyopaque ,
35
- EndAddressOfRawData : * anyopaque ,
36
- AddressOfIndex : * anyopaque ,
37
- AddressOfCallBacks : * anyopaque ,
33
+ StartAddressOfRawData : * ? * anyopaque ,
34
+ EndAddressOfRawData : * ? * anyopaque ,
35
+ AddressOfIndex : * u32 ,
36
+ AddressOfCallBacks : [ * : null ] windows.PIMAGE_TLS_CALLBACK ,
38
37
SizeOfZeroFill : u32 ,
39
38
Characteristics : u32 ,
40
39
};
41
40
export const _tls_used linksection (".rdata$T" ) = IMAGE_TLS_DIRECTORY {
42
41
.StartAddressOfRawData = & _tls_start ,
43
42
.EndAddressOfRawData = & _tls_end ,
44
43
.AddressOfIndex = & _tls_index ,
45
- .AddressOfCallBacks = @as (* anyopaque , @ptrCast (& __xl_a )),
44
+ // __xl_a is just a global variable containing a null pointer; the actual callbacks sit in
45
+ // between __xl_a and __xl_z. So we need to skip over __xl_a here. If there are no callbacks,
46
+ // this just means we point to __xl_z (the null terminator).
47
+ .AddressOfCallBacks = @as ([* :null ]windows .PIMAGE_TLS_CALLBACK , @ptrCast (& __xl_a )) + 1 ,
46
48
.SizeOfZeroFill = 0 ,
47
49
.Characteristics = 0 ,
48
50
};
0 commit comments